<?php include 'load.php'; 

if(isset($_POST['delete_purchase_order'])){
    $oid = $_GET['oid'];

    $purchase_order_items = mysqli_query($conn, "SELECT * FROM purchase_order_items WHERE purchase_order_id='$oid'");

    if ($purchase_order_items->num_rows > 0) {
        while($row = $purchase_order_items->fetch_assoc()) {
            $qty = $row['qty'];
            $product_id = $row['product_id'];
            $sql3 = "UPDATE tbl_stock SET qty = qty - $qty WHERE prid = '$product_id'";
            if ($conn->query($sql3) === TRUE) {
                $sql = "DELETE FROM purchase_order_items WHERE purchase_order_id='$oid'";
                if ($conn->query($sql) === TRUE) {
                    $sql1 = "DELETE FROM mobile_purchase_order WHERE purchase_order_id ='$oid'";
                    if ($conn->query($sql1) === TRUE) {
                        header("Location: listpurchaseorder.php");
                    }
                }
            }

        }
    }


}


?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
<meta name="description" content="POS - Payroll | Pos & Invetory System | Dashboard
">
<meta name="keywords" content="Payroll | Pos & Invetory System | Dashboard
">
<meta name="author" content="Payroll | Pos & Invetory System | Dashboard
">
<meta name="robots" content="noindex, nofollow">

<title>Delete Purchsase Order | Payroll | Pos & Invetory System | Dashboard
</title>


<?php
require_once 'headercss.php';
?>
</head>
<body>
<div id="global-loader">
<div class="whirly-loader"> </div>
</div>

<div class="main-wrapper">
<?php
require_once 'header.php';
?>

<?php
require_once 'navbar.php';
?>

<div class="page-wrapper">
<div class="content">
<div class="page-header">
<div class="page-title">
<h4>Purchase Order Management</h4>
<h6>Delete Purchase Order</h6>
</div>

					





</div>

<div class="card">
<div class="card-body">

									<?php

									if(isset($errMSG)){

									?>
			
									<div class="alert alert-danger alert-dismissible alert-alt solid fade show">
									<button type="button" class="close h-100" data-dismiss="alert" aria-label="Close"><span><i class="mdi mdi-close"></i></span></button>
									<strong>Error!</strong> <?php echo $errMSG; ?>
									</div>

									<?php

									}
	
									else if(isset($successMSG)){

									?>
			
									<div class="alert alert-success alert-dismissible alert-alt solid fade show">
									<button type="button" class="close h-100" data-dismiss="alert" aria-label="Close"><span><i class="mdi mdi-close"></i></span></button>
									<strong>Success!</strong> <?php echo $successMSG; ?>.
									</div>

									<?php

									}

									?>
<div class="row">
<form action="" method="post">
<h3 class="mb-2">Your are going to delete Purchase Order?</h3>
<div class="col-lg-12">
   
                     <input type="submit" name="delete_purchase_order" class="btn btn-danger" value="Delete Purchse Order">
                     
                     <a class="btn btn-primary" href="listpurchaseorder.php">Back</a>
                </div>    
                </form>









</div>
</div>
</div>


<script src="assets/js/jquery-3.6.0.min.js"></script>

<script src="assets/js/feather.min.js"></script>

<script src="assets/js/jquery.slimscroll.min.js"></script>

<script src="assets/js/jquery.dataTables.min.js"></script>
<script src="assets/js/dataTables.bootstrap4.min.js"></script>

<script src="assets/js/bootstrap.bundle.min.js"></script>

<script src="assets/plugins/select2/js/select2.min.js"></script>

<script src="assets/plugins/sweetalert/sweetalert2.all.min.js"></script>
<script src="assets/plugins/sweetalert/sweetalerts.min.js"></script>

<script src="assets/js/script.js"></script>

<script>
	$(document).ready(function(){
		$('.select2').select2();
		loaditems();

		$(".add").click(function(){
			var product = $('.product').val(); 
			var supplier = $('#inputsupplier').val(); 
			var qty = $('.qty').val();
			var invoice_no = $('#invoice_no').val();
			$.ajax({
           		type: "POST",
				data: {product:product,qty:qty,supplier:supplier,invoice_no:invoice_no},
				url: "add-prodcut-to-list.php",
					success: function(data){
						loaditems();
					}
				});
  		});

		 
		  
	});

	
</script>


</body>
</html>