<?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'] : "";
$cmid			= (isset($_GET['cmid'])) ? $_GET['cmid'] : "";
$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, ChequeMonitorId, OrNum, ReplaceOption, ModePayment, Transcode, PenaltyStatus FROM buyer_cheque_monitoring_past_due "; 

  //$sql			=	$resultQuery."  WHERE Transcode NOT IN ('Downpayment', 'Equity') AND Status = 'active' AND ReservationId = '$reserveId' AND ChequeMonitorId = '$cmid' ";
$sql			=	$resultQuery."  WHERE Transcode NOT IN ('Downpayment') AND Status = 'active' AND ReservationId = '$reserveId' AND ChequeMonitorId = '$cmid' "; //9JUL25

 

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;

$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[] = '<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;<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;<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[] = wordwrap($result[$a]['Bank'],20,"<br>\n");
	$nestedData[] = $result[$a]['ChequeNo']; 
	$nestedData[] = number_format($result[$a]['Amount'],2);

	if(strtoupper($result[$a]['ModePayment'])=="OVER_THE_COUNTER_DEPOSIT"){
			$mode_payment = "OVER THE COUNTER DEPOSIT";
	}else{
			$mode_payment = strtoupper($result[$a]['ModePayment']);
	}
	
	$nestedData[] = $mode_payment; 

if(strtoupper($result[$a]['Transcode'])=="PENALTY"){
	$transcode = "PENALTY & INTEREST";
}else{
	$transcode = strtoupper($result[$a]['Transcode']); 
}

	$nestedData[] = $transcode; 
	$nestedData[] = '<span style="word-wrap:break-word; word-break:break-all; font-size:8pt">'.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'])); 

	$nestedData[] = strtoupper($result[$a]['ChequeStatus']);
	//$nestedData[] = '<span style="word-wrap:break-word; word-break:break-all; font-size:8pt">'.wordwrap(strtoupper($result[$a]['ReturnRemarks']),20,"<br>\n").'</span>';

	//RETURNED CHECK REASON START 27JUL25 
	$nestedData[] = '<input type="text" class="form-control" style="width:250px !important" onchange="updateReturnRemarks(this,'.$result[$a]['Id'].');" value="'.wordwrap(strtoupper($result[$a]['ReturnRemarks']),20,"<br>\n").'">';
	//RETURNED CHECK REASON END 27JUL25

	//$nestedData[] = strtoupper($result[$a]['PenaltyStatus']);

if(strtoupper($result[$a]['Transcode'])!="EQUITY" && strtoupper($result[$a]['Transcode'])!="OTHERS"){
	 $penaltystatus = '<select class="form-control" style="width:100px" id="select'.$result[$a]['Id'].'" onchange="updatePenaltyStatus('.$result[$a]['ReservationId'].','.$result[$a]['Id'].',\''.$result[$a]['Transcode'].'\',\''.$result[$a]['ChequeStatus'].'\');" >';

	 $unpaidStat 	= (strtolower($result[$a]['PenaltyStatus'])=='unpaid') ? 'selected' : '';
	 $paidStat 		= (strtolower($result[$a]['PenaltyStatus'])=='paid') ? 'selected' : '';

	 $penaltystatus .= '<option value="Unpaid" '.$unpaidStat.'>Unpaid</option>';
	 $penaltystatus .= '<option value="Paid" '.$paidStat.'>Paid</option>';
	 $penaltystatus .= '</select>';
	 $nestedData[] = $penaltystatus;	
	 $nestedData[] = $result[$a]['PenaltyStatus'];	
}else{
	$nestedData[] = "";
	$nestedData[] = "";
} 
	
			$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();