<?php
require_once('functions.php');
require_once __DIR__ . '/fpd/fpdf.php';

 $dbc=mysqli_connect("localhost","juwakaco_231","@@!!jwaki#$%","juwakaco_pot");
if (!$dbc) {
    die('Error connecting to MySQL database: ' . mysqli_error($dbc));
}

$date1 = $_SESSION['date1'];
$date2 = $_SESSION['date2'];

// Fetch MPESA data
$r=mysqli_query($dbc,("SELECT * from sales where date between '$date1' and '$date2'
 
group by sale_id order by number
"));
// Create an FPDF instance
$pdf = new FPDF();

// Add a new page for MPESA data
$pdf->AddPage();

// Add MPESA heading
$pdf->SetFont('Arial', 'B', 24);
$pdf->Cell(0, 10, 'Sales Transactions ', 0, 1,1, 'C');

// Add MPESA table header
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(40, 10, 'Product', 1, 0, 'C');
$pdf->Cell(60, 10, 'Qty', 1, 0, 'C');
$pdf->Cell(60, 10, 'Price', 1, 1, 'C');
$pdf->Cell(60, 10, 'Total', 1, 1, 'C');
// Add MPESA table data
$pdf->SetFont('Arial', '', 12);
while ($row = mysqli_fetch_array($r)) {
    $pname = $row['p_name'];
    $qty = $row['qty'];
    $price = $row['price'];
    $tota=$row['total_p'];
	$total+=($row['total_p']);

    $pdf->Cell(40, 10, $pname, 1, 0);
    $pdf->Cell(60, 10, $qty, 1, 0, 'R');
    $pdf->Cell(60, 10, $price, 1, 1, 'C');
	 $pdf->Cell(60, 10, $tota, 1, 1, 'C');
}

// Calculate and display MPESA total
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(100, 10, 'Total:', 1, 0, 'R');
$pdf->Cell(60, 10, round($total), 1, 1, 'R');

// Add a new page for CASH data
//$pdf->AddPage();


// Output the PDF
$pdf->Output('transacations_report.pdf', 'D');
mysqli_close($dbc);
?>