<?php 
include("../config.php");
if(isset($_GET['vendor_id']) && !empty($_GET['vendor_id'])) {
	$vendor_id = strtoupper($_GET['vendor_id']);
}

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

$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($bill_no) && !empty($sd) && !empty($ed)){
	$lr = "SELECT * FROM `contract_bill` WHERE vendor_id = '".$vendor_id."' AND DATE(con_bill_date) >= '".$sd."' AND DATE(con_bill_date) <= '".$ed."' AND con_bill = '".$bill_no."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY con_bill";
}else if(!empty($vendor_id) && !empty($sd) && !empty($ed)){
	$lr = "SELECT * FROM `contract_bill` WHERE vendor_id = '".$vendor_id."' AND DATE(con_bill_date) >= '".$sd."' AND DATE(con_bill_date) <= '".$ed."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY con_bill";
}else if(!empty($vendor_id)){
	$lr = "SELECT * FROM `contract_bill` WHERE vendor_id = '".$vendor_id."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY con_bill";

}else if(!empty($bill_no) && !empty($sd) && !empty($ed)){
	$lr = "SELECT * FROM `contract_bill` WHERE DATE(con_bill_date) >= '".$sd."' AND DATE(con_bill_date) <= '".$ed."' AND con_bill = '".$bill_no."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY con_bill";
}else{
	$lr = "SELECT * FROM `contract_bill` WHERE DATE(con_bill_date) >= '".$sd."' AND DATE(con_bill_date) <= '".$ed."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY con_bill";
}

$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;
	$amount = 0;

	while ($row = mysqli_fetch_array($result)) {
		$total_fright += $row['contract_amount'];
		$table .= '<tr class="text-center">';
		$table .= '<td>'.$row["con_bill"].'</td>';
		$table .= '<td>'.$row["con_bill_date"].'</td>';
		$table .= '<td>' . @$master_accounts[$row['vendor_id']] .'</td>';
		$table .= '<td>' . $row["contract_truck"] . '</td>';
		$table .= '<td>' . $row["contract_from"]. '</td>';
		$table .= '<td>' . $row["contract_to"] . '</td>';
		$table .= '<td>' . $row["contract_weight"] . '</td>';
		$table .= '<td>' . customFormatMoney($row['contract_advance']) . '</td>';
		$table .= '<td>' . customFormatMoney($row['contract_balance']) . '</td>';
		$table .= '<td>' . customFormatMoney($row['contract_amount']) . '</td>';
		$table .= '</tr>';
	}
	$table .= '<tr class="text-center">';
	$table .= '<td colspan="9"><b>TOTAL</b></td>';
	$table .= '<td>' . customFormatMoney($total_fright) . '</td>';
	$table .= '</tr>';
}
$data = array('status' => 'success', 'total_count' => $total, 'tbody' => $table);
	echo json_encode($data);
    die();
?>