Files
firefly-iii/app/Services/Internal/Update/BillUpdateService.php

189 lines
6.5 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* BillUpdateService.php
2020-02-16 13:56:35 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* 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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Services\Internal\Update;
use FireflyIII\Events\Model\Bill\UpdatedExistingBill;
2018-12-21 15:42:40 +01:00
use FireflyIII\Factory\TransactionCurrencyFactory;
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;
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;
/**
* 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;
public function update(Bill $bill, array $data): Bill
{
2020-06-30 19:06:05 +02:00
$this->user = $bill->user;
$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)) {
$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.
$currency->enabled = true;
2021-03-13 17:16:38 +01:00
$currency->save();
$bill->transaction_currency_id = $currency->id;
$bill->save();
}
// update bill properties:
$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();
// 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']);
}
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
}
event(new UpdatedExistingBill($bill, $oldData));
2020-06-30 19:06:05 +02: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']) {
$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)) {
$bill->end_date = $data['end_date'];
$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)) {
$bill->extension_date = $data['extension_date'];
$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
$bill->match = 'EMPTY';
2021-03-12 18:31:19 +01:00
$bill->automatch = true;
$bill->save();
return $bill;
}
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
}