Browse Source

售后退货优化

wufeng 2 years ago
parent
commit
058643d218

+ 7 - 1
app/admin/controller/AfterChild.php

@@ -162,7 +162,7 @@ class AfterChild extends Base
             ->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');
+            ->column('a.id,a.return_wsm_code,a.outChildCode,b.wsm_code,a.status,a.return_num', 'a.id');
         if (empty($info)) return json_show(1004, '退货工单不存在');
 
         $val_child = Validate::rule([
@@ -193,6 +193,12 @@ class AfterChild extends Base
                         'updatetime' => $date,
                         'status' => 2
                     ]);
+
+                //发货工单数量减少
+                Db::name('order_out_child')
+                    ->where(['is_del' => 0, 'outChildCode' => $info[$value['id']]['outChildCode']])
+                    ->dec('num', $info[$value['id']]['return_num'])
+                    ->update(['updatetime' => $date]);
             }
 
             $total = array_sum(array_column($param['list'], 'can_sell_num'));

+ 140 - 133
app/admin/controller/Reorder.php

@@ -132,75 +132,82 @@ class Reorder extends Base
                 Db::execute("UPDATE `wsm_standing_book` SET `returnGoodCode`=CONCAT(IFNULL(`returnGoodCode`,''),',{$returnCode}'),`updatetime`='" . $date . "' WHERE `orderCode`='{$ordeCode}'");
 
                 //如果有退货地址,那么可能有发货单,可能有发货工单
-                if ($returnadr != "") {
-                    $inf = [];
-                    foreach ($returnadr as $val) {
-
-                        if ($val['return_num'] == 0) continue;//当退货数量为0时,跳过
-
-                        $temp = [];
-                        $addrinfo = Db::name("order_addr")->where(['id' => $val['id'], "orderCode" => $ordeCode])->findOrEmpty();
-                        if ($addrinfo == false) throw new Exception("地址信息未找到");
-
-                        $send = Db::name('order_out')->where([["addrid", "=", $val['id']]])->findOrEmpty();
-                        if ($send['status'] >= 2) throw new Exception("地址已发货");
-
-                        if ($order['is_stock'] == 1) {
-                            if ($addrinfo['receipt_quantity'] < $val['return_num']) throw new Exception("地址发货数量不足");
-
-                            $addrinfo['receipt_quantity'] -= $val['return_num'];
-                            $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0;
-                            $addrinfo['updatetime'] = $date;
-                            $addrup = Db::name("order_addr")->save($addrinfo);
-                            if ($addrup == false) throw new Exception("地址发货数量更新失败");
-
-                            $out = Db::name("order_out")->where(["addrid" => $val['id']])->findOrEmpty();
-                            if ($out == false) throw new Exception("地址发货单数据未找到");
-
-//                                if ($send!=false) {
-                            if ($out['status'] >= 2) throw new Exception("地址发货单已发货");
+//                if ($returnadr != "") {
+//                    $inf = [];
+//                    foreach ($returnadr as $val) {
+//
+//                        if ($val['return_num'] == 0) continue;//当退货数量为0时,跳过
+//
+//                        $temp = [];
+//                        $addrinfo = Db::name("order_addr")->where(['id' => $val['id'], "orderCode" => $ordeCode])->findOrEmpty();
+//                        if ($addrinfo == false) throw new Exception("地址信息未找到");
+//
+//                        $send = Db::name('order_out')->where([["addrid", "=", $val['id']]])->findOrEmpty();
+//                        if ($send['status'] >= 2) throw new Exception("地址已发货");
+//
+//                        if ($order['is_stock'] == 1) {
+//                            if ($addrinfo['receipt_quantity'] < $val['return_num']) throw new Exception("地址发货数量不足");
+//
+//                            $addrinfo['receipt_quantity'] -= $val['return_num'];
+//                            $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0;
+//                            $addrinfo['updatetime'] = $date;
+//                            $addrup = Db::name("order_addr")->save($addrinfo);
+//                            if ($addrup == false) throw new Exception("地址发货数量更新失败");
+//
+//                            $out = Db::name("order_out")->where(["addrid" => $val['id']])->findOrEmpty();
+//                            if ($out == false) throw new Exception("地址发货单数据未找到");
+//
+////                                if ($send!=false) {
+//                            if ($out['status'] >= 2) throw new Exception("地址发货单已发货");
+//
+//                            if ($out['send_num'] < $val['return_num']) throw new Exception("地址发货单发货数量不足");
+//
+//                            $out['send_num'] -= $val['return_num'];
+//                            $out['is_del'] = $out['send_num'] == 0 ? 1 : 0;
+//                            $out['updatetime'] = $date;
+//                            $outup = Db::name("order_out")->save($out);
+//                            if ($outup == false) throw new Exception("地址发货单更新失败");
+//
+//
+////                                    $ordersend = Db::name("order_send")->where(["outCode" => $out['outCode']])->find();
+////                                    if ($ordersend['send_num'] < $val['return_num']) {
+////                                        Db::rollback();
+////                                        return json_show(1004, "发货单发货数量不足");
+////                                    }
+////                                    $ordersend['send_num'] -= $val['return_num'];
+////                                    $ordersend['status'] = $ordersend['send_num'] <= 0 ? 0 : 1;
+////                                    $ordersend['updatetime'] = $date;
+////                                    $sendip = Db::name("order_send")->save($ordersend);
+////                                    if ($sendip == false) {
+////                                        Db::rollback();
+////                                        return json_show(1004, "发货单更新失败");
+////                                    }
+//
+////                                }
+//                        }
+//                        $temp['returnCode'] = $returnCode;
+//                        $temp['orderCode'] = $ordeCode;
+//                        $temp['outCode'] = isset($send['outCode']) ? $send['outCode'] : "";
+//                        $temp['addrid'] = $val['id'];
+//                        $temp['send_num'] = $addrinfo['receipt_quantity'];
+//                        $temp['return_num'] = $val['return_num'];
+//                        $temp['is_del'] = 0;
+//                        $temp['addtime'] = $date;
+//                        $temp['updatetime'] = $date;
+//                        $inf[] = $temp;
+//                    }
+//                    $inadd = Db::name("sale_returnaddr")->insertAll($inf);
+//                    if ($inadd == 0) throw new Exception("退货单新建失败");
+//                }
 
-                            if ($out['send_num'] < $val['return_num']) throw new Exception("地址发货单发货数量不足");
+                if ($in['status'] == 4 && $order['is_stock'] == 1) {
 
-                            $out['send_num'] -= $val['return_num'];
-                            $out['is_del'] = $out['send_num'] == 0 ? 1 : 0;
-                            $out['updatetime'] = $date;
-                            $outup = Db::name("order_out")->save($out);
-                            if ($outup == false) throw new Exception("地址发货单更新失败");
+                    //未发货数量要减去发货单上的发货数量
+                    $out_send_num = Db::name('order_out')
+                        ->where(['is_del' => 0, 'orderCode' => $ordeCode, 'status' => [0, 1]])
+                        ->sum('send_num');
 
-
-//                                    $ordersend = Db::name("order_send")->where(["outCode" => $out['outCode']])->find();
-//                                    if ($ordersend['send_num'] < $val['return_num']) {
-//                                        Db::rollback();
-//                                        return json_show(1004, "发货单发货数量不足");
-//                                    }
-//                                    $ordersend['send_num'] -= $val['return_num'];
-//                                    $ordersend['status'] = $ordersend['send_num'] <= 0 ? 0 : 1;
-//                                    $ordersend['updatetime'] = $date;
-//                                    $sendip = Db::name("order_send")->save($ordersend);
-//                                    if ($sendip == false) {
-//                                        Db::rollback();
-//                                        return json_show(1004, "发货单更新失败");
-//                                    }
-
-//                                }
-                        }
-                        $temp['returnCode'] = $returnCode;
-                        $temp['orderCode'] = $ordeCode;
-                        $temp['outCode'] = isset($send['outCode']) ? $send['outCode'] : "";
-                        $temp['addrid'] = $val['id'];
-                        $temp['send_num'] = $addrinfo['receipt_quantity'];
-                        $temp['return_num'] = $val['return_num'];
-                        $temp['is_del'] = 0;
-                        $temp['addtime'] = $date;
-                        $temp['updatetime'] = $date;
-                        $inf[] = $temp;
-                    }
-                    $inadd = Db::name("sale_returnaddr")->insertAll($inf);
-                    if ($inadd == 0) throw new Exception("退货单新建失败");
-                }
-
-                if ($order['is_stock'] == 1) {
+                    $order['wsend_num'] -= $out_send_num;
 
                     if ($order['wsend_num'] < $thnum) throw new Exception("销售单未发货数量不足退货");
 
@@ -282,72 +289,72 @@ class Reorder extends Base
 //                        }
 //                    }
 
-                    $saleinfo = Db::name("sale_info")
-                        ->where([["orderCode", "=", $ordeCode], ["num", ">", 0]])
-                        ->select()
-                        ->toArray();
-                    if (empty($saleinfo)) {
-                        $ordernum = Db::name("order_num")->where(['orderCode' => $ordeCode])->findOrEmpty();
-                        if (empty($ordernum)) throw new Exception('未找到关联采购单');
-
-                        $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->findOrEmpty();
-                        if (empty($cgd)) throw new Exception('未找到采购单数据');
-
-                        $bn = makeNo("BN");
-                        $stock = Db::name("good_stock")
-                            ->where(["spuCode" => $order['good_code'], 'wsm_code' => $cgd['wsm_code'], "is_del" => 0, "status" => 1])
-                            ->findOrEmpty();
-                        if ($stock == false) throw new Exception('商品库存数据未找到');
-
-                        $stock['usable_stock'] += $thnum;
-                        $stock['wait_out_stock'] -= $thnum;
-                        $stock['updatetime'] = $date;
-                        $st_up = Db::name("good_stock")->save($stock);
-                        if ($st_up == false) throw new Exception('可售商品入库失败');
-
-                        $yp = GoodStockInfo::AddBn($stock['id'], $bn, $thnum, $cgd['good_price']);
-                        if ($yp == false) throw new Exception('商品批次退货入库失败');
-
-                    } else {
-                        $tempnum = $thnum;
-                        foreach ($saleinfo as $va) {
-                            if ($tempnum == 0) break;
-                            $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'id' => $va['stockid']])
-                                ->findOrEmpty();
-                            if ($stock == false) throw new Exception('商品库存数据未找到');
-
-                            if ($va['num'] >= $tempnum) {
-                                $tnm = $tempnum;
-                                $va['num'] -= $tempnum;
-                                $va['th_num'] += $tempnum;
-                                $tempnum = 0;
-                            } else {
-                                $tnm = $va['num'];
-                                $tempnum -= $va['num'];
-                                $va['th_num'] += $va['num'];
-                                $va['num'] = 0;
-                            }
-                            $stock['usable_stock'] += $tnm;
-                            $stock['wait_out_stock'] -= $tnm;
-                            $stock['updatetime'] = $date;
-                            $st_up = Db::name("good_stock")->save($stock);
-                            if ($st_up == false) throw new Exception('可售商品入库失败');
-
-                            $ps = GoodStockInfo::AddBn($va['stockid'], $va['bnCode'], $tnm);
-                            if ($ps == false) throw new Exception('商品批次退货入库失败');
-
-                            $ret = GoodStockInfo::ReturnBn($returnCode, $va['id'], $tnm);
-                            if ($ret == false) throw new Exception('商品批次退货入库失败');
-
-                            $va['updatetime'] = $date;
-                            $sal = Db::name("sale_info")->save($va);
-                            if ($sal == false) throw new Exception('商品批次退货入库失败');
-
-                            $good_data[] = ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 1, 'stock' => $thnum, "stock_name" => "usable_stock"];
-                            $good_data[] = ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 2, 'stock' => $thnum, "stock_name" => "wait_out_stock"];
-                            GoodLog::LogAdd(['id' => $this->uid, 'nickname' => $this->uname], $good_data, 'XSTHD');
-                        }
-                    }
+//                    $saleinfo = Db::name("sale_info")
+//                        ->where([["orderCode", "=", $ordeCode], ["num", ">", 0]])
+//                        ->select()
+//                        ->toArray();
+//                    if (empty($saleinfo)) {
+//                        $ordernum = Db::name("order_num")->where(['orderCode' => $ordeCode])->findOrEmpty();
+//                        if (empty($ordernum)) throw new Exception('未找到关联采购单');
+//
+//                        $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->findOrEmpty();
+//                        if (empty($cgd)) throw new Exception('未找到采购单数据');
+//
+//                        $bn = makeNo("BN");
+//                        $stock = Db::name("good_stock")
+//                            ->where(["spuCode" => $order['good_code'], 'wsm_code' => $cgd['wsm_code'], "is_del" => 0, "status" => 1])
+//                            ->findOrEmpty();
+//                        if ($stock == false) throw new Exception('商品库存数据未找到');
+//
+//                        $stock['usable_stock'] += $thnum;
+//                        $stock['wait_out_stock'] -= $thnum;
+//                        $stock['updatetime'] = $date;
+//                        $st_up = Db::name("good_stock")->save($stock);
+//                        if ($st_up == false) throw new Exception('可售商品入库失败');
+//
+//                        $yp = GoodStockInfo::AddBn($stock['id'], $bn, $thnum, $cgd['good_price']);
+//                        if ($yp == false) throw new Exception('商品批次退货入库失败');
+//
+//                    } else {
+//                        $tempnum = $thnum;
+//                        foreach ($saleinfo as $va) {
+//                            if ($tempnum == 0) break;
+//                            $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'id' => $va['stockid']])
+//                                ->findOrEmpty();
+//                            if ($stock == false) throw new Exception('商品库存数据未找到');
+//
+//                            if ($va['num'] >= $tempnum) {
+//                                $tnm = $tempnum;
+//                                $va['num'] -= $tempnum;
+//                                $va['th_num'] += $tempnum;
+//                                $tempnum = 0;
+//                            } else {
+//                                $tnm = $va['num'];
+//                                $tempnum -= $va['num'];
+//                                $va['th_num'] += $va['num'];
+//                                $va['num'] = 0;
+//                            }
+//                            $stock['usable_stock'] += $tnm;
+//                            $stock['wait_out_stock'] -= $tnm;
+//                            $stock['updatetime'] = $date;
+//                            $st_up = Db::name("good_stock")->save($stock);
+//                            if ($st_up == false) throw new Exception('可售商品入库失败');
+//
+//                            $ps = GoodStockInfo::AddBn($va['stockid'], $va['bnCode'], $tnm);
+//                            if ($ps == false) throw new Exception('商品批次退货入库失败');
+//
+//                            $ret = GoodStockInfo::ReturnBn($returnCode, $va['id'], $tnm);
+//                            if ($ret == false) throw new Exception('商品批次退货入库失败');
+//
+//                            $va['updatetime'] = $date;
+//                            $sal = Db::name("sale_info")->save($va);
+//                            if ($sal == false) throw new Exception('商品批次退货入库失败');
+//
+//                            $good_data[] = ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 1, 'stock' => $thnum, "stock_name" => "usable_stock"];
+//                            $good_data[] = ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 2, 'stock' => $thnum, "stock_name" => "wait_out_stock"];
+//                            GoodLog::LogAdd(['id' => $this->uid, 'nickname' => $this->uname], $good_data, 'XSTHD');
+//                        }
+//                    }
 
 
 //                    $data=[
@@ -874,7 +881,7 @@ class Reorder extends Base
                                     ->findOrEmpty();
                                 if (empty($addrinfo)) throw new Exception("地址数据未找到");
 
-                                if ($addrinfo['receipt_quantity'] < $value['return_num']) throw new Exception( "地址发货数量不足");
+                                if ($addrinfo['receipt_quantity'] < $value['return_num']) throw new Exception("地址发货数量不足");
 
                                 $addrinfo['receipt_quantity'] -= $value['return_num'];
                                 $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0;

+ 29 - 7
app/admin/controller/ReorderChild.php

@@ -158,8 +158,11 @@ class ReorderChild extends Base
         if ($val->check($param)) return json_show(1004, $val->getError());
 
         $info = Db::name('sale_return_child')
-            ->field('id,num,status,returnCode')
-            ->where(['is_del' => 0, 'id' => $param['id']])
+            ->alias('a')
+            ->field('a.id,a.num,a.status,a.returnCode,a.outChildCode,a.return_num,a.return_wsm_code,a.orderCode,a.outCode,b.good_code spuCode,c.status order_out_status')
+            ->leftJoin('sale b', 'b.orderCode=a.orderCode')
+            ->leftJoin('order_out_child c', 'c.outChildCode=a.outChildCode AND c.is_del=0')
+            ->where(['a.is_del' => 0, 'a.id' => $param['id']])
             ->findOrEmpty();
         if (empty($info)) return json_show(1004, '该退货工单不存在');
         if ($info['status'] == 1) return json_show(1004, '该退货工单已收货');
@@ -171,7 +174,8 @@ class ReorderChild extends Base
 
             $date = date('Y-m-d H:i:s');
 
-            $rs = Db::name('sale_return_child')
+            //维护退货工单
+            Db::name('sale_return_child')
                 ->where(['is_del' => 0, 'id' => $param['id'], 'status' => 1])
                 ->update([
                     'status' => 2,
@@ -193,10 +197,28 @@ class ReorderChild extends Base
                     ->update(['status' => 4, 'updatetime' => $date]);
             }
 
-            if ($rs) {
-                Db::commit();
-                return json_show(0, '设置退货工单成功');
-            } else throw new Exception('设置退货工单失败');
+            //发货工单数量减少
+            Db::name('order_out_child')
+                ->where(['is_del' => 0, 'outChildCode' => $info['outChildCode']])
+                ->dec('num', $info['return_num'])
+                ->update(['updatetime' => $date]);
+
+            //维护bn
+            if ($info['order_out_status'] == 1) {
+                //待发货,说明此时有库存,有bn号
+                Db::name('child_bn')
+                    ->where(['orderCode' => $info['orderCode'], 'outCode' => $info['outCode'], 'childCode' => $info['outChildCode']])
+                    ->inc('num', $info['return_num'])
+                    ->update(['updatetime' => $date]);
+                Db::name('good_stock')
+                    ->where(['is_del' => 0, 'spuCode' => $info['spuCode'], 'wsm_code' => $info['return_wsm_code']])
+                    ->inc('usable_stock', $info['return_num'])
+                    ->dec('wait_out_stock', $info['return_num'])
+                    ->update(['updatetime' => $date]);
+            }
+
+            Db::commit();
+            return json_show(0, '设置退货工单成功');
 
         } catch (Exception $exception) {
             Db::rollback();