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


	$obj = new Datatable($conn);

	$obj->aColumns 		= array('id','mr_no', 'mr_date', 'vendor_id', 'net_amt', 'payment_mode', 'created_id');
    $obj->eColumns 		= array('id');
    $obj->sIndexColumn 	= "id";
    $obj->sTable 		= "mr";
    $obj->myWhere 		= " WHERE company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
    $obj->groupBy       = ' GROUP BY mr.mr_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)) {

        $mr = mysqli_query($conn, "SELECT sum(net_amt) AS net_amount FROM `mr` WHERE `mr_no` = '".$aRow['mr_no']."'");
        $mr_row = mysqli_fetch_assoc($mr);


        $temp_arr = array();
        $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[] = $aRow['mr_no'];
		$temp_arr[] = date("d-m-Y", strtotime($aRow['mr_date']));
		$temp_arr[] = $mr_row['net_amount'];
        $temp_arr[] = $aRow['payment_mode'];
        $temp_arr[] = @$master_user[$aRow['created_id']];
        
        if($aRow['cancel_flag'] == 'Y'){
            $style = "display:none";
        }else{
            $style = "display:-webkit-inline-box";
        }

        $str = '';

        if($aRow['cancel_flag'] != 'Y'){
    		if( hasPermission($_SESSION['user_detail']['role_id'], 'mr_edit') ) {
    		$str .= '<a href="index.php?page=mr/edit.php&eid='.$aRow['mr_no'].'" name="edit" class="btn btn-primary" data-toggle="tooltip" data-placement="top" title="EDIT MR...!!"><em class="fa fa-pencil"></em></a>&nbsp;';
    		}
        $str .= '<a href="mr/gg_mr.php?eid='.$aRow['mr_no'].'" name="edit" class="btn btn-primary" data-toggle="tooltip" data-placement="top" title="MR PRINT...!!" target="_blank"><em class="fa fa-print"></em></a>&nbsp;';
        }else{
            $str .= '<label class="cancel-flag">CANCELED</label>&nbsp;';
        }

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

        $str .= '<button type="button" class="btn btn-danger delete_data" id="'.$aRow["mr_no"].'" data-toggle="tooltip" data-placement="top" title="CANCEL MR..!!" onclick="CancleFlag(this);" style="'.$style.'"><em class="fa fa-times"></em></button>';

		$temp_arr[] = $str;

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

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