2018-02-22 20:07:14 +01:00
|
|
|
<?php
|
2024-11-25 04:18:55 +01:00
|
|
|
|
2018-02-22 20:07:14 +01:00
|
|
|
/**
|
|
|
|
|
* BillUpdateService.php
|
2020-02-16 13:56:35 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2018-02-22 20:07:14 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-02-22 20:07:14 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
* License, or (at your option) any later version.
|
2018-02-22 20:07:14 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-02-22 20:07:14 +01:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-02-22 20:07:14 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-02-22 20:07:14 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace FireflyIII\Services\Internal\Update;
|
|
|
|
|
|
2026-02-11 07:00:59 +01:00
|
|
|
use FireflyIII\Events\Model\Bill\UpdatedExistingBill;
|
2018-12-21 15:42:40 +01:00
|
|
|
use FireflyIII\Factory\TransactionCurrencyFactory;
|
2018-02-22 20:07:14 +01:00
|
|
|
use FireflyIII\Models\Bill;
|
2025-10-05 12:59:43 +02:00
|
|
|
use FireflyIII\Models\ObjectGroup;
|
2020-06-30 19:06:05 +02:00
|
|
|
use FireflyIII\Repositories\ObjectGroup\CreatesObjectGroups;
|
2018-02-22 20:07:14 +01:00
|
|
|
use FireflyIII\Services\Internal\Support\BillServiceTrait;
|
2026-01-23 15:09:50 +01:00
|
|
|
use FireflyIII\Support\Facades\Amount;
|
2020-07-01 15:33:06 +02:00
|
|
|
use FireflyIII\User;
|
2019-02-12 21:49:28 +01:00
|
|
|
|
2018-02-22 20:07:14 +01:00
|
|
|
/**
|
|
|
|
|
* Class BillUpdateService
|
|
|
|
|
*/
|
|
|
|
|
class BillUpdateService
|
|
|
|
|
{
|
2022-10-30 14:24:37 +01:00
|
|
|
use BillServiceTrait;
|
|
|
|
|
use CreatesObjectGroups;
|
2020-06-30 19:06:05 +02:00
|
|
|
|
2020-10-24 17:27:36 +02:00
|
|
|
protected User $user;
|
2018-09-06 12:29:32 +02:00
|
|
|
|
2018-02-22 20:07:14 +01:00
|
|
|
public function update(Bill $bill, array $data): Bill
|
|
|
|
|
{
|
2020-06-30 19:06:05 +02:00
|
|
|
$this->user = $bill->user;
|
2026-02-13 07:59:43 +01:00
|
|
|
$oldData = $bill->toArray();
|
2018-12-21 15:42:40 +01:00
|
|
|
|
2021-03-13 17:16:38 +01:00
|
|
|
if (array_key_exists('currency_id', $data) || array_key_exists('currency_code', $data)) {
|
2026-01-23 15:14:29 +01:00
|
|
|
$factory = app(TransactionCurrencyFactory::class);
|
|
|
|
|
$currency = $factory->find(
|
2026-01-23 15:09:50 +01:00
|
|
|
(int) ($data['currency_id'] ?? null),
|
|
|
|
|
$data['currency_code'] ?? null
|
|
|
|
|
) ?? Amount::getPrimaryCurrencyByUserGroup($bill->user->userGroup);
|
2018-12-21 15:42:40 +01:00
|
|
|
|
2021-03-13 17:16:38 +01:00
|
|
|
// enable the currency if it isn't.
|
2026-01-23 15:14:29 +01:00
|
|
|
$currency->enabled = true;
|
2021-03-13 17:16:38 +01:00
|
|
|
$currency->save();
|
|
|
|
|
$bill->transaction_currency_id = $currency->id;
|
|
|
|
|
$bill->save();
|
|
|
|
|
}
|
|
|
|
|
// update bill properties:
|
2026-01-23 15:14:29 +01:00
|
|
|
$bill = $this->updateBillProperties($bill, $data);
|
2020-10-27 06:53:33 +01:00
|
|
|
$bill->save();
|
2021-03-13 17:16:38 +01:00
|
|
|
$bill->refresh();
|
2018-02-22 20:07:14 +01:00
|
|
|
// update note:
|
2021-03-13 17:16:38 +01:00
|
|
|
if (array_key_exists('notes', $data)) {
|
2024-12-22 08:43:12 +01:00
|
|
|
$this->updateNote($bill, (string) $data['notes']);
|
2018-02-22 20:07:14 +01:00
|
|
|
}
|
|
|
|
|
|
2020-07-01 15:33:06 +02:00
|
|
|
// update order.
|
2021-03-13 17:16:38 +01:00
|
|
|
if (array_key_exists('order', $data)) {
|
|
|
|
|
// update the order of the piggy bank:
|
2023-11-05 19:55:39 +01:00
|
|
|
$oldOrder = $bill->order;
|
2024-12-22 08:43:12 +01:00
|
|
|
$newOrder = (int) ($data['order'] ?? $oldOrder);
|
2021-03-13 17:16:38 +01:00
|
|
|
if ($oldOrder !== $newOrder) {
|
|
|
|
|
$this->updateOrder($bill, $oldOrder, $newOrder);
|
|
|
|
|
}
|
2020-07-01 15:33:06 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-30 19:06:05 +02:00
|
|
|
// update using name:
|
2021-03-13 17:16:38 +01:00
|
|
|
if (array_key_exists('object_group_title', $data)) {
|
|
|
|
|
$objectGroupTitle = $data['object_group_title'] ?? '';
|
|
|
|
|
if ('' !== $objectGroupTitle) {
|
|
|
|
|
$objectGroup = $this->findOrCreateObjectGroup($objectGroupTitle);
|
2025-05-27 17:06:15 +02:00
|
|
|
if ($objectGroup instanceof ObjectGroup) {
|
2021-03-13 17:16:38 +01:00
|
|
|
$bill->objectGroups()->sync([$objectGroup->id]);
|
|
|
|
|
$bill->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $bill;
|
|
|
|
|
}
|
|
|
|
|
// remove if name is empty. Should be overruled by ID.
|
2023-11-05 16:55:16 +01:00
|
|
|
$bill->objectGroups()->sync([]);
|
|
|
|
|
$bill->save();
|
2020-06-30 19:06:05 +02:00
|
|
|
}
|
2021-03-13 17:16:38 +01:00
|
|
|
if (array_key_exists('object_group_id', $data)) {
|
|
|
|
|
// try also with ID:
|
2024-12-22 08:43:12 +01:00
|
|
|
$objectGroupId = (int) ($data['object_group_id'] ?? 0);
|
2021-03-13 17:16:38 +01:00
|
|
|
if (0 !== $objectGroupId) {
|
|
|
|
|
$objectGroup = $this->findObjectGroupById($objectGroupId);
|
2025-05-27 17:06:15 +02:00
|
|
|
if ($objectGroup instanceof ObjectGroup) {
|
2021-03-13 17:16:38 +01:00
|
|
|
$bill->objectGroups()->sync([$objectGroup->id]);
|
|
|
|
|
$bill->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $bill;
|
|
|
|
|
}
|
2023-11-05 16:55:16 +01:00
|
|
|
$bill->objectGroups()->sync([]);
|
|
|
|
|
$bill->save();
|
2020-06-30 19:06:05 +02:00
|
|
|
}
|
2026-02-11 07:00:59 +01:00
|
|
|
event(new UpdatedExistingBill($bill, $oldData));
|
2020-06-30 19:06:05 +02:00
|
|
|
|
2018-02-22 20:07:14 +01:00
|
|
|
return $bill;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-22 20:12:38 +01:00
|
|
|
/**
|
2025-01-03 15:53:10 +01:00
|
|
|
* @SuppressWarnings("PHPMD.NPathComplexity")
|
2023-12-22 20:12:38 +01:00
|
|
|
*/
|
2021-03-12 18:31:19 +01:00
|
|
|
private function updateBillProperties(Bill $bill, array $data): Bill
|
|
|
|
|
{
|
2024-12-22 08:43:12 +01:00
|
|
|
if (array_key_exists('name', $data) && '' !== (string) $data['name']) {
|
2021-03-12 18:31:19 +01:00
|
|
|
$bill->name = $data['name'];
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-22 08:43:12 +01:00
|
|
|
if (array_key_exists('amount_min', $data) && '' !== (string) $data['amount_min']) {
|
2021-03-12 18:31:19 +01:00
|
|
|
$bill->amount_min = $data['amount_min'];
|
|
|
|
|
}
|
2024-12-22 08:43:12 +01:00
|
|
|
if (array_key_exists('amount_max', $data) && '' !== (string) $data['amount_max']) {
|
2021-03-12 18:31:19 +01:00
|
|
|
$bill->amount_max = $data['amount_max'];
|
|
|
|
|
}
|
2024-12-22 08:43:12 +01:00
|
|
|
if (array_key_exists('date', $data) && '' !== (string) $data['date']) {
|
2026-01-23 15:14:29 +01:00
|
|
|
$bill->date = $data['date'];
|
2024-11-06 11:57:12 +01:00
|
|
|
$bill->date_tz = $data['date']->format('e');
|
2021-03-12 18:31:19 +01:00
|
|
|
}
|
2024-12-22 08:43:12 +01:00
|
|
|
if (array_key_exists('repeat_freq', $data) && '' !== (string) $data['repeat_freq']) {
|
2021-03-12 18:31:19 +01:00
|
|
|
$bill->repeat_freq = $data['repeat_freq'];
|
|
|
|
|
}
|
2021-03-13 17:16:38 +01:00
|
|
|
if (array_key_exists('skip', $data)) {
|
2021-03-12 18:31:19 +01:00
|
|
|
$bill->skip = $data['skip'];
|
|
|
|
|
}
|
2021-03-13 17:16:38 +01:00
|
|
|
if (array_key_exists('active', $data)) {
|
2021-03-12 18:31:19 +01:00
|
|
|
$bill->active = $data['active'];
|
|
|
|
|
}
|
2022-03-29 14:59:58 +02:00
|
|
|
if (array_key_exists('end_date', $data)) {
|
2026-01-23 15:14:29 +01:00
|
|
|
$bill->end_date = $data['end_date'];
|
2024-11-08 09:30:24 +01:00
|
|
|
$bill->end_date_tz = $data['end_date']?->format('e');
|
2022-03-28 12:23:46 +02:00
|
|
|
}
|
2022-03-29 14:59:58 +02:00
|
|
|
if (array_key_exists('extension_date', $data)) {
|
2026-01-23 15:14:29 +01:00
|
|
|
$bill->extension_date = $data['extension_date'];
|
2024-11-08 09:30:24 +01:00
|
|
|
$bill->extension_date_tz = $data['extension_date']?->format('e');
|
2022-03-28 12:23:46 +02:00
|
|
|
}
|
2021-03-12 18:31:19 +01:00
|
|
|
|
2026-01-23 15:14:29 +01:00
|
|
|
$bill->match = 'EMPTY';
|
2021-03-12 18:31:19 +01:00
|
|
|
$bill->automatch = true;
|
|
|
|
|
$bill->save();
|
|
|
|
|
|
|
|
|
|
return $bill;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-06 13:55:17 +01:00
|
|
|
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)
|
|
|
|
|
->decrement('bills.order')
|
|
|
|
|
;
|
|
|
|
|
$bill->order = $newOrder;
|
|
|
|
|
$bill->save();
|
|
|
|
|
}
|
|
|
|
|
if ($newOrder < $oldOrder) {
|
|
|
|
|
$this->user
|
|
|
|
|
->bills()
|
|
|
|
|
->where('order', '>=', $newOrder)
|
|
|
|
|
->where('order', '<', $oldOrder)
|
|
|
|
|
->where('bills.id', '!=', $bill->id)
|
|
|
|
|
->increment('bills.order')
|
|
|
|
|
;
|
|
|
|
|
$bill->order = $newOrder;
|
|
|
|
|
$bill->save();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-05 19:35:58 +01:00
|
|
|
}
|