<?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']);
    $givenby        = stripslashes($_POST['givenby']);
    $receivedby     = stripslashes($_POST['receivedby']);
    $remarks        = stripslashes($_POST['remarks']);
    $explosiveID        = $_POST['explosiveID'];
    $explosiveqty       = $_POST['explosiveqty'];
    
    $accountyear    = $financialYearStart."-".$financialYearEnd;
    $createdate  	= date("Y-m-d");
    $createdby      = $_SESSION['username'];
    $userID         = $_SESSION['user_id'];
	
	$autoinc=mysqli_query($conn,"ALTER TABLE internalpro_explosive_hdr CHANGE id id INT(11) NOT NULL AUTO_INCREMENT ");
	
	$autoinc2=mysqli_query($conn,"ALTER TABLE internalpro_explosive_dtl CHANGE id id INT(11) NOT NULL AUTO_INCREMENT ");

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

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

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

}





?>