<?
session_start();
$_SESSION["fin_year"]='2018-19';
$branchcde=$_REQUEST['branchcde'];
$login_date=$_REQUEST['login_date'];
$uname=$_REQUEST['uname'];

$pswd =$_REQUEST['pswd'];
$fin_yr =$_REQUEST['fin_yr'];
include("opendb.php");
 $re=mysqli_query($db,"select * from branch where brcode='$branchcde'")or die('err2'.mysqli_error($db));
 $rr=mysqli_fetch_array($re);
 $br=$rr['brname'];
 $brtyp=$rr['brtype'];
$fin_year1 = substr($fin_yr,2,2).substr($fin_yr,-2);
//echo $fin_year1;
if($fin_year1=='1516')
{
//$database1='khadidb';
$database1='psn_amplcnewdb';

}
else
{
//$database1='khadidb'.$fin_year1;
//$database1='psn_psneicherdb';
$database1='gen4psnauto_psn_ampleicher';

}

$database1='gen4psnauto_psn_ampleicher';

//  $re1=mysqli_query($db,"select invdate from $database1.spareinvoice where brcode='$branchcde' order by invdate desc")or die('err2'.mysqli_error($db));
//  $rr1=mysqli_fetch_array($re1);

// if($login_date<$rr1['invdate'])
// {
//   //print "<script> alert('Login date less than previous login; Entry done on ".$rr1[invdate]."'); history.back(1); </script>";
//   //return;
// }

// ================start branch clearing check ================================


 $rew=mysqli_query($db,"select brled from branch where brcode='$branchcde'");
 $rerow=mysqli_fetch_row($rew);

$led=$rerow[0];

$dt = date('Y-m-d');

$sql="select min(date) from journal2  where partic='$led'  and date>='2018-04-01' and date<='$dt' and brcode!='$brcode' and j_type!='CH' and j_type!='Spare trans out'  and j_type!='GRN' and against!='done' ";
$result1=mysqli_query($db,$sql)or die(mysqli_error($db));
$user_row1=mysqli_fetch_array($result1);

  


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

if($dtrow1[0]>10)

{
 
//print "<script> alert('You cannot login.Branch Clearing pending for more than 7 days.Get approval from Finace Manager for login'); history.back(1); </script>";

// return;

 
print "<script> alert(if(confirm'Bbranch Clearing pending for more than 10 days.Clear immediately else  login will get blocked'));  </script>";




}


// ===========end  branch clearing check ==============================


$result=mysqli_query($db,"select * from $database1.holiday where date='$login_date'");
 
  if(mysqli_num_rows($result)>0)
  {
    print "<script> alert('Holiday!'); history.back(1); </script>";
	return;
  }
  
 mysqli_query($db,"update branch set login_date = '$login_date' where brcode='$branchcde'")or die('err2'.mysqli_error($db));

if($uname=='admin' && $pswd=='tel@321op')
{
   $_SESSION["user"];
   $user=str_replace('.','',$uname);
   $_SESSION['user']=$uname;
   $_SESSION["branch"];
   $branch=$br;
   $_SESSION["brcode"];
   $brcode=$branchcde; //$rr[0];
   $_SESSION["brcode"]=$brcode;
   $_SESSION["fin_year"];
   $_SESSION["fin_year"]=$fin_yr;
   $_SESSION["level"];
   $level=10;
   $_SESSION["level"]=$level;
header('Location:index1.php');

/*

 print "<script> parent.contents.document.location='left.htm'</script> ";
print "<script>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($db));
  $user_row=mysqli_fetch_array($result);
$usrlvl=$user_row[2];

$dt=$user_row['passdate'];
$sq="select to_days(curdate())-to_days('$dt')";
$dtre=mysqli_query($db,$sq)or die(mysqli_error($db));
$dtrow=mysqli_fetch_row($dtre);
$xy=0;
$xy= 90-$dtrow[0];
$yr='2012-2013';
//print $xy;

if($dtrow[0]>90)
{
 print "<script> alert('Password expired'); history.back(1); </script>";
 return;
}
 if(mysqli_num_rows($result)>0)
  {
   $_SESSION["user"];
   $user=str_replace('.','',$uname);
   $_SESSION['user']=$uname;
   $_SESSION["branch"];
   $branch=$br;

   $_SESSION["brcode"];
   $brcode=$branchcde; //$rr[0];
    $_SESSION["brcode"]=$brcode;
      $_SESSION["fin_year"];
   $_SESSION["fin_year"]=$fin_yr;
//echo $fin_yr;
  //echo $brcode;
   $_SESSION["level"];
   $level=$usrlvl;
         $_SESSION["level"]=$level;

  $_SESSION["cccode"];
   $cccode=$brtyp;
  $_SESSION['expir']=$xy;

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"));
//$ip_location= $details->city.", ".$details->region.", ".$details->country;
//$sqlnn="insert into $database1.log_dup values('$branchcde','$uname','$ip','$dt','$deviceType','$ip_location','Ampl Login')";
//mysqli_query($db,$sqlnn);

mysqli_query($db,"update $database1.branch set login_date = '$login_date' where brcode='$branchcde'")or die('err2'.mysqli_error($db));
//    print "Loading .......";

if($level==10)
{
    echo "<meta http-equiv='refresh' content='.1;url=index1.php'>";
}
else
{
    echo "<meta http-equiv='refresh' content='.1;url=index1.php'>";
}
  }
 else
 {
   print "<script> alert('Invalid Login'); history.back(1); </script>";
   return;
 }
}
/*


*/
?>

