<?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)){
	$billing = "SELECT billing.lr_no, lr_mst.lr_no, billing.vendor_id, billing.invoice_no, lr_mst.gst_paid_by, billing.branch_id, billing.company_id, billing.invoice_no, billing.bill_date, billing.gross_amt, billing.sgst_per, billing.sgst_amount, billing.cgst_per, billing.cgst_amount, billing.igst_per, billing.igst_amount, billing.net_amt
	FROM billing
	INNER JOIN lr_mst ON billing.lr_no=billing.lr_no
	WHERE (lr_mst.consignor_name = '".$vendor_id."' OR lr_mst.consignee_name = '".$vendor_id."') AND billing.branch_id ='".$_SESSION['branch_id']."' AND billing.company_id ='".$_SESSION['company_id']."' AND billing.vendor_id ='".$vendor_id."' AND DATE(bill_date) >= '".$sd."' AND DATE(bill_date) <= '".$ed."'
	GROUP BY billing.invoice_no";


}else if(!empty($vendor_id)){
	$billing = "SELECT billing.lr_no, lr_mst.lr_no, billing.vendor_id, billing.invoice_no, lr_mst.gst_paid_by, billing.branch_id, billing.company_id, billing.invoice_no, billing.bill_date, billing.gross_amt, billing.sgst_per, billing.sgst_amount, billing.cgst_per, billing.cgst_amount, billing.igst_per, billing.igst_amount, billing.net_amt
	FROM billing
	INNER JOIN lr_mst ON billing.lr_no=billing.lr_no
	WHERE (lr_mst.consignor_name = '".$vendor_id."' OR lr_mst.consignee_name = '".$vendor_id."') AND billing.branch_id ='".$_SESSION['branch_id']."' AND billing.company_id ='".$_SESSION['company_id']."' AND billing.vendor_id ='".$vendor_id."'
	GROUP BY billing.invoice_no";


}else{
	$billing = "SELECT billing.lr_no, lr_mst.lr_no, billing.vendor_id, billing.invoice_no, lr_mst.gst_paid_by, billing.branch_id, billing.company_id, billing.invoice_no, billing.bill_date, billing.gross_amt, billing.sgst_per, billing.sgst_amount, billing.cgst_per, billing.cgst_amount, billing.igst_per, billing.igst_amount, billing.net_amt
	FROM billing
	INNER JOIN lr_mst ON billing.lr_no=billing.lr_no
	WHERE  billing.branch_id ='".$_SESSION['branch_id']."' AND billing.company_id ='".$_SESSION['company_id']."' AND DATE(bill_date) >= '".$sd."' AND DATE(bill_date) <= '".$ed."' OR (lr_mst.consignor_name = '".$vendor_id."' OR lr_mst.consignee_name = '".$vendor_id."')
	GROUP BY billing.invoice_no";
}

$billing_result = mysqli_query($conn,$billing);
$bill_data      = array();
while ($billing_row = mysqli_fetch_assoc($billing_result)) {
	$bill_data[] = $billing_row;
}
$total  = mysqli_num_rows($billing_result);
$master_accounts    = combine(getAllRecords('vendor_master'), 'id', 'name');
$table       		= '';

foreach ($bill_data as $key => $datas) {
	$add = mysqli_query($conn,"SELECT * FROM `billing_details` WHERE `type` = 'add' AND `bill_no` = '".$datas['invoice_no']."'");
	$total_add = 0;
	while ($add_row = mysqli_fetch_assoc($add)) {
		$total_add += $add_row['amount'];
	}

	$deduction = mysqli_query($conn,"SELECT * FROM `billing_details` WHERE `type` = 'sub' AND `bill_no` = '".$datas['invoice_no']."'");
	$total_deduction = 0;
	while ($deduction_row = mysqli_fetch_assoc($deduction)) {
		$total_deduction += $deduction_row['amount'];
	}

	$vendor = mysqli_query($conn, "SELECT * FROM `vendor_master` WHERE `id` = '".$datas['vendor_id']."'");
	$vendor_row = mysqli_fetch_assoc($vendor);

	$table .= '<tr class="text-center">';
	$table .= '<td class="text-center">' . $datas['invoice_no'] . '</td>';
	$table .= '<td class="text-center">' . risFormatDate($datas['bill_date']) . '</td>';
	$table .= '<td class="text-center">' . $vendor_row['name'] . '</td>';
	$table .= '<td class="text-center">' . $vendor_row['gstin'] . '</td>';
	$table .= '<td class="text-center">' . customFormatMoney($datas['gross_amt']) . '</td>';
	$table .= '<td class="text-center">' . customFormatMoney($total_add) . '</td>';
	$table .= '<td class="text-center">' . customFormatMoney($total_deduction) . '</td>';
	$table .= '<td class="text-center">' . customFormatMoney($datas['sgst_per']) . '</td>';
	$table .= '<td class="text-center">' . customFormatMoney($datas['sgst_amount']) . '</td>';
	$table .= '<td class="text-center">' . customFormatMoney($datas['cgst_per']) . '</td>';
	$table .= '<td class="text-center">' . customFormatMoney($datas['cgst_amount']) . '</td>';
	$table .= '<td class="text-center">' . customFormatMoney($datas['igst_per']) . '</td>';
	$table .= '<td class="text-center">' . customFormatMoney($datas['igst_amount']) . '</td>';
	$table .= '<td class="text-center">' . customFormatMoney($datas['net_amt']) . '</td>';
	$table .= '<td class="text-center">' . $datas['gst_paid_by'] . '</td>';
	$table .= '</tr>';
}

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