mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-05 12:12:18 +00:00
Can now store and validate auto budget
This commit is contained in:
@@ -26,7 +26,7 @@ namespace FireflyIII\Http\Controllers\Budget;
|
|||||||
|
|
||||||
use FireflyIII\AutoBudget;
|
use FireflyIII\AutoBudget;
|
||||||
use FireflyIII\Http\Controllers\Controller;
|
use FireflyIII\Http\Controllers\Controller;
|
||||||
use FireflyIII\Http\Requests\BudgetFormRequest;
|
use FireflyIII\Http\Requests\BudgetFormStoreRequest;
|
||||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||||
use Illuminate\Http\RedirectResponse;
|
use Illuminate\Http\RedirectResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
@@ -106,13 +106,14 @@ class CreateController extends Controller
|
|||||||
/**
|
/**
|
||||||
* Stores a budget.
|
* Stores a budget.
|
||||||
*
|
*
|
||||||
* @param BudgetFormRequest $request
|
* @param BudgetFormStoreRequest $request
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Http\RedirectResponse
|
* @return \Illuminate\Http\RedirectResponse
|
||||||
*/
|
*/
|
||||||
public function store(BudgetFormRequest $request): RedirectResponse
|
public function store(BudgetFormStoreRequest $request): RedirectResponse
|
||||||
{
|
{
|
||||||
$data = $request->getBudgetData();
|
$data = $request->getBudgetData();
|
||||||
|
|
||||||
$budget = $this->repository->store($data);
|
$budget = $this->repository->store($data);
|
||||||
$this->repository->cleanupBudgets();
|
$this->repository->cleanupBudgets();
|
||||||
$request->session()->flash('success', (string)trans('firefly.stored_new_budget', ['name' => $budget->name]));
|
$request->session()->flash('success', (string)trans('firefly.stored_new_budget', ['name' => $budget->name]));
|
||||||
|
@@ -25,7 +25,7 @@ namespace FireflyIII\Http\Controllers\Budget;
|
|||||||
|
|
||||||
|
|
||||||
use FireflyIII\Http\Controllers\Controller;
|
use FireflyIII\Http\Controllers\Controller;
|
||||||
use FireflyIII\Http\Requests\BudgetFormRequest;
|
use FireflyIII\Http\Requests\BudgetFormUpdateRequest;
|
||||||
use FireflyIII\Models\Budget;
|
use FireflyIII\Models\Budget;
|
||||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||||
use Illuminate\Http\RedirectResponse;
|
use Illuminate\Http\RedirectResponse;
|
||||||
@@ -90,12 +90,12 @@ class EditController extends Controller
|
|||||||
/**
|
/**
|
||||||
* Budget update routine.
|
* Budget update routine.
|
||||||
*
|
*
|
||||||
* @param BudgetFormRequest $request
|
* @param BudgetFormUpdateRequest $request
|
||||||
* @param Budget $budget
|
* @param Budget $budget
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Http\RedirectResponse
|
* @return \Illuminate\Http\RedirectResponse
|
||||||
*/
|
*/
|
||||||
public function update(BudgetFormRequest $request, Budget $budget): RedirectResponse
|
public function update(BudgetFormUpdateRequest $request, Budget $budget): RedirectResponse
|
||||||
{
|
{
|
||||||
$data = $request->getBudgetData();
|
$data = $request->getBudgetData();
|
||||||
$this->repository->update($budget, $data);
|
$this->repository->update($budget, $data);
|
||||||
|
116
app/Http/Requests/BudgetFormStoreRequest.php
Normal file
116
app/Http/Requests/BudgetFormStoreRequest.php
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* BudgetFormStoreRequest.php
|
||||||
|
* 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\Requests;
|
||||||
|
|
||||||
|
use FireflyIII\AutoBudget;
|
||||||
|
use Illuminate\Validation\Validator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @codeCoverageIgnore
|
||||||
|
* Class BudgetFormStoreRequest
|
||||||
|
*/
|
||||||
|
class BudgetFormStoreRequest extends Request
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Verify the request.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return auth()->check();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the data required by the controller.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getBudgetData(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name' => $this->string('name'),
|
||||||
|
'active' => $this->boolean('active'),
|
||||||
|
'auto_budget_option' => $this->integer('auto_budget_option'),
|
||||||
|
'transaction_currency_id' => $this->integer('transaction_currency_id'),
|
||||||
|
'auto_budget_amount' => $this->string('auto_budget_amount'),
|
||||||
|
'auto_budget_period' => $this->string('auto_budget_period'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rules for this request.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name' => 'required|between:1,100|uniqueObjectForUser:budgets,name',
|
||||||
|
'active' => 'numeric|between:0,1',
|
||||||
|
'auto_budget_option' => 'numeric|between:0,2',
|
||||||
|
'transaction_currency_id' => 'required|exists:transaction_currencies,id',
|
||||||
|
'auto_budget_amount' => 'min:0|max:1000000000',
|
||||||
|
'auto_budget_period' => 'in:daily,weekly,monthly,quarterly,half_year,yearly',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configure the validator instance with special rules for after the basic validation rules.
|
||||||
|
*
|
||||||
|
* @param Validator $validator
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function withValidator(Validator $validator): void
|
||||||
|
{
|
||||||
|
$validator->after(
|
||||||
|
function (Validator $validator) {
|
||||||
|
// validate all account info
|
||||||
|
$this->validateAmount($validator);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Validator $validator
|
||||||
|
*/
|
||||||
|
private function validateAmount(Validator $validator): void
|
||||||
|
{
|
||||||
|
$data = $validator->getData();
|
||||||
|
$option = (int)$data['auto_budget_option'];
|
||||||
|
$amount = $data['auto_budget_amount'] ?? '';
|
||||||
|
switch ($option) {
|
||||||
|
case AutoBudget::AUTO_BUDGET_RESET:
|
||||||
|
case AutoBudget::AUTO_BUDGET_ROLLOVER:
|
||||||
|
// basic float check:
|
||||||
|
if ('' === $amount) {
|
||||||
|
$validator->errors()->add('auto_budget_amount', (string)trans('validation.amount_required_for_auto_budget'));
|
||||||
|
}
|
||||||
|
if (1 !== bccomp((string)$amount, '0')) {
|
||||||
|
$validator->errors()->add('auto_budget_amount', (string)trans('validation.auto_budget_amount_positive'));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -25,12 +25,10 @@ namespace FireflyIII\Http\Requests;
|
|||||||
use FireflyIII\Models\Budget;
|
use FireflyIII\Models\Budget;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class BudgetFormRequest.
|
|
||||||
*
|
|
||||||
* @codeCoverageIgnore
|
* @codeCoverageIgnore
|
||||||
* TODO AFTER 4.8,0, split for update/store
|
* Class BudgetFormUpdateRequest
|
||||||
*/
|
*/
|
||||||
class BudgetFormRequest extends Request
|
class BudgetFormUpdateRequest extends Request
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Verify the request.
|
* Verify the request.
|
@@ -21,7 +21,9 @@
|
|||||||
|
|
||||||
namespace FireflyIII;
|
namespace FireflyIII;
|
||||||
|
|
||||||
|
use FireflyIII\Models\Budget;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -35,5 +37,13 @@ class AutoBudget extends Model
|
|||||||
public const AUTO_BUDGET_ROLLOVER = 2;
|
public const AUTO_BUDGET_ROLLOVER = 2;
|
||||||
|
|
||||||
use SoftDeletes;
|
use SoftDeletes;
|
||||||
//
|
|
||||||
|
/**
|
||||||
|
* @codeCoverageIgnore
|
||||||
|
* @return BelongsTo
|
||||||
|
*/
|
||||||
|
public function budget(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Budget::class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -23,6 +23,7 @@ declare(strict_types=1);
|
|||||||
namespace FireflyIII\Models;
|
namespace FireflyIII\Models;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use FireflyIII\AutoBudget;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
@@ -122,6 +123,16 @@ class Budget extends Model
|
|||||||
return $this->hasMany(BudgetLimit::class);
|
return $this->hasMany(BudgetLimit::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @codeCoverageIgnore
|
||||||
|
* @return HasMany
|
||||||
|
*/
|
||||||
|
public function autoBudgets(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(AutoBudget::class);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @codeCoverageIgnore
|
* @codeCoverageIgnore
|
||||||
* @return BelongsToMany
|
* @return BelongsToMany
|
||||||
|
@@ -25,6 +25,7 @@ namespace FireflyIII\Repositories\Budget;
|
|||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use DB;
|
use DB;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use FireflyIII\AutoBudget;
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
use FireflyIII\Models\Budget;
|
use FireflyIII\Models\Budget;
|
||||||
use FireflyIII\Models\BudgetLimit;
|
use FireflyIII\Models\BudgetLimit;
|
||||||
@@ -282,10 +283,24 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
'name' => $data['name'],
|
'name' => $data['name'],
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
} catch(QueryException $e) {
|
} catch (QueryException $e) {
|
||||||
|
Log::error($e->getMessage());
|
||||||
|
Log::error($e->getTraceAsString());
|
||||||
throw new FireflyException('400002: Could not store budget.');
|
throw new FireflyException('400002: Could not store budget.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// try to create associated auto budget:
|
||||||
|
$option = $data['auto_budget_option'] ?? 0;
|
||||||
|
if (0 === $option) {
|
||||||
|
return $newBudget;
|
||||||
|
}
|
||||||
|
$autoBudget = new AutoBudget;
|
||||||
|
$autoBudget->budget()->associate($newBudget);
|
||||||
|
$autoBudget->transaction_currency_id = $data['transaction_currency_id'] ?? 1;
|
||||||
|
$autoBudget->auto_budget_type = $option;
|
||||||
|
$autoBudget->amount = $data['auto_budget_amount'] ?? '1';
|
||||||
|
$autoBudget->period = $data['auto_budget_period'] ?? 'monthly';
|
||||||
|
$autoBudget->save();
|
||||||
return $newBudget;
|
return $newBudget;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -130,6 +130,7 @@ return [
|
|||||||
'amount_zero' => 'The total amount cannot be zero.',
|
'amount_zero' => 'The total amount cannot be zero.',
|
||||||
'current_target_amount' => 'The current amount must be less than the target amount.',
|
'current_target_amount' => 'The current amount must be less than the target amount.',
|
||||||
'unique_piggy_bank_for_user' => 'The name of the piggy bank must be unique.',
|
'unique_piggy_bank_for_user' => 'The name of the piggy bank must be unique.',
|
||||||
|
|
||||||
'secure_password' => 'This is not a secure password. Please try again. For more information, visit https://bit.ly/FF3-password-security',
|
'secure_password' => 'This is not a secure password. Please try again. For more information, visit https://bit.ly/FF3-password-security',
|
||||||
'valid_recurrence_rep_type' => 'Invalid repetition type for recurring transactions.',
|
'valid_recurrence_rep_type' => 'Invalid repetition type for recurring transactions.',
|
||||||
'valid_recurrence_rep_moment' => 'Invalid repetition moment for this type of repetition.',
|
'valid_recurrence_rep_moment' => 'Invalid repetition moment for this type of repetition.',
|
||||||
@@ -200,4 +201,7 @@ return [
|
|||||||
'gte.file' => 'The :attribute must be greater than or equal to :value kilobytes.',
|
'gte.file' => 'The :attribute must be greater than or equal to :value kilobytes.',
|
||||||
'gte.string' => 'The :attribute must be greater than or equal to :value characters.',
|
'gte.string' => 'The :attribute must be greater than or equal to :value characters.',
|
||||||
'gte.array' => 'The :attribute must have :value items or more.',
|
'gte.array' => 'The :attribute must have :value items or more.',
|
||||||
|
|
||||||
|
'amount_required_for_auto_budget' => 'The amount is required.',
|
||||||
|
'auto_budget_amount_positive' => 'The amount must be more than zero.',
|
||||||
];
|
];
|
||||||
|
Reference in New Issue
Block a user