2019-03-31 13:36:49 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* TransactionGroupFactory.php
|
2020-02-16 14:00:57 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2019-03-31 13:36:49 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2019-03-31 13:36:49 +02: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.
|
2019-03-31 13:36:49 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2019-03-31 13:36:49 +02: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.
|
2019-03-31 13:36:49 +02: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/>.
|
2019-03-31 13:36:49 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Factory;
|
|
|
|
|
2019-10-26 07:49:12 +02:00
|
|
|
use FireflyIII\Exceptions\DuplicateTransactionException;
|
2021-03-21 11:06:08 +01:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2019-03-31 13:36:49 +02:00
|
|
|
use FireflyIII\Models\TransactionGroup;
|
|
|
|
use FireflyIII\User;
|
2020-02-22 11:05:16 +01:00
|
|
|
use Log;
|
2019-03-31 13:36:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class TransactionGroupFactory
|
2019-08-30 07:28:48 +02:00
|
|
|
*
|
2019-06-16 13:15:32 +02:00
|
|
|
* @codeCoverageIgnore
|
2019-03-31 13:36:49 +02:00
|
|
|
*/
|
|
|
|
class TransactionGroupFactory
|
|
|
|
{
|
|
|
|
/** @var TransactionJournalFactory */
|
|
|
|
private $journalFactory;
|
|
|
|
/** @var User The user */
|
|
|
|
private $user;
|
2021-03-28 11:46:23 +02:00
|
|
|
|
2019-03-31 13:36:49 +02:00
|
|
|
/**
|
|
|
|
* TransactionGroupFactory constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->journalFactory = app(TransactionJournalFactory::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a new transaction journal.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*
|
2020-03-17 14:57:04 +01:00
|
|
|
* @return TransactionGroup
|
2021-03-21 09:15:40 +01:00
|
|
|
* @throws DuplicateTransactionException
|
2021-05-24 08:50:17 +02:00
|
|
|
* @throws FireflyException
|
2019-03-31 13:36:49 +02:00
|
|
|
*/
|
|
|
|
public function create(array $data): TransactionGroup
|
|
|
|
{
|
2020-10-04 17:11:59 +02:00
|
|
|
Log::debug('Now in TransactionGroupFactory::create()');
|
2019-03-31 13:36:49 +02:00
|
|
|
$this->journalFactory->setUser($this->user);
|
2019-10-26 16:21:12 +02:00
|
|
|
$this->journalFactory->setErrorOnHash($data['error_if_duplicate_hash'] ?? false);
|
2020-02-22 11:05:16 +01:00
|
|
|
try {
|
|
|
|
$collection = $this->journalFactory->create($data);
|
2020-03-17 14:57:04 +01:00
|
|
|
} catch (DuplicateTransactionException $e) {
|
2020-02-22 11:05:16 +01:00
|
|
|
Log::warning('GroupFactory::create() caught journalFactory::create() with a duplicate!');
|
2021-04-07 07:28:43 +02:00
|
|
|
throw new DuplicateTransactionException($e->getMessage(), 0, $e);
|
2020-02-22 11:05:16 +01:00
|
|
|
}
|
2020-03-17 14:57:04 +01:00
|
|
|
$title = $data['group_title'] ?? null;
|
|
|
|
$title = '' === $title ? null : $title;
|
2019-08-30 07:28:48 +02:00
|
|
|
|
|
|
|
if (null !== $title) {
|
2019-09-21 07:33:13 +02:00
|
|
|
$title = substr($title, 0, 1000);
|
2019-08-30 07:28:48 +02:00
|
|
|
}
|
2021-03-28 11:46:23 +02:00
|
|
|
if (0 === $collection->count()) {
|
2021-03-21 11:06:08 +01:00
|
|
|
throw new FireflyException('Created zero transaction journals.');
|
|
|
|
}
|
2019-08-30 07:28:48 +02:00
|
|
|
|
|
|
|
$group = new TransactionGroup;
|
2019-08-16 17:54:38 +02:00
|
|
|
$group->user()->associate($this->user);
|
2019-06-16 13:15:32 +02:00
|
|
|
$group->title = $title;
|
2019-03-31 13:36:49 +02:00
|
|
|
$group->save();
|
|
|
|
|
|
|
|
$group->transactionJournals()->saveMany($collection);
|
|
|
|
|
|
|
|
return $group;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the user.
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
public function setUser(User $user): void
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
2019-08-17 12:09:03 +02:00
|
|
|
}
|