mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Add notes to budget https://github.com/firefly-iii/firefly-iii/issues/5373
This commit is contained in:
parent
52a5995bd1
commit
1e1aa28ab2
@ -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',
|
||||
|
@ -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',
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
*
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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>
|
||||
|
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user