<?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") {
    
    $invoice            = stripslashes($_POST['invoice']);
    $invoicedate_temp   = new DateTime(stripslashes($_POST['invoicedate']));
    $invoicedate 		= $invoicedate_temp->format('Y-m-d');

    $vehicleno      = stripslashes($_POST['vehicleno']);
	 $kms      = stripslashes($_POST['kms']);
	 $hrs      = stripslashes($_POST['hrs']);
    $givenby        = stripslashes($_POST['givenby']);
    $receivedby     = stripslashes($_POST['receivedby']);
    $remarks        = stripslashes($_POST['remarks']);
    $spareID        = $_POST['spareID'];
    $spareqty       = $_POST['spareqty'];
    
    $accountyear    = $financialYearStart."-".$financialYearEnd;
    $createdate  	= date("Y-m-d");
    $createdby      = $_SESSION['username'];
    $userID         = $_SESSION['user_id'];
	
	$autoinc=mysqli_query($conn,"ALTER TABLE internalpro_spare_hdr CHANGE id id INT(11) NOT NULL AUTO_INCREMENT ");
	
	$autoinc2=mysqli_query($conn,"ALTER TABLE internalpro_spare_dtl CHANGE id id INT(11) NOT NULL AUTO_INCREMENT ");

    $sql_header  = "INSERT INTO internalpro_spare_hdr ( invoice, invoicedate, vehicleno,kms,hrs, givenby, receivedby,remarks,accountyear,isdelete,createdby,userID,createdate) VALUES ('".$invoice."', '".$invoicedate."', '".$vehicleno."','".$kms."','".$hrs."', '".$givenby."', '".$receivedby."',  '".$remarks."','".$accountyear."', '1','".$createdby."','".$userID."','".$createdate."')";

    mysqli_query($conn,$sql_header);
    $HDR_ID = mysqli_insert_id($conn);

    if($HDR_ID) {

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

            $sql_details  = "INSERT INTO internalpro_spare_dtl (hdr_id, spareID, spareqty, accountyear,isdelete,createdby,userID,createdate) VALUES ('".$HDR_ID."', '".$spareID[$key]."', '".$spareqty[$key]."', '".$accountyear."', '1','".$createdby."','".$userID."','".$createdate."')";
        
            mysqli_query( $conn, $sql_details );        
            
        }
    }
    header("Location:inSpareProDashboard.php");

}





?>