Push empty object {} and recurrence doesn't change. #2483

This commit is contained in:
James Cole 2019-08-26 19:09:55 +02:00
parent f9dc58c3a8
commit 2b40b60d01
7 changed files with 346 additions and 227 deletions

View File

@ -274,7 +274,7 @@ class RecurrenceController extends Controller
*/ */
public function update(RecurrenceUpdateRequest $request, Recurrence $recurrence): JsonResponse public function update(RecurrenceUpdateRequest $request, Recurrence $recurrence): JsonResponse
{ {
$data = $request->getAllRecurrenceData(); $data = $request->getAll();
$category = $this->repository->update($recurrence, $data); $category = $this->repository->update($recurrence, $data);
$manager = new Manager(); $manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';

View File

@ -74,8 +74,8 @@ class RecurrenceStoreRequest extends Request
'apply_rules' => $applyRules, 'apply_rules' => $applyRules,
'active' => $active, 'active' => $active,
], ],
'transactions' => $this->getRecurrenceTransactionData(), 'transactions' => $this->getTransactionData(),
'repetitions' => $this->getRecurrenceRepetitionData(), 'repetitions' => $this->getRepetitionData(),
]; ];
return $return; return $return;
@ -120,10 +120,9 @@ class RecurrenceStoreRequest extends Request
'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.*.piggy_bank_name' => ['between:1,255', 'nullable', new BelongsUser],
'transactions.*.piggy_bank_id' => ['numeric', 'mustExist:piggy_banks,id', new BelongsUser], 'transactions.*.piggy_bank_id' => ['numeric', 'mustExist:piggy_banks,id', new BelongsUser],
'transactions.*.piggy_bank_name' => ['between:1,255', 'nullable', new BelongsUser],
'transactions.*.tags' => 'between:1,64000', 'transactions.*.tags' => 'between:1,64000',
]; ];
@ -149,4 +148,76 @@ class RecurrenceStoreRequest extends Request
} }
); );
} }
/**
* Returns the repetition data as it is found in the submitted data.
*
* @return array
*/
private function getRepetitionData(): array
{
$return = [];
// repetition data:
/** @var array $repetitions */
$repetitions = $this->get('repetitions');
if (null === $repetitions) {
return [];
}
/** @var array $repetition */
foreach ($repetitions as $repetition) {
$return[] = [
'type' => $repetition['type'],
'moment' => $repetition['moment'],
'skip' => (int)$repetition['skip'],
'weekend' => (int)$repetition['weekend'],
];
}
return $return;
}
/**
* Returns the transaction data as it is found in the submitted data. It's a complex method according to code
* standards but it just has a lot of ??-statements because of the fields that may or may not exist.
*
* @return array
*/
private function getTransactionData(): array
{
$return = [];
// transaction data:
/** @var array $transactions */
$transactions = $this->get('transactions');
if (null === $transactions) {
return [];
}
/** @var array $transaction */
foreach ($transactions as $transaction) {
$return[] = [
'amount' => $transaction['amount'],
'currency_id' => isset($transaction['currency_id']) ? (int)$transaction['currency_id'] : null,
'currency_code' => $transaction['currency_code'] ?? null,
'foreign_amount' => $transaction['foreign_amount'] ?? null,
'foreign_currency_id' => isset($transaction['foreign_currency_id']) ? (int)$transaction['foreign_currency_id'] : null,
'foreign_currency_code' => $transaction['foreign_currency_code'] ?? null,
'source_id' => isset($transaction['source_id']) ? (int)$transaction['source_id'] : null,
'source_name' => isset($transaction['source_name']) ? (string)$transaction['source_name'] : null,
'destination_id' => isset($transaction['destination_id']) ? (int)$transaction['destination_id'] : null,
'destination_name' => isset($transaction['destination_name']) ? (string)$transaction['destination_name'] : null,
'description' => $transaction['description'],
'type' => $this->string('type'),
// new and updated fields:
'piggy_bank_id' => isset($transaction['piggy_bank_id']) ? (int)$transaction['piggy_bank_id'] : null,
'piggy_bank_name' => $transaction['piggy_bank_name'] ?? null,
'tags' => $transaction['tags'],
'budget_id' => isset($transaction['budget_id']) ? (int)$transaction['budget_id'] : null,
'budget_name' => $transaction['budget_name'] ?? null,
'category_id' => isset($transaction['category_id']) ? (int)$transaction['category_id'] : null,
'category_name' => $transaction['category_name'] ?? null,
];
}
return $return;
}
} }

View File

@ -48,6 +48,39 @@ class RecurrenceUpdateRequest extends Request
return auth()->check(); return auth()->check();
} }
/**
* Get all data from the request.
*
* @return array
*/
public function getAll(): array
{
$active = null;
$applyRules = null;
if (null !== $this->get('active')) {
$active = $this->boolean('active');
}
if (null !== $this->get('apply_rules')) {
$applyRules = $this->boolean('apply_rules');
}
$return = [
'recurrence' => [
'type' => $this->nullableString('type'),
'title' => $this->nullableString('title'),
'description' => $this->nullableString('description'),
'first_date' => $this->date('first_date'),
'repeat_until' => $this->date('repeat_until'),
'nr_of_repetitions' => $this->nullableInteger('nr_of_repetitions'),
'apply_rules' => $applyRules,
'active' => $active,
],
'transactions' => $this->getTransactionData(),
'repetitions' => $this->getRepetitionData(),
];
return $return;
}
/** /**
* The rules that the incoming request must be matched against. * The rules that the incoming request must be matched against.
* *
@ -61,15 +94,15 @@ class RecurrenceUpdateRequest extends Request
$first->subDay(); $first->subDay();
return [ return [
'type' => 'required|in:withdrawal,transfer,deposit', 'type' => 'in:withdrawal,transfer,deposit',
'title' => sprintf('required|between:1,255|uniqueObjectForUser:recurrences,title,%d', $recurrence->id), 'title' => sprintf('between:1,255|uniqueObjectForUser:recurrences,title,%d', $recurrence->id),
'description' => 'between:1,65000', 'description' => 'between:1,65000',
'first_date' => sprintf('required|date|after:%s', $first->format('Y-m-d')), 'first_date' => sprintf('date|after:%s', $first->format('Y-m-d')),
'apply_rules' => [new IsBoolean], 'apply_rules' => [new IsBoolean],
'active' => [new IsBoolean], 'active' => [new IsBoolean],
'repeat_until' => sprintf('date|after:%s', $first->format('Y-m-d')), 'repeat_until' => sprintf('date|after:%s', $first->format('Y-m-d')),
'nr_of_repetitions' => 'numeric|between:1,31', 'nr_of_repetitions' => 'numeric|between:1,31',
'repetitions.*.type' => 'required|in:daily,weekly,ndom,monthly,yearly', 'repetitions.*.type' => 'in:daily,weekly,ndom,monthly,yearly',
'repetitions.*.moment' => 'between:0,10', 'repetitions.*.moment' => 'between:0,10',
'repetitions.*.skip' => 'required|numeric|between:0,31', 'repetitions.*.skip' => 'required|numeric|between:0,31',
'repetitions.*.weekend' => 'required|numeric|min:1|max:4', 'repetitions.*.weekend' => 'required|numeric|min:1|max:4',
@ -87,11 +120,13 @@ class RecurrenceUpdateRequest extends Request
// new and updated fields: // new and updated fields:
'transactions.*.budget_id' => ['mustExist:budgets,id', new BelongsUser], 'transactions.*.budget_id' => ['mustExist:budgets,id', new BelongsUser],
'transactions.*.budget_name' => 'between:1,255|nullable', '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.*.tags' => 'between:1,64000',
'transactions.*.piggy_bank_id' => ['numeric', 'mustExist:piggy_banks,id', new BelongsUser], 'transactions.*.piggy_bank_id' => ['numeric', 'mustExist:piggy_banks,id', new BelongsUser],
'transactions.*.piggy_bank_name' => ['between:1,255', 'nullable', new BelongsUser],
'transactions.*.tags' => 'between:1,64000',
]; ];
} }
@ -106,8 +141,8 @@ class RecurrenceUpdateRequest extends Request
{ {
$validator->after( $validator->after(
function (Validator $validator) { function (Validator $validator) {
$this->validateOneTransaction($validator); $this->validateOneRecurrenceTransactionUpdate($validator);
$this->validateOneRepetition($validator); $this->validateOneRepetitionUpdate($validator);
$this->validateRecurrenceRepetition($validator); $this->validateRecurrenceRepetition($validator);
$this->validateRepetitionMoment($validator); $this->validateRepetitionMoment($validator);
$this->validateForeignCurrencyInformation($validator); $this->validateForeignCurrencyInformation($validator);
@ -115,4 +150,76 @@ class RecurrenceUpdateRequest extends Request
} }
); );
} }
/**
* Returns the repetition data as it is found in the submitted data.
*
* @return array|null
*/
private function getRepetitionData(): ?array
{
$return = [];
// repetition data:
/** @var array $repetitions */
$repetitions = $this->get('repetitions');
if (null === $repetitions) {
return null;
}
/** @var array $repetition */
foreach ($repetitions as $repetition) {
$return[] = [
'type' => $repetition['type'],
'moment' => $repetition['moment'],
'skip' => (int)$repetition['skip'],
'weekend' => (int)$repetition['weekend'],
];
}
return $return;
}
/**
* Returns the transaction data as it is found in the submitted data. It's a complex method according to code
* standards but it just has a lot of ??-statements because of the fields that may or may not exist.
*
* @return array|null
*/
private function getTransactionData(): ?array
{
$return = [];
// transaction data:
/** @var array $transactions */
$transactions = $this->get('transactions');
if (null === $transactions) {
return null;
}
/** @var array $transaction */
foreach ($transactions as $transaction) {
$return[] = [
'amount' => $transaction['amount'],
'currency_id' => isset($transaction['currency_id']) ? (int)$transaction['currency_id'] : null,
'currency_code' => $transaction['currency_code'] ?? null,
'foreign_amount' => $transaction['foreign_amount'] ?? null,
'foreign_currency_id' => isset($transaction['foreign_currency_id']) ? (int)$transaction['foreign_currency_id'] : null,
'foreign_currency_code' => $transaction['foreign_currency_code'] ?? null,
'source_id' => isset($transaction['source_id']) ? (int)$transaction['source_id'] : null,
'source_name' => isset($transaction['source_name']) ? (string)$transaction['source_name'] : null,
'destination_id' => isset($transaction['destination_id']) ? (int)$transaction['destination_id'] : null,
'destination_name' => isset($transaction['destination_name']) ? (string)$transaction['destination_name'] : null,
'description' => $transaction['description'],
'type' => $this->string('type'),
// new and updated fields:
'piggy_bank_id' => isset($transaction['piggy_bank_id']) ? (int)$transaction['piggy_bank_id'] : null,
'piggy_bank_name' => $transaction['piggy_bank_name'] ?? null,
'tags' => $transaction['tags'],
'budget_id' => isset($transaction['budget_id']) ? (int)$transaction['budget_id'] : null,
'budget_name' => $transaction['budget_name'] ?? null,
'category_id' => isset($transaction['category_id']) ? (int)$transaction['category_id'] : null,
'category_name' => $transaction['category_name'] ?? null,
];
}
return $return;
}
} }

View File

@ -79,110 +79,4 @@ class Request extends FireflyIIIRequest
return $data; return $data;
} }
/**
* Get all data from the request.
*
* @return array
*/
public function getAllRecurrenceData(): array
{
$active = true;
$applyRules = true;
if (null !== $this->get('active')) {
$active = $this->boolean('active');
}
if (null !== $this->get('apply_rules')) {
$applyRules = $this->boolean('apply_rules');
}
$return = [
'recurrence' => [
'type' => $this->string('type'),
'title' => $this->string('title'),
'description' => $this->string('description'),
'first_date' => $this->date('first_date'),
'repeat_until' => $this->date('repeat_until'),
'repetitions' => $this->integer('nr_of_repetitions'),
'apply_rules' => $applyRules,
'active' => $active,
],
'meta' => [
'piggy_bank_id' => $this->integer('piggy_bank_id'),
'piggy_bank_name' => $this->string('piggy_bank_name'),
'tags' => $this->get('tags'),
],
'transactions' => $this->getRecurrenceTransactionData(),
'repetitions' => $this->getRecurrenceRepetitionData(),
];
return $return;
}
/**
* Returns the repetition data as it is found in the submitted data.
*
* @return array
*/
protected function getRecurrenceRepetitionData(): array
{
$return = [];
// repetition data:
/** @var array $repetitions */
$repetitions = $this->get('repetitions');
/** @var array $repetition */
foreach ($repetitions as $repetition) {
$return[] = [
'type' => $repetition['type'],
'moment' => $repetition['moment'],
'skip' => (int)$repetition['skip'],
'weekend' => (int)$repetition['weekend'],
];
}
return $return;
}
/**
* Returns the transaction data as it is found in the submitted data. It's a complex method according to code
* standards but it just has a lot of ??-statements because of the fields that may or may not exist.
*
* @return array
*/
protected function getRecurrenceTransactionData(): array
{
$return = [];
// transaction data:
/** @var array $transactions */
$transactions = $this->get('transactions');
/** @var array $transaction */
foreach ($transactions as $transaction) {
$return[] = [
'amount' => $transaction['amount'],
'currency_id' => isset($transaction['currency_id']) ? (int)$transaction['currency_id'] : null,
'currency_code' => $transaction['currency_code'] ?? null,
'foreign_amount' => $transaction['foreign_amount'] ?? null,
'foreign_currency_id' => isset($transaction['foreign_currency_id']) ? (int)$transaction['foreign_currency_id'] : null,
'foreign_currency_code' => $transaction['foreign_currency_code'] ?? null,
'source_id' => isset($transaction['source_id']) ? (int)$transaction['source_id'] : null,
'source_name' => isset($transaction['source_name']) ? (string)$transaction['source_name'] : null,
'destination_id' => isset($transaction['destination_id']) ? (int)$transaction['destination_id'] : null,
'destination_name' => isset($transaction['destination_name']) ? (string)$transaction['destination_name'] : null,
'description' => $transaction['description'],
'type' => $this->string('type'),
// new and updated fields:
'piggy_bank_id' => isset($transaction['piggy_bank_id']) ? (int)$transaction['piggy_bank_id'] : null,
'piggy_bank_name' => $transaction['piggy_bank_name'] ?? null,
'tags' => $transaction['tags'],
'budget_id' => isset($transaction['budget_id']) ? (int)$transaction['budget_id'] : null,
'budget_name' => $transaction['budget_name'] ?? null,
'category_id' => isset($transaction['category_id']) ? (int)$transaction['category_id'] : null,
'category_name' => $transaction['category_name'] ?? null,
];
}
return $return;
}
} }

View File

@ -31,6 +31,7 @@ use FireflyIII\User;
/** /**
* Class RecurrenceUpdateService * Class RecurrenceUpdateService
*
* @codeCoverageIgnore * @codeCoverageIgnore
*/ */
class RecurrenceUpdateService class RecurrenceUpdateService
@ -52,7 +53,10 @@ class RecurrenceUpdateService
public function update(Recurrence $recurrence, array $data): Recurrence public function update(Recurrence $recurrence, array $data): Recurrence
{ {
$this->user = $recurrence->user; $this->user = $recurrence->user;
$transactionType = $this->findTransactionType(ucfirst($data['recurrence']['type'])); $transactionType = $recurrence->transactionType;
if (isset($data['recurrence']['type'])) {
$transactionType = $this->findTransactionType(ucfirst($data['recurrence']['type']));
}
// update basic fields first: // update basic fields first:
$recurrence->transaction_type_id = $transactionType->id; $recurrence->transaction_type_id = $transactionType->id;
$recurrence->title = $data['recurrence']['title'] ?? $recurrence->title; $recurrence->title = $data['recurrence']['title'] ?? $recurrence->title;
@ -75,15 +79,19 @@ class RecurrenceUpdateService
$recurrence->save(); $recurrence->save();
// update all meta data: // update all meta data:
$this->updateMetaData($recurrence, $data); //$this->updateMetaData($recurrence, $data);
// update all repetitions // update all repetitions
$this->deleteRepetitions($recurrence); if (null !== $data['repetitions']) {
$this->createRepetitions($recurrence, $data['repetitions'] ?? []); $this->deleteRepetitions($recurrence);
$this->createRepetitions($recurrence, $data['repetitions'] ?? []);
}
// update all transactions (and associated meta-data); // update all transactions (and associated meta-data);
$this->deleteTransactions($recurrence); if (null !== $data['transactions']) {
$this->createTransactions($recurrence, $data['transactions'] ?? []); $this->deleteTransactions($recurrence);
$this->createTransactions($recurrence, $data['transactions'] ?? []);
}
return $recurrence; return $recurrence;
} }

View File

@ -52,6 +52,24 @@ trait RecurrenceValidation
} }
} }
/**
* Adds an error to the validator when there are no repetitions in the array of data.
*
* @param Validator $validator
*/
public function validateOneRepetitionUpdate(Validator $validator): void
{
$data = $validator->getData();
$repetitions = $data['repetitions'] ?? null;
if (null === $repetitions) {
return;
}
// need at least one transaction
if (0 === count($repetitions)) {
$validator->errors()->add('repetitions', (string)trans('validation.at_least_one_repetition'));
}
}
/** /**
* Validates that the recurrence has valid repetition information. It either doesn't stop, * Validates that the recurrence has valid repetition information. It either doesn't stop,
* or stops after X times or at X date. Not both of them., * or stops after X times or at X date. Not both of them.,

View File

@ -81,6 +81,51 @@ trait TransactionValidation
} }
} }
/**
* Validates the given account information. Switches on given transaction type.
*
* @param Validator $validator
*/
public function validateAccountInformationUpdate(Validator $validator): void
{
$data = $validator->getData();
$transactions = $data['transactions'] ?? [];
/** @var AccountValidator $accountValidator */
$accountValidator = app(AccountValidator::class);
foreach ($transactions as $index => $transaction) {
$originalType = $this->getOriginalType($transaction['transaction_journal_id'] ?? 0);
$originalData = $this->getOriginalData($transaction['transaction_journal_id'] ?? 0);
$transactionType = $transaction['type'] ?? $originalType;
$accountValidator->setTransactionType($transactionType);
// validate source account.
$sourceId = isset($transaction['source_id']) ? (int)$transaction['source_id'] : $originalData['source_id'];
$sourceName = $transaction['source_name'] ?? $originalData['source_name'];
$validSource = $accountValidator->validateSource($sourceId, $sourceName);
// do something with result:
if (false === $validSource) {
$validator->errors()->add(sprintf('transactions.%d.source_id', $index), $accountValidator->sourceError);
$validator->errors()->add(sprintf('transactions.%d.source_name', $index), $accountValidator->sourceError);
continue;
}
// validate destination account
$destinationId = isset($transaction['destination_id']) ? (int)$transaction['destination_id'] : $originalData['destination_id'];
$destinationName = $transaction['destination_name'] ?? $originalData['destination_name'];
$validDestination = $accountValidator->validateDestination($destinationId, $destinationName);
// do something with result:
if (false === $validDestination) {
$validator->errors()->add(sprintf('transactions.%d.destination_id', $index), $accountValidator->destError);
$validator->errors()->add(sprintf('transactions.%d.destination_name', $index), $accountValidator->destError);
continue;
}
}
}
/** /**
* Adds an error to the "description" field when the user has submitted no descriptions and no * Adds an error to the "description" field when the user has submitted no descriptions and no
* journal description. * journal description.
@ -151,13 +196,13 @@ trait TransactionValidation
* *
* @param Validator $validator * @param Validator $validator
*/ */
public function validateOneTransaction(Validator $validator): void public function validateOneRecurrenceTransaction(Validator $validator): void
{ {
$data = $validator->getData(); $data = $validator->getData();
$transactions = $data['transactions'] ?? []; $transactions = $data['transactions'] ?? [];
// need at least one transaction // need at least one transaction
if (0 === count($transactions)) { if (0 === count($transactions)) {
$validator->errors()->add('transactions.0.description', (string)trans('validation.at_least_one_transaction')); $validator->errors()->add('transactions', (string)trans('validation.at_least_one_transaction'));
} }
} }
@ -166,13 +211,31 @@ trait TransactionValidation
* *
* @param Validator $validator * @param Validator $validator
*/ */
public function validateOneRecurrenceTransaction(Validator $validator): void public function validateOneRecurrenceTransactionUpdate(Validator $validator): void
{
$data = $validator->getData();
$transactions = $data['transactions'] ?? null;
if (null === $transactions) {
return;
}
// need at least one transaction
if (0 === count($transactions)) {
$validator->errors()->add('transactions', (string)trans('validation.at_least_one_transaction'));
}
}
/**
* Adds an error to the validator when there are no transactions in the array of data.
*
* @param Validator $validator
*/
public function validateOneTransaction(Validator $validator): void
{ {
$data = $validator->getData(); $data = $validator->getData();
$transactions = $data['transactions'] ?? []; $transactions = $data['transactions'] ?? [];
// need at least one transaction // need at least one transaction
if (0 === count($transactions)) { if (0 === count($transactions)) {
$validator->errors()->add('transactions', (string)trans('validation.at_least_one_transaction')); $validator->errors()->add('transactions.0.description', (string)trans('validation.at_least_one_transaction'));
} }
} }
@ -228,48 +291,64 @@ trait TransactionValidation
} }
/** /**
* Validates the given account information. Switches on given transaction type. * @param array $array
* *
* @param Validator $validator * @return bool
*/ */
public function validateAccountInformationUpdate(Validator $validator): void private function arrayEqual(array $array): bool
{ {
$data = $validator->getData(); return 1 === count(array_unique($array));
$transactions = $data['transactions'] ?? []; }
/** @var AccountValidator $accountValidator */ /**
$accountValidator = app(AccountValidator::class); * @param int $journalId
*
foreach ($transactions as $index => $transaction) { * @return array
$originalType = $this->getOriginalType($transaction['transaction_journal_id'] ?? 0); */
$originalData = $this->getOriginalData($transaction['transaction_journal_id'] ?? 0); private function getOriginalData(int $journalId): array
$transactionType = $transaction['type'] ?? $originalType; {
$accountValidator->setTransactionType($transactionType); $return = [
'source_id' => 0,
// validate source account. 'source_name' => '',
$sourceId = isset($transaction['source_id']) ? (int)$transaction['source_id'] : $originalData['source_id']; 'destination_id' => 0,
$sourceName = $transaction['source_name'] ?? $originalData['source_name']; 'destination_name' => '',
$validSource = $accountValidator->validateSource($sourceId, $sourceName); ];
if (0 === $journalId) {
// do something with result: return $return;
if (false === $validSource) {
$validator->errors()->add(sprintf('transactions.%d.source_id', $index), $accountValidator->sourceError);
$validator->errors()->add(sprintf('transactions.%d.source_name', $index), $accountValidator->sourceError);
continue;
}
// validate destination account
$destinationId = isset($transaction['destination_id']) ? (int)$transaction['destination_id'] : $originalData['destination_id'];
$destinationName = $transaction['destination_name'] ?? $originalData['destination_name'];
$validDestination = $accountValidator->validateDestination($destinationId, $destinationName);
// do something with result:
if (false === $validDestination) {
$validator->errors()->add(sprintf('transactions.%d.destination_id', $index), $accountValidator->destError);
$validator->errors()->add(sprintf('transactions.%d.destination_name', $index), $accountValidator->destError);
continue;
}
} }
/** @var Transaction $source */
$source = Transaction::where('transaction_journal_id', $journalId)->where('amount', '<', 0)->with(['account'])->first();
if (null !== $source) {
$return['source_id'] = $source->account_id;
$return['source_name'] = $source->account->name;
}
/** @var Transaction $destination */
$destination = Transaction::where('transaction_journal_id', $journalId)->where('amount', '>', 0)->with(['account'])->first();
if (null !== $source) {
$return['destination_id'] = $destination->account_id;
$return['destination_name'] = $destination->account->name;
}
return $return;
}
/**
* @param int $journalId
*
* @return string
*/
private function getOriginalType(int $journalId): string
{
if (0 === $journalId) {
return 'invalid';
}
/** @var TransactionJournal $journal */
$journal = TransactionJournal::with(['transactionType'])->find($journalId);
if (null !== $journal) {
return strtolower($journal->transactionType->type);
}
return 'invalid';
} }
/** /**
@ -313,7 +392,7 @@ trait TransactionValidation
} }
/** /**
* @param Validator $validator * @param Validator $validator
* @param TransactionGroup $transactionGroup * @param TransactionGroup $transactionGroup
*/ */
private function validateEqualAccountsForUpdate(Validator $validator, TransactionGroup $transactionGroup): void private function validateEqualAccountsForUpdate(Validator $validator, TransactionGroup $transactionGroup): void
@ -396,56 +475,7 @@ trait TransactionValidation
} }
/** /**
* @param int $journalId * @param Validator $validator
* @return array
*/
private function getOriginalData(int $journalId): array
{
$return = [
'source_id' => 0,
'source_name' => '',
'destination_id' => 0,
'destination_name' => '',
];
if (0 === $journalId) {
return $return;
}
/** @var Transaction $source */
$source = Transaction::where('transaction_journal_id', $journalId)->where('amount', '<', 0)->with(['account'])->first();
if (null !== $source) {
$return['source_id'] = $source->account_id;
$return['source_name'] = $source->account->name;
}
/** @var Transaction $destination */
$destination = Transaction::where('transaction_journal_id', $journalId)->where('amount', '>', 0)->with(['account'])->first();
if (null !== $source) {
$return['destination_id'] = $destination->account_id;
$return['destination_name'] = $destination->account->name;
}
return $return;
}
/**
* @param int $journalId
* @return string
*/
private function getOriginalType(int $journalId): string
{
if (0 === $journalId) {
return 'invalid';
}
/** @var TransactionJournal $journal */
$journal = TransactionJournal::with(['transactionType'])->find($journalId);
if (null !== $journal) {
return strtolower($journal->transactionType->type);
}
return 'invalid';
}
/**
* @param Validator $validator
* @param TransactionGroup $transactionGroup * @param TransactionGroup $transactionGroup
*/ */
private function validateJournalIds(Validator $validator, TransactionGroup $transactionGroup): void private function validateJournalIds(Validator $validator, TransactionGroup $transactionGroup): void
@ -464,13 +494,4 @@ trait TransactionValidation
} }
} }
} }
/**
* @param array $array
* @return bool
*/
private function arrayEqual(array $array): bool
{
return 1 === count(array_unique($array));
}
} }