<?php
session_start();
require_once '../system/conn.php';
$date=explode('-',date("Y-m-d", strtotime("last day of previous month")));
$month=$date[1];
$year=$date[0];
$select_sa_at=$conn->query("SELECT * FROM `admin_salary_attendance` WHERE `month`='$month' AND `year`='$year' AND `b_a`>0 ");
while($row=$select_sa_at->fetch_assoc()){
$select_emp_de=$conn->query("SELECT * FROM `emp_details` WHERE `e_id`='".$row['emp_id']."' AND `branch`='".$row['branch']."'");
$row_de=$select_emp_de->fetch_assoc();

$calculation=$conn->query("SELECT * FROM `calculation`");
$cal_sa=$calculation->fetch_assoc();

$advance=$row['maual_advance'];
$advance=round($advance);
$salary=$row_de['salary']/26;
$salary=round($salary);
$attendace=$row['b_a'];
$salary=$salary*$attendace;//Gross pay
$salary=round($salary);

//$basic_sa=($salary*$cal_sa['basic'])/100;
//$basic_sa=round($basic_sa);
//$hra_sa=($salary*$cal_sa['hra'])/100;
//$hra_sa=round($hra_sa);

$branch_code=$row['branch'];
if($branch_code == 4 || $branch_code == 14) {
  $basic_sa=$salary;
  $hra_sa=0;
} elseif($branch_code == 21) {
  $basic_sa=$salary;
  $hra_sa= $basic_sa*8.33/100;
} else {
  $basic_sa=($salary*$cal_sa['basic'])/100;
  $basic_sa=round($basic_sa);
  $hra_sa=($salary*$cal_sa['hra'])/100;
  $hra_sa=round($hra_sa);
}

if($row_de['pf']=='y'){
$pf_sa=($basic_sa*$cal_sa['pf'])/100;
$pf_sa=round($pf_sa);
}else{
  $pf_sa=0;  
}
if($row_de['esi']=='y'){
$esic=($salary*$cal_sa['esic'])/100;
$esic=ceil($esic);
}else{
  $esic=0;
}
$name=$row_de['e_name'];
$designation=$row_de['designation'];
$emp_ac_no=$row_de['bank_ac_no'];
$total_a=$row['t_a'];
$convyance=0;
$spc_allow=$row['spc_allow'];
$medical=0;
if($salary>=14000 && $salary<30000){
    $tax=125;
}
if($salary>=30000){
    $tax=200;
}
if($salary<14000){
    $tax=0;
}
$deduction=$advance+$tax+$pf_sa+$esic;
$gain=$basic_sa+$hra_sa+$convyance+$medical+$spc_allow;
$net_pay=$gain-$deduction;
$net_pay=round($net_pay);
$cheque_val=$row['cheque'];

if($emp_ac_no=='' || $emp_ac_no=='NA' || $row['cheque']=='yes')
{
	$cheque_val= "yes";
}

else
{
	$cheque_val="";
}
//$insert=$conn->query("INSERT INTO `emp_salary`(`name`,`designation`,`ac_no`, `emp_id`, `month`, `year`, `attendance`, `t_at`, `basic_pay`, `hra`, `convyance`, `medical`, `gross_pay`, `pf`, `esic`, `advance`, `tax`, `deduction`, `net_pay`, `block`, `cheque`, `branch`) VALUES ('$name','$designation','$emp_ac_no','".$row['emp_id']."','$month','$year','$attendace','$total_a','$basic_sa','$hra_sa','$convyance','$medical','$salary','$pf_sa','$esic','$advance','$tax','$deduction','$net_pay','".$row['block']."','$cheque_val','".$_SESSION['branch']."')");
$insert=$conn->query("INSERT INTO `admin_emp_salary`(`name`,`designation`,`ac_no`, `emp_id`, `month`, `year`, `attendance`, `t_at`, `basic_pay`, `hra`, `convyance`, `medical`, `gross_pay`, `pf`, `esic`, `advance`, `tax`, `spc_allow`, `deduction`, `net_pay`, `block`, `cheque`, `branch`) VALUES ('$name','$designation','$emp_ac_no','".$row['emp_id']."','$month','$year','$attendace','$total_a','$basic_sa','$hra_sa','$convyance','$medical','$gain','$pf_sa','$esic','$advance','$tax','$spc_allow','$deduction','$net_pay','".$row['block']."','$cheque_val','".$row['branch']."')");  
    
    
}
    
    
    
?>