<?php 
	include("../config.php");
	include("../Datatable.php");


	$obj = new Datatable($conn);

	$obj->aColumns 		= array('substr(invoice_no, 1, 13) AS invoice_no', 'vendor_id', 'invoice_no', 'bill_date', 'net_amt', 'gross_amt', 'created_id');
    $obj->eColumns 		= array('id');
    $obj->sIndexColumn 	= "id";
    $obj->sTable 		= "billing";
    $obj->myWhere 		= "WHERE company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
    $obj->groupBy       = 'GROUP BY billing.invoice_no';
    $obj->datatable_process();

    $cou = 0;
    if(isset($_GET['iDisplayStart'])) {
    	$cou = $_GET['iDisplayStart'];
    }
    $master_user = combine(getAllRecords('users'), 'id', 'name');
    while ($aRow = mysqli_fetch_array($obj->rResult)) {
        $temp_arr = array();
        
        $mr 	  = "SELECT * FROM `mr` WHERE `bill_no` = '".$aRow['invoice_no']."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
        $mr_res   = mysqli_query($conn,$mr);
        $mr_num   = mysqli_num_rows($mr_res);

        if($mr_num != 0){
        	$display= "display:none";
        }else{
        	$display= "";
        }

        if($aRow['cancel_flag'] == 'Y'){
            $style = "display:none";
        }else{
            $style = "display:-webkit-inline-box";
        }

        $party      = "SELECT * FROM `vendor_master` WHERE `id` = '".$aRow['vendor_id']."'";
        $party_res  = mysqli_query($conn,$party);
        $party_row  = mysqli_fetch_array($party_res);
        
		$temp_arr[] = ++$cou;
		$temp_arr[] = $party_row['name'];
		$temp_arr[] = substr($aRow['invoice_no'], 9);
		$temp_arr[] = date("d-m-Y", strtotime($aRow['bill_date']));
		$temp_arr[] = $aRow['net_amt'];
		$temp_arr[] = $aRow['gross_amt'];
        $temp_arr[] = @$master_user[$aRow['created_id']];
		
		$str = '';
		if( hasPermission($_SESSION['user_detail']['role_id'], 'bill_edit') ) {
        $str .= '<a href="index.php?page=bill/edit.php&eid='.$aRow['invoice_no'].'" name="edit" class="btn btn-primary" data-toggle="tooltip" data-placement="top" title="EDIT BILL...!!" style='.$display.'><em class="fa fa-pencil"></em></a>&nbsp;';
        }

        if( hasPermission($_SESSION['user_detail']['role_id'], 'bill_show') ) {
        $str .= '<a href="index.php?page=bill/manage_bill.php&eid='.$aRow['invoice_no'].'" name="edit" class="btn btn-primary" data-toggle="tooltip" data-placement="top" title="BILL SUBMIT CONFORMATION...!!" style='.$display.'><em class="fa fa-copyright"></em></a>&nbsp;';

        }
        $str .= '<a href="bill/sl_bill.php?p1='.$aRow['invoice_no'].'" name="edit" class="btn btn-primary" data-toggle="tooltip" data-placement="top" title="PRINT BILL...!!" target="_blank"><em class="fa fa-print"></em></a>&nbsp;';
        
        // $str .= '<button type="button" class="btn btn-primary" id="'.$aRow["invoice_no"].'" data-toggle="tooltip" data-placement="top" title="Bill PRINT..!!" onclick="billPrint(this);"><em class="fa fa-print"></em></button>&nbsp;';

    
		if( hasPermission($_SESSION['user_detail']['role_id'], 'bill_delete') ) {
		$str .= '<button type="button" class="btn btn-danger delete_data" id="'.$aRow["invoice_no"].'" data-toggle="tooltip" data-placement="top" title="DELETE BILL..!!" onclick="confirmDelete(this);" style='.$display.'><em class="fa fa-trash"></em></button>&nbsp;';
		}

        $temp_arr[] = $str;
        $obj->output['aaData'][] = $temp_arr;
    }

    echo json_encode($obj->output);
    exit();
?>