This commit is contained in:
James Cole 2022-03-19 11:38:02 +01:00
parent 52a5995bd1
commit 1e1aa28ab2
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
8 changed files with 79 additions and 7 deletions

View File

@ -50,6 +50,7 @@ class StoreRequest extends FormRequest
'name' => ['name', 'string'],
'active' => ['active', 'boolean'],
'order' => ['active', 'integer'],
'notes' => ['notes', 'string'],
// auto budget currency:
'currency_id' => ['auto_budget_currency_id', 'integer'],
@ -74,6 +75,7 @@ class StoreRequest extends FormRequest
'active' => [new IsBoolean],
'currency_id' => 'exists:transaction_currencies,id',
'currency_code' => 'exists:transaction_currencies,code',
'notes' => 'nullable|between:1,65536',
// auto budget info
'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',

View File

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

View File

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

View File

@ -30,6 +30,7 @@ use FireflyIII\Models\Attachment;
use FireflyIII\Models\AutoBudget;
use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\Note;
use FireflyIII\Models\RecurrenceTransactionMeta;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\RuleTrigger;
@ -47,8 +48,7 @@ use Storage;
*/
class BudgetRepository implements BudgetRepositoryInterface
{
/** @var User */
private $user;
private User $user;
/**
* @return bool
@ -323,6 +323,12 @@ class BudgetRepository implements BudgetRepositoryInterface
Log::error($e->getTraceAsString());
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)) {
return $newBudget;
}
@ -400,6 +406,9 @@ class BudgetRepository implements BudgetRepositoryInterface
if (array_key_exists('active', $data)) {
$budget->active = $data['active'];
}
if(array_key_exists('notes', $data)) {
$this->setNoteText($budget, (string)$data['notes']);
}
$budget->save();
// update or create auto-budget:
@ -515,4 +524,44 @@ class BudgetRepository implements BudgetRepositoryInterface
$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;
/**
* @param Budget $budget
* @return string|null
*/
public function getNoteText(Budget $budget): ?string;
/**
* @param Budget $budget
*

View File

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

View File

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

View File

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