1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Siak\Tontine\Validation\Meeting; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Validator; |
6
|
|
|
use Siak\Tontine\Service\LocaleService; |
7
|
|
|
use Siak\Tontine\Validation\AbstractValidator; |
8
|
|
|
use Siak\Tontine\Validation\Traits\ValidationTrait; |
9
|
|
|
use Siak\Tontine\Validation\ValidationException; |
10
|
|
|
|
11
|
|
|
use function trans; |
12
|
|
|
|
13
|
|
|
class LoanValidator extends AbstractValidator |
14
|
|
|
{ |
15
|
|
|
use ValidationTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param LocaleService $localeService |
19
|
|
|
*/ |
20
|
|
|
public function __construct(private LocaleService $localeService) |
21
|
|
|
{} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return array<string> |
25
|
|
|
*/ |
26
|
|
|
protected function amountFields(): array |
27
|
|
|
{ |
28
|
|
|
return ['principal', 'interest']; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array $values |
33
|
|
|
* |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
|
|
public function validateItem(array $values): array |
37
|
|
|
{ |
38
|
|
|
$validator = Validator::make($this->values($values), [ |
39
|
|
|
'member' => 'required|integer|min:1', |
40
|
|
|
'fund' => 'required|integer|min:1', |
41
|
|
|
'principal' => $this->amountRule(), |
42
|
|
|
'interest_type' => 'required|in:f,u,s,c', |
43
|
|
|
'interest' => $this->amountRule(), |
44
|
|
|
]); |
45
|
|
|
$validator->after(function($validator) use($values) { |
46
|
|
|
if((float)$values['principal'] <= 0) |
47
|
|
|
{ |
48
|
|
|
$validator->errors()->add('principal', trans('validation.gt.numeric', [ |
49
|
|
|
'attribute' => trans('meeting.loan.labels.principal'), |
50
|
|
|
'value' => 0, |
51
|
|
|
])); |
52
|
|
|
} |
53
|
|
|
}); |
54
|
|
|
if($validator->fails()) |
55
|
|
|
{ |
56
|
|
|
throw new ValidationException($validator); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$validated = $validator->validated(); |
60
|
|
|
$validated['principal'] = $this->localeService |
61
|
|
|
->convertMoneyToInt((float)$validated['principal']); |
62
|
|
|
// Interest rates must be saved as int, so the value is multiplied by 100. |
63
|
|
|
$validated['interest_rate'] = $validated['interest_type'] === 'f' ? 0 : |
64
|
|
|
(int)(100 * $validated['interest']); |
65
|
|
|
$validated['interest'] = $validated['interest_type'] === 'f' ? |
66
|
|
|
$this->localeService->convertMoneyToInt((float)$validated['interest']) : |
67
|
|
|
(int)($validated['principal'] * ((float)$validated['interest'] / 100)); |
68
|
|
|
|
69
|
|
|
return $validated; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|