<?php 
include("../config.php");

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

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

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

$sd = mysqli_real_escape_string($conn, date('Y-m-d', strtotime($_GET['start_date'])));
$ed = mysqli_real_escape_string($conn, date('Y-m-d', strtotime($_GET['end_date'])));

if(!empty($vendor_id) && !empty($broker) && !empty($truck_no) && !empty($sd) && !empty($ed)){
	$lr = "SELECT * FROM `contract` WHERE agent = '".$broker."' AND truck_no = '".$truck_no."' AND (vendor_id = '".$vendor_id."')AND DATE(c_date) >= '".$sd."' AND DATE(c_date) <= '".$ed."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY code";
}else if(!empty($vendor_id) && !empty($truck_no) && !empty($sd) && !empty($ed)){
	$lr = "SELECT * FROM `contract` WHERE truck_no = '".$truck_no."' AND (vendor_id = '".$vendor_id."')AND DATE(c_date) >= '".$sd."' AND DATE(c_date) <= '".$ed."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY code";
}elseif(!empty($vendor_id) && !empty($sd) && !empty($ed)){
	$lr = "SELECT * FROM `contract` WHERE (vendor_id = '".$vendor_id."') AND DATE(c_date) >= '".$sd."' AND DATE(c_date) <= '".$ed."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY code";
}else if(!empty($vendor_id)){
	$lr = "SELECT * FROM `contract` WHERE vendor_id = '".$vendor_id."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY code";

}else if(!empty($broker) && !empty($sd) && !empty($ed)){
	$lr = "SELECT * FROM `contract` WHERE agent = '".$broker."' AND DATE(c_date) >= '".$sd."' AND DATE(c_date) <= '".$ed."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY code";
}else if(!empty($truck_no) && !empty($sd) && !empty($ed)){
	$lr = "SELECT * FROM `contract` WHERE truck_no = '".$truck_no."' AND DATE(c_date) >= '".$sd."' AND DATE(c_date) <= '".$ed."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY code";
}else if(!empty($sd) && !empty($ed)){
	$lr = "SELECT * FROM `contract` WHERE DATE(c_date) >= '".$sd."' AND DATE(c_date) <= '".$ed."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY code";
}else{
	$lr = "SELECT * FROM `contract` WHERE DATE(c_date) >= '".$sd."' AND DATE(c_date) <= '".$ed."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY code";
}


$result 		= mysqli_query($conn,$lr);
$total       	= mysqli_num_rows($result);

$master_accounts       = combine(getAllRecords('vendor_master'), 'id', 'name');

$table       	= '';
$total_count    = 0;
if(count($total) > 0) {
	$total_fright = 0;
	while ($row = mysqli_fetch_array($result)) {

		$total_fright += $row['amount'];
		$table .= '<tr class="text-center">';
		$table .= '<td>'.$row["code"].'</td>';
		$table .= '<td>'.$row["c_date"].'</td>';
		$table .= '<td>'.$row["truck_no"].'</td>';
		$table .= '<td>' . @$master_accounts[$row['vendor_id']] .'</td>';
		$table .= '<td>'.$row["from_place"].'</td>';
		$table .= '<td>'. $row['to_place'] . '</td>';
		$table .= '<td>' . $row['weight'] . '</td>';
		$table .= '<td>' . $row['km'] . '</td>';
		$table .= '<td>' . customFormatMoney($row['amount']) . '</td>';
		$table .= '<td>' . $row['agent'] . '</td>';
		$table .= '</tr>';
	}
	$table .= '<tr>';
	$table .= '<td colspan="8"><b>TOTAL FREIGHT</b></td>';
	$table .= '<td class="text-center">' . customFormatMoney($total_fright) . '</td>';
	$table .= '</tr>';
}
$data = array('status' => 'success', 'total_count' => $total, 'tbody' => $table);
	echo json_encode($data);
    die();
?>