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
$oldData = [
'name' => $bill->name,
'amount_min' => $bill->amount_min,
'amount_max' => $bill->amount_max,
'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']) {
if (isset($data['name']) && '' !== (string)$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'];
}
if (isset($data['amount_max']) && '' !== (string) $data['amount_max']) {
if (isset($data['amount_max']) && '' !== (string)$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'];
}
if (isset($data['repeat_freq']) && '' !== (string) $data['repeat_freq']) {
if (isset($data['repeat_freq']) && '' !== (string)$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'];
}
if (isset($data['active']) && is_bool($data['active'])) {
@ -113,19 +113,19 @@ class BillUpdateService
}
$bill->transaction_currency_id = $currency->id;
$bill->match = 'EMPTY';
$bill->automatch = true;
$bill->match = 'EMPTY';
$bill->automatch = true;
$bill->save();
// update note:
if (isset($data['notes'])) {
$this->updateNote($bill, (string) $data['notes']);
$this->updateNote($bill, (string)$data['notes']);
}
// update order.
// update the order of the piggy bank:
$oldOrder = (int) $bill->order;
$newOrder = (int) ($data['order'] ?? $oldOrder);
$oldOrder = (int)$bill->order;
$newOrder = (int)($data['order'] ?? $oldOrder);
if ($oldOrder !== $newOrder) {
$this->updateOrder($bill, $oldOrder, $newOrder);
}
@ -142,22 +142,34 @@ class BillUpdateService
$bill->objectGroups()->sync([$objectGroup->id]);
$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:
$objectGroupId = (int) ($data['object_group_id'] ?? 0);
$objectGroupId = (int)($data['object_group_id'] ?? 0);
if (0 !== $objectGroupId) {
$objectGroup = $this->findObjectGroupById($objectGroupId);
if (null !== $objectGroup) {
$bill->objectGroups()->sync([$objectGroup->id]);
$bill->save();
}
return $bill;
}
if(0 === $objectGroupId) {
$bill->objectGroups()->sync([]);
$bill->save();
}
return $bill;
}
/**
* @param Bill $bill
* @param Bill $bill
* @param array $oldData
* @param array $newData
*/
@ -175,9 +187,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]) {
@ -191,9 +203,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
{
@ -218,7 +230,7 @@ class BillUpdateService
/**
* @param Rule $rule
* @param Rule $rule
* @param string $key
*
* @return RuleTrigger|null
@ -230,8 +242,8 @@ 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
{
@ -239,8 +251,8 @@ class BillUpdateService
/** @var User $user */
$user = $this->user;
$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();
}
@ -248,8 +260,8 @@ class BillUpdateService
/** @var User $user */
$user = $this->user;
$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();
}