This commit is contained in:
James Cole
2022-03-19 11:38:02 +01:00
parent 52a5995bd1
commit 1e1aa28ab2
8 changed files with 79 additions and 7 deletions

View File

@@ -50,6 +50,7 @@ class StoreRequest extends FormRequest
'name' => ['name', 'string'], 'name' => ['name', 'string'],
'active' => ['active', 'boolean'], 'active' => ['active', 'boolean'],
'order' => ['active', 'integer'], 'order' => ['active', 'integer'],
'notes' => ['notes', 'string'],
// auto budget currency: // auto budget currency:
'currency_id' => ['auto_budget_currency_id', 'integer'], 'currency_id' => ['auto_budget_currency_id', 'integer'],
@@ -74,6 +75,7 @@ class StoreRequest extends FormRequest
'active' => [new IsBoolean], 'active' => [new IsBoolean],
'currency_id' => 'exists:transaction_currencies,id', 'currency_id' => 'exists:transaction_currencies,id',
'currency_code' => 'exists:transaction_currencies,code', 'currency_code' => 'exists:transaction_currencies,code',
'notes' => 'nullable|between:1,65536',
// auto budget info // auto budget info
'auto_budget_type' => 'in:reset,rollover,none', 'auto_budget_type' => 'in:reset,rollover,none',
'auto_budget_amount' => 'numeric|min:0|max:1000000000|required_if:auto_budget_type,reset|required_if:auto_budget_type,rollover', 'auto_budget_amount' => 'numeric|min:0|max:1000000000|required_if:auto_budget_type,reset|required_if:auto_budget_type,rollover',

View File

@@ -51,6 +51,7 @@ class UpdateRequest extends FormRequest
'name' => ['name', 'string'], 'name' => ['name', 'string'],
'active' => ['active', 'boolean'], 'active' => ['active', 'boolean'],
'order' => ['order', 'integer'], 'order' => ['order', 'integer'],
'notes' => ['notes', 'string'],
'currency_id' => ['auto_budget_currency_id', 'integer'], 'currency_id' => ['auto_budget_currency_id', 'integer'],
'currency_code' => ['auto_budget_currency_code', 'string'], 'currency_code' => ['auto_budget_currency_code', 'string'],
'auto_budget_type' => ['auto_budget_type', 'string'], 'auto_budget_type' => ['auto_budget_type', 'string'],
@@ -82,6 +83,7 @@ class UpdateRequest extends FormRequest
return [ return [
'name' => sprintf('between:1,100|uniqueObjectForUser:budgets,name,%d', $budget->id), 'name' => sprintf('between:1,100|uniqueObjectForUser:budgets,name,%d', $budget->id),
'active' => [new IsBoolean], 'active' => [new IsBoolean],
'notes' => 'nullable|between:1,65536',
'auto_budget_type' => 'in:reset,rollover,none', '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',

View File

@@ -150,6 +150,15 @@ class Budget extends Model
return $this->hasMany(BudgetLimit::class); return $this->hasMany(BudgetLimit::class);
} }
/**
* @codeCoverageIgnore
* Get all of the notes.
*/
public function notes(): MorphMany
{
return $this->morphMany(Note::class, 'noteable');
}
/** /**
* @codeCoverageIgnore * @codeCoverageIgnore
* @return BelongsToMany * @return BelongsToMany

View File

@@ -30,6 +30,7 @@ use FireflyIII\Models\Attachment;
use FireflyIII\Models\AutoBudget; use FireflyIII\Models\AutoBudget;
use FireflyIII\Models\Budget; use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\Note;
use FireflyIII\Models\RecurrenceTransactionMeta; use FireflyIII\Models\RecurrenceTransactionMeta;
use FireflyIII\Models\RuleAction; use FireflyIII\Models\RuleAction;
use FireflyIII\Models\RuleTrigger; use FireflyIII\Models\RuleTrigger;
@@ -47,8 +48,7 @@ use Storage;
*/ */
class BudgetRepository implements BudgetRepositoryInterface class BudgetRepository implements BudgetRepositoryInterface
{ {
/** @var User */ private User $user;
private $user;
/** /**
* @return bool * @return bool
@@ -323,6 +323,12 @@ class BudgetRepository implements BudgetRepositoryInterface
Log::error($e->getTraceAsString()); Log::error($e->getTraceAsString());
throw new FireflyException('400002: Could not store budget.', 0, $e); throw new FireflyException('400002: Could not store budget.', 0, $e);
} }
// set notes
if(array_key_exists('notes', $data)) {
$this->setNoteText($newBudget, (string)$data['notes']);
}
if (!array_key_exists('auto_budget_type', $data) || !array_key_exists('auto_budget_amount', $data) || !array_key_exists('auto_budget_period', $data)) { if (!array_key_exists('auto_budget_type', $data) || !array_key_exists('auto_budget_amount', $data) || !array_key_exists('auto_budget_period', $data)) {
return $newBudget; return $newBudget;
} }
@@ -400,6 +406,9 @@ class BudgetRepository implements BudgetRepositoryInterface
if (array_key_exists('active', $data)) { if (array_key_exists('active', $data)) {
$budget->active = $data['active']; $budget->active = $data['active'];
} }
if(array_key_exists('notes', $data)) {
$this->setNoteText($budget, (string)$data['notes']);
}
$budget->save(); $budget->save();
// update or create auto-budget: // update or create auto-budget:
@@ -515,4 +524,44 @@ class BudgetRepository implements BudgetRepositoryInterface
$autoBudget->save(); $autoBudget->save();
} }
/**
* @inheritDoc
*/
public function getNoteText(Budget $budget): ?string
{
$note = $budget->notes()->first();
if (null === $note) {
return null;
}
return $note->text;
}
/**
* @param Budget $budget
* @param string $text
* @return void
*/
private function setNoteText(Budget $budget, string $text): void
{
$dbNote = $budget->notes()->first();
if ('' !== $text) {
if (null === $dbNote) {
$dbNote = new Note;
$dbNote->noteable()->associate($budget);
}
$dbNote->text = trim($text);
$dbNote->save();
return;
}
if (null !== $dbNote) {
try {
$dbNote->delete();
} catch (Exception $e) { // @phpstan-ignore-line
// @ignoreException
}
}
}
} }

View File

@@ -39,6 +39,12 @@ interface BudgetRepositoryInterface
*/ */
public function cleanupBudgets(): bool; public function cleanupBudgets(): bool;
/**
* @param Budget $budget
* @return string|null
*/
public function getNoteText(Budget $budget): ?string;
/** /**
* @param Budget $budget * @param Budget $budget
* *

View File

@@ -73,6 +73,7 @@ class BudgetTransformer extends AbstractTransformer
$abType = null; $abType = null;
$abAmount = null; $abAmount = null;
$abPeriod = null; $abPeriod = null;
$notes = $this->repository->getNoteText($budget);
$types = [ $types = [
AutoBudget::AUTO_BUDGET_RESET => 'reset', AutoBudget::AUTO_BUDGET_RESET => 'reset',
@@ -80,20 +81,21 @@ class BudgetTransformer extends AbstractTransformer
]; ];
if (null !== $autoBudget) { if (null !== $autoBudget) {
$abCurrencyId = (string)$autoBudget->transactionCurrency->id; $abCurrencyId = (string) $autoBudget->transactionCurrency->id;
$abCurrencyCode = $autoBudget->transactionCurrency->code; $abCurrencyCode = $autoBudget->transactionCurrency->code;
$abType = $types[$autoBudget->auto_budget_type]; $abType = $types[$autoBudget->auto_budget_type];
$abAmount = number_format((float)$autoBudget->amount, $autoBudget->transactionCurrency->decimal_places, '.', ''); $abAmount = number_format((float) $autoBudget->amount, $autoBudget->transactionCurrency->decimal_places, '.', '');
$abPeriod = $autoBudget->period; $abPeriod = $autoBudget->period;
} }
return [ return [
'id' => (string)$budget->id, 'id' => (string) $budget->id,
'created_at' => $budget->created_at->toAtomString(), 'created_at' => $budget->created_at->toAtomString(),
'updated_at' => $budget->updated_at->toAtomString(), 'updated_at' => $budget->updated_at->toAtomString(),
'active' => $budget->active, 'active' => $budget->active,
'name' => $budget->name, 'name' => $budget->name,
'order' => $budget->order, 'order' => $budget->order,
'notes' => $notes,
'auto_budget_type' => $abType, 'auto_budget_type' => $abType,
'auto_budget_period' => $abPeriod, 'auto_budget_period' => $abPeriod,
'auto_budget_currency_id' => $abCurrencyId, 'auto_budget_currency_id' => $abCurrencyId,
@@ -118,7 +120,7 @@ class BudgetTransformer extends AbstractTransformer
{ {
$return = []; $return = [];
foreach ($array as $data) { foreach ($array as $data) {
$data['sum'] = number_format((float)$data['sum'], (int)$data['currency_decimal_places'], '.', ''); $data['sum'] = number_format((float) $data['sum'], (int) $data['currency_decimal_places'], '.', '');
$return[] = $data; $return[] = $data;
} }

View File

@@ -14,6 +14,7 @@
<div class="col-12"> <div class="col-12">
<q-card bordered> <q-card bordered>
<q-card-section> <q-card-section>
<!-- TODO dont forget budget notes -->
<div class="text-h6">Info for new budget</div> <div class="text-h6">Info for new budget</div>
</q-card-section> </q-card-section>
<q-card-section> <q-card-section>

View File

@@ -14,6 +14,7 @@
<div class="col-12"> <div class="col-12">
<q-card bordered> <q-card bordered>
<q-card-section> <q-card-section>
<!-- TODO dont forget budget notes -->
<div class="text-h6">Edit budget</div> <div class="text-h6">Edit budget</div>
</q-card-section> </q-card-section>
<q-card-section> <q-card-section>