cqpCow 2 gadi atpakaļ
vecāks
revīzija
a690b726d0

+ 77 - 0
app/Http/Controllers/Api/FinishedOrderController.php

@@ -0,0 +1,77 @@
+<?php
+
+namespace App\Http\Controllers\Api;
+
+
+use App\Service\FinishedOrderService;
+use Illuminate\Http\Request;
+
+
+class FinishedOrderController extends BaseController
+{
+    public function add(Request $request)
+    {
+        $service = new FinishedOrderService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->add($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function edit(Request $request)
+    {
+        $service = new FinishedOrderService();
+        $user = $request->get('auth');
+        list($status,$data) = $service->edit($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function del(Request $request)
+    {
+        $service = new FinishedOrderService();
+        $user = $request->get('auth');
+        list($status,$data) = $service->del($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function orderList(Request $request)
+    {
+        $service = new FinishedOrderService();
+        $user = $request->get('auth');
+        list($status,$data) = $service->orderList($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+
+    public function orderDetail(Request $request)
+    {
+        $service = new FinishedOrderService();
+        $user = $request->get('auth');
+        list($status,$data) = $service->orderDetail($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+}

+ 14 - 0
app/Model/FinishedOrder.php

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

+ 18 - 0
app/Model/FinishedOrderSub.php

@@ -0,0 +1,18 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+/**
+ *
+ * Class Unit
+ * @package App\Models
+ */
+class FinishedOrderSub extends Model
+{
+    protected $table = "finished_order_sub"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+}

+ 16 - 8
app/Service/DispatchService.php

@@ -52,11 +52,13 @@ class DispatchService extends Service
             //主表
             Dispatch::insert(['dispatch_no' => $dispatch_no,'process_id' => $data['process_id'],'equipment_id' => $data['equipment_id'], 'team_id' => $data['team_id'],'dispatch_time_start' => $data['dispatch_time'][0],'dispatch_time_end' => $data['dispatch_time'][1], 'crt_time' => time()]);
 
-            date_default_timezone_set("PRC");
-            $process_model = new OrdersProductProcess(['channel' => date('Ymd',$data['out_order_no_time'][0])]);
-
             //生产数据的源数据
             $result = $msg;
+
+            date_default_timezone_set("PRC");
+            $time_tmp = date("Ymd", $data['out_order_no_time'][0]);
+            $process_model = new OrdersProductProcess(['channel' => $time_tmp]);
+
             $time = time();
             foreach ($result as $key => $value){
                 $quantity_tmp = $data['quantity'][$key];
@@ -74,7 +76,10 @@ class DispatchService extends Service
                     ->where('process_id',$data['process_id'])
                     ->where('dispatch_no','')
                     ->take($quantity_tmp)
-                    ->update(['dispatch_no' => $dispatch_no]);
+                    ->update([
+                        'dispatch_no' => $dispatch_no,
+                        'status' => 1
+                    ]);
             }
 
             DispatchSub::insert($result);
@@ -121,7 +126,7 @@ class DispatchService extends Service
 
         $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','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','crt_id')
-            ->whereBetween('out_order_no_time',[$data['out_order_no_time'][0],$data['out_order_no_time'][1]])
+//            ->whereBetween('out_order_no_time',[$data['out_order_no_time'][0],$data['out_order_no_time'][1]])
             ->whereIn('id',$msg)
             ->orderBy('id','desc');
 
@@ -156,7 +161,8 @@ class DispatchService extends Service
         if(! $bool) return [false,'制单日期必须同月!'];
 
         date_default_timezone_set("PRC");
-        $process_model = new OrdersProductProcess(['process' => date('Ymd',$data['out_order_no_time'][0])]);
+        $time = date("Ymd",$data['out_order_no_time'][0]);
+        $process_model = new OrdersProductProcess(['channel' => $time]);
         if(! $process_model->is_table_isset()) return [true, []];//不存在process子表 返回空数据
         $process_array = $process_model->where('del_time',0)
             ->where('process_id',$data['process_id'])
@@ -172,13 +178,15 @@ class DispatchService extends Service
     public function orderRule($data){
         if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
         if($this->isEmpty($data,'quantity')) return [false,'请填写数量!'];
-        if(empty($data['dispatch_time'][0]) || empty($data['dispatch_time'][1])) return [false,'计划生产时间!'];
+        if(in_array(false, $data['quantity'], true) || in_array(0, $data['quantity'], true) || in_array('', $data['quantity'], true))return [false,'数量不能为空!'];
+        if(empty($data['dispatch_time'][0]) || empty($data['dispatch_time'][1])) return [false,'计划生产时间不能为空!'];
+        if(empty($data['out_order_no_time'][0]) || empty($data['out_order_no_time'][1])) return [false,'制单时间不能为空!'];
         if($this->isEmpty($data,'process_id')) return [false,'工序不能为空!'];
         if($this->isEmpty($data,'equipment_id')) return [false,'设备不能为空!'];
         if($this->isEmpty($data,'team_id')) return [false,'班组不能为空!'];
 
         $result = OrdersProduct::whereIn('id',$data['id'])
-            ->select('id as order_product_id','order_no','table_header_mark','product_no','product_title','product_size','product_unit','production_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark')
+            ->select('id as order_product_id','sale_orders_product_id','order_no','table_header_mark','product_no','product_title','product_size','product_unit','production_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','sale_orders_product_id','out_order_no_time')
             ->get()->toArray();
 
         //已派工数据

+ 256 - 0
app/Service/FinishedOrderService.php

@@ -0,0 +1,256 @@
+<?php
+
+namespace App\Service;
+
+use App\Model\Dispatch;
+use App\Model\DispatchSub;
+use App\Model\Employee;
+use App\Model\EmployeeTeamPermission;
+use App\Model\Equipment;
+use App\Model\FinishedOrder;
+use App\Model\FinishedOrderSub;
+use App\Model\OrdersProduct;
+use App\Model\OrdersProductProcess;
+use App\Model\Process;
+use App\Model\SaleOrdersProduct;
+use App\Model\Team;
+use Illuminate\Support\Facades\DB;
+
+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];
+
+        $finished_no = $this->setOrderNO();
+        try{
+            DB::beginTransaction();
+
+            //主表
+            FinishedOrder::insert(['finished_no' => $finished_no, 'crt_time' => time()]);
+
+            //生产数据的源数据
+            $result = $msg;
+            $time = time();
+
+            date_default_timezone_set("PRC");
+            foreach ($result as $key => $value){
+                $quantity_tmp = $data['quantity'][$key];
+                $quantity_tmp2 = $data['waste_quantity'][$key] ?? 0;
+                $scrapp = $data['scrapp'][$key] ?? 0;
+
+                $result[$key]['finished_no'] = $finished_no;
+                $result[$key]['finished_num'] = $quantity_tmp;
+                $result[$key]['waste_quantity'] = $quantity_tmp2;
+                $result[$key]['scrapp_id'] = $scrapp;
+                $result[$key]['crt_time'] = $time;
+                $result[$key]['crt_id'] = $user['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',$data['process_id'])
+                    ->where('dispatch_no',$value['dispatch_no'])
+                    ->take($quantity_tmp)
+                    ->update([
+                        'finished_no' => $finished_no,
+                        'status' => 2
+                    ]);
+
+                unset($result[$key]['order_product_id']);
+                unset($result[$key]['dispatch_no']);
+            }
+
+            FinishedOrderSub::insert($result);
+
+            $this->writeFinishedQuantity(array_column($result,'sale_orders_product_id'));
+
+            DB::commit();
+        }catch (\Exception $e){
+            DB::rollBack();
+            return [false,$e->getLine().':'.$e->getMessage()];
+        }
+
+        return [true,'保存成功!'];
+    }
+
+    public function del($data){
+        if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
+
+        return [true,'删除成功'];
+    }
+
+    public function orderDetail($data){
+        return [200,''];
+    }
+
+    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 orderList($data){
+        $model = FinishedOrderSub::where('del_time',0)
+            ->select('id','order_no','table_header_mark','product_no','product_title','product_size','product_unit','order_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','dispatch_quantity','finished_num','waste_quantity','production_no','status','crt_id','process_id','equipment_id','team_id','dispatch_time_start','dispatch_time_end','dispatch_time')
+            ->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(isset($data['status'])) $model->where('status',$data['status']);
+
+        $list = $this->limit($model,'',$data);
+        $list = $this->fillData($list);
+
+        return [true,$list];
+    }
+
+    public function orderRule($data){
+        if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
+        if($this->isEmpty($data,'quantity')) return [false,'请填写完工数量!'];
+
+        $result = DispatchSub::whereIn('id',$data['id'])
+            ->select('order_product_id','dispatch_no','id as dispatch_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','process_id','equipment_id','team_id','dispatch_time_start','dispatch_time_end','crt_time as dispatch_time','sale_orders_product_id','out_order_no_time')
+            ->get()->toArray();
+
+        //已完工数据
+        $map = $this->getFinishedQuantity($data['id']);
+
+        foreach ($result as $key => $value){
+            if(isset($map[$value['dispatch_id']])){
+                if($map[$value['dispatch_id']] + $data['quantity'][$key] > $value['dispatch_quantity']) return [false,'完工数量不能大于派工数量'];
+            }else{
+                if($data['quantity'][$key] > $value['dispatch_quantity']) return [false,'完工数量不能大于派工数量'];
+            }
+        }
+
+        return [true, $result];
+    }
+
+    public function fillData($data){
+        if(empty($data['data'])) return $data;
+
+        $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['finished_num'] = array_sum(array_column($data['data'], 'finished_num'));
+        $data['waste_quantity'] = array_sum(array_column($data['data'], 'waste_quantity'));
+
+        return $data;
+    }
+
+    //返回已完工工数量
+    public function getFinishedQuantity($dispatch_id = []){
+        if(empty($dispatch_id)) return [];
+
+        $result = FinishedOrderSub::where('del_time',0)
+            ->whereIn("dispatch_id",$dispatch_id)
+            ->select(DB::raw("sum(dispatch_quantity) as finished_num"),'dispatch_id')
+            ->groupby('dispatch_id')
+            ->pluck('finished_num','dispatch_id')
+            ->toArray();
+
+        return $result;
+    }
+
+    //反写写完工数量
+    public function writeFinishedQuantity($sale_orders_product_id){
+        if(empty($sale_orders_product_id)) return;
+
+        $result = FinishedOrderSub::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
+            ]);
+        }
+    }
+}

+ 19 - 0
app/Service/FyySqlServerService.php

@@ -84,9 +84,28 @@ class FyySqlServerService extends Service
         return $return;
     }
 
+    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 getDataFromSqlServer($data){
         if(empty($data['out_order_no_time'][0]) || empty($data['out_order_no_time'][1])) return [false,'制单日期不能为空!',''];
+        $bool = $this->is_same_month($data['out_order_no_time'][0],$data['out_order_no_time'][1]);
+        if(! $bool) return [false,'制单日期必须同月!'];
 
         //查询产品主表副表数据
         date_default_timezone_set("PRC");

+ 15 - 10
app/Service/ProductionOrderService.php

@@ -80,11 +80,17 @@ class ProductionOrderService extends Service
             $last_insert_id = array_column($last_insert_id,'id');
 
             //组织process bom的数据 写入与主表的关联id
+
+            $time_arr = [];
+            date_default_timezone_set("PRC");
             foreach ($result as $key => $value){
                 $quantity_tmp = $data['quantity'][$key];
 
+                $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++) {
-                    $boom[$value['order_no']][] = [
+                    $boom[$time_tmp][] = [
                         'order_product_id' => $last_insert_id[$key],
                         'production_no' => $production_no,
                         'order_no' => $value['order_no'],
@@ -95,7 +101,7 @@ class ProductionOrderService extends Service
                     ];
 
                     foreach ($process_arr as $v){
-                        $process[$value['order_no']][] = [
+                        $process[$time_tmp][] = [
                             'order_product_id' => $last_insert_id[$key],
                             'production_no' => $production_no,
                             'process_id' => $v,
@@ -112,16 +118,15 @@ class ProductionOrderService extends Service
             //反写已经生产数量
             $this->writeProductionQuantity($data['id']);
 
-            foreach ($boom as $key => $value){
-                $boom_model = new OrdersProductBom(['channel'=> $key]);
-                $boom_model->insert($value);
-            }unset($boom);
+            foreach ($time_arr as $t){
+                $boom_model = new OrdersProductBom(['channel'=> $t]);
+                $boom_model->insert($boom[$t]);
 
-            foreach ($process as $key => $value){
-                $process_model = new OrdersProductProcess(['channel' => $key]);
-                $process_model->insert($value);
-            }unset($process);
+                $process_model = new OrdersProductProcess(['channel' => $t]);
+                $process_model->insert($process[$t]);
+            }
 
+            unset($boom);unset($process);
             DB::commit();
         }catch (\Exception $e){
             DB::rollBack();

+ 6 - 0
routes/api.php

@@ -123,6 +123,12 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('dispatchOrderList', 'Api\DispatchController@dispatchOrderList');
     $route->any('dispatchOrderDel', 'Api\DispatchController@dispatchOrderDel');
 
+    //工序完工
+    $route->any('finishedOrderAdd', 'Api\FinishedOrderController@add');
+    $route->any('finishedOrderEdit', 'Api\FinishedOrderController@edit');
+    $route->any('finishedOrderDel', 'Api\FinishedOrderController@del');
+    $route->any('finishedOrderList', 'Api\FinishedOrderController@orderList');
+
     $route->any('productList', 'Api\MaterialController@productList');
     $route->any('productEdit', 'Api\MaterialController@edit');
     $route->any('productAdd', 'Api\MaterialController@edd');