cqpCow 1 năm trước cách đây
mục cha
commit
474c3a9a05
1 tập tin đã thay đổi với 128 bổ sung43 xóa
  1. 128 43
      app/Service/CheckService.php

+ 128 - 43
app/Service/CheckService.php

@@ -2,7 +2,12 @@
 
 namespace App\Service;
 
+use App\Model\InOutRecord;
+use App\Model\ProductInventory;
+use App\Model\PurchaseOrder;
+use App\Model\PurchaseOrderInfo;
 use App\Model\SalesOrder;
+use App\Model\Setting;
 use Illuminate\Support\Facades\DB;
 
 class CheckService extends Service
@@ -10,7 +15,7 @@ class CheckService extends Service
     //审批操作对应的数值
     const one = 1; //收货
     const two = 2; //发货
-    const three = 3; //采购
+    const three = 3; //采购
     const four = 4; //销售订单
     const five = 5; //施工单
 
@@ -18,24 +23,75 @@ class CheckService extends Service
     public $map = [
         self::one => '收货单',
         self::two => '发货单',
-        self::three => '采购单',
+        self::three => '采购单',//确认后入库
         self::four => '销售订单',
         self::five => '施工单',
     ];
 
+    //入库操作
+    public static $in_opt = [
+        self::three,
+    ];
+
+    //出库操作
+    public static $out_opt = [
+        self::five,
+    ];
+
     const TYPE_ONE = 1;//通过
     const TYPE_TWO = 2;//不通过
 
+    //单据操作
     public static $opt_case = [
-        self::four => 'confirmSalesOrder'
+        self::three => 'confirmPurchaseOrder',
+        self::four => 'confirmSalesOrder',
     ];
 
+    //单据库存流水
     public static $record = [
-
+        self::three => 'recordPurchaseOrder',
     ];
 
+    public function confirmPurchaseOrder($data){
+        $model = PurchaseOrder::where('id',$data['id'])
+            ->where('del_time',0)
+            ->first();
+        if(empty($model)) return [false, '采购订单不存在或已被删除'];
+        if($model->state != PurchaseOrder::STATE_ZERO) return [false, '请确认采购订单状态,操作失败'];
+
+        PurchaseOrder::where('del_time',0)->where('id',$data['id'])
+            ->update(['state' => PurchaseOrder::STATE_ONE]);
+
+        return [true, $model->toArray()];
+    }
+
+    public function recordPurchaseOrder($data, $order){
+        $result = PurchaseOrderInfo::where('del_time',0)
+            ->where('order_number',$order['order_number'])
+            ->get()->toArray();
+        if(empty($result)) return [false,'采购单产品信息不存在或已被删除'];
+        $insert = [];
+        foreach ($result as $value){
+            if(isset($insert[$value['product_id']])){
+                $insert[$value['product_id']]['number'] += $value['number'];
+            }else{
+                $insert[$value['product_id']] = [
+                    'product_id' => $value['product_id'],
+                    'number' => $value['number'],
+                    'order_type' => PurchaseOrder::prefix
+                ];
+            }
+        }
+        $insert = array_values($insert);
+        $bool = InOutRecord::insert($insert);
+        if(! $bool) return [false,'流水写入失败'];
+
+        return [true,''];
+    }
+
     public function confirmSalesOrder($data){
         $model = SalesOrder::where('id',$data['id'])->where('del_time',0)->first();
+        if(empty($model)) return [false, '销售订单不存在或已被删除'];
 
         if($data['type'] == self::TYPE_ONE){
             if($model->state == SalesOrder::State_one) return [false,'已锁定,操作失败!'];
@@ -46,25 +102,40 @@ class CheckService extends Service
         }
         $model->save();
 
-        return [true,$model];
+        return [true,$model->toArray()];
     }
 
     public function checkAll($data,$user){
         if(empty($data['id']) || empty($data['opt_case']) || empty($data['type'])) return [false,'必传参数不能为空或者参数值错误!'];
 
         //具体方法
-        $function = self::$opt_case[$data['opt_case']];
-
+        $function = self::$opt_case[$data['opt_case']] ?? "";
+        $record = self::$record[$data['opt_case']] ?? "";
         try{
             DB::beginTransaction();
 
-            //更新单据的状态 从待审变成已审核
             list($bool,$msg) = $this->$function($data);
 
             if(! $bool){
                 DB::rollBack();
                 return [false, $msg];
             }
+            $order = $msg;
+
+            if($record) {
+                //流水
+                $bool = $this->$record($data, $order);
+                if(! $bool) {
+                    DB::rollBack();
+                    return [false, $msg];
+                }
+                //库存
+                $bool = $this->changeInventory($data, $order);
+                if(! $bool) {
+                    DB::rollBack();
+                    return [false, $msg];
+                }
+            }
 
             DB::commit();
             return [true, ''];
@@ -72,46 +143,60 @@ class CheckService extends Service
             DB::rollBack();
             return [false, $exception->getMessage()];
         }
-
     }
 
-    public function createRecordAndInventory($data = []){
-        if(empty($data['order_number']) || empty($data['type']) || empty($data['opt_case']) || ! isset(self::$opt_case[$data['opt_case']])) return [false,300];
-
-        //具体方法
-        $function = self::$opt_case[$data['opt_case']];
-
-        try{
-            DB::beginTransaction();
-
-            //更新单据的状态
-            $bool = $this->$function($data);
-
-            if($bool && $data['type'] == self::TYPE_ONE && isset(self::$record[$data['opt_case']])){
-                //审批通过 创建流水
-                $function_record = self::$record[$data['opt_case']];
-
-                $boolean = $this->$function_record($data);
+    //更新库存
+    public function changeInventory($data,$order){
+        $number_symbol = ">";
+        if(in_array($data['opt_case'],self::$in_opt)){
+            $number_symbol = ">";
+        }elseif (in_array($data['opt_case'],self::$out_opt)){
+            $number_symbol = "<";
+        }
 
-                if(! $boolean) { //创建流水失败 数据库回滚
-                    DB::rollBack();
-                    return [false, 300];
-                }
+        //获取单据最新数据时间 正常审核的数据
+        $latest = InOutRecord::where('del_time',0)
+            ->where('order_number',$order['order_number'])
+            ->where('number',$number_symbol,0)
+            ->select('crt_time')
+            ->orderBy('crt_time', 'desc')
+            ->first();
+
+        $model = InOutRecord::where('del_time',0)
+            ->where('order_number',$order['order_number'])
+            ->where('number',$number_symbol,0)
+            ->select(DB::raw("sum(number) as number"),'crt_time','product_id');
+        if(! empty($latest)) {
+            $t = $latest->toArray();
+            $model->where('crt_time',$t['crt_time']);
+        }
 
-                //更新库存
-                $inventory = new InventoryService();
-                $boole = $inventory->changeInventory($data);
-                if(! $boole){
-                    DB::rollBack();
-                    return [false, 300];
-                }
+        $result = $model->get()->toArray();
+        if (empty($result)) return [false,'流水记录不存在'];
+
+        //是否锁定
+        $setting_map = Setting::where('setting_name','lock_number')
+            ->pluck('setting_value','setting_name')->toArray();
+        foreach ($result as $key => $value){
+            $m = ProductInventory::where('product_id',$value['product_id'])
+                ->select('product_id','number')
+                ->first();
+
+            if(empty($m)){
+                ProductInventory::insert($result[$key]);
+            }else{
+                $lock_number = 0;
+                if(! empty($setting_map['lock_number']) && in_array($data['opt_case'],self::$out_opt)) $lock_number = $value['number'];
+
+                ProductInventory::where('product_id',$m->product_id)
+                    ->lockForUpdate()
+                    ->update([
+                        'number' => DB::raw('number + ('. $value['number'] . ')'),
+                        'lock_number' => DB::raw('lock_number + ('. $lock_number . ')')
+                    ]);
             }
-
-            DB::commit();
-            return [true, 200];
-        }catch (\Throwable $exception){
-            DB::rollBack();
-            return [false, 201];
         }
+
+        return [true,''];
     }
 }