0xV3NOMx
Linux ip-172-26-7-228 5.4.0-1103-aws #111~18.04.1-Ubuntu SMP Tue May 23 20:04:10 UTC 2023 x86_64



Your IP : 18.225.54.147


Current Path : /var/www/oasis/src_old/
Upload File :
Current File : /var/www/oasis/src_old/uploadmoduledetails.php

<?php
function getModuleDetailsHeaders($aobj_context)
{
	$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
	$schema_id = $aobj_context->mobj_data["schema_id"];
	$main_src_obj=(explode("/",$_SERVER["REQUEST_URI"]));
	$main_src=$main_src_obj[1];
	$excel_writer=$aobj_context->main_src."/Excel_Writer/Writer.php";
	require_once($excel_writer);
	$tarka_format_file=$aobj_context->main_src."/custom_src/TarkaFormatStyles.php";
	require_once($tarka_format_file);	
	$objfrmt =& new FormatStyle();  
 
	$workbook = new Spreadsheet_Excel_Writer();
	$Title_Center =& $workbook->addFormat($objfrmt->Title_Center);
	$worksheet =& $workbook->addWorksheet();
	
	$get_headers="(select 
				ue.internal_code,
				ue.name as element_name,
				s.value as Type,
				ifnull(ug.name,'0None') as group_name,
				s1.value as meta_data,decimals,
				 
				optional,list_of_values,
				u.internal_code as ref_s_user_schema_code,
				ue.ref_module_code  
				from s_user_schema_elements ue
				inner join s_user_schema u on u.internal_code=ue.ref_s_user_schema_code
				left join s_user_schema_group ug on ug.internal_code=ref_s_user_schema_group_code
				left join s_ref_type s on s.internal_code=ue.data_type
				left join s_ref_type s1 on s1.internal_code=ue.s_ref_type_meta
				where u.internal_code= {$schema_id}
				 and  is_multiple_group=0 order by ifnull(ug.sequence,0),ue.sequence,ue.internal_code )";
				 $lobj_details = $aobj_context->mobj_db->GetAll($get_headers);
   
	$row=0;
	$cell=1;
	  $worksheet->write($row,0, "internal_code");
  foreach($lobj_details as $key=> $value)
   {
	if($value[optional]==1)
	{
    $worksheet->write($row,$cell, $value[element_name]);
	}
	else
	{
	 $worksheet->write($row,$cell, $value[element_name],$Title_Center);
	}
    $cell++; 		                
	} 

   	$workbook->send($schema_id.'.xls','D');	
	$workbook->close();  
          
 
}

function getGroupModuleDetailsHeaders($aobj_context)
{
	$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
	$schema_id = $aobj_context->mobj_data["schema_id"];
	$group_id = $aobj_context->mobj_data["group_id"];
	$group_name = $aobj_context->mobj_data["group_name"];
	$group_entry_table_name = $aobj_context->mobj_data["group_entry_table_name"];
	$main_src_obj=(explode("/",$_SERVER["REQUEST_URI"]));
	$main_src=$main_src_obj[1];
	$excel_writer=$aobj_context->main_src."/Excel_Writer/Writer.php";
	include($excel_writer);
	$workbook = new Spreadsheet_Excel_Writer();
	$worksheet =& $workbook->addWorksheet();
	
	$get_headers="(select 
				ue.internal_code,
				ue.name as element_name
				from s_user_schema_elements ue
				 where ue.ref_s_user_schema_code = {$schema_id} and ref_s_user_schema_group_code = {$group_id}
				 and  is_multiple_group=1 and is_visible=1 and ifnull(ue.is_defined,1)=1
				 order by ue.sequence,ue.internal_code )";
				 
	$lobj_details = $aobj_context->mobj_db->GetAll($get_headers);
	
	
	$row=0;
	$cell=1;
	  $worksheet->write($row,0, "internal_code");
  foreach($lobj_details as $key=> $value)
   {
   $worksheet->write($row,$cell, $value[element_name]);
    $cell++; 		                
	} 

   	$workbook->send($schema_id.'.xls','D');	
	$workbook->close();  
          
 
}


function downloadModuleData($aobj_context)
{
	$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
	$schema_id = $aobj_context->mobj_data["schema_id"];
	$main_src_obj=(explode("/",$_SERVER["REQUEST_URI"]));
	$main_src=$main_src_obj[1];
	$excel_writer=$aobj_context->main_src."/Excel_Writer/Writer.php";
	require_once($excel_writer);
	$workbook = new Spreadsheet_Excel_Writer();
	$workbook->setVersion(8);

	$worksheet =& $workbook->addWorksheet('sheet 1');
	
	$get_data_type="SELECT name,t.value
					from 
					s_user_schema_elements s
					inner join s_ref_type t
					on t.internal_code=data_type
					where ref_s_user_schema_code={$schema_id}";
	$obj_get_data_type =  $aobj_context->mobj_db->GetAll($get_data_type);
		 $data_type_arr= array();		
		for($k=0;$k<count($obj_get_data_type);$k++)
		{
		$data_type_arr[$obj_get_data_type[$k]['name']]=$obj_get_data_type[$k]['value'];
		}
		 
	$aobj_context->mobj_db->execute("SET @@session.group_concat_max_len=100000");
	$aobj_context->mobj_db->execute("set @a:=0");
 		$select_ref_module_code="select 
			GROUP_CONCAT(join_table_name SEPARATOR ' ') as qry from 
			(select   @a:=@a+1,ref_module_code,
			concat(' left join module_',ref_module_code, ' as module_',ref_module_code,'_',internal_code,' on module_',ref_module_code,'_',internal_code,'.internal_code =module_',ref_s_user_schema_code,'.m_',ref_s_user_schema_code, '_ele_',internal_code) as join_table_name
			from s_user_schema_elements
			where ref_s_user_schema_code={$schema_id}
			and is_multiple_group=0 and ref_module_code>0)j";
		$obj_select_ref_module_code =  $aobj_context->mobj_db->GetRow($select_ref_module_code);
				 
		$join_qry=(count($obj_select_ref_module_code)>0)?$obj_select_ref_module_code[qry]:'';
	$group_table=" select GROUP_CONCAT(group_table  SEPARATOR ' ') as group_table from 
						(select concat(' left join user_schema_',ref_s_user_schema_code,'_group_',internal_code, ' as ug_',internal_code,' on ',' ug_',internal_code,'.ref_s_ref_data_code=module_',ref_s_user_schema_code,'.internal_code') as group_table
						 from s_user_schema_group where ref_s_user_schema_code={$schema_id} and multiple=1)d";
						 
	$obj_group_table =  $aobj_context->mobj_db->GetRow($group_table);	
	
		if(!is_null($obj_group_table[group_table]))
		{
		$group_table_join_qry=$obj_group_table[group_table];
		$get_group_display_cols="select GROUP_CONCAT(field_name) as field_name  from (select table_name,name,
									field_name as grid_field_name,
									if(ref_module_code=0,concat('ug_',group_int_code,'.',field_name,' as ',\"'\",name,\"'\"),
									concat('module_',ref_module_code,'_',ele_ic_code,'.',field_id1,' as ',\"'\",name,\"'\")) as field_name
									,ref_module_code from 
									(select  s.internal_code, ref_module_code,concat('module_',s.internal_code) as table_name ,
									field_id1,ref_s_user_schema_group_code as group_int_code,su.internal_code as ele_ic_code,
									su.name,ifnull(multiple,0) as multiple,ifnull(ug.name,'0None') as group_name,
									db_field as field_name 
									from s_user_schema s inner join s_user_schema_elements su on 
									su.ref_s_user_schema_code=s.internal_code
									inner join s_ref_type t on t.internal_code=su.s_ref_type_meta
									left join s_user_schema_group ug on ug.internal_code=su.ref_s_user_schema_group_code
									left join module_meta_columns mmc on mmc.module_id=su.ref_module_code
									where  s.internal_code={$schema_id} and t.value='Meta'
									 order by ug.sequence,su.sequence,su.internal_code ) l 
									 where   multiple=1
									 ) a ";
		 				 		 
		$lobj_get_group_display_cols = $aobj_context->mobj_db->GetRow($get_group_display_cols);
		 $group_table_dis_cols=','.$lobj_get_group_display_cols[field_name];
		
		$aobj_context->mobj_db->execute("SET @@session.group_concat_max_len=100000");
		$aobj_context->mobj_db->execute("set @a:=0");
	 	$select_group_table_ref_module_code="select GROUP_CONCAT(join_table_name SEPARATOR ' ') as qry from 
		(select   ref_module_code,
		concat(' left join module_',ref_module_code, ' as module_',ref_module_code,'_',sus.internal_code,' on module_',ref_module_code,'_',sus.internal_code,'.internal_code =ug_',ref_s_user_schema_group_code,'.',db_field) as join_table_name
		from s_user_schema_elements sus
		inner join s_user_schema s on s.internal_code=sus.ref_s_user_schema_code
		where s.internal_code={$schema_id}
		and is_multiple_group=1 and ref_module_code>0)j";
		$obj_select_group_table_ref_module_code =  $aobj_context->mobj_db->GetRow($select_group_table_ref_module_code);
		$group_table_ref_module= (!is_null($obj_select_group_table_ref_module_code[qry]))?$obj_select_group_table_ref_module_code[qry]:'';
		}
		else
		{
		$group_table_join_qry='';
		$group_table_dis_cols='';
		}
	
	$get_display_cols="select GROUP_CONCAT(field_name) as field_name  from 
							(select table_name,name,field_name as grid_field_name,
							if(ref_module_code<>0,
							concat(\"concat(\",f1,f2,\") as \",'\'',name,'\''),
							if(data_type='Date',
							concat('DATE_FORMAT(','module_',internal_code,'.',field_name,',\'%d-%b-%Y\') as  \'',name,'\''),
											
							concat('module_',internal_code,'.',field_name,' as \"', name,'\"'))) as field_name
							,ref_module_code from 
							(select  s.internal_code, su.internal_code as ele_id,t1.value as data_type,
							ref_module_code,concat('module_',s.internal_code) as table_name ,
							field_id,	
							if(ifnull(field_id1,'')!='',concat('module_',ref_module_code,'_',su.internal_code,'.',field_id1),'') as f1,
							if(ifnull(field_id2,'')!='',concat(',\'-\',module_',ref_module_code,'_',su.internal_code,'.',field_id2),'') as f2,
							su.name,
							ifnull(multiple,0) as multiple,ifnull(ug.name,'0None') as group_name,
							concat('m_',s.internal_code,'_ele_',su.internal_code) as field_name 
							from s_user_schema s inner join s_user_schema_elements su on 
							su.ref_s_user_schema_code=s.internal_code
							inner join s_ref_type t on t.internal_code=su.s_ref_type_meta
							inner join s_ref_type t1 on t1.internal_code=data_type
							left join s_user_schema_group ug on ug.internal_code=su.ref_s_user_schema_group_code
							left join module_meta_columns mmc on mmc.module_id=su.ref_module_code
							where s.internal_code={$schema_id} and t.value='Meta'
							order by ug.sequence,su.sequence,su.internal_code ) l 
							where   multiple=0 
							) a ";
		$lobj_get_display_cols = $aobj_context->mobj_db->GetRow($get_display_cols);
		 //echo $get_display_cols; die();
		ini_set("memory_limit",-1);
	ini_set('max_execution_time', 1400);
		$select_data="(select  module_{$schema_id}.internal_code as internal_code,
							{$lobj_get_display_cols[field_name]} {$group_table_dis_cols}
							from   module_{$schema_id} {$join_qry} {$group_table_join_qry} {$group_table_ref_module} ) ";
		//echo $select_data; die();					
	 	$lobj_details = $aobj_context->mobj_db->GetAll($select_data);	
		 
 	  
	$header_row=(array_keys($lobj_details[0]));
 	$header_row_cell_indx=0;
	foreach($header_row as $key1=> $value1)
   {
    $worksheet->write(0,$header_row_cell_indx,$value1);
	$header_row_cell_indx++;	 
   }
      $xlsRow = 1;
	  
     foreach($lobj_details as $key=> $value)
	{
		$xlscol=0;
	 
	    foreach($value as $key1=> $value1)
	    {
		if($data_type_arr[$key1]=='Number' or $key1=='internal_code' or $data_type_arr[$key1]=='Date')
		 $worksheet->write($xlsRow,$xlscol,$value1);
		else
		 $worksheet->writeString($xlsRow,$xlscol,$value1);
	     $xlscol++;
		}
		 $xlsRow++; 
	}	         
	$workbook->close();	
$workbook->send("report_{$schema_id}.xls");	
}


function uploadModuleGroupDetailsIntoDB($aobj_context)
 {
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
	$schema_id = $aobj_context->mobj_data["schema_id"];
	$table_name='module_'.$schema_id;
	$login_ctr = $aobj_context->mobj_data["login_ctr"];
	$group_id = $aobj_context->mobj_data["group_id"];
	$group_name = $aobj_context->mobj_data["group_name"];
	
	$user_id = $_SESSION['user_id'];
	//$schema_id = $aobj_context->mobj_data["schema_id"];
	
	ini_set("memory_limit",-1);
	ini_set('max_execution_time', 1400);
			
	require_once($aobj_context->main_src."/custom_src/inventory_master.php");
	$obj_master=new inventoryMaster($aobj_context);
	$obj_master->getBillofMaterialDetails();
	$obj_master->getItemMaster();
	$obj_master->getStageMaster();
		
	$main_src_obj=(explode("/",$_SERVER["REQUEST_URI"]));
	$main_src=$main_src_obj[1];
	$root_file_name=$aobj_context->main_src."/imported_files/module_details_{$schema_id}";
	$root_file_name = $root_file_name . $_SESSION['user_id']."_".basename( $_FILES['module_group_details_uploaded_file']['name']); 
	 
	$failed=0;
 
 
       if(isset($_FILES['module_group_details_uploaded_file']['name']) && $_FILES['module_group_details_uploaded_file']['name'] != '')         
        { 
		
		if(move_uploaded_file($_FILES['module_group_details_uploaded_file']['tmp_name'],$root_file_name))
		  {
		 
		$reader_path=$aobj_context->main_src."/Excel_reader/reader.php";
		require_once $reader_path;
		$data = new Spreadsheet_Excel_Reader();
		$data->setOutputEncoding('CP1251');
		$data->read($root_file_name);
		error_reporting(E_ALL ^ E_NOTICE);
		$header_cols="";
		
		
	  //	echo '<pre>';
		
		//print_r($data->sheets[0]['cells']);
		
		//die();
		
		for($bil = 2 ; $bil <= count($data->sheets[0]['cells']) ; $bil++ )
		 {
		      
			  $stage = $data->sheets[0]['cells'][$bil][2];
			  $item = $data->sheets[0]['cells'][$bil][3];
			  $qty = $data->sheets[0]['cells'][$bil][4];
			  $alt_qty = $data->sheets[0]['cells'][$bil][5];
			  $alt_qty = (empty($alt_qty))?0:$alt_qty;
			  $reference = $data->sheets[0]['cells'][$bil][6];
			  $notes = $data->sheets[0]['cells'][$bil][7];
			  		 		  
              $stage_int_code = getStageCode($aobj_context,$obj_master,$stage);
              $item_int_code = getItemCode($aobj_context,$obj_master,$item);
			  		  
			  if(empty($stage_int_code))
			    continue;
			  if(empty($item_int_code))
			    continue;
				
			  $get_dup_code = "select internal_code from {$obj_master->billofmaterial_entry_table}
			                   where {$obj_master->billofmaterial_item} = {$item_int_code} 
							   and {$obj_master->billofmaterial_stage} = {$stage_int_code}
							   and login_cnt = {$login_ctr}
							   							   
							   ";	
			 	
			  $lobj_get_dup_code = $aobj_context->mobj_db->GetRow($get_dup_code);
				
			  if(!empty($lobj_get_dup_code[internal_code]))	
				 continue;
							 				  
			  $insert_bill_group_details = "INSERT INTO {$obj_master->billofmaterial_entry_table}
																(
																	internal_code,
																	login_cnt,
																	group_int_code,
																	ref_s_user_schema_code,
																	ref_s_user_schema_group_code,
																	ref_s_ref_data_code,
																	{$obj_master->billofmaterial_item},
																	{$obj_master->billofmaterial_quantity},
																	{$obj_master->billofmaterial_alt_quantity},
																	{$obj_master->billofmaterial_refference},
																	{$obj_master->billofmaterial_notes},
																	{$obj_master->billofmaterial_stage},
																	created_by,
																	created_date,
																	modified_by,
																	modified_date
																																
																)
														 values (0,
														         {$login_ctr},
																 0,
																 {$schema_id},
																 {$group_id},
																 0,
																 {$item_int_code},
																 {$qty},
																 {$alt_qty},
																 '{$reference}',
																 '{$notes}',
																 {$stage_int_code},
																'{$user_id}',
																  now(),
																'{$user_id}',
																   now()
																 )";
																 
           // echo $insert_bill_group_details;
           // die();			

				
			  $lobj_insert_bill_group_details = $aobj_context->mobj_db->execute($insert_bill_group_details); 
		  		 
		 
		 }
	   }
    }
	
	 unlink($root_file_name);
	 unset($_FILES);
	 
	$lobj_get_ids = 'Successfully Uploaded' ;
	 
    echo $aobj_context->mobj_output->ToJSONEnvelope($lobj_get_ids,0,"success");  
 }
 
function getStageCode($aobj_context,$obj_master,$stage)
 {
      $find_stgae_code = "select internal_code
					           from {$obj_master->stage_master_table} where {$obj_master->stage_master_name} = '{$stage}'";
												
	  $lobj_find_stgae_code = $aobj_context->mobj_db->GetRow($find_stgae_code);
	   
	  return  $lobj_find_stgae_code[internal_code]; 

 }

function getItemCode($aobj_context,$obj_master,$item)
 {
 
      $find_item_code = "select internal_code
					           from {$obj_master->item_master_table} 
					    where {$obj_master->item_master_name} = '{$item}' or {$obj_master->item_master_code} = '{$item}' 
						      or concat({$obj_master->item_master_name},'-',{$obj_master->item_master_code}) ='{$item}'  ";
							  
												
	  $lobj_find_item_code = $aobj_context->mobj_db->GetRow($find_item_code);
      return $lobj_find_item_code[internal_code];
 
 }

function uploadModuleDetailsIntoDB($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
	$schema_id = $aobj_context->mobj_data["schema_id"];
	 $table_name='module_'.$schema_id;
	$aobj_context->mobj_data["ref_s_user_schema_code"] = $schema_id;
	ini_set("memory_limit",-1);
	ini_set('max_execution_time', 1400);
	
	
	$main_src_obj=(explode("/",$_SERVER["REQUEST_URI"]));
	$main_src=$main_src_obj[1];
	$root_file_name=$aobj_context->main_src."/imported_files/module_details_{$schema_id}";
	$root_file_name = $root_file_name . $_SESSION['user_id']."_".basename( $_FILES['module_details_uploaded_file']['name']); 
	 
	$failed=0;
 
 
        if(isset($_FILES['module_details_uploaded_file']['name']) && $_FILES['module_details_uploaded_file']['name'] != '')         
        { 
		
		if(move_uploaded_file($_FILES['module_details_uploaded_file']['tmp_name'],$root_file_name))
		  {
		 
		$reader_path=$aobj_context->main_src."/Excel_reader/reader.php";
		require_once $reader_path;
		$data = new Spreadsheet_Excel_Reader();
		$data->setOutputEncoding('CP1251');
		$data->read($root_file_name);
		error_reporting(E_ALL ^ E_NOTICE);
		$header_cols="";
		 
		for ($i = 1; $i <=1; $i++) 
				{
					for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
						$xls_header_columns[$j]=strtolower($data->sheets[0]['cells'][$i][$j]);
						$header_cols.="'{$xls_header_columns[$j]}',";
					}					
				}
				$header_cols=substr($header_cols,0,strlen($header_cols)-1);
				//getting the column through the query
				 $get_details="(select 
				ue.internal_code,is_unique,
				ue.name as element_name,
				s.value as Type,
				ifnull(ug.name,'0None') as group_name,
				if(ug.internal_code is null,u.internal_code,concat(u.internal_code,'_',ug.internal_code)) as user_right_group,
				description,s1.value as meta_data,decimals,
				if(s1.value='Meta',concat('m_',u.internal_code,'_ele_',ue.internal_code),'') as meta_field,
				default_value,
				optional,list_of_values,
				SUBSTRING(list_of_values,1, LOCATE('|',list_of_values)-1) as first_lov,
				u.internal_code as ref_s_user_schema_code,db_field as field_id,
				ue.ref_module_code ,is_read_only,
				IFNULL(ug.internal_code,0) as ref_s_user_schema_group_code,ifnull(multiple,0) as multiple,
				concat('user_schema_',u.internal_code ,'_group_',IFNULL(ug.internal_code,0)) as table_id,
				ifnull(suffix,'None') as suffix,width*9 as width,linked_field,source_field,
				if(ue.ref_module_code>0,concat('module_',ue.ref_module_code),'') as ref_table_name 
				from s_user_schema_elements ue
				inner join s_user_schema u on u.internal_code=ue.ref_s_user_schema_code
				left join s_user_schema_group ug on ug.internal_code=ref_s_user_schema_group_code
				left join s_ref_type s on s.internal_code=ue.data_type
				left join s_ref_type s1 on s1.internal_code=ue.s_ref_type_meta
				where u.internal_code= {$schema_id}
				and ue.name in ({$header_cols})
				 and  is_multiple_group=0 order by ifnull(ug.sequence,0),ue.sequence,ue.internal_code )";
 
		 
			 
			$lobj_get_name=$aobj_context->mobj_db->GetAll($get_details);
			
			$module_meta_insert_qry="insert into module_{$schema_id} (ref_s_user_schema_code,";
			$module_user_insert_qry="insert into module_{$schema_id}_details (
									ref_s_user_schema_code,
									ref_s_module_code,
									ref_s_user_schema_group_code,
									ref_s_user_schema_elements_code,
									value,
									ref_ref_s_user_schema_code,
									created_by,
									created_date,
									modified_by,
									modified_date
									) 
									values (";
				$input_fields_arr="\$input_fields_arr=array (";					
				foreach ($lobj_get_name as $key=>$value)
				{
					if($value['meta_data']=='Meta' )
					{
					 
					$module_meta_insert_qry.=$value['meta_field'].",";
					$input_fields_arr.="'{$value['meta_field']}'=>'0',\n";
					}
					 
				}
				$input_fields_arr.=");";
				$module_meta_insert_qry=substr($module_meta_insert_qry,0,strlen($module_meta_insert_qry) - 1);
		 		 $module_meta_insert_qry= $module_meta_insert_qry.",created_by,
					created_date,
					modified_by,
					modified_date) values ( {$schema_id},";
				//Ends getting the Data Cols	
					
					
					
					$l=1;	$m=0; 
					$db_header_columns[0]="internal_code";
		foreach ($lobj_get_name as $key=>$value)
			{
			$db_header_columns[$l]=strtolower($value[element_name]);
			$l++;
			}
			$result = array_diff($xls_header_columns, $db_header_columns);
			 
			  
			 if(!empty($result))
			{
			foreach($result as $key=>$value)
			{
			$error_data.="\"".strtoupper($value)."\" column are not matching with DB Column";
			$error_data.="\n";
			}
			header("Content-type:application/txt");
			header("Content-Disposition:attachment;filename=error_log");  
			//readfile($root_file_name);
			echo $error_data;
			return;
			}    
		 session_start();
		$validation_file_name=$aobj_context->main_src."/validation_files/validate_".$_SESSION['g_file_id'].".php"; 
		$format_file=$aobj_context->main_src."/src/format.php"; 
				//$update_qry="update {$table_name} set "		
				$data_str="<?php \n";

		
					$data_str.=" if (file_exists('$validation_file_name')) {
					require_once('$validation_file_name');
					}
						require_once('$format_file');	";
			
				$data_str.="\$s_ref_error_flag=0; \n";
				$data_str.="\$validate_ref_error_flag=0; \n";
				$data_str.="  {$input_fields_arr} \n";
				$data_str.="\$original_value=''; \n";
				$data_str.="\$error_msg=''; \n";
				$data_str.="\$down_load_value=''; \n";
				$data_str.="\$validation=''; \n";
				$data_str.="\$header_value='Following Records are not inserted'.\"\\n\"; \n";
			 	$data_str.="for (\$i = 2; \$i <= \$data->sheets[0]['numRows']; \$i++) 
				{
				\$values='';
				\$original_value='';
				\$validation='' \n;
				\$error_msg='';
				\$validate_ref_error_flag=0;
				\$s_ref_error_flag=0;
					for (\$j = 1; \$j <= \$data->sheets[0]['numCols']; \$j++) {
					if(\$i==2)
					{
					\$header_value.=\$data->sheets[0]['cells'][1][\$j].\",\";
					} \n
					";
					//To Map Internal_code
			$data_str.="if(strtolower(\$data->sheets[0]['cells'][1][\$j])==strtolower('internal_code')) { \n ";
					$data_str.="\$original_value.=(\$data->sheets[0]['cells'][\$i][\$j]).','; \n";
					$data_str.="\$internal_code=trim(\$data->sheets[0]['cells'][\$i][\$j]); \n";
					$data_str.="\$internal_code=empty(\$internal_code)?0:\$internal_code; \n
					}";
					
				$cell_row=1;
			 foreach ($lobj_get_name as $key=>$value)
			{
				if(1)
				{
					if($value[is_unique]==1)
					{
					$update_cond.="{$value[field_id]} = '{\$".$value[field_id]."}' and ";
					}
					if($value[ref_module_code]!=0 && $value[meta_data]=='Meta')
					{
					$qry=" select table_name,field_name as name,field_id1 ,field_id2 
					from module_meta_columns WHERE module_id={$value[ref_module_code]}"; 
			  
					$lobj_rs = $aobj_context->mobj_db->GetRow($qry);
					$data_str.="\$where_cond1='';\$where_cond2='';\$concat_cond='';";
					 
					 $data_str.="\n if(strtolower(\$data->sheets[0]['cells'][1][\$j])==strtolower('{$value[element_name]}')) \n { \n";
					 $data_str.="\$cell_value=addslashes(\$data->sheets[0]['cells'][\$i][\$j]); \n";
					 $data_str.="\$original_value.=(\$data->sheets[0]['cells'][\$i][\$j]).','; \n";
					 $data_str.="\$where_cond1=\"{$lobj_rs[field_id1]} like '{\$cell_value}'\"; \n";
					//$where_cond1= $data[".$k."] ;
					
					 
					if(!empty($lobj_rs[field_id2]))
					{
					$data_str.="\$where_cond2=\" or  {$lobj_rs[field_id2]} like '{\$cell_value}'\"; \n";
					$data_str.="\$concat_cond=\" or  concat({$lobj_rs[field_id1]},' - ',{$lobj_rs[field_id2]})  like '{\$cell_value}'\"; \n";
					}
					$data_str.="\$select_ref_data_{$value[ref_module_code]}=\"select internal_code from {$value[ref_table_name]} where {\$where_cond1}  {\$where_cond2} {\$concat_cond}\"; \n";
					 $data_str.="\$obj_select_ref_data_{$value[ref_module_code]} =  \$aobj_context->mobj_db->GetRow(\$select_ref_data_{$value[ref_module_code]}); \n";
					if($value[optional]=='0')
					{
					$data_str.=" if(empty(\$cell_value)) \n
							{ \n
							\$s_ref_error_flag=1; \n
							\$error_msg.='{$value[element_name]} is Mandatory:'; \n
							} \n";
					}
					$data_str.=" if(empty(\$obj_select_ref_data_{$value[ref_module_code]}) && !empty(\$data->sheets[0]['cells'][\$i][\$j])) \n
							{ \n
							\$s_ref_error_flag=1; \n
							\$error_msg.='{$value[element_name]} not Found:'; \n
							} \n";
					 $data_str.="$".$value[field_id]."  = empty(\$obj_select_ref_data_{$value[ref_module_code]}[internal_code])?0:\$obj_select_ref_data_{$value[ref_module_code]}[internal_code]; \n ";
							$data_str.="\$validation.=\"{$value[field_id]}->\".\${$value[field_id]}.'~';";
							$data_str.="\$input_fields_arr['{$value[field_id]}']=\${$value[field_id]};";
							$data_str.=" } \n ";
					$insert_value.="'{\$".$value[field_id]."}',";
					$update_cond_value.=$value[field_id]."= '{\$".$value[field_id]."}' , ";
					 
					$k++;
					}
					else if($value[Type]=='List Of Values' && $value[meta_data]=='Meta')
					{
					
					$data_str.="if(strtolower(\$data->sheets[0]['cells'][1][\$j])==strtolower('{$value[element_name]}')) { \n ";
					$data_str.="\$original_value.=(\$data->sheets[0]['cells'][\$i][\$j]).','; \n";
					 $data_str.="\$lov_val= \$data->sheets[0]['cells'][\$i][\$j];";
					 
					 $data_str.="$".$value[field_id]."  = (empty(\$lov_val))?'".$value[first_lov]."':\$lov_val;\n ";
					 $data_str.="\$validation.=\"{$value[field_id]}->\".\${$value[field_id]}.'~'; \n";
					 $data_str.="\$input_fields_arr['{$value[field_id]}']=\${$value[field_id]};";
					  $data_str.=" } \n";
					 $insert_value.="'{\$".$value[field_id]."}',";
					 $update_cond_value.=$value[field_id]."= '{\$".$value[field_id]."}' , ";
					 
					$k++;
					}
					else if($value[Type]=='Date' && $value[meta_data]=='Meta' )
					{					 
					 $data_str.="if(strtolower(\$data->sheets[0]['cells'][1][\$j])==strtolower('{$value[element_name]}')) { \n ";
					 $data_str.="\$original_value.=(\$data->sheets[0]['cells'][\$i][\$j]).\",\"; \n";
					 $data_str.="\$date_obj  = addslashes(\$data->sheets[0]['cells'][\$i][\$j]) ; \n  \n";
					 if($value[optional]=='0')
						{
						$data_str.=" if(empty(\$date_obj)) \n
								{ \n
								\$s_ref_error_flag=1; \n
								\$error_msg.='{$value[element_name]} is Mandatory:'; \n
								} \n";
						}
						
					 $data_str.="if(empty(\$date_obj))
						{
						\${$value[field_id]}='0000-00-00';
						}
						else {
						";
					
					 $data_str.="$".$value[field_id]."  = GetDbDateFormatFromExcel(\$date_obj);}\n";
					  $data_str.="\$validation.=\"{$value[field_id]}->\".\${$value[field_id]}.'~'; \n";
					  $data_str.="\$input_fields_arr['{$value[field_id]}']=\${$value[field_id]};";
					 $data_str.="}";
					$insert_value.="'{\$".$value[field_id]."}',";
					$update_cond_value.=$value[field_id]."= '{\$".$value[field_id]."}' , ";
				 
					$k++;
					}
					else if($value[meta_data]=='Meta' && $value[Type]=='Number')
					{					 
					 $data_str.="if(strtolower(\$data->sheets[0]['cells'][1][\$j])==strtolower('{$value[element_name]}')) { \n ";
					 $data_str.="\$original_value.=(\$data->sheets[0]['cells'][\$i][\$j]).\",\"; \n";
					 $data_str.="$".$value[field_id]."  = addslashes(\$data->sheets[0]['cells'][\$i][\$j]); \n ";
					  if($value[optional]=='0')
						{
						$data_str.=" if(empty(\${$value[field_id]})) \n
								{ \n
								\$s_ref_error_flag=1; \n
								\$error_msg.='{$value[element_name]} is Mandatory:'; \n
								} \n";
						}
						
					 $data_str.="\${$value[field_id]}=empty(\${$value[field_id]})?0:\${$value[field_id]};";
					 $data_str.="\$validation.=\"{$value[field_id]}->\".\${$value[field_id]}.'~'; \n";
					 $data_str.="\$input_fields_arr['{$value[field_id]}']=\${$value[field_id]};";
					 $data_str.=" } \n";
					$insert_value.="'{\$".$value[field_id]."}',";
					$update_cond_value.=$value[field_id]."= '{\$".$value[field_id]."}' , ";
					 
					$k++;
					}else if($value[meta_data]=='Meta' && ($value[Type]=='Text' or $value[Type]=='Large Text'))
					{					 
					 $data_str.="if(strtolower(\$data->sheets[0]['cells'][1][\$j])==strtolower('{$value[element_name]}')) { \n ";
					 $data_str.="\$original_value.=(\$data->sheets[0]['cells'][\$i][\$j]).\",\"; \n";
					 $data_str.="$".$value[field_id]."  = addslashes(\$data->sheets[0]['cells'][\$i][\$j]); \n ";
					  if($value[optional]=='0')
						{
						$data_str.=" if(empty(\${$value[field_id]})) \n
								{ \n
								\$s_ref_error_flag=1; \n
								\$error_msg.='{$value[element_name]} is Mandatory:'; \n
								} \n";
						}
					 $data_str.="\$validation.=\"{$value[field_id]}->\".\${$value[field_id]}.'~'; \n";
					  $data_str.="\$input_fields_arr['{$value[field_id]}']=\${$value[field_id]};";
					 $data_str.=" } \n";
					$insert_value.="'{\$".$value[field_id]."}',";
					$update_cond_value.=$value[field_id]."= '{\$".$value[field_id]."}' , ";
					 
					$k++;
					}
					// User Checking Starts Here
					else if( $value[meta_data]=='User' && $value[Type]=='List Of Values' )
					{ 
				
					 $data_str.="if(strtolower(\$data->sheets[0]['cells'][1][\$j])==strtolower('{$value[element_name]}')) { \n ";
					$data_str.="\$original_value.=(\$data->sheets[0]['cells'][\$i][\$j]).',';  \n";
					 $data_str.="\$user_details_array[{$m}][element_name]='$value[element_name]' ; \n";
					 $data_str.="\$user_details_array[{$m}][ref_s_user_schema_code]=$value[ref_s_user_schema_code]; \n";
					 $data_str.="\$user_details_array[{$m}][ref_s_user_schema_group_code]=$value[ref_s_user_schema_group_code]; \n";
					 $data_str.="\$user_details_array[{$m}][ref_s_user_schema_elements_code]=$value[internal_code]; \n";
					 $data_str.="\$user_details_array[{$m}][ref_ref_s_user_schema_code]=$value[ref_module_code]; \n";
					  $data_str.="\$pos1 = stripos(\"{$value[list_of_values]}\", \$data->sheets[0]['cells'][\$i][\$j]);
					  \n if(\$pos1===false)
						{ \n
							\$s_ref_error_flag=1; \n
							} \n else \n";
					 $data_str.="\$user_details_array[{$m}][value]=addslashes(\$data->sheets[0]['cells'][\$i][\$j]); \n } \n";
					 
					$m++;  
					$k++;
					}
					else if( $value[meta_data]=='User' && $value[ref_module_code]!=0 )
					{ 
					
					 
					 $qry=" select table_name,field_name as name,field_id1 ,field_id2 
					from module_meta_columns WHERE module_id={$value[ref_module_code]}"; 
			  
					$lobj_rs = $aobj_context->mobj_db->GetRow($qry);
					
					 
					 $data_str.="\n if(strtolower(\$data->sheets[0]['cells'][1][\$j])==strtolower('{$value[element_name]}')) \n { \n";
					$data_str.="\$original_value.=(\$data->sheets[0]['cells'][\$i][\$j]).',';  \n";
					$data_str.="\$user_details_array[{$m}][element_name]='$value[element_name]' ; \n";
					 $data_str.="\$user_details_array[{$m}][ref_s_user_schema_code]=$value[ref_s_user_schema_code]; \n";
					 $data_str.="\$user_details_array[{$m}][ref_s_user_schema_group_code]=$value[ref_s_user_schema_group_code]; \n";
					 $data_str.="\$user_details_array[{$m}][ref_s_user_schema_elements_code]=$value[internal_code]; \n";
					 $data_str.="\$user_details_array[{$m}][ref_ref_s_user_schema_code]=$value[ref_module_code]; \n";
					 $data_str.="\$where_cond1=\"{$lobj_rs[field_id1]} like '{\$data->sheets[0]['cells'][\$i][\$j]}'\"; \n";
					//$where_cond1= $data[".$k."] ;
					
					 
					if(!empty($lobj_rs[field_id2]))
					$data_str.="\$where_cond2=\" or  {$lobj_rs[field_id2]} like '{\$data->sheets[0]['cells'][\$i][\$j]}'\"; \n";
					
					$data_str.="\$select_ref_data_{$value[ref_module_code]}=\"select internal_code from {$value[ref_table_name]} where {\$where_cond1}  {\$where_cond2}\"; \n";
					 $data_str.="\$obj_select_ref_data_{$value[ref_module_code]} =  \$aobj_context->mobj_db->GetRow(\$select_ref_data_{$value[ref_module_code]}); \n";
					$data_str.=" if(empty(\$obj_select_ref_data_{$value[ref_module_code]})) \n
							{ \n
							\$s_ref_error_flag=1; \n
							} \n";
					 $data_str.="\$user_details_array[{$m}][value] = \$obj_select_ref_data_{$value[ref_module_code]}[internal_code]; \n } \n ";
					 
					  
					$m++;  
					$k++;
					}
					else if( $value[meta_data]=='User' && $value[Type]!='List Of Values'  && $value[ref_module_code]==0)
					{ 
					 $data_str.="if(strtolower(\$data->sheets[0]['cells'][1][\$j])==strtolower('{$value[element_name]}')) { \n ";
					 $data_str.="\$original_value.=(\$data->sheets[0]['cells'][\$i][\$j]).\",\"; \n";
					 $data_str.="\$user_details_array[{$m}][element_name]='$value[element_name]' ; \n";
					 $data_str.="\$user_details_array[{$m}][ref_s_user_schema_code]=$value[ref_s_user_schema_code]; \n";
					 $data_str.="\$user_details_array[{$m}][ref_s_user_schema_group_code]=$value[ref_s_user_schema_group_code]; \n";
					 $data_str.="\$user_details_array[{$m}][ref_s_user_schema_elements_code]=$value[internal_code]; \n";
					 $data_str.="\$user_details_array[{$m}][ref_ref_s_user_schema_code]=$value[ref_module_code]; \n";
					 $data_str.="\$user_details_array[{$m}][value]=addslashes(\$data->sheets[0]['cells'][\$i][\$j]); \n } \n";
					$m++;  
					$k++;
					}
					
			 	}
				
				 				
			}
			 
			
 			$update_cond_value=substr($update_cond_value,0,strlen($update_cond_value)-2) ;
			$final_update_qry.=" update {$table_name}  set ".$update_cond_value."\n"; 
			$who_fields_update=" ,modified_by=".$_SESSION['user_id'].",modified_date=now()";
			$update_cond=substr($update_cond,0,strlen($update_cond)-4) ;
			  $final_update_through_internal_code= $final_update_qry.$who_fields_update." where internal_code={\$internal_code}";  
			  
			  $final_update_qry.=$who_fields_update." where ".$update_cond;  
			  	  
				$insert_value=substr($insert_value,0,strlen($insert_value)-1);
				$who_fields=",".$_SESSION['user_id'].",now(),".$_SESSION['user_id'].",now() ";
				$data_str.="\n }   ";
				$data_str.=" \n \$validate_ref_error_flag=0; 
						  if (function_exists('validate'))
							{
							 \$aobj_context->mobj_data['save_schema_details_arr']='';
							 \$aobj_context->mobj_data['save_schema_details_arr']=\$input_fields_arr;\n
							 \$aobj_context->mobj_data['internal_code']=\$internal_code;\n
								validate(\$aobj_context,'upload');\n
								\$valide_arr=validate(\$aobj_context,'upload');\n
							 \$validate_ref_error_flag=empty(\$valide_arr['validate_ref_error_flag'])?0:\$valide_arr['validate_ref_error_flag'];
							 \$error_msg.= \$valide_arr['error_msg'];		
							};  ";
				$data_str.="if(\$s_ref_error_flag==0 && \$validate_ref_error_flag==0) \n
					{ \n  if(\$internal_code==0 ){ \n";
				
				$data_str.="\$insert_qry=\"".$module_meta_insert_qry.$insert_value.$who_fields.")\"; \n";
 							$data_str.="\$obj_qry =  \$aobj_context->mobj_db->Execute(\$insert_qry); \n 
						
							\$mysql_error=mysql_error();
							 
								 \$last_inserted_id  = \$aobj_context->mobj_db->Insert_ID(); \n 
								 	
							if(\$mysql_error !='') \n
						{ \n
						\$pos=strpos(\$mysql_error,'Duplicate');
						if(\$pos===false) 
						 {
						  \$down_load_value.=\$original_value.\",\".\$mysql_error .\"\\n\";
						 }
						 else
						{
						\$get_update_int_code=\"select internal_code from module_{$schema_id}  where {$update_cond}\";
						 \$obj_get_update_int_code =  \$aobj_context->mobj_db->getRow(\$get_update_int_code); \n 
						 \$last_inserted_id= \$obj_get_update_int_code[internal_code];
						 \$update_qry=\" {$final_update_qry} \"; \n 
						  \$obj_qry =  \$aobj_context->mobj_db->Execute(\$update_qry); \n 
						} \n  
						} \n
						} \n
						else
						{
						// TO CHECK USER 	RIGHT PERMINSSIONS
						   \$get_created_by_id=\" select created_by from module_{$schema_id} where 
						   internal_code=\$internal_code \"; \n 
						     \$obj_get_created_by_id =  \$aobj_context->mobj_db->GetRow(\$get_created_by_id); \n 
						   \$created_by_id=  \$obj_get_created_by_id[created_by];
						   \$edit_right=getUserEditRights(\$aobj_context, \$created_by_id);
						   if( \$edit_right=='1')
						   {
						   
						 \$update_qry=\" {$final_update_through_internal_code} \"; \n 
						  \$obj_qry =  \$aobj_context->mobj_db->Execute(\$update_qry); \n 
						   \$last_inserted_id =\$internal_code;
						   }
						   else
						   {
						   	\$original_value.=\"You Dont have a right to update this record\\n\";
						\$down_load_value.=\$original_value \n;
						   }
						}
						
						\n 
						  if (function_exists('postSave'))
							{
							 
								postSave(\$aobj_context,\$last_inserted_id,\$internal_code); 
							}
						if(\$last_inserted_id >0 && !empty(\$user_details_array))
						{
						\$insert_who_fields_for_details_table=\" ,\". \$_SESSION['user_id'].\", now(),\" . \$_SESSION['user_id'].\", now()\";	
						\$delete_qry=\"delete from module_{$schema_id}_details where ref_s_module_code=\$last_inserted_id\";
						 \$obj_delete_qry =  \$aobj_context->mobj_db->Execute(\$delete_qry); \n 
						foreach(\$user_details_array as \$k=>\$v)
							{
							 
									
							\$user_insert_qry=\"{$module_user_insert_qry} 
								'{\$v[ref_s_user_schema_code]}',
								'\$last_inserted_id',
								'{\$v[ref_s_user_schema_group_code]}',
								'{\$v[ref_s_user_schema_elements_code]}',
								'{\$v[value]}',
								'{\$v[ref_ref_s_user_schema_code]}' {\$insert_who_fields_for_details_table}) \";
							 \$obj_user_insert_qry =  \$aobj_context->mobj_db->Execute(\$user_insert_qry);   \n 
							//echo \$user_insert_qry;
							}
						}
						}";
				$data_str.="			
						else
					{
					\$original_value.=\",\$error_msg\\n\";
					\$down_load_value.=\$original_value \n;
					} \n";	
					
			$data_str.=" } "; 
			$data_str.=" if(!empty(\$down_load_value)) \n
			{ \n
			header(\"Content-type:application/csv\"); \n
			header(\"Content-Disposition:attachment;filename=error_log.csv\");   \n
			echo \$header_value.\"\\n\".\$down_load_value; \n
			return; \n
			}
			
			else \n
			echo \"<script type='text/javascript'> alert('File has been uploaded.');</script>\";";
		
			$data_str.=getUserRightsString($schema_id)." ?>";
			$tmp_root_file_name=$aobj_context->main_src."/imported_files/upload_module_details_qry_".$_SESSION['user_id'].".php";
			  file_put_contents($tmp_root_file_name,$data_str);
			require_once($tmp_root_file_name);
			    unlink($tmp_root_file_name);
			 unlink($root_file_name);
			unset($_FILES);
			}
			else 
			echo "cannot move the files";exit;
		}
}
function getUserRightsString($schema_id)
{
	$edit_right_str="
	function getUserEditRights(\$aobj_context, \$created_by_id)
	{
				\$edit_right=1;
				session_start();
					\$user_type=\$_SESSION['user_type'];
					\$user_id=\$_SESSION['user_id'];
					\$user_dept=\$_SESSION['user_dept'];
					if(strtolower(\$user_type)!='admin')
					{
					\$get_rights=\"select   is_edit,is_edit_group_records,is_edit_dep_records,is_edit_all_records
							from user_rights where ref_s_sysdb_code={\$user_id}
							and module_id={$schema_id}\";
							 
						 \$obj_get_rights = \$aobj_context->mobj_db->GetRow(\$get_rights);	
						 \$own=\$obj_get_rights[is_edit];
						 \$group=\$obj_get_rights[is_edit_group_records];
						 \$dep=\$obj_get_rights[is_edit_dep_records];
						\$all=\$obj_get_rights[is_edit_all_records];
						if(strtolower(\$user_type)=='admin' || \$all=='1')
						{
						\$edit_right=1;
						}
						else if(\$own=='1' && \$dep=='0') //checks for Own Records Only
						 {
							if(\$created_by_id==\$user_id)
							\$edit_right=1;
							else \$edit_right=0;
							 \$get_own_created_by_id=\"select created_by from {\$table} 
												where internal_code in ({\$lstr_ids})\";
							 \$obj_get_own_created_by_id = \$aobj_context->mobj_db->GetRow(\$get_own_created_by_id);
						}
						else if(\$dep=='1')//checks for Department Records Only
						 {
								\$get_users_of_department=\"select GROUP_CONCAT(ref_user_code) as dep_users
									from dept_user_mapping where ref_dept_code={\$user_dept}
									and ref_user_code={\$created_by_id}\";
								\$obj_u_dep = \$aobj_context->mobj_db->GetRow(\$get_users_of_department);	
								  if(\$obj_u_dep)
									\$edit_right=1;else \$edit_right=0;						  
						}	 
					}
	return \$edit_right;				
	}";
return $edit_right_str;
	}
	
?>