<?php
session_start();
$user_id=$_SESSION['LOGIN_USERID'];
include("../../php_crud/my_class.php");
$obj = new my_class();
extract($_REQUEST);
$date = strtoupper(date("d-M-y"));


if($operation == 'date_change'){
	$dt_change = "-";
	$dt_dot = ".";
	$input_date = $_POST['date'];
	$sel_in_dot = str_replace($dt_dot,$dt_change,$input_date);	

	$sel_in_date = str_replace("/",$dt_change,$sel_in_dot);
	$ret_msg .= "Raw date: $sel_in_date\n";
	$date_arr = explode($dt_change,$sel_in_date);
	if(strlen($date_arr[2])==2){
		$curr_year = date('Y');
		$curr_year_str = str_split($curr_year);
		$curr_year_two_digit = $curr_year_str[0].$curr_year_str[1];
		$sel_in_date =  $date_arr[0]."-".$date_arr[1]."-".$curr_year_two_digit.$date_arr[2];
		$ret_msg .= $sel_in_date."\n";
		$in_date = date('y-m-d', strtotime($sel_in_date));
		$curr_date = date('y-m-d');
		$ret_msg .= "\ninput date: $in_date\ncurr date: $curr_date";
	}
	else{
		$in_date = date('Y-m-d', strtotime($sel_in_date));
		$curr_date = date('Y-m-d');
		$ret_msg = "input date: $in_date\ncurr date: $curr_date";
	}

	$sel_in_date = date('d-M-y', strtotime($in_date));
	
	$ret_msg = 0;
	if(strtotime($in_date) > strtotime($curr_date)){
		$ret_msg = 0;
	}
	else{
		$ret_msg = strtoupper($sel_in_date);
	}
	echo $ret_msg;
}




if($operation=='save_multiple'){
	$up_success=0;
	$dt_change = "-";
	$data_arr = explode("#",$data_list);
	$in_date = strtoupper($data_arr[2]);
	$id_val = $data_arr[0];
	$form_data['VERIFIED_WL']= "'".$data_arr[1]."'";
	$form_data['STATUS']="'VR'";
	$form_data['VERIFIED_LOG_DATE']="TO_DATE('$in_date', 'DD-MON-RR')";
	$form_data['UPDATE_BY']="'$user_id'";
	$form_data['UPDATE_DATE']= "TO_DATE('$date','DD-MON-RR')";
	$table_name='MDM_WELL_DATA_LOG';
	//print_r($form_data);
	$where_cond = "ID='$id_val'";	
	$update = $obj->Update_Data($table_name, $form_data, $where_cond);
	if($update){
		$up_success++;
		$well_data_cols = "RAW_DATA_ID";
		$well_data_tbl = "MDM_WELL_DATA_LOG";
		$well_data_cond = "ID='$id_val'";
		$well_data_info = $obj->View_column_details_By_Cond($well_data_tbl,$well_data_cols,$well_data_cond);

		$raw_form_data['STATUS_FLAG']="'VR'";
		$raw_form_data['UPDATE_BY']="'$user_id'";
		$raw_form_data['UPDATE_DATE']= "TO_DATE('$date','DD-MON-RR')";
		$raw_where_cond = "ID='".$well_data_info['RAW_DATA_ID']."'";	
		$raw_update = $obj->Update_Data("MDM_SMS_INBOX_LOG", $raw_form_data, $raw_where_cond);
	}
	echo $up_success;
}

if($operation=='save_multiple_three_hourly_water_level'){
	$up_success=0;
	$dt_change = "-";
	$in_date = strtoupper($v_date);
	$id_val = $sel_id;
	$form_data['VERIFIED_HR6']=$sel_hr6_vwl;
	$form_data['VERIFIED_HR9']=$sel_hr9_vwl;
	$form_data['VERIFIED_HR12']=$sel_hr12_vwl;
	$form_data['VERIFIED_HR15']=$sel_hr15_vwl;
	$form_data['VERIFIED_HR18']=$sel_hr15_vwl;
	$form_data['VERIFIED_RAINFALL']=$sel_vrf;
	$form_data['STATUS']="'VR'";
	$form_data['VERIFIED_LOG_DATE']="TO_DATE('$in_date', 'DD-MON-RR')";
	$form_data['UPDATE_BY']="'$user_id'";
	$form_data['UPDATE_DATE']= "TO_DATE('$date','DD-MON-RR')";
	$table_name='MDM_WL_RAINFALL_DATA_LOG';
	//print_r($form_data);
	$where_cond = "ID='$id_val'";	
	$update = $obj->Update_Data($table_name, $form_data, $where_cond);
	if($update){
		$up_success++;
		$well_data_cols = "SMS_ID_LIST";
		$well_data_tbl = "MDM_WL_RAINFALL_DATA_LOG";
		$well_data_cond = "ID='$id_val'";
		$well_data_info = $obj->View_column_details_By_Cond($well_data_tbl,$well_data_cols,$well_data_cond);

		$raw_form_data['STATUS_FLAG']="'VR'";
		$raw_form_data['UPDATE_BY']="'$user_id'";
		$raw_form_data['UPDATE_DATE']= "TO_DATE('$date','DD-MON-RR')";
		$raw_where_cond = "ID='".$well_data_info['SMS_ID_LIST']."'";	
		$raw_update = $obj->Update_Data("MDM_SMS_INBOX_LOG", $raw_form_data, $raw_where_cond);
	}
	echo $up_success;
}

if($operation=='reject_hydro_data'){
	$dt_change = "-";
	$data_arr = explode("#",$data_list);
	$in_date = strtoupper($data_arr[2]);
	$id_val = $data_arr[0];
	$form_data['VERIFIED_WL']= "'".$data_arr[1]."'";
	$form_data['STATUS']="'R'";
	$form_data['VERIFIED_LOG_DATE']="TO_DATE('$in_date', 'DD-MON-RR')";
	$form_data['UPDATE_BY']="'$user_id'";
	$form_data['UPDATE_DATE']= "TO_DATE('$date','DD-MON-RR')";
	$table_name='MDM_WELL_DATA_LOG';
	//print_r($form_data);
	$where_cond = "ID='$id_val'";	
	$update = $obj->Update_Data($table_name, $form_data, $where_cond);
	if($update){
		$well_data_cols = "RAW_DATA_ID";
		$well_data_tbl = "MDM_WELL_DATA_LOG";
		$well_data_cond = "ID='$id_val'";
		$well_data_info = $obj->View_column_details_By_Cond($well_data_tbl,$well_data_cols,$well_data_cond);

		$raw_form_data['STATUS_FLAG']="'VR'";
		$raw_form_data['UPDATE_BY']="'$user_id'";
		$raw_form_data['UPDATE_DATE']= "TO_DATE('$date','DD-MON-RR')";
		$raw_where_cond = "ID='".$well_data_info['RAW_DATA_ID']."'";	
		$raw_update = $obj->Update_Data("MDM_SMS_INBOX_LOG", $raw_form_data, $raw_where_cond);
	}
	
}

if($operation=='update_sms_content'){
	$ret_msg = "";
//	echo $data_list;
	if(strpos($data_list, ',')){
		$data_arr = explode(",",$data_list);
		$up_success = 0;
		for( $i=0; $i<count($data_arr); $i++) {
			//$ret_msg .= $data_arr[$i]."<br/>";
			$input_arr = explode('#', $data_arr[$i]);
			$form_data['UP_SMS_CONTENT']="'".$input_arr[1]."'";
			$form_data['UPDATE_BY']="'$user_id'";
			$form_data['UPDATE_DATE']="'$date'";
			$table_name='MDM_SMS_INBOX_LOG';
			$where_cond="ID='".$input_arr[0]."'";	
			 $update = $obj->Update_Data($table_name, $form_data, $where_cond);
			if($update) {
				$up_success++;
				$raw_sms_info = $obj->Details_By_Cond($table_name,$where_cond);
				$text = $raw_sms_info['UP_SMS_CONTENT'];
				$mobile = $raw_sms_info['MOBILE_NUMBER'];
				$new_raw_id = $raw_sms_info['ID'];
				if(strpos($text, '*') === false){
					$hydro_res = hydrology_sms($text,$mobile,$new_raw_id,$obj,$user_id,$date);
				} else {
					//$text = "";
					$ffwc_res = ffwc_sms($text,$mobile,$new_raw_id,$obj,$user_id,$date); 
				}
			}
		}
	}
	else{
		$input_arr = explode('#', $data_list);
		//var_dump($input_arr);
		$form_data['UP_SMS_CONTENT']="'".$input_arr[1]."'";
		$form_data['UPDATE_BY']="'$user_id'";
		$form_data['UPDATE_DATE']="'$date'";
		$table_name='MDM_SMS_INBOX_LOG';
		$where_cond="ID='".$input_arr[0]."'";	
		$update = $obj->Update_Data($table_name, $form_data, $where_cond);
		if($update) {
			$up_success++;
			$raw_sms_info = $obj->Details_By_Cond($table_name,$where_cond);
			$text = $raw_sms_info['UP_SMS_CONTENT'];
			$mobile = $raw_sms_info['MOBILE_NUMBER'];
			$new_raw_id = $raw_sms_info['ID'];
			if(strpos($text, '*') === false){
			//	echo "line 120";
				$hydro_res = hydrology_sms($text,$mobile,$new_raw_id,$obj,$user_id,$date);
			} else {
			//	echo "line 123";
				$ffwc_res = ffwc_sms($text,$mobile,$new_raw_id,$obj,$user_id,$date); 
			}
		}
	}
	
	
	echo $up_success; 
	
}


if($operation=='delete_raw_sms'){
	$data_arr = explode(",",$data_list);
	$ret_msg = 0;
	for( $i=0; $i<count($data_arr); $i++) {
		$table_name='MDM_SMS_INBOX_LOG';
		$where_cond="ID='".$data_arr[$i]."'";	
		$del_res = $obj->Delete_Data($table_name,$where_cond);
		if($del_res) {
			$ret_msg++;
		}
	}
	echo $del_res; 
}

if($operation=='approve'){
	//print_r($data_list);
	$ret_msg = "";
	if(strpos($data_list,",")!=""){
		$data_arr = explode(",",$data_list);
	}
	else{
		$data_arr = $data_list;
	}
	//print_r($data_arr);
	$up_success = 0;
	
	for( $i=0; $i<count($data_arr); $i++) {
		if(count($data_arr) == 1){
			$input_arr = explode('#', $data_arr);
		}else{
			$input_arr = explode('#', $data_arr[$i]);
		}
		//print_r($input_arr);
		$in_date = strtoupper($input_arr[2]);
		$sel_id = trim($input_arr[0]);
		$form_data['VERIFIED_WL'] = "'".$input_arr[1]."'";
		$form_data['WELL_ID'] = "'".$input_arr[3]."'";
		$form_data['VERIFIED_LOG_DATE'] = "TO_DATE('$in_date', 'DD-MON-RR')";
		$form_data['STATUS'] =  "'A'";
		$form_data['TRANSFER_STATUS'] =  "'P'";
		$form_data['UPDATE_BY'] = "'$user_id'";
		$form_data['UPDATE_DATE'] = "TO_DATE('$date','DD-MON-RR')";
		//$form_data['UPDATE_DATE'] = "'"$date"'";
		// print_r($form_data);
		// exit();
		$table_name = 'MDM_WELL_DATA_LOG';
		$where_cond = "ID='$sel_id'";	
		$approve = $obj->Update_Data($table_name, $form_data, $where_cond);
		if($approve) {
			$well_data_cols = "ID,WELL_ID,LOG_DATE,VERIFIED_LOG_DATE,VERIFIED_WL,RAW_DATA_ID";
			$well_data_tbl = "MDM_WELL_DATA_LOG";
			$well_data_cond = "ID='$sel_id'";
			$well_data_info = $obj->View_column_details_By_Cond($well_data_tbl,$well_data_cols,$well_data_cond);
		
			$raw_form_data['STATUS_FLAG']="'VR'";
			$raw_form_data['UPDATE_BY']="'$user_id'";
			$raw_form_data['UPDATE_DATE']= "TO_DATE('$date','DD-MON-RR')";
			$raw_where_cond = "ID='".$well_data_info['RAW_DATA_ID']."'";	
			$raw_update = $obj->Update_Data("MDM_SMS_INBOX_LOG", $raw_form_data, $raw_where_cond);
			
			$newDate = $well_data_info['VERIFIED_LOG_DATE'];
			$newDate = strtoupper($newDate);
			$date_time = "";
				$date_time = "$newDate";
				$form_data_update['WELL_ID'] = "'".trim($well_data_info['WELL_ID'])."'";
				$form_data_update['DT'] = "TO_DATE('$newDate', 'DD-MON-RR')";
				$form_data_update['WATER_LVL'] = "'".$well_data_info['VERIFIED_WL']."'";
				$form_data_update['UPDT_BY'] = "'$user_id'";
				$form_data_update['UPDT_DTM'] = "TO_DATE('$date','DD-MON-YY  HH24:MI:SS')";
			 //  print_r($form_data_update);
			$update_cond = "WELL_ID='".trim($well_data_info['WELL_ID'])."' AND TO_CHAR(DT, 'DD-MON-YY')='$date_time'";
			$update2 = $obj->Update_Or_Insert_Data_By_Check_Duplicate_hydro('GWT_DATA_TBL', $form_data_update, $update_cond);
			if($update2){
				$up_transfer_stat_data['TRANSFER_STATUS'] = "'T'";
				$up_transfer_stat_data['UPDATE_BY'] = "'$user_id'";
				$up_transfer_stat_data['UPDATE_DATE'] = "TO_DATE('$date','DD-MON-RR')";
				$table_name = 'MDM_WELL_DATA_LOG';
				$where_cond = "ID='$sel_id'";
				$update = $obj->Update_Data($table_name, $up_transfer_stat_data, $where_cond);
			}
			if($update) {
				$up_success++;
			}
		}
	}
	echo $up_success;
	
}

function hydrology_sms($content,$mobile,$new_raw_id,$obj,$user_id,$curr_date) {
	$hydro_ret_msg = "";
	$data = explode(' ', $content);
    $others = array();
    $water_lvl = array();
    $date = array();
    for ($i = 0; $i <= count($data); $i++) {
      if (isset($data[$i])) {
        if ($i != 0) {
          $others[] = $data[$i];
          if (($i % 2) == 0) {
            if (isset($data[$i])) {
              $water_lvl[] = $data[$i];
            }
          } else {
            $date[] = $data[$i];
          }
        } else {
          $well_id = $data[$i];
        }
      }
    }
	/*set system flag*/
	$form_data['SYSTEM_FLAG']="'1'";
	$form_data['STATUS_FLAG']="'A'";
	$form_data['UPDATE_BY']="'$user_id'";
	$form_data['UPDATE_DATE']="'$curr_date'";
	$table_name='MDM_SMS_INBOX_LOG';
	$where_cond="ID='$new_raw_id'";	
	$update = $obj->Update_Data($table_name, $form_data, $where_cond);
	/*set system flag*/
	$new_well_id_info = $obj->View_column_details_By_Cond_hydro("GWM_TBL","WELL_ID","OLD_ID='$well_id'");
	$new_well_id = $new_well_id_info['WELL_ID'];
	if($new_well_id) {
		for ($j = 0, $l = 0; $j <= count($water_lvl), $l <= count($date); $j++, $l++) {
			if (isset($date[$l]) && isset($water_lvl[$j]) && isset($new_well_id)) {
				$new_well_id = trim($new_well_id);
				$date_arr = str_split($date[$l]);
				$day = $date_arr[0].$date_arr[1];
				$mon = $date_arr[2].$date_arr[3];
				$year = "20".$date_arr[4].$date_arr[5];
				$temp_date = "$day-$mon-$year";
				$sel_date =  date('d-M-y', strtotime($temp_date));
				$is_pending = $obj->Total_Count_By_Cond("MDM_WELL_DATA_LOG","WELL_ID='$new_well_id' AND LOG_DATE='$sel_date' AND STATUS='P'");
				//$hydro_ret_msg .= $is_pending."\n";
				//if($is_pending!=0){
					$hydro_form_data_update['WELL_ID'] = "'$new_well_id'";
					$hydro_form_data_update['LOG_DATE'] = "'$sel_date'";
					$hydro_form_data_update['WATER_LVL'] = "'".$water_lvl[$j]."'";
					$hydro_form_data_update['FLAG'] = "'1'";
					$hydro_form_data_update['STATUS'] = "'P'";
					$hydro_form_data_update['RAW_DATA_ID'] = "'$new_raw_id'";
					$hydro_form_data_update['OLD_WELL_ID'] = "'$well_id'";
					$hydro_cond = "WELL_ID='$new_well_id' AND LOG_DATE='$sel_date'";
					$update_wl_info = $obj->Update_Or_Insert_Data_By_Check_Duplicate("MDM_WELL_DATA_LOG", $hydro_form_data_update, $hydro_cond);
					$hydro_ret_msg .= $update_wl_info."\n";
			//	}
				
			}
		}
	}
	return $hydro_ret_msg;
	/*get new well id*/
}


function ffwc_sms($text,$mobile,$new_raw_id,$obj,$user_id,$curr_date) {
	/*set system flag*/
	$curr_year = date('Y');
	$curr_year_str = str_split($curr_year);
	$curr_year_two_digit = $curr_year_str[0].$curr_year_str[1];
	$form_data['UP_SMS_CONTENT']="'$text'";
	$form_data['SYSTEM_FLAG']="'2'";
	$form_data['STATUS_FLAG']="'A'";
	$form_data['UPDATE_BY']="'$user_id'";
	$form_data['UPDATE_DATE']="'$curr_date'";
	$table_name='MDM_SMS_INBOX_LOG';
	$where_cond="ID='$new_raw_id'";	
	$update = $obj->Update_Data($table_name, $form_data, $where_cond);
	$sms_id = $new_raw_id;
	/*set system flag*/

	$is_duplicate_sql = "";
	$ret_msg = "";
	$text = preg_replace('!\s+!', ' ', $text);
	$text = str_replace("'", "\'", trim($text));
	$sms_content = explode("*",$text);
	$station_id = strtoupper(trim($sms_content[0]));
	$is_valid_station_id = $obj->Total_Count_By_Cond("FFWC.STATIONS_TBL","UPPER(KEYWORD)='$station_id'");
	$date_length = strlen(trim($sms_content[1]));
	
	if($date_length==6 && $is_valid_station_id>0){
		$ret_msg=1;
		$sms_log_data['MOBILE_NUMBER'] = "'$mobile'";
		$sms_log_data['SMS_CONTENT'] = "'$text'";
		$sms_log_data['SMS_DATE_TIME'] = "'$datetime'";
		$sms_log_data['SYSTEM_FLAG'] = "'2'";
		$sms_log_data['STATUS_FLAG'] = "'VR'";
		$sms_log_data['DATA_READ_TYPE'] = "'A'";
		$sms_log_data['INSERT_BY'] = "'$user_id'";
		$sms_log_data['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";

	
		$sms_log_tbl = 'MDM_SMS_INBOX_LOG';
		$sms_log_ins = $obj->Insert_Data($sms_log_tbl, $sms_log_data);
		$process_res .= "insert correct sms log $sms_log_ins<br/>";
		$sms_id = $obj->get_last_row_id($sms_log_tbl);
		
		$high_low_wl_tbl = "FFWC.STATIONS_TBL";
		$high_low_wl_cols = "RECORDED_HIGH_WL,QC_MEAN_WL,STATION_CODE";
		$high_low_wl_cond = "DATA='WL' AND UPPER(KEYWORD)='$station_id'";
		$high_low_wl_info = $obj->View_column_details_By_Cond($high_low_wl_tbl,$high_low_wl_cols,$high_low_wl_cond);
		
		$hydro_station_id = $high_low_wl_info['STATION_CODE'];
		$high_wl = (float)$high_low_wl_info['RECORDED_HIGH_WL'];
		$low_wl = (float)$high_low_wl_info['QC_MEAN_WL'];
		
		$high_low_rf_tbl = "FFWC.STATIONS_TBL";
		$high_low_rf_cols = "QC_HIGH_RF,QC_LOW_RF";
		$high_low_rf_cond = "DATA='RF' AND UPPER(KEYWORD)='$station_id'";
		$high_low_rf_info = $obj->View_column_details_By_Cond($high_low_rf_tbl,$high_low_rf_cols,$high_low_rf_cond);
		
		$high_rf = (float)$high_low_rf_info['QC_HIGH_RF'];
		$low_rf = (float)$high_low_rf_info['QC_LOW_RF'];
		
		$process_res .= "Hydro Station ID: $hydro_station_id Ref High: $high_wl Ref Low: $low_wl<br/>";
		

		$date ="";
		$d ="";
		$m ="";
		$y ="";
		$first_val ="";
		$second_val ="";
		$third_val ="";
		$hr6 ="";
		$hr9 ="";
		$hr12 ="";
		$hr15 ="";
		$hr18 ="";
		$rainfall ="";
		$current_date ="";
		$previous_date ="";
		$counter = count($sms_content);
		
		if($counter == 5) {
			//$station_id = strtoupper(trim($sms_content[0]));
			$date = trim($sms_content[1]);
			$date_str = str_split($date);
			$d = $date_str[0].$date_str[1];
			$m = $date_str[2].$date_str[3];
			$y = $curr_year_two_digit.$date_str[4].$date_str[5];
			
			$first_val = (float)($sms_content[2]/100);
			$second_val = (float)($sms_content[3]/100);
			$third_val = (float)($sms_content[4]/100);
			
			$hr18 =  number_format($first_val, 2, '.', '');
			$hr6 = number_format($second_val, 2, '.', '');
			$hr9 = number_format($third_val, 2, '.', '');
			
			$input = $m.'/'.$d.'/'.$y;
			$date = strtotime($input);
			$current_date = date('d-M-y', $date);
			$current_date = strtoupper($current_date);
			$previous_date = date("d-M-y",strtotime ( '-1 day' , strtotime ( $input ) ));
			$previous_date = strtoupper($previous_date);
			
			/*$ret_msg.="current_date: $current_date, previous_date: $previous_date, HR18: $hr18".
			"HR6: $hr6, HR9: $hr9<br/>";*/
			if($hr18<$high_wl && $hr18>=$low_wl){
				$date_hr18 = "$previous_date 18:00:00";
				$form_data_hr18['M_DATE']="TO_DATE('$date_hr18','DD-MON-YY HH24:MI:SS')";
				$form_data_hr18['STATION_CODE']="'$station_id'";
				$form_data_hr18['DATA_VALUE']="'$hr18'";
				$form_data_hr18['D_TYPE']="'W'";

				$del_cond_hr18 = "UPPER(STATION_CODE)='$station_id' AND TO_CHAR(M_DATE, 'DD-MON-YY HH24:MI:SS')='$date_hr18' AND D_TYPE='W'";
				$del_data_hr18 = $obj->Delete_Data('FFWC.DATA_TBL',$del_cond_hr18);
				$insert_hr18 = $obj->Insert_Data('FFWC.DATA_TBL', $form_data_hr18);
				$process_res .= "ffwc hr18 insert: $insert_hr18<br/>";
				if($hydro_station_id!=""){
					$hydro_form_data_hr18['STATION_ID'] = "'$hydro_station_id'";
					$hydro_form_data_hr18['SWL_DATE'] = "TO_DATE('$date_hr18','DD-MON-YY HH24:MI:SS')";
					$hydro_form_data_hr18['WL'] = "'$hr18'";
					$hydro_form_data_hr18['DATA_TYPE'] = "'R'";
					$hydro_form_data_hr18['INSERT_BY'] = "'$user_id'";
					$hydro_form_data_hr18['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					$hydro_del_cond_hr18 = "STATION_ID='$hydro_station_id' AND TO_CHAR(SWL_DATE, 'DD-MON-YY HH24:MI:SS')='$date_hr18' AND DATA_TYPE='R'";
					
					$hydro_del_data_hr18 = $obj->Delete_Data('HYDROLOGY.SWL_DATA@HYDRO10',$hydro_del_cond_hr18);
					$hydro_ins_data_hr18 = $obj->Insert_Data('HYDROLOGY.SWL_DATA@HYDRO10', $hydro_form_data_hr18);
					$process_res .= "hydro hr18 insert: $hydro_ins_data_hr18<br/>";
				}
			}
			else{
				$prev_id_tbl = "MDM_WL_RAINFALL_DATA_LOG";
				$prev_id_cond = "UPPER(WELL_ID)='$station_id' AND TO_CHAR(LOG_DATE, 'DD-MON-YY')='$previous_date'";
				$prev_id_info = $obj->View_column_details_By_Cond($prev_id_tbl,"ID,SMS_ID_LIST",$prev_id_cond);
				$prev_data_id = $prev_id_info['ID'];
				$prev_sms_list = $prev_id_info['SMS_ID_LIST'];
				
				if($prev_data_id){
					$curr_sms_list = "";
					if($prev_sms_list){
						$curr_sms_list = "$prev_sms_list,$sms_id";
					}
					else{
						$curr_sms_list = $sms_id;
					}
					
					$up_prev_data_hr18['HR18'] = "'$hr18'";
					$up_prev_data_hr18['SMS_ID_LIST'] = "'$curr_sms_list'";
					$up_prev_data_hr18['STATUS'] = "'P'";
					$up_prev_data_hr18['DATA_READ_TYPE'] = "'A'";
					$up_prev_data_hr18['INSERT_BY'] = "'$user_id'";
					$up_prev_data_hr18['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					$up_prev_cond_hr18 = "ID='$prev_data_id'";
					$up_prev_data_res = $obj->Update_Data($prev_id_tbl, $up_prev_data_hr18, $up_prev_cond_hr18);
					$process_res .= "mdm log update: $up_prev_data_res<br/>";
				}
				else{
					$ins_prev_data_hr18['WELL_ID'] = "'$station_id'";
					$ins_prev_data_hr18['LOG_DATE'] = "TO_DATE('$previous_date', 'DD-MON-RR')";
					$ins_prev_data_hr18['HR18'] = "'$hr18'";
					$ins_prev_data_hr18['FLAG'] = "'1'";
					$ins_prev_data_hr18['STATUS'] = "'P'";
					$ins_prev_data_hr18['SMS_ID_LIST'] = "'$sms_id'";
					$ins_prev_data_hr18['DATA_READ_TYPE'] = "'A'";
					$ins_prev_data_hr18['INSERT_BY'] = "'$user_id'";
					$ins_prev_data_hr18['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					$ins_prev_data_res = $obj->Insert_Data($prev_id_tbl, $ins_prev_data_hr18);
					$process_res .= "mdm log insert: $ins_prev_data_res<br/>";
				}
			}
			
			if($hr6<$high_wl && $hr6>=$low_wl){
				$date_hr6 = "$current_date 06:00:00";
				$form_data_hr6['M_DATE']="TO_DATE('$date_hr6','DD-MON-YY HH24:MI:SS')";
				$form_data_hr6['STATION_CODE']="'$station_id'";
				$form_data_hr6['DATA_VALUE']="'$hr6'";
				$form_data_hr6['D_TYPE']="'W'";

				$del_cond_hr6 = "UPPER(STATION_CODE)='$station_id' AND TO_CHAR(M_DATE, 'DD-MON-YY HH24:MI:SS')='$date_hr6' AND D_TYPE='W'";
				$del_data_hr6 = $obj->Delete_Data('FFWC.DATA_TBL',$del_cond_hr6);
				$ins_data_hr6 = $obj->Insert_Data('FFWC.DATA_TBL', $form_data_hr6);
				$process_res .= "ffwc hr6 insert: $ins_data_hr6<br/>";
				if($hydro_station_id!=""){
					$hydro_form_data_hr6['STATION_ID'] = "'$hydro_station_id'";
					$hydro_form_data_hr6['SWL_DATE'] = "TO_DATE('$date_hr6','DD-MON-YY HH24:MI:SS')";
					$hydro_form_data_hr6['WL'] = "'$hr6'";
					$hydro_form_data_hr6['DATA_TYPE'] = "'R'";
					$hydro_form_data_hr6['INSERT_BY'] = "'$user_id'";
					$hydro_form_data_hr6['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					
					$hydro_del_cond_hr6 = "STATION_ID='$hydro_station_id' AND TO_CHAR(SWL_DATE, 'DD-MON-YY HH24:MI:SS')='$date_hr6' AND DATA_TYPE='R'";
					
					$hydro_del_data_hr6 = $obj->Delete_Data('HYDROLOGY.SWL_DATA@HYDRO10',$hydro_del_cond_hr6);
					$hydro_ins_data_hr6 = $obj->Insert_Data('HYDROLOGY.SWL_DATA@HYDRO10', $hydro_form_data_hr6);
					$process_res .= "hydro hr6 insert: $hydro_ins_data_hr6<br/>";
				}
			}
			else{
				$curr_id_tbl = "MDM_WL_RAINFALL_DATA_LOG";
				$curr_id_cond = "UPPER(WELL_ID)='$station_id' AND TO_CHAR(LOG_DATE, 'DD-MON-YY')='$current_date'";
				$curr_id_info = $obj->View_column_details_By_Cond($curr_id_tbl,"ID,SMS_ID_LIST",$curr_id_cond);
				$curr_data_id = $curr_id_info['ID'];
				$prev_sms_list = $curr_id_info['SMS_ID_LIST'];
				
				if($curr_data_id){
					$curr_sms_list = "";
					if($prev_sms_list){
						$curr_sms_list = "$prev_sms_list,$sms_id";
					}
					else{
						$curr_sms_list = $sms_id;
					}
					
					$up_curr_data_hr6['HR6'] = "'$hr6'";
					$up_curr_data_hr6['SMS_ID_LIST'] = "'$curr_sms_list'";
					$up_curr_data_hr6['STATUS'] = "'P'";
					$up_curr_data_hr6['DATA_READ_TYPE'] = "'A'";
					$up_curr_data_hr6['INSERT_BY'] = "'$user_id'";
					$up_curr_data_hr6['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					$up_curr_cond_hr6 = "ID='$curr_data_id'";
					$up_curr_data_res = $obj->Update_Data($curr_id_tbl, $up_curr_data_hr6, $up_curr_cond_hr6);
					$process_res .= "mdm hr6 update: $up_curr_data_res<br/>";
				}
				else{
					$ins_curr_data_hr6['WELL_ID'] = "'$station_id'";
					$ins_curr_data_hr6['LOG_DATE'] = "TO_DATE('$current_date', 'DD-MON-RR')";
					$ins_curr_data_hr6['HR6'] = "'$hr6'";
					$ins_curr_data_hr6['FLAG'] = "'1'";
					$ins_curr_data_hr6['STATUS'] = "'P'";
					$ins_curr_data_hr6['SMS_ID_LIST'] = "'$sms_id'";
					$ins_curr_data_hr6['DATA_READ_TYPE'] = "'A'";
					$ins_curr_data_hr6['INSERT_BY'] = "'$user_id'";
					$ins_curr_data_hr6['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					$ins_curr_data_res = $obj->Insert_Data($curr_id_tbl, $ins_curr_data_hr6);
					$process_res .= "mdm hr6 insert: $ins_curr_data_res<br/>";
				}
			}
			
			if($hr9<$high_wl && $hr9>=$low_wl){
				$date_hr9 = "$current_date 09:00:00";
				$form_data_hr9['M_DATE']="TO_DATE('$date_hr9','DD-MON-YY HH24:MI:SS')";
				$form_data_hr9['STATION_CODE']="'$station_id'";
				$form_data_hr9['DATA_VALUE']="'$hr9'";
				$form_data_hr9['D_TYPE']="'W'";

				$del_cond_hr9 = "UPPER(STATION_CODE)='$station_id' AND TO_CHAR(M_DATE, 'DD-MON-YY HH24:MI:SS')='$date_hr9' AND D_TYPE='W'";
				$del_data_hr9 = $obj->Delete_Data('FFWC.DATA_TBL',$del_cond_hr9);
				$update_hr9 = $obj->Insert_Data('FFWC.DATA_TBL', $form_data_hr9);
				$process_res .= "ffwc hr9 update: $update_hr9<br/>";
				if($hydro_station_id!=""){
					$hydro_form_data_hr9['STATION_ID'] = "'$hydro_station_id'";
					$hydro_form_data_hr9['SWL_DATE'] = "TO_DATE('$date_hr9','DD-MON-YY HH24:MI:SS')";
					$hydro_form_data_hr9['WL'] = "'$hr9'";
					$hydro_form_data_hr9['DATA_TYPE'] = "'R'";
					$hydro_form_data_hr9['INSERT_BY'] = "'$user_id'";
					$hydro_form_data_hr9['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					
					$hydro_del_cond_hr9 = "STATION_ID='$hydro_station_id' AND TO_CHAR(SWL_DATE, 'DD-MON-YY HH24:MI:SS')='$date_hr9' AND DATA_TYPE='R'";
					
					$hydro_del_data_hr9 = $obj->Delete_Data('HYDROLOGY.SWL_DATA@HYDRO10',$hydro_del_cond_hr9);
					$hydro_ins_data_hr9 = $obj->Insert_Data('HYDROLOGY.SWL_DATA@HYDRO10', $hydro_form_data_hr9);
					$process_res .= "hydro hr9 update: $hydro_ins_data_hr9<br/>";
				}
			}
			else{
				$curr_id_tbl = "MDM_WL_RAINFALL_DATA_LOG";
				$curr_id_cond = "UPPER(WELL_ID)='$station_id' AND TO_CHAR(LOG_DATE, 'DD-MON-YY')='$current_date'";
				$curr_id_info = $obj->View_column_details_By_Cond($curr_id_tbl,"ID,SMS_ID_LIST",$curr_id_cond);
				$curr_data_id = $curr_id_info['ID'];
				$prev_sms_list = $curr_id_info['SMS_ID_LIST'];
				
				if($curr_data_id){
					$curr_sms_list = "";
					if($prev_sms_list){
						$curr_sms_list = "$prev_sms_list,$sms_id";
					}
					else{
						$curr_sms_list = $sms_id;
					}
					
					$up_curr_data_hr9['HR9'] = "'$hr9'";
					$up_curr_data_hr9['SMS_ID_LIST'] = "'$curr_sms_list'";
					$up_curr_data_hr9['STATUS'] = "'P'";
					$up_curr_data_hr9['DATA_READ_TYPE'] = "'A'";
					$up_curr_data_hr9['INSERT_BY'] = "'$user_id'";
					$up_curr_data_hr9['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					$up_curr_cond_hr9 = "ID='$curr_data_id'";
					$up_curr_data_res = $obj->Update_Data($curr_id_tbl, $up_curr_data_hr9, $up_curr_cond_hr9);
					$process_res .= "mdm hr9 update: $up_curr_data_res<br/>";
				}
				else{
					$ins_curr_data_hr9['WELL_ID'] = "'$station_id'";
					$ins_curr_data_hr9['LOG_DATE'] = "TO_DATE('$current_date', 'DD-MON-RR')";
					$ins_curr_data_hr9['HR9'] = "'$hr9'";
					$ins_curr_data_hr9['FLAG'] = "'1'";
					$ins_curr_data_hr9['STATUS'] = "'P'";
					$ins_curr_data_hr9['SMS_ID_LIST'] = "'$sms_id'";
					$ins_curr_data_hr9['DATA_READ_TYPE'] = "'A'";
					$ins_curr_data_hr9['INSERT_BY'] = "'$user_id'";
					$ins_curr_data_hr9['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					$ins_curr_data_res = $obj->Insert_Data($curr_id_tbl, $ins_curr_data_hr9);
					$process_res .= "mdm hr9 insert: $ins_curr_data_res<br/>";
				}
			}
		}
		elseif($counter == 4) {
			//$station_id = strtoupper(trim($sms_content[0]));
			$date = $sms_content[1];
			$date_str = str_split($date);
			$d = $date_str[0].$date_str[1];
			$m = $date_str[2].$date_str[3];
			$y = $curr_year_two_digit.$date_str[4].$date_str[5];
			
			$first_val = (float)($sms_content[2]/100);
			$second_val = (float)($sms_content[3]/100);
			
			$hr12 =  number_format($first_val, 2, '.', '');
			$hr15 = number_format($second_val, 2, '.', '');
		
			$input = $m.'/'.$d.'/'.$y;
			$date = strtotime($input);
			$current_date = date('d-M-y', $date);
			$current_date = strtoupper($current_date);
			
			if($hr12<$high_wl && $hr12>=$low_wl){
				$date_hr12 = "$current_date 12:00:00";
				$form_data_hr12['M_DATE']="TO_DATE('$date_hr12','DD-MON-YY HH24:MI:SS')";
				$form_data_hr12['STATION_CODE']="'$station_id'";
				$form_data_hr12['DATA_VALUE']="'$hr12'";
				$form_data_hr12['D_TYPE']="'W'";
		
				$del_cond_hr12 = "UPPER(STATION_CODE)='$station_id' AND TO_CHAR(M_DATE, 'DD-MON-YY HH24:MI:SS')='$date_hr12' AND D_TYPE='W'";
				$del_data_hr12= $obj->Delete_Data('FFWC.DATA_TBL',$del_cond_hr12);
				$update_hr12 = $obj->Insert_Data('FFWC.DATA_TBL', $form_data_hr12);
				$process_res .= "ffwc hr12 update: $update_hr12<br/>";
				if($hydro_station_id!=""){
					$hydro_form_data_hr12['STATION_ID'] = "'$hydro_station_id'";
					$hydro_form_data_hr12['SWL_DATE'] = "TO_DATE('$date_hr12','DD-MON-YY HH24:MI:SS')";
					$hydro_form_data_hr12['WL'] = "'$hr12'";
					$hydro_form_data_hr12['DATA_TYPE'] = "'R'";
					$hydro_form_data_hr12['INSERT_BY'] = "'$user_id'";
					$hydro_form_data_hr12['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					
					$hydro_del_cond_hr12 = "STATION_ID='$hydro_station_id' AND TO_CHAR(SWL_DATE, 'DD-MON-YY HH24:MI:SS')='$date_hr12' AND DATA_TYPE='R'";
					
					$hydro_del_data_hr12 = $obj->Delete_Data('HYDROLOGY.SWL_DATA@HYDRO10',$hydro_del_cond_hr12);
					$hydro_ins_data_hr12 = $obj->Insert_Data('HYDROLOGY.SWL_DATA@HYDRO10', $hydro_form_data_hr12);
					$process_res .= "hydro hr12 update: $hydro_ins_data_hr12<br/>";
				}
			}
			else{
				$curr_id_tbl = "MDM_WL_RAINFALL_DATA_LOG";
				$curr_id_cond = "UPPER(WELL_ID)='$station_id' AND TO_CHAR(LOG_DATE, 'DD-MON-YY')='$current_date'";
				$curr_id_info = $obj->View_column_details_By_Cond($curr_id_tbl,"ID,SMS_ID_LIST",$curr_id_cond);

				$curr_data_id = $curr_id_info['ID'];
				$prev_sms_list = $curr_id_info['SMS_ID_LIST'];
				
				if($curr_data_id){
					$curr_sms_list = "";
					if($prev_sms_list){
						$curr_sms_list = "$prev_sms_list,$sms_id";
					}
					else{
						$curr_sms_list = $sms_id;
					}
					
					$up_curr_data_hr12['HR12'] = "'$hr12'";
					$up_curr_data_hr12['SMS_ID_LIST'] = "'$curr_sms_list'";
					$up_curr_data_hr12['STATUS'] = "'P'";
					$up_curr_data_hr12['DATA_READ_TYPE'] = "'A'";
					$up_curr_data_hr12['INSERT_BY'] = "'$user_id'";
					$up_curr_data_hr12['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					$up_curr_cond_hr12 = "ID='$curr_data_id'";
					$up_curr_data_res = $obj->Update_Data($curr_id_tbl, $up_curr_data_hr12, $up_curr_cond_hr12);
					$process_res .= "mdm hr12 update: $up_curr_data_res<br/>";
				}
				else{
					$ins_curr_data_hr12['WELL_ID'] = "'$station_id'";
					$ins_curr_data_hr12['LOG_DATE'] = "TO_DATE('$current_date', 'DD-MON-RR')";
					$ins_curr_data_hr12['HR12'] = "'$hr12'";
					$ins_curr_data_hr12['FLAG'] = "'1'";
					$ins_curr_data_hr12['STATUS'] = "'P'";
					$ins_curr_data_hr12['SMS_ID_LIST'] = "'$sms_id'";
					$ins_curr_data_hr12['DATA_READ_TYPE'] = "'A'";
					$ins_curr_data_hr12['INSERT_BY'] = "'$user_id'";
					$ins_curr_data_hr12['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					$ins_curr_data_res = $obj->Insert_Data($curr_id_tbl, $ins_curr_data_hr12);
					$process_res .= "mdm hr12 update: $ins_curr_data_res<br/>";
				}
			}
			
			if($hr15<$high_wl && $hr15>=$low_wl){
				$date_hr15 = "$current_date 15:00:00";
				$form_data_hr15['M_DATE']="TO_DATE('$date_hr15','DD-MON-YY HH24:MI:SS')";
				$form_data_hr15['STATION_CODE']="'$station_id'";
				$form_data_hr15['DATA_VALUE']="'$hr15'";
				$form_data_hr15['D_TYPE']="'W'";
		
				$del_cond_hr15 = "UPPER(STATION_CODE)='$station_id' AND TO_CHAR(M_DATE, 'DD-MON-YY HH24:MI:SS')='$date_hr15' AND D_TYPE='W'";
				$del_data_hr15= $obj->Delete_Data('FFWC.DATA_TBL',$del_cond_hr15);
				$update_hr15 = $obj->Insert_Data('FFWC.DATA_TBL', $form_data_hr15);
				$process_res .= "ffwc hr15 update: $update_hr15<br/>";
				if($hydro_station_id!=""){
					$hydro_form_data_hr15['STATION_ID'] = "'$hydro_station_id'";
					$hydro_form_data_hr15['SWL_DATE'] = "TO_DATE('$date_hr15','DD-MON-YY HH24:MI:SS')";
					$hydro_form_data_hr15['WL'] = "'$hr15'";
					$hydro_form_data_hr15['DATA_TYPE'] = "'R'";
					$hydro_form_data_hr15['INSERT_BY'] = "'$user_id'";
					$hydro_form_data_hr15['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					
					$hydro_del_cond_hr15 = "STATION_ID='$hydro_station_id' AND TO_CHAR(SWL_DATE, 'DD-MON-YY HH24:MI:SS')='$date_hr15' AND DATA_TYPE='R'";
					
					$hydro_del_data_hr15 = $obj->Delete_Data('HYDROLOGY.SWL_DATA@HYDRO10',$hydro_del_cond_hr15);
					$hydro_ins_data_hr15 = $obj->Insert_Data('HYDROLOGY.SWL_DATA@HYDRO10', $hydro_form_data_hr15);
					$process_res .= "hydro hr15 update: $hydro_ins_data_hr15<br/>";
				}
			}
			else{
				$curr_id_tbl = "MDM_WL_RAINFALL_DATA_LOG";
				$curr_id_cond = "UPPER(WELL_ID)='$station_id' AND TO_CHAR(LOG_DATE, 'DD-MON-YY')='$current_date'";
				$curr_id_info = $obj->View_column_details_By_Cond($curr_id_tbl,"ID,SMS_ID_LIST",$curr_id_cond);

				$curr_data_id = $curr_id_info['ID'];
				$prev_sms_list = $curr_id_info['SMS_ID_LIST'];
				
				if($curr_data_id){
					$curr_sms_list = "";
					if($prev_sms_list){
						$curr_sms_list = "$prev_sms_list,$sms_id";
					}
					else{
						$curr_sms_list = $sms_id;
					}
					$up_curr_data_hr15['HR15'] = "'$hr15'";
					$up_curr_data_hr15['SMS_ID_LIST'] = "'$curr_sms_list'";
					$up_curr_data_hr15['DATA_READ_TYPE'] = "'A'";
					$up_curr_data_hr15['STATUS'] = "'P'";
					$up_curr_data_hr15['INSERT_BY'] = "'$user_id'";
					$up_curr_data_hr15['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					$up_curr_cond_hr15 = "ID='$curr_data_id'";
					$up_curr_data_res = $obj->Update_Data($curr_id_tbl, $up_curr_data_hr15, $up_curr_cond_hr15);
					$process_res .= "mdm hr15 update: $up_curr_data_res<br/>";
				}
				else{
					$ins_curr_data_hr15['WELL_ID'] = "'$station_id'";
					$ins_curr_data_hr15['LOG_DATE'] = "TO_DATE('$current_date', 'DD-MON-RR')";
					$ins_curr_data_hr15['HR15'] = "'$hr15'";
					$ins_curr_data_hr15['FLAG'] = "'1'";
					$ins_curr_data_hr15['STATUS'] = "'P'";
					$ins_curr_data_hr15['SMS_ID_LIST'] = "'$sms_id'";
					$ins_curr_data_hr15['DATA_READ_TYPE'] = "'A'";
					$ins_curr_data_hr15['INSERT_BY'] = "'$user_id'";
					$ins_curr_data_hr15['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					$ins_curr_data_res = $obj->Insert_Data($curr_id_tbl, $ins_curr_data_hr15);
					$process_res .= "mdm hr12 hr15 update: $ins_curr_data_res<br/>";
				}
			}
		}
		elseif($counter == 6) {
			//$station_id = strtoupper(trim($sms_content[0]));
			$date = $sms_content[1];
			$date_str = str_split($date);
			$d = $date_str[0].$date_str[1];
			$m = $date_str[2].$date_str[3];
			$y = $curr_year_two_digit.$date_str[4].$date_str[5];

			$first_val = (float)($sms_content[2]/100);
			$second_val = (float)($sms_content[3]/100);
			$third_val = (float)($sms_content[4]/100);
	
			$hr18 =  number_format($first_val, 2, '.', '');
			$hr6 = number_format($second_val, 2, '.', '');
			$hr9 = number_format($third_val, 2, '.', '');
	
			$rainfall = $sms_content[5];

			$input = $m.'/'.$d.'/'.$y;
			$date = strtotime($input);
			$current_date = date('d-M-y', $date);
			$current_date = strtoupper($current_date);
			$previous_date = date("d-M-y",strtotime ( '-1 day' , strtotime ( $input ) ));
			$previous_date = strtoupper($previous_date);
	
			if($hr18<$high_wl && $hr18>=$low_wl){
				$date_hr18 = "$previous_date 18:00:00";
				$form_data_hr18['M_DATE']="TO_DATE('$date_hr18','DD-MON-YY HH24:MI:SS')";
				$form_data_hr18['STATION_CODE']="'$station_id'";
				$form_data_hr18['DATA_VALUE']="'$hr18'";
				$form_data_hr18['D_TYPE']="'W'";

				$del_cond_hr18 = "UPPER(STATION_CODE)='$station_id' AND TO_CHAR(M_DATE, 'DD-MON-YY HH24:MI:SS')='$date_hr18' AND D_TYPE='W'";
				$del_data_hr18 = $obj->Delete_Data('FFWC.DATA_TBL',$del_cond_hr18);
				$insert_hr18 = $obj->Insert_Data('FFWC.DATA_TBL', $form_data_hr18);
				$process_res .= "ffwc hr18 insert: $insert_hr18<br/>";
				
				if($hydro_station_id!=""){
					$hydro_form_data_hr18['STATION_ID'] = "'$hydro_station_id'";
					$hydro_form_data_hr18['SWL_DATE'] = "TO_DATE('$date_hr18','DD-MON-YY HH24:MI:SS')";
					$hydro_form_data_hr18['WL'] = "'$hr18'";
					$hydro_form_data_hr18['DATA_TYPE'] = "'R'";
					$hydro_form_data_hr18['INSERT_BY'] = "'$user_id'";
					$hydro_form_data_hr18['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					$hydro_del_cond_hr18 = "STATION_ID='$hydro_station_id' AND TO_CHAR(SWL_DATE, 'DD-MON-YY HH24:MI:SS')='$date_hr18' AND DATA_TYPE='R'";
					
					$hydro_del_data_hr18 = $obj->Delete_Data('HYDROLOGY.SWL_DATA@HYDRO10',$hydro_del_cond_hr18);
					$hydro_ins_data_hr18 = $obj->Insert_Data('HYDROLOGY.SWL_DATA@HYDRO10', $hydro_form_data_hr18);
					$process_res .= "hydro hr18 insert: $hydro_ins_data_hr18<br/>";
				}
			}
			else{
				$prev_id_tbl = "MDM_WL_RAINFALL_DATA_LOG";
				$prev_id_cond = "UPPER(WELL_ID)='$station_id' AND TO_CHAR(LOG_DATE, 'DD-MON-YY')='$previous_date'";
				$prev_id_info = $obj->View_column_details_By_Cond($prev_id_tbl,"ID,SMS_ID_LIST",$prev_id_cond);
				$prev_data_id = $prev_id_info['ID'];
				$prev_sms_list = $prev_id_info['SMS_ID_LIST'];
				
				if($prev_data_id){
					$curr_sms_list = "";
					if($prev_sms_list){
						$curr_sms_list = "$prev_sms_list,$sms_id";
					}
					else{
						$curr_sms_list = $sms_id;
					}
					
					$up_prev_data_hr18['HR18'] = "'$hr18'";
					$up_prev_data_hr18['SMS_ID_LIST'] = "'$curr_sms_list'";
					$up_prev_data_hr18['DATA_READ_TYPE'] = "'A'";
					$up_prev_data_hr18['STATUS'] = "'P'";
					$up_prev_data_hr18['INSERT_BY'] = "'$user_id'";
					$up_prev_data_hr18['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					$up_prev_cond_hr18 = "ID='$prev_data_id'";
					$up_prev_data_res = $obj->Update_Data($prev_id_tbl, $up_prev_data_hr18, $up_prev_cond_hr18);
					$process_res .= "mdm log update: $up_prev_data_res<br/>";
				}
				else{
					$ins_prev_data_hr18['WELL_ID'] = "'$station_id'";
					$ins_prev_data_hr18['LOG_DATE'] = "TO_DATE('$previous_date', 'DD-MON-RR')";
					$ins_prev_data_hr18['HR18'] = "'$hr18'";
					$ins_prev_data_hr18['FLAG'] = "'1'";
					$ins_prev_data_hr18['STATUS'] = "'P'";
					$ins_prev_data_hr18['SMS_ID_LIST'] = "'$sms_id'";
					$ins_prev_data_hr18['DATA_READ_TYPE'] = "'A'";
					$ins_prev_data_hr18['INSERT_BY'] = "'$user_id'";
					$ins_prev_data_hr18['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					$ins_prev_data_res = $obj->Insert_Data($prev_id_tbl, $ins_prev_data_hr18);
					$process_res .= "mdm log insert: $ins_prev_data_res<br/>";
				}
			}
			
			if($hr6<$high_wl && $hr6>=$low_wl){
				$date_hr6 = "$current_date 06:00:00";
				$form_data_hr6['M_DATE']="TO_DATE('$date_hr6','DD-MON-YY HH24:MI:SS')";
				$form_data_hr6['STATION_CODE']="'$station_id'";
				$form_data_hr6['DATA_VALUE']="'$hr6'";
				$form_data_hr6['D_TYPE']="'W'";

				$del_cond_hr6 = "UPPER(STATION_CODE)='$station_id' AND TO_CHAR(M_DATE, 'DD-MON-YY HH24:MI:SS')='$date_hr6' AND D_TYPE='W'";
				$del_data_hr6 = $obj->Delete_Data('FFWC.DATA_TBL',$del_cond_hr6);
				$ins_data_hr6 = $obj->Insert_Data('FFWC.DATA_TBL', $form_data_hr6);
				
				$process_res .= "ffwc hr6 insert: $ins_data_hr6<br/>";
				if($hydro_station_id!=""){
					$hydro_form_data_hr6['STATION_ID'] = "'$hydro_station_id'";
					$hydro_form_data_hr6['SWL_DATE'] = "TO_DATE('$date_hr6','DD-MON-YY HH24:MI:SS')";
					$hydro_form_data_hr6['WL'] = "'$hr6'";
					$hydro_form_data_hr6['DATA_TYPE'] = "'R'";
					$hydro_form_data_hr6['INSERT_BY'] = "'$user_id'";
					$hydro_form_data_hr6['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					$hydro_del_cond_hr6 = "STATION_ID='$hydro_station_id' AND TO_CHAR(SWL_DATE, 'DD-MON-YY HH24:MI:SS')='$date_hr6' AND DATA_TYPE='R'";
					
					$hydro_del_data_hr6 = $obj->Delete_Data('HYDROLOGY.SWL_DATA@HYDRO10',$hydro_del_cond_hr6);
					$hydro_ins_data_hr6 = $obj->Insert_Data('HYDROLOGY.SWL_DATA@HYDRO10', $hydro_form_data_hr6);
					$process_res .= "hydro hr6 insert: $hydro_ins_data_hr6<br/>";
				}
			}
			else{
				$curr_id_tbl = "MDM_WL_RAINFALL_DATA_LOG";
				$curr_id_cond = "UPPER(WELL_ID)='$station_id' AND TO_CHAR(LOG_DATE, 'DD-MON-YY')='$current_date'";
				$curr_id_info = $obj->View_column_details_By_Cond($curr_id_tbl,"ID,SMS_ID_LIST",$curr_id_cond);
				$curr_data_id = $curr_id_info['ID'];
				$prev_sms_list = $curr_id_info['SMS_ID_LIST'];
				
				if($curr_data_id){
					$curr_sms_list = "";
					if($prev_sms_list){
						$curr_sms_list = "$prev_sms_list,$sms_id";
					}
					else{
						$curr_sms_list = $sms_id;
					}
					
					$up_curr_data_hr6['HR6'] = "'$hr6'";
					$up_curr_data_hr6['SMS_ID_LIST'] = "'$curr_sms_list'";
					$up_curr_data_hr6['DATA_READ_TYPE'] = "'A'";
					$up_curr_data_hr6['STATUS'] = "'P'";
					$up_curr_data_hr6['INSERT_BY'] = "'$user_id'";
					$up_curr_data_hr6['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					$up_curr_cond_hr6 = "ID='$curr_data_id'";
					$up_curr_data_res = $obj->Update_Data($curr_id_tbl, $up_curr_data_hr6, $up_curr_cond_hr6);
					$process_res .= "mdm hr6 update: $up_curr_data_res<br/>";
				}
				else{
					$ins_curr_data_hr6['WELL_ID'] = "'$station_id'";
					$ins_curr_data_hr6['LOG_DATE'] = "TO_DATE('$current_date', 'DD-MON-RR')";
					$ins_curr_data_hr6['HR6'] = "'$hr6'";
					$ins_curr_data_hr6['FLAG'] = "'1'";
					$ins_curr_data_hr6['STATUS'] = "'P'";
					$ins_curr_data_hr6['SMS_ID_LIST'] = "'$sms_id'";
					$ins_curr_data_hr6['DATA_READ_TYPE'] = "'A'";
					$ins_curr_data_hr6['INSERT_BY'] = "'$user_id'";
					$ins_curr_data_hr6['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					$ins_curr_data_res = $obj->Insert_Data($curr_id_tbl, $ins_curr_data_hr6);
					$process_res .= "mdm hr6 insert: $ins_curr_data_res<br/>";
				}
			}
			
			if($hr9<$high_wl && $hr9>=$low_wl){
				$date_hr9 = "$current_date 09:00:00";
				$form_data_hr9['M_DATE']="TO_DATE('$date_hr9','DD-MON-YY HH24:MI:SS')";
				$form_data_hr9['STATION_CODE']="'$station_id'";
				$form_data_hr9['DATA_VALUE']="'$hr9'";
				$form_data_hr9['D_TYPE']="'W'";

				$del_cond_hr9 = "UPPER(STATION_CODE)='$station_id' AND TO_CHAR(M_DATE, 'DD-MON-YY HH24:MI:SS')='$date_hr9' AND D_TYPE='W'";
				$del_data_hr9 = $obj->Delete_Data('FFWC.DATA_TBL',$del_cond_hr9);
				$update_hr9 = $obj->Insert_Data('FFWC.DATA_TBL', $form_data_hr9);
				$process_res .= "ffwc hr9 update: $update_hr9<br/>";
				if($hydro_station_id!=""){
					$hydro_form_data_hr9['STATION_ID'] = "'$hydro_station_id'";
					$hydro_form_data_hr9['SWL_DATE'] = "TO_DATE('$date_hr9','DD-MON-YY HH24:MI:SS')";
					$hydro_form_data_hr9['WL'] = "'$hr9'";
					$hydro_form_data_hr9['DATA_TYPE'] = "'R'";
					$hydro_form_data_hr9['INSERT_BY'] = "'$user_id'";
					$hydro_form_data_hr9['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					
					$hydro_del_cond_hr9 = "STATION_ID='$hydro_station_id' AND TO_CHAR(SWL_DATE, 'DD-MON-YY HH24:MI:SS')='$date_hr9' AND DATA_TYPE='R'";
					
					$hydro_del_data_hr9 = $obj->Delete_Data('HYDROLOGY.SWL_DATA@HYDRO10',$hydro_del_cond_hr9);
					$hydro_ins_data_hr9 = $obj->Insert_Data('HYDROLOGY.SWL_DATA@HYDRO10', $hydro_form_data_hr9);
					$process_res .= "hydro hr9 update: $hydro_ins_data_hr9<br/>";
				}
			}
			else{
				$curr_id_tbl = "MDM_WL_RAINFALL_DATA_LOG";
				$curr_id_cond = "UPPER(WELL_ID)='$station_id' AND TO_CHAR(LOG_DATE, 'DD-MON-YY')='$current_date'";
				$curr_id_info = $obj->View_column_details_By_Cond($curr_id_tbl,"ID,SMS_ID_LIST",$curr_id_cond);
				$curr_data_id = $curr_id_info['ID'];
				$prev_sms_list = $curr_id_info['SMS_ID_LIST'];
				
				if($curr_data_id){
					$curr_sms_list = "";
					if($prev_sms_list){
						$curr_sms_list = "$prev_sms_list,$sms_id";
					}
					else{
						$curr_sms_list = $sms_id;
					}
					
					$up_curr_data_hr9['HR9'] = "'$hr9'";
					$up_curr_data_hr9['SMS_ID_LIST'] = "'$curr_sms_list'";
					$up_curr_data_hr9['DATA_READ_TYPE'] = "'A'";
					$up_curr_data_hr9['STATUS'] = "'P'";
					$up_curr_data_hr9['INSERT_BY'] = "'$user_id'";
					$up_curr_data_hr9['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					$up_curr_cond_hr9 = "ID='$curr_data_id'";
					$up_curr_data_res = $obj->Update_Data($curr_id_tbl, $up_curr_data_hr9, $up_curr_cond_hr9);
					$process_res .= "mdm hr9 update: $up_curr_data_res<br/>";
				}
				else{
					$ins_curr_data_hr9['WELL_ID'] = "'$station_id'";
					$ins_curr_data_hr9['LOG_DATE'] = "TO_DATE('$current_date', 'DD-MON-RR')";
					$ins_curr_data_hr9['HR9'] = "'$hr9'";
					$ins_curr_data_hr9['FLAG'] = "'1'";
					$ins_curr_data_hr9['STATUS'] = "'P'";
					$ins_curr_data_hr9['SMS_ID_LIST'] = "'$sms_id'";
					$ins_curr_data_hr9['DATA_READ_TYPE'] = "'A'";
					$ins_curr_data_hr9['INSERT_BY'] = "'$user_id'";
					$ins_curr_data_hr9['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					$ins_curr_data_res = $obj->Insert_Data($curr_id_tbl, $ins_curr_data_hr9);
					$process_res .= "mdm hr9 insert: $ins_curr_data_res<br/>";
				}
			}
			if($rainfall<$high_rf && $rainfall>=$low_rf){
				$rf_stat_cond = "UPPER(KEYWORD)='$station_id' AND TRIM(DATA)=TRIM('RF')";
				$rf_stat_info = $obj->View_column_details_By_Cond("FFWC.STATIONS_TBL","STATION_CODE",$rf_stat_cond);
				$stat_rf_id = $rf_stat_info['STATION_CODE'];
				$process_res .= "rainfall station id: $stat_rf_id<br/>";
				
				$date_rf = "$current_date 09:00:00";
				$form_data_rf['M_DATE']="TO_DATE('$date_rf','DD-MON-YY HH24:MI:SS')";
				$form_data_rf['STATION_CODE']="'$station_id'";
				$form_data_rf['DATA_VALUE']="'$rainfall'";
				$form_data_rf['D_TYPE']="'R'";
				
				$del_cond_rf = "UPPER(STATION_CODE)='$station_id' AND TO_CHAR(M_DATE, 'DD-MON-YY HH24:MI:SS')='$date_rf' AND D_TYPE='R'";
				
				$del_rf = $obj->Delete_Data('FFWC.DATA_TBL',$del_cond_rf);
				$up_rf = $obj->Insert_Data('FFWC.DATA_TBL', $form_data_rf);
				$process_res .= "insert ffwc rainfall: $up_rf<br/>";
				if($stat_rf_id!=""){
					$rf_data_update['STATION_ID'] = "'$stat_rf_id'";
					$rf_data_update['RF_DATE'] = "TO_DATE('$date_rf','DD-MON-YY HH24:MI:SS')";
					$rf_data_update['RAIN_FALL'] = "'$rainfall'";
					$rf_data_update['UPDT_BY'] = "'$user_id'";
					$rf_data_update['UPDT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					
					$update_rf_cond_hydro = "STATION_ID='$stat_rf_id' AND TO_CHAR(RF_DATE,'DD-MON-YY HH24:MI:SS')='$date_rf'";
					$del_rf_hydro = $obj->Delete_Data('HYDROLOGY.RAIN_FALL_DATA@HYDRO10',$update_rf_cond_hydro);
					$up_rf_hydro = $obj->Insert_Data('HYDROLOGY.RAIN_FALL_DATA@HYDRO10', $rf_data_update);
					$process_res .= "insert hydrology rainfall: $up_rf_hydro<br/>";
				}
			}
			else{
				$curr_id_tbl = "MDM_WL_RAINFALL_DATA_LOG";
				$curr_id_cond = "UPPER(WELL_ID)='$station_id' AND TO_CHAR(LOG_DATE, 'DD-MON-YY')='$current_date'";
				$curr_id_info = $obj->View_column_details_By_Cond($curr_id_tbl,"ID,SMS_ID_LIST",$curr_id_cond);
				$curr_data_id = $curr_id_info['ID'];
				$prev_sms_list = $curr_id_info['SMS_ID_LIST'];
				
				if($curr_data_id){
					$curr_sms_list = "";
					if($prev_sms_list){
						$curr_sms_list = "$prev_sms_list,$sms_id";
					}
					else{
						$curr_sms_list = $sms_id;
					}
					
					$up_curr_data_rf['RAINFALL'] = "'$rainfall'";
					$up_curr_data_rf['SMS_ID_LIST'] = "'$curr_sms_list'";
					$up_curr_data_rf['DATA_READ_TYPE'] = "'A'";
					$up_curr_data_rf['STATUS'] = "'P'";
					$up_curr_data_rf['INSERT_BY'] = "'$user_id'";
					$up_curr_data_rf['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					$up_curr_cond_rf = "ID='$curr_data_id'";
					$up_curr_data_res = $obj->Update_Data($curr_id_tbl, $up_curr_data_rf, $up_curr_cond_rf);
					$process_res .= "mdm update cond rainfall: $up_curr_cond_rf<br/>";
				}
				else{
					$ins_curr_data_rf['WELL_ID'] = "'$station_id'";
					$ins_curr_data_rf['LOG_DATE'] = "TO_DATE('$current_date', 'DD-MON-RR')";
					$ins_curr_data_rf['FLAG'] = "'1'";
					$ins_curr_data_rf['STATUS'] = "'P'";
					$ins_curr_data_rf['RAINFALL'] = "'$rainfall'";
					$ins_curr_data_rf['SMS_ID_LIST'] = "'$curr_sms_list'";
					$ins_curr_data_rf['DATA_READ_TYPE'] = "'A'";
					$ins_curr_data_rf['INSERT_BY'] = "'$user_id'";
					$ins_curr_data_rf['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					$ins_curr_data_res = $obj->Insert_Data($curr_id_tbl, $ins_curr_data_rf);
					$process_res .= "mdm update cond rainfall: $ins_curr_data_res<br/>";
				}
			}
		}
		elseif($counter == 3){
			//$station_id = strtoupper(trim($sms_content[0]));
			$date = $sms_content[1];
			$date_str = str_split($date);
			$d = $date_str[0].$date_str[1];
			$m = $date_str[2].$date_str[3];
			$y = $curr_year_two_digit.$date_str[4].$date_str[5];
			$rainfall = $sms_content[2];

			$input = $m.'/'.$d.'/'.$y;
			$date = strtotime($input);
			$current_date = date('d-M-y', $date);
			$current_date = strtoupper($current_date);
			
			$rf_stat_cond = "UPPER(KEYWORD)='$station_id' AND TRIM(DATA)=TRIM('RF')";
			$rf_stat_info = $obj->View_column_details_By_Cond("FFWC.STATIONS_TBL","STATION_CODE",$rf_stat_cond);
			$stat_rf_id = $rf_stat_info['STATION_CODE'];
			if($rainfall<$high_rf && $rainfall>=$low_rf){
				$date_rf = "$current_date 09:00:00";
				$form_data_rf['M_DATE']="TO_DATE('$date_rf','DD-MON-YY HH24:MI:SS')";
				$form_data_rf['STATION_CODE']="'$station_id'";
				$form_data_rf['DATA_VALUE']="'$rainfall'";
				$form_data_rf['D_TYPE']="'R'";
			
				$del_cond_rf = "UPPER(STATION_CODE)='$station_id' AND TO_CHAR(M_DATE, 'DD-MON-YY HH24:MI:SS')='$date_rf' AND D_TYPE='R'";
				
				$del_rf = $obj->Delete_Data('FFWC.DATA_TBL',$del_cond_rf);
				$up_rf = $obj->Insert_Data('FFWC.DATA_TBL', $form_data_rf);
				$process_res .= "insert ffwc rainfall: $up_rf<br/>";
				if($stat_rf_id!=""){
					$rf_data_update['STATION_ID'] = "'$stat_rf_id'";
					$rf_data_update['RF_DATE'] = "TO_DATE('$date_rf','DD-MON-YY HH24:MI:SS')";
					$rf_data_update['RAIN_FALL'] = "'$rainfall'";
					$rf_data_update['UPDT_BY'] = "'$user_id'";
					$rf_data_update['UPDT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY  HH24:MI:SS')";
					
					$update_rf_cond_hydro = "STATION_ID='$stat_rf_id' AND TO_CHAR(RF_DATE,'DD-MON-YY HH24:MI:SS')='$date_rf'";
					$del_rf_hydro = $obj->Delete_Data('HYDROLOGY.RAIN_FALL_DATA@HYDRO10',$update_rf_cond_hydro);
					$up_rf_hydro = $obj->Insert_Data('HYDROLOGY.RAIN_FALL_DATA@HYDRO10', $rf_data_update);
					$process_res .= "insert hydro rainfall: $up_rf_hydro<br/>";
				}
			}
			else{
				$rf_tbl = "MDM_WL_RAINFALL_DATA_LOG";
				$rf_cond = "UPPER(WELL_ID)='$station_id' AND TO_CHAR(LOG_DATE, 'DD-MON-YY')='$current_date'";
				$rf_info = $obj->View_column_details_By_Cond($rf_tbl,"ID,SMS_ID_LIST",$rf_cond);
				
				$prev_data_id = $rf_info['ID'];
				$prev_sms_list = $rf_info['SMS_ID_LIST'];
				
				if($prev_data_id){
					$curr_sms_list = "";
					if($prev_sms_list){
						$curr_sms_list = "$prev_sms_list,$sms_id";
					}
					else{
						$curr_sms_list = $sms_id;
					}
					$up_prev_data_rf['RAINFALL'] = "'$rainfall'";
					$up_prev_data_rf['SMS_ID_LIST'] = "'$curr_sms_list'";
					$up_prev_data_rf['DATA_READ_TYPE'] = "'A'";
					$up_prev_data_rf['STATUS'] = "'P'";
					$up_prev_data_rf['INSERT_BY'] = "'$user_id'";
					$up_prev_data_rf['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					$up_prev_cond_rf = "ID='$prev_data_id'";
					$up_prev_data_res = $obj->Update_Data($rf_tbl, $up_prev_data_rf, $up_prev_cond_rf);
					$process_res .= "update mdm rainfall: $up_prev_data_res<br/>";
				}
				else{
					$ins_rf_data['WELL_ID'] = "'$station_id'";
					$ins_rf_data['LOG_DATE'] = "TO_DATE('$current_date', 'DD-MON-RR')";
					$ins_rf_data['RAINFALL'] = "'$rainfall'";
					$ins_rf_data['FLAG'] = "'1'";
					$ins_rf_data['STATUS'] = "'P'";
					$ins_rf_data['SMS_ID_LIST'] = "'$curr_sms_list'";
					$ins_rf_data['DATA_READ_TYPE'] = "'A'";
					$ins_rf_data['INSERT_BY'] = "'$user_id'";
					$ins_rf_data['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";
					$ins_prev_data_res = $obj->Insert_Data($rf_tbl, $ins_rf_data);
					$process_res .= "insert mdm rainfall: $ins_prev_data_res<br/>";
				}
			}
		}
	}
	else{
		//echo "else ffwc if ".$date_length."<br/>";
		$ret_msg=1;
		$wrong_sl++;
		$sms_log_data['MOBILE_NUMBER'] = "'$mobile'";
		$sms_log_data['SMS_CONTENT'] = "'$text'";
		$sms_log_data['SMS_DATE_TIME'] = "'$datetime'";
		$sms_log_data['SYSTEM_FLAG'] = "'2'";
		$sms_log_data['STATUS_FLAG'] = "'P'";
		$sms_log_data['DATA_READ_TYPE'] = "'A'";
		$sms_log_data['INSERT_BY'] = "'$user_id'";
		$sms_log_data['INSERT_DTM'] = "TO_DATE('$db_ins_date','DD-MON-YY HH24:MI:SS')";

	
		$sms_log_tbl = 'MDM_SMS_INBOX_LOG';
		$sms_log_ins = $obj->Insert_Data($sms_log_tbl, $sms_log_data);
		$process_res .= "insert incorrect sms log: $sms_log_ins<br/>";			
	}
	return $ret_msg;
}
?>