| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 | 
							- <?php
 
- namespace App\Service;
 
- use App\Model\CommandList;
 
- use App\Model\OrdersProduct;
 
- use App\Model\OrdersProductBom;
 
- use App\Model\OrdersProductMain;
 
- use App\Model\OrdersProductProcess;
 
- use Illuminate\Support\Facades\DB;
 
- class CommandService extends Service
 
- {
 
-     public function getForSettle(){
 
-         $command = CommandList::where('del_time',0)
 
-             ->where('is_use',0)
 
-             ->first();
 
-         if(! $command) return;
 
-         $command->is_use = 1;
 
-         $command->save();
 
-         if($command->function == "productionAdd"){
 
-             $args =  json_decode($command->data, true);
 
-             $this->productionAdd($args['data'], $args['user'],$command->id);
 
-         }
 
- //        if($command->function == "productionDel"){
 
- //            $args =  json_decode($command->data, true);
 
- //            $this->productionDel($args['data'],$command->id);
 
- //        }
 
-     }
 
-     public function productionAdd($data,$user,$id){
 
-         $service = new ProductionOrderService();
 
-         list($status,$msg) = $service->orderRule($data);
 
-         //生产数据的源数据
 
-         $result = $msg[0];
 
-         $quantity_map = $msg[1];
 
-         if(! $status) {
 
-             CommandList::where('id', $id)
 
-                 ->update([
 
-                     'mark' => $msg,
 
-                     'del_time' => 1,
 
-                 ]);
 
-             (new ProductionOrderService())->delBoxLock();
 
-             return;
 
-         }
 
-         $production_no = $service->setOrderNO();
 
-         if(empty($production_no)) {
 
-             CommandList::where('id', $id)
 
-                 ->update([
 
-                     'mark' => '单据号生成失败',
 
-                     'del_time' => 1,
 
-                 ]);
 
-             (new ProductionOrderService())->delBoxLock();
 
-             return;
 
-         }
 
-         //工序
 
-         $process_arr = $data['process_id'];
 
-         try{
 
-             DB::beginTransaction();
 
-             $time = time();
 
-             //主表数据写入
 
-             OrdersProductMain::insert(['production_no' => $production_no,'crt_time' => $time,'crt_id' => $user['id'], 'process_id' => implode(',',$process_arr)]);
 
-             //是否自动审核
 
-             $em = new EmployeeService();
 
-             $auto = $em->is_auto($user, "sc_auto");
 
-             $boom = $process = $other_map = [];
 
-             foreach ($result as $key => $value){
 
-                 $not_production_num = bcsub($value['order_quantity'], $value['production_quantity'], 3);
 
-                 $production_num = $quantity_map[$value['sale_orders_product_id']];
 
-                 if($production_num > $not_production_num) $other_map[$value['sale_orders_product_id']] = bcsub($production_num, $not_production_num, 3) * 1000;
 
-                 $result[$key]['process_id'] = implode(',', $process_arr);
 
-                 $result[$key]['production_no'] = $production_no;
 
-                 $result[$key]['production_quantity'] = $production_num;
 
-                 $result[$key]['production_time'] = $time;
 
-                 $result[$key]['crt_id'] = $user['id'];
 
-                 $result[$key]['status'] = $auto;
 
-             }
 
-             OrdersProduct::insert($result);
 
-             //反写销售订单中 已经生产数量
 
-             $service->writeProductionQuantity($data['id']);
 
-             //获取上一次插入订单的所有id
 
-             $last_insert_id = OrdersProduct::where('production_no',$production_no)->select('id')->get()->toArray();
 
-             $last_insert_id = array_column($last_insert_id,'id');
 
-             $size = 0;
 
-             //组织process bom的数据 写入与主表的关联id
 
-             $time_arr = [];
 
-             foreach ($result as $key => $value){
 
-                 $quantity_tmp = $quantity_map[$value['sale_orders_product_id']];
 
-                 $quantity_tmp = $quantity_tmp * 1000;
 
-                 $time_tmp = date("Ymd", $value['out_order_no_time']);
 
-                 if(! in_array($time_tmp,$time_arr)) $time_arr[] = $time_tmp;
 
-                 $count = $other_map[$value['sale_orders_product_id']] ?? 0;
 
-                 for ($i = 1; $i <= $quantity_tmp; $i++) {
 
-                     if($size == 100){
 
-                         $this->batchInsertData($boom,$process,$time_arr);
 
-                         unset($boom);unset($process);
 
-                         $size = 0;
 
-                         echo "100条写入成功";
 
-                     }
 
-                     $boom[$time_tmp][] = [
 
-                         'order_product_id' => $last_insert_id[$key],
 
-                         'production_no' => $production_no,
 
-                         'order_no' => $value['order_no'],
 
-                         'out_order_no' => $value['out_order_no'],
 
-                         'product_no' => $value['product_no'],
 
-                         'product_title' => $value['product_title'],
 
-                         'crt_time' => $time
 
-                     ];
 
-                     $is_more_production = 0;
 
-                     if($count>= $i) $is_more_production = 1;
 
-                     foreach ($process_arr as $k => $v){
 
-                         $process[$time_tmp][] = [
 
-                             'order_product_id' => $last_insert_id[$key],
 
-                             'production_no' => $production_no,
 
-                             'process_id' => $v,
 
-                             'order_no' => $value['order_no'],
 
-                             'out_order_no' => $value['out_order_no'],
 
-                             'product_no' => $value['product_no'],
 
-                             'product_title' => $value['product_title'],
 
-                             'crt_time' => $time,
 
-                             'sort' => $k,
 
-                             'is_more_production' => $is_more_production
 
-                         ];
 
-                     }
 
-                     $size ++;
 
-                 }
 
-             }
 
-             if($size > 0){
 
-                 $this->batchInsertData($boom, $process, $time_arr);
 
-                 echo "剩余 " . $size . " 条写入成功\n";
 
-             }
 
-             CommandList::where('id', $id)
 
-                 ->update([
 
-                     'mark' => '',
 
-                     'del_time' => time(),
 
-                     'is_use' => 2
 
-                 ]);
 
-             (new ProductionOrderService())->delBoxLock();
 
-             DB::commit();
 
-         }catch (\Exception $e){
 
-             (new ProductionOrderService())->delBoxLock();
 
-             DB::rollBack();
 
-             CommandList::where('id', $id)
 
-                 ->update([
 
-                     'mark' => $e->getLine().':'.$e->getMessage(),
 
-                     'del_time' => 1,
 
-                 ]);
 
-         }
 
-     }
 
-     function delList($result){
 
-         if(empty($result)) return;
 
-         foreach ($result as $value){
 
-             $key = "productionAdd" . $value['sale_orders_product_id'];
 
-             $this->dellimitingSendRequestBackgNeed($key);
 
-         }
 
-     }
 
-     function batchInsertData(array $boom, array $process, array $time_arr)
 
-     {
 
-         foreach ($time_arr as $t) {
 
-             if (isset($boom[$t]) && count($boom[$t]) > 0) {
 
-                 $boom_model = new OrdersProductBom(['channel'=> $t]);
 
-                 $boom_model->insert($boom[$t]);
 
-             }
 
-             if (isset($process[$t]) && count($process[$t]) > 0) {
 
-                 $process_model = new OrdersProductProcess(['channel' => $t]);
 
-                 $process_model->insert($process[$t]);
 
-             }
 
-         }
 
-     }
 
-     public function productionDel($data,$command_id){
 
-         $id = $data['id'];
 
-         try {
 
-             DB::beginTransaction();
 
-             //生产订单
 
-             OrdersProduct::whereIn('id',$id)->update([
 
-                 'del_time' => time()
 
-             ]);
 
-             //生产订单主表
 
-             $subquery = DB::table('orders_product')
 
-                 ->select('production_no')
 
-                 ->whereIn('id',$id);
 
-             $order_no = DB::table('orders_product')
 
-                 ->select('orders_product.production_no','orders_product.del_time')
 
-                 ->joinSub($subquery, 'sub', function ($join) {
 
-                     $join->on('orders_product.production_no', '=', 'sub.production_no');
 
-                 })->get()->toArray();
 
-             $update_order = [];
 
-             if(! empty($order_no)){
 
-                 $tmp = [];
 
-                 foreach ($order_no as $value){
 
-                     $value = (array)$value;
 
-                     if($value['del_time'] == 0){
 
-                         $tmp[] = $value['production_no'];
 
-                     }else{
 
-                         $update_order[] = $value['production_no'];
 
-                     }
 
-                 }
 
-                 $tmp = array_unique($tmp);
 
-                 $update_order = array_unique($update_order);
 
-                 $update_order = array_diff($update_order, $tmp);
 
-             }
 
-             if(! empty($update_order)) OrdersProductMain::whereIn('production_no',$update_order)->update(['del_time' => time()]);
 
-             //生产订单子表
 
-             $message = OrdersProduct::whereIn('id',$id)->select('out_order_no_time','sale_orders_product_id')->get()->toArray();
 
-             //销售订单里的已生产数量
 
-             (new ProductionOrderService())->writeProductionQuantityDEL(array_column($message,'sale_orders_product_id'));
 
-             $time = array_unique(array_column($message,'out_order_no_time'));
 
-             $arr_time = [];
 
-             if(! empty($time)){
 
-                 foreach ($time as $value){
 
-                     $time_tmp = date("Ymd", $value);
 
-                     if(! in_array($time_tmp,$arr_time)) $arr_time[] = $time_tmp;
 
-                 }
 
-             }
 
-             if(! empty($arr_time)){
 
-                 foreach ($arr_time as $value){
 
-                     $modelBom = new OrdersProductBom(['channel'=> $value]);
 
-                     $modelBom->whereIn('order_product_id',$id)->update(['del_time' => time()]);
 
-                     $modelProcess = new OrdersProductProcess(['channel' => $value]);
 
-                     $modelProcess->whereIn('order_product_id',$id)->update(['del_time' => time()]);
 
-                 }
 
-             }
 
-             DB::commit();
 
-         }catch (\Throwable $e){
 
-             DB::rollBack();
 
-             CommandList::where('id', $command_id)
 
-                 ->update([
 
-                     'mark' => $e->getLine().':'.$e->getMessage(),
 
-                     'del_time' => 1,
 
-                 ]);
 
-         }
 
-     }
 
- }
 
 
  |