Files
firefly-iii/app/Http/Controllers/Budget/BudgetLimitController.php

287 lines
11 KiB
PHP
Raw Normal View History

<?php
/**
* BudgetLimitController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Budget;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
2023-10-28 06:58:33 +02:00
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\Http\Controllers\DateCalculation;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Collection;
2023-05-29 13:56:55 +02:00
use Illuminate\View\View;
/**
* Class BudgetLimitController
*/
class BudgetLimitController extends Controller
{
use DateCalculation;
private BudgetLimitRepositoryInterface $blRepository;
2020-10-23 06:15:56 +02:00
private CurrencyRepositoryInterface $currencyRepos;
private OperationsRepositoryInterface $opsRepository;
private BudgetRepositoryInterface $repository;
/**
* AmountController constructor.
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
app('view')->share('title', (string) trans('firefly.budgets'));
app('view')->share('mainTitleIcon', 'fa-pie-chart');
$this->repository = app(BudgetRepositoryInterface::class);
$this->opsRepository = app(OperationsRepositoryInterface::class);
$this->blRepository = app(BudgetLimitRepositoryInterface::class);
$this->currencyRepos = app(CurrencyRepositoryInterface::class);
return $next($request);
}
);
}
/**
* @return Factory|View
*/
public function create(Budget $budget, Carbon $start, Carbon $end)
{
2020-10-31 08:00:44 +01:00
$collection = $this->currencyRepos->get();
$budgetLimits = $this->blRepository->getBudgetLimits($budget, $start, $end);
// remove already budgeted currencies with the same date range
2024-12-14 05:45:54 +01:00
$currencies = $collection->filter(
static function (TransactionCurrency $currency) use ($budgetLimits, $start, $end) {
/** @var BudgetLimit $limit */
foreach ($budgetLimits as $limit) {
if ($limit->transaction_currency_id === $currency->id && $limit->start_date->isSameDay($start) && $limit->end_date->isSameDay($end)
) {
return false;
}
}
return true;
}
);
return view('budgets.budget-limits.create', compact('start', 'end', 'currencies', 'budget'));
}
/**
2024-12-22 08:43:12 +01:00
* @return Redirector|RedirectResponse
*/
2024-12-22 08:43:12 +01:00
public function delete(BudgetLimit $budgetLimit)
{
2024-12-22 08:43:12 +01:00
$this->blRepository->destroyBudgetLimit($budgetLimit);
session()->flash('success', trans('firefly.deleted_bl'));
2024-12-14 05:45:54 +01:00
2024-12-22 08:43:12 +01:00
return redirect(route('budgets.index'));
}
/**
* @return Factory|View
*/
public function edit(BudgetLimit $budgetLimit)
{
$notes = $this->blRepository->getNoteText($budgetLimit);
2024-12-14 05:45:54 +01:00
return view('budgets.budget-limits.edit', compact('budgetLimit', 'notes'));
}
/**
2024-12-22 08:43:12 +01:00
* @return Factory|View
*/
2024-12-22 08:43:12 +01:00
public function show(BudgetLimit $budgetLimit)
{
2024-12-22 08:43:12 +01:00
$notes = $this->blRepository->getNoteText($budgetLimit);
2024-12-22 08:43:12 +01:00
return view('budgets.budget-limits.show', compact('budgetLimit', 'notes'));
}
/**
2022-12-31 13:32:42 +01:00
* TODO why redirect AND json response?
2023-06-21 12:34:58 +02:00
*
2020-10-23 06:15:56 +02:00
* @throws FireflyException
*/
2024-12-14 05:45:54 +01:00
public function store(Request $request): JsonResponse|RedirectResponse
{
2023-10-29 06:33:43 +01:00
app('log')->debug('Going to store new budget-limit.', $request->all());
// first search for existing one and update it if necessary.
$currency = $this->currencyRepos->find((int) $request->get('transaction_currency_id'));
$budget = $this->repository->find((int) $request->get('budget_id'));
if (null === $currency || null === $budget) {
throw new FireflyException('No valid currency or budget.');
}
2024-12-14 05:45:54 +01:00
$start = Carbon::createFromFormat('Y-m-d', $request->get('start'));
$end = Carbon::createFromFormat('Y-m-d', $request->get('end'));
2023-11-26 12:10:42 +01:00
if (null === $start || null === $end) {
2023-11-26 12:10:42 +01:00
return response()->json([]);
}
2024-12-14 05:45:54 +01:00
$amount = (string) $request->get('amount');
$start->startOfDay();
2020-10-01 18:43:41 +02:00
$end->startOfDay();
2021-04-13 06:26:51 +02:00
if ('' === $amount) {
return response()->json([]);
}
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Start: %s, end: %s', $start->format('Y-m-d'), $end->format('Y-m-d')));
2024-12-14 05:45:54 +01:00
$limit = $this->blRepository->find($budget, $currency, $start, $end);
2022-08-23 05:46:43 +02:00
// sanity check on amount:
2022-12-24 05:06:39 +01:00
if (0 === bccomp($amount, '0')) {
2022-10-01 16:35:29 +02:00
if (null !== $limit) {
$this->blRepository->destroyBudgetLimit($limit);
}
2023-12-20 19:35:52 +01:00
2022-10-01 16:35:29 +02:00
// return empty=ish array:
return response()->json([]);
2022-08-23 05:46:43 +02:00
}
if ((int) $amount > 268435456) { // intentional cast to integer
2022-10-08 06:05:59 +02:00
$amount = '268435456';
2022-08-23 05:46:43 +02:00
}
2022-12-24 05:06:39 +01:00
if (-1 === bccomp($amount, '0')) {
2022-10-18 20:37:33 +02:00
$amount = bcmul($amount, '-1');
}
2022-08-23 05:46:43 +02:00
if (null !== $limit) {
2021-04-13 06:26:51 +02:00
$limit->amount = $amount;
$limit->save();
}
if (null === $limit) {
$limit = $this->blRepository->store(
[
2020-10-23 06:15:56 +02:00
'budget_id' => $request->get('budget_id'),
'currency_id' => (int) $request->get('transaction_currency_id'),
2020-10-23 06:15:56 +02:00
'start_date' => $start,
'end_date' => $end,
2021-04-13 06:26:51 +02:00
'amount' => $amount,
]
);
}
// parse notes, if any.
2024-12-14 05:45:54 +01:00
$notes = (string) $request->get('notes');
$this->blRepository->setNoteText($limit, $notes);
if ($request->expectsJson()) {
2024-12-14 05:45:54 +01:00
$array = $limit->toArray();
2022-10-01 16:35:29 +02:00
// add some extra metadata:
2024-12-14 05:45:54 +01:00
$spentArr = $this->opsRepository->sumExpenses($limit->start_date, $limit->end_date, null, new Collection([$budget]), $currency);
$array['spent'] = $spentArr[$currency->id]['sum'] ?? '0';
$array['left_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, bcadd($array['spent'], $array['amount']));
$array['amount_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, $limit['amount']);
$array['days_left'] = (string) $this->activeDaysLeft($start, $end);
// left per day:
2024-12-14 05:45:54 +01:00
$array['left_per_day'] = 0 === bccomp('0', $array['days_left']) ? bcadd($array['spent'], $array['amount']) : bcdiv(bcadd($array['spent'], $array['amount']), $array['days_left']);
// left per day formatted.
$array['left_per_day_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, $array['left_per_day']);
// notes:
2024-12-14 05:45:54 +01:00
$array['notes'] = $this->blRepository->getNoteText($limit);
return response()->json($array);
}
2021-11-25 06:26:38 +01:00
return redirect(route('budgets.index'));
}
public function update(Request $request, BudgetLimit $budgetLimit): JsonResponse|RedirectResponse
{
2024-12-14 05:45:54 +01:00
$amount = (string) $request->get('amount');
2021-04-13 06:26:51 +02:00
if ('' === $amount) {
$amount = '0';
}
if ((int) $amount > 268435456) { // 268 million, intentional integer
2024-01-06 14:23:20 +01:00
$amount = '268435456';
}
2022-08-23 05:46:59 +02:00
// sanity check on amount:
2022-12-24 05:06:39 +01:00
if (0 === bccomp($amount, '0')) {
2022-10-01 16:35:29 +02:00
$budgetId = $budgetLimit->budget_id;
$currency = $budgetLimit->transactionCurrency;
$this->blRepository->destroyBudgetLimit($budgetLimit);
2024-12-14 05:45:54 +01:00
$array = [
2022-10-01 16:35:29 +02:00
'budget_id' => $budgetId,
'left_formatted' => app('amount')->formatAnything($currency, '0'),
'left_per_day_formatted' => app('amount')->formatAnything($currency, '0'),
'transaction_currency_id' => $currency->id,
];
2023-12-20 19:35:52 +01:00
2022-10-01 16:35:29 +02:00
return response()->json($array);
2022-08-23 05:46:59 +02:00
}
2024-01-06 14:23:20 +01:00
2022-12-24 05:06:39 +01:00
if (-1 === bccomp($amount, '0')) {
2022-10-18 20:37:33 +02:00
$amount = bcmul($amount, '-1');
}
2024-12-22 08:43:12 +01:00
$notes = (string) $request->get('notes');
2024-12-14 05:45:54 +01:00
if (strlen($notes) > 32768) {
$notes = substr($notes, 0, 32768);
}
2022-08-23 05:46:59 +02:00
2024-12-14 05:45:54 +01:00
$limit = $this->blRepository->update($budgetLimit, ['amount' => $amount, 'notes' => $notes]);
2023-07-09 18:45:44 +02:00
app('preferences')->mark();
2024-12-14 05:45:54 +01:00
$array = $limit->toArray();
2019-09-01 11:13:03 +02:00
2024-12-14 05:45:54 +01:00
$spentArr = $this->opsRepository->sumExpenses(
$limit->start_date,
$limit->end_date,
null,
new Collection([$budgetLimit->budget]),
$budgetLimit->transactionCurrency
2019-09-01 11:13:03 +02:00
);
2024-12-14 05:45:54 +01:00
$daysLeft = $this->activeDaysLeft($limit->start_date, $limit->end_date);
$array['spent'] = $spentArr[$budgetLimit->transactionCurrency->id]['sum'] ?? '0';
$array['left_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, bcadd($array['spent'], $array['amount']));
$array['amount_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, $limit['amount']);
$array['days_left'] = (string) $daysLeft;
$array['left_per_day'] = 0 === $daysLeft ? bcadd($array['spent'], $array['amount']) : bcdiv(bcadd($array['spent'], $array['amount']), $array['days_left']);
2019-09-01 11:13:03 +02:00
// left per day formatted.
2022-12-24 05:06:39 +01:00
$array['amount'] = app('steam')->bcround($limit['amount'], $limit->transactionCurrency->decimal_places);
2019-09-01 11:13:03 +02:00
$array['left_per_day_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, $array['left_per_day']);
if ('true' === $request->get('redirect')) {
return redirect(route('budgets.index'));
}
2024-12-14 05:45:54 +01:00
2019-09-01 11:13:03 +02:00
return response()->json($array);
}
}