Files
firefly-iii/app/Http/Controllers/Transaction/CreateController.php

156 lines
5.9 KiB
PHP
Raw Normal View History

2019-04-19 07:00:19 +02:00
<?php
/**
* CreateController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2019-04-19 07:00:19 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2019-04-19 07:00:19 +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-04-19 07:00:19 +02:00
*
* This program is distributed in the hope that it will be useful,
2019-04-19 07:00:19 +02: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.
2019-04-19 07:00:19 +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-04-19 07:00:19 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Transaction;
2021-02-22 18:41:03 +01:00
use FireflyIII\Events\StoredTransactionGroup;
use FireflyIII\Exceptions\FireflyException;
2019-04-19 07:00:19 +02:00
use FireflyIII\Http\Controllers\Controller;
2019-05-25 09:17:46 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2021-09-20 06:39:10 +02:00
use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface;
2020-02-07 20:51:25 +01:00
use FireflyIII\Services\Internal\Update\GroupCloneService;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
2021-09-20 06:39:10 +02:00
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
2019-04-19 07:00:19 +02:00
/**
* Class CreateController
*/
class CreateController extends Controller
{
2021-09-20 06:39:10 +02:00
private TransactionGroupRepositoryInterface $repository;
2019-05-04 20:58:43 +02:00
/**
* CreateController constructor.
*/
public function __construct()
{
parent::__construct();
$this->middleware(
2021-09-20 06:39:10 +02:00
function ($request, $next) {
2024-01-04 07:26:45 +01:00
app('view')->share('title', (string) trans('firefly.transactions'));
2020-05-01 06:24:24 +02:00
app('view')->share('mainTitleIcon', 'fa-exchange');
2021-09-20 06:39:10 +02:00
$this->repository = app(TransactionGroupRepositoryInterface::class);
2019-05-04 20:58:43 +02:00
return $next($request);
}
);
}
2021-09-20 06:39:10 +02:00
public function cloneGroup(Request $request): JsonResponse
2020-02-07 20:51:25 +01:00
{
2024-01-04 07:26:45 +01:00
$groupId = (int) $request->get('id');
2021-09-20 06:39:10 +02:00
if (0 !== $groupId) {
$group = $this->repository->find($groupId);
if (null !== $group) {
/** @var GroupCloneService $service */
$service = app(GroupCloneService::class);
$newGroup = $service->cloneGroup($group);
2020-02-07 20:51:25 +01:00
2021-09-20 06:39:10 +02:00
// event!
event(new StoredTransactionGroup($newGroup, true, true));
2020-02-07 20:51:25 +01:00
2021-09-20 06:39:10 +02:00
app('preferences')->mark();
2021-02-22 18:41:03 +01:00
2024-01-04 07:26:45 +01:00
$title = $newGroup->title ?? $newGroup->transactionJournals->first()->description;
$link = route('transactions.show', [$newGroup->id]);
2021-09-20 06:39:10 +02:00
session()->flash('success', trans('firefly.stored_journal', ['description' => $title]));
session()->flash('success_url', $link);
2020-02-19 19:55:35 +01:00
2023-10-28 15:03:33 +02:00
if ('edit' === $request->get('redirect')) {
2023-10-21 07:38:08 +02:00
return response()->json(['redirect' => route('transactions.edit', [$newGroup->id])]);
}
2021-09-20 06:39:10 +02:00
return response()->json(['redirect' => route('transactions.show', [$newGroup->id])]);
}
}
2020-02-22 06:28:18 +01:00
2021-09-20 06:39:10 +02:00
return response()->json(['redirect' => route('transactions.show', [$groupId])]);
2020-02-07 20:51:25 +01:00
}
2019-05-04 20:58:43 +02:00
/**
* Create a new transaction group.
*
* @return Factory|View
2023-12-20 19:35:52 +01:00
*
* @throws FireflyException
2023-12-22 07:58:35 +01:00
* */
2019-08-07 18:51:35 +02:00
public function create(?string $objectType)
2019-05-04 20:58:43 +02:00
{
2019-08-23 13:06:00 +02:00
app('preferences')->mark();
2024-01-04 07:26:45 +01:00
$sourceId = (int) request()->get('source');
$destinationId = (int) request()->get('destination');
2020-03-13 12:46:52 +01:00
2021-12-17 17:27:01 +01:00
/** @var AccountRepositoryInterface $accountRepository */
$accountRepository = app(AccountRepositoryInterface::class);
$cash = $accountRepository->getCashAccount();
2019-05-04 20:58:43 +02:00
$preFilled = session()->has('preFilled') ? session('preFilled') : [];
2024-01-04 07:26:45 +01:00
$subTitle = (string) trans(sprintf('breadcrumbs.create_%s', strtolower((string) $objectType)));
2019-05-04 20:58:43 +02:00
$subTitleIcon = 'fa-plus';
$optionalFields = app('preferences')->get('transaction_journal_optional_fields', [])->data;
$allowedOpposingTypes = config('firefly.allowed_opposing_types');
$accountToTypes = config('firefly.account_to_transaction');
$defaultCurrency = app('amount')->getDefaultCurrency();
2022-04-12 18:19:30 +02:00
$previousUrl = $this->rememberPreviousUrl('transactions.create.url');
2021-10-03 06:05:11 +02:00
$parts = parse_url($previousUrl);
2019-10-26 17:06:46 +02:00
$search = sprintf('?%s', $parts['query'] ?? '');
2021-10-03 06:05:11 +02:00
$previousUrl = str_replace($search, '', $previousUrl);
2024-01-04 07:26:45 +01:00
// not really a fan of this, but meh.
$optionalDateFields = [
'interest_date' => $optionalFields['interest_date'],
'book_date' => $optionalFields['book_date'],
'process_date' => $optionalFields['process_date'],
'due_date' => $optionalFields['due_date'],
'payment_date' => $optionalFields['payment_date'],
'invoice_date' => $optionalFields['invoice_date'],
];
// var_dump($optionalFields);
// exit;
$optionalFields['external_url'] = $optionalFields['external_url'] ?? false;
$optionalFields['location'] = $optionalFields['location'] ?? false;
2019-05-04 20:58:43 +02:00
session()->put('preFilled', $preFilled);
2021-03-28 11:46:23 +02:00
return view(
'transactions.create',
compact(
'subTitleIcon',
'cash',
'objectType',
2024-01-04 07:26:45 +01:00
'optionalDateFields',
'subTitle',
'defaultCurrency',
2021-10-03 06:05:11 +02:00
'previousUrl',
'optionalFields',
'preFilled',
'allowedOpposingTypes',
'accountToTypes',
'sourceId',
'destinationId'
)
2019-05-04 20:58:43 +02:00
);
}
2019-08-17 12:09:03 +02:00
}