firefly-iii/app/Http/Controllers/PiggyBank/IndexController.php

211 lines
7.9 KiB
PHP
Raw Normal View History

2020-06-07 04:30:02 -05:00
<?php
2020-06-30 12:05:35 -05:00
/**
* IndexController.php
* Copyright (c) 2020 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-06-07 04:30:02 -05:00
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\PiggyBank;
use Carbon\Carbon;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\PiggyBank;
2020-06-07 09:38:15 -05:00
use FireflyIII\Repositories\ObjectGroup\OrganisesObjectGroups;
2020-06-07 04:30:02 -05:00
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use FireflyIII\Transformers\AccountTransformer;
use FireflyIII\Transformers\PiggyBankTransformer;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Symfony\Component\HttpFoundation\ParameterBag;
/**
* Class IndexController
*/
class IndexController extends Controller
{
2020-06-07 09:38:15 -05:00
use OrganisesObjectGroups;
2020-06-07 04:30:02 -05:00
private PiggyBankRepositoryInterface $piggyRepos;
/**
* PiggyBankController constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
app('view')->share('title', (string) trans('firefly.piggyBanks'));
app('view')->share('mainTitleIcon', 'fa-bullseye');
$this->piggyRepos = app(PiggyBankRepositoryInterface::class);
return $next($request);
}
);
}
/**
* Show overview of all piggy banks.
2020-06-07 09:38:15 -05:00
* TODO complicated
2020-06-07 04:30:02 -05:00
*
* @param Request $request
*
* @return Factory|View
*/
public function index(Request $request)
{
2020-06-07 09:38:15 -05:00
$this->cleanupObjectGroups();
2020-06-07 04:30:02 -05:00
$this->piggyRepos->correctOrder();
$collection = $this->piggyRepos->getPiggyBanks();
$accounts = [];
/** @var Carbon $end */
$end = session('end', Carbon::now()->endOfMonth());
// transform piggies using the transformer:
$parameters = new ParameterBag;
$parameters->set('end', $end);
// make piggy bank groups:
$piggyBanks = [
0 => [ // the index is the order, not the ID.
'object_group_id' => 0,
'object_group_title' => (string) trans('firefly.default_group_title_name'),
'piggy_banks' => [],
],
];
/** @var PiggyBankTransformer $transformer */
$transformer = app(PiggyBankTransformer::class);
$transformer->setParameters(new ParameterBag);
/** @var AccountTransformer $accountTransformer */
$accountTransformer = app(AccountTransformer::class);
$accountTransformer->setParameters($parameters);
/** @var PiggyBank $piggy */
foreach ($collection as $piggy) {
$array = $transformer->transform($piggy);
2020-06-20 11:57:20 -05:00
$groupOrder = (int) $array['object_group_order'];
2020-06-07 04:30:02 -05:00
// make group array if necessary:
$piggyBanks[$groupOrder] = $piggyBanks[$groupOrder] ?? [
'object_group_id' => $array['object_group_id'],
'object_group_title' => $array['object_group_title'],
'piggy_banks' => [],
];
$account = $accountTransformer->transform($piggy->account);
$accountId = (int) $account['id'];
$array['attachments'] = $this->piggyRepos->getAttachments($piggy);
if (!isset($accounts[$accountId])) {
// create new:
$accounts[$accountId] = $account;
// add some interesting details:
$accounts[$accountId]['left'] = $accounts[$accountId]['current_balance'];
$accounts[$accountId]['saved'] = 0;
$accounts[$accountId]['target'] = 0;
$accounts[$accountId]['to_save'] = 0;
}
// calculate new interesting fields:
2020-06-20 11:57:20 -05:00
$accounts[$accountId]['left'] -= $array['current_amount'];
$accounts[$accountId]['saved'] += $array['current_amount'];
$accounts[$accountId]['target'] += $array['target_amount'];
$accounts[$accountId]['to_save'] += ($array['target_amount'] - $array['current_amount']);
$array['account_name'] = $account['name'];
2020-06-07 04:30:02 -05:00
$piggyBanks[$groupOrder]['piggy_banks'][] = $array;
}
2020-06-20 11:57:20 -05:00
// do a bunch of summaries.
$piggyBanks = $this->makeSums($piggyBanks);
2020-06-07 04:30:02 -05:00
2020-06-09 03:33:55 -05:00
ksort($piggyBanks);
2020-06-07 04:30:02 -05:00
return view('piggy-banks.index', compact('piggyBanks', 'accounts'));
}
2020-06-20 11:57:20 -05:00
/**
* @param array $piggyBanks
*
* @return array
*/
private function makeSums(array $piggyBanks): array
{
$sums = [];
foreach ($piggyBanks as $groupOrder => $group) {
$groupId = $group['object_group_id'];
foreach ($group['piggy_banks'] as $piggy) {
$currencyId = $piggy['currency_id'];
$sums[$groupId][$currencyId] = $sums[$groupId][$currencyId] ?? [
'target' => '0',
'saved' => '0',
'left_to_save' => '0',
'save_per_month' => '0',
'currency_id' => $currencyId,
'currency_code' => $piggy['currency_code'],
'currency_symbol' => $piggy['currency_symbol'],
'currency_decimal_places' => $piggy['currency_decimal_places'],
];
// target_amount
// current_amount
// left_to_save
// save_per_month
$sums[$groupId][$currencyId]['target'] = bcadd($sums[$groupId][$currencyId]['target'], (string) $piggy['target_amount']);
$sums[$groupId][$currencyId]['saved'] = bcadd($sums[$groupId][$currencyId]['saved'], (string) $piggy['current_amount']);
$sums[$groupId][$currencyId]['left_to_save'] = bcadd($sums[$groupId][$currencyId]['left_to_save'], (string) $piggy['left_to_save']);
$sums[$groupId][$currencyId]['save_per_month'] = bcadd($sums[$groupId][$currencyId]['save_per_month'], (string) $piggy['save_per_month']);
}
}
foreach ($piggyBanks as $groupOrder => $group) {
$groupId = $group['object_group_id'];
2020-06-23 08:30:02 -05:00
$piggyBanks[$groupOrder]['sums'] = $sums[$groupId] ?? [];
2020-06-20 11:57:20 -05:00
}
return $piggyBanks;
}
2020-06-07 04:30:02 -05:00
/**
* Set the order of a piggy bank.
*
* @param Request $request
* @param PiggyBank $piggyBank
*
* @return JsonResponse
*/
public function setOrder(Request $request, PiggyBank $piggyBank): JsonResponse
{
2020-06-09 10:16:21 -05:00
$objectGroupTitle = $request->get('objectGroupTitle');
$newOrder = (int) $request->get('order');
2020-06-07 04:30:02 -05:00
$this->piggyRepos->setOrder($piggyBank, $newOrder);
2020-06-09 10:16:21 -05:00
if ('' !== $objectGroupTitle) {
$this->piggyRepos->setObjectGroup($piggyBank, $objectGroupTitle);
}
if ('' === $objectGroupTitle) {
$this->piggyRepos->removeObjectGroup($piggyBank);
}
2020-06-07 04:30:02 -05:00
return response()->json(['data' => 'OK']);
}
}