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

132 lines
4.7 KiB
PHP
Raw Normal View History

2019-04-08 13:31:31 -05:00
<?php
/**
2019-04-16 09:20:46 -05:00
* ViewController.php
2019-04-08 13:31:31 -05:00
* 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\Http\Controllers\Transaction;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface;
2019-04-16 09:20:46 -05:00
use FireflyIII\Transformers\TransactionGroupTransformer;
2019-05-23 22:47:26 -05:00
use Illuminate\Http\Request;
2019-04-16 09:20:46 -05:00
use Symfony\Component\HttpFoundation\ParameterBag;
2019-04-08 13:31:31 -05:00
/**
* Class ShowController
*/
class ShowController extends Controller
{
/** @var TransactionGroupRepositoryInterface */
2019-04-16 09:20:46 -05:00
private $repository;
2019-04-08 13:31:31 -05:00
/**
* ShowController constructor.
2019-04-08 13:31:31 -05:00
*/
public function __construct()
{
parent::__construct();
2019-04-16 09:20:46 -05:00
2019-04-08 13:31:31 -05:00
// some useful repositories:
$this->middleware(
function ($request, $next) {
2019-04-16 09:20:46 -05:00
$this->repository = app(TransactionGroupRepositoryInterface::class);
2019-04-08 13:31:31 -05:00
app('view')->share('title', (string)trans('firefly.transactions'));
2019-04-16 09:20:46 -05:00
app('view')->share('mainTitleIcon', 'fa-exchange');
2019-04-08 13:31:31 -05:00
return $next($request);
}
);
}
/**
* @param TransactionGroup $transactionGroup
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
2019-05-23 22:47:26 -05:00
public function show(Request $request, TransactionGroup $transactionGroup)
2019-04-08 13:31:31 -05:00
{
/** @var TransactionJournal $first */
2019-04-16 09:20:46 -05:00
$first = $transactionGroup->transactionJournals->first();
$splits = $transactionGroup->transactionJournals->count();
2019-08-04 10:26:00 -05:00
$type = $first->transactionType->type;
2019-04-16 09:20:46 -05:00
$title = 1 === $splits ? $first->description : $transactionGroup->title;
$subTitle = sprintf('%s: "%s"', $type, $title);
2019-07-19 23:47:34 -05:00
$message = $request->get('message');
2019-04-16 09:20:46 -05:00
/** @var TransactionGroupTransformer $transformer */
$transformer = app(TransactionGroupTransformer::class);
$transformer->setParameters(new ParameterBag);
$groupArray = $transformer->transformObject($transactionGroup);
// do some amount calculations:
$amounts = $this->getAmounts($groupArray);
$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',
'events', 'attachments', 'links', 'message'
)
);
}
/**
* @param array $group
* @return array
*/
private function getAmounts(array $group): array
{
2019-04-16 09:20:46 -05:00
$amounts = [];
foreach ($group['transactions'] as $transaction) {
2019-04-16 09:20:46 -05: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 13:05:40 -05:00
$amounts[$foreignSymbol]['amount'] = bcadd($amounts[$foreignSymbol]['amount'], $transaction['foreign_amount']);
2019-04-16 09:20:46 -05:00
}
2019-04-08 13:31:31 -05:00
}
return $amounts;
2019-04-16 09:20:46 -05:00
}
2019-04-08 13:31:31 -05:00
}