Simplify method.

This commit is contained in:
James Cole
2020-03-18 20:55:31 +01:00
parent 2c7891bfb2
commit 3e74fce885

View File

@@ -153,56 +153,14 @@ class JournalUpdateService
$this->transactionJournal->save(); $this->transactionJournal->save();
$this->transactionJournal->refresh(); $this->transactionJournal->refresh();
// update category $this->updateCategory();
if ($this->hasFields(['category_id', 'category_name'])) { $this->updateBudget();
Log::debug('Will update category.'); $this->updateTags();
$this->updateNotes();
$this->storeCategory($this->transactionJournal, new NullArrayObject($this->data)); $this->updateMeta();
} $this->updateCurrency();
// update budget $this->updateAmount();
if ($this->hasFields(['budget_id', 'budget_name'])) { $this->updateForeignAmount();
Log::debug('Will update budget.');
$this->storeBudget($this->transactionJournal, new NullArrayObject($this->data));
}
// update tags
if ($this->hasFields(['tags'])) {
Log::debug('Will update tags.');
$tags = $this->data['tags'] ?? null;
$this->storeTags($this->transactionJournal, $tags);
}
// update notes.
if ($this->hasFields(['notes'])) {
$notes = '' === (string)$this->data['notes'] ? null : $this->data['notes'];
$this->storeNotes($this->transactionJournal, $notes);
}
// update meta fields.
// first string
if ($this->hasFields($this->metaString)) {
Log::debug('Meta string fields are present.');
$this->updateMetaFields();
}
// then date fields.
if ($this->hasFields($this->metaDate)) {
Log::debug('Meta date fields are present.');
$this->updateMetaDateFields();
}
// update transactions.
if ($this->hasFields(['currency_id', 'currency_code'])) {
$this->updateCurrency();
}
if ($this->hasFields(['amount'])) {
$this->updateAmount();
}
// amount, foreign currency.
if ($this->hasFields(['foreign_currency_id', 'foreign_currency_code', 'foreign_amount'])) {
$this->updateForeignAmount();
}
// TODO update hash // TODO update hash
@@ -477,6 +435,10 @@ class JournalUpdateService
*/ */
private function updateAmount(): void private function updateAmount(): void
{ {
if (!$this->hasFields(['amount'])) {
return;
}
$value = $this->data['amount'] ?? ''; $value = $this->data['amount'] ?? '';
try { try {
$amount = $this->getAmount($value); $amount = $this->getAmount($value);
@@ -513,19 +475,48 @@ class JournalUpdateService
) )
&& TransactionType::WITHDRAWAL === $type && TransactionType::WITHDRAWAL === $type
) { ) {
$billId = (int)($this->data['bill_id'] ?? 0); $billId = (int) ($this->data['bill_id'] ?? 0);
$billName = (string)($this->data['bill_name'] ?? ''); $billName = (string) ($this->data['bill_name'] ?? '');
$bill = $this->billRepository->findBill($billId, $billName); $bill = $this->billRepository->findBill($billId, $billName);
$this->transactionJournal->bill_id = null === $bill ? null : $bill->id; $this->transactionJournal->bill_id = null === $bill ? null : $bill->id;
Log::debug('Updated bill ID'); Log::debug('Updated bill ID');
} }
} }
/**
*
*/
private function updateBudget(): void
{
// update budget
if ($this->hasFields(['budget_id', 'budget_name'])) {
Log::debug('Will update budget.');
$this->storeBudget($this->transactionJournal, new NullArrayObject($this->data));
}
}
/**
*
*/
private function updateCategory(): void
{
// update category
if ($this->hasFields(['category_id', 'category_name'])) {
Log::debug('Will update category.');
$this->storeCategory($this->transactionJournal, new NullArrayObject($this->data));
}
}
/** /**
* *
*/ */
private function updateCurrency(): void private function updateCurrency(): void
{ {
// update transactions.
if (!$this->hasFields(['currency_id', 'currency_code'])) {
return;
}
$currencyId = $this->data['currency_id'] ?? null; $currencyId = $this->data['currency_id'] ?? null;
$currencyCode = $this->data['currency_code'] ?? null; $currencyCode = $this->data['currency_code'] ?? null;
$currency = $this->currencyRepository->findCurrency($currencyId, $currencyCode); $currency = $this->currencyRepository->findCurrency($currencyId, $currencyCode);
@@ -568,6 +559,11 @@ class JournalUpdateService
*/ */
private function updateForeignAmount(): void private function updateForeignAmount(): void
{ {
// amount, foreign currency.
if (!$this->hasFields(['foreign_currency_id', 'foreign_currency_code', 'foreign_amount'])) {
return;
}
$amount = $this->data['foreign_amount'] ?? null; $amount = $this->data['foreign_amount'] ?? null;
$foreignAmount = $this->getForeignAmount($amount); $foreignAmount = $this->getForeignAmount($amount);
$source = $this->getSourceTransaction(); $source = $this->getSourceTransaction();
@@ -622,6 +618,25 @@ class JournalUpdateService
$this->destinationTransaction->refresh(); $this->destinationTransaction->refresh();
} }
/**
*
*/
private function updateMeta(): void
{
// update meta fields.
// first string
if ($this->hasFields($this->metaString)) {
Log::debug('Meta string fields are present.');
$this->updateMetaFields();
}
// then date fields.
if ($this->hasFields($this->metaDate)) {
Log::debug('Meta date fields are present.');
$this->updateMetaDateFields();
}
}
/** /**
* *
*/ */
@@ -672,6 +687,30 @@ class JournalUpdateService
} }
} }
/**
*
*/
private function updateNotes(): void
{
// update notes.
if ($this->hasFields(['notes'])) {
$notes = '' === (string) $this->data['notes'] ? null : $this->data['notes'];
$this->storeNotes($this->transactionJournal, $notes);
}
}
/**
*
*/
private function updateTags(): void
{
if ($this->hasFields(['tags'])) {
Log::debug('Will update tags.');
$tags = $this->data['tags'] ?? null;
$this->storeTags($this->transactionJournal, $tags);
}
}
/** /**
* Updates journal transaction type. * Updates journal transaction type.
*/ */