<?
session_start();
$brcode = $_SESSION['brcode'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}
$dt=date('Y-m-d');

include("../opendb.php");
include("../test.php");
?>
<script>
var pc=[];
var pn=[];

function handleKeyUp(maxNumToShow)
{
    var selectObj, textObj, functionListLength;
    var i, searchPattern, numShown;

    // Set references to the form elements
    selectObj = document.forms[0].functionselect;
    textObj = document.forms[0].pcode;

    // Remember the function list length for loop speedup
    functionListLength = pc.length;

    // Set the search pattern depending
/*    if(document.forms[0].functionradio[0].checked == true)
    {
        searchPattern = "^"+textObj.value;
    }
    else
    {
        searchPattern = textObj.value;
    }
*/
        searchPattern = "^"+textObj.value;

    // Create a regulare expression
    re = new RegExp(searchPattern,"gi");
    // Clear the options list
    selectObj.length = 0;

    // Loop through the array and re-add matching options
    numShown = 0;
    for(i = 0; i < functionListLength; i++)
    {
        if(pc[i].search(re) != -1)
        {
            selectObj[numShown] = new Option(pc[i],pc[i]);
            numShown++;
        }
        // Stop when the number to show is reached
        if(numShown == maxNumToShow)
        {
            break;
        }
    }
    // When options list whittled to one, select that entry
    if(selectObj.length == 1)
    {
        selectObj.options[0].selected = true;
    }
}

// this function gets the selected value and loads the appropriate
// php reference page in the display frame
// it can be modified to perform whatever action is needed, or nothing
function handleSelectClick()
{
//    document.f1.pname.value=pn[document.f1.functionselect.selectedIndex];
    document.f1.pcode.value=document.f1.functionselect.value;

}

</script>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
th{background-color:steelblue;color:white}
</style>

<center>
<form name='f1' action='<?print $PHP_SELF?>' onSubmit="handleSelectClick();return true;">
<table>
<th colspan=4>TRACE AN ITEM</th>
<tr><td>Part Code </td><td><input type="text" name="pcode" VALUE="<?print $pcode?>" style="font-size:10pt;width:34ex;"><br>

</td></tr>
<!-- <tr><td>Part Name </td>
    <td><input type=text name='pname' size=32></td></tr> -->
<th colspan=3><input type=submit value='Search' name='sea'>
</td></tr>
</table>
</form>
<?
$sea = $_REQUEST['sea'];
$pcode = $_REQUEST['pcode'];
if($sea)
{
 $sql="select * from spareopstockitem where itemcode='$pcode' and brcode='$brcode'";
 $re1=mysqli_query($db,$sql);
 print "<table border=1 width=70%><th>Type <th>Qty";
 while($row=mysqli_fetch_array($re1))
 {
   print "<tr><td>Opening Stock </td><td align=right>$row[qty]</td></tr>";
   $mcl = $row[qty];
 }
 print "</table><br>";
 //*********

 $sql="select * from item_purchase_veh where item_code='$pcode' and brcode='$brcode'";
 $re2=mysqli_query($db,$sql)or die(mysqli_error($db));
 if(mysqli_num_rows($re2)>0)
 { print "<table border=1 width=70%><th colspan=4>PURCHASE<tr><th>PBill No <th> Date <th>Qty";
  while($row=mysqli_fetch_array($re2))
  {
    print "<tr><td>$row[purinvoice] </td><td>".datetohtml($row[pdate])."</td><td align=right>$row[qty]</td></tr>";
   $t+=$row[qty];
  }
  print "<tr><td colspan=2 align=right><b>Total</td><td align=right><b>".putComa($t)."</td></tr>";
 print "</table><br>";
 $mcl = $mcl + $t;
 }


 //*********
 
$t=0;
 $sql="select * from item_purchase_ret where partcode='$pcode' and brcode='$brcode'";
 $re2=mysqli_query($db,$sql)or die(mysqli_error($db));
 if(mysqli_num_rows($re2)>0)
 { print "<table border=1 width=70%><th colspan=4>PURCHASE RETURN<tr><th>PBill No <th> Date <th>Qty";
  while($row=mysqli_fetch_array($re2))
  {
    print "<tr><td>bill no : $row[pbillno] retno: $row[retno] </td><td>".datetohtml($row[entrydate])."</td><td align=right>$row[retqty]</td></tr>";
   $t+=$row[retqty];
  }
  print "<tr><td colspan=2 align=right><b>Total</td><td align=right><b>".putComa($t)."</td></tr>";
 print "</table><br>";
 }
 $mcl = $mcl - $t;
 //*********


 //*********
/*
 $sql="select * from spareinvoice where partcode='$pcode' and brcode='$brcode'";
 $re3=mysqli_query($db,$sql)or die(mysqli_error($db));
 if(mysqli_num_rows($re3)>0)
 { print "<table border=1 width=70%><th colspan=4>PURCHASE<tr><th>Inv No <th>P Date <th>Customer <th>Qty";
  while($row=mysqli_fetch_array($re3))
  {
    print "<tr><td align=center>$row[invno] </td><td>".datetohtml($row[entrydate])."</td>";
    print "<td>$row[customer]</td><td align=right>$row[qty]</td></tr>";
  }
 print "</table><br>";
 }
*/
 //*********
/*$t=0;
 $sql="select * from sparetransout where partcode='$pcode' and brcode='$brcode'";
 $re4=mysqli_query($db,$sql)or die(mysqli_error($db));
 if(mysqli_num_rows($re4)>0)
 { print "<table border=1 width=70%><th colspan=4>TRANSFER OUT<tr><th>Tr No <th>Date <th>Customer <th>Qty";
  while($row=mysqli_fetch_array($re4))
  {
    $brname=getBranch($row[to_branch]);
    print "<tr><td align=center>$row[tr_no] </td><td>".datetohtml($row[entrydate])."</td>";
    print "<td>$brname</td><td align=right>$row[qty]</td></tr>";
   $t+=$row[qty];
  }
 print "<tr><td colspan=3 align=right><b>Total</td><td align=right><b>".putComa($t)."</td></tr>";
 print "</table><br>";
 }
 $mcl = $mcl - $t;*/
$t=0;
 //*********
 /*
 $sql="select * from sparetransin where partcode='$pcode' and brcode='$brcode'";
 $re42=mysqli_query($db,$sql)or die(mysqli_error($db));
 if(mysqli_num_rows($re42)>0)
 { print "<table border=1 width=70%><th colspan=4>TRANSFER IN<tr><th>Tr No <th>Date <th>From <th>Qty";
  while($row=mysqli_fetch_array($re42))
  {
    $brname=getBranch($row[from_branch]);
    print "<tr><td align=center>$row[tr_no] </td><td>".datetohtml($row[entrydate])."</td>";
    print "<td>$brname</td><td align=right>$row[qty]</td></tr>";
   $t+=$row[qty];
  }
 print "<tr><td colspan=3 align=right><b>Total</td><td align=right><b>".putComa($t)."</td></tr>";

 print "</table><br>";
 }
 $mcl = $mcl + $t;
 */
 // other item veh pur
 
/* $t=0;
 //*********
 $sql="select * from item_purchase_veh where item_code='$pcode' and brcode='$brcode'";
 $re42=mysqli_query($db,$sql)or die(mysqli_error($db));
 if(mysqli_num_rows($re42)>0)
 { print "<table border=1 width=70%><th colspan=4>Other Item Veh Purchase<tr><th>Bill No <th>Date <th>From <th>Qty";
  while($row=mysqli_fetch_array($re42))
  {
    $brnames=getsupname($row[sup]);
    print "<tr><td align=center>$row[purinvoice] </td><td>".datetohtml($row[curdate])."</td>";
    print "<td>$brnames</td><td align=right>$row[qty]</td></tr>";
   $t+=$row[qty];
  }
 print "<tr><td colspan=3 align=right><b>Total</td><td align=right><b>".putComa($t)."</td></tr>";

 print "</table><br>";
 }
 $mcl = $mcl + $t;
 */
 //other item veh pur end
 
 //other item veh inv
 
 /* 
$t=0;
 $sql="select * from spareinvoice_oth_veh where partcode='$pcode' and brcode='$brcode' and qty>0";
 $re5=mysqli_query($db,$sql)or die(mysqli_error($db));
 if(mysqli_num_rows($re5)>0)
 { print "<table border=1 width=70%><th colspan=5>INVOICE<tr><th>Inv No <th>Date <th>Customer <th>Qty <th>CHASIS";
  while($row=mysqli_fetch_array($re5))
  {
    print "<tr><td align=center>$row[invno] </td><td>".datetohtml($row[entrydate])."</td>";
    print "<td>$row[customer]</td><td align=right>$row[qty]</td><td>$row[saleordno]</td></tr>";
    $t+=$row[qty];
  }
 print "<tr><td colspan=3 align=right><b>Total</td><td align=right><b>".putComa($t)."</td></tr>";
 print "</table><br>";
 }
 $mcl = $mcl - $t;*/
 //other item veh inv end
 
 $t=0;
 //*********
 $sql="select * from sparetransinexcess where partcode='$pcode' and brcode='$brcode'";
 $re42=mysqli_query($db,$sql)or die(mysqli_error($db));
 if(mysqli_num_rows($re42)>0)
 { print "<table border=1 width=70%><th colspan=4>TRANSFER IN Excess<tr><th>Tr No <th>Date <th>From <th>Qty";
  while($row=mysqli_fetch_array($re42))
  {
    $brname=getBranch($row[from_branch]);
    print "<tr><td align=center>$row[tr_no] </td><td>".datetohtml($row[entrydate])."</td>";
    print "<td>$brname</td><td align=right>$row[qty]</td></tr>";
   $t+=$row[qty];
  }
 print "<tr><td colspan=3 align=right><b>Total</td><td align=right><b>".putComa($t)."</td></tr>";

 print "</table><br>";
 }
 $mcl = $mcl + $t;
 
 
 $t=0;
 //********* excess out
 $sql="select * from sparetransoutexcess where partcode='$pcode' and brcode='$brcode'";
 $re42=mysqli_query($db,$sql)or die(mysqli_error($db));
 if(mysqli_num_rows($re42)>0)
 { print "<table border=1 width=70%><th colspan=4>TRANSFER Out Excess<tr><th>Tr No <th>Date <th>From <th>Qty";
  while($row=mysqli_fetch_array($re42))
  {
    $brname=getBranch($row[from_branch]);
    print "<tr><td align=center>$row[tr_no] </td><td>".datetohtml($row[entrydate])."</td>";
    print "<td>$brname</td><td align=right>$row[qty]</td></tr>";
   $t+=$row[qty];
  }
 print "<tr><td colspan=3 align=right><b>Total</td><td align=right><b>".putComa($t)."</td></tr>";

 print "</table><br>";
 }
 $mcl = $mcl - $t;
 
 /*
$t=0;
 $sql="select * from spareinvoice where partcode='$pcode' and brcode='$brcode' and qty>0";
 $re5=mysqli_query($db,$sql)or die(mysqli_error($db));
 if(mysqli_num_rows($re5)>0)
 { print "<table border=1 width=70%><th colspan=5>INVOICE<tr><th>Inv No <th>Date <th>Customer <th>Qty <th>MSNO";
  while($row=mysqli_fetch_array($re5))
  {
    print "<tr><td align=center>$row[invno] </td><td>".datetohtml($row[entrydate])."</td>";
    print "<td>$row[customer]</td><td align=right>$row[qty]</td><td>$row[msno]</td></tr>";
    $t+=$row[qty];
  }
 print "<tr><td colspan=3 align=right><b>Total</td><td align=right><b>".putComa($t)."</td></tr>";
 print "</table><br>";
 }*/
 //$mcl = $mcl - $t;
 // sales ret
 /*
 $sql="select * from salesreturn where partcode='$pcode' and brcode='$brcode'";
 $re511=mysqli_query($db,$sql)or die(mysqli_error($db));
 if(mysqli_num_rows($re511)>0)
 { print "<table border=1 width=70%><th colspan=5>Sales return<tr><th>Inv No <th>Date <th>Customer <th>Qty <th>MSNO";
  while($row=mysqli_fetch_array($re511))
  {
    print "<tr><td align=center>$row[invno] </td><td>".datetohtml($row[entrydate])."</td>";
    print "<td>$row[customer]</td><td align=right>$row[qty]</td><td>$row[msno]</td></tr>";
    $mcl = $mcl + $row[qty];
  }
 print "</table><br>";
 }
*/
//spare inv others


$t=0;
 $sql="select * from spareinvoice_oth where partcode='$pcode' and brcode='$brcode' and qty>0";
 $re5=mysqli_query($db,$sql)or die(mysqli_error($db));
 if(mysqli_num_rows($re5)>0)
 { print "<table border=1 width=70%><th colspan=5>INVOICE<tr><th>Inv No <th>Date <th>Customer <th>Qty <th>MSNO";
  while($row=mysqli_fetch_array($re5))
  {
    print "<tr><td align=center>$row[invno] </td><td>".datetohtml($row[entrydate])."</td>";
    print "<td>$row[customer]</td><td align=right>$row[qty]</td><td>$row[msno]</td></tr>";
    $t+=$row[qty];
  }
 print "<tr><td colspan=3 align=right><b>Total</td><td align=right><b>".putComa($t)."</td></tr>";
 print "</table><br>";
 }
 $mcl = $mcl - $t;
 
 //spare inv other end
/*$t=0;
//*********************** jcard
 $sql="select * from jcardparts where partcode='$pcode' and brcode='$brcode'";
 $re511=mysqli_query($db,$sql)or die(mysqli_error($db));
 if(mysqli_num_rows($re511)>0)
 { print "<table border=1 width=70%><th colspan=5>JOB CARD<tr><th>Inv No <TH>JV No<TH>JOB No<th>Date <th>Qty";
  while($row=mysqli_fetch_array($re511))
  {
    print "<tr><td align=center>$row[invno]</td><td align=center>$row[jvno]</td><td align=center>$row[jcno]</td>";
    print "<td align=center>".datetohtml($row[date])."</td>";
    print "<td align=right>$row[qty]</td></tr>";
    $t+=$row[qty];
  }
 print "<tr><td colspan=3 align=right><b>Total</td><td align=right><b>".putComa($t)."</td></tr>";
 print "</table><br>";
 }
 $mcl = $mcl - $t;*/
//*********************** Warranty jcard

//*********************** adjstock
 $sql="select * from adjstock where partcode='$pcode' and brcode='$brcode'";
 $re512=mysqli_query($db,$sql)or die(mysqli_error($db));
 if(mysqli_num_rows($re512)>0)
 { print "<table border=1 width=70%><th colspan=5>Stock Adjustment<tr><th>Ref No";
   print "<th>Date <th>In Qty <th>Out Qty";
  while($row=mysqli_fetch_array($re512))
  {
    print "<tr><td align=center>$row[refno]</td>";
    print "<td align=center>".datetohtml($row[entrydate])."</td>";
    print "<td align=right>$row[qty]</td><td align=right>$row[outqty]</td></tr>";
    $mcl = $mcl + $row[qty] - $row[outqty];
  }
 print "</table><br>";
 }

}// end if sea
    $mcl = $mcl + $tph;
$part=mysqli_query($db,"select lstprice from spareprmast where partcode = '$pcode' order by effectfrom desc limit 1");
$part1=mysqli_fetch_row($part);
$unit_price = $part1[0];
$tot = $mcl * $unit_price;
print  "<tr><td colspan=2 align=right><b>Closing Balance : </td><td align=right><b>$mcl  List Price : $unit_price  Total : $tot</td></tr>";


function getBranch($cde)
{
 $brre=mysqli_query($db,"select brname from branch where brcode='$cde'");
 $brrow=mysqli_fetch_row($brre);
 return $brrow[0];
}

function getsupname($cde)
{
 $brres=mysqli_query($db,"select supname from sup_master where icode='$cde'");
 $brrows=mysqli_fetch_row($brres);
 return $brrows[0];
}

?>
