<?
include_once('includes/top.php');
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$code=$_REQUEST['code'];
$code=trim($code);
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
 return;
}
$checktable = mysqli_query($db,"SHOW TABLES LIKE 'stockverification'");
if (mysqli_num_rows($checktable) > 0)
{
  $query="select NOT EXISTS (select item_code from prod_invoice where item_code = '$code') AND NOT EXISTS (select item_code from salesretn where item_code = '$code') AND
NOT EXISTS (select item_code from brtransferin where item_code = '$code') AND NOT EXISTS (select item_code from brtransferout where item_code = '$code') AND
NOT EXISTS (select itemcode from qtytransf where itemcode = '$code') AND NOT EXISTS (select itemcode from stockverification where itemcode = '$code')";
}
else
{
$query="select NOT EXISTS (select item_code from prod_invoice where item_code = '$code') AND NOT EXISTS (select item_code from salesretn where item_code = '$code') AND
NOT EXISTS (select item_code from brtransferin where item_code = '$code') AND NOT EXISTS (select item_code from brtransferout where item_code = '$code') AND
NOT EXISTS (select itemcode from qtytransf where itemcode = '$code')";
}


$rs=mysqli_query($db,$query);
$row=mysqli_fetch_array($rs);
if($row[0]==0)
    {
        print "<script> alert('Cannot delete item as invoice exists');document.location='partmastrepo.php';</script>";
    }
else
    {
        $sql1="delete from partmaster where partcode='$code'";
        $sql2="delete from spareprmast where partcode='$code'";
        $sql3="delete from sparestock where itemcode='$code'";
        
        mysqli_query($db,$sql1) or die(mysqli_error($db));
        mysqli_query($db,$sql2) or die(mysqli_error($db));
        mysqli_query($db,$sql3) or die(mysqli_error($db));
        print "<font color=green face='verdana'>Item ($code) has been deleted </font>";
//log_entry($brcode,$user,$REMOTE_ADDR,"ledger delete: $led");
        include("partmastrepo.php");
        }
       ?>
