shish vor 2 Monaten
Ursprung
Commit
c6e613e787
2 geänderte Dateien mit 111 neuen und 17 gelöschten Zeilen
  1. 27 15
      biz-ghs/order/classes/OrderClearClass.php
  2. 84 2
      biz-hd/purchase/services/PurchaseService.php

+ 27 - 15
biz-ghs/order/classes/OrderClearClass.php

@@ -334,21 +334,7 @@ class OrderClearClass extends BaseClass
             }
             OrderClass::updateById($orderId, $orderUpdate);
 
-            if ($cgId > 0) {
-                $purchase = PurchaseClass::getById($cgId, true);
-                if (!empty($purchase)) {
-                    $pRemain = bcsub((string)($purchase->remainDebtPrice ?? 0), $mayClear, 2);
-                    $pUpdate = [
-                        'remainDebtPrice' => bccomp($pRemain, '0', 2) > 0 ? $pRemain : '0.00',
-                        'clearTime' => $clearTime,
-                    ];
-                    if (bccomp($pRemain, '0', 2) <= 0) {
-                        $pUpdate['debt'] = 2;
-                        $pUpdate['clearId'] = $clearId;
-                    }
-                    $purchase->save(false, array_keys($pUpdate));
-                }
-            }
+            self::syncHdPurchaseAfterOrderClear($orderId, $cgId, $mayClear, $clearId, $clearTime);
 
             $totalSettled = bcadd($totalSettled, $mayClear, 2);
             $os->amount = $mayClear;
@@ -401,4 +387,30 @@ class OrderClearClass extends BaseClass
         OrderCgClearClass::markDoneByClearId($clearId);
     }
 
+    /**
+     * 批发订单销账后同步花店采购单待结(充值 FIFO 等);cgId 缺失时按 saleId 回查。
+     */
+    protected static function syncHdPurchaseAfterOrderClear($orderId, $cgId, $mayClear, $clearId, $clearTime)
+    {
+        $purchase = null;
+        if (intval($cgId) > 0) {
+            $purchase = PurchaseClass::getById($cgId, true);
+        } elseif (intval($orderId) > 0) {
+            $purchase = PurchaseClass::getByCondition(['saleId' => $orderId], true);
+        }
+        if (empty($purchase)) {
+            return;
+        }
+        $pRemain = bcsub((string)($purchase->remainDebtPrice ?? 0), (string)$mayClear, 2);
+        $pUpdate = [
+            'remainDebtPrice' => bccomp($pRemain, '0', 2) > 0 ? $pRemain : '0.00',
+            'clearTime' => $clearTime,
+        ];
+        if (bccomp($pRemain, '0', 2) <= 0) {
+            $pUpdate['debt'] = PurchaseClass::DEBT_NO;
+            $pUpdate['clearId'] = $clearId;
+        }
+        $purchase->save(false, array_keys($pUpdate));
+    }
+
 }

+ 84 - 2
biz-hd/purchase/services/PurchaseService.php

@@ -186,8 +186,90 @@ class PurchaseService extends BaseService
     public static function getDebtList($where)
     {
         $list = PurchaseClass::getAllList('*', $where, 'addTime DESC');
-        $count = PurchaseClass::getCount($where);
-        return ['list' => $list, 'debtNum' => $count];
+        $synced = [];
+        $clearedSum = '0.00';
+        $remainSum = '0.00';
+        foreach ($list as $val) {
+            $row = is_array($val) ? $val : (array)$val;
+            $row = self::syncPurchaseDebtFromGhsOrder($row);
+            if (intval($row['debt'] ?? 0) != PurchaseClass::DEBT_YES) {
+                continue;
+            }
+            $row = self::enrichPurchaseDebtRow($row);
+            $synced[] = $row;
+            $clearedSum = bcadd($clearedSum, (string)($row['hasPayDebt'] ?? 0), 2);
+            $remainSum = bcadd($remainSum, (string)($row['remainDebtPrice'] ?? 0), 2);
+        }
+        return [
+            'list' => $synced,
+            'debtNum' => count($synced),
+            'clearedAmount' => floatval($clearedSum),
+            'remainDebtAmount' => floatval($remainSum),
+        ];
+    }
+
+    /** 列表展示:与 ghs order/debt-list 一致,按 debtPrice - remainDebtPrice 算已清 */
+    public static function enrichPurchaseDebtRow(array $row)
+    {
+        $debtPrice = bcadd((string)($row['debtPrice'] ?? '0'), '0', 2);
+        $remain = bcadd((string)($row['remainDebtPrice'] ?? '0'), '0', 2);
+        $hasPayDebt = '0.00';
+        if (bccomp($debtPrice, $remain, 2) > 0) {
+            $hasPayDebt = bcsub($debtPrice, $remain, 2);
+        }
+        $row['hasPayDebt'] = floatval($hasPayDebt);
+        return $row;
+    }
+
+    /**
+     * 充值销账后批发订单已更新,采购单可能滞后:按 saleId 对齐 remainDebtPrice / debt。
+     */
+    public static function syncPurchaseDebtFromGhsOrder(array $row)
+    {
+        $saleId = intval($row['saleId'] ?? 0);
+        if ($saleId <= 0) {
+            return $row;
+        }
+        $order = OrderClass::getById($saleId, true);
+        if (empty($order)) {
+            return $row;
+        }
+        $purchaseId = intval($row['id'] ?? 0);
+        if ($purchaseId <= 0) {
+            return $row;
+        }
+        $purchase = PurchaseClass::getById($purchaseId, true);
+        if (empty($purchase)) {
+            return $row;
+        }
+
+        $orderRemain = bcadd((string)($order->remainDebtPrice ?? '0'), '0', 2);
+        $orderDebt = bcadd((string)($order->debtPrice ?? $order->orderPrice ?? '0'), '0', 2);
+        $needSave = false;
+
+        if (bccomp((string)($purchase->remainDebtPrice ?? '0'), $orderRemain, 2) !== 0) {
+            $purchase->remainDebtPrice = bccomp($orderRemain, '0', 2) > 0 ? $orderRemain : '0.00';
+            $needSave = true;
+        }
+        if (intval($order->debt ?? 0) === 0 && intval($purchase->debt) === PurchaseClass::DEBT_YES) {
+            $purchase->debt = PurchaseClass::DEBT_NO;
+            if (!empty($order->clearId)) {
+                $purchase->clearId = $order->clearId;
+            }
+            $needSave = true;
+        }
+        if (bccomp((string)($purchase->debtPrice ?? '0'), '0', 2) <= 0 && bccomp($orderDebt, '0', 2) > 0) {
+            $purchase->debtPrice = $orderDebt;
+            $needSave = true;
+        }
+        if ($needSave) {
+            $purchase->save(false);
+        }
+
+        $row['remainDebtPrice'] = $purchase->remainDebtPrice;
+        $row['debtPrice'] = $purchase->debtPrice;
+        $row['debt'] = $purchase->debt;
+        return $row;
     }
 
     //采购单失效 ssh 2021.5.14