<?php
session_start();
require_once("config/configDB.php"); 
require "financial_year_calculation.php";

function validation_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $tdate_temp             = new DateTime(stripslashes($_POST['invoicedate']));
    $invoicedate 		    = $tdate_temp->format('Y-m-d');

    

    $labouremployeenameID   = $_POST['labouremployeenameID'];
    $shifttypeID            = $_POST['shifttypeID'];
    $extraworkinghrs        = $_POST['extraworkinghrs'];
    $advancesalary        = 0;
    
    $accountyear    = $financialYearStart."-".$financialYearEnd;
    $createdate  	= date("Y-m-d");
    $createdby      = $_SESSION['username'];
    $userID         = $_SESSION['user_id'];
	
	$autoinc=mysqli_query($conn,"ALTER TABLE employee_attendance_hdr CHANGE id id INT(11) NOT NULL AUTO_INCREMENT ");
$autoinc1=mysqli_query($conn,"ALTER TABLE employee_attendance_dtl CHANGE id id INT(11) NOT NULL AUTO_INCREMENT ");
    
   
    $sql_header  = "INSERT INTO employee_attendance_hdr (invoicedate,accountyear,isdelete,createdby,userID,createdate) VALUES ('".$invoicedate."','".$accountyear."', '1','".$createdby."','".$userID."','".$createdate."')  ";
    

    mysqli_query($conn,$sql_header);

    $HDR_ID = mysqli_insert_id($conn);

    if($HDR_ID) {

        foreach ($labouremployeenameID as $key => $value) {

            $result = mysqli_query($conn,"SELECT  lm.shiftworkamt,lm.extrahoursamt  FROM marketing_master as lm WHERE lm.id='".$labouremployeenameID[$key]."' ");

            $results   = mysqli_fetch_array($result,MYSQLI_ASSOC);                    

            $shiftworkamt   = ($results['shiftworkamt']>0) ? $results['shiftworkamt'] : 0;  
            $extrahoursamt   = ($results['extrahoursamt']>0) ? $results['extrahoursamt'] : 0;
            
            $sql_details  = "INSERT INTO employee_attendance_dtl (hdr_id,invoicedate,enameid,shifttype,extraworkinghrs,shiftworkamt,extrahoursamt,advancesalary,accountyear,isdelete,createdby,userID,createdate) VALUES ('".$HDR_ID."', '".$invoicedate."', '".$labouremployeenameID[$key]."', '".$shifttypeID[$key]."', '".$extraworkinghrs[$key]."','".$shiftworkamt."','".$extrahoursamt."','".$advancesalary."','".$accountyear."', '1','".$createdby."','".$userID."','".$createdate."')";
        
            mysqli_query( $conn, $sql_details );        
        }
    }
    header("Location:accEmpDashboard.php");

}





?>