Browse Source

给销售单添加地址优化,订单取消优化

wufeng 2 years ago
parent
commit
52fbd57ff7
1 changed files with 144 additions and 132 deletions
  1. 144 132
      app/admin/controller/Sale.php

+ 144 - 132
app/admin/controller/Sale.php

@@ -628,10 +628,10 @@ class Sale extends Base
         if (empty($etid)) {
             return error_show(1002, "未找到数据");
         }
-        $retrun =Db::name("sale_return")->where([["orderCode","=",$orderCode],["is_del","=",0],["status","in",[1,2,3,
-        7,9,10]]])->count();
-        if($retrun>0){
-            return error_show(1005,"存在未完成退货订单数据");
+        $retrun = Db::name("sale_return")->where([["orderCode", "=", $orderCode], ["is_del", "=", 0], ["status", "in", [1, 2, 3,
+            7, 9, 10, 11, 12]]])->count('id');
+        if ($retrun > 0) {
+            return error_show(1005, "存在未完成退货订单数据");
         }
         $arrtime = isset($this->post['arrtime']) && $this->post['arrtime'] != "" ? $this->post['arrtime'] : "";
 
@@ -641,18 +641,21 @@ class Sale extends Base
         if ($addrlist == "" || empty($addrlist) || !is_array($addrlist)) {
             return error_show(1004, "参数addrlist不能为空");
         }
-        $userCommon= \app\admin\common\User::getIns();
+
+//        $userCommon = \app\admin\common\User::getIns();
         Db::startTrans();
         try {
-        	$GoodModel=Db::name("good");
-        	if(in_array($etid['order_type'],[3,4])){
-        		$GoodModel=Db::name("good_zixun");
-        	}
-			$goodStock=$GoodModel->where(["spuCode"=>$etid['good_code']])->lock(true)->findOrEmpty();
-            $i=1;
-        	$goodStock_num =$goodStock['usable_stock']??0;
-        	$total_num = array_sum(array_column($addrlist,"receipt_quantity"));
-        	if($goodStock_num<$total_num && $etid['is_stock'] == 1) throw new Exception("库存品库存数不足发货");
+//            $GoodModel = Db::name("good");
+//            if (in_array($etid['order_type'], [3, 4])) {
+//                $GoodModel = Db::name("good_zixun");
+//            }
+//			$goodStock=$GoodModel->where(["spuCode"=>$etid['good_code']])->lock(true)->findOrEmpty();
+            $i = 1;
+//        	$goodStock_num =$goodStock['usable_stock']??0;
+            $total_num = array_sum(array_column($addrlist, "receipt_quantity"));
+            $goodStock_num = Db::name('good_stock')->where(['is_del' => 0, 'spuCode' => $etid['good_code']])->sum('usable_stock');
+
+            if ($goodStock_num < $total_num && $etid['is_stock'] == 1) throw new Exception("库存品库存数不足发货");
             foreach ($addrlist as $value) {
                 $temp = [];
                 $addrs = [];
@@ -681,45 +684,45 @@ class Sale extends Base
                 $vmp = Db::name('order_addr')->insert($temp, true);
                 if ($vmp > 0) {
                     $num = $value['receipt_quantity'];
-                      $outCode = makeNo("DF");
-                      //改变编码规则,将原来的outCode后两位换成序列号
+                    $outCode = makeNo("DF");
+                    //改变编码规则,将原来的outCode后两位换成序列号
                     //str_pad字符串填充
                     $outCode = substr($outCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
                     $i++;
                     //维护商品所在仓库的库存 --- start
-                    if ($etid['is_stock'] != 1){
-                    	//查找关联采购单
-                    	$order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->findOrEmpty();
-	                    if (empty($order)) {
-	                         throw new Exception("未找到可以发货得采购单数据");
-	                    }
-	                    $tep = [
-	                        "cgdNo" => $order['cgdNo'],
-	                        "outCode" => $outCode,
-	                        "send_num" => $num,
-	                        "status" => 1,
-	                        "addtime" => date("Y-m-d H:i:s"),
-	                        "updatetime" => date("Y-m-d H:i:s")
-	                    ];
-	                    $sen = Db::name("order_send")->save($tep);
-	                    if ($sen == false) {
-	                        throw new Exception("发货地址添加创建失败");
-	                    }
-	                    $cgdinfo = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find();
-	                    if ($cgdinfo == false) {
-	                        throw new Exception("采购单数据不存在");
-	                    }
-                        if ($goodStock_num==0) {
-                                $status = 0;
+                    if ($etid['is_stock'] != 1) {
+                        //查找关联采购单
+                        $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->findOrEmpty();
+                        if (empty($order)) {
+                            throw new Exception("未找到可以发货得采购单数据");
+                        }
+                        $tep = [
+                            "cgdNo" => $order['cgdNo'],
+                            "outCode" => $outCode,
+                            "send_num" => $num,
+                            "status" => 1,
+                            "addtime" => date("Y-m-d H:i:s"),
+                            "updatetime" => date("Y-m-d H:i:s")
+                        ];
+                        $sen = Db::name("order_send")->save($tep);
+                        if ($sen == false) {
+                            throw new Exception("发货地址添加创建失败");
+                        }
+                        $cgdinfo = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find();
+                        if ($cgdinfo == false) {
+                            throw new Exception("采购单数据不存在");
+                        }
+                        if ($goodStock_num == 0) {
+                            $status = 0;
                         } else {
-                            if (($goodStock_num - $num) < 0)throw new Exception("非库存品可用库存不足");
-                            $goodStock_num =- $num;
+                            if (($goodStock_num - $num) < 0) throw new Exception("非库存品可用库存不足");
+                            $goodStock_num = -$num;
                             $status = 1;
                         }
-                    }else{
-                    if ($goodStock_num==0 ||($goodStock_num - $num) < 0)throw new Exception("库存品可用库存不足");
-                    $goodStock_num =- $num;
-					$status=1;
+                    } else {
+                        if ($goodStock_num == 0 || ($goodStock_num - $num) < 0) throw new Exception("库存品可用库存不足");
+                        $goodStock_num = -$num;
+                        $status = 1;
                     }
 
                     $out = [
@@ -735,81 +738,80 @@ class Sale extends Base
                         "send_num" => $num,
                         "check_num" => 0,
                         "error_num" => 0,
-                        "wsm_code" => $cgdinfo['wsm_code']??'',
+                        "wsm_code" => $cgdinfo['wsm_code'] ?? '',
                         "order_type" => $etid['order_type'],
+                        "send_status" => $status,
                         "status" => $status,
                         "addtime" => date("Y-m-d H:i:s"),
                         "updatetime" => date("Y-m-d H:i:s")
                     ];
                     $ou = Db::name("order_out")->insertGetId($out);
-                    if ($ou == false) {
-                        throw new Exception("发货单创建失败");
-//                        return error_show(1002, "发货地址添加创建失败");
-                    } else {
-                    	OrderOutChild::makeChild($outCode);
+                    if ($ou == false) throw new Exception("发货单创建失败");
+                    else {
+                        if ($out['send_status'] == 1) OrderOutChild::makeChild($outCode);
 
                         //修改状态,添加待办
-                        ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
-                            "order_code" => $outCode,//出库单号
-                            "status" => 0,//这里的status是之前的值
-                            "action_remark" => '',//备注
-                            "action_type" => "create"//新建create,编辑edit,更改状态status
-                        ], "CKD", 0, $out);
+//                        ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
+//                            "order_code" => $outCode,//出库单号
+//                            "status" => 0,//这里的status是之前的值
+//                            "action_remark" => '',//备注
+//                            "action_type" => "create"//新建create,编辑edit,更改状态status
+//                        ], "CKD", 0, $out);
 
                         //区分是否是库存品订单
-                        $process=[];
-                        if($status == 1){
-                            //库存品,推给库管和库管-张凯旋
-                            if($etid['is_stock']==1){
-                                $roleid = config('app.wsm_cgder_role');
-                                $uids = Db::name('user_role')
-                                    ->where('is_del', 0)
-                                    ->whereIn('roleid', $roleid)
-                                    ->column('uid');
-                                $process=[
-                                    "order_type" => 'CKD',
-                                    "order_code" => $outCode,//出库单号
-                                    "order_id" => $ou,
-                                    "order_status" => $status,
-                                    "before_status" => 0,
-//                                    'holder_id'=>$out['apply_id'],
-                                    'handle_user_list' => implode(',', $uids)
-                                ];
-                            }else{
-                                //推给供应商负责人
-//                                $supplier = Db::name('supplier')
-//                                    ->field('person,personid')
-//                                    ->where('code',$cgdinfo['supplierNo'])
-//                                    ->findOrEmpty();
-
-								$supplier_temp =$userCommon->handle("sInfo",["code"=>$cgdinfo['supplierNo']]);
-								if($supplier_temp['code']!=0 || empty($supplier_temp['data']) ) return error_show(1002,"采购单供应商不存在");
-								$holder_id=$supplier_temp['data']['personid']??0;
-								$holder_name=$supplier_temp['data']['person']??'';
-								$supplier_name=$supplier_temp['data']['name']??'';
-                                $process = [
-                                    "order_type" => 'CKD',
-                                    "order_code" => $outCode,//出库单号
-                                    "order_id" => $ou,
-                                    "order_status" => $status,
-                                    "before_status" => 0,
-                                    'wait_id'=>$holder_id,
-                                    'wait_name'=>$holder_name,
-                                    'holder_id'=>$holder_id,
-                                ];
-                            }
-                        }
-
-                        if(empty($process)) $process=[
-                            "order_type" => 'CKD',
-                            "order_code" => $outCode,//出库单号
-                            "order_id" => $ou,
-                            "order_status" => $status,
-                            "before_status" => 0,
-                            'holder_id'=>$out['apply_id'],
-                        ];
+                        $process = [];
+//                        if($status == 1){
+//                            //库存品,推给库管和库管-张凯旋
+//                            if($etid['is_stock']==1){
+//                                $roleid = config('app.wsm_cgder_role');
+//                                $uids = Db::name('user_role')
+//                                    ->where('is_del', 0)
+//                                    ->whereIn('roleid', $roleid)
+//                                    ->column('uid');
+//                                $process=[
+//                                    "order_type" => 'CKD',
+//                                    "order_code" => $outCode,//出库单号
+//                                    "order_id" => $ou,
+//                                    "order_status" => $status,
+//                                    "before_status" => 0,
+////                                    'holder_id'=>$out['apply_id'],
+//                                    'handle_user_list' => implode(',', $uids)
+//                                ];
+//                            }else{
+//                                //推给供应商负责人
+////                                $supplier = Db::name('supplier')
+////                                    ->field('person,personid')
+////                                    ->where('code',$cgdinfo['supplierNo'])
+////                                    ->findOrEmpty();
+//
+//								$supplier_temp =$userCommon->handle("sInfo",["code"=>$cgdinfo['supplierNo']]);
+//								if($supplier_temp['code']!=0 || empty($supplier_temp['data']) ) return error_show(1002,"采购单供应商不存在");
+//								$holder_id=$supplier_temp['data']['personid']??0;
+//								$holder_name=$supplier_temp['data']['person']??'';
+//								$supplier_name=$supplier_temp['data']['name']??'';
+//                                $process = [
+//                                    "order_type" => 'CKD',
+//                                    "order_code" => $outCode,//出库单号
+//                                    "order_id" => $ou,
+//                                    "order_status" => $status,
+//                                    "before_status" => 0,
+//                                    'wait_id'=>$holder_id,
+//                                    'wait_name'=>$holder_name,
+//                                    'holder_id'=>$holder_id,
+//                                ];
+//                            }
+//                        }
 
-                        ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri],$process);
+//                        if(empty($process)) $process=[
+//                            "order_type" => 'CKD',
+//                            "order_code" => $outCode,//出库单号
+//                            "order_id" => $ou,
+//                            "order_status" => $status,
+//                            "before_status" => 0,
+//                            'holder_id'=>$out['apply_id'],
+//                        ];
+//
+//                        ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri],$process);
 
                         //处理台账-既然是根据orderCode更新的数据,那么肯定是有数据的,只需要更新就行了
                         Db::execute("UPDATE `wsm_standing_book` SET `outCode`=CONCAT(IFNULL(`outCode`,''),',{$outCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$orderCode}'");
@@ -822,8 +824,8 @@ class Sale extends Base
                     return error_show(1002, "发货地址添加创建失败");
                 }
             }
-            $usable = $GoodModel->where($goodStock)->update(["usable_stock"=>$goodStock_num,"updatetime"=>date("Y-m-d H:i:s")]);
-            if($usable==false) throw new Exception("商品可用库存数更新失败");
+//            $usable = $GoodModel->where($goodStock)->update(["usable_stock"=>$goodStock_num,"updatetime"=>date("Y-m-d H:i:s")]);
+//            if($usable==false) throw new Exception("商品可用库存数更新失败");
             Db::commit();
             return app_show(0, "发货地址添加完成");
         } catch (\Exception $e) {
@@ -5111,28 +5113,30 @@ class Sale extends Base
                 if ($sale['sale_status'] == 3) throw new \think\Exception($sale['orderCode'] . '已取消,无法重复操作');
                 if (in_array($sale['cat_id'], $cat_id)) throw new \think\Exception($sale['orderCode'] . '为贵金属,无法取消');
 
-                if ($sale['order_type'] == 1) {
-                    if (in_array($sale['sale_status'], [0, 1]) == false) throw new \think\Exception($sale['orderCode'] . '未处于待发货状态,无法取消');
-                } else {
-                    if (in_array($sale['cgd_status'], [0, 1]) == false) throw new \think\Exception($sale['orderCode'] . '采购单已入库,无法取消');
-                }
+                if (in_array($sale['sale_status'], [0, 1]) == false) throw new \think\Exception($sale['orderCode'] . '已发货,无法取消');
 
-                //修改状态,添加待办
-                ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
-                    "order_code" => $sale['orderCode'],//销售单号
-                    "status" => $sale['sale_status'],//这里的status是之前的值
-                    "action_remark" => '',//备注
-                    "action_type" => "edit"//新建create,编辑edit,更改状态status
-                ], "XSQRD", 3, ['orderCode' => $orderCode]);
+//                if ($sale['order_type'] == 1) {
+//                    if (in_array($sale['sale_status'], [0, 1]) == false) throw new \think\Exception($sale['orderCode'] . '未处于待发货状态,无法取消');
+//                } else {
+//                    if (in_array($sale['cgd_status'], [0, 1]) == false) throw new \think\Exception($sale['orderCode'] . '采购单已入库,无法取消');
+//                }
 
-                ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
-                    "order_type" => 'XSQRD',
-                    "order_code" => $sale['orderCode'],
-                    "order_id" => $sale['sale_id'],
-                    "order_status" => 3,
-                    "before_status" => $sale['sale_status'],
-                    'holder_id' => $sale['good_createrid'],
-                ]);
+                //修改状态,添加待办
+//                ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
+//                    "order_code" => $sale['orderCode'],//销售单号
+//                    "status" => $sale['sale_status'],//这里的status是之前的值
+//                    "action_remark" => '',//备注
+//                    "action_type" => "edit"//新建create,编辑edit,更改状态status
+//                ], "XSQRD", 3, ['orderCode' => $orderCode]);
+//
+//                ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
+//                    "order_type" => 'XSQRD',
+//                    "order_code" => $sale['orderCode'],
+//                    "order_id" => $sale['sale_id'],
+//                    "order_status" => 3,
+//                    "before_status" => $sale['sale_status'],
+//                    'holder_id' => $sale['good_createrid'],
+//                ]);
             }
             Db::name('sale')
                 ->where(['is_del' => 0, 'id' => array_column($list, 'sale_id')])
@@ -5142,6 +5146,14 @@ class Sale extends Base
                 ->where(['is_del' => 0, 'id' => array_column($list, 'cgd_id')])
                 ->update(['status' => 4, 'updatetime' => $date]);
 
+            Db::name('order_out')
+                ->where(['is_del' => 0, 'orderCode' => $orderCode])
+                ->update(['status' => 5, 'updatetime' => $date]);
+
+            Db::name('order_out_child')
+                ->where(['is_del' => 0, 'orderCode' => $orderCode])
+                ->update(['status' => 5, 'updatetime' => $date]);
+
             Db::commit();
             return json_show(0, '操作成功');
         } catch (\think\Exception $exception) {