<?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	=>	'ModePayment',
	7	=>	'Transcode',
	8	=>	'Remarks',
	9	=>	'OrNum',
	10	=>	'ChequeProcessDate',
	11	=>	'ChequeStatus',
	12	=>	'ReturnRemarks',
	13  =>  'PenaltyStatus',
	14  =>  'PenaltyStatus'  
);    
$resultQuery 		= "SELECT Id, RefId, ReservationId, ChequeDate, ChequeNo, Bank, Amount, Remarks, ChequeStatus, ChequeProcessDate, ReplaceDate, ReturnRemarks, OrNum, ReplaceOption, ModePayment, Transcode, PenaltyStatus FROM buyer_cheque_monitoring  "; 
//$sql			=	$resultQuery."  WHERE Transcode IN ('Downpayment', 'Equity', 'Others') AND Transcode IS NOT NULL AND Status = 'active' AND ReservationId = '$reserveId' ";
$sql			=	$resultQuery."  WHERE Transcode IN ('Downpayment', 'Equity', 'Others', 'Full Payment') AND Transcode IS NOT NULL AND Status = 'active' AND ReservationId = '$reserveId' "; //8MAY25 added Full Payment in condition for Transcode
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 Transcode LIKE '%".$ind."%' "; 
		$sql.=" OR ReturnRemarks LIKE '%".$ind."%' "; 
		$sql.=" OR ModePayment LIKE '%".$ind."%' "; 
		$sql.=" OR PenaltyStatus LIKE '%".$ind."%' ";
		$sql.=" OR UPPER(DATE_FORMAT(ChequeProcessDate,'%m/%d/%Y')) LIKE '%".$ind."%' ) ";   
	}
}

$mecon->query($sql);
$result			= $mecon->resultset(); 
$totalData		= count($result);
$totalFiltered	=	$totalData;
$checkStatus 	= "";
$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();
			$toolbar = '<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;';
			$toolbar .= '<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;';
			$toolbar .= '<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>&nbsp;&nbsp;';

			if(strtolower($result[$a]['ModePayment'])=='cheque'){
				$toolbar .= '<a href="index.php?pg=payment&type=postdated_checks_received_past_due&rid='.$result[$a]['ReservationId'].'&cmid='.$result[$a]['Id'].'"><span class="btn btn-xs btn-warning glyphicon glyphicon-list" title="Past Due History"></span></a>';
			}else{
				$toolbar .= '<span class="btn btn-xs btn-warning glyphicon glyphicon-list" title="Past Due History" style="cursor:not-allowed;" ></span>';
			}
			 
			$nestedData[] = $toolbar;

	$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[] = wordwrap($result[$a]['Bank'],20,"<br>\n");
	$nestedData[] = $result[$a]['ChequeNo']; 
	$nestedData[] = number_format($result[$a]['Amount'],2);
	$nestedData[] = strtoupper($result[$a]['ModePayment']); 
	$nestedData[] = strtoupper($result[$a]['Transcode']); 
	$nestedData[] = '<span style="word-wrap:break-word; word-break:break-all; ">'.wordwrap(strtoupper($result[$a]['Remarks']),20,"<br>\n").'</span>';



	$nestedData[] = strtoupper($result[$a]['OrNum']); 
	$nestedData[] = (($result[$a]['ChequeProcessDate']=='1970-01-01') || ($result[$a]['ChequeProcessDate']=='')) ? "" : date('m/d/Y', strtotime($result[$a]['ChequeProcessDate'])); 

	if(strtoupper($result[$a]['ChequeStatus'])=="RETURNED"){
		$checkStatus = "RETURNED DUE TO DAIF";
	}else if(strtoupper($result[$a]['ChequeStatus'])=="CLOSED"){
		$checkStatus = "RETURNED DUE TO ACCOUNT CLOSED";
	}else{
		$checkStatus = strtoupper($result[$a]['ChequeStatus']);
	} 
	$nestedData[] = $checkStatus;
	$nestedData[] = '<span style="word-wrap:break-word; word-break:break-all; ">'.wordwrap(strtoupper($result[$a]['ReturnRemarks']),20,"<br>\n").'</span>';

	if(strtolower($result[$a]['PenaltyStatus'])=='unpaid' && strtolower($result[$a]['ChequeStatus'])=='returned'){
		$nestedData[] = '<span style="color:#ff0000">'.strtoupper($result[$a]['PenaltyStatus']).'</span>';
	}else if(strtolower($result[$a]['PenaltyStatus'])=='unpaid' && strtolower($result[$a]['ChequeStatus'])=='closed'){ //29DEC24
		$nestedData[] = '<span style="color:#ffffff">'.strtoupper($result[$a]['PenaltyStatus']).'</span>';
	}else{
		$nestedData[] = strtoupper($result[$a]['PenaltyStatus']);	
	}
	$nestedData[] = strtoupper($result[$a]['PenaltyStatus']);	

			$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();