<?php 

include('db_con.php');

$sql = "select * from items where stock > 0";

$statement = $connect->prepare($sql);
$statement->execute();

$result = $statement->fetchAll();

foreach ($result as $row ) {

	$item_no = $row['item_id'];

	$statement = $connect->query("select p_id from purchase_detail where item_no = '".$item_no."'");

	$p_id = $statement->fetchColumn();

	$statement = $connect->query("delete from purchase where p_id='".$p_id."'");
	$statement = $connect->query("delete from purchase_detail where p_id='".$p_id."'"); 

	$sql = "select jv_id from jv where source='item' and ref_id ='".$item_no."'";
	$statement = $connect->query($sql);

	$jv_id = $statement->fetchColumn();

	$statement= $connect->query("delete from jv where jv_id='".$jv_id."'");
	$statement= $connect->query("delete from jv_detail where jv_id='".$jv_id."'");
}

header('Location: dashboard');

?>