mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-04-29 02:53:05 +00:00
Clean up events and filters.
This commit is contained in:
@@ -31,7 +31,6 @@ use FireflyIII\Enums\UserRoleEnum;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Support\Debug\Timer;
|
||||
use FireflyIII\Support\Facades\Amount;
|
||||
use FireflyIII\Support\Facades\Steam;
|
||||
use FireflyIII\Support\Http\Api\AccountFilter;
|
||||
@@ -51,7 +50,7 @@ class AccountController extends Controller
|
||||
protected array $acceptedRoles = [UserRoleEnum::READ_ONLY];
|
||||
|
||||
/** @var array<int, string> */
|
||||
private array $balanceTypes;
|
||||
private array $balanceTypes;
|
||||
private AccountRepositoryInterface $repository;
|
||||
|
||||
/**
|
||||
@@ -80,19 +79,17 @@ class AccountController extends Controller
|
||||
*/
|
||||
public function accounts(AutocompleteApiRequest $request): JsonResponse
|
||||
{
|
||||
Log::debug('Before All.');
|
||||
['types' => $types, 'query' => $query, 'date' => $date, 'limit' => $limit] = $request->attributes->all();
|
||||
// Log::debug('Before All.');
|
||||
['types' => $types, 'query' => $query, 'date' => $date, 'limit' => $limit] = $request->attributes->all();
|
||||
|
||||
$date ??= today(config('app.timezone'));
|
||||
|
||||
// set date to end-of-day for account balance. so it is at $date 23:59:59
|
||||
$date->endOfDay();
|
||||
|
||||
$return = [];
|
||||
$timer = Timer::getInstance();
|
||||
$timer->start(sprintf('AC accounts "%s"', $query));
|
||||
$result = $this->repository->searchAccount((string) $query, $types, $limit);
|
||||
$allBalances = Steam::accountsBalancesOptimized($result, $date, $this->primaryCurrency, $this->convertToPrimary);
|
||||
$return = [];
|
||||
$result = $this->repository->searchAccount((string)$query, $types, $limit);
|
||||
$allBalances = Steam::accountsBalancesOptimized($result, $date, $this->primaryCurrency, $this->convertToPrimary);
|
||||
|
||||
/** @var Account $account */
|
||||
foreach ($result as $account) {
|
||||
@@ -109,18 +106,18 @@ class AccountController extends Controller
|
||||
$nameWithBalance = sprintf('%s (%s)', $account->name, Amount::formatAnything($useCurrency, $amount, false));
|
||||
}
|
||||
|
||||
$return[] = [
|
||||
'id' => (string) $account->id,
|
||||
$return[] = [
|
||||
'id' => (string)$account->id,
|
||||
'name' => $account->name,
|
||||
'name_with_balance' => $nameWithBalance,
|
||||
'active' => $account->active,
|
||||
'type' => $account->accountType->type,
|
||||
'currency_id' => (string) $useCurrency->id,
|
||||
'currency_id' => (string)$useCurrency->id,
|
||||
'currency_name' => $useCurrency->name,
|
||||
'currency_code' => $useCurrency->code,
|
||||
'currency_symbol' => $useCurrency->symbol,
|
||||
'currency_decimal_places' => $useCurrency->decimal_places,
|
||||
'account_currency_id' => (string) $currency->id,
|
||||
'account_currency_id' => (string)$currency->id,
|
||||
'account_currency_name' => $currency->name,
|
||||
'account_currency_code' => $currency->code,
|
||||
'account_currency_symbol' => $currency->symbol,
|
||||
@@ -131,12 +128,11 @@ class AccountController extends Controller
|
||||
// custom order.
|
||||
usort($return, static function (array $left, array $right): int {
|
||||
$order = [AccountTypeEnum::ASSET->value, AccountTypeEnum::REVENUE->value, AccountTypeEnum::EXPENSE->value];
|
||||
$posA = (int) array_search($left['type'], $order, true);
|
||||
$posB = (int) array_search($right['type'], $order, true);
|
||||
$posA = (int)array_search($left['type'], $order, true);
|
||||
$posB = (int)array_search($right['type'], $order, true);
|
||||
|
||||
return $posA - $posB;
|
||||
});
|
||||
$timer->stop(sprintf('AC accounts "%s"', $query));
|
||||
|
||||
return response()->api($return);
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ abstract class AggregateFormRequest extends ApiRequest
|
||||
parent::initialize($query, $request, $attributes, $cookies, $files, $server, $content);
|
||||
|
||||
// instantiate all subrequests and share current requests' bags with them
|
||||
Log::debug('Initializing AggregateFormRequest.');
|
||||
// Log::debug('Initializing AggregateFormRequest.');
|
||||
|
||||
/** @var array|string $config */
|
||||
foreach ($this->getRequests() as $config) {
|
||||
@@ -62,7 +62,7 @@ abstract class AggregateFormRequest extends ApiRequest
|
||||
if (!is_a($requestClass, Request::class, true)) {
|
||||
throw new RuntimeException('getRequests() must return class-strings of subclasses of Request');
|
||||
}
|
||||
Log::debug(sprintf('Initializing subrequest %s', $requestClass));
|
||||
// Log::debug(sprintf('Initializing subrequest %s', $requestClass));
|
||||
|
||||
$instance = $this->requests[] = new $requestClass();
|
||||
$instance->request = $this->request;
|
||||
@@ -77,7 +77,7 @@ abstract class AggregateFormRequest extends ApiRequest
|
||||
$instance->handleConfig(is_array($config) ? $config : []);
|
||||
}
|
||||
}
|
||||
Log::debug('Done initializing AggregateFormRequest.');
|
||||
// Log::debug('Done initializing AggregateFormRequest.');
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
|
||||
@@ -35,10 +35,5 @@ class CreatedSingleTransactionGroup extends Event
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*/
|
||||
public function __construct(
|
||||
public TransactionGroupEventFlags $flags,
|
||||
public TransactionGroupEventObjects $objects
|
||||
) {
|
||||
Log::debug(__METHOD__);
|
||||
}
|
||||
public function __construct(public TransactionGroupEventFlags $flags, public TransactionGroupEventObjects $objects) {}
|
||||
}
|
||||
|
||||
@@ -328,7 +328,7 @@ class TransactionJournalFactory
|
||||
|
||||
throw new FireflyException($e->getMessage(), 0, $e);
|
||||
}
|
||||
Log::debug(sprintf('Is part of a batch submission? %s', var_export($row['batch_submission'], true)));
|
||||
// Log::debug(sprintf('Is part of a batch submission? %s', var_export($row['batch_submission'], true)));
|
||||
$journal->save();
|
||||
$this->storeBudget($journal, $row);
|
||||
$this->storeCategory($journal, $row);
|
||||
|
||||
@@ -26,17 +26,13 @@ namespace FireflyIII\Listeners\Model\TransactionGroup;
|
||||
|
||||
use FireflyIII\Enums\WebhookTrigger;
|
||||
use FireflyIII\Events\Model\TransactionGroup\DestroyedSingleTransactionGroup;
|
||||
use FireflyIII\Events\Model\Webhook\WebhookMessagesRequestSending;
|
||||
use FireflyIII\Generator\Webhook\MessageGeneratorInterface;
|
||||
use FireflyIII\Support\Facades\FireflyConfig;
|
||||
use FireflyIII\Support\Models\AccountBalanceCalculator;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class ProcessesDestroyedTransactionGroup implements ShouldQueue
|
||||
{
|
||||
use SupportsGroupProcessingTrait;
|
||||
|
||||
public function handle(DestroyedSingleTransactionGroup $event): void
|
||||
{
|
||||
Log::debug(sprintf('User called %s', get_class($event)));
|
||||
@@ -52,7 +48,7 @@ class ProcessesDestroyedTransactionGroup implements ShouldQueue
|
||||
$this->recalculateCredit($event->objects->accounts);
|
||||
}
|
||||
if ($event->flags->fireWebhooks) {
|
||||
$this->fireWebhooks($event->objects->transactionGroups, WebhookTrigger::DESTROY_TRANSACTION);
|
||||
$this->createWebhookMessages($event->objects->transactionGroups, WebhookTrigger::DESTROY_TRANSACTION);
|
||||
}
|
||||
$this->removePeriodStatistics($event->objects);
|
||||
$this->recalculateRunningBalance($event->objects);
|
||||
|
||||
@@ -38,7 +38,7 @@ class ProcessesNewTransactionGroup implements ShouldQueue
|
||||
|
||||
public function handle(CreatedSingleTransactionGroup|UserRequestedBatchProcessing $event): void
|
||||
{
|
||||
Log::debug(sprintf('User called %s', get_class($event)));
|
||||
Log::debug(sprintf('Running event handler for %s', get_class($event)));
|
||||
|
||||
$setting = FireflyConfig::get('enable_batch_processing', false)->data;
|
||||
if (true === $event->flags->batchSubmission && true === $setting) {
|
||||
@@ -46,7 +46,6 @@ class ProcessesNewTransactionGroup implements ShouldQueue
|
||||
|
||||
return;
|
||||
}
|
||||
Log::debug('Will also collect all open transaction groups and process them as well.');
|
||||
$repository = app(JournalRepositoryInterface::class);
|
||||
$journals = $event->objects->transactionJournals->merge($repository->getAllUncompletedJournals());
|
||||
|
||||
@@ -68,7 +67,7 @@ class ProcessesNewTransactionGroup implements ShouldQueue
|
||||
$this->recalculateCredit($event->objects->accounts);
|
||||
}
|
||||
if ($event->flags->fireWebhooks) {
|
||||
$this->fireWebhooks($event->objects->transactionGroups, WebhookTrigger::STORE_TRANSACTION);
|
||||
$this->createWebhookMessages($event->objects->transactionGroups, WebhookTrigger::STORE_TRANSACTION);
|
||||
}
|
||||
$this->removePeriodStatistics($event->objects);
|
||||
$this->recalculateRunningBalance($event->objects);
|
||||
|
||||
@@ -60,7 +60,7 @@ class ProcessesUpdatedTransactionGroup
|
||||
$this->recalculateCredit($event->objects->accounts);
|
||||
}
|
||||
if ($event->flags->fireWebhooks) {
|
||||
$this->fireWebhooks($event->objects->transactionGroups, WebhookTrigger::UPDATE_TRANSACTION);
|
||||
$this->createWebhookMessages($event->objects->transactionGroups, WebhookTrigger::UPDATE_TRANSACTION);
|
||||
}
|
||||
$this->removePeriodStatistics($event->objects);
|
||||
$this->recalculateRunningBalance($event->objects);
|
||||
|
||||
@@ -34,15 +34,16 @@ trait SupportsGroupProcessingTrait
|
||||
$object = app(CreditRecalculateService::class);
|
||||
$object->setAccounts($accounts);
|
||||
$object->recalculate();
|
||||
Log::debug(sprintf('Done with recalculateCredit for %d account(s)', $accounts->count()));
|
||||
}
|
||||
|
||||
private function fireWebhooks(Collection $groups, WebhookTrigger $trigger): void
|
||||
private function createWebhookMessages(Collection $groups, WebhookTrigger $trigger): void
|
||||
{
|
||||
Log::debug(__METHOD__);
|
||||
Log::debug(sprintf('Will now create webhook messages for %d group(s)', $groups->count()));
|
||||
|
||||
/** @var TransactionGroup $first */
|
||||
$first = $groups->first();
|
||||
$user = $first->user;
|
||||
$first = $groups->first();
|
||||
$user = $first->user;
|
||||
|
||||
/** @var MessageGeneratorInterface $engine */
|
||||
$engine = app(MessageGeneratorInterface::class);
|
||||
@@ -54,36 +55,41 @@ trait SupportsGroupProcessingTrait
|
||||
$engine->setObjects($groups);
|
||||
// tell the generator to generate the messages
|
||||
$engine->generateMessages();
|
||||
|
||||
Log::debug(sprintf('Done with create webhook messages for %d group(s)', $groups->count()));
|
||||
}
|
||||
|
||||
protected function removePeriodStatistics(TransactionGroupEventObjects $objects): void
|
||||
{
|
||||
if (auth()->check()) {
|
||||
// since you get a bunch of journals AND a bunch of
|
||||
// objects, this needs to be a collection
|
||||
/** @var PeriodStatisticRepositoryInterface $repository */
|
||||
$repository = app(PeriodStatisticRepositoryInterface::class);
|
||||
$dates = $this->collectDatesFromJournals($objects->transactionJournals);
|
||||
$repository->deleteStatisticsForType(Account::class, $objects->accounts, $dates);
|
||||
$repository->deleteStatisticsForType(Budget::class, $objects->budgets, $dates);
|
||||
$repository->deleteStatisticsForType(Category::class, $objects->categories, $dates);
|
||||
$repository->deleteStatisticsForType(Tag::class, $objects->tags, $dates);
|
||||
|
||||
// remove if no stuff present:
|
||||
// remove for no tag, no cat, etc.
|
||||
if (0 === $objects->budgets->count()) {
|
||||
Log::debug('No budgets, delete "no_category" stats.');
|
||||
$repository->deleteStatisticsForPrefix('no_budget', $dates);
|
||||
}
|
||||
if (0 === $objects->categories->count()) {
|
||||
Log::debug('No categories, delete "no_category" stats.');
|
||||
$repository->deleteStatisticsForPrefix('no_category', $dates);
|
||||
}
|
||||
if (0 === $objects->tags->count()) {
|
||||
Log::debug('No tags, delete "no_category" stats.');
|
||||
$repository->deleteStatisticsForPrefix('no_tag', $dates);
|
||||
}
|
||||
if (!auth()->check()) {
|
||||
Log::debug('Will NOT remove period statistics for all objects, because no user detected.');
|
||||
}
|
||||
Log::debug('Will now remove period statistics for all objects.');
|
||||
// since you get a bunch of journals AND a bunch of
|
||||
// objects, this needs to be a collection
|
||||
/** @var PeriodStatisticRepositoryInterface $repository */
|
||||
$repository = app(PeriodStatisticRepositoryInterface::class);
|
||||
$dates = $this->collectDatesFromJournals($objects->transactionJournals);
|
||||
$repository->deleteStatisticsForType(Account::class, $objects->accounts, $dates);
|
||||
$repository->deleteStatisticsForType(Budget::class, $objects->budgets, $dates);
|
||||
$repository->deleteStatisticsForType(Category::class, $objects->categories, $dates);
|
||||
$repository->deleteStatisticsForType(Tag::class, $objects->tags, $dates);
|
||||
|
||||
// remove if no stuff present:
|
||||
// remove for no tag, no cat, etc.
|
||||
if (0 === $objects->budgets->count()) {
|
||||
Log::debug('No budgets, delete "no_category" stats.');
|
||||
$repository->deleteStatisticsForPrefix('no_budget', $dates);
|
||||
}
|
||||
if (0 === $objects->categories->count()) {
|
||||
Log::debug('No categories, delete "no_category" stats.');
|
||||
$repository->deleteStatisticsForPrefix('no_category', $dates);
|
||||
}
|
||||
if (0 === $objects->tags->count()) {
|
||||
Log::debug('No tags, delete "no_category" stats.');
|
||||
$repository->deleteStatisticsForPrefix('no_tag', $dates);
|
||||
}
|
||||
Log::debug('Done with remove period statistics for all objects.');
|
||||
}
|
||||
|
||||
private function collectDatesFromJournals(Collection $journals): Collection
|
||||
@@ -99,12 +105,12 @@ trait SupportsGroupProcessingTrait
|
||||
protected function processRules(Collection $set, string $type): void
|
||||
{
|
||||
Log::debug(sprintf('Will now processRules("%s") for %d journal(s)', $type, $set->count()));
|
||||
$array = $set->pluck('id')->toArray();
|
||||
$array = $set->pluck('id')->toArray();
|
||||
|
||||
/** @var TransactionJournal $first */
|
||||
$first = $set->first();
|
||||
$journalIds = implode(',', $array);
|
||||
$user = $first->user;
|
||||
$first = $set->first();
|
||||
$journalIds = implode(',', $array);
|
||||
$user = $first->user;
|
||||
Log::debug(sprintf('Add local operator for journal(s): %s', $journalIds));
|
||||
|
||||
// collect rules:
|
||||
@@ -114,20 +120,21 @@ trait SupportsGroupProcessingTrait
|
||||
// add the groups to the rule engine.
|
||||
// it should run the rules in the group and cancel the group if necessary.
|
||||
Log::debug(sprintf('Fire processRules with ALL %s rule groups.', $type));
|
||||
$groups = $ruleGroupRepository->getRuleGroupsWithRules($type);
|
||||
$groups = $ruleGroupRepository->getRuleGroupsWithRules($type);
|
||||
|
||||
// create and fire rule engine.
|
||||
$newRuleEngine = app(RuleEngineInterface::class);
|
||||
$newRuleEngine = app(RuleEngineInterface::class);
|
||||
$newRuleEngine->setUser($user);
|
||||
$newRuleEngine->addOperator(['type' => 'journal_id', 'value' => $journalIds]);
|
||||
$newRuleEngine->addOperator(['type' => 'journal_id', 'value' => $journalIds]);
|
||||
$newRuleEngine->setRuleGroups($groups);
|
||||
$newRuleEngine->fire();
|
||||
Log::debug(sprintf('Done with processRules("%s") for %d journal(s)', $type, $set->count()));
|
||||
}
|
||||
|
||||
protected function recalculateRunningBalance(TransactionGroupEventObjects $objects): void
|
||||
{
|
||||
Log::debug('Now in recalculateRunningBalance');
|
||||
if (true === FireflyConfig::get('use_running_balance', config('firefly.feature_flags.running_balance_column'))->data) {
|
||||
if (false === FireflyConfig::get('use_running_balance', config('firefly.feature_flags.running_balance_column'))->data) {
|
||||
Log::debug('Running balance is disabled.');
|
||||
|
||||
return;
|
||||
|
||||
@@ -420,7 +420,6 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface,
|
||||
$flags->applyRules = $data['apply_rules'] ?? true;
|
||||
$flags->fireWebhooks = $data['fire_webhooks'] ?? true;
|
||||
$flags->batchSubmission = $data['batch_submission'] ?? false;
|
||||
Log::debug('CreatedSingleTransactionGroup');
|
||||
event(new CreatedSingleTransactionGroup($flags, $objects));
|
||||
Log::debug(sprintf('send event WebhookMessagesRequestSending from %s', __METHOD__));
|
||||
event(new WebhookMessagesRequestSending());
|
||||
|
||||
Reference in New Issue
Block a user