<?
 session_start();
 include("opendb.php");
 $branchcde=$_REQUEST['branchcde'];
 $uname=$_REQUEST['uname'];
 $login_date=$_REQUEST['login_date'];
 $pswd=$_REQUEST['pswd'];
 $fin_yr =$_REQUEST['fin_yr'];
$fin_year1 = substr($fin_yr,2,2).substr($fin_yr,-2);
//$database1='gen4psnauto_psn_forcedb2425';
 $database1='forcedb2526';
 $re=mysqli_query($db,"select * from $database1.branch where brcode='$branchcde'")or die('err2'.mysqli_error());
 $rr=mysqli_fetch_array($re);
 $br=$rr['brname'];
 $brtyp=$rr['brtype'];
 $reg=$rr['region'];
 $brcd=$rr['brcode'];


date_default_timezone_set("Asia/Calcutta");
if($uname=='admin' && $pswd=='tel@321op')
{
 session_register('user');
 $user=$uname;
 session_register('brcode');
 $brcode=$branchcde;
 session_register("branch");
 $branch=$br;
 session_register("level");
 $level=10;
 session_register("fin_year");
 $fin_year='2023-24';

 print "<script> parent.contents.document.location='left.htm'; ";
 print "parent.banner.document.location='top.php'</script>";
}
else
{
 $sql="select * from $database1.bruser where username='$uname' and pswd='$pswd' and brcode='$branchcde' and active='Y'";

 $result=mysqli_query($db,$sql)or die('err1:'.mysqli_error());
  $user_row=mysqli_fetch_array($result);
$last_login = $user_row['ph'];
    $current_time= date('Y-m-d h:i:s');
    $start_date = new DateTime($last_login);
    $since_start = $start_date->diff(new DateTime($current_time));
    $mtot = $since_start->days+$since_start->d+$since_start->h+$since_start->i;
    if($mtot<25 && strlen($last_login) > 0)
    {
       // print "<script> alert('Session already active for user :".$uname.". Muliple login not allowed....'); parent.document.location='index.php'; </script>";
        //return;
    }
$dt=$user_row['passdate'];
$sq="select to_days(curdate())-to_days('$dt')";
$dtre=mysqli_query($db,$sq)or die(mysqli_error());
$dtrow=mysqli_fetch_row($dtre);
$xy=0;
$xy= 90-$dtrow[0];
$yr='2025-2026';

if($dtrow[0]>90)
{
 print "<script> alert('Password expired'); history.back(1); </script>";
 return;
}


  $sql1="select date from $database1.jcardparts where  brcode='$branchcde' order by date asc limit 1";
  $result1=mysqli_query($db,$sql1)or die('err1:'.mysqli_error());
  $user_row1=mysqli_fetch_array($result1);

$dt1=$user_row1['date'];
 $sq1="select to_days(curdate())+1-to_days('$dt1')";
$dtre1=mysqli_query($db,$sq1)or die(mysqli_error());
$dtrow1=mysqli_fetch_row($dtre1);

 
 if(mysqli_num_rows($result)>0)
  {
	

   $_SESSION["user"];
	 $_SESSION['user']=str_replace('.','',$uname);
   
   $_SESSION["branch"];
	 $_SESSION["branch"]=$brcd;
   
   $_SESSION["brcode"];
	 $_SESSION["brcode"]=$rr[0];
   
   $_SESSION["level"];
	 $_SESSION["level"]=$user_row['level'];
   
   $_SESSION["fin_year"];
	 $_SESSION["fin_year"]=$fin_yr;

   $_SESSION["expir"]=$xy;
	 $_SESSION["yr"]=$yr;
	 
    $_SESSION["user_session"];
    $_SESSION["user_session"]=str_replace('.','',$uname).date('Y-m-d H:i:s');
	 
	 include('Mobile_Detect.php');
$detect = new Mobile_Detect;
$deviceType = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer');
date_default_timezone_set('Asia/Kolkata');
$dt=date("Y-m-d H:i:s");
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
    $ip = $_SERVER['HTTP_CLIENT_IP'];
    }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
    $ip = $_SERVER['REMOTE_ADDR'];
}
//$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
print "aaaaaaaaaaaaaaa";
//$ip_location= $details->city.", ".$details->region.", ".$details->country;
$sqlnn="insert into $database1.log_dup values('$branchcde','$uname','$ip','$dt','$deviceType','$ip_location','Force1920 Login')";
mysqli_query($db,$sqlnn);

	 
echo "update branch set logindt='$login_date' where brcode='$brcd'";
	 mysqli_query($db,"update branch set logindt='$login_date' where brcode='$brcd'");

   print "Loading .......";
echo "<meta http-equiv='refresh' content='.1;url=index2.php'>";
  }
 else
 {
   print "<script> alert('Invalid Login11'); history.back(1); </script>";
   return;
 } 
}
?>

 
