This commit is contained in:
James Cole
2025-03-05 20:12:44 +01:00
parent 3ce111550e
commit 0031c5a5ad
4 changed files with 56 additions and 34 deletions

View File

@@ -82,7 +82,7 @@ class UpdateRequest extends FormRequest
'accounts' => 'required', 'accounts' => 'required',
'accounts.*' => 'array|required', 'accounts.*' => 'array|required',
'accounts.*.account_id' => ['required', 'numeric', 'belongsToUser:accounts,id'], 'accounts.*.account_id' => ['required', 'numeric', 'belongsToUser:accounts,id'],
'accounts.*.current_amount' => ['numeric', new IsValidZeroOrMoreAmount()], 'accounts.*.current_amount' => ['numeric','nullable', new IsValidZeroOrMoreAmount(true)],
'object_group_id' => 'numeric|belongsToUser:object_groups,id', 'object_group_id' => 'numeric|belongsToUser:object_groups,id',
'object_group_title' => ['min:1', 'max:255'], 'object_group_title' => ['min:1', 'max:255'],
'transaction_currency_id' => 'exists:transaction_currencies,id|nullable', 'transaction_currency_id' => 'exists:transaction_currencies,id|nullable',

View File

@@ -147,8 +147,7 @@ class PiggyBankFactory
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id') ->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
->where('accounts.user_id', $this->user->id) ->where('accounts.user_id', $this->user->id)
->where('piggy_banks.id', $piggyBankId) ->where('piggy_banks.id', $piggyBankId)
->first(['piggy_banks.*']) ->first(['piggy_banks.*']);
;
if (null !== $piggyBank) { if (null !== $piggyBank) {
return $piggyBank; return $piggyBank;
} }
@@ -172,8 +171,7 @@ class PiggyBankFactory
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id') ->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
->where('accounts.user_id', $this->user->id) ->where('accounts.user_id', $this->user->id)
->where('piggy_banks.name', $name) ->where('piggy_banks.name', $name)
->first(['piggy_banks.*']) ->first(['piggy_banks.*']);
;
} }
private function setOrder(PiggyBank $piggyBank, array $data): PiggyBank private function setOrder(PiggyBank $piggyBank, array $data): PiggyBank
@@ -201,8 +199,7 @@ class PiggyBankFactory
'objectGroups', 'objectGroups',
] ]
) )
->orderBy('piggy_banks.order', 'ASC')->get(['piggy_banks.*']) ->orderBy('piggy_banks.order', 'ASC')->get(['piggy_banks.*']);
;
$current = 1; $current = 1;
foreach ($set as $piggyBank) { foreach ($set as $piggyBank) {
if ($piggyBank->order !== $current) { if ($piggyBank->order !== $current) {
@@ -227,15 +224,15 @@ class PiggyBankFactory
// TODO this is a tedious check. Feels like a hack. // TODO this is a tedious check. Feels like a hack.
$toBeLinked = []; $toBeLinked = [];
foreach ($piggyBank->accounts as $account) { foreach ($piggyBank->accounts as $account) {
Log::debug(sprintf('Checking account #%d', $account->id));
foreach ($accounts as $info) { foreach ($accounts as $info) {
if ($account->id === $info['account_id']) { Log::debug(sprintf(' Checking other account #%d', $info['account_id']));
if (array_key_exists($account->id, $accounts)) { if ((int) $account->id === (int) $info['account_id']) {
$toBeLinked[$account->id] = ['current_amount' => $account->pivot->current_amount ?? '0']; $toBeLinked[$account->id] = ['current_amount' => $account->pivot->current_amount ?? '0'];
Log::debug(sprintf('Prefilled for account #%d with amount %s', $account->id, $account->pivot->current_amount ?? '0')); Log::debug(sprintf('Prefilled for account #%d with amount %s', $account->id, $account->pivot->current_amount ?? '0'));
} }
} }
} }
}
/** @var array $info */ /** @var array $info */
@@ -244,9 +241,13 @@ class PiggyBankFactory
if (null === $account) { if (null === $account) {
continue; continue;
} }
if (array_key_exists('current_amount', $info)) { if (array_key_exists('current_amount', $info) && null !== $info['current_amount']) {
$toBeLinked[$account->id] = ['current_amount' => $info['current_amount']]; $toBeLinked[$account->id] = ['current_amount' => $info['current_amount']];
Log::debug(sprintf('Will link account #%d with amount %s', $account->id, $account->pivot->current_amount ?? '0')); Log::debug(sprintf('[a] Will link account #%d with amount %s', $account->id, $info['current_amount']));
}
if (array_key_exists('current_amount', $info) && null === $info['current_amount']) {
$toBeLinked[$account->id] = ['current_amount' => $toBeLinked[$account->id]['current_amount'] ?? '0'];
Log::debug(sprintf('[b] Will link account #%d with amount %s', $account->id, $toBeLinked[$account->id]['current_amount'] ?? '0'));
} }
if (!array_key_exists('current_amount', $info)) { if (!array_key_exists('current_amount', $info)) {
$toBeLinked[$account->id] ??= []; $toBeLinked[$account->id] ??= [];

View File

@@ -31,6 +31,14 @@ use Illuminate\Support\Facades\Log;
class IsValidZeroOrMoreAmount implements ValidationRule class IsValidZeroOrMoreAmount implements ValidationRule
{ {
private bool $nullable = false;
public function __construct(bool $nullable = false)
{
$this->nullable = $nullable;
}
use ValidatesAmountsTrait; use ValidatesAmountsTrait;
/** /**
@@ -38,6 +46,9 @@ class IsValidZeroOrMoreAmount implements ValidationRule
*/ */
public function validate(string $attribute, mixed $value, \Closure $fail): void public function validate(string $attribute, mixed $value, \Closure $fail): void
{ {
if (true === $this->nullable && null === $value) {
return;
}
$value = (string) $value; $value = (string) $value;
// must not be empty: // must not be empty:
if ($this->emptyString($value)) { if ($this->emptyString($value)) {

View File

@@ -400,9 +400,19 @@ trait ConvertsDataTypes
if (!is_array($entry)) { if (!is_array($entry)) {
continue; continue;
} }
$amount = null;
if(array_key_exists('current_amount',$entry)) {
$amount = $this->clearString((string) ($entry['current_amount'] ?? '0'));
if(null === $entry['current_amount']) {
$amount = null;
}
}
if(!array_key_exists('current_amount',$entry)) {
$amount = null;
}
$return[] = [ $return[] = [
'account_id' => $this->integerFromValue((string) ($entry['account_id'] ?? '0')), 'account_id' => $this->integerFromValue((string) ($entry['account_id'] ?? '0')),
'current_amount' => $this->clearString((string) ($entry['current_amount'] ?? '0')), 'current_amount' => $amount,
]; ];
} }