Simplify methods.

This commit is contained in:
James Cole
2020-10-27 06:53:33 +01:00
parent 0fb81a6112
commit 8d84bffc2d
2 changed files with 79 additions and 52 deletions

View File

@@ -328,8 +328,7 @@ trait ModifiesPiggyBanks
*
* @return PiggyBank
*/
public function update(PiggyBank $piggyBank, array $data): PiggyBank
{
private function updateProperties(PiggyBank $piggyBank, array $data): PiggyBank {
if (array_key_exists('name', $data) && '' !== $data['name']) {
$piggyBank->name = $data['name'];
}
@@ -344,7 +343,18 @@ trait ModifiesPiggyBanks
}
$piggyBank->startdate = $data['startdate'] ?? $piggyBank->startdate;
$piggyBank->save();
return $piggyBank;
}
/**
* @param PiggyBank $piggyBank
* @param array $data
*
* @return PiggyBank
*/
public function update(PiggyBank $piggyBank, array $data): PiggyBank
{
$piggyBank = $this->updateProperties($piggyBank, $data);
$this->updateNote($piggyBank, $data['notes'] ?? '');
// update the order of the piggy bank:

View File

@@ -69,6 +69,11 @@ class BillUpdateService
$currency->enabled = true;
$currency->save();
// new values
$data['transaction_currency_name'] = $currency->name;
$bill = $this->updateBillProperties($bill, $data);
$bill->transaction_currency_id = $currency->id;
$bill->save();
// old values
$oldData = [
'name' => $bill->name,
@@ -76,36 +81,7 @@ class BillUpdateService
'amount_max' => $bill->amount_max,
'transaction_currency_name' => $bill->transactionCurrency->name,
];
// new values
$data['transaction_currency_name'] = $currency->name;
if (isset($data['name']) && '' !== (string)$data['name']) {
$bill->name = $data['name'];
}
if (isset($data['amount_min']) && '' !== (string)$data['amount_min']) {
$bill->amount_min = $data['amount_min'];
}
if (isset($data['amount_max']) && '' !== (string)$data['amount_max']) {
$bill->amount_max = $data['amount_max'];
}
if (isset($data['date']) && '' !== (string)$data['date']) {
$bill->date = $data['date'];
}
if (isset($data['repeat_freq']) && '' !== (string)$data['repeat_freq']) {
$bill->repeat_freq = $data['repeat_freq'];
}
if (isset($data['skip']) && '' !== (string)$data['skip']) {
$bill->skip = $data['skip'];
}
if (isset($data['active']) && is_bool($data['active'])) {
$bill->active = $data['active'];
}
$bill->transaction_currency_id = $currency->id;
$bill->match = 'EMPTY';
$bill->automatch = true;
$bill->save();
// update note:
if (isset($data['notes'])) {
@@ -132,6 +108,7 @@ class BillUpdateService
$bill->objectGroups()->sync([$objectGroup->id]);
$bill->save();
}
return $bill;
}
// remove if name is empty. Should be overruled by ID.
@@ -148,6 +125,7 @@ class BillUpdateService
$bill->objectGroups()->sync([$objectGroup->id]);
$bill->save();
}
return $bill;
}
if (0 === $objectGroupId) {
@@ -252,4 +230,43 @@ class BillUpdateService
}
}
/**
* @param Bill $bill
* @param array $data
*
* @return Bill
*/
private function updateBillProperties(Bill $bill, array $data): Bill
{
if (isset($data['name']) && '' !== (string)$data['name']) {
$bill->name = $data['name'];
}
if (isset($data['amount_min']) && '' !== (string)$data['amount_min']) {
$bill->amount_min = $data['amount_min'];
}
if (isset($data['amount_max']) && '' !== (string)$data['amount_max']) {
$bill->amount_max = $data['amount_max'];
}
if (isset($data['date']) && '' !== (string)$data['date']) {
$bill->date = $data['date'];
}
if (isset($data['repeat_freq']) && '' !== (string)$data['repeat_freq']) {
$bill->repeat_freq = $data['repeat_freq'];
}
if (isset($data['skip']) && '' !== (string)$data['skip']) {
$bill->skip = $data['skip'];
}
if (isset($data['active']) && is_bool($data['active'])) {
$bill->active = $data['active'];
}
$bill->match = 'EMPTY';
$bill->automatch = true;
$bill->save();
return $bill;
}
}