|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Siak\Tontine\Service\Meeting\Pool; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
|
7
|
|
|
use Illuminate\Support\Facades\DB; |
|
8
|
|
|
use Siak\Tontine\Exception\MessageException; |
|
9
|
|
|
use Siak\Tontine\Model\DepositReal; |
|
10
|
|
|
use Siak\Tontine\Model\Pool; |
|
11
|
|
|
use Siak\Tontine\Model\Receivable; |
|
12
|
|
|
use Siak\Tontine\Model\Session; |
|
13
|
|
|
use Siak\Tontine\Service\Payment\PaymentServiceInterface; |
|
14
|
|
|
use Siak\Tontine\Service\TenantService; |
|
15
|
|
|
|
|
16
|
|
|
use function trans; |
|
17
|
|
|
|
|
18
|
|
|
trait DepositServiceTrait |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var TenantService |
|
22
|
|
|
*/ |
|
23
|
|
|
protected TenantService $tenantService; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var PaymentServiceInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
protected PaymentServiceInterface $paymentService; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param Builder|Relation |
|
32
|
|
|
* @param int $page |
|
33
|
|
|
* |
|
34
|
|
|
* @return Builder|Relation |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getReceivableDetailsQuery(Builder|Relation $query, |
|
37
|
|
|
int $page = 0): Builder|Relation |
|
38
|
|
|
{ |
|
39
|
|
|
return $query |
|
|
|
|
|
|
40
|
|
|
->select('receivables.*', DB::raw('pd.amount, member_defs.name as member')) |
|
41
|
|
|
->join('pools', 'pools.id', '=', 'subscriptions.pool_id') |
|
42
|
|
|
->join(DB::raw('pool_defs as pd'), 'pools.def_id', '=', 'pd.id') |
|
43
|
|
|
->join('members', 'members.id', '=', 'subscriptions.member_id') |
|
44
|
|
|
->join('member_defs', 'members.def_id', '=', 'member_defs.id') |
|
45
|
|
|
->with(['deposit']) |
|
46
|
|
|
->page($page, $this->tenantService->getLimit()) |
|
47
|
|
|
->orderBy('member_defs.name', 'asc') |
|
|
|
|
|
|
48
|
|
|
->orderBy('subscriptions.id', 'asc'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Create a deposit. |
|
53
|
|
|
* |
|
54
|
|
|
* @param Receivable $receivable |
|
55
|
|
|
* @param Session $session The session |
|
56
|
|
|
* @param int $amount |
|
57
|
|
|
* |
|
58
|
|
|
* @return void |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function saveDeposit(Receivable $receivable, Session $session, int $amount = 0): void |
|
61
|
|
|
{ |
|
62
|
|
|
if($receivable->deposit !== null) |
|
63
|
|
|
{ |
|
64
|
|
|
// The deposit exists. It is then modified. |
|
65
|
|
|
DepositReal::where(['id' => $receivable->deposit->id]) |
|
66
|
|
|
->update(['amount' => $amount]); |
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$deposit = new DepositReal(); |
|
71
|
|
|
$deposit->amount = $amount; |
|
72
|
|
|
$deposit->receivable()->associate($receivable); |
|
73
|
|
|
$deposit->session()->associate($session); |
|
74
|
|
|
$deposit->save(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param Pool $pool The pool |
|
79
|
|
|
* @param Receivable|null $receivable |
|
80
|
|
|
* @param int $amount |
|
81
|
|
|
* |
|
82
|
|
|
* @return void |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function checkDepositCreation(Pool $pool, ?Receivable $receivable, int $amount = 0): void |
|
85
|
|
|
{ |
|
86
|
|
|
if(!$receivable) |
|
87
|
|
|
{ |
|
88
|
|
|
throw new MessageException(trans('tontine.subscription.errors.not_found')); |
|
89
|
|
|
} |
|
90
|
|
|
if($pool->deposit_fixed && $receivable->deposit !== null) |
|
91
|
|
|
{ |
|
92
|
|
|
throw new MessageException(trans('tontine.subscription.errors.not_found')); |
|
93
|
|
|
} |
|
94
|
|
|
if(!$pool->deposit_fixed) |
|
95
|
|
|
{ |
|
96
|
|
|
if($amount <= 0) |
|
97
|
|
|
{ |
|
98
|
|
|
throw new MessageException(trans('tontine.subscription.errors.amount')); |
|
99
|
|
|
} |
|
100
|
|
|
if($receivable->deposit !== null && |
|
101
|
|
|
!$this->paymentService->isEditable($receivable->deposit)) |
|
102
|
|
|
{ |
|
103
|
|
|
throw new MessageException(trans('tontine.errors.editable')); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Delete a deposit. |
|
110
|
|
|
* |
|
111
|
|
|
* @param Receivable|null $receivable |
|
112
|
|
|
* |
|
113
|
|
|
* @return void |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function _deleteDeposit(?Receivable $receivable): void |
|
116
|
|
|
{ |
|
117
|
|
|
if(!$receivable || !$receivable->deposit) |
|
118
|
|
|
{ |
|
119
|
|
|
throw new MessageException(trans('tontine.subscription.errors.not_found')); |
|
120
|
|
|
} |
|
121
|
|
|
if(!$this->paymentService->isEditable($receivable->deposit)) |
|
122
|
|
|
{ |
|
123
|
|
|
throw new MessageException(trans('tontine.errors.editable')); |
|
124
|
|
|
} |
|
125
|
|
|
$receivable->deposit_real()->delete(); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|