<?php 
include 'session.php';

extract($_REQUEST);



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

if($_REQUEST['pageaction'] == "purchase_listing"){
	include "includes/Mdme-functions.php";	
	$mdme = getMdmePurchaseEntry($db,'');	
	include_once "includes/pagepermission.php";	
	include_once('includes/display-functions.php');

		
	$cond_subtbl = ""; $cond_mntbl = "";
	if(isset($_REQUEST['srch_type']) && $_REQUEST['srch_type'] !="" ){		
				
		if($_REQUEST['srch_type'] == "pdt_sku"){
			if(isset($_REQUEST['srch_keyword']) && $_REQUEST['srch_keyword'] !="" ){
				$cond_subtbl ="  and (pdt_sku like '%".$_REQUEST['srch_keyword']."%') ";
			}			
		}
		else if($_REQUEST['srch_type'] == "inv_no"){
			if(isset($_REQUEST['srch_keyword']) && $_REQUEST['srch_keyword'] !="" ){
				$cond_mntbl ="  and (t1.actu_inv_no like '%".$_REQUEST['srch_keyword']."%' ) ";
			}				
		}
		else if($_REQUEST['srch_type'] == "ref_inv_no"){
			if(isset($_REQUEST['srch_keyword']) && $_REQUEST['srch_keyword'] !="" ){
				$cond_mntbl ="  and (t1.ref_inv_no like '%".$_REQUEST['srch_keyword']."%') ";
			}	
		}		
	} 
		
	$cond_finyear = "";	
	if(isset($_REQUEST['srch_finyear']) && $_REQUEST['srch_finyear'] !="" ){	
		$cond_finyear ="  and (t1.financial_id='".$_REQUEST['srch_finyear']."') ";
	} 
		
	$cond_company = "";	
	if(isset($_REQUEST['srch_company']) && $_REQUEST['srch_company'] !="" ){	
		$cond_company ="  and (t1.company_id='".$_REQUEST['srch_company']."') ";
	} 
		
	$cond_supplier = "";	
	if(isset($_REQUEST['srch_supplier']) && $_REQUEST['srch_supplier'] !="" ){	
		$cond_supplier ="  and (t1.sup_id='".$_REQUEST['srch_supplier']."') ";
	}
		
	$whrcon = "";
	if(isset($_REQUEST['srch_fmdate']) && $_REQUEST['srch_fmdate'] !="" )
	{
		$whrcon .= " and CAST(t1.p_invoice_date as date) >=  cast(DATE_FORMAT(STR_TO_DATE('".$_REQUEST['srch_fmdate']."','%d-%m-%Y'), '%Y-%m-%d') as date) ";
	}
	if(isset($_REQUEST['srch_todate']) && $_REQUEST['srch_todate'] !="" )
	{
		$whrcon .= " and CAST(t1.p_invoice_date as date) <= cast(DATE_FORMAT(STR_TO_DATE('".$_REQUEST['srch_todate']."','%d-%m-%Y'), '%Y-%m-%d') as date) ";
	}
// 	echo 'hi';
// exit;
	$str_all = "select t1.*,DATE_FORMAT(t1.p_invoice_date,'%d-%m-%Y') AS disp_inv_date, t1.IsActive AS status, t2.company_name, t3.sup_name, t3.sup_state, t3.sup_city, t3.sup_address, t3.sup_gstno, t3.sup_postcode
	from t_purchase_entry_main t1 
	inner join m_company t2 on t1.company_id=t2.company_id
	inner join m_supplier t3 on t1.sup_id = t3.sup_id where 1=1 and t1.IsActive <>2 and t1.p_invoice_no IN	
	(
		select p_invoice_no from t_purchase_entry_details where IsActive <>2 ".$cond_subtbl."
	) ".$cond_mntbl."  ".$cond_finyear." ".$cond_company." ".$cond_supplier."  ". $whrcon."  order by t1.p_invoice_no desc  ";
	
	// print_r($str_all);
	// exit;
	
	$get_tota = $db->get_rsltset($str_all);
	$totalData = count($get_tota);
	$totalFiltered = $totalData;	
		
	$str_all .= " limit ".$stt.",".$len;		
	
    $rslt_info =  $db->get_rsltset($str_all);
		

    foreach($rslt_info as $rs){
		$nestedData=array();

		$editid = base64_encode($rs['p_invoice_no']);
		$stats = $rs['status'];					
					
		$pdt_skus = $db->get_rsltset("SELECT purchase_rate, pdt_sku FROM t_purchase_entry_details WHERE p_invoice_no = '".$rs['p_invoice_no']."'");
		
            $pdt_values = array(); 
            $pdt_amount = array();
            
            foreach ($pdt_skus as $pdt_sku) {
                $pdt_values[] = $pdt_sku['pdt_sku'];
            	$pdt_amount[] = $pdt_sku['purchase_rate'];
            }
            
            // Convert the array of values to a comma-separated string
            $pdt_csv = implode(', ', $pdt_values);
            $pdt_amt = implode(', ', $pdt_amount);

		if( $stats == '1') 

		 {
			//change status active to inactive
			$statusurl = "'purchase_actions.php','Id=$editid&action=changestatus&actval=0'";

		   $incstat =  '<div class="btn-group" data-toggle="btn-toggle" >
				<button class="btn btn-success btn-sm active" type="button"><i class="fa fa-square text-green"></i> Active</button>
				<button class="btn btn-default btn-sm" type="button" onclick="funchangestatus(this,'.$statusurl.');"><i class="fa fa-square text-red"></i> &nbsp;</button>
				</div>'	;				
		 } else {
			//change status inactive to active
			$statusurl = "'purchase_actions.php','Id=$editid&action=changestatus&actval=1'";
			
			 $incstat =  '<div class="btn-group" data-toggle="btn-toggle" >
				<button class="btn btn-default btn-sm" type="button" onclick="funchangestatus(this,'.$statusurl.');"><i class="fa fa-square text-green"></i> &nbsp;</button>
				<button class="btn btn-danger btn-sm active" type="button"><i class="fa fa-square text-red"></i> InActive</button>
			</div>'	;	
		}

			
		
		$edit_html = '<a href="purchase_form.php?act=edit&id='.$editid.'" title="edit" data-toggle="tooltip" class="btn btn-info btn-sm"> <i class="fa fa-edit"></i> </a>';      
		       
		//del 
	    $delurl = "purchase_actions.php?Id=$editid&action=del";

		$del_html = '<a href="javascript:void(0);" title="delete" data-toggle="tooltip" class="btn btn-danger btn-sm" onClick="javascript:funStatsPurchase(this, \'' . $delurl . '\')"><i class="fa fa-times"></i></a>';	     
				
		$pdt_cnt =$db->get_a_line("select count(*) as disp_cnt from t_purchase_entry_details where 1=1 and IsActive=1 and p_invoice_no='".$rs['p_invoice_no']."' ");

		if(trim($res_modm_prm['EditPrm'])=="1") {
			$edit_html = $edit_html;
		  }else{
			$edit_html = "";			
		}
		 
		if(trim($res_modm_prm['DeletePrm'])=="1") {
			$del_html = $del_html;
		}else{
			$del_html = "";
		}			
			
		//get product count lists 		
		$pdt_sku_cnt = get_purchase_pdt_cnt_lists($db,$rs['p_invoice_no']);	
					
		
		$nestedData[]=$rs['p_invoice_no'];
		$nestedData[]=$rs['actu_inv_no'];
		$nestedData[]=$rs['disp_inv_date'];
		$nestedData[]=$rs['company_name'];
		$nestedData[]=$rs['sup_name']. '</br><span style="color:green; font-size:12px">' . $rs['sup_gstno'] . '</span>';
		$nestedData[] = $rs['sup_city'].'</br>'.$rs['sup_state'].'</br>'.$rs['sup_postcode']; 
		// $nestedData[] = $pdt_csv;
		// $nestedData[]=$rs['sup_city'];
		$nestedData[]=$pdt_cnt['disp_cnt'].'</br>(<span style="color:blue; font-size:12px">'.$pdt_csv.'</span>)';
		$nestedData[]=$rs['tot_pur_amount'];
 		
//  		$nestedData[]=$pdt_cnt['disp_cnt'];
		// $nestedData[]=$rs['discount_val'].' ('.$rs['discount_type'].')';
		// $nestedData[]=$rs['discount_amount'];
		// $nestedData[]=$rs['total_amount'];
		// $nestedData[]=$rs['cgst_total'].' ('.$rs['cgst_per'].')';
		// $nestedData[]=$rs['sgst_total'].' ('.$rs['sgst_per'].')';
		// $nestedData[]=$rs['igst_total'].' ('.$rs['igst_per'].')';		
		$nestedData[]=$rs['tot_tax_amt'];
		$nestedData[]=$rs['final_pur_amt'];
		$nestedData[] = $incstat;					
			
		$nestedData[] =  $edit_html.'&nbsp;'.$del_html;
		$nestedData[] = $pdt_amt;
		
		$data[] = $nestedData;
	}		
	
	
}

else if($_REQUEST['pageaction'] == "sales_listing"){
	
	include "includes/Mdme-functions.php";	
	$mdme = getMdmeSalesEntry($db,'');	
	include_once "includes/pagepermission.php";	
	include_once('includes/display-functions.php');
// echo 'hi';
// echo $_REQUEST['pdt_sku'];
		
	$cond_subtbl = ""; $cond_mntbl = "";
	if(isset($_REQUEST['srch_type']) && $_REQUEST['srch_type'] !="" ){		
				
		if($_REQUEST['srch_type'] == "pdt_sku"){
			if(isset($_REQUEST['srch_keyword']) && $_REQUEST['srch_keyword'] !="" ){
				$cond_subtbl ="  and (pdt_sku like '%".$_REQUEST['srch_keyword']."%') ";
			}			
		}
		else if($_REQUEST['srch_type'] == "bill_no"){
			if(isset($_REQUEST['srch_keyword']) && $_REQUEST['srch_keyword'] !="" ){
				$cond_mntbl ="  and (t1.actu_bill_no like '%".$_REQUEST['srch_keyword']."%' ) ";
			}				
		}		
	} 
		
	$cond_finyear = "";	
	if(isset($_REQUEST['srch_finyear']) && $_REQUEST['srch_finyear'] !="" ){	
		$cond_finyear ="  and (t1.financial_id='".$_REQUEST['srch_finyear']."') ";
	} 
		
	$cond_company = "";	
	if(isset($_REQUEST['srch_company']) && $_REQUEST['srch_company'] !="" ){	
		$cond_company ="  and (t1.company_id='".$_REQUEST['srch_company']."') ";
	} 
		
	$cond_customer = "";	
	if(isset($_REQUEST['srch_customer']) && $_REQUEST['srch_customer'] !="" ){	
		$cond_customer ="  and (t1.cus_id='".$_REQUEST['srch_customer']."') ";
	}
		
	$whrcon = "";
	if(isset($_REQUEST['srch_fmdate']) && $_REQUEST['srch_fmdate'] !="" )
	{
		$whrcon .= " and (t1.s_bill_date  >=  '".$_REQUEST['srch_fmdate']."') ";
	}
	if(isset($_REQUEST['srch_todate']) && $_REQUEST['srch_todate'] !="" )
	{
		$whrcon .= " and (t1.s_bill_date  <=  '".$_REQUEST['srch_todate']."') ";
	}
		
	$str_all = "select t1.*,DATE_FORMAT(t1.s_bill_date,'%d-%m-%Y') AS disp_bill_date,t1.IsActive AS status, t2.company_name, t3.cus_name, t3.cus_state, t3.cus_city,t3.cus_postcode,t3.cus_gstno from t_sales_entry_main t1 
	inner join m_company t2 on t1.company_id=t2.company_id
	inner join m_customer t3 on t1.cus_id = t3.cus_id where 1=1 and t1.IsActive <>2 and t1.s_bill_no IN		
	(
		select s_bill_no from t_sales_entry_details where IsActive <>2 ".$cond_subtbl."
	) ".$cond_mntbl."  ".$cond_finyear." ".$cond_company." ".$cond_customer."  ". $whrcon."  order by t1.s_bill_no desc  ";
		
	$get_tota = $db->get_rsltset($str_all);
	// print_r($str_all);
	// die();
	$totalData = count($get_tota);
	$totalFiltered = $totalData;	
		
	$str_all .= " limit ".$stt.",".$len;		
	
    $rslt_info =  $db->get_rsltset($str_all);
	
	// print_r($totalData);

    foreach($rslt_info as $rs){
		$nestedData=array();
			
		
		$editid = base64_encode($rs['s_bill_no']);
		$stats = $rs['status'];					

    	$pdt_skus = $db->get_rsltset("SELECT pdt_sku, tot_amount FROM t_sales_entry_details WHERE s_bill_no = 
    		'".$rs['s_bill_no']."'");
    	 
    	 $pdt_values = array(); 
         $pdt_amount = array();
        
        foreach ($pdt_skus as $pdt_sku) {
            $pdt_values[] = $pdt_sku['pdt_sku'];
        	$pdt_amount[] = $pdt_sku['tot_amount'];
        }
        
        // Convert the array of values to a comma-separated string
        $pdt_csv = implode(', ', $pdt_values);
        $pdt_amt = implode(', ', $pdt_amount);
		
		
///////////from here, newly added for act status					
		if( $stats == '1') 

		 {
			//change status active to inactive
			$statusurl = "'sales_actions.php','Id=$editid&action=changestatus&actval=0'";

		   $incstat =  '<div class="btn-group" data-toggle="btn-toggle" >
				<button class="btn btn-success btn-sm active" type="button"><i class="fa fa-square text-green"></i> Active</button>
				<button class="btn btn-default btn-sm" type="button" onclick="funchangestatus(this,'.$statusurl.');"><i class="fa fa-square text-red"></i> &nbsp;</button>
				</div>'	;				
		 } else {
			//change status inactive to active
			$statusurl = "'sales_actions.php','Id=$editid&action=changestatus&actval=1'";
			
			 $incstat =  '<div class="btn-group" data-toggle="btn-toggle" >
				<button class="btn btn-default btn-sm" type="button" onclick="funchangestatus(this,'.$statusurl.');"><i class="fa fa-square text-green"></i> &nbsp;</button>
				<button class="btn btn-danger btn-sm active" type="button"><i class="fa fa-square text-red"></i> InActive</button>
			</div>'	;	
		}

	

		//del 
		$delurl = "'sales_actions.php','Id=$editid&action=del'";
		
		
		$delstat='<a href="javascript:void(0);" title="delete" data-toggle="tooltip" class="btn btn-danger btn-sm" onClick="javascript:funStats(this,'.$delurl.')" ><span><i class="fa fa-times"style="font-size:15px"></i></span></a>';
		$inv_print='<a target="_blank" href="sales_invoice_print.php?s_bill_no='.$editid.'" title="Bill" class="btn btn-warning btn-sm"><span><i class="fa fa-print"></i></span></a>';	
			$edtstat='<a href="sales_form.php?act=edit&id='.$editid.'" title="edit" data-toggle="tooltip" class="btn btn-info btn-sm" ><i class="fa fa-edit"style="font-size:12px"></i></a>';
			$payment_link='<br/> <a target="_blank" href="salespayment_mng.php?customer_id='.base64_encode($rs['cus_id']).'&company_id='.base64_encode($rs['company_id']).'" title="invoice">Add Payment</a>';
			
			$pdt_cnt =$db->get_a_line("select count(*) as disp_cnt from t_sales_entry_details where 1=1 and IsActive=1 and s_bill_no='".$rs['s_bill_no']."' ");     
				
			if(trim($res_modm_prm['EditPrm'])=="1") {
				$edtstat = $edtstat;
			  }else{
				$edtstat = "";			
			  }
			  
			 if(trim($res_modm_prm['DeletePrm'])=="1") {
			  $delstat = $delstat;
			  }else{
			  $delstat = "";
			  }
			  
			  
			  if( trim($res_modm_prm['EditPrm'])!="1" &&  trim($res_modm_prm['DeletePrm'])!="1" ){
				 $delstat = "-"; 
			  }	
			 
				
			 
		//get product count lists 		
		$pdt_sku_cnt = get_sales_pdt_cnt_lists($db,$rs['s_bill_no']);
		
		
		$nestedData[]=$rs['s_bill_no'];
		$nestedData[]=$rs['actu_bill_no'];
		$nestedData[]=$rs['disp_bill_date'];
		$nestedData[]=$rs['company_name'];
		$nestedData[] = $rs['cus_name'] . '</br><span style="color:green; font-size:12px">' . $rs['cus_gstno'] . '</span>';
		$nestedData[] = $rs['cus_city'].'</br>'.$rs['cus_state'].'</br>'.$rs['cus_postcode']; 
		$nestedData[] = $pdt_cnt['disp_cnt'].'</br>(<span style="color:blue; font-size:12px">'.$pdt_csv.'</span>)';	
		$nestedData[] = $rs['total_sale_amount'];
		$nestedData[] = $rs['discount_per'];
		$nestedData[] = $rs['after_discount_amt'];
		$nestedData[] = $rs['tot_tax_amt'];
		$nestedData[] = $rs['final_sale_amt'];		
		$nestedData[] = $incstat;		 
		$nestedData[] =  $edtstat.'&nbsp;'.$delstat.'&nbsp;'.$inv_print;
		$nestedData[] = $rs['cus_name'];
		$nestedData[] = $rs['cus_gstno'];
		$nestedData[] = $rs['cus_city'];
		$nestedData[] = $rs['cus_state'];
		$nestedData[] = $rs['cus_postcode'];
		$nestedData[] = $pdt_amount;
		
		
		
		$data[] = $nestedData;
		// print_r($data);
	}	
		
}

else if($_REQUEST['pageaction'] == "return_listing"){
	
	include "includes/Mdme-functions.php";	
	$mdme = getMdme_salesreturn($db,'');	
	include_once "includes/pagepermission.php";	
	include_once('includes/display-functions.php');
// echo 'hi';
// echo $_REQUEST['pdt_sku'];
		
	$cond_subtbl = ""; $cond_mntbl = "";
	if(isset($_REQUEST['srch_type']) && $_REQUEST['srch_type'] !="" ){		
				
		if($_REQUEST['srch_type'] == "pdt_sku"){
			if(isset($_REQUEST['srch_keyword']) && $_REQUEST['srch_keyword'] !="" ){
				$cond_subtbl ="  and (pdt_sku like '%".$_REQUEST['srch_keyword']."%') ";
			}			
		}
		else if($_REQUEST['srch_type'] == "bill_no"){
			if(isset($_REQUEST['srch_keyword']) && $_REQUEST['srch_keyword'] !="" ){
				$cond_mntbl ="  and (t1.actu_bill_no like '%".$_REQUEST['srch_keyword']."%' ) ";
			}				
		}		
		else if($_REQUEST['srch_type'] == "invoice_no"){
			if(isset($_REQUEST['srch_keyword']) && $_REQUEST['srch_keyword'] !="" ){
				$cond_mntbl ="  and (t1.invoice_id like '%".$_REQUEST['srch_keyword']."%' ) ";
			}				
		}		
	} 
		
	$cond_finyear = "";	
	if(isset($_REQUEST['srch_finyear']) && $_REQUEST['srch_finyear'] !="" ){	
		$cond_finyear ="  and (t1.financial_id='".$_REQUEST['srch_finyear']."') ";
	} 
		
	$cond_company = "";	
	if(isset($_REQUEST['srch_company']) && $_REQUEST['srch_company'] !="" ){	
		$cond_company ="  and (t1.company_id='".$_REQUEST['srch_company']."') ";
	} 
		
	$cond_customer = "";	
	if(isset($_REQUEST['srch_customer']) && $_REQUEST['srch_customer'] !="" ){	
		$cond_customer ="  and (t1.cus_id='".$_REQUEST['srch_customer']."') ";
	}
		
	$whrcon = "";
	if(isset($_REQUEST['srch_fmdate']) && $_REQUEST['srch_fmdate'] !="" )
	{
		$whrcon .= " and (t1.return_date  >=  '".$_REQUEST['srch_fmdate']."') ";
	}
	if(isset($_REQUEST['srch_todate']) && $_REQUEST['srch_todate'] !="" )
	{
		$whrcon .= " and (t1.return_date  <=  '".$_REQUEST['srch_todate']."') ";
	}
		
	$str_all = "select t1.*,DATE_FORMAT(t1.return_date,'%d-%m-%Y') AS disp_bill_date,t1.IsActive AS status, t2.company_name, t3.cus_name, t3.cus_state, t3.cus_city,t3.cus_postcode,t3.cus_gstno from t_return_entry_main t1 
	inner join m_company t2 on t1.company_id=t2.company_id
	inner join m_customer t3 on t1.cus_id = t3.cus_id where 1=1 and t1.IsActive <>2 and t1.r_bill_no IN		
	(
		select r_bill_no from t_return_entry_details where IsActive <>2 ".$cond_subtbl."
	) ".$cond_mntbl."  ".$cond_finyear." ".$cond_company." ".$cond_customer."  ". $whrcon."  order by t1.r_bill_no desc  ";
		
	$get_tota = $db->get_rsltset($str_all);
	// print_r($str_all);
	// die();
	$totalData = count($get_tota);
	$totalFiltered = $totalData;	
		
	$str_all .= " limit ".$stt.",".$len;		
	
    $rslt_info =  $db->get_rsltset($str_all);
	
	// print_r($totalData);

    foreach($rslt_info as $rs){
		$nestedData=array();
			
		
		$editid = base64_encode($rs['r_bill_no']);
		$stats = $rs['status'];					

		
		
		
///////////from here, newly added for act status					
		if( $stats == '1') 

		 {
			//change status active to inactive
			$statusurl = "'return_actions.php','Id=$editid&action=changestatus&actval=0'";

		   $incstat =  '<div class="btn-group" data-toggle="btn-toggle" >
				<button class="btn btn-success btn-sm active" type="button"><i class="fa fa-square text-green"></i> Active</button>
				<button class="btn btn-default btn-sm" type="button" onclick="funchangestatus(this,'.$statusurl.');"><i class="fa fa-square text-red"></i> &nbsp;</button>
				</div>'	;				
		 } else {
			//change status inactive to active
			$statusurl = "'return_actions.php','Id=$editid&action=changestatus&actval=1'";
			
			 $incstat =  '<div class="btn-group" data-toggle="btn-toggle" >
				<button class="btn btn-default btn-sm" type="button" onclick="funchangestatus(this,'.$statusurl.');"><i class="fa fa-square text-green"></i> &nbsp;</button>
				<button class="btn btn-danger btn-sm active" type="button"><i class="fa fa-square text-red"></i> InActive</button>
			</div>'	;	
		}

	

		//del 
		$delurl = "'return_actions.php','Id=$editid&action=del'";
		
		
		$delstat='<a href="javascript:void(0);" title="delete" data-toggle="tooltip" class="btn btn-danger btn-sm" onClick="javascript:funStatsReturn(this,'.$delurl.')" ><span><i class="fa fa-times"style="font-size:15px"></i></span></a>';
		
		$inv_print='<a target="_blank" href="return_invoice_print.php?s_bill_no='.$editid.'" title="Bill" class="btn btn-warning btn-sm"><span><i class="fa fa-print"></i></span></a>';	

		$edtstat='<a href="return_form.php?act=edit&id='.$editid.'" title="edit" data-toggle="tooltip" class="btn btn-info btn-sm" ><i class="fa fa-edit"style="font-size:12px"></i></a>';
		$payment_link='<br/> <a target="_blank" href="returnpayment_mng.php?customer_id='.base64_encode($rs['cus_id']).'&company_id='.base64_encode($rs['company_id']).'" title="invoice">Add Payment</a>';
		
		$pdt_cnt =$db->get_a_line("select count(*) as disp_cnt from t_return_entry_details where 1=1 and IsActive=1 
		and r_bill_no='".$rs['r_bill_no']."' ");     
				
			if(trim($res_modm_prm['EditPrm'])=="1") {
				$edtstat = $edtstat;
			  }else{
				$edtstat = "";			
			  }
			  
			 if(trim($res_modm_prm['DeletePrm'])=="1") {
			  $delstat = $delstat;
			  }else{
			  $delstat = "";
			  }
			  
			  
			  if( trim($res_modm_prm['EditPrm'])!="1" &&  trim($res_modm_prm['DeletePrm'])!="1" ){
				 $delstat = "-"; 
			  }	
			 
			
		
		$nestedData[]=$rs['r_bill_no'];
		$nestedData[]=$rs['invoice_id'];
		$nestedData[]=$rs['disp_bill_date'];
		$nestedData[]=$rs['company_name'];
		$nestedData[] = $rs['cus_name'] . '</br><span style="color:green; font-size:12px">' . $rs['cus_gstno'] . '</span>';
		$nestedData[] = $rs['cus_city'].'</br>'.$rs['cus_state'].'</br>'.$rs['cus_postcode']; 
		$nestedData[] = $pdt_cnt['disp_cnt'];		
		$nestedData[] = $rs['total_sale_amount'];
		$nestedData[] = $rs['discount_per'];
		$nestedData[] = $rs['after_discount_amt'];
		$nestedData[] = $rs['tot_tax_amt'];
		$nestedData[] = $rs['final_sale_amt'];		
		$nestedData[] = $incstat;		 
		$nestedData[] =  $edtstat.'&nbsp;'.$delstat.'&nbsp;' ;
		$nestedData[] = $rs['cus_name'];
		$nestedData[] = $rs['cus_gstno'];
		$nestedData[] = $rs['cus_city'];
		$nestedData[] = $rs['cus_state'];
		$nestedData[] = $rs['cus_postcode'];
		
		$data[] = $nestedData;
		// print_r($data);
	}	
		
}

else if($_REQUEST['pageaction'] == "payment_listing"){
	
	include "includes/Mdme-functions.php";	
	$mdme = getMdme_Payment_Details($db,'');	
	include_once "includes/pagepermission.php";	
	include_once('includes/display-functions.php');
// echo $_REQUEST['pdt_sku'];
		
	$cond_subtbl = ""; $cond_mntbl = "";
	if(isset($_REQUEST['srch_type']) && $_REQUEST['srch_type'] !="" ){		
				
		if($_REQUEST['srch_type'] == "customer_name"){
			if(isset($_REQUEST['srch_keyword']) && $_REQUEST['srch_keyword'] !="" ){
				$cond_subtbl ="  and (t3.cus_name like '%".$_REQUEST['srch_keyword']."%') ";
			}			
		}
		else if($_REQUEST['srch_type'] == "bill_no"){
			if(isset($_REQUEST['srch_keyword']) && $_REQUEST['srch_keyword'] !="" ){
				$cond_mntbl ="  and (t4.s_bill_no like '%".$_REQUEST['srch_keyword']."%' ) ";
			}				
		}		
	} 
		
	$cond_finyear = "";	
	if(isset($_REQUEST['srch_finyear']) && $_REQUEST['srch_finyear'] !="" ){	
		$cond_finyear ="  and (t1.financial_id='".$_REQUEST['srch_finyear']."') ";
	} 
		
	$cond_company = "";	
	if(isset($_REQUEST['srch_company']) && $_REQUEST['srch_company'] !="" ){	
		$cond_company ="  and (t1.company_id='".$_REQUEST['srch_company']."') ";
	} 
		
	$cond_customer = "";	
	if(isset($_REQUEST['srch_customer']) && $_REQUEST['srch_customer'] !="" ){	
		$cond_customer ="  and (t1.cus_id='".$_REQUEST['srch_customer']."') ";
	}
		
	$whrcon = "";
	if(isset($_REQUEST['srch_fmdate']) && $_REQUEST['srch_fmdate'] !="" )
	{
		$whrcon .= " and (t1.payment_date  >=  '".$_REQUEST['srch_fmdate']."') ";
	}
	if(isset($_REQUEST['srch_todate']) && $_REQUEST['srch_todate'] !="" )
	{
		$whrcon .= " and (t1.payment_date  <=  '".$_REQUEST['srch_todate']."') ";
	}
		
	$str_all = "SELECT DISTINCT  t1.*,DATE_FORMAT(t1.payment_date,'%d-%m-%Y') AS disp_bill_date,t1.IsActive AS status, t1.payment_status AS pay_status,
	t2.company_name, 
	t3.cus_name, t3.cus_state, t3.cus_city,t3.cus_postcode,t3.cus_gstno, 

	t4.payment_id, GROUP_CONCAT(t4.s_bill_no ORDER BY t4.s_bill_no ASC SEPARATOR ', ') AS s_bill_no,
	t5.payment_type as type  
	from t_payment_main t1 
	inner join m_company t2 on t1.company_id=t2.company_id
	inner join m_customer t3 on t1.cus_id = t3.cus_id 
	inner join t_payment_details t4 on t1.id = t4.payment_id 
	join status_payment_type t5 on t1.payment_type = t5.payment_type_id  where 1=1 and t1.IsActive <>2 and t1.id IN		
	(
		SELECT payment_id from t_payment_details where IsActive <>2 ".$cond_subtbl."
	) ".$cond_mntbl."   ".$cond_company." ".$cond_customer."  ". $whrcon." GROUP BY t4.payment_id order by t1.id desc  ";
// echo $str_all;
		
	$get_tota = $db->get_rsltset($str_all);
	// print_r($str_all);
	// die();
	$totalData = count($get_tota);
	$totalFiltered = $totalData;	
		
	$str_all .= " limit ".$stt.",".$len;		
	
    $rslt_info =  $db->get_rsltset($str_all);
	
	// print_r($totalData);

    foreach($rslt_info as $rs){
		$nestedData=array();
			
		$editid = base64_encode($rs['id']);
		
		//del 
		$delurl = "'payment_actions.php','Id=$editid&action=del'";
		
		
		$delstat='<a href="javascript:void(0);" title="delete" data-toggle="tooltip" class="btn btn-danger btn-sm" onClick="javascript:funStatsPayment(this,'.$delurl.')" ><span><i class="fa fa-times"style="font-size:15px"></i></span></a>';
		
			$edtstat='<a href="payment_details.php?act=edit&id='.$editid.'" title="edit" data-toggle="tooltip" class="btn btn-info btn-sm" ><i class="fa fa-edit"style="font-size:12px"></i></a>';
		
			
			$pdt_cnt =$db->get_a_line("select count(*) as disp_cnt from t_payment_details where 1=1 and IsActive=1 and s_bill_no='".$rs['s_bill_no']."' ");     
				
			if(trim($res_modm_prm['EditPrm'])=="1") {
				$edtstat = $edtstat;
			  }else{
				$edtstat = "";			
			  }
			  
			 if(trim($res_modm_prm['DeletePrm'])=="1") {
			  $delstat = $delstat;
			  }else{
			  $delstat = "";
			  }
			  
			  
			  if( trim($res_modm_prm['EditPrm'])!="1" &&  trim($res_modm_prm['DeletePrm'])!="1" ){
				 $delstat = "-"; 
			  }	
			 
				
			 
		//get product count lists 		
		$pdt_sku_cnt = get_sales_pdt_cnt_lists($db,$rs['s_bill_no']);
		
		
		$nestedData[]=$rs['id'];
		$nestedData[]=$rs['s_bill_no'];
		// $nestedData[]=$rs['actu_bill_no'];
		$nestedData[]=$rs['disp_bill_date'];
		$nestedData[]=$rs['company_name'];
		$nestedData[] = $rs['cus_name'] . '</br><span style="color:green; font-size:12px">' . $rs['cus_gstno'] . '</span>';
		$nestedData[] = $rs['cus_city'].'</br>'.$rs['cus_state'].'</br>'.$rs['cus_postcode']; 
		// $nestedData[] = $pdt_cnt['disp_cnt'];		
		$nestedData[] = $rs['bill_amount'];
		$nestedData[] = $rs['received_amount'];
		$nestedData[] = $rs['less_amount'];
		$nestedData[] = $rs['notes'];
		$nestedData[] = $rs['type'];		
		$nestedData[] = $rs['ref_no'];
		$nestedData[] = $rs['pay_status'];
		// $nestedData[] = $incstat;		 
		$nestedData[] =  $edtstat.'&nbsp;'.$delstat;
		$nestedData[] = $rs['cus_name'];
		$nestedData[] = $rs['cus_gstno'];
		$nestedData[] = $rs['cus_city'];
		$nestedData[] = $rs['cus_state'];
		$nestedData[] = $rs['cus_postcode'];
		
		$data[] = $nestedData;
		// print_r($data);
	}	
	// $nestedData[]=$rs['s_bill_no'];
	// $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

?>