mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-03 03:21:39 +00:00
Finish budget tests.
This commit is contained in:
@@ -68,6 +68,7 @@ class StoreController extends Controller
|
||||
public function store(StoreRequest $request): JsonResponse
|
||||
{
|
||||
$budget = $this->repository->store($request->getAll());
|
||||
$budget->refresh();
|
||||
$manager = $this->getManager();
|
||||
|
||||
/** @var BudgetTransformer $transformer */
|
||||
|
@@ -46,23 +46,20 @@ class StoreRequest extends FormRequest
|
||||
*/
|
||||
public function getAll(): array
|
||||
{
|
||||
$active = true;
|
||||
if (null !== $this->get('active')) {
|
||||
$active = $this->boolean('active');
|
||||
}
|
||||
$fields = [
|
||||
'name' => ['name', 'string'],
|
||||
'active' => ['active', 'boolean'],
|
||||
'order' => ['active', 'integer'],
|
||||
|
||||
return [
|
||||
'name' => $this->string('name'),
|
||||
'active' => $active,
|
||||
'order' => 0,
|
||||
'transaction_currency_id' => $this->integer('auto_budget_currency_id'),
|
||||
'transaction_currency_code' => $this->string('auto_budget_currency_code'),
|
||||
|
||||
// auto budget info
|
||||
'auto_budget_type' => $this->string('auto_budget_type'),
|
||||
'auto_budget_amount' => $this->string('auto_budget_amount'),
|
||||
'auto_budget_period' => $this->string('auto_budget_period'),
|
||||
// auto budget currency:
|
||||
'currency_id' => ['auto_budget_currency_id', 'integer'],
|
||||
'currency_code' => ['auto_budget_currency_code', 'string'],
|
||||
'auto_budget_type' => ['auto_budget_type', 'string'],
|
||||
'auto_budget_amount' => ['auto_budget_amount', 'string'],
|
||||
'auto_budget_period' => ['auto_budget_period', 'string'],
|
||||
];
|
||||
|
||||
return $this->getAllData($fields);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,14 +70,14 @@ class StoreRequest extends FormRequest
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'required|between:1,100|uniqueObjectForUser:budgets,name',
|
||||
'active' => [new IsBoolean],
|
||||
'auto_budget_currency_id' => 'exists:transaction_currencies,id',
|
||||
'auto_budget_currency_code' => 'exists:transaction_currencies,code',
|
||||
'name' => 'required|between:1,100|uniqueObjectForUser:budgets,name',
|
||||
'active' => [new IsBoolean],
|
||||
'currency_id' => 'exists:transaction_currencies,id',
|
||||
'currency_code' => 'exists:transaction_currencies,code',
|
||||
// auto budget info
|
||||
'auto_budget_type' => 'in:reset,rollover,none',
|
||||
'auto_budget_amount' => 'min:0|max:1000000000',
|
||||
'auto_budget_period' => 'in:daily,weekly,monthly,quarterly,half_year,yearly',
|
||||
'auto_budget_type' => 'in:reset,rollover,none',
|
||||
'auto_budget_amount' => 'min:0|max:1000000000',
|
||||
'auto_budget_period' => 'in:daily,weekly,monthly,quarterly,half_year,yearly',
|
||||
];
|
||||
}
|
||||
|
||||
|
@@ -46,21 +46,19 @@ class UpdateRequest extends FormRequest
|
||||
*/
|
||||
public function getAll(): array
|
||||
{
|
||||
$active = true;
|
||||
if (null !== $this->get('active')) {
|
||||
$active = $this->boolean('active');
|
||||
}
|
||||
|
||||
return [
|
||||
'name' => $this->string('name'),
|
||||
'active' => $active,
|
||||
'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'),
|
||||
// this is the way:
|
||||
$fields = [
|
||||
'name' => ['name', 'string'],
|
||||
'active' => ['active', 'boolean'],
|
||||
'order' => ['order', 'integer'],
|
||||
'currency_id' => ['auto_budget_currency_id', 'integer'],
|
||||
'currency_code' => ['auto_budget_currency_code', 'string'],
|
||||
'auto_budget_type' => ['auto_budget_type', 'string'],
|
||||
'auto_budget_amount' => ['auto_budget_amount', 'string'],
|
||||
'auto_budget_period' => ['auto_budget_period', 'string'],
|
||||
];
|
||||
|
||||
return $this->getAllData($fields);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,7 +71,7 @@ class UpdateRequest extends FormRequest
|
||||
$budget = $this->route()->parameter('budget');
|
||||
|
||||
return [
|
||||
'name' => sprintf('required|between:1,100|uniqueObjectForUser:budgets,name,%d', $budget->id),
|
||||
'name' => sprintf('between:1,100|uniqueObjectForUser:budgets,name,%d', $budget->id),
|
||||
'active' => [new IsBoolean],
|
||||
'auto_budget_type' => 'in:reset,rollover,none',
|
||||
'auto_budget_currency_id' => 'exists:transaction_currencies,id',
|
||||
|
@@ -320,6 +320,7 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
'user_id' => $this->user->id,
|
||||
'name' => $data['name'],
|
||||
'order' => $order + 1,
|
||||
'active' => array_key_exists('active', $data) ? $data['active'] : true,
|
||||
]
|
||||
);
|
||||
} catch (QueryException $e) {
|
||||
@@ -327,25 +328,27 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
Log::error($e->getTraceAsString());
|
||||
throw new FireflyException('400002: Could not store budget.');
|
||||
}
|
||||
|
||||
// try to create associated auto budget:
|
||||
$type = $data['auto_budget_type'] ?? 0;
|
||||
if (0 === $type || '' === $type || 'none' === $type) {
|
||||
if (!array_key_exists('auto_budget_type', $data)) {
|
||||
return $newBudget;
|
||||
}
|
||||
$type = $data['auto_budget_type'];
|
||||
if ('none' === $type) {
|
||||
return $newBudget;
|
||||
}
|
||||
|
||||
if ('reset' === $type) {
|
||||
$type = AutoBudget::AUTO_BUDGET_RESET;
|
||||
}
|
||||
if ('rollover' === $type) {
|
||||
$type = AutoBudget::AUTO_BUDGET_ROLLOVER;
|
||||
}
|
||||
$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);
|
||||
$repos = app(CurrencyRepositoryInterface::class);
|
||||
if (array_key_exists('currency_id', $data)) {
|
||||
$currency = $repos->findNull((int)$data['currency_id']);
|
||||
}
|
||||
if (array_key_exists('currency_code', $data)) {
|
||||
$currency = $repos->findByCode((string)$data['currency_code']);
|
||||
}
|
||||
if (null === $currency) {
|
||||
$currency = app('amount')->getDefaultCurrencyByUser($this->user);
|
||||
@@ -387,56 +390,68 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
*/
|
||||
public function update(Budget $budget, array $data): Budget
|
||||
{
|
||||
$oldName = $budget->name;
|
||||
$budget->name = $data['name'];
|
||||
$budget->active = $data['active'];
|
||||
$oldName = $budget->name;
|
||||
if (array_key_exists('name', $data)) {
|
||||
$budget->name = $data['name'];
|
||||
}
|
||||
if (array_key_exists('active', $data)) {
|
||||
$budget->active = $data['active'];
|
||||
}
|
||||
$budget->save();
|
||||
|
||||
// update or create auto-budget:
|
||||
$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) {
|
||||
$autoBudget = $this->getAutoBudget($budget);
|
||||
if (null === $autoBudget) {
|
||||
$autoBudget = new AutoBudget;
|
||||
$autoBudget->budget()->associate($budget);
|
||||
}
|
||||
$autoBudget = $this->getAutoBudget($budget);
|
||||
|
||||
// get currency:
|
||||
$currency = null;
|
||||
if (array_key_exists('currency_id', $data) || array_key_exists('currency_code', $data)) {
|
||||
$repos = app(CurrencyRepositoryInterface::class);
|
||||
$currencyId = (int)($data['transaction_currency_id'] ?? 0);
|
||||
$currencyCode = (string)($data['transaction_currency_code'] ?? '');
|
||||
|
||||
$currency = $repos->findNull($currencyId);
|
||||
$currencyId = (int)($data['currency_id'] ?? 0);
|
||||
$currencyCode = (string)($data['currency_code'] ?? '');
|
||||
$currency = $repos->findNull($currencyId);
|
||||
if (null === $currency) {
|
||||
$currency = $repos->findByCodeNull($currencyCode);
|
||||
}
|
||||
if (null === $currency) {
|
||||
$currency = app('amount')->getDefaultCurrencyByUser($this->user);
|
||||
}
|
||||
}
|
||||
if (null === $currency) {
|
||||
$currency = app('amount')->getDefaultCurrencyByUser($this->user);
|
||||
}
|
||||
|
||||
if (null === $autoBudget
|
||||
&& array_key_exists('auto_budget_type', $data)
|
||||
&& array_key_exists('auto_budget_amount', $data)
|
||||
) {
|
||||
// only create if all are here:
|
||||
$autoBudget = new AutoBudget;
|
||||
$autoBudget->budget_id = $budget->id;
|
||||
$autoBudget->transaction_currency_id = $currency->id;
|
||||
$autoBudget->auto_budget_type = $autoBudgetType;
|
||||
$autoBudget->amount = $data['auto_budget_amount'] ?? '0';
|
||||
$autoBudget->period = $data['auto_budget_period'] ?? 'monthly';
|
||||
}
|
||||
|
||||
// update existing type
|
||||
if (array_key_exists('auto_budget_type', $data)) {
|
||||
$autoBudgetType = $data['auto_budget_type'];
|
||||
if ('reset' === $autoBudgetType) {
|
||||
$autoBudget->auto_budget_type = AutoBudget::AUTO_BUDGET_RESET;
|
||||
}
|
||||
if ('rollover' === $autoBudgetType) {
|
||||
$autoBudget->auto_budget_type = AutoBudget::AUTO_BUDGET_ROLLOVER;
|
||||
}
|
||||
if ('none' === $autoBudgetType && null !== $autoBudget->id) {
|
||||
$autoBudget->delete();
|
||||
|
||||
return $budget;
|
||||
}
|
||||
}
|
||||
if (array_key_exists('auto_budget_amount', $data)) {
|
||||
$autoBudget->amount = $data['auto_budget_amount'];
|
||||
}
|
||||
if (array_key_exists('auto_budget_period', $data)) {
|
||||
$autoBudget->period = $data['auto_budget_period'];
|
||||
}
|
||||
if (null !== $autoBudget) {
|
||||
$autoBudget->save();
|
||||
}
|
||||
if (0 === $autoBudgetType) {
|
||||
$autoBudget = $this->getAutoBudget($budget);
|
||||
if (null !== $autoBudget) {
|
||||
$this->destroyAutoBudget($budget);
|
||||
}
|
||||
}
|
||||
$this->updateRuleTriggers($oldName, $data['name']);
|
||||
$this->updateRuleActions($oldName, $data['name']);
|
||||
app('preferences')->mark();
|
||||
|
||||
|
||||
return $budget;
|
||||
}
|
||||
|
@@ -81,13 +81,12 @@ class BudgetTransformer extends AbstractTransformer
|
||||
];
|
||||
|
||||
if (null !== $autoBudget) {
|
||||
$abCurrencyId = (int)$autoBudget->transactionCurrency->id;
|
||||
$abCurrencyId = (string)$autoBudget->transactionCurrency->id;
|
||||
$abCurrencyCode = $autoBudget->transactionCurrency->code;
|
||||
$abType = $types[$autoBudget->auto_budget_type];
|
||||
$abAmount = number_format((float)$autoBudget->amount, $autoBudget->transactionCurrency->decimal_places, '.', '');
|
||||
$abPeriod = $autoBudget->period;
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => (string)$budget->id,
|
||||
'created_at' => $budget->created_at->toAtomString(),
|
||||
@@ -96,7 +95,7 @@ class BudgetTransformer extends AbstractTransformer
|
||||
'name' => $budget->name,
|
||||
'auto_budget_type' => $abType,
|
||||
'auto_budget_period' => $abPeriod,
|
||||
'auto_budget_currency_id' => (string)$abCurrencyId,
|
||||
'auto_budget_currency_id' => $abCurrencyId,
|
||||
'auto_budget_currency_code' => $abCurrencyCode,
|
||||
'auto_budget_amount' => $abAmount,
|
||||
'spent' => $spent,
|
||||
|
Reference in New Issue
Block a user