
<?
session_start();

$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];

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");
?>

<?


mysqli_query($db,"drop table stockledger");

$sq="create table stockledger (dat date,partic varchar(250),inv double(12,2) default 0,outv double(12,2) default 0, brcode varchar(25),led varchar(240),rate double(10,2))";
mysqli_query($db,$sq);

mysqli_query($db,"delete from stockledger where brcode='$brcode'");


$pcode=$_REQUEST[pcode];
print  $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('0000-00-00','Opening Stock','$row[qty]','0','$brcode','Opening','$row[ndpvalue]')")or die('op err'.mysqli_error($db));
 }

 //*********
 print $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))
  {
 print    $sq="insert into stockledger values('$row[entrydate]','P.Bill No:-$row[pbillno],Pdate=".datetohtml($row[purdate]).", entrydate=".datetohtml($row[entrydate]).",P.No=$row[purno],Rate=$row[price],discount%=$row[disc],tax%=$row[tax],taxtype=$row[taxtype],- $row[suplier]','$row[supqty]','0','$brcode','$suplier','$row[price]')";
  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','$brname','$row[amount]')";
  mysqli_query($db,$sq) or die('tr out err'.mysqli_error($db));

  }
 }
 //*********
 //*********
 $sql="select * from sparetransoutexcess 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 excess to $brname No:$row[tr_no]','0','$row[qty]','$brcode','$brname','$row[amount]')";
  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','$brname','$row[amount]')";
  mysqli_query($db,$sq) or die('pur err'.mysqli_error($db));

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

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

  }
 }
 //*********
 
 
 //*********
 $sql="select * from spareinvoice_oth 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 other to $row[customer] No:$row[invno] - MSNO:$row[msno]','0','$row[qty]','$brcode','$row[customer]','$row[aprice]')";
  mysqli_query($db,$sq) or die('tr out err'.mysqli_error($db));

  }
 }
 //*********
 
 
 
 //********* preturn eicher
 $sql="select * from sparepurreteich  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 return to $row[customer] No:$row[invno] - MSNO:$row[msno]','0','$row[qty]','$brcode','$row[customer]','$row[aprice]')";
  mysqli_query($db,$sq) or die('tr out err'.mysqli_error($db));

  }
 }
 //*********
 
 
 
  //*********
 $sql="select * from mb51 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[invdate]','mb51','0','$row[outqty]','$brcode','','0')";
  mysqli_query($db,$sq) or die('tr out err'.mysqli_error($db));

  }
 }
 //*********
 
   //*********
 $sql="select * from mb51 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[invdate]','mb51','$row[inqty]','0','$brcode','','0')";
  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','$row[customer]','$row[aprice]')";
  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','$row[customer]','$row[aprice]')";
  mysqli_query($db,$sq) or die('tr out err'.mysqli_error($db));
  }
 }


 

$t=0;
//*********************** jcard
 $sql="select * from jcardpartsstock 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))
  {

 $sql="select * from jobcardstock where jcno='$row[jcno]' and brcode='$brcode'";
//print $sql;

 $r1=mysqli_query($db,$sql)or die(mysqli_error($db));
$r=mysqli_fetch_array($r1);

    $sq="insert into stockledger values('$row[date]','Jobcard InvNo:$row[invno], jcno:$row[jcno] - MSNO:$r[msno]','0','$row[qty]','$brcode','$r[cname]','$row[rate]')";
//print $sq;
  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','aa','$row[rate]')";
  mysqli_query($db,$sq) or die('tr out err'.mysqli_error($db));
  }

 }

//*********************** PH stock trinwar

$res=mysqli_query($db,"select * from stockledger where brcode='$brcode' order by dat");
print "<table border=1 cellspacing=1 cellpadding=5> 
<th colspan=6>Stock ledger of the item : $pcode<br>Branch $brcode <br>";
print "as on ".date("d-m-Y")."</th>
<tr><th>Date <th>Partic<th>Legder<th>Rate <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>$row[led]</td><td>$row[rate]</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=4 align=center><b>Balance </td><td align=right>$nbsp;</td><td align=right>".number_format($bal,2)."</td></tr>";
print "<tr><td colspan=4 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=4 align=center><b>Balance </td><td align=right>$nbsp;</td><td align=right>".number_format($bal,2)."</td></tr>";
print "<tr><td colspan=4 align=center><b>Total </td><td align=right>".number_format($tout,2)."</td><td align=right>".number_format($tout,2)."</td></tr>";

}
print "</table>";
?>
<input type=button value="<- Back" onClick=history.back(1)>
<?
return;

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