ソースを参照

批发店采购版块优化

shish 2 ヶ月 前
コミット
37f91fb0b1

+ 2 - 23
biz-ghs/cg/services/CgRefundService.php

@@ -11,7 +11,7 @@ use bizGhs\cg\classes\CgOrderItemSendClass;
 use bizGhs\cg\classes\CgRefundClass;
 use bizGhs\cg\classes\CgRefundItemClass;
 use bizGhs\ghs\classes\GhsClass;
-use bizGhs\ghs\classes\GhsDebtRecordClass;
+use bizGhs\order\classes\PurchaseOrderClass;
 use bizGhs\order\classes\PurchaseOrderItemClass;
 use bizGhs\product\classes\ProductClass;
 use common\components\dict;
@@ -237,28 +237,7 @@ class CgRefundService extends BaseService
         if (empty($ghs)) {
             util::fail('没有找到供货商');
         }
-        // 采购退款:净 balance 增加
-        \bizGhs\custom\classes\AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
-        $ghsNewBalance = bcadd($ghs->balance ?? '0.00', $refundPrice, 2);
-        $ghs->balance = $ghsNewBalance;
-        $ghs->save(false, ['balance']);
-
-        $capitalType = dict::getDict('capitalType', 'ghsCgOrderRefund', 'id');
-        $debtData = [
-            'ghsId' => $ghsId,
-            'relateId' => $refund->id,
-            'ptStyle' => 2,
-            'capitalType' => $capitalType,
-            'amount' => $refundPrice,
-            'balance' => $ghsNewBalance,
-            'io' => 1,
-            'staffId' => $refund->shopAdminId,
-            'staffName' => $refund->shopAdminName,
-            'event' => '采购单 ' . $order->orderSn . ' 申请售后 ' . $orderSn,
-            'sjId' => $sjId,
-            'shopId' => $shopId,
-        ];
-        \bizGhs\ghs\classes\GhsBalanceChangeClass::add($debtData, true);
+        PurchaseOrderClass::applyPurchaseRefundOnAccounts($ghs, $refundPrice, $refund, $order, $sjId, $shopId);
 
         return $refund;
     }

+ 2 - 42
biz-ghs/clear/classes/ClearClass.php

@@ -6,7 +6,6 @@ use biz\ghs\classes\GhsClass;
 use biz\shop\classes\ShopClass;
 use bizGhs\base\classes\BaseClass;
 use bizGhs\custom\classes\CustomClass;
-use bizGhs\ghs\classes\GhsDebtRecordClass;
 use bizGhs\clear\classes\OrderCgClearClass;
 use bizGhs\order\classes\OrderClass;
 use bizGhs\order\classes\PurchaseOrderClass;
@@ -179,30 +178,7 @@ class ClearClass extends BaseClass
         if (!$lock) {
             util::fail("系统繁忙,请重试");
         }
-        // 采购单结账:供货商净 balance 增加(原减 debtAmount)
-        \bizGhs\custom\classes\AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
-        $ghsNewBalance = bcadd($ghs->balance ?? '0.00', $order->prePrice, 2);
-        $ghs->balance = $ghsNewBalance;
-        $ghs->debtNum -= count($arr);
-        $ghs->debt = bccomp($ghsNewBalance, '0', 2) < 0 ? 2 : 1;
-        $ghs->save(false, ['balance', 'debtNum', 'debt']);
-
-        $capitalType = dict::getDict('capitalType', 'ghsCgOrderClear', 'id');
-        $debtData = [
-            'ghsId' => $ghsId,
-            'relateId' => $order->id,
-            'ptStyle' => 2,
-            'capitalType' => $capitalType,
-            'amount' => $order->prePrice,
-            'staffId' => $order->customShopAdminId,
-            'staffName' => $order->customShopAdminName,
-            'balance' => $ghsNewBalance,
-            'io' => 1,
-            'event' => '采购单结账 ' . $order->orderSn,
-            'sjId' => $order->sjId,
-            'shopId' => $order->shopId,
-        ];
-        \bizGhs\ghs\classes\GhsBalanceChangeClass::add($debtData, true);
+        PurchaseOrderClass::applyPurchaseClearOnBuyerGhs($ghs, $order->prePrice, $order, count($arr));
         util::unlock($ghsAddDebtKey);
 
         $shopId = $ghs->shopId;
@@ -228,23 +204,7 @@ class ClearClass extends BaseClass
             $purchase->save();
         }
 
-        //客户欠款变化
-        $customId = $ghs->customId;
-        $custom = CustomClass::getLockById($customId);
-        if (empty($custom)) {
-            util::fail('没有找到客户信息');
-        }
-        // 客户侧同步:净 balance 增加
-        \bizGhs\custom\classes\AccountMoneyClass::ensureCustomMoneyReady($custom, true);
-        $customNewBalance = bcadd($custom->balance ?? '0.00', $order->actPrice, 2);
-        $custom->balance = $customNewBalance;
-        $custom->debtNum -= count($arr);
-        $custom->isDebt = bccomp($customNewBalance, '0', 2) < 0 ? 1 : 0;
-        $custom->save(false, ['balance', 'debtNum', 'isDebt']);
-        if ($custom->debtNum == 0) {
-            $main->mayGatheringNum -= 1;
-            $main->save();
-        }
+        $custom = PurchaseOrderClass::applyPurchaseClearOnSupplierCustom($ghs, $order->actPrice, count($arr), $main);
 
         //应付供货商款减少
         $customShopId = $custom->shopId;

+ 166 - 71
biz-ghs/order/classes/PurchaseOrderClass.php

@@ -19,8 +19,9 @@ use bizGhs\book\classes\BookItemCustomClass;
 use bizGhs\book\classes\BookItemGhsClass;
 use bizGhs\cg\classes\CgOrderItemClass;
 use bizGhs\cg\classes\CgOrderItemSendClass;
+use bizGhs\custom\classes\AccountMoneyClass;
 use bizGhs\custom\classes\CustomClass;
-use bizGhs\ghs\classes\GhsDebtRecordClass;
+use bizGhs\ghs\classes\GhsBalanceChangeClass;
 use bizGhs\item\classes\CostChangeClass;
 use bizGhs\item\classes\ItemClassClass;
 use bizGhs\order\traits\OrderTrait;
@@ -70,6 +71,162 @@ class PurchaseOrderClass extends BaseClass
     const IN_TYPE_LATER = 0;
     const IN_TYPE_NOW = 1;
 
+    /**
+     * 采购挂账:买方 xhGhs 净 balance 减少,并记余额变动(合并后模型,不再写欠款字段)
+     */
+    public static function applyPurchaseDebtOnBuyerGhs($ghs, $amount, $purchase, $sjId, $shopId)
+    {
+        AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
+        $ghsNewBalance = bcsub($ghs->balance ?? '0.00', $amount, 2);
+        $ghs->debt = GhsClass::DEBT_YES;
+        $ghs->balance = $ghsNewBalance;
+        $ghs->debtNum += 1;
+        $ghs->expendAmount = bcadd($ghs->expendAmount ?? '0.00', $amount, 2);
+        $ghs->expendNum += 1;
+        $ghs->save(false, ['debt', 'balance', 'debtNum', 'expendAmount', 'expendNum']);
+
+        $orderSn = $purchase->orderSn ?? '';
+        $capitalType = dict::getDict('capitalType', 'ghsPurchase', 'id');
+        GhsBalanceChangeClass::add([
+            'ghsId' => $ghs->id ?? 0,
+            'relateId' => $purchase->id ?? 0,
+            'ptStyle' => 2,
+            'capitalType' => $capitalType,
+            'amount' => $amount,
+            'balance' => $ghsNewBalance,
+            'staffId' => $purchase->shopAdminId ?? 0,
+            'staffName' => $purchase->shopAdminName ?? '',
+            'io' => 0,
+            'event' => '新增采购 ' . $orderSn,
+            'sjId' => $sjId,
+            'shopId' => $shopId,
+        ], true);
+
+        return $ghs;
+    }
+
+    /**
+     * 采购挂账:供货方 custom 净 balance 同步减少(双边关系)
+     */
+    public static function applyPurchaseDebtOnSupplierCustom($ghs, $amount, $ghsMain = null)
+    {
+        $customId = $ghs->customId ?? 0;
+        $custom = CustomClass::getLockById($customId);
+        if (empty($custom)) {
+            util::fail('没有找到客户信息');
+        }
+        AccountMoneyClass::ensureCustomMoneyReady($custom, true);
+        $custom->isDebt = CustomClass::IS_DEBT_YES;
+        $custom->balance = bcsub($custom->balance ?? '0.00', $amount, 2);
+        if ($custom->debtNum == 0 && $ghsMain !== null) {
+            $ghsMain->mayGatheringNum += 1;
+        }
+        $custom->debtNum += 1;
+        $custom->buyNum += 1;
+        $custom->buyAmount = bcadd($custom->buyAmount ?? '0.00', $amount, 2);
+        $custom->save();
+
+        return $custom;
+    }
+
+    /**
+     * 采购售后退款:买方 xhGhs 净 balance 增加 + 流水;供货方 custom 同步
+     */
+    public static function applyPurchaseRefundOnAccounts($ghs, $amount, $refund, $order, $sjId, $shopId)
+    {
+        AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
+        $ghsNewBalance = bcadd($ghs->balance ?? '0.00', $amount, 2);
+        $ghs->balance = $ghsNewBalance;
+        $ghs->save(false, ['balance']);
+
+        $orderSn = $order->orderSn ?? '';
+        $refundSn = $refund->refundSn ?? '';
+        $capitalType = dict::getDict('capitalType', 'ghsCgOrderRefund', 'id');
+        GhsBalanceChangeClass::add([
+            'ghsId' => $ghs->id ?? 0,
+            'relateId' => $refund->id ?? 0,
+            'ptStyle' => 2,
+            'capitalType' => $capitalType,
+            'amount' => $amount,
+            'balance' => $ghsNewBalance,
+            'io' => 1,
+            'staffId' => $refund->shopAdminId ?? 0,
+            'staffName' => $refund->shopAdminName ?? '',
+            'event' => '采购单 ' . $orderSn . ' 申请售后 ' . $refundSn,
+            'sjId' => $sjId,
+            'shopId' => $shopId,
+        ], true);
+
+        $customId = $ghs->customId ?? 0;
+        if ($customId > 0) {
+            $custom = CustomClass::getLockById($customId);
+            if (!empty($custom)) {
+                AccountMoneyClass::ensureCustomMoneyReady($custom, true);
+                $customNewBalance = bcadd($custom->balance ?? '0.00', $amount, 2);
+                $custom->balance = $customNewBalance;
+                $custom->isDebt = bccomp($customNewBalance, '0', 2) < 0 ? CustomClass::IS_DEBT_YES : CustomClass::IS_DEBT_NO;
+                $custom->save(false, ['balance', 'isDebt']);
+            }
+        }
+
+        return $ghs;
+    }
+
+    /**
+     * 采购结账:买方 xhGhs 净 balance 增加 + 流水
+     */
+    public static function applyPurchaseClearOnBuyerGhs($ghs, $amount, $clear, $orderCount)
+    {
+        AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
+        $ghsNewBalance = bcadd($ghs->balance ?? '0.00', $amount, 2);
+        $ghs->balance = $ghsNewBalance;
+        $ghs->debtNum -= $orderCount;
+        $ghs->debt = bccomp($ghsNewBalance, '0', 2) < 0 ? GhsClass::DEBT_YES : GhsClass::DEBT_NO;
+        $ghs->save(false, ['balance', 'debtNum', 'debt']);
+
+        $capitalType = dict::getDict('capitalType', 'ghsCgOrderClear', 'id');
+        GhsBalanceChangeClass::add([
+            'ghsId' => $ghs->id ?? 0,
+            'relateId' => $clear->id ?? 0,
+            'ptStyle' => 2,
+            'capitalType' => $capitalType,
+            'amount' => $amount,
+            'balance' => $ghsNewBalance,
+            'staffId' => $clear->customShopAdminId ?? 0,
+            'staffName' => $clear->customShopAdminName ?? '',
+            'io' => 1,
+            'event' => '采购单结账 ' . ($clear->orderSn ?? ''),
+            'sjId' => $clear->sjId ?? 0,
+            'shopId' => $clear->shopId ?? 0,
+        ], true);
+
+        return $ghs;
+    }
+
+    /**
+     * 采购结账:供货方 custom 净 balance 同步增加
+     */
+    public static function applyPurchaseClearOnSupplierCustom($ghs, $amount, $orderCount, $ghsMain = null)
+    {
+        $customId = $ghs->customId ?? 0;
+        $custom = CustomClass::getLockById($customId);
+        if (empty($custom)) {
+            util::fail('没有找到客户信息');
+        }
+        AccountMoneyClass::ensureCustomMoneyReady($custom, true);
+        $customNewBalance = bcadd($custom->balance ?? '0.00', $amount, 2);
+        $custom->balance = $customNewBalance;
+        $custom->debtNum -= $orderCount;
+        $custom->isDebt = bccomp($customNewBalance, '0', 2) < 0 ? CustomClass::IS_DEBT_YES : CustomClass::IS_DEBT_NO;
+        $custom->save(false, ['balance', 'debtNum', 'isDebt']);
+        if ($custom->debtNum == 0 && $ghsMain !== null) {
+            $ghsMain->mayGatheringNum -= 1;
+            $ghsMain->save();
+        }
+
+        return $custom;
+    }
+
     public static function assign($cg, $shop, $params)
     {
         $bookSn = $cg->bookSn ?? 0;
@@ -708,36 +865,10 @@ class PurchaseOrderClass extends BaseClass
             util::fail('没有找到供货商');
         }
 
-        // 采购挂账:合并后 balance 减少,记 GhsBalanceChange 而非 GhsDebtRecord
-        \bizGhs\custom\classes\AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
-        $ghs->debt = GhsClass::DEBT_YES;
-        $ghsNewBalance = bcsub($ghs->balance ?? '0.00', $currentPrice, 2);
-        $ghs->balance = $ghsNewBalance;
-
-        $ghs->debtNum += 1;
-        $ghs->expendAmount = bcadd($ghs->expendAmount, $currentPrice, 2);
-        $ghs->expendNum += 1;
-        $ghs->save(false, ['debt', 'balance', 'debtNum', 'expendAmount', 'expendNum']);
-
-        $capitalType = dict::getDict('capitalType', 'ghsPurchase', 'id');
-        $debtData = [
-            'ghsId' => $ghsId,
-            'relateId' => $cg->id,
-            'ptStyle' => 2,
-            'capitalType' => $capitalType,
-            'amount' => $currentPrice,
-            'balance' => $ghsNewBalance,
-            'staffId' => $cg->shopAdminId,
-            'staffName' => $cg->shopAdminName,
-            'io' => 0,
-            'event' => '新增采购 ' . $orderSn,
-            'sjId' => $sjId,
-            'shopId' => $shopId,
-        ];
-        \bizGhs\ghs\classes\GhsBalanceChangeClass::add($debtData, true);
+        self::applyPurchaseDebtOnBuyerGhs($ghs, $currentPrice, $cg, $sjId, $shopId);
 
         //采购统计
-        $cgNum = $order->itemNum ?? 0;
+        $cgNum = $cg->itemNum ?? 0;
         StatCgClass::replace($main, $shop, $currentPrice, $cgNum);
         //采购按供货商统计
         StatCgGhsClass::ghsReplace($cg);
@@ -756,31 +887,14 @@ class PurchaseOrderClass extends BaseClass
         $currentMayGathering = bcadd($ghsMain->mayGathering, $currentPrice, 2);
         $ghsMain->mayGathering = $currentMayGathering;
 
-        //客户资产增加
-        $customId = $ghs->customId;
-        $custom = CustomClass::getLockById($customId);
-        if (empty($custom)) {
-            util::fail('没有找到客户信息');
-        }
-        \bizGhs\custom\classes\AccountMoneyClass::ensureCustomMoneyReady($custom, true);
-        $custom->isDebt = CustomClass::IS_DEBT_YES;
-        $custom->balance = bcsub($custom->balance ?? '0.00', $currentPrice, 2); // 客户待结增加=净 balance 减少
-        if ($custom->debtNum == 0) {
-            $ghsMain->mayGatheringNum += 1;
-        }
+        self::applyPurchaseDebtOnSupplierCustom($ghs, $currentPrice, $ghsMain);
         $ghsMain->save();
-        $custom->debtNum += 1;
-        $custom->buyNum += 1;
-        $custom->buyAmount = bcadd($custom->buyAmount, $currentPrice, 2);
-        $custom->save();
 
         //当天和当月支出统计
         StatOutClass::updateOrInsert($main, $shop, $currentPrice);
         //支出流水
         $payWay = dict::getDict('payWay', 'unknown');
         $capitalType = dict::getDict('capitalType', 'ghsPurchase', 'id');
-        $sjId = $order->sjId ?? 0;
-        $shopId = $order->shopId ?? 0;
         $event = '采购';
         $mainId = $shop->mainId ?? 0;
         $capitalData = [
@@ -1194,14 +1308,7 @@ class PurchaseOrderClass extends BaseClass
                 if (empty($ghs)) {
                     util::fail('没有找到供货商');
                 }
-                \bizGhs\custom\classes\AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
-                $ghs->debt = GhsClass::DEBT_YES;
-                $ghs->balance = bcsub($ghs->balance ?? '0.00', $currentPrice, 2);
-
-                $ghs->debtNum += 1;
-                $ghs->expendAmount = bcadd($ghs->expendAmount, $currentPrice, 2);
-                $ghs->expendNum += 1;
-                $ghs->save(false, ['debt', 'balance', 'debtNum', 'expendAmount', 'expendNum']);
+                self::applyPurchaseDebtOnBuyerGhs($ghs, $currentPrice, $order, $sjId, $shopId);
 
                 //采购统计
                 $cgNum = $order->itemNum ?? 0;
@@ -1223,23 +1330,11 @@ class PurchaseOrderClass extends BaseClass
                 $currentMayGathering = bcadd($ghsMain->mayGathering, $currentPrice, 2);
                 $ghsMain->mayGathering = $currentMayGathering;
 
-                //客户资产增加
-                $customId = $ghs->customId;
-                $custom = CustomClass::getLockById($customId);
-                if (empty($custom)) {
-                    util::fail('没有找到客户信息');
-                }
-                \bizGhs\custom\classes\AccountMoneyClass::ensureCustomMoneyReady($custom, true);
-                $custom->isDebt = CustomClass::IS_DEBT_YES;
-                $custom->balance = bcsub($custom->balance ?? '0.00', $currentPrice, 2);
-                if ($custom->debtNum == 0) {
-                    $ghsMain->mayGatheringNum += 1;
-                }
+                self::applyPurchaseDebtOnSupplierCustom($ghs, $currentPrice, $ghsMain);
                 $ghsMain->save();
-                $custom->debtNum += 1;
-                $custom->buyNum += 1;
-                $custom->buyAmount = bcadd($custom->buyAmount, $currentPrice, 2);
-                $custom->save();
+
+                $order->debt = self::DEBT_YES;
+                $order->save(false, ['debt']);
 
                 //当天和当月支出统计
                 StatOutClass::updateOrInsert($main, $shop, $currentPrice);