<?php
require_once '../../Server/connection.php';

## Read value
$draw = $_POST['draw'];
$row = $_POST['start'];
$rowperpage = $_POST['length']; // Rows display per page
$columnIndex = $_POST['order'][0]['column']; // Column index
$columnName = $_POST['columns'][$columnIndex]['data']; // Column name
$columnSortOrder = $_POST['order'][0]['dir']; // asc or desc
$searchValue = $_POST['search']['value']; // Search value

## Custom Field value
$searchByExpCate = $_POST['searchByExpCate'];
$searchByCurrencyName = $_POST['searchByCurrencyName'];
$searchByYear = $_POST['searchByYear'];
$searchByMonth1 = $_POST['searchByMonth'];
$searchByDate = $_POST['searchByDate'];
$searchByDate11 = date('Y-m-d', strtotime($searchByDate));
//$searchByStaffCurrent = 1;
## Search 




if($searchByExpCate != ''){
    $searchQuery .= " and (expenses.expCate_id='".$searchByExpCate."') ";
}

if($searchByCurrencyName != ''){
    $searchQuery .= " and (expenses.currency_id='".$searchByCurrencyName."') ";
}

if($searchByYear != ''){
    $searchQuery .= " and (expenses.year='".$searchByYear."') ";
}


if($searchByMonth1 != ''){
	
	
	if($searchByMonth1==1){
	 $searchByMonth = 'JAN';
	 $searchQuery .= " and (expenses.month='".$searchByMonth."') ";
	  }else if($searchByMonth1==2){
	 $searchByMonth = 'FEB';	
     $searchQuery .= " and (expenses.month='".$searchByMonth."') ";	 
	  }else if($searchByMonth1==3){
	$searchByMonth = 'MARCH';
    $searchQuery .= " and (expenses.month='".$searchByMonth."') ";	
	  }else if($searchByMonth1==4){
	$searchByMonth = 'APR';	
    $searchQuery .= " and (expenses.month='".$searchByMonth."') ";	
	  }else if($searchByMonth1==5){
	$searchByMonth = 'MAY';	
    $searchQuery .= " and (expenses.month='".$searchByMonth."') ";	
	  }else if($searchByMonth1==6){
	$searchByMonth = 'JUNE';
    $searchQuery .= " and (expenses.month='".$searchByMonth."') ";	
	  }else if($searchByMonth1==7){
	$searchByMonth = 'JULY';
    $searchQuery .= " and (expenses.month='".$searchByMonth."') ";	
	  }else if($searchByMonth1==8){
	$searchByMonth = 'AUG';
    $searchQuery .= " and (expenses.month='".$searchByMonth."') ";	
	  }else if($searchByMonth1==9){
	$searchByMonth = 'SEPT';
    $searchQuery .= " and (expenses.month='".$searchByMonth."') ";	
	  }else if($searchByMonth1==10){
	$searchByMonth = 'OCT';
    $searchQuery .= " and (expenses.month='".$searchByMonth."') ";	
	  }else if($searchByMonth1==11){
	$searchByMonth = 'NOV';
    $searchQuery .= " and (expenses.month='".$searchByMonth."') ";	
	  }else{
	$searchByMonth = 'DEC';	
    $searchQuery .= " and (expenses.month='".$searchByMonth."') ";	
	  }
	
	
}


if($searchByDate != ''){
    $searchQuery .= " and (expenses.date='".$searchByDate11."') ";
}




## Total number of records without filtering
$sel = mysqli_query($conn,"select count(*) as allcount from expenses");
$records = mysqli_fetch_assoc($sel);
$totalRecords = $records['allcount'];

## Total number of records with filtering
$sel = mysqli_query($conn,"select count(*) as allcount from expenses where 1 ".$searchQuery);
$records = mysqli_fetch_assoc($sel);
$totalRecordwithFilter = $records['allcount'];

## Total number of records with filtering
//$sel7 = mysqli_query($conn,"select sum(amount_paid) as total from expenses,invoices WHERE collected_payments.inv_id=invoices.inv_id and 1 ".$searchQuery);
//$records7 = mysqli_fetch_assoc($sel);
//$totalAmountwithFilter7 = $records7['total'];

## Fetch records
$empQuery = "select * from expenses WHERE 1 ".$searchQuery." order by expenses.exp_id desc limit ".$row.",".$rowperpage;
//$empQuery = "select * from collected_payments,invoices WHERE collected_payments.inv_id=invoices.inv_id and 1 ".$searchQuery." order by ".$columnName." ".$columnSortOrder." limit ".$row.",".$rowperpage;
$empRecords = mysqli_query($conn, $empQuery);
$data = array();
$x = 1;
while ($row = mysqli_fetch_assoc($empRecords)){
	
	$categoriesId = $row['exp_id'];
	$exp_desc = $row['exp_desc'];
	$currencyId = $row['currency_id'];
	$amount_used = number_format($row['amount'],2);
	$expdate = $row['date'];
	$expensesDate = date('d-m-Y', strtotime($expdate));
	$space = " ";
	
	$sql8120 = "SELECT * FROM currency where currency_id='$currencyId'";
			$result8120 = $conn->query($sql8120);
				while($data8120 = mysqli_fetch_array($result8120))
							{
							$currencyName = $data8120['currencyName']; 
							}
							
	$tt = $amount_paid.$space.$currencyName;
	

	
	
	$button = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="editExpenses.php?i='.$categoriesId.'" id="editOrderModalBtn"> <center><i class="glyphicon glyphicon-edit"></i></center></a>
	</div>';
	
	
	
	
	
	$button4 = '<!-- Single button -->
	<div class="btn-group">
	<a type="button" class="btn btn-primary" data-toggle="modal" data-target="#removeCategoriesModal" id="removeCategoriesModalBtn" onclick="removeCategories('.$categoriesId.')"> <i class="glyphicon glyphicon-trash"></i></a>
	
	</div>';
	
	
	
    $data[] = array(
    		"exp_id"=>$x,
			"AmountUsed"=>$amount_used,
			"In"=>$tt,
			"usedFor"=>$exp_desc,
			"expensesDate"=>$expensesDate,
			"edit"=>$button,
			"delete"=>$button4
    	);
		$x++;
		
	
		
		
		
}




## Response
$response = array(
    "draw" => intval($draw),
	//"iTotalRecords" => $totalAmountwithFilter7,
    "iTotalRecords" => $totalRecords,
    "iTotalDisplayRecords" => $totalRecordwithFilter,
	//"iTotalAmount" => $totalAmountwithFilter7,
    "aaData" => $data
);

echo json_encode($response);
