tapakan / yii2-balance
支持用户余额和交易,从历史记录中计算实际余额
0.3.2
2017-04-25 13:35 UTC
Requires
- php: >=5.6
- yiisoft/yii2: ~2.0
Requires (Dev)
- codeception/codeception: ^2.2
- codeception/specify: ~0.4.5
- codeception/verify: ~0.3.1
- fzaninotto/faker: ^1.6
- phpunit/php-code-coverage: ~4.0
- satooshi/php-coveralls: ~1.0
- squizlabs/php_codesniffer: ~2.8.0
This package is not auto-updated.
Last update: 2024-09-14 20:50:56 UTC
README
Yii2 Balance 可以执行与用户余额相关的简单操作。操作历史记录保持不变。
##安装
运行以下命令
composer require tapakan/yii2-balance
或者添加
"tapakan/yii2-balance": "*"
到您的 composer.json 的 require 部分
##配置
'components' => [
'balance' => [
'class' => 'Tapakan\Balance\ManagerActiveRecord',
'accountClass' => 'common\models\UserBalance',
'transactionClass' => 'common\models\UserBalanceHistory',
'accountLinkAttribute' => 'account_id',
'amountAttribute' => 'value',
'balanceAttribute' => 'value',
'accountUserIdAttribute' => 'user_id'
],
],
##使用 给用户添加一些值
Yii:$app->balance->increase($accountId_OR_userId_OR_condition, 500);
或者取出
Yii:$app->balance->decrease($accountId_OR_userId_OR_condition, 100);
从历史记录计算余额
echo Yii:$app->balance->calculateBalance($accountId_OR_userId); // 400
包含可能存储在余额历史记录表中的额外信息
Yii:$app->balance->increase($accountId_OR_userId_OR_condition, 750, [ 'order_id' => 1, // other usefull info ]);
#####自 0.1.1 版本起,您可以撤销交易。让我们允许交易 #35,这是从用户账户中删除 200 个积分。以下命令将它们返回到账户。
Yii:$app->balance->revert($transactionId)
表结构示例
// History of operations $this->createTable('balance_history', [ 'account_id' => $this->integer(), 'value' => $this->decimal(13, 4), 'order_id' => $this->integer(), // Other usefull information ]); // Calculated balance $this->createTable('balance', [ 'id' => $this->primaryKey(), 'user_id' => $this->integer(), 'value' => $this->decimal(13, 4) ]);