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


	$obj = new Datatable($conn);
	$obj->aColumns 		= array('payment.voucher_no', 'payment.b_date', 'act_master.ac_name AS aname', 'payment.bank', 'payment.account_name', 'payment.amount', 'payment.created_id');
    $obj->eColumns 		= array('payment.id');
    $obj->sIndexColumn 	= "payment.id";
    $obj->sTable 		= " payment";
    $obj->myWhere 		= " JOIN act_master ON payment.account_name=act_master.act_code WHERE 1=1 AND flag = 'CR' AND payment.company_id = '".$_SESSION['company_id']."' AND payment.branch_id = '".$_SESSION['branch_id']."' AND DATE(payment.b_date)>='".$_SESSION['from_year']."' AND DATE(payment.b_date)<='".$_SESSION['to_year']."'";
    $obj->groupBy       = ' GROUP BY payment.voucher_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();
        // CHECK CASH RECEIPT
        $br 	= "SELECT * FROM `mr_payment_status` WHERE `voucher_no` = '".$aRow['voucher_no']."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
        $br_res = mysqli_query($conn,$br);
        $br_row = mysqli_fetch_assoc($br_res);
        

        if($br_row['voucher_no'] == $aRow['voucher_no']){
			$style = "display:none";
		}else{
        	$style = "display:inline-block";
        }


        $bank     = "SELECT * FROM `act_master` WHERE `id` ='".$aRow['bank']."'";
	    $bank_res = mysqli_query($conn,$bank);
	    $bank_row = mysqli_fetch_array($bank_res);
		
		$temp_arr[] = ++$cou;
		$temp_arr[] = $aRow['voucher_no'];
		$temp_arr[] = date("d-m-Y", strtotime($aRow['b_date']));
		$temp_arr[] = $bank_row['ac_name'];
		$temp_arr[] = $aRow['aname'];
		$temp_arr[] = $aRow['amount'];
		$temp_arr[] = @$master_user[$aRow['created_id']];
		
		$str = '';
		if( hasPermission($_SESSION['user_detail']['role_id'], 'cash_receipt_edit') ) {
		$str .= '<a href="index.php?page=cash_receipt/manage.php&p1='.$aRow['voucher_no'].'" name="edit" class="btn btn-primary" data-toggle="tooltip" data-placement="top" title="EDIT CASH RECEIPT...!!" style="'.$style.'"><em class="fa fa-pencil"></em></a>&nbsp;';
		}

		$str .= '<a href="cash_receipt/cash_receipt_print_new.php?eid='.$aRow['voucher_no'].'" name="print" class="btn btn-info" data-toggle="tooltip" data-placement="top" title="CASH RECEIPT PRINT..!!" target="_blank"><em class="fa fa-print"></em></a>&nbsp;';
		
		if( hasPermission($_SESSION['user_detail']['role_id'], 'cash_receipt_delete') ) {
		$str .= '<button type="button" class="btn btn-danger delete_data" id="'.$aRow["voucher_no"].'" data-toggle="tooltip" data-placement="top" title="DELETE CASH RECEIPT..!!" onclick="confirmDelete(this);" style="'.$style.'"><em class="fa fa-trash"></em></button>';
		}
		$temp_arr[] = $str;

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

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