<?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 con_bill = '".$bill_no."' 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) && !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($sd) && !empty($ed) && !empty($bill_no)){
	$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);
$data = array();
while ($row = mysqli_fetch_array($result)) {
	$data[] = $row;
}
$total       	= mysqli_num_rows($result);
$master_vendor  = combine(getAllRecords('vendor_master'), 'id','gstin');
$master_accounts= combine(getAllRecords('vendor_master'), 'id', 'name');
$table       	= '';
$total_count    = 0;

foreach ($data as $key => $data) {
	

	$lr_data 	 		= "SELECT * FROM contract_bill WHERE con_bill = '".$data['con_bill']."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
	$lr_data_res 		= mysqli_query($conn,$lr_data);
	$lr_data_rowspan 	= mysqli_num_rows($lr_data_res);
	$lr_data 			= array();
	$lr_amnt 			= 0;
	$other   			= 0;
	$ga    	 			= 0;
	while ($lr_data_row = mysqli_fetch_array($lr_data_res)) {
		$lr_data[] = $lr_data_row;
		$lr_amnt   += $lr_data_row['contract_amount'];
	}

	$table .= '<tr class="text-center">';
	$table .= '<td colspan="9" class="report-bg"><b>'.@$master_accounts[$data['vendor_id']].' - '.@$master_vendor[$data['vendor_id']].'-'.$data["con_bill"].' - ('.risFormatDate($data["con_bill_date"]).')</b></td>';
	$table .= '</tr>';
	$lastColumnPrinted 	= false;
	foreach ($lr_data as $key => $lr_datas){
		$table .= '<tr class="text-center">';
		$table .= '<td class="text-center">' . $lr_datas['contract_no'] . '</td>';
		$table .= '<td class="text-center">' . $lr_datas['con_bill_date'] . '</td>';
		$table .= '<td class="text-center">' . $lr_datas['contract_truck'] . '</td>';
		$table .= '<td class="text-center">' . $lr_datas['contract_from'] . '</td>';
		$table .= '<td class="text-center">' . $lr_datas['contract_to'] . '</td>';
		$table .= '<td class="text-center">' . $lr_datas['contract_weight'] . '</td>';
		$table .= '<td class="text-center">' . $lr_datas['contract_amount'] . '</td>';
		$table .= '<td class="text-center">' . $lr_datas['contract_advance'] . '</td>';
		$table .= '<td class="text-center">' . $lr_datas['contract_balance'] . '</td>';
		$table .= '</tr>';
	}
	
}

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