cqp 9 months ago
parent
commit
adb36765e5

+ 75 - 0
app/Http/Controllers/Api/ApplyOrderController.php

@@ -0,0 +1,75 @@
+<?php
+
+namespace App\Http\Controllers\Api;
+
+use App\Service\ApplyOrderService;
+use Illuminate\Http\Request;
+
+class ApplyOrderController extends BaseController
+{
+    public function materialList(Request $request)
+    {
+        $service = new ApplyOrderService();
+        $user = $request->get('auth');
+        list($status,$data) = $service->materialList($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function materialDetail(Request $request)
+    {
+        $service = new ApplyOrderService();
+        $user = $request->get('auth');
+        list($status,$data) = $service->materialDetail($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+
+    public function materialEdit(Request $request)
+    {
+        $service = new ApplyOrderService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->materialEdit($request->all(), $userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function materialDel(Request $request)
+    {
+        $service = new ApplyOrderService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->materialDel($request->all(), $userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function materialAdd(Request $request)
+    {
+        $service = new ApplyOrderService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->materialAdd($request->all(), $userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+}

+ 13 - 0
app/Http/Controllers/Api/BoxController.php

@@ -251,4 +251,17 @@ class BoxController extends BaseController
             return $this->json_return(201,$data);
         }
     }
+
+    public function boxList(Request $request)
+    {
+        $service = new BoxService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->boxList($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
 }

+ 13 - 0
app/Http/Controllers/Api/DeleteOrderController.php

@@ -21,4 +21,17 @@ class DeleteOrderController extends BaseController
             return $this->json_return(201,$data);
         }
     }
+
+    public function checkOrders(Request $request)
+    {
+        $service = new DeleteOrderService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->checkOrders($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
 }

+ 13 - 0
app/Http/Controllers/Api/DispatchController.php

@@ -114,4 +114,17 @@ class DispatchController extends BaseController
             return $this->json_return(201,$data);
         }
     }
+
+    public function dispatchOrderForSqList(Request $request)
+    {
+        $service = new DispatchService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->dispatchOrderForSqList($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
 }

+ 91 - 0
app/Http/Controllers/Api/EmployeeController.php

@@ -353,5 +353,96 @@ class EmployeeController extends BaseController
 
 
 
+    public function settingList(Request $request)
+    {
+        $service = new EmployeeService();
+        $user = $request->get('auth');
+        list($status,$data) = $service->settingList($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function settingEdit(Request $request)
+    {
+        $service = new EmployeeService();
+        $user = $request->get('auth');
+        list($status,$data) = $service->settingEdit($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function dxjEdit(Request $request)
+    {
+
+
+        $service = new EmployeeService();
+        $user = $request->get('auth');
+        list($status,$data) = $service->dxjEdit($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+
+    }
 
+
+    public function dxjAdd(Request $request)
+    {
+
+
+        $service = new EmployeeService();
+        $user = $request->get('auth');
+        list($status,$data) = $service->dxjAdd($request->all(),$request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+
+    }
+
+
+    public function dxjDel(Request $request)
+    {
+
+
+        $service = new EmployeeService();
+        $user = $request->get('auth');
+        list($status,$data) = $service->dxjDel($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+
+    }
+
+
+    public function dxjList(Request $request)
+    {
+
+
+        $service = new EmployeeService();
+        $user = $request->get('auth');
+        list($status,$data) = $service->dxjList($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+
+    }
 }

+ 13 - 0
app/Http/Controllers/Api/FyyOrderController.php

@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Api;
 
 
 use App\Service\FyyOrderService;
+use App\Service\FyySqlServerService;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Redis;
 
@@ -160,4 +161,16 @@ class FyyOrderController extends BaseController
             return $this->json_return(201,$data);
         }
     }
+
+    public function getStorehouseDataFromSqlServer(Request $request){
+        $service = new FyyOrderService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->getStorehouseDataFromSqlServer($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
 }

+ 4 - 0
app/Http/Middleware/CheckLogin.php

@@ -3,6 +3,7 @@
 namespace App\Http\Middleware;
 
 use App\Http\Controllers\Api\LoginController;
+use App\Model\Setting;
 use App\Service\EmployeeService;
 use Closure;
 use App\Service\TokenService;
@@ -47,6 +48,9 @@ class CheckLogin
         $data['role'] = EmployeeService::getPersonRole($user_id);
         //部门权限
         $data['rule_depart'] = EmployeeService::getPersonDepart($user_id);
+        //系统设置
+        $setting = Setting::select('*')->get()->toArray();
+        $data['setting'] = array_column($setting,null, 'setting_name');
         //写入user信息
         $request->userData = $data;
 

+ 38 - 0
app/Model/ApplyOrder.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+/**
+ * 物料分类
+ * Class Unit
+ * @package App\Models
+ */
+class ApplyOrder extends Model
+{
+    protected $table = "apply_order"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+
+    const type_one = 1;
+    const type_two = 2;
+    const type_three = 3;
+    const type_four = 4;
+
+    public static $type_name = [
+        self::type_one => '领料申请单',
+        self::type_two => '完工入库申请单',
+        self::type_three => '包装入库申请单',
+        self::type_four => '包装领料申请单',
+    ];
+
+    const state_zero = 0;
+    const state_one = 1;
+
+    public static $state_name = [
+        self::state_zero => '未审核',
+        self::state_one => '已审核',
+    ];
+}

+ 13 - 0
app/Model/ApplyOrderDetail.php

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

+ 13 - 0
app/Model/ApplyOrderForYy.php

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

+ 7 - 0
app/Model/Box.php

@@ -15,4 +15,11 @@ class Box extends Model
     const CREATED_AT = 'crt_time';
     const UPDATED_AT = 'upd_time';
     protected $dateFormat = 'U';
+
+    const status_zero = 0;
+    const status_one = 1;
+    public static $status_name = [
+        self::status_zero => '未审核',
+        self::status_one => '已审核',
+    ];
 }

+ 13 - 0
app/Model/DXJ.php

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

+ 6 - 0
app/Model/DispatchSub.php

@@ -15,4 +15,10 @@ class DispatchSub extends Model
     const CREATED_AT = 'crt_time';
     const UPDATED_AT = 'upd_time';
     protected $dateFormat = 'U';
+    const status_zero = 0;
+    const status_one = 1;
+    public static $status_name = [
+        self::status_zero => '未审核',
+        self::status_one => '已审核',
+    ];
 }

+ 7 - 0
app/Model/OrdersProduct.php

@@ -15,4 +15,11 @@ class OrdersProduct extends Model
     const CREATED_AT = 'crt_time';
     const UPDATED_AT = 'upd_time';
     protected $dateFormat = 'U';
+
+    const status_zero = 0;
+    const status_one = 1;
+    public static $status_name = [
+        self::status_zero => '未审核',
+        self::status_one => '已审核',
+    ];
 }

+ 15 - 0
app/Model/Setting.php

@@ -0,0 +1,15 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Setting extends Model
+{
+    protected $table = "setting"; //指定表
+    const CREATED_AT = null;
+    const UPDATED_AT = null;
+    protected $dateFormat = 'U';
+    protected $primaryKey = 'setting_name';
+    protected $keyType = 'string'; // 设置主键类型为字符串
+}

+ 477 - 0
app/Service/ApplyOrderService.php

@@ -0,0 +1,477 @@
+<?php
+
+namespace App\Service;
+
+use App\Model\ApplyOrder;
+use App\Model\ApplyOrderDetail;
+use App\Model\Box;
+use App\Model\DispatchSub;
+use App\Model\Employee;
+use App\Model\InOutRecord;
+use Illuminate\Support\Facades\DB;
+
+class ApplyOrderService extends Service
+{
+    public function MaterialEdit($data, $user){
+        list($status,$msg) = $this->MaterialRule($data,false);
+        if(!$status) return [$status,$msg];
+
+        DB::beginTransaction();
+        try{
+            $time = time();
+            $model = new ApplyOrder();
+            $model = $model->where('id',$data['id'])->first();
+            $model->apply_id = $data['apply_id'];
+            $model->apply_time = $data['apply_time'] ;
+            $model->mark = $data['mark'] ?? "";
+            $model->storehouse_id = $data['storehouse_id'];
+            $model->storehouse_title = $data['storehouse_title'] ?? "";
+            $model->type = $data['type'];
+            $model->save();
+            $id = $model->id;
+
+            ApplyOrderDetail::where('del_time',0)->where('apply_order_id',$id)->update([
+                'del_time' => $time
+            ]);
+
+            $detail_insert = [];
+            foreach ($data['order_data'] as $v){
+                $detail_insert[] = [
+                    'apply_order_id' => $id,
+                    'data_id' => $v['id'],
+                    'quantity' => $v['quantity'] ?? 0,
+                    'product_no' => $v['product_no'] ?? "",
+                    'product_title' => $v['product_title'] ?? "",
+                    'product_size' => $v['product_size'] ?? "",
+                    'product_unit' => $v['product_unit'] ?? "",
+                    'technology_name' => $v['technology_name'] ?? "", //颜色
+                    'top_product_title' => $v['top_product_title'] ?? "",
+                    'top_product_no' => $v['top_product_title'] ?? "",
+                    'type' => $data['type'],
+                    'storehouse_id' => $data['storehouse_id'],
+                    'crt_time' => $time,
+                ];
+            }
+            ApplyOrderDetail::insert($detail_insert);
+
+            DB::commit();
+        }catch (\Exception $e){
+            DB::rollBack();
+            return [false,$e->getMessage()];
+        }
+        return [true,''];
+    }
+
+    public function MaterialAdd($data,$user){
+        list($status,$msg) = $this->MaterialRule($data);
+        if(!$status) return [$status,$msg];
+
+        DB::beginTransaction();
+        try{
+            $time = time();
+            $model = new ApplyOrder();
+            $model->order_number = $data['order_number'];
+            $model->apply_id = $data['apply_id'];
+            $model->apply_time = $data['apply_time'] ;
+            $model->mark = $data['mark'] ?? "";
+            $model->storehouse_id = $data['storehouse_id'];
+            $model->storehouse_title = $data['storehouse_title'] ?? "";
+            $model->type = $data['type'];
+            $model->crt_id = $user['id'];
+            $model->save();
+            $id = $model->id;
+
+            $detail_insert = [];
+            foreach ($data['order_data'] as $v){
+                $detail_insert[] = [
+                    'apply_order_id' => $id,
+                    'data_id' => $v['id'],
+                    'quantity' => $v['quantity'] ?? 0,
+                    'product_no' => $v['product_no'] ?? "",
+                    'product_title' => $v['product_title'] ?? "",
+                    'product_size' => $v['product_size'] ?? "",
+                    'product_unit' => $v['product_unit'] ?? "",
+                    'technology_name' => $v['technology_name'] ?? "", //颜色
+                    'top_product_title' => $v['top_product_title'] ?? "",
+                    'top_product_no' => $v['top_product_title'] ?? "",
+                    'type' => $data['type'],
+                    'storehouse_id' => $data['storehouse_id'],
+                    'crt_time' => $time,
+                ];
+            }
+            ApplyOrderDetail::insert($detail_insert);
+
+            DB::commit();
+        }catch (\Exception $e){
+            DB::rollBack();
+            return [false,$e->getMessage()];
+        }
+
+        return [true, ''];
+    }
+
+    public function MaterialDel($data){
+        if($this->isEmpty($data,'id')) return [false,'ID必须!'];
+
+        $apply = ApplyOrder::where('id',$data['id'])->first();
+        if($apply->del_time > 0) return [false,'申请单不存在或已被删除'];
+        if($apply->state != ApplyOrder::state_zero) return [false, '申请单已审核,删除失败'];
+
+        $time = time();
+        try {
+            DB::beginTransaction();
+
+            $apply->del_time = $time;
+            $apply->save();
+
+            ApplyOrderDetail::where('del_time', 0)->where('apply_order_id',$data['id'])->update([
+                'del_time'=>time()
+            ]);
+
+            DB::commit();
+        }catch (\Throwable $exception){
+            DB::rollBack();
+            return [false, $exception->getMessage()];
+        }
+
+        return [true,''];
+    }
+
+    public function MaterialList($data){
+        $model = ApplyOrder::where('del_time',0)
+            ->select('*')
+            ->orderBy('id','desc');
+
+        if(isset($data['status'])) $model->where('status', $data['status']);
+        if(! empty($data['type'])) $model->where('type', $data['type']);
+        if(! empty($data['storehouse_title'])) $model->where('storehouse_title', 'LIKE', '%'.$data['storehouse_title'].'%');
+        if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
+        if(! empty($data['apply_id'])) $model->where('apply_id', $data['apply_id']);
+        if(! empty($data['crt_id'])) $model->where('crt_id', $data['crt_id']);
+        if(! empty($data['apply_time'][0]) && ! empty($data['apply_time'][1])) $model->whereBetween('apply_time',[$data['apply_time'][0],$data['apply_time'][1]]);
+        if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
+
+        $list = $this->limit($model,'',$data);
+        $list = $this->fillData($list);
+
+        return [true,$list];
+    }
+
+    public function fillData($data){
+        if(empty($data['data'])) return $data;
+
+        $emp = Employee::whereIn('id',array_merge_recursive(array_unique(array_column($data['data'],'crt_id')), array_unique(array_column($data['data'],'apply_id'))))
+            ->pluck('emp_name','id')
+            ->toArray();
+
+        foreach ($data['data'] as $key => $value){
+            $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
+            $data['data'][$key]['status_title'] = ApplyOrder::$state_name[$value['status']] ?? "";
+            $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
+            $data['data'][$key]['apply_name'] = $emp[$value['apply_id']] ?? '';
+        }
+
+        return $data;
+    }
+
+    public function MaterialDetail($data){
+        if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
+        $id = $data['id'];
+        $detail = ApplyOrder::where('del_time',0)
+            ->where('id',$id)
+            ->first();
+        if(empty($detail)) return [false,'申请单不存在或已被删除'];
+        $detail = $detail->toArray();
+        $detail['apply_title'] = Employee::where('id', $detail['apply_id'])->value('emp_name');
+        $apply_d = ApplyOrderDetail::where('del_time', 0)
+            ->where('apply_order_id', $id)
+            ->get()->toArray();
+        $apply_id = array_column($apply_d, 'data_id');
+
+        $d = [];
+        if($detail['type'] == ApplyOrder::type_one){
+            $d = DispatchSub::where('del_time',0)
+                ->whereIn('id', $apply_id)
+                ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time')
+                ->get()->toArray();
+            $args = "";
+            foreach ($d as $value){
+                $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
+            }
+            $args = rtrim($args, 'OR ');
+
+            $d_no_map = [];
+            $d_no = DispatchSub::where('del_time',0)
+                ->whereRaw($args)
+                ->select('dispatch_no as order_no','order_product_id','crt_time')
+                ->get()->toArray();
+            foreach ($d_no as $value){
+                if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){
+                    if(! in_array($value['order_no'], $d_no_map[$value['order_product_id'] . $value['crt_time']])) $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
+                }else{
+                    $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
+                }
+            }
+        }elseif ($detail['type'] == ApplyOrder::type_two){
+            $d = DispatchSub::where('del_time',0)
+                ->whereIn('id', $apply_id)
+                ->select('id', 'dispatch_no as order_no','technology_name','wood_name','order_product_id','crt_time')
+                ->get()->toArray();
+            $args = "";
+            foreach ($d as $value){
+                $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
+            }
+            $args = rtrim($args, 'OR ');
+
+            $d_no_map = [];
+            $d_no = DispatchSub::where('del_time',0)
+                ->whereRaw($args)
+                ->select('dispatch_no as order_no','order_product_id','crt_time')
+                ->get()->toArray();
+            foreach ($d_no as $value){
+                if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){
+                    if(! in_array($value['order_no'], $d_no_map[$value['order_product_id'] . $value['crt_time']])) $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
+                }else{
+                    $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
+                }
+            }
+        }elseif ($detail['type'] == ApplyOrder::type_three){
+            $d = Box::where('del_time',0)
+                ->whereIn('id', $apply_id)
+                ->select('id', 'order_no')
+                ->get()->toArray();
+        }elseif ($detail['type'] == ApplyOrder::type_four){
+            $d = Box::where('del_time',0)
+                ->whereIn('id', $apply_id)
+                ->select('id', 'order_no')
+                ->get()->toArray();
+        }
+        $d_tmp = array_column($d,null,'id');
+
+        $return = [];
+        foreach ($apply_d as $t){
+            $tmp = $d_tmp[$t['data_id']] ?? [];
+            $technology_name = ! empty($t['technology_name']) ? $t['technology_name'] : $tmp['technology_name'] ?? "";
+            $wood_name = ! empty($t['wood_name']) ? $t['wood_name'] : $tmp['wood_name'] ?? "";
+            $top_product_title = ! empty($t['top_product_title']) ? $t['top_product_title'] : $tmp['product_title'] ?? "";
+            $top_product_no = ! empty($t['top_product_no']) ? $t['top_product_no'] : $tmp['product_no'] ?? "";
+
+            if(! empty($tmp['order_product_id']) && ! empty($tmp['crt_time']) && isset($d_no_map[$tmp['order_product_id'] . $tmp['crt_time']])){
+                $order_no = implode(',', $d_no_map[$tmp['order_product_id'] . $tmp['crt_time']]);
+            }else{
+                $order_no = $tmp['order_no'] ?? "";
+            }
+
+            if($t['type'] == ApplyOrder::type_two || $t['type'] == ApplyOrder::type_three){
+                $product_unit = "吨";
+            }else{
+                $product_unit = $t['product_unit'] ?? "";
+            }
+
+            $return[] = [
+                'id' => $t['data_id'] ?? 0,
+                'quantity' => $t['quantity'] ?? 0,
+                'product_no' => $t['product_no'] ?? "",
+                'product_title' => $t['product_title'] ?? "",
+                'product_size' => $t['product_size'] ?? "",
+                'product_unit' => $product_unit,
+                'top_product_title' => $top_product_title,
+                'top_product_no' => $top_product_no,
+                'technology_name' => $technology_name, //颜色
+                'wood_name' => $wood_name,
+                'order_no' => $order_no,
+            ];
+        }
+
+        $detail['order_data'] = $return;
+
+        return [true, $detail];
+    }
+
+    public function MaterialRule(&$data,$is_add = true){
+        if($this->isEmpty($data,'apply_id')) return [false,'申请人不能为空'];
+        if($this->isEmpty($data,'apply_time')) return [false,'申请时间不能为空'];
+        if($this->isEmpty($data,'storehouse_id')) return [false,'仓库不能为空'];
+        if($this->isEmpty($data,'type')) return [false,'申请单类型不能为空'];
+        if(empty($data['order_data'])) return [false, '申请单详细信息不能为空'];
+        foreach ($data['order_data'] as $value){
+            if(empty($value['id'])) return [false, '申请单详细信息ID不能为空'];
+            if($data['type'] != ApplyOrder::type_three){
+                if(empty($value['quantity'])) return [false, '申请单详细信息数量不能为空'];
+            }
+        }
+
+        $id = array_unique(array_column($data['order_data'],'id'));
+        if($data['type'] == ApplyOrder::type_one){
+            $dispatch = DispatchSub::where('del_time',0)
+                ->whereIn('id', $id)
+                ->select('id', 'dispatch_no', 'status')
+                ->get()->toArray();
+            if(count($dispatch) != count($id)) return [false, '派工单信息错误,请重新选择'];
+        }elseif ($data['type'] == ApplyOrder::type_two){
+            $dispatch = DispatchSub::where('del_time',0)
+                ->whereIn('id', $id)
+                ->select('id', 'dispatch_no', 'status')
+                ->get()->toArray();
+            if(count($dispatch) != count($id)) return [false, '完工单信息错误,请重新选择'];
+        }elseif ($data['type'] == ApplyOrder::type_three){
+            $box = Box::where('del_time',0)
+                ->whereIn('id', $id)
+                ->select('id', 'order_no', 'status')
+                ->get()->toArray();
+            if(count($box) != count($id)) return [false, '包装单信息错误,请重新选择'];
+        }elseif ($data['type'] == ApplyOrder::type_four){
+            $box = Box::where('del_time',0)
+                ->whereIn('id', $id)
+                ->select('id', 'order_no', 'status')
+                ->get()->toArray();
+            if(count($box) != count($id)) return [false, '包装单信息错误,请重新选择'];
+        }else{
+            return [false, '申请单类型错误'];
+        }
+
+        if($is_add){
+            $data['order_number'] = $this->setOrderNO($data['type']);
+            if(empty($data['order_number'])) return [false, '申请单唯一标识生成失败'];
+        }else{
+            if(empty($data['id'])) return [false, '申请单唯一标识不能为空'];
+            if(empty($data['order_number'])) return [false, '申请单唯一标识不能为空'];
+            $apply = ApplyOrder::where('id', $data['id'])->where('del_time', 0)->first();
+            if(empty($apply)) return [false, '申请单不存在或已被删除'];
+            if($apply->status != 0) return [false, '申请单已审核,编辑失败'];
+
+        }
+
+        return [true,''];
+    }
+
+    public function setOrderNO($type = ""){
+        $str = date('Ymd',time());
+
+        $order_number = ApplyOrder::where('order_number','Like','%'. $str . '%')
+            ->where('type', $type)
+            ->max('order_number');
+
+        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;
+
+            //超过999
+            if(strlen($tmp) > 3) return '';
+
+            $number = str_pad($tmp,3,'0',STR_PAD_LEFT);
+            $number = $str . $number;
+        }
+
+        return $number;
+    }
+
+    public function createSQ($data, $user, $type = 0, $status = 1){
+        if(empty($data) || empty($type)) return [false, '自动生成申请单参数不能为空'];
+
+        if($type == ApplyOrder::type_one){
+            $storehouse_id = "001";
+            $storehouse_title = "原料仓";
+        }elseif ($type == ApplyOrder::type_two){
+            $storehouse_id = "004";
+            $storehouse_title = "待清洗原材料仓";
+        }elseif ($type == ApplyOrder::type_three){
+            $storehouse_id = "002";
+            $storehouse_title = "产成品仓";
+        }else{
+            $storehouse_id = "003";
+            $storehouse_title = "包材、辅料仓" ;
+        }
+
+        try{
+            DB::beginTransaction();
+
+            $order_number = $this->setOrderNO($type);
+//            list($status,$msg) = $this->limitingSendRequestBackg("spAdd" . $order_number . $type);
+//            if(! $status) return [false, $msg];
+
+            $time = time();
+            $model = new ApplyOrder();
+            $model->order_number = $order_number;
+            $model->apply_id = $user['id'];
+            $model->apply_time = time();
+            $model->storehouse_id = $storehouse_id;
+            $model->storehouse_title = $storehouse_title;
+            $model->type = $type;
+            $model->status = $status; //是否审核
+            $model->save();
+            $id = $model->id;
+
+            $detail_insert = [];
+            foreach ($data as $v){
+                $detail_insert[] = [
+                    'apply_order_id' => $id,
+                    'data_id' => $v['id'],
+                    'quantity' => $v['quantity'] ?? 0,
+                    'product_no' => $v['product_no'] ?? "",
+                    'product_title' => $v['product_title'] ?? "",
+                    'product_size' => $v['product_size'] ?? "",
+                    'product_unit' => $v['product_unit'] ?? "",
+                    'technology_name' => $v['technology_name'] ?? "",
+                    'wood_name' => $v['wood_name'] ?? "",
+                    'top_product_no' => $v['top_product_no'] ?? "",
+                    'top_product_title' => $v['top_product_title'] ?? "",
+                    'type' => $type,
+                    'storehouse_id' => $storehouse_id,
+                    'crt_time' => $time,
+                ];
+            }
+            ApplyOrderDetail::insert($detail_insert);
+
+            DB::commit();
+        }catch (\Exception $e){
+            DB::rollBack();
+            return [false,$e->getMessage()];
+        }
+
+        return [true, $id];
+    }
+
+    //流水
+    public function createRecord($id){
+        $record = ApplyOrderDetail::where('apply_order_id', $id)
+            ->where('del_time',0)
+            ->get()->toArray();
+        if(empty($record)) return [false, '申请单明细数据不存在或已被删除'];
+
+        try{
+            DB::beginTransaction();
+
+            $time = time();
+            $insert = [];
+            foreach ($record as $value){
+                if(empty($value['product_no'])) continue;
+
+                if($value['type'] == ApplyOrder::type_one){
+                    $number = - $value['quantity'];
+                }else{
+                    $number = $value['quantity'];
+                }
+                $insert[] = [
+                    'apply_order_id' => $id,
+                    'product_no' => $value['product_no'],
+                    'number' => $number,
+                    'type' => $value['type'],
+                    'storehouse_id' => $value['storehouse_id'],
+                    'crt_time' => $time,
+                ];
+            }
+            if(! empty($insert)) InOutRecord::insert($insert);
+
+            DB::commit();
+        }catch (\Exception $e){
+            DB::rollBack();
+            return [false,$e->getMessage()];
+        }
+
+        return [true, ''];
+    }
+}

+ 315 - 9
app/Service/Box/BoxService.php

@@ -3,6 +3,8 @@
 namespace App\Service\Box;
 
 use App\Jobs\BoxAddJob;
+use App\Model\ApplyOrder;
+use App\Model\ApplyOrderForYy;
 use App\Model\Box;
 use App\Model\BoxDetail;
 use App\Model\DispatchSub;
@@ -10,8 +12,12 @@ use App\Model\Header_ext;
 use App\Model\OrdersProduct;
 use App\Model\OrdersProductProcess;
 use App\Model\SaleOrdersProduct;
+use App\Model\Setting;
 use App\Model\Team;
+use App\Service\ApplyOrderService;
+use App\Service\EmployeeService;
 use App\Service\FinishedOrderService;
+use App\Service\FyyOrderService;
 use App\Service\FyySqlServerService;
 use App\Service\Service;
 use Illuminate\Support\Facades\DB;
@@ -127,6 +133,7 @@ class BoxService extends Service
         }
         $insert = [];
 
+        //锁
         $limit_key = $this->lock_key;
         try{
             DB::beginTransaction();
@@ -188,6 +195,7 @@ class BoxService extends Service
             $insert['top_order_no'] = $top_order_no;
             $insert['order_no'] = $box_no;
             $insert['shipment_order_no'] = $transport_no;
+            //生成包装单
             list($status,$msg) = self::$box_hook->boxInsert($insert);
             if(! $status) {
                 $this->dellimitingSendRequestBackgNeed($limit_key);
@@ -195,17 +203,47 @@ class BoxService extends Service
                 return [false, $msg];
             }
 
-            //更新完工数量
-            list($status, $msg1) = $this->updateFinish($product_list,$key_list);
+            //自动完工 更新完工数量
+            $em = new EmployeeService();
+            $auto = $em->is_auto($user, "bzwg_auto");
+            if($auto){
+                list($status, $msg1) = $this->updateFinish($product_list,$key_list,$user);
+                if(! $status) {
+                    $this->dellimitingSendRequestBackgNeed($limit_key);
+                    DB::rollBack();
+                    return [false, $msg1];
+                }
+            }
+
+            //包装单
+            $package_data = $msg->toArray();
+            //包装单产品
+            $boxDetail = new BoxDetail(['channel'=>$package_data['top_order_no']]);
+            $boxDetail = $boxDetail->where('del_time',0)
+                ->where('order_no',$package_data['order_no'])
+                ->select('ext_1 as product_no', 'ext_3 as technology_name', 'ext_8 as product_title', 'ext_9 as product_size', 'num as quantity')
+                ->get()->toArray();
+
+            //生成包装领料申请单
+            list($status, $msg1) = $this->createllSQ($package_data,$boxDetail, $user);
             if(! $status) {
                 $this->dellimitingSendRequestBackgNeed($limit_key);
                 DB::rollBack();
                 return [false, $msg1];
             }
 
-            //用友 ------产成品入库
-            $package_data = $msg->toArray();
-            if(! empty($package_data)) {
+            //生成包装入库申请单
+            list($status, $msg) = $this->createBZSQ($package_data,$boxDetail, $user);
+            if(! $status) {
+                $this->dellimitingSendRequestBackgNeed($limit_key);
+                DB::rollBack();
+                return [false, $msg];
+            }
+
+            //是否自动审核  包装入库申请单
+            $auto = $em->is_auto($user,"bzsq_auto");
+            if(! empty($package_data) && $auto) {
+                //用友 ------产成品入库
                 $service = new FinishedOrderService();
                 list($status,$msg) = $service->U8Rdrecord10Save($package_data,$user);
                 if(! $status) {
@@ -216,8 +254,8 @@ class BoxService extends Service
             }
 
             DB::commit();
-
             $this->dellimitingSendRequestBackgNeed($limit_key);
+
             return [true, ['package_data' => $package_data]];
         }catch (\Throwable $e){
             DB::rollBack();
@@ -229,11 +267,129 @@ class BoxService extends Service
         }
     }
 
+    public function createllSQ($package_data, $insert_id, $user){
+        //是否自动审核  包装领料申请单
+        $em = new EmployeeService();
+        $auto = $em->is_auto($user,"bzllsq_auto");
+
+        $return = [];
+        $product_no = array_unique(array_column($insert_id,'product_no'));
+        //获取包装原料
+        $service = new FyyOrderService();
+        list($status, $msg) = $service->getProductBzDataFromSqlServer(['product_no' => $product_no], $user);
+        if($status) $return = $msg;
+
+        //组织包装原材料写入数据
+        $insert = [];
+        $box_id = $package_data['id'] ?? 0;
+        foreach ($insert_id as $value){
+            $t = $return[$value['product_no']] ?? [];
+            $tmp = [
+                'id' => $box_id,
+                'quantity' => $value['quantity'] ?? 0,
+                'product_no' => "",
+                'product_title' => "",
+                'product_size' => "",
+                'product_unit' => "",
+                'top_product_no' => $value['product_no'],
+                'top_product_title' => $value['product_title'],
+                'technology_name' => $value['technology_name'],
+            ];
+            if(! empty($t)){
+                foreach ($t as $v){
+                    $tmp['product_no'] = $v['product_no'];
+                    $tmp['product_title'] = $v['product_title'];
+                    $tmp['product_size'] = $v['product_size'];
+                    $tmp['product_unit'] = $v['product_unit'];
+                    $insert[] = $tmp;
+                }
+            }else{
+                $tmp['quantity'] = 0;
+                $insert[] = $tmp;
+            }
+        }
+
+        try {
+            DB::beginTransaction();
+
+            //生成申请单
+            $service = new ApplyOrderService();
+            list($status, $msg) = $service->createSQ($insert, $user, ApplyOrder::type_four, $auto);
+            if(! $status) {
+                DB::rollBack();
+                return [false, $msg];
+            }
+
+            if($auto) {
+                //生成流水
+                list($status, $msg) = $service->createRecord($msg);
+                if(! $status) {
+                    DB::rollBack();
+                    return [false, $msg];
+                }
+            }
+
+            DB::commit();
+        }catch (\Throwable $exception){
+            DB::rollBack();
+            return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
+        }
+
+        return [true, ''];
+    }
+
+    public function createBZSQ($package_data, $insert_id, $user){
+        //是否自动审核  包装入库申请单
+        $em = new EmployeeService();
+        $auto = $em->is_auto($user,"bzsq_auto");
+
+        try {
+            DB::beginTransaction();
+
+            $box_id = $package_data['id'] ?? 0;
+            foreach ($insert_id as $key => $value){
+                $insert_id[$key]['id'] = $box_id;
+            }
+
+            //生成申请单
+            $service = new ApplyOrderService();
+            list($status, $msg) = $service->createSQ($insert_id, $user, ApplyOrder::type_three, $auto);
+            if(! $status) {
+                DB::rollBack();
+                return [false, $msg];
+            }
+
+            //用友写入数据信息
+            $apply_order_id = $msg;
+            ApplyOrderForYy::insert([
+                'apply_order_id' => $apply_order_id,
+                'crt_time' => time(),
+                'post' => json_encode($package_data),
+            ]);
+
+            if($auto) {
+                //生成流水
+                list($status, $msg) = $service->createRecord($msg);
+                if(! $status) {
+                    DB::rollBack();
+                    return [false, $msg];
+                }
+            }
+
+            DB::commit();
+        }catch (\Throwable $exception){
+            DB::rollBack();
+            return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
+        }
+
+        return [true, ''];
+    }
+
     public function delBoxLock(){
         $this->dellimitingSendRequestBackgNeed($this->lock_key);
     }
 
-    public function updateFinish($production_list = [], $map){
+    public function updateFinish($production_list = [], $map,$user){
         if(empty($production_list)) return [true,''];
 
         //组织派工数据
@@ -255,7 +411,7 @@ class BoxService extends Service
                 if($val['dispatch_quantity'] <= $val['finished_num']) continue; //某一个工序完工
                 //包装数量
                 $tmp_num = $map[$val['order_product_id']]['total'] ?? 0;
-                if($tmp_num < 0) continue;
+                if($tmp_num <= 0) continue;
                 $tmp_num2 = bcsub($val['dispatch_quantity'] , $val['finished_num'],3);
                 if($tmp_num2 >= $tmp_num){
                     $val['quantity'] = $tmp_num;
@@ -271,13 +427,28 @@ class BoxService extends Service
         try {
             DB::beginTransaction();
             //根据派工数据回写
+            $last_update = $tmp = [];
             foreach ($result as $value){
                 $finished_num = $value['quantity'] + $value['finished_num'];
 
                 DispatchSub::where('id',$value['id'])->update([
-                    'finished_num' => $finished_num
+                    'finished_num' => $finished_num,
+                    'wg_status' => 1,
                 ]);
 
+                $str = $value['order_product_id'] . $value['crt_time'] . $value['product_no'] . $value['technology_name'];
+                if(! in_array($str, $tmp)){
+                    $last_update[] = [
+                        'id' => $value['id'],
+                        'quantity' => $value['quantity'],
+                        'product_no' => $value['product_no'] ?? "",
+                        'product_title' => $value['product_title'] ?? "",
+                        'product_size' => $value['product_size'] ?? "",
+                        'product_unit' => $value['product_unit'] ?? "",
+                    ];
+                    $tmp[] = $str;
+                }
+
                 $quantity = $value['quantity'] * 1000;
                 //工序表
                 $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
@@ -302,6 +473,11 @@ class BoxService extends Service
             $service->writeFinishedQuantityByOrdersProductId($id);
             //销售订单
             $service->writeFinishedQuantity($id);
+
+            //生成完工入库申请单
+            list($status, $msg) = $this->createWGSQ($last_update, $user);
+            if(! $status) return [false, $msg];
+
             DB::commit();
         }catch (\Throwable $exception){
             DB::rollBack();
@@ -311,6 +487,40 @@ class BoxService extends Service
         return [true, ''];
     }
 
+    public function createWGSQ($insert_id, $user){
+        //完工入库申请单自动审核并同步用友
+        $em = new EmployeeService();
+        $auto = $em->is_auto($user, "wgsq_auto");
+
+        try {
+            DB::beginTransaction();
+
+            //生成申请单
+            $service = new ApplyOrderService();
+            list($status, $msg) = $service->createSQ($insert_id, $user, ApplyOrder::type_two, $auto);
+            if(! $status) {
+                DB::rollBack();
+                return [false, $msg];
+            }
+
+            if($auto) {
+                //生成流水
+                list($status, $msg) = $service->createRecord($msg);
+                if(! $status) {
+                    DB::rollBack();
+                    return [false, $msg];
+                }
+            }
+
+            DB::commit();
+        }catch (\Throwable $exception){
+            DB::rollBack();
+            return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
+        }
+
+        return [true, ''];
+    }
+
     public function saveOutOrder($data,$user){
         list($status,$msg) = $this->saveOutOrderRule($data,$user);
         if(! $status) return [false, $msg];
@@ -1140,4 +1350,100 @@ class BoxService extends Service
 
         return [true, array_values($return)];
     }
+
+    public function boxList($data,$user){
+        $model = Box::where('del_time',0)
+            ->select('id','order_no','out_order_no','top_order_no','crt_time','status','shipment_order_no')
+            ->orderBy('id','desc');
+
+        if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
+        if(! empty($data['out_order_no']) || ! empty($data['shipment_order_no'])) {
+            $id = $this->searchDetail($data);
+            $model->whereIn('order_no', $id);
+        }
+        if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
+        if(isset($data['status'])) $model->where('status',$data['status']);
+
+        $list = $this->limit($model,'',$data);
+        $list = $this->fillBoxListData($list, $data,$user);
+
+        return [true, $list];
+    }
+
+    public function fillBoxListData($data, $erg,$user){
+        if(empty($data['data'])) return $data;
+
+        $product = $material = [];
+        if(! empty($erg['box'])){
+            //获取包装单里的产品
+            $id = array_column($data['data'], 'id');
+            list($product,$product_no) = $this->getBoxDetail($id);
+            if(! empty($erg['box_material'])){
+                //产品的包装材料
+                $service = new FyyOrderService();
+                list($status, $msg) = $service->getProductBzDataFromSqlServer(['product_no' => $product_no], $user);
+                if($status) $material = $msg;
+            }
+        }
+
+        foreach ($data['data'] as $key => $value){
+            $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
+            $data['data'][$key]['status_title'] = Box::$status_name[$value['status']] ?? "";
+            $p = $product[$value['id']] ?? [];
+            if(! empty($p)){
+                foreach ($p as $k => $v){
+                    $p[$k]['box_material'] = $material[$v['product_no']] ?? [];
+                }
+            }
+            $data['data'][$key]['product'] = $p;
+        }
+
+        return $data;
+    }
+
+    public function getBoxDetail($id = []){
+        $return = $product_no= [];
+        if(empty($id)) return [$return,$product_no];
+
+        $box = Box::where('del_time',0)
+            ->whereIn('id', $id)
+            ->select('id', 'order_no','top_order_no')
+            ->get()->toArray();
+        if (empty($box)) return [$return,$product_no];
+        $map = array_column($box,'id','order_no');
+
+        $result = [];
+        foreach ($box as $v){
+            $model = new BoxDetail(['channel'=>$v['top_order_no']]);
+            $result = array_merge($result,$model->where('del_time',0)
+                ->where('order_no',$v['order_no'])
+                ->select('order_no', 'ext_1 as product_no', 'ext_8 as product_title', 'ext_9 as product_size','num as quantity','ext_3 as technology_name')->get()->toArray());
+
+        }
+
+        foreach ($result as $value){
+            $value['id'] = $map[$value['order_no']] ?? 0;
+            $value['product_unit'] = "吨";
+            $return[$value['id']][] = $value;
+            if(! in_array($value['product_no'], $product_no)) $product_no[] = $value['product_no'];
+        }
+
+        return [$return, $product_no];
+    }
+
+    public function searchDetail($data){
+        $out_order_no = $data['out_order_no'] ?? '';
+        $shipment_order_no = $data['shipment_order_no'] ?? '';
+        $result = BoxDetail::where('del_time',0)
+            ->when(! empty($out_order_no), function ($query) use ($out_order_no) {
+                return $query->where('out_order_no', 'LIKE', '%'. $out_order_no .'%');
+            })
+            ->when(! empty($shipment_order_no), function ($query) use ($shipment_order_no) {
+                return $query->where('shipment_order_no', 'LIKE', '%'. $shipment_order_no .'%');
+            })
+            ->select('order_no')
+            ->get()->toArray();
+
+        return array_unique(array_column($result,'order_no'));
+    }
 }

+ 5 - 0
app/Service/CommandService.php

@@ -68,6 +68,10 @@ class CommandService extends Service
             //主表数据写入
             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);
@@ -79,6 +83,7 @@ class CommandService extends Service
                 $result[$key]['production_quantity'] = $production_num;
                 $result[$key]['production_time'] = $time;
                 $result[$key]['crt_id'] = $user['id'];
+                $result[$key]['status'] = $auto;
             }
             OrdersProduct::insert($result);
 

+ 255 - 11
app/Service/DeleteOrderService.php

@@ -2,6 +2,10 @@
 
 namespace App\Service;
 
+use App\Model\ApplyOrder;
+use App\Model\ApplyOrderForYy;
+use App\Model\Box;
+use App\Model\BoxDetail;
 use App\Model\CommandList;
 use App\Model\Dispatch;
 use App\Model\DispatchEmpSub;
@@ -34,6 +38,8 @@ class DeleteOrderService extends Service
             case 4:
 //                list($status,$msg) = $this->delFinished($data['id']);
                 list($status,$msg) = [false,'删除失败!'];
+            case 5:
+                list($status,$msg) = $this->delBox($data['id']);
                 break;
             default:
                 list($status,$msg) = [false,'删除失败!'];
@@ -99,17 +105,11 @@ 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;
+        $bool = OrdersProduct::whereIn('id', $id)
+            ->where('status','>',OrdersProduct::status_zero)
+            ->exists();
+        if($bool) return [false,'生产订单已审核,删除失败!'];
+
         try {
             DB::beginTransaction();
 
@@ -178,6 +178,11 @@ class DeleteOrderService extends Service
 
     //派工单删除
     public function delDispatch($id){
+        $bool = DispatchSub::whereIn('id', $id)
+            ->where('status','>',DispatchSub::status_zero)
+            ->exists();
+        if($bool) return [false,'工序派工单已审核,删除失败!'];
+
         //校验工序派工单是否完工
         $dispatch = DispatchSub::where('del_time',0)
             ->whereIn('id',$id)
@@ -320,4 +325,243 @@ class DeleteOrderService extends Service
 
         return [true,''];
     }
+
+    //包装单删除
+    public function delBox($id){
+        $box = Box::whereIn('id', $id)->get()->toArray();
+
+        try {
+            DB::beginTransaction();
+
+            $order_no = [];
+            foreach ($box as $value){
+                if($value['status'] > Box::status_zero) return [false, '包装单已审核,删除失败'];
+                $order_no[] = $value['order_no'];
+            }
+            $time = time();
+
+            Box::whereIn('id',$id)->update([
+                'del_time' => $time
+            ]);
+
+            foreach ($box as $value){
+                $model = new BoxDetail(['channel'=> $value['top_order_no']]);
+                $model->where('del_time',0)
+                    ->where('order_no',$value['order_no'])
+                    ->update(['del_time' => $time]);
+            }
+
+            DB::commit();
+        }catch (\Throwable $e){
+            DB::rollBack();
+            return [false,$e->getMessage()];
+        }
+
+        return [true,''];
+    }
+
+    public function checkOrders($data,$user){
+        if($this->isEmpty($data,'id')) return [false,'数据必须选择!'];
+        if($this->isEmpty($data,'type')) return [false,'单据类型不能为空!'];
+        if($this->isEmpty($data,'check')) return [false,'审核操作数值不能为空!'];
+
+        switch ($data['type']){
+            case 1:
+                list($status,$msg) = $this->checkOrdersProduct($data);
+                break;
+            case 2:
+                list($status,$msg) = $this->checkDispatch($data);
+                break;
+            case 3:
+                list($status,$msg) = $this->checkFinish($data);
+                break;
+            case 4:
+                list($status,$msg) = $this->checkBox($data);
+                break;
+            case 5:
+                list($status,$msg) = $this->checkSP($data,$user);
+                break;
+            default:
+                list($status,$msg) = [false,'审核失败'];
+        }
+
+        return [$status,$msg];
+    }
+
+    public function checkOrdersProduct($data){
+        $order = OrdersProduct::where('del_time',0)
+            ->whereIn('id',$data['id'])
+            ->get()->toArray();
+
+        $check = $data['check'];// 1 通过  2 弃审
+        if($check == 1){
+            $status = 1;
+        }else{
+            $status = 0;
+        }
+
+        try {
+            DB::beginTransaction();
+
+            foreach ($order as $value){
+                if($check == 1 && $value['status'] > OrdersProduct::status_zero) return [false, '生产订单已审核,操作失败'];
+                if($check == 2 && $value['status'] < OrdersProduct::status_one) return [false, '生产订单未审核,操作失败'];
+            }
+
+            //生产订单
+            OrdersProduct::whereIn('id',$data['id'])->update([
+                'status' => $status
+            ]);
+
+            DB::commit();
+        }catch (\Throwable $e){
+            DB::rollBack();
+            return [false,$e->getMessage()];
+        }
+
+        return [true,''];
+    }
+
+    public function checkDispatch($data){
+        $order = DispatchSub::where('del_time',0)
+            ->whereIn('id',$data['id'])
+            ->get()->toArray();
+
+        $check = $data['check'];// 1 通过  2 弃审
+        if($check == 1){
+            $status = 1;
+        }else{
+            $status = 0;
+        }
+
+        try {
+            DB::beginTransaction();
+
+            foreach ($order as $value){
+                if($check == 1 && $value['status'] > DispatchSub::status_zero) return [false, '派工单已审核,操作失败'];
+                if($check == 2 && $value['status'] < DispatchSub::status_one) return [false, '派工单未审核,操作失败'];
+            }
+
+            DispatchSub::whereIn('id',$data['id'])->update([
+                'status' => $status
+            ]);
+
+            DB::commit();
+        }catch (\Throwable $e){
+            DB::rollBack();
+            return [false,$e->getMessage()];
+        }
+
+        return [true,''];
+    }
+
+    public function checkFinish($data){
+        $order = DispatchSub::where('del_time',0)
+            ->whereIn('id',$data['id'])
+            ->get()->toArray();
+
+        $check = $data['check'];// 1 通过  2 弃审
+        if($check == 1){
+            $status = 1;
+        }else{
+            $status = 0;
+        }
+
+        try {
+            DB::beginTransaction();
+
+            foreach ($order as $value){
+                if($check == 1 && $value['wg_status'] > DispatchSub::status_zero) return [false, '完工单已审核,操作失败'];
+                if($check == 2 && $value['wg_status'] < DispatchSub::status_one) return [false, '完工单未审核,操作失败'];
+            }
+
+            DispatchSub::whereIn('id',$data['id'])->update([
+                'wg_status' => $status
+            ]);
+
+            DB::commit();
+        }catch (\Throwable $e){
+            DB::rollBack();
+            return [false,$e->getMessage()];
+        }
+
+        return [true,''];
+    }
+
+    public function checkBox($data){
+        $order = Box::where('del_time',0)
+            ->whereIn('id',$data['id'])
+            ->get()->toArray();
+
+        $check = $data['check'];// 1 通过  2 弃审
+        if($check == 1){
+            $status = 1;
+        }else{
+            $status = 0;
+        }
+
+        try {
+            DB::beginTransaction();
+
+            foreach ($order as $value){
+                if($check == 1 && $value['status'] > Box::status_zero) return [false, '包装单已审核,操作失败'];
+                if($check == 2 && $value['status'] < Box::status_one) return [false, '包装单未审核,操作失败'];
+            }
+
+            Box::whereIn('id',$data['id'])->update([
+                'status' => $status
+            ]);
+
+            DB::commit();
+        }catch (\Throwable $e){
+            DB::rollBack();
+            return [false,$e->getMessage()];
+        }
+
+        return [true,''];
+    }
+
+    public function checkSP($data,$user){
+        $order = ApplyOrder::where('del_time',0)
+            ->whereIn('id',$data['id'])
+            ->get()->toArray();
+
+        $check = $data['check'];// 1 通过  2 弃审
+        if($check == 1){
+            $status = 1;
+        }else{
+            $status = 0;
+        }
+
+        try {
+            DB::beginTransaction();
+
+            foreach ($order as $value){
+                if($check == 1 && $value['status'] > ApplyOrder::state_zero) return [false, '申请单已审核,操作失败'];
+                if($check == 2 && $value['status'] < ApplyOrder::state_one) return [false, '申请单未审核,操作失败'];
+            }
+
+            ApplyOrder::whereIn('id',$data['id'])->update([
+                'status' => $status
+            ]);
+
+            DB::commit();
+
+            if($check == 1){
+                $result = ApplyOrderForYy::whereIn('apply_order_id', $data['id'])->get()->toArray();
+
+                if(! empty($result)){
+                    $service = new FinishedOrderService();
+                    foreach ($result as $value){
+                        list($status, $msg) = $service->insertYy($value,$user);
+                    }
+                }
+            }
+        }catch (\Throwable $e){
+            DB::rollBack();
+            return [false,$e->getMessage()];
+        }
+
+        return [true,''];
+    }
 }

+ 209 - 5
app/Service/DispatchService.php

@@ -2,14 +2,12 @@
 
 namespace App\Service;
 
+use App\Model\ApplyOrder;
 use App\Model\Dispatch;
 use App\Model\DispatchEmpSub;
 use App\Model\DispatchSub;
 use App\Model\Employee;
-use App\Model\EmployeeTeamPermission;
 use App\Model\Equipment;
-use App\Model\FinishedOrderSub;
-use App\Model\Orders;
 use App\Model\OrdersProduct;
 use App\Model\OrdersProductProcess;
 use App\Model\Process;
@@ -52,19 +50,44 @@ class DispatchService extends Service
             DB::beginTransaction();
 
             $time = time();
+            $insert_data = [];
             if($data['is_split']){
                 foreach ($msg as $value){
                     list($s,$m) = $this->insertDispatch([$value],$data,$user,$time);
                     if(! $s) return [false,$m];
+
+                    foreach ($m as $v_m){
+                        $insert_data[] = $v_m;
+                    }
                 }
             }else{
                 list($s,$m) = $this->insertDispatch($msg,$data,$user,$time);
                 if(! $s) return [false, $m];
+
+                $insert_data = $m;
+            }
+            $insert = [];$tmp = [];
+            if(! empty($insert_data)){
+                foreach ($insert_data as $value){
+                    $str = $value['order_product_id'] . $value['crt_time'] . $value['product_no'] . $value['technology_name'];
+                    if(! in_array($str, $tmp)) {
+                        $insert[] = [
+                            'id' => $value['id'],
+                            'quantity' => $value['quantity'],
+                            'product_no' => $value['product_no'],
+                        ];
+                        $tmp[] = $str;
+                    }
+                }
             }
 
             //反写已派工数量
             $this->writeDispatchQuantity(array_column($msg,'order_product_id'));
 
+            //是否自动审核 领料申请单
+            list($status, $msg) = $this->createSQ($insert, $user);
+            if(! $status) return [false, $msg];
+
             DB::commit();
         }catch (\Exception $e){
             DB::rollBack();
@@ -74,6 +97,73 @@ class DispatchService extends Service
         return [true,''];
     }
 
+    public function createSQ($insert_data, $user){
+        try {
+            DB::beginTransaction();
+
+            //是否自动审核领料申请单
+            $em = new EmployeeService();
+            $auto = $em->is_auto($user, "llsq_auto");
+
+            $return = [];
+            $product_no = array_unique(array_column($insert_data,'product_no'));
+            //获取原料
+            $service = new FyyOrderService();
+            list($status, $msg) = $service->getProductDataFromSqlServer(['product_no' => $product_no], $user);
+            if($status) $return = $msg;
+
+            //组织原材料写入数据
+            $insert = [];
+            foreach ($insert_data as $value){
+                $t = $return[$value['product_no']] ?? [];
+                $tmp = [
+                    'id' => $value['id'],
+                    'quantity' => $value['quantity'] ?? 0,
+                    'product_no' => "",
+                    'product_title' => "",
+                    'product_size' => "",
+                    'product_unit' => "",
+                ];
+                if(! empty($t)){
+                    foreach ($t as $v){
+                        $tmp['product_no'] = $v['product_no'];
+                        $tmp['product_title'] = $v['product_title'];
+                        $tmp['product_size'] = $v['product_size'];
+                        $tmp['product_unit'] = $v['product_unit'];
+                        $insert[] = $tmp;
+                    }
+                }else{
+                    $tmp['quantity'] = 0;
+                    $insert[] = $tmp;
+                }
+            }
+
+            //生成申请单
+            $service = new ApplyOrderService();
+            list($status, $msg) = $service->createSQ($insert, $user, ApplyOrder::type_one, $auto);
+            if(! $status) {
+                DB::rollBack();
+                return [false, $msg];
+            }
+
+            if($auto) {
+                //生成流水
+                list($status, $msg) = $service->createRecord($msg);
+                if(! $status) {
+                    DB::rollBack();
+                    return [false, $msg];
+                }
+            }
+
+            DB::commit();
+        }catch (\Throwable $exception){
+            DB::rollBack();
+            return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
+        }
+
+        return [true, ''];
+    }
+
     public function makeData($equipment_id, $team_id,$employee_id,$message){
         $arr = [];
         if(! empty($equipment_id)){
@@ -161,10 +251,15 @@ class DispatchService extends Service
         $time_tmp = date("Ymd", $data['out_order_no_time'][0]);
         $process_model = new OrdersProductProcess(['channel' => $time_tmp]);
 
+        //是否自动审核
+        $em = new EmployeeService();
+        $auto = $em->is_auto($user, "pg_auto");
+
         foreach ($result as $key => $value){
             $result[$key]['dispatch_no'] = $dispatch_no;
             $result[$key]['crt_time'] = $time;
             $result[$key]['crt_id'] = $user['id'];
+            $result[$key]['status'] = $auto;
 
             $dispatch_quantity = $value['dispatch_quantity'] * 1000;
             $process_model->where('order_product_id',$value['order_product_id'])
@@ -179,7 +274,15 @@ class DispatchService extends Service
 
         DispatchSub::insert($result);
 
-        return [true,''];
+        //获取上一次插入订单的所有id
+        $last_insert_id = DispatchSub::where('dispatch_no',$dispatch_no)
+            ->where('crt_time',$time)
+            ->where('crt_id',$user['id'])
+            ->select('id','dispatch_quantity as quantity',"product_no","order_product_id","product_no",'technology_name','crt_time')
+            ->orderBy('order_product_id')
+            ->get()->toArray();
+
+        return [true, $last_insert_id];
     }
 
     public function del($data){
@@ -381,6 +484,7 @@ class DispatchService extends Service
             }else{
                 $data['data'][$key]['is_create'] = 0;
             }
+            $data['data'][$key]['status_title'] = DispatchSub::$status_name[$value['status']] ?? "";
         }
         $data['production_quantity'] = $this->getTotal($data['data'], 'production_quantity');
         $data['dispatch_quantity'] = $this->getTotal($data['data'], 'dispatch_quantity');
@@ -454,13 +558,22 @@ class DispatchService extends Service
 
     public function dispatchOrderList($data){
         $model = DispatchSub::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','production_quantity','dispatch_no','status','crt_id','process_id','dispatch_time_start','dispatch_time_end','crt_time','finished_num','waste_num','customer_name','order_product_id','out_order_no','team_id','device_id')
+            ->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','production_quantity','dispatch_no','crt_id','process_id','dispatch_time_start','dispatch_time_end','crt_time','finished_num','waste_num','customer_name','order_product_id','out_order_no','team_id','device_id','wg_status','status')
             ->orderBy('id','desc');
 
+        if(isset($data['status'])) $model->where('status', $data['status']);
+        if(isset($data['wg_status'])) $model->where('wg_status', $data['wg_status']);
         if(isset($data['finished_num'])) $model->where('finished_num', '>',0);
         if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
         if(! empty($data['dispatch_no'])) $model->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%');
         if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
+        if(! empty($data['production_no'])) {
+            $id = OrdersProduct::where('del_time', 0)
+                ->where('production_no', 'LIKE', '%'.$data['production_no'].'%')
+                ->select('id')
+                ->get()->toArray();
+            $model->whereIn('order_product_id', array_column($id,'id'));
+        }
         if(! empty($data['process_id'])) $model->where('process_id',$data['process_id']);
         if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
         if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
@@ -498,6 +611,8 @@ class DispatchService extends Service
             ->toArray();
 
         foreach ($data['data'] as $key => $value){
+            $data['data'][$key]['wg_status_title'] = DispatchSub::$status_name[$value['wg_status']] ?? "";
+            $data['data'][$key]['status_title'] = DispatchSub::$status_name[$value['status']] ?? "";
             $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']) : '';
@@ -689,4 +804,93 @@ class DispatchService extends Service
 
         return [true, $return];
     }
+
+    public function dispatchOrderForSqList($data,$user){
+        $model = DispatchSub::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','production_quantity','crt_id','process_id','dispatch_time_start','dispatch_time_end','crt_time','finished_num','waste_num','customer_name','order_product_id','out_order_no','team_id','device_id','wg_status','status',DB::raw('GROUP_CONCAT(DISTINCT dispatch_no ORDER BY dispatch_no SEPARATOR ",") as dispatch_no'))
+            ->orderBy('id','desc')
+            ->groupBy('order_product_id','crt_time');
+
+        if(isset($data['status'])) $model->where('status', $data['status']);
+        if(isset($data['wg_status'])) $model->where('wg_status', $data['wg_status']);
+        if(isset($data['finished_num'])) $model->where('finished_num', '>',0);
+        if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
+        if(! empty($data['dispatch_no'])) $model->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%');
+        if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
+        if(! empty($data['production_no'])) {
+            $id = OrdersProduct::where('del_time', 0)
+                ->where('production_no', 'LIKE', '%'.$data['production_no'].'%')
+                ->select('id')
+                ->get()->toArray();
+            $model->whereIn('order_product_id', array_column($id,'id'));
+        }
+        if(! empty($data['process_id'])) $model->where('process_id',$data['process_id']);
+        if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
+        if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
+        if(! empty($data['dispatch_time'][0]) && ! empty($data['dispatch_time'][1])){
+            $model->where('dispatch_time_start','<=',$data['dispatch_time'][0]);
+            $model->where('dispatch_time_end','>=',$data['dispatch_time'][1]);
+        }
+        if(! empty($data['team_id'])) $model->where('team_id',$data['team_id']);
+        if(! empty($data['device_id'])) $model->whereIn('device_id',$data['device_id']);
+        if(isset($data['is_finished'])) {
+            if($data['is_finished']){
+                $model->wherecolumn('dispatch_quantity','=','finished_num');
+            }else{
+                $model->wherecolumn('dispatch_quantity','>','finished_num');
+            }
+        }
+
+        $list = $this->limit($model,'',$data);
+        $list = $this->fillDispatchOrderForSqListData($list,$data,$user);
+
+        return [true,$list];
+    }
+
+    public function fillDispatchOrderForSqListData($data,$erg,$user){
+        if(empty($data['data'])) return $data;
+
+        $team_map = Team::whereIn('id',array_unique(array_column($data['data'],'team_id')))
+            ->pluck('title','id')
+            ->toArray();
+        $equipment_map = Equipment::whereIn('id',array_unique(array_column($data['data'],'device_id')))
+            ->pluck('title','id')
+            ->toArray();
+        $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
+            ->pluck('title','id')
+            ->toArray();
+
+        $orders = OrdersProduct::whereIn('id', array_column($data['data'],'order_product_id'))
+            ->pluck('production_no','id')
+            ->toArray();
+        $return = [];
+        if(! empty($erg['material'])){
+            $product_no = array_unique(array_column($data['data'],'product_no'));
+            $service = new FyyOrderService();
+            list($status, $msg) = $service->getProductDataFromSqlServer(['product_no' => $product_no], $user);
+            if($status) $return = $msg;
+        }
+
+        foreach ($data['data'] as $key => $value){
+            $data['data'][$key]['material'] = $return[$value['product_no']] ?? [];
+            $data['data'][$key]['wg_status_title'] = DispatchSub::$status_name[$value['wg_status']] ?? "";
+            $data['data'][$key]['status_title'] = DispatchSub::$status_name[$value['status']] ?? "";
+            $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
+            $data['data'][$key]['production_no'] = $orders[$value['order_product_id']] ?? '';
+
+            $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_time'] = $time1 . ' ' . $time2;
+            $data['data'][$key]['process_name'] = $process_map[$value['process_id']] ?? '';
+            $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'] = bcsub($value['dispatch_quantity'] , $value['finished_num'],3);
+        }
+
+        $total = $this->getTotal($data['data'], 'dispatch_quantity');
+        $data['dispatch_quantity'] = $total;
+
+        return $data;
+    }
 }

+ 105 - 0
app/Service/EmployeeService.php

@@ -3,6 +3,7 @@
 namespace App\Service;
 
 use App\Model\Depart;
+use App\Model\DXJ;
 use App\Model\Employee;
 use App\Model\EmployeeDepartPermission;
 use App\Model\EmployeeManagerDepart;
@@ -10,6 +11,7 @@ use App\Model\EmployeeMenuPermission;
 use App\Model\EmployeeRole;
 use App\Model\EmployeeTeamPermission;
 use App\Model\Role;
+use App\Model\Setting;
 use App\Model\SysMenu;
 use App\Model\Team;
 use Illuminate\Support\Facades\DB;
@@ -614,4 +616,107 @@ class EmployeeService extends Service
 
         return [true,''];
     }
+
+    public function settingList(){
+        $model = Setting::select('show_title','setting_name','setting_value')
+            ->orderby('sort', 'asc');
+        $list = $model->get()->toArray();
+
+        return [true, $list];
+    }
+
+    public function settingEdit($data){
+        $model = new Setting();
+        $model = $model->where('setting_name',$data['setting_name'])->first();
+        $model->setting_value = $data['setting_value'];
+        $model->save();
+
+        return [true,''];
+    }
+
+    public function dxjEdit($data){
+        list($status,$msg) = $this->dxjRule($data,false);
+        if(!$status) return [$status,$msg];
+
+        $update = $msg['data'][0];
+
+        $model = new DXJ();
+        $model->where('id',$data['id'])->update($update);
+
+        return [true,''];
+    }
+
+    public function dxjAdd($data){
+        list($status,$msg) = $this->dxjRule($data);
+        if(!$status) return [$status,$msg];
+
+        DXJ::insert($msg['data']);
+
+        return [true,''];
+    }
+
+    public function dxjDel($data){
+        DXJ::whereIn('id',$data['id'])->update([
+            'del_time'=>time()
+        ]);
+
+        return [true,''];
+    }
+
+    public function dxjList($data){
+        $model = DXJ::where('del_time',0)
+            ->select('title','id')
+            ->orderby('id', 'asc');
+        if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
+
+        $list = $model->get()->toArray();
+
+        return [true, $list];
+    }
+
+    public function dxjRule($data, $is_check = true){
+        if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
+
+        $title = array_column($data['data'],'title');
+        $title = array_map(function($val) {
+            return $val !== null ? $val : 0;
+        }, $title);
+        $title_count = array_count_values($title);
+        foreach ($title as $value){
+            if(empty($value)) return [false,'名称不能为空!'];
+            if($title_count[$value] > 1) return [false,'名称不能重复'];
+        }
+
+        foreach ($data['data'] as $key => $value){
+            $data['data'][$key]['upd_time'] = time();
+            if($is_check){
+                $data['data'][$key]['crt_time'] = time();
+                $bool = DXJ::where('title', $value['title'])
+                    ->where('del_time',0)
+                    ->exists();
+            }else{
+                if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
+
+                $bool = Depart::where('title', $value['title'])
+                    ->where('id','<>',$data['id'])
+                    ->where('del_time',0)
+                    ->exists();
+            }
+            if($bool) return [false,'名称不能重复'];
+        }
+
+        return [true, $data];
+    }
+
+    public function is_auto($user, $column = ""){
+        if(empty($column)) return 1;
+
+        //是否自动审核
+        $auto = $user['setting'][$column]['setting_value'] ?? 1;
+        $auto = intval($auto);
+        if($auto >= 1) $auto = 1;
+        else $auto = 0;
+
+        return $auto;
+    }
 }

+ 38 - 0
app/Service/FinishedOrderService.php

@@ -19,6 +19,7 @@ use App\Model\SaleOrdersProduct;
 use App\Model\Scrapp;
 use App\Model\ScrappCount;
 use App\Model\Team;
+use App\Service\Box\BoxService;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Redis;
 
@@ -59,6 +60,10 @@ class FinishedOrderService extends Service
         $time = time();
         try{
             DB::beginTransaction();
+
+            $em = new EmployeeService();
+            $auto = $em->is_auto($user, "wg_auto");
+            $last_update = $tmp = [];
             foreach ($result as $value){
                 $finished_num = $value['quantity'] + $value['finished_num'];
                 $waste_num = $value['waste_quantity'] + $value['waste_num'];
@@ -66,8 +71,22 @@ class FinishedOrderService extends Service
                 DispatchSub::where('id',$value['id'])->update([
                     'finished_num' => $finished_num,
                     'waste_num' => $waste_num,
+                    'wg_status' => $auto,
                 ]);
 
+                $str = $value['order_product_id'] . $value['crt_time'] . $value['product_no'] . $value['technology_name'];
+                if(! in_array($str, $tmp)){
+                    $last_update[] = [
+                        'id' => $value['id'],
+                        'quantity' => $value['quantity'],
+                        'product_no' => $value['product_no'] ?? "",
+                        'product_title' => $value['product_title'] ?? "",
+                        'product_size' => $value['product_size'] ?? "",
+                        'product_unit' => $value['product_unit'] ?? "",
+                    ];
+                    $tmp[] = $str;
+                }
+
                 $insert_waste = $scrapp = [];
                 if(! empty($value['waste_array'])){
                     foreach ($value['waste_array'] as $v){
@@ -150,6 +169,11 @@ class FinishedOrderService extends Service
             //销售订单
             $this->writeFinishedQuantity($id);
 
+            //生成完工入库申请单
+            $service = new BoxService();
+            list($status, $msg) = $service->createWGSQ($last_update, $user);
+            if(! $status) return [false, $msg];
+
             DB::commit();
         }catch (\Exception $e){
             DB::rollBack();
@@ -956,4 +980,18 @@ class FinishedOrderService extends Service
 
         return $mysql;
     }
+
+    public function insertYy($order, $user){
+        //包装单
+        $package_data = json_decode($order['post'], true);
+
+        if(! empty($package_data)) {
+            //用友 ------产成品入库
+            $service = new FinishedOrderService();
+            list($status,$msg) = $service->U8Rdrecord10Save($package_data,$user);
+            if(! $status) return [false, $msg];
+        }
+
+        return [true, ''];
+    }
 }

+ 27 - 0
app/Service/FyyOrderService.php

@@ -476,4 +476,31 @@ class FyyOrderService extends Service
 
         return [true,$result];
     }
+
+    public function getStorehouseDataFromSqlServer($data,$user){
+        $sqlServerModel = new FyySqlServerService($user);
+        if($sqlServerModel->error) return [false,$sqlServerModel->error];
+
+        $result = $sqlServerModel->getStorehouseDataFromSqlServer($data);
+
+        return [true, $result];
+    }
+
+    public function getProductDataFromSqlServer($data,$user){
+        $sqlServerModel = new FyySqlServerService($user);
+        if($sqlServerModel->error) return [false,$sqlServerModel->error];
+
+        $result = $sqlServerModel->getProductFromSqlServer($data);
+
+        return [true, $result];
+    }
+
+    public function getProductBzDataFromSqlServer($data,$user){
+        $sqlServerModel = new FyySqlServerService($user);
+        if($sqlServerModel->error) return [false,$sqlServerModel->error];
+
+        $result = $sqlServerModel->getProductBzFromSqlServer($data);
+
+        return [true, $result];
+    }
 }

+ 234 - 0
app/Service/FyySqlServerService.php

@@ -7,6 +7,7 @@ use App\Model\Employee;
 use App\Model\ErrorTable;
 use App\Model\Orders;
 use App\Model\SaleOrdersProduct;
+use App\Model\SalesFrom;
 use Illuminate\Support\Facades\Config;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Log;
@@ -652,4 +653,237 @@ class FyySqlServerService extends Service
             'order_no' => $data['order_no'] ?? ""
         ]);
     }
+
+    public function getStorehouseDataFromSqlServer($data)
+    {
+        if (!empty($this->error)) return [false, $this->error, ''];
+
+        $model = $this->db->table('Warehouse as a')
+            ->select('cWhCode as code', 'cWhName as name');
+
+        if (!empty($data['code'])) $model->where('cWhCode', 'LIKE', '%' . $data['code'] . '%');
+        if (!empty($data['name'])) $model->where('cWhName', 'LIKE', '%' . $data['name'] . '%');
+
+        $list = $this->limit($model, '', $data);
+
+        return $list;
+    }
+
+    //获取产品的原材料
+    public function getProductFromSqlServer($data){
+        if (!empty($this->error)) return [false, $this->error, ''];
+
+        $return = [];
+        $result = $this->db->table('Inventory')
+            ->whereIn('cInvCode', $data['product_no'])
+            ->select('cInvCode as product_code','CINVDEFINE1 as placode', 'CINVDEFINE2 as paper')
+            ->get()
+            ->toArray();
+        $product_code = [];
+        foreach ($result as $value){
+            if(! in_array($value->placode, $product_code) && $value->placode) $product_code[] = $value->placode;
+            if(! in_array($value->paper, $product_code) && $value->paper) $product_code[] = $value->paper;
+        }
+        if(! empty($product_code)){
+            $product_list = $this->db->table('Inventory as a')
+                ->join('ComputationUnit as b', function ($join) {
+                    $join->on('a.cGroupCode', '=', 'b.cGroupCode')
+                        ->on('a.cComUnitCode', '=', 'b.cComUnitCode');
+                }, null, null, 'left')
+                ->whereIn('a.cInvCode', $product_code)
+                ->select('a.cInvCode as product_no', 'a.cInvName as product_title', 'a.cInvStd as product_size', 'b.cComUnitName as product_unit')
+                ->get()
+                ->toArray();
+            $map = array_column($product_list, null, 'product_no');
+            foreach ($result as $value){
+                $tmp = [];
+                if(! empty($value->placode)) $tmp[] = (array)$map[$value->placode] ?? [];
+                if(! empty($value->paper)) $tmp[] = (array)$map[$value->paper] ?? [];
+                $return[$value->product_code] = $tmp;
+            }
+        }
+
+        return $return;
+    }
+
+    //获取产品的包装材料
+    public function getProductBzFromSqlServer($data){
+        if (!empty($this->error)) return [false, $this->error, ''];
+
+        $return = [];
+        $result = $this->db->table('Inventory')
+            ->whereIn('cInvCode', $data['product_no'])
+            ->select('cInvCode as product_code','CINVDEFINE3 as bz')
+            ->get()
+            ->toArray();
+        $product_code = [];
+        foreach ($result as $value){
+            if(! in_array($value->bz, $product_code) && $value->bz) $product_code[] = $value->bz;
+        }
+        if(! empty($product_code)){
+            $product_list = $this->db->table('Inventory as a')
+                ->join('ComputationUnit as b', function ($join) {
+                    $join->on('a.cGroupCode', '=', 'b.cGroupCode')
+                        ->on('a.cComUnitCode', '=', 'b.cComUnitCode');
+                }, null, null, 'left')
+                ->whereIn('a.cInvCode', $product_code)
+                ->select('a.cInvCode as product_no', 'a.cInvName as product_title', 'a.cInvStd as product_size', 'b.cComUnitName as product_unit')
+                ->get()
+                ->toArray();
+            $map = array_column($product_list, null, 'product_no');
+            foreach ($result as $value){
+                if(isset($map[$value->bz])){
+                    $return[$value->product_code][] = (array)$map[$value->bz];
+                }
+            }
+        }
+
+        return $return;
+    }
+
+    //获取销售单客户来源
+    public function getCustomerFromSqlServer1($data){
+        if (! empty($this->error)) return [false, $this->error, ''];
+
+        $result = $this->db->table('SO_SOMain')
+            ->whereIn('cSOCode', $data['sale_order'])
+            ->select('cCusCode as customer_no', DB::raw("count(cCusCode) as num"))
+            ->groupBy('cCusCode')
+            ->get()
+            ->toArray();
+        if(empty($result)) return [true, []];
+        $map = [];
+        foreach ($result as $value){
+            $map[$value->customer_no] = $value->num;
+        }
+
+        $return = [];
+        $customer = $this->db->table('Customer')
+            ->whereIn('cCusCode', array_column($result,'customer_no'))
+            ->select('cCusCode as customer_no', 'cDCCode as address')
+            ->get()
+            ->toArray();
+        foreach ($customer as $value){
+            if(empty($value->address)) continue;
+            $num = $map[$value->customer_no];
+            if(isset($return[$value->address])){
+                $return[$value->address] += $num;
+            }else{
+                $return[$value->address] = $num;
+            }
+        }
+
+        return [true, $return];
+    }
+
+    public function insertSaleOrderFrom1(){
+        $time = time();
+        $return = [];
+        $address_map = config('address');
+        foreach ($address_map as $value){
+            $return[$value['value']] = 0;
+        }
+
+        try {
+            Orders::where('del_time', 0)
+                ->select('out_order_no')
+                ->orderBy('id','desc')
+                ->chunk(200, function ($records) use(&$return) {
+                    $out_order_no = [];
+                    foreach ($records as $record){
+                        $out_order_no[] = $record->out_order_no;
+                    }
+
+                    $sqlServerModel = new FyySqlServerService(['id' => 1, 'zt' => '001']);
+                    list($status,$msg) = $sqlServerModel->getCustomerFromSqlServer(['sale_order' => $out_order_no]);
+                    if($status){
+                        foreach ($return as $key => $value){
+                            if(isset($msg[$key])){
+                                $return[$key] += $msg[$key];
+                            }
+                        }
+                    }
+
+                    echo '更新中--------' . "\n";
+                });
+
+            $insert = [];
+            foreach ($return as $key => $value){
+                $insert[] = [
+                    'code' => $key,
+                    'num' => $value,
+                    'crt_time' => $time
+                ];
+            }
+            SalesFrom::where('del_time', 0)
+                ->update(['del_time' => $time]);
+
+            SalesFrom::insert($insert);
+
+            echo '更新结束--------' . "\n";
+        }catch (\Throwable $exception){
+            echo $exception->getMessage() . "\n";
+        }
+    }
+
+    //获取客户来源
+    public function getCustomerFromSqlServer(){
+        if (! empty($this->error)) return [false, $this->error, ''];
+
+        $return = [];
+        $customer = $this->db->table('Customer')
+            ->select('cCusCode as customer_no', 'cDCCode as address')
+            ->get()->toArray();
+
+        foreach ($customer as $value){
+            if(empty($value->address)) continue;
+            if(isset($return[$value->address])){
+                $return[$value->address] += 1;
+            }else{
+                $return[$value->address] = 1;
+            }
+        }
+
+        return [true, $return];
+    }
+
+    public function insertSaleOrderFrom(){
+        $time = time();
+        $return = [];
+        $address_map = config('address');
+        foreach ($address_map as $value){
+            $return[$value['value']] = 0;
+        }
+
+        try {
+            $sqlServerModel = new FyySqlServerService(['id' => 1, 'zt' => '001']);
+            list($status,$msg) = $sqlServerModel->getCustomerFromSqlServer();
+            if($status){
+                foreach ($return as $key => $value){
+                    if(isset($msg[$key])){
+                        $return[$key] += $msg[$key];
+                    }
+                }
+            }
+
+            echo '更新中--------' . "\n";
+
+            $insert = [];
+            foreach ($return as $key => $value){
+                $insert[] = [
+                    'code' => $key,
+                    'num' => $value,
+                    'crt_time' => $time
+                ];
+            }
+            SalesFrom::where('del_time', 0)
+                ->update(['del_time' => $time]);
+
+            SalesFrom::insert($insert);
+
+            echo '更新结束--------' . "\n";
+        }catch (\Throwable $exception){
+            echo $exception->getMessage() . "\n";
+        }
+    }
 }

+ 1 - 0
app/Service/ProductionOrderService.php

@@ -264,6 +264,7 @@ class ProductionOrderService extends Service
             }else{
                 $data['data'][$key]['is_create'] = 0;
             }
+            $data['data'][$key]['status_title'] = OrdersProduct::$status_name[$value['status']] ?? "";
         }
 
         return $data;

+ 26 - 0
routes/api.php

@@ -30,6 +30,7 @@ Route::any('getProductionOrder/{file_name}','Api\ProductionOrderController@getIt
 Route::any('getCloudData','Api\CloudDataController@getCloudData');
 Route::any('delTestQ','Api\TestController@delTestQ');//包装
 Route::any('delTestQ2','Api\TestController@delTestQ2');//生产
+Route::any('test','Api\TestController@test');//测试
 
 Route::any('output_value','Api\ScreenController@output_value');
 Route::any('order_process1','Api\ScreenController@order_process1');
@@ -149,6 +150,9 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('dispatchOrderList', 'Api\DispatchController@dispatchOrderList');
     //工序派工单列表 pc端-------
 
+    //申请单使用的
+    $route->any('dispatchOrderForSqList', 'Api\DispatchController@dispatchOrderForSqList');
+
     //工序派工单列表 手机端-------
     $route->any('finishedOrderList', 'Api\DispatchController@dispatchMobileOrderList');
     //工序派工单列表 手机端-------
@@ -163,6 +167,8 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('finishedOrderDetail', 'Api\FinishedOrderController@orderDetail');
     //pc端 工序完工------------
 
+    //审核统一入口
+    $route->any('checkOrders','Api\DeleteOrderController@checkOrders');
     //删除单据统一入口
     $route->any('delOrders','Api\DeleteOrderController@del');
 
@@ -214,6 +220,7 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('getHeaderWord', 'Api\HeaderWordController@getHeaderWord');
     //包装相关
     $route->any('boxDetail', 'Api\BoxController@boxDetail');
+    $route->any('boxList', 'Api\BoxController@boxList');
     $route->any('boxOrderDetail', 'Api\BoxController@boxOrderDetail');
     $route->any('boxProductList', 'Api\BoxController@boxProductList');
     $route->any('boxOrderGroupGwp', 'Api\BoxController@boxOrderGroupGwp');
@@ -251,4 +258,23 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('deviceOrderOverView','Api\InspectController@deviceOrderOverView');//总览
     $route->any('deviceOrderXj','Api\InspectController@deviceOrderXj');//巡检任务
     $route->any('deviceOrderWx','Api\InspectController@deviceOrderWx');//维修
+
+    $route->any('dxjAdd', 'Api\EmployeeController@dxjAdd');
+    $route->any('dxjEdit', 'Api\EmployeeController@dxjEdit');
+    $route->any('dxjDel', 'Api\EmployeeController@dxjDel');
+    $route->any('dxjList', 'Api\EmployeeController@dxjList');
+
+    //申请单
+    $route->any('applyOrderList', 'Api\ApplyOrderController@materialList');
+    $route->any('applyOrderDetail', 'Api\ApplyOrderController@materialDetail');
+    $route->any('applyOrderEdit', 'Api\ApplyOrderController@materialEdit');
+    $route->any('applyOrderAdd', 'Api\ApplyOrderController@materialAdd');
+    $route->any('applyOrderDel', 'Api\ApplyOrderController@materialDel');
+
+    //仓库
+    $route->any('storeHouseList', 'Api\FyyOrderController@getStorehouseDataFromSqlServer');
+
+    //设置列表
+    $route->any('settingList', 'Api\EmployeeController@settingList');
+    $route->any('settingEdit', 'Api\EmployeeController@settingEdit');
 });