<?php
    require("../connection.php");
    include("../user.php");
    // ini_set('display_errors', '1');
    // ini_set('display_startup_errors', '1');
    // error_reporting(E_ALL);
?>
<link rel="stylesheet" href="../css/sweet-alert.css" /> 
<?php
    $cur_date=date('Y-m-d H:i:s');
    $sys_date=date('Y-m-d');
    $user=$_SESSION['user'];
    $submit = 0;
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['material_name'])) {
        // Safe to access $_POST data now
        $po_date = $_POST['po_date']; // This will not throw a warning
    	$vendor_name=$_POST['vendor_name'];
    	$billno=$_POST['billno'];
    	$gatentry=$_POST['gatentry'];
		
		$branch=$_POST['branch'] ?? null;
		$store=$_POST['store'] ?? null;
		$total_amount=$_POST['final_amount'] == '' ? 0 : $_POST['final_amount'];
	    $gst_percentage=$_POST['gst_percentage'] == '' ? 0 : $_POST['gst_percentage'];
		$discount=$_POST['discount'] == '' ? 0 : $_POST['discount'];
		foreach($_POST['material_name'] as $key => $value){
		    $material_name = $value;
		    $department=$_POST['department'][$key];
		    $qty = $_POST['qty'][$key];
		    $qum = $_POST['qum'][$key];
		    $value = $_POST['value'][$key];
		    $amount = $_POST['amount'][$key];
            $inward_table_query = $con->query("INSERT INTO item_master(po_date, department, category, branch, division, material_name, vendor_name, billno, gatentry, quantity, value, amount, gst_percentage, discount, total_amount, qum, status, created_on, created_by) VALUES ('$po_date', '$department', '$category', '$branch', '$store', '$material_name', '$vendor_name', '$billno', '$gatentry', '$qty', '$value', '$amount', '$gst_percentage', '$discount', '$total_amount', '$qum', 0, NOW(), '$user')");
            $submit = 0;
            if($inward_table_query){
                $submit = 1;
            }
        }
	}
    if($submit)
	{
	?>
	<script>
    	setTimeout(function() {
            swal({
                title: "Successfully",
                text: "completed!",
                type: "success"
            }, function() {
          window.location = "inward_master.php";
            });
        }, 100);
    </script>  
    <?php
	}	
	else
	{
	?>
	<script>
        setTimeout(function() {
            swal({
                    title: "Not",
                    text: "completed!",
                    type: "error"
                }, function() {
              window.location = "inward_master.php";
            });
        }, 100);
    </script> 	  
	<?Php
	}	
?>
<script src="../js/sweet-alert.js"></script>