James Cole 2022-01-24 07:38:05 +01:00
parent ecc72dd01b
commit 75d52b7024
3 changed files with 76 additions and 50 deletions

View File

@ -284,76 +284,76 @@ class UpdateRequest extends FormRequest
{ {
return [ return [
// basic fields for group: // basic fields for group:
'group_title' => 'between:1,1000', 'group_title' => 'between:1,1000',
'apply_rules' => [new IsBoolean], 'apply_rules' => [new IsBoolean],
// transaction rules (in array for splits): // transaction rules (in array for splits):
'transactions.*.type' => 'in:withdrawal,deposit,transfer,opening-balance,reconciliation', 'transactions.*.type' => 'in:withdrawal,deposit,transfer,opening-balance,reconciliation',
'transactions.*.date' => [new IsDateOrTime], 'transactions.*.date' => [new IsDateOrTime],
'transactions.*.order' => 'numeric|min:0', 'transactions.*.order' => 'numeric|min:0',
// group id: // group id:
'transactions.*.transaction_journal_id' => ['numeric','exists:transaction_journals,id', new BelongsUser], 'transactions.*.transaction_journal_id' => ['numeric', 'exists:transaction_journals,id', new BelongsUser],
// currency info // currency info
'transactions.*.currency_id' => 'numeric|exists:transaction_currencies,id', 'transactions.*.currency_id' => 'numeric|exists:transaction_currencies,id',
'transactions.*.currency_code' => 'min:3|max:3|exists:transaction_currencies,code', 'transactions.*.currency_code' => 'min:3|max:3|exists:transaction_currencies,code',
'transactions.*.foreign_currency_id' => 'nullable|numeric|exists:transaction_currencies,id', 'transactions.*.foreign_currency_id' => 'nullable|numeric|exists:transaction_currencies,id',
'transactions.*.foreign_currency_code' => 'nullable|min:3|max:3|exists:transaction_currencies,code', 'transactions.*.foreign_currency_code' => 'nullable|min:3|max:3|exists:transaction_currencies,code',
// amount // amount
'transactions.*.amount' => 'numeric|gt:0|max:100000000000', 'transactions.*.amount' => 'numeric|gt:0|max:100000000000',
'transactions.*.foreign_amount' => 'nullable|numeric|gte:0', 'transactions.*.foreign_amount' => 'nullable|numeric|gte:0',
// description // description
'transactions.*.description' => 'nullable|between:1,1000', 'transactions.*.description' => 'nullable|between:1,1000',
// source of transaction // source of transaction
'transactions.*.source_id' => ['numeric', 'nullable', new BelongsUser], 'transactions.*.source_id' => ['numeric', 'nullable', new BelongsUser],
'transactions.*.source_name' => 'between:1,255|nullable', 'transactions.*.source_name' => 'between:1,255|nullable',
// destination of transaction // destination of transaction
'transactions.*.destination_id' => ['numeric', 'nullable', new BelongsUser], 'transactions.*.destination_id' => ['numeric', 'nullable', new BelongsUser],
'transactions.*.destination_name' => 'between:1,255|nullable', 'transactions.*.destination_name' => 'between:1,255|nullable',
// budget, category, bill and piggy // budget, category, bill and piggy
'transactions.*.budget_id' => ['mustExist:budgets,id', new BelongsUser], 'transactions.*.budget_id' => ['mustExist:budgets,id', new BelongsUser],
'transactions.*.budget_name' => ['between:1,255', 'nullable', new BelongsUser], 'transactions.*.budget_name' => ['between:1,255', 'nullable', new BelongsUser],
'transactions.*.category_id' => ['mustExist:categories,id', new BelongsUser], 'transactions.*.category_id' => ['mustExist:categories,id', new BelongsUser],
'transactions.*.category_name' => 'between:1,255|nullable', 'transactions.*.category_name' => 'between:1,255|nullable',
'transactions.*.bill_id' => ['numeric', 'nullable', 'mustExist:bills,id', new BelongsUser], 'transactions.*.bill_id' => ['numeric', 'nullable', 'mustExist:bills,id', new BelongsUser],
'transactions.*.bill_name' => ['between:1,255', 'nullable', new BelongsUser], 'transactions.*.bill_name' => ['between:1,255', 'nullable', new BelongsUser],
// other interesting fields // other interesting fields
'transactions.*.reconciled' => [new IsBoolean], 'transactions.*.reconciled' => [new IsBoolean],
'transactions.*.notes' => 'min:1,max:50000|nullable', 'transactions.*.notes' => 'min:1,max:50000|nullable',
'transactions.*.tags' => 'between:0,255', 'transactions.*.tags' => 'between:0,255',
// meta info fields // meta info fields
'transactions.*.internal_reference' => 'min:1,max:255|nullable', 'transactions.*.internal_reference' => 'min:1,max:255|nullable',
'transactions.*.external_id' => 'min:1,max:255|nullable', 'transactions.*.external_id' => 'min:1,max:255|nullable',
'transactions.*.recurrence_id' => 'min:1,max:255|nullable', 'transactions.*.recurrence_id' => 'min:1,max:255|nullable',
'transactions.*.bunq_payment_id' => 'min:1,max:255|nullable', 'transactions.*.bunq_payment_id' => 'min:1,max:255|nullable',
'transactions.*.external_uri' => 'min:1,max:255|nullable|url', 'transactions.*.external_uri' => 'min:1,max:255|nullable|url',
// SEPA fields: // SEPA fields:
'transactions.*.sepa_cc' => 'min:1,max:255|nullable', 'transactions.*.sepa_cc' => 'min:1,max:255|nullable',
'transactions.*.sepa_ct_op' => 'min:1,max:255|nullable', 'transactions.*.sepa_ct_op' => 'min:1,max:255|nullable',
'transactions.*.sepa_ct_id' => 'min:1,max:255|nullable', 'transactions.*.sepa_ct_id' => 'min:1,max:255|nullable',
'transactions.*.sepa_db' => 'min:1,max:255|nullable', 'transactions.*.sepa_db' => 'min:1,max:255|nullable',
'transactions.*.sepa_country' => 'min:1,max:255|nullable', 'transactions.*.sepa_country' => 'min:1,max:255|nullable',
'transactions.*.sepa_ep' => 'min:1,max:255|nullable', 'transactions.*.sepa_ep' => 'min:1,max:255|nullable',
'transactions.*.sepa_ci' => 'min:1,max:255|nullable', 'transactions.*.sepa_ci' => 'min:1,max:255|nullable',
'transactions.*.sepa_batch_id' => 'min:1,max:255|nullable', 'transactions.*.sepa_batch_id' => 'min:1,max:255|nullable',
// dates // dates
'transactions.*.interest_date' => 'date|nullable', 'transactions.*.interest_date' => 'date|nullable',
'transactions.*.book_date' => 'date|nullable', 'transactions.*.book_date' => 'date|nullable',
'transactions.*.process_date' => 'date|nullable', 'transactions.*.process_date' => 'date|nullable',
'transactions.*.due_date' => 'date|nullable', 'transactions.*.due_date' => 'date|nullable',
'transactions.*.payment_date' => 'date|nullable', 'transactions.*.payment_date' => 'date|nullable',
'transactions.*.invoice_date' => 'date|nullable', 'transactions.*.invoice_date' => 'date|nullable',
]; ];
} }

View File

@ -79,7 +79,17 @@ class GroupUpdateService
$existing = $transactionGroup->transactionJournals->pluck('id')->toArray(); $existing = $transactionGroup->transactionJournals->pluck('id')->toArray();
$updated = $this->updateTransactions($transactionGroup, $transactions); $updated = $this->updateTransactions($transactionGroup, $transactions);
$result = array_diff($existing, $updated); Log::debug('Array of updated IDs: ', $updated);
if(0 === count($updated)) {
Log::error('There were no transactions updated or created. Will not delete anything.');
$transactionGroup->refresh();
app('preferences')->mark();
return $transactionGroup;
}
$result = array_diff($existing, $updated);
Log::debug('Result of DIFF: ', $result);
if (count($result) > 0) { if (count($result) > 0) {
/** @var string $deletedId */ /** @var string $deletedId */
foreach ($result as $deletedId) { foreach ($result as $deletedId) {
@ -127,6 +137,7 @@ class GroupUpdateService
*/ */
private function updateTransactions(TransactionGroup $transactionGroup, array $transactions): array private function updateTransactions(TransactionGroup $transactionGroup, array $transactions): array
{ {
// updated or created transaction journals:
$updated = []; $updated = [];
/** /**
* @var int $index * @var int $index
@ -151,8 +162,14 @@ class GroupUpdateService
} }
} }
Log::debug('Call createTransactionJournal'); Log::debug('Call createTransactionJournal');
$this->createTransactionJournal($transactionGroup, $transaction); $newJournal = $this->createTransactionJournal($transactionGroup, $transaction);
Log::debug('Done calling createTransactionJournal'); Log::debug('Done calling createTransactionJournal');
if (null !== $newJournal) {
$updated[] = $newJournal->id;
}
if (null === $newJournal) {
Log::error('createTransactionJournal returned NULL, indicating something went wrong.');
}
} }
if (null !== $journal) { if (null !== $journal) {
Log::debug('Call updateTransactionJournal'); Log::debug('Call updateTransactionJournal');
@ -169,12 +186,13 @@ class GroupUpdateService
* @param TransactionGroup $transactionGroup * @param TransactionGroup $transactionGroup
* @param array $data * @param array $data
* *
* @return TransactionJournal|null
*
* @throws FireflyException * @throws FireflyException
* @throws DuplicateTransactionException * @throws DuplicateTransactionException
*/ */
private function createTransactionJournal(TransactionGroup $transactionGroup, array $data): void private function createTransactionJournal(TransactionGroup $transactionGroup, array $data): ?TransactionJournal
{ {
$submission = [ $submission = [
'transactions' => [ 'transactions' => [
$data, $data,
@ -195,6 +213,11 @@ class GroupUpdateService
$transactionGroup->transactionJournals()->save($journal); $transactionGroup->transactionJournals()->save($journal);
} }
); );
if (0 === $collection->count()) {
return null;
}
return $collection->first();
} }
} }

View File

@ -83,6 +83,9 @@ trait GroupValidation
} }
} }
/**
* @param Validator $validator
*/
protected function preventNoAccountInfo(Validator $validator): void protected function preventNoAccountInfo(Validator $validator): void
{ {
$transactions = $this->getTransactionsArray($validator); $transactions = $this->getTransactionsArray($validator);
@ -92,7 +95,7 @@ trait GroupValidation
/** @var array $transaction */ /** @var array $transaction */
foreach ($transactions as $transaction) { foreach ($transactions as $transaction) {
foreach($keys as $key) { foreach($keys as $key) {
if(array_key_exists($key, $transaction) && '' !== $transaction[$key]) { if(array_key_exists($key, $transaction) && '' !== (string) $transaction[$key]) {
$hasAccountInfo = true; $hasAccountInfo = true;
} }
} }