1. 分账资金变动功能
This commit is contained in:
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
11
common/models/newDb/AccountAbleChangeLogModel.php
Normal file
11
common/models/newDb/AccountAbleChangeLogModel.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace common\models\newDb;
|
||||
|
||||
class AccountAbleChangeLogModel extends \common\core\BaseNewActiveRecord
|
||||
{
|
||||
public static function tableName()
|
||||
{
|
||||
return 'xk_account_able_change_log';
|
||||
}
|
||||
}
|
||||
101
common/services/AccountAbleChangeLogService.php
Normal file
101
common/services/AccountAbleChangeLogService.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace common\services;
|
||||
|
||||
use common\models\newDb\AccountAbleChangeLogModel;
|
||||
use Yii;
|
||||
|
||||
class AccountAbleChangeLogService
|
||||
{
|
||||
public const ACCOUNT_TABLE_CASH = 'yii_cash_account';
|
||||
public const ACCOUNT_TABLE_BALANCE = 'xk_account_balance';
|
||||
public const FIELD_ABLE_CASH = 'able_cash';
|
||||
public const FIELD_BALANCE = 'balance';
|
||||
|
||||
public static function recordWithdrawableChange(
|
||||
int $userId,
|
||||
int $userType,
|
||||
string $accountTable,
|
||||
string $fieldName,
|
||||
$before,
|
||||
$after,
|
||||
string $sourceType,
|
||||
string $sourceTable,
|
||||
int $sourceId = 0,
|
||||
array $extra = []
|
||||
): void {
|
||||
$change = bcsub((string)$after, (string)$before, 6);
|
||||
if (bccomp($change, '0', 6) === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$time = time();
|
||||
$model = new AccountAbleChangeLogModel();
|
||||
$model->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
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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([
|
||||
|
||||
Reference in New Issue
Block a user