<?php 
//error_reporting(-1);
include_once "session.php";

$requestData= $_REQUEST;
$stt = $requestData['start'];
$len = $requestData['length'];
 
$data = array();

if($_REQUEST['pageaction'] == "rpt_pur_payment_list"){	
	include "includes/Mdme-functions.php";		
	// check permission	
	$mdme = getMdme_Rpt_Purchase_Payment_List($db,''); 
	include_once "includes/pagepermission.php";	
						
	$dispFields = array("p_payment_id", "disp_created_date", "company_name", "sup_name", "payment_type", "amount_recv");
			
	$order_clmn = $requestData['order'][0]['column'];
	$order_oper = $requestData['order'][0]['dir'];
	
	if($order_clmn == 1){
		$ordr = " order by t1.createddate $order_oper ";
	}	
	else{
		$ordr = " order by $dispFields[$order_clmn] $order_oper ";
	}
	
	$whrcon = "";
	/*
	if(isset($_REQUEST['srch_from']) && !empty($_REQUEST['srch_from'])){
		$cv_srch_from = date("Y-m-d", strtotime($_REQUEST['srch_from']));
		$whrcon .= " and DATE_FORMAT(t1.createddate,'%Y-%m-%d') >= '".$cv_srch_from."' ";
	}
	if(isset($_REQUEST['srch_to']) && !empty($_REQUEST['srch_to'])){
		$cv_srch_to = date("Y-m-d", strtotime($_REQUEST['srch_to']));
		$whrcon .= " and DATE_FORMAT(t1.createddate,'%Y-%m-%d') <= '".$cv_srch_to."' ";
	}
	*/	
	
	$cond_company='';
	if(isset($srch_company) && !empty($srch_company)){
		$cond_company=" and t1.company_id ='".$srch_company."' ";
	}
	
	$cond_supplier='';
	if(isset($srch_supplier) && !empty($srch_supplier)){
		$cond_supplier=" and t1.sup_id ='".$srch_supplier."' ";
	}	
  			
$str_all = " SELECT t1.*, t2.sup_name, t3.company_name ,t4.payment_type, DATE_FORMAT(t1.paid_on,'%d-%b-%Y') AS disp_paid_on, DATE_FORMAT(t1.cheque_issue_date,'%d-%b-%Y') AS disp_cheque_issue_date, DATE_FORMAT(t1.cleared_date,'%d-%b-%Y') AS disp_cleared_date, 
DATE_FORMAT(t1.createddate,'%d-%b-%Y') AS disp_created_date FROM `t_suppliers_payments` t1
inner join m_supplier t2 on t1.`sup_id`=t2.`sup_id`
inner join m_company t3 on t1.`company_id`=t3.`company_id`
inner join status_payment_type t4 on t1.`payment_type_id`=t4.`payment_type_id`  where 1=1 and t1.IsActive=1 ".$whrcon." ".$cond_company."  ".$cond_supplier." ";  
	
	
	//search	
	if(!empty($requestData['search']['value'])){
		$wrcon = " and (t1.p_payment_id like '%".$requestData['search']['value']."%' OR t2.sup_name like '%".$requestData['search']['value']."%' OR t3.company_name like '%".$requestData['search']['value']."%' OR t4.payment_type like '%".$requestData['search']['value']."%' )";
	
		$str_all .= $wrcon;
	}	
		
	if(trim($ordr) != "")
		$str_all .= $ordr;	
			
	$get_tota = $db->get_rsltset($str_all);
	$totalData = count($get_tota);
	$totalFiltered = $totalData;	
		
	$str_all .= " limit ".$stt.",".$len;		
	
    $rslt_data =  $db->get_rsltset($str_all);	 		

   	foreach($rslt_data as $r){
		$nestedData=array();
		$editid = base64_encode($r['p_payment_id']);
		$actmodul = $_REQUEST['pageaction'];
									
		foreach ($dispFields as $dispFields_S) {
			$nestedData[] = $r[$dispFields_S];		
		}
		
		/*
		$disp_cleared_html='';	
		if($r['IsActive'] == "1"){
			if($r['cleared_status'] == "2"){
				$disp_cleared_html = 'Cleared '.$r['disp_cleared_date']; 			
			}
			else{				
				$disp_cleared_html = '<a target="_blank" href="'.$manage_mem_payments_url.''.$editid.'&popup=1&payment_id='.$r['payment_id'].'">Not Cleared </a>'; 
			}
		}
		else{
			$disp_cleared_html='[DELETED]';
		}
			
		
		
		$nestedData[] = $disp_cleared_html;
		$nestedData[] = $r['user_firstname'];
		*/
		
		$disp_payment_det_html='';
		if($r['payment_type_id'] == "1"){
			//cash
			$disp_payment_det_html='Paid on - '.$r['disp_paid_on'];
		}
		else if($r['payment_type_id'] == "2"){
			//cheque
			$disp_payment_det_html='Cheque issue dt - '.$r['disp_cheque_issue_date'].' <br/> Cleared dt - '.$r['disp_cleared_date'];
		}
		else if($r['payment_type_id'] == "3"){
			//netbanking
			$disp_payment_det_html='Transfer dt - '.$r['disp_cheque_issue_date'];
		}
		
		
		$nestedData[]=$disp_payment_det_html;
		
				
		$data[] = $nestedData;			
	} 	
}
else{
	echo "No-Data";
}

$json_data = array(
				"draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. 
				"recordsTotal"    => intval( $totalData ),  // total number of records
				"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
				"data"            => $data   // total data array			
);	

echo json_encode($json_data);  // send data as json format

?>