| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959 | 
							- <?php
 
- namespace App\Service;
 
- use App\Jobs\ProcessDataJob;
 
- use App\Model\Box;
 
- use App\Model\BoxDetail;
 
- 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\SaleOrdersProduct;
 
- use App\Model\Scrapp;
 
- use App\Model\ScrappCount;
 
- use App\Model\Team;
 
- use Illuminate\Support\Facades\DB;
 
- use Illuminate\Support\Facades\Redis;
 
- class FinishedOrderService extends Service
 
- {
 
-     public function edit($data){}
 
-     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){
 
-         //数据校验以及填充
 
-         list($status,$msg) = $this->orderRule($data);
 
-         if(!$status) return [$status,$msg];
 
-         //数据源
 
-         $result = $msg;
 
-         $time = time();
 
-         try{
 
-             DB::beginTransaction();
 
-             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,
 
-                 ]);
 
-                 $insert_waste = $scrapp = [];
 
-                 if(! empty($value['waste_array'])){
 
-                     foreach ($value['waste_array'] as $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'],
 
-                             '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']
 
-                         ];
 
-                     }
 
-                 }
 
-                 $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'],
 
-                         'team_id' => $value['team_id'],
 
-                         'equipment_id' => $value['device_id'],
 
-                     ]);
 
-                 if(! empty($insert_waste)) $process_model->insert($insert_waste);
 
-                 if(! empty($scrapp)) {
 
-                     $scrapp_model = new ScrappCount();
 
-                     $scrapp_model->insert($scrapp);
 
-                 }
 
- // todo 暂时注释
 
- //                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);
 
-             DB::commit();
 
-         }catch (\Exception $e){
 
-             DB::rollBack();
 
-             return [false,$e->getFile() . $e->getLine(). $e->getMessage()];
 
-         }
 
-         return [true, ''];
 
-     }
 
-     //产成品入库
 
-     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();
 
-         $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 = [];
 
-         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($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($value['num']) || $value['num'] < 0) return [false,'损耗数量不能小于0!'];
 
-                     if(isset($waste_map[$value['id']])){
 
-                         $waste_map[$value['id']] += $value['num'];
 
-                     }else{
 
-                         $waste_map[$value['id']] = $value['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;
 
-             $result[$key]['waste_array'] = $detail2['waste'] ?? [];
 
-             $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'] = array_sum(array_column($data['data'], 'finished_num'));
 
-         $data['dispatch_quantity'] = array_sum(array_column($data['data'], 'dispatch_quantity'));
 
-         $data['waste_quantity'] = array_sum(array_column($data['data'], 'waste_quantity'));
 
-         $data['not_finished_num'] = array_sum(array_column($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){
 
-         //数据校验以及填充
 
-         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'],
 
-                                 'team_id' => $d['team_id'],
 
-                                 'equipment_id' => $d['equipment_id']
 
-                             ]);
 
-                         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'],
 
-                                         'equipment_id' => $d['equipment_id'],
 
-                                         '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']
 
-                                 ];
 
-                             }
 
-                         }
 
-                     }
 
-                 }
 
-                 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;
 
-     }
 
- }
 
 
  |