Skip to content

Commit

Permalink
Configure and use facade and service; add unbiased
Browse files Browse the repository at this point in the history
  • Loading branch information
MannikJ committed Jun 6, 2019
1 parent 580ab26 commit b249c1c
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 38 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"MannikJ\\Laravel\\Wallet\\WalletServiceProvider"
],
"aliases": {
"Wallet": "MannikJ\\Laravel\\Wallet\\WalletFacade"
"Wallet": "MannikJ\\Laravel\\Wallet\\Facades\\WalletFacade"
}
}
},
Expand Down
21 changes: 15 additions & 6 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
use MannikJ\Laravel\Wallet\Models\Transaction;

return [
/**
* Disable auto-loading of the vendor migrations
* You can then publish the migrations and
* change them for more flexibility
*/
/**
* Disable auto-loading of the vendor migrations
* You can then publish the migrations and
* change them for more flexibility
*/
'load_migrations' => env('WALLET_LOAD_MIGRATIONS', true),
/**
* Change this to specify the money amount column types
Expand Down Expand Up @@ -43,4 +43,13 @@
* All amounts will be converted to a negative value
*/
'subtracting_transaction_types' => explode(',', env('WALLET_SUBTRACTING_TRANSACTION_TYPES', 'withdraw,payout')),
];

/**
* Transaction types that can be positive or negative
* Per default all types that are not explicitly specified
* as positive or negative are treated as unbiased types.
* It still might be convenient to specify your the available
* unbiased types here explicitly so you can show the options
*/
'unbiased_transaction_types' => explode(',', env('WALLET_UNBIASED_TRANSACTION_TYPES', '')),
];
3 changes: 2 additions & 1 deletion src/Jobs/RecalculateWalletBalance.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class RecalculateWalletBalance implements ShouldQueue
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

protected $wallet;

const CACHE_PREFIX = 'recalculate:wallet:';


Expand Down Expand Up @@ -52,4 +53,4 @@ public static function buildCacheKey(Wallet $wallet = id, $name = null)
$base .= $wallet ? $wallet->id : '';
return $base .= $name ? ':' . $name : '';
}
}
}
20 changes: 9 additions & 11 deletions src/Models/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ public function setAmountAttribute($amount)

public function getAmountWithSign($amount = null, $type = null)
{
$amount = $amount ? : array_get($this->attributes, 'amount');
$type = $type ? : $this->type;
$amount = $amount ?: array_get($this->attributes, 'amount');
$type = $type ?: $this->type;
$amount = $this->shouldConvertToAbsoluteAmount() ? abs($amount) : $amount;
if (in_array($type, config('wallet.subtracting_transaction_types', []))) {
return $amount * -1;
Expand All @@ -112,9 +112,9 @@ public function getAmountWithSign($amount = null, $type = null)

public function shouldConvertToAbsoluteAmount($type = null)
{
$type = $type ? : $this->type;
return in_array($type, config('wallet.subtracting_transaction_types', [])) ||
in_array($type, config('wallet.adding_transaction_types', []));
$type = $type ?: $this->type;
return in_array($type, \Wallet::subtractingTransactionTypes()) ||
in_array($type, \Wallet::addingTransactionTypes());
}

public function getTotalAmount()
Expand All @@ -128,18 +128,18 @@ public function getTotalAmount()

public static function getSignedAmountRawSql($table = null)
{
$table = $table ? : (new static())->getTable();
$table = $table ?: (new static())->getTable();
$subtractingTypes = implode(',', array_map(
function ($type) {
return "'{$type}'";
},
config('wallet.subtracting_transaction_types')
\Wallet::subtractingTransactionTypes()
));
$addingTypes = implode(',', array_map(
function ($type) {
return "'{$type}'";
},
config('wallet.adding_transaction_types')
\Wallet::addingTransactionTypes()
));
return "CASE
WHEN {$table}.type
Expand Down Expand Up @@ -191,6 +191,4 @@ public function getTotalAmountAttribute()
$totalAmount = array_get($this->attributes, 'total_amount', $this->getTotalAmount());
return $totalAmount;
}


}
}
13 changes: 4 additions & 9 deletions src/Models/Wallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
use Exception;
use MannikJ\Laravel\Wallet\Exceptions\UnacceptedTransactionException;

class Wallet extends Model
Expand Down Expand Up @@ -164,17 +163,14 @@ public function setBalance($amount, $comment = 'Manual offset transaction')
public function actualBalance(bool $save = false)
{
$undefined = $this->transactions()
->whereNotIn('type', array_merge(
config('wallet.adding_transaction_types'),
config('wallet.subtracting_transaction_types')
))
->whereNotIn('type', \Wallet::biasedTransactionTypes())
->sum('amount');
$credits = $this->transactions()
->whereIn('type', config('wallet.adding_transaction_types'))
->whereIn('type', \Wallet::addingTransactionTypes())
->sum(\DB::raw('abs(amount)'));

$debits = $this->transactions()
->whereIn('type', config('wallet.subtracting_transaction_types'))
->whereIn('type', \Wallet::subtractingTransactionTypes())
->sum(\DB::raw('abs(amount)'));
$balance = $undefined + $credits - $debits;

Expand All @@ -184,5 +180,4 @@ public function actualBalance(bool $save = false)
}
return $balance;
}

}
}
3 changes: 0 additions & 3 deletions src/Observers/TransactionObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace MannikJ\Laravel\Wallet\Observers;

use MannikJ\Laravel\Wallet\Models\Wallet;
use MannikJ\Laravel\Wallet\Models\Transaction;

class TransactionObserver
{
public function creating($transaction)
Expand Down
11 changes: 6 additions & 5 deletions src/Observers/WalletObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ class WalletObserver
{
public function saved(Wallet $wallet)
{
if ($wallet->getOriginal('balance') != $wallet->balance
&& config('auto_recalculate_balance', false)) {
$job = new RecalculateWalletBalance($wallet);
dispatch($job);
if (
$wallet->isDirty('balance')
&& \Wallet::autoRecalculationActive()
) {
RecalculateWalletBalance::dispatch($wallet);
}
}
}
}
36 changes: 36 additions & 0 deletions src/Services/Wallet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace MannikJ\Laravel\Wallet\Services;

class Wallet
{
public function addingTransactionTypes()
{
return config('wallet.adding_transaction_types', []);
}

public function subtractingTransactionTypes()
{
return config('wallet.subtracting_transaction_types', []);
}

public function unbiasedTransactionTypes()
{
return config('wallet.unbiased_transaction_types', []);
}

public function biasedTransactionTypes()
{
return array_merge($this->addingTransactionTypes(), $this->subtractingTransactionTypes());
}

public function transactionTypes()
{
return array_merge($this->biasedTransactionTypes(), $this->unbiasedTransactionTypes());
}

public function autoRecalculationActive()
{
return config('auto_recalculate_balance', false);
}
}
7 changes: 5 additions & 2 deletions src/WalletServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
namespace MannikJ\Laravel\Wallet;

use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Eloquent\Factory;
use Illuminate\Support\Carbon;
use MannikJ\Laravel\Wallet\Observers\WalletObserver;
use MannikJ\Laravel\Wallet\Observers\TransactionObserver;
use MannikJ\Laravel\Wallet\Services\Wallet;

class WalletServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -37,5 +36,9 @@ public function boot()
public function register()
{
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'wallet');

$this->app->singleton('wallet', function () {
return new Wallet;
});
}
}
8 changes: 8 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Orchestra\Testbench\TestCase as BaseTestCase;
use MannikJ\Laravel\Wallet\WalletServiceProvider;
use MannikJ\Laravel\Wallet\Facades\WalletFacade;

class TestCase extends BaseTestCase
{
Expand All @@ -24,4 +25,11 @@ protected function getPackageProviders($app)
WalletServiceProvider::class
];
}

protected function getPackageAliases()
{
return [
'Wallet' => WalletFacade::class
];
}
}

0 comments on commit b249c1c

Please sign in to comment.