<?
session_start();
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 onKeyUp="handleKeyUp(20);" type="text" name="pcode" VALUE="<?print $pcode?>" style="font-size:10pt;width:34ex;"><br>
<select onClick="handleSelectClick();" name="functionselect" size="2" style="font-size:10pt;width:34ex;">
<?
 $i=0;
 $re=mysqli_query($db,"select partcode,partname from partmaster order by partcode");
 while($prow=mysqli_fetch_array($re))
 {
  print "\n<option value='$prow[0]'>$prow[0]</option>";
  print "\n<script>pc[$i]='$prow[0]'; pn[$i]='$prow[1]'; </script>";
 $i++;
 }
?>
</select>
</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>
<?
if($sea)
{
 $sql="select * from spareopstock 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>";
 }
 print "</table><br>";
 //*********
 $sql="select * from sparepurchase where partcode='$pcode' and brcode='$brcode'";
 $re2=mysqli_query($db,$sql)or die(mysqli_error());
 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[pbillno] </td><td>".datetohtml($row[purdate])."</td><td align=right>$row[supqty]</td></tr>";
   $t+=$row[supqty];
  }
  print "<tr><td colspan=2 align=right><b>Total</td><td align=right><b>".putComa($t)."</td></tr>";
 print "</table><br>";
 }


 //*********
$t=0;
 $sql="select * from sparepurreturn where partcode='$pcode' and brcode='$brcode'";
 $re2=mysqli_query($db,$sql)or die(mysqli_error());
 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>$row[pbillno] </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>";
 }

 //*********


 //*********
/*
 $sql="select * from spareinvoice where partcode='$pcode' and brcode='$brcode'";
 $re3=mysqli_query($db,$sql)or die(mysqli_error());
 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());
 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>";
 }
$t=0;
 //*********
 $sql="select * from sparetransin where partcode='$pcode' and brcode='$brcode'";
 $re42=mysqli_query($db,$sql)or die(mysqli_error());
 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>";
 }
$t=0;
 //*********
 $sql="select * from warsparetransout where partcode='$pcode' and brcode='$brcode'";
 $re41=mysqli_query($db,$sql)or die(mysqli_error());
 if(mysqli_num_rows($re41)>0)
 { print "<table border=1 width=70%><th colspan=6>WARANTY TRANSFER OUT <tr><th>Tr No <th>Date <th>To <th>PSN Qty <th>Telc Qty<th>MSNO";
  while($row=mysqli_fetch_array($re41))
  {
    print "<tr><td align=center>$row[warno] </td><td>".datetohtml($row[date])."</td>";
    print "<td>$row[cust]</td><td align=right>$row[psnqty]</td><td align=right>$row[telcqty]</td><td>$row[msno]</td></tr>";
   $t1+=$row[psnqty];
   $t2+=$row[telcqty];
  }
 print "<tr><td colspan=3 align=right><b>Total</td><td align=right><b>".putComa($t1)."</td>";
 print "<td align=right><b>".putComa($t2)."</tr>";
 print "</table><br>";
 }

 //*********
 $sql="select * from spareinvoice where partcode='$pcode' and brcode='$brcode'";
 $re5=mysqli_query($db,$sql)or die(mysqli_error());
 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>";
 }
 //*********

 $sql="select * from warspareinvoice where partcode='$pcode' and brcode='$brcode'";
 $re5=mysqli_query($db,$sql)or die(mysqli_error());
 if(mysqli_num_rows($re5)>0)
 { print "<table border=1 width=70%><th colspan=5>WARINV<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>";
 }
 //*********


$t=0;
 $sql="select * from spareinvoicefoc where partcode='$pcode' and brcode='$brcode'";
 $re51=mysqli_query($db,$sql)or die(mysqli_error());
 if(mysqli_num_rows($re51)>0)
 { print "<table border=1 width=70%><th colspan=5>INVOICE FOC<tr><th>Inv No <th>Date <th>Customer <th>Qty <th>MSNO";
  while($row=mysqli_fetch_array($re51))
  {
    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>";
 }

 $sql="select * from salesreturn where partcode='$pcode' and brcode='$brcode'";
 $re511=mysqli_query($db,$sql)or die(mysqli_error());
 if(mysqli_num_rows($re511)>0)
 { print "<table border=1 width=70%><th colspan=5>INVOICE FOC<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>";
  }
 print "</table><br>";
 }

$tbl1="spareinvwar".$brcode;
 $sql="select * from $tbl1 where partcode='$pcode' and trans='n'";
 $re5111=mysqli_query($db,$sql)or die(mysqli_error());
 if(mysqli_num_rows($re5111)>0)
 { print "<table border=1 width=70%><th colspan=5>WARRANTY OUT - PENDING<tr><th>War No <th>Date  <th>Qty <th>MSNO <th>Stat";
  while($row=mysqli_fetch_array($re5111))
  {
    print "<tr><td align=center>$row[warno] </td><td>".datetohtml($row[date])."</td>";
    print "<td align=right>$row[psnqty]</td><td>$row[msno]</td>";
     if($row[trans]=='n')
       print "<td><center>Pending</td>";
    print "</tr>";
  }
 print "</table><br>";
 }
$t=0;
//*********************** jcard
 $sql="select * from jcardparts where partcode='$pcode' and brcode='$brcode'";
 $re511=mysqli_query($db,$sql)or die(mysqli_error());
 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>";
 }

//*********************** adjstock
 $sql="select * from adjstock where partcode='$pcode' and brcode='$brcode'";
 $re512=mysqli_query($db,$sql)or die(mysqli_error());
 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>";
  }
 print "</table><br>";
 }

//*********************** PH stock trinwar
 $sql="select * from sparetrinwarph where partcode='$pcode' and brcode='$brcode'";
 $re5ph=mysqli_query($db,$sql)or die(mysqli_error());
 if(mysqli_num_rows($re5ph)>0)
 { print "<table border=1 width=70%><th colspan=5>Physical Stock Tranfer In<tr><th>War No";
   print "<th>Date <th> Qty ";
  while($row=mysqli_fetch_array($re5ph))
  {
    print "<tr><td align=center>$row[warno]</td>";
    print "<td align=center>".datetohtml($row[invdate])."</td>";
    print "<td align=right>$row[recqty]</td></tr>";
    $tph+=$row[recqty];
  }
 print "<tr><td colspan=2 align=right><b>Total</td><td align=right><b>".putComa($tph)."</td></tr>";
 print "</table><br>";
 }

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