Simplify methods.

This commit is contained in:
James Cole 2020-10-27 06:53:33 +01:00
parent 0fb81a6112
commit 8d84bffc2d
No known key found for this signature in database
GPG Key ID: B5669F9493CDE38D
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

@ -47,7 +47,7 @@ class BillUpdateService
protected User $user;
/**
* @param Bill $bill
* @param Bill $bill
* @param array $data
*
* @return Bill
@ -69,43 +69,19 @@ class BillUpdateService
$currency->enabled = true;
$currency->save();
// old values
$oldData = [
'name' => $bill->name,
'amount_min' => $bill->amount_min,
'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 = $this->updateBillProperties($bill, $data);
$bill->transaction_currency_id = $currency->id;
$bill->save();
// old values
$oldData = [
'name' => $bill->name,
'amount_min' => $bill->amount_min,
'amount_max' => $bill->amount_max,
'transaction_currency_name' => $bill->transactionCurrency->name,
];
// 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,9 +125,10 @@ class BillUpdateService
$bill->objectGroups()->sync([$objectGroup->id]);
$bill->save();
}
return $bill;
}
if(0 === $objectGroupId) {
if (0 === $objectGroupId) {
$bill->objectGroups()->sync([]);
$bill->save();
}
@ -159,7 +137,7 @@ class BillUpdateService
}
/**
* @param Bill $bill
* @param Bill $bill
* @param array $oldData
* @param array $newData
*/
@ -177,9 +155,9 @@ class BillUpdateService
}
Log::debug(sprintf('Found %d rules', $rules->count()));
$fields = [
'name' => 'description_contains',
'amount_min' => 'amount_more',
'amount_max' => 'amount_less',
'name' => 'description_contains',
'amount_min' => 'amount_more',
'amount_max' => 'amount_less',
'transaction_currency_name' => 'currency_is'];
foreach ($fields as $field => $ruleTriggerKey) {
if ($oldData[$field] === $newData[$field]) {
@ -193,9 +171,9 @@ class BillUpdateService
/**
* @param Collection $rules
* @param string $key
* @param string $oldValue
* @param string $newValue
* @param string $key
* @param string $oldValue
* @param string $newValue
*/
private function updateRules(Collection $rules, string $key, string $oldValue, string $newValue): void
{
@ -219,7 +197,7 @@ class BillUpdateService
/**
* @param Rule $rule
* @param Rule $rule
* @param string $key
*
* @return RuleTrigger|null
@ -231,25 +209,64 @@ class BillUpdateService
/**
* @param Bill $bill
* @param int $oldOrder
* @param int $newOrder
* @param int $oldOrder
* @param int $newOrder
*/
private function updateOrder(Bill $bill, int $oldOrder, int $newOrder): void
{
if ($newOrder > $oldOrder) {
$this->user->bills()->where('order', '<=', $newOrder)->where('order', '>', $oldOrder)
->where('bills.id', '!=', $bill->id)
->update(['order' => DB::raw('bills.order-1')]);
->where('bills.id', '!=', $bill->id)
->update(['order' => DB::raw('bills.order-1')]);
$bill->order = $newOrder;
$bill->save();
}
if ($newOrder < $oldOrder) {
$this->user->bills()->where('order', '>=', $newOrder)->where('order', '<', $oldOrder)
->where('bills.id', '!=', $bill->id)
->update(['order' => DB::raw('bills.order+1')]);
->where('bills.id', '!=', $bill->id)
->update(['order' => DB::raw('bills.order+1')]);
$bill->order = $newOrder;
$bill->save();
}
}
/**
* @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;
}
}