wugg 1 year ago
parent
commit
b2a0d2922e
1 changed files with 146 additions and 15 deletions
  1. 146 15
      app/admin/controller/AfterChild.php

+ 146 - 15
app/admin/controller/AfterChild.php

@@ -182,7 +182,7 @@ class AfterChild extends Base
 
         $order_return = Db::name('order_return')
             ->alias('a')
-            ->field('a.id,a.good_code,a.good_name,c.sale_price,c.cat_id,a.error_num,a.apply_id,a.apply_name,a.outCode,b.orderCode,c.is_stock,c.order_type,a.status')
+            ->field('a.id,a.good_code,a.good_name,a.companyNo,c.sale_price,c.cat_id,a.error_num,a.apply_id,a.apply_name,a.outCode,b.orderCode,c.is_stock,c.order_type,a.status')
             ->leftJoin('order_out b', 'b.outCode=a.outCode AND b.is_del=0')
             ->leftJoin('sale c', 'c.orderCode=b.orderCode AND c.is_del=0')
             ->where(['a.is_del' => 0, 'a.returnCode' => $param['returnCode']])
@@ -214,6 +214,12 @@ class AfterChild extends Base
             'loss_num|丢失数量' => 'require|number|egt:0|max:999999999999',
             'remark|备注' => 'max:255'
         ]);
+        $defective_sum =array_sum(array_column($param['list'],"defective_num"));
+        if($defective_sum>0){
+        	$defective_wsm = Db::name("warehouse_info")->where(["companyNo"=>$order_return['companyNo'],
+        	"wsm_type"=>4,"is_del"=>0,"status"=>1])->findOrEmpty();
+        	if(empty($defective_wsm)) return  json_show(1004, '退货工单次品仓库不存在');
+        }
 
         Db::startTrans();
         try {
@@ -255,6 +261,7 @@ class AfterChild extends Base
                 }
                 //如果次品仓入库数量大于0,另生成退货工单,以类型区分
                 if ($value['defective_num'] > 0) {
+                	
                     $insert[] = [
                         'returnCode' => $param['returnCode'],
                         'orderCode' => $info[$value['id']]['orderCode'],
@@ -264,7 +271,7 @@ class AfterChild extends Base
                         'send_wsm_code' => $info[$value['id']]['send_wsm_code'],
                         'send_num' => $info[$value['id']]['send_num'],
                         'return_num' => $info[$value['id']]['return_num'],
-                        'return_wsm_code' => $info[$value['id']]['return_wsm_code'],
+                        'return_wsm_code' => $defective_wsm['wsm_code'],
                         'can_sell_num' => $value['can_sell_num'],
                         'defective_num' => $value['defective_num'],
                         'loss_num' => $value['loss_num'],
@@ -317,10 +324,11 @@ class AfterChild extends Base
                         $sup = Db::name('sale')->save($sale);
                         if ($sup == false) throw new \Exception('销售单更新失败');
             }
+            
 
             $total = array_sum(array_column($param['list'], 'can_sell_num'));
 
-            if ($total > 0) {
+//            if ($total > 0 || ) {
                 if ($order_return['is_stock'] != 1) {
                     //根据转化表判断该非库存品是否转化为库存品
                     $spuCode = Db::name('good_change_stock')
@@ -344,7 +352,7 @@ class AfterChild extends Base
                                     'createrid'=>0,
                                     'creater'=>'system'
                                 ]));
-                                 if($up2==false) throw new \Exception("{$order_return['good_code']} 商品库新增失败");
+                                 if($up2==false) throw new Exception("{$order_return['good_code']} 商品库新增失败");
                             }
                         } else {
                             $good = Db::name('good_zixun')
@@ -422,7 +430,7 @@ class AfterChild extends Base
                                     'is_support_stock' => 1
                                 ]);
                                
-                               if($up==false) throw new \Exception("咨询商品录入商品库失败");
+                               if($up==false) throw new Exception("咨询商品录入商品库失败");
                             }
                         }
 
@@ -436,12 +444,45 @@ class AfterChild extends Base
                                     'addtime' => $date,
                                     'updatetime' => $date,
                                 ]);
-                           if($dd==false) throw new \Exception('关联表新增失败');
+                           if($dd==false) throw new Exception('关联表新增失败');
 
                         } else throw new Exception('未找到对应的商品数据');
 
                     }
-
+					if($defective_sum>0){
+	                    $defective_stock= Db::name('good_stock')
+	                    ->where(['is_del' => 0, 'spuCode' => $spuCode, 'wsm_code' => $defective_wsm['wsm_code']])
+	                    ->findOrEmpty();
+	                    if(empty($defective_stock)){
+	                         $defective_stockid = Db::name('good_stock')
+	                                ->insertGetId([
+	                                    'project_code' => '',
+	                                    'spuCode' => $spuCode,
+	                                    'wsm_code' => $defective_wsm['wsm_code'],
+	                                    'wait_in_stock' => 0,
+	                                    'wait_out_stock' => 0,
+	                                    'usable_stock' => $defective_sum,
+	                                    'intra_stock' => 0,
+	                                    'total_stock' => $defective_sum,
+	                                    'status' => 1,
+	                                    'is_del' => 0,
+	                                    'warn_stock' => 0,
+	                                    'addtime' => $date,
+	                                    'updatetime' => $date,
+	                                ]);
+	                         if($defective_stockid==0)  throw new Exception('次品仓库库存更新失败');
+	                    }else{
+	                        $defective_stockid = $defective_stock['id'];
+	                          $defective_up=  Db::name('good_stock')
+	                                ->data(['updatetime' => $date])
+	                                ->where(['is_del' => 0, 'id' => $defective_stockid])
+	                                ->inc('usable_stock', $defective_sum)
+	                                ->inc('total_stock', $defective_sum)
+	                                ->update();
+	                          if($defective_up==false)  throw new Exception('次品仓库库存更新失败');
+	                    }
+	                    $total+=$defective_sum;
+                }
                     //新增bn记录,维护旧有bn记录
                     $child_bns = Db::name('child_bn')
                         ->field('id,num,origin_price')
@@ -450,6 +491,7 @@ class AfterChild extends Base
                         ->cursor();
 
                     $origin_price = $j = 0;
+                    
                     foreach ($child_bns as $child_bn) {
                         if ($origin_price === 0) $origin_price = $child_bn['origin_price'];
                        $bn= Db::name('child_bn')
@@ -494,18 +536,69 @@ class AfterChild extends Base
                                 ]);
                          if($stockinfo==false) throw new \Exception('商品库存BN新增失败');
                         }
+                        if($value['defective_num']>0){
+                        	 $defective_info=   Db::name('good_stock_info')
+                                ->insert([
+                                    'stockid' => $defective_stockid,
+                                    'bnCode' => substr(makeNo('BN'), 0, -2) . str_pad($j++, 2, '0', STR_PAD_LEFT),
+                                    'total_num' => $value['defective_num'],
+                                    'used_num' => 0,
+                                    'balance_num' => $value['defective_num'],
+                                    'origin_price' => $origin_price,
+                                    'addtime' => $date,
+                                    'updatetime' => $date,
+                                ]);
+                         if($defective_info==false) throw new \Exception('次品仓商品库存BN新增失败');
+                        }
+                        
                     }
                 }
                 else {
+                	$spuCode = $order_return['good_code'];
                     //库存品,直接维护stock和info
                     $stockids = Db::name('good_stock')
                         ->where(['is_del' => 0, 'spuCode' => $order_return['good_code'], 'wsm_code' => array_column($info, 'return_wsm_code')])
                         ->column('id', 'wsm_code');
-
+					if($defective_sum>0){
+	                    $defective_stock= Db::name('good_stock')
+	                    ->where(['is_del' => 0, 'spuCode' => $spuCode, 'wsm_code' => $defective_wsm['wsm_code']])
+	                    ->findOrEmpty();
+	                    if(empty($defective_stock)){
+	                         $defective_stockid = Db::name('good_stock')
+	                                ->insertGetId([
+	                                    'project_code' => '',
+	                                    'spuCode' => $spuCode,
+	                                    'wsm_code' => $defective_wsm['wsm_code'],
+	                                    'wait_in_stock' => 0,
+	                                    'wait_out_stock' => 0,
+	                                    'usable_stock' => $defective_sum,
+	                                    'intra_stock' => 0,
+	                                    'total_stock' => $defective_sum,
+	                                    'status' => 1,
+	                                    'is_del' => 0,
+	                                    'warn_stock' => 0,
+	                                    'addtime' => $date,
+	                                    'updatetime' => $date,
+	                                ]);
+	                         if($defective_stockid==0)  throw new Exception('次品仓库库存更新失败');
+	                    }else{
+	                        $defective_stockid = $defective_stock['id'];
+	                          $defective_up=  Db::name('good_stock')
+	                                ->data(['updatetime' => $date])
+	                                ->where(['is_del' => 0, 'id' => $defective_stockid])
+	                                ->inc('usable_stock', $defective_sum)
+	                                ->inc('total_stock', $defective_sum)
+	                                ->update();
+	                          if($defective_up==false)  throw new Exception('次品仓库库存更新失败');
+	                    }
+//	                    $total+=$defective_sum;
+                }
                     foreach ($param['list'] as $value) {
 
                         //定义了stockid,维护bn
-                        if (isset($stockids[$info[$value['id']]['return_wsm_code']])) {
+                        if($value['can_sell_num']>0){
+                        	
+                        	if (isset($stockids[$info[$value['id']]['return_wsm_code']])) {
                             $good_stock_info = Db::name('good_stock_info')
                                 ->where(['stockid' => $stockids[$info[$value['id']]['return_wsm_code']]])
                                 ->findOrEmpty();
@@ -559,8 +652,9 @@ class AfterChild extends Base
                                 ->inc('usable_stock', $value['can_sell_num'])
                                 ->inc('total_stock', $value['can_sell_num'])
                                 ->update();
-                    if($stockinfoe==false) throw new \Exception('商品库存更新失败');
-                        } else {
+                            if($stockinfoe==false) throw new \Exception('商品库存更新失败');
+                        }
+                            else {
                             //新建good_stock和good_stock_info
                             $stockid = Db::name('good_stock')
                                 ->insertGetId([
@@ -611,6 +705,43 @@ class AfterChild extends Base
                             }
 
                         }
+                        }
+                        
+                        if($value['defective_num']>0){
+                        	 
+                          //从child_bn查询bn号并维护,新建到good_stock_info中
+                            $child_bns = Db::name('child_bn')
+                                ->field('id,num,bnCode,origin_price')
+                                ->where(['orderCode' => $order_return['orderCode'], 'outCode' => $order_return['outCode'], 'childCode' => $info[$value['id']]['outChildCode']])
+                                ->order(['num' => 'desc', 'id' => 'asc'])
+                                ->cursor();
+
+                            $total = $value['defective_num'];
+                            foreach ($child_bns as $child_bn) {
+                                $bn=Db::name('child_bn')
+                                    ->data(['updatetime' => $date])
+                                    ->where(['id' => $child_bn['id']])
+                                    ->dec('num', ($child_bn['num'] >= $total) ? $total : $child_bn['num'])
+                                    ->update();
+								if($bn==false) throw new \Exception('商品库存BN更新失败');
+                                $bninfo=Db::name('good_stock_info')
+                                    ->insert([
+                                        'stockid' => $defective_stockid,
+                                        'bnCode' => $child_bn['bnCode'],
+                                        'total_num' => ($child_bn['num'] >= $total) ? $total : $child_bn['num'],
+                                        'used_num' => 0,
+                                        'balance_num' => ($child_bn['num'] >= $total) ? $total : $child_bn['num'],
+                                        'origin_price' => $child_bn['origin_price'],
+                                        'addtime' => $date,
+                                        'updatetime' => $date,
+                                    ]);
+								if($bninfo==false) throw new \Exception('次品商品库存BN新增失败');
+                                $total = ($child_bn['num'] >= $total) ? 0 : ($total - $child_bn['num']);
+                                if ($total == 0) break;
+                            }
+
+                        }
+                        
                     }
 
                     //维护商品层面的库存数
@@ -622,18 +753,18 @@ class AfterChild extends Base
                     if($goodinfo==false) throw new \Exception('商品可售库存数更新失败');
 
                 }
-            }
-
+//            }
+          
             //售后申请单状态维护
             $Reup=Db::name('order_return')
                 ->where(['is_del' => 0, 'returnCode' => $param['returnCode'], 'status' => 12])
                 ->update(['status' => 5, 'updatetime' => $date]);
-			 if($Reup==false) throw new \Exception('售后申请退货状态更新失败');
+			 if($Reup==false) throw new Exception('售后申请退货状态更新失败');
             //售后退货工单设置信息维护
             $Reupchild=Db::name('order_return_prepare')
                 ->where(['is_del' => 0, 'status' => 1, 'id' => array_column($info, 'id')])
                 ->update(['updatetime' => $date, 'status' => 3]);
-             if($Reupchild==false) throw new \Exception('售后申请退货工单状态更新失败');
+             if($Reupchild==false) throw new Exception('售后申请退货工单状态更新失败');
             $data = [
                             "orderCode" => $order_return['orderCode'],
                             "th_type" => 2,