| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906 | <?phpnamespace App\Service;use App\Model\ApplyOrder;use App\Model\Dispatch;use App\Model\DispatchEmpSub;use App\Model\DispatchSub;use App\Model\Employee;use App\Model\EmployeeTeamPermission;use App\Model\Equipment;use App\Model\OrdersProduct;use App\Model\OrdersProductProcess;use App\Model\Process;use App\Model\Team;use Illuminate\Support\Facades\DB;class DispatchService extends Service{    public function edit($data){}    public function setOrderNO(){        $str = date('Ymd',time());        $order_number = Dispatch::where('dispatch_no','Like','%'. $str . '%')            ->max('dispatch_no');        if(empty($order_number)){            $number = str_pad(1,3,'0',STR_PAD_LEFT);            $number = $str . $number;        }else{            $tmp = substr($order_number, -3);            $tmp = $tmp + 1;            //超过99999            if(strlen($tmp) > 3) return '';            $number = str_pad($tmp,3,'0',STR_PAD_LEFT);            $number = $str . $number;        }        return $number;    }    public function add($data,$user){        //数据校验以及填充        list($status,$msg) = $this->orderRule($data);        if(! $status) return [$status,$msg];        try{            DB::beginTransaction();            $time = time();            $insert_data = [];            if($data['is_split']){                foreach ($msg as $value){                    list($s,$m) = $this->insertDispatch([$value],$data,$user,$time);                    if(! $s) return [false,$m];                    foreach ($m as $v_m){                        $insert_data[] = $v_m;                    }                }            }else{                list($s,$m) = $this->insertDispatch($msg,$data,$user,$time);                if(! $s) return [false, $m];                $insert_data = $m;            }            $insert = [];$tmp = [];            if(! empty($insert_data)){                foreach ($insert_data as $value){                    $str = $value['order_product_id'] . $value['crt_time'] . $value['product_no'] . $value['technology_name'];                    if(! in_array($str, $tmp)) {                        $insert[] = [                            'id' => $value['id'],                            'quantity' => $value['quantity'],                            'product_no' => $value['product_no'],                        ];                        $tmp[] = $str;                    }                }            }            //反写已派工数量            $this->writeDispatchQuantity(array_column($msg,'order_product_id'));            //是否自动审核 领料申请单            list($status, $msg) = $this->createSQ($insert, $user);            if(! $status) return [false, $msg];            DB::commit();        }catch (\Exception $e){            DB::rollBack();            return [false,$e->getFile() . ':' .$e->getLine().':'.$e->getMessage()];        }        return [true,''];    }    public function createSQ($insert_data, $user){        try {            DB::beginTransaction();            //是否自动审核领料申请单            $em = new EmployeeService();            $auto = $em->is_auto($user, "llsq_auto");            $return = [];            $product_no = array_unique(array_column($insert_data,'product_no'));            //获取原料            $service = new FyyOrderService();            list($status, $msg) = $service->getProductDataFromSqlServer(['product_no' => $product_no], $user);            if($status) $return = $msg;            //组织原材料写入数据            $insert = [];            foreach ($insert_data as $value){                $t = $return[$value['product_no']] ?? [];                $tmp = [                    'id' => $value['id'],                    'quantity' => $value['quantity'] ?? 0,                    'product_no' => "",                    'product_title' => "",                    'product_size' => "",                    'product_unit' => "",                ];                if(! empty($t)){                    foreach ($t as $v){                        $tmp['product_no'] = $v['product_no'];                        $tmp['product_title'] = $v['product_title'];                        $tmp['product_size'] = $v['product_size'];                        $tmp['product_unit'] = $v['product_unit'];                        $insert[] = $tmp;                    }                }else{                    $tmp['quantity'] = 0;                    $insert[] = $tmp;                }            }            //生成申请单            $service = new ApplyOrderService();            list($status, $msg) = $service->createSQ($insert, $user, ApplyOrder::type_one, $auto);            if(! $status) {                DB::rollBack();                return [false, $msg];            }            if($auto) {                //生成流水                list($status, $msg) = $service->createRecord($msg);                if(! $status) {                    DB::rollBack();                    return [false, $msg];                }            }            DB::commit();        }catch (\Throwable $exception){            DB::rollBack();            return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];        }        return [true, ''];    }    public function makeData($equipment_id, $team_id,$employee_id,$message){        $arr = [];        if(! empty($equipment_id)){            foreach ($equipment_id as $v_e){                if(! empty($team_id)){                    foreach ($team_id as $t){                        if(! empty($employee_id)){                            foreach ($employee_id as $e){                                $arr[] = [                                    'equipment_id' => $v_e,                                    'team_id' => $t,                                    'employee_id' => $e,                                    'order_product_id' => $message['order_product_id'],                                    'dispatch_no' => $message['dispatch_no'],                                ];                            }                        }else{                            $arr[] = [                                'equipment_id' => $v_e,                                'team_id' => $t,                                'order_product_id' => $message['order_product_id'],                                'dispatch_no' => $message['dispatch_no'],                            ];                        }                    }                }elseif(! empty($employee_id)){                    foreach ($employee_id as $e){                        $arr[] = [                            'equipment_id' => $v_e,                            'employee_id' => $e,                            'order_product_id' => $message['order_product_id'],                            'dispatch_no' => $message['dispatch_no'],                        ];                    }                }else{                    $arr[] = [                        'equipment_id' => $v_e,                        'order_product_id' => $message['order_product_id'],                        'dispatch_no' => $message['dispatch_no'],                    ];                }            }        }elseif (! empty($team_id)){            foreach ($team_id as $t){                if(! empty($employee_id)){                    foreach ($employee_id as $e){                        $arr[] = [                            'team_id' => $t,                            'employee_id' => $e,                            'order_product_id' => $message['order_product_id'],                            'dispatch_no' => $message['dispatch_no'],                        ];                    }                }else{                    $arr[] = [                        'team_id' => $t,                        'order_product_id' => $message['order_product_id'],                        'dispatch_no' => $message['dispatch_no'],                    ];                }            }        }elseif(! empty($employee_id)){            foreach ($employee_id as $e){                $arr[] = [                    'employee_id' => $e,                    'order_product_id' => $message['order_product_id'],                    'dispatch_no' => $message['dispatch_no'],                ];            }        }        return $arr;    }    public function insertDispatch($msg, $data, $user, $time){        //生产数据的源数据        $result = $msg;        $dispatch_no = $this->setOrderNO();        if(! $dispatch_no) return [false,'单号生成失败!'];        //主表        Dispatch::insert(['dispatch_no' => $dispatch_no,'crt_time' => $time]);        $time_tmp = date("Ymd", $data['out_order_no_time'][0]);        $process_model = new OrdersProductProcess(['channel' => $time_tmp]);        //是否自动审核        $em = new EmployeeService();        $auto = $em->is_auto($user, "pg_auto");        foreach ($result as $key => $value){            $result[$key]['dispatch_no'] = $dispatch_no;            $result[$key]['crt_time'] = $time;            $result[$key]['crt_id'] = $user['id'];            $result[$key]['status'] = $auto;            $dispatch_quantity = $value['dispatch_quantity'] * 1000;            $process_model->where('order_product_id',$value['order_product_id'])                ->where('process_id',$value['process_id'])                ->where('dispatch_no','')                ->take($dispatch_quantity)                ->update([                    'dispatch_no' => $dispatch_no,                    'status' => 1                ]);        }        DispatchSub::insert($result);        //获取上一次插入订单的所有id        $last_insert_id = DispatchSub::where('dispatch_no',$dispatch_no)            ->where('crt_time',$time)            ->where('crt_id',$user['id'])            ->select('id','dispatch_quantity as quantity',"product_no","order_product_id","product_no",'technology_name','crt_time')            ->orderBy('order_product_id')            ->get()->toArray();        return [true, $last_insert_id];    }    public function del($data){        if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];        return [true,'删除成功'];    }    public function orderDetail($data){        return [200,''];    }    public function is_same_month($timestamp1,$timestamp2){        // 格式化时间戳为年份和月份        $year1 = date('Y', $timestamp1);        $month1 = date('m', $timestamp1);        $year2 = date('Y', $timestamp2);        $month2 = date('m', $timestamp2);        if ($year1 === $year2 && $month1 === $month2) {            return true;        } else {            return false;        }    }    public function orderList($data){        list($status,$msg) = $this->orderListRule($data);        if(! $status) return [false, $msg];        $model = OrdersProduct::where('del_time',0)            ->select('id','order_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','order_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man','out_checker_man','out_checker_time','production_quantity','production_time','production_no','status','crt_id','dispatch_complete_quantity','pre_shipment_time','process_id')            ->whereBetween('out_order_no_time',[$data['out_order_no_time'][0],$data['out_order_no_time'][1]])            ->whereIn('id',$msg)            ->orderBy('id','desc');        if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');        if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');        if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');        if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');        if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%');        if(! empty($data['product_size'])) $model->where('product_size', 'LIKE', '%'.$data['product_size'].'%');        if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');        if(! empty($data['technology_name'])) $model->where('technology_name', 'LIKE', '%'.$data['technology_name'].'%');        if(! empty($data['wood_name'])) $model->where('wood_name', 'LIKE', '%'.$data['wood_name'].'%');        if(! empty($data['process_mark'])) $model->where('process_mark', 'LIKE', '%'.$data['process_mark'].'%');        if(! empty($data['table_header_mark'])) $model->where('table_header_mark', 'LIKE', '%'.$data['table_header_mark'].'%');        if(! empty($data['table_body_mark'])) $model->where('table_body_mark', 'LIKE', '%'.$data['table_body_mark'].'%');        if(! empty($data['out_checker_man'])) $model->where('out_checker_man', 'LIKE', '%'.$data['out_checker_man'].'%');        if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');        if(! empty($data['out_checker_time'][0]) && ! empty($data['out_checker_time'][1])) $model->whereBetween('out_checker_time',[$data['out_checker_time'][0],$data['out_checker_time'][1]]);        if(! empty($data['production_time'][0]) && ! empty($data['production_time'][1])) $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);        if(! empty($data['pre_shipment_time'][0]) && ! empty($data['pre_shipment_time'][1])) $model->whereBetween('pre_shipment_time',[$data['pre_shipment_time'][0],$data['pre_shipment_time'][1]]);        if(isset($data['status'])) $model->where('status',$data['status']);        if(isset($data['is_create'])) {            if($data['is_create']){                $model->whereColumn('dispatch_complete_quantity', '>=', 'production_quantity');            }else{                $model->whereColumn('production_quantity', '>', 'dispatch_complete_quantity');            }        }        $list = $this->limit($model,'',$data);        $list = $this->fillData($list);        return [true,$list];    }    public function orderListRule($data){//        if(empty($data['process_id'])) return [false, '工序必须选择!'];        if(empty($data['out_order_no_time'][0]) || empty($data['out_order_no_time'][1])) return [false,'制单日期必须选择'];        $bool = $this->is_same_month($data['out_order_no_time'][0],$data['out_order_no_time'][1]);        if(! $bool) return [false,'制单日期必须同月!'];        $time = date("Ymd",$data['out_order_no_time'][0]);        $process_model = new OrdersProductProcess(['channel' => $time]);        if(! $process_model->is_table_isset()) return [true, []];//不存在process子表 返回空数据        $process_array = $process_model->where('del_time',0)//            ->where('process_id',$data['process_id'])            ->distinct()            ->select('order_product_id')            ->get()->toArray();        $order_product_id = array_column($process_array,'order_product_id');        if(empty($order_product_id)) return [true,[]];//process子表无数据 返回空数据        return [true, $order_product_id];    }    public function orderRule($data){        if(empty($data['out_order_no_time'][0]) || empty($data['out_order_no_time'][1])) return [false,'制单时间不能为空!'];        if(! isset($data['is_split'])) return [false,'是否拆分标识不能为空!'];        if(empty($data['detail'])) return [false, '派工明细数据不能为空!'];        $id = [];        $detail_map = $detail_2 = [];        $device_map = Equipment::where('del_time',0)            ->pluck('title','id')            ->toArray();        foreach ($data['detail'] as $value){            if(empty($value['id'])) return [false,'请选择派工数据!'];            if(empty($value['process_id'])) return [false,'工序不能为空!'];            if(! is_numeric($value['quantity']) || $value['quantity'] < 0) return [false,'数量不能小于0!'];            if(empty($value['dispatch_time'][0]) || empty($value['dispatch_time'][1])) return [false,'计划生产时间不能为空!'];            if(! in_array($value['id'], $id)) $id[] = $value['id'];            $key = $value['id'] . $value['process_id'];            if(isset($detail_map[$key])){                $detail_map[$key] += $value['quantity'];            }else{                $detail_map[$key] = $value['quantity'];            }            if(isset($detail_2[$value['id']][$value['process_id']])){                $detail_2[$value['id']][$value['process_id']] += $value['quantity'];            }else{                $detail_2[$value['id']][$value['process_id']] = $value['quantity'];            }            if(! empty($value['device_id']) && ! isset($device_map[$value['device_id']])) return [false, '设备不存在或已被删除'];        }        $result = OrdersProduct::whereIn('id',$id)            ->select('id as order_product_id','sale_orders_product_id','order_no','table_header_mark','product_no','product_title','product_size','product_unit','production_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','sale_orders_product_id','out_order_no_time','price','customer_name','out_order_no','customer_no','pre_shipment_time','process_id','production_no')            ->orderBy('id','desc')            ->get()->toArray();        $result_map = array_column($result,null,'order_product_id');        //每个生产订单所有的工序        $process_map = $this->getProcess($result);        //已派工数据        $map = $this->getDispatchQuantity($id);        //校验        foreach ($result as $value){            //总数量校验            $detail2 = $detail_2[$value['order_product_id']] ?? [];            $uniqueValuesCount = count(array_unique($detail2));            if($uniqueValuesCount != 1) return [false , "生产订单号:" . $value['production_no'] . "所有工序派工数量必须相等"];            //本次提交的工序//            $submit_process = array_keys($detail2);            //工序            $tmp = $process_map[$value['order_product_id']] ?? [];            foreach ($tmp as $v) {//                if(! in_array($v['process_id'], $submit_process)) continue;                //键值 生成订单id + 工序id                $key = $value['order_product_id'] . $v['process_id'];                //本次提交数量                $quantity_tmp = $detail_map[$key] ?? 0;                //工序已派工                $q = $map[$key] ?? 0;                if($q + $quantity_tmp > $value['production_quantity']) return [false, "生产订单号:" . $value['production_no'] . "的工序:" . $v['process_title'] . "的派单数量不能超过生产订单数量"];            }        }        $return = [];        foreach ($data['detail'] as $value){            $tmp = $result_map[$value['id']] ?? [];            unset($tmp['process_id']);unset($tmp['production_no']);            $tmp['process_id'] = $value['process_id'];            $tmp['team_id'] = $value['team_id'];            $tmp['device_id'] = $value['device_id'];            $tmp['dispatch_time_start'] = $value['dispatch_time'][0];            $tmp['dispatch_time_end'] = $value['dispatch_time'][1];            $tmp['dispatch_quantity'] = $value['quantity'];            $return[] = $tmp;        }        return [true, $return];    }    public function fillData($data){        if(empty($data['data'])) return $data;        $map = $this->getDispatchQuantity(array_column($data['data'],'id'));        $emp_map = Employee::whereIn('id',array_column($data['data'],'crt_id'))            ->pluck('emp_name','id')            ->toArray();        $process_map = $this->getProcess($data['data']);        foreach ($data['data'] as $key => $value){            $tmp = $process_map[$value['id']] ?? [];            foreach ($tmp as $t => $v) {                $q = $map[$value['id'] . $v['process_id']] ?? 0;                $tmp[$t]['dispatch_quantity'] = $q;                $tmp[$t]['not_dispatch_quantity'] = $value['production_quantity'] - $q;            }            $data['data'][$key]['process'] = $tmp;            $last_process = end($tmp);            $dispatch_quantity = $last_process['dispatch_quantity'] ?? 0;            $data['data'][$key]['dispatch_quantity'] = $dispatch_quantity;            $data['data'][$key]['not_dispatch_quantity'] = $value['production_quantity'] - $dispatch_quantity;            $data['data'][$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';            $data['data'][$key]['out_checker_time'] = $value['out_checker_time'] ? date('Y-m-d',$value['out_checker_time']) : '';            $data['data'][$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';            $data['data'][$key]['pre_shipment_time'] = $value['pre_shipment_time'] ? date('Y-m-d',$value['pre_shipment_time']) : '';            $data['data'][$key]['order_product_man'] = $emp_map[$value['crt_id']] ?? '';            if($value['dispatch_complete_quantity'] >= $value['production_quantity']){                $data['data'][$key]['is_create'] = 1;            }else{                $data['data'][$key]['is_create'] = 0;            }            $data['data'][$key]['status_title'] = DispatchSub::$status_name[$value['status']] ?? "";        }        $data['production_quantity'] = $this->getTotal($data['data'], 'production_quantity');        $data['dispatch_quantity'] = $this->getTotal($data['data'], 'dispatch_quantity');        $data['not_dispatch_quantity'] = $this->getTotal($data['data'], 'not_dispatch_quantity');        return $data;    }    //返回已派工数量    public function getDispatchQuantity($order_product_id = []){        if(empty($order_product_id)) return [];        $result = DispatchSub::where('del_time',0)            ->whereIn("order_product_id",$order_product_id)            ->select('dispatch_quantity','order_product_id','process_id')            ->get()            ->toArray();        $return = [];        foreach ($result as $value){            $key = $value['order_product_id'] . $value['process_id'];            if(isset($return[$key])){                $return[$key] += $value['dispatch_quantity'];            }else{                $return[$key] = $value['dispatch_quantity'];            }        }        return $return;    }    public function getProcess($data){        if(empty($data)) return [];        $process_id_array = [];        $process_id = array_column($data,'process_id');        foreach ($process_id as $value){            $tmp = explode(',',$value);            foreach ($tmp as $v){                if(! in_array($v,$process_id_array)) $process_id_array[] = $v;            }        }        $process_array = Process::whereIn('id', $process_id_array)->get()->toArray();        $process_map = array_column($process_array,null,'id');        $return = [];        foreach ($data as $value){            $tmp = explode(',',$value['process_id']);            foreach ($tmp as $v){               $process_tmp = $process_map[$v] ?? [];               if(isset($value['id'])){                   $return[$value['id']][] = [                       'process_id' => $v,                       'process_title' => $process_tmp['title'] ?? "",                       'team_id' => $process_tmp['team_id'] ?? 0,                       'device_id' => $process_tmp['device_id'] ?? 0,                   ];               }else{                   $return[$value['order_product_id']][] = [                       'process_id' => $v,                       'process_title' => $process_tmp['title'] ?? "",                       'team_id' => $process_tmp['team_id'] ?? 0,                       'device_id' => $process_tmp['device_id'] ?? 0,                   ];               }            }        }        return $return;    }    public function dispatchOrderList($data){        $model = DispatchSub::where('del_time',0)            ->select('id','order_no','table_header_mark','product_no','product_title','product_size','product_unit','dispatch_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','production_quantity','dispatch_no','crt_id','process_id','dispatch_time_start','dispatch_time_end','crt_time','finished_num','waste_num','customer_name','order_product_id','out_order_no','team_id','device_id','wg_status','status')            ->orderBy('id','desc');        if(isset($data['status'])) $model->where('status', $data['status']);        if(isset($data['wg_status'])) $model->where('wg_status', $data['wg_status']);        if(isset($data['finished_num'])) $model->where('finished_num', '>',0);        if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');        if(! empty($data['dispatch_no'])) $model->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%');        if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');        if(! empty($data['production_no'])) {            $id = OrdersProduct::where('del_time', 0)                ->where('production_no', 'LIKE', '%'.$data['production_no'].'%')                ->select('id')                ->get()->toArray();            $model->whereIn('order_product_id', array_column($id,'id'));        }        if(! empty($data['process_id'])) $model->where('process_id',$data['process_id']);        if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');        if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);        if(! empty($data['dispatch_time'][0]) && ! empty($data['dispatch_time'][1])){            $model->where('dispatch_time_start','<=',$data['dispatch_time'][0]);            $model->where('dispatch_time_end','>=',$data['dispatch_time'][1]);        }        if(! empty($data['team_id'])) $model->where('team_id',$data['team_id']);        if(! empty($data['device_id'])) $model->whereIn('device_id',$data['device_id']);        if(isset($data['is_finished'])) {            if($data['is_finished']){                $model->wherecolumn('dispatch_quantity','=','finished_num');            }else{                $model->wherecolumn('dispatch_quantity','>','finished_num');            }        }        if(! empty($data['employee_id'])) {//todo            $team = EmployeeTeamPermission::where('employee_id',$data['employee_id'])                ->select('team_id')                ->get()->toArray();            $model->whereIn('team_id',array_unique(array_column($team,'team_id')));        }        $list = $this->limit($model,'',$data);        $list = $this->fillDispatchOrderListData($list);        return [true,$list];    }    public function fillDispatchOrderListData($data){        if(empty($data['data'])) return $data;        $team_map = Team::whereIn('id',array_unique(array_column($data['data'],'team_id')))            ->pluck('title','id')            ->toArray();        $equipment_map = Equipment::whereIn('id',array_unique(array_column($data['data'],'device_id')))            ->pluck('title','id')            ->toArray();        $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))            ->pluck('title','id')            ->toArray();        $orders = OrdersProduct::whereIn('id', array_column($data['data'],'order_product_id'))            ->pluck('production_no','id')            ->toArray();        foreach ($data['data'] as $key => $value){            $data['data'][$key]['wg_status_title'] = DispatchSub::$status_name[$value['wg_status']] ?? "";            $data['data'][$key]['status_title'] = DispatchSub::$status_name[$value['status']] ?? "";            $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';            $time1 = $value['dispatch_time_start'] ? date('Y-m-d',$value['dispatch_time_start']) : '';            $time2 = $value['dispatch_time_end'] ? date('Y-m-d',$value['dispatch_time_end']) : '';            $data['data'][$key]['dispatch_time'] = $time1 . ' ' . $time2;            $data['data'][$key]['process_name'] = $process_map[$value['process_id']] ?? '';            $data['data'][$key]['team_name'] = $team_map[$value['team_id']] ?? "";            $data['data'][$key]['equipment_name'] = $equipment_map[$value['device_id']] ?? "";            $data['data'][$key]['equipment_id'] = $value['device_id'];            $data['data'][$key]['un_finished_quantity'] = bcsub($value['dispatch_quantity'] , $value['finished_num'],3);            $data['data'][$key]['production_no'] = $orders[$value['order_product_id']] ?? '';        }        $total = $this->getTotal($data['data'], 'dispatch_quantity');        $data['dispatch_quantity'] = $total;        return $data;    }    //反写写派工数量(增加)    public function writeDispatchQuantity($order_product_id){        if(empty($order_product_id)) return;        //最后一道工序        $last_process = [];        $process_id = OrdersProduct::whereIn('id',$order_product_id)            ->select('id','process_id')            ->get()->toArray();        foreach ($process_id as $value){            $tmp_process = explode(',', $value['process_id']);            $last_process[$value['id']] = end($tmp_process);        }        $result = DispatchSub::where('del_time',0)            ->whereIn('order_product_id',$order_product_id)            ->select('dispatch_quantity','order_product_id','process_id')            ->get()            ->toArray();        if(empty($result)) return;        $new_result = [];        foreach ($result as $value){           //统计最后一道工序的派工数量           $tmp_last_process = $last_process[$value['order_product_id']];           if($tmp_last_process == $value['process_id']){               if(isset($new_result[$value['order_product_id']])){                   $new_result[$value['order_product_id']] += $value['dispatch_quantity'];               }else{                   $new_result[$value['order_product_id']] = $value['dispatch_quantity'];               }           }        }        foreach ($new_result as $order_product_id => $num){            OrdersProduct::where('id',$order_product_id)->update([                'dispatch_complete_quantity' => $num            ]);        }    }    //反写写派工数量(删除)    public function writeDispatchQuantityDEL($order_product_id){        if(empty($order_product_id)) return;        //最后一道工序        $last_process = [];        $process_id = OrdersProduct::whereIn('id',$order_product_id)            ->select('id','process_id')            ->get()->toArray();        foreach ($process_id as $value){            $tmp_process = explode(',', $value['process_id']);            $last_process[$value['id']] = end($tmp_process);        }        $result = DispatchSub::where('del_time',0)            ->whereIn('order_product_id',$order_product_id)            ->select('dispatch_quantity','order_product_id','process_id')            ->get()            ->toArray();        $new_result = [];        foreach ($result as $value){            //统计最后一道工序的派工数量            $tmp_last_process = $last_process[$value['order_product_id']];            if($tmp_last_process == $value['process_id']){                if(isset($new_result[$value['order_product_id']])){                    $new_result[$value['order_product_id']] += $value['dispatch_quantity'];                }else{                    $new_result[$value['order_product_id']] = $value['dispatch_quantity'];                }            }        }        foreach ($order_product_id as $value){            $quantity = $new_result[$value] ?? 0;            OrdersProduct::where('id',$value)->update([                'dispatch_complete_quantity' => $quantity            ]);        }    }    //设备上的去完工列表    public function dispatchMobileOrderList($data){        $model = DispatchSub::where('del_time',0)            ->select('id','product_title','product_no','dispatch_quantity','finished_num','dispatch_no','waste_num')            ->whereRaw('dispatch_quantity > finished_num')            ->orderBy('id','desc');        if(! empty($data['process_id'])) $model->where('process_id',$data['process_id']);        if(! empty($data['order_number'])) $model->where('dispatch_no',$data['dispatch_no']);        $list = $model->get()->toArray();        $list = $this->fillDispatchMobileOrderList($list);        return [true,$list];    }    public function fillDispatchMobileOrderList($data){        if(empty($data)) return $data;        foreach ($data as $key => $value){            $data[$key]['un_finished_quantity'] = $value['dispatch_quantity'] - $value['finished_num'] - $value['waste_num'];        }        $return['product_num'] = count($data);        $return['finished_num'] = $this->getTotal($data,'finished_num');        $return['un_finished_quantity'] = $this->getTotal($data,'un_finished_quantity');        $return['data'] = $data;        unset($data);        return $return;    }    //设备上完工填写数据的页面    public function dispatchMobileOrderDetailsList($data){        if(empty($data['id'])) return [false,'派工单ID不能为空!'];        $dispatch = DispatchSub::whereIn('id',$data['id'])            ->where('del_time',0)            ->select('id','product_title','product_no','dispatch_no',DB::raw('(dispatch_quantity - finished_num) as quantity'))            ->get()->toArray();        $sub = DispatchEmpSub::where('del_time',0)            ->whereIn('dispatch_no',array_column($dispatch,'dispatch_no'))            ->select('dispatch_no','equipment_id','team_id','employee_id')            ->get()->toArray();        $sub_map = [];        foreach ($sub as $s){            $array = [                'equipment_id' => $s['equipment_id'],                'team_id' => $s['team_id'],                'employee_id' => $s['employee_id'],            ];            if(empty($sub_map[$s['dispatch_no']])){                $sub_map[$s['dispatch_no']][] = $array;            }else{                if(! in_array($array,$sub_map[$s['dispatch_no']])) {                    $sub_map[$s['dispatch_no']][] = $array;                }            }        }        $return_list = $return_details = [];        foreach ($dispatch as $value){            $dispatch_tmp = $sub_map[$value['dispatch_no']];            $counts = count($dispatch_tmp) ?? 1;            // 计算每个人应该得到的数量            $per_person = floor($value['quantity'] / $counts);            // 计算最后一个人拿到的数量            $last_person = $value['quantity'] - ($per_person * ($counts - 1));            foreach ($dispatch_tmp as $k => $t){                $dispatch_tmp[$k]['id'] = $value['id'];                $dispatch_tmp[$k]['product_title'] = $value['product_title'];                $dispatch_tmp[$k]['product_no'] = $value['product_no'];                $dispatch_tmp[$k]['dispatch_no'] = $value['dispatch_no'];                if ($k == $counts - 1) {                    $dispatch_tmp[$k]['quantity'] = (int)$last_person;                }else{                    $dispatch_tmp[$k]['quantity'] = (int)$per_person;                }            }            //列数据            $return_list = array_merge_recursive($return_list,$dispatch_tmp);            //总数量            $return_details[$value['id']] = $value['quantity'];        }        $return['list'] = $return_list;        $return['list_details'] = $return_details;        return [true, $return];    }    public function dispatchOrderForSqList($data,$user){        $model = DispatchSub::where('del_time',0)            ->select('id','order_no','table_header_mark','product_no','product_title','product_size','product_unit','dispatch_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','production_quantity','crt_id','process_id','dispatch_time_start','dispatch_time_end','crt_time','finished_num','waste_num','customer_name','order_product_id','out_order_no','team_id','device_id','wg_status','status',DB::raw('GROUP_CONCAT(DISTINCT dispatch_no ORDER BY dispatch_no SEPARATOR ",") as dispatch_no'))            ->orderBy('id','desc')            ->groupBy('order_product_id','crt_time');        if(isset($data['status'])) $model->where('status', $data['status']);        if(isset($data['wg_status'])) $model->where('wg_status', $data['wg_status']);        if(isset($data['finished_num'])) $model->where('finished_num', '>',0);        if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');        if(! empty($data['dispatch_no'])) $model->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%');        if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');        if(! empty($data['production_no'])) {            $id = OrdersProduct::where('del_time', 0)                ->where('production_no', 'LIKE', '%'.$data['production_no'].'%')                ->select('id')                ->get()->toArray();            $model->whereIn('order_product_id', array_column($id,'id'));        }        if(! empty($data['process_id'])) $model->where('process_id',$data['process_id']);        if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');        if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);        if(! empty($data['dispatch_time'][0]) && ! empty($data['dispatch_time'][1])){            $model->where('dispatch_time_start','<=',$data['dispatch_time'][0]);            $model->where('dispatch_time_end','>=',$data['dispatch_time'][1]);        }        if(! empty($data['team_id'])) $model->where('team_id',$data['team_id']);        if(! empty($data['device_id'])) $model->whereIn('device_id',$data['device_id']);        if(isset($data['is_finished'])) {            if($data['is_finished']){                $model->wherecolumn('dispatch_quantity','=','finished_num');            }else{                $model->wherecolumn('dispatch_quantity','>','finished_num');            }        }        $list = $this->limit($model,'',$data);        $list = $this->fillDispatchOrderForSqListData($list,$data,$user);        return [true,$list];    }    public function fillDispatchOrderForSqListData($data,$erg,$user){        if(empty($data['data'])) return $data;        $team_map = Team::whereIn('id',array_unique(array_column($data['data'],'team_id')))            ->pluck('title','id')            ->toArray();        $equipment_map = Equipment::whereIn('id',array_unique(array_column($data['data'],'device_id')))            ->pluck('title','id')            ->toArray();        $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))            ->pluck('title','id')            ->toArray();        $orders = OrdersProduct::whereIn('id', array_column($data['data'],'order_product_id'))            ->pluck('production_no','id')            ->toArray();        $return = [];        if(! empty($erg['material'])){            $product_no = array_unique(array_column($data['data'],'product_no'));            $service = new FyyOrderService();            list($status, $msg) = $service->getProductDataFromSqlServer(['product_no' => $product_no], $user);            if($status) $return = $msg;        }        foreach ($data['data'] as $key => $value){            $data['data'][$key]['material'] = $return[$value['product_no']] ?? [];            $data['data'][$key]['wg_status_title'] = DispatchSub::$status_name[$value['wg_status']] ?? "";            $data['data'][$key]['status_title'] = DispatchSub::$status_name[$value['status']] ?? "";            $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';            $data['data'][$key]['production_no'] = $orders[$value['order_product_id']] ?? '';            $time1 = $value['dispatch_time_start'] ? date('Y-m-d',$value['dispatch_time_start']) : '';            $time2 = $value['dispatch_time_end'] ? date('Y-m-d',$value['dispatch_time_end']) : '';            $data['data'][$key]['dispatch_time'] = $time1 . ' ' . $time2;            $data['data'][$key]['process_name'] = $process_map[$value['process_id']] ?? '';            $data['data'][$key]['team_name'] = $team_map[$value['team_id']] ?? "";            $data['data'][$key]['equipment_name'] = $equipment_map[$value['device_id']] ?? "";            $data['data'][$key]['equipment_id'] = $value['device_id'];            $data['data'][$key]['un_finished_quantity'] = bcsub($value['dispatch_quantity'] , $value['finished_num'],3);        }        $total = $this->getTotal($data['data'], 'dispatch_quantity');        $data['dispatch_quantity'] = $total;        return $data;    }}
 |