mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-15 08:35:00 +00:00
Clean up some code.
This commit is contained in:
@@ -50,10 +50,10 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
*/
|
||||
public function budgeted(Carbon $start, Carbon $end, TransactionCurrency $currency, ?Collection $budgets = null): string
|
||||
{
|
||||
$query = BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
|
||||
$query = BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
|
||||
|
||||
// same complex where query as below.
|
||||
->where(
|
||||
->where(
|
||||
static function (Builder $q5) use ($start, $end): void {
|
||||
$q5->where(
|
||||
static function (Builder $q1) use ($start, $end): void {
|
||||
@@ -63,27 +63,30 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
$q2->where('budget_limits.end_date', '<=', $end->format('Y-m-d'));
|
||||
}
|
||||
)
|
||||
->orWhere(
|
||||
static function (Builder $q3) use ($start, $end): void {
|
||||
$q3->where('budget_limits.start_date', '>=', $start->format('Y-m-d'));
|
||||
$q3->where('budget_limits.start_date', '<=', $end->format('Y-m-d'));
|
||||
}
|
||||
);
|
||||
->orWhere(
|
||||
static function (Builder $q3) use ($start, $end): void {
|
||||
$q3->where('budget_limits.start_date', '>=', $start->format('Y-m-d'));
|
||||
$q3->where('budget_limits.start_date', '<=', $end->format('Y-m-d'));
|
||||
}
|
||||
)
|
||||
;
|
||||
}
|
||||
)
|
||||
->orWhere(
|
||||
static function (Builder $q4) use ($start, $end): void {
|
||||
// or start is before start AND end is after end.
|
||||
$q4->where('budget_limits.start_date', '<=', $start->format('Y-m-d'));
|
||||
$q4->where('budget_limits.end_date', '>=', $end->format('Y-m-d'));
|
||||
}
|
||||
);
|
||||
->orWhere(
|
||||
static function (Builder $q4) use ($start, $end): void {
|
||||
// or start is before start AND end is after end.
|
||||
$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('budget_limits.transaction_currency_id', $currency->id)
|
||||
->whereNull('budgets.deleted_at')
|
||||
->where('budgets.active', true)
|
||||
->where('budgets.user_id', $this->user->id)
|
||||
;
|
||||
if (null !== $budgets && $budgets->count() > 0) {
|
||||
$query->whereIn('budget_limits.budget_id', $budgets->pluck('id')->toArray());
|
||||
}
|
||||
@@ -135,17 +138,19 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
// both are NULL:
|
||||
if (null === $start && null === $end) {
|
||||
return BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
|
||||
->with(['budget'])
|
||||
->where('budgets.user_id', $this->user->id)
|
||||
->whereNull('budgets.deleted_at')
|
||||
->get(['budget_limits.*']);
|
||||
->with(['budget'])
|
||||
->where('budgets.user_id', $this->user->id)
|
||||
->whereNull('budgets.deleted_at')
|
||||
->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);
|
||||
->with(['budget'])
|
||||
->whereNull('budgets.deleted_at')
|
||||
->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'));
|
||||
@@ -160,36 +165,39 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
|
||||
// neither are NULL:
|
||||
return BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
|
||||
->with(['budget'])
|
||||
->where('budgets.user_id', $this->user->id)
|
||||
->whereNull('budgets.deleted_at')
|
||||
->where(
|
||||
static function (Builder $q5) use ($start, $end): void {
|
||||
$q5->where(
|
||||
static function (Builder $q1) use ($start, $end): void {
|
||||
$q1->where(
|
||||
static function (Builder $q2) use ($start, $end): void {
|
||||
$q2->where('budget_limits.end_date', '>=', $start->format('Y-m-d'));
|
||||
$q2->where('budget_limits.end_date', '<=', $end->format('Y-m-d'));
|
||||
}
|
||||
)
|
||||
->orWhere(
|
||||
static function (Builder $q3) use ($start, $end): void {
|
||||
$q3->where('budget_limits.start_date', '>=', $start->format('Y-m-d'));
|
||||
$q3->where('budget_limits.start_date', '<=', $end->format('Y-m-d'));
|
||||
}
|
||||
);
|
||||
}
|
||||
)
|
||||
->orWhere(
|
||||
static function (Builder $q4) use ($start, $end): void {
|
||||
// or start is before start AND end is after end.
|
||||
$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.*']);
|
||||
->with(['budget'])
|
||||
->where('budgets.user_id', $this->user->id)
|
||||
->whereNull('budgets.deleted_at')
|
||||
->where(
|
||||
static function (Builder $q5) use ($start, $end): void {
|
||||
$q5->where(
|
||||
static function (Builder $q1) use ($start, $end): void {
|
||||
$q1->where(
|
||||
static function (Builder $q2) use ($start, $end): void {
|
||||
$q2->where('budget_limits.end_date', '>=', $start->format('Y-m-d'));
|
||||
$q2->where('budget_limits.end_date', '<=', $end->format('Y-m-d'));
|
||||
}
|
||||
)
|
||||
->orWhere(
|
||||
static function (Builder $q3) use ($start, $end): void {
|
||||
$q3->where('budget_limits.start_date', '>=', $start->format('Y-m-d'));
|
||||
$q3->where('budget_limits.start_date', '<=', $end->format('Y-m-d'));
|
||||
}
|
||||
)
|
||||
;
|
||||
}
|
||||
)
|
||||
->orWhere(
|
||||
static function (Builder $q4) use ($start, $end): void {
|
||||
// or start is before start AND end is after end.
|
||||
$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.*'])
|
||||
;
|
||||
}
|
||||
|
||||
public function getBudgetLimits(Budget $budget, ?Carbon $start = null, ?Carbon $end = null): Collection
|
||||
@@ -214,38 +222,41 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
|
||||
// when both dates are set:
|
||||
return $budget->budgetlimits()
|
||||
->where(
|
||||
static function (Builder $q5) use ($start, $end): void { // @phpstan-ignore-line
|
||||
$q5->where(
|
||||
static function (Builder $q1) use ($start, $end): void {
|
||||
// budget limit ends within period
|
||||
$q1->where(
|
||||
static function (Builder $q2) use ($start, $end): void {
|
||||
$q2->where('budget_limits.end_date', '>=', $start->format('Y-m-d 00:00:00'));
|
||||
$q2->where('budget_limits.end_date', '<=', $end->format('Y-m-d 23:59:59'));
|
||||
}
|
||||
)
|
||||
// budget limit start within period
|
||||
->orWhere(
|
||||
static function (Builder $q3) use ($start, $end): void {
|
||||
$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(
|
||||
static function (Builder $q4) use ($start, $end): void {
|
||||
// or start is before start AND end is after end.
|
||||
$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.*']);
|
||||
->where(
|
||||
static function (Builder $q5) use ($start, $end): void { // @phpstan-ignore-line
|
||||
$q5->where(
|
||||
static function (Builder $q1) use ($start, $end): void {
|
||||
// budget limit ends within period
|
||||
$q1->where(
|
||||
static function (Builder $q2) use ($start, $end): void {
|
||||
$q2->where('budget_limits.end_date', '>=', $start->format('Y-m-d 00:00:00'));
|
||||
$q2->where('budget_limits.end_date', '<=', $end->format('Y-m-d 23:59:59'));
|
||||
}
|
||||
)
|
||||
// budget limit start within period
|
||||
->orWhere(
|
||||
static function (Builder $q3) use ($start, $end): void {
|
||||
$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(
|
||||
static function (Builder $q4) use ($start, $end): void {
|
||||
// or start is before start AND end is after end.
|
||||
$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.*'])
|
||||
;
|
||||
}
|
||||
|
||||
public function setUser(null | Authenticatable | User $user): void
|
||||
public function setUser(null|Authenticatable|User $user): void
|
||||
{
|
||||
if ($user instanceof User) {
|
||||
$this->user = $user;
|
||||
@@ -259,33 +270,34 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
{
|
||||
// if no currency has been provided, use the user's default currency:
|
||||
/** @var TransactionCurrencyFactory $factory */
|
||||
$factory = app(TransactionCurrencyFactory::class);
|
||||
$currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null);
|
||||
$factory = app(TransactionCurrencyFactory::class);
|
||||
$currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null);
|
||||
if (null === $currency) {
|
||||
$currency = app('amount')->getDefaultCurrencyByUserGroup($this->user->userGroup);
|
||||
}
|
||||
$currency->enabled = true;
|
||||
$currency->enabled = true;
|
||||
$currency->save();
|
||||
|
||||
// find the budget:
|
||||
$budget = $this->user->budgets()->find((int) $data['budget_id']);
|
||||
$budget = $this->user->budgets()->find((int) $data['budget_id']);
|
||||
if (null === $budget) {
|
||||
throw new FireflyException('200004: Budget does not exist.');
|
||||
}
|
||||
|
||||
// find limit with same date range and currency.
|
||||
$limit = $budget->budgetlimits()
|
||||
->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.*']);
|
||||
$limit = $budget->budgetlimits()
|
||||
->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.*'])
|
||||
;
|
||||
if (null !== $limit) {
|
||||
throw new FireflyException('200027: Budget limit already exists.');
|
||||
}
|
||||
app('log')->debug('No existing budget limit, create a new one');
|
||||
|
||||
// or create one and return it.
|
||||
$limit = new BudgetLimit();
|
||||
$limit = new BudgetLimit();
|
||||
$limit->budget()->associate($budget);
|
||||
$limit->start_date = $data['start_date']->format('Y-m-d');
|
||||
$limit->end_date = $data['end_date']->format('Y-m-d');
|
||||
@@ -293,7 +305,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
$limit->transaction_currency_id = $currency->id;
|
||||
$limit->save();
|
||||
|
||||
$noteText = (string) ($data['notes'] ?? '');
|
||||
$noteText = (string) ($data['notes'] ?? '');
|
||||
if ('' !== $noteText) {
|
||||
$this->setNoteText($limit, $noteText);
|
||||
}
|
||||
@@ -303,13 +315,13 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
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('transaction_currency_id', $currency->id)
|
||||
->where('start_date', $start->format('Y-m-d'))
|
||||
->where('end_date', $end->format('Y-m-d'))->first()
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -317,8 +329,8 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
*/
|
||||
public function update(BudgetLimit $budgetLimit, array $data): BudgetLimit
|
||||
{
|
||||
$budgetLimit->amount = array_key_exists('amount', $data) ? $data['amount'] : $budgetLimit->amount;
|
||||
$budgetLimit->budget_id = array_key_exists('budget_id', $data) ? $data['budget_id'] : $budgetLimit->budget_id;
|
||||
$budgetLimit->amount = array_key_exists('amount', $data) ? $data['amount'] : $budgetLimit->amount;
|
||||
$budgetLimit->budget_id = array_key_exists('budget_id', $data) ? $data['budget_id'] : $budgetLimit->budget_id;
|
||||
|
||||
if (array_key_exists('start', $data)) {
|
||||
$budgetLimit->start_date = $data['start']->startOfDay();
|
||||
@@ -330,7 +342,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
}
|
||||
|
||||
// if no currency has been provided, use the user's default currency:
|
||||
$currency = null;
|
||||
$currency = null;
|
||||
|
||||
// update if relevant:
|
||||
if (array_key_exists('currency_id', $data) || array_key_exists('currency_code', $data)) {
|
||||
@@ -342,14 +354,14 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
if (null === $currency) {
|
||||
$currency = $budgetLimit->transactionCurrency ?? app('amount')->getDefaultCurrencyByUserGroup($this->user->userGroup);
|
||||
}
|
||||
$currency->enabled = true;
|
||||
$currency->enabled = true;
|
||||
$currency->save();
|
||||
|
||||
$budgetLimit->transaction_currency_id = $currency->id;
|
||||
$budgetLimit->save();
|
||||
|
||||
// update notes if they exist.
|
||||
if(array_key_exists('notes', $data)) {
|
||||
if (array_key_exists('notes', $data)) {
|
||||
$this->setNoteText($budgetLimit, (string)$data['notes']);
|
||||
}
|
||||
|
||||
@@ -359,26 +371,29 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
public function updateLimitAmount(Budget $budget, Carbon $start, Carbon $end, string $amount): ?BudgetLimit
|
||||
{
|
||||
// count the limits:
|
||||
$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.*');
|
||||
$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.*')
|
||||
;
|
||||
app('log')->debug(sprintf('Found %d budget limits.', $limits));
|
||||
|
||||
// there might be a budget limit for these dates:
|
||||
/** @var null|BudgetLimit $limit */
|
||||
$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.*']);
|
||||
$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.*'])
|
||||
;
|
||||
|
||||
// if more than 1 limit found, delete the others:
|
||||
if ($limits > 1 && null !== $limit) {
|
||||
app('log')->debug(sprintf('Found more than 1, delete all except #%d', $limit->id));
|
||||
$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.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()
|
||||
;
|
||||
}
|
||||
|
||||
// delete if amount is zero.
|
||||
@@ -400,7 +415,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
}
|
||||
app('log')->debug('No existing budget limit, create a new one');
|
||||
// or create one and return it.
|
||||
$limit = new BudgetLimit();
|
||||
$limit = new BudgetLimit();
|
||||
$limit->budget()->associate($budget);
|
||||
$limit->start_date = $start->startOfDay();
|
||||
$limit->start_date_tz = $start->format('e');
|
||||
@@ -413,12 +428,14 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||
return $limit;
|
||||
}
|
||||
|
||||
#[\Override] public function getNoteText(BudgetLimit $budgetLimit): string
|
||||
#[\Override]
|
||||
public function getNoteText(BudgetLimit $budgetLimit): string
|
||||
{
|
||||
return (string) $budgetLimit->notes()->first()?->text;
|
||||
}
|
||||
|
||||
#[\Override] public function setNoteText(BudgetLimit $budgetLimit, string $text): void
|
||||
#[\Override]
|
||||
public function setNoteText(BudgetLimit $budgetLimit, string $text): void
|
||||
{
|
||||
$dbNote = $budgetLimit->notes()->first();
|
||||
if ('' !== $text) {
|
||||
|
Reference in New Issue
Block a user