<?php 
include("../config.php");
$master_maintance_type = combine(getAllRecords('maintance_type'), 'id', 'name');
$master_truck     	   = combine(getAllRecords('truck_mst'), 'id', 'truck_no');
if(isset($_GET['start_date']) && !empty($_GET['start_date'])) {
	$strat_date = mysqli_real_escape_string($conn, date('Y-m-d', strtotime($_GET['start_date'])));
}

if(isset($_GET['end_date']) && !empty($_GET['end_date'])) {
	$end_date = mysqli_real_escape_string($conn, date('Y-m-d', strtotime($_GET['end_date'])));
}

if(isset($_GET['truck_id']) && !empty($_GET['truck_id'])) {
	$truck_id = strtoupper($_GET['truck_id']);
}

$truck_no = @$master_truck[$truck_id];

if($strat_date !='' && $end_date !='' && $truck_id ==''){
	$query = mysqli_query($conn, "SELECT * FROM `con_lhc` WHERE `lhc_date` >= '".$strat_date."' AND `lhc_date` <= '".$end_date."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'");

	$expense     = mysqli_query($conn, "SELECT * FROM `maintance` WHERE `date` >= '".$strat_date."' AND `date` <= '".$end_date."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'");
}else if($strat_date !='' && $end_date !='' && $truck_id !=''){
	
	$query = mysqli_query($conn, "SELECT * FROM `con_lhc` WHERE `lhc_date` >= '".$strat_date."' AND `lhc_date` <= '".$end_date."' AND `truck_no` = '".$truck_no."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'");

	$expense     = mysqli_query($conn, "SELECT * FROM `maintance` WHERE `truck_id` = '".$truck_id."' AND `date` >= '".$strat_date."' AND `date` <= '".$end_date."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'");
}

$data = array();
while ($rows = mysqli_fetch_assoc($query)) {
	$data[] = $rows;
}

$expense_data = array();
while ($expense_row = mysqli_fetch_assoc($expense)) {
	$expense_data[] = $expense_row;
}


$table .= '<tr>';
$table .= '<th class="text-center" colspan="3" style="background: #c2c2c2">INCOME</th>';
$table .= '</tr>';

$table .= '<tr>';
$table .= '<th class="text-center" width="100">TRUCK NO</th>';
$table .= '<th class="text-center">CON LHC</th>';
$table .= '<th class="text-center">AMOUNT</th>';
$table .= '</tr>';

$total_income = 0;
foreach ($data as $key => $datas) {
	$total_income += $datas['net_amnt'];
	$truck_no = @$master_truck[$datas['truck_no']];
	$expense     = mysqli_query($conn, "SELECT * FROM `maintance` WHERE `truck_id` = '".$truck_no."'");
	$expense_row = mysqli_fetch_assoc($expense);
	
	$table .= '<tr>';
	$table .= '<td class="text-center">'.$datas['truck_no'].'</td>';
	$table .= '<td class="text-center">'.$datas['code'].'</td>';
	$table .= '<td class="text-center">'.customFormatMoney($datas['net_amnt']).'</td>';
	$table .= '</tr>';
}
$table .= '<tr style="background: antiquewhite;">';
$table .= '<th class="text-center" colspan="2">TOTAL AMOUNT</th>';
$table .= '<th class="text-center">'.customFormatMoney($total_income).'</th>';
$table .= '</tr>';

$table .= '<tr>';
$table .= '<th class="text-center" colspan="3" style="background: #c2c2c2">EXPENSE</th>';
$table .= '</tr>';

$table .= '<tr>';
$table .= '<th class="text-center">TRUCK NO</th>';
$table .= '<th class="text-center">MAINTANCE TYPE</th>';
$table .= '<th class="text-center">AMOUNT</th>';
$table .= '</tr>';

$total_expense = 0;
foreach ($expense_data as $key => $expense_datas) {
	
	$total_expense += $expense_datas['amount'];
	$table .= '<tr>';
	$table .= '<td class="text-center">'.@$master_truck[$expense_datas['truck_id']].'</td>';
	$table .= '<td class="text-center">'.@$master_maintance_type[$expense_datas['maintance_type_id']].'</td>';
	$table .= '<td class="text-center">'.customFormatMoney($expense_datas['amount']).'</td>';
	$table .= '</tr>';
}

$table .= '<tr style="background: antiquewhite;">';
$table .= '<th class="text-center" colspan="2">TOTAL AMOUNT</th>';
$table .= '<th class="text-center">'.customFormatMoney($total_expense).'</th>';
$table .= '</tr>';

$table .= '<tr style="background: #00800042;">';
$table .= '<th class="text-center" colspan="2">PROFIT</th>';
$table .= '<th class="text-center">'.customFormatMoney($total_income - $total_expense).'</th>';
$table .= '</tr>';

$data = array('status' => 'success', 'total_count' => $total, 'tbody' => $table);
	echo json_encode($data);
    die();
?>