<?php
include("db.php");

require("./admin-module/library/fpdf.php");

class PDF extends FPDF
{
/* Page header */
function Header()
{

    $this->SetFont('Arial','B',30);
    /* Move to the right */
    $this->Cell(105);
    $connect = traffic_db_connect();
    $start = $_POST['start'];
    $end = $_POST['end'];
    $result = mysqli_query($connect, "SELECT * FROM trips WHERE car='$_POST[car]' AND start_mileage BETWEEN '$start' and '$end' ORDER BY id DESC ");
    $get = mysqli_fetch_array($result);
    $this->Cell(50,20, '',0,0,'C');
    $this->Cell(90,20, $get['car'],0,0,'C');

    $this->Ln(20);
}
/* Page footer */
function Footer()
{
    /* Position at 1.5 cm from bottom */
    $this->SetY(-15);
    /* Arial italic 8 */
    $this->SetFont('Arial','I',8);
    /* Page number */
    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}

/* Instanciation of inherited class */
$pdf = new PDF('L', 'mm', array(500,300));
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','B',13);


$pdf->cell(30, 10, "Date", 1, 0, 'C');
$pdf->cell(40, 10, "Driver", 1, 0, 'C');
$pdf->cell(30, 10, "Client", 1, 0, 'C');
$pdf->cell(90, 10, "Description", 1, 0, 'C');
$pdf->cell(23, 10, "Start-Time", 1, 0, 'C');
$pdf->cell(23, 10, "End-Time", 1, 0, 'C');
$pdf->cell(24, 10, "Total-Time", 1, 0, 'C');
$pdf->cell(30, 10, "Start Mileage", 1, 0, 'C');
$pdf->cell(30, 10, "End Mileage", 1, 0, 'C');
$pdf->cell(25, 10, "Fueling", 1, 0, 'C');
$pdf->cell(25, 10, "Mileage", 1, 0, 'C');
$pdf->cell(25, 10, "Amount $", 1, 0, 'C');
$pdf->cell(25, 10, "Literage", 1, 0, 'C');
$pdf->cell(25, 10, "Cost/trip $", 1, 0, 'C');
$pdf->cell(30, 10, "Expense A/c", 1, 1, 'C');

$pdf->SetFont('Arial', '', 11);
$connect = traffic_db_connect();
$start = $_POST['start'];
$end = $_POST['end'];
$result = mysqli_query($connect, "SELECT * FROM trips WHERE car='$_POST[car]'  AND start_mileage BETWEEN '$start' and '$end' ORDER BY start_mileage ASC  ");
$result2 = mysqli_query($connect, "SELECT * FROM trips WHERE car='$_POST[car]'  AND start_mileage BETWEEN '$start' and '$end' ORDER BY start_mileage ASC  ");
$distance = array();
while($row = mysqli_fetch_array($result)){
  $distance[] = $row['end_mileage'] - $row['start_mileage'];
}
$cost = $row['amount'];
while($row = mysqli_fetch_array($result2)){
  $mileage = $row['end_mileage'] - $row['start_mileage'];
$pdf ->SetTextColor(0,0,0);
$pdf ->SetFont("Arial","",12);
$pdf->cell(30,10, $row['date'], 1, 0, 'C');
$pdf->cell(40,10, $row['username'], 1, 0, 'C');
$pdf->cell(30,10, $row['client'], 1, 0, 'C');
$pdf->cell(90,10, $row['description'], 1, 0, 'C');
$pdf->cell(23,10, $row['start_time'], 1, 0, 'C');
$pdf->cell(23,10, $row['end_time'], 1, 0, 'C');
ob_start();
$dt = (strtotime($row['end_time']) - strtotime($row['start_time']))/60 + (strtotime($row['end_time2']) - strtotime($row['start_time2']))/60;
echo sprintf("%d : %02d ", floor($dt/60), $dt%60);
$time = ob_get_clean();
 $pdf->cell(24,10,$time ,1,0,'C');
$pdf->cell(30,10, $row['start_mileage'], 1, 0, 'C');
$pdf->cell(30,10, $row['end_mileage'], 1, 0, 'C');
$pdf->cell(25,10, $row['station_mileage'], 1, 0, 'C');
$pdf -> SetTextColor(255,0,0);
$pdf -> SetFont("Arial","B",12);
$pdf->cell(25,10, $mileage , 1, 0, 'C');
$total_cost = mysqli_query($connect, "SELECT DISTINCT amount from trips WHERE car='$_POST[car]' AND start_mileage BETWEEN '$start' and '$end' AND amount > 0");
$get = mysqli_fetch_array($total_cost);
$pdf->cell(25,10, $row['amount'], 1, 0, 'C');
$cost = $get['amount'];
$cost_per_trip = ($cost*($row['end_mileage'] - $row['start_mileage'])/(array_sum($distance)));
ob_start();
echo number_format((float)$cost_per_trip,2,'.','');
$costs = ob_get_clean();

$pdf->cell(25,10, $row['litres'], 1, 0, 'C');
$pdf->cell(25,10, $costs, 1, 0, 'C');
$pdf->SetFont("Arial", "",8);
$pdf->cell(30,10, $row['expense_account'], 1, 1, 'C');
}
$pdf ->SetTextColor(0,0,0);
$pdf -> SetFont("Arial","B",20);
$pdf->cell(23,10, '', 0, 0, 'C');
$pdf->cell(23,10, '', 0, 0, 'C');
$pdf->cell(23,10, '', 0, 0, 'C');
$pdf->cell(23,10, '', 0, 0, 'C');
$pdf->cell(30,10, '', 0, 0, 'C');
$pdf->cell(30,10, '', 0, 0, 'C');
$pdf->cell(90,10, '', 0, 0, 'C');
$pdf->cell(100,10, '', 0, 0, 'C');
ob_start();
print_r(array_sum($distance));
$tot = ob_get_clean();
$pdf->cell(33,10, $tot, 0, 0, 'C');
$pdf->cell(30,10, '', 0, 0, 'C');
$pdf->cell(13,10, '', 0, 0, 'C');
ob_start();
$result3 = mysqli_query($connect, "SELECT DISTINCT litres FROM trips WHERE car='$_POST[car]'  AND start_mileage BETWEEN '$start' and '$end' ORDER BY start_mileage ASC");
while($row2 = mysqli_fetch_array($result3)){
  $literage = $row2['litres'];
$get2 = array_sum($distance)/$row2['litres'];
$total_l = number_format($get2,2,'.','');
echo $total_l;
  $litres = ob_get_clean();

}


ob_start();
$result3 = mysqli_query($connect, "SELECT * FROM trips WHERE car='$_POST[car]'  AND start_mileage BETWEEN '$start' and '$end' ORDER BY start_mileage ASC  ");
while($tot = mysqli_fetch_array($result3)){
$tot_cost[] = ($tot['end_mileage'] - $tot['start_mileage'])/(array_sum($distance))*$cost;
}
$sum_ = array_sum($tot_cost);
$tott = number_format($sum_,2,'.','');
echo $tott;
$totcost2 = ob_get_clean();
$pdf->cell(30,10, $totcost2, 0, 0, 'C');

$pdf->Output();
?>
