mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-12-25 08:21:08 -06:00
Budget limit now has period.
This commit is contained in:
parent
3dbc74b040
commit
c659d67172
@ -1,92 +0,0 @@
|
||||
<?php
|
||||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
namespace FireflyIII\Api\V1\Controllers\BudgetLimit;
|
||||
|
||||
|
||||
use FireflyIII\Api\V1\Controllers\Controller;
|
||||
use FireflyIII\Api\V1\Requests\DateRequest;
|
||||
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
|
||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||
use FireflyIII\Transformers\BudgetLimitTransformer;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
||||
use League\Fractal\Resource\Collection as FractalCollection;
|
||||
|
||||
/**
|
||||
* Class IndexController
|
||||
*/
|
||||
class IndexController extends Controller
|
||||
{
|
||||
|
||||
private BudgetLimitRepositoryInterface $blRepository;
|
||||
private BudgetRepositoryInterface $repository;
|
||||
|
||||
/**
|
||||
* IndexController constructor.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->middleware(
|
||||
function ($request, $next) {
|
||||
/** @var User $user */
|
||||
$user = auth()->user();
|
||||
$this->repository = app(BudgetRepositoryInterface::class);
|
||||
$this->blRepository = app(BudgetLimitRepositoryInterface::class);
|
||||
$this->repository->setUser($user);
|
||||
$this->blRepository->setUser($user);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all budget limits in a range.
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function index(DateRequest $request): JsonResponse
|
||||
{
|
||||
$dates = $request->getAll();
|
||||
$manager = $this->getManager();
|
||||
$manager->parseIncludes('budget');
|
||||
$budgetLimits = $this->blRepository->getAllBudgetLimits($dates['start'], $dates['end']);
|
||||
$budgetLimits = $budgetLimits->slice(0, 5);
|
||||
/** @var BudgetLimitTransformer $transformer */
|
||||
$transformer = app(BudgetLimitTransformer::class);
|
||||
$transformer->setParameters($this->parameters);
|
||||
|
||||
$paginator = new LengthAwarePaginator($budgetLimits, $budgetLimits->count(), 1000, $this->parameters->get('page'));
|
||||
$paginator->setPath(route('api.v1.budget_limits.index') . $this->buildParams());
|
||||
|
||||
$resource = new FractalCollection($budgetLimits, $transformer, 'budget_limits');
|
||||
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
||||
|
||||
|
||||
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
|
||||
}
|
||||
}
|
@ -102,6 +102,7 @@ class BudgetLimitController extends Controller
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$manager = $this->getManager();
|
||||
$manager->parseIncludes('budget');
|
||||
$budgetId = (int)($request->get('budget_id') ?? 0);
|
||||
$budget = $this->repository->findNull($budgetId);
|
||||
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
|
||||
|
174
app/Console/Commands/Upgrade/AppendBudgetLimitPeriods.php
Normal file
174
app/Console/Commands/Upgrade/AppendBudgetLimitPeriods.php
Normal file
@ -0,0 +1,174 @@
|
||||
<?php
|
||||
/*
|
||||
* AppendBudgetLimitPeriods.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/>.
|
||||
*/
|
||||
|
||||
namespace FireflyIII\Console\Commands\Upgrade;
|
||||
|
||||
use FireflyIII\Models\BudgetLimit;
|
||||
use Illuminate\Console\Command;
|
||||
use Log;
|
||||
|
||||
class AppendBudgetLimitPeriods extends Command
|
||||
{
|
||||
public const CONFIG_NAME = '550_budget_limit_periods';
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Append budget limits with their (estimated) timeframe.';
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'firefly-iii:budget-limit-periods {--F|force : Force the execution of this command.}';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle(): int
|
||||
{
|
||||
$start = microtime(true);
|
||||
if ($this->isExecuted() && true !== $this->option('force')) {
|
||||
$this->warn('This command has already been executed.');
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
$this->theresNoLimit();
|
||||
|
||||
$this->markAsExecuted();
|
||||
|
||||
$end = round(microtime(true) - $start, 2);
|
||||
$this->info(sprintf('Fixed budget limits in %s seconds.', $end));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
private function isExecuted(): bool
|
||||
{
|
||||
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
|
||||
if (null !== $configVar) {
|
||||
return (bool)$configVar->data;
|
||||
}
|
||||
|
||||
return false; // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
/**
|
||||
* @param BudgetLimit $limit
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function getLimitPeriod(BudgetLimit $limit): ?string
|
||||
{
|
||||
// is daily
|
||||
if ($limit->end_date->isSameDay($limit->start_date)) {
|
||||
return 'daily';
|
||||
}
|
||||
// is weekly
|
||||
if ('1' === $limit->start_date->format('N') && '7' === $limit->end_date->format('N') && 6 === $limit->end_date->diffInDays($limit->start_date)) {
|
||||
return 'weekly';
|
||||
}
|
||||
|
||||
// is monthly
|
||||
if (
|
||||
'1' === $limit->start_date->format('j') // first day
|
||||
&& $limit->end_date->format('j') === $limit->end_date->format('t') // last day
|
||||
&& $limit->start_date->isSameMonth($limit->end_date)
|
||||
) {
|
||||
return 'monthly';
|
||||
}
|
||||
|
||||
// is quarter
|
||||
$start = ['1-1', '1-4', '1-7', '1-10'];
|
||||
$end = ['31-3', '30-6', '30-9', '31-12'];
|
||||
if (
|
||||
in_array($limit->start_date->format('j-n'), $start, true) // start of quarter
|
||||
&& in_array($limit->end_date->format('j-n'), $end, true) // end of quarter
|
||||
&& 2 === $limit->start_date->diffInMonths($limit->end_date)
|
||||
) {
|
||||
return 'quarterly';
|
||||
}
|
||||
// is half year
|
||||
$start = ['1-1', '1-7'];
|
||||
$end = ['30-6', '31-12'];
|
||||
if (
|
||||
in_array($limit->start_date->format('j-n'), $start) // start of quarter
|
||||
&& in_array($limit->end_date->format('j-n'), $end) // end of quarter
|
||||
&& 5 === $limit->start_date->diffInMonths($limit->end_date)
|
||||
) {
|
||||
return 'half_year';
|
||||
}
|
||||
// is yearly
|
||||
if ('1-1' === $limit->start_date->format('j-n') && '31-12' === $limit->end_date->format('j-n')) {
|
||||
return 'yearly';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private function markAsExecuted(): void
|
||||
{
|
||||
app('fireflyconfig')->set(self::CONFIG_NAME, true);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private function theresNoLimit(): void
|
||||
{
|
||||
$limits = BudgetLimit::whereNull('period')->get();
|
||||
/** @var BudgetLimit $limit */
|
||||
foreach ($limits as $limit) {
|
||||
$this->fixLimit($limit);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param BudgetLimit $limit
|
||||
*/
|
||||
private function fixLimit(BudgetLimit $limit)
|
||||
{
|
||||
$period = $this->getLimitPeriod($limit);
|
||||
|
||||
if (null === $period) {
|
||||
$message = sprintf('Could not guesstimate budget limit #%d (%s - %s) period.', $limit->id, $limit->start_date->format('Y-m-d'), $limit->end_date->format('Y-m-d'));
|
||||
$this->warn($message);
|
||||
Log::warning($message);
|
||||
return;
|
||||
}
|
||||
$limit->period = $period;
|
||||
$limit->save();
|
||||
|
||||
$msg = sprintf('Budget limit #%d (%s - %s) period is "%s".', $limit->id, $limit->start_date->format('Y-m-d'), $limit->end_date->format('Y-m-d'), $period);
|
||||
Log::debug($msg);
|
||||
|
||||
}
|
||||
}
|
@ -45,8 +45,7 @@ class CreateAutoBudgetLimits implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
/** @var Carbon The current date */
|
||||
private $date;
|
||||
private Carbon $date;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
@ -60,9 +59,9 @@ class CreateAutoBudgetLimits implements ShouldQueue
|
||||
if (null !== $date) {
|
||||
$date->startOfDay();
|
||||
$this->date = $date;
|
||||
}
|
||||
Log::debug(sprintf('Created new CreateAutoBudgetLimits("%s")', $this->date->format('Y-m-d')));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
@ -106,6 +105,8 @@ class CreateAutoBudgetLimits implements ShouldQueue
|
||||
$budgetLimit->start_date = $start;
|
||||
$budgetLimit->end_date = $end;
|
||||
$budgetLimit->amount = $amount ?? $autoBudget->amount;
|
||||
$budgetLimit->period = $autoBudget->period;
|
||||
$budgetLimit->generated = true;
|
||||
$budgetLimit->save();
|
||||
|
||||
Log::debug(sprintf('Created budget limit #%d.', $budgetLimit->id));
|
||||
@ -203,6 +204,7 @@ class CreateAutoBudgetLimits implements ShouldQueue
|
||||
if (null === $autoBudget->budget) {
|
||||
Log::info(sprintf('Auto budget #%d is associated with a deleted budget.', $autoBudget->id));
|
||||
$autoBudget->delete();
|
||||
|
||||
return;
|
||||
}
|
||||
if (!$this->isMagicDay($autoBudget)) {
|
||||
|
@ -24,6 +24,9 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Transformers;
|
||||
|
||||
use FireflyIII\Models\BudgetLimit;
|
||||
use FireflyIII\Repositories\Budget\BudgetLimitRepository;
|
||||
use FireflyIII\Repositories\Budget\OperationsRepository;
|
||||
use Illuminate\Support\Collection;
|
||||
use League\Fractal\Resource\Item;
|
||||
|
||||
/**
|
||||
@ -46,7 +49,7 @@ class BudgetLimitTransformer extends AbstractTransformer
|
||||
*/
|
||||
public function includeBudget(BudgetLimit $limit)
|
||||
{
|
||||
return $this->item($limit->budget, new BudgetTransformer,'budgets ');
|
||||
return $this->item($limit->budget, new BudgetTransformer, 'budgets');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -58,6 +61,11 @@ class BudgetLimitTransformer extends AbstractTransformer
|
||||
*/
|
||||
public function transform(BudgetLimit $budgetLimit): array
|
||||
{
|
||||
$repository = app(OperationsRepository::class);
|
||||
$repository->setUser($budgetLimit->budget->user);
|
||||
$expenses = $repository->sumExpenses($budgetLimit->start_date, $budgetLimit->end_date, null, new Collection([$budgetLimit->budget]), $budgetLimit->transactionCurrency);
|
||||
|
||||
|
||||
$currency = $budgetLimit->transactionCurrency;
|
||||
$amount = $budgetLimit->amount;
|
||||
$currencyDecimalPlaces = 2;
|
||||
@ -88,8 +96,9 @@ class BudgetLimitTransformer extends AbstractTransformer
|
||||
'currency_decimal_places' => $currencyName,
|
||||
'currency_symbol' => $currencySymbol,
|
||||
'amount' => $amount,
|
||||
'repeat_freq' => $budgetLimit->repeat_freq,
|
||||
'period' => $budgetLimit->period,
|
||||
'auto_budget' => $budgetLimit->auto_budget,
|
||||
'spent' => $expenses[$currencyId]['sum'] ?? '0',
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
|
@ -49,7 +49,7 @@ class ChangesForV550 extends Migration
|
||||
Schema::table(
|
||||
'budget_limits',
|
||||
static function (Blueprint $table) {
|
||||
$table->dropColumn('repeat_freq');
|
||||
$table->dropColumn('period');
|
||||
$table->dropColumn('generated');
|
||||
}
|
||||
);
|
||||
@ -105,7 +105,7 @@ class ChangesForV550 extends Migration
|
||||
Schema::table(
|
||||
'budget_limits',
|
||||
static function (Blueprint $table) {
|
||||
$table->string('repeat_freq', 12)->nullable();
|
||||
$table->string('period', 12)->nullable();
|
||||
$table->boolean('generated')->default(false);
|
||||
}
|
||||
);
|
||||
|
@ -175,17 +175,6 @@ Route::group(
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
Route::group(
|
||||
['namespace' => 'FireflyIII\Api\V1\Controllers\BudgetLimit', 'prefix' => 'limits',
|
||||
'as' => 'api.v1.limits.',],
|
||||
static function () {
|
||||
|
||||
// Budget API routes:
|
||||
Route::get('', ['uses' => 'IndexController@index', 'as' => 'index']);
|
||||
}
|
||||
);
|
||||
|
||||
Route::group(
|
||||
['namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'categories',
|
||||
'as' => 'api.v1.categories.',],
|
||||
|
Loading…
Reference in New Issue
Block a user