chenqp 1 tahun lalu
induk
melakukan
c47e2455bb

+ 47 - 0
app/Console/Commands/settleCommand.php

@@ -0,0 +1,47 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Service\CloudDataService;
+use App\Service\CommandService;
+use Illuminate\Console\Command;
+
+class settleCommand extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'command:settle_data';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        echo 'start---------------' . date("Y-m-d H:i:s");
+        $service = new CommandService();
+        $service->getForSettle();
+        echo 'end-------------' . date("Y-m-d H:i:s");
+    }
+}

+ 14 - 0
app/Model/CommandList.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+class CommandList extends Model
+{
+    protected $table = "command_list"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+
+}

+ 8 - 3
app/Service/Box/BoxService.php

@@ -83,7 +83,7 @@ class BoxService extends Service
                 $total = $num_list['total'];
                 $detail = $num_list['detail'];
                 $team_id = $num_list['team_id'];
-                $un_box_num = $v['dispatch_complete_quantity'] - $v['box_num'];
+                $un_box_num = $v['dispatch_complete_quantity'] - $v['box_num'];//todo
                 if ($total > $un_box_num) return [false, $v['product_title'] . '数量不足'];
 
                 $ext_1 = $v['product_no'];//产品编号
@@ -134,7 +134,7 @@ class BoxService extends Service
                 $detail = $num_list['detail'];
                 $team_id = $num_list['team_id'];
                 $box_detail = new BoxDetail(['channel'=>$top_order_no]);
-                $un_box_num = $v['order_quantity'] - $v['box_num'] - $box_detail->where('top_id',$v['id'])->where('del_time',0)->where('box_type',1)->sum('num');
+                $un_box_num = $v['order_quantity'] - $v['box_num'] - $box_detail->where('top_id',$v['id'])->where('del_time',0)->where('box_type',1)->sum('num');//todo
                 if ($total > $un_box_num) return [false, $v['product_title'] . '数量不足'];
 
                 $ext_1 = $v['product_no'];//产品编号
@@ -303,7 +303,7 @@ class BoxService extends Service
             $tmp_pro = [];
             foreach ($tmp['product'] as $t){
                 $t_k = $t['cinvcode'] . $t['cfree1'];
-                $tmp_pro[$t_k] = $t['iquantity'] - $t['out_quantity'];//未发货数量
+                $tmp_pro[$t_k] = bcsub($t['iquantity'] , $t['out_quantity'],3);//未发货数量
             }
             if(empty($value['product'])) return [false, '发货产品不能为空!'];
             foreach ($value['product'] as $p){
@@ -707,6 +707,9 @@ class BoxService extends Service
         $detail_list = $model->where('del_time',0)
             ->where('out_order_no',$data['out_order_no'])
             ->get()->toArray();
+        $map = Box::whereIn('order_no',array_unique(array_column($detail_list,'order_no')))
+            ->pluck('ext_1','order_no')
+            ->toArray();
 
         $return = [];
         foreach ($detail_list as $value){
@@ -716,12 +719,14 @@ class BoxService extends Service
                 $return[$key]['num'] += $value['num'];
             }else{
                 $return[$key] = [
+                    'box_no' => $value['order_no'],
                     'out_order_no' =>  $value['out_order_no'],
                     'ext_8' =>  $value['ext_8'],
                     'ext_3' =>  $value['ext_3'],
                     'num' =>  $value['num'],
                     'crt_time' => date("Y-m-d",$value['crt_time']),
                     'top_order_no' => $value['top_order_no'],
+                    'customer_name' => $map[$value['order_no']] ?? "",
                 ];
             }
         }

+ 245 - 0
app/Service/CommandService.php

@@ -0,0 +1,245 @@
+<?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);
+        if(! $status) {
+            CommandList::where('id', $id)
+                ->update([
+                    'mark' => $msg,
+                    'del_time' => 1,
+                ]);
+            return;
+        }
+
+        $production_no = $service->setOrderNO();
+        if(empty($production_no)) {
+            CommandList::where('id', $id)
+                ->update([
+                    'mark' => '单据号生成失败',
+                    'del_time' => 1,
+                ]);
+            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)]);
+
+            //生产数据的源数据
+            $result = $msg[0];
+            $quantity_map = $msg[1];
+
+            $boom = $process = [];
+
+            foreach ($result as $key => $value){
+                $result[$key]['process_id'] = implode(',', $process_arr);
+                $result[$key]['production_no'] = $production_no;
+                $result[$key]['production_quantity'] = $quantity_map[$value['sale_orders_product_id']];
+                $result[$key]['production_time'] = $time;
+                $result[$key]['crt_id'] = $user['id'];
+            }
+            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;
+
+                for ($i = 1; $i <= $quantity_tmp; $i++) {
+                    if($size == 500){
+                        $this->batchInsertData($boom,$process,$time_arr);
+                        unset($boom);unset($process);
+                        $size = 0;
+                        echo "500条写入成功";
+                    }
+
+                    $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
+                    ];
+                    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,
+                        ];
+                    }
+                    $size ++;
+                }
+            }
+
+            if($size > 0){
+                $this->batchInsertData($boom, $process, $time_arr);
+                echo "剩余 " . $size . " 条写入成功\n";
+            }
+
+            foreach ($result as $value){
+                $key = "productionAdd" . $value['sale_orders_product_id'];
+                $this->dellimitingSendRequestBackgNeed($key);
+            }
+
+            CommandList::where('id', $id)
+                ->update([
+                    'mark' => '',
+                    'del_time' => time(),
+                    'is_use' => 2
+                ]);
+
+            DB::commit();
+        }catch (\Exception $e){
+            DB::rollBack();
+            CommandList::where('id', $id)
+                ->update([
+                    'mark' => $e->getLine().':'.$e->getMessage(),
+                    'del_time' => 1,
+                ]);
+        }
+    }
+
+    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,
+                ]);
+        }
+    }
+}

+ 12 - 0
app/Service/DeleteOrderService.php

@@ -2,6 +2,7 @@
 
 namespace App\Service;
 
+use App\Model\CommandList;
 use App\Model\Dispatch;
 use App\Model\DispatchEmpSub;
 use App\Model\DispatchSub;
@@ -98,6 +99,17 @@ class DeleteOrderService extends Service
             ->exists();
         if($bool) return [false,'生产订单已生成派工单,删除失败!'];
 
+//        $insert_data = [
+//            'data' => ['id' => $id],
+//        ];
+//        CommandList::insert([
+//            'data' => json_encode($insert_data),
+//            'function' => 'productionDel'
+//        ]);
+//        return [true, '生成订单数据后台删除中......'];
+//
+//        //以下用不到-----------------------------
+//        return;
         try {
             DB::beginTransaction();
 

+ 3 - 2
app/Service/DispatchService.php

@@ -166,10 +166,11 @@ class DispatchService extends Service
             $result[$key]['crt_time'] = $time;
             $result[$key]['crt_id'] = $user['id'];
 
+            $dispatch_quantity = $value['dispatch_quantity'] * 1000;
             $process_model->where('order_product_id',$value['order_product_id'])
                 ->where('process_id',$value['process_id'])
                 ->where('dispatch_no','')
-                ->take($value['dispatch_quantity'])
+                ->take($dispatch_quantity)
                 ->update([
                     'dispatch_no' => $dispatch_no,
                     'status' => 1
@@ -500,7 +501,7 @@ class DispatchService extends Service
             $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'] = $value['dispatch_quantity'] - $value['finished_num'];
+            $data['data'][$key]['un_finished_quantity'] = bcsub($value['dispatch_quantity'] , $value['finished_num'],3);
         }
         $data['dispatch_quantity'] = array_sum(array_column($data['data'], 'dispatch_quantity'));
 

+ 6 - 3
app/Service/FinishedOrderService.php

@@ -71,7 +71,8 @@ class FinishedOrderService extends Service
                 $insert_waste = $scrapp = [];
                 if(! empty($value['waste_array'])){
                     foreach ($value['waste_array'] as $v){
-                        for($i = 0 ;$i < $v['num']; $i++){
+                        $num = $v['num'] * 1000;
+                        for($i = 0 ;$i < $num; $i++){
                             $insert_waste[] = [
                                 'order_product_id' => $value['order_product_id'],
                                 'order_no' => $value['order_no'],
@@ -112,12 +113,14 @@ class FinishedOrderService extends Service
                     }
                 }
 
+                $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'])
-                    ->take($value['quantity'])
+                    ->where('status', 1)
+                    ->take($quantity)
                     ->update([
                         'finished_time' => $time,
                         'status' => 2,
@@ -471,7 +474,7 @@ class FinishedOrderService extends Service
             $result[$key]['waste_quantity'] = $waste_map[$value['id']] ?? 0;
             $result[$key]['waste_array'] = $detail2['waste'] ?? [];
             $q = $detail_map[$value['id']] ?? 0;
-            $quantity_tmp = $q + $value['finished_num'];
+            $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 .'完工数量不能大于派工数量'];

+ 23 - 2
app/Service/ProductionOrderService.php

@@ -3,6 +3,7 @@
 namespace App\Service;
 
 use App\Exports\Exports;
+use App\Model\CommandList;
 use App\Model\OrdersProduct;
 use App\Model\OrdersProductBom;
 use App\Model\OrdersProductMain;
@@ -49,9 +50,28 @@ class ProductionOrderService extends Service
         list($status,$msg) = $this->orderRule($data);
         if(!$status) return [$status,$msg];
 
+        //生成键值限制
+        foreach ($msg[0] as $value){
+            $key = "productionAdd" . $value['sale_orders_product_id'];
+            list($status,$msg) = $this->limitingSendRequestBackgNeed($key);
+            if(! $status) return [false, '销售订单正在生成生产订单数据,请稍后尝试!'];
+        }
+
+        $insert_data = [
+            'data' => $data,
+            'user' => $user
+        ];
+        CommandList::insert([
+            'data' => json_encode($insert_data),
+            'function' => 'productionAdd'
+        ]);
+        return [true, '生成订单数据后台生成中......'];
+
+        //以下用不到-----------------------------
+        return;
         $production_no = $this->setOrderNO();
         if(empty($production_no)) return [false,'单据号生成失败!'];
-        
+
         //工序
         $process_arr = $data['process_id'];
 
@@ -137,6 +157,7 @@ class ProductionOrderService extends Service
         }
 
         return [true, ''];
+        //以下用不到-----------------------------
     }
 
     public function del($data){
@@ -231,7 +252,7 @@ class ProductionOrderService extends Service
             $data['data'][$key]['out_checker_time'] = $value['out_checker_time'] ? date('Y-m-d',$value['out_checker_time']) : '';
             $data['data'][$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
             $data['data'][$key]['pre_shipment_time'] = $value['pre_shipment_time'] ? date('Y-m-d',$value['pre_shipment_time']) : '';
-            $data['data'][$key]['not_production'] = $value['order_quantity'] - ($production_map[$value['sale_orders_product_id']] ?? 0);
+            $data['data'][$key]['not_production'] = bcsub($value['order_quantity'] ,($production_map[$value['sale_orders_product_id']] ?? 0),3);
             if($value['dispatch_complete_quantity'] >= $value['production_quantity']){
                 $data['data'][$key]['is_create'] = 1;
             }else{

+ 14 - 0
app/Service/Service.php

@@ -307,4 +307,18 @@ dd(filter_var($ip, FILTER_VALIDATE_IP) === false);
 
         return false;  // 连接失败
     }
+
+    //后台端 某些需要限制请求频率的接口
+    //需要主动删除  Redis::del($key)
+    public function limitingSendRequestBackgNeed($key,$value=0){
+        if(! empty($value)) $value = 1;
+        // 使用Redis Facade设置,当键名不存在时才设置成功
+        if (Redis::setnx($key, $value)) return [true, ''];
+
+        return [false,'操作频繁!'];
+    }
+
+    public function dellimitingSendRequestBackgNeed($key){
+        Redis::del($key);
+    }
 }