<?php 
include("../config.php");
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']);
}

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

$master_truck     	   = combine(getAllRecords('truck_mst'), 'id', 'truck_no');
$master_maintance_type = combine(getAllRecords('maintance_type'), 'id', 'name');
if($report_type == 'S'){
	$table .= '<tr>';
	$table .= '<th class="text-center" width="10">SR.NO</th>';
	$table .= '<th class="text-left">MAINTANCE TYPE</th>';
	$table .= '<th class="text-right" width="100">AMOUNT</th>';
	$table .= '</tr>';

	//MAINTANCE TYPE
	$maintance = mysqli_query($conn, "SELECT * FROM `maintance_type` WHERE `status` = '0'");
	$maintance_data = array();
	while ($maintance_row = mysqli_fetch_assoc($maintance)) {
		$maintance_data[] = $maintance_row;
	}
	$cou = 1;
	$total_amount = 0;
	foreach ($maintance_data as $key => $maintance_datas) {
		
		if($strat_date != '' || $end_date !='' || $truck_id !=''){
			$maintance_expence = mysqli_query($conn, "SELECT SUM(`amount`) AS expence FROM `maintance` WHERE `maintance_type_id` = '".$maintance_datas['id']."' AND `truck_id` = '".$truck_id."' AND `date` >= '".$strat_date."' AND `date` <= '".$end_date."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'");	
		}

		$maintance_expence_row = mysqli_fetch_assoc($maintance_expence);
		$amount = $maintance_expence_row['expence'];
		$total_amount += $maintance_expence_row['expence'];
		$table .= '<tr>';
		$table .= '<th class="text-center">'.$cou++.'</th>';
		$table .= '<th class="text-left">'.$maintance_datas['name'].'</th>';
		$table .= '<th class="text-right">'.customFormatMoney($amount).'</th>';
		$table .= '</tr>';
	}
	$table .= '<tr>';
	$table .= '<th class="text-right" colspan="2">TOTAL AMOUNT</th>';
	$table .= '<th class="text-right">'.customFormatMoney($total_amount).'</th>';
	$table .= '</tr>';
}else{
	$table .= '<tr>';
	$table .= '<th class="text-center" width="50">SR.NO</th>';
	$table .= '<th class="text-center" width="100">DATE</th>';
	$table .= '<th class="text-center" width="100">TRUCK NO</th>';
	$table .= '<th class="text-left">MAINTANCE TYPE</th>';
	$table .= '<th class="text-right" width="100">AMOUNT</th>';
	$table .= '</tr>';

	
	if($strat_date != '' && $end_date !='' && $truck_id !=''){
		$maintance_expence = 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']."'");

	}else if($strat_date != '' && $end_date !='' && $truck_id ==''){
		$maintance_expence = 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']."'");
	}

	$data = array();
	while ($maintance_expence_row = mysqli_fetch_assoc($maintance_expence)) {
		$data[] = $maintance_expence_row;
	}

	$cou = 1;
	$grand_total = 0;
	foreach ($data as $key => $datas) {
		$grand_total += $datas['amount'];
		$table .= '<tr>';
		$table .= '<th class="text-center" width="50">'.$cou++.'</th>';
		$table .= '<th class="text-center" width="100">'.risFormatDate($datas['date']).'</th>';
		$table .= '<th class="text-center" width="100">'.@$master_truck[$datas['truck_id']].'</th>';
		$table .= '<th class="text-left">'.@$master_maintance_type[$datas['maintance_type_id']].'</th>';
		$table .= '<th class="text-right" width="100">'.customFormatMoney($datas['amount']).'</th>';
		$table .= '</tr>';
	}

	$table .= '<tr>';
	$table .= '<th class="text-right" colspan="4">TOTAL AMOUNT</th>';
	$table .= '<th class="text-right">'.customFormatMoney($grand_total).'</th>';
	$table .= '</tr>';
}
$data = array('status' => 'success', 'total_count' => $total, 'tbody' => $table);
	echo json_encode($data);
    die();
?>