mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-05 12:12:18 +00:00
User can submit new journal through API.
This commit is contained in:
84
app/Factory/TransactionGroupFactory.php
Normal file
84
app/Factory/TransactionGroupFactory.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
/**
|
||||
* TransactionGroupFactory.php
|
||||
* Copyright (c) 2019 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/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Factory;
|
||||
|
||||
use Exception;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\TransactionGroup;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\User;
|
||||
|
||||
/**
|
||||
* Class TransactionGroupFactory
|
||||
*/
|
||||
class TransactionGroupFactory
|
||||
{
|
||||
/** @var TransactionJournalFactory */
|
||||
private $journalFactory;
|
||||
/** @var User The user */
|
||||
private $user;
|
||||
|
||||
/**
|
||||
* TransactionGroupFactory constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->journalFactory = app(TransactionJournalFactory::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a new transaction journal.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return TransactionGroup
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function create(array $data): TransactionGroup
|
||||
{
|
||||
$this->journalFactory->setUser($this->user);
|
||||
$collection = $this->journalFactory->create($data);
|
||||
$title = $data['group_title'] ?? null;
|
||||
/** @var TransactionJournal $first */
|
||||
$first = $collection->first();
|
||||
$group = new TransactionGroup;
|
||||
$group->user()->associate($first->user);
|
||||
$group->title = $title ?? $first->description;
|
||||
$group->save();
|
||||
|
||||
$group->transactionJournals()->saveMany($collection);
|
||||
|
||||
return $group;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the user.
|
||||
*
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user