Coder Social home page Coder Social logo

yii2-balance's Introduction

Yii2 Balance Manager

Build Status Coverage Status Dependency Status Latest Stable Version Total Downloads

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)
        ]);

yii2-balance's People

Contributors

tapakan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.