Skip to content

Commit

Permalink
feat(frozen period): show exchange rates in show route
Browse files Browse the repository at this point in the history
  • Loading branch information
KennethTrecy committed Sep 20, 2023
1 parent fd77459 commit 0538051
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 16 deletions.
69 changes: 54 additions & 15 deletions app/Controllers/FrozenPeriodController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use CodeIgniter\I18n\Time;
use CodeIgniter\Validation\Validation;

use App\Casts\ModifierAction;
use App\Contracts\OwnedResource;
use App\Entities\SummaryCalculation;
use App\Exceptions\UnprocessableRequest;
Expand Down Expand Up @@ -72,9 +73,41 @@ protected static function enrichResponseDocument(array $initial_document): array
$currencies = static::getRelatedCurrencies($accounts);
$enriched_document["currencies"] = $currencies;

// TODO: Show exchange rates here too
$exchange_modifiers = model(ModifierModel::class)
->where("action", ModifierAction::set(EXCHANGE_MODIFIER_ACTION))
->findAll();
$financial_entries = count($exchange_modifiers) > 0
? model(FinancialEntryModel::class)
->where(
"transacted_at >=",
$enriched_document[static::getIndividualName()]->started_at
)
->where(
"transacted_at <=",
$enriched_document[static::getIndividualName()]->finished_at
)
->whereIn("modifier_id", array_map(
function ($modifier) {
return $modifier->id;
},
$exchange_modifiers
))
->findAll()
: [];
$grouped_financial_entries = count($financial_entries) > 0
? static::groupFinancialEntriesByModifier($financial_entries)
: [];

$raw_exchange_rates = count($grouped_financial_entries) > 0
? static::makeExchangeRates(
$exchange_modifiers,
$accounts,
$grouped_financial_entries
) : [];

$enriched_document["@meta"] = [
"statements" => static::makeStatements($currencies, $accounts, $summary_calculations)
"statements" => static::makeStatements($currencies, $accounts, $summary_calculations),
"exchange_rates" => $raw_exchange_rates
];

return $enriched_document;
Expand Down Expand Up @@ -321,19 +354,7 @@ function ($previous_time, $current_entry) {
->findAll();
}

$grouped_financial_entries = array_reduce(
$financial_entries,
function ($groups, $entry) {
if (!isset($groups[$entry->modifier_id])) {
$groups[$entry->modifier_id] = [];
}

array_push($groups[$entry->modifier_id], $entry);

return $groups;
},
[]
);
$grouped_financial_entries = static::groupFinancialEntriesByModifier($financial_entries);

$raw_summary_calculations = array_reduce(
$modifiers,
Expand Down Expand Up @@ -737,6 +758,24 @@ private static function getRelatedCurrencies(array $accounts): array {
return $currencies;
}

private static function groupFinancialEntriesByModifier(array $financial_entries): array {
$grouped_financial_entries = array_reduce(
$financial_entries,
function ($groups, $entry) {
if (!isset($groups[$entry->modifier_id])) {
$groups[$entry->modifier_id] = [];
}

array_push($groups[$entry->modifier_id], $entry);

return $groups;
},
[]
);

return $grouped_financial_entries;
}

private static function keySummaryCalculationsWithAccounts(array $summary_calculations): array {
$keyed_calculations = array_reduce(
$summary_calculations,
Expand Down
3 changes: 2 additions & 1 deletion t/Feature/Resource/FrozenPeriodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ public function testDefaultShow()
->minus($closed_financial_entry->debit_amount)
]
]
]
],
"exchange_rates" => []
],
"accounts" => json_decode(json_encode([
$equity_account,
Expand Down

0 comments on commit 0538051

Please sign in to comment.