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

239 lines
9.2 KiB
PHP
Raw Normal View History

2019-04-08 20:31:31 +02:00
<?php
2019-04-08 20:31:31 +02:00
/**
* ShowController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2019-04-08 20:31:31 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2019-04-08 20:31:31 +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-08 20:31:31 +02:00
*
* This program is distributed in the hope that it will be useful,
2019-04-08 20:31:31 +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-08 20:31:31 +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-08 20:31:31 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Transaction;
2020-02-14 05:46:34 +01:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
2019-04-08 20:31:31 +02:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
2022-10-02 14:37:50 +02:00
use FireflyIII\Repositories\AuditLogEntry\ALERepositoryInterface;
2019-04-08 20:31:31 +02:00
use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface;
use FireflyIII\Support\JsonApi\Enrichments\TransactionGroupEnrichment;
2019-04-16 16:20:46 +02:00
use FireflyIII\Transformers\TransactionGroupTransformer;
use FireflyIII\User;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\JsonResponse;
use Illuminate\View\View;
2019-04-16 16:20:46 +02:00
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2019-04-08 20:31:31 +02:00
/**
* Class ShowController
*/
class ShowController extends Controller
{
private ALERepositoryInterface $aleRepository;
2022-12-29 19:42:26 +01:00
private TransactionGroupRepositoryInterface $repository;
2019-04-08 20:31:31 +02:00
/**
* ShowController constructor.
2019-04-08 20:31:31 +02:00
*/
public function __construct()
{
parent::__construct();
2019-04-16 16:20:46 +02:00
2019-04-08 20:31:31 +02:00
// some useful repositories:
$this->middleware(
function ($request, $next) {
2022-10-02 14:37:50 +02:00
$this->repository = app(TransactionGroupRepositoryInterface::class);
$this->aleRepository = app(ALERepositoryInterface::class);
2019-04-08 20:31:31 +02:00
app('view')->share('title', (string)trans('firefly.transactions'));
2019-04-16 16:20:46 +02:00
app('view')->share('mainTitleIcon', 'fa-exchange');
2019-04-08 20:31:31 +02:00
return $next($request);
}
);
}
2020-02-08 06:42:07 +01:00
/**
* @return JsonResponse
2020-02-08 06:42:07 +01:00
*/
public function debugShow(TransactionGroup $transactionGroup)
{
return response()->json($this->repository->expandGroup($transactionGroup));
}
2019-04-08 20:31:31 +02:00
/**
* @return Factory|View
2023-12-20 19:35:52 +01:00
*
2021-03-28 11:46:23 +02:00
* @throws FireflyException
2019-04-08 20:31:31 +02:00
*/
2023-12-10 06:51:59 +01:00
public function show(TransactionGroup $transactionGroup)
2019-04-08 20:31:31 +02:00
{
/** @var User $admin */
$admin = auth()->user();
// use new group collector:
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setUser($admin)->setTransactionGroup($transactionGroup)->withAPIInformation();
$selectedGroup = $collector->getGroups()->first();
if (null === $selectedGroup) {
throw new NotFoundHttpException();
}
// enrich
$enrichment = new TransactionGroupEnrichment();
$enrichment->setUser($admin);
$selectedGroup = $enrichment->enrichSingle($selectedGroup);
2023-12-20 19:35:52 +01:00
/** @var null|TransactionJournal $first */
$first = $transactionGroup->transactionJournals()->first(['transaction_journals.*']);
$splits = $transactionGroup->transactionJournals()->count();
$splits = count($selectedGroup['transactions']);
$keys = array_keys($selectedGroup['transactions']);
$first = $selectedGroup['transactions'][array_shift($keys)];
unset($keys);
2020-02-14 05:46:34 +01:00
2020-03-16 19:40:48 +01:00
if (null === $first) {
2020-02-14 05:46:34 +01:00
throw new FireflyException('This transaction is broken :(.');
}
$type = (string)trans(sprintf('firefly.%s', $first['transaction_type_type']));
$title = 1 === $splits ? $first['description'] : $selectedGroup['title'];
$subTitle = sprintf('%s: "%s"', $type, $title);
2020-02-14 05:46:34 +01:00
// enrich
$enrichment = new TransactionGroupEnrichment();
$enrichment->setUser($admin);
$selectedGroup = $enrichment->enrichSingle($selectedGroup);
2019-04-16 16:20:46 +02:00
/** @var TransactionGroupTransformer $transformer */
$transformer = app(TransactionGroupTransformer::class);
2022-10-30 14:24:28 +01:00
$transformer->setParameters(new ParameterBag());
$groupArray = $transformer->transformObject($transactionGroup);
2019-04-16 16:20:46 +02:00
// do some calculations:
$amounts = $this->getAmounts($selectedGroup);
$accounts = $this->getAccounts($selectedGroup);
foreach (array_keys($selectedGroup['transactions']) as $index) {
$selectedGroup['transactions'][$index]['tags'] = $this->repository->getTagObjects((int)$selectedGroup['transactions'][$index]['transaction_journal_id']);
2020-01-02 19:41:14 +01:00
}
2022-10-02 14:37:50 +02:00
// get audit log entries:
2023-06-21 05:55:57 +02:00
$groupLogEntries = $this->aleRepository->getForObject($transactionGroup);
$logEntries = [];
foreach ($selectedGroup['transactions'] as $journal) {
$logEntries[$journal['transaction_journal_id']] = $this->aleRepository->getForId(TransactionJournal::class, $journal['transaction_journal_id']);
2022-10-02 14:37:50 +02:00
}
$events = $this->repository->getPiggyEvents($transactionGroup);
$attachments = $this->repository->getAttachments($transactionGroup);
$links = $this->repository->getLinks($transactionGroup);
return view(
2020-03-16 19:40:48 +01:00
'transactions.show',
compact(
'transactionGroup',
'amounts',
'first',
'type',
2022-10-02 14:37:50 +02:00
'logEntries',
2023-06-21 05:55:57 +02:00
'groupLogEntries',
2020-03-16 19:40:48 +01:00
'subTitle',
'splits',
'selectedGroup',
2020-03-16 19:40:48 +01:00
'groupArray',
'events',
'attachments',
'links',
'accounts',
2020-03-16 19:40:48 +01:00
)
);
}
private function getAmounts(array $group): array
{
2019-04-16 16:20:46 +02:00
$amounts = [];
foreach ($group['transactions'] as $transaction) {
// add normal amount:
$symbol = $transaction['currency_symbol'];
$amounts[$symbol] ??= [
'amount' => '0',
'symbol' => $symbol,
'decimal_places' => $transaction['currency_decimal_places'],
];
$amounts[$symbol]['amount'] = bcadd($amounts[$symbol]['amount'], (string)$transaction['amount']);
// add foreign amount:
if (null !== $transaction['foreign_amount'] && '' !== $transaction['foreign_amount'] && 0 !== bccomp('0', (string)$transaction['foreign_amount'])) {
// same for foreign currency:
$foreignSymbol = $transaction['foreign_currency_symbol'];
$amounts[$foreignSymbol] ??= [
2019-04-16 16:20:46 +02:00
'amount' => '0',
'symbol' => $foreignSymbol,
'decimal_places' => $transaction['foreign_currency_decimal_places'],
2019-04-16 16:20:46 +02:00
];
$amounts[$foreignSymbol]['amount'] = bcadd($amounts[$foreignSymbol]['amount'], (string)$transaction['foreign_amount']);
2019-04-16 16:20:46 +02:00
}
// add primary currency amount
if (null !== $transaction['pc_amount'] && $transaction['currency_id'] !== $this->primaryCurrency->id) {
2019-04-16 16:20:46 +02:00
// same for foreign currency:
$primarySymbol = $this->primaryCurrency->symbol;
$amounts[$primarySymbol] ??= [
'amount' => '0',
'symbol' => $this->primaryCurrency->symbol,
'decimal_places' => $this->primaryCurrency->decimal_places,
];
$amounts[$primarySymbol]['amount'] = bcadd($amounts[$primarySymbol]['amount'], (string)$transaction['pc_amount']);
2019-04-16 16:20:46 +02:00
}
2019-04-08 20:31:31 +02:00
}
return $amounts;
2019-04-16 16:20:46 +02:00
}
2023-06-21 05:55:57 +02:00
private function getAccounts(array $group): array
{
$accounts = [
2023-11-28 04:45:07 +01:00
'source' => [],
'destination' => [],
];
2023-06-21 05:55:57 +02:00
foreach ($group['transactions'] as $transaction) {
$accounts['source'][] = [
'type' => $transaction['source_account_type'],
'id' => $transaction['source_account_id'],
'name' => $transaction['source_account_name'],
'iban' => $transaction['source_account_iban'],
2023-06-21 05:55:57 +02:00
];
$accounts['destination'][] = [
'type' => $transaction['destination_account_type'],
'id' => $transaction['destination_account_id'],
'name' => $transaction['destination_account_name'],
'iban' => $transaction['destination_account_iban'],
2023-06-21 05:55:57 +02:00
];
}
$accounts['source'] = array_unique($accounts['source'], SORT_REGULAR);
$accounts['destination'] = array_unique($accounts['destination'], SORT_REGULAR);
return $accounts;
}
2019-08-17 12:09:03 +02:00
}