1. 分账资金变动功能

This commit is contained in:
李琦
2026-06-02 08:15:42 +08:00
parent 6a8c4dcdf6
commit 6b7f9e8971
4 changed files with 227 additions and 23 deletions

View File

@@ -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
);
}
}