Can now update (and remove) auto budget over api

This commit is contained in:
James Cole
2020-03-14 07:43:49 +01:00
parent d1d11ae717
commit bde0732135
4 changed files with 81 additions and 10 deletions

View File

@@ -78,7 +78,7 @@ class BudgetStoreRequest extends Request
return [ return [
'name' => 'required|between:1,100|uniqueObjectForUser:budgets,name', 'name' => 'required|between:1,100|uniqueObjectForUser:budgets,name',
'active' => [new IsBoolean], 'active' => [new IsBoolean],
'auto_budget_type' => 'in:reset,rollover', 'auto_budget_type' => 'in:reset,rollover,none',
'auto_budget_currency_id' => 'exists:transaction_currencies,id', 'auto_budget_currency_id' => 'exists:transaction_currencies,id',
'auto_budget_currency_code' => 'exists:transaction_currencies,code', 'auto_budget_currency_code' => 'exists:transaction_currencies,code',
'auto_budget_amount' => 'min:0|max:1000000000', 'auto_budget_amount' => 'min:0|max:1000000000',

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests; namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Rules\IsBoolean; use FireflyIII\Rules\IsBoolean;
use Illuminate\Validation\Validator;
/** /**
* Class BudgetUpdateRequest * Class BudgetUpdateRequest
@@ -56,9 +57,14 @@ class BudgetUpdateRequest extends Request
} }
return [ return [
'name' => $this->string('name'), 'name' => $this->string('name'),
'active' => $active, 'active' => $active,
'order' => 0, 'order' => 0,
'auto_budget_type' => $this->string('auto_budget_type'),
'transaction_currency_id' => $this->integer('auto_budget_currency_id'),
'transaction_currency_code' => $this->string('auto_budget_currency_code'),
'auto_budget_amount' => $this->string('auto_budget_amount'),
'auto_budget_period' => $this->string('auto_budget_period'),
]; ];
} }
@@ -72,8 +78,30 @@ class BudgetUpdateRequest extends Request
$budget = $this->route()->parameter('budget'); $budget = $this->route()->parameter('budget');
return [ return [
'name' => sprintf('required|between:1,100|uniqueObjectForUser:budgets,name,%d', $budget->id), 'name' => sprintf('required|between:1,100|uniqueObjectForUser:budgets,name,%d', $budget->id),
'active' => [new IsBoolean], 'active' => [new IsBoolean],
'auto_budget_type' => 'in:reset,rollover,none',
'auto_budget_currency_id' => 'exists:transaction_currencies,id',
'auto_budget_currency_code' => 'exists:transaction_currencies,code',
'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->validateAutoBudgetAmount($validator);
}
);
}
} }

View File

@@ -305,7 +305,6 @@ class BudgetRepository implements BudgetRepositoryInterface
$currencyId = (int)($data['transaction_currency_id'] ?? 0); $currencyId = (int)($data['transaction_currency_id'] ?? 0);
$currencyCode = (string)($data['transaction_currency_code'] ?? ''); $currencyCode = (string)($data['transaction_currency_code'] ?? '');
$currency = $repos->findNull($currencyId); $currency = $repos->findNull($currencyId);
if(null === $currency) { if(null === $currency) {
$currency = $repos->findByCodeNull($currencyCode); $currency = $repos->findByCodeNull($currencyCode);
@@ -339,20 +338,48 @@ class BudgetRepository implements BudgetRepositoryInterface
$budget->save(); $budget->save();
// update or create auto-budget: // update or create auto-budget:
$autoBudgetType = $data['auto_budget_option'] ?? 0; $autoBudgetType = $data['auto_budget_type'] ?? 0;
if ('reset' === $autoBudgetType) {
$autoBudgetType = AutoBudget::AUTO_BUDGET_RESET;
}
if ('rollover' === $autoBudgetType) {
$autoBudgetType = AutoBudget::AUTO_BUDGET_ROLLOVER;
}
if ('none' === $autoBudgetType) {
$autoBudgetType = 0;
}
if (0 !== $autoBudgetType) { if (0 !== $autoBudgetType) {
$autoBudget = $this->getAutoBudget($budget); $autoBudget = $this->getAutoBudget($budget);
if (null === $autoBudget) { if (null === $autoBudget) {
$autoBudget = new AutoBudget; $autoBudget = new AutoBudget;
$autoBudget->budget()->associate($budget); $autoBudget->budget()->associate($budget);
} }
$autoBudget->transaction_currency_id = $data['transaction_currency_id'] ?? 1;
$repos = app(CurrencyRepositoryInterface::class);
$currencyId = (int)($data['transaction_currency_id'] ?? 0);
$currencyCode = (string)($data['transaction_currency_code'] ?? '');
$currency = $repos->findNull($currencyId);
if(null === $currency) {
$currency = $repos->findByCodeNull($currencyCode);
}
if(null === $currency) {
$currency = app('amount')->getDefaultCurrencyByUser($this->user);
}
$autoBudget->transaction_currency_id = $currency->id;
$autoBudget->auto_budget_type = $autoBudgetType; $autoBudget->auto_budget_type = $autoBudgetType;
$autoBudget->amount = $data['auto_budget_amount'] ?? '0'; $autoBudget->amount = $data['auto_budget_amount'] ?? '0';
$autoBudget->period = $data['auto_budget_period'] ?? 'monthly'; $autoBudget->period = $data['auto_budget_period'] ?? 'monthly';
$autoBudget->save(); $autoBudget->save();
} }
if (0 === $autoBudgetType) {
$autoBudget = $this->getAutoBudget($budget);
if (null !== $autoBudget) {
$this->destroyAutoBudget($budget);
}
}
$this->updateRuleTriggers($oldName, $data['name']); $this->updateRuleTriggers($oldName, $data['name']);
$this->updateRuleActions($oldName, $data['name']); $this->updateRuleActions($oldName, $data['name']);
app('preferences')->mark(); app('preferences')->mark();
@@ -425,4 +452,15 @@ class BudgetRepository implements BudgetRepositoryInterface
{ {
return $budget->autoBudgets()->first(); return $budget->autoBudgets()->first();
} }
/**
* @inheritDoc
*/
public function destroyAutoBudget(Budget $budget): void
{
/** @var AutoBudget $autoBudget */
foreach ($budget->autoBudgets()->get() as $autoBudget) {
$autoBudget->delete();
}
}
} }

View File

@@ -46,6 +46,11 @@ interface BudgetRepositoryInterface
*/ */
public function getAutoBudget(Budget $budget): ?AutoBudget; public function getAutoBudget(Budget $budget): ?AutoBudget;
/**
* @param Budget $budget
*/
public function destroyAutoBudget(Budget $budget): void;
/** /**
* @return bool * @return bool