<?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") {
    
    $edit_id            = stripslashes($_POST['edit_id']);
    $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'];

    
    $sql_update = "UPDATE internalpro_spare_hdr SET ";
    $sql_update .= "invoicedate='$invoicedate',";
    $sql_update .= "vehicleno='$vehicleno',";
	$sql_update .= "kms='$kms',";
	$sql_update .= "hrs='$hrs',";
    $sql_update .= "givenby='$givenby',";
    $sql_update .= "receivedby='$receivedby',";
    $sql_update .= "remarks='$remarks',";
    $sql_update .= "lastupdated='$createdate'";
    $sql_update .= "WHERE id='$edit_id' ";

    mysqli_query($conn,$sql_update);


    $sql_DELETE = "DELETE FROM internalpro_spare_dtl WHERE hdr_id = '".$edit_id."' ";
    mysqli_query( $conn, $sql_DELETE); 

    $HDR_ID = $edit_id;

    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");

}
?>
