wugg 1 vuosi sitten
vanhempi
commit
d0177a2be9

+ 29 - 348
app/admin/controller/After.php

@@ -7,7 +7,7 @@ use app\admin\model\DataGroup as DataGroupModel;
 use app\admin\model\GoodLog;
 use app\admin\model\GoodStockInfo;
 use app\admin\model\ProcessOrder;
-use think\App;
+use app\admin\model\ThData;use think\App;
 use think\Exception;
 use think\facade\Db;
 use think\facade\Validate;
@@ -505,7 +505,7 @@ class After extends Base
     //9供应商审核,
     //10业务公司修改待供应商确认
     //11待设置退货工单(已收到货才有),
-    //12待库管收货(已收到货才有)
+    //12待库管收货(已收到货才有),
     public function status()
     {
         try {
@@ -724,29 +724,7 @@ class After extends Base
                     $order = ["order_code" => $info['returnCode'], "status" => $var, "action_remark" => '', "action_type" => "edit"];
                     ActionLog::logAdd($action_log_user, $order, "SHD", $info['status'], $this->post);
 
-//                switch ($param['status']) {
-//                    //待采购审核
-//                    case 2:
-//                        $process = ["order_code" => $info['returnCode'], "order_id" => $info['id'], "order_status" => $param['status'], "order_type" => "SHD", "before_status" => $var, 'wait_id' => $info['person_id'], 'wait_name' => $info['person'], 'holder_id' => $info['person_id']];
-//                        break;
-//
-//                    //待设置仓库
-//                    case 3:
-//                        $process = ["order_code" => $info['returnCode'], "order_id" => $info['id'], "order_status" => $param['status'], "order_type" => "SHD", "before_status" => $var, 'wait_id' => $info['cgderid'], 'wait_name' => $info['cgder'], 'holder_id' => $info['cgderid']];
-//                        break;
-//
-//                    //待客户退货
-//                    case 4:
-//                        $process = ["order_code" => $info['returnCode'], "order_id" => $info['id'], "order_status" => $param['status'], "order_type" => "SHD", "before_status" => $var, 'wait_id' => $info['apply_id'], 'wait_name' => $info['apply_name'], 'holder_id' => $info['apply_id']];
-//                        break;
-//
-//                    default:
-//                        $process = ["order_code" => $info['returnCode'], "order_id" => $info['id'], "order_status" => $param['status'], "order_type" => "SHD", "before_status" => $var, 'holder_id' => $info['apply_id']];
-//                }
-//                if (in_array($param['status'], [2, 3])) $process = ["order_code" => $info['returnCode'], "order_id" => $info['id'], "order_status" => $param['status'], "order_type" => "SHD", "before_status" => $var, 'wait_id' => $info['cgderid'], 'wait_name' => $info['cgder'],'holder_id'=>$info['cgderid']];
-//                elseif ($param['status'] == 4) $process = ["order_code" => $info['returnCode'], "order_id" => $info['id'], "order_status" => $param['status'], "order_type" => "SHD", "before_status" => $var, 'wait_id' => $info['apply_id'], 'wait_name' => $info['apply_name'],'holder_id'=>$info['apply_id']];
-//                else $process = ["order_code" => $info['returnCode'], "order_id" => $info['id'], "order_status" => $param['status'], "order_type" => "SHD", "before_status" => $var,'holder_id'=>$info['apply_id']];
-                    $process = ["order_code" => $info['returnCode'], "order_id" => $info['id'], "order_status" => $param['status'], "order_type" => "SHD", "before_status" => $var, 'wait_id' => $info['person_id'], 'wait_name' => $info['person'], 'holder_id' => $info['person_id']];
+				   $process = ["order_code" => $info['returnCode'], "order_id" => $info['id'], "order_status" => $param['status'], "order_type" => "SHD", "before_status" => $var, 'wait_id' => $info['person_id'], 'wait_name' => $info['person'], 'holder_id' => $info['person_id']];
 
                     ProcessOrder::AddProcess($action_log_user, $process);
                     Db::commit();
@@ -1312,25 +1290,6 @@ class After extends Base
             ->where(["outCode" => $info["outCode"], "is_del" => 0])
             ->findOrEmpty();
         if (empty($out)) return error_show(1005, "未找到发货单数据");
-
-//        $is_th = isset($this->post['is_th'])&&$this->post['is_th']!=="" ? intval($this->post['is_th']):"";
-//        if($is_th===""){
-//            return error_show(1005,"参数is_th不能为空");
-//        }
-//        $company=isset($this->post['company'])&&$this->post['company']!=""?trim($this->post['company']):"";
-//        $post_code=isset($this->post['post_code'])&&$this->post['post_code']!=""?trim($this->post['post_code']):"";
-//        $post_fee=isset($this->post['post_fee'])&&$this->post['post_fee']!==""?floatval($this->post['post_fee']):"";
-//        if($is_th==1){
-//            if($company==""){
-//                return error_show(1005,"参数company不能为空");
-//            }
-//            if($post_code==""){
-//                return error_show(1005,"参数post_code不能为空");
-//            }
-//            if($post_fee===""){
-//                return error_show(1005,"参数post_fee不能为空");
-//            }
-//        }
         $sale = Db::name('sale')
             ->where(['orderCode' => $info['orderCode'], "is_del" => 0])
             ->findOrEmpty();
@@ -1406,9 +1365,7 @@ class After extends Base
                     //如果售后已完成,维护销售单、发货单和采购单
                     if ($info['status'] == 5) {
                         $sale['th_num'] += $info['error_num'];
-//                        if ($sale['th_num'] == $sale['send_num'] && $sale['wsend_num'] == 0)  $sale['status'] = 3;
                         if ($sale['th_num'] == $sale['good_num']) $sale['status'] = 3;
-
                         $sale['th_fee'] += round($info['error_num'] * $sale['sale_price'], 2);
                         $sale['updatetime'] = date("Y-m-d H:i:s");
                         Db::name("sale")->save($sale);
@@ -1419,14 +1376,6 @@ class After extends Base
                         $out['updatetime'] = date("Y-m-d H:i:s");
                         $upout = Db::name("order_out")->save($out);
                         if ($upout == false) throw new Exception('出库单数据更新失败');
-
-                        //发货工单,有可能会有多个,要按照顺序扣减发货数
-//                        Db::name('order_out_child')
-//                        ->data(['updatetime' => date("Y-m-d H:i:s")])
-//                            ->where(['is_del' => 0, 'outCode' => $info["outCode"]])
-//                            ->dec('num', $info['error_num'])
-//                            ->update();
-
                         $error_num = $info['error_num'];
                         $tmp_ooc = Db::name('order_out_child')
                             ->where(['is_del' => 0, 'outCode' => $info["outCode"]])
@@ -1452,8 +1401,8 @@ class After extends Base
 
                         }
 
-                        //如果供应商承担,维护采购单
-                        if ($info['return_tag'] == 1) {
+                        //如果供应商承担,维护采购单 todo:: 新逻辑 无论供应商是否同意退货采购单都要减退回
+//                        if ($info['return_tag'] == 1) {
                             $cgd_info = Db::name('order_num')
                                 ->alias('a')
                                 ->field('a.id,b.id cgd_id,b.good_price')
@@ -1469,305 +1418,37 @@ class After extends Base
                                     ->update();
                                if($up==false) throw new Exception('采购单更新失败');
                             }
-                        }
-						 $data = [
-                            "orderCode" => $info['orderCode'],
-                            "th_type" => 2,
-                            "th_num" => $info['error_num'],
-                            "th_fee" => round($info['error_num'] * $sale['sale_price'], 2),
-                            "thCode" => $info['returnCode'],
-                            "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" => $info['updatetime'],
-                            "status" => 1,
-                            "is_del" => 0
-                        ];
-                        $inse = Db::name("th_data")->insert($data);
-                        if ($inse == false) throw new Exception('退回单更新失败');
+//                        }
+//todo::供应商不同意退货 生成转库存数据
+					
                     }
-
+				
                     Db::commit();
+                    if($info["status"]==5){
+                    	 $data = [
+                            'orderCode' => $info['orderCode'],
+                            'th_type' => 2,
+                            'th_num' => $info['error_num'],
+                            'th_fee' => round($info['error_num'] * $sale['sale_price'], 2),
+                            'thCode' => $info['returnCode'],
+                            'spuCode' => $sale['good_code'],
+                            'good_name' => $sale['good_name'],
+                            'sale_price' => $sale['sale_price'],
+                            'cgd_price' => $cgd_info['good_price'],
+                            'cat_id' => $sale['cat_id'],
+                            'apply_id' => $info['apply_id'],
+                            'apply_name' => $info['apply_name'],
+                            'addtime' => $info['updatetime'],
+                            'status' => 1,
+                            'is_del' => 0
+                        ];
+                        event('thdata',$data);
+                    }
                     return json_show(0, '退货成功');
                 } else throw new Exception('修改售后申请单状态失败');
 
             } else throw new Exception('退货失败');
 
-            //售后退货单数据
-//                    $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){
-////
-////                        		$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,'采购单数据更新失败');
-////	                            }
-//                            }
-//
-//                            $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,'出库单数据更新失败');
-////                                    }
-//                            }
-//                            $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::commit();
-//            return json_show(0, '退货成功');
         } catch (Exception $e) {
             Db::rollback();
             return json_show(1004, $e->getMessage());

+ 93 - 72
app/admin/controller/Reorder.php

@@ -967,7 +967,7 @@ class Reorder extends Base
                         if ($stock == false) throw new Exception('商品库存更新失败');
                     }
                     else {
-                        if ($info['is_th'] == 1) {
+//                        if ($info['is_th'] == 1) {
                             $orderinfo['th_num'] += $info['num'];
                             if ($orderinfo['th_num'] == $orderinfo['send_num'] && $orderinfo['wsend_num'] == 0) {
                                 $orderinfo['status'] = 3;
@@ -1035,60 +1035,61 @@ class Reorder extends Base
                             if ($st_up == false) throw new Exception('可售商品入库失败');
 
 
-                        } else {
-                            $orderinfo['th_num'] += $info['num'];
-                            if ($orderinfo['th_num'] == $orderinfo['send_num'] && $orderinfo['wsend_num'] == 0) {
-                                $orderinfo['status'] = 3;
-                            }
-                            $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
-                            $orderinfo['updatetime'] = $date;
-                            $uap = Db::name("sale")->save($orderinfo);
-                            if ($uap == false) throw new Exception('销售单订单更新失败');
-
-                            $ordernum = Db::name("order_num")
-                                ->where(['orderCode' => $orderinfo['orderCode']])
-                                ->findOrEmpty();
-                            if (empty($ordernum)) throw new Exception('未找到关联采购单');
-
-                            $ordernum['send_num'] -= $info['num'];
-                            $ordernum['wsend_num'] = $info['num'];
-                            $orderup = Db::name("order_num")->save($ordernum);
-                            if ($orderup == false) throw new Exception('关联数据更新失败');
-
-                            $cgd = Db::name("purchease_order")
-                                ->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])
-                                ->findOrEmpty();
-                            if (empty($cgd)) throw new Exception('未找到采购单数据');
-
-                            $stock = Db::name("good_stock")
-                                ->where(['is_del' => 0, "spuCode" => $orderinfo['good_code'], 'wsm_code' => $cgd['wsm_code']])
-                                ->findOrEmpty();
-                            if (empty($stock)) throw new Exception('商品仓库未找到');
-
-                            if ($info['is_all'] == 1) {
-                                if ($stock['wait_in_stock'] < $info['num']) throw new Exception('商品可用库存不足退回数量');
-                                else $stock['wait_in_stock'] -= $info['num'];
-
-                            } else {
-
-                                if ($stock['usable_stock'] + $stock['wait_out_stock'] < $info['num']) throw new Exception('商品可用库存不足退回数量');
-                                else {
-
-                                    if ($stock['usable_stock'] > $info['num']) {
-                                        $stock['usable_stock'] -= $info['num'];
-                                    } else {
-                                        $stock['usable_stock'] = 0;
-                                        $stock['wait_out_stock'] -= $info['num'] - $stock['usable_stock'];
-                                    }
-                                    $stock['total_stock'] = $stock['usable_stock'] + $stock['wait_out_stock'];
-                                }
-                            }
-
-                            $stock['updatetime'] = $date;
-                            $st_up = Db::name("good_stock")->save($stock);
-                            if ($st_up == false) throw new Exception('可售商品入库失败');
-
-                        }
+//                        }
+//                        else {
+//                            $orderinfo['th_num'] += $info['num'];
+//                            if ($orderinfo['th_num'] == $orderinfo['send_num'] && $orderinfo['wsend_num'] == 0) {
+//                                $orderinfo['status'] = 3;
+//                            }
+//                            $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
+//                            $orderinfo['updatetime'] = $date;
+//                            $uap = Db::name("sale")->save($orderinfo);
+//                            if ($uap == false) throw new Exception('销售单订单更新失败');
+//
+//                            $ordernum = Db::name("order_num")
+//                                ->where(['orderCode' => $orderinfo['orderCode']])
+//                                ->findOrEmpty();
+//                            if (empty($ordernum)) throw new Exception('未找到关联采购单');
+//
+//                            $ordernum['send_num'] -= $info['num'];
+//                            $ordernum['wsend_num'] = $info['num'];
+//                            $orderup = Db::name("order_num")->save($ordernum);
+//                            if ($orderup == false) throw new Exception('关联数据更新失败');
+//
+//                            $cgd = Db::name("purchease_order")
+//                                ->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])
+//                                ->findOrEmpty();
+//                            if (empty($cgd)) throw new Exception('未找到采购单数据');
+//
+//                            $stock = Db::name("good_stock")
+//                                ->where(['is_del' => 0, "spuCode" => $orderinfo['good_code'], 'wsm_code' => $cgd['wsm_code']])
+//                                ->findOrEmpty();
+//                            if (empty($stock)) throw new Exception('商品仓库未找到');
+//
+//                            if ($info['is_all'] == 1) {
+//                                if ($stock['wait_in_stock'] < $info['num']) throw new Exception('商品可用库存不足退回数量');
+//                                else $stock['wait_in_stock'] -= $info['num'];
+//
+//                            } else {
+//
+//                                if ($stock['usable_stock'] + $stock['wait_out_stock'] < $info['num']) throw new Exception('商品可用库存不足退回数量');
+//                                else {
+//
+//                                    if ($stock['usable_stock'] > $info['num']) {
+//                                        $stock['usable_stock'] -= $info['num'];
+//                                    } else {
+//                                        $stock['usable_stock'] = 0;
+//                                        $stock['wait_out_stock'] -= $info['num'] - $stock['usable_stock'];
+//                                    }
+//                                    $stock['total_stock'] = $stock['usable_stock'] + $stock['wait_out_stock'];
+//                                }
+//                            }
+//
+//                            $stock['updatetime'] = $date;
+//                            $st_up = Db::name("good_stock")->save($stock);
+//                            if ($st_up == false) throw new Exception('可售商品入库失败');
+//
+//                        }
 
                     }
                     ActionLog::logAdd(['id' => $info['apply_id'], 'nickname' => $info['apply_name']], [
@@ -1144,27 +1145,47 @@ class Reorder extends Base
                         }
                     }
 
-                    $data = [
-                        "orderCode" => $info['orderCode'],
-                        "th_type" => 1,
-                        "th_num" => $info['num'],
-                        "th_fee" => round($info['num'] * $orderinfo['sale_price'], 2),
-                        "thCode" => $info['returnCode'],
-                        "spuCode" => $orderinfo['good_code'],
-                        "good_name" => $orderinfo['good_name'],
-                        "cat_id" => $orderinfo['cat_id'],
-                        "apply_id" => $info['apply_id'],
-                        "apply_name" => $info['apply_name'],
-                        "addtime" => $date,
-                        "status" => 1,
-                        "is_del" => 0
-                    ];
-                    $inse = Db::name("th_data")->insert($data);
-                    if ($inse == false) throw new Exception('退货单更新失败');
+//                    $data = [
+//                        "orderCode" => $info['orderCode'],
+//                        "th_type" => 1,
+//                        "th_num" => $info['num'],
+//                        "th_fee" => round($info['num'] * $orderinfo['sale_price'], 2),
+//                        "thCode" => $info['returnCode'],
+//                        "spuCode" => $orderinfo['good_code'],
+//                        "good_name" => $orderinfo['good_name'],
+//                        "cat_id" => $orderinfo['cat_id'],
+//                        "apply_id" => $info['apply_id'],
+//                        "apply_name" => $info['apply_name'],
+//                        "addtime" => $date,
+//                        "status" => 1,
+//                        "is_del" => 0
+//                    ];
+//                    $inse = Db::name("th_data")->insert($data);
+//                    if ($inse == false) throw new Exception('退货单更新失败');
 
                 }
             }
             Db::commit();
+             if($status==4){
+                    	  $data = [
+	                        'orderCode' => $info['orderCode'],
+	                        'th_type' => 1,
+	                        'th_num' => $info['num'],
+	                        'th_fee' => round($info['num'] * $orderinfo['sale_price'], 2),
+	                        'thCode' => $info['returnCode'],
+	                        'spuCode' => $orderinfo['good_code'],
+	                        'good_name' => $orderinfo['good_name'],
+	                        'cat_id' => $orderinfo['cat_id'],
+	                        'sale_price' => $orderinfo['sale_price'],
+                            'cgd_price' => $cgd['good_price'],
+	                        'apply_id' => $info['apply_id'],
+	                        'apply_name' => $info['apply_name'],
+	                        'addtime' => $date,
+	                        'status' => 1,
+	                        'is_del' => 0
+                            ];
+                        event('thdata',$data);
+                    }
             return json_show(0, "更新成功");
 
         } catch (Exception $e) {

+ 91 - 0
app/admin/listener/ThData.php

@@ -0,0 +1,91 @@
+<?php
+declare (strict_types = 1);
+
+namespace app\admin\listener;
+
+use app\admin\model\OrderReturn;use app\admin\model\SaleReturn;class ThData
+{
+    /**
+     * 事件监听处理
+     *
+     * @return mixed
+     */
+    public function handle($event)
+    {//
+        \app\admin\model\ThData::create($event);
+        $data=[];
+	       switch ($event['th_type']){
+	       	case 1:
+	       		$data=$this->beforeOrder($event);
+	       		break;
+	       	case 2:
+	       		$data=$this->afterOrder($event);
+	       		break;
+	       	case 3:
+	       		break;
+	       	case 4:
+	       		break;
+	       }
+	       if(empty($data)==false)\app\admin\model\ThStock::create($data);
+	       
+    }
+    
+    protected function afterOrder($event){
+    	$model =new OrderReturn();
+    	$info =$model->where(["returnCode"=>$event['thCode']])->findOrEmpty();
+    	if($info->isEmpty())return[];
+    	if($info->status==5 && $info->return_tag==2){
+    	    $data=[
+    	    	"thCode"=>$info->returnCode,
+    	    	"th_type"=>2,
+    	    	"order_type"=>$info->order_type,
+    	    	"supplierNo"=>$info->supplierNo,
+    	    	"supplierName"=>$info->supplierName,
+    	    	"companyNo"=>$info->companyNo,
+    	    	"companyName"=>$info->companyName,
+    	    	"customerNo"=>$info->customer_code,
+    	    	"customerName"=>$info->customer_name,
+    	    	"th_num"=>$info->error_num,
+    	    	"th_cgd_price"=>$event['sale_price'],
+    	    	"th_sale_price"=>$event['cgd_price'],
+    	    	"good_code"=>$info->good_code,
+    	    	"good_name"=>$info->good_code,
+    	    	"orderCode"=>$info->orderCode,
+    	    	"apply_id"=>$info->apply_id,
+    	    	"apply_name"=>$info->apply_name,
+    	    ];
+    	}
+    	return $data;
+    }
+    
+    protected function beforeOrder($event){
+    	$model =new SaleReturn();
+    	$info =$model->where(['returnCode'=>$event['thCode']])->findOrEmpty();
+    	if($info->isEmpty())return[];
+    	if($info->status==4 && $info->is_th==1){
+    	    $data=[
+    	    	'thCode'=>$info->returnCode,
+    	    	'th_type'=>1,
+    	    	'order_type'=>$info->order_type,
+    	    	'supplierNo'=>$info->supplierNo,
+    	    	'supplierName'=>$info->supplierName,
+    	    	'companyNo'=>$info->companyNo,
+    	    	'companyName'=>$info->companyName,
+    	    	'customerNo'=>$info->customer_code,
+    	    	'customerName'=>$info->customer_name,
+    	    	'th_num'=>$info->num,
+    	    	'th_cgd_price'=>$event['sale_price'],
+    	    	'th_sale_price'=>$event['cgd_price'],
+    	    	'good_code'=>$info->good_code,
+    	    	'good_name'=>$info->good_code,
+    	    	'orderCode'=>$info->orderCode,
+    	    	'apply_id'=>$info->apply_id,
+    	    	'apply_name'=>$info->apply_name,
+    	    ];
+    	}
+    	return $data;
+    }
+    
+    
+    
+}

+ 17 - 0
app/admin/listener/ThStock.php

@@ -0,0 +1,17 @@
+<?php
+declare (strict_types = 1);
+
+namespace app\admin\listener;
+
+class ThStock
+{
+    /**
+     * 事件监听处理
+     *
+     * @return mixed
+     */
+    public function handle($event)
+    {
+        //
+    }
+}

+ 14 - 0
app/admin/model/OrderReturn.php

@@ -0,0 +1,14 @@
+<?php
+declare (strict_types = 1);
+
+namespace app\admin\model;
+
+use think\Model;
+
+/**
+ * @mixin \think\Model
+ */
+class OrderReturn extends Model
+{
+    //
+}

+ 14 - 0
app/admin/model/SaleReturn.php

@@ -0,0 +1,14 @@
+<?php
+declare (strict_types = 1);
+
+namespace app\admin\model;
+
+use think\Model;
+
+/**
+ * @mixin \think\Model
+ */
+class SaleReturn extends Model
+{
+    //
+}

+ 14 - 0
app/admin/model/ThData.php

@@ -0,0 +1,14 @@
+<?php
+declare (strict_types = 1);
+
+namespace app\admin\model;
+
+use think\Model;
+
+/**
+ * @mixin \think\Model
+ */
+class ThData extends Model
+{
+    //
+}

+ 14 - 0
app/admin/model/ThStock.php

@@ -0,0 +1,14 @@
+<?php
+declare (strict_types = 1);
+
+namespace app\admin\model;
+
+use think\Model;
+
+/**
+ * @mixin \think\Model
+ */
+class ThStock extends Model
+{
+    //
+}