| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 | 
							- <?php
 
- namespace App\Service;
 
- use App\Jobs\ProcessDataJob;
 
- 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\OrdersProductProcess;
 
- use App\Model\Process;
 
- use App\Model\SaleOrdersProduct;
 
- use App\Model\Scrapp;
 
- 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];
 
-         $user = [
 
-             'id' => $user['id'],
 
-             'operate_time' => time()
 
-         ];
 
-         $redis = [
 
-             'result' => $msg,
 
-             'data' => $data,
 
-         ];
 
-         $job = dispatch(new ProcessDataJob($redis,$user,1))->onQueue(ProcessDataJob::job_one);
 
-         if(! $job){
 
-             return [false,'任务没有进入队列!'];
 
-         }
 
-         //错误计数
 
-         Redis::hSet('order_failures', md5(json_encode($redis)), 0);
 
-         //标记进入队列的数据
 
-         DispatchSub::whereIn('id',$data['id'])->update([
 
-             'job_status' => 1,
 
-         ]);
 
-         return [true,'任务已进入队列!'];
 
-     }
 
-     public function addInJob($result,$data){
 
-         try{
 
-             date_default_timezone_set("PRC");
 
-             //用友数据插入------------
 
-             $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_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();
 
-                 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'];
 
-                 DispatchSub::where('id',$value['id'])->update([
 
-                     'finished_num' => $finished_num,
 
-                     'waste_num' => $waste[$key],
 
-                     'job_status' => 0
 
-                 ]);
 
-                 $insert_waste = [];
 
-                 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']
 
-                             ];
 
-                         }
 
-                     }
 
-                 }
 
-                 //工序表
 
-                 $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'])
 
-                     ->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);
 
-             }
 
-             //反写数量
 
-             $this->writeFinishedQuantity(array_column($result,'sale_orders_product_id'));
 
-             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不能为空!'];
 
-         date_default_timezone_set("PRC");
 
-         $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){
 
-         date_default_timezone_set("PRC");
 
-         // 格式化时间戳为年份和月份
 
-         $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($this->isEmpty($data,'id')) return [false,'请选择数据!'];
 
-         if($this->isEmpty($data,'quantity')) return [false,'请填写完工数量!'];
 
-         if($this->isEmpty($data,'finish_id') && $this->isEmpty($data,'team_id')) return [false,'人员和班组不能都为空!'];
 
-         $bool = DispatchSub::whereIn('id',$data['id'])->where('job_status',1)->exists();
 
-         if($bool) return [false,'正在队列中,请不要重复操作!'];
 
-         $result = DispatchSub::whereIn('id',$data['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')
 
-             ->orderBy('id','desc')
 
-             ->get()->toArray();
 
-         foreach ($result as $key => $value){
 
-             if(($data['quantity'][$key] + $value['finished_num']) > $value['dispatch_quantity']) return [false,'完工数量不能大于派工数量'];
 
-         }
 
-         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('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();
 
-         date_default_timezone_set("PRC");
 
-         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($sale_orders_product_id){
 
-         if(empty($sale_orders_product_id)) return;
 
-         $result = DispatchSub::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();
 
-         if(empty($result)) return;
 
-         foreach ($result as $key => $value){
 
-             SaleOrdersProduct::where('id',$key)->update([
 
-                 'finished_num' => $value
 
-             ]);
 
-         }
 
-     }
 
-     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()
 
-         ];
 
-         $redis = [
 
-             'result' => $msg,
 
-             'data' => $data,
 
-             'quantity_count' => $count_arr,
 
-         ];
 
-         $job = dispatch(new ProcessDataJob($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){
 
-         try{
 
-             //用友数据写入------------
 
-             date_default_timezone_set("PRC");
 
-             $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_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();
 
-                 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();
 
-             $time = time();
 
-             foreach ($result as $key => $value){
 
-                 $finished_num = $value['quantity'] + $value['finished_num'];
 
-                 DispatchSub::where('id',$value['id'])
 
-                     ->update([
 
-                         'finished_num' => $finished_num,
 
-                         'job_status' => 0
 
-                     ]);
 
-                 //工序
 
-                 $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
 
-                 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']
 
-                             ]);
 
-                     }
 
-                 }
 
-             }
 
-             //反写数量
 
-             $this->writeFinishedQuantity(array_column($result,'sale_orders_product_id'));
 
-             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 = [];
 
-         foreach ($data as $value){
 
-             if(empty($value['quantity'])) 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'];
 
-             }
 
-         }
 
-         $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')
 
-             ->orderBy('id','desc')
 
-             ->get()->toArray();
 
-         foreach ($result as $key => $value){
 
-             if(($post[$value['id']] + $value['finished_num']) > $value['dispatch_quantity']) return [false,"{$value['dispatch_no']}单号的完工数量不能大于派工数量",''];
 
-         }
 
-         return [true, $result, $post];
 
-     }
 
- }
 
 
  |