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


	$obj = new Datatable($conn);

	$obj->aColumns 		= array('general_purchase_new.id', 'general_purchase_new.vendor_id', 'general_purchase_new.bill_no','general_purchase_new.gp_img', 'general_purchase_new.bill_date', 'general_purchase_new.net_amt', 'general_purchase_new.gross_amt', 'general_purchase_new.created_id','act_master.ac_name AS aname');

    $obj->eColumns 		= array('general_purchase_new.id');
    $obj->sIndexColumn 	= "general_purchase_new.id";
    $obj->sTable 		= "general_purchase_new";
    $obj->myWhere 		= "JOIN act_master ON general_purchase_new.vendor_id = act_master.act_code   WHERE general_purchase_new.company_id = '".$_SESSION['company_id']."' AND general_purchase_new.branch_id = '".$_SESSION['branch_id']."' AND DATE(general_purchase_new.bill_date)>='".$_SESSION['from_year']."' AND DATE(general_purchase_new.bill_date)<='".$_SESSION['to_year']."'";
    $obj->groupBy       = 'GROUP BY general_purchase_new.bill_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();
        $party      = "SELECT * FROM `act_master` WHERE `act_code` = '".$aRow['vendor_id']."'";
        $party_res  = mysqli_query($conn,$party);
        $party_row  = mysqli_fetch_array($party_res);

		$temp_arr[] = ++$cou;
		$temp_arr[] = $party_row['ac_name'];
		$temp_arr[] = $aRow['bill_no'];
		$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']];
        $temp_arr[] = '<a href="assets/upload/general_purchase_img/'.$aRow['gp_img'].'">View</a>';

		$str = '';
		if( hasPermission($_SESSION['user_detail']['role_id'], 'general_purchase_edit') ) {
		$str .= '<a href="index.php?page=general_purchase/edit.php&eid='.$aRow['id'].'" name="edit" class="btn btn-primary" data-toggle="tooltip" data-placement="top" title="EDIT...!!" style='.$display.'><em class="fa fa-pencil"></em></a>&nbsp;';
		}

		

		if( hasPermission($_SESSION['user_detail']['role_id'], 'general_sales_delete') ) {
		$str .= '<button type="button" class="btn btn-danger delete_data" id="'.$aRow["id"].'" data-toggle="tooltip" data-placement="top" title="DELETE...!!" 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();
?>