Browse Source

细节优化,售后退货部分接口提交

wufeng 2 years ago
parent
commit
8c082d0e8b

+ 312 - 309
app/admin/controller/After.php

@@ -90,7 +90,7 @@ class After extends Base
         } else {
             $goon = Db::name('good_platform')
                 ->alias('a')
-                ->field("b.creater,b.createrid,b.supplierNo")
+                ->field("b.creater,b.createrid,b.supplierNo,b.supplierName")
                 ->join('good b', 'b.spuCode=a.spuCode', 'left')
                 ->where(['a.skuCode' => $oder['skuCode']])
                 ->findOrEmpty();
@@ -109,7 +109,7 @@ class After extends Base
 //        if (!isset($supplier['code']) || $supplier['code'] != 0) return json_show($supplier['code'], $supplier['message'], $supplier['data']);
         $supplier = $supplier['data'];
 
-        $names = $userCommon->handle('getCodeAndName', ['code' => [$oder['customer_code'], $oder['supplierNo']]]);
+//        $names = $userCommon->handle('getCodeAndName', ['code' => [$oder['customer_code'], $oder['supplierNo']]]);
 
 //         $supplier =Db::name("supplier")->where(["code"=>$goon['supplierNo']])->find();
 //        if($supplier==false){
@@ -157,11 +157,11 @@ class After extends Base
                 "good_code" => $oder['good_code'],
                 "good_name" => $oder['good_name'],
                 "customer_code" => $oder['customer_code'],
-                "customer_name" => $names['data'][$oder['customer_code']] ?? '',
+                "customer_name" =>$oder['customerName'],
                 "apply_id" => $rm,
                 "apply_name" => $ri,
                 'supplierNo' => $goon['supplierNo'],
-                'supplierName' => $supplier['name'],
+                'supplierName' => $goon['supplierName'],
                 "cgder" => $wsm['contactor_name'] ?? '',
                 "cgderid" => $wsm['contactor'] ?? 0,
                 "person" => $supplier['person'] ?? '',
@@ -177,7 +177,7 @@ class After extends Base
                 "except_code" => $param['except_code'],
                 "order_type" => $orderout['order_type'],
                 'companyNo' => $oder['supplierNo'],
-                'companyName' => $names['data'][$oder['supplierNo']] ?? '',
+                'companyName' => $oder['supplierName'],
                 "status" => 1,
                 "is_del" => 0,
                 "addtime" => date("Y-m-d H:i:s"),
@@ -213,13 +213,13 @@ class After extends Base
                 $process = ["order_code" => $returnCode, "order_id" => $create, "order_status" => $in['status'], "order_type" => 'SHD', "before_status" => 0, 'holder_id' => $in['apply_id']];
                 ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $process);
                 Db::commit();
-                return app_show(0, "售后申请单新建成功", ["returnCode" => $returnCode]);
+                return json_show(0, "售后申请单新建成功", ["returnCode" => $returnCode]);
             }
             Db::rollback();
             return error_show(1005, "售后申请单新建失败");
-        } catch (Exception $e) {
+        } catch (Exception $exception) {
             Db::rollback();
-            return error_show(1005, $e->getMessage());
+            return json_show(1005, $exception->getMessage());
         }
     }
 
@@ -384,6 +384,8 @@ class After extends Base
 //        $info['supplierNo'] = $goon['supplierNo'];
 
         $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
+        $info['can'] = $int;
+
         $addcode = '';
         if ($info['addr_code'] != "") {
             list($a, $b, $c) = explode(",", $info['addr_code']);
@@ -401,27 +403,27 @@ class After extends Base
                 ->alias("a")
 //                ->leftJoin("supplier b", "a.supplierNo=b.code")
                 ->where(["a.wsm_code" => $info['return_wsm']])
-                ->field("a.name as wsm_name,a.wsm_code,a.contactor_name,a.position,a.mobile,a.addrs_code,a.addr,a.companyNo,a.supplierNo")
+//                ->field("a.name as wsm_name,a.wsm_code,a.contactor_name,a.position,a.mobile,a.addrs_code,a.addr,a.companyNo,a.supplierNo")
                 ->find();
 
         }
 
-        $userCommon = \app\admin\common\User::getIns();
-        $names = $userCommon->handle('getCodeAndName', ['code' => [
-            $wsmcode['supplierNo'] ?? '',
-            $wsmcode['companyNo'] ?? '',
-//            $info['customer_code'],
-//            $info['companyNo'],
-//            $info['supplierNo'],
-        ]]);
+//        $userCommon = \app\admin\common\User::getIns();
+//        $names = $userCommon->handle('getCodeAndName', ['code' => [
+//            $wsmcode['supplierNo'] ?? '',
+//            $wsmcode['companyNo'] ?? '',
+////            $info['customer_code'],
+////            $info['companyNo'],
+////            $info['supplierNo'],
+//        ]]);
 
         if ($info['return_wsm'] != '') {
             $info['wsm_supplierNo'] = $wsmcode['supplierNo'] ?? "";
-            $info['wsm_supplier'] = $names['data'][$wsmcode['supplierNo']] ?? '';//isset($wsmcode['name']) ? $wsmcode['name'] : "";
-            $info['wsm_customer_name'] = $names['data'][$wsmcode['companyNo']] ?? '';//isset($customer['company']) ? trim($customer['company']) : '';
+            $info['wsm_supplier'] = $wsmcode['supplierName'];//isset($wsmcode['name']) ? $wsmcode['name'] : "";
+            $info['wsm_customer_name'] = $wsmcode['companyName'];//isset($customer['company']) ? trim($customer['company']) : '';
             $info['wsm_customer_code'] = $wsmcode['companyNo'] ?? "";//isset($customer['companyNo']) ? trim($customer['companyNo']) : '';
             $info['wsm_code'] = isset($wsmcode['wsm_code']) ? $wsmcode['wsm_code'] : "";
-            $info['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
+            $info['wsm_name'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
         }
 
 //        $customer = [];
@@ -433,8 +435,6 @@ class After extends Base
 //        $info['companyName'] = $names['data'][$info['companyNo']] ?? '';
 //        $info['supplierName'] = $names['data'][$info['supplierNo']] ?? '';
 
-        $info['can'] = $int;
-
         //仓库的联系人、职位、电话、省市区和地址
         isset($wsmcode['contactor_name']) ? $info['contactor'] = $wsmcode['contactor_name'] : '';//联系人
         isset($wsmcode['mobile']) ? $info['mobile'] = $wsmcode['mobile'] : '';//电话
@@ -446,14 +446,15 @@ class After extends Base
 //        $info['addr'] = $wsmcode['addr'];//地址
         $info['spuCode'] = $orderinfo['good_code'];
         $info['skuCode'] = $orderinfo['skuCode'];
-        $info['return_tag'] = $info['return_tag'] == 0 ? "" : $info['return_tag'];//地址
-        $info['is_th'] = $info['is_th'] == 0 ? "" : $info['is_th'];//地址
+        $info['return_tag'] = $info['return_tag'] == 0 ? "" : $info['return_tag'];
+        $info['is_th'] = $info['is_th'] == 0 ? "" : $info['is_th'];
 
         return app_show(0, "获取成功", $info);
     }
 
     //售后申请单 审核
-    //申请单状态:1待业务审核,2待采购审核,3待设置仓库(该节点废除),4待客户退货,5售后已完成,6业务已驳回,7采购已驳回(该节点废除),8申请已取消,9供应商审核,10业务公司修改待供应商确认
+    //申请单状态:1待业务审核,2待采购审核,3待设置仓库(该节点废除),4待客户退货(已收到货才有),5售后已完成,6业务已驳回,7采购已驳回(该节点废除),8申请已取消,9供应商审核,10业务公司修改待供应商确认
+    //11待设置退货工单(已收到货才有),12待库管收货(已收到货才有)
     public function status()
     {
         try {
@@ -500,7 +501,9 @@ class After extends Base
                     if (in_array($param['status'], [6, 9]) == false) throw new Exception('选项错误');
                     break;
                 case 9:
-                    if (in_array($param['status'], [2, 4, 5]) == false) throw new Exception('选项错误');
+                    if ((($info['is_receive'] == 1) && (in_array($param['status'], [2, 4]) == false) || (($info['is_receive'] == 0) && (in_array($param['status'], [2, 5]) == false)))) throw new Exception('选项错误');
+
+//                    if (in_array($param['status'], [2, 4, 5]) == false) throw new Exception('选项错误');
 //                    if ($this->level != 3) throw new Exception('当前状态下只能由供应商账号操作');
                     if ($param['status'] == 4) {
                         if ($param['contactor'] == '') throw new Exception('收货人不能为空');
@@ -510,22 +513,13 @@ class After extends Base
                     }
                     break;
                 case 2:
-                    if (in_array($param['status'], [10, 4, 5]) == false) throw new Exception('选项错误');
-                    if ($param['status'] == 4) {
-                        if ($param['wsm_code'] == '') throw new Exception('请填写退回仓库');
-                        else {
-                            $ware = Db::name('warehouse_info')
-                                ->field('contactor_name contactor,mobile,addr,addrs_code addr_code')
-                                ->where(['wsm_code' => $param['wsm_code'], 'is_del' => 0])
-                                ->findOrEmpty();
-                            if (empty($ware)) throw new Exception('该仓库不存在');
-                            else $param = array_merge($param, $ware);
-                        }
-                    }
+
+                    if ((($info['is_receive'] == 1) && (in_array($param['status'], [10, 11]) == false) || (($info['is_receive'] == 0) && (in_array($param['status'], [10, 5]) == false)))) throw new Exception('选项错误');
 
                     break;
                 case 10:
-                    if (in_array($param['status'], [2, 4, 5]) == false) throw new Exception('选项错误');
+                    if ((($info['is_receive'] == 1) && (in_array($param['status'], [2, 4]) == false) || (($info['is_receive'] == 0) && (in_array($param['status'], [2, 5]) == false)))) throw new Exception('选项错误');
+
                     if ($param['status'] == 4) {
                         if ($param['contactor'] == '') throw new Exception('收货人不能为空');
                         if ($param['mobile'] == '') throw new Exception('联系电话不能为空');
@@ -573,7 +567,7 @@ class After extends Base
 
                 //判断 退回供应商 or 退回业务公司
                 if ($info['status'] == 9) {
-                    if ($param['status'] == 4) $info['return_tag'] = 1;
+                    if (in_array($param['status'],[4,5])) $info['return_tag'] = 1;
                     else $info['return_tag'] = 2;
                 }
 
@@ -1317,8 +1311,17 @@ class After extends Base
                 ->update($returninfo);
 
             if ($up) {
+
+                //查看是否有退货工单
+                $temp = Db::name('order_return_child')
+                    ->field('id')
+                    ->where(['is_del' => 0, 'returnCode' => $bkcode])
+                    ->findOrEmpty();
+                if (empty($temp)) $info['status'] = 5;//没有退货工单,更新到status5,售后申请已结束
+                else $info['status'] = 12;//存在退货工单,要更新到status12(待库管收货)
+
                 $old_info_status = $info['status'];
-                $info['status'] = 5;
+//                $info['status'] = 5;
                 $info['updatetime'] = date("y-m-d H:i:s");
                 $ro = Db::name("order_return")->save($info);
                 if ($ro) {
@@ -1341,281 +1344,281 @@ class After extends Base
                     ]);
 
                     //售后退货单数据
-                    $thNo = makeNo("ST");
-                    $thdata = [
-                        "thNo" => $thNo,
-                        "orderCode" => $out["orderCode"],
-                        "outCode" => $out["outCode"],
-                        "order_type" => $info["order_type"],
-                        "returnCode" => $info["returnCode"],
-                        "good_code" => $info["good_code"],
-                        "good_name" => $info["good_name"],
-                        "return_wsm" => $returninfo["return_wsm"],
-                        "total_fee" => round($sale['sale_price'] * $info["error_num"], 2),
-                        "good_price" => $sale['sale_price'],
-                        "platform_id" => $sale['platform_id'],
-                        "return_msg" => $info["error_msg"],
-                        "return_num" => $info["error_num"],
-                        "normal_num" => 0,
-                        "received_num" => 0,
-                        "apply_id" => $info['apply_id'],
-                        "apply_name" => $info['apply_name'],
-                        "cgder" => $info['cgder'],
-                        "cgderid" => $info['cgderid'],
-                        "person" => $info['person'],
-                        "person_id" => $info['person_id'],
-                        "contactor" => $returninfo["contactor"],
-                        "mobile" => $returninfo["mobile"],
-                        "addr" => $returninfo["addr"],
-                        "addr_code" => $returninfo["addr_code"],
-                        "return_code" => $info["error_code"],
-                        "post_fee" => $returninfo["post_fee"],
-                        "post_code" => $returninfo["post_code"],
-                        "post_company" => $returninfo["post_company"],
-                        "customer_code" => $info["customer_code"],
-                        "customer_name" => $info["customer_name"],
-                        "status" => $info['return_tag'] == 1 ? 4 : 1, //return_tag==1退回供应商,status:4完成退货,1待验收
-                        "addtime" => date("Y-m-d H:i:s"),
-                        "updatetime" => date("Y-m-d H:i:s"),
-                        'supplierNo' => $info['supplierNo'],
-                        'supplierName' => $info['supplierName'],
-                        'companyNo' => $info['companyNo'],
-                        'companyName' => $info['companyName'],
-                    ];
-                    $sav = Db::name("order_back")->insert($thdata, true);
-                    if ($sav) {
-
-                        //修改状态,添加待办
-                        ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
-                            "order_code" => $thdata['thNo'],//销售单code
-                            "status" => $thdata['status'],//这里的status是之前的值
-                            "action_remark" => '',//备注
-                            "action_type" => "create"//新建create,编辑edit,更改状态status
-                        ], "CKTHD", $thdata['status'], $thdata);
-
-
-                        //售后退货单,供应商不同意退货,退回到业务公司仓的时候,要把待办数据推给供应商负责人和库管人员
-                        if ($thdata['status'] == 1) {
-
-                            //31库管人员,41库管-张凯旋
-                            $uids = Db::name('user_role')
-                                ->where(['is_del' => 0, 'roleid' => [31, 41], 'status' => 1])
-                                ->column('uid');
-
-                            ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
-                                "order_type" => 'CKTHD',
-                                "order_code" => $thdata['thNo'],//销售单code
-                                "order_id" => $sav,
-                                "order_status" => $thdata['status'],
-                                "before_status" => 0,
-                                'handle_user_list' => implode(',', array_merge($uids, [$sale['cgderid']])),
-                            ]);
-                        } else {
-                            ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
-                                "order_type" => 'CKTHD',
-                                "order_code" => $thdata['thNo'],//销售单code
-                                "order_id" => $sav,
-                                "order_status" => $thdata['status'],
-                                "before_status" => 0
-                            ]);
-                        }
-
-
-                        //维护台账记录
-//                        Db::name('standing_book')
-//                            ->where(['returnCode' => $bkcode])
-//                            ->update([
-//                                'thNo' => $thNo,
-//                                'updatetime' => date("Y-m-d H:i:s")
-//                            ]);
-                        Db::execute("UPDATE `wsm_standing_book` SET `thNo`=CONCAT(IFNULL(`thNo`,''),',{$thNo}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE FIND_IN_SET('{$bkcode}',`returnCode`)");//这里的returnCode可能有多个
-                        // 库存品退货 不退商品 需要计算成本
-//						if($info['except_code']==1){
-//							$sabebn =Db::name("sale_info")->where(["orderCode"=>$info['orderCode']])->select()->toArray();
-//                            	 if(!empty($sabebn)){
-//                        	$total_num =$info["error_num"];
-//                        	foreach ($sabebn as $ve){
+//                    $thNo = makeNo("ST");
+//                    $thdata = [
+//                        "thNo" => $thNo,
+//                        "orderCode" => $out["orderCode"],
+//                        "outCode" => $out["outCode"],
+//                        "order_type" => $info["order_type"],
+//                        "returnCode" => $info["returnCode"],
+//                        "good_code" => $info["good_code"],
+//                        "good_name" => $info["good_name"],
+//                        "return_wsm" => $returninfo["return_wsm"],
+//                        "total_fee" => round($sale['sale_price'] * $info["error_num"], 2),
+//                        "good_price" => $sale['sale_price'],
+//                        "platform_id" => $sale['platform_id'],
+//                        "return_msg" => $info["error_msg"],
+//                        "return_num" => $info["error_num"],
+//                        "normal_num" => 0,
+//                        "received_num" => 0,
+//                        "apply_id" => $info['apply_id'],
+//                        "apply_name" => $info['apply_name'],
+//                        "cgder" => $info['cgder'],
+//                        "cgderid" => $info['cgderid'],
+//                        "person" => $info['person'],
+//                        "person_id" => $info['person_id'],
+//                        "contactor" => $returninfo["contactor"],
+//                        "mobile" => $returninfo["mobile"],
+//                        "addr" => $returninfo["addr"],
+//                        "addr_code" => $returninfo["addr_code"],
+//                        "return_code" => $info["error_code"],
+//                        "post_fee" => $returninfo["post_fee"],
+//                        "post_code" => $returninfo["post_code"],
+//                        "post_company" => $returninfo["post_company"],
+//                        "customer_code" => $info["customer_code"],
+//                        "customer_name" => $info["customer_name"],
+//                        "status" => $info['return_tag'] == 1 ? 4 : 1, //return_tag==1退回供应商,status:4完成退货,1待验收
+//                        "addtime" => date("Y-m-d H:i:s"),
+//                        "updatetime" => date("Y-m-d H:i:s"),
+//                        'supplierNo' => $info['supplierNo'],
+//                        'supplierName' => $info['supplierName'],
+//                        'companyNo' => $info['companyNo'],
+//                        'companyName' => $info['companyName'],
+//                    ];
+//                    $sav = Db::name("order_back")->insert($thdata, true);
+//                    if ($sav) {
+//
+//                        //修改状态,添加待办
+//                        ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
+//                            "order_code" => $thdata['thNo'],//销售单code
+//                            "status" => $thdata['status'],//这里的status是之前的值
+//                            "action_remark" => '',//备注
+//                            "action_type" => "create"//新建create,编辑edit,更改状态status
+//                        ], "CKTHD", $thdata['status'], $thdata);
+//
 //
-//                        		$tempnum=0;
-//                        		if ($total_num==0) break;
-//                        		if($total_num>=$ve['num']){
-//                        			$tempnum = $ve['num'];
-//                        			$total_num-=$ve['num'];
-//                        			$ve['num']=0;
-//                        		}else{
-//                        		    $tempnum = $total_num;
-//                        			$ve['num']-=$total_num;
-//                        			$total_num=0;
-//                        		}
-//                        		$bnin=GoodStockInfo::ReturnAdd($stockid,$ve['bnCode'],$tempnum,$ve['stockid']);
-//                        		if($bnin==false){
-//                        			 Db::rollback();
-//                                    return error_show(1005, '可售商品Bn库存数入库失败');
-//                        		}
-//								$up=Db::name("sale_info")->save($ve);
-//                        		if($up==false){
-//                        			 Db::rollback();
-//                                    return error_show(1005, '可售商品Bn库存数入库失败');
-//                        		}
-//								$bnin=GoodStockInfo::ReturnBn($info['returnCode'],$ve['id'],$tempnum);
-//                        		if($bnin==false){
-//                        			 Db::rollback();
-//                                    return error_show(1005, '可售商品Bn库存数入库失败');
-//                        		}
-//                        	}
+//                        //售后退货单,供应商不同意退货,退回到业务公司仓的时候,要把待办数据推给供应商负责人和库管人员
+//                        if ($thdata['status'] == 1) {
+//
+//                            //31库管人员,41库管-张凯旋
+//                            $uids = Db::name('user_role')
+//                                ->where(['is_del' => 0, 'roleid' => [31, 41], 'status' => 1])
+//                                ->column('uid');
+//
+//                            ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
+//                                "order_type" => 'CKTHD',
+//                                "order_code" => $thdata['thNo'],//销售单code
+//                                "order_id" => $sav,
+//                                "order_status" => $thdata['status'],
+//                                "before_status" => 0,
+//                                'handle_user_list' => implode(',', array_merge($uids, [$sale['cgderid']])),
+//                            ]);
+//                        } else {
+//                            ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
+//                                "order_type" => 'CKTHD',
+//                                "order_code" => $thdata['thNo'],//销售单code
+//                                "order_id" => $sav,
+//                                "order_status" => $thdata['status'],
+//                                "before_status" => 0
+//                            ]);
 //                        }
-//						}else{
 //
-//						}
-
-                        //退回供应商的时候,处理库存
-                        if ($info['return_tag'] == 1) {
-                            $sale['th_num'] += $info['error_num'];
-                            if ($sale['th_num'] == $sale['send_num'] && $sale['wsend_num'] == 0) {
-                                $sale['status'] = 3;
-                            }
-                            $sale['th_fee'] += round($info['error_num'] * $sale['sale_price'], 2);
-                            $sale['updatetime'] = date("Y-m-d H:i:s");
-                            $uap = Db::name("sale")->save($sale);
-
-                            if ($uap == false) throw new Exception('销售单订单更新失败');
-//                            if($uap==false){
-//                                Db::rollback();
-//                                return error_show(1005,'销售单订单更新失败');
+//
+//                        //维护台账记录
+////                        Db::name('standing_book')
+////                            ->where(['returnCode' => $bkcode])
+////                            ->update([
+////                                'thNo' => $thNo,
+////                                'updatetime' => date("Y-m-d H:i:s")
+////                            ]);
+//                        Db::execute("UPDATE `wsm_standing_book` SET `thNo`=CONCAT(IFNULL(`thNo`,''),',{$thNo}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE FIND_IN_SET('{$bkcode}',`returnCode`)");//这里的returnCode可能有多个
+//                        // 库存品退货 不退商品 需要计算成本
+////						if($info['except_code']==1){
+////							$sabebn =Db::name("sale_info")->where(["orderCode"=>$info['orderCode']])->select()->toArray();
+////                            	 if(!empty($sabebn)){
+////                        	$total_num =$info["error_num"];
+////                        	foreach ($sabebn as $ve){
+////
+////                        		$tempnum=0;
+////                        		if ($total_num==0) break;
+////                        		if($total_num>=$ve['num']){
+////                        			$tempnum = $ve['num'];
+////                        			$total_num-=$ve['num'];
+////                        			$ve['num']=0;
+////                        		}else{
+////                        		    $tempnum = $total_num;
+////                        			$ve['num']-=$total_num;
+////                        			$total_num=0;
+////                        		}
+////                        		$bnin=GoodStockInfo::ReturnAdd($stockid,$ve['bnCode'],$tempnum,$ve['stockid']);
+////                        		if($bnin==false){
+////                        			 Db::rollback();
+////                                    return error_show(1005, '可售商品Bn库存数入库失败');
+////                        		}
+////								$up=Db::name("sale_info")->save($ve);
+////                        		if($up==false){
+////                        			 Db::rollback();
+////                                    return error_show(1005, '可售商品Bn库存数入库失败');
+////                        		}
+////								$bnin=GoodStockInfo::ReturnBn($info['returnCode'],$ve['id'],$tempnum);
+////                        		if($bnin==false){
+////                        			 Db::rollback();
+////                                    return error_show(1005, '可售商品Bn库存数入库失败');
+////                        		}
+////                        	}
+////                        }
+////						}else{
+////
+////						}
+//
+//                        //退回供应商的时候,处理库存
+//                        if ($info['return_tag'] == 1) {
+//                            $sale['th_num'] += $info['error_num'];
+//                            if ($sale['th_num'] == $sale['send_num'] && $sale['wsend_num'] == 0) {
+//                                $sale['status'] = 3;
+//                            }
+//                            $sale['th_fee'] += round($info['error_num'] * $sale['sale_price'], 2);
+//                            $sale['updatetime'] = date("Y-m-d H:i:s");
+//                            $uap = Db::name("sale")->save($sale);
+//
+//                            if ($uap == false) throw new Exception('销售单订单更新失败');
+////                            if($uap==false){
+////                                Db::rollback();
+////                                return error_show(1005,'销售单订单更新失败');
+////                            }
+//                            if ($sale['is_stock'] == 0) {
+//                                $ordernum = Db::name("order_num")
+//                                    ->where(['orderCode' => $info['orderCode']])
+//                                    ->find();
+//                                if ($ordernum == false) throw new Exception('未找到关联采购单1');
+////	                            if($ordernum==false){
+////	                                Db::rollback();
+////	                                return error_show(1005,'未找到关联采购单');
+////	                            }
+//                                $ordernum['send_num'] -= $info['error_num'];
+//                                $orderup = Db::name("order_num")->save($ordernum);
+//
+//                                if ($orderup == false) throw new Exception('关联数据更新失败');
+//
+////	                            if($orderup==false){
+////	                                Db::rollback();
+////	                                return error_show(1005,'关联数据更新失败');
+////	                            }
+//                                $cgd = Db::name("purchease_order")
+//                                    ->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])
+//                                    ->findOrEmpty();
+//                                if (empty($cgd)) throw new Exception('未找到采购单数据');
+////	                            if($cgd==false){
+////	                                Db::rollback();
+////	                                return error_show(1005,'未找到采购单数据');
+////	                            }
+//                                $cgd['th_num'] += $info['error_num'];
+//                                if ($cgd['th_num'] == $cgd['send_num'] && $cgd['wsend_num'] == 0) {
+//                                    $cgd['status'] = 4;
+//                                }
+//                                $cgd['th_fee'] += round($info['error_num'] * $cgd['good_price'], 2);
+//                                $cgd['updatetime'] = date("Y-m-d H:i:s");
+//                                $cgdup = Db::name("purchease_order")->save($cgd);
+//
+//                                if ($cgdup == false) throw new Exception('采购单数据更新失败');
+////	                            if($cgdup==false){
+////	                                Db::rollback();
+////	                                return error_show(1005,'采购单数据更新失败');
+////	                            }
 //                            }
-                            if ($sale['is_stock'] == 0) {
-                                $ordernum = Db::name("order_num")
-                                    ->where(['orderCode' => $info['orderCode']])
-                                    ->find();
-                                if ($ordernum == false) throw new Exception('未找到关联采购单1');
-//	                            if($ordernum==false){
-//	                                Db::rollback();
-//	                                return error_show(1005,'未找到关联采购单');
-//	                            }
-                                $ordernum['send_num'] -= $info['error_num'];
-                                $orderup = Db::name("order_num")->save($ordernum);
-
-                                if ($orderup == false) throw new Exception('关联数据更新失败');
-
-//	                            if($orderup==false){
-//	                                Db::rollback();
-//	                                return error_show(1005,'关联数据更新失败');
-//	                            }
-                                $cgd = Db::name("purchease_order")
-                                    ->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])
-                                    ->findOrEmpty();
-                                if (empty($cgd)) throw new Exception('未找到采购单数据');
-//	                            if($cgd==false){
-//	                                Db::rollback();
-//	                                return error_show(1005,'未找到采购单数据');
-//	                            }
-                                $cgd['th_num'] += $info['error_num'];
-                                if ($cgd['th_num'] == $cgd['send_num'] && $cgd['wsend_num'] == 0) {
-                                    $cgd['status'] = 4;
-                                }
-                                $cgd['th_fee'] += round($info['error_num'] * $cgd['good_price'], 2);
-                                $cgd['updatetime'] = date("Y-m-d H:i:s");
-                                $cgdup = Db::name("purchease_order")->save($cgd);
-
-                                if ($cgdup == false) throw new Exception('采购单数据更新失败');
-//	                            if($cgdup==false){
-//	                                Db::rollback();
-//	                                return error_show(1005,'采购单数据更新失败');
-//	                            }
-                            }
-
-                            $sabebn = Db::name("sale_info")
-                                ->where([["orderCode", "=", $info['orderCode']], ["num", ">", 0]])
-                                ->select()
-                                ->toArray();
-                            if (!empty($sabebn)) {
-                                $total_num = $info["error_num"];
-                                foreach ($sabebn as $ve) {
-                                    $tempnum = 0;
-                                    if ($total_num == 0) break;
-                                    if ($total_num >= $ve['num']) {
-                                        $tempnum = $ve['num'];
-                                        $total_num -= $ve['num'];
-                                        $ve['th_num'] += $ve['num'];
-                                        $ve['num'] = 0;
-                                    } else {
-                                        $tempnum = $total_num;
-                                        $ve['num'] -= $total_num;
-                                        $ve['th_num'] += $total_num;
-                                        $total_num = 0;
-                                    }
-
-                                    if ($info['except_code'] == 1) {
-//		                                	if($sale['is_stock']==1){
-//		                                		$bnin=GoodStockInfo::AddBn($ve['stockid'],$ve['bnCode'],$tempnum);
-//				                                if($bnin==false){
-//				                                     Db::rollback();
-//				                                    return error_show(1005, '可售商品Bn库存数入库失败');
-//				                                }
-//		                                	}
-                                        $up = Db::name("sale_info")->save($ve);
-
-                                        if ($up == false) throw new Exception('可售商品Bn库存数入库失败');
-//				                                if($up==false){
-//				                                     Db::rollback();
-//				                                    return error_show(1005, '可售商品Bn库存数入库失败');
-//				                                }
-                                    }
-
-                                    $bnin = GoodStockInfo::ReturnBn($info['returnCode'], $ve['id'], $tempnum);
-                                    if ($bnin == false) throw new Exception('可售商品Bn库存数入库失败');
-//		                                if($bnin==false){
-//		                                     Db::rollback();
-//		                                    return error_show(1005, '可售商品Bn库存数入库失败');
-//		                                }
-                                }
-                            }
-                            if ($info['error_num'] >= $out['send_num']) {
-                                $out['status'] = 4;
-                                $out['updatetime'] = date("Y-m-d H:i:s");
-                                $upout = Db::name("order_out")->save($out);
-                                if ($upout == false) throw new Exception('出库单数据更新失败');
-//                                    if($upout==false){
-//                                        Db::rollback();
-//                                        return error_show(1005,'出库单数据更新失败');
+//
+//                            $sabebn = Db::name("sale_info")
+//                                ->where([["orderCode", "=", $info['orderCode']], ["num", ">", 0]])
+//                                ->select()
+//                                ->toArray();
+//                            if (!empty($sabebn)) {
+//                                $total_num = $info["error_num"];
+//                                foreach ($sabebn as $ve) {
+//                                    $tempnum = 0;
+//                                    if ($total_num == 0) break;
+//                                    if ($total_num >= $ve['num']) {
+//                                        $tempnum = $ve['num'];
+//                                        $total_num -= $ve['num'];
+//                                        $ve['th_num'] += $ve['num'];
+//                                        $ve['num'] = 0;
+//                                    } else {
+//                                        $tempnum = $total_num;
+//                                        $ve['num'] -= $total_num;
+//                                        $ve['th_num'] += $total_num;
+//                                        $total_num = 0;
 //                                    }
-                            }
-                            $data = [
-                                "orderCode" => $info['orderCode'],
-                                "th_type" => 3,
-                                "th_num" => $info['error_num'],
-                                "th_fee" => round($sale['sale_price'] * $info["error_num"], 2),
-                                "thCode" => $thNo,
-                                "spuCode" => $sale['good_code'],
-                                "good_name" => $sale['good_name'],
-                                "cat_id" => $sale['cat_id'],
-                                "apply_id" => $info['apply_id'],
-                                "apply_name" => $info['apply_name'],
-                                "addtime" => date("Y-m-d H:i:s"),
-                                "status" => 1,
-                                "is_del" => 0
-                            ];
-                            $inse = Db::name("th_data")->insert($data);
-                            if ($inse == false) throw new Exception('退货单更新失败');
-
-
-//                            if($inse==false){
-//                                Db::rollback();
-//                                return error_show(1004,"退货单更新失败");
+//
+//                                    if ($info['except_code'] == 1) {
+////		                                	if($sale['is_stock']==1){
+////		                                		$bnin=GoodStockInfo::AddBn($ve['stockid'],$ve['bnCode'],$tempnum);
+////				                                if($bnin==false){
+////				                                     Db::rollback();
+////				                                    return error_show(1005, '可售商品Bn库存数入库失败');
+////				                                }
+////		                                	}
+//                                        $up = Db::name("sale_info")->save($ve);
+//
+//                                        if ($up == false) throw new Exception('可售商品Bn库存数入库失败');
+////				                                if($up==false){
+////				                                     Db::rollback();
+////				                                    return error_show(1005, '可售商品Bn库存数入库失败');
+////				                                }
+//                                    }
+//
+//                                    $bnin = GoodStockInfo::ReturnBn($info['returnCode'], $ve['id'], $tempnum);
+//                                    if ($bnin == false) throw new Exception('可售商品Bn库存数入库失败');
+////		                                if($bnin==false){
+////		                                     Db::rollback();
+////		                                    return error_show(1005, '可售商品Bn库存数入库失败');
+////		                                }
+//                                }
 //                            }
-
-                        }
-
-                        Db::commit();
-                        return app_show(0, "退货单新建成功", ["thNo" => $thNo]);
-                    }
+//                            if ($info['error_num'] >= $out['send_num']) {
+//                                $out['status'] = 4;
+//                                $out['updatetime'] = date("Y-m-d H:i:s");
+//                                $upout = Db::name("order_out")->save($out);
+//                                if ($upout == false) throw new Exception('出库单数据更新失败');
+////                                    if($upout==false){
+////                                        Db::rollback();
+////                                        return error_show(1005,'出库单数据更新失败');
+////                                    }
+//                            }
+//                            $data = [
+//                                "orderCode" => $info['orderCode'],
+//                                "th_type" => 3,
+//                                "th_num" => $info['error_num'],
+//                                "th_fee" => round($sale['sale_price'] * $info["error_num"], 2),
+//                                "thCode" => $thNo,
+//                                "spuCode" => $sale['good_code'],
+//                                "good_name" => $sale['good_name'],
+//                                "cat_id" => $sale['cat_id'],
+//                                "apply_id" => $info['apply_id'],
+//                                "apply_name" => $info['apply_name'],
+//                                "addtime" => date("Y-m-d H:i:s"),
+//                                "status" => 1,
+//                                "is_del" => 0
+//                            ];
+//                            $inse = Db::name("th_data")->insert($data);
+//                            if ($inse == false) throw new Exception('退货单创建失败');
+//
+//
+////                            if($inse==false){
+////                                Db::rollback();
+////                                return error_show(1004,"退货单更新失败");
+////                            }
+//
+//                        }
+//
+//                        Db::commit();
+//                        return app_show(0, "退货单新建成功", ["thNo" => $thNo]);
+//                    }
                 }
             }
-            Db::rollback();
-            return error_show(1004, '退货失败');
+            Db::commit();
+            return json_show(0, '退货成功');
         } catch (Exception $e) {
             Db::rollback();
-            return error_show(1004, $e->getMessage());
+            return json_show(1004, $e->getMessage());
         }
     }
 
@@ -1636,11 +1639,11 @@ class After extends Base
                 ->field('id,status,apply_id')
                 ->where([
                     ['returnCode', '=', $data['returnCode']],
-                    ['is_del', '=', 0],
-                    ['status', '<>', 8]
+                    ['is_del', '=', 0]
                 ])->findOrEmpty();
 
-            if (!$rs) throw new Exception('该售后单记录不存在');
+            if (empty($rs)) throw new Exception('该售后单记录不存在');
+            if (in_array($rs['status'], [4, 12, 5, 8])) throw new Exception('不允许取消');
 
             //修改记录
             $res = Db::name('order_return')->where(['id' => $rs['id']])->update(['status' => 8, 'updatetime' => date('Y-m-d H:i:s')]);

+ 464 - 0
app/admin/controller/AfterChild.php

@@ -0,0 +1,464 @@
+<?php
+
+namespace app\admin\controller;
+
+use app\admin\model\GoodStockInfo;
+use think\Exception;
+use think\facade\Db;
+use think\facade\Validate;
+
+//退货工单
+class AfterChild extends Base
+{
+
+    //退货工单列表
+    public function getList()
+    {
+
+        $param = $this->request->only(['page' => 1, 'size' => 10, 'returnCode' => '', 'status' => '', 'orderCode' => '', 'outCode' => ''], 'post', 'trim');
+
+        $where = [['a.is_del', '=', 0]];
+        if ($param['returnCode'] != '') $where[] = ['a.returnCode', 'like', '%' . $param['returnCode'] . '%'];
+        if ($param['status'] !== '') $where[] = ['a.status', '=', $param['status']];
+        if ($param['orderCode'] != '') $where[] = ['a.orderCode', 'like', '%' . $param['orderCode'] . '%'];
+        if ($param['outCode'] != '') $where[] = ['a.outCode', 'like', '%' . $param['outCode'] . '%'];
+
+        $count = Db::name('order_return_child')
+            ->alias('a')
+            ->leftJoin('warehouse_info b', 'b.wsm_code=a.send_wsm_code')
+            ->leftJoin('warehouse_info c', 'c.wsm_code=a.return_wsm_code')
+            ->where($where)
+            ->count('a.id');
+
+        $list = Db::name('order_return_child')
+            ->alias('a')
+            ->field('a.id,a.returnCode,a.orderCode,a.outCode,a.order_out_status,a.outChildCode,b.supplierName send_supplierName,b.supplierNo send_supplierNo,b.name send_wsm_name,a.send_wsm_code,b.contactor_name send_contactor_name,a.send_num,a.return_num,c.supplierName return_supplierName,c.supplierNo return_supplierNo,c.name return_wsm_name,a.return_wsm_code,c.contactor_name return_contactor_name,a.status')
+            ->leftJoin('warehouse_info b', 'b.wsm_code=a.send_wsm_code')
+            ->leftJoin('warehouse_info c', 'c.wsm_code=a.return_wsm_code')
+            ->where($where)
+            ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
+            ->page($param['page'], $param['size'])
+            ->select()
+            ->toArray();
+
+        return json_show(0, '获取列表成功', ['count' => $count, 'list' => $list]);
+
+    }
+
+    //设置退货工单
+    public function setOrderReturnChild()
+    {
+
+        $param = $this->request->only(['returnCode', 'list'], 'post', 'trim');
+
+        $val = Validate::rule([
+            'returnCode|售后申请单编码' => 'require',
+            'list|工单集合' => 'require|array|max:100',
+        ]);
+        if ($val->check($param) == false) return json_show(1004, $val->getError());
+
+        $info = Db::name('order_return')
+            ->where(['is_del' => 0, 'returnCode' => $param['returnCode']])
+            ->findOrEmpty();
+        if (empty($info)) return json_show(1004, '售后申请单不存在');
+        if ($info['status'] != 11) return json_show(1004, '售后申请单状态错误');
+        if ($info['is_receive'] != 1) return json_show(1004, '未收到货,无需设置退货工单');
+
+        $order_out = Db::name('order_out')
+            ->field('id,status order_out_status,orderCode')
+            ->where(['is_del' => 0, 'outCode' => $info['outCode']])
+            ->findOrEmpty();
+        if (empty($order_out)) return json_show(1004, '未找到对应的发货单');
+
+        $val_child = Validate::rule([
+            'outChildCode|发货工单号' => 'require',
+            'return_num|退货数量' => 'require|number|gt:0|max:999999999999',
+            'return_wsm_code|退货仓库编码' => 'require'
+        ]);
+
+        //发货工单详情
+        $order_out_child = Db::name('order_out_child')
+            ->where(['is_del' => 0, 'outCode' => $info['outCode'], 'outChildCode' => array_column($param['list'], 'outChildCode')])
+            ->column('num,wsm_code', 'outChildCode');
+
+        //所有仓库详情
+        $wsm_info = Db::name('warehouse_info')
+            ->where(['is_del' => 0, 'wsm_code' => array_unique(array_merge(array_column($order_out_child, 'wsm_code'), array_column($param['list'], 'return_wsm_code')))])
+            ->column('id', 'wsm_code');
+
+        Db::startTrans();
+        try {
+
+            $insert = [];
+            $date = date('Y-m-d H:i:s');
+            foreach ($param['list'] as $value) {
+                if ($val_child->check($value) == false) throw new Exception($val_child->getError());
+
+                if (!isset($order_out_child[$value['outChildCode']])) throw new Exception($value['outChildCode'] . '发货工单不存在');
+                if (!isset($wsm_info[$value['return_wsm_code']])) throw new Exception($value['return_wsm_code'] . '退货仓库不存在');
+
+                $insert[] = [
+                    'returnCode' => $param['returnCode'],
+                    'orderCode' => $order_out['orderCode'],
+                    'outCode' => $info['outCode'],
+                    'order_out_status' => $order_out['order_out_status'],
+                    'outChildCode' => $value['outChildCode'],
+                    'send_wsm_code' => $order_out_child[$value['outChildCode']]['wsm_code'],
+                    'send_num' => $order_out_child[$value['outChildCode']]['num'],
+                    'return_num' => $value['return_num'],
+                    'return_wsm_code' => $value['return_wsm_code'],
+                    'can_sell_num' => 0,
+                    'defective_num' => 0,
+                    'loss_num' => 0,
+                    'apply_id' => $this->uid,
+                    'apply_name' => $this->uname,
+                    'status' => 1,
+                    'is_del' => 0,
+                    'remark' => '',
+                    'addtime' => $date,
+                    'updatetime' => $date,
+                ];
+
+            }
+
+            if ($insert) Db::name('order_return_child')->insertAll($insert);
+
+            Db::name('order_return')
+                ->where(['is_del' => 0, 'id' => $info['id'], 'status' => 11])
+                ->update(['status' => 4, 'updatetime' => $date]);
+
+            Db::commit();
+            return json_show(0, '设置退货工单成功');
+        } catch (Exception $exception) {
+            Db::rollback();
+            return json_show(1004, $exception->getMessage());
+        }
+
+
+    }
+
+    //库管收货
+    public function deliveryOrderReturnChild()
+    {
+        $param = $this->request->only(['returnCode', 'list'], 'post', 'trim');
+
+        $val = Validate::rule([
+            'returnCode|退货申请单号' => 'require',
+            'list|退货工单集合' => 'require|array|max:100',
+        ]);
+
+        if ($val->check($param)) return json_show(1004, $val->getError());
+
+        $order_return = Db::name('order_return')
+            ->alias('a')
+            ->field('a.id,a.good_code,a.outCode,b.orderCode,c.is_stock,c.order_type')
+            ->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']])
+            ->findOrEmpty();
+        if (empty($order_return)) return json_show(1004, '售后申请单不存在');
+
+        $info = Db::name('order_return_child')
+            ->alias('a')
+            ->leftJoin('order_out_child b', 'b.outChildCode=a.outChildCode')
+            ->where(['a.is_del' => 0, 'a.returnCode' => $param['returnCode'], 'a.id' => array_column($param['list'], 'id')])
+            ->column('a.id,a.return_wsm_code,a.outChildCode,b.wsm_code,a.status', 'a.id');
+        if (empty($info)) return json_show(1004, '退货工单不存在');
+
+        $val_child = Validate::rule([
+            'id|退货工单id' => 'require|gt:0|max:999999999',
+            'can_sell_num|销售仓入库数量' => 'require|number|egt:0|max:999999999999',
+            'defective_num|次品仓入库数量' => 'require|number|egt:0|max:999999999999',
+            'loss_num|丢失数量' => 'require|number|egt:0|max:999999999999',
+            'remark|备注' => 'max:255'
+        ]);
+
+        Db::startTrans();
+        try {
+
+            $date = date('Y-m-d H:i:s');
+            foreach ($param['list'] as $value) {
+                if ($val_child->check($value) == false) throw new Exception($val_child->getError());
+                if (!isset($info[$value['id']])) throw new Exception($value['id'] . '退货工单不存在');
+                if ($info[$value['id']]['status'] != 1) throw new Exception($value['id'] . '退货工单状态有误');
+
+                //退货工单
+                Db::name('order_return_child')
+                    ->where(['is_del' => 0, 'id' => $value['id']])
+                    ->update([
+                        'can_sell_num' => $value['can_sell_num'],
+                        'defective_num' => $value['defective_num'],
+                        'loss_num' => $value['loss_num'],
+                        'remark' => $value['remark'],
+                        'updatetime' => $date,
+                        'status' => 2
+                    ]);
+            }
+
+            $total = array_sum(array_column($param['list'],'can_sell_num'));
+
+            if($total>0){
+                if ($order_return['is_stock'] != 1) {
+                    //根据转化表判断该非库存品是否转化为库存品
+                    $spuCode = Db::name('good_change_stock')
+                        ->where(['old_spuCode'=>$order_return['good_code'],'old_order_type'=>$order_return['order_type']])
+                        ->value('spuCode','');
+                    if ($spuCode=='') {
+                        //将非库存品转化为库存品
+                        if ($order_return['order_type'] == 2) {
+                            $good = Db::name('good_basic')
+                                ->where('spuCode',$order_return['good_code'])
+                                ->findOrEmpty();
+                        } else {
+                            $good = Db::name('good_zixun')
+                                ->where([])
+                                ->findOrEmpty();
+                        }
+
+                        if (!empty($good)) {
+                            $spuCode = makeNo('SKU');
+                            Db::name('good_basic')->insert([
+                                'spuCode' => $spuCode,
+                                'good_code' => $good['good_code'],
+                                'good_name' => $good['good_name'],
+                                'cat_id' => $good['cat_id'],
+                                'brand_id' => $good['brand_id'],
+                                'companyNo' => $good['companyNo'],
+                                'companyName' => $good['companyName'],
+                                'good_unit' => $good['good_unit'],
+                                'good_type' => $good['good_type'],
+                                'moq' => $good['moq'],
+                                'is_exclusive' => 0,
+                                'customized' => $good['customized'],
+                                'tax' => $good['tax'],
+                                'supplierNo' => $good['supplierNo'],
+                                'supplierName' => $good['supplierName'],
+                                'is_auth' => $good['is_auth'],
+                                'auth_img' => $good['auth_img'],
+                                'is_stock' => 1,
+                                'after_sales' => '',
+                                'craft_desc' => $good['craft_desc'],
+                                'good_remark' => $good['good_remark'],
+                                'good_size' => '',
+                                'weight' => $good['weight'],
+                                'packing_way' => '',
+                                'packing_size' => '',
+                                'packing_spec' => '',
+                                'packing_list' => '',
+                                'packing_weight' => 0,
+                                'good_bar' => '',
+                                'supply_area' => $good['supply_area'],
+                                'delivery_place' => '',
+                                'origin_place' => '',
+                                'delivery_day' => '',
+                                'lead_time' => '',
+                                'sample_day' => '',
+                                'sample_fee' => '',
+                                'good_img' => $good['good_img'],
+                                'good_thumb_img' => $good['good_thumb_img'],
+                                'good_info_img' => $good['good_info_img'],
+                                'cert_fee' => 0,
+                                'packing_fee' => 0,
+                                'cost_fee' => 0,
+                                'mark_fee' => 0,
+                                'demo_fee' => 0,
+                                'open_fee' => 0,
+                                'noble_metal' => $good['noble_metal'],
+                                'noble_weight' => 0,
+                                'is_gold_price' => $good['is_gold_price'],
+                                'cgd_gold_price' => 0,
+                                'market_price' => 0,
+                                'nake_price' => 0,
+                                'is_step' => 0,
+                                'is_online' => 0,
+                                'status' => 0,
+                                'createrid' => $good['createrid'],
+                                'creater' => $good['creater'],
+                                'field_change' => '',
+                                'is_del' => 0,
+                                'addtime' => $date,
+                                'updatetime' => $date,
+                                'is_diff' => 0,
+                                'config' => '',
+                                'other_config' => '',
+                                'stock_moq' => 0,
+                                'is_support_barter' => 1,
+                                'chargerid' => $good['createrid'],
+                                'charger' => $good['creater'],
+                                'is_support_stock' => 1
+                            ]);
+
+                            //关联表增数据
+                            Db::name('good_change_stock')
+                                ->insert([
+                                    'old_spuCode'=>$order_return['good_code'],
+                                    'old_order_type'=>$order_return['order_type'],
+                                    'spuCode'=>$spuCode,
+                                    'addtime' => $date,
+                                    'updatetime' => $date,
+                                ]);
+
+                        }throw new Exception('未找到对应的商品数据');
+
+                    }
+
+                    //新增bn记录,维护旧有bn记录
+                    $child_bns=Db::name('child_bn')
+                        ->field('id,num,origin_price')
+                        ->where(['orderCode'=>$order_return['orderCode'],'outCode'=>$order_return['outCode']])
+                        ->order(['num'=>'desc','id'=>'asc'])
+                        ->cursor();
+
+                    $origin_price=$j=0;
+                    foreach ($child_bns as $child_bn){
+                        if($origin_price===0) $origin_price=$child_bn['origin_price'];
+                        Db::name('child_bn')
+                            ->where(['id'=>$child_bn['id']])
+                            ->dec('num',($child_bn['num']>=$total)?$total:$child_bn['num'])
+                            ->update(['updatetime'=>$date]);
+                        $total=($child_bn['num']>=$total)?0:($total-$child_bn['num']);
+                        if($total==0) break;
+                    }
+
+                    foreach ($param['list'] as $value) {
+                        if ($value['can_sell_num'] > 0) {
+                            $stockid = Db::name('good_stock')
+                                ->insertGetId([
+                                    'project_code' => '',
+                                    'spuCode' => $spuCode,
+                                    'wsm_code' => $value['return_wsm_code'],
+                                    'wait_in_stock' => 0,
+                                    'wait_out_stock' => 0,
+                                    'usable_stock' => $value['can_sell_num'],
+                                    'intra_stock' => 0,
+                                    'total_stock' => $value['can_sell_num'],
+                                    'status' => 1,
+                                    'is_del' => 0,
+                                    'warn_stock' => 0,
+                                    'addtime' => $date,
+                                    'updatetime' => $date,
+                                ]);
+
+                            Db::name('good_stock_info')
+                                ->insert([
+                                    'stockid'=>$stockid,
+                                    'bnCode'=>substr(makeNo("BN"), 0, -2) . str_pad($j++, 2, '0', STR_PAD_LEFT),
+                                    'total_num'=>$value['can_sell_num'],
+                                    'used_num'=>0,
+                                    'balance_num'=>$value['can_sell_num'],
+                                    'origin_price'=>$origin_price,
+                                    'addtime' => $date,
+                                    'updatetime' => $date,
+                                ]);
+                        }
+                    }
+                }
+                else{
+                    //库存品,直接维护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');
+
+                    foreach ($param['list'] as $value){
+
+                        //定义了stockid,维护bn
+                        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();
+
+                            if(empty($good_stock_info)){
+                                //从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['can_sell_num'];
+                                foreach ($child_bns as $child_bn){
+                                    Db::name('child_bn')
+                                        ->where(['id'=>$child_bn['id']])
+                                        ->dec('num',($child_bn['num']>=$total)?$total:$child_bn['num'])
+                                        ->update(['updatetime'=>$date]);
+
+                                    Db::name('good_stock_info')
+                                        ->insert([
+                                            'stockid'=>$stockids[$info[$value['id']]['return_wsm_code']],
+                                            '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,
+                                        ]);
+
+                                    $total=($child_bn['num']>=$total)?0:($total-$child_bn['num']);
+                                    if($total==0) break;
+                                }
+                            }else{
+                                //维护记录
+                                Db::name('good_stock_info')
+                                    ->where(['id'=>$good_stock_info['id']])
+                                    ->inc('balance_num',$value['can_sell_num'])//可用数量增加
+                                    ->dec('used_num',$value['can_sell_num'])//已用数量减少
+                                    ->update(['updatetime'=>$date]);
+                            }
+                        }
+                        else{
+                            //新建good_stock和good_stock_info
+
+                            $stockid = Db::name('good_stock')
+                                ->insertGetId([
+                                    'project_code' => '',
+                                    'spuCode' => $order_return['good_code'],
+                                    'wsm_code' => $value['return_wsm_code'],
+                                    'wait_in_stock' => 0,
+                                    'wait_out_stock' => 0,
+                                    'usable_stock' => $value['can_sell_num'],
+                                    'intra_stock' => 0,
+                                    'total_stock' => $value['can_sell_num'],
+                                    'status' => 1,
+                                    'is_del' => 0,
+                                    'warn_stock' => 0,
+                                    'addtime' => $date,
+                                    'updatetime' => $date,
+                                ]);
+
+                            Db::name('good_stock_info')
+                                ->insert([
+                                    'stockid'=>$stockid,
+                                    'bnCode'=>substr(makeNo("BN"), 0, -2) . str_pad($j++, 2, '0', STR_PAD_LEFT),
+                                    'total_num'=>$value['can_sell_num'],
+                                    'used_num'=>0,
+                                    'balance_num'=>$value['can_sell_num'],
+                                    'origin_price'=>$origin_price,
+                                    'addtime' => $date,
+                                    'updatetime' => $date,
+                                ]);
+
+
+
+
+
+                        }
+
+
+
+                    }
+
+                }
+            }
+
+            Db::commit();
+            return json_show(0,'库管收货成功');
+        } catch (Exception $exception) {
+            Db::rollback();
+            return json_show(1004, $exception->getMessage());
+        }
+
+
+    }
+
+}

+ 38 - 37
app/admin/controller/CompanyCatProfit.php

@@ -200,57 +200,58 @@ class CompanyCatProfit extends Base
             ['a.is_del', '=', 0],
             ['b.level', '=', 3],
         ];
+        if ($param['cat_name'] !== '') $where[] = ['b.search', 'like', '%' . $param['cat_name'] . '%'];
 
         //如果搜索名字,要把所有符合要求的三级分类id展现出来
-        if ($param['cat_name'] != '') {
-
-            $cat_id = Db::name('cat')
-                ->where(['is_del' => 0])
-                ->whereLike('cat_name', '%' . $param['cat_name'] . '%')
-                ->column('id');
-            if (!empty($cat_id)) {
-                $catid = [];
+//        if ($param['cat_name'] != '') {
+//
+//            $cat_id = Db::name('cat')
+//                ->where(['is_del' => 0])
+//                ->whereLike('cat_name', '%' . $param['cat_name'] . '%')
+//                ->column('id');
+//            if (!empty($cat_id)) {
+//                $catid = [];
+////                $tmp_1 = Db::name('cat')
+////                    ->where(['is_del'=>0,'pid'=>$cat_id,'level'=>3])
+////                    ->column('id');
+////                if(!empty($tmp_1)) $catid = array_merge($catid,$tmp_1);
+//
 //                $tmp_1 = Db::name('cat')
-//                    ->where(['is_del'=>0,'pid'=>$cat_id,'level'=>3])
+//                    ->where(['is_del' => 0, 'pid' => $cat_id])
 //                    ->column('id');
-//                if(!empty($tmp_1)) $catid = array_merge($catid,$tmp_1);
-
-                $tmp_1 = Db::name('cat')
-                    ->where(['is_del' => 0, 'pid' => $cat_id])
-                    ->column('id');
-                if (!empty($tmp_1)) {
-                    $catid = array_merge($catid, $tmp_1);
-                    $tmp_2 = Db::name('cat')
-                        ->where(['is_del' => 0, 'pid' => $tmp_1])
-                        ->column('id');
-                    if (!empty($tmp_2)) {
-                        $catid = array_merge($catid, $tmp_2);
-                        $tmp_3 = Db::name('cat')
-                            ->where(['is_del' => 0, 'pid' => $tmp_2])
-                            ->column('id');
-                        if (!empty($tmp_3)) $catid = array_merge($catid, $tmp_3);
-                    }
-                }
-
-                if ($catid) $where[] = ['a.cat_id', 'in', $catid];
-            }
-
-        }
+//                if (!empty($tmp_1)) {
+//                    $catid = array_merge($catid, $tmp_1);
+//                    $tmp_2 = Db::name('cat')
+//                        ->where(['is_del' => 0, 'pid' => $tmp_1])
+//                        ->column('id');
+//                    if (!empty($tmp_2)) {
+//                        $catid = array_merge($catid, $tmp_2);
+//                        $tmp_3 = Db::name('cat')
+//                            ->where(['is_del' => 0, 'pid' => $tmp_2])
+//                            ->column('id');
+//                        if (!empty($tmp_3)) $catid = array_merge($catid, $tmp_3);
+//                    }
+//                }
+//
+//                if ($catid) $where[] = ['a.cat_id', 'in', $catid];
+//            }
+//
+//        }
 
         $count = Db::name('company_cat_profit')
             ->alias('a')
             ->leftJoin('cat b', 'b.id=a.cat_id')
-            ->leftJoin('cat c', 'b.id=b.pid')
-            ->leftJoin('cat d', 'd.id=c.pid')
+//            ->leftJoin('cat c', 'b.id=b.pid')
+//            ->leftJoin('cat d', 'd.id=c.pid')
             ->where($where)
             ->count('a.id');
 
         $list = Db::name('company_cat_profit')
             ->alias('a')
             ->leftJoin('cat b', 'b.id=a.cat_id')
-            ->leftJoin('cat c', 'c.id=b.pid')
-            ->leftJoin('cat d', 'd.id=c.pid')
-            ->field('a.id,concat_ws("_",d.cat_name,c.cat_name,b.cat_name) cat_name,a.cat_id,a.rate,a.order_rate,a.money_rate,a.low_rate,a.sale_rate,a.status,c.pid')
+//            ->leftJoin('cat c', 'c.id=b.pid')
+//            ->leftJoin('cat d', 'd.id=c.pid')
+            ->field('a.id,b.search cat_name,a.cat_id,a.rate,a.order_rate,a.money_rate,a.low_rate,a.sale_rate,a.status')
             ->where($where)
             ->order(['a.id' => 'desc'])
             ->page($param['page'], $param['size'])

+ 13 - 13
app/admin/controller/Consult.php

@@ -99,7 +99,7 @@ class Consult extends Base
         $all_createrid = array_column($list,'createrid');
         $item = get_company_name_by_uid($all_createrid);
 
-		 $userCommon = new \app\admin\common\User();
+		 $userCommon = \app\admin\common\User::getIns();
         $names = $userCommon->handle('getCodeAndName', ['code' =>array_unique(array_merge(array_column($list,"khNo"),array_column($list,"companyNo")))]);
 //        $kh = $tmp['data']??[];
 
@@ -108,7 +108,7 @@ class Consult extends Base
             ->where(['id'=>array_column($list,'brand_id')])
             ->column('brand_name','id');
 
-//        $userCommon = new \app\admin\common\User();
+//        $userCommon = \app\admin\common\User::getIns();
 //        $names = $userCommon->handle('getCodeAndName', ['code' => array_column($list, 'khNo')]);
 
 
@@ -417,7 +417,7 @@ class Consult extends Base
 ////            $zxinfo['khname']= isset($kh['companyName'])?$kh['companyName']:"";
 //=======
 
-        $userCommon = new \app\admin\common\User();
+        $userCommon = \app\admin\common\User::getIns();
         $tmp = $userCommon->handle('getCodeAndName', ['code' => [$zxorder['companyNo'],$zxorder['khNo']]]);
 
 //        if($zxorder['khNo']!==""){
@@ -443,7 +443,7 @@ class Consult extends Base
 //            $kccode[]=$zxorder['companyNo'];
 //        }
 //        if(!empty($kccode)){
-//            $userCommon = new \app\admin\common\User();
+//            $userCommon = \app\admin\common\User::getIns();
 //            $tmp = $userCommon->handle('getCodeAndName', ['code' =>$kccode]);
 ////=======
 ////>>>>>>> master-new
@@ -911,7 +911,7 @@ class Consult extends Base
 //            ->field('id,person,personid')
 //            ->where('code',$supplierNo)
 //            ->findOrEmpty();
-	    $userCommon= new \app\admin\common\User();
+	    $userCommon= \app\admin\common\User::getIns();
         $supplier_temp =$userCommon->handle("hqInfo",["code"=>$supplierNo]);
         if($supplier_temp['code']!=0 || empty($supplier_temp['data']) ) return error_show(1004,"供应商信息未找到");
 		$supplierArr=$supplier_temp['data']??[];
@@ -1094,7 +1094,7 @@ class Consult extends Base
         $info['unit'] = isset($unit['unit'])?$unit['unit']:'';
         $info['cat_info'] = made($info['cat_id'],[]);
         $info['noble_weight'] = $info['good_weight'];
-        $user= new \app\admin\common\User();
+        $user= \app\admin\common\User::getIns();
         $supplier =$user->handle("getCodeAndName",["code"=>$info['supplierNo']]);
 //        $supplier = Db::name("supplier")->where(["code"=>$info['supplierNo']])->find();
         $info['supplierName'] = isset($supplier['data'][$info['supplierNo']])?$supplier['data'][$info['supplierNo']]:"";
@@ -1260,7 +1260,7 @@ class Consult extends Base
             $saleprice = ($demo_fee / $zxinfo['num']+$delivery_fee+$open_fee / $zxinfo['num'] + $cost_fee*$noble_weight + $pakge_fee + $mark_fee + $cert_fee + $nake_fee) / (1 - $budget);
             $total_fee = $demo_fee/$zxinfo['num']+$delivery_fee+$open_fee/$zxinfo['num'] + $cost_fee * $noble_weight + $pakge_fee + $mark_fee+$cert_fee +$nake_fee;
         }
-        $userCommon= new \app\admin\common\User();
+        $userCommon= \app\admin\common\User::getIns();
         $supplier_temp =$userCommon->handle("hqInfo",["code"=>$supplierNo]);
         if($supplier_temp['code']!=0 || empty($supplier_temp['data']) ) return error_show(1004,"供应商信息未找到");
 		$supplierArr=$supplier_temp['data']??[];
@@ -1918,7 +1918,7 @@ class Consult extends Base
             $info['origin_addr'] = $addr;
         }
         $info['supplierNo'] =$bidinfos['supplierNo'];
-        $user= new \app\admin\common\User();
+        $user= \app\admin\common\User::getIns();
         $supplier_temp =$user->handle("getCodeAndName",["code"=>$info['supplierNo']]);
 //        if($supplier_temp['code']!=0 || empty($supplier_temp['data']) ) throw new Exception('该供应商不存在');
 		$supplier=$supplier_temp['data']??[];
@@ -2014,7 +2014,7 @@ class Consult extends Base
         $page = $page >= $total ? $total : $page;
         $list =Db::name('consult_bids')->where($where)->page($page,$size)->select()->toArray();
         $data=[];
-        $user= new \app\admin\common\User();
+        $user= \app\admin\common\User::getIns();
         $supplier_temp =$user->handle("getCodeAndName",["code"=>array_column($list,"supplierNo")]);
 //        if($supplier_temp['code']!=0 || empty($supplier_temp['data']) ) throw new Exception('该供应商不存在');
 		$supplier=$supplier_temp['data']??[];
@@ -2144,7 +2144,7 @@ class Consult extends Base
         }
         $list =Db::name('consult_bids')->where($where)->select();
         $data=[];
-        $user= new \app\admin\common\User();
+        $user= \app\admin\common\User::getIns();
         $supplier_temp =$user->handle("getCodeAndName",["code"=>array_column($list,"supplierNo")]);
 //        if($supplier_temp['code']!=0 || empty($supplier_temp['data']) ) throw new Exception('该供应商不存在');
 		$supplier=$supplier_temp['data']??[];
@@ -2298,7 +2298,7 @@ class Consult extends Base
         $all_createrid = array_column($list, 'createrid');
         $item = get_company_name_by_uid($all_createrid);
 
-        $userCommon = new \app\admin\common\User();
+        $userCommon = \app\admin\common\User::getIns();
         $names = $userCommon->handle('getCodeAndName', ['code' => array_column($list, 'companyNo')]);
 
         $bidinfos_list = Db::name('consult_bids')
@@ -2413,7 +2413,7 @@ class Consult extends Base
         if ($status !== "") {
             $where[]= ["a.status",'=',$status];
         }
-        $userCommon=new \app\admin\common\User();
+        $userCommon=\app\admin\common\User::getIns();
 
         $supplierNo = trim($this->post['supplierNo'] ?? '');
         if ($supplierNo != '') {
@@ -2575,7 +2575,7 @@ class Consult extends Base
             ->select()
             ->toArray();
         $data = [];
-        $user= new \app\admin\common\User();
+        $user= \app\admin\common\User::getIns();
         $supplier_temp =$user->handle("getCodeAndName",["code"=>array_unique(array_merge(array_column($list,"supplierNo"),array_column($list,"companyNo")))]);
 //        if($supplier_temp['code']!=0 || empty($supplier_temp['data']) ) throw new Exception('该供应商不存在');
 		$supplier=$supplier_temp['data']??[];

+ 2 - 1
app/admin/controller/Sale.php

@@ -378,7 +378,8 @@ class Sale extends Base
                                 throw new Exception("发货地址添加创建失败");
 //                                return error_show(1002, "发货地址添加创建失败");
                             } else {
-                            	OrderOutChild::makeChild($outCode);
+                                if($is_stock==1) OrderOutChild::makeChild($outCode);//库存品拆单
+
                                 //修改状态,添加待办
                                 ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
                                     "order_code" => $outCode,//出库单号

+ 2 - 0
app/admin/route/app.php

@@ -301,6 +301,8 @@ Route::rule('afterwsm', 'admin/After/GetWsm');
 Route::rule('aftercancel', 'admin/After/Cancel');
 Route::rule('createSaleAndCgdByAfter', 'admin/After/createSaleAndCgdByAfter');
 Route::rule('afterExport', 'admin/After/export');
+Route::rule('after_child_list', 'admin/AfterChild/getList');//退货工单列表
+Route::rule('set_order_return_child', 'admin/AfterChild/setOrderReturnChild');//设置退货工单
 //Route::rule('aftersetwsm','admin/After/setWsm');
 
 Route::rule('processolist', 'admin/Proorder/list');//我的已办