<?php
declare(strict_types=1);
require __DIR__.'/app/bootstrap.php'; require_permission('reports.financial.view'); require __DIR__.'/app/layout.php';
$from=(string)($_GET['from']??date('Y-01-01')); $to=(string)($_GET['to']??date('Y-m-d')); $accountId=(int)($_GET['account_id']??0);
$accounts=db()->prepare('SELECT id,code,name_ar FROM accounts WHERE company_id=? ORDER BY code'); $accounts->execute([company_id()]); $accounts=$accounts->fetchAll();
$trial=db()->prepare("SELECT a.id,a.code,a.name_ar,COALESCE(SUM(CASE WHEN je.id IS NOT NULL THEN jl.debit_amount ELSE 0 END),0) debit,COALESCE(SUM(CASE WHEN je.id IS NOT NULL THEN jl.credit_amount ELSE 0 END),0) credit FROM accounts a LEFT JOIN journal_lines jl ON jl.account_id=a.id LEFT JOIN journal_entries je ON je.id=jl.journal_entry_id AND je.status='posted' AND je.entry_date BETWEEN ? AND ? WHERE a.company_id=? GROUP BY a.id HAVING debit<>0 OR credit<>0 ORDER BY a.code"); $trial->execute([$from,$to,company_id()]); $trial=$trial->fetchAll();
$ledger=[]; if($accountId){$s=db()->prepare("SELECT je.entry_no,je.entry_date,je.description,jl.debit_amount,jl.credit_amount FROM journal_lines jl JOIN journal_entries je ON je.id=jl.journal_entry_id WHERE jl.account_id=? AND je.company_id=? AND je.status='posted' AND je.entry_date BETWEEN ? AND ? ORDER BY je.entry_date,je.id,jl.id");$s->execute([$accountId,company_id(),$from,$to]);$ledger=$s->fetchAll();}
render_header('reports','التقارير المحاسبية','ميزان المراجعة ودفتر الأستاذ من القيود المرحلة فقط.'); ?>
<form class="panel form-card" method="get"><div class="form-grid"><label>من<input type="date" name="from" value="<?= e($from) ?>"></label><label>إلى<input type="date" name="to" value="<?= e($to) ?>"></label><label>حساب دفتر الأستاذ<select name="account_id"><option value="0">— بدون —</option><?php foreach($accounts as $a):?><option value="<?= (int)$a['id'] ?>" <?= $accountId===(int)$a['id']?'selected':'' ?>><?= e($a['code'].' — '.$a['name_ar']) ?></option><?php endforeach;?></select></label></div><button class="primary-btn">عرض التقرير</button></form>
<section class="panel data-card"><header><h2>ميزان المراجعة</h2></header><div class="responsive-table"><table><thead><tr><th>الحساب</th><th>إجمالي المدين</th><th>إجمالي الدائن</th><th>رصيد مدين</th><th>رصيد دائن</th></tr></thead><tbody><?php $td=0;$tc=0;foreach($trial as $r):$bal=(float)$r['debit']-(float)$r['credit'];$td+=(float)$r['debit'];$tc+=(float)$r['credit'];?><tr><td><?= e($r['code'].' — '.$r['name_ar']) ?></td><td><?= number_format((float)$r['debit'],2) ?></td><td><?= number_format((float)$r['credit'],2) ?></td><td><?= number_format(max($bal,0),2) ?></td><td><?= number_format(max(-$bal,0),2) ?></td></tr><?php endforeach;?></tbody><tfoot><tr><th>الإجمالي</th><th><?= number_format($td,2) ?></th><th><?= number_format($tc,2) ?></th><th colspan="2"><?= abs($td-$tc)<0.01?'متوازن':'غير متوازن' ?></th></tr></tfoot></table></div></section>
<?php if($accountId):?><section class="panel data-card"><header><h2>دفتر الأستاذ</h2></header><div class="responsive-table"><table><thead><tr><th>التاريخ</th><th>القيد</th><th>البيان</th><th>مدين</th><th>دائن</th><th>الرصيد</th></tr></thead><tbody><?php $balance=0;foreach($ledger as $r):$balance+=(float)$r['debit_amount']-(float)$r['credit_amount'];?><tr><td><?= e($r['entry_date']) ?></td><td><?= e($r['entry_no']) ?></td><td><?= e($r['description']) ?></td><td><?= number_format((float)$r['debit_amount'],2) ?></td><td><?= number_format((float)$r['credit_amount'],2) ?></td><td><?= number_format($balance,2) ?></td></tr><?php endforeach;?></tbody></table></div></section><?php endif; render_footer();
