<?php 
date_default_timezone_set("Asia/Kolkata"); 
include_once "session.php";
include "includes/Mdme-functions.php";

// storing  request (ie, get/post) global array to a variable  

$requestData= $_REQUEST;
include_once "display-grid-data-functions.php"; 
  
$wrcon = "";
$srchcollen = count($requestData['search']);

//print_r($requestData);

 $stt = $requestData['start'];
 $len = $requestData['length'];
  
 switch ($_REQUEST['finaltab']) { 
			
	case "menu":	    		 
		$dispFields = array("MenuName");
		$disporder_ID= "MenuId";
		$mdme = getMdmeMenu($db,''); 
		
		$wrcon .= " and (MenuName like '%".$requestData['search']['value']."%')";	
						
		$order_clmn = $requestData['order'][0]['column'];
		$order_oper = $requestData['order'][0]['dir'];			
		$ordr = " order by $dispFields[$order_clmn] $order_oper ";	
				
		$totalData = getMenuArray_tot($db,$act,$wrcon,$ordr,$stt,$len);
		$res = getMenuArray_Ajx($db,$act,$wrcon,$ordr,$stt,$len); 		
	
	break;		
		
	case "module":	    		 
		$dispFields = array("ModuleName","Description","ModulePath");
		$disporder_ID= "ModuleId";
		$mdme = getMdmeModule($db,''); 		
		
        $wrcon = " and (ModuleName like '%".$requestData['search']['value']."%' or Description like '%".$requestData['search']['value']."%' or ModulePath like '%".$requestData['search']['value']."%')";		
		
		$order_clmn = $requestData['order'][0]['column'];
		$order_oper = $requestData['order'][0]['dir'];			
		$ordr = " order by $dispFields[$order_clmn] $order_oper ";	
				
		$totalData = getModuleArray_tot($db,$act,$wrcon,$ordr,$stt,$len);
		$res = getModuleArray_Ajx($db,$act,$wrcon,$ordr,$stt,$len); 		
	
	break;
	
	case "modulemenu":	    		 
		$dispFields = array("MenuName");
		$disporder_ID= "MenuId";
		$mdme = getMdmeModulemenu($db,''); 		
		
        $wrcon = " and (MenuName like '%".$requestData['search']['value']."%')";		
		
		$order_clmn = $requestData['order'][0]['column'];
		$order_oper = $requestData['order'][0]['dir'];			
		$ordr = " order by $dispFields[$order_clmn] $order_oper ";	
				
		$totalData = getModuleMenuArray_tot($db,$act,$wrcon,$ordr,$stt,$len);
		$res = getModuleMenuArray_Ajx($db,$act,$wrcon,$ordr,$stt,$len); 		
	
	break;
	
	case "permissioninfo":	    		 
		$dispFields = array("RoleName");
		$disporder_ID= "RoleId";
		$mdme = getMdmePermissioninfo($db,''); 		
		
        $wrcon = " and (RoleName like '%".$requestData['search']['value']."%')";		
		
		$order_clmn = $requestData['order'][0]['column'];
		$order_oper = $requestData['order'][0]['dir'];			
		$ordr = " order by $dispFields[$order_clmn] $order_oper ";	
				
		$totalData = getPermissionInfoArray_tot($db,$act,$wrcon,$ordr,$stt,$len);
		$res = getPermissionInfoArray_Ajx($db,$act,$wrcon,$ordr,$stt,$len); 		
	
	break;
	
	case "roleinfo":	    		 
		$dispFields = array("RoleName");
		$disporder_ID= "RoleId";
		$mdme = getMdmeRole($db,''); 		
		
		$wrcon = " and (RoleName like '%".$requestData['search']['value']."%')";		
		
		$order_clmn = $requestData['order'][0]['column'];
		$order_oper = $requestData['order'][0]['dir'];			
		$ordr = " order by $dispFields[$order_clmn] $order_oper ";	
				
		$totalData = getRoleArray_tot($db,$act,$wrcon,$ordr,$stt,$len);
		$res = getRoleArray_Ajx($db,$act,$wrcon,$ordr,$stt,$len); 		
	
	break;
	
	case "userinfo":	    		 
		$dispFields = array("user_firstname","user_lastname","user_name","user_email","RoleName");
		$disporder_ID= "user_ID";
		$mdme = getMdmeRole($db,''); 		
		
		$wrcon = " and (user_name like '%".$requestData['search']['value']."%' or user_firstname like '%".$requestData['search']['value']."%' or user_lastname like '%".$requestData['search']['value']."%' or r.RoleName like '%".$requestData['search']['value']."%')";		
		
		$order_clmn = $requestData['order'][0]['column'];
		$order_oper = $requestData['order'][0]['dir'];	
		
		$ordr = " order by $dispFields[$order_clmn] $order_oper ";	
				
		$totalData = getUserArray_tot($db,$act,$wrcon,$ordr,$stt,$len);
		$res = getUserArray_Ajx($db,$act,$wrcon,$ordr,$stt,$len); 		
	
	break;	

	case "company":	    		 
		$dispFields = array("company_name","proprietor_name","c_mobile","c_email","gst_no");
		$disporder_ID= "company_id";
		$mdme = getMdmeCompany($db,''); 		
		
		$wrcon = " and (company_name like '%".$requestData['search']['value']."%' or proprietor_name like '%".$requestData['search']['value']."%' or c_mobile like '%".$requestData['search']['value']."%' or c_email like '%".$requestData['search']['value']."%'  or c_email like '%".$requestData['search']['value']."%' or gst_no like '%".$requestData['search']['value']."%' )";		
		
		$order_clmn = $requestData['order'][0]['column'];
		$order_oper = $requestData['order'][0]['dir'];			
		$ordr = " order by $dispFields[$order_clmn] $order_oper ";	
				
		$totalData = getCompanyArray_tot($db,$act,$wrcon,$ordr,$stt,$len);
		$res = getCompanyArray_Ajx($db,$act,$wrcon,$ordr,$stt,$len); 		
	
	break;		
	
	case "supplier":	    		 
		$dispFields = array("code","sup_name","disp_sup_type", "sup_city", "disp_sup_payment_type", "sup_mobile","sup_email","sup_gstno");
		$disporder_ID= "sup_id";
		$mdme = getMdmeSupplier($db,''); 		
		if(!empty($requestData['search']['value'])) {
			$wrcon = " and (sup_name like '%".$requestData['search']['value']."%' or sup_mobile like '%".$requestData['search']['value']."%' or sup_email like '%".$requestData['search']['value']."%' or sup_gstno like '%".$requestData['search']['value']."%' or sup_mobile like '%".$requestData['search']['value']."%' or sup_gstno like '%".$requestData['search']['value']."%'";	
			
			$pos = strpos('Individual', $requestData['search']['value']);	
			if ($pos !== false) {
				$wrcon .=" or sup_type = 1";
			}
			$pos = strpos('Company', $requestData['search']['value']);	
			if ($pos !== false) {
				$wrcon .=" or sup_type != 1";
			}

			$pos = strpos('Non-GST', $requestData['search']['value']);	
			if ($pos !== false) {
				$wrcon .=" or sup_payment_type != 1";
			}else {
				$pos = strpos('GST', $requestData['search']['value']);	
				if ($pos !== false) {
					$wrcon .=" or sup_payment_type = 1";
				}
			}

			$wrcon .= " )";
		}
		
		$order_clmn = $requestData['order'][0]['column'];
		$order_oper = $requestData['order'][0]['dir'];			
		$ordr = " order by $dispFields[$order_clmn] $order_oper ";	
				
		$totalData = getSupplierArray_tot($db,$act,$wrcon,$ordr,$stt,$len);
		$res = getSupplierArray_Ajx($db,$act,$wrcon,$ordr,$stt,$len); 		
	
	break;	

	case "customer":	    		 
		$dispFields = array("code","cus_name","disp_cus_type","cus_mobile","cus_email","cus_gstno","cus_address","cus_state","cus_city","cus_postcode");
		$disporder_ID= "cus_id";
		$mdme = getMdmeCustomer($db,''); 		
		if(!empty($requestData['search']['value'])) {
		$wrcon = " and (cus_name like '%".$requestData['search']['value']."%' or cus_mobile like '%".$requestData['search']['value']."%' or cus_email like '%".$requestData['search']['value']."%' or cus_gstno like '%".$requestData['search']['value']."%' or cus_address like '%".$requestData['search']['value']."%' or cus_state like '%".$requestData['search']['value']."%' or cus_city like '%".$requestData['search']['value']."%' or cus_postcode like '%".$requestData['search']['value']."%' ";
		
			$pos = strpos('Individual', $requestData['search']['value']);	
			if ($pos !== false) {
				$wrcon .=" or cus_type = 1";
			}
			$pos = strpos('Company', $requestData['search']['value']);	
			if ($pos !== false) {
				$wrcon .=" or cus_type != 1";
			}

			$wrcon .= " )";
		}		
		
		$order_clmn = $requestData['order'][0]['column'];
		$order_oper = $requestData['order'][0]['dir'];			
		$ordr = " order by $dispFields[$order_clmn] $order_oper ";	
				
		$totalData = getCustomerArray_tot($db,$act,$wrcon,$ordr,$stt,$len);
		$res = getCustomerArray_Ajx($db,$act,$wrcon,$ordr,$stt,$len); 		
	
	break;
	case "financial":	    
		$dispFields = array("f_start_date","f_end_date");
		$disporder_ID= "financial_id";
		$mdme = getMdmeFinance($db,''); 		
		
		$wrcon = "";		
		// echo $wrcon		 ;
		
		$order_clmn = $requestData['order'][0]['column'];
		$order_oper = $requestData['order'][0]['dir'];			
		$ordr = " order by $dispFields[$order_clmn] $order_oper ";	
				
		$totalData = getFyearArray_tot($db,$act,$wrcon,$ordr,$stt,$len);
		$res = getFyearArray_Ajx($db,$act,$wrcon,$ordr,$stt,$len); 		
	
	break;
	
	case "purchase":	    		 
		$dispFields = array("p_invoice_no", "actu_inv_no", "disp_inv_date", "company_name", "sup_name", "final_pur_amt");
		$disporder_ID= "p_invoice_no";
		$mdme = getMdmePurchaseEntry($db,''); 		
		
		$wrcon = " and (t2.company_name like '%".$requestData['search']['value']."%' or t3.sup_name like '%".$requestData['search']['value']."%' )";		
		
		$order_clmn = $requestData['order'][0]['column'];
		$order_oper = $requestData['order'][0]['dir'];			
		$ordr = " order by $dispFields[$order_clmn] $order_oper ";	
				
		$totalData = getPurchaseEntryArray_tot($db,$act,$wrcon,$ordr,$stt,$len);
		$res = getPurchaseEntryArray_Ajx($db,$act,$wrcon,$ordr,$stt,$len); 		
	
	break;
	
	case "payment":	    		 
		$dispFields = array("s_bill_no","actu_bill_no","disp_bill_date","company_name");
		$disporder_ID= "s_bill_no";
		$mdme = getMdme_Payment_Details($db,''); 		
		
		$wrcon = " and (t3.company_name like '%".$requestData['search']['value']."%' or t4.cus_name like '%".$requestData['search']['value']."%' )";		
		
		$order_clmn = $requestData['order'][0]['column'];
		$order_oper = $requestData['order'][0]['dir'];			
		$ordr = " order by $dispFields[$order_clmn] $order_oper ";	
				
		$totalData = getPaymentEntryArray_tot($db,$act,$wrcon,$ordr,$stt,$len);
		$res = getPaymentEntryArray_Ajx($db,$act,$wrcon,$ordr,$stt,$len); 
	
	break;

	case "sales":	    		 
		$dispFields = array("s_bill_no","actu_bill_no","disp_bill_date","company_name");
		$disporder_ID= "s_bill_no";
		$mdme = getMdmeSalesEntry($db,''); 		
		
		$wrcon = " and (t3.company_name like '%".$requestData['search']['value']."%' or t4.cus_name like '%".$requestData['search']['value']."%' )";		
		
		$order_clmn = $requestData['order'][0]['column'];
		$order_oper = $requestData['order'][0]['dir'];			
		$ordr = " order by $dispFields[$order_clmn] $order_oper ";	
				
		$totalData = getSalesEntryArray_tot($db,$act,$wrcon,$ordr,$stt,$len);
		$res = getSalesEntryArray_Ajx($db,$act,$wrcon,$ordr,$stt,$len); 
	
	break;

	case "return":	    		 
		$dispFields = array("r_bill_no","invoice_id","disp_rtn_date","company_name");
		$disporder_ID= "r_bill_no";
		$mdme = getMdme_salesreturn($db,''); 		
		
		$wrcon = " and (t3.company_name like '%".$requestData['search']['value']."%' or t4.cus_name like '%".$requestData['search']['value']."%' )";		
		
		$order_clmn = $requestData['order'][0]['column'];
		$order_oper = $requestData['order'][0]['dir'];			
		$ordr = " order by $dispFields[$order_clmn] $order_oper ";	
				
		$totalData = getReturnEntryArray_tot($db,$act,$wrcon,$ordr,$stt,$len);
		$res = getReturnEntryArray_Ajx($db,$act,$wrcon,$ordr,$stt,$len); 
	
	break;
	
	case "rpt_product_list":	    		 
		$dispFields = array("fyear","company_name","disp_inv_date","p_invoice_no","pdt_sku","upload_img","saree_desc","sup_name","purchase_rate","status","actu_bill_no","sale_date","cus_name","sale_price","discount","discountprice","taxamt","soldprice");
		$disporder_ID= "p_invoice_no";
		$mdme = getMdmePurchaseEntry($db,''); 		
		
		$wrcon = " and (t3.company_name like '%".$requestData['search']['value']."%' or t4.sup_name like '%".$requestData['search']['value']."%' or t1.pdt_sku like '%".$requestData['search']['value']."%' or t1.saree_desc like '%".$requestData['search']['value']."%' )";		
		
		$order_clmn = $requestData['order'][0]['column'];
		$order_oper = $requestData['order'][0]['dir'];	
		
			$ordr = " order by t2.p_invoice_date desc,t1.pdt_sku desc ";	
		
				
		$totalData = getRptPdtListArray_tot($db,$act,$wrcon,$ordr,$stt,$len);
		$res = getRptPdtListArray_Ajx($db,$act,$wrcon,$ordr,$stt,$len); 		
	
	break;
	
	case "banks":	    		 
		$dispFields = array("company_name","bank_name","branch_name");
		$disporder_ID= "bank_id";
		$mdme = getMdmeBanks($db,''); 		
		
		$wrcon = " and (t2.company_name like '%".$requestData['search']['value']."%' or t1.bank_name like '%".$requestData['search']['value']."%' or t1.branch_name like '%".$requestData['search']['value']."%' )";		
		
		$order_clmn = $requestData['order'][0]['column'];
		$order_oper = $requestData['order'][0]['dir'];			
		$ordr = " order by $dispFields[$order_clmn] $order_oper ";	
				
		$totalData = getBanksArray_tot($db,$act,$wrcon,$ordr,$stt,$len);
		$res = getBanksArray_Ajx($db,$act,$wrcon,$ordr,$stt,$len); 		
	
	break;		
		
	default:
       echo "No-Data";
 }
 
$totalFiltered = $totalData;
$data = array();


	foreach($res as $r)
	{
		$nestedData=array(); 			
       		
		$editid = base64_encode($r[$disporder_ID]);                    
        $stats = $r['IsActive'];					
		$actmodul = $_REQUEST['finaltab'];	
		
		
		
///////////from here, newly added for act status					
		if( $stats == '1') 

		 {
			//change status active to inactive
			$statusurl = "'".$_REQUEST['finaltab']."_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 = "'".$_REQUEST['finaltab']."_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>'	;	
		}
        //finance
		if($_REQUEST['finaltab'] == 'financial' && $r['disp_status'] == '1') 
		{
		   //change status active to inactive
		   $statusurl1 = "'".$_REQUEST['finaltab']."_actions.php','Id=$editid&action=changedispstatus&actval=0'";

		  $incstat1 =  '<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,'.$statusurl1.');"><i class="fa fa-square text-red"></i> &nbsp;</button>
			   </div>'	;				
		} else {
		   //change status inactive to active
		   $statusurl1 = "'".$_REQUEST['finaltab']."_actions.php','Id=$editid&action=changedispstatus&actval=1'";
		   
			$incstat1 =  '<div class="btn-group" data-toggle="btn-toggle" >
			   <button class="btn btn-default btn-sm" type="button" onclick="funchangestatus(this,'.$statusurl1.');"><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
$edtstat='<a href="'.$actmodul.'_form.php?act=edit&id='.$editid.'" title="edit" data-toggle="tooltip" class="btn btn-info btn-sm" ><span>&nbsp;<i class="fa fa-edit"></i></span></a>';	

//del 
$delurl = "'".$_REQUEST['finaltab']."_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>';	
$returnstat='<a href="javascript:void(0);" title="delete" data-toggle="tooltip" class="btn btn-danger btn-sm" onClick="javascript:funStatsReturn(this,'.$delurl.')" ><i class="fa fa-times"></i></a>';	


include_once "includes/pagepermission.php";
		  		  
		  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 = "-"; 
		  }

		  
		///////////till here newly added for act inact status
				
		
		foreach ($dispFields as $dispFields_S) {
			if($_REQUEST['finaltab'] == 'rpt_product_list' && $dispFields_S == 'upload_img'){				
				$pdt_prev_img='NoImage.png';				
				if($r['upload_img'] != '' && file_exists(CAAROOT_DIR."/uploads/product_image/".$r['upload_img'])) {					
					$pdt_prev_img = $r['upload_img'];	
				}
				 $nestedData[] = '<img src="'.BASE_URL.'uploads/product_image/'.$pdt_prev_img.'" onclick="hoverImage(this)" class="w3-hover-opacity"  width="50px" height="50px" align="absmiddle" />';
			}else $nestedData[] = $r[$dispFields_S];			
		}

			
		$inv_print='';
		$payment_link='';
		if($_REQUEST['finaltab'] == 'purchase'){
			/*
			$inv_print='<a target="_blank" href="purchase_invoice_print.php?p_invoice_id='.$editid.'" title="invoice">Invoice</a>';
			$payment_link='<br/> <a target="_blank" href="purchasepayment_mng.php?supplier_id='.base64_encode($r['sup_id']).'&company_id='.base64_encode($r['company_id']).'" title="invoice">Add Payment</a>';				
						
			$nestedData[] = $r['sup_name'].$payment_link; */
			
			$pdt_cnt =$db->get_a_line("select count(*) as disp_cnt, GROUP_CONCAT(pdt_sku) as disp_pdt_sku from t_purchase_entry_details where 1=1 and IsActive=1 and p_invoice_no='".$r['p_invoice_no']."' ");
			 
			$nestedData[] = '<a class="hoverToolTip" id="'.$r['p_invoice_no'].'">'.$pdt_cnt['disp_pdt_sku']." (".$pdt_cnt['disp_cnt'].")</a>";
            			
		}
		else if($_REQUEST['finaltab'] == 'sales'){
			$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="'.$actmodul.'_form.php?act=edit&id='.$editid.'" title="edit" data-toggle="tooltip" class="btn btn-info btn-sm" ><i class="fa fa-edit"></i></a>';
			$payment_link='<br/> <a target="_blank" href="salespayment_mng.php?customer_id='.base64_encode($r['cus_id']).'&company_id='.base64_encode($r['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='".$r['s_bill_no']."' ");
			
			 
			$nestedData[] = $r['cus_name'] . '</br><span style="color:green; font-size:12px">' . $r['cus_gstno'] . '</span>';
			$nestedData[] = $r['cus_city'].'</br>'.$r['cus_state'].'</br>'.$r['cus_postcode']; 
			$nestedData[] = $pdt_cnt['disp_cnt'];	
			$nestedData[] = $r['total_sale_amount'];
			$nestedData[] = $r['discount_per'];
			$nestedData[] = $r['after_discount_amt'];
			$nestedData[] = $r['tot_tax_amt'];
			$nestedData[] = $r['final_sale_amt'];		
			$nestedData[] = $incstat;		 
		$nestedData[] =  $edtstat.'&nbsp;'.$delstat.'&nbsp;'.$inv_print;
			
		}
		else if($_REQUEST['finaltab'] == 'return'){
			// $inv_print='<a target="_blank" href="sales_invoice_print.php?s_bill_no='.$editid.'" title="Bill">Bill</a>';
			// $payment_link='<br/> <a target="_blank" href="salespayment_mng.php?customer_id='.base64_encode($r['cus_id']).'&company_id='.base64_encode($r['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='".$r['r_bill_no']."' ");
			
			$nestedData[] = $r['cus_name'].$payment_link; 
			$nestedData[] = $pdt_cnt['disp_cnt'];	
			$nestedData[] = $r['total_sale_amount'];
			$nestedData[] = $r['tot_tax_amt'];
			$nestedData[] = $r['final_sale_amt'];	
			$nestedData[] = $incstat;		 
			$nestedData[] =  $edtstat.'&nbsp;'.$returnstat;	
		}
				
		
								
		$nestedData[] = $incstat;	
		if($_REQUEST['finaltab'] == 'financial'){
			$nestedData[] = $incstat1;		
		}		
		$nestedData[] =  $edtstat.'&nbsp;'.$delstat.'&nbsp;'.$inv_print;
								
		$data[] = $nestedData;		
			
	}
				
$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
?>