Skip to content

Commit

Permalink
feat(frozen period): make account statements per currency
Browse files Browse the repository at this point in the history
  • Loading branch information
KennethTrecy committed Aug 26, 2023
1 parent b942989 commit c805547
Showing 1 changed file with 131 additions and 0 deletions.
131 changes: 131 additions & 0 deletions app/Controllers/FrozenPeriodController.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,137 @@ protected static function enrichResponseDocument(array $initial_document): array
}
$enriched_document["currencies"] = $currencies;

$keyed_summaries = array_reduce(
$summary_calculations,
function ($keyed_collection, $summary) {
$keyed_collection[$summary->account_id] = $summary;

return $keyed_collection;
},
[]
);
$grouped_summaries = array_reduce(
$accounts,
function ($groups, $account) use ($keyed_summaries) {
if (!isset($groups[$account->currency_id])) {
$groups[$account->currency_id] = array_fill_keys(
[ ...ACCEPTABLE_ACCOUNT_KINDS ],
[]
);
}

array_push(
$groups[$account->currency_id][$account->kind],
$keyed_summaries[$account->id]
);

return $groups;
},
[]
);
$statements = array_reduce(
$currencies,
function ($statements, $currency) use ($grouped_summaries) {
$summaries = $grouped_summaries[$currency->id];

$unadjusted_total_income = array_reduce(
$summaries[INCOME_ACCOUNT_KIND],
function ($previous_total, $summary) {
return $previous_total
->plus($summary["unadjusted_credit_amount"])
->minus($summary["unadjusted_debit_amount"]);
},
BigRational::zero()
);
$unadjusted_total_expenses = array_reduce(
$summaries[EXPENSE_ACCOUNT_KIND],
function ($previous_total, $summary) {
return $previous_total
->plus($summary["unadjusted_debit_amount"])
->minus($summary["unadjusted_credit_amount"]);
},
BigRational::zero()
);

$unadjusted_total_assets = array_reduce(
$summaries[ASSET_ACCOUNT_KIND],
function ($previous_total, $summary) {
return $previous_total
->plus($summary["unadjusted_debit_amount"])
->minus($summary["unadjusted_credit_amount"]);
},
BigRational::zero()
);
$unadjusted_total_liabilities = array_reduce(
$summaries[LIABILITY_ACCOUNT_KIND],
function ($previous_total, $summary) {
return $previous_total
->plus($summary["unadjusted_credit_amount"])
->minus($summary["unadjusted_debit_amount"]);
},
BigRational::zero()
);
$unadjusted_total_equities = array_reduce(
$summaries[EQUITY_ACCOUNT_KIND],
function ($previous_total, $summary) {
return $previous_total
->plus($summary["unadjusted_credit_amount"])
->minus($summary["unadjusted_debit_amount"]);
},
BigRational::zero()
);

$adjusted_total_expenses = array_reduce(
$summaries[EXPENSE_ACCOUNT_KIND],
function ($previous_total, $summary) {
return $previous_total
->plus($summary["adjusted_debit_amount"])
->minus($summary["adjusted_credit_amount"]);
},
BigRational::zero()
);
$adjusted_total_assets = array_reduce(
$summaries[ASSET_ACCOUNT_KIND],
function ($previous_total, $summary) {
return $previous_total
->plus($summary["adjusted_debit_amount"])
->minus($summary["adjusted_credit_amount"]);
},
BigRational::zero()
);

$unadjusted_trial_balance_total = $unadjusted_total_expenses
->plus($unadjusted_total_assets);
$income_statement_total = $unadjusted_total_income
->minus($unadjusted_total_expenses);
$adjusted_trial_balance_total = $adjusted_total_income
->plus($adjusted_total_expenses);

$statements[$currency->id] = [
"currency_id" => $currency->id,
"unadjusted_trial_balance" => [
"total" => $unadjusted_trial_balance_total
],
"income_statement" => [
"total" => $income_statement_total
],
"balance_sheet" => [
"total_assets" => $unadjusted_total_assets,
"total_liabilities" => $unadjusted_total_liabilities,
"total_equities" => $unadjusted_total_equities
->plus($income_statement_total)
],
"adjusted_trial_balance" => [
"total" => $adjusted_trial_balance_total
]
];

return $statements;
},
[]
);
$enriched_document["@meta"] = [ "statements" => $statements ];

return $enriched_document;
}

Expand Down

0 comments on commit c805547

Please sign in to comment.