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

302 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;
2017-04-28 13:08:25 -05:00
use FireflyIII\Helpers\Filter\InternalTransferFilter;
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;
2016-10-15 05:39:34 -05:00
use FireflyIII\Repositories\Journal\JournalTaskerInterface;
use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface;
2017-03-11 00:41:26 -06:00
use FireflyIII\Support\CacheProperties;
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;
2016-04-30 23:59:08 -05:00
use Response;
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
{
/**
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');
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
/**
2016-11-26 02:16:06 -06:00
* @param Request $request
* @param JournalRepositoryInterface $repository
* @param string $what
2017-04-09 00:56:46 -05:00
* @param string $moment
*
2016-05-15 05:08:41 -05:00
* @return View
2017-12-22 11:32:43 -06:00
*
2017-12-17 07:30:53 -06:00
* @throws FireflyException
2016-04-30 23:59:08 -05:00
*/
2017-03-11 00:41:26 -06:00
public function index(Request $request, JournalRepositoryInterface $repository, string $what, string $moment = '')
2016-04-30 23:59:08 -05:00
{
2017-03-11 00:41:26 -06:00
// default values:
2016-11-05 05:47:21 -05:00
$subTitleIcon = config('firefly.transactionIconsByWhat.' . $what);
$types = config('firefly.transactionTypesByWhat.' . $what);
2017-07-30 06:42:30 -05:00
$page = intval($request->get('page'));
$pageSize = intval(Preferences::get('listPageSize', 50)->data);
2016-11-20 05:51:33 -06:00
$range = Preferences::get('viewRange', '1M')->data;
2017-03-11 00:41:26 -06:00
$start = null;
$end = null;
$periods = new Collection;
2017-07-26 09:40:00 -05:00
$path = route('transactions.index', [$what]);
2017-03-11 00:41:26 -06:00
// prep for "all" view.
2017-11-15 05:25:49 -06:00
if ('all' === $moment) {
2017-03-11 00:41:26 -06:00
$subTitle = trans('firefly.all_' . $what);
$first = $repository->first();
$start = $first->date ?? new Carbon;
$end = new Carbon;
2017-07-26 09:40:00 -05:00
$path = route('transactions.index', [$what, 'all']);
2016-11-20 05:51:33 -06:00
}
2017-03-11 00:41:26 -06:00
// prep for "specific date" view.
2017-11-15 05:25:49 -06:00
if (strlen($moment) > 0 && 'all' !== $moment) {
2017-03-11 00:41:26 -06:00
$start = new Carbon($moment);
$end = app('navigation')->endOfPeriod($start, $range);
2017-07-30 06:42:30 -05:00
$path = route('transactions.index', [$what, $moment]);
2017-03-11 00:41:26 -06:00
$subTitle = trans(
'firefly.title_' . $what . '_between',
['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]
);
$periods = $this->getPeriodOverview($what);
2017-03-11 00:41:26 -06:00
}
2016-11-20 05:51:33 -06:00
2017-03-11 00:41:26 -06:00
// prep for current period
2017-11-15 05:25:49 -06:00
if (0 === strlen($moment)) {
$start = clone session('start', app('navigation')->startOfPeriod(new Carbon, $range));
$end = clone session('end', app('navigation')->endOfPeriod(new Carbon, $range));
$periods = $this->getPeriodOverview($what);
2017-03-11 00:41:26 -06:00
$subTitle = trans(
'firefly.title_' . $what . '_between',
['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]
);
}
2016-11-20 05:51:33 -06:00
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setAllAssetAccounts()->setRange($start, $end)->setTypes($types)->setLimit($pageSize)->setPage($page)->withOpposingAccount();
$collector->removeFilter(InternalTransferFilter::class);
$transactions = $collector->getPaginatedJournals();
$transactions->setPath($path);
return view('transactions.index', compact('subTitle', 'what', 'subTitleIcon', 'transactions', 'periods', 'start', 'end', 'moment'));
2016-04-30 23:59:08 -05:00
}
/**
2017-11-17 22:46:19 -06:00
* @param Request $request
* @param JournalRepositoryInterface $repository
2017-12-17 07:30:53 -06:00
*
* @return \Illuminate\Http\JsonResponse
*/
public function reconcile(Request $request, JournalRepositoryInterface $repository)
{
$transactionIds = $request->get('transactions');
foreach ($transactionIds as $transactionId) {
$transactionId = intval($transactionId);
$transaction = $repository->findTransaction($transactionId);
Log::debug(sprintf('Transaction ID is %d', $transaction->id));
$repository->reconcile($transaction);
}
2017-12-22 11:32:43 -06:00
return Response::json(['ok' => 'reconciled']);
}
2016-04-30 23:59:08 -05:00
/**
2016-05-15 05:08:41 -05:00
* @param Request $request
2016-04-30 23:59:08 -05:00
* @param JournalRepositoryInterface $repository
*
2016-05-15 05:08:41 -05:00
* @return \Illuminate\Http\JsonResponse
2016-04-30 23:59:08 -05:00
*/
2016-05-15 05:08:41 -05:00
public function reorder(Request $request, JournalRepositoryInterface $repository)
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'));
2016-04-30 23:59:08 -05:00
if (count($ids) > 0) {
$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) {
2016-06-15 02:58:33 -05:00
$journal = $repository->find(intval($id));
2017-03-24 09:01:53 -05:00
if ($journal && $journal->date->isSameDay($date)) {
$repository->setOrder($journal, $order);
2017-11-15 05:25:49 -06:00
++$order;
2016-04-30 23:59:08 -05:00
}
}
}
Preferences::mark();
return Response::json([true]);
}
2015-02-25 14:19:06 -06:00
/**
2017-09-08 23:41:45 -05:00
* @param TransactionJournal $journal
* @param JournalTaskerInterface $tasker
* @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
2015-02-25 14:19:06 -06:00
*/
public function show(TransactionJournal $journal, JournalTaskerInterface $tasker, 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);
}
2017-12-22 11:32:43 -06:00
if (TransactionType::RECONCILIATION === $journal->transactionType->type) {
2017-12-17 07:30:53 -06:00
return redirect(route('accounts.reconcile.show', [$journal->id])); // @codeCoverageIgnore
}
$linkTypes = $linkTypeRepository->get();
$links = $linkTypeRepository->getLinks($journal);
2017-06-05 04:12:50 -05:00
$events = $tasker->getPiggyBankEvents($journal);
$transactions = $tasker->getTransactionsOverview($journal);
$what = strtolower($journal->transaction_type_type ?? $journal->transactionType->type);
2017-10-19 11:08:50 -05:00
$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
*
* @return Collection
*/
private function getPeriodOverview(string $what): Collection
2017-03-11 00:41:26 -06:00
{
$repository = app(JournalRepositoryInterface::class);
$first = $repository->first();
$start = $first->date ?? new Carbon;
$range = Preferences::get('viewRange', '1M')->data;
$start = app('navigation')->startOfPeriod($start, $range);
$end = app('navigation')->endOfX(new Carbon, $range, null);
2017-03-11 00:41:26 -06:00
$entries = new Collection;
$types = config('firefly.transactionTypesByWhat.' . $what);
// properties for cache
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($what);
$cache->addProperty('transaction-list-entries');
if ($cache->has()) {
2017-09-08 23:41:45 -05:00
return $cache->get(); // @codeCoverageIgnore
2017-03-11 00:41:26 -06:00
}
2017-03-17 10:34:57 -05:00
Log::debug(sprintf('Going to get period expenses and incomes between %s and %s.', $start->format('Y-m-d'), $end->format('Y-m-d')));
2017-03-11 00:41:26 -06:00
while ($end >= $start) {
2017-03-17 10:34:57 -05:00
Log::debug('Loop start!');
$end = app('navigation')->startOfPeriod($end, $range);
$currentEnd = app('navigation')->endOfPeriod($end, $range);
2017-03-11 00:41:26 -06:00
// count journals without budget in this period:
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
2017-04-28 13:08:25 -05:00
$collector->setAllAssetAccounts()->setRange($end, $currentEnd)->withOpposingAccount()->setTypes($types);
$collector->removeFilter(InternalTransferFilter::class);
2017-08-30 00:40:39 -05:00
$journals = $collector->getJournals();
$sum = $journals->sum('transaction_amount');
// count per currency:
$sums = $this->sumPerCurrency($journals);
2017-03-11 00:41:26 -06:00
$dateStr = $end->format('Y-m-d');
$dateName = app('navigation')->periodShow($end, $range);
2017-03-11 00:41:26 -06:00
$array = [
2017-08-30 00:40:39 -05:00
'string' => $dateStr,
'name' => $dateName,
'sum' => $sum,
'sums' => $sums,
'date' => clone $end,
2017-03-11 00:41:26 -06:00
];
2017-03-17 10:34:57 -05:00
Log::debug(sprintf('What is %s', $what));
2017-08-30 00:40:39 -05:00
if ($journals->count() > 0) {
2017-12-17 07:30:53 -06:00
$entries->push($array); // @codeCoverageIgnore
2017-03-11 00:41:26 -06:00
}
$end = app('navigation')->subtractPeriod($end, $range, 1);
2017-03-11 00:41:26 -06:00
}
2017-03-17 10:34:57 -05:00
Log::debug('End of loop');
2017-03-11 00:41:26 -06:00
$cache->store($entries);
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) {
$currencyId = $transaction->transaction_currency_id;
// 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
}