Skip to content

Commit fd80185

Browse files
committed
Add features
1 parent c54b721 commit fd80185

9 files changed

+248
-14
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateWalletTypesTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('wallet_types', function (Blueprint $table) {
17+
$table->bigIncrements('id');
18+
$table->string('name');
19+
$table->integer('decimals')->default(0);
20+
$table->timestamps();
21+
});
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*
27+
* @return void
28+
*/
29+
public function down()
30+
{
31+
Schema::dropIfExists('wallet_types');
32+
}
33+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateWalletsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('wallets', function (Blueprint $table) {
17+
$table->bigIncrements('id');
18+
$table->morphs('user');
19+
$table->unsignedBigInteger('wallet_type_id')->nullable();
20+
$table->bigInteger('raw_balance')->default(0);
21+
$table->timestamps();
22+
23+
$table->foreign('wallet_type_id')->references('id')->on('wallet_types')->onDelete('set null');
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::dropIfExists('wallets');
35+
}
36+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateWalletLedgersTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('wallet_ledgers', function (Blueprint $table) {
17+
$table->bigIncrements('id');
18+
$table->unsignedBigInteger('wallet_id')->nullable();
19+
$table->nullableMorphs('transaction');
20+
$table->bigInteger('amount')->default(0);
21+
$table->bigInteger('running_raw_balance')->default(0);
22+
$table->timestamps();
23+
24+
$table->foreign('wallet_id')->references('id')->on('wallets')->onDelete('set null');
25+
});
26+
}
27+
28+
/**
29+
* Reverse the migrations.
30+
*
31+
* @return void
32+
*/
33+
public function down()
34+
{
35+
Schema::dropIfExists('wallet_ledgers');
36+
}
37+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace CoreProc\WalletPlus\Contracts;
4+
5+
interface WalletTransaction
6+
{
7+
public function getAmount();
8+
}

src/Models/Traits/HasWallets.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace CoreProc\WalletPlus\Models\Traits;
4+
5+
use CoreProc\WalletPlus\Models\Wallet;
6+
7+
trait HasWallets
8+
{
9+
public function wallets()
10+
{
11+
return $this->morphMany(Wallet::class, 'user');
12+
}
13+
14+
public function wallet($walletTypeId)
15+
{
16+
return $this->wallets()->where('wallet_type_id', $walletTypeId)->first();
17+
}
18+
}

src/Models/Wallet.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace CoreProc\WalletPlus\Models;
4+
5+
use CoreProc\WalletPlus\Contracts\WalletTransaction;
6+
use Exception;
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
class Wallet extends Model
10+
{
11+
protected $fillable = [
12+
'user_id',
13+
'user_type',
14+
'wallet_type_id',
15+
'raw_balance',
16+
];
17+
18+
public function user()
19+
{
20+
return $this->morphTo('user');
21+
}
22+
23+
public function walletType()
24+
{
25+
return $this->belongsTo(WalletType::class);
26+
}
27+
28+
/**
29+
* @param $transaction WalletTransaction|integer
30+
* @throws Exception
31+
*/
32+
public function incrementBalance($transaction)
33+
{
34+
if (is_int($transaction)) {
35+
$this->increment('raw_balance', $transaction);
36+
$this->createWalletLedgerEntry($transaction, $this->raw_balance);
37+
38+
return;
39+
}
40+
41+
if (! $transaction instanceof WalletTransaction) {
42+
throw new Exception('Increment balance expects parameter to be an integer or a WalletTransaction object.');
43+
}
44+
45+
$this->increment('raw_balance', $transaction->getAmount());
46+
47+
// Record in ledger
48+
$this->createWalletLedgerEntry($transaction, $this->raw_balance);
49+
50+
return;
51+
}
52+
53+
private function createWalletLedgerEntry($transaction, $newRunningRawBalance)
54+
{
55+
if (is_int($transaction)) {
56+
return WalletLedger::query()->create([
57+
'wallet_id' => $this->id,
58+
'amount' => $transaction,
59+
'running_raw_balance' => $newRunningRawBalance,
60+
]);
61+
}
62+
63+
if (! $transaction instanceof WalletTransaction) {
64+
throw new Exception('Increment balance expects parameter to be an integer or a WalletTransaction object.');
65+
}
66+
67+
return WalletLedger::query()->create([
68+
'wallet_id' => $this->id,
69+
'transaction_id' => $transaction->id,
70+
'transaction_type' => get_class($transaction),
71+
'amount' => $transaction->getAmount(),
72+
'running_raw_balance' => $newRunningRawBalance,
73+
]);
74+
}
75+
}

src/Models/WalletLedger.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace CoreProc\WalletPlus\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class WalletLedger extends Model
8+
{
9+
protected $fillable = [
10+
'wallet_id',
11+
'transaction_id',
12+
'transaction_type',
13+
'amount',
14+
'running_raw_balance',
15+
];
16+
17+
public function wallet()
18+
{
19+
return $this->belongsTo(Wallet::class);
20+
}
21+
22+
public function transaction()
23+
{
24+
return $this->morphTo('transaction');
25+
}
26+
}

src/Models/WalletType.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace CoreProc\WalletPlus\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class WalletType extends Model
8+
{
9+
protected $fillable = [
10+
'name',
11+
'decimals',
12+
];
13+
}

src/WalletPlusServiceProvider.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,14 @@ class WalletPlusServiceProvider extends ServiceProvider
1111
*/
1212
public function boot()
1313
{
14-
if ($this->app->runningInConsole()) {
15-
$this->publishes([
16-
__DIR__ . '/../config/config.php' => config_path('wallet-plus.php'),
17-
], 'config');
18-
19-
/*
20-
$this->loadViewsFrom(__DIR__.'/../resources/views', 'wallet-plus');
21-
22-
$this->publishes([
23-
__DIR__.'/../resources/views' => base_path('resources/views/vendor/wallet-plus'),
24-
], 'views');
25-
*/
26-
}
14+
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
2715
}
2816

2917
/**
3018
* Register the application services.
3119
*/
3220
public function register()
3321
{
34-
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'wallet-plus');
22+
// $this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'wallet-plus');
3523
}
3624
}

0 commit comments

Comments
 (0)