<?php 
error_reporting(0);

require_once '../assets/plugins/tcpdf/tcpdf.php'; 
$pdf = new PDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetMargins(10, 2, 7);
include("../config.php");

$trip_id = $_GET['p1']; // Assuming you pass trip_id as a parameter

$trip_data = mysqli_query($conn,"SELECT * FROM `trip` WHERE id = '".$trip_id."'");
$trip_id_data = mysqli_fetch_assoc($trip_data);

//print_r($trip_id_data);

$trip_data_driver = mysqli_query($conn,"select * FROM `driver_mst` WHERE id = '".$trip_id_data['driver_id']."'");
$trip_id_data_driver = mysqli_fetch_assoc($trip_data_driver);

$cb = "SELECT 
SUM(hamali) AS total_hamali,
SUM(loading_unloading) AS total_loading_unloading,
SUM(cash_driver) AS total_cash_driver,
SUM(bank_driver) AS total_bank_driver,
SUM(toll) AS total_toll,
SUM(kata) AS total_kata,
SUM(border) AS total_border,
SUM(battha) AS total_battha,
SUM(salary) AS total_salary,
SUM(diesel) AS total_diesel,
SUM(diesel_rate) AS total_diesel_rate,
SUM(diesel_amount) AS total_diesel_amount,
SUM(maintance) AS total_maintanance,
SUM(tyre) AS total_tyre,
SUM(electric) AS total_electric,
SUM(mechanic) AS total_mechanic,
SUM(police_rto) AS total_police_rto,
SUM(other) AS total_other,
SUM(extra_height) AS total_extra_height,
SUM(extra_weight) AS total_extra_weight,
`date`
FROM trip_detail
WHERE master_id = '".$trip_id."'
GROUP BY `date`";
$cb_res     = mysqli_query($conn,$cb);
$num        = mysqli_num_rows($cb_res);
$trip_data_fetch  = array();
while ($cb_row = mysqli_fetch_assoc($cb_res)) { 
$trip_data_fetch[] = $cb_row;
}

if($trip_id_data['recharge_date'] == '1970-01-01' || $trip_id_data['recharge_date'] == '0000-00-00'){
$recharge_date = '';
}else{
$recharge_date = risFormatDate($trip_id_data['recharge_date']);
}
if( $trip_id_data['total_freight_1'] == '0.00'){
$trip_freight1 = '';
}else{
$trip_freight1 = $trip_id_data['total_freight_1'];
}

if( $trip_id_data['total_freight_2'] == '0.00'){
$trip_freight2 = '';
}else{
$trip_freight2 = $trip_id_data['total_freight_2'];
}

if( $trip_id_data['total_freight_3'] == '0.00'){
$trip_freight3 = '';
}else{
$trip_freight3 = $trip_id_data['total_freight_3'];
}

if( $trip_id_data['total_freight_4'] == '0.00'){
$trip_freight4 = '';
}else{
$trip_freight4 = $trip_id_data['total_freight_4'];
}

if( $trip_id_data['total_freight_5'] == '0.00'){
$trip_freight5 = '';
}else{
$trip_freight5 = $trip_id_data['total_freight_5'];
}

if( $trip_id_data['total_freight_6'] == '0.00'){
$trip_freight6 = '';
}else{
$trip_freight6 = $trip_id_data['total_freight_6'];
}

if( $trip_id_data['advance_1'] == '0.00'){
$advance1 = '';
}else{
$advance1 = $trip_id_data['advance_1'];
}

if( $trip_id_data['advance_2'] == '0.00'){
$advance2 = '';
}else{
$advance2 = $trip_id_data['advance_2'];
}

if( $trip_id_data['advance_3'] == '0.00'){
$advance3 = '';
}else{
$advance3 = $trip_id_data['advance_3'];
}

if( $trip_id_data['advance_4'] == '0.00'){
$advance4 = '';
}else{
$advance4 = $trip_id_data['advance_4'];
}

if( $trip_id_data['advance_5'] == '0.00'){
$advance5 = '';
}else{
$advance5 = $trip_id_data['advance_5'];
}

if( $trip_id_data['advance_6'] == '0.00'){
$advance6 = '';
}else{
$advance6 = $trip_id_data['advance_6'];
}

if($trip_id_data['date_1'] == '1970-01-01' || $trip_id_data['date_1'] == '0000-00-00'){
$trip_date1 = '';
}else{
$trip_date1 = risFormatDate($trip_id_data['date_1']);
}

if($trip_id_data['date_2'] == '1970-01-01' || $trip_id_data['date_2'] == '0000-00-00'){
$trip_date2 = '';
}else{
$trip_date2 = risFormatDate($trip_id_data['date_2']);
}

if($trip_id_data['date_3'] == '1970-01-01' || $trip_id_data['date_3'] == '0000-00-00'){
$trip_date3 = '';
}else{
$trip_date3 = risFormatDate($trip_id_data['date_3']);
}

if($trip_id_data['date_4'] == '1970-01-01' || $trip_id_data['date_4'] == '0000-00-00'){
$trip_date4 = '';
}else{
$trip_date4 = risFormatDate($trip_id_data['date_4']);
}  

if($trip_id_data['date_5'] == '1970-01-01' || $trip_id_data['date_5'] == '0000-00-00'){
$trip_date5 = '';
}else{
$trip_date5 = risFormatDate($trip_id_data['date_5']);
}

if($trip_id_data['date_6'] == '1970-01-01' || $trip_id_data['date_6'] == '0000-00-00'){
$trip_date6 = '';
}else{
$trip_date6 = risFormatDate($trip_id_data['date_6']);
}

if($trip_id_data['from_place_1'] == '' ){
$trip_date1 = '';
}else{
$trip_date1 = risFormatDate($trip_id_data['date_1']);
}


if($trip_id_data['from_place_2'] == '' ){
$trip_date2 = '';
}else{
$trip_date2 = risFormatDate($trip_id_data['date_2']);
}
if($trip_id_data['from_place_3'] == '' ){
$trip_date3 = '';
}else{
$trip_date3 = risFormatDate($trip_id_data['date_3']);
}
if($trip_id_data['from_place_4'] == '' ){
$trip_date4 = '';
}else{
$trip_date4 = risFormatDate($trip_id_data['date_4']);
}
if($trip_id_data['from_place_5'] == '' ){
$trip_date5 = '';
}else{
$trip_date5 = risFormatDate($trip_id_data['date_5']);
}

if($trip_id_data['from_place_6'] == '' ){
$trip_date6 = '';
}else{
$trip_date6 = risFormatDate($trip_id_data['date_6']);
}

if($trip_id_data['single_start_km'] == '' ){
$trip_start = '';
}else{
$trip_start = $trip_id_data['single_start_km'];
}

if($trip_id_data['single_start_km2'] == '' ){
$trip_start2 = '';
}else{
$trip_start2 = $trip_id_data['single_start_km2'];
}

if($trip_id_data['single_start_km3'] == '' ){
$trip_start3 = '';
}else{
$trip_start3 = $trip_id_data['single_start_km3'];
}

if($trip_id_data['single_start_km4'] == '' ){
$trip_start4 = '';
}else{
$trip_start4 = $trip_id_data['single_start_km4'];
}

if($trip_id_data['single_start_km5'] == '' ){
$trip_start5 = '';
}else{
$trip_start5 = $trip_id_data['single_start_km5'];
}
if($trip_id_data['single_start_km6'] == '' ){
$trip_start6 = '';
}else{
$trip_start6 = $trip_id_data['single_start_km6'];
}

if($trip_id_data['single_end_km'] == '' ){
$trip_end = '';
}else{
$trip_end = $trip_id_data['single_end_km'];
}
if($trip_id_data['single_end_km2'] == '' ){
$trip_end2 = '';
}else{
$trip_end2 = $trip_id_data['single_end_km2'];
}
if($trip_id_data['single_end_km3'] == '' ){
$trip_end3 = '';
}else{
$trip_end3 = $trip_id_data['single_end_km3'];
}

if($trip_id_data['single_end_km4'] == '' ){
$trip_end4 = '';
}else{
$trip_end4 = $trip_id_data['single_end_km4'];
}
if($trip_id_data['single_end_km5'] == '' ){
$trip_end5 = '';
}else{
$trip_end5 = $trip_id_data['single_end_km5'];
}
if($trip_id_data['single_end_km6'] == '' ){
$trip_end6 = '';
}else{
$trip_end6 = $trip_id_data['single_end_km6'];
}

if($trip_id_data['single_total_km'] == '' ){
$trip_total = '';
}else{
$trip_total = $trip_id_data['single_total_km'];
}
if($trip_id_data['single_total_km2'] == '' ){
$trip_total2 = '';
}else{
$trip_total2 = $trip_id_data['single_total_km2'];
}
if($trip_id_data['single_total_km3'] == '' ){
$trip_total3 = '';
}else{
$trip_total3 = $trip_id_data['single_total_km3'];
}

if($trip_id_data['single_total_km4'] == '' ){
$trip_total4 = '';
}else{
$trip_total4 = $trip_id_data['single_total_km4'];
}

if($trip_id_data['single_total_km5'] == '' ){
$trip_total5 = '';
}else{
$trip_total5 = $trip_id_data['single_total_km5'];
}
if($trip_id_data['single_total_km6'] == '' ){
$trip_total6 = '';
}else{
$trip_total6 = $trip_id_data['single_total_km6'];
}




if( $trip_id_data['total_freight_7'] == '0.00'){
$trip_freight7 = '';
}else{
$trip_freight7 = $trip_id_data['total_freight_7'];
}

if( $trip_id_data['total_freight_8'] == '0.00'){
$trip_freight8 = '';
}else{
$trip_freight8 = $trip_id_data['total_freight_8'];
}

if( $trip_id_data['total_freight_9'] == '0.00'){
$trip_freight9 = '';
}else{
$trip_freight9 = $trip_id_data['total_freight_9'];
}

if( $trip_id_data['total_freight_10'] == '0.00'){
$trip_freight10 = '';
}else{
$trip_freight10 = $trip_id_data['total_freight_10'];
}

if( $trip_id_data['total_freight_11'] == '0.00'){
$trip_freight11 = '';
}else{
$trip_freight11 = $trip_id_data['total_freight_11'];
}

if( $trip_id_data['total_freight_12'] == '0.00'){
$trip_freight12 = '';
}else{
$trip_freight12 = $trip_id_data['total_freight_12'];
}

if( $trip_id_data['advance_7'] == '0.00'){
$advance7 = '';
}else{
$advance7 = $trip_id_data['advance_7'];
}

if( $trip_id_data['advance_8'] == '0.00'){
$advance8 = '';
}else{
$advance8 = $trip_id_data['advance_8'];
}

if( $trip_id_data['advance_9'] == '0.00'){
$advance9 = '';
}else{
$advance9 = $trip_id_data['advance_9'];
}

if( $trip_id_data['advance_10'] == '0.00'){
$advance10 = '';
}else{
$advance10 = $trip_id_data['advance_10'];
}

if( $trip_id_data['advance_11'] == '0.00'){
$advance11 = '';
}else{
$advance11 = $trip_id_data['advance_11'];
}

if( $trip_id_data['advance_12'] == '0.00'){
$advance12 = '';
}else{
$advance12 = $trip_id_data['advance_12'];
}

if($trip_id_data['date_7'] == '1970-01-01' || $trip_id_data['date_7'] == '0000-00-00'){
$trip_date7 = '';
}else{
$trip_date7 = risFormatDate($trip_id_data['date_7']);
}

if($trip_id_data['date_8'] == '1970-01-01' || $trip_id_data['date_8'] == '0000-00-00'){
$trip_date8 = '';
}else{
$trip_date8 = risFormatDate($trip_id_data['date_8']);
}

if($trip_id_data['date_9'] == '1970-01-01' || $trip_id_data['date_9'] == '0000-00-00'){
$trip_date9 = '';
}else{
$trip_date9 = risFormatDate($trip_id_data['date_9']);
}

if($trip_id_data['date_10'] == '1970-01-01' || $trip_id_data['date_10'] == '0000-00-00'){
$trip_date10 = '';
}else{
$trip_date10 = risFormatDate($trip_id_data['date_10']);
}  

if($trip_id_data['date_11'] == '1970-01-01' || $trip_id_data['date_11'] == '0000-00-00'){
$trip_date11 = '';
}else{
$trip_date11 = risFormatDate($trip_id_data['date_11']);
}

if($trip_id_data['date_12'] == '1970-01-01' || $trip_id_data['date_12'] == '0000-00-00'){
$trip_date12 = '';
}else{
$trip_date12 = risFormatDate($trip_id_data['date_12']);
}



if($trip_id_data['single_start_km7'] == '' ){
$trip_start7 = '';
}else{
$trip_start7 = $trip_id_data['single_start_km7'];
}

if($trip_id_data['single_start_km8'] == '' ){
$trip_start8 = '';
}else{
$trip_start8 = $trip_id_data['single_start_km8'];
}

if($trip_id_data['single_start_km9'] == '' ){
$trip_start9 = '';
}else{
$trip_start9 = $trip_id_data['single_start_km9'];
}

if($trip_id_data['single_start_km10'] == '' ){
$trip_start10 = '';
}else{
$trip_start10 = $trip_id_data['single_start_km10'];
}

if($trip_id_data['single_start_km11'] == '' ){
$trip_start11 = '';
}else{
$trip_start11 = $trip_id_data['single_start_km11'];
}
if($trip_id_data['single_start_km12'] == '' ){
$trip_start12 = '';
}else{
$trip_start12 = $trip_id_data['single_start_km12'];
}

if($trip_id_data['single_end_km7'] == '' ){
$trip_end7 = '';
}else{
$trip_end7 = $trip_id_data['single_end_km7'];
}
if($trip_id_data['single_end_km8'] == '' ){
$trip_end8 = '';
}else{
$trip_end8 = $trip_id_data['single_end_km8'];
}
if($trip_id_data['single_end_km9'] == '' ){
$trip_end9 = '';
}else{
$trip_end9 = $trip_id_data['single_end_km9'];
}

if($trip_id_data['single_end_km10'] == '' ){
$trip_end10 = '';
}else{
$trip_end10 = $trip_id_data['single_end_km10'];
}
if($trip_id_data['single_end_km11'] == '' ){
$trip_end11 = '';
}else{
$trip_end11 = $trip_id_data['single_end_km11'];
}
if($trip_id_data['single_end_km12'] == '' ){
$trip_end12 = '';
}else{
$trip_end12 = $trip_id_data['single_end_km12'];
}

if($trip_id_data['single_total_km7'] == '' ){
$trip_total7 = '';
}else{
$trip_total7 = $trip_id_data['single_total_km7'];
}
if($trip_id_data['single_total_km8'] == '' ){
$trip_total8 = '';
}else{
$trip_total8 = $trip_id_data['single_total_km8'];
}
if($trip_id_data['single_total_km9'] == '' ){
$trip_total9 = '';
}else{
$trip_total9 = $trip_id_data['single_total_km9'];
}

if($trip_id_data['single_total_km10'] == '' ){
$trip_total10 = '';
}else{
$trip_total10 = $trip_id_data['single_total_km10'];
}

if($trip_id_data['single_total_km11'] == '' ){
$trip_total11 = '';
}else{
$trip_total11 = $trip_id_data['single_total_km11'];
}
if($trip_id_data['single_total_km12'] == '' ){
$trip_total12 = '';
}else{
$trip_total12 = $trip_id_data['single_total_km12'];
}





$driver_balance_amtt = $trip_id_data['total_amount'] - $row['total_cash_driver'] - $row['total_bank_driver'] - $trip_id_data['extra_paid_to_driver'];
$total_expenses = $row['total_hamali'] +
$row['total_loading_unloading'] +
$row['total_toll'] +
$row['total_kata'] +
$row['total_border'] +
$row['total_battha'] +
$row['total_salary'] +
$row['total_maintanance'] +
$row['total_tyre'] +
$row['total_electric'] +
$row['total_mechanic'] +
$row['total_police_rto'] +
$row['total_other'];
$total_net_amt = $trip_id_data['total_freight'] - $total_expenses;                   


// Initialize totals
$total_diesell = 0;
$total_diesel_amount = 0;
$total_batthaa = 0;
$total_salaryy = 0;
$total_expensess = 0;

// Query to get diesel and diesel_amount from the trip_detail table
$sql = "SELECT SUM(cash_driver) AS total_cash_driver, SUM(battha) AS total_batthaa,SUM(loading_unloading) AS total_loading_unloadingg,SUM(hamali) AS total_hamalii,SUM(toll) AS total_tolll,SUM(kata) AS total_kataa,SUM(border) AS total_borderr,SUM(salary) AS total_salaryy,SUM(maintance) AS total_maintanancee,SUM(tyre) AS total_tyree,SUM(electric) AS total_electricc,SUM(mechanic) AS total_mechanicc, SUM(police_rto) AS total_police_rtoo, SUM(other) AS total_otherr,SUM(bank_driver) AS total_bank_driver, SUM(diesel) AS tot_diesel, SUM(diesel_amount) AS tot_diesel_amount FROM trip_detail WHERE master_id = '".$trip_id."'";
$result = $conn->query($sql);

if ($result->num_rows > 0) 
{
  // Fetch the sum totals
$rowss = $result->fetch_assoc();

$total_diesell = $rowss['tot_diesel'];

$total_diesel_amount = $rowss['tot_diesel_amount'];

$total_driver_cashh = $rowss['total_cash_driver'];

$total_expensess = $rowss['total_loading_unloadingg'] + $rowss['total_hamalii'] + $rowss['total_batthaa'] + $rowss['total_tolll'] + $rowss['total_kataa'] + $rowss['total_borderr'] + $rowss['total_salaryy'] + $rowss['total_maintanancee'] + $rowss['total_tyree'] + $rowss['total_electricc'] + $rowss['total_mechanicc'] + $rowss['total_police_rtoo'];

$total_driver_bankk = $rowss['total_bank_driver'];
$total_batthaa = $rowss['total_batthaa'];
$total_salaryy = $rowss['total_salaryy'];
$paid_to_driverr = $total_driver_cashh + $total_driver_bankk;
$driver_balancee = ( $trip_id_data['total_amount'] +    $trip_id_data['extra_paid_to_driver'] + $total_expensess)  - ( $total_driver_cashh + $total_driver_bankk + $total_diesel_amount );
}else {
echo "No records found.";
}

$company_id        = $_SESSION['company_id'];
$branch_id         = $_SESSION['branch_id'];

$select_driver_balance = mysqli_query($conn, "SELECT SUM(driver_balance) AS driver_balance_sum FROM `trip` WHERE `cancel_flag` = 'N' AND `branch_id`='".$branch_id."' AND `company_id`='".$company_id."' AND `driver_id` = '".$trip_id_data['driver_id']."' AND `id` < '".$trip_id_data['id']."' ");
$fetch_driver_balance = mysqli_fetch_assoc($select_driver_balance);


$driver_balance_sum = $fetch_driver_balance['driver_balance_sum'];

$total_driver_balance = $driver_balance_sum + $driver_balancee;

$update_driver_balance = mysqli_query($conn, "UPDATE `trip` SET `driver_balance`= '".$total_driver_balance."' WHERE `id` = '".$trip_id_data['id']."' AND `driver_id` = '".$trip_id_data['driver_id']."' AND `branch_id`='".$branch_id."' AND `company_id`='".$company_id."'");


$diesel_data = mysqli_query($conn,"SELECT * FROM `trip` WHERE truck_no = '".$trip_id_data['truck_no']."' AND trip_no !=  '".$trip_id_data['trip_no']."'   ");

if ($diesel_data && mysqli_num_rows($diesel_data) > 0) {  
$diesel_id_data = mysqli_fetch_assoc($diesel_data);
$diesel_op = $diesel_id_data['close_diesel'];
}
else{
$truckQuery = "SELECT tr_diesel FROM truck_mst WHERE truck_no = '".$trip_id_data['truck_no']."' LIMIT 1";
$truckResult = mysqli_query($conn, $truckQuery);

if ($truckResult && mysqli_num_rows($truckResult) > 0) {
  $truckRow = mysqli_fetch_assoc($truckResult);
          $diesel_op = $truckRow['tr_diesel']; // Set fast tag balance as opening
        } else {
          // Debugging: Check if truck_mst query returns no data
          error_log("No record found in truck_mst table for truck: $truck_no");
          $diesel_op = 0; // Default to 0 if no record is found
        }
      }
      $diesel_used = $diesel_op + $total_diesell - $trip_id_data['close_diesel']; 

      $output = '
      <table style="" cellpadding="2" width="100%" >
      <tr>
      <td style="text-align:center;width:100%;height:50%; "><img src="../assets/logo/anshu_logo.png"></td>
      </tr>
      <tr>
      <td  style="width:16.6%; font-weight:bold;border:1px solid black;"> Trip No</td>
      <td  style="text-align:center;width:16.6%; font-weight:bold;border:1px solid black;">'.$trip_id_data['trip_no'].'</td>
      <td  style="width:16.6%; font-weight:bold;border:1px solid black;" >Start Date</td>
      <td  style="text-align:center;width:16.6%; font-weight:bold;border:1px solid black;" >'.risFormatDate($trip_id_data['date']).'</td>
      <td  style="width:16.6%; font-weight:bold;border:1px solid black;" > End Date</td>
      <td  style="text-align:center;width:16.6%; font-weight:bold;border:1px solid black;" >'.risFormatDate($trip_id_data['close_date']).'</td>
      </tr>

      <tr>
      <td style="width:25%;font-weight:bold;border:1px solid black;"> Vehicle No</td>
      <td style="text-align:center;width:25%;font-weight:bold;border:1px solid black;">'.$trip_id_data['truck_no'].'</td>
      <td style="width:25%;text-align:center;font-weight:bold;border:1px solid black;text-align:center;">Total Freight</td>
      <td style="width:25%;text-align:left;font-weight:bold;border:1px solid black;" > '.$trip_id_data['total_freight'].'</td>

      </tr>
      <tr>
      <td style="width:25%;font-weight:bold;border:1px solid black;"> Driver</td>
      <td style="text-align:center;width:25%;font-weight:bold;border:1px solid black;" >'.$trip_id_data_driver['name'].'</td>
      <td style="width:25%;text-align:center;font-size:10px;font-weight:bold;border:1px solid black;text-align:center;">Total Advance</td>
      <td style="width:25%;text-align:left;font-weight:bold;border:1px solid black;" > '.$trip_id_data['advance_amount'].'</td>   
      </tr>
      <tr>
      <td style="width:25%; text-align:center; font-weight:bold;border:1px solid black;"> Start Kilometer</td>
      <td style="width:25%; text-align:center; font-weight:bold;border:1px solid black;"> End Kilometer</td>
      <td style="width:25%; text-align:center; font-weight:bold;border:1px solid black;">  Total  Km</td>
      <td style="width:25%; text-align:center; font-weight:bold;border:1px solid black;"> Rate Per Km</td>
      </tr>
      <tr>
      <td style="width:25%; font-weight:bold;border:1px solid black;text-align:center;">  '.$trip_id_data['start_km'].'</td>
      <td style="width:25%; font-weight:bold;border:1px solid black; text-align:center;"> '.$trip_id_data['end_km'].'</td>
      <td style="width:25%; font-weight:bold;border:1px solid black;text-align:center;"> '.$trip_id_data['total_km'].'</td>
      <td style="width:25%; font-weight:bold;border:1px solid black;text-align:center;"> '.$trip_id_data['rate_per_km'].'</td>
      </tr>
      <tr>
      <td style="width:25%; font-weight:bold;border:1px solid black;">  TOTAL Amount</td>
      <td style="width:75%; font-weight:bold;border:1px solid black;"> '.$trip_id_data['total_amount'].'</td>
      </tr>
      <tr>
      <td style="width:25%; font-weight:bold;border:1px solid black;"> Total Expences</td>
      <td style="width:75%; font-weight:bold;border:1px solid black;"> '.$total_expensess.'</td>
      </tr>
      <tr>
      <td style="width:25%;font-weight:bold;border-left:1px solid black;border-top:1px solid black;border-right:1px solid black;"> EX Payable To Driver : </td>
      <td style="width:75%;font-weight:bold;border-top:1px solid black;border-right:1px solid black;">
      '.$trip_id_data['extra_paid_to_driver'].' &nbsp; <span style="font-size:8px;">'.$trip_id_data['driver_remark'].'</span></td>

      </tr>
      <tr>
      <td style="width:25%;font-weight:bold;border-left:1px solid black;border-top:1px solid black;border-right:1px solid black;">
      DRIVER CASH  </td>
      <td style="width:75%;font-weight:bold;border-top:1px solid black;border-right:1px solid black;">
      '.$total_driver_cashh.'</td>
      </tr>

      <tr>
      <td style="width:25%;font-weight:bold;border-left:1px solid black;border-top:1px solid black;border-right:1px solid black;">
      DRIVER BANK : 
      </td>
      <td style="width:75%;font-weight:bold;border-top:1px solid black;border-right:1px solid black;">
      '.$total_driver_bankk.'</td>
      </tr>
      <tr>
      <td style="width:25%;font-weight:bold;border-left:1px solid black;border-top:1px solid black;border-right:1px solid black;">
      Diesel Amount :</td>
      <td style="width:30%;font-weight:bold;border-top:1px solid black;border-right:1px solid black;">
      '.$total_diesel_amount.'   </td>
      <td style="width:20%;font-weight:bold;border-top:1px solid black;border-right:1px solid black;">
      Diesel Qty : 
      </td>
      <td style="width:25%;font-weight:bold;border-top:1px solid black;border-right:1px solid black;">
      '.$total_diesell.'</td>
      </tr>

       <tr>
      <td style="width:25%;font-weight:bold;border:1px solid black;font-size:10px;" > Previous Driver Balance</td>
      <td style="width:75%;font-weight:bold;border:1px solid black;" > '.$driver_balance_sum.'</td>
      </tr>

       <tr>
      <td style="width:25%;font-weight:bold;border:1px solid black;font-size:10px;" > Current Driver Balance</td>
      <td style="width:75%;font-weight:bold;border:1px solid black;" > '.$driver_balancee.'</td>
      </tr>

      <tr>
      <td style="width:25%;font-weight:bold;border:1px solid black;font-size:10px;" > Total Driver Balance</td>
      <td style="width:75%;font-weight:bold;border:1px solid black;" > '.$total_driver_balance.'</td>
      </tr>
      <tr>
      <td></td>
      <td></td>
      </tr>
      <tr>
      <td  style="width:12.5%; font-weight:bold;border:1px solid black;">DIESEL OPEING</td>
      <td  style="width:12.5%; font-weight:bold;border:1px solid black;">'.$diesel_op.'</td>
      <td  style="width:12.5%; font-weight:bold;border:1px solid black;" >DIESEL FILLED</td>
      <td  style="width:12.5%; font-weight:bold;border:1px solid black;" >'.$total_diesell.'</td>
      <td  style="width:12.5%; font-weight:bold;border:1px solid black;"> Diesel Used</td>
      <td  style="width:12.5%; font-weight:bold;border:1px solid black;"> '.$diesel_used.'</td>
      <td  style="width:12.5%; font-weight:bold;border:1px solid black;">Closing DIESEL QTY</td>
      <td  style="width:12.5%; font-weight:bold;border:1px solid black;">'.$trip_id_data['close_diesel'].'</td>

      </tr>

      <tr>
      <td></td>
      <td></td>
      </tr>

      <tr>
      <th style="text-align:center;width:20%;font-weight:bold;border:1px solid black;" colpan="2"> FROM PLACE </th>
      <th style="text-align:center;width:20%;font-weight:bold;border:1px solid black;" colspan="2"> TO PLACE </th>
      <th style="text-align:center;width:20%;font-weight:bold;border:1px solid black;"> DATE </th>
      <th style="text-align:center;width:13.3%;font-weight:bold;border:1px solid black;" >  START KM </th>
      <th style="text-align:center;width:13.3%;font-weight:bold;border:1px solid black;" > END KM </th>
      <th style="text-align:center;width:13.3%;font-weight:bold;border:1px solid black;" > TOTAL KM </th>
      </tr>';
      if($trip_id_data['from_place_1'] != ''  || $trip_id_data['to_place_1'] != '' ||  $trip_date1 != '' ||  $trip_freight1 != '' || $advance1 != '' || $trip_start != '' || $trip_end != '' || $trip_total != '' ){
        $output .='
        <tr>
        <td style="width:20%;text-align:center;border:1px solid black;text-align:center;"> '.$trip_id_data['from_place_1'].'</td>
        <td style="width:20%;text-align:center;border:1px solid black; text-align:center;"> '.$trip_id_data['to_place_1'].'</td>
        <td style="width:20%;text-align:center;border:1px solid black;text-align:center;"> '.$trip_date1.'</td>
        <td style="width:13.3%;text-align:center;border:1px solid black;"> '.$trip_start.'  </td>
        <td style="width:13.3%;text-align:center;border:1px solid black;"> '.$trip_end.' </td>
        <td style="width:13.3%;text-align:center;border:1px solid black;text-align:center;">'.$trip_total.' </td>
        </tr>';
      }
      if($trip_id_data['from_place_2'] != ''  || $trip_id_data['to_place_2'] != '' ||  $trip_date2  != ''  ||  $trip_freight2   != '' || $advance2   != '' || $trip_start2 != '' || $trip_end2 != '' || $trip_total2 != ''  ){
        $output .='
        <tr>
        <td style="width:20%;text-align:center;border:1px solid black;text-align:center;"> '.$trip_id_data['from_place_2'].'</td>
        <td style="width:20%;text-align:center;border:1px solid black; text-align:center;"> '.$trip_id_data['to_place_2'].'</td>
        <td style="width:20%;text-align:center;border:1px solid black;text-align:center;"> '.$trip_date2.'</td>
        <td style="width:13.3%;text-align:center;border:1px solid black;"> '.$trip_start2.'  </td>
        <td style="width:13.3%;text-align:center;border:1px solid black;"> '.$trip_end2.' </td>
        <td style="width:13.3%;text-align:center;border:1px solid black;text-align:center;">'.$trip_total2.' </td>
        </tr>';
      }

      if($trip_id_data['from_place_3'] != ''  || $trip_id_data['to_place_3'] != '' ||  $trip_date3  != ''  ||  $trip_freight3   != '' || $advance3   != '' || $trip_start3 != '' || $trip_end3 != '' || $trip_total3 != ''   ){
        $output .='
        <tr>
        <td style="width:20%;text-align:center;border:1px solid black;text-align:center;"> '.$trip_id_data['from_place_3'].'</td>
        <td style="width:20%;text-align:center;border:1px solid black; text-align:center;"> '.$trip_id_data['to_place_3'].'</td>
        <td style="width:20%;text-align:center;border:1px solid black;text-align:center;"> '.$trip_date3.'</td>
        <td style="width:13.3%;text-align:center;border:1px solid black;"> '.$trip_start3.'  </td>
        <td style="width:13.3%;text-align:center;border:1px solid black;"> '.$trip_end3.' </td>
        <td style="width:13.3%;text-align:center;border:1px solid black;text-align:center;">'.$trip_total3.' </td>
        </tr>';
      }

      if($trip_id_data['from_place_4'] != ''  || $trip_id_data['to_place_4'] != '' ||  $trip_date2  != ''  ||  $trip_freight4   != '' || $advance4   != '' || $trip_start4 != '' || $trip_end4 != '' || $trip_total4 != ''   ){
        $output .='
        <tr>
        <td style="width:20%;text-align:center;border:1px solid black;text-align:center;"> '.$trip_id_data['from_place_4'].'</td>
        <td style="width:20%;text-align:center;border:1px solid black; text-align:center;"> '.$trip_id_data['to_place_4'].'</td>
        <td style="width:20%;text-align:center;border:1px solid black;text-align:center;"> '.$trip_date4.'</td>
        <td style="width:13.3%;text-align:center;border:1px solid black;"> '.$trip_start4.'  </td>
        <td style="width:13.3%;text-align:center;border:1px solid black;"> '.$trip_end4.' </td>
        <td style="width:13.3%;text-align:center;border:1px solid black;text-align:center;">'.$trip_total4.' </td>
        </tr>';
      }

      if($trip_id_data['from_place_5'] != ''  || $trip_id_data['to_place_5'] != '' ||  $trip_date5  != ''  ||  $trip_freight5   != '' || $advance5   != '' || $trip_start5 != '' || $trip_end5 != '' || $trip_total5 != ''   ){
        $output .='
        <tr>
        <td style="width:20%;text-align:center;border:1px solid black;text-align:center;"> '.$trip_id_data['from_place_5'].'</td>
        <td style="width:20%;text-align:center;border:1px solid black; text-align:center;"> '.$trip_id_data['to_place_5'].'</td>
        <td style="width:20%;text-align:center;border:1px solid black;text-align:center;"> '.$trip_date5.'</td>
        <td style="width:13.3%;text-align:center;border:1px solid black;"> '.$trip_start5.'  </td>
        <td style="width:13.3%;text-align:center;border:1px solid black;"> '.$trip_end5.' </td>
        <td style="width:13.3%;text-align:center;border:1px solid black;text-align:center;">'.$trip_total5.' </td>
        </tr>';
      }

      if($trip_id_data['from_place_6'] != ''  || $trip_id_data['to_place_6'] != '' ||  $trip_date6  != ''  ||  $trip_freight6   != '' || $advance6   != '' || $trip_start6 != '' || $trip_end6 != '' || $trip_total6 != ''   ){
        $output .='
        <tr>
        <td style="width:20%;text-align:center;border:1px solid black;text-align:center;"> '.$trip_id_data['from_place_6'].'</td>
        <td style="width:20%;text-align:center;border:1px solid black; text-align:center;"> '.$trip_id_data['to_place_6'].'</td>
        <td style="width:20%;text-align:center;border:1px solid black;text-align:center;"> '.$trip_date6.'</td>
        <td style="width:13.3%;text-align:center;border:1px solid black;"> '.$trip_start6.'  </td>
        <td style="width:13.3%;text-align:center;border:1px solid black;"> '.$trip_end6.' </td>
        <td style="width:13.3%;text-align:center;border:1px solid black;text-align:center;">'.$trip_total6.' </td>
        </tr>';
      }
      if($trip_id_data['from_place_7'] != ''  || $trip_id_data['to_place_7'] != '' ||  $trip_date7 != '' ||  $trip_freight7 != '' || $advance7 != '' || $trip_start7 != '' || $trip_end7 != '' || $trip_total7 != '' ){
        $output .='
        <tr>
        <td style="width:20%;text-align:center;border:1px solid black;text-align:center;"> '.$trip_id_data['from_place_7'].'</td>
        <td style="width:20%;text-align:center;border:1px solid black; text-align:center;"> '.$trip_id_data['to_place_7'].'</td>
        <td style="width:20%;text-align:center;border:1px solid black;text-align:center;"> '.$trip_date7.'</td>
        <td style="width:13.3%;text-align:center;border:1px solid black;"> '.$trip_start7.'  </td>
        <td style="width:13.3%;text-align:center;border:1px solid black;"> '.$trip_end7.' </td>
        <td style="width:13.3%;text-align:center;border:1px solid black;text-align:center;">'.$trip_total7.' </td>
        </tr>';
      }
      if($trip_id_data['from_place_8'] != ''  || $trip_id_data['to_place_8'] != '' ||  $trip_date8  != ''  ||  $trip_freight8   != '' || $advance8   != '' || $trip_start8 != '' || $trip_end8 != '' || $trip_total8 != ''  ){
        $output .='
        <tr>
        <td style="width:20%;text-align:center;border:1px solid black;text-align:center;"> '.$trip_id_data['from_place_8'].'</td>
        <td style="width:20%;text-align:center;border:1px solid black; text-align:center;"> '.$trip_id_data['to_place_8'].'</td>
        <td style="width:20%;text-align:center;border:1px solid black;text-align:center;"> '.$trip_date8.'</td>
        <td style="width:13.3%;text-align:center;border:1px solid black;"> '.$trip_start8.'  </td>
        <td style="width:13.3%;text-align:center;border:1px solid black;"> '.$trip_end8.' </td>
        <td style="width:13.3%;text-align:center;border:1px solid black;text-align:center;">'.$trip_total8.' </td>
        </tr>';
      }

      if($trip_id_data['from_place_9'] != ''  || $trip_id_data['to_place_9'] != '' ||  $trip_date9  != ''  ||  $trip_freight9   != '' || $advance9   != '' || $trip_start9 != '' || $trip_end9 != '' || $trip_total9 != ''   ){
        $output .='
        <tr>
        <td style="width:20%;text-align:center;border:1px solid black;text-align:center;"> '.$trip_id_data['from_place_9'].'</td>
        <td style="width:20%;text-align:center;border:1px solid black; text-align:center;"> '.$trip_id_data['to_place_9'].'</td>
        <td style="width:20%;text-align:center;border:1px solid black;text-align:center;"> '.$trip_date9.'</td>
        <td style="width:13.3%;text-align:center;border:1px solid black;"> '.$trip_start9.'  </td>
        <td style="width:13.3%;text-align:center;border:1px solid black;"> '.$trip_end9.' </td>
        <td style="width:13.3%;text-align:center;border:1px solid black;text-align:center;">'.$trip_total9.' </td>
        </tr>';
      }

      if($trip_id_data['from_place_10'] != ''  || $trip_id_data['to_place_10'] != '' ||  $trip_date10  != ''  ||  $trip_freight10   != '' || $advance10   != '' || $trip_start10 != '' || $trip_end10 != '' || $trip_total10 != ''   ){
        $output .='
        <tr>
        <td style="width:20%;text-align:center;border:1px solid black;text-align:center;"> '.$trip_id_data['from_place_10'].'</td>
        <td style="width:20%;text-align:center;border:1px solid black; text-align:center;"> '.$trip_id_data['to_place_10'].'</td>
        <td style="width:20%;text-align:center;border:1px solid black;text-align:center;"> '.$trip_date10.'</td>
        <td style="width:13.3%;text-align:center;border:1px solid black;"> '.$trip_start10.'  </td>
        <td style="width:13.3%;text-align:center;border:1px solid black;"> '.$trip_end10.' </td>
        <td style="width:13.3%;text-align:center;border:1px solid black;text-align:center;">'.$trip_total10.' </td>
        </tr>';
      }

      if($trip_id_data['from_place_11'] != ''  || $trip_id_data['to_place_11'] != '' ||  $trip_date11  != ''  ||  $trip_freight11   != '' || $advance11   != '' || $trip_start11 != '' || $trip_end11 != '' || $trip_total11 != ''   ){
        $output .='
        <tr>
        <td style="width:20%;text-align:center;border:1px solid black;text-align:center;"> '.$trip_id_data['from_place_11'].'</td>
        <td style="width:20%;text-align:center;border:1px solid black; text-align:center;"> '.$trip_id_data['to_place_11'].'</td>
        <td style="width:20%;text-align:center;border:1px solid black;text-align:center;"> '.$trip_date11.'</td>
        <td style="width:13.3%;text-align:center;border:1px solid black;"> '.$trip_start11.'  </td>
        <td style="width:13.3%;text-align:center;border:1px solid black;"> '.$trip_end11.' </td>
        <td style="width:13.3%;text-align:center;border:1px solid black;text-align:center;">'.$trip_total11.' </td>
        </tr>';
      }

      if($trip_id_data['from_place_12'] != ''  || $trip_id_data['to_place_12'] != '' ||  $trip_date12  != ''  ||  $trip_freight12   != '' || $advance12   != '' || $trip_start12 != '' || $trip_end12 != '' || $trip_total12 != ''   ){
        $output .='
        <tr>
        <td style="width:20%;text-align:center;border:1px solid black;text-align:center;"> '.$trip_id_data['from_place_12'].'</td>
        <td style="width:20%;text-align:center;border:1px solid black; text-align:center;"> '.$trip_id_data['to_place_12'].'</td>
        <td style="width:20%;text-align:center;border:1px solid black;text-align:center;"> '.$trip_date12.'</td>
        <td style="width:13.3%;text-align:center;border:1px solid black;"> '.$trip_start12.'  </td>
        <td style="width:13.3%;text-align:center;border:1px solid black;"> '.$trip_end12.' </td>
        <td style="width:13.3%;text-align:center;border:1px solid black;text-align:center;">'.$trip_total12.' </td>
        </tr>';
      }

      $output .= '
      <tr>
      <td></td>
      <td></td>
      </tr>
      <tr>
      <th style="width:16.6%;text-align:center;font-weight:bold;border:1px solid black;text-align:center;">DATE</th>
      <th style="width:16.6%;text-align:center;font-weight:bold;border:1px solid black;text-align:center;">Driver Cash</th>
      <th style="width:16.6%;text-align:center;font-weight:bold;border:1px solid black;text-align:center;">Driver BANK</th>
      <th style="width:16.6%;text-align:center;font-weight:bold;border:1px solid black;text-align:center;">Diesel Qty</th>
      <th style="width:16.6%;text-align:center;font-weight:bold;border:1px solid black;text-align:center;">Diesel Amount</th>
      <th style="width:16.6%;text-align:center;font-weight:bold;border:1px solid black;text-align:center;">Fast Tag</th>
      </tr>';
      foreach ($trip_data_fetch as $trip_data_fetchs) {
        "SELECT * FROM `trip_detail` WHERE  `master_id` = '".$trip_data_fetchs['id']."'";
        $con_rto_details = mysqli_query($conn,"SELECT * FROM `trip_detail` WHERE  `master_id` = '".$trip_data_fetchs['id']."'");
        $con_rto_row = mysqli_fetch_assoc($con_rto_details);

        $total_driver_cash += $trip_data_fetchs['total_cash_driver'];
        $total_driver_bank += $trip_data_fetchs['total_bank_driver'];
        $total_diesel += $trip_data_fetchs['total_diesel'];
        $total_deisel_amount += $trip_data_fetchs['total_diesel_amount'];
        $total_other += $trip_data_fetchs['total_other'];
        $paid_to_driver = $total_driver_cash + $total_driver_bank;
        $driver_balance = $driver_balance_amt - $paid_to_driver - $total_deisel_amount;
        if($trip_data_fetchs['total_cash_driver'] != '0'  || $trip_data_fetchs['total_bank_driver'] != '0' ||  $trip_data_fetchs['total_diesel'] != '0'  ||  $trip_data_fetchs['total_diesel_amount'] != '0' || $trip_data_fetchs['total_other'] != '0'){  
         $output .='
         <tr>
         <td style="width:16.6%;border-left:1px solid black;border-bottom:1px solid black;border-top:1px solid black;border-right:1px solid black;text-align:center;">'.risFormatDate($trip_data_fetchs['date']).'</td>
         <td style="width:16.6%;border-left:1px solid black;border-bottom:1px solid black;border-top:1px solid black;border-right:1px solid black;text-align:center;">'.$trip_data_fetchs['total_cash_driver'].'</td>
         <td style="width:16.6%;border-left:1px solid black;border-bottom:1px solid black;border-top:1px solid black;border-right:1px solid black;text-align:center;">'.$trip_data_fetchs['total_bank_driver'].'</td>
         <td style="width:16.6%;border-left:1px solid black;border-bottom:1px solid black;border-top:1px solid black;border-right:1px solid black;text-align:center;">'.$trip_data_fetchs['total_diesel'].'</td>
         <td style="width:16.6%;border-left:1px solid black;border-bottom:1px solid black;border-top:1px solid black;border-right:1px solid black;text-align:center;">'.$trip_data_fetchs['total_diesel_amount'].'</td>
         <td style="width:16.6%;border-left:1px solid black;border-bottom:1px solid black;border-top:1px solid black;border-right:1px solid black;text-align:center;">'.$trip_data_fetchs['total_other'].'</td>
         </tr>';  
       }      
     }
     if( $num < 3) {
      for ($j=1; $j <= ( 3 - $num ); $j++) {
       $output .='
       <tr>
       <td style="width:16.6%;border:1px solid black;border-right:1px solid black;text-align:center;"><b></b></td>
       <td style="width:16.6%;border:1px solid black;border-right:1px solid black;text-align:center;"><b></b></td>
       <td style="width:16.6%;border:1px solid black;border-right:1px solid black;text-align:center;"><b></b></td>
       <td style="width:16.6%;border:1px solid black;border-right:1px solid black;text-align:center;"><b></b></td>
       <td style="width:16.6%;border:1px solid black;border-right:1px solid black;text-align:center;"><b></b></td>
       <td style="width:16.6%;border:1px solid black;border-right:1px solid black;text-align:center;"><b></b></td>
       </tr>';
     }
   }
   $output .='
   <tr>
   <td style="width:16.6%;font-weight:bold;border:1px solid black;border-right:1px solid black;text-align:center;">TOTAL </td>
   <td style="width:16.6%;font-weight:bold;border:1px solid black;border-right:1px solid black;text-align:center;">'.$total_driver_cash.'</td>
   <td style="width:16.6%;font-weight:bold;border:1px solid black;border-right:1px solid black;text-align:center;">'.$total_driver_bank.'</td>
   <td style="width:16.6%;font-weight:bold;border:1px solid black;border-right:1px solid black;text-align:center;">'.$total_diesel.'</td>
   <td style="width:16.6%;font-weight:bold;border:1px solid black;border-right:1px solid black;text-align:center;">'.$total_deisel_amount.'</td>
   <td style="width:16.6%;font-weight:bold;border:1px solid black;border-right:1px solid black;text-align:center;">'.$total_other.'</td>
   </tr>

   <tr>
   <td></td>
   <td></td>
   </tr>
   <tr>
   <th style="width:14.28%;text-align:center;font-weight:bold;border:1px solid black;text-align:center;">DATE</th>
   <th style="width:14.28%;text-align:center;font-weight:bold;border:1px solid black;text-align:center;">HAMALI</th>
   <th style="width:14.28%;text-align:center;font-size:8px;font-weight:bold;border:1px solid black;text-align:center;">Loading/Unloading</th>
   <th style="width:14.28%;text-align:center;font-weight:bold;border:1px solid black;text-align:center;">Toll</th>
   <th style="width:14.28%;text-align:center;font-weight:bold;border:1px solid black;text-align:center;">Kata</th>
   <th style="width:14.28%;text-align:center;font-weight:bold;border:1px solid black;text-align:center;">Border</th>
   <th style="width:14.28%;text-align:center;font-weight:bold;border:1px solid black;text-align:center;">Battha</th>
   </tr>';
   foreach ($trip_data_fetch as $trip_data_fetchs) {
    "SELECT * FROM `trip_detail` WHERE  `master_id` = '".$trip_data_fetchs['id']."'";
    $con_rto_details = mysqli_query($conn,"SELECT * FROM `trip_detail` WHERE  `master_id` = '".$trip_data_fetchs['id']."'");
    $con_rto_row = mysqli_fetch_assoc($con_rto_details);

    $total_hamali += $trip_data_fetchs['total_hamali'];
    $total_loading_unloading += $trip_data_fetchs['total_loading_unloading'];
    $total_toll += $trip_data_fetchs['total_toll'];
    $total_kata += $trip_data_fetchs['total_kata'];
    $total_border += $trip_data_fetchs['total_border'];
    $total_battha += $trip_data_fetchs['total_battha'];
    $paid_to_driver = $total_driver_cash + $total_driver_bank;
    $driver_balance = $driver_balance_amt - $paid_to_driver - $total_deisel_amount;


    if($trip_data_fetchs['total_hamali'] != '0'  || $trip_data_fetchs['total_loading_unloading'] != '0' ||  $trip_data_fetchs['total_toll'] != '0'  ||  $trip_data_fetchs['total_kata'] != '0' || $trip_data_fetchs['total_border'] != '0' || $trip_data_fetchs['total_battha'] != '0' ){
     $output .='
     <tr>
     <td style="width:14.28%;border-left:1px solid black;border-bottom:1px solid black;border-top:1px solid black;border-right:1px solid black;text-align:center;">'.risFormatDate($trip_data_fetchs['date']).'</td>
     <td style="width:14.28%;border-left:1px solid black;border-bottom:1px solid black;border-top:1px solid black;border-right:1px solid black;text-align:center;">'.$trip_data_fetchs['total_hamali'].'</td>
     <td style="width:14.28%;border-left:1px solid black;border-bottom:1px solid black;border-top:1px solid black;border-right:1px solid black;text-align:center;">'.$trip_data_fetchs['total_loading_unloading'].'</td>
     <td style="width:14.28%;border-left:1px solid black;border-bottom:1px solid black;border-top:1px solid black;border-right:1px solid black;text-align:center;">'.$trip_data_fetchs['total_toll'].'</td>
     <td style="width:14.28%;border-left:1px solid black;border-bottom:1px solid black;border-top:1px solid black;border-right:1px solid black;text-align:center;">'.$trip_data_fetchs['total_kata'].'</td>
     <td style="width:14.28%;border-left:1px solid black;border-bottom:1px solid black;border-top:1px solid black;border-right:1px solid black;text-align:center;">'.$trip_data_fetchs['total_border'].'</td>
     <td style="width:14.28%;border-left:1px solid black;border-bottom:1px solid black;border-top:1px solid black;border-right:1px solid black;text-align:center;">'.$trip_data_fetchs['total_battha'].'</td>
     </tr>';
   }        
 }
 if( $num < 3) {
  for ($j=1; $j <= ( 3 - $num ); $j++) {
   $output .='
   <tr>
   <td style="width:14.28%;border:1px solid black;border-right:1px solid black;text-align:center;"><b></b></td>
   <td style="width:14.28%;border:1px solid black;border-right:1px solid black;text-align:center;"><b></b></td>
   <td style="width:14.28%;border:1px solid black;border-right:1px solid black;text-align:center;"><b></b></td>
   <td style="width:14.28%;border:1px solid black;border-right:1px solid black;text-align:center;"><b></b></td>
   <td style="width:14.28%;border:1px solid black;border-right:1px solid black;text-align:center;"><b></b></td>
   <td style="width:14.28%;border:1px solid black;border-right:1px solid black;text-align:center;"><b></b></td>
   <td style="width:14.28%;border:1px solid black;border-right:1px solid black;text-align:center;"><b></b></td>
   </tr>';
 }
}
if($total_hamali != ''  || $total_loading_unloading != '' ||  $total_toll != ''  ||  $total_kata != '' || $total_border != '' || $total_battha != '' ){
$output .=' 
<tr>
<td style="width:14.28%;font-weight:bold;border:1px solid black;border-right:1px solid black;text-align:center;">TOTAL </td>
<td style="width:14.28%;font-weight:bold;border:1px solid black;border-right:1px solid black;text-align:center;">'.$total_hamali.'</td>
<td style="width:14.28%;font-weight:bold;border:1px solid black;border-right:1px solid black;text-align:center;">'.$total_loading_unloading.'</td>
<td style="width:14.28%;font-weight:bold;border:1px solid black;border-right:1px solid black;text-align:center;">'.$total_toll.'</td>
<td style="width:14.28%;font-weight:bold;border:1px solid black;border-right:1px solid black;text-align:center;">'.$total_kata.'</td>
<td style="width:14.28%;font-weight:bold;border:1px solid black;border-right:1px solid black;text-align:center;">'.$total_border.'</td>
<td style="width:14.28%;font-weight:bold;border:1px solid black;border-right:1px solid black;text-align:center;">'.$total_battha.'</td>
</tr>';
}
$output .=' 
<tr>
<td></td>
<td></td>
</tr>
<tr>
<th style="width:14.28%;text-align:center;font-weight:bold;border:1px solid black;text-align:center;">DATE</th>
<th style="width:14.28%;text-align:center;font-weight:bold;border:1px solid black;text-align:center;">Salary</th>
<th style="width:14.28%;text-align:center;font-size:8px;font-weight:bold;border:1px solid black;text-align:center;">Maintanance</th>
<th style="width:14.28%;text-align:center;font-weight:bold;border:1px solid black;text-align:center;">Tyre</th>
<th style="width:14.28%;text-align:center;font-weight:bold;border:1px solid black;text-align:center;">Electric</th>
<th style="width:14.28%;text-align:center;font-weight:bold;border:1px solid black;text-align:center;">Mechanic</th>
<th style="width:14.28%;text-align:center;font-weight:bold;border:1px solid black;text-align:center;">Police/RTO</th>
</tr>';
foreach ($trip_data_fetch as $trip_data_fetchs) {
"SELECT * FROM `trip_detail` WHERE  `master_id` = '".$trip_data_fetchs['id']."'";
$con_rto_details = mysqli_query($conn,"SELECT * FROM `trip_detail` WHERE  `master_id` = '".$trip_data_fetchs['id']."'");
$con_rto_row = mysqli_fetch_assoc($con_rto_details);

$total_salary += $trip_data_fetchs['total_salary'];
$total_maintanance += $trip_data_fetchs['total_maintanance'];
$total_tyre += $trip_data_fetchs['total_tyre'];
$total_electric += $trip_data_fetchs['total_electric'];
$total_mechanic += $trip_data_fetchs['total_mechanic'];
$total_police_rto += $trip_data_fetchs['total_police_rto'];
$paid_to_driver = $total_driver_cash + $total_driver_bank;
$driver_balance = $driver_balance_amt - $paid_to_driver - $total_deisel_amount;

if($trip_data_fetchs['total_salary'] != '0'  || $trip_data_fetchs['total_maintanance'] != '0' ||  $trip_data_fetchs['total_tyre'] != '0'  ||  $trip_data_fetchs['total_electric'] != '0' || $trip_data_fetchs['total_mechanic'] != '0' || $trip_data_fetchs['total_police_rto'] != '0' ){  
 $output .='
 <tr>
 <td style="width:14.28%;border-left:1px solid black;border-bottom:1px solid black;border-top:1px solid black;border-right:1px solid black;text-align:center;">'.risFormatDate($trip_data_fetchs['date']).'</td>
 <td style="width:14.28%;border-left:1px solid black;border-bottom:1px solid black;border-top:1px solid black;border-right:1px solid black;text-align:center;">'.$trip_data_fetchs['total_salary'].'</td>
 <td style="width:14.28%;border-left:1px solid black;border-bottom:1px solid black;border-top:1px solid black;border-right:1px solid black;text-align:center;">'.$trip_data_fetchs['total_maintanance'].'</td>
 <td style="width:14.28%;border-left:1px solid black;border-bottom:1px solid black;border-top:1px solid black;border-right:1px solid black;text-align:center;">'.$trip_data_fetchs['total_tyre'].'</td>
 <td style="width:14.28%;border-left:1px solid black;border-bottom:1px solid black;border-top:1px solid black;border-right:1px solid black;text-align:center;">'.$trip_data_fetchs['total_electric'].'</td>
 <td style="width:14.28%;border-left:1px solid black;border-bottom:1px solid black;border-top:1px solid black;border-right:1px solid black;text-align:center;">'.$trip_data_fetchs['total_mechanic'].'</td>
 <td style="width:14.28%;border-left:1px solid black;border-bottom:1px solid black;border-top:1px solid black;border-right:1px solid black;text-align:center;">'.$trip_data_fetchs['total_police_rto'].'</td>
 </tr>';
}        
}
if( $num < 3) {
for ($j=1; $j <= ( 3 - $num ); $j++) {
 $output .='
 <tr>
 <td style="width:14.28%;border:1px solid black;border-right:1px solid black;text-align:center;"><b></b></td>
 <td style="width:14.28%;border:1px solid black;border-right:1px solid black;text-align:center;"><b></b></td>
 <td style="width:14.28%;border:1px solid black;border-right:1px solid black;text-align:center;"><b></b></td>
 <td style="width:14.28%;border:1px solid black;border-right:1px solid black;text-align:center;"><b></b></td>
 <td style="width:14.28%;border:1px solid black;border-right:1px solid black;text-align:center;"><b></b></td>
 <td style="width:14.28%;border:1px solid black;border-right:1px solid black;text-align:center;"><b></b></td>
 <td style="width:14.28%;border:1px solid black;border-right:1px solid black;text-align:center;"><b></b></td>
 </tr>';
}
}
if($total_salary != ''  || $total_maintanance != '' ||  $total_tyre != ''  ||  $total_electric != '' || $total_mechanic != '' || $total_police_rto != ''){  
$output .=' 
<tr>
<td style="width:14.28%;font-weight:bold;border:1px solid black;border-right:1px solid black;text-align:center;">TOTAL </td>
<td style="width:14.28%;font-weight:bold;border:1px solid black;border-right:1px solid black;text-align:center;">'.$total_salary.'</td>
<td style="width:14.28%;font-weight:bold;border:1px solid black;border-right:1px solid black;text-align:center;">'.$total_maintanance.'</td>
<td style="width:14.28%;font-weight:bold;border:1px solid black;border-right:1px solid black;text-align:center;">'.$total_tyre.'</td>
<td style="width:14.28%;font-weight:bold;border:1px solid black;border-right:1px solid black;text-align:center;">'.$total_electric.'</td>
<td style="width:14.28%;font-weight:bold;border:1px solid black;border-right:1px solid black;text-align:center;">'.$total_mechanic.'</td>
<td style="width:14.28%;font-weight:bold;border:1px solid black;border-right:1px solid black;text-align:center;">'.$total_police_rto.'</td>
</tr>';
}



$output .=' 

</table>';


$pdf->SetPrintHeader(true);
$pdf->SetPrintFooter(true);
$pdf->AddPage('P','A4');
$pdf->writeHTML($output, true, false, false, false, '');
$pdf->SetTitle('BILL COPY'.'-'.$_GET['eid']);
$pdf->Output('bill_copy'.'_'.$_GET['eid'].'.pdf', 'I');
?>