<?php
declare(strict_types=1);
require __DIR__.'/app/bootstrap.php';require_permission('reports.financial.view');require __DIR__.'/app/layout.php';
$companyId=company_id();$from=(string)($_GET['from']??date('Y-01-01'));$to=(string)($_GET['to']??date('Y-m-d'));
$income=db()->prepare("SELECT at.code,at.name_ar,COALESCE(SUM(CASE WHEN je.id IS NULL THEN 0 WHEN at.normal_balance='credit' THEN jl.credit_amount-jl.debit_amount ELSE jl.debit_amount-jl.credit_amount END),0) amount FROM account_types at LEFT JOIN accounts a ON a.account_type_id=at.id AND a.company_id=? 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 at.statement_group='income_statement' GROUP BY at.id ORDER BY at.sort_order");$income->execute([$companyId,$from,$to]);$income=$income->fetchAll();
$balance=db()->prepare("SELECT at.code,at.name_ar,at.normal_balance,COALESCE(SUM(CASE WHEN je.id IS NOT NULL THEN (CASE WHEN at.normal_balance='debit' THEN jl.debit_amount-jl.credit_amount ELSE jl.credit_amount-jl.debit_amount END) ELSE 0 END),0) amount FROM account_types at LEFT JOIN accounts a ON a.account_type_id=at.id AND a.company_id=? 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<=? WHERE at.statement_group='balance_sheet' GROUP BY at.id ORDER BY at.sort_order");$balance->execute([$companyId,$to]);$balance=$balance->fetchAll();
$receipts=(float)scalar("SELECT COALESCE(SUM(amount),0) FROM receipt_vouchers WHERE company_id=? AND status='posted' AND receipt_date BETWEEN ? AND ?",[$companyId,$from,$to]);$payments=(float)scalar("SELECT COALESCE(SUM(amount),0) FROM payment_vouchers WHERE company_id=? AND status='posted' AND payment_date BETWEEN ? AND ?",[$companyId,$from,$to]);
$revenues=0;$expenses=0;foreach($income as $r){if(in_array($r['code'],['REVENUE','OTHER_INCOME'],true))$revenues+=(float)$r['amount'];else$expenses+=(float)$r['amount'];}$net=$revenues-$expenses;
render_header('statements','القوائم المالية','قائمة الدخل والمركز المالي والتدفق النقدي من القيود المرحلة.');?>
<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></div><button class="primary-btn">تحديث</button> <a class="secondary-btn" href="<?= e(app_url('accounting_setup.php')) ?>">الربط المحاسبي</a></form>
<div class="settings-grid"><section class="panel data-card"><header><h2>قائمة الدخل</h2></header><table><tbody><?php foreach($income as $r):?><tr><td><?= e($r['name_ar']) ?></td><td><?= number_format((float)$r['amount'],2) ?></td></tr><?php endforeach;?></tbody><tfoot><tr><th>صافي الربح (الخسارة)</th><th><?= number_format($net,2) ?></th></tr></tfoot></table></section><section class="panel data-card"><header><h2>المركز المالي</h2></header><table><tbody><?php foreach($balance as $r):?><tr><td><?= e($r['name_ar']) ?></td><td><?= number_format((float)$r['amount'],2) ?></td></tr><?php endforeach;?></tbody></table></section></div>
<section class="panel data-card"><header><h2>التدفق النقدي المباشر</h2></header><div class="detail-grid"><div><span>المقبوضات</span><strong><?= number_format($receipts,2) ?></strong></div><div><span>المدفوعات</span><strong><?= number_format($payments,2) ?></strong></div><div><span>صافي التدفق</span><strong><?= number_format($receipts-$payments,2) ?></strong></div></div></section>
<?php render_footer();
