shish 2 сар өмнө
parent
commit
ab889edbfe

+ 79 - 3
biz-ghs/clear/classes/OrderCgClearClass.php

@@ -7,6 +7,7 @@ use bizGhs\order\classes\OrderClass;
 use bizHd\purchase\classes\PurchaseClass;
 use bizHd\purchase\classes\PurchaseClearClass;
 use common\components\dict;
+use Yii;
 
 class OrderCgClearClass extends BaseClass
 {
@@ -40,7 +41,7 @@ class OrderCgClearClass extends BaseClass
         foreach ($orderList as $order) {
             $orderId = is_array($order) ? ($order['id'] ?? 0) : ($order->id ?? 0);
             $orderSn = is_array($order) ? ($order['orderSn'] ?? '') : ($order->orderSn ?? '');
-            $amount = is_array($order) ? ($order['remainDebtPrice'] ?? 0) : ($order->remainDebtPrice ?? 0);
+            $amount = self::resolveDebtAmount($order);
             $cgId = intval(is_array($order) ? ($order['purchaseId'] ?? 0) : ($order->purchaseId ?? 0));
             $cgSn = '';
             if ($cgId > 0 && isset($cgMap[$cgId])) {
@@ -80,7 +81,7 @@ class OrderCgClearClass extends BaseClass
         foreach ($purchaseList as $purchase) {
             $cgId = is_array($purchase) ? ($purchase['id'] ?? 0) : ($purchase->id ?? 0);
             $cgSn = is_array($purchase) ? ($purchase['orderSn'] ?? '') : ($purchase->orderSn ?? '');
-            $amount = is_array($purchase) ? ($purchase['remainDebtPrice'] ?? 0) : ($purchase->remainDebtPrice ?? 0);
+            $amount = self::resolveDebtAmount($purchase);
             $orderId = intval(is_array($purchase) ? ($purchase['saleId'] ?? 0) : ($purchase->saleId ?? 0));
             $orderSn = '';
             if ($orderId > 0 && isset($orderMap[$orderId])) {
@@ -115,7 +116,9 @@ class OrderCgClearClass extends BaseClass
                 'orderId' => $orderId,
                 'cgId' => $cgId,
             ], true);
+            $newAmount = self::normalizeAmount($pair['amount'] ?? 0);
             if (!empty($exists)) {
+                self::patchAmountIfEmpty($exists, $newAmount);
                 continue;
             }
             self::add([
@@ -125,7 +128,7 @@ class OrderCgClearClass extends BaseClass
                 'orderSn' => $pair['orderSn'] ?? '',
                 'cgId' => $cgId,
                 'cgSn' => $pair['cgSn'] ?? '',
-                'amount' => $pair['amount'] ?? 0,
+                'amount' => $newAmount,
                 'status' => $status,
             ]);
         }
@@ -285,6 +288,79 @@ class OrderCgClearClass extends BaseClass
         return array_values(array_unique(array_map('intval', array_column($list, 'clearId'))));
     }
 
+    /**
+     * 关系行已存在但 amount 为空/0 的 clearId(用于回填补金额)
+     * @param int[] $clearIds
+     * @return int[]
+     */
+    public static function getClearIdsWithEmptyAmount(array $clearIds)
+    {
+        $clearIds = array_values(array_unique(array_filter(array_map('intval', $clearIds))));
+        if (empty($clearIds)) {
+            return [];
+        }
+        $table = self::$baseFile::tableName();
+        $rows = Yii::$app->db->createCommand(
+            'SELECT DISTINCT clearId FROM ' . $table
+            . ' WHERE clearId IN (' . implode(',', $clearIds) . ')'
+            . ' AND (amount IS NULL OR amount = 0 OR amount = \'0\' OR amount = \'0.00\')'
+        )->queryColumn();
+        if (empty($rows)) {
+            return [];
+        }
+        return array_values(array_unique(array_map('intval', $rows)));
+    }
+
+    /**
+     * 结账时优先 remainDebtPrice;历史已结清单 remainDebtPrice=0 时回退 debtPrice
+     */
+    protected static function resolveDebtAmount($entity)
+    {
+        $remain = self::readField($entity, 'remainDebtPrice');
+        if (bccomp(self::normalizeAmount($remain), '0', 2) > 0) {
+            return self::normalizeAmount($remain);
+        }
+        $debt = self::readField($entity, 'debtPrice');
+        if (bccomp(self::normalizeAmount($debt), '0', 2) > 0) {
+            return self::normalizeAmount($debt);
+        }
+        $act = self::readField($entity, 'actPrice');
+        if (bccomp(self::normalizeAmount($act), '0', 2) > 0) {
+            return self::normalizeAmount($act);
+        }
+        return self::normalizeAmount(self::readField($entity, 'orderPrice'));
+    }
+
+    protected static function readField($entity, $key)
+    {
+        return is_array($entity) ? ($entity[$key] ?? 0) : ($entity->$key ?? 0);
+    }
+
+    protected static function normalizeAmount($amount)
+    {
+        if ($amount === '' || $amount === null) {
+            return '0.00';
+        }
+        return bcadd((string)$amount, '0', 2);
+    }
+
+    protected static function patchAmountIfEmpty($row, $amount)
+    {
+        $amount = self::normalizeAmount($amount);
+        if (bccomp($amount, '0', 2) <= 0) {
+            return false;
+        }
+        $current = self::normalizeAmount(is_object($row) ? ($row->amount ?? 0) : ($row['amount'] ?? 0));
+        if (bccomp($current, '0', 2) > 0) {
+            return false;
+        }
+        if (is_object($row)) {
+            $row->amount = $amount;
+            return $row->save(false);
+        }
+        return false;
+    }
+
     protected static function parseIds($idStr)
     {
         if ($idStr === '' || $idStr === null) {

+ 52 - 3
biz-hd/purchase/classes/OrderCgClearClass.php

@@ -39,7 +39,7 @@ class OrderCgClearClass extends BaseClass
         foreach ($orderList as $order) {
             $orderId = is_array($order) ? ($order['id'] ?? 0) : ($order->id ?? 0);
             $orderSn = is_array($order) ? ($order['orderSn'] ?? '') : ($order->orderSn ?? '');
-            $amount = is_array($order) ? ($order['remainDebtPrice'] ?? 0) : ($order->remainDebtPrice ?? 0);
+            $amount = self::resolveDebtAmount($order);
             $cgId = intval(is_array($order) ? ($order['purchaseId'] ?? 0) : ($order->purchaseId ?? 0));
             $cgSn = '';
             if ($cgId > 0 && isset($cgMap[$cgId])) {
@@ -79,7 +79,7 @@ class OrderCgClearClass extends BaseClass
         foreach ($purchaseList as $purchase) {
             $cgId = is_array($purchase) ? ($purchase['id'] ?? 0) : ($purchase->id ?? 0);
             $cgSn = is_array($purchase) ? ($purchase['orderSn'] ?? '') : ($purchase->orderSn ?? '');
-            $amount = is_array($purchase) ? ($purchase['remainDebtPrice'] ?? 0) : ($purchase->remainDebtPrice ?? 0);
+            $amount = self::resolveDebtAmount($purchase);
             $orderId = intval(is_array($purchase) ? ($purchase['saleId'] ?? 0) : ($purchase->saleId ?? 0));
             $orderSn = '';
             if ($orderId > 0 && isset($orderMap[$orderId])) {
@@ -114,7 +114,9 @@ class OrderCgClearClass extends BaseClass
                 'orderId' => $orderId,
                 'cgId' => $cgId,
             ], true);
+            $newAmount = self::normalizeAmount($pair['amount'] ?? 0);
             if (!empty($exists)) {
+                self::patchAmountIfEmpty($exists, $newAmount);
                 continue;
             }
             self::add([
@@ -124,7 +126,7 @@ class OrderCgClearClass extends BaseClass
                 'orderSn' => $pair['orderSn'] ?? '',
                 'cgId' => $cgId,
                 'cgSn' => $pair['cgSn'] ?? '',
-                'amount' => $pair['amount'] ?? 0,
+                'amount' => $newAmount,
                 'status' => $status,
             ]);
         }
@@ -221,6 +223,53 @@ class OrderCgClearClass extends BaseClass
         return 0;
     }
 
+    protected static function resolveDebtAmount($entity)
+    {
+        $remain = self::readField($entity, 'remainDebtPrice');
+        if (bccomp(self::normalizeAmount($remain), '0', 2) > 0) {
+            return self::normalizeAmount($remain);
+        }
+        $debt = self::readField($entity, 'debtPrice');
+        if (bccomp(self::normalizeAmount($debt), '0', 2) > 0) {
+            return self::normalizeAmount($debt);
+        }
+        $act = self::readField($entity, 'actPrice');
+        if (bccomp(self::normalizeAmount($act), '0', 2) > 0) {
+            return self::normalizeAmount($act);
+        }
+        return self::normalizeAmount(self::readField($entity, 'orderPrice'));
+    }
+
+    protected static function readField($entity, $key)
+    {
+        return is_array($entity) ? ($entity[$key] ?? 0) : ($entity->$key ?? 0);
+    }
+
+    protected static function normalizeAmount($amount)
+    {
+        if ($amount === '' || $amount === null) {
+            return '0.00';
+        }
+        return bcadd((string)$amount, '0', 2);
+    }
+
+    protected static function patchAmountIfEmpty($row, $amount)
+    {
+        $amount = self::normalizeAmount($amount);
+        if (bccomp($amount, '0', 2) <= 0) {
+            return false;
+        }
+        $current = self::normalizeAmount(is_object($row) ? ($row->amount ?? 0) : ($row['amount'] ?? 0));
+        if (bccomp($current, '0', 2) > 0) {
+            return false;
+        }
+        if (is_object($row)) {
+            $row->amount = $amount;
+            return $row->save(false);
+        }
+        return false;
+    }
+
     protected static function parseIds($idStr)
     {
         if ($idStr === '' || $idStr === null) {

+ 3 - 1
console/controllers/ClearController.php

@@ -147,7 +147,9 @@ class ClearController extends Controller
             $batchMinId = null;
             $clearIds = array_map('intval', array_column($list, 'id'));
             $existingClearIds = OrderCgClearClass::getExistingClearIds($clearIds);
+            $emptyAmountClearIds = OrderCgClearClass::getClearIdsWithEmptyAmount($clearIds);
             $existingMap = array_fill_keys($existingClearIds, true);
+            $emptyAmountMap = array_fill_keys($emptyAmountClearIds, true);
 
             foreach ($list as $clear) {
                 $total++;
@@ -161,7 +163,7 @@ class ClearController extends Controller
                     $skipped++;
                     continue;
                 }
-                if (isset($existingMap[$currentClearId])) {
+                if (isset($existingMap[$currentClearId]) && !isset($emptyAmountMap[$currentClearId])) {
                     $skipped++;
                     continue;
                 }