tapakan / yii2-balance
Supports user balance and transactions, calculating actual balance from history
Installs: 1 339
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 2
Forks: 2
Open Issues: 0
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-11-09 21:32:29 UTC
README
Yii2 Balance can perform simple operations with the user's balance. History of operations remains.
##Installation
Run following command
composer require tapakan/yii2-balance
or add
"tapakan/yii2-balance": "*"
to the require section of your composer.json
##Configuration
'components' => [
'balance' => [
'class' => 'Tapakan\Balance\ManagerActiveRecord',
'accountClass' => 'common\models\UserBalance',
'transactionClass' => 'common\models\UserBalanceHistory',
'accountLinkAttribute' => 'account_id',
'amountAttribute' => 'value',
'balanceAttribute' => 'value',
'accountUserIdAttribute' => 'user_id'
],
],
##Usage Add some value to user
Yii:$app->balance->increase($accountId_OR_userId_OR_condition, 500);
Or take
Yii:$app->balance->decrease($accountId_OR_userId_OR_condition, 100);
Calculate balance from history
echo Yii:$app->balance->calculateBalance($accountId_OR_userId); // 400
With additional information that may be stored in the balance history table
Yii:$app->balance->increase($accountId_OR_userId_OR_condition, 750, [ 'order_id' => 1, // other usefull info ]);
#####Since 0.1.1 version you can revert a transaction. Let's allow transaction #35 it is removal of 200 points from the account of the user. The following command will return them into the account.
Yii:$app->balance->revert($transactionId)
Example of table structure
// 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) ]);