From 6b7f9e8971de4d953837f8ffdabf3bcf86ba5b87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E7=90=A6?= Date: Tue, 2 Jun 2026 08:15:42 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=88=86=E8=B4=A6=E8=B5=84=E9=87=91?= =?UTF-8?q?=E5=8F=98=E5=8A=A8=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/forms/LedgerForm.php | 113 ++++++++++++++---- .../newDb/AccountAbleChangeLogModel.php | 11 ++ .../services/AccountAbleChangeLogService.php | 101 ++++++++++++++++ .../controllers/EplpayCallbackController.php | 25 +++- 4 files changed, 227 insertions(+), 23 deletions(-) create mode 100644 common/models/newDb/AccountAbleChangeLogModel.php create mode 100644 common/services/AccountAbleChangeLogService.php diff --git a/common/forms/LedgerForm.php b/common/forms/LedgerForm.php index 79f4ec2..1c0c503 100644 --- a/common/forms/LedgerForm.php +++ b/common/forms/LedgerForm.php @@ -16,6 +16,7 @@ use common\models\oldDb\Reconciliation; use common\models\oldDb\Register; use common\models\oldDb\RegisterLog; use common\models\oldDb\Store; +use common\services\AccountAbleChangeLogService; use yii\db\Exception; @@ -694,7 +695,12 @@ class LedgerForm extends BaseModel $log->saveOrFail(); //门店可提现及累计金额更新 - CashAccount::updateAllCounters(['able_cash' => $storeTotal, 'total_cash' => $storeTotal],['user_type' => 1, 'user_id' => $store->id]); //门店 + $this->applyCashAccountDeltaWithLog((int)$store->id, 1, $storeTotal, 'settle', [ + 'order_id' => (int)$this->order_id, + 'order_type' => 2, + 'order_no' => (string)($Register->order_no ?? ''), + 'remark' => '挂号订单分账结算-门店', + ]); break; case 2://已取消 @@ -720,17 +726,17 @@ class LedgerForm extends BaseModel $log->saveOrFail(); //可提现及累计金额更新 - CashAccount::updateAllCounters([ - 'able_cash' => $v['amount'] * -1, - 'total_cash' => $v['amount'] * -1], + $this->applyCashAccountDeltaWithLog( + (int)$v['user_id'], + (int)$v['user_type'], + (float)$v['amount'] * -1, + 'refund', [ - 'user_type' => $v['user_type'], - 'user_id' => $v['user_id' - ], - 'order_type' => $v['order_type'], - 'fee_type' => $v['fee_type' - ] - ]); + 'order_id' => (int)$v['order_id'], + 'order_type' => (int)$v['order_type'], + 'remark' => '订单退款扣减账户余额', + ] + ); } } else { // 未结算 Ledger::updateAll(['status' => 2, 'updated_at' => $time], ['order_id' => $this->order_id, 'order_type' => 2]); @@ -902,8 +908,13 @@ class LedgerForm extends BaseModel $money1 = bcadd($platformDrugTotal, $platformProcessTotal, 2); $money2 = bcadd($platformDeliveryTotal, $platformDecoctTotal, 2); $platformTotal = round(bcadd($money1, $money2, 2), 2); //该订单平台分佣总金额 - //可提现金额表更新 - CashAccount::updateAllCounters(['able_cash' => $platformTotal, 'total_cash' => $platformTotal],['user_type' => 2]); //平台 + //门店可提现及累计金额更新 + $this->applyCashAccountDeltaWithLog(0, 2, $platformTotal, 'settle', [ + 'order_id' => (int)$this->order_id, + 'order_type' => 1, + 'order_no' => (string)($productOrder->order_no ?? ''), + 'remark' => '产品订单分账结算-平台', + ]); /******************************************** 门店 ********************************************** */ @@ -942,7 +953,12 @@ class LedgerForm extends BaseModel } //门店可提现及累计金额更新 - CashAccount::updateAllCounters(['able_cash' => $storeTotal, 'total_cash' => $storeTotal],['user_type' => 1, 'user_id' => $store->id]); //门店 + $this->applyCashAccountDeltaWithLog((int)$store->id, 1, $storeTotal, 'settle', [ + 'order_id' => (int)$this->order_id, + 'order_type' => 1, + 'order_no' => (string)($productOrder->order_no ?? ''), + 'remark' => '产品订单分账结算-门店', + ]); break; case 2://已取消 @@ -971,13 +987,16 @@ class LedgerForm extends BaseModel $log->saveOrFail(); //供应商冻结金额及累计金额更新 - AccountBalanceModel::updateAllCounters([ -// 'total' => bcmul($v['amount'], -1, 6), - 'balance' => bcmul($v['amount'], -1, 6), - ], [ - 'user_id' => $v['user_id'], - 'type' => 3 - ]); + $this->applySupplierBalanceDeltaWithLog( + (int)$v['user_id'], + (float)bcmul($v['amount'], -1, 6), + 'refund', + [ + 'order_id' => (int)$v['order_id'], + 'order_type' => (int)$v['order_type'], + 'remark' => '订单退款扣减供应商余额', + ] + ); } else { $log = clone $model; $log->order_id = $v['order_id']; @@ -993,7 +1012,17 @@ class LedgerForm extends BaseModel $log->saveOrFail(); //可提现及累计金额更新 - CashAccount::updateAllCounters(['able_cash' => $v['amount'] * -1, 'total_cash' => $v['amount'] * -1],['user_type' => $v['user_type'], 'user_id' => $v['user_id']]); + $this->applyCashAccountDeltaWithLog( + (int)$v['user_id'], + (int)$v['user_type'], + (float)$v['amount'] * -1, + 'refund', + [ + 'order_id' => (int)$v['order_id'], + 'order_type' => (int)$v['order_type'], + 'remark' => '产品订单退款扣减账户余额', + ] + ); } } } else { // 未结算 @@ -1015,4 +1044,44 @@ class LedgerForm extends BaseModel } } + private function applyCashAccountDeltaWithLog(int $userId, int $userType, $delta, string $sourceType, array $extra = []): void + { + $account = CashAccount::findOne(['user_type' => $userType, 'user_id' => $userId]); + $before = $account ? $account->able_cash : 0; + CashAccount::updateAllCounters( + ['able_cash' => $delta, 'total_cash' => $delta], + ['user_type' => $userType, 'user_id' => $userId] + ); + $account = CashAccount::findOne(['user_type' => $userType, 'user_id' => $userId]); + $after = $account ? $account->able_cash : bcadd((string)$before, (string)$delta, 2); + AccountAbleChangeLogService::logCashAccountAbleChange( + $userId, + $userType, + $before, + $after, + $sourceType, + 'yii_ledger_log', + (int)($extra['source_id'] ?? 0), + $extra + ); + } + + private function applySupplierBalanceDeltaWithLog(int $userId, $delta, string $sourceType, array $extra = []): void + { + $account = AccountBalanceModel::findOne(['user_id' => $userId, 'type' => 3]); + $before = $account ? $account->balance : 0; + AccountBalanceModel::updateAllCounters(['balance' => $delta], ['user_id' => $userId, 'type' => 3]); + $account = AccountBalanceModel::findOne(['user_id' => $userId, 'type' => 3]); + $after = $account ? $account->balance : bcadd((string)$before, (string)$delta, 6); + AccountAbleChangeLogService::logSupplierBalanceChange( + $userId, + $before, + $after, + $sourceType, + 'yii_ledger_log', + (int)($extra['source_id'] ?? 0), + $extra + ); + } + } \ No newline at end of file diff --git a/common/models/newDb/AccountAbleChangeLogModel.php b/common/models/newDb/AccountAbleChangeLogModel.php new file mode 100644 index 0000000..048f784 --- /dev/null +++ b/common/models/newDb/AccountAbleChangeLogModel.php @@ -0,0 +1,11 @@ +user_id = $userId; + $model->user_type = $userType; + $model->account_table = $accountTable; + $model->field_name = $fieldName; + $model->before_amount = $before; + $model->after_amount = $after; + $model->change_amount = $change; + $model->source_type = $sourceType; + $model->source_table = $sourceTable; + $model->source_id = $sourceId; + $model->order_id = (int)($extra['order_id'] ?? 0); + $model->order_type = (int)($extra['order_type'] ?? 0); + $model->order_no = (string)($extra['order_no'] ?? ''); + $model->remark = (string)($extra['remark'] ?? ''); + $model->created_at = $time; + $model->updated_at = $time; + if (!$model->save(false)) { + Yii::warning('写入 xk_account_able_change_log 失败', __METHOD__); + } + } + + public static function logCashAccountAbleChange( + int $userId, + int $userType, + $before, + $after, + string $sourceType, + string $sourceTable, + int $sourceId = 0, + array $extra = [] + ): void { + self::recordWithdrawableChange( + $userId, + $userType, + self::ACCOUNT_TABLE_CASH, + self::FIELD_ABLE_CASH, + $before, + $after, + $sourceType, + $sourceTable, + $sourceId, + $extra + ); + } + + public static function logSupplierBalanceChange( + int $userId, + $before, + $after, + string $sourceType, + string $sourceTable, + int $sourceId = 0, + array $extra = [] + ): void { + self::recordWithdrawableChange( + $userId, + 3, + self::ACCOUNT_TABLE_BALANCE, + self::FIELD_BALANCE, + $before, + $after, + $sourceType, + $sourceTable, + $sourceId, + $extra + ); + } +} diff --git a/member/modules/v1/controllers/EplpayCallbackController.php b/member/modules/v1/controllers/EplpayCallbackController.php index f628811..3178e2e 100644 --- a/member/modules/v1/controllers/EplpayCallbackController.php +++ b/member/modules/v1/controllers/EplpayCallbackController.php @@ -13,6 +13,7 @@ use common\models\oldDb\CashAccount; use common\models\oldDb\CashApply; use common\models\oldDb\ChargeCashPayRecord; use common\models\oldDb\FundWater; +use common\services\AccountAbleChangeLogService; use common\models\oldDb\PaymentProductOrder; use common\models\oldDb\PaymentProductRefund; use common\models\oldDb\PaymentRegister; @@ -309,13 +310,35 @@ class EplpayCallbackController extends \yii\web\Controller $applyOrder->saveOrFail(); if ($applyOrder->user_type === 3) { + $before = $CashAccount->balance; $CashAccount->balance = bcadd($CashAccount->balance, $applyOrder->apply_cash, 6); $CashAccount->withdrawn_frozen = bcsub($CashAccount->withdrawn_frozen, $applyOrder->apply_cash, 6); + $CashAccount->saveOrFail(); + AccountAbleChangeLogService::logSupplierBalanceChange( + (int)$applyOrder->user_id, + $before, + $CashAccount->balance, + 'withdraw', + 'yii_cash_apply', + (int)$applyOrder->id, + ['order_no' => (string)$applyOrder->order_no, 'remark' => '提现回调失败回退可提现余额'] + ); } else { + $before = $CashAccount->able_cash; $CashAccount->able_cash = $CashAccount->able_cash + $applyOrder->apply_cash; $CashAccount->frozen_cash -= $applyOrder->apply_cash; + $CashAccount->saveOrFail(); + AccountAbleChangeLogService::logCashAccountAbleChange( + (int)$applyOrder->user_id, + (int)$applyOrder->user_type, + $before, + $CashAccount->able_cash, + 'withdraw', + 'yii_cash_apply', + (int)$applyOrder->id, + ['order_no' => (string)$applyOrder->order_no, 'remark' => '提现回调失败回退可提现金额'] + ); } - $CashAccount->saveOrFail(); } else { // 获取代扣订单 $chargeCpr = ChargeCashPayRecord::find()->where([