<?php

include '../smtp.php';
include"initialize_page.php";
include"sms_function.php";
require "../twilio_sms.php";

//include"systemfunctions.php";


       if(isset($_POST['q'])&&$_POST['q']!="")
          {
			  $q=$_POST['q'];
			  
			  switch($q)
			      {
					
					  case "contact_withadmin":
					    {
							
							$subject_u=mysqli_real_escape_string($con,trim($_POST['csubjcet']));
							$reason_u=mysqli_real_escape_string($con,trim($_POST['creason']));
							$msg_u=mysqli_real_escape_string($con,trim($_POST['cmsg']));
							    if(($subject_u=="")||($reason_u=="")||($msg_u==""))
								 {
								echo "0";	 
								 }
								 else
								 {
									 mysqli_query($con,"INSERT into x_contact SET x_userid='$user_id', x_subject='$subject_u', x_reason='$reason_u', x_msg='$msg_u'");
									 
									 
									 echo "1";
								 }
							
						  break;	
						}
						case "update_userdetails":
						{
							
							$phone_number=mysqli_real_escape_string($con,trim($_POST['uphonenumber']));
							$address_user=mysqli_real_escape_string($con,trim($_POST['uaddress']));
							$city_user=mysqli_real_escape_string($con,trim($_POST['ucity']));
							$state_user=mysqli_real_escape_string($con,trim($_POST['ustate']));
							$zip_user=mysqli_real_escape_string($con,trim($_POST['uzip']));
							    if(($phone_number=="")||($address_user=="")||($city_user=="")||($state_user=="")||($zip_user==""))
							       {
									   
									   
									   echo "0";
								   }
								   else
								   {
									mysqli_query($con,"UPDATE x_users SET x_phonenumber='$phone_number', x_address='$address_user', x_city='$city_user', x_state='$state_user', x_zip='$zip_user' WHERE x_id='$user_id'");
									
									echo "1";   
								   }
							
						break;	
						}
						case "add_beneficiary":
						 {
							 
							 $n_fullname=mysqli_real_escape_string($con,trim($_POST['ufullname']));
							 $n_emailid=mysqli_real_escape_string($con,trim($_POST['uemailid']));
							 $n_phonenumber=mysqli_real_escape_string($con,trim($_POST['uphonenumber']));
							 $n_address=mysqli_real_escape_string($con,trim($_POST['uaddress']));
							 $n_idnumber=mysqli_real_escape_string($con,trim($_POST['uidnumber']));
							 $n_idtype=mysqli_real_escape_string($con,trim($_POST['uidtype']));
							 $n_relation=mysqli_real_escape_string($con,trim($_POST['urelation']));
							          if(($n_fullname=="")||($n_emailid=="")||($n_phonenumber=="")||($n_address=="")||($n_idnumber=="")||($n_idtype=="")||($n_relation==""))
								  {
									echo "0";  
									  
								  }
								  else
								  {
									mysqli_query($con,"INSERT into x_beneficiary SET x_fullname='$n_fullname', x_emailid='$n_emailid', x_phonenumber='$n_phonenumber', x_address='$n_address', x_idnumber='$n_idnumber', x_idtype='$n_idtype', x_relation='$n_relation', x_userid='$user_id'");
									
									echo "1";  
								  }
							 
							 
						break;	 
						 }
						 case "remove_beneficiaryfrom":
						     {
								 
								$id=$_POST['id'];
								mysqli_query($con,"DELETE FROM x_beneficiary WHERE x_id='$id' and x_userid='$user_id'"); 
							break;	 
							 }
							 case "edit_beneficiary":
							 {
								 	$id=$_POST['id'];
								 $n_fullname=mysqli_real_escape_string($con,trim($_POST['ufullname']));
							 $n_emailid=mysqli_real_escape_string($con,trim($_POST['uemailid']));
							 $n_phonenumber=mysqli_real_escape_string($con,trim($_POST['uphonenumber']));
							 $n_address=mysqli_real_escape_string($con,trim($_POST['uaddress']));
							 $n_idnumber=mysqli_real_escape_string($con,trim($_POST['uidnumber']));
							 $n_idtype=mysqli_real_escape_string($con,trim($_POST['uidtype']));
							 $n_relation=mysqli_real_escape_string($con,trim($_POST['urelation']));
							          if(($n_fullname=="")||($n_emailid=="")||($n_phonenumber=="")||($n_address=="")||($n_idnumber=="")||($n_idtype=="")||($n_relation==""))
								  {
									echo "0";  
									  
								  }
								  else
								  {
									mysqli_query($con,"UPDATE x_beneficiary SET x_fullname='$n_fullname', x_emailid='$n_emailid', x_phonenumber='$n_phonenumber', x_address='$n_address', x_idnumber='$n_idnumber', x_idtype='$n_idtype', x_relation='$n_relation' WHERE x_userid='$user_id' and x_id='$id'");
									
									echo "1";  
								  } 
							break;	 
							 }
							 case "change_password":
							    {
									
								$current_password=mysqli_real_escape_string($con,trim($_POST['currentpassword']));
								$new_password=mysqli_real_escape_string($con,trim($_POST['newpass']));
								$confirm_password=mysqli_real_escape_string($con,trim($_POST['confirmnewpass']));
								      if(($current_password=="")||($new_password=="")||($confirm_password==""))
									    {
											
										echo "0";	
										}
										else
										{
											if($new_password==$confirm_password)
											  {
												     $my_password=user_details($user_id,'x_password'); 
													   if($my_password==$current_password)
													     {
															 mysqli_query($con,"UPDATE x_users SET x_password='$new_password' WHERE x_id='$user_id'");
															 
															 echo "1";
														 }
														 else
														 {
														echo "3";	 
														 }
											  }
											  else
											  {
												  
												 echo "2"; 
											  }
											
										}
								
								
								break;	
								}
								case "change_pin":
								 {
									 $current_pin=mysqli_real_escape_string($con,trim($_POST['currentpin']));
									 $new_pin=mysqli_real_escape_string($con,trim($_POST['newpin']));
									 $confirm_pin=mysqli_real_escape_string($con,trim($_POST['confirmpin']));
									     if(($current_pin=="")||($new_pin=="")||($confirm_pin==""))
										   {
											echo "0";   
										   }
										   else
										   {
											    if($new_pin==$confirm_pin)
												   {
													    $my_pin=user_details($user_id,'x_acpin'); 
														  if($my_pin==$current_pin)
														    {
															mysqli_query($con,"UPDATE x_users SET x_acpin='$new_pin' WHERE x_id='$user_id'");
															 
															 echo "1";	
															}
															else
															{
															echo "3";	
															}
												   }
												   else
												   {
													echo "2";   
												   }
										   }
									 
								break;	 
								 }
	 case "start_local_fundtransfer":
	   {
	   	$x_id=get_x_id($con);
		  $amount = mysqli_real_escape_string($con,trim($_POST['transferamountx']));
                  $_SESSION['transferamountx']=$amount;
				  
				   if(isset($_POST['x_bankname'])) {
					   $bankName = trim($_POST['x_bankname']);
				   } else {
					   $bankName = trim($_POST['x_banknames']);
				   }
				  
                  $receiver_bankname=mysqli_real_escape_string($con,$bankName);
                  $_SESSION['bank_name']=$receiver_bankname;
		  $account_number=mysqli_real_escape_string($con,trim($_POST['receiveracnumberid']));
		  $description_tr=mysqli_real_escape_string($con,trim($_POST['trdescription'])); 
		  $account = mysqli_real_escape_string($con,trim($_POST['accountid'])); 
		  
		   $data_q = mysqli_query($con,"SELECT x_currencyid from x_anotheracc where x_time='$account_number'");
									 
		   $data_array=mysqli_fetch_array($data_q);
									 
		  $rec_currency_id =$data_array['x_currencyid'] ;
									
		  $user_currency_id = user_details($account,"x_currencyid");
									
		  $rec_currency_val = currency_data("x_currencyvalue",$rec_currency_id);
									 						 
		  $user_currency_val = currency_data("x_currencyvalue",$user_currency_id); 
									 
		 $amount_user =  currencyConverter($user_currency_val, $rec_currency_val,$amount);
		 $amount_user = str_replace(",","",$amount_user);
		
		
			 if(($amount_user == "")||($account_number == "")||($description_tr == "" || $account == ""))
			  {
				  
			
			  }
			  else
			  {
				  //test status
				  
				  $u_statusll=user_details($account_id,'x_status');
				  
				  if($u_statusll == "Y")
				  {
				  
				   //test amount
					 $amount_test = amount_testfun($amount_user,$account);
					   if($amount_test == 3)
					   {

						 //$account_test =account_testfun($account_number);
						 
//						  if($account_test == 5)
//							{
//								echo "7";
//							}
//							else
//							{
							//final step one transfer
							
							 $tr_date = date("Y-m-d");
							 $tr_fee = get_properties("local.transfer.fees");
							 $tr_code = rand(111111,666666);
							 $_SESSION['temp_trans_code'] = $tr_code;
							  mysqli_query($con,"INSERT into x_fundtransfer SET $x_id x_userid='$user_id',x_accountid ='$account',x_amount='$amount_user', x_receiveraccountnu='$account_number', x_description='$description_tr', x_trdate='$tr_date', x_trtype='local', x_fee='$tr_fee', x_step='step1', x_code='$tr_code'");
							
							//sedn email
							$my_name=user_details($user_id,'x_fname')." ".user_details($user_id,'x_lname');;
							$msg_template=otp_firsttemplate($tr_code,$my_name);
							
							$my_mail=user_details($user_id,'x_email');
							$admin_webmail = website_settingdata("x_webmail");
							$to =$my_mail;
$subject = "Funds Transfer Access Pin";

$message = $msg_template;

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <'.$admin_webmail.'>' . "\r\n";

//mail($to,$subject,$message,$headers);
sendMailSmtp($to,$subject,$message,$admin_webmail);

//****************changes*******************

$user_phone_number=user_details($user_id,"x_phonenumber");
$f_name=user_details($user_id,'x_fname');//." ".user_details($user_id,'x_lname');;
$message_text='You are transferring fund';
$message_text='Dear '.$f_name.' Your Funds Transfer Access Pin is:'.$tr_code;
sendMessage($user_phone_number,$message_text);
					
					
					
							echo "10";	
							//}
					   }
					   else
					   {
						echo $amount_test;   
					   }
				  }
				  else
				  { 
					  echo "25";
				  }
			  }
		   
		break;   
	   }
	   
	   case "test_amount_localtransfer":
	   {
		 
		   $input_amount =mysqli_real_escape_string($con,trim($_POST['amount'])); 
		  $account =mysqli_real_escape_string($con,trim($_POST['accid']));
	     $recever_accountno =mysqli_real_escape_string($con,trim($_POST['receveraccountno']));
		  

          if($input_amount!="")
		   {
			   $total_fund = user_details($account,'x_totalbalance');
			   $total_fund = str_replace(",","",$total_fund );
			
			   $tr_min = local_minmax("min");
			   $tr_max = local_minmax("max");
			   $tr_fee = get_properties("local.transfer.fees");
			   $input_amount1 = $input_amount + $tr_fee;
				  //test numeric or not
			   if(is_numeric($input_amount))
				{
					   if(($input_amount>=$tr_min)&&($input_amount<=$tr_max))
						{
							   //test in his account
							   
								 if($input_amount1 <= $total_fund)
								   {
									// echo "3";
									//echo $sql = "SELECT x_currencyid from x_anotheracc where x_time='$recever_accountno'";
									 $data_q=mysqli_query($con,"SELECT x_currencyid from x_anotheracc where x_time='$recever_accountno'");
									 
									 $data_array=mysqli_fetch_array($data_q);
									 
									 $rec_currency_id =$data_array['x_currencyid'] ;
									
									 $user_currency_id = user_details($account,"x_currencyid");
									
									 $rec_currency_val = currency_data("x_currencyvalue",$rec_currency_id);
									 
									 $rec_currency_sym = currency_data("x_currencysymbol",$rec_currency_id);
									 
									 $user_currency_val = currency_data("x_currencyvalue",$user_currency_id); 
									 
								 $converted_amount =  currencyConverter($user_currency_val, $rec_currency_val,$input_amount);
								 
								 echo "Converted Amount :". $rec_currency_sym. " " . $converted_amount ;
									   
								   }
								   else
								   {
									   echo "2";   
								   }
						}
						else
						{
							 echo "1";
						}
				}
				else
				{
				   echo "0";	 
				}
			   
		   }			   
		  
		   
		 /* $input_amount =mysqli_real_escape_string($con,trim($_POST['amount'])); 
		  
			   if($input_amount!="")
				 {
				  //get total fund in account 
				  
				  $total_fund= user_details($account_id,'x_totalbalance');
				  
				  //find min amount and max
				  
				  $tr_min=local_minmax("min");
				  $tr_max=local_minmax("max");
				  $tr_fee=get_properties("local.transfer.fees");
				 $input_amount1=$input_amount+$tr_fee;
				  //test numeric or not
				  
					  if(is_numeric($input_amount))
						 {
							   if(($input_amount>=$tr_min)&&($input_amount<=$tr_max))
								 {
									   //test in his account
									   
										 if($input_amount1<=$total_fund)
										   {
											   echo "3";
										   }
										   else
										   {
											echo "2";   
										   }
								}
								 else
								 {
									 echo "1";
								 }
						 }
						 else
						 {
						echo "0";	 
						 }
}*/
		break;   
	   }
		case "test_amount_localtransfer1":
	   {
		   $input_amount = mysqli_real_escape_string($con,trim($_POST['amount']));
            $accid = mysqli_real_escape_string($con,trim($_POST['accid'])); 		  
            $recever_accountno =mysqli_real_escape_string($con,trim($_POST['receveraccountno']));
            
			   if($input_amount!="")
				 {
				  //get total fund in account 
				  
				  $total_fund = user_details($accid,'x_totalbalance');
				  $total_fund = str_replace(",","",$total_fund);
				  
				  //find min amount and max
				  
				  $tr_min = local_minmax1("min");
				   $tr_max = local_minmax1("max");
				  $tr_fee = get_properties("international.transfer.fees");
				 $input_amount1 = $input_amount + $tr_fee;
				  //test numeric or not
				  
					  if(is_numeric($input_amount))
						 {
							   if(($input_amount>=$tr_min)&&($input_amount<=$tr_max))
								 {
									   //test in his account
									   
										 if($input_amount1 <= $total_fund)
										   {
											    $data_q=mysqli_query($con,"SELECT x_currencyid from x_anotheracc where x_time='$recever_accountno'");
									 
									 $data_array=mysqli_fetch_array($data_q);
									 
									 $rec_currency_id =$data_array['x_currencyid'] ;
									
									 $user_currency_id = user_details($accid,"x_currencyid");
									
									 $rec_currency_val = currency_data("x_currencyvalue",$rec_currency_id);
									 
									 $rec_currency_sym = currency_data("x_currencysymbol",$rec_currency_id);
									 
									 $user_currency_val = currency_data("x_currencyvalue",$user_currency_id); 
									 
								 $converted_amount =  currencyConverter($user_currency_val, $rec_currency_val,$input_amount);
								 
								 echo "Converted Amount :". $rec_currency_sym. " " . $converted_amount ;
										   }
										   else
										   {
											echo "2";   
											}
										   
										   
										   
								 }
								 else
								 {
									 echo "1";
								 }
						 }
						 else
						 {
						echo "0";	 
						 }
					 
					 
				 }
		break;   
	   }
	   case "test_accountnumber":
		{
			$account_number=mysqli_real_escape_string($con,trim($_POST['accountnu']));
			  if($account_number!="")
			   {
				   //fetch account user respective
				   
				   $data_q=mysqli_query($con,"SELECT xu.x_fname,xu.x_lname FROM x_users xu INNER JOIN x_anotheracc xa ON xu.x_id=xa.x_userid WHERE xa.x_time='$account_number'");
					$data_row=mysqli_num_rows($data_q);
					   if($data_row==0)
						{
						echo "0";	
						}
						else
						{
						$data_array=mysqli_fetch_array($data_q);
						echo $data_array['x_fname']." ".$data_array['x_lname'];	
						}
				   
			   }
			
			
		break;	
		}
		case "international_transfer":
		  {
			  $x_id=get_x_id($con);
							
							
				$receiver_bankname=mysqli_real_escape_string($con,trim($_POST['rbankname']));
				$account_number=mysqli_real_escape_string($con,trim($_POST['raccountnumber']));
				$name_reciever=mysqli_real_escape_string($con,trim($_POST['recname']));
				$xsxwxixfxtxcode=mysqli_real_escape_string($con,trim($_POST['xsxwxixfxtxcodeu']));
				$amount_user=mysqli_real_escape_string($con,trim($_POST['transferamountx']));
                                $_SESSION['transferamountx']=$amount_user;
                                $_SESSION['bank_name']=$receiver_bankname;
				$user_country=mysqli_real_escape_string($con,trim($_POST['ucountry']));
		        $description_tr=mysqli_real_escape_string($con,trim($_POST['transferdescription'])); 
				$account =mysqli_real_escape_string($con,trim($_POST['accountid'])); 
		      $data_q = mysqli_query($con,"SELECT x_currencyid from x_anotheracc where x_time='$account_number'");
									 
		   $data_array=mysqli_fetch_array($data_q);
									 
		  $rec_currency_id =$data_array['x_currencyid'] ;
									
		  $user_currency_id = user_details($account,"x_currencyid");
									
		  $rec_currency_val = currency_data("x_currencyvalue",$rec_currency_id);
									 						 
		  $user_currency_val = currency_data("x_currencyvalue",$user_currency_id); 
									 
//		 $amount_user =  currencyConverter($user_currency_val, $rec_currency_val,$amount_user);
//		 $amount_user = str_replace(",","",$amount_user);
			 if(($amount_user=="")||($account_number=="")||($description_tr=="")||($receiver_bankname=="")||($name_reciever=="")||($xsxwxixfxtxcode=="") || ($account == ""))
			  {
				  
				  echo "4";
			  }
			  else
			  {
				  
				   $u_statusll = user_details($account,'x_status');
				  
				  if($u_statusll=="Y")
				  {
					  
				   //test amount
					 $amount_test = amount_testfun1($amount_user,$account);
					   if($amount_test==3)
					   {
						
						    $tr_date=date("Y-m-d");
							$tr_fee=get_properties("international.transfer.fees");
							$tr_code = rand(111111,666666);

							mysqli_query($con,"INSERT into x_fundtransfer SET $x_id x_userid='$user_id',x_accountid ='$account', x_amount='$amount_user', x_receiveraccountnu='$account_number', x_description='$description_tr', x_trdate='$tr_date', x_trtype='international', x_fee='$tr_fee', x_step='step1', x_code='$tr_code', x_receiverbankname='$receiver_bankname', x_receivername='$name_reciever', x_xsxwxixfxtxcode='$xsxwxixfxtxcode', x_country='$user_country'");
							
							$my_name=user_details($user_id,'x_fname')." ".user_details($user_id,'x_lname');
							$msg_template=otp_firsttemplate($tr_code,$my_name);
							$my_mail=user_details($user_id,'x_email');
							$admin_webmail=website_settingdata("x_webmail");
							$to =$my_mail;
$subject = "Funds Transfer Access Pin";

$message = $msg_template;

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <'.$admin_webmail.'>' . "\r\n";


//mail($to,$subject,$message,$headers);
sendMailSmtp($to,$subject,$message,$admin_webmail);

/*************change*******/
$user_phone_number=user_details($user_id,"x_phonenumber");
$f_name = user_details($user_id,'x_fname');
$message_text='Dear '.$f_name.' Your Funds Transfer Access Pin is:'.$tr_code;
sendMessage($user_phone_number,$message_text);							
							
							
							echo "10";	
							
					   }
					   else
					   {
						echo $amount_test;   
					   }
				  }
				  else
				  {
					  echo "25";
				  }
					   
			  }
		   
		break;   
		  }
									  case "local_test_timer":
									  {
										  $test_id=$_POST['id'];
										  
										  $sql = mysqli_query($con,"SELECT TIMEDIFF(NOW(), x_date) AS difference FROM `x_fundtransfer` where x_id='$test_id'"); 
$result = mysqli_fetch_array($sql); 

 $remaing_timetime=$result['difference'];
				$apply_exp=explode(":",$remaing_timetime);
				 
				 $app_minutes=$apply_exp[1];
			
				  $app_second=$apply_exp[2];
				 
				 //fetch 
				
					
				  $xx_minute=9-$app_minutes;
				
				 $xx_second=59-$app_second;

				  if($xx_minute<0)
	               
				   {
					   
					echo "1";   
				   }
				   else
				   {
					echo $xx_minute.":".$xx_second;   
				   }
										  
										break;  
									  }
									  case "local_test_timerblock":
									  {
										  
									$test_id=$_POST['id'];
									  mysqli_query($con,"UPDATE x_fundtransfer SET x_step='B' WHERE x_id='$test_id'");
									break;	  
										  
									  }
									  case "validate_step_local1":
									    {
											$test_id=$_POST['id'];
											$input_code=mysqli_real_escape_string($con,trim($_POST['pinipix']));
											
											if($input_code=="")
											{
												echo "0";
											}
											else
											{
												$test_q=mysqli_query($con,"SELECT * FROM x_fundtransfer WHERE x_id='$test_id' and x_userid='$user_id'");
												$test_row=mysqli_num_rows($test_q);
											    if($test_row!=0)
												{
													$test_array=mysqli_fetch_array($test_q);
													$test_pin=$test_array['x_code'];
													if(website_settingdata("noofstepinfundtransfer")==2)
														$step='step5';
													else if(website_settingdata("noofstepinfundtransfer")==1)
														$step='C';
													else
														$step='step2';
														
														if($test_pin==$input_code)
													    {
															mysqli_query($con,"UPDATE x_fundtransfer SET x_step='$step' WHERE x_id='$test_id'");
															$_SESSION['xx_id']=$test_id;         	
															echo "1";

															//send email with attachment
															$email_userbalance=user_details($user_id ,"x_totalbalance");
															$e_useraccount=user_details($user_id,"x_time");
															$e_email=user_details($user_id,"x_email");
															$u_phonenumber=user_details($user_id,"x_phonenumber");
															$mailer_template =mailer_debitcredittempl($e_useraccount,$full_name,$tr_description,$statenentlast_id,$email_userbalance,$converted_currency,'debit',$curr_id);

															$admin_webmail=website_settingdata("x_webmail");
															$to =$e_email;
															 
															$subject = "Transaction Notification Mail ";
										
															$message = $mailer_template;
										
															// Always set content-type when sending HTML email
															$headers = "MIME-Version: 1.0" . "\r\n";
															$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
										
															// More headers
															$headers .= 'From: <'.$admin_webmail.'>' . "\r\n";
										
										
															//mail($to,$subject,$message,$headers);
															//sendMailSmtp($to,$subject,$message,$admin_webmail);


														}
														else
														{
															echo "2";	 
														}
														
												}
											}
											break;
										}
										case "validate_step_local2":
										{
											$id=$_POST['id'];
											
											$pin_code=trim($_POST['pinopin']);
											if($pin_code=="")
											  {
												echo "0";  
											  }
											  else
											  {
													
													$userQry = mysqli_query($con, "SELECT * FROM x_users where x_userid = '".$_SESSION['mastersession']."' ");
													$getUserData = mysqli_fetch_array($userQry);
																									  
												  //fetch pin
												  $test_q=mysqli_query($con,"SELECT * FROM x_code WHERE x_userid=".$getUserData['x_id']." and x_type='COT' ORDER by x_id DESC");
												  $test_row=mysqli_num_rows($test_q);
												      if($test_row==0)
													    {
														echo "2";	
														}
														else
														{
															$test_array=mysqli_fetch_array($test_q);
															$test_code=$test_array['x_code'];
															if(website_settingdata("noofstepinfundtransfer")==3)
														$step='step5';
													else
														$step='step3';
															  if($test_code==$pin_code)
                                                                  {
																	  mysqli_query($con,"UPDATE x_fundtransfer SET x_step='$step' WHERE x_id='$id'");
																	echo "1";  
																  }
																  else
																  {
																	echo "2";  
																  }
  														}
												    
											  }
											
										break;	
										}
										case "validate_step_local3":
										{
											
										$id=$_POST['id'];
											
											$pin_code=trim($_POST['pinopin']);
											if($pin_code=="")
											  {
												echo "0";  
											  }
											  else
											  {

												$userQry = mysqli_query($con, "SELECT * FROM x_users where x_userid = '".$_SESSION['mastersession']."' ");
												$getUserData = mysqli_fetch_array($userQry);

												  //fetch pin
												  $test_q=mysqli_query($con,"SELECT * FROM x_code WHERE x_userid=".$getUserData['x_id']." and x_type='TAX' ORDER by x_id DESC");
												  $test_row=mysqli_num_rows($test_q);
												      if($test_row==0)
													    {
														echo "2";	
														}
														else
														{
															$test_array=mysqli_fetch_array($test_q);
															$test_code=$test_array['x_code'];
															if(website_settingdata("noofstepinfundtransfer")==4)
														$step='step5';
													else
														$step='step2';
															  if($test_code==$pin_code)
                                                                  {
																	  mysqli_query($con,"UPDATE x_fundtransfer SET x_step='step4' WHERE x_id='$id'");
																	echo "1";  
																  }
																  else
																  {
																	echo "2";  
																  }
  														}
												    
											  }
											
										break;		
											
										}
										case "validate_step_local4":
										{
											
											$id=$_POST['id'];
											
											$pin_code=trim($_POST['pinopin']);
											if($pin_code=="")
											  {
												echo "0";  
											  }
											  else
											  {

												$userQry = mysqli_query($con, "SELECT * FROM x_users where x_userid = '".$_SESSION['mastersession']."' ");
												$getUserData = mysqli_fetch_array($userQry);

												  //fetch pin
												  $test_q=mysqli_query($con,"SELECT * FROM x_code WHERE x_userid=".$getUserData['x_id']." and x_type='ATC' ORDER by x_id DESC");
												  $test_row=mysqli_num_rows($test_q);
														if($test_row==0)
													    {
															echo "2";	
														}
														else
														{
															$test_array=mysqli_fetch_array($test_q);
															$test_code=$test_array['x_code'];
															if($test_code==$pin_code)
                                                            {
																  mysqli_query($con,"UPDATE x_fundtransfer SET x_step='step5' WHERE x_id='$id'");
																echo "1";  
															}
															else
															  {
																echo "2";  
															  }
  														}
												    
											  }
											
										break;			
											
											
										}
										case "validate_step_local5":
										{
										$id=$_POST['id'];
											
											$pin_code=trim($_POST['pinopin']);
											if($pin_code=="")
											  {
												echo "0";  
											  }
											  else
											  {
												$userQry = mysqli_query($con, "SELECT * FROM x_users where x_userid = '".$_SESSION['mastersession']."' ");
												$getUserData = mysqli_fetch_array($userQry);

												  //fetch pin
												  $test_q=mysqli_query($con,"SELECT * FROM x_code WHERE x_userid=".$getUserData['x_id']." and x_type='IFI' ORDER by x_id DESC");
												  $test_row=mysqli_num_rows($test_q);
												      if($test_row==0)
													    {
														echo "2";	
														}
														else
														{
															$test_array=mysqli_fetch_array($test_q);
															$test_code=$test_array['x_code'];
															  if($test_code==$pin_code)
                                                                  {
																	  mysqli_query($con,"UPDATE x_fundtransfer SET x_step='C' WHERE x_id='$id'");
																	  $_SESSION['xx_id']=$id;
                                                                     	echo "1";  
																  }
																  else
																  {
																	echo "2";  
																  }
  														}
												    
											  }
											
										break;				
											
											
										}
										case "send_chatmsg":
										{
											
											$msg_msg=mysqli_real_escape_string($con,trim($_POST['trimmsg']));
											  if($msg_msg=="")
											   {
												   echo "0";
											   }
											   else
											   {
												   
												  mysqli_query($con,"INSERT into x_chat SET x_userid='$user_id', x_by='user', x_msg='$msg_msg', x_readstatus='U'"); 
												   echo "1";
											   }
											   
											   break;
										}
										case "show_all_transfers":
										    {
										        
										      $from_date=$_POST['from'];
	                                          $to_date=$_POST['to'];
    $next_date=date('Y-m-d', strtotime('+1 day', strtotime($to_date)));
	
	/*CAST(x_trdate AS DATE) to order by */
    $statement_q = mysqli_query($con,"SELECT * FROM x_statement WHERE (x_userid='$user_id' and x_acountid='$account_id' and x_date>='$from_date' AND x_date<='$next_date') ORDER by x_trdate DESC");
    //echo $xx=mysqli_num_rows($statement_q);
	                                   echo'<thead>
											<tr>
												<th>Transaction Date</th>
												<th>Refrence No#</th>
												<th>Description</th>
												<th>Fee</th>
												<th>Debit</th>
												<th>Credit</th>
											</tr>
										</thead>';
										
	while($statement_array=mysqli_fetch_array($statement_q))
	   {
		    if($statement_array['x_type']=="debit") 
			{  
		       $amount = $statement_array["x_amount"];
			   if($amount !=""){
				  $amount =  number_format($amount,2); 
			   }
			
		      $dunia_mc = currency_data("x_currencysymbol",$currency_id)."".$amount; 
			} 
			else 
			{  
		      $dunia_mc=""; 
			} 
			
			 if($statement_array['x_type']=="credit")
			 {  
		       $amount = $statement_array["x_amount"];
			   if($amount !=""){
			     $amount = number_format($amount,2);
			   }
		       $t_pkd =currency_data("x_currencysymbol",$currency_id)."".$amount; 
			 } 
			 else 
			 { 
 			   $t_pkd=""; 
			 } 
			   $har_chima=str_pad($statement_array["x_id"], 5, "0", STR_PAD_LEFT);
			   
			  $fee = $statement_array['x_fee'];
		      $fee = number_format($fee,2);
			  if($fee == ""){
				  $fee = "0.00";
			  }
                                        echo
											'
											
											<tbody>
											<tr>
												<td>'.$statement_array['x_trdate'].'</td>
												<td>#'.$har_chima.'</td>
												<td>'.$statement_array['x_description'].'</td>
												<td>'.currency_data("x_currencysymbol",$currency_id).''.$fee.'</td>
												<td class="text-danger">'.$dunia_mc.'</td>
												<td class="text-success">'.$t_pkd.'</td>
												
											</tr>
											</tbody>
											';
										
										
	    }
										        
										        break;
										    }
										    case "show_last_10_transfers":
										    {
										        
										      
   
	
    $statement_q = mysqli_query($con,"SELECT * FROM x_statement WHERE (x_userid='$user_id' and x_acountid='$account_id') ORDER by x_trdate DESC limit 0,10");
    //echo $xx=mysqli_num_rows($statement_q);
	                                   echo'<thead>
											<tr>
												<th>Transaction Date</th>
												<th>Refrence No#</th>
												<th>Description</th>
												<th>Fee</th>
												<th>Debit</th>
												<th>Credit</th>
											</tr>
										</thead>';
										
	while($statement_array=mysqli_fetch_array($statement_q))
	   {
		    if($statement_array['x_type']=="debit") 
			{  
		       $amount = $statement_array["x_amount"];
			   if($amount !=""){
				  $amount =  number_format($amount,2); 
			   }
			
		      $dunia_mc = currency_data("x_currencysymbol",$currency_id)."".$amount; 
			} 
			else 
			{  
		      $dunia_mc=""; 
			} 
			
			 if($statement_array['x_type']=="credit")
			 {  
		       $amount = $statement_array["x_amount"];
			   if($amount !=""){
			     $amount = number_format($amount,2);
			   }
		       $t_pkd =currency_data("x_currencysymbol",$currency_id)."".$amount; 
			 } 
			 else 
			 { 
 			   $t_pkd=""; 
			 } 
			   $har_chima=str_pad($statement_array["x_id"], 5, "0", STR_PAD_LEFT);
			   
			  $fee = $statement_array['x_fee'];
		      $fee = number_format($fee,2);
			  if($fee == ""){
				  $fee = "0.00";
			  }
                                        echo
											'
											
											<tbody>
											<tr>
												<td>'.$statement_array['x_trdate'].'</td>
												<td>#'.$har_chima.'</td>
												<td>'.$statement_array['x_description'].'</td>
												<td>'.currency_data("x_currencysymbol",$currency_id).''.$fee.'</td>
												<td  class="text-danger">'.$dunia_mc.'</td>
												<td  class="text-success">'.$t_pkd.'</td>
												
											</tr>
											</tbody>
											';
										
										
	    }
										        
										        break;
										    }
										case "show_last_10_fund_transfers":
										    {
										        
										      
   
	
    $statement_q = mysqli_query($con,"SELECT * FROM x_fundtransfer WHERE (x_userid='$user_id' and x_accountid='$account_id') and (x_step='C' or x_step='CF' ) ORDER by x_trdate DESC limit 0,10");
    //echo $xx=mysqli_num_rows($statement_q);
	                                   echo'<thead>
											<tr>
												<th>Transaction Date</th>
												<th>Refrence No#</th>
												<th>Description</th>
												<th>Fee</th>
												<th>Amount</th>
												<th>Status</th>
											</tr>
										</thead>';
										
	while($statement_array=mysqli_fetch_array($statement_q))
	   {
		   
                                        echo
											'
											
											<tbody>
											<tr>
												<td>'.$statement_array['x_trdate'].'</td>
												<td>#'.$statement_array['x_id'].'</td>
												<td>'.$statement_array['x_description'].'</td>
												<td>'.currency_data("x_currencysymbol",$currency_id).''.$statement_array['x_fee'].'</td>"
												<td>'.currency_data("x_currencysymbol",$currency_id).''.$statement_array['x_amount'].'</td>';
												if($statement_array['x_step']=='C')
												echo '<td  class="text-danger">Pending</td>';
											else if($statement_array['x_step']=='CF')
												echo '<td  class="text-success">Completed</td>';
											else
												echo '<td></td>';
												
											echo '</tr>
											</tbody>
											';
										
										
	    }
										        
										        break;
										    }
		 case "apply_for_loan":{
				   
					 $loan_type = trim($_POST['loantypeid']);
					 $emp_type = $_POST['emptype'];
					 $req_amount = $_POST['reqamount'];
					 $noof_month = $_POST['loanmonth'];
					 $monthly_pay = $_POST['monthlypay'];
					 $interest_pay = $_POST['interestpay'];
					 $total_pay = $_POST['totalpay'];
					
					  if($loan_type == "" || $emp_type=="" ||$req_amount == "")
					  {
						 echo "empty"; 
					  }
					  else
					  {
						  if(is_numeric($req_amount) && is_numeric($noof_month))
						  {
						
							// echo $sql = "INSERT into x_loanreq SET x_userid='$user_id', x_loantypeid='$loan_type', x_emptype='$emp_type', x_reqamount='$req_amount',x_status='N'";
							  mysqli_query($con,"INSERT into x_loanreq SET x_userid='$user_id', x_loantypeid='$loan_type', x_emptype='$emp_type', x_reqamount='$req_amount',x_nomonth='$noof_month',x_monthlypay='$monthly_pay',x_interestpay='$interest_pay',x_totalpay='$total_pay',x_status='N'"); 
							  
							  echo "done"; 
						  }
						  else
						  {
							echo "notnumeric";  
						  }
					  }
					break;
				}
				
		case "get_loan_interest":
		{
			$loantypeid=mysqli_real_escape_string($con,trim($_POST['loantypeid']));
			
			  if($loantypeid!="")
			   {
				   //fetch account user respective
				   
				   $data_q=mysqli_query($con,"SELECT x_loaninterest from x_loantype where x_id ='$loantypeid'");
					$data_row=mysqli_num_rows($data_q);
					   if($data_row==0)
						{
						echo "0";	
						}
						else
						{
						$data_array=mysqli_fetch_array($data_q);
						echo $data_array['x_loaninterest']." %";	
						}
				   
			   }
			
			
		break;	
		}

       case "test_cds_minamount":
	   {
		   $input_amount=mysqli_real_escape_string($con,trim($_POST['amount'])); 
		   
              			 
			   if($input_amount!="")
				 {
				 
				  //test numeric or not
				  
					  if(is_numeric($input_amount))
						{
							  $min_investement =  cds_setting('x_minimuminv');
							  $interest = cds_setting('x_interest');          
							   if(($input_amount >= $min_investement))
								 {
									echo  $interest;  
								 }
								 else
								 {
									 echo "amount_min";
								 }
					    }
					    else
					    {
						  echo "notnumeric";	 
					    }
			    }
		break;   
	   }
	   case "test_investment_minamount":
	   {
		   $input_amount=mysqli_real_escape_string($con,trim($_POST['amount'])); 
		   
              			 
			   if($input_amount!="")
				 {
				 
				  //test numeric or not
				  
					  if(is_numeric($input_amount))
						{
							  $min_investement =  investment_setting('x_mininvest');
							  $interest = investment_setting('x_interest');          
							   if(($input_amount >= $min_investement))
								 {
									echo  $interest;  
								 }
								 else
								 {
									 echo "amount_min";
								 }
					    }
					    else
					    {
						  echo "notnumeric";	 
					    }
			    }
		break;   
	   }	
        case "apply_for_cds":
		{
		   
			
			 $amount = $_POST['amount'];
			 $interest = $_POST['interest'];
			 $interest = str_replace(" %","",$interest);
			 
			 $number = $_POST['number'];
			 $period = $_POST['period'];
			 $matured_amount= $_POST['matutredamount'];
			 $earn_interest = $_POST['earninterest'];
	        
		    if( $period =="365")
			{
			       $period = 'days';
		    }
			 elseif( $period =="12")
			{
			       $period = 'months';
		    }
			 if( $period =="1")
			{
			       $period = 'years';
		    }
			    $total_period =  $number ." ". $period;
					  if( $amount=="" ||$interest == ""||$number == ""||$period == ""||$matured_amount == ""||$earn_interest == "")
					  {
						 echo "empty"; 
					  }
					  else
					  {
						  if(is_numeric($number) )
						  {
					
							  mysqli_query($con,"INSERT into x_cds SET x_userid='$user_id', x_investamount='$amount'
							  ,x_maturedamount='$matured_amount',x_interest='$interest',x_earninterest='$earn_interest'
							  ,x_status='N',x_period='$total_period'"); 
							  
							  echo "done"; 
						  }
						  else
						  {
							echo "notnumeric";  
						  }
					  }
					break;
	    }

        case "apply_for_investment":
		{
		   
			
			 $amount = $_POST['amount'];
			 $interest = $_POST['interest'];
			 $interest = str_replace(" %","",$interest);
			 
			 $number = $_POST['number'];
			 $period = $_POST['period'];
			 $total_amount= $_POST['matutredamount'];
			 $earn_interest = $_POST['earninterest'];
			 $frequency = $_POST['frequency'];
			 
	      
		    if( $period =="365")
			{
			       $period = 'days';
		    }
			 elseif( $period =="12")
			{
			       $period = 'months';
		    }
			 if( $period =="1")
			{
			       $period = 'years';
		    }
			    $total_period =  $number ." ". $period;
					  if( $amount=="" ||$interest == ""||$number == ""||$period == ""||$total_amount == ""||$earn_interest == "" ||$frequency=="")
					  {
						 echo "empty"; 
					  }
					  else
					  {
						  if(is_numeric($number) )
						  {
					
				//echo $sql = "INSERT into x_investment SET x_userid='$user_id', x_invamount='$amount',x_totalearn='$total_amount',x_interest='$interest',x_interestearn='$earn_interest',x_status='N',x_invperiod='$total_period',x_reqbal='N'";
							  
							  mysqli_query($con,"INSERT into x_investment SET x_userid='$user_id', x_invamount='$amount'
							  ,x_totalearn='$total_amount',x_interest='$interest',x_interestearn='$earn_interest'
							  ,x_status='N',x_invperiod='$total_period',x_reqbal='N',x_interestreq='$frequency'"); 
							  
							  echo "done"; 
						  }
						  else
						  {
							echo "notnumeric";  
						  }
					  }
					break;
	    }		

        case "loan_payment":
	    {
			 $acc_id = $_POST['account'];
			 
			if($acc_id!="")
			{
				
				
				$date = date('Y-m-d', strtotime('+1 months'));
				 
				$select_query = "select * from x_loanstatement where x_loanid = '$loan_id' and x_duedate <= '$date' and x_status='N'";
				$select_result = mysqli_query($con,$select_query);
				$num = mysqli_num_rows($select_result);
				
				$due_total = 0;
			
				$i=0;
				if($num >0)
				{
					$cur_date = date("Y-m-d"); 
					while($row = mysqli_fetch_array($select_result))
					{	
						 $due_pay = $row['x_duepay'];
						 $due_total +=$due_pay;
						 //$rem_bal = $row['x_rembalance'];
						 $due_date = $row['x_duedate'];
					}
					
					   $curr_date = date('Y-m-d');
					   $acc_bal = user_details($acc_id,'x_totalbalance');
					   $acc_bal = str_replace(",","",$acc_bal);
					   $curr_id = user_details($acc_id,'x_currencyid');
					   $acc_no = user_details($acc_id,'x_time');
					   
					   $f_name = user_details($user_id,'x_fname');
					   $l_name = user_details($user_id,'x_lname');
					   $full_name = $f_name." ".$l_name;
					   $currency_symbol = currency_data('x_currencysymbol', $curr_id);
					   
					   
					   $currency_id = user_details($acc_id,'x_currencyid');
			           $to =  currency_data('x_currencyvalue',$currency_id);
	                   $from = main_account_currencyvalue($user_id);
                       $converted_currency = currencyConverter($from,$to,$due_total);
					   
					   
					   $tr_description = "Debited Loan Installment of ".$currency_symbol. $converted_currency." from ".$full_name." Account ".$acc_no;
					 
		               
                      //echo $converted_currency;
					  
					   $converted_currency = str_replace(",","",$converted_currency);
					  
					  if($acc_bal >= $converted_currency)
					  {
						$loan_query = "select x_remainingamount from x_loan where x_id='$loan_id'";
				        $loan_result = mysqli_query($con,$loan_query);
				        $num = mysqli_num_rows($loan_result); 
						$loanrow = mysqli_fetch_array($loan_result);
						$remaining_amount = $loanrow['x_remainingamount'];
						 
						
						$new_remamount =   $remaining_amount - $converted_currency ;
						
						// $now_userbal = $acc_bal - $due_total;
						  mysqli_query($con,"UPDATE x_loan SET x_remainingamount='$new_remamount' WHERE x_id= '$loan_id'");
						 
					      mysqli_query($con,"INSERT into x_statement SET x_userid='$user_id',x_acountid='$acc_id',x_amount='$converted_currency',x_type='debit',x_trdate='$curr_date',	x_description='$tr_description',x_fee='0',x_status='Y'"); 
						  
						  $statenentlast_id = mysqli_insert_id($con);
						  
						  $now_userbal = $acc_bal - $converted_currency;
						  mysqli_query($con,"UPDATE x_anotheracc SET x_totalbalance='$now_userbal' WHERE x_id= '$acc_id'");
						  
						  for($a=0;$a < $num;$a++){
							 
							
							  mysqli_query($con,"UPDATE x_loanstatement SET x_paydate='$curr_date',x_status='Y' WHERE  x_loanid = '$loan_id' and x_duedate <= '$date' and x_status='N' ");
						  }
					$converted_currency =  number_format($converted_currency,2);	  
						 
					//send mail to user
					$email_userbalance=user_details($acc_id ,"x_totalbalance");
					$e_useraccount=user_details($acc_id,"x_time");
					$e_email=user_details($user_id,"x_email");
					$u_phonenumber=user_details($user_id,"x_phonenumber");
					$mailer_template =mailer_debitcredittempl($e_useraccount,$full_name,$tr_description,$statenentlast_id,$email_userbalance,$converted_currency,'debit',$curr_id);
					
					$admin_webmail=website_settingdata("x_webmail");
					$to =$e_email;
					 
                    $subject = "Transaction Notification Mail ";

                    $message = $mailer_template;

					// Always set content-type when sending HTML email
					$headers = "MIME-Version: 1.0" . "\r\n";
					$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

					// More headers
					$headers .= 'From: <'.$admin_webmail.'>' . "\r\n";


					//mail($to,$subject,$message,$headers);
					sendMailSmtp($to,$subject,$message,$admin_webmail);

					$tttx=date("l jS \of F Y h:i:s A");
					$sms_text='Debit Alert
					Acc: '.$e_useraccount.'
					Amt:'.$converted_currency.'
					Desc: '.$tr_description.' 
					Time:'.$tttx.'
					Avail Bal:'.$email_userbalance;
					sendsms($u_phonenumber,$sms_text);
															
					echo "done";
						  
						  
						  
					  }
					  else
					  {
						  echo "lessbal";
					  }
					
				}
				else
				{
					echo "nodue";
				}
		
			}
			else
			{
				echo 'empty';
			}
		  break;
	    }		

		case "investment_recive":
	    {
			
			 
	                  $date = date('Y-m-d', strtotime('+1 days'));
					 //$loan_id = $_SESSION['loan_id'];
					$select_query = "select * from x_investmentstatement where x_invid = '$inv_id' and x_recivedate <= '$date' and x_status='N' ";
					$select_result = mysqli_query($con,$select_query);
					 $num = mysqli_num_rows($select_result);
					
					$recive_total = 0;
				
					$i=0;
					if($num >0)
					{
						$cur_date = date("Y-m-d"); 
						while($row = mysqli_fetch_array($select_result))
						{	
							 $recive_pay = $row['x_reciveinterest'];
							 $recive_total +=$recive_pay;
							 //$rem_bal = $row['x_rembalance'];
							 $recive_date = $row['x_recivedate'];
						}
					
					
				 $account_query = "select * from x_anotheracc where x_userid='$user_id' and x_for_main='main_account'";
					
				$account_result = mysqli_query($con,$account_query);
				$account_row = mysqli_fetch_array($account_result);
				
				 $acc_id = $account_row['x_id'];
					
					
					   $curr_date = date('Y-m-d');
					   $acc_bal = user_details($acc_id,'x_totalbalance');
					   $acc_bal = str_replace(",","",$acc_bal);
					   $curr_id = user_details($acc_id,'x_currencyid');
					   $acc_no = user_details($acc_id,'x_time');
					   
					   $f_name = user_details($user_id,'x_fname');
					   $l_name = user_details($user_id,'x_lname');
					   $full_name = $f_name." ".$l_name;
					   $currency_symbol = currency_data('x_currencysymbol',$curr_id);
					   
					   $recive_total =  number_format($recive_total,2);
					   
					   $tr_description = "Credited Investment Installment of ".$currency_symbol. $recive_total." from ".$full_name." Account ".$acc_no;
					   
					   $recive_total = str_replace(",","",$recive_total);
					   
					      mysqli_query($con,"INSERT into x_statement SET x_userid='$user_id',x_acountid='$acc_id',x_amount='$recive_total',x_type='credit',x_trdate='$curr_date',x_description='$tr_description',x_fee='0',x_status='Y'"); 
						  
						  $statenentlast_id = mysqli_insert_id($con);
						  
						  $now_userbal = $acc_bal + $recive_total;
						  mysqli_query($con,"UPDATE x_anotheracc SET x_totalbalance='$now_userbal' WHERE x_id= '$acc_id'");
						  
						  for($a=0;$a < $num;$a++){
							 
							
							  mysqli_query($con,"UPDATE x_investmentstatement SET x_reciveddate='$curr_date',x_status='Y' WHERE x_invid = '$inv_id' AND x_recivedate <= '$date' and  x_status='N' ");
						  }
					$recive_total =  number_format($recive_total,2);	  
						
					//send mail to user
					$email_userbalance=user_details($acc_id ,"x_totalbalance");
					$e_useraccount=user_details($acc_id,"x_time");
					$e_email=user_details($user_id,"x_email");
					$u_phonenumber=user_details($user_id,"x_phonenumber");
					$mailer_template =mailer_debitcredittempl($e_useraccount,$full_name,$tr_description,$statenentlast_id,$email_userbalance,$recive_total,'credit',$curr_id);
					
					 $admin_webmail=website_settingdata("x_webmail");
					 $to =$e_email;
					 
                     $subject = "Transaction Notification Mail ";

                    $message = $mailer_template;

					// Always set content-type when sending HTML email
					$headers = "MIME-Version: 1.0" . "\r\n";
					$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

					// More headers
					$headers .= 'From: <'.$admin_webmail.'>' . "\r\n";


					//mail($to,$subject,$message,$headers);
					sendMailSmtp($to,$subject,$message,$admin_webmail);

					$tttx=date("l jS \of F Y h:i:s A");
					$sms_text='Credit Alert
					Acc: '.$e_useraccount.'
					Amt:'.$recive_total.'
					Desc: '.$tr_description.' 
					Time:'.$tttx.'
					Avail Bal:'.$email_userbalance;
					sendsms($u_phonenumber,$sms_text);
															
					echo "done";
						  
						  
						  
					
					
				}
				else
				{
					echo "norecive";
				}
		
				

        break;
	    }	

        case "request_tobank":
		{
			 $id = $_POST['id'];
			
			 mysqli_query($con,"UPDATE x_cds SET x_maturestatus='Y' WHERE x_id='$id' ");
			
			break;
		}	
         case "request_investbalance":
		{
			 $id = $_POST['id'];
			
			 mysqli_query($con,"UPDATE x_investment SET x_reqbal = 'Y' WHERE x_id='$id' ");
			
			break;
			
		}	
   			
		case "get_balance":
		{
			 $acc_id = $_POST['accid'];
		     $total_balance = user_details($acc_id,"x_totalbalance");
		     $currency_id = user_details($acc_id,"x_currencyid");
			 $currency = currency_data("x_currencysymbol",$currency_id);
			 $transfer_fee =  get_properties("local.transfer.fees");  
			 
			 echo "Account Balance : ".$currency." ".$total_balance;
			
			break;
			 
		}	
		case "get_fee_local":
		{
			 $acc_id = $_POST['accid'];
	
		     $currency_id = user_details($acc_id,"x_currencyid");
			 $currency = currency_data("x_currencysymbol",$currency_id);
			 $transfer_fee =  get_properties("local.transfer.fees");  
			 
		     echo "Amount to Transfer [+ Transfer Fee: ".$currency ."".$transfer_fee." ]";
			 
			break;
			 
		}	
		case "get_fee_international":
		{
			 $acc_id = $_POST['accid'];
	
		     $currency_id = user_details($acc_id,"x_currencyid");
			 $currency = currency_data("x_currencysymbol",$currency_id);
			 $transfer_fee =  get_properties("international.transfer.fees");  
			 
		     echo "Amount to Transfer [+ Transfer Fee: ".$currency ."".$transfer_fee." ]";
			 
			break;
			 
		}
		case "get_currency":
		{
			 $acc_id = $_POST['accid'];
			 $currency_id = user_details($acc_id,"x_currencyid");
			echo $currency = currency_data("x_currencysymbol",$currency_id);
			
			break;
		}
					      //next case here
				  
		case "change_account":
		{
			$acc_id = $_POST['account'];
			if($acc_id == "")
			{
				echo "empty";
			}
			else 
			{
				 $currency_id = user_details($acc_id,'x_currencyid');
				 
				   $_SESSION["account_id"] =  $acc_id;
                   $_SESSION["currency_id"] = $currency_id ;
				
				
			     echo "done";	
			}
			break;
		}
		case "add_merchant":
						 {
							 
							 $x_merchant_name=mysqli_real_escape_string($con,trim($_POST['x_merchant_name']));
							 $x_nick_name=mysqli_real_escape_string($con,trim($_POST['x_nick_name']));
							 $x_account_number=mysqli_real_escape_string($con,trim($_POST['x_account_number']));
							 $x_city=mysqli_real_escape_string($con,trim($_POST['x_city']));
							 $x_state=mysqli_real_escape_string($con,trim($_POST['x_state']));
							 $x_zip=mysqli_real_escape_string($con,trim($_POST['x_zip']));
							 $x_phone_no=mysqli_real_escape_string($con,trim($_POST['x_phone_no']));
							 $x_category=mysqli_real_escape_string($con,trim($_POST['x_category']));
							          if(($x_merchant_name=="")||($x_nick_name=="")||($x_account_number=="")||($x_city=="")||($x_state=="")||($x_zip=="")||($x_phone_no=="") || ($x_category==""))
								  {
									echo "0";  
									  
								  }
								  else
								  {
									mysqli_query($con,"INSERT into x_merchants SET x_merchant_name='$x_merchant_name', x_nick_name='$x_nick_name', x_account_number='$x_account_number', x_city='$x_city', x_state='$x_state', x_zip='$x_zip', x_phone_no='$x_phone_no', x_user_id='$user_id',x_category='$x_category'");

									 $merchant_id = mysqli_insert_id($con);

									mysqli_query($con,"INSERT into x_billpay SET x_merchant_id='$merchant_id',x_amount='',x_delivered_by=''");

									echo "1";  
								  }
							 
							 
						break;	 
						 }			  
					  case "update_bill_pay":
						 {
							 
						 	$name = $_POST['key'];
						 	$amount = $_POST['amount'];
						 	$delivered_by = $_POST['delivered_by'];
						 	
						 	$accountid=$_POST['x_account_id'];
							 foreach( $name as $key => $n ) {
							
						mysqli_query($con,"update x_billpay SET x_amount='$amount[$key]',x_delivered_by='$delivered_by[$key]',x_account_id='$accountid[$key]' where x_merchant_id='$name[$key]'");

									 
								  }
								
							 echo "1"; 
							 
						break;	 
						 }	
						 case "delete_user":
						 {
						 	$merchant_id = $_POST['id'];
						 	mysqli_query($con,"update x_merchants set x_status=0 where x_merchant_id='".$merchant_id."'");
						 	echo 1;
						 	break;
						 }
						 case "set_priority_merchant":
						 {
							 
						 	$name = $_POST['key'];

							 foreach( $name as $key => $n ) {
							$s=$key+1;
						mysqli_query($con,"update x_merchants SET x_priority='$s' where x_merchant_id='$name[$key]'");

									 
								  }
								
							 echo "1"; 
							 
						break;	 
						 }
						 case "pay_now_bill_pay":
						 {
							 
						 	$name = $_POST['key'];
						 	$amount = $_POST['amount'];
						 	$accountid=$_POST['x_account_id'];
						 	$curr_date = date('Y-m-d');
						 	$merchant_name=$_POST['merchant_name'];

						 	$i=0;
							 foreach( $name as $key => $n ) {
							if(is_numeric($amount[$key]) && $amount[$key]!="" && $amount[$key]>0 && !empty($accountid[$key])){
								$q=mysqli_query($con,"select x_totalbalance from x_anotheracc where x_id='$accountid[$key]'");
						 	 $data_array=mysqli_fetch_array($q);
						 	 $totalbalance=$data_array['x_totalbalance'];

						 	 if($totalbalance<$amount[$key]){
													echo 'Dont have enough Balance to make Payment';
												}
												else
												{
													mysqli_query($con,"INSERT into x_billpay_company SET x_merchant_id='$name[$key]',x_amount='$amount[$key]',x_payfrom='$accountid[$key]',x_date='$curr_date'");

													mysqli_query($con,"update x_anotheracc set x_totalbalance=x_totalbalance-$amount[$key] where x_id='$accountid[$key]'");
													mysqli_query($con,"INSERT into x_statement SET x_userid='$user_id',x_acountid='$accountid[$key]',x_amount='$amount[$key]',x_type='debit',x_trdate='$curr_date',	x_description='Payment Through Bill Pay $merchant_name[$key]',x_fee='0',x_status='Y'");
													$i++;
												}
												}
								  }
								if($i>0)
							 echo "1"; 
							else
								echo "5";
							 
						break;	 
						 }
						 case "delete_message":
						 {
							 
						 	$name = $_POST['id'];

							
							mysqli_query($con,"delete from x_notification where x_id='$name'");

									 
								
							 echo "1"; 
							 
						break;	 
						 }
						 case "pay_now_individual_company":
						 {
							 
						 	$name = $_POST['x_name'];
						 	$amount = $_POST['x_amount'];
						 	$accountid=$_POST['x_account_id'];
						 	$curr_date = date('Y-m-d');
						 	$bank_name=$_POST['x_bank'];
						 	$paytoaccountnumber=$_POST['x_account_number'];
						 	$x_service_description=$_POST['x_service_description'];

						 	 if(($name=="")||($amount=="")||($accountid=="")||($bank_name=="")||($paytoaccountnumber=="")||($x_service_description==""))
						 	 {
						 	 	echo "0";
						 	 }
						 	 else
						 	 {
						 	 	if(is_numeric($amount))
						 	 	{
						 	 	if($amount!="" && $amount>0 && !empty($accountid)){
						 	 		$q=mysqli_query($con,"select x_totalbalance from x_anotheracc where x_id='$accountid'");
						 	 $data_array=mysqli_fetch_array($q);
						 	 $totalbalance=$data_array['x_totalbalance'];

						 	 if($totalbalance<$amount){
													echo '2';
												}
												else
												{
													mysqli_query($con,"INSERT into x_bill_pay_history SET x_amount='$amount',x_bank_name='$bank_name',x_service_description='$x_service_description',x_payfromaccount='$accountid',x_paytoaccountno='$paytoaccountnumber',x_date='$curr_date'");


													mysqli_query($con,"update x_anotheracc set x_totalbalance=x_totalbalance-$amount where x_id='$accountid'");
													mysqli_query($con,"INSERT into x_statement SET x_userid='$user_id',x_acountid='$accountid',x_amount='$amount',x_type='debit',x_trdate='$curr_date',	x_description='Payment Through Bill Pay $x_service_description',x_fee='0',x_status='Y'");
													 echo "1"; 
												}
											
						 	 	}
						 	 }
						 	 	 else
						 	 {
						 	 	echo "3";
						 	 }
						 	 }


						 	
						break;	 
						 }				  
				  }
			  
			  
			  
		  }



function get_x_id($con)
{
	
	$test_q = mysqli_query($con, "SELECT * FROM x_fundtransfer WHERE x_id>100000");
$test_row = mysqli_num_rows($test_q);
if ($test_row == 0) {
	return 'x_id=198760,';
}
else
{
	return '';
}
}
?>
