<?
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>

<?
if($sea)
{
$sq="create table stockledger (dat date,partic varchar(250),inv double(12,2) default 0,outv double(12,2) default 0, brcode varchar(25))";
mysqli_query($db,$sql);
mysqli_query($db,"delete from stockledger where brcode='$brcode'");

 $sql="select * from spareopstock where itemcode='$pcode' and brcode='$brcode'";
 $re1=mysqli_query($db,$sql);
 while($row=mysqli_fetch_array($re1))
 {
   mysqli_query($db,"insert into stockledger values('','Opening Stock','$row[qty]','0','$brcode')")or die('op err'.mysqli_error($db));
 }

 //*********
 $sql="select * from sparepurchase where partcode='$pcode' and brcode='$brcode'";
 $re2=mysqli_query($db,$sql)or die(mysqli_error($db));
 if(mysqli_num_rows($re2)>0)
 { 
  while($row=mysqli_fetch_array($re2))
  {
    $sq="insert into stockledger values('$row[purdate]','Purchase Bill No:$row[pbillno]','$row[supqty]','0','$brcode')";
  mysqli_query($db,$sq) or die('pur err'.mysqli_error($db));
  }
 }

 //*********
/*
 $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>";
 }
*/
 //*********
 $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)
 { 
  while($row=mysqli_fetch_array($re4))
  {
    $brname=getBranch($row[to_branch]);
    $sq="insert into stockledger values('$row[entrydate]','Transfer out to $brname No:$row[tr_no]','0','$row[qty]','$brcode')";
  mysqli_query($db,$sq) or die('tr out err'.mysqli_error($db));

  }
 }
 //*********
 $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)
 { 
  while($row=mysqli_fetch_array($re42))
  {
    $brname=getBranch($row[from_branch]);
    $sq="insert into stockledger values('$row[entrydate]','Transfer in from $brname No:$row[tr_no]','$row[qty]','0','$brcode')";
  mysqli_query($db,$sq) or die('pur err'.mysqli_error($db));

  }
 }
 //*********
 $sql="select * from warsparetransout where partcode='$pcode' and brcode='$brcode'";
 $re41=mysqli_query($db,$sql)or die(mysqli_error($db));
 if(mysqli_num_rows($re41)>0)
 { 
  while($row=mysqli_fetch_array($re41))
  {
    $sq="insert into stockledger values('$row[date]','War transfer out (PSN)to $row[cust] No:$row[warno] - MSNO:$row[msno]','0','$row[psnqty]','$brcode')";
  mysqli_query($db,$sq) or die('tr out err'.mysqli_error($db));

  }
 }

 //*********
 $sql="select * from spareinvoice where partcode='$pcode' and brcode='$brcode'";
 $re5=mysqli_query($db,$sql)or die(mysqli_error($db));
 if(mysqli_num_rows($re5)>0)
 {
  while($row=mysqli_fetch_array($re5))
  {
    $sq="insert into stockledger values('$row[entrydate]','Spare Invoice to $row[customer] No:$row[invno] - MSNO:$row[msno]','0','$row[qty]','$brcode')";
  mysqli_query($db,$sq) or die('tr out err'.mysqli_error($db));

  }
 }
 //*********

 $sql="select * from warspareinvoice where partcode='$pcode' and brcode='$brcode'";
 $re5=mysqli_query($db,$sql)or die(mysqli_error($db));
 if(mysqli_num_rows($re5)>0)
 { 
  while($row=mysqli_fetch_array($re5))
  {
    $sq="insert into stockledger values('$row[entrydate]','War Spare Invoice to $row[customer] No:$row[invno] - MSNO:$row[msno]','0','$row[qty]','$brcode')";
  mysqli_query($db,$sq) or die('tr out err'.mysqli_error($db));

  }
 }
 //*********


$t=0;
 $sql="select * from spareinvoicefoc where partcode='$pcode' and brcode='$brcode'";
 $re51=mysqli_query($db,$sql)or die(mysqli_error($db));
 if(mysqli_num_rows($re51)>0)
 { 
  while($row=mysqli_fetch_array($re51))
  {
    $sq="insert into stockledger values('$row[entrydate]','Spare invoice FOC to $row[customer] No:$row[invno] - MSNO:$row[msno]','0','$row[qty]','$brcode')";
  mysqli_query($db,$sq) or die('tr out err'.mysqli_error($db));
  }
 }

 $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)
 { 
  while($row=mysqli_fetch_array($re511))
  {
    $sq="insert into stockledger values('$row[entrydate]','Sales return from $row[customer] No:$row[invno] - MSNO:$row[msno]','$row[qty]','0','$brcode')";
  mysqli_query($db,$sq) or die('tr out err'.mysqli_error($db));
  }
 }

$tbl1="spareinvwar".$brcode;
 $sql="select * from $tbl1 where partcode='$pcode' and trans='n'";
 $re5111=mysqli_query($db,$sql)or die(mysqli_error($db));
 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))
  {
    $sq="insert into stockledger values('$row[date]','War Spare Invoice(PSN) No:$row[warno] - MSNO:$row[msno]','$row[psnqty]','0','$brcode')";
  mysqli_query($db,$sq) or die('tr out err'.mysqli_error($db));

    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($db));
 if(mysqli_num_rows($re511)>0)
 {
  while($row=mysqli_fetch_array($re511))
  {
    $sq="insert into stockledger values('$row[date]','Jobcard InvNo:$row[invno], jcno:$row[jcno] - MSNO:$row[msno]','0','$row[qty]','$brcode')";
  mysqli_query($db,$sq) or die('jcard'.mysqli_error($db));

  }
 }

//*********************** 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)
 {
  while($row=mysqli_fetch_array($re512))
  {
    $sq="insert into stockledger values('$row[entrydate]','Stock ADJ No:$row[refno]','$row[qty]','$row[outqty]','$brcode')";
  mysqli_query($db,$sq) or die('tr out err'.mysqli_error($db));
  }

 }

//*********************** PH stock trinwar
 $sql="select * from sparetrinwarph where partcode='$pcode' and brcode='$brcode'";
 $re5ph=mysqli_query($db,$sql)or die(mysqli_error($db));
 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))
  {
    $sq="insert into stockledger values('$row[invdate]','Physical Stock War In No:$row[warno] - MSNO:$row[msno]','$row[recqty]','0','$brcode')";
  mysqli_query($db,$sq) or die('tr out err'.mysqli_error($db));

  }
 }
$res=mysqli_query($db,"select * from stockledger where brcode='$brcode' order by dat");
print "<table border=1 cellspacing=1 cellpadding=5> 
<th colspan=4>Stock ledger of the item : $pcode<br>Branch $brname <br>";
print "as on ".date("d-m-Y")."</th>
<tr><th>Date <th>Partic <th>In <th>Out";
$tin=0;$tout=0;
while($row=mysqli_fetch_array($res))
{
 print "<tr><td>".datetohtml($row[dat])."</td><td>$row[partic]</td><td align=right>".nozero($row[inv])."</td> <td align=right>".nozero($row[outv])."</td></tr>";
$tin+=$row[inv];
$tout+=$row[outv];
}

$bal=($tin-$tout);
if($bal>=0)
{
print "<tr><td colspan=2 align=center><b>Balance </td><td align=right>".number_format($bal,2)."</td><td align=right>&nbsp;</td></tr>";
print "<tr><td colspan=2 align=center><b>Total </td><td align=right>".number_format($tin,2)."</td><td align=right>".number_format($tin,2)."</td></tr>";

}
else
{
print "<tr><td colspan=2 align=center><b>Balance </td><td align=right>$nbsp;</td><td align=right>".number_format($bal,2)."</td></tr>";
print "<tr><td colspan=2 align=center><b>Total </td><td align=right>".number_format($tout,2)."</td><td align=right>".number_format($tout,2)."</td></tr>";

}
print "</table>";
return;
}// end if sea
?>

<center>
<form name='f1' action='<?print $PHP_SELF?>' onSubmit="handleSelectClick();return true;">
<table>
<th colspan=4>SELECT AN ITEM FOR LEDGER</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>
<?
function getBranch($cde)
{
 $brre=mysqli_query($db,"select brname from branch where brcode='$cde'");
 $brrow=mysqli_fetch_row($brre);
 return $brrow[0];
}
?>
