| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483 | <?phpnamespace App\Service;use App\Jobs\ProcessDataJob;use App\Model\Box;use App\Model\BoxDetail;use App\Model\Dispatch;use App\Model\DispatchSub;use App\Model\Employee;use App\Model\EmployeeTeamPermission;use App\Model\Equipment;use App\Model\FinishedOrder;use App\Model\FinishedOrderScrapp;use App\Model\FinishedOrderSub;use App\Model\OrdersProduct;use App\Model\OrdersProductProcess;use App\Model\Process;use App\Model\ReportWorking;use App\Model\ReportWorkingDetail;use App\Model\SaleOrdersProduct;use App\Model\Scrapp;use App\Model\ScrappCount;use App\Model\Team;use App\Service\Box\BoxService;use Illuminate\Support\Facades\DB;use Illuminate\Support\Facades\Redis;class FinishedOrderService extends Service{    public function edit($data){}    public function getZj($data, $user){        if(empty($data['id'])) return [false, '完工单ID不能为空'];        if(empty($data['process_id'])) return [false, '工序ID不能为空'];        $old = ScrappCount::where('del_time',0)            ->where('dispatch_sub_id', $data['id'])            ->where('process_id', $data['process_id'])            ->select('scrapp_id','scrapp_num as num','process_id')            ->get()->toArray();        $return = [];        foreach ($old as $value){            $key = $value['scrapp_id'] . $value['process_id'];            if(isset($return[$key])){                $return[$key]['num'] += $value['num'];            }else{                $return[$key] = [                    'scrapp_id' => $value['scrapp_id'],                    'num' => $value['num'],                    'process_id' => $value['process_id'],                ];            }        }        return [true, ['save' => array_values($return)]];    }    public function editZj($data, $user){        list($status, $msg) = $this->editZjRule($data);        if(! $status) return [false, $msg];        $order = $msg;        try{            DB::beginTransaction();            $time = time();            //更新次品数量            $num = 0;            if(! empty($data['waste'])){                foreach ($data['waste'] as $value){                    $num += $value['num'];                }            }            DispatchSub::where('id',$order['id'])->update([                'waste_num' => $num,            ]);            $insert = [];            foreach ($data['waste'] as $value){                $insert[] = [                    'sale_orders_product_id' => $order['sale_orders_product_id'],                    'order_product_id' => $order['order_product_id'],                    'dispatch_sub_id' => $order['id'],                    'out_order_no' => $order['out_order_no'],                    'order_no' => $order['order_no'],                    'customer_no' => $order['customer_no'],                    'customer_name' => $order['customer_name'],                    'product_no' => $order['product_no'],                    'product_title' => $order['product_title'],                    'product_size' => $order['product_size'],                    'product_unit' => $order['product_unit'],                    'technology_material' => $order['technology_material'],                    'technology_name' => $order['technology_name'],                    'wood_name' => $order['wood_name'],                    'price' => $order['price'],                    'process_mark' => $order['process_mark'],                    'table_body_mark' => $order['table_body_mark'],                    'table_header_mark' => $order['table_header_mark'],                    'crt_id' => $user['id'],                    'crt_time' => $time,                    'scrapp_num' => $value['num'],                    'scrapp_id' => $value['scrapp_id'],                    'team_id' => $data['team_id'],                    'finished_id' => 0,                    'equipment_id' => $data['equipment_id'],                    'order_number' => $order['id'] . $time . $order['process_id'],                    'process_id' => $order['process_id'],                    'quantity' => $order['finished_num'],                ];            }            //质检单 不良品单            ScrappCount::where('del_time',0)                ->where('dispatch_sub_id', $order['id'])                ->where('process_id',$order['process_id'])                ->update(['del_time' => $time]);            if(! empty($insert)) ScrappCount::insert($insert);            DB::commit();        }catch (\Throwable $exception){            DB::rollBack();            return [false, $exception->getMessage()];        }        return [true, ''];    }    public function editZjRule($data){        if(empty($data['id'])) return [false, '完工ID不能为空'];        $order = DispatchSub::where('del_time',0)            ->where('id',$data['id'])            ->first();        if(empty($order)) return [false, '完工单不存在或已被删除'];        $order = $order->toArray();        if(empty($data['team_id'])) return [false, '质检班组不能为空'];//            if(empty($value['finished_id'])) return [false, '质检人员不能为空'];        if(empty($data['equipment_id'])) return [false, '设备不能为空'];//        if(empty($data['waste'])) return [false, '质检信息不能为空'];//        if(! empty($data['waste'])){//            $num = 0;//            foreach ($data['waste'] as $value){//                if(empty($value['num'])) return [false, '质检损耗数量不能为空'];//                if(empty($value['scrapp_id'])) return [false, '质检损耗原因不能为空'];//                $num += $value['num'];//            }////            if($num > $order['finished_num']) return [false, '质检总数不能超过完工数量'];//        }        return [true, $order];    }    public function setOrderNO(){        $str = date('Ymd',time());        $order_number = FinishedOrder::where('finished_no','Like','%'. $str . '%')            ->max('finished_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){return [false, '接口功能已关闭'];        //数据校验以及填充        list($status,$msg) = $this->orderRule($data);        if(!$status) return [$status,$msg];        //数据源        $result = $msg;        $time = time();        try{            DB::beginTransaction();            $em = new EmployeeService();            $auto = $em->is_auto($user, "wg_auto");            //班组下的人            $team_man = [];            $team_array = EmployeeTeamPermission::select('employee_id', 'team_id')->get()->toArray();            foreach ($team_array as $v){                $team_man[$v['team_id']][] = $v['employee_id'];            }            $last_update = $last_update2 = $str_tmp = [];            foreach ($result as $value){                $finished_num = $value['quantity'] + $value['finished_num'];                $waste_num = $value['waste_quantity'] + $value['waste_num'];                DispatchSub::where('id',$value['id'])->update([                    'finished_num' => $finished_num,                    'waste_num' => $waste_num,                    'wg_status' => $auto,                ]);                $tmp = [                    'id' => $value['id'],                    'quantity' => $value['quantity'],                    'product_no' => $value['product_no'] ?? "",                    'product_title' => $value['product_title'] ?? "",                    'product_size' => $value['product_size'] ?? "",                    'product_unit' => $value['product_unit'] ?? "",                ];                $str = $value['order_product_id'] . $value['crt_time'] . $value['product_no'] . $value['technology_name'];                if(! in_array($str, $str_tmp)){                    $last_update[] = $tmp;                    $str_tmp[] = $str;                }                //班组下的人                $man = $team_man[$value['team_id']] ?? [];                $f_m = 0;                if(! in_array($value['finish_id'], $man) && $value['finish_id'] > 0) {                    $man[] = $value['finish_id'];                    $f_m = 1;                }                $tmp['process_id'] = $value['process_id'];                foreach ($man as $m_v){                    if($f_m && $m_v == $value['finish_id']){                        $tmp['team_id'] = 0;                    }else{                        $tmp['team_id'] = $value['team_id'];                    }                    $tmp['finished_id'] = $m_v;                    $last_update2[] = $tmp;                }                $insert_waste = $scrapp = [];                if(! empty($value['waste_array'])){                    foreach ($value['waste_array'] as $k => $v){//                        $num = $v['num'] * 1000;//                        for($i = 0 ;$i < $num; $i++){//                            $insert_waste[] = [//                                'order_product_id' => $value['order_product_id'],//                                'order_no' => $value['order_no'],//                                'product_no' => $value['product_no'],//                                'product_title' => $value['product_title'],//                                'process_id' => $value['process_id'],//                                'crt_time' => $time,//                                'dispatch_no' => $value['dispatch_no'],//                                'status' => 4,//不良品//                                'team_id' => $value['team_id'],//                                'finished_id' => $value['finish_id'],//                                'equipment_id' => $value['device_id'],//                                'scrapp_id' => $v['scrapp_id']//                            ];//                        }                        $scrapp[] = [                            'sale_orders_product_id' => $value['sale_orders_product_id'],                            'order_product_id' => $value['order_product_id'],                            'dispatch_sub_id' => $value['id'],                            'out_order_no' => $value['out_order_no'],                            'order_no' => $value['order_no'],                            'customer_no' => $value['customer_no'],                            'customer_name' => $value['customer_name'],                            'product_no' => $value['product_no'],                            'product_title' => $value['product_title'],                            'product_size' => $value['product_size'],                            'product_unit' => $value['product_unit'],                            'technology_material' => $value['technology_material'],                            'technology_name' => $value['technology_name'],                            'wood_name' => $value['wood_name'],                            'price' => $value['price'],                            'process_mark' => $value['process_mark'],                            'table_body_mark' => $value['table_body_mark'],                            'table_header_mark' => $value['table_header_mark'],                            'crt_id' => 1,                            'crt_time' => $time,                            'scrapp_num' => $v,                            'scrapp_id' => $k,                            'team_id' => $value['team_id'] ?? 0,                            'finished_id' => $value['finish_id'] ?? 0,                            'equipment_id' => $value['device_id'] ?? 0,                            'order_number' => $value['id'] . $time . $value['process_id'],                            'process_id' => $value['process_id'] ?? 0,                            'quantity' => $value['quantity'],                        ];                    }                }                $quantity = $value['quantity'] * 1000;                //工序表                $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);                $process_model->where('order_product_id',$value['order_product_id'])                    ->where('process_id',$value['process_id'])                    ->where('dispatch_no',$value['dispatch_no'])                    ->where('status', 1)                    ->take($quantity)                    ->update([                        'finished_time' => $time,                        'status' => 2,                        'finished_id' => $value['finish_id'] ?? 0,                        'team_id' => $value['team_id'] ?? 0,                        'equipment_id' => $value['device_id'] ?? 0,                    ]);                if(! empty($insert_waste)) $process_model->insert($insert_waste);                if(! empty($scrapp)) {                    $scrapp_model = new ScrappCount();                    $scrapp_model->insert($scrapp);                }////                if(! empty($value['waste_quantity'])){//                    $num = $value['waste_quantity'];//                    OrdersProduct::where('id',$value['order_product_id'])->update([//                        'scrapp_num' => DB::raw("scrapp_num + {$num}"),//                    ]);//                }            }            //反写数量            //生产订单            $id = array_column($result,'order_product_id');            $this->writeFinishedQuantityByOrdersProductId($id);            //销售订单            $this->writeFinishedQuantity($id);            //生成完工入库申请单            $service = new BoxService();            list($status, $msg) = $service->createWGSQ($last_update, $user);            if(! $status) return [false, $msg];            //生成报工单            list($status, $msg) = $this->createbg($last_update2, $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 createbg($insert, $user,$dispatch_time = 0){        //报工单        $em = new EmployeeService();        $auto = $em->is_auto($user, "wgbg_auto");        $order_number = (new ApplyOrderService())->setOrderNO2($dispatch_time);        if(empty($order_number)) return [false, '报工单编号生成失败'];        try {            DB::beginTransaction();            if(empty($dispatch_time)){                $time = time();            }else{                $time = $dispatch_time;            }            $model = new ReportWorking();            $model->order_number = $order_number;            $model->report_time = $time;            $model->mark = $data['mark']?? "";            $model->crt_id = $user['id'];            $model->status = $auto;            $model->save();            $id = $model->id;            $detail_insert = [];            foreach ($insert as $v){                $detail_insert[] = [                    'report_working_id' => $id,                    'data_id' => $v['id'],                    'quantity' => $v['quantity'] ?? 0,                    'finished_id' => $v['finished_id'] ?? 0,                    'process_id' => $v['process_id'] ?? 0,                    'team_id' => $v['team_id'] ?? 0,                    'crt_time' => $time,                ];            }            ReportWorkingDetail::insert($detail_insert);            DB::commit();        }catch (\Throwable $exception){            DB::rollBack();            return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];        }        return [true, $id];    }    public function createZJFirst($report_id, $insert){        foreach ($insert as $key => $value) {            $t_report_id = $report_id[$value['dispatch_sub_id']] ?? 0;            $insert[$key]['report_id'] = $t_report_id;        }        ScrappCount::insert($insert);        return [true, ''];    }    public function createZJSecond($get_order_number, $user, $dispatch_time, $c_time,$max_id,$team_man,$report_order_number){        try {            $time = time();            //获取新增的单据            $result = ScrappCount::whereIn('order_number',$get_order_number)                ->where('result','>',0)                ->select('scrapp_num as num','id','dispatch_sub_id','warehouse_no','warehouse_title','result','product_no')                ->get()->toArray();            if(empty($result)) return [true, $get_order_number];            $dispatchList = DispatchSub::whereIn('id', array_column($result,'dispatch_sub_id'))->get()->toArray();            $dispatch_map = array_column($dispatchList,null,'id');            //是否自动审核            $em = new EmployeeService();            $auto = $em->is_auto($user, "pg_auto");            $dispatch_no = (new DispatchService())->setOrderNO($dispatch_time);            if(! $dispatch_no) return [false,'返工派工单号生成失败!'];            $prefix = substr($dispatch_no, 0, -3);            $startNum = (int)substr($dispatch_no, -3);            $is_first = true;            $dispatch_main = $dispatch = $is_need_ll = $is_need_warehouse = $zj = [];            foreach ($result as $value){                //派工单                $t = $dispatch_map[$value['dispatch_sub_id']] ?? [];                if(! $is_first){                    $startNum += 1;                    $number = str_pad($startNum,3,'0',STR_PAD_LEFT);                    $number = $prefix . $number;                    $dispatch_no_t = $number;                }else{                    $dispatch_no_t = $dispatch_no;                    $is_first = false;                }                if($value['result'] == ReportWorking::type_one){                    //重新入生产流程 需要生成返工派工单 报工单 质检单                }else{                    //入仓库 那么需要生成生成返工派工单 报工单 质检单 质检入库申请单 生成领料申请单                    $is_need_warehouse[] = [                        'warehouse_no' => $value['warehouse_no'],                        'warehouse_title' => $value['warehouse_title'],                    ];                    $zj[] = [                        'id' => $value['id'],                        'quantity' => $value['num'],                        'product_no' => $value['product_no'],                    ];                }                $is_need_ll[] = $dispatch_no_t;                $dispatch_main[] = ['dispatch_no' => $dispatch_no_t,'crt_time' => $time];                $dispatch[] = [                    'order_product_id' => $t['order_product_id'],                    'order_no' => $t['order_no'],                    'product_no' => $t['product_no'],                    'product_title' => $t['product_title'],                    'product_size' => $t['product_size'],                    'product_unit' => $t['product_unit'],                    'production_quantity' => $t['production_quantity'],                    'dispatch_quantity' => $value['num'],//                    'finished_num' => $value['num'],                    'wg_status' => 1,                    'technology_material' => $t['technology_material'],                    'technology_name' => $t['technology_name'],                    'wood_name' => $t['wood_name'],                    'process_mark' => $t['process_mark'],                    'table_body_mark' => $t['table_body_mark'],                    'table_header_mark' => $t['table_header_mark'],                    'crt_time' => $dispatch_time,                    'status' => $auto,                    'dispatch_no' => $dispatch_no_t,                    'process_id' => $t['process_id'],                    'dispatch_time_start' => $t['dispatch_time_start'],                    'dispatch_time_end' => $t['dispatch_time_end'],                    'crt_id' => $user['id'],                    'sale_orders_product_id' => $t['sale_orders_product_id'],                    'out_order_no' => $t['out_order_no'],                    'out_order_no_time' => $t['out_order_no_time'],                    'price' => $t['price'],                    'customer_no' => $t['customer_no'],                    'customer_name' => $t['customer_name'],                    'pre_shipment_time' => $t['pre_shipment_time'],                    'team_id' => $t['team_id'],                    'device_id' => $t['device_id'],                    'type' => 1,                ];            }            Dispatch::insert($dispatch_main);            DispatchSub::insert($dispatch);            $get_order_number1 = [];            //获取上一次插入订单的所有数据            $insert = DispatchSub::whereIn('dispatch_no', $is_need_ll)                ->where('crt_time',$dispatch_time)                ->where('crt_id',$user['id'])                ->get()->toArray();            $insert_bg = [];            foreach ($insert as $value){                $t = $team_man[$value['team_id']] ?? 0;                $insert_bg[] = [                    'id' => $value['id'],                    'quantity' => $value['dispatch_quantity'],                    'product_no' => $value['product_no'] ?? "",                    'product_title' => $value['product_title'] ?? "",                    'product_size' => $value['product_size'] ?? "",                    'process_id' => $value['process_id'] ?? "",                    'team_id' => $value['team_id'] ?? "",                    'finished_id' => $t,                ];            }            //生成报工单            $service_box = new BoxService();            $first_order_number = end($report_order_number);            list($status, $msg) = $service_box->createbg($insert_bg, $user, $dispatch_time, $first_order_number);            if(! $status) return [false, $msg];            list($report_id, $report_order_number1) = $msg;            //生成质检单            $service1 = new ApplyOrderService();            $date = date("Ymd", $c_time);            $result = [];            foreach ($insert as $value){                //质检                $max_id += 1;                $order_number = $date . str_pad($max_id,3,'0',STR_PAD_LEFT);                $get_order_number1[] = $order_number;                $t_report_id = $report_id[$value['id']] ?? 0;                $result[] = [                    'sale_orders_product_id' => $value['sale_orders_product_id'],                    'order_product_id' => $value['order_product_id'],                    'dispatch_sub_id' => $value['id'],                    'out_order_no' => $value['out_order_no'],                    'order_no' => $value['order_no'],                    'customer_no' => $value['customer_no'],                    'customer_name' => $value['customer_name'],                    'product_no' => $value['product_no'],                    'product_title' => $value['product_title'],                    'product_size' => $value['product_size'],                    'product_unit' => $value['product_unit'],                    'technology_material' => $value['technology_material'],                    'technology_name' => $value['technology_name'],                    'wood_name' => $value['wood_name'],                    'price' => $value['price'],                    'process_mark' => $value['process_mark'],                    'table_body_mark' => $value['table_body_mark'],                    'table_header_mark' => $value['table_header_mark'],                    'crt_id' => $user['id'],                    'crt_time' => $c_time,                    'team_id' => 73,                    'finished_id' => $user['id'],                    'equipment_id' => $value['device_id'],                    'order_number' => $order_number,                    'process_id' => $value['process_id'],                    'quantity' => $value['dispatch_quantity'],                    'report_id' => $t_report_id,                    'daily_id' => $max_id,                ];            }            ScrappCount::insert($result);            if(! empty($is_need_warehouse)){                //领料申请单                list($status, $msg) = (new DispatchService())->createSQ($insert, $user, $dispatch_time);                if(! $status) return [false, $msg];                //质检入库申请单 原材料                list($status, $msg) = $service1->createZJSQ($zj, $user, $is_need_warehouse, $c_time);                if(! $status) return [false, $msg];            }            return [true, array_merge($get_order_number, $get_order_number1)];        }catch (\Throwable $exception){            return[false, $exception->getMessage() . $exception->getLine()];        }    }    public function isNeedZJ($sale_orders_product_id = []){        $sale_order = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)            ->where('order_quantity','>=', 20)            ->select('id', 'order_quantity')            ->get()->toArray();        if(empty($sale_order)) return [false, ''];        $need = [];        $sale_order_id = array_column($sale_order,'id');        //每个销售订单有4次的不良品创建需要        foreach ($sale_order_id as $value){            $need[$value] = 4;        }        $scrapp = ScrappCount::where("del_time",0)            ->whereIn("sale_orders_product_id",$sale_order_id)            ->where('scrapp_num','>',0)            ->select('sale_orders_product_id','process_id')            ->orderBy('process_id','asc')            ->get()->toArray();        $orderBy = [];        foreach ($scrapp as $value){            if(isset($need[$value['sale_orders_product_id']])) $need[$value['sale_orders_product_id']] -= 1;            $orderBy[$value['sale_orders_product_id']][] = $value['process_id'];        }        $count = [];        foreach ($need as $key => $value){            if($value > 0) $count[$key] = $value;        }        //是否需要创建        if(empty($count)) return [false, ''];        $return = [];        $process_id = [12,12,14,14];        foreach ($count as $key => $value){            $p_t = $orderBy[$key] ?? [];            $p_t_k = count($p_t) ?? 0;            if(isset($process_id[$p_t_k])){                $t = $process_id[$p_t_k];                $return[$key] = $t;            }        }        return [true, $return];    }    //产成品入库    public function U8Rdrecord10Save($package_data, $user){        if(empty($package_data)) return [false, '产成品入库为空,请确认!'];        try{            //获取包装单信息            $box = $package_data;            $boxDetail = new BoxDetail(['channel'=>$box['top_order_no']]);            $boxDetail = $boxDetail->where('del_time',0)                ->where('order_no',$box['order_no'])                ->select('id','top_id','num','ext_1','ext_2','ext_3','ext_4','ext_5','out_order_no','box_type','price')                ->get()->toArray();            //用友数据插入------------            if(! empty($boxDetail)){                $sqlServerModel = new FyySqlServerService($user);                if($sqlServerModel->error) return [false, $sqlServerModel->error];                $username = $sqlServerModel->getYongyouName();                $box['create_name'] = $username;                list($status,$msg) = $sqlServerModel->U8Rdrecord10Save($box,$boxDetail);                if(! $status) return [false, $msg];            }            //用友数据插入------------        }catch (\Exception $e){            return [false,$e->getMessage() . '|' . $e->getFile() . '|' . $e->getLine()];        }        return [true,''];    }    //销售发货出库    public function U8Rdrecord32Save($send_data, $user){        if(empty($send_data)) return [false, '销售发货出库为空,请确认!'];        try{            //用友数据插入------------            $sqlServerModel = new FyySqlServerService($user);            if($sqlServerModel->error) return [false, $sqlServerModel->error];            $create_name = $sqlServerModel->getYongyouName();            list($status,$msg) = $sqlServerModel->U8Rdrecord32Save($send_data,$create_name);            if(! $status) return [false, $msg];            //用友数据插入------------        }catch (\Exception $e){            return [false,$e->getMessage() . '|' . $e->getFile() . '|' . $e->getLine()];        }        return [true,''];    }    public function addInJob($result,$data,$user){        try{            //获取数据库连接            $database = $this->getConnectionName($user['zt']);            //用友数据插入------------            $insert_sql_server = [];            foreach ($result as $key => $value){                $quantity_tmp = $data['quantity'][$key];                $result[$key]['quantity'] = $quantity_tmp;                //工序表                $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);                $process_model->setConnection($database);                $process_id = $process_model->select('process_id')                    ->where('sort',$process_model->where('del_time',0)                        ->where('order_product_id',$value['order_product_id'])                        ->max('sort'))                    ->first();                if(empty($process_id)) return [false,"未找到最后一道工序"];                $process_id = $process_id->process_id;                if($process_id == $value['process_id']){                    $insert_sql_server[] = $result[$key];                }            }            if(! empty($insert_sql_server)){                $sqlServerModel = new FyySqlServerService($user);                if($sqlServerModel->error) return [false,$sqlServerModel->error];                foreach ($insert_sql_server as $value){//                    list($status,$msg) = $sqlServerModel->U8Rdrecord10Save($value);//                    if(! $status) return [false,$msg];                }            }            //用友数据插入结束----------            //本地数据更新            DB::beginTransaction();            $waste = [];            foreach ($data['waste'] as $key => $value){                $waste[$key] = array_sum(array_column($value,'num'));            }            $time = time();            foreach ($result as $key => $value){                $finished_id_tmp = $data['finish_id'][$key];                $team_tmp = $data['team_id'][$key];                $equipment_id_tmp = $data['equipment_id'][$key];                $finished_num = $value['quantity'] + $value['finished_num'];                $model = new DispatchSub();                $model->setConnection($database);                $model->where('id',$value['id'])->update([                    'finished_num' => $finished_num,                    'waste_num' => $waste[$key],                    'job_status' => 0,                    'dispatch_quantity' => DB::raw("dispatch_quantity + {$waste[$key]}"),//派工数量增加 派工单可以继续派送                ]);                $insert_waste = $insert_dispatch = $scrapp = [];                if(! empty($data['waste'][$key])){                    foreach ($data['waste'][$key] as $v){                        for($i = 0 ;$i < $v['num']; $i++){                            $insert_waste[] = [                                'order_product_id' => $value['order_product_id'],                                'order_no' => $value['order_no'],                                'product_no' => $value['product_no'],                                'product_title' => $value['product_title'],                                'process_id' => $value['process_id'],                                'crt_time' => $time,                                'dispatch_no' => $value['dispatch_no'],                                'status' => 4,//不良品                                'team_id' => $team_tmp,                                'finished_id' => $finished_id_tmp,                                'equipment_id' => $equipment_id_tmp,                                'scrapp_id' => $v['scrapp_id']                            ];                            $insert_dispatch[] = [                                'order_product_id' => $value['order_product_id'],                                'out_order_no' => $value['out_order_no'],                                'order_no' => $value['order_no'],                                'product_no' => $value['product_no'],                                'product_title' => $value['product_title'],                                'process_id' => $value['process_id'],                                'crt_time' => $time,                                'dispatch_no' => $value['dispatch_no'],                                'status' => 1, //已派工                                'team_id' => 0,                                'finished_id' => 0,                                'equipment_id' => 0,                                'scrapp_id' => 0                            ];                        }                        $scrapp[] = [                            'sale_orders_product_id' => $value['sale_orders_product_id'],                            'order_product_id' => $value['order_product_id'],                            'out_order_no' => $value['out_order_no'],                            'order_no' => $value['order_no'],                            'customer_no' => $value['customer_no'],                            'customer_name' => $value['customer_name'],                            'product_no' => $value['product_no'],                            'product_title' => $value['product_title'],                            'product_size' => $value['product_size'],                            'product_unit' => $value['product_unit'],                            'technology_material' => $value['technology_material'],                            'technology_name' => $value['technology_name'],                            'wood_name' => $value['wood_name'],                            'price' => $value['price'],                            'process_mark' => $value['process_mark'],                            'table_body_mark' => $value['table_body_mark'],                            'table_header_mark' => $value['table_header_mark'],                            'crt_time' => $time,                            'scrapp_num' => $v['num'],                            'scrapp_id' => $v['scrapp_id']                        ];                    }                }                //工序表                $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);                $process_model->setConnection($database);                $process_model->where('order_product_id',$value['order_product_id'])                    ->where('process_id',$value['process_id'])                    ->where('dispatch_no',$value['dispatch_no'])                    ->take($value['quantity'])                    ->update([                        'finished_time' => $time,                        'status' => 2,                        'finished_id' => $finished_id_tmp,                        'team_id' => $team_tmp,                        'equipment_id' => $equipment_id_tmp                    ]);                if(! empty($insert_waste)) $process_model->insert($insert_waste);                if(! empty($insert_dispatch)) $process_model->insert($insert_dispatch);                if(! empty($scrapp)) {                    $scrapp_model = new ScrappCount();                    $scrapp_model->setConnection($database);                    $scrapp_model->insert($scrapp);                }                //生产订单数量                if(! empty($waste[$key])){                    $num = $waste[$key];                    $model2 = new OrdersProduct();                    $model2->setConnection($database);                    $model2->where('id',$value['order_product_id'])->update([                        'production_quantity' => DB::raw("production_quantity + {$num}"),                        'scrapp_num' => DB::raw("scrapp_num + {$num}"),                        'dispatch_complete_quantity' => DB::raw("dispatch_complete_quantity + {$num}"),//已派工数量增加                    ]);                }            }            //反写数量//            $this->writeFinishedQuantity(array_column($result,'sale_orders_product_id'),$database);//            $this->writeFinishedQuantityByOrdersProductId(array_column($result,'order_product_id'),$database);            DB::commit();        }catch (\Exception $e){            DB::rollBack();            return [false,$e->getFile() . $e->getLine(). $e->getMessage()];        }        return [true,''];    }    public function del($data){        if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];        return [true,'删除成功'];    }    public function orderDetail($data){        if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];        $first = DispatchSub::where('id',$data['id'])->first();        $result = [];        $scrapp = ScrappCount::where('del_time',0)            ->where('order_product_id',$first->order_product_id)            ->select('order_product_id','scrapp_num as num', 'scrapp_id')            ->get()->toArray();        foreach ($scrapp as $value){            if(isset($result[$value['order_product_id']])){                $tmp = bcadd($result[$value['scrapp_id']],$value['num'],3);                $result[$value['scrapp_id']]['num'] = $tmp;            }else{                $result[$value['scrapp_id']] = [                    'scrapp_id' => $value['scrapp_id'],                    'num' =>  $value['num']                ];            }        }        $result = array_values($result);//        $first = DispatchSub::where('id',$data['id'])->first();//        $process_model = new OrdersProductProcess(['channel' => date("Ymd",$first->out_order_no_time)]);////        $result = $process_model->where('del_time',0)//            ->where('dispatch_no',$first->dispatch_no)//            ->where('order_product_id',$first->order_product_id)//            ->where('status',4)//            ->select(DB::raw('count(id) as num'),'scrapp_id')//            ->groupBy('scrapp_id')//            ->get()->toArray();        $map = Scrapp::whereIn('id',array_column($result,'scrapp_id'))            ->pluck('title','id')            ->toArray();        foreach ($result as $key => $value){            $result[$key]['scrapp_name'] = $map[$value['scrapp_id']] ?? '';        }        return [true,$result];    }    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 orderRule($data){        if(! isset($data['is_finish_all'])) return [false, '完工类型不能为空!'];        if(empty($data['detail'])) return [false, '完工明细数据不能为空!'];        $detail_map = $id = $detail_map2 = $waste_map = [];        //班组下的人        $team_man = [];        $team_array = EmployeeTeamPermission::select('employee_id', 'team_id')->get()->toArray();        foreach ($team_array as $v){            $team_man[$v['team_id']][] = $v['employee_id'];        }        foreach ($data['detail'] as $value){            if(empty($value['id'])) return [false,'请选择完工数据!'];            $id[] = $value['id'];            if(empty($value['device_id'])) return [false, '请选择设备!'];            if(empty($value['team_id'])) return [false, '班组必须选择'];            if(empty($team_man[$value['team_id']])) return [false, '班组下的人不能为空,请设置'];//            if(empty($value['team_id']) && empty($value['finish_id'])) return [false,'班组和人员必须选择一个!'];            if(! is_numeric($value['quantity']) || $value['quantity'] < 0) return [false,'完工数量不能小于0!'];            if(! empty($value['waste'])){                foreach ($value['waste'] as $waste){                    if(empty($waste['scrapp_id'])) return [false,'请选择损耗原因!'];                    if(! is_numeric($waste['num']) || $waste['num'] < 0) return [false,'损耗数量不能小于0!'];                    if(isset($waste_map[$value['id']])){                        $waste_map[$value['id']] += $waste['num'];                    }else{                        $waste_map[$value['id']] = $waste['num'];                    }                }            }            //完工数量            if(isset($detail_map[$value['id']])){                $detail_map[$value['id']] += $value['quantity'];            }else{                $detail_map[$value['id']] = $value['quantity'];            }            if(! isset($detail_map2[$value['id']])){                $detail_map2[$value['id']] = $value;            }        }        //校验        $result = DispatchSub::where('del_time',0)            ->whereIn('id',$id)            ->select('id','finished_num','dispatch_quantity','out_order_no_time','process_id','dispatch_no','order_product_id','sale_orders_product_id','order_no','product_no','product_title','price','customer_name','technology_material','technology_name','wood_name','customer_no','out_order_no','waste_num','product_size','product_unit','process_mark','table_body_mark','table_header_mark','sale_orders_product_id','crt_time')            ->get()->toArray();        if(empty($result)) return [false, '派工单不存在或已被删除!'];        $process_map = Process::whereIn('id',array_unique(array_column($result,'process_id')))->pluck('title','id')->toArray();        $search = "";$args = [];        foreach ($result as $key => $value){            $detail2 = $detail_map2[$value['id']] ?? [];            $result[$key]['device_id'] = $detail2['device_id'] ?? 0;            $result[$key]['team_id'] = $detail2['team_id'] ?? 0;            $result[$key]['finish_id'] = $detail2['finish_id'] ?? 0;            $result[$key]['waste_quantity'] = $waste_map[$value['id']] ?? 0;            $tmp = [];            if(! empty($detail2['waste'])){                foreach ($detail2['waste'] as $v){                    if(isset($tmp[$v['id']])){                        $n = bcadd($v['num'],$tmp[$v['scrapp_id']],3);                        $tmp[$v['scrapp_id']] = $n;                    }else{                        $tmp[$v['scrapp_id']] = $v['num'];                    }                }            }            $result[$key]['waste_array'] = $tmp;            $q = $detail_map[$value['id']] ?? 0;            $quantity_tmp = bcadd($q,$value['finished_num'],3);            if($quantity_tmp > $value['dispatch_quantity']) {                $process_tmp = $process_map[$value['process_id']] ?? "";                return [false,'派工单:' . $value['dispatch_no']. '的工序' . $process_tmp .'完工数量不能大于派工数量'];            }            $result[$key]['quantity'] = $q;            $search_key = $value['order_product_id'] . $value['crt_time'];            if($data['is_finish_all'] && ! in_array($search_key , $args)){                $search .= "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";                $args[] = $search_key;            }        }        $search = rtrim($search,'OR ');        $search = "($search)";        if($data['is_finish_all']){            //系统帮助完工的数据            $result2 = DispatchSub::where('del_time',0)                ->whereNotIn('id',$id)                ->whereColumn('dispatch_quantity', '>', 'finished_num')                ->whereRaw($search)                ->select('id','finished_num','dispatch_quantity','out_order_no_time','process_id','dispatch_no','order_product_id','sale_orders_product_id','order_no','product_no','product_title','price','customer_name','technology_material','technology_name','wood_name','customer_no','out_order_no','waste_num','product_size','product_unit','process_mark','table_body_mark','table_header_mark','sale_orders_product_id','crt_time')                ->get()->toArray();            if(! empty($result2)){                foreach ($result2 as $key => $value){                    $not_finished_num = $value['dispatch_quantity'] - $value['finished_num'];                    $result2[$key]['quantity'] = $not_finished_num;                    $result2[$key]['waste_array'] = [];                    $result2[$key]['waste_quantity'] = 0;                    $result2[$key]['device_id'] = 0;                    $result2[$key]['team_id'] = 0;                    $result2[$key]['finish_id'] = 0;                }            }            $result = array_merge_recursive($result, $result2);        }        return [true, $result];    }    public function orderList($data){        $model = FinishedOrderSub::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','dispatch_quantity','finished_num','status','crt_id','process_id','equipment_id','team_id','dispatch_time_start','dispatch_time_end','dispatch_time','crt_time','dispatch_no')            ->orderBy('upd_time','desc')            ->orderBy('id','desc');        if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');        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['dispatch_time'][0]) && ! empty($data['dispatch_time'][1])) $model->whereBetween('dispatch_time',[$data['dispatch_time'][0],$data['dispatch_time'][1]]);        if(! empty($data['employee_id'])) {            $team_id = Employee::from('employee as a')                ->leftJoin('employee_team_permission as b','b.employee_id','a.id')                ->where('a.id', $data['employee_id'])                ->select('b.team_id')                ->get()->toArray();            $team_id = array_column($team_id,'team_id');            $model->where('team_id',$team_id ?? []);        }        if(isset($data['status'])) $model->where('status',$data['status']);        $list = $this->limit($model,'',$data);        $list = $this->fillData($list);        return [true,$list];    }    public function fillData($data){        if(empty($data['data'])) return $data;        $waste_map = $waste_map_2 = [];        $waste = FinishedOrderScrapp::where('del_time',0)            ->whereIn('finished_order_id',array_column($data['data'],'id'))            ->select('finished_order_id','num','scrapp_id')            ->get()->toArray();        if(! empty($waste)){            foreach ($waste as $value){                $waste_map[$value['finished_order_id']][] = [                    'num' => $value['num'],                    'scrapp_id' => $value['scrapp_id']                ];                if(isset($waste_map_2[$value['finished_order_id']])){                    $waste_map_2[$value['finished_order_id']] += $value['num'];                }else{                    $waste_map_2[$value['finished_order_id']] = $value['num'];                }            }        }        $team = EmployeeTeamPermission::from('employee_team_permission as a')            ->leftJoin('employee as b','b.id','a.employee_id')            ->whereIn('a.team_id',array_column($data['data'],'team_id'))            ->select('b.emp_name','a.team_id')            ->get()            ->toArray();        $team_map = [];        if(! empty($team)){            foreach ($team as $value){                if(isset($team_map[$value['team_id']])){                    $team_map[$value['team_id']] .= ','. $value['emp_name'];                }else{                    $team_map[$value['team_id']] = $value['emp_name'];                }            }        }        $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))            ->pluck('title','id')            ->toArray();        $team_maps = Team::whereIn('id',array_column($data['data'],'team_id'))            ->pluck('title','id')            ->toArray();        $equipment_map = Equipment::whereIn('id',array_column($data['data'],'equipment_id'))            ->pluck('title','id')            ->toArray();        foreach ($data['data'] as $key => $value){            $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_plan_time'] = $time1 . ' ' . $time2;            $data['data'][$key]['dispatch_time'] = $value['dispatch_time'] ? date('Y-m-d',$value['dispatch_time']) : '';            $data['data'][$key]['team_man'] = $team_map[$value['team_id']] ?? '';            $data['data'][$key]['process_name'] = $process_map[$value['process_id']] ?? '';            $data['data'][$key]['team_name'] = $team_maps[$value['team_id']] ?? '';            $data['data'][$key]['equipment_name'] = $equipment_map[$value['equipment_id']] ?? '';            $data['data'][$key]['waste'] = $waste_map[$value['id']] ?? [];            $data['data'][$key]['waste_quantity'] = $waste_map_2[$value['id']] ?? 0;            $data['data'][$key]['not_finished_num'] = $value['dispatch_quantity'] - $value['finished_num'];        }        $data['finished_num'] = $this->getTotal($data['data'], 'finished_num');        $data['dispatch_quantity'] = $this->getTotal($data['data'], 'dispatch_quantity');        $data['waste_quantity'] = $this->getTotal($data['data'], 'waste_quantity');        $data['not_finished_num'] = $this->getTotal($data['data'], 'not_finished_num');        return $data;    }    //反写销售订单完工数量    public function writeFinishedQuantity($orders_product_id){        if(empty($orders_product_id)) return;        $sale_orders_product_id = OrdersProduct::where('del_time',0)            ->whereIn('id', $orders_product_id)            ->select('sale_orders_product_id')            ->get()->toArray();        $sale_orders_product_id = array_unique(array_column($sale_orders_product_id,'sale_orders_product_id'));        $production_order = OrdersProduct::where('del_time',0)            ->whereIn('sale_orders_product_id', $sale_orders_product_id)            ->select(DB::raw("sum(finished_num) as finished_num"),'sale_orders_product_id')            ->groupby('sale_orders_product_id')            ->pluck('finished_num','sale_orders_product_id')            ->toArray();        foreach ($production_order as $sale_orders_product_id => $finished_num){            SaleOrdersProduct::where('id',$sale_orders_product_id)->update([                'finished_num' => $finished_num,            ]);        }    }    //反写生产订单完工数量    public function writeFinishedQuantityByOrdersProductId($order_product_id){        if(empty($order_product_id)) return;        $result = DispatchSub::where('del_time',0)            ->whereIn('order_product_id',$order_product_id)            ->select('finished_num','order_product_id','crt_time','process_id')            ->get()            ->toArray();        if(empty($result)) return;        //生产订单        $production_order = OrdersProduct::where('del_time',0)            ->whereIn('id',array_unique(array_column($result,'order_product_id')))            ->select('process_id','id','production_quantity')            ->get()            ->toArray();        $judge = [];        foreach ($result as $value){            if(isset($judge[$value['order_product_id']][$value['process_id']])){                $judge[$value['order_product_id']][$value['process_id']] += $value['finished_num'];            }else{                $judge[$value['order_product_id']][$value['process_id']] = $value['finished_num'];            }        }        foreach ($production_order as $value){            //生产订单下的所有工序派工单            $tmp = $judge[$value['id']] ?? [];            if(empty($tmp)) continue;            //生产订单下的工序种类            $ori_process = explode(',',$value['process_id']);            $ori_process_count = count($ori_process);            //生产订单下的派工单的工序种类            $process = array_keys($tmp);            $process_count = count($process);            if($ori_process_count != $process_count) continue;            //获取工序里最小的数值            $min_finished_num = min($tmp);            //更新完工数量            OrdersProduct::where('id',$value['id'])->update([                'finished_num' => $min_finished_num            ]);        }    }    public function mobileAdd($data,$user){return [false, '接口功能已关闭'];        //数据校验以及填充        list($status,$msg,$count_arr) = $this->orderMobileRule($data);        if(!$status) return [$status,$msg];        $user = [            'id' => $user['id'],            'operate_time' => time(),            'zt' => Request()->header('zt'),        ];        $redis = [            'result' => $msg,            'data' => $data,            'quantity_count' => $count_arr,        ];        $job = ProcessDataJob::dispatch($redis,$user,3)->onQueue(ProcessDataJob::job_one);        if(! $job) return [false,'任务没有进入队列!'];        //错误计数        Redis::hSet('order_failures', md5(json_encode($redis)), 0);        //标记进入队列的数据        DispatchSub::whereIn('id',array_column($msg,'id'))->update([            'job_status' => 1,        ]);        return [true,'任务已进入队列!'];    }    public function addMobileInJob($result, $data, $count_arr,$user){        try{            //获取数据库连接            $database = $this->getConnectionName($user['zt']);            //用友数据写入------------            $insert_sql_server = [];            foreach ($result as $key => $value){                $quantity_tmp = $count_arr[$value['id']];                $result[$key]['quantity'] = $quantity_tmp;                $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);                $process_model->setConnection($database);                $process_id = $process_model->select('process_id')                    ->where('sort',$process_model->where('del_time',0)                        ->where('order_product_id',$value['order_product_id'])                        ->max('sort'))                    ->first();                if(empty($process_id)) return [false,"未找到最后一道工序"];                $process_id = $process_id->process_id;                if($process_id == $value['process_id']){                    $insert_sql_server[] = $result[$key];                }            }            if(! empty($insert_sql_server)){                $sqlServerModel = new FyySqlServerService($user);                if($sqlServerModel->error) return [false,$sqlServerModel->error];                foreach ($insert_sql_server as $value){//                    list($status,$msg) = $sqlServerModel->U8Rdrecord10Save($value);//                    if(! $status) return [false,$msg];                }            }            //用友数据写入结束------------            //本地数据写入-----------            DB::beginTransaction();            $waste = $waste2 = [];            foreach ($data as $value){                if(! empty($value['waste'])){                    foreach ($value['waste'] as $v){                        if(isset($waste[$value['id']])){                            $waste[$value['id']] += $v['num'];                            $waste2[$value['order_product_id']] += $v['num'];                        }else{                            $waste[$value['id']] = $v['num'];                            $waste2[$value['order_product_id']] = $v['num'];                        }                    }                }            }            $time = time();            foreach ($result as $value){                $finished_num = $value['quantity'] + $value['finished_num'];                $watste_num = $waste[$value['id']] ?? 0;                $model = new DispatchSub();                $model->setConnection($database);                $model->where('id',$value['id'])->update([                        'finished_num' => $finished_num,                        'waste_num' => $watste_num,                        'job_status' => 0,                        'dispatch_quantity' => DB::raw("dispatch_quantity + {$watste_num}"),//派工数量增加 派工单可以继续派送                    ]);                //生产订单数量                if(! empty($waste2[$value['order_product_id']])){                    $num = $waste2[$value['order_product_id']];                    $model2 = new OrdersProduct();                    $model2->setConnection($database);                    $model2->where('id',$value['order_product_id'])->update([                        'production_quantity' => DB::raw("production_quantity + {$num}"),                        'scrapp_num' => DB::raw("scrapp_num + {$num}"),                        'dispatch_complete_quantity' => DB::raw("dispatch_complete_quantity + {$num}"),//已派工数量增加                    ]);                }                //损耗和派工  损耗统计                $insert_waste = $insert_dispatch = $scrapp = [];                //工序                $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);                $process_model->setConnection($database);                foreach ($data as $d){                    if($d['id'] == $value['id']){                        $process_model->where('order_product_id',$value['order_product_id'])                            ->where('process_id',$value['process_id'])                            ->where('dispatch_no',$value['dispatch_no'])                            ->take($d['quantity'])                            ->update([                                'finished_time' => $time,                                'status' => 2,                                'finished_id' => $d['finished_id'] ?? 0,                                'team_id' => $d['team_id'] ?? 0,                                'equipment_id' => $d['equipment_id'] ?? 0                            ]);                        if(! empty($d['waste'])){                            foreach ($d['waste'] as $v){                                for($i = 0 ;$i < $v['num']; $i++){                                    $insert_waste[] = [                                        'order_product_id' => $value['order_product_id'],                                        'out_order_no' => $value['out_order_no'],                                        'order_no' => $value['order_no'],                                        'product_no' => $value['product_no'],                                        'product_title' => $value['product_title'],                                        'process_id' => $value['process_id'],                                        'crt_time' => $time,                                        'dispatch_no' => $value['dispatch_no'],                                        'status' => 4,                                        'team_id' => $d['team_id'],                                        'finished_id' => $d['finished_id'] ?? 0,                                        'equipment_id' => $d['equipment_id'] ?? 0,                                        'scrapp_id' => $v['scrapp_id'] ?? 0                                    ];                                    $insert_dispatch[] = [                                        'order_product_id' => $value['order_product_id'],                                        'out_order_no' => $value['out_order_no'],                                        'order_no' => $value['order_no'],                                        'product_no' => $value['product_no'],                                        'product_title' => $value['product_title'],                                        'process_id' => $value['process_id'],                                        'crt_time' => $time,                                        'dispatch_no' => $value['dispatch_no'],                                        'status' => 1, //已派工                                        'team_id' => 0,                                        'finished_id' => 0,                                        'equipment_id' => 0,                                        'scrapp_id' => 0                                    ];                                }                                $scrapp[] = [                                    'sale_orders_product_id' => $value['sale_orders_product_id'],                                    'order_product_id' => $value['order_product_id'],                                    'out_order_no' => $value['out_order_no'],                                    'order_no' => $value['order_no'],                                    'customer_no' => $value['customer_no'],                                    'customer_name' => $value['customer_name'],                                    'product_no' => $value['product_no'],                                    'product_title' => $value['product_title'],                                    'product_size' => $value['product_size'],                                    'product_unit' => $value['product_unit'],                                    'technology_material' => $value['technology_material'],                                    'technology_name' => $value['technology_name'],                                    'wood_name' => $value['wood_name'],                                    'price' => $value['price'],                                    'process_mark' => $value['process_mark'],                                    'table_body_mark' => $value['table_body_mark'],                                    'table_header_mark' => $value['table_header_mark'],                                    'crt_time' => $time,                                    'scrapp_num' => $v['num'],                                    'scrapp_id' => $v['scrapp_id']                                ];                            }                        }                    }                }                if(! empty($insert_waste)) $process_model->insert($insert_waste);                if(! empty($insert_dispatch)) $process_model->insert($insert_dispatch);                if(! empty($scrapp)) {                    $scrapp_model = new ScrappCount();                    $scrapp_model->setConnection($database);                    $scrapp_model->insert($scrapp);                }            }            //反写数量//            $this->writeFinishedQuantity(array_column($result,'sale_orders_product_id'),$database);//            $this->writeFinishedQuantityByOrdersProductId(array_column($result,'order_product_id'),$database);            DB::commit();            //本地数据写入结束-----------        }catch (\Exception $e){            DB::rollBack();            return [false,$e->getFile() . $e->getLine() . $e->getMessage()];        }        return [true,''];    }    public function orderMobileRule($data){        if(empty($data)) return [false,'数据不能为空!',''];        $dispatch_id = array_unique(array_column($data,'id'));        $bool = DispatchSub::whereIn('id',$dispatch_id)->where('job_status',1)->exists();        if($bool) return [false,'正在队列中,请不要重复操作!',''];        $post = $waste = [];        foreach ($data as $value){            if(! is_numeric($value['quantity']) || $value['quantity'] <= 0) return [false,'请填写正确的完工数量!',''];            if(empty($value['finished_id']) && empty($value['team_id'])) return [false,'人员和班组必须填写一项!',''];            if(isset($post[$value['id']])){                $post[$value['id']] += $value['quantity'];            }else{                $post[$value['id']] = $value['quantity'];            }            if(! empty($value['waste'])){                foreach ($value['waste'] as $v){                    if(isset($waste[$value['id']])){                        $waste[$value['id']] += $v['num'];                    }else{                        $waste[$value['id']] = $v['num'];                    }                }            }        }        $result = DispatchSub::whereIn('id',$dispatch_id)            ->select('id','finished_num','dispatch_quantity','out_order_no_time','process_id','dispatch_no','order_product_id','sale_orders_product_id','order_no','product_no','product_title','price','customer_name','technology_material','technology_name','wood_name','customer_no','out_order_no','waste_num','product_size','product_unit','process_mark','table_body_mark','table_header_mark','sale_orders_product_id')            ->orderBy('id','desc')            ->get()->toArray();        foreach ($result as $key => $value){            $tmp = $waste[$value['id']] ?? 0;            $quantity_tmp = $post[$value['id']] + $value['finished_num'] + $value['waste_num'] + $tmp;            if($quantity_tmp > $value['dispatch_quantity']) return [false,"完工数量加上损耗数量不能大于派工数量",''];        }        return [true, $result, $post];    }    public function getConnectionName($name)    {        $mysql = "mysql";        if ($name === '001') {            $mysql = "mysql_001";        } elseif ($name === '002') {            $mysql = "mysql_002";        }        return $mysql;    }    public function insertYy($order, $user){        //包装单        $package_data = json_decode($order['post'], true);        if(! empty($package_data)) {            //用友 ------产成品入库            $service = new FinishedOrderService();            list($status,$msg) = $service->U8Rdrecord10Save($package_data,$user);            if(! $status) return [false, $msg];        }        return [true, ''];    }}
 |