-
Notifications
You must be signed in to change notification settings - Fork 3
/
HasBalance.php
136 lines (120 loc) · 3.29 KB
/
HasBalance.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace MrEduar\Balance;
use Illuminate\Support\Arr;
trait HasBalance
{
/**
* Get the model's balance amount.
*
* @return float|int
*/
public function getBalanceAttribute()
{
return $this->balanceHistory()->sum('amount') / 100;
}
/**
* Get the model's balance amount.
*
* @return int
*/
public function getIntBalanceAttribute()
{
return (int) $this->balanceHistory()->sum('amount');
}
/**
* Increase the balance amount.
*
* @param int $amount
* @param array $parameters
* @return \MrEduar\Balance\Balance
*/
public function increaseBalance(int $amount, array $parameters = [])
{
return $this->createBalanceHistory($amount, $parameters);
}
/**
* Decrease the balance amount
*
* @param int $amount
* @param array $parameters
* @return \MrEduar\Balance\Balance
*/
public function decreaseBalance(int $amount, array $parameters = [])
{
return $this->createBalanceHistory(-1 * abs($amount), $parameters);
}
/**
* Modify the balance sheet with the given value.
*
* @param int $amount
* @param array $parameters
* @return \MrEduar\Balance\Balance
*/
public function modifyBalance(int $amount, array $parameters = [])
{
return $this->createBalanceHistory($amount, $parameters);
}
/**
* Reset the balance to 0 or set a new value.
*
* @param int|null $newAmount
* @param array $parameters
* @return \MrEduar\Balance\Balance
*/
public function resetBalance(int $newAmount = null, $parameters = [])
{
$this->balanceHistory()->delete();
if (is_null($newAmount)) {
return true;
}
return $this->createBalanceHistory($newAmount, $parameters);
}
/**
* Check if there is a positive balance.
*
* @param int $amount
* @return bool
*/
public function hasBalance(int $amount = 1)
{
return $this->balance > 0 && $this->balanceHistory()->sum('amount') >= $amount;
}
/**
* Check if there is no more balance.
*
* @return bool
*/
public function hasNoBalance()
{
return $this->balance <= 0;
}
/**
* Function to handle mutations (increase, decrease).
*
* @param int $amount
* @param array $parameters
* @return \MrEduar\Balance\Balance
*/
protected function createBalanceHistory(int $amount, array $parameters = [])
{
$reference = Arr::get($parameters, 'reference');
$createArguments = collect([
'amount' => $amount,
'description' => Arr::get($parameters, 'description'),
])->when($reference, function ($collection) use ($reference) {
return $collection
->put('referenceable_type', $reference->getMorphClass())
->put('referenceable_id', $reference->getKey());
})->toArray();
return $this->balanceHistory()->create($createArguments);
}
/**
* Get all Balance History.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function balanceHistory()
{
return $this->morphMany(Balance::class, 'balanceable');
}
}