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

218 lines
7.5 KiB
PHP
Raw Normal View History

2019-04-08 20:31:31 +02:00
<?php
/**
* 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;
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;
2019-04-16 16:20:46 +02:00
use FireflyIII\Transformers\TransactionGroupTransformer;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\JsonResponse;
2019-05-24 05:47:26 +02:00
use Illuminate\Http\Request;
use Illuminate\View\View;
2019-04-16 16:20:46 +02:00
use Symfony\Component\HttpFoundation\ParameterBag;
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
2022-12-29 19:42:26 +01: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
/**
2023-06-21 05:55:57 +02:00
* @param TransactionGroup $transactionGroup
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
/**
2023-06-21 05:55:57 +02:00
* @param Request $request
* @param TransactionGroup $transactionGroup
2019-04-08 20:31:31 +02:00
*
* @return Factory|View
2021-03-28 11:46:23 +02:00
* @throws FireflyException
2019-04-08 20:31:31 +02:00
*/
2019-05-24 05:47:26 +02:00
public function show(Request $request, TransactionGroup $transactionGroup)
2019-04-08 20:31:31 +02:00
{
/** @var TransactionJournal $first */
$first = $transactionGroup->transactionJournals()->first(['transaction_journals.*']);
$splits = $transactionGroup->transactionJournals()->count();
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 :(.');
}
2022-12-29 19:42:26 +01:00
$type = (string)trans(sprintf('firefly.%s', $first->transactionType->type));
2019-04-16 16:20:46 +02:00
$title = 1 === $splits ? $first->description : $transactionGroup->title;
$subTitle = sprintf('%s: "%s"', $type, $title);
/** @var TransactionGroupTransformer $transformer */
$transformer = app(TransactionGroupTransformer::class);
2022-10-30 14:24:28 +01:00
$transformer->setParameters(new ParameterBag());
2019-04-16 16:20:46 +02:00
$groupArray = $transformer->transformObject($transactionGroup);
// do some calculations:
2021-03-28 11:46:23 +02:00
$amounts = $this->getAmounts($groupArray);
$accounts = $this->getAccounts($groupArray);
2020-01-02 19:41:14 +01:00
foreach ($groupArray['transactions'] as $index => $transaction) {
2023-06-21 05:55:57 +02:00
$groupArray['transactions'][$index]['tags'] = $this->repository->getTagObjects(
$groupArray['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 = [];
2022-10-30 14:24:28 +01:00
foreach ($transactionGroup->transactionJournals as $journal) {
$logEntries[$journal->id] = $this->aleRepository->getForObject($journal);
2022-10-02 14:37:50 +02:00
}
$events = $this->repository->getPiggyEvents($transactionGroup);
$attachments = $this->repository->getAttachments($transactionGroup);
$links = $this->repository->getLinks($transactionGroup);
2022-10-02 14:37:50 +02:00
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',
'groupArray',
'events',
'attachments',
'links',
'accounts',
2020-03-16 19:40:48 +01:00
)
);
}
2023-05-29 13:56:55 +02:00
/**
2023-06-21 05:55:57 +02:00
* @param array $group
2020-02-08 06:42:07 +01:00
*
* @return array
*/
private function getAmounts(array $group): array
{
2019-04-16 16:20:46 +02:00
$amounts = [];
foreach ($group['transactions'] as $transaction) {
2019-04-16 16:20:46 +02:00
$symbol = $transaction['currency_symbol'];
2021-04-07 07:28:43 +02:00
if (!array_key_exists($symbol, $amounts)) {
2019-04-16 16:20:46 +02:00
$amounts[$symbol] = [
'amount' => '0',
'symbol' => $symbol,
'decimal_places' => $transaction['currency_decimal_places'],
];
}
$amounts[$symbol]['amount'] = bcadd($amounts[$symbol]['amount'], $transaction['amount']);
2023-06-21 05:55:57 +02:00
if (null !== $transaction['foreign_amount'] && '' !== $transaction['foreign_amount']
&& bccomp(
'0',
$transaction['foreign_amount']
) !== 0) {
2019-04-16 16:20:46 +02:00
// same for foreign currency:
$foreignSymbol = $transaction['foreign_currency_symbol'];
2021-04-07 07:28:43 +02:00
if (!array_key_exists($foreignSymbol, $amounts)) {
2019-04-16 16:20:46 +02:00
$amounts[$foreignSymbol] = [
'amount' => '0',
'symbol' => $foreignSymbol,
'decimal_places' => $transaction['foreign_currency_decimal_places'],
];
}
2023-06-21 05:55:57 +02:00
$amounts[$foreignSymbol]['amount'] = bcadd(
$amounts[$foreignSymbol]['amount'],
$transaction['foreign_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
/**
* @param array $group
*
* @return array
*/
private function getAccounts(array $group): array
{
$accounts = [];
foreach ($group['transactions'] as $transaction) {
$accounts['source'][] = [
'type' => $transaction['source_type'],
'id' => $transaction['source_id'],
'name' => $transaction['source_name'],
'iban' => $transaction['source_iban'],
];
$accounts['destination'][] = [
'type' => $transaction['destination_type'],
'id' => $transaction['destination_id'],
'name' => $transaction['destination_name'],
'iban' => $transaction['destination_iban'],
];
}
$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
}