Files
firefly-iii/app/Http/Controllers/TransactionController.php

344 lines
13 KiB
PHP
Raw Normal View History

2016-05-01 06:37:47 +02:00
<?php
/**
2016-05-01 06:59:08 +02:00
* TransactionController.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
2016-05-01 06:37:47 +02:00
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02:00
* 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
2017-12-17 14:41:58 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2016-05-01 06:37:47 +02:00
*/
2018-07-08 12:08:53 +02:00
/** @noinspection CallableParameterUseCaseInTypeContextInspection */
/** @noinspection MoreThanThreeArgumentsInspection */
2017-03-24 15:01:53 +01:00
declare(strict_types=1);
2016-05-01 06:59:08 +02:00
namespace FireflyIII\Http\Controllers;
2015-02-23 21:55:52 +01:00
2016-05-01 06:59:08 +02:00
use Carbon\Carbon;
2017-03-11 07:41:26 +01:00
use FireflyIII\Exceptions\FireflyException;
2016-11-20 12:51:33 +01:00
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
2018-03-25 13:30:55 +02:00
use FireflyIII\Helpers\Filter\CountAttachmentsFilter;
2017-04-28 20:08:25 +02:00
use FireflyIII\Helpers\Filter\InternalTransferFilter;
2018-03-25 13:30:55 +02:00
use FireflyIII\Helpers\Filter\SplitIndicatorFilter;
2017-08-30 07:40:39 +02:00
use FireflyIII\Models\Transaction;
2015-02-25 21:19:06 +01:00
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
2015-02-24 22:53:38 +01:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface;
2018-03-11 14:09:44 +01:00
use FireflyIII\Transformers\TransactionTransformer;
2018-06-09 07:09:43 +02:00
use Illuminate\Http\JsonResponse;
2016-05-15 12:08:41 +02:00
use Illuminate\Http\Request;
2016-11-20 12:51:33 +01:00
use Illuminate\Support\Collection;
use Log;
2018-03-11 14:09:44 +01:00
use Symfony\Component\HttpFoundation\ParameterBag;
2015-04-01 09:16:41 +02:00
use View;
2015-02-27 14:27:04 +01:00
2015-02-23 21:55:52 +01:00
/**
2017-11-15 12:25:49 +01:00
* Class TransactionController.
2018-07-20 14:34:56 +02:00
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2015-02-23 21:55:52 +01:00
*/
2016-05-01 06:59:08 +02:00
class TransactionController extends Controller
2015-02-23 21:55:52 +01:00
{
/** @var JournalRepositoryInterface Journals and transactions overview */
2018-03-25 13:30:55 +02:00
private $repository;
2015-02-23 21:55:52 +01:00
/**
2016-10-21 19:20:03 +02:00
* TransactionController constructor.
2015-02-23 21:55:52 +01:00
*/
public function __construct()
{
2015-04-28 15:26:30 +02:00
parent::__construct();
2016-10-29 07:44:46 +02:00
$this->middleware(
function ($request, $next) {
2018-07-15 09:38:49 +02:00
app('view')->share('title', (string)trans('firefly.transactions'));
2017-12-16 19:46:36 +01:00
app('view')->share('mainTitleIcon', 'fa-repeat');
2018-03-25 13:30:55 +02:00
$this->repository = app(JournalRepositoryInterface::class);
2016-10-29 07:44:46 +02:00
return $next($request);
}
);
2015-02-27 14:27:04 +01:00
}
2016-05-01 06:59:08 +02:00
/**
2018-03-25 13:30:55 +02:00
* Index for a range of transactions.
*
2018-07-08 12:08:53 +02:00
* @param Request $request
* @param string $what
* @param Carbon|null $start
* @param Carbon|null $end
2017-12-22 18:32:43 +01:00
*
2018-07-08 12:08:53 +02:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2016-05-01 06:59:08 +02:00
*/
2018-03-25 13:30:55 +02:00
public function index(Request $request, string $what, Carbon $start = null, Carbon $end = null)
2016-05-01 06:59:08 +02:00
{
2016-11-05 11:47:21 +01:00
$subTitleIcon = config('firefly.transactionIconsByWhat.' . $what);
$types = config('firefly.transactionTypesByWhat.' . $what);
2018-03-25 13:30:55 +02:00
$page = (int)$request->get('page');
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data;
2018-03-25 13:30:55 +02:00
if (null === $start) {
$start = session('start');
$end = session('end');
2016-11-20 12:51:33 +01:00
}
2018-03-25 13:30:55 +02:00
if (null === $end) {
$end = session('end');
2017-03-11 07:41:26 +01:00
}
2016-11-20 12:51:33 +01:00
2018-03-25 13:30:55 +02:00
if ($end < $start) {
2018-04-02 15:10:40 +02:00
[$start, $end] = [$end, $start];
2017-03-11 07:41:26 +01:00
}
2018-06-09 07:09:43 +02:00
$path = route('transactions.index', [$what, $start->format('Y-m-d'), $end->format('Y-m-d')]);
2018-03-25 13:30:55 +02:00
$startStr = $start->formatLocalized($this->monthAndDayFormat);
$endStr = $end->formatLocalized($this->monthAndDayFormat);
2018-07-15 09:38:49 +02:00
$subTitle = (string)trans('firefly.title_' . $what . '_between', ['start' => $startStr, 'end' => $endStr]);
2018-03-25 13:30:55 +02:00
$periods = $this->getPeriodOverview($what, $end);
2016-11-20 12:51:33 +01:00
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
2018-03-25 13:30:55 +02:00
$collector->setAllAssetAccounts()->setRange($start, $end)
->setTypes($types)->setLimit($pageSize)->setPage($page)->withOpposingAccount()
->withBudgetInformation()->withCategoryInformation();
$collector->removeFilter(InternalTransferFilter::class);
2018-03-25 13:30:55 +02:00
$collector->addFilter(SplitIndicatorFilter::class);
$collector->addFilter(CountAttachmentsFilter::class);
$transactions = $collector->getPaginatedJournals();
$transactions->setPath($path);
2018-03-25 13:30:55 +02:00
return view('transactions.index', compact('subTitle', 'what', 'subTitleIcon', 'transactions', 'periods', 'start', 'end'));
2016-05-01 06:59:08 +02:00
}
/**
2018-07-22 08:10:16 +02:00
* Index for ALL transactions.
*
2018-03-25 13:30:55 +02:00
* @param Request $request
* @param string $what
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function indexAll(Request $request, string $what)
{
$subTitleIcon = config('firefly.transactionIconsByWhat.' . $what);
$types = config('firefly.transactionTypesByWhat.' . $what);
$page = (int)$request->get('page');
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data;
2018-03-25 13:30:55 +02:00
$path = route('transactions.index.all', [$what]);
$first = $this->repository->firstNull();
$start = null === $first ? new Carbon : $first->date;
2018-03-25 13:30:55 +02:00
$end = new Carbon;
2018-07-15 09:38:49 +02:00
$subTitle = (string)trans('firefly.all_' . $what);
2018-03-25 13:30:55 +02:00
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setAllAssetAccounts()->setRange($start, $end)
->setTypes($types)->setLimit($pageSize)->setPage($page)->withOpposingAccount()
->withBudgetInformation()->withCategoryInformation();
$collector->removeFilter(InternalTransferFilter::class);
$collector->addFilter(SplitIndicatorFilter::class);
$collector->addFilter(CountAttachmentsFilter::class);
$transactions = $collector->getPaginatedJournals();
$transactions->setPath($path);
return view('transactions.index', compact('subTitle', 'what', 'subTitleIcon', 'transactions', 'start', 'end'));
}
/**
2018-07-22 08:10:16 +02:00
* Do a reconciliation.
*
2018-03-25 13:30:55 +02:00
* @param Request $request
2017-12-17 14:30:53 +01:00
*
2018-06-09 07:09:43 +02:00
* @return JsonResponse
*/
2018-06-09 07:09:43 +02:00
public function reconcile(Request $request): JsonResponse
{
$transactionIds = $request->get('transactions');
foreach ($transactionIds as $transactionId) {
2018-03-25 13:30:55 +02:00
$transactionId = (int)$transactionId;
$transaction = $this->repository->findTransaction($transactionId);
2018-07-09 19:24:08 +02:00
if (null !== $transaction) {
Log::debug(sprintf('Transaction ID is %d', $transaction->id));
$this->repository->reconcile($transaction);
}
}
2017-12-22 18:32:43 +01:00
2018-03-10 20:30:09 +01:00
return response()->json(['ok' => 'reconciled']);
}
2016-05-01 06:59:08 +02:00
/**
2018-07-22 08:10:16 +02:00
* Reorder transactions.
*
2018-03-25 13:30:55 +02:00
* @param Request $request
2016-05-01 06:59:08 +02:00
*
2016-05-15 12:08:41 +02:00
* @return \Illuminate\Http\JsonResponse
2018-07-20 14:34:56 +02:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2016-05-01 06:59:08 +02:00
*/
2018-07-08 12:28:42 +02:00
public function reorder(Request $request): JsonResponse
2016-05-01 06:59:08 +02:00
{
2016-05-15 12:08:41 +02:00
$ids = $request->get('items');
$date = new Carbon($request->get('date'));
2018-04-28 06:23:13 +02:00
if (\count($ids) > 0) {
2016-05-01 06:59:08 +02:00
$order = 0;
2016-10-10 07:49:39 +02:00
$ids = array_unique($ids);
2016-05-01 06:59:08 +02:00
foreach ($ids as $id) {
2018-07-08 07:59:58 +02:00
$journal = $this->repository->findNull((int)$id);
if (null !== $journal && $journal->date->isSameDay($date)) {
2018-03-25 13:30:55 +02:00
$this->repository->setOrder($journal, $order);
2017-11-15 12:25:49 +01:00
++$order;
2016-05-01 06:59:08 +02:00
}
}
}
2018-07-08 12:08:53 +02:00
app('preferences')->mark();
2016-05-01 06:59:08 +02:00
2018-03-10 20:30:09 +01:00
return response()->json([true]);
2016-05-01 06:59:08 +02:00
}
2015-02-25 21:19:06 +01:00
/**
2018-07-22 08:10:16 +02:00
* Show a transaction.
*
2017-09-09 06:41:45 +02:00
* @param TransactionJournal $journal
* @param LinkTypeRepositoryInterface $linkTypeRepository
2015-02-25 21:19:06 +01:00
*
2017-07-23 08:16:11 +02:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View
2018-03-11 16:24:07 +01:00
* @throws FireflyException
2015-02-25 21:19:06 +01:00
*/
2018-03-25 13:30:55 +02:00
public function show(TransactionJournal $journal, LinkTypeRepositoryInterface $linkTypeRepository)
2015-02-25 21:19:06 +01:00
{
2016-11-22 19:10:17 +01:00
if ($this->isOpeningBalance($journal)) {
return $this->redirectToAccount($journal);
}
2018-03-11 14:09:44 +01:00
$transactionType = $journal->transactionType->type;
if (TransactionType::RECONCILIATION === $transactionType) {
2017-12-17 14:30:53 +01:00
return redirect(route('accounts.reconcile.show', [$journal->id])); // @codeCoverageIgnore
}
2018-03-11 14:09:44 +01:00
$linkTypes = $linkTypeRepository->get();
$links = $linkTypeRepository->getLinks($journal);
// get transactions using the collector:
$collector = app(JournalCollectorInterface::class);
$collector->setUser(auth()->user());
$collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
// filter on specific journals.
$collector->setJournals(new Collection([$journal]));
$set = $collector->getJournals();
$transactions = [];
$transformer = new TransactionTransformer(new ParameterBag);
/** @var Transaction $transaction */
foreach ($set as $transaction) {
$transactions[] = $transformer->transform($transaction);
}
2018-03-25 13:30:55 +02:00
$events = $this->repository->getPiggyBankEvents($journal);
2018-03-11 14:09:44 +01:00
$what = strtolower($transactionType);
2018-07-15 09:38:49 +02:00
$subTitle = (string)trans('firefly.' . $what) . ' "' . $journal->description . '"';
2017-08-30 07:40:39 +02:00
return view('transactions.show', compact('journal', 'events', 'subTitle', 'what', 'transactions', 'linkTypes', 'links'));
2015-02-25 21:19:06 +01:00
}
2016-11-22 19:10:17 +01:00
2017-03-11 07:41:26 +01:00
/**
2018-07-22 08:10:16 +02:00
* Get period overview for index.
*
2017-03-11 07:41:26 +01:00
* @param string $what
*
2018-03-27 19:29:58 +02:00
* @param Carbon $date
*
2017-03-11 07:41:26 +01:00
* @return Collection
2018-07-20 14:34:56 +02:00
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2017-03-11 07:41:26 +01:00
*/
2018-03-25 13:30:55 +02:00
private function getPeriodOverview(string $what, Carbon $date): Collection
2017-03-11 07:41:26 +01:00
{
$range = app('preferences')->get('viewRange', '1M')->data;
2018-04-02 15:10:40 +02:00
$first = $this->repository->firstNull();
2018-07-23 21:49:15 +02:00
$start = Carbon::create()->subYear();
2018-03-25 13:30:55 +02:00
$types = config('firefly.transactionTypesByWhat.' . $what);
$entries = new Collection;
if (null !== $first) {
$start = $first->date;
2017-03-11 07:41:26 +01:00
}
2018-03-25 13:30:55 +02:00
if ($date < $start) {
2018-04-02 15:10:40 +02:00
[$start, $date] = [$date, $start]; // @codeCoverageIgnore
2018-03-25 13:30:55 +02:00
}
2018-03-27 19:29:58 +02:00
2018-03-25 13:30:55 +02:00
/** @var array $dates */
$dates = app('navigation')->blockPeriods($start, $date, $range);
2017-03-11 07:41:26 +01:00
2018-03-25 13:30:55 +02:00
foreach ($dates as $currentDate) {
2017-03-11 07:41:26 +01:00
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
2018-03-25 13:30:55 +02:00
$collector->setAllAssetAccounts()->setRange($currentDate['start'], $currentDate['end'])->withOpposingAccount()->setTypes($types);
2017-04-28 20:08:25 +02:00
$collector->removeFilter(InternalTransferFilter::class);
2017-08-30 07:40:39 +02:00
$journals = $collector->getJournals();
2018-03-25 13:30:55 +02:00
2017-08-30 07:40:39 +02:00
if ($journals->count() > 0) {
2018-03-25 13:30:55 +02:00
$sums = $this->sumPerCurrency($journals);
$dateName = app('navigation')->periodShow($currentDate['start'], $currentDate['period']);
$sum = $journals->sum('transaction_amount');
2018-07-09 19:24:08 +02:00
/** @noinspection PhpUndefinedMethodInspection */
2018-03-25 13:30:55 +02:00
$entries->push(
[
2018-07-13 15:50:42 +02:00
'name' => $dateName,
'sums' => $sums,
'sum' => $sum,
2018-03-25 13:30:55 +02:00
'start' => $currentDate['start']->format('Y-m-d'),
'end' => $currentDate['end']->format('Y-m-d'),
]
);
2017-03-11 07:41:26 +01:00
}
}
return $entries;
}
2017-08-30 07:40:39 +02:00
/**
2018-07-22 08:10:16 +02:00
* Collect the sum per currency.
*
2017-08-30 07:40:39 +02:00
* @param Collection $collection
*
* @return array
*/
private function sumPerCurrency(Collection $collection): array
{
$return = [];
/** @var Transaction $transaction */
foreach ($collection as $transaction) {
2018-03-25 13:30:55 +02:00
$currencyId = (int)$transaction->transaction_currency_id;
2017-08-30 07:40:39 +02:00
// save currency information:
if (!isset($return[$currencyId])) {
$currencySymbol = $transaction->transaction_currency_symbol;
$decimalPlaces = $transaction->transaction_currency_dp;
$currencyCode = $transaction->transaction_currency_code;
$return[$currencyId] = [
'currency' => [
'id' => $currencyId,
'code' => $currencyCode,
'symbol' => $currencySymbol,
'dp' => $decimalPlaces,
],
'sum' => '0',
'count' => 0,
];
}
// save amount:
$return[$currencyId]['sum'] = bcadd($return[$currencyId]['sum'], $transaction->transaction_amount);
2017-11-15 12:25:49 +01:00
++$return[$currencyId]['count'];
2017-08-30 07:40:39 +02:00
}
asort($return);
return $return;
}
2016-05-01 06:59:08 +02:00
}