<?php
include 'db_connect.php';
// Include the tcpdf library
require_once('tcpdf/tcpdf_main/tcpdf.php');
$agreement_id=intval($_GET['agreement_id']);
$month=$_GET['month'];
$start=$month.'-01';
$end=date('Y-m-t',strtotime($start));

$result=$conn->prepare("SELECT * FROM Financial_Contributions
WHERE agreement_id=? AND contribution_date BETWEEN ? AND ?");
$result->bind_param("iss",$agreement_id,$start,$end);
$result->execute();
$data=$result->get_result();

$pdf=new TCPDF();
$pdf->AddPage();
$pdf->SetFont('helvetica','',10);

$html="<h2>Monthly Contribution Report</h2>
<p><strong>Month:</strong> $month</p>
<table border='1' cellpadding='5'>
<tr>
<th>Date</th><th>Amount</th><th>Method</th><th>Reference</th><th>Remarks</th>
</tr>";

$total=0;
while($r=$data->fetch_assoc()){
$total+=$r['amount'];
$html.="<tr>
<td>{$r['contribution_date']}</td>
<td>".number_format($r['amount'],2)." {$r['currency']}</td>
<td>{$r['payment_method']}</td>
<td>{$r['reference_no']}</td>
<td>{$r['remarks']}</td>
</tr>";
}

$html.="<tr>
<td colspan='4'><strong>Total</strong></td>
<td><strong>".number_format($total,2)."</strong></td>
</tr>
</table>";

$pdf->writeHTML($html);
$pdf->Output("Monthly_Contribution_$month.pdf","I");
