| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577 | <?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\Log;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,&$return_dispatch){        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,                    'zj_plan_id' => $t['zj_plan_id'] ?? 0,                ];            }            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,                ];                if($value['process_id'] == 14){                    $return_dispatch[] = [                        'id' => $value['id'],                        'num' => $value['dispatch_quantity']                    ];                }            }            //生成报工单            $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, $out_order_no,$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,$out_order_no,$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, ''];    }    public function finishedOrderList($data){        $model = ReportWorkingDetail::from("report_working_detail as a")            ->leftJoin("dispatch_sub as b",'a.data_id','b.id')            ->leftjoin("report_working as c",'a.report_working_id', 'c.id')            ->where("a.del_time", 0)            ->where('b.del_time', 0)            ->where('c.del_time', 0)            ->select('a.quantity as finished_num','b.id','b.order_no','b.table_header_mark','b.product_no','b.product_title','b.product_size','b.product_unit','b.dispatch_quantity','b.technology_material','b.technology_name','b.wood_name','b.process_mark','b.table_body_mark','b.dispatch_no','b.crt_id','b.process_id','b.dispatch_time_start','b.dispatch_time_end','a.crt_time','b.customer_name','b.order_product_id','b.out_order_no','b.team_id','b.device_id','b.wg_status','b.status','b.type','c.order_number')            ->orderBy('a.id','desc');        if(isset($data['type'])) $model->where('b.type', $data['type']);        if(isset($data['status'])) $model->where('b.status', $data['status']);        if(isset($data['wg_status'])) $model->where('b.wg_status', $data['wg_status']);        if(! empty($data['order_no'])) $model->where('b.order_no', 'LIKE', '%'.$data['order_no'].'%');        if(! empty($data['dispatch_no'])) $model->where('b.dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%');        if(! empty($data['out_order_no'])) $model->where('b.out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');        if(! empty($data['order_number'])) $model->where('c.order_number', 'LIKE', '%'.$data['order_number'].'%');        if(! empty($data['production_no'])) {            $id = OrdersProduct::where('del_time', 0)                ->where('b.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('b.process_id',$data['process_id']);        if(! empty($data['technology_material'])) $model->where('b.technology_material', 'LIKE', '%'.$data['technology_material'].'%');        if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('a.crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);        if(! empty($data['dispatch_time'][0]) && ! empty($data['dispatch_time'][1])){            $model->where('b.dispatch_time_start','>=',$data['dispatch_time'][0]);            $model->where('b.dispatch_time_end','<=',$data['dispatch_time'][1]);        }        if(! empty($data['pc_number'])) $model->where('b.out_order_no', 'LIKE', '%'.$data['pc_number'].'%');        if(! empty($data['team_id'])) $model->where('b.team_id',$data['team_id']);        if(! empty($data['device_id'])) $model->where('b.device_id',$data['device_id']);        if(! empty($data['equipment_id'])) $model->where('b.device_id',$data['equipment_id']);        if(! empty($data['employee_id'])) {            $team = EmployeeTeamPermission::where('employee_id',$data['employee_id'])                ->select('team_id')                ->get()->toArray();            $model->whereIn('b.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;    }}
 |