|
|
@@ -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
|