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.*' => 'array|required',
'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_title' => ['min:1', 'max:255'],
'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')
->where('accounts.user_id', $this->user->id)
->where('piggy_banks.id', $piggyBankId)
->first(['piggy_banks.*'])
;
->first(['piggy_banks.*']);
if (null !== $piggyBank) {
return $piggyBank;
}
@@ -172,8 +171,7 @@ class PiggyBankFactory
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
->where('accounts.user_id', $this->user->id)
->where('piggy_banks.name', $name)
->first(['piggy_banks.*'])
;
->first(['piggy_banks.*']);
}
private function setOrder(PiggyBank $piggyBank, array $data): PiggyBank
@@ -201,8 +199,7 @@ class PiggyBankFactory
'objectGroups',
]
)
->orderBy('piggy_banks.order', 'ASC')->get(['piggy_banks.*'])
;
->orderBy('piggy_banks.order', 'ASC')->get(['piggy_banks.*']);
$current = 1;
foreach ($set as $piggyBank) {
if ($piggyBank->order !== $current) {
@@ -227,15 +224,15 @@ class PiggyBankFactory
// TODO this is a tedious check. Feels like a hack.
$toBeLinked = [];
foreach ($piggyBank->accounts as $account) {
Log::debug(sprintf('Checking account #%d', $account->id));
foreach ($accounts as $info) {
if ($account->id === $info['account_id']) {
if (array_key_exists($account->id, $accounts)) {
Log::debug(sprintf(' Checking other account #%d', $info['account_id']));
if ((int) $account->id === (int) $info['account_id']) {
$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'));
}
}
}
}
/** @var array $info */
@@ -244,9 +241,13 @@ class PiggyBankFactory
if (null === $account) {
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']];
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)) {
$toBeLinked[$account->id] ??= [];

View File

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

View File

@@ -400,9 +400,19 @@ trait ConvertsDataTypes
if (!is_array($entry)) {
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[] = [
'account_id' => $this->integerFromValue((string) ($entry['account_id'] ?? '0')),
'current_amount' => $this->clearString((string) ($entry['current_amount'] ?? '0')),
'current_amount' => $amount,
];
}