cqpCow 2 years ago
parent
commit
93c5093582

+ 32 - 17
app/Model/OrdersProductBom.php

@@ -13,40 +13,55 @@ use Illuminate\Support\Facades\Schema;
  */
 class OrdersProductBom extends Model
 {
-    protected  $table = "";
-    protected $prefix_table = 'orders_product_bom'; // 对照表
+    protected  $table = "orders_product_bom";
     const CREATED_AT = 'crt_time';
     const UPDATED_AT = 'upd_time';
     protected $dateFormat = 'U';
     //$fillable属性里面的字段是可以赋值的,其他的所有属性不能被赋值
     //$guarded属性里面的字段是不可以赋值,其他的所有属性都能被赋值
-    protected $fillable = ['param']; // $attributes 里传参数需要配置这个参数名字
+    protected $guarded = [];
+
+    protected $month = [
+        1 => '01_03',
+        2 => '01_03',
+        3 => '01_03',
+        4 => '04_06',
+        5 => '04_06',
+        6 => '04_06',
+        7 => '07_09',
+        8 => '07_09',
+        9 => '07_09',
+        10 => '10_12',
+        11 => '10_12',
+        12 => '10_12',
+    ];
 
     public function __construct(array $attributes = [])
     {
-        $param = request()->get('param');
-
-        if(! empty($param)) $this->setTableByParam($param);
-
-        if(isset($attributes['param']) && $attributes['param'] > 0) $this->setTableByParam($attributes['param']);
-
-        $this->createTable($this->table);
-
         parent::__construct($attributes);
+        if (isset($attributes['channel']) && $attributes['channel'] > 0) {
+            $channel = $attributes['channel'];
+            $month = (int)substr($channel,4,2);
+            $channel = substr($channel,0,4).$this->month[$month];
+            $this->setTableById($channel);
+        }
     }
 
-    public function setTableByParam($param){
-        if($param > 0){
-            $table = $this->prefix_table . '_' . $param;
-            $this->table = $table;
+    public function setTableById($channel)
+    {
+        if ($channel > 0) {
+            $tb = $this->table.'_' . (string)$channel;
+            $this->createTable($tb);
+            $this->setTable($tb);
         }
+
     }
 
     //创建表
     private function createTable($table){
-        if(! empty($table) && ! Schema::hasTable($table) && Schema::hasTable($this->prefix_table)){
+        if(! empty($table) && ! Schema::hasTable($table)){
             //执行建表语句
-            DB::statement('create table '. $table .' like ' . $this->prefix_table);
+            DB::statement('create table '. $table .' like orders_product_bom');
         }
     }
 }

+ 32 - 17
app/Model/OrdersProductProcess.php

@@ -13,40 +13,55 @@ use Illuminate\Support\Facades\Schema;
  */
 class OrdersProductProcess extends Model
 {
-    protected  $table = "";
-    protected $prefix_table = 'orders_product_process'; // 对照表
+    protected  $table = "orders_product_process";
     const CREATED_AT = 'crt_time';
     const UPDATED_AT = 'upd_time';
     protected $dateFormat = 'U';
     //$fillable属性里面的字段是可以赋值的,其他的所有属性不能被赋值
     //$guarded属性里面的字段是不可以赋值,其他的所有属性都能被赋值
-    protected $fillable = ['param']; // $attributes 里传参数需要配置这个参数名字
+    protected $guarded = [];
+
+    protected $month = [
+        1 => '01_03',
+        2 => '01_03',
+        3 => '01_03',
+        4 => '04_06',
+        5 => '04_06',
+        6 => '04_06',
+        7 => '07_09',
+        8 => '07_09',
+        9 => '07_09',
+        10 => '10_12',
+        11 => '10_12',
+        12 => '10_12',
+    ];
 
     public function __construct(array $attributes = [])
     {
-        $param = request()->get('param');
-
-        if(! empty($param)) $this->setTableByParam($param);
-
-        if(isset($attributes['param']) && $attributes['param'] > 0) $this->setTableByParam($attributes['param']);
-
-        $this->createTable($this->table);
-
         parent::__construct($attributes);
+        if (isset($attributes['channel']) && $attributes['channel'] > 0) {
+            $channel = $attributes['channel'];
+            $month = (int)substr($channel,4,2);
+            $channel = substr($channel,0,4).$this->month[$month];
+            $this->setTableById($channel);
+        }
     }
 
-    public function setTableByParam($param){
-        if($param > 0){
-            $table = $this->prefix_table . '_' . $param;
-            $this->table = $table;
+    public function setTableById($channel)
+    {
+        if ($channel > 0) {
+            $tb = $this->table.'_' . (string)$channel;
+            $this->createTable($tb);
+            $this->setTable($tb);
         }
+
     }
 
     //创建表
     private function createTable($table){
-        if(! empty($table) && ! Schema::hasTable($table) && Schema::hasTable($this->prefix_table)){
+        if(! empty($table) && ! Schema::hasTable($table)){
             //执行建表语句
-            DB::statement('create table '. $table .' like ' . $this->prefix_table);
+            DB::statement('create table '. $table .' like orders_product_process');
         }
     }
 }

+ 6 - 1
app/Service/FyyOrderService.php

@@ -87,7 +87,7 @@ class FyyOrderService extends Service
 
     public function orderList($data){
         $model = SaleOrdersProduct::where('del_time',0)
-            ->select('id','order_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','product_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man','out_checker_man','out_checker_time')
+            ->select('id','order_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','order_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man','out_checker_man','out_checker_time')
             ->orderBy('id','desc');
 
         if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
@@ -136,6 +136,9 @@ class FyyOrderService extends Service
             }
         }
 
+        //已生产数量
+        $production_map = (new ProductionOrderService())->getProductionOrderQuantity($data['data']);
+
         date_default_timezone_set("PRC");
         foreach ($data['data'] as $key => $value){
             $keys = $value['product_no'] . $value['technology_name'] . $value['wood_name'];
@@ -143,7 +146,9 @@ class FyyOrderService extends Service
             $data['data'][$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
             $data['data'][$key]['out_checker_time'] = $value['out_checker_time'] ? date('Y-m-d',$value['out_checker_time']) : '';
             $data['data'][$key]['product_quantity_on_hand'] = $detailMap[$keys] ?? 0;
+            $data['data'][$key]['production_quantity'] = $production_map[$value['order_no'] . $value['technology_name'] . $value['wood_name']] ?? 0;
         }
+        $data['order_quantity'] = array_sum(array_column($data['data'], 'order_quantity'));
 
         return $data;
     }

+ 4 - 4
app/Service/FyySqlServerService.php

@@ -27,7 +27,7 @@ class FyySqlServerService extends Service
                 'product_title' => '产品名称001',
                 'product_size' => '产品规格30c',
                 'product_unit' => '产品单位米',
-                'product_quantity' => 100,
+                'order_quantity' => 100,
                 'product_quantity_on_hand' => 50,
                 'technology_material' => '工艺材质',
                 'technology_name' => '工艺名称:同命',
@@ -48,7 +48,7 @@ class FyySqlServerService extends Service
                 'product_title' => '产品名称002',
                 'product_size' => '产品规格30d',
                 'product_unit' => '产品单位米',
-                'product_quantity' => 100,
+                'order_quantity' => 100,
                 'product_quantity_on_hand' => 50,
                 'technology_material' => '工艺材质',
                 'technology_name' => '工艺名称:同命',
@@ -69,7 +69,7 @@ class FyySqlServerService extends Service
                 'product_title' => '产品名称002',
                 'product_size' => '产品规格30d',
                 'product_unit' => '产品单位米',
-                'product_quantity' => 100,
+                'order_quantity' => 100,
                 'product_quantity_on_hand' => 50,
                 'technology_material' => '工艺材质',
                 'technology_name' => '工艺名称:同命',
@@ -95,7 +95,7 @@ class FyySqlServerService extends Service
         $model = $this->db->table('SO_SOMain as a')
             ->leftJoin('SO_SODetails as b','b.cSOCode','a.cSOCode')
             ->whereBetween('a.dDate',[$start, $end])
-            ->select('a.cSOCode as out_order_no','a.dDate as out_order_no_time','a.cCusCode as customer_no','a.cCusName as customer_name','a.cMemo as table_header_mark','a.cMaker as out_crt_man','a.cVerifier as out_checker_man','a.dverifydate as out_checker_time','b.cInvCode as product_no','b.iQuantity as product_quantity','b.cDefine28 as technology_material','b.cFree1 as technology_name','b.cFree2 as wood_name','b.cDefine30 as process_mark','b.cMemo as table_body_mark');
+            ->select('a.cSOCode as out_order_no','a.dDate as out_order_no_time','a.cCusCode as customer_no','a.cCusName as customer_name','a.cMemo as table_header_mark','a.cMaker as out_crt_man','a.cVerifier as out_checker_man','a.dverifydate as out_checker_time','b.cInvCode as product_no','b.iQuantity as order_quantity','b.cDefine28 as technology_material','b.cFree1 as technology_name','b.cFree2 as wood_name','b.cDefine30 as process_mark','b.cMemo as table_body_mark');
 
         if(! empty($data['out_order_no'])) $model->where('a.cSOCode', 'LIKE', '%'.$data['out_order_no'].'%');
         if(! empty($data['customer_no'])) $model->where('a.cCusCode', 'LIKE', '%'.$data['customer_no'].'%');

+ 6 - 0
app/Service/ProcessService.php

@@ -4,6 +4,7 @@ namespace App\Service;
 
 use App\Model\BasicProcess;
 use App\Model\Process;
+use App\Model\Technology;
 
 /**
  * 工序相关
@@ -129,6 +130,11 @@ class ProcessService extends Service
     public function processDel($data){
         if($this->isEmpty($data,'id')) return [false,'ID必须!'];
 
+        $bool = Technology::where('del_time',0)
+            ->whereRaw("find_in_set('{$data['id']}',process_id)")
+            ->exists();
+        if($bool) return [false, '工序已经绑定工艺路线!'];
+
         Process::where('id',$data['id'])->update([
             'del_time' => time()
         ]);

+ 87 - 26
app/Service/ProductionOrderService.php

@@ -7,6 +7,7 @@ use App\Model\OrdersProductBom;
 use App\Model\OrdersProductProcess;
 use App\Model\Process;
 use App\Model\SaleOrdersProduct;
+use App\Model\Technology;
 use Illuminate\Support\Facades\DB;
 
 /**
@@ -47,27 +48,31 @@ class ProductionOrderService extends Service
 
         $production_no = $this->setOrderNO();
 
-        //工序
-        $process = Process::where('del_time',0)
+        //工艺 -> 工
+        $technology = Technology::where('del_time',0)
             ->orderBy('id','desc')
             ->first();
-        if(empty($process)) return [false,'工序不存在!'];
+        if(empty($technology)) return [false,'工艺不存在!'];
+        $process_arr = explode(',',$technology->process_id);
 
         try{
             DB::beginTransaction();
 
-            $result = SaleOrdersProduct::whereIn('id',$data['id'])
-                ->select('order_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','product_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man','out_checker_man','out_checker_time')->get()->toArray();
+            //生产数据的源数据
+            $result = $msg;
 
             $boom = $process = [];
             $time = time();
             foreach ($result as $key => $value){
+                $quantity_tmp = $data['quantity'][$key];
+
                 $result[$key]['production_no'] = $production_no;
-                $result[$key]['production_quantity'] = $data['quantity'][$key];
+                $result[$key]['production_quantity'] = $quantity_tmp;
                 $result[$key]['production_time'] = $time;
 
-                foreach ($data['quantity'][$key] as $v){
+                for ($i = 1; $i <= $quantity_tmp; $i++) {
                     $boom[$value['order_no']][] = [
+                        'production_no' => $production_no,
                         'order_no' => $value['order_no'],
                         'out_order_no' => $value['out_order_no'],
                         'product_no' => $value['product_no'],
@@ -75,29 +80,31 @@ class ProductionOrderService extends Service
                         'crt_time' => $time
                     ];
 
-                    $process[$value['order_no']][] = [
-                        'process_id' => $process->id,
-                        '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 $v){
+                        $process[$value['order_no']][] = [
+                            '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
+                        ];
+                    }
                 }
-
             }
 
             OrdersProduct::insert($result);
 
             foreach ($boom as $key => $value){
-                $boom = new OrdersProductBom(['param' => $key]);
-                $boom->insert($value);
-            }
+                $boom_model = new OrdersProductBom(['channel'=> $key]);
+                $boom_model->insert($value);
+            }unset($boom);
 
             foreach ($process as $key => $value){
-                $boom = new OrdersProductProcess(['param' => $key]);
-                $boom->insert($value);
-            }
+                $process_model = new OrdersProductProcess(['channel' => $key]);
+                $process_model->insert($value);
+            }unset($process);
 
             DB::commit();
         }catch (\Exception $e){
@@ -120,11 +127,12 @@ class ProductionOrderService extends Service
 
     public function orderList($data){
         $model = OrdersProduct::where('del_time',0)
-            ->select('id','order_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','product_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man','out_checker_man','out_checker_time','production_quantity','production_quantity','production_no','status')
+            ->select('id','order_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','order_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man','out_checker_man','out_checker_time','production_quantity','production_time','production_no','status')
             ->orderBy('id','desc');
 
         if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
         if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
+        if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
         if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
         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'].'%');
@@ -135,19 +143,72 @@ class ProductionOrderService extends Service
         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['out_checker_man'])) $model->where('out_checker_man', 'LIKE', '%'.$data['out_checker_man'].'%');
-        if(! empty($data['out_checker_time'])) $model->where('out_checker_time', $data['out_checker_time']);
         if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
-        if(! empty($data['out_order_no_time'])) $model->where('out_order_no_time', $data['out_order_no_time']);
+        if(! empty($data['out_checker_time'][0]) && ! empty($data['out_checker_time'][1])) $model->whereBetween('out_checker_time',[$data['out_checker_time'][0],$data['out_checker_time'][1]]);
+        if(! empty($data['out_order_no_time'][0]) && ! empty($data['out_order_no_time'][1])) $model->whereBetween('out_order_no_time',[$data['out_order_no_time'][0],$data['out_order_no_time'][1]]);
+        if(! empty($data['production_time'][0]) && ! empty($data['production_time'][1])) $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
         if(isset($data['status'])) $model->where('status',$data['status']);
 
         $list = $this->limit($model,'',$data);
+        $list = $this->fillData($list);
 
         return [200,$list];
     }
 
     public function orderRule($data){
         if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
+        if($this->isEmpty($data,'quantity')) return [false,'数量不能为空!'];
+        if(in_array(false, $data['quantity'], true) || in_array(0, $data['quantity'], true) || in_array('', $data['quantity'], true))return [false,'数量不能为空!'];
+
+        $result = SaleOrdersProduct::whereIn('id',$data['id'])
+            ->select('order_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','order_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man','out_checker_man','out_checker_time')->get()->toArray();
+        $map = $this->getProductionOrderQuantity($result);
+
+        foreach ($result as $key => $value){
+            $keys = $value['order_no'] . $value['technology_name'] . $value['wood_name'];
+            if(isset($map[$keys])){
+                if($map[$keys] + $data['quantity'][$key] > $value['order_quantity']) return [false,'生产数量不能大于订单数量'];
+            }else{
+                if($data['quantity'][$key] > $value['order_quantity']) return [false,'生产数量不能大于订单数量'];
+            }
+        }
+
+        return [true, $result];
+    }
+
+    public function fillData($data){
+        if(empty($data['data'])) return $data;
+
+        date_default_timezone_set("PRC");
+        foreach ($data['data'] as $key => $value){
+            $data['data'][$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
+            $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']) : '';
+        }
+
+        return $data;
+    }
+
+    //返回已生产数量
+    public function getProductionOrderQuantity($data){
+        if(empty($data)) return [];
+
+        $args = '';
+        foreach ($data as $value){
+            $args .= "(order_no = '{$value['order_no']}' and technology_name = '{$value['technology_name']}' and wood_name = '{$value['wood_name']}') OR ";
+        }
+        $args = rtrim($args,'OR ');
+        $result = OrdersProduct::where('del_time',0)
+            ->whereRaw("($args)")
+            ->select(DB::raw("sum(production_quantity) as production_quantity"),'order_no','technology_name','wood_name')
+            ->groupby('order_no','technology_name','wood_name')
+            ->get()->toArray();
+
+        $map = [];
+        foreach ($result as $value){
+            $map[$value['order_no'] . $value['technology_name'] . $value['wood_name']] = $value['production_quantity'];
+        }
 
-        return [true,''];
+        return $map;
     }
 }