<?php
error_reporting(0);
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start();
session_start();  
include_once('../config/minho.php');
include_once('../config/autoloadClass.php');
$autoload		=	new autoloadClass(); 
$mecon			=	new condbClass();
$requestData	=	$_REQUEST; 
$reserveId	= (isset($_GET['rid'])) ? $_GET['rid'] : "";
$columns = array(
	0	=>	'RefId',
	1	=>	'Id',
	2	=>	'ChequeDate',
	3	=>	'Bank',
	4	=>	'ChequeNo',
	5	=>	'Amount',
	6	=>	'Remarks',
	7	=>	'OrNum',
	8	=>	'ChequeProcessDate',
	9	=>	'ChequeStatus',
	10	=>	'ReturnRemarks',
	11	=>	'ModePayment' 
);    
$resultQuery 		= "SELECT Id, RefId, ReservationId, ChequeDate, ChequeNo, Bank, Amount, Remarks, ChequeStatus, ChequeProcessDate, ReplaceDate, ReturnRemarks, OrNum, ReplaceOption, ModePayment FROM buyer_cheque_monitoring  "; 

$sql			=	$resultQuery."  WHERE Status = 'active'  AND ReservationId = '$reserveId' ";
if(!empty($requestData['search']['value']) ) { 
	$arr = explode(" ",$requestData['search']['value']);  
	foreach($arr as $ind){
		$ind = str_replace(',','',$ind);
		$sql.=" AND (UPPER(DATE_FORMAT(ChequeDate,'%m/%d/%Y')) LIKE '%".$ind."%' "; 
		$sql.=" OR ChequeNo LIKE '%".$ind."%' "; 
		$sql.=" OR Bank LIKE '%".$ind."%' "; 
		$sql.=" OR Amount LIKE '%".$ind."%' "; 
		$sql.=" OR Remarks LIKE '%".$ind."%' "; 
		$sql.=" OR OrNum LIKE '%".$ind."%' "; 
		$sql.=" OR ChequeStatus LIKE '%".$ind."%' "; 
		$sql.=" OR ReturnRemarks LIKE '%".$ind."%' "; 
		$sql.=" OR ModePayment LIKE '%".$ind."%' "; 
		$sql.=" OR UPPER(DATE_FORMAT(ChequeProcessDate,'%m/%d/%Y')) LIKE '%".$ind."%' ) ";   
	}
}

$mecon->query($sql);
$result			= $mecon->resultset(); 
$totalData		= count($result);
$totalFiltered	=	$totalData;

$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']."   "; 

$mecon->query($sql);
$data = array();
$result = $mecon->resultset();

		for($a=0; $a<count($result); $a++){
			$nestedData=array();
			$nestedData[] ='';
			//$nestedData[] = '<span onclick="updatePDC('.$result[$a]['ReservationId'].','.$result[$a]['Id'].');" class="btn btn-xs btn-primary glyphicon glyphicon-pencil" title="Updated Postdated Checks Received for Equity"></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span onclick="deletePDC('.$result[$a]['Id'].');" class="btn btn-xs btn-danger glyphicon glyphicon-trash" title="Delete Postdated Checks Received for Equity"></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span onclick="updatePDCStatus('.$result[$a]['ReservationId'].','.$result[$a]['Id'].')" class="btn btn-xs btn-info glyphicon glyphicon-time" title="Change Status of Postdated Checks Received for Equity"></span>';
	$nestedData[] = $a+1; 
	
	if((strtoupper($result[$a]['ChequeStatus'])=="REPLACED" && strtoupper($result[$a]['ReplaceOption'])=="CASH") || (strtoupper($result[$a]['ChequeStatus'])=="REPLACED" && strtoupper($result[$a]['ReplaceOption'])=="CHEQUE")){
		$nestedData[] = (($result[$a]['ReplaceDate']=='1970-01-01') || ($result[$a]['ReplaceDate']=='')) ? "" : date('m/d/Y', strtotime($result[$a]['ReplaceDate'])); 
	}else{
		$nestedData[] = (($result[$a]['ChequeDate']=='1970-01-01') || ($result[$a]['ChequeDate']=='')) ? "" : date('m/d/Y', strtotime($result[$a]['ChequeDate'])); 
	}
	 
	$nestedData[] = $result[$a]['Bank'];
	$nestedData[] = $result[$a]['ChequeNo']; 
	//$nestedData[] = number_format($result[$a]['Amount'],2);
	$nestedData[] = '';
	$nestedData[] = strtoupper($result[$a]['Remarks']); 
	$nestedData[] = strtoupper($result[$a]['OrNum']); 
	$nestedData[] = (($result[$a]['ChequeProcessDate']=='1970-01-01') || ($result[$a]['ChequeProcessDate']=='')) ? "" : date('m/d/Y', strtotime($result[$a]['ChequeProcessDate'])); 
	$nestedData[] = strtoupper($result[$a]['ChequeStatus']);
	$nestedData[] = strtoupper($result[$a]['ReturnRemarks']);
	$nestedData[] = strtoupper($result[$a]['ModePayment']); 
			$data[] = $nestedData;
		} 
$json_data = array(
			"draw"            => intval( $requestData['draw'] ),
			"recordsTotal"    => intval( $totalData ),
			"recordsFiltered" => intval( $totalFiltered ),
			"data"            => $data
			);
			 
echo json_encode($json_data);
exit();
ob_end_flush();