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

$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($sd) && !empty($ed)){
	$lr = "SELECT * FROM `mr` WHERE vendor_id = '".$vendor_id."' AND DATE(mr_date) >= '".$sd."' AND DATE(mr_date) <= '".$ed."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY mr_no";

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

}else{
	$lr = "SELECT * FROM `mr` WHERE DATE(mr_date) >= '".$sd."' AND DATE(mr_date) <= '".$ed."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY mr_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;
$total_bank  	 	= 0;
$total_add    		= 0;
$total_sub    		= 0;
$total_cash   		= 0;

foreach ($data as $key => $data) {
	$mr 				= "SELECT * FROM mr WHERE mr_no = '".$data['mr_no']."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
	$mr_res 			= mysqli_query($conn,$mr);
	$mr_data_rowspan 	= mysqli_num_rows($mr_res);
	$mr_data = array();
	
	while ($mr_row = mysqli_fetch_assoc($mr_res)) {
		$mr_data[] = $mr_row;
	}
	
	$tota_rec_amnt = "SELECT SUM(bill_rec) AS bill_rec_amnt FROM mr WHERE mr_no = '".$data['mr_no']."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY mr_no";
	$total_rec_amnt_res = mysqli_query($conn,$tota_rec_amnt);
	$total_rec_amnt_row = mysqli_fetch_assoc($total_rec_amnt_res);

	$add      = "SELECT SUM(amount) AS total_add FROM mr_details WHERE type = 'add' AND mr_no = '".$data['mr_no']."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY mr_no";
	$add_res  = mysqli_query($conn,$add);
	$add_row  = mysqli_fetch_assoc($add_res);
	
	$sub      = "SELECT SUM(amount) AS total_sub FROM mr_details WHERE type = 'sub' AND mr_no = '".$data['mr_no']."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY mr_no";
	$sub_res  = mysqli_query($conn,$sub);
	$sub_row  = mysqli_fetch_assoc($sub_res);

	$bank     = "SELECT * FROM `mr_payment_status` WHERE `mr_no` = '".$data['mr_no']."' AND payment_type = 'BANK' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY mr_no";
	$bank_res = mysqli_query($conn,$bank);
	$bank_row = mysqli_fetch_assoc($bank_res);

	$cash     = "SELECT * FROM `mr_payment_status` WHERE `mr_no` = '".$data['mr_no']."' AND payment_type = 'CASH' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY mr_no";
	
	$cash_res = mysqli_query($conn,$cash);
	$cash_row = mysqli_fetch_assoc($cash_res);
	
	$table .= '<tr class="text-center report-bg">';
	$table .= '<td colspan="12"><b>'. @$master_accounts[$data['vendor_id']] .' - '.$data["mr_no"].' - ('.risFormatDate($data["mr_date"]).')</b></td>';
	$table .= '</tr>';
	$lastColumnPrinted 	= false;
	foreach ($mr_data as $key => $mr_datas) {
		$remove_alies = substr($mr_datas['bill_no'], 6);
		$new_bill_no   = ltrim($remove_alies, "0");
		$table .= '<tr class="text-center">';
		$table .= '<td class="text-center">' . $mr_datas['bill_no'] . '</td>';
		$table .= '<td class="text-center">' . customFormatMoney($mr_datas["bill_amount"]) . '</td>';
		$table .= '<td class="text-center">' . customFormatMoney($mr_datas["bill_rec"]) . '</td>';
		$table .= '<td class="text-center">' . customFormatMoney($mr_datas["tds"]) . '</td>';
		//$table .= '<td class="text-center mr-report-vertical" rowspan='.$mr_data_rowspan.'>' . customFormatMoney($data["tds"]) . '</td>';

		
		if($lastColumnPrinted == false): $lastColumnPrinted = true;
		$table .= '<td class="text-center mr-report-vertical" rowspan='.$mr_data_rowspan.'>' . customFormatMoney($add_row['total_add']) . '</td>';

		$table .= '<td class="text-center mr-report-vertical" rowspan='.$mr_data_rowspan.'>' . customFormatMoney($sub_row['total_sub']) . '</td>';

		$table .= '<td class="text-center mr-report-vertical" rowspan='.$mr_data_rowspan.'>' . customFormatMoney($total_rec_amnt_row['bill_rec_amnt'] - $sub_row['total_sub']) . '</td>';

		$table .= '<td class="text-center mr-report-vertical" rowspan='.$mr_data_rowspan.'>' . customFormatMoney($data["pay_mode_amnt"]) . '</td>';

		$table .= '<td class="text-center mr-report-vertical" rowspan='.$mr_data_rowspan.'>' . customFormatMoney($data["cash"]) . '</td>';

		$table .= '<td class="text-center mr-report-vertical" rowspan='.$mr_data_rowspan.'>' . customFormatMoney($data["cash_diss"]) . '</td>';

		$table .= '<td class="text-center mr-report-vertical" rowspan='.$mr_data_rowspan.'>' . customFormatMoney($data["late_delivery"]) . '</td>';

		$table .= '<td class="text-center mr-report-vertical" rowspan='.$mr_data_rowspan.'>' . customFormatMoney($data["claim"]) . '</td>';
		endif;
		$table .= '</tr>';
	}
	
}

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