2016-04-30 23:37:47 -05:00
|
|
|
<?php
|
|
|
|
/**
|
2016-04-30 23:59:08 -05:00
|
|
|
* TransactionController.php
|
2016-04-30 23:37:47 -05:00
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-10-04 23:52:15 -05:00
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
2016-04-30 23:37:47 -05:00
|
|
|
*/
|
|
|
|
|
2017-03-24 09:01:53 -05:00
|
|
|
declare(strict_types=1);
|
2016-05-20 05:27:31 -05:00
|
|
|
|
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;
|
2015-02-25 14:19:06 -06:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
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;
|
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;
|
2016-11-19 05:57:35 -06:00
|
|
|
use Log;
|
2016-11-20 05:51:33 -06:00
|
|
|
use Navigation;
|
2015-06-02 10:44:50 -05:00
|
|
|
use Preferences;
|
2016-04-30 23:59:08 -05:00
|
|
|
use Response;
|
2017-03-11 00:41:26 -06:00
|
|
|
use Steam;
|
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
|
|
|
/**
|
2016-04-30 23:59:08 -05:00
|
|
|
* Class TransactionController
|
2015-02-23 14:55:52 -06:00
|
|
|
*
|
2016-04-30 23:59:08 -05:00
|
|
|
* @package FireflyIII\Http\Controllers
|
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) {
|
|
|
|
View::share('title', trans('firefly.transactions'));
|
|
|
|
View::share('mainTitleIcon', 'fa-repeat');
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
2016-09-09 04:19:40 -05:00
|
|
|
|
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
|
2016-04-30 23:59:08 -05:00
|
|
|
*
|
2017-04-09 00:56:46 -05:00
|
|
|
* @param string $moment
|
|
|
|
*
|
2016-05-15 05:08:41 -05:00
|
|
|
* @return View
|
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-15 09:41:07 -05:00
|
|
|
$page = intval($request->get('page')) === 0 ? 1 : intval($request->get('page'));
|
2017-03-11 00:41:26 -06:00
|
|
|
$pageSize = intval(Preferences::get('transactionPageSize', 50)->data);
|
|
|
|
$count = 0;
|
|
|
|
$loop = 0;
|
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-06-20 14:04:25 -05:00
|
|
|
$path = '/transactions/' . $what;
|
2017-03-11 00:41:26 -06:00
|
|
|
|
|
|
|
// prep for "all" view.
|
|
|
|
if ($moment === 'all') {
|
|
|
|
$subTitle = trans('firefly.all_' . $what);
|
|
|
|
$first = $repository->first();
|
|
|
|
$start = $first->date ?? new Carbon;
|
|
|
|
$end = new Carbon;
|
2017-07-07 01:09:42 -05:00
|
|
|
$path = '/transactions/' . $what . '/all/';
|
2016-11-20 05:51:33 -06:00
|
|
|
}
|
|
|
|
|
2017-03-11 00:41:26 -06:00
|
|
|
// prep for "specific date" view.
|
|
|
|
if (strlen($moment) > 0 && $moment !== 'all') {
|
|
|
|
$start = new Carbon($moment);
|
|
|
|
$end = Navigation::endOfPeriod($start, $range);
|
2017-07-13 23:41:10 -05:00
|
|
|
$path = '/transactions/' . $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)]
|
|
|
|
);
|
2017-03-18 02:09:14 -05:00
|
|
|
$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
|
|
|
|
if (strlen($moment) === 0) {
|
|
|
|
$start = clone session('start', Navigation::startOfPeriod(new Carbon, $range));
|
|
|
|
$end = clone session('end', Navigation::endOfPeriod(new Carbon, $range));
|
2017-03-18 02:09:14 -05:00
|
|
|
$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)]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// grab journals, but be prepared to jump a period back to get the right ones:
|
|
|
|
Log::info('Now at transaction loop start.');
|
|
|
|
while ($count === 0 && $loop < 3) {
|
|
|
|
$loop++;
|
|
|
|
Log::info('Count is zero, search for journals.');
|
|
|
|
/** @var JournalCollectorInterface $collector */
|
|
|
|
$collector = app(JournalCollectorInterface::class);
|
2017-04-28 13:08:25 -05:00
|
|
|
$collector->setAllAssetAccounts()->setRange($start, $end)->setTypes($types)->setLimit($pageSize)->setPage($page)->withOpposingAccount();
|
|
|
|
$collector->removeFilter(InternalTransferFilter::class);
|
2017-03-11 00:41:26 -06:00
|
|
|
$journals = $collector->getPaginatedJournals();
|
2017-06-20 14:04:25 -05:00
|
|
|
$journals->setPath($path);
|
2017-03-11 00:41:26 -06:00
|
|
|
$count = $journals->getCollection()->count();
|
2017-07-01 03:29:07 -05:00
|
|
|
if ($count === 0 && $loop < 3) {
|
2017-03-11 00:41:26 -06:00
|
|
|
$start->subDay();
|
|
|
|
$start = Navigation::startOfPeriod($start, $range);
|
|
|
|
$end = Navigation::endOfPeriod($start, $range);
|
|
|
|
Log::info(sprintf('Count is still zero, go back in time to "%s" and "%s"!', $start->format('Y-m-d'), $end->format('Y-m-d')));
|
|
|
|
}
|
|
|
|
}
|
2016-11-20 05:51:33 -06:00
|
|
|
|
2017-07-15 09:41:07 -05:00
|
|
|
if ($moment !== 'all' && $loop > 1) {
|
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
|
|
|
|
2017-03-11 00:41:26 -06:00
|
|
|
return view('transactions.index', compact('subTitle', 'what', 'subTitleIcon', 'journals', 'periods', 'start', 'end', 'moment'));
|
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);
|
2016-04-30 23:59:08 -05:00
|
|
|
$order++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Preferences::mark();
|
|
|
|
|
|
|
|
return Response::json([true]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-25 14:19:06 -06:00
|
|
|
/**
|
2016-10-21 12:06:22 -05:00
|
|
|
* @param TransactionJournal $journal
|
|
|
|
* @param JournalTaskerInterface $tasker
|
2015-02-25 14:19:06 -06:00
|
|
|
*
|
2016-05-15 05:08:41 -05:00
|
|
|
* @return View
|
2015-02-25 14:19:06 -06:00
|
|
|
*/
|
2016-10-21 12:06:22 -05:00
|
|
|
public function show(TransactionJournal $journal, JournalTaskerInterface $tasker)
|
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-06-05 04:12:50 -05:00
|
|
|
$events = $tasker->getPiggyBankEvents($journal);
|
|
|
|
$transactions = $tasker->getTransactionsOverview($journal);
|
|
|
|
$what = strtolower($journal->transaction_type_type ?? $journal->transactionType->type);
|
|
|
|
$subTitle = trans('firefly.' . $what) . ' "' . e($journal->description) . '"';
|
2017-04-14 07:37:04 -05:00
|
|
|
|
2017-06-04 06:39:16 -05:00
|
|
|
return view('transactions.show', compact('journal', 'events', 'subTitle', 'what', 'transactions'));
|
2016-05-13 02:55:06 -05:00
|
|
|
|
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
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
2017-03-18 02:09:14 -05:00
|
|
|
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 = Navigation::startOfPeriod($start, $range);
|
|
|
|
$end = Navigation::endOfX(new Carbon, $range);
|
|
|
|
$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-03-17 10:34:57 -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!');
|
2017-03-11 00:41:26 -06:00
|
|
|
$end = Navigation::startOfPeriod($end, $range);
|
|
|
|
$currentEnd = Navigation::endOfPeriod($end, $range);
|
|
|
|
|
|
|
|
// 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-03-11 00:41:26 -06:00
|
|
|
$set = $collector->getJournals();
|
|
|
|
$sum = $set->sum('transaction_amount');
|
|
|
|
$journals = $set->count();
|
|
|
|
$dateStr = $end->format('Y-m-d');
|
|
|
|
$dateName = Navigation::periodShow($end, $range);
|
|
|
|
$array = [
|
|
|
|
'string' => $dateStr,
|
|
|
|
'name' => $dateName,
|
|
|
|
'count' => $journals,
|
|
|
|
'spent' => 0,
|
|
|
|
'earned' => 0,
|
|
|
|
'transferred' => 0,
|
|
|
|
'date' => clone $end,
|
|
|
|
];
|
2017-03-17 10:34:57 -05:00
|
|
|
Log::debug(sprintf('What is %s', $what));
|
2017-03-11 00:41:26 -06:00
|
|
|
switch ($what) {
|
|
|
|
case 'withdrawal':
|
|
|
|
$array['spent'] = $sum;
|
|
|
|
break;
|
|
|
|
case 'deposit':
|
|
|
|
$array['earned'] = $sum;
|
|
|
|
break;
|
|
|
|
case 'transfers':
|
|
|
|
case 'transfer':
|
|
|
|
$array['transferred'] = Steam::positive($sum);
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
$entries->push($array);
|
|
|
|
$end = Navigation::subtractPeriod($end, $range, 1);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2016-04-30 23:59:08 -05:00
|
|
|
}
|