<?php 
include "../config.php";
if(isset($_POST['search'])){
    $search 	= $_POST['search'];
    $vendor_id 	= $_POST['vendor_id'];

    $master_vendor       = combine(getAllRecords('vendor_master'), 'id', 'name');
    $vendor_name 	     = @$master_vendor[$_POST['vendor_id']];
    $aco 				 = "SELECT * FROM `act_master` WHERE `ac_name` = '".$vendor_name."'";
    $aco_res 			 = mysqli_query($conn,$aco);
    $aco_row 			 = mysqli_fetch_assoc($aco_res);
    
    /*$bill = "SELECT 
                trans.voucher_no,
                trans.voucher_date AS v_date,
                IFNULL(billing.lr_from, contract.from_place) AS l_from,
                IFNULL(billing.lr_to, contract.to_place) AS l_to,
                trans.amount AS amount,
                trans.balance AS balance,
                trans.voucher_no,
                trans.flag,
                billing.invoice_no,
                opening_balance.invoice_no,
                contract.code
		FROM trans 
		LEFT JOIN billing ON trans.voucher_no = billing.invoice_no 
		LEFT JOIN opening_balance ON trans.voucher_no = opening_balance.invoice_no
        LEFT JOIN contract ON trans.voucher_no = contract.code
		WHERE (billing.vendor_id = '".$vendor_id."' or opening_balance.account_name = '".$aco_row['act_code']."' or contract.vendor_id = '".$vendor_id."') AND trans.balance >= '0' AND (billing.branch_id = '".$_SESSION['branch_id']."' OR opening_balance.branch_id = '".$_SESSION['branch_id']."' OR contract.branch_id = '".$_SESSION['branch_id']."') AND (billing.company_id = '".$_SESSION['company_id']."' OR opening_balance.company_id = '".$_SESSION['company_id']."' OR contract.company_id = '".$_SESSION['company_id']."') AND (trans.flag = 'BILLING' OR trans.flag = 'CONTRACT') AND trans.branch_id = '".$_SESSION['branch_id']."' AND trans.company_id = '".$_SESSION['company_id']."' AND trans.voucher_no like '%$search%' GROUP BY trans.voucher_no"; */



    $billing = "SELECT 
                trans.voucher_no,
                trans.voucher_date AS v_date,
                billing.lr_from AS l_from,
                billing.lr_to AS l_to,
                trans.amount AS amount,
                trans.balance AS balance,
                trans.voucher_no,
                trans.flag,
                billing.invoice_no,
                opening_balance.invoice_no,
                general_sales_details.from AS g_from,
                general_sales_details.to AS g_to
        FROM trans 
        LEFT JOIN billing ON trans.voucher_no = billing.invoice_no
        LEFT JOIN general_sales_details ON trans.voucher_no = general_sales_details.bill_no 
        LEFT JOIN opening_balance ON trans.voucher_no = opening_balance.invoice_no
        WHERE (trans.flag = 'BILLING' OR trans.flag = 'GENERAL SALES') AND (billing.vendor_id = '".$vendor_id."' or opening_balance.account_name = '".$aco_row['act_code']."'  or trans.act_code = '".$aco_row['act_code']."') AND trans.balance > 0 AND trans.branch_id = '".$_SESSION['branch_id']."' AND trans.company_id = '".$_SESSION['company_id']."' AND trans.voucher_no like '%$search%' GROUP BY trans.voucher_no";

    $bill_res = mysqli_query($conn,$billing);
    $response  = array();
    while($billl_row = mysqli_fetch_assoc($bill_res) ){

        $temp        = array();
        $temp['voucher_no']  = $billl_row['voucher_no'];
        $temp['v_date']      = risFormatDate($billl_row['v_date']);
        if($billl_row['flag'] == 'BILLING'){
            $temp['l_from']      = $billl_row['l_from'];
            $temp['l_to']        = $billl_row['l_to'];    
        }else{
            $temp['l_from']      = $billl_row['g_from'];
            $temp['l_to']        = $billl_row['g_to'];
        }
        
        $temp['amount']      = $billl_row['amount'];
        $temp['balance']     = $billl_row['balance'];
        $response[]          = $temp;
    }

  
    echo json_encode($response);
}
exit;
