Yii2 Balance Manager
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)
]);