zhanglinxin vor 1 Jahr
Ursprung
Commit
a18cc99df3

+ 81 - 18
application/common/service/ApproveInfoService.php

@@ -156,27 +156,17 @@ class ApproveInfoService
             except('出现错误:' . $e->getMessage());
         }
 
-        // 4=领用申请 冻结库存
+        // 4=领用申请 增加冻结库存
         if ($module == CommonConstant::MODULE_4) {
-            $goodsStockSave = [];
-            $redis_stock = [];
-            foreach ($result['other']['data'] as $key=>$val){
-                foreach ($val['goods_stock'] as $k => $v) {
-                    $goodsStockSave[] = [
-                        'id' => $v['id'],
-                        'stock' => ['dec', $v['stock']],
-                        'freeze_stock' => ['inc', $v['stock']],
-                    ];
-                    $redis_stock[] = [
-                        'id'=>'dingtalk_stock_'.$val['info_id'].'_'.$v['goods_id'].'_'.$v['id'],
-                        'stock'=>$v['stock'],
-                    ];
-                }
-            }
+            list($goodsStockSave,$goodsStockLog,$redisStock) = self::composition_data($result['other']['data'],CommonConstant::MODULE_4,CommonConstant::STATUS_2);
             if ($goodsStockSave) {
+                // 批量更新商品库存
                 (new GoodsStock)->saveAll($goodsStockSave);
+            }
+            if($redisStock){
+                // 库存缓存记录
                 $redis = new Redis(config('redis.'));
-                foreach ($redis_stock as $key=>$val){
+                foreach ($redisStock as $key=>$val){
                     $redis->inc($val['id'], $val['stock']);
                 }
             }
@@ -345,7 +335,7 @@ class ApproveInfoService
     }
 
     /**
-     * 添加申购物品/入库物品/领用物
+     * 添加申购商品/入库商品/领用商
      *
      * @param integer $info_id 申请ID
      * @param integer $module 模块类型
@@ -716,6 +706,79 @@ class ApproveInfoService
         }
     }
 
+    /**
+     * 入库商品/领用商品组成数据
+     *
+     * @param mixed $goods 商品
+     * @param integer $module 模块
+     * @param integer $status 状态:2=审批中,3=审批同意,4=审批驳回
+     **/
+    public static function composition_data($goods,$module,$status){
+        $goodsStockSave = [];
+        $goodsStockLog = [];
+        $redisStock = [];
+        $type = 0;
+
+        foreach ($goods as $key => $val) {
+            $goods_stock = $status == CommonConstant::STATUS_2 ? $val['goods_stock'] : $val['goods_stock_text'];
+            foreach ($goods_stock as $k => $v) {
+                if($module == CommonConstant::MODULE_3){
+                    // 增加剩余库存
+                    $goodsStockSave[] = [
+                        'id' => $v['id'],
+                        'stock' => ['inc', $v['stock']],
+                    ];
+                }
+                if($module == CommonConstant::MODULE_4){
+                    if($status == CommonConstant::STATUS_2){
+                        // 扣除剩余库存 增加冻结库存
+                        $goodsStockSave[] = [
+                            'id' => $v['id'],
+                            'stock' => ['dec', $v['stock']],
+                            'freeze_stock' => ['inc', $v['stock']],
+                        ];
+                    }
+                    if($status == CommonConstant::STATUS_3){
+                        // 扣除冻结库存
+                        $goodsStockSave[] = [
+                            'id' => $v['id'],
+                            'freeze_stock' => ['dec', $v['stock']],
+                        ];
+                        $type = 1;
+                    }
+                    if($status == CommonConstant::STATUS_4){
+                        // 扣除冻结库存 增加剩余库存
+                        $goodsStockSave[] = [
+                            'id' => $v['id'],
+                            'stock' => ['inc', $v['stock']],
+                            'freeze_stock' => ['dec', $v['stock']],
+                        ];
+                    }
+                    $redisStock[] = [
+                        'id'=>'dingtalk_stock_'.$val['info_id'].'_'.$v['goods_id'].'_'.$v['id'],
+                        'stock'=>$v['stock'],
+                    ];
+                }
+                if($status == CommonConstant::STATUS_3) {
+                    $goodsStockLog[] = [
+                        'type' => $type,
+                        'info_id' => $val['info_id'],
+                        'goods_id' => $v['goods_id'],
+                        'stock_id' => $v['id'],
+                        'name' => $v['name'],
+                        'price' => $v['price'],
+                        'stock' => $v['stock'],
+                    ];
+                }
+            }
+        }
+        return [
+            $goodsStockSave,
+            $goodsStockLog,
+            $redisStock
+        ];
+    }
+
 
     /**
      * 列表

+ 59 - 52
application/common/service/ApproveService.php

@@ -16,6 +16,7 @@ use app\common\model\ApproveUseGoods;
 use app\common\model\GoodsStock;
 use app\common\model\GoodsStockLog;
 use app\common\model\User;
+use think\cache\driver\Redis;
 use think\Db;
 use think\Exception;
 
@@ -448,10 +449,10 @@ class ApproveService
                 }
                 // TODO 没有判断反馈状态
 
-                // 更新维修信息
-                $params['maintain_user_id'] = $info->maintain_user_id;
-                $params['feedback_status'] = CommonConstant::IS_WHO_1;
                 try {
+                    // 更新维修信息
+                    $params['maintain_user_id'] = $info->maintain_user_id;
+                    $params['feedback_status'] = CommonConstant::IS_WHO_1;
                     ApproveMaintain::where('info_id', $info->id)->update($params);
                 } catch (Exception $e) {
                     except('出现错误:' . $e->getMessage());
@@ -477,36 +478,58 @@ class ApproveService
         $approve_time = date('Y-m-d H:i:s');
         $duration = time() - strtotime($approve['approve_time']);
 
-        if ($status == CommonConstant::STATUS_3) {
-            if ($info->module == CommonConstant::MODULE_3) {
-                // 3=入库申请
-                $goods = $info->stock_goods;
-                if (!$goods->toArray()) {
-                    except('入库商品不存在');
-                }
-            }
-            if ($info->module == CommonConstant::MODULE_8) {
-                // 8=维修申请
-                $is_maintain = CommonService::isMaintain($user);
-                if ($is_maintain && !$maintain_user_id) {
-                    except('请选择维修人员');
-                }
-                // 维修人员ID
-                if ($maintain_user_id > 0) {
-                    $info->maintain_user_id = $maintain_user_id;
-                }
-            }
-        }
+        $goodsStockSave = [];
+        $goodsStockLog = [];
         if ($info->module == CommonConstant::MODULE_4) {
             // 4=领用申请
             $goods = $info->use_goods;
             if (!$goods->toArray()) {
                 except('领用商品不存在');
             }
+            if ($status == CommonConstant::STATUS_3) {
+                if ($approve['approve_flow'] < $info['approve_num']) {
+                } else {
+                    list($goodsStockSave,$goodsStockLog,$redisStock) = ApproveInfoService::composition_data($goods->toArray(),CommonConstant::MODULE_4,CommonConstant::STATUS_3);
+                }
+            }
+            if ($status == CommonConstant::STATUS_4) {
+                list($goodsStockSave,$goodsStockLog,$redisStock) = ApproveInfoService::composition_data($goods->toArray(),CommonConstant::MODULE_4,CommonConstant::STATUS_4);
+            }
+        }
+        if ($info->module == CommonConstant::MODULE_3 && $status == CommonConstant::STATUS_3) {
+            // 3=入库申请
+            $goods = $info->stock_goods;
+            if (!$goods->toArray()) {
+                except('入库商品不存在');
+            }
+            if ($approve['approve_flow'] < $info['approve_num']) {
+            } else{
+                list($goodsStockSave,$goodsStockLog,$redisStock) = ApproveInfoService::composition_data($goods->toArray(),CommonConstant::MODULE_3,CommonConstant::STATUS_3);
+            }
+        }
+        if ($info->module == CommonConstant::MODULE_8 && $status == CommonConstant::STATUS_3) {
+            // 8=维修申请
+            $is_maintain = CommonService::isMaintain($user);
+            if ($is_maintain && !$maintain_user_id) {
+                except('请选择维修人员');
+            }
+            // 维修人员ID
+            if ($maintain_user_id > 0) {
+                $info->maintain_user_id = $maintain_user_id;
+            }
         }
 
         Db::startTrans();
         try {
+            if ($goodsStockSave) {
+                // 批量更新商品库存
+                (new GoodsStock)->saveAll($goodsStockSave);
+            }
+            if ($goodsStockLog) {
+                // 批量添加商品入库出库记录
+                GoodsStockLog::insertAll($goodsStockLog);
+            }
+
             // 更新审批申请
             if ($status == CommonConstant::STATUS_3) {
                 // 审批流程批次小于审批次数 更新下一级审批状态
@@ -515,35 +538,6 @@ class ApproveService
                 } else {
                     // 审批通过
                     $info->status = $status;
-                    if ($info->module == CommonConstant::MODULE_3) {
-                        // 入库商品列表
-                        $goodsStockSave = [];
-                        $goodsStockLog = [];
-                        foreach ($goods as $key => $val) {
-                            foreach ($val['goods_stock_text'] as $k => $v) {
-                                $goodsStockSave[] = [
-                                    'id' => $v['id'],
-                                    'stock' => ['inc', $v['stock']],
-                                ];
-                                $goodsStockLog[] = [
-                                    'info_id' => $val['info_id'],
-                                    'goods_id' => $v['goods_id'],
-                                    'stock_id' => $v['id'],
-                                    'name' => $v['name'],
-                                    'price' => $v['price'],
-                                    'stock' => $v['stock'],
-                                ];
-                            }
-                        }
-                        if ($goodsStockSave) {
-                            // 批量更新商品库存
-                            (new GoodsStock)->saveAll($goodsStockSave);
-                        }
-                        if ($goodsStockLog) {
-                            // 批量添加商品入库记录
-                            GoodsStockLog::insertAll($goodsStockLog);
-                        }
-                    }
                 }
             }
             if ($status == CommonConstant::STATUS_4) {
@@ -564,9 +558,22 @@ class ApproveService
         } catch (Exception $e) {
             except('出现错误:' . $e->getMessage(), 2, $e);
         }
+
+        // 4=领用申请 扣除冻结库存
+        if ($module == CommonConstant::MODULE_4) {
+            if($redisStock){
+                // 库存缓存记录
+                $redis = new Redis(config('redis.'));
+                foreach ($redisStock as $key=>$val){
+                    $redis->dec($val['id'], $val['stock']);
+                }
+            }
+        }
         return true;
     }
 
+
+
     /**
      * 修改
      *