Fix bill edit submission group thing.

This commit is contained in:
James Cole
2020-10-19 18:28:39 +02:00
parent 2e3af087e8
commit 30f708ba7a

View File

@@ -81,31 +81,31 @@ class BillUpdateService
// old values // old values
$oldData = [ $oldData = [
'name' => $bill->name, 'name' => $bill->name,
'amount_min' => $bill->amount_min, 'amount_min' => $bill->amount_min,
'amount_max' => $bill->amount_max, 'amount_max' => $bill->amount_max,
'transaction_currency_name' => $bill->transactionCurrency->name, 'transaction_currency_name' => $bill->transactionCurrency->name,
]; ];
// new values // new values
$data['transaction_currency_name'] = $currency->name; $data['transaction_currency_name'] = $currency->name;
if (isset($data['name']) && '' !== (string) $data['name']) { if (isset($data['name']) && '' !== (string)$data['name']) {
$bill->name = $data['name']; $bill->name = $data['name'];
} }
if (isset($data['amount_min']) && '' !== (string) $data['amount_min']) { if (isset($data['amount_min']) && '' !== (string)$data['amount_min']) {
$bill->amount_min = $data['amount_min']; $bill->amount_min = $data['amount_min'];
} }
if (isset($data['amount_max']) && '' !== (string) $data['amount_max']) { if (isset($data['amount_max']) && '' !== (string)$data['amount_max']) {
$bill->amount_max = $data['amount_max']; $bill->amount_max = $data['amount_max'];
} }
if (isset($data['date']) && '' !== (string) $data['date']) { if (isset($data['date']) && '' !== (string)$data['date']) {
$bill->date = $data['date']; $bill->date = $data['date'];
} }
if (isset($data['repeat_freq']) && '' !== (string) $data['repeat_freq']) { if (isset($data['repeat_freq']) && '' !== (string)$data['repeat_freq']) {
$bill->repeat_freq = $data['repeat_freq']; $bill->repeat_freq = $data['repeat_freq'];
} }
if (isset($data['skip']) && '' !== (string) $data['skip']) { if (isset($data['skip']) && '' !== (string)$data['skip']) {
$bill->skip = $data['skip']; $bill->skip = $data['skip'];
} }
if (isset($data['active']) && is_bool($data['active'])) { if (isset($data['active']) && is_bool($data['active'])) {
@@ -113,19 +113,19 @@ class BillUpdateService
} }
$bill->transaction_currency_id = $currency->id; $bill->transaction_currency_id = $currency->id;
$bill->match = 'EMPTY'; $bill->match = 'EMPTY';
$bill->automatch = true; $bill->automatch = true;
$bill->save(); $bill->save();
// update note: // update note:
if (isset($data['notes'])) { if (isset($data['notes'])) {
$this->updateNote($bill, (string) $data['notes']); $this->updateNote($bill, (string)$data['notes']);
} }
// update order. // update order.
// update the order of the piggy bank: // update the order of the piggy bank:
$oldOrder = (int) $bill->order; $oldOrder = (int)$bill->order;
$newOrder = (int) ($data['order'] ?? $oldOrder); $newOrder = (int)($data['order'] ?? $oldOrder);
if ($oldOrder !== $newOrder) { if ($oldOrder !== $newOrder) {
$this->updateOrder($bill, $oldOrder, $newOrder); $this->updateOrder($bill, $oldOrder, $newOrder);
} }
@@ -142,22 +142,34 @@ class BillUpdateService
$bill->objectGroups()->sync([$objectGroup->id]); $bill->objectGroups()->sync([$objectGroup->id]);
$bill->save(); $bill->save();
} }
return $bill;
} }
// remove if name is empty. Should be overruled by ID.
if ('' === $objectGroupTitle) {
$bill->objectGroups()->sync([]);
$bill->save();
}
// try also with ID: // try also with ID:
$objectGroupId = (int) ($data['object_group_id'] ?? 0); $objectGroupId = (int)($data['object_group_id'] ?? 0);
if (0 !== $objectGroupId) { if (0 !== $objectGroupId) {
$objectGroup = $this->findObjectGroupById($objectGroupId); $objectGroup = $this->findObjectGroupById($objectGroupId);
if (null !== $objectGroup) { if (null !== $objectGroup) {
$bill->objectGroups()->sync([$objectGroup->id]); $bill->objectGroups()->sync([$objectGroup->id]);
$bill->save(); $bill->save();
} }
return $bill;
}
if(0 === $objectGroupId) {
$bill->objectGroups()->sync([]);
$bill->save();
} }
return $bill; return $bill;
} }
/** /**
* @param Bill $bill * @param Bill $bill
* @param array $oldData * @param array $oldData
* @param array $newData * @param array $newData
*/ */
@@ -175,9 +187,9 @@ class BillUpdateService
} }
Log::debug(sprintf('Found %d rules', $rules->count())); Log::debug(sprintf('Found %d rules', $rules->count()));
$fields = [ $fields = [
'name' => 'description_contains', 'name' => 'description_contains',
'amount_min' => 'amount_more', 'amount_min' => 'amount_more',
'amount_max' => 'amount_less', 'amount_max' => 'amount_less',
'transaction_currency_name' => 'currency_is']; 'transaction_currency_name' => 'currency_is'];
foreach ($fields as $field => $ruleTriggerKey) { foreach ($fields as $field => $ruleTriggerKey) {
if ($oldData[$field] === $newData[$field]) { if ($oldData[$field] === $newData[$field]) {
@@ -191,9 +203,9 @@ class BillUpdateService
/** /**
* @param Collection $rules * @param Collection $rules
* @param string $key * @param string $key
* @param string $oldValue * @param string $oldValue
* @param string $newValue * @param string $newValue
*/ */
private function updateRules(Collection $rules, string $key, string $oldValue, string $newValue): void private function updateRules(Collection $rules, string $key, string $oldValue, string $newValue): void
{ {
@@ -218,7 +230,7 @@ class BillUpdateService
/** /**
* @param Rule $rule * @param Rule $rule
* @param string $key * @param string $key
* *
* @return RuleTrigger|null * @return RuleTrigger|null
@@ -230,8 +242,8 @@ class BillUpdateService
/** /**
* @param Bill $bill * @param Bill $bill
* @param int $oldOrder * @param int $oldOrder
* @param int $newOrder * @param int $newOrder
*/ */
private function updateOrder(Bill $bill, int $oldOrder, int $newOrder): void private function updateOrder(Bill $bill, int $oldOrder, int $newOrder): void
{ {
@@ -239,8 +251,8 @@ class BillUpdateService
/** @var User $user */ /** @var User $user */
$user = $this->user; $user = $this->user;
$user->bills()->where('order', '<=', $newOrder)->where('order', '>', $oldOrder) $user->bills()->where('order', '<=', $newOrder)->where('order', '>', $oldOrder)
->where('bills.id', '!=', $bill->id) ->where('bills.id', '!=', $bill->id)
->update(['order' => DB::raw('bills.order-1')]); ->update(['order' => DB::raw('bills.order-1')]);
$bill->order = $newOrder; $bill->order = $newOrder;
$bill->save(); $bill->save();
} }
@@ -248,8 +260,8 @@ class BillUpdateService
/** @var User $user */ /** @var User $user */
$user = $this->user; $user = $this->user;
$user->bills()->where('order', '>=', $newOrder)->where('order', '<', $oldOrder) $user->bills()->where('order', '>=', $newOrder)->where('order', '<', $oldOrder)
->where('bills.id', '!=', $bill->id) ->where('bills.id', '!=', $bill->id)
->update(['order' => DB::raw('bills.order+1')]); ->update(['order' => DB::raw('bills.order+1')]);
$bill->order = $newOrder; $bill->order = $newOrder;
$bill->save(); $bill->save();
} }