mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-12 09:52:20 +00:00
Optimize available budgets.
This commit is contained in:
@@ -144,8 +144,7 @@ class AccountEnrichment implements EnrichmentInterface
|
|||||||
{
|
{
|
||||||
$set = AccountMeta::whereIn('name', ['is_multi_currency', 'include_net_worth', 'currency_id', 'account_role', 'account_number', 'BIC', 'liability_direction', 'interest', 'interest_period', 'current_debt'])
|
$set = AccountMeta::whereIn('name', ['is_multi_currency', 'include_net_worth', 'currency_id', 'account_role', 'account_number', 'BIC', 'liability_direction', 'interest', 'interest_period', 'current_debt'])
|
||||||
->whereIn('account_id', $this->ids)
|
->whereIn('account_id', $this->ids)
|
||||||
->get(['account_meta.id', 'account_meta.account_id', 'account_meta.name', 'account_meta.data'])->toArray()
|
->get(['account_meta.id', 'account_meta.account_id', 'account_meta.name', 'account_meta.data'])->toArray();
|
||||||
;
|
|
||||||
|
|
||||||
/** @var array $entry */
|
/** @var array $entry */
|
||||||
foreach ($set as $entry) {
|
foreach ($set as $entry) {
|
||||||
@@ -154,10 +153,12 @@ class AccountEnrichment implements EnrichmentInterface
|
|||||||
$this->currencies[(int)$entry['data']] = true;
|
$this->currencies[(int)$entry['data']] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (count($this->currencies) > 0) {
|
||||||
$currencies = TransactionCurrency::whereIn('id', array_keys($this->currencies))->get();
|
$currencies = TransactionCurrency::whereIn('id', array_keys($this->currencies))->get();
|
||||||
foreach ($currencies as $currency) {
|
foreach ($currencies as $currency) {
|
||||||
$this->currencies[(int)$currency->id] = $currency;
|
$this->currencies[(int)$currency->id] = $currency;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
$this->currencies[0] = $this->primaryCurrency;
|
$this->currencies[0] = $this->primaryCurrency;
|
||||||
foreach ($this->currencies as $id => $currency) {
|
foreach ($this->currencies as $id => $currency) {
|
||||||
if (true === $currency) {
|
if (true === $currency) {
|
||||||
@@ -171,8 +172,7 @@ class AccountEnrichment implements EnrichmentInterface
|
|||||||
$notes = Note::query()->whereIn('noteable_id', $this->ids)
|
$notes = Note::query()->whereIn('noteable_id', $this->ids)
|
||||||
->whereNotNull('notes.text')
|
->whereNotNull('notes.text')
|
||||||
->where('notes.text', '!=', '')
|
->where('notes.text', '!=', '')
|
||||||
->where('noteable_type', Account::class)->get(['notes.noteable_id', 'notes.text'])->toArray()
|
->where('noteable_type', Account::class)->get(['notes.noteable_id', 'notes.text'])->toArray();
|
||||||
;
|
|
||||||
foreach ($notes as $note) {
|
foreach ($notes as $note) {
|
||||||
$this->notes[(int)$note['noteable_id']] = (string)$note['text'];
|
$this->notes[(int)$note['noteable_id']] = (string)$note['text'];
|
||||||
}
|
}
|
||||||
@@ -182,8 +182,7 @@ class AccountEnrichment implements EnrichmentInterface
|
|||||||
private function collectLocations(): void
|
private function collectLocations(): void
|
||||||
{
|
{
|
||||||
$locations = Location::query()->whereIn('locatable_id', $this->ids)
|
$locations = Location::query()->whereIn('locatable_id', $this->ids)
|
||||||
->where('locatable_type', Account::class)->get(['locations.locatable_id', 'locations.latitude', 'locations.longitude', 'locations.zoom_level'])->toArray()
|
->where('locatable_type', Account::class)->get(['locations.locatable_id', 'locations.latitude', 'locations.longitude', 'locations.zoom_level'])->toArray();
|
||||||
;
|
|
||||||
foreach ($locations as $location) {
|
foreach ($locations as $location) {
|
||||||
$this->locations[(int)$location['locatable_id']]
|
$this->locations[(int)$location['locatable_id']]
|
||||||
= [
|
= [
|
||||||
@@ -205,8 +204,7 @@ class AccountEnrichment implements EnrichmentInterface
|
|||||||
->setUserGroup($this->userGroup)
|
->setUserGroup($this->userGroup)
|
||||||
->setAccounts($this->collection)
|
->setAccounts($this->collection)
|
||||||
->withAccountInformation()
|
->withAccountInformation()
|
||||||
->setTypes([TransactionTypeEnum::OPENING_BALANCE->value])
|
->setTypes([TransactionTypeEnum::OPENING_BALANCE->value]);
|
||||||
;
|
|
||||||
$journals = $collector->getExtractedJournals();
|
$journals = $collector->getExtractedJournals();
|
||||||
foreach ($journals as $journal) {
|
foreach ($journals as $journal) {
|
||||||
$this->openingBalances[(int)$journal['source_account_id']]
|
$this->openingBalances[(int)$journal['source_account_id']]
|
||||||
@@ -340,7 +338,8 @@ class AccountEnrichment implements EnrichmentInterface
|
|||||||
$this->lastActivities = Steam::getLastActivities($this->ids);
|
$this->lastActivities = Steam::getLastActivities($this->ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function collectBalances(): void {
|
private function collectBalances(): void
|
||||||
|
{
|
||||||
$this->balances = Steam::finalAccountsBalanceOptimized($this->collection, $this->getDate(), $this->primaryCurrency, $this->convertToPrimary);
|
$this->balances = Steam::finalAccountsBalanceOptimized($this->collection, $this->getDate(), $this->primaryCurrency, $this->convertToPrimary);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,6 +45,8 @@ class AvailableBudgetEnrichment implements EnrichmentInterface
|
|||||||
private TransactionCurrency $primaryCurrency;
|
private TransactionCurrency $primaryCurrency;
|
||||||
private bool $convertToPrimary = false;
|
private bool $convertToPrimary = false;
|
||||||
private array $ids = [];
|
private array $ids = [];
|
||||||
|
private array $currencyIds = [];
|
||||||
|
private array $currencies = [];
|
||||||
private Collection $collection;
|
private Collection $collection;
|
||||||
private array $spentInBudgets = [];
|
private array $spentInBudgets = [];
|
||||||
private array $spentOutsideBudgets = [];
|
private array $spentOutsideBudgets = [];
|
||||||
@@ -72,6 +74,7 @@ class AvailableBudgetEnrichment implements EnrichmentInterface
|
|||||||
{
|
{
|
||||||
$this->collection = $collection;
|
$this->collection = $collection;
|
||||||
$this->collectIds();
|
$this->collectIds();
|
||||||
|
$this->collectCurrencies();
|
||||||
$this->collectSpentInfo();
|
$this->collectSpentInfo();
|
||||||
$this->appendCollectedData();
|
$this->appendCollectedData();
|
||||||
|
|
||||||
@@ -109,6 +112,7 @@ class AvailableBudgetEnrichment implements EnrichmentInterface
|
|||||||
/** @var AvailableBudget $availableBudget */
|
/** @var AvailableBudget $availableBudget */
|
||||||
foreach ($this->collection as $availableBudget) {
|
foreach ($this->collection as $availableBudget) {
|
||||||
$this->ids[] = (int)$availableBudget->id;
|
$this->ids[] = (int)$availableBudget->id;
|
||||||
|
$this->currencyIds[(int)$availableBudget->id] = (int)$availableBudget->transaction_currency_id;
|
||||||
}
|
}
|
||||||
$this->ids = array_unique($this->ids);
|
$this->ids = array_unique($this->ids);
|
||||||
}
|
}
|
||||||
@@ -122,14 +126,16 @@ class AvailableBudgetEnrichment implements EnrichmentInterface
|
|||||||
$spentOutsideBudgets = $this->noBudgetRepository->collectExpenses($start, $end, null, null, null);
|
$spentOutsideBudgets = $this->noBudgetRepository->collectExpenses($start, $end, null, null, null);
|
||||||
foreach ($this->collection as $availableBudget) {
|
foreach ($this->collection as $availableBudget) {
|
||||||
$id = (int)$availableBudget->id;
|
$id = (int)$availableBudget->id;
|
||||||
$filteredSpentInBudgets = $this->opsRepository->sumCollectedExpenses($spentInBudgets, $availableBudget->start_date, $availableBudget->end_date, $availableBudget->transactionCurrency, false);
|
$currencyId = $this->currencyIds[$id];
|
||||||
$filteredSpentOutsideBudgets = $this->opsRepository->sumCollectedExpenses($spentOutsideBudgets, $availableBudget->start_date, $availableBudget->end_date, $availableBudget->transactionCurrency, false);
|
$currency = $this->currencies[$currencyId];
|
||||||
|
$filteredSpentInBudgets = $this->opsRepository->sumCollectedExpenses($spentInBudgets, $availableBudget->start_date, $availableBudget->end_date, $currency, false);
|
||||||
|
$filteredSpentOutsideBudgets = $this->opsRepository->sumCollectedExpenses($spentOutsideBudgets, $availableBudget->start_date, $availableBudget->end_date, $currency, false);
|
||||||
$this->spentInBudgets[$id] = array_values($filteredSpentInBudgets);
|
$this->spentInBudgets[$id] = array_values($filteredSpentInBudgets);
|
||||||
$this->spentOutsideBudgets[$id] = array_values($filteredSpentOutsideBudgets);
|
$this->spentOutsideBudgets[$id] = array_values($filteredSpentOutsideBudgets);
|
||||||
|
|
||||||
if (true === $this->convertToPrimary) {
|
if (true === $this->convertToPrimary) {
|
||||||
$pcFilteredSpentInBudgets = $this->opsRepository->sumCollectedExpenses($spentInBudgets, $availableBudget->start_date, $availableBudget->end_date, $availableBudget->transactionCurrency, true);
|
$pcFilteredSpentInBudgets = $this->opsRepository->sumCollectedExpenses($spentInBudgets, $availableBudget->start_date, $availableBudget->end_date, $currency, true);
|
||||||
$pcFilteredSpentOutsideBudgets = $this->opsRepository->sumCollectedExpenses($spentOutsideBudgets, $availableBudget->start_date, $availableBudget->end_date, $availableBudget->transactionCurrency, true);
|
$pcFilteredSpentOutsideBudgets = $this->opsRepository->sumCollectedExpenses($spentOutsideBudgets, $availableBudget->start_date, $availableBudget->end_date, $currency, true);
|
||||||
$this->pcSpentInBudgets[$id] = array_values($pcFilteredSpentInBudgets);
|
$this->pcSpentInBudgets[$id] = array_values($pcFilteredSpentInBudgets);
|
||||||
$this->pcSpentOutsideBudgets[$id] = array_values($pcFilteredSpentOutsideBudgets);
|
$this->pcSpentOutsideBudgets[$id] = array_values($pcFilteredSpentOutsideBudgets);
|
||||||
}
|
}
|
||||||
@@ -145,22 +151,29 @@ class AvailableBudgetEnrichment implements EnrichmentInterface
|
|||||||
|
|
||||||
private function appendCollectedData(): void
|
private function appendCollectedData(): void
|
||||||
{
|
{
|
||||||
$spentInsideBudgets = $this->spentInBudgets;
|
$this->collection = $this->collection->map(function (AvailableBudget $item) {
|
||||||
$spentOutsideBudgets = $this->spentOutsideBudgets;
|
|
||||||
$pcSpentInBudgets = $this->pcSpentInBudgets;
|
|
||||||
$pcSpentOutsideBudgets = $this->pcSpentOutsideBudgets;
|
|
||||||
$this->collection = $this->collection->map(function (AvailableBudget $item) use ($spentInsideBudgets, $spentOutsideBudgets, $pcSpentInBudgets, $pcSpentOutsideBudgets) {
|
|
||||||
$id = (int)$item->id;
|
$id = (int)$item->id;
|
||||||
|
$currencyId = $this->currencyIds[$id];
|
||||||
|
$currency = $this->currencies[$currencyId];
|
||||||
$meta = [
|
$meta = [
|
||||||
'spent_in_budgets' => $spentInsideBudgets[$id] ?? [],
|
'currency' => $currency,
|
||||||
'pc_spent_in_budgets' => $pcSpentInBudgets[$id] ?? [],
|
'spent_in_budgets' => $this->spentInsideBudgets[$id] ?? [],
|
||||||
|
'pc_spent_in_budgets' => $this->pcSpentInBudgets[$id] ?? [],
|
||||||
'spent_outside_budgets' => $spentOutsideBudgets[$id] ?? [],
|
'spent_outside_budgets' => $this->spentOutsideBudgets[$id] ?? [],
|
||||||
'pc_spent_outside_budgets' => $pcSpentOutsideBudgets[$id] ?? [],
|
'pc_spent_outside_budgets' => $this->pcSpentOutsideBudgets[$id] ?? [],
|
||||||
];
|
];
|
||||||
$item->meta = $meta;
|
$item->meta = $meta;
|
||||||
|
|
||||||
return $item;
|
return $item;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function collectCurrencies(): void
|
||||||
|
{
|
||||||
|
$ids = array_unique(array_values($this->currencyIds));
|
||||||
|
$set = TransactionCurrency::whereIn('id', $ids)->get();
|
||||||
|
foreach ($set as $currency) {
|
||||||
|
$this->currencies[(int)$currency->id] = $currency;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ class AvailableBudgetTransformer extends AbstractTransformer
|
|||||||
*/
|
*/
|
||||||
public function transform(AvailableBudget $availableBudget): array
|
public function transform(AvailableBudget $availableBudget): array
|
||||||
{
|
{
|
||||||
$currency = $availableBudget->transactionCurrency;
|
$currency = $availableBudget->meta['currency'];
|
||||||
$amount = Steam::bcround($availableBudget->amount, $currency->decimal_places);
|
$amount = Steam::bcround($availableBudget->amount, $currency->decimal_places);
|
||||||
$pcAmount = null;
|
$pcAmount = null;
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,14 @@
|
|||||||
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content'),
|
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content'),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
$.ajax({
|
||||||
|
url: 'api/v1/available-budgets?size=50&date=2025-08-06',
|
||||||
|
type: 'GET',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content'),
|
||||||
|
},
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user