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

 $dbc=mysqli_connect("localhost","root","","point_of_sale");
if (!$dbc) {
    die('Error connecting to MySQL database: ' . mysqli_error($dbc));
}

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

// Fetch MPESA data
$j = mysqli_query($dbc, ("select amount,type,receipt,time from x_report where date between '$date1' and '$date2'
and type='MPESA' and refe='SALES' and amount!='0'
"));

// 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, 'MPESA Transactions', 0, 1, 'C');

// Add MPESA table header
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(40, 10, 'Receipt NO', 1, 0, 'C');
$pdf->Cell(60, 10, 'Amount', 1, 0, 'C');
$pdf->Cell(60, 10, 'Time', 1, 1, 'C');

// Add MPESA table data
$pdf->SetFont('Arial', '', 12);
while ($row = mysqli_fetch_array($j)) {
    $receipt = $row['receipt'];
    $amount = $row['amount'];
    $time = $row['time'];
    $amoun+=($row['amount']);

    $pdf->Cell(40, 10, $receipt, 1, 0);
    $pdf->Cell(60, 10, $amount, 1, 0, 'R');
    $pdf->Cell(60, 10, $time, 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($amoun), 1, 1, 'R');

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

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

// Add CASH table header
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(40, 10, 'Receipt NO', 1, 0, 'C');
$pdf->Cell(60, 10, 'Amount', 1, 0, 'C');
$pdf->Cell(60, 10, 'Time', 1, 1, 'C');

// Fetch CASH data
$jww = mysqli_query($dbc, ("select  amount,type,receipt,time from x_report  where date between '$date1' and '$date2'
 and type='BANK' and refe='SALES' and amount!='0'
"));

// Add CASH table data
$pdf->SetFont('Arial', '', 12);
while ($rowss = mysqli_fetch_array($jww)) {
    $receip = $rowss['receipt'];
    $amoun = $rowss['amount'];
    $time = $rowss['time'];
$bank+=($rowss['amount']);
    $pdf->Cell(40, 10, $receip, 1, 0);
    $pdf->Cell(60, 10, $amoun, 1, 0, 'R');
    $pdf->Cell(60, 10, $time, 1, 1, 'C');
}

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


//$pdf->AddPage();

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

// Add CASH table header
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(40, 10, 'Receipt NO', 1, 0, 'C');
$pdf->Cell(60, 10, 'Amount', 1, 0, 'C');
$pdf->Cell(60, 10, 'Time', 1, 1, 'C');

// Fetch CASH data
$jw = mysqli_query($dbc, ("select amount,type,receipt,time from x_report where date between '$date1' and '$date2'
and type='CASH' and refe='SALES' and amount!='0'
"));

// Add CASH table data
$pdf->SetFont('Arial', '', 12);
while ($rows = mysqli_fetch_array($jw)) {
    $receip = $rows['receipt'];
    $amoun = $rows['amount'];
    $time = $rows['time'];
$cash+=($rows['amount']);
    $pdf->Cell(40, 10, $receip, 1, 0);
    $pdf->Cell(60, 10, $amoun, 1, 0, 'R');
    $pdf->Cell(60, 10, $time, 1, 1, 'C');
}

// Calculate and display CASH total
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(100, 10, 'Total:', 1, 0, 'R');
$pdf->Cell(60, 10, round($cash), 1, 1, 'R');
// Output the PDF
$pdf->Output('transacations_report.pdf', 'D');
mysqli_close($dbc);
?>