mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
API allows update/set of budget limit notes. https://github.com/firefly-iii/firefly-iii/issues/5523
This commit is contained in:
parent
c25c0d37c5
commit
f5c56e02da
@ -69,6 +69,7 @@ class StoreController extends Controller
|
||||
$data = $request->getAll();
|
||||
$data['start_date'] = $data['start'];
|
||||
$data['end_date'] = $data['end'];
|
||||
$data['notes'] = $data['notes'];
|
||||
$data['budget_id'] = $budget->id;
|
||||
|
||||
$budgetLimit = $this->blRepository->store($data);
|
||||
|
@ -48,6 +48,7 @@ class StoreRequest extends FormRequest
|
||||
'amount' => $this->convertString('amount'),
|
||||
'currency_id' => $this->convertInteger('currency_id'),
|
||||
'currency_code' => $this->convertString('currency_code'),
|
||||
'notes' => $this->stringWithNewlines('notes'),
|
||||
];
|
||||
}
|
||||
|
||||
@ -62,6 +63,7 @@ class StoreRequest extends FormRequest
|
||||
'amount' => ['required', new IsValidPositiveAmount()],
|
||||
'currency_id' => 'numeric|exists:transaction_currencies,id',
|
||||
'currency_code' => 'min:3|max:51|exists:transaction_currencies,code',
|
||||
'notes' => 'nullable|min:0|max:32768',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -51,8 +51,12 @@ class UpdateRequest extends FormRequest
|
||||
'amount' => ['amount', 'convertString'],
|
||||
'currency_id' => ['currency_id', 'convertInteger'],
|
||||
'currency_code' => ['currency_code', 'convertString'],
|
||||
'notes' => ['notes', 'stringWithNewlines'],
|
||||
];
|
||||
|
||||
if(false === $this->has('notes')) {
|
||||
// ignore notes, not submitted.
|
||||
unset($fields['notes']);
|
||||
}
|
||||
return $this->getAllData($fields);
|
||||
}
|
||||
|
||||
@ -67,6 +71,7 @@ class UpdateRequest extends FormRequest
|
||||
'amount' => ['nullable', new IsValidPositiveAmount()],
|
||||
'currency_id' => 'numeric|exists:transaction_currencies,id',
|
||||
'currency_code' => 'min:3|max:51|exists:transaction_currencies,code',
|
||||
'notes' => 'nullable|min:0|max:32768',
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -144,7 +144,7 @@ class Account extends Model
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all of the notes.
|
||||
* Get all the notes.
|
||||
*/
|
||||
public function notes(): MorphMany
|
||||
{
|
||||
|
@ -31,6 +31,7 @@ use FireflyIII\Support\Models\ReturnsIntegerIdTrait;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
/**
|
||||
@ -90,6 +91,14 @@ class BudgetLimit extends Model
|
||||
return $this->belongsTo(TransactionCurrency::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the notes.
|
||||
*/
|
||||
public function notes(): MorphMany
|
||||
{
|
||||
return $this->morphMany(Note::class, 'noteable');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the amount
|
||||
*/
|
||||
|
@ -29,6 +29,7 @@ use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Factory\TransactionCurrencyFactory;
|
||||
use FireflyIII\Models\Budget;
|
||||
use FireflyIII\Models\BudgetLimit;
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
@ -67,8 +68,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
$q3->where('budget_limits.start_date', '>=', $start->format('Y-m-d'));
|
||||
$q3->where('budget_limits.start_date', '<=', $end->format('Y-m-d'));
|
||||
}
|
||||
)
|
||||
;
|
||||
);
|
||||
}
|
||||
)
|
||||
->orWhere(
|
||||
@ -77,15 +77,13 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
$q4->where('budget_limits.start_date', '<=', $start->format('Y-m-d'));
|
||||
$q4->where('budget_limits.end_date', '>=', $end->format('Y-m-d'));
|
||||
}
|
||||
)
|
||||
;
|
||||
);
|
||||
}
|
||||
)
|
||||
->where('budget_limits.transaction_currency_id', $currency->id)
|
||||
->whereNull('budgets.deleted_at')
|
||||
->where('budgets.active', true)
|
||||
->where('budgets.user_id', $this->user->id)
|
||||
;
|
||||
->where('budgets.user_id', $this->user->id);
|
||||
if (null !== $budgets && $budgets->count() > 0) {
|
||||
$query->whereIn('budget_limits.budget_id', $budgets->pluck('id')->toArray());
|
||||
}
|
||||
@ -140,16 +138,14 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
->with(['budget'])
|
||||
->where('budgets.user_id', $this->user->id)
|
||||
->whereNull('budgets.deleted_at')
|
||||
->get(['budget_limits.*'])
|
||||
;
|
||||
->get(['budget_limits.*']);
|
||||
}
|
||||
// one of the two is NULL.
|
||||
if (null === $start xor null === $end) {
|
||||
$query = BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
|
||||
->with(['budget'])
|
||||
->whereNull('budgets.deleted_at')
|
||||
->where('budgets.user_id', $this->user->id)
|
||||
;
|
||||
->where('budgets.user_id', $this->user->id);
|
||||
if (null !== $end) {
|
||||
// end date must be before $end.
|
||||
$query->where('end_date', '<=', $end->format('Y-m-d 00:00:00'));
|
||||
@ -182,8 +178,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
$q3->where('budget_limits.start_date', '>=', $start->format('Y-m-d'));
|
||||
$q3->where('budget_limits.start_date', '<=', $end->format('Y-m-d'));
|
||||
}
|
||||
)
|
||||
;
|
||||
);
|
||||
}
|
||||
)
|
||||
->orWhere(
|
||||
@ -192,11 +187,9 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
$q4->where('budget_limits.start_date', '<=', $start->format('Y-m-d'));
|
||||
$q4->where('budget_limits.end_date', '>=', $end->format('Y-m-d'));
|
||||
}
|
||||
)
|
||||
;
|
||||
);
|
||||
}
|
||||
)->get(['budget_limits.*'])
|
||||
;
|
||||
)->get(['budget_limits.*']);
|
||||
}
|
||||
|
||||
public function getBudgetLimits(Budget $budget, ?Carbon $start = null, ?Carbon $end = null): Collection
|
||||
@ -238,8 +231,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
$q3->where('budget_limits.start_date', '>=', $start->format('Y-m-d 00:00:00'));
|
||||
$q3->where('budget_limits.start_date', '<=', $end->format('Y-m-d 23:59:59'));
|
||||
}
|
||||
)
|
||||
;
|
||||
);
|
||||
}
|
||||
)
|
||||
->orWhere(
|
||||
@ -248,11 +240,9 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
$q4->where('budget_limits.start_date', '<=', $start->format('Y-m-d 23:59:59'));
|
||||
$q4->where('budget_limits.end_date', '>=', $end->format('Y-m-d 00:00:00'));
|
||||
}
|
||||
)
|
||||
;
|
||||
);
|
||||
}
|
||||
)->orderBy('budget_limits.start_date', 'DESC')->get(['budget_limits.*'])
|
||||
;
|
||||
)->orderBy('budget_limits.start_date', 'DESC')->get(['budget_limits.*']);
|
||||
}
|
||||
|
||||
public function setUser(null | Authenticatable | User $user): void
|
||||
@ -288,8 +278,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
->where('budget_limits.start_date', $data['start_date']->format('Y-m-d'))
|
||||
->where('budget_limits.end_date', $data['end_date']->format('Y-m-d'))
|
||||
->where('budget_limits.transaction_currency_id', $currency->id)
|
||||
->first(['budget_limits.*'])
|
||||
;
|
||||
->first(['budget_limits.*']);
|
||||
if (null !== $limit) {
|
||||
throw new FireflyException('200027: Budget limit already exists.');
|
||||
}
|
||||
@ -303,18 +292,24 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
$limit->amount = $data['amount'];
|
||||
$limit->transaction_currency_id = $currency->id;
|
||||
$limit->save();
|
||||
|
||||
$noteText = (string) ($data['notes'] ?? '');
|
||||
if ('' !== $noteText) {
|
||||
$this->setNoteText($limit, $noteText);
|
||||
}
|
||||
|
||||
app('log')->debug(sprintf('Created new budget limit with ID #%d and amount %s', $limit->id, $data['amount']));
|
||||
|
||||
return $limit;
|
||||
}
|
||||
|
||||
|
||||
public function find(Budget $budget, TransactionCurrency $currency, Carbon $start, Carbon $end): ?BudgetLimit
|
||||
{
|
||||
return $budget->budgetlimits()
|
||||
->where('transaction_currency_id', $currency->id)
|
||||
->where('start_date', $start->format('Y-m-d'))
|
||||
->where('end_date', $end->format('Y-m-d'))->first()
|
||||
;
|
||||
->where('end_date', $end->format('Y-m-d'))->first();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -353,6 +348,11 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
$budgetLimit->transaction_currency_id = $currency->id;
|
||||
$budgetLimit->save();
|
||||
|
||||
// update notes if they exist.
|
||||
if(array_key_exists('notes', $data)) {
|
||||
$this->setNoteText($budgetLimit, (string)$data['notes']);
|
||||
}
|
||||
|
||||
return $budgetLimit;
|
||||
}
|
||||
|
||||
@ -362,8 +362,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
$limits = $budget->budgetlimits()
|
||||
->where('budget_limits.start_date', $start->format('Y-m-d 00:00:00'))
|
||||
->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00'))
|
||||
->count('budget_limits.*')
|
||||
;
|
||||
->count('budget_limits.*');
|
||||
app('log')->debug(sprintf('Found %d budget limits.', $limits));
|
||||
|
||||
// there might be a budget limit for these dates:
|
||||
@ -371,8 +370,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
$limit = $budget->budgetlimits()
|
||||
->where('budget_limits.start_date', $start->format('Y-m-d 00:00:00'))
|
||||
->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00'))
|
||||
->first(['budget_limits.*'])
|
||||
;
|
||||
->first(['budget_limits.*']);
|
||||
|
||||
// if more than 1 limit found, delete the others:
|
||||
if ($limits > 1 && null !== $limit) {
|
||||
@ -380,8 +378,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
$budget->budgetlimits()
|
||||
->where('budget_limits.start_date', $start->format('Y-m-d 00:00:00'))
|
||||
->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00'))
|
||||
->where('budget_limits.id', '!=', $limit->id)->delete()
|
||||
;
|
||||
->where('budget_limits.id', '!=', $limit->id)->delete();
|
||||
}
|
||||
|
||||
// delete if amount is zero.
|
||||
@ -415,4 +412,25 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
|
||||
return $limit;
|
||||
}
|
||||
|
||||
#[\Override] public function getNoteText(BudgetLimit $budgetLimit): string
|
||||
{
|
||||
return (string) $budgetLimit->notes()->first()?->text;
|
||||
}
|
||||
|
||||
#[\Override] public function setNoteText(BudgetLimit $budgetLimit, string $text): void
|
||||
{
|
||||
$dbNote = $budgetLimit->notes()->first();
|
||||
if ('' !== $text) {
|
||||
if (null === $dbNote) {
|
||||
$dbNote = new Note();
|
||||
$dbNote->noteable()->associate($budgetLimit);
|
||||
}
|
||||
$dbNote->text = trim($text);
|
||||
$dbNote->save();
|
||||
|
||||
return;
|
||||
}
|
||||
$dbNote?->delete();
|
||||
}
|
||||
}
|
||||
|
@ -48,6 +48,9 @@ interface BudgetLimitRepositoryInterface
|
||||
*/
|
||||
public function destroyAll(): void;
|
||||
|
||||
public function getNoteText(BudgetLimit $budgetLimit): string;
|
||||
public function setNoteText(BudgetLimit $budgetLimit, string $text): void;
|
||||
|
||||
/**
|
||||
* Destroy a budget limit.
|
||||
*/
|
||||
|
@ -25,6 +25,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Transformers;
|
||||
|
||||
use FireflyIII\Models\BudgetLimit;
|
||||
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
|
||||
use FireflyIII\Repositories\Budget\OperationsRepository;
|
||||
use Illuminate\Support\Collection;
|
||||
use League\Fractal\Resource\Item;
|
||||
@ -55,7 +56,9 @@ class BudgetLimitTransformer extends AbstractTransformer
|
||||
public function transform(BudgetLimit $budgetLimit): array
|
||||
{
|
||||
$repository = app(OperationsRepository::class);
|
||||
$limitRepos = app(BudgetLimitRepositoryInterface::class);
|
||||
$repository->setUser($budgetLimit->budget->user);
|
||||
$limitRepos->setUser($budgetLimit->budget->user);
|
||||
$expenses = $repository->sumExpenses(
|
||||
$budgetLimit->start_date,
|
||||
$budgetLimit->end_date,
|
||||
@ -65,6 +68,7 @@ class BudgetLimitTransformer extends AbstractTransformer
|
||||
);
|
||||
$currency = $budgetLimit->transactionCurrency;
|
||||
$amount = $budgetLimit->amount;
|
||||
$notes = $limitRepos->getNoteText($budgetLimit);
|
||||
$currencyDecimalPlaces = 2;
|
||||
$currencyId = null;
|
||||
$currencyName = null;
|
||||
@ -95,6 +99,7 @@ class BudgetLimitTransformer extends AbstractTransformer
|
||||
'amount' => $amount,
|
||||
'period' => $budgetLimit->period,
|
||||
'spent' => $expenses[$currencyId]['sum'] ?? '0',
|
||||
'notes' => '' === $notes ? null : $notes,
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
|
Loading…
Reference in New Issue
Block a user