<?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']);
    $givenby        = stripslashes($_POST['givenby']);
    $receivedby     = stripslashes($_POST['receivedby']);
    $remarks        = stripslashes($_POST['remarks']);
    $explosiveID        = $_POST['explosiveID'];
    $explosiveqty       = $_POST['explosiveqty'];
    
    $createdate  	= date("Y-m-d");

    
    $sql_update = "UPDATE internalpro_explosive_hdr SET ";
    $sql_update .= "invoicedate='$invoicedate',";
    $sql_update .= "vehicleno='$vehicleno',";
    $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_explosive_dtl WHERE hdr_id = '".$edit_id."' ";
    mysqli_query( $conn, $sql_DELETE); 

    $HDR_ID = $edit_id;

    if($HDR_ID) {

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

            $sql_details  = "INSERT INTO internalpro_explosive_dtl (hdr_id, explosiveID, explosiveqty, accountyear,isdelete,createdby,userID,createdate) VALUES ('".$HDR_ID."', '".$explosiveID[$key]."', '".$explosiveqty[$key]."', '".$accountyear."', '1','".$createdby."','".$userID."','".$createdate."')";
        
            mysqli_query( $conn, $sql_details );        
            
        }
    }
    header("Location:inExplosiveProDashboard.php");

}
?>
