@php
/*
|----------------------------------------------------------------------
| TALLY-STYLE BALANCE SHEET
| Left = Liabilities + Capital
| Right = Assets
|----------------------------------------------------------------------
*/
// Split top-level natures into two sides
$leftSide = []; // Liabilities, Capital, Equity, etc.
$rightSide = []; // Assets
$leftTotal = 0;
$rightTotal = 0;
foreach ($group as $key => $nature) {
$natureName = strtolower($nature['group_nature'] ?? '');
if (str_contains($natureName, 'asset')) {
$rightSide[$key] = $nature;
$rightTotal += $nature['amount'] ?? 0;
} else {
$leftSide[$key] = $nature;
$leftTotal += $nature['amount'] ?? 0;
}
}
// 🔴 Move Equity nature to the bottom of the Liabilities side
$equityNature = null;
foreach ($leftSide as $key => $nature) {
if (strtolower($nature['group_nature'] ?? '') === 'equity') {
$equityNature = $nature;
unset($leftSide[$key]);
break;
}
}
if ($equityNature) {
$leftSide[] = $equityNature;
}
// Mandatory groups that should ALWAYS show
$mandatoryGroups = [
'Fixed Assets', 'Investment', 'Investments', 'Current Assets',
'Capital Accounts', 'Loan Accounts', 'Current Liability', 'Current Liabilities'
];
// Helper: render one side recursively, returns HTML string
function renderSide(array $natures, string $fromDate, string $toDate, array $mandatoryGroups): string
{
$ledgerBase = route('account-ledger-report.index');
$html = '';
static $grpId = 0;
foreach ($natures as $L1 => $nature) {
if (!is_array($nature) || !isset($nature['group_nature'])) continue;
$hasMandatory = false;
foreach ($nature as $L2 => $grp) {
if (is_array($grp) && isset($grp['group_name']) && in_array($grp['group_name'], $mandatoryGroups)) {
$hasMandatory = true;
break;
}
}
if (round(abs($nature['amount'] ?? 0), 2) == 0 && !$hasMandatory) continue;
if (strtolower($nature['group_nature'] ?? '') !== 'equity') {
$html .= '
';
$html .= '| '
. e($nature['group_nature'])
. ' | ';
$html .= '
';
}
foreach ($nature as $L2 => $grp) {
if (!is_array($grp) || !isset($grp['group_name'])) continue;
$isMandatory = in_array($grp['group_name'], $mandatoryGroups);
if (round(abs($grp['amount'] ?? 0), 2) == 0 && !$isMandatory) continue;
$nameToShow = e($grp['group_name']);
if ($grp['group_name'] === 'Current Year Profit') {
$plLink = route('profit-and-loss-report.index')
. '?from_date=' . urlencode($fromDate)
. '&to_date=' . urlencode($toDate);
$nameToShow = ''
. $nameToShow . '';
}
$html .= '';
$html .= '| '
. $nameToShow . ' | ';
$html .= ''
. number_format($grp['amount'], 2) . ' | ';
$html .= '
';
foreach ($grp as $L3 => $aGrp) {
if (!is_array($aGrp) || !isset($aGrp['a_group_name'])) continue;
if (round(abs($aGrp['amount'] ?? 0), 2) == 0) continue;
$ledgers = array_filter($aGrp, fn($v) =>
is_array($v) && isset($v['ledger_account']) && round(abs($v['amount'] ?? 0), 2) != 0
);
$grpId++;
$hasChildren = count($ledgers) > 0;
$chevron = $hasChildren ? '' : '';
// Default to collapsed if has children
$rowClass = $hasChildren ? 'bs-toggle bs-collapsed' : '';
$html .= '';
$html .= '| ' . $chevron . e($aGrp['a_group_name']) . ' | ';
$html .= ''
. number_format($aGrp['amount'], 2) . ' | ';
$html .= '
';
foreach ($ledgers as $ledger) {
$ledId = $ledger['ledger_id'] ?? null;
$ledName = $ledger['ledger_account'];
$ledAmt = $ledger['amount'];
$link = ($ledId === 'CLOSING_STOCK')
? route('stock-valuation.index') . '?as_of_date=' . urlencode($toDate)
: ($ledId
? $ledgerBase . '?ledger_id=' . $ledId
. '&from_date=' . urlencode($fromDate)
. '&to_date=' . urlencode($toDate)
: '#');
// Hidden by default
$html .= '';
$html .= '| '
. ''
. e($ledName) . ' | ';
$html .= ''
. number_format($ledAmt, 2) . ' | ';
$html .= '
';
}
}
}
}
return $html;
}
@endphp
Balance Sheet
As on {{ date('d-M-Y', strtotime($toDate)) }}
{{-- ============ LEFT SIDE: Liabilities + Capital ============ --}}
{!! renderSide($leftSide, $fromDate, $toDate, $mandatoryGroups) !!}
| Total Liabilities & Capital |
{{ number_format($leftTotal, 2) }} |
{{-- ============ RIGHT SIDE: Assets ============ --}}
{!! renderSide($rightSide, $fromDate, $toDate, $mandatoryGroups) !!}
| Total Assets |
{{ number_format($rightTotal, 2) }} |
@if(round(abs($leftTotal - $rightTotal), 2) > 0)
⚠️ Balance Sheet is out of balance by
{{ number_format(abs($leftTotal - $rightTotal), 2) }}
@endif