<?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 `billing` WHERE vendor_id = '".$vendor_id."'  AND invoice_no = '".$bill_no."' AND DATE(bill_date) >= '".$sd."' AND DATE(bill_date) <= '".$ed."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY invoice_no";

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

}else if(!empty($vendor_id)){
	$lr = "SELECT * FROM `billing` WHERE vendor_id = '".$vendor_id."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY invoice_no";

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

$result = mysqli_query($conn,$lr);
$data = array();
while ($row = mysqli_fetch_array($result)) {
	$data[] = $row;
}
$total       	= mysqli_num_rows($result);

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

foreach ($data as $key => $data) {
	$add      = "SELECT SUM(amount) AS total_add FROM billing_details WHERE type = 'add' AND bill_no = '".$data['invoice_no']."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY bill_no";
	$add_res  = mysqli_query($conn,$add);
	$add_row  = mysqli_fetch_assoc($add_res);

	$sub      = "SELECT SUM(amount) AS total_sub FROM billing_details WHERE type = 'sub' AND bill_no = '".$data['invoice_no']."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY bill_no";
	$sub_res  = mysqli_query($conn,$sub);
	$sub_row  = mysqli_fetch_assoc($sub_res);
	

	$total_add = $add_row['total_add'];
	$total_sub = $sub_row['total_sub'];

	$lr_data 	 		= "SELECT * FROM billing WHERE invoice_no = '".$data['invoice_no']."' 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['lr_amount'];
		$other     += $lr_data_row['other_charges'];
		$ga    	   = $lr_data_row['gross_amt'];
	}

	$table .= '<tr class="text-center">';
	$table .= '<td colspan="9" class="report-bg"><b>'.@$master_accounts[$data['vendor_id']].' - '.$data["invoice_no"].' - ('.risFormatDate($data["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['lr_no'] . '</td>';
		$table .= '<td class="text-center">' . $lr_datas['lr_weight'] . '</td>';
		$table .= '<td class="text-center">' . $lr_datas['lr_rate'] . '</td>';
		$table .= '<td class="text-center">' . $lr_datas['lr_amount'] . '</td>';
		$table .= '<td class="text-center">' . $lr_datas['other_charges'] . '</td>';
		
		if($lastColumnPrinted == false): $lastColumnPrinted = true;
		$table .= '<td class="text-center mr-report-vertical" rowspan='.$lr_data_rowspan.'>' . $data['gross_amt'] . '</td>';
		$table .= '<td class="text-center mr-report-vertical" rowspan='.$lr_data_rowspan.'>' . $add_row['total_add'] . '</td>';
		$table .= '<td class="text-center mr-report-vertical" rowspan='.$lr_data_rowspan.'>' . $sub_row['total_sub'] . '</td>';
		$table .= '<td class="text-center mr-report-vertical" rowspan='.$lr_data_rowspan.'>' . $data['net_amt'] . '</td>';
		endif;
		$table .= '</tr>';
	}
	
}

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