Skip to content

Commit

Permalink
feat' add ledger to statements
Browse files Browse the repository at this point in the history
  • Loading branch information
jesusantguerrero committed Jan 29, 2023
1 parent fb0e835 commit a5eb87d
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/Http/Controllers/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,25 @@ public function statements(Request $request, string $category = "income") {


$accountIds = explode(",", $accountIds[0]);
$balance = DB::table('transaction_lines')

$balanceByAccounts = DB::table('transaction_lines')
->whereIn('transaction_lines.account_id', $accountIds)
->selectRaw('sum(amount * transaction_lines.type) as total, transaction_lines.account_id, accounts.id, accounts.name, accounts.display_id')
->selectRaw("
sum(amount * transaction_lines.type) as total,
transaction_lines.account_id,
accounts.id,
accounts.name,
accounts.display_id,
categories.display_id category,
g.display_id ledger,
g.id ledger_id
")
->join('accounts', 'accounts.id', '=', 'transaction_lines.account_id')
->groupBy('transaction_lines.account_id')
->get()->toArray();
->join('categories', 'accounts.category_id', 'categories.id')
->join(DB::raw('categories g'), 'categories.parent_id', 'g.id')
->groupBy('transaction_lines.account_id');

// all the accounts with balance are here


$categoryAccounts = Category::where([
Expand All @@ -229,7 +242,8 @@ public function statements(Request $request, string $category = "income") {
'category'
])->get()->toArray();

$categoryAccounts = array_map(function ($subCategory) use($balance) {
$balance = $balanceByAccounts->get()->toArray();
$categoryAccounts = array_map(function ($subCategory) use ($balance) {
$total = [];
if (isset($subCategory['accounts'])) {
foreach ($subCategory['accounts'] as $accountIndex => $account) {
Expand All @@ -245,9 +259,16 @@ public function statements(Request $request, string $category = "income") {
}, $categoryAccounts);


$ledger = DB::table('categories')
->whereIn('categories.display_id', $categories[$category])
->selectRaw('sum(COALESCE(total, 0)) total, categories.alias, categories.display_id, categories.name')
->leftJoinSub($balanceByAccounts, 'balance', function($join) {
$join->on('categories.id', 'balance.ledger_id');
})->groupBy('categories.id')->get();

return Jetstream::inertia()->render($request, config('journal.statements_inertia_path') . '/Category', [
"categories" => $categoryAccounts,
"ledger" => $ledger->groupBy('display_id'),
'categoryType' => $category
]);
}
Expand Down

0 comments on commit a5eb87d

Please sign in to comment.