Przeglądaj źródła

预订单与退款

shish 4 lat temu
rodzic
commit
f53d10ffa1

+ 4 - 0
app-ghs/controllers/OrderController.php

@@ -38,6 +38,10 @@ class OrderController extends BaseController
         try {
             $order = OrderClass::getById($id, true);
             OrderClass::valid($order, $this->shopId);
+
+            $post['shopAdminId'] = $this->shopAdminId;
+            $post['shopAdminName'] = $this->shopAdmin ? $this->shopAdmin->name : '';
+
             OrderService::arrival($order, $post);
             $transaction->commit();
             util::complete();

+ 81 - 16
biz-ghs/order/classes/OrderClass.php

@@ -3,12 +3,16 @@
 namespace bizGhs\order\classes;
 
 use biz\shop\classes\ShopAdminClass;
+use biz\shop\classes\ShopCapitalClass;
 use biz\shop\classes\ShopClass;
 use biz\shop\classes\ShopExtClass;
 use biz\shop\models\Shop;
 use biz\sj\classes\SjClass;
 use biz\stat\classes\StatItemClass;
+use biz\stat\classes\StatKhCgClass;
 use biz\stat\classes\StatOrderCountClass;
+use biz\stat\classes\StatOutClass;
+use biz\stat\classes\StatRefundClass;
 use bizGhs\custom\models\Custom;
 use bizGhs\express\services\DadaExpressServices;
 use bizGhs\order\models\Order;
@@ -153,7 +157,7 @@ class OrderClass extends BaseClass
         $currentPrice = bcadd($itemPrice, $sendCost, 2);
 
         $shopId = $order->shopId ?? 0;
-        $shop = ShopClass::getById($shopId, true);
+        $shop = ShopClass::getLockById($shopId);
         $kiloFee = $shop->kiloFee ?? 0;
         $miniKilo = $shop->miniKilo ?? 0;
         $calcWeight = $weight > $miniKilo ? $weight : $miniKilo;
@@ -167,22 +171,83 @@ class OrderClass extends BaseClass
         $order->save();
         $bookPrice = $order->bookPrice ?? 0;
 
+        $mainId = $shop->mainId ?? 0;
+        $sjId = $shop->sjId ?? 0;
+        $customId = $order->customId ?? 0;
+        $relateOrderSn = $order->orderSn ?? '';
+        $shopAdminId = $data['shopAdminId'] ?? 0;
+        $shopAdminName = $data['shopAdminName'] ?? '';
+
         $refundId = 0;
-        if ($bookPrice == $currentPrice) {
-            return true;
-        } elseif ($bookPrice > $currentPrice) {
-//            $refundPrice = bcsub($bookPrice, $currentPrice, 2);
-//            //退款
-//            RefundOrderClass::add($data, true);
-//            //门店余额减少
-//            ShopClass::saleRefundReduceBalance($shop, $refund, $refundPrice);
-        } else {
-            //欠款
-            $currentDebt = bcsub($currentPrice, $bookPrice, 2);
-            $order->debtPrice = $currentDebt;
-            $order->remainDebtPrice = $currentDebt;
-            $order->debt = OrderClass::DEBT_YES;
-            $order->save();
+        if ($bookPrice != $currentPrice) {
+            if ($bookPrice > $currentPrice) {
+                $refundPrice = bcsub($bookPrice, $currentPrice, 2);
+
+                $order->refundPrice = $refundPrice;
+                $order->tkPrice = bcadd($order->tkPrice, $refundPrice, 2);
+                $order->save();
+
+                $orderSn = orderSn::getGhsRefundSn();
+                $book = 1;
+                $refundData = [
+                    'mainId' => $mainId,
+                    'shopId' => $shopId,
+                    'sjId' => $sjId,
+                    'customId' => $customId,
+                    'orderSn' => $orderSn,
+                    'relateOrderSn' => $relateOrderSn,
+                    'book' => $book,
+                    'status' => 0,
+                    'shopAdminId' => $shopAdminId,
+                    'shopAdminName' => $shopAdminName,
+                    'refundPrice' => $refundPrice,
+                    'refundType' => RefundOrderClass::REFUND_TYPE_MONEY,
+                ];
+                //退款
+                $refund = RefundOrderClass::add($refundData, true);
+
+                //余额减少
+                ShopClass::ghsBookOrderRefundReduceBalance($shop, $refund, $refundPrice);
+
+                //总支出增加
+                $currentTotalExpend = bcadd($shop->totalExpend, $refundPrice, 2);
+                $shop->totalExpend = $currentTotalExpend;
+                $totalRefund = bcadd($shop->totalRefund, $refundPrice, 2);
+                $shop->totalRefund = $totalRefund;
+                $shop->save();
+
+                //支出流水
+                $customName = $order->customName ?? '';
+                $thisType = dict::getDict('capitalType', 'ghsBookRefund', 'id');
+                $sjId = $order->sjId ?? 0;
+                $event = "预订单多付退给{$customName}";
+                $capitalData = [
+                    'capitalType' => $thisType,
+                    'io' => 0,
+                    'totalExpend' => $currentTotalExpend,
+                    'payWay' => 0,
+                    'amount' => $refundPrice,
+                    'sjId' => $sjId,
+                    'shopId' => $shopId,
+                    'event' => $event,
+                ];
+                ShopCapitalClass::addCapital($capitalData);
+                //出统计
+                StatOutClass::updateOrInsert($shop, $refundPrice);
+                //退款统计
+                $payWay = $order->payWay ?? 0;
+                StatRefundClass::replace($shop, $payWay, $refundPrice);
+                //客户采购减少统计
+                StatKhCgClass::refundReplace($refund);
+
+            } else {
+                //欠款
+                $currentDebt = bcsub($currentPrice, $bookPrice, 2);
+                $order->debtPrice = $currentDebt;
+                $order->remainDebtPrice = $currentDebt;
+                $order->debt = OrderClass::DEBT_YES;
+                $order->save();
+            }
         }
         return ['kiloFee' => $kiloFee, 'refundId' => $refundId, 'miniKilo' => $miniKilo];
     }

+ 2 - 0
biz-ghs/order/services/OrderService.php

@@ -89,10 +89,12 @@ class OrderService extends BaseService
         //零售到货
         $kiloFee = $respond['kiloFee'] ?? 0;
         $miniKilo = $respond['miniKilo'] ?? 0;
+        $saleRefundId = $respond['refundId'] ?? 0;
         $cgData = [
             'product' => $cgProduct,
             'kiloFee' => $kiloFee,
             'miniKilo' => $miniKilo,
+            'saleRefundId' => $saleRefundId,
         ];
         PurchaseClass::arrival($cg, $cgData);
     }

+ 106 - 5
biz-hd/purchase/classes/PurchaseClass.php

@@ -4,6 +4,7 @@ namespace bizHd\purchase\classes;
 
 use biz\ghs\classes\GhsClass;
 use biz\product\classes\XjClass;
+use biz\shop\classes\ShopCapitalClass;
 use biz\shop\classes\ShopClass;
 use biz\shop\classes\ShopCouponClass;
 use biz\sj\classes\SjClass;
@@ -14,7 +15,10 @@ use bizGhs\order\traits\OrderTrait;
 use bizGhs\product\classes\ProductClass;
 use bizGhs\stock\classes\StockRecordClass;
 use bizGhs\stock\services\StockRecordService;
+use bizHd\cg\classes\CgRefundClass;
 use bizHd\cg\classes\CgXjClass;
+use bizHd\stat\classes\StatIncomeClass;
+use bizHd\wx\classes\WxOpenClass;
 use common\components\dict;
 use common\components\imgUtil;
 use common\components\orderSn;
@@ -102,16 +106,113 @@ class PurchaseClass extends BaseClass
         $cg->realPrice = $currentPrice;
         $cg->stockChange = 1;
         $cg->save();
+
+        $mainId = $cg->mainId ?? 0;
+        $sjId = $cg->sjId ?? 0;
+        $shopId = $cg->shopId ?? 0;
+        $cgId = $cg->id ?? 0;
+        $saleRefundId = $data['saleRefundId'] ?? 0;
+
+        $shop = ShopClass::getLockById($shopId);
+        if (empty($shop)) {
+            util::fail('没有找到零售店的门店');
+        }
+
         $bookPrice = $cg->bookPrice ?? 0;
         if ($bookPrice == $currentPrice) {
             return true;
         } elseif ($bookPrice > $currentPrice) {
             //退款
-//            $refundPrice = bcsub($bookPrice, $currentPrice, 2);
-//            //退款
-//            RefundOrderClass::add($data, true);
-//            //门店余额减少
-//            ShopClass::saleRefundReduceBalance($shop, $refund, $refundPrice);
+            $refundPrice = bcsub($bookPrice, $currentPrice, 2);
+
+            $book = 1;
+            $orderSn = orderSn::getCgRefundSn();
+            $refundType = CgRefundClass::REFUND_TYPE_MONEY;
+            $refundData = [
+                'book' => $book,
+                'mainId' => $mainId,
+                'orderSn' => $orderSn,
+                'cgId' => $cgId,
+                'thirdRefundNo' => '',
+                'status' => 0,
+                'shopAdminId' => '',
+                'shopAdminName' => '',
+                'refundType' => $refundType,
+                'refundPrice' => $refundPrice,
+                'saleRefundId' => $saleRefundId,
+                'sjId' => $sjId,
+                'shopId' => $shopId,
+            ];
+            $cgRefund = CgRefundClass::add($refundData, true);
+
+            //门店收入增加
+            $currentTotalIncome = bcadd($shop->totalIncome, $refundPrice, 2);
+            $shop->totalIncome = $currentTotalIncome;
+            $shop->save();
+
+            $currentType = dict::getDict('capitalType', 'hdBookRefund', 'id');
+            $sjId = $cg->sjId ?? 0;
+            $shopId = $cg->shopId ?? 0;
+            $ghsName = $cg->ghsName ?? '';
+            $event = "预订单多付退款({$ghsName})";
+            $capitalData = [
+                'capitalType' => $currentType,
+                'io' => 1,
+                'totalIncome' => $currentTotalIncome,
+                'payWay' => 0,
+                'amount' => $refundPrice,
+                'sjId' => $sjId,
+                'shopId' => $shopId,
+                'event' => $event,
+            ];
+            ShopCapitalClass::addCapital($capitalData);
+            //每天每月收入
+            StatIncomeClass::updateOrInsert($shop, $refundPrice);
+
+            //微信收款的原路退回
+            ini_set('date.timezone', 'Asia/Shanghai');
+            $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
+            require_once($wx . '/lib/WxPay.Api.php');
+            require_once($wx . '/example/WxPay.Config.php');
+
+            $totalFee = $cg->realPrice * 100;
+            $refundFee = $refundPrice * 100;
+            $transaction_id = $cg->thirdNo ?? '';
+
+            $merchantExt = WxOpenClass::getWxInfo();
+            $mid = $merchantExt['wxPayMerchantId'] ?? '';
+            $appId = $merchantExt['miniAppId'] ?? '';
+            $key = $merchantExt['wxPayKey'] ?? '';
+
+            $input = new \WxPayRefund();
+            $input->SetTransaction_id($transaction_id);
+            $input->SetTotal_fee($totalFee);
+            $input->SetRefund_fee($refundFee);
+            $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-refund-callback/');
+
+            $config = new \WxPayConfig();
+            $config->SetAppId($appId);
+            $config->SetMerchantId($mid);
+            $config->SetKey($key);
+
+            $input->SetOut_refund_no($orderSn);
+            $input->SetOp_user_id($mid);
+            $return = \WxPayApi::refund($config, $input);
+
+            if (isset($return['return_code']) == false || $return['return_code'] != 'SUCCESS') {
+                $msg = $return['return_msg'] ?? '';
+                Yii::info("退款失败 cgId:{$cgId} msg:{$msg}");
+                util::fail('退款失败');
+            }
+            $wxRefundId = $return['refund_id'] ?? '';
+            $cgRefund->thirdRefundNo = $wxRefundId;
+            $cgRefund->save();
+
+
+            $cg->refundPrice = $refundPrice;
+            $cg->tkPrice = bcadd($cg->tkPrice, $refundPrice, 2);
+            $cg->save();
+
         } else {
             //欠款
             $currentDebt = bcsub($currentPrice, $bookPrice, 2);

+ 74 - 0
biz/pt/classes/PtAssetClass.php

@@ -277,6 +277,80 @@ class PtAssetClass extends BaseClass
         PtYeChangeClass::addChange($change, true);
     }
 
+    //供应商预订被多付了退款减少余额 ssh 20220324
+    public static function ghsBookOrderRefundReduceBalance($shop, $refund, $refundPrice)
+    {
+        $ptStyle = $shop->ptStyle;
+        $asset = null;
+        if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
+            $asset = self::getHdBalance();
+        } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
+            $asset = self::getGhsBalance();
+        } else {
+            util::fail('没有找到平台');
+        }
+        if (bccomp($refundPrice, $asset->amount, 2) == 1) {
+            util::fail('平台余额不足');
+        }
+        $currentAmount = bcsub($asset->amount, $refundPrice, 2);
+        $asset->amount = $currentAmount;
+        $asset->save();
+
+        $txAsset = null;
+        if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
+            $txAsset = self::getHdTxBalance();
+        } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
+            $txAsset = self::getGhsTxBalance();
+        } else {
+            util::fail('没有找到平台');
+        }
+        $tx = dict::getDict('yeTx', 'can');
+        $currentTxBalance = $txAsset->amount;
+        if (!empty($currentTxBalance)) {
+            $reduceTxBalance = $currentTxBalance > $refundPrice ? $refundPrice : $currentTxBalance;
+            $currentTxBalance = bcsub($currentTxBalance, $reduceTxBalance, 2);
+            $txAsset->amount = $currentTxBalance;
+            $txAsset->save();
+        }
+
+        $merchantName = $shop->merchantName ?? '';
+        if ($shop->default != 1) {
+            $shopName = $shop->shopName ?? '';
+            $merchantName = $merchantName . '-' . $shopName;
+        }
+
+        $event = "{$merchantName} 预订单被多付退还(退款单 {$refund->orderSn} 订单{$refund->relateOrderSn})";
+
+        $relateOrderSn = $refund->relateOrderSn ?? '';
+        if (!empty($relateOrderSn)) {
+            $relateOrder = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
+            $customName = $relateOrder->customName ?? '';
+            if (!empty($customName)) {
+                $event = "{$merchantName} 的预订单被{$customName}多付退还(退款单 {$refund->orderSn} 订单{$refund->relateOrderSn})";
+            }
+        }
+
+        $id = $refund->id;
+        $currentType = dict::getDict('capitalType', 'ghsBookRefund');
+        $change = [
+            'relateId' => $id,
+            'capitalType' => $currentType,
+            'amount' => $refundPrice,
+            'balance' => $currentAmount,
+            'txBalance' => $currentTxBalance,
+            'io' => 0,
+            'payWay' => 0,
+            'event' => $event,
+            'sjId' => $shop->sjId,
+            'shopId' => $shop->id,
+            'tx' => $tx,
+            'ptStyle' => $ptStyle,
+            'shopName' => $shop->shopName,
+            'sjName' => $shop->merchantName,
+        ];
+        PtYeChangeClass::addChange($change, true);
+    }
+
     public static function saleRefundReduceBalance($shop, $refund, $refundPrice)
     {
         $ptStyle = $shop->ptStyle;

+ 53 - 0
biz/shop/classes/ShopClass.php

@@ -459,6 +459,59 @@ class ShopClass extends BaseClass
         PtAssetClass::cgRefundAddBalance($shop, $refundPrice, $cgRefund, $currentType, $tx);
     }
 
+    //供应商预订被多付了退款减少余额 ssh 20220324
+    public static function ghsBookOrderRefundReduceBalance($shop, $refund, $refundPrice)
+    {
+        $currentBalance = bcsub($shop->balance, $refundPrice, 2);
+        if ($currentBalance < 0) {
+            util::fail('余额不足,请先充值');
+        }
+        $shop->balance = $currentBalance;
+        $currentTx = $shop->txBalance;
+        if ($currentTx > 0) {
+            if ($refundPrice > $currentTx) {
+                $currentTx = 0;
+            } else {
+                $currentTx = bcsub($currentTx, $refundPrice, 2);
+            }
+        }
+        $shop->txBalance = $currentTx;
+        $shop->save();
+
+        $currentType = dict::getDict('capitalType', 'ghsBookRefund');
+        $id = $refund->id;
+        $event = "预订单被多付退给客户(退款单 {$refund->orderSn} 订单{$refund->relateOrderSn})";
+
+        $relateOrderSn = $refund->relateOrderSn ?? '';
+        if (!empty($relateOrderSn)) {
+            $relateOrder = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
+            $customName = $relateOrder->customName ?? '';
+            if (!empty($customName)) {
+                $event = "预订单{$customName}多付退款(退款单 {$refund->orderSn} 订单{$refund->relateOrderSn})";
+            }
+        }
+        $tx = dict::getDict('yeTx', 'can');
+        $change = [
+            'relateId' => $id,
+            'capitalType' => $currentType,
+            'amount' => $refundPrice,
+            'balance' => $currentBalance,
+            'txBalance' => $currentTx,
+            'io' => 0,
+            'payWay' => 0,
+            'event' => $event,
+            'sjId' => $shop->sjId,
+            'shopId' => $shop->id,
+            'tx' => $tx,
+            'ptStyle' => $shop->ptStyle,
+        ];
+        ShopYeChangeClass::addChange($change, true);
+
+        //平台余额增加
+        PtAssetClass::ghsBookOrderRefundReduceBalance($shop, $refund, $refundPrice);
+
+    }
+
     //销售单退款减少余额 ssh 2021.2.21
     public static function saleRefundReduceBalance($shop, $refund, $refundPrice)
     {

+ 7 - 0
common/components/dict.php

@@ -211,6 +211,10 @@ class dict
             'brokerage' => ['id' => 46, 'name' => 'brokerage'],
             //供应商销售单对账
             'ghsXsClear' => ['id' => 20, 'name' => 'ghsXsClear'],
+            //供应商预订单被多付退款
+            'ghsBookRefund' => ['id' => 48, 'name' => 'ghsBookRefund'],
+            //花店预订退款
+            'hdBookRefund' => ['id' => 50, 'name' => 'hdBookRefund'],
         ],
         "capitalTypeList" => [//流水类型的对应链接,后台收支明细查看时跳转的链接
             0 => ['link' => '/capital/order-detail', 'name' => '网店', 'orderLink' => '/order/detail', 'id' => 0,],
@@ -231,6 +235,9 @@ class dict
             38 => ['link' => '', 'name' => '采购退款', 'orderLink' => '', 'id' => 38,],
             40 => ['link' => '', 'name' => '销售退款', 'orderLink' => '', 'id' => 40,],
             45 => ['link' => '', 'name' => '盘点', 'orderLink' => '', 'id' => 45,],
+            46 => ['link' => '', 'name' => '佣金', 'orderLink' => '', 'id' => 46,],
+            48 => ['link' => '', 'name' => '预订单多付退款', 'orderLink' => '', 'id' => 48,],
+            50 => ['link' => '', 'name' => '预订单多付退款', 'orderLink' => '', 'id' => 50,],
         ],
 
         //用户来源

+ 2 - 0
sql.txt

@@ -631,5 +631,7 @@ ALTER TABLE xhRefund ADD book TINYINT NOT NULL DEFAULT 0 COMMENT '0非预订退
 ALTER TABLE xhCgRefund ADD book TINYINT NOT NULL DEFAULT 0 COMMENT '0非预订退款,有花材详情 1预订退款,暂无详情页' AFTER `saleRefundId`;
 ALTER TABLE xhRefund MODIFY `status` TINYINT(2) UNSIGNED NOT NULL DEFAULT '0' COMMENT '0 处理中 1已完成';
 ALTER TABLE xhCgRefund MODIFY `status` TINYINT(2) UNSIGNED NOT NULL DEFAULT '0' COMMENT '0 处理中 1已完成';
+ALTER TABLE xhRefund ADD mainId INT NOT NULL DEFAULT 0 COMMENT '中央id' AFTER `id`;
+ALTER TABLE xhCgRefund ADD mainId INT NOT NULL DEFAULT 0 COMMENT '中央id' AFTER `id`;
 
 ====预订单 ssh 20220321