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
|
|
|
|
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
|
|
|
*
|
|
|
|
* This file is part of Firefly III.
|
|
|
|
*
|
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-05-11 03:08:34 -05:00
|
|
|
declare(strict_types=1);
|
2018-02-19 12:44:46 -06:00
|
|
|
|
|
|
|
namespace FireflyIII\Factory;
|
|
|
|
|
|
|
|
use FireflyIII\Models\Bill;
|
2018-08-18 07:12:39 -05:00
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
2018-02-22 13:07:14 -06:00
|
|
|
use FireflyIII\Services\Internal\Support\BillServiceTrait;
|
2018-02-19 12:44:46 -06:00
|
|
|
use FireflyIII\User;
|
2018-03-01 13:54:50 -06:00
|
|
|
use Log;
|
2018-02-19 12:44:46 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class BillFactory
|
|
|
|
*/
|
|
|
|
class BillFactory
|
|
|
|
{
|
2018-12-21 08:42:40 -06:00
|
|
|
use BillServiceTrait;
|
|
|
|
|
2019-02-13 10:38:41 -06:00
|
|
|
/** @var User */
|
|
|
|
private $user;
|
|
|
|
|
2018-09-06 05:29:32 -05:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2018-12-15 00:59:02 -06:00
|
|
|
if ('testing' === config('app.env')) {
|
2019-06-07 11:20:15 -05:00
|
|
|
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
|
2018-09-06 05:29:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-19 12:44:46 -06:00
|
|
|
/**
|
2018-03-01 13:54:50 -06:00
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Bill|null
|
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
|
|
|
{
|
2018-08-18 07:12:39 -05:00
|
|
|
/** @var TransactionCurrencyFactory $factory */
|
|
|
|
$factory = app(TransactionCurrencyFactory::class);
|
|
|
|
/** @var TransactionCurrency $currency */
|
2018-12-21 09:38:10 -06:00
|
|
|
$currency = $factory->find((int)($data['currency_id'] ?? null), (string)($data['currency_code'] ?? null));
|
2018-12-21 08:42:40 -06:00
|
|
|
|
2019-02-13 10:38:41 -06:00
|
|
|
if (null === $currency) {
|
2018-12-21 08:42:40 -06:00
|
|
|
// use default currency:
|
|
|
|
$currency = app('amount')->getDefaultCurrencyByUser($this->user);
|
|
|
|
}
|
|
|
|
|
2018-03-01 13:54:50 -06:00
|
|
|
/** @var Bill $bill */
|
|
|
|
$bill = Bill::create(
|
|
|
|
[
|
2018-04-08 09:27:52 -05:00
|
|
|
'name' => $data['name'],
|
|
|
|
'match' => 'MIGRATED_TO_RULES',
|
|
|
|
'amount_min' => $data['amount_min'],
|
|
|
|
'user_id' => $this->user->id,
|
2018-08-18 07:12:39 -05:00
|
|
|
'transaction_currency_id' => $currency->id,
|
2018-04-08 09:27:52 -05:00
|
|
|
'amount_max' => $data['amount_max'],
|
|
|
|
'date' => $data['date'],
|
|
|
|
'repeat_freq' => $data['repeat_freq'],
|
|
|
|
'skip' => $data['skip'],
|
2018-12-21 08:42:57 -06:00
|
|
|
'automatch' => true,
|
2018-06-24 06:20:29 -05:00
|
|
|
'active' => $data['active'] ?? true,
|
2018-03-01 13:54:50 -06:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
// update note:
|
|
|
|
if (isset($data['notes'])) {
|
|
|
|
$this->updateNote($bill, $data['notes']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $bill;
|
2018-02-19 12:44:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-07 23:19:21 -05:00
|
|
|
* @param int|null $billId
|
2018-02-19 12:44:46 -06:00
|
|
|
* @param null|string $billName
|
|
|
|
*
|
|
|
|
* @return Bill|null
|
|
|
|
*/
|
|
|
|
public function find(?int $billId, ?string $billName): ?Bill
|
|
|
|
{
|
2018-04-02 07:42:07 -05:00
|
|
|
$billId = (int)$billId;
|
|
|
|
$billName = (string)$billName;
|
2018-07-06 12:06:08 -05:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2018-07-06 12:06:08 -05:00
|
|
|
return $bill;
|
2018-02-19 12:44:46 -06:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-01 13:54:50 -06:00
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @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
|
|
|
{
|
2019-03-07 22:47:51 -06:00
|
|
|
$query = sprintf('%%%s%%', $name);
|
2019-06-07 23:19:21 -05:00
|
|
|
/** @var Bill $first */
|
|
|
|
$first = $this->user->bills()->where('name', 'LIKE', $query)->first();
|
2018-02-19 12:44:46 -06:00
|
|
|
|
2019-06-07 23:19:21 -05:00
|
|
|
return $first;
|
2018-02-19 12:44:46 -06:00
|
|
|
}
|
|
|
|
|
2018-02-22 13:07:14 -06:00
|
|
|
/**
|
2018-03-01 13:54:50 -06:00
|
|
|
* @param User $user
|
2018-02-22 13:07:14 -06:00
|
|
|
*/
|
2018-07-06 12:06:08 -05:00
|
|
|
public function setUser(User $user): void
|
2018-02-22 13:07:14 -06:00
|
|
|
{
|
2018-03-01 13:54:50 -06:00
|
|
|
$this->user = $user;
|
2018-02-22 13:07:14 -06:00
|
|
|
}
|
|
|
|
|
2018-03-05 12:35:58 -06:00
|
|
|
}
|