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

158 lines
5.6 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;
use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface;
2019-04-16 16:20:46 +02:00
use FireflyIII\Transformers\TransactionGroupTransformer;
2019-05-24 05:47:26 +02:00
use Illuminate\Http\Request;
2020-01-02 19:41:14 +01:00
use Illuminate\Support\Str;
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
{
/** @var TransactionGroupRepositoryInterface */
2019-04-16 16:20:46 +02:00
private $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) {
2019-04-16 16:20:46 +02:00
$this->repository = app(TransactionGroupRepositoryInterface::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
/**
* @param TransactionGroup $transactionGroup
*
* @return \Illuminate\Http\JsonResponse
*/
public function debugShow(TransactionGroup $transactionGroup)
{
return response()->json($this->repository->expandGroup($transactionGroup));
}
2019-04-08 20:31:31 +02:00
/**
* @param TransactionGroup $transactionGroup
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2020-02-14 05:46:34 +01: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 */
2019-04-16 16:20:46 +02:00
$first = $transactionGroup->transactionJournals->first();
$splits = $transactionGroup->transactionJournals->count();
2020-02-14 05:46:34 +01:00
if(null === $first) {
throw new FireflyException('This transaction is broken :(.');
}
2020-02-29 13:58:34 +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);
$transformer->setParameters(new ParameterBag);
$groupArray = $transformer->transformObject($transactionGroup);
// do some amount calculations:
$amounts = $this->getAmounts($groupArray);
2020-01-02 19:41:14 +01:00
// make sure notes are escaped but not double escaped.
foreach ($groupArray['transactions'] as $index => $transaction) {
$search = ['&amp;', '&gt;', '&lt;'];
if (!Str::contains($transaction['notes'], $search)) {
$groupArray['transactions'][$index]['notes'] = e($transaction['notes']);
}
}
$events = $this->repository->getPiggyEvents($transactionGroup);
$attachments = $this->repository->getAttachments($transactionGroup);
$links = $this->repository->getLinks($transactionGroup);
return view(
'transactions.show', compact(
'transactionGroup', 'amounts', 'first', 'type', 'subTitle', 'splits', 'groupArray',
2019-08-09 20:33:57 +02:00
'events', 'attachments', 'links'
)
);
}
/**
* @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'];
if (!isset($amounts[$symbol])) {
$amounts[$symbol] = [
'amount' => '0',
'symbol' => $symbol,
'decimal_places' => $transaction['currency_decimal_places'],
];
}
$amounts[$symbol]['amount'] = bcadd($amounts[$symbol]['amount'], $transaction['amount']);
if (null !== $transaction['foreign_amount']) {
// same for foreign currency:
$foreignSymbol = $transaction['foreign_currency_symbol'];
if (!isset($amounts[$foreignSymbol])) {
$amounts[$foreignSymbol] = [
'amount' => '0',
'symbol' => $foreignSymbol,
'decimal_places' => $transaction['foreign_currency_decimal_places'],
];
}
2019-04-18 20:05:40 +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
}
2019-08-17 12:09:03 +02:00
}