firefly-iii/app/Factory/BillFactory.php

154 lines
4.9 KiB
PHP
Raw Normal View History

2018-02-19 12:44:46 -06:00
<?php
2018-05-11 03:08:34 -05:00
2018-02-19 12:44:46 -06:00
/**
* BillFactory.php
2020-02-16 07:00:57 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2018-02-19 12:44:46 -06:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-02-19 12:44:46 -06: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-19 12:44:46 -06:00
*
* This program is distributed in the hope that it will be useful,
2018-02-19 12:44:46 -06:00
* 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.
2018-02-19 12:44:46 -06: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-19 12:44:46 -06:00
*/
2018-05-11 03:08:34 -05:00
declare(strict_types=1);
2018-02-19 12:44:46 -06:00
namespace FireflyIII\Factory;
2019-10-30 14:02:21 -05:00
use FireflyIII\Exceptions\FireflyException;
2018-02-19 12:44:46 -06:00
use FireflyIII\Models\Bill;
2020-06-30 12:06:05 -05:00
use FireflyIII\Repositories\ObjectGroup\CreatesObjectGroups;
use FireflyIII\Services\Internal\Support\BillServiceTrait;
2018-02-19 12:44:46 -06:00
use FireflyIII\User;
2019-10-30 14:02:21 -05:00
use Illuminate\Database\QueryException;
2023-04-01 00:04:42 -05:00
use Illuminate\Support\Facades\Log;
2023-05-29 06:56:55 -05:00
use JsonException;
2018-02-19 12:44:46 -06:00
/**
* Class BillFactory
*/
class BillFactory
{
2022-10-30 08:24:10 -05:00
use BillServiceTrait;
use CreatesObjectGroups;
2018-12-21 08:42:40 -06:00
2020-10-23 12:11:25 -05:00
private User $user;
2018-02-19 12:44:46 -06:00
/**
2023-06-21 05:34:58 -05:00
* @param array $data
2018-03-01 13:54:50 -06:00
*
* @return Bill|null
2020-08-01 10:51:52 -05:00
* @throws FireflyException
2022-12-29 12:41:57 -06:00
* @throws JsonException
2018-02-19 12:44:46 -06:00
*/
2018-03-01 13:54:50 -06:00
public function create(array $data): ?Bill
2018-02-19 12:44:46 -06:00
{
2021-03-13 09:47:29 -06:00
Log::debug(sprintf('Now in %s', __METHOD__), $data);
$factory = app(TransactionCurrencyFactory::class);
2022-12-29 12:41:57 -06:00
$currency = $factory->find((int)($data['currency_id'] ?? null), (string)($data['currency_code'] ?? null)) ??
2021-03-13 09:47:29 -06:00
app('amount')->getDefaultCurrencyByUser($this->user);
2018-12-21 08:42:40 -06:00
2019-10-30 14:02:21 -05:00
try {
2021-03-13 09:47:29 -06:00
$skip = array_key_exists('skip', $data) ? $data['skip'] : 0;
$active = array_key_exists('active', $data) ? $data['active'] : 0;
2019-10-30 14:02:21 -05:00
/** @var Bill $bill */
$bill = Bill::create(
[
'name' => $data['name'],
'match' => 'MIGRATED_TO_RULES',
'amount_min' => $data['amount_min'],
'user_id' => $this->user->id,
'transaction_currency_id' => $currency->id,
'amount_max' => $data['amount_max'],
'date' => $data['date'],
2021-07-25 12:39:35 -05:00
'end_date' => $data['end_date'] ?? null,
'extension_date' => $data['extension_date'] ?? null,
2019-10-30 14:02:21 -05:00
'repeat_freq' => $data['repeat_freq'],
2021-03-13 09:47:29 -06:00
'skip' => $skip,
2019-10-30 14:02:21 -05:00
'automatch' => true,
2021-03-13 09:47:29 -06:00
'active' => $active,
2019-10-30 14:02:21 -05:00
]
);
} catch (QueryException $e) {
2019-10-30 14:02:21 -05:00
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
2021-04-07 00:28:43 -05:00
throw new FireflyException('400000: Could not store bill.', 0, $e);
2019-10-30 14:02:21 -05:00
}
2018-12-21 08:42:40 -06:00
2020-08-01 10:51:52 -05:00
if (array_key_exists('notes', $data)) {
2022-12-29 12:41:57 -06:00
$this->updateNote($bill, (string)$data['notes']);
2018-03-01 13:54:50 -06:00
}
2021-03-13 09:47:29 -06:00
$objectGroupTitle = $data['object_group_title'] ?? '';
2020-06-30 12:06:05 -05:00
if ('' !== $objectGroupTitle) {
$objectGroup = $this->findOrCreateObjectGroup($objectGroupTitle);
if (null !== $objectGroup) {
$bill->objectGroups()->sync([$objectGroup->id]);
$bill->save();
}
}
// try also with ID:
2022-12-29 12:41:57 -06:00
$objectGroupId = (int)($data['object_group_id'] ?? 0);
2020-06-30 12:06:05 -05:00
if (0 !== $objectGroupId) {
$objectGroup = $this->findObjectGroupById($objectGroupId);
if (null !== $objectGroup) {
$bill->objectGroups()->sync([$objectGroup->id]);
$bill->save();
}
}
2018-03-01 13:54:50 -06:00
return $bill;
2018-02-19 12:44:46 -06:00
}
/**
2023-06-21 05:34:58 -05:00
* @param int|null $billId
* @param null|string $billName
2018-02-19 12:44:46 -06:00
*
* @return Bill|null
*/
public function find(?int $billId, ?string $billName): ?Bill
{
2022-12-29 12:41:57 -06:00
$billId = (int)$billId;
$billName = (string)$billName;
$bill = null;
2018-02-19 12:44:46 -06:00
// first find by ID:
if ($billId > 0) {
/** @var Bill $bill */
2018-03-01 13:54:50 -06:00
$bill = $this->user->bills()->find($billId);
2018-02-19 12:44:46 -06:00
}
// then find by name:
2019-02-13 10:38:41 -06:00
if (null === $bill && '' !== $billName) {
2018-03-01 13:54:50 -06:00
$bill = $this->findByName($billName);
2018-02-19 12:44:46 -06:00
}
return $bill;
2018-02-19 12:44:46 -06:00
}
/**
2023-06-21 05:34:58 -05:00
* @param string $name
2018-03-01 13:54:50 -06:00
*
* @return Bill|null
2018-02-19 12:44:46 -06:00
*/
2018-03-01 13:54:50 -06:00
public function findByName(string $name): ?Bill
2018-02-19 12:44:46 -06:00
{
2020-10-23 12:11:25 -05:00
return $this->user->bills()->where('name', 'LIKE', sprintf('%%%s%%', $name))->first();
2018-02-19 12:44:46 -06:00
}
/**
2023-06-21 05:34:58 -05:00
* @param User $user
*/
public function setUser(User $user): void
{
2018-03-01 13:54:50 -06:00
$this->user = $user;
}
2018-03-05 12:35:58 -06:00
}