├── LICENSE ├── README.md ├── composer.json ├── database └── migrations │ └── 2016_02_03_10000_create_point_transactions_table.php └── src ├── Contracts └── Pointable.php ├── Models └── Transaction.php ├── PointableServiceProvider.php └── Traits └── Pointable.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 PackageBackup 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Latest Stable Version](https://poser.pugx.org/trexology/pointable/v/stable)](https://packagist.org/packages/trexology/pointable) 2 | [![Total Downloads](https://poser.pugx.org/trexology/pointable/downloads)](https://packagist.org/packages/trexology/pointable) 3 | [![Latest Unstable Version](https://poser.pugx.org/trexology/pointable/v/unstable)](https://packagist.org/packages/trexology/pointable) [![License](https://poser.pugx.org/trexology/pointable/license)](https://packagist.org/packages/trexology/pointable) 4 | 5 | # Laravel Pointable 6 | Point Transaction system for laravel 5 7 | 8 | ## Installation 9 | 10 | First, pull in the package through Composer. 11 | 12 | ```js 13 | composer require trexology/pointable 14 | ``` 15 | 16 | And then include the service provider within `app/config/app.php`. 17 | 18 | ```php 19 | 'providers' => [ 20 | Trexology\Pointable\PointableServiceProvider::class 21 | ]; 22 | ``` 23 | 24 | At last you need to publish and run the migration. 25 | ``` 26 | php artisan vendor:publish --provider="Trexology\Pointable\PointableServiceProvider" && php artisan migrate 27 | ``` 28 | 29 | ----- 30 | 31 | ### Setup a Model 32 | ```php 33 | 'someReferId', 56 | ]; 57 | 58 | $transaction = $user->addPoints($amount,$message,$data); 59 | 60 | dd($transaction); 61 | ``` 62 | 63 | ### Get Current Points 64 | ```php 65 | $user = User::first(); 66 | $points = $user->currentPoints(); 67 | 68 | dd($points); 69 | ``` 70 | 71 | ### Get Transactions 72 | ```php 73 | $user = User::first(); 74 | $user->transactions; 75 | 76 | //OR 77 | //$user['transactions'] = $user->transactions(2)->get(); //Get last 2 transactions 78 | 79 | dd($user); 80 | ``` 81 | 82 | ### Count Transactions 83 | ```php 84 | $user = User::first(); 85 | $user['transactions_total'] = $user->countTransactions(); 86 | 87 | dd($user); 88 | ``` 89 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "trexology/pointable", 3 | "description": "Point system for Laravel 5", 4 | "keywords": ["points", "pointable", "laravel", "loyalty", "reward"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Trex Lim", 9 | "email": "trexlim@gmail.com" 10 | } 11 | ], 12 | "minimum-stability": "dev", 13 | "require": { 14 | "php": ">=5.5.9", 15 | "illuminate/support": "~5.0" 16 | }, 17 | "autoload": { 18 | "psr-4": { 19 | "Trexology\\Pointable\\": "src/" 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /database/migrations/2016_02_03_10000_create_point_transactions_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 12 | $table->text('message'); 13 | $table->morphs('pointable'); 14 | $table->double('amount'); 15 | $table->double('current'); 16 | $table->timestamps(); 17 | }); 18 | } 19 | 20 | public function down() 21 | { 22 | Schema::dropIfExists('point_transactions'); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Contracts/Pointable.php: -------------------------------------------------------------------------------- 1 | morphTo(); 25 | } 26 | 27 | /** 28 | * @param Model $pointable 29 | * 30 | * @return static 31 | */ 32 | public function getCurrentPoints(Model $pointable) 33 | { 34 | $currentPoint = Transaction:: 35 | where('pointable_id', $pointable->id) 36 | ->where('pointable_type', $pointable->getMorphClass()) 37 | ->orderBy('created_at', 'desc') 38 | ->pluck('current')->first(); 39 | 40 | if (!$currentPoint) { 41 | $currentPoint = 0.0; 42 | } 43 | 44 | return $currentPoint; 45 | } 46 | 47 | /** 48 | * @param Model $pointable 49 | * @param $amount 50 | * @param $message 51 | * @param $data 52 | * 53 | * @return static 54 | */ 55 | public function addTransaction(Model $pointable, $amount, $message, $data = null) 56 | { 57 | $transaction = new static(); 58 | $transaction->amount = $amount; 59 | 60 | $transaction->current = $this->getCurrentPoints($pointable) + $amount; 61 | 62 | $transaction->message = $message; 63 | if ($data) { 64 | $transaction->fill($data); 65 | } 66 | // $transaction->save(); 67 | $pointable->transactions()->save($transaction); 68 | 69 | return $transaction; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/PointableServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 17 | __DIR__.'/../database/migrations/' => database_path('migrations') 18 | ], 'migrations'); 19 | } 20 | 21 | /** 22 | * Register any application services. 23 | * 24 | * @return void 25 | */ 26 | public function register() 27 | { 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Traits/Pointable.php: -------------------------------------------------------------------------------- 1 | morphMany(Transaction::class, 'pointable')->orderBy('created_at','desc')->take($amount); 16 | } 17 | 18 | // /** 19 | // * 20 | // * @return mix 21 | // */ 22 | // public function averagePoint($round= null) 23 | // { 24 | // if ($round) { 25 | // return $this->transactions() 26 | // ->selectRaw('ROUND(AVG(amount), '.$round.') as averagePointTransaction') 27 | // ->pluck('averagePointTransaction'); 28 | // } 29 | // 30 | // return $this->transactions() 31 | // ->selectRaw('AVG(amount) as averagePointTransaction') 32 | // ->pluck('averagePointTransaction'); 33 | // } 34 | // 35 | // /** 36 | // * 37 | // * @return mix 38 | // */ 39 | // public function countPoint(){ 40 | // return $this->transactions() 41 | // ->selectRaw('count(amount) as countTransactions') 42 | // ->pluck('countTransactions'); 43 | // } 44 | // 45 | // /** 46 | // * 47 | // * @return mix 48 | // */ 49 | // public function sumPoint() 50 | // { 51 | // return $this->transactions() 52 | // ->selectRaw('SUM(amount) as sumPointTransactions') 53 | // ->pluck('sumPointTransactions'); 54 | // } 55 | // 56 | // /** 57 | // * @param $max 58 | // * 59 | // * @return mix 60 | // */ 61 | // public function pointPercent($max = 5) 62 | // { 63 | // $transactions = $this->transactions(); 64 | // $quantity = $transactions->count(); 65 | // $total = $transactions->selectRaw('SUM(amount) as total')->pluck('total'); 66 | // return ($quantity * $max) > 0 ? $total / (($quantity * $max) / 100) : 0; 67 | // } 68 | 69 | /** 70 | * 71 | * @return mix 72 | */ 73 | public function countTransactions(){ 74 | return $this->transactions() 75 | ->count(); 76 | } 77 | 78 | /** 79 | * 80 | * @return double 81 | */ 82 | public function currentPoints() 83 | { 84 | return (new Transaction())->getCurrentPoints($this); 85 | } 86 | 87 | /** 88 | * @param $amount 89 | * @param $message 90 | * @param $data 91 | * 92 | * @return static 93 | */ 94 | public function addPoints($amount, $message, $data = null) 95 | { 96 | return (new Transaction())->addTransaction($this, $amount, $message, $data); 97 | } 98 | } 99 | --------------------------------------------------------------------------------