mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-18 18:40:12 +00:00
Fix #9914
This commit is contained in:
@@ -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',
|
||||||
|
@@ -67,7 +67,7 @@ class PiggyBankFactory
|
|||||||
public function store(array $data): PiggyBank
|
public function store(array $data): PiggyBank
|
||||||
{
|
{
|
||||||
|
|
||||||
$piggyBankData = $data;
|
$piggyBankData = $data;
|
||||||
|
|
||||||
// unset some fields
|
// unset some fields
|
||||||
unset($piggyBankData['object_group_title'], $piggyBankData['transaction_currency_code'], $piggyBankData['transaction_currency_id'], $piggyBankData['accounts'], $piggyBankData['object_group_id'], $piggyBankData['notes']);
|
unset($piggyBankData['object_group_title'], $piggyBankData['transaction_currency_code'], $piggyBankData['transaction_currency_id'], $piggyBankData['accounts'], $piggyBankData['object_group_id'], $piggyBankData['notes']);
|
||||||
@@ -91,11 +91,11 @@ class PiggyBankFactory
|
|||||||
|
|
||||||
throw new FireflyException('400005: Could not store new piggy bank.', 0, $e);
|
throw new FireflyException('400005: Could not store new piggy bank.', 0, $e);
|
||||||
}
|
}
|
||||||
$piggyBank = $this->setOrder($piggyBank, $data);
|
$piggyBank = $this->setOrder($piggyBank, $data);
|
||||||
$this->linkToAccountIds($piggyBank, $data['accounts']);
|
$this->linkToAccountIds($piggyBank, $data['accounts']);
|
||||||
$this->piggyBankRepository->updateNote($piggyBank, $data['notes']);
|
$this->piggyBankRepository->updateNote($piggyBank, $data['notes']);
|
||||||
|
|
||||||
$objectGroupTitle = $data['object_group_title'] ?? '';
|
$objectGroupTitle = $data['object_group_title'] ?? '';
|
||||||
if ('' !== $objectGroupTitle) {
|
if ('' !== $objectGroupTitle) {
|
||||||
$objectGroup = $this->findOrCreateObjectGroup($objectGroupTitle);
|
$objectGroup = $this->findOrCreateObjectGroup($objectGroupTitle);
|
||||||
if (null !== $objectGroup) {
|
if (null !== $objectGroup) {
|
||||||
@@ -103,7 +103,7 @@ class PiggyBankFactory
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// try also with ID
|
// try also with ID
|
||||||
$objectGroupId = (int) ($data['object_group_id'] ?? 0);
|
$objectGroupId = (int) ($data['object_group_id'] ?? 0);
|
||||||
if (0 !== $objectGroupId) {
|
if (0 !== $objectGroupId) {
|
||||||
$objectGroup = $this->findObjectGroupById($objectGroupId);
|
$objectGroup = $this->findObjectGroupById($objectGroupId);
|
||||||
if (null !== $objectGroup) {
|
if (null !== $objectGroup) {
|
||||||
@@ -111,7 +111,7 @@ class PiggyBankFactory
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Log::debug('Touch piggy bank');
|
Log::debug('Touch piggy bank');
|
||||||
$piggyBank->encrypted = false;
|
$piggyBank->encrypted = false;
|
||||||
$piggyBank->save();
|
$piggyBank->save();
|
||||||
$piggyBank->touch();
|
$piggyBank->touch();
|
||||||
|
|
||||||
@@ -144,11 +144,10 @@ class PiggyBankFactory
|
|||||||
// first find by ID:
|
// first find by ID:
|
||||||
if ($piggyBankId > 0) {
|
if ($piggyBankId > 0) {
|
||||||
$piggyBank = PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
|
$piggyBank = PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
|
||||||
->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;
|
||||||
}
|
}
|
||||||
@@ -169,17 +168,16 @@ class PiggyBankFactory
|
|||||||
public function findByName(string $name): ?PiggyBank
|
public function findByName(string $name): ?PiggyBank
|
||||||
{
|
{
|
||||||
return PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
|
return PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
|
||||||
->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
|
||||||
{
|
{
|
||||||
$this->resetOrder();
|
$this->resetOrder();
|
||||||
$order = $this->getMaxOrder() + 1;
|
$order = $this->getMaxOrder() + 1;
|
||||||
if (array_key_exists('order', $data)) {
|
if (array_key_exists('order', $data)) {
|
||||||
$order = $data['order'];
|
$order = $data['order'];
|
||||||
}
|
}
|
||||||
@@ -194,15 +192,14 @@ class PiggyBankFactory
|
|||||||
{
|
{
|
||||||
// TODO duplicate code
|
// TODO duplicate code
|
||||||
$set = PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
|
$set = PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
|
||||||
->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)
|
||||||
->with(
|
->with(
|
||||||
[
|
[
|
||||||
'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,12 +224,12 @@ 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'));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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] ??= [];
|
||||||
|
@@ -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)) {
|
||||||
|
@@ -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,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user