<?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'){
	if(strpos($data_list,",")!=""){
		$data_arr = explode(",",$data_list);
	}
	else{
		$data_arr = [$data_list];
	}
	$up_success = 0;
	for($i=0; $i<count($data_arr); $i++) {
		if(count($data_arr) == 1){
			$input_arr = explode('#', $data_arr[$i]);
		}else{
			$input_arr = explode('#', $data_arr[$i]);
		}
		//$dt_change = "-";
		$in_date = strtoupper($input_arr[2]);
		$id_val = $input_arr[0];
		$form_data['VERIFIED_WL']= "'".$input_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);
			if($well_data_info['RAW_DATA_ID']!=""){
				$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_approve'){
	if(strpos($data_list,",")!=""){
		$data_arr = explode(",",$data_list);
	}
	else{
		$data_arr = $data_list;
	}
	$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]);
		}
		//$dt_change = "-";
		$in_date = strtoupper($input_arr[2]);
		$id_val = $input_arr[0];
		$form_data['VERIFIED_WL']= "'".$input_arr[1]."'";
		$form_data['STATUS']="'A'";
		$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);
			if($well_data_info['RAW_DATA_ID']!=""){
				$raw_form_data['STATUS_FLAG']="'A'";
				$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);
		if($well_data_info['RAW_DATA_ID']!=""){
			$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'){
	$up_success=0;
	if(strpos($data_list,",")!=""){
		$data_arr = explode(",",$data_list);
	}
	else{
		$data_arr = [$data_list];
	}
	$up_success = 0;
	for($i=0; $i<count($data_arr); $i++) {
		if(count($data_arr) == 1){
			$input_arr = explode('#', $data_arr[$i]);
		}else{
			$input_arr = explode('#', $data_arr[$i]);
		}
		$in_date = strtoupper($input_arr[2]);
		$id_val = $input_arr[0];
		$form_data['VERIFIED_WL']= "'".$input_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){
			$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);
			if($well_data_info['RAW_DATA_ID']!=""){
				$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=='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]."'";
			//echo "line 161";
			$update = $obj->Update_Data($table_name, $form_data, $where_cond);
			if($update) {
			//	echo "line 163";
				$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'];
			//echo "line 194";
			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);
		// echo "if";
	}
	else{
		$data_arr = [$data_list];
		// echo "else";
	}
	
	$up_success = 0;
	
	for( $i=0; $i<count($data_arr); $i++) {
		if(count($data_arr) == 1){
			$input_arr = explode('#', $data_arr[$i]);
		}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['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('HYDROLOGY.GWT_DATA_TBL@HYDRO10', $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("HYDROLOGY.GWM_TBL@HYDRO10","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));
				$sel_date = strtoupper($sel_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 TO_CHAR(LOG_DATE, 'DD-MON-YY')='$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['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*/

	$sms_content = explode("*",$text);
	$counter = count($sms_content);
	$is_duplicate_sql = "";
	$ret_msg = "";
	if($counter == 5) {
		$station_id = $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];
		$hr18 = (float)($sms_content[2]/100);
		$hr6 = (float)($sms_content[3]/100);
		$hr9 = (float)($sms_content[4]/100);
		
		$hr18 =  number_format($hr18, 2, '.', '');
		$hr6 =  number_format($hr6, 2, '.', '');
		$hr9 =  number_format($hr9, 2, '.', '');

		$input = $m.'/'.$d.'/'.$y;
		$date = strtotime($input);
		$current_date = date('d-M-y', $date);
		$previous_date = date("d-M-y",strtotime ( '-1 day' , strtotime ( $input ) ));
		
		$current_date = strtoupper($current_date);
		$previous_date = strtoupper($previous_date);
		
		$input = '18:00:00';
		$time = strtotime($input);
		$previous_date_time = date('H:i:s', $time);

		$input = '06:00:00';
		$time = strtotime($input);
		$current_date_time_1 = date('H:i:s', $time);

		$input = '09:00:00';
		$time = strtotime($input);
		$current_date_time_2 = date('H:i:s', $time);
		
		$previous_log_info = $obj->View_column_details_By_Cond("MDM_WL_RAINFALL_DATA_LOG","ID,SMS_ID_LIST","WELL_ID='$station_id' AND LOG_DATE='$previous_date'");
		$prev_data_id = $previous_log_info['ID'];
		
		
		$prev_sms_list = $previous_log_info['SMS_ID_LIST'];
		$curr_sms_list = "";
		if($prev_sms_list){
			if(strpos($prev_sms_list,$sms_id)){
				$curr_sms_list = $prev_sms_list;
			}
			else{
				$curr_sms_list = "$prev_sms_list,$sms_id";
			}
		}
		else{
			$curr_sms_list = $sms_id;
		}
		$up_prev_data['WELL_ID'] = "'$station_id'";
		$up_prev_data['LOG_DATE'] = "'$previous_date'";
		$up_prev_data['HR18'] = "'$hr18'";
		$up_prev_data['FLAG'] = "'1'";
		$up_prev_data['STATUS'] = "'P'";
		$up_prev_data['SMS_ID_LIST'] = "'$curr_sms_list'";
		$up_prev_data['UPDATE_BY'] = "'$user_id'";
		$up_prev_data['UPDATE_DATE'] = "TO_DATE('$curr_date','DD-MON-RR')";
		//var_dump($up_prev_data);
		$table_name = 'MDM_WL_RAINFALL_DATA_LOG';
		$prev_where_cond = "WELL_ID='$station_id' AND TO_CHAR(LOG_DATE, 'DD-MON-YY')='$previous_date'";
		$up_prev_res = $obj->Update_Or_Insert_Data_By_Check_Duplicate($table_name, $up_prev_data, $prev_where_cond);
		$ret_msg = $up_prev_res."\n";
		
		$ins_curr_data['WELL_ID'] = "'$station_id'";
		$ins_curr_data['LOG_DATE'] = "'$current_date'";
		$ins_curr_data['HR6'] = "'$hr6'";
		$ins_curr_data['HR9'] = "'$hr9'";
		$ins_curr_data['FLAG'] = "'1'";
		$ins_curr_data['STATUS'] = "'P'";
		$ins_curr_data['SMS_ID_LIST'] = "'$sms_id'";
		$ins_curr_data['UPDATE_BY'] = "'$user_id'";
		$ins_curr_data['UPDATE_DATE'] = "TO_DATE('$curr_date','DD-MON-RR')";
		$curr_where_cond = "WELL_ID='$station_id' AND TO_CHAR(LOG_DATE, 'DD-MON-YY')='$current_date'";
		//var_dump($ins_curr_data);
		$ins_curr_res = $obj->Update_Or_Insert_Data_By_Check_Duplicate($table_name, $ins_curr_data, $curr_where_cond);
		$ret_msg .= $ins_curr_res."\n";
	
	} elseif($counter == 4) {
		$ret_msg = "count is 4";
		$station_id = $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];
		$hr12 = (float)($sms_content[2]/100);
		$hr15 = (float)($sms_content[3]/100);
		
		$hr12 =  number_format($hr12, 2, '.', '');
		$hr15 =  number_format($hr15, 2, '.', '');

		$input = $m.'/'.$d.'/'.$y;
		$date = strtotime($input);
		$current_date = date('d-M-y', $date);

		$input = '12:00:00';
		$time = strtotime($input);
		$current_date_time_3 = date('H:i:s', $time);

		$input = '15:00:00';
		$time = strtotime($input);
		$current_date_time_4 = date('H:i:s', $time);
		
		
		$current_date = strtoupper($current_date);
		
		$curr_date_data['WELL_ID'] = "'$station_id'";
		$curr_date_data['LOG_DATE'] = "'$current_date'";
		$curr_date_data['HR12'] = "'$hr12'";
		$curr_date_data['HR15'] = "'$hr15'";
		$curr_date_data['FLAG'] = "'1'";
		$curr_date_data['STATUS'] = "'P'";
		$curr_date_data['SMS_ID_LIST'] = "'$sms_id'";
		$curr_date_data['UPDATE_BY'] = "'$user_id'";
		$curr_date_data['UPDATE_DATE'] = "TO_DATE('$curr_date','DD-MON-RR')";
		$table_name = 'MDM_WL_RAINFALL_DATA_LOG';
		$where_cond = "WELL_ID='$station_id' AND TO_CHAR(LOG_DATE, 'DD-MON-YY')='$current_date'";
		//var_dump($curr_date_data);
		$update = $obj->Update_Or_Insert_Data_By_Check_Duplicate($table_name, $curr_date_data, $where_cond);
		$ret_msg .= $update."\n";

	} elseif($counter == 6) {
		$ret_msg = "count is 6";
		$station_id = $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];
		$hr18 = (float)($sms_content[2]/100);
		$hr6 = (float)($sms_content[3]/100);
		$hr9 = (float)($sms_content[4]/100);
		$rainfall = $sms_content[5];
		
		$hr18 =  number_format($hr18, 2, '.', '');
		$hr6 =  number_format($hr6, 2, '.', '');
		$hr9 =  number_format($hr9, 2, '.', '');
		
		$input = $m.'/'.$d.'/'.$y;
		$date = strtotime($input);
		$current_date = date('d-M-y', $date);
		$previous_date = date("d-M-y",strtotime ( '-1 day' , strtotime ( $input ) ));
		
		$input = '18:00:00';
		$time = strtotime($input);
		$previous_date_time = date('H:i:s', $time);

		$input = '06:00:00';
		$time = strtotime($input);
		$current_date_time_1 = date('H:i:s', $time);

		$input = '09:00:00';
		$time = strtotime($input);
		$current_date_time_2 = date('H:i:s', $time);
		
		
		$current_date = strtoupper($current_date);
		$previous_date = strtoupper($previous_date);
		
		$previous_log_info = $obj->View_column_details_By_Cond("MDM_WL_RAINFALL_DATA_LOG","ID,SMS_ID_LIST","WELL_ID='$station_id' AND LOG_DATE='$previous_date'");
		$prev_data_id = $previous_log_info['ID'];
		
		
		$prev_sms_list = $previous_log_info['SMS_ID_LIST'];
		$curr_sms_list = "";
		if($prev_sms_list){
			if(strpos($prev_sms_list,$sms_id)){
				$curr_sms_list = $prev_sms_list;
			}
			else{
				$curr_sms_list = "$prev_sms_list,$sms_id";
			}
		}
		else{
			$curr_sms_list = $sms_id;
		}
		$up_prev_data['WELL_ID'] = "'$station_id'";
		$up_prev_data['LOG_DATE'] = "'$previous_date'";
		$up_prev_data['HR18'] = "'$hr18'";
		$up_prev_data['FLAG'] = "'1'";
		$up_prev_data['STATUS'] = "'P'";
		$up_prev_data['SMS_ID_LIST'] = "'$curr_sms_list'";
		$up_prev_data['UPDATE_BY'] = "'$user_id'";
		$up_prev_data['UPDATE_DATE'] = "TO_DATE('$curr_date','DD-MON-RR')";
		$table_name = 'MDM_WL_RAINFALL_DATA_LOG';
		$prev_where_cond = "WELL_ID='$station_id' AND TO_CHAR(LOG_DATE, 'DD-MON-YY')='$previous_date'"; 
		//var_dump($up_prev_data);
		$up_prev_res = $obj->Update_Or_Insert_Data_By_Check_Duplicate($table_name, $up_prev_data, $prev_where_cond);
		$ret_msg .= $up_prev_res."\n";
		
		$ins_curr_data['WELL_ID'] = "'$station_id'";
		$ins_curr_data['LOG_DATE'] = "'$current_date'";
		$ins_curr_data['HR6'] = "'$hr6'";
		$ins_curr_data['HR9'] = "'$hr9'";
		$ins_curr_data['FLAG'] = "'1'";
		$ins_curr_data['STATUS'] = "'P'";
		$ins_curr_data['RAINFALL'] = "'$rainfall'";
		$ins_curr_data['SMS_ID_LIST'] = "'$sms_id'";
		$ins_curr_data['UPDATE_BY'] = "'$user_id'";
		$ins_curr_data['UPDATE_DATE'] = "TO_DATE('$curr_date','DD-MON-RR')";
		$curr_where_cond = "WELL_ID='$station_id' AND TO_CHAR(LOG_DATE, 'DD-MON-YY')='$current_date'";
		//var_dump($ins_curr_data);
		$ins_curr_res = $obj->Update_Or_Insert_Data_By_Check_Duplicate($table_name, $ins_curr_data, $curr_where_cond);
		$ret_msg .= $ins_curr_res."\n";

	} elseif($counter == 3) {
		$ret_msg = "count is 3";
		$station_id = $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);

		$previous_log_info = $obj->View_column_details_By_Cond("MDM_WL_RAINFALL_DATA_LOG","ID,SMS_ID_LIST","WELL_ID='$station_id' AND LOG_DATE='$curr_date'");
		$prev_data_id = $previous_log_info['ID'];
		
		
		$prev_sms_list = $previous_log_info['SMS_ID_LIST'];
		$curr_sms_list = "";
		if($prev_sms_list){
			if(strpos($prev_sms_list,$sms_id)){
				$curr_sms_list = $prev_sms_list;
			}
			else{
				$curr_sms_list = "$prev_sms_list,$sms_id";
			}
		}
		else{
			$curr_sms_list = $sms_id;
		}
		$up_rain_data['WELL_ID'] = "'$station_id'";
		$up_rain_data['LOG_DATE'] = "'$current_date'";
		$up_rain_data['RAINFALL'] = "'$rainfall'";
		$up_rain_data['FLAG'] = "'1'";
		$up_rain_data['STATUS'] = "'P'";
		$up_rain_data['SMS_ID_LIST'] = "'$curr_sms_list'";
		$up_rain_data['UPDATE_BY'] = "'$user_id'";
		$up_rain_data['UPDATE_DATE'] = "TO_DATE('$curr_date','DD-MON-RR')";
		$table_name = 'MDM_WL_RAINFALL_DATA_LOG';
		$rain_where_cond = "WELL_ID='$station_id' AND TO_CHAR(LOG_DATE, 'DD-MON-YY')='$current_date'"; 
		//var_dump($up_rain_data);
		$ins_rain_res = $obj->Update_Or_Insert_Data_By_Check_Duplicate($table_name, $up_rain_data, $rain_where_cond);
		$ret_msg .= $ins_rain_res."\n";
	}
	else{
		$ret_msg = "count is null";
	}
	return $ret_msg;
}
?>