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

326 lines
12 KiB
PHP
Raw Normal View History

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