Allow sending of webhooks from budget limit store.

This commit is contained in:
James Cole
2025-09-14 09:14:41 +02:00
parent 9d9483e20f
commit 768bd892c8
5 changed files with 103 additions and 29 deletions

View File

@@ -31,8 +31,10 @@ use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\Support\Repositories\UserGroup\UserGroupInterface;
use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait;
use FireflyIII\Support\Singleton\PreferencesSingleton;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
@@ -271,7 +273,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface, UserGroup
$factory = app(TransactionCurrencyFactory::class);
$currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null);
if (null === $currency) {
$currency = app('amount')->getPrimaryCurrencyByUserGroup($this->user->userGroup);
$currency = Amount::getPrimaryCurrencyByUserGroup($this->user->userGroup);
}
$currency->enabled = true;
$currency->save();
@@ -293,7 +295,11 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface, UserGroup
if (null !== $limit) {
throw new FireflyException('200027: Budget limit already exists.');
}
app('log')->debug('No existing budget limit, create a new one');
Log::debug('No existing budget limit, create a new one');
// this is a lame trick to communicate with the observer.
$singleton = PreferencesSingleton::getInstance();
$singleton->setPreference('fire_webhooks_bl_store', $data['fire_webhooks'] ?? true);
// or create one and return it.
$limit = new BudgetLimit();
@@ -309,7 +315,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface, UserGroup
$this->setNoteText($limit, $noteText);
}
app('log')->debug(sprintf('Created new budget limit with ID #%d and amount %s', $limit->id, $data['amount']));
Log::debug(sprintf('Created new budget limit with ID #%d and amount %s', $limit->id, $data['amount']));
return $limit;
}
@@ -393,7 +399,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface, UserGroup
->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));
Log::debug(sprintf('Found %d budget limits.', $limits));
// there might be a budget limit for these dates:
/** @var null|BudgetLimit $limit */
@@ -405,7 +411,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface, UserGroup
// 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));
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'))
@@ -417,20 +423,20 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface, UserGroup
// Returns 0 if the two operands are equal,
// 1 if the left_operand is larger than the right_operand, -1 otherwise.
if (null !== $limit && bccomp($amount, '0') <= 0) {
app('log')->debug(sprintf('%s is zero, delete budget limit #%d', $amount, $limit->id));
Log::debug(sprintf('%s is zero, delete budget limit #%d', $amount, $limit->id));
$limit->delete();
return null;
}
// update if exists:
if (null !== $limit) {
app('log')->debug(sprintf('Existing budget limit is #%d, update this to amount %s', $limit->id, $amount));
Log::debug(sprintf('Existing budget limit is #%d, update this to amount %s', $limit->id, $amount));
$limit->amount = $amount;
$limit->save();
return $limit;
}
app('log')->debug('No existing budget limit, create a new one');
Log::debug('No existing budget limit, create a new one');
// or create one and return it.
$limit = new BudgetLimit();
$limit->budget()->associate($budget);
@@ -440,7 +446,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface, UserGroup
$limit->end_date_tz = $end->format('e');
$limit->amount = $amount;
$limit->save();
app('log')->debug(sprintf('Created new budget limit with ID #%d and amount %s', $limit->id, $amount));
Log::debug(sprintf('Created new budget limit with ID #%d and amount %s', $limit->id, $amount));
return $limit;
}