<?php
ini_set('display_errors', 0);
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

session_start();
ob_start();

require_once('configuration/server_connect.php');
require_once('configuration/all_function.php');

$status='CHECKED';

$emp_code = $_REQUEST['emp_code'];
$mon = $_REQUEST['mon'];
$year = $_REQUEST['year'];
$td = $_REQUEST['td'];
$hd = $_REQUEST['hd'];
$wd = $_REQUEST['wd'];
$lv = $_REQUEST['lv'];
$ab = $_REQUEST['ab'];
$att = $_REQUEST['att'];
$lt = $_REQUEST['lt'];
$ot = $_REQUEST['ot'];
$advance_deduction = $_REQUEST['advance'];
$deduction = $_REQUEST['deduction'];
$benefits = $_REQUEST['benefits'];
$flag = $_REQUEST['flag'];

$dateString = ''.$year.'-'.$mon.'-01';
$tr_date = date("Y-m-t", strtotime($dateString));

$year_sm = date('y',strtotime($tr_date));
$mon_no = sprintf("%02d", $mon);
$tr_no = $year_sm.''.$mon_no.''.$emp_code;

$emp_sql="SELECT * FROM  employee_info  WHERE emp_code='".$emp_code."'";
$emp_data = find_all_field_sql($conn,$emp_sql);

$day_salary=$emp_data->gross_salary/$wd;
$hourly_salary=$day_salary/8;
$ot_wages=$ot*$day_salary;
$total_earnings=($emp_data->gross_salary+$benefits+$ot_wages);

$tds_amt=$emp_data->tds_amt;
$ab_deduction=$day_salary*$ab;
$total_deduction=($deduction+$advance_deduction+$ab_deduction+$tds_amt);
$net_salary=($total_earnings-$total_deduction);

$entry_by=$_SESSION['user']['id'];
$entry_at=date('Y-m-d H:i:s');

if($flag>0){

$sql_del = 'delete from employee_monthly_salary where emp_code="'.$emp_code.'" and mon="'.$mon.'" and year="'.$year.'" ';
mysqli_query($conn,$sql_del);

}

$ins_sql = 'INSERT INTO  employee_monthly_salary (emp_code, department, designation, tr_no, tr_date, mon, year, td, hd, wd, lv, ab, att, lt, ot, day_salary, hourly_salary, gross_salary, basic_salary, house_rent, conveyance, medical, others, benefits, ot_wages, total_earnings, deduction, advance_deduction, ab_deduction, total_deduction, net_salary, status, entry_at, entry_by, tds_amt) VALUES
("'.$emp_code.'", "'.$emp_data->department.'", "'.$emp_data->designation.'", "'.$tr_no.'", "'.$tr_date.'", "'.$mon.'", "'.$year.'", "'.$td.'", "'.$hd.'", "'.$wd.'", "'.$lv.'", "'.$ab.'", "'.$att.'", "'.$lt.'", "'.$ot.'", "'.$day_salary.'", "'.$hourly_salary.'", "'.$emp_data->gross_salary.'", "'.$emp_data->basic_salary.'", "'.$emp_data->house_rent.'", "'.$emp_data->conveyance.'", "'.$emp_data->medical.'", "'.$emp_data->others.'", "'.$benefits.'", "'.$ot_wages.'", "'.$total_earnings.'", "'.$deduction.'", "'.$advance_deduction.'", "'.$ab_deduction.'", "'.$total_deduction.'", "'.$net_salary.'", "'.$status.'", "'.$entry_at.'", "'.$entry_by.'", "'.$tds_amt.'")';
	
mysqli_query($conn,$ins_sql);


echo 'Success!';
?>