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


	$obj = new Datatable($conn);

	$obj->aColumns 		= array('invoice_no', 'v_date', 'flag', 'account_name', 'amount', 'created_id');
    $obj->eColumns 		= array('id');
    $obj->sIndexColumn 	= "id";
    $obj->sTable 		= "opening_balance";
    $obj->myWhere 		= " WHERE company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' AND DATE(created_datetime)>='".$_SESSION['from_year']."' AND DATE(created_datetime)<='".$_SESSION['to_year']."'";
    $obj->groupBy       = ' GROUP BY opening_balance.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();
        
        $ac     = "SELECT * FROM `act_master` WHERE `act_code` ='".$aRow['account_name']."'";
	    $ac_res = mysqli_query($conn,$ac);
	    $ac_row = mysqli_fetch_array($ac_res);

		$temp_arr[] = ++$cou;
		$temp_arr[] = $aRow['invoice_no'];
		$temp_arr[] = date("d-m-Y", strtotime($aRow['v_date']));
		$temp_arr[] = $ac_row['ac_name'];
		$temp_arr[] = $aRow['flag'];
		$temp_arr[] = $aRow['amount'];
		$temp_arr[] = @$master_user[$aRow['created_id']];
		
		
		$str = '';
		if( hasPermission($_SESSION['user_detail']['role_id'], 'ob_edit') ) {
		$str .= '<a href="index.php?page=opening_balance/add.php&eid='.$aRow['id'].'" name="edit" class="btn btn-primary" data-toggle="tooltip" data-placement="top" title="EDIT OPENING BALANCE...!!"><em class="fa fa-pencil"></em></a>&nbsp;';
		}

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

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

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