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

include("../opendb.php");
include("../test.php");
include("../util/exqty2.php");
?>
<script>
var msn=[];
msn[0]='';
function getmsno()
{
document.f1.msno.value=msn[document.f1.to.selectedIndex];
}
function valid()
{
 if(document.f1.msno.value.length<=0)
 {
  alert('Invalid MSNO');
  return false;
 }
 if(document.f1.to.selectedIndex<=0)
 {
   alert('Invalid Customer');
   return false;
 }

}
</script>

<?


if(isset($_POST['save']))
{

$to=$_REQUEST['to'];
$msno=$_REQUEST['msno'];
$tbl='invotmp'.$brcode;
$re=mysqli_query($db,"select * from $tbl");
$dt=date("Y-m-d");
$pres=mysqli_query($db,"select max(orderno) from warorder_spare where brcode='$brcode'");
$prow=mysqli_fetch_row($pres);
if($prow[0]<=0)
 $orderno=1;
else
 $orderno=($prow[0]+1);

while($ro=mysqli_fetch_array($re))
{

 $sql="insert into warorder_spare (partcode,partname,qty,date,orderto,brcode,orderno,sqty,msno,closed,reas) values('$ro[0]','$ro[1]','$ro[2]','$dt','$to','$brcode','$orderno','$ro[3]','$msno','N','-')";

 mysqli_query($db,$sql) or die(mysqli_error($db));
}
mysqli_query($db,"delete from $tbl");
print "<script> window.open('prnwarorderspare1.php?orderno=$orderno'); </script>";
}
?>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
	tr{background-color:white}
	th{background-color:steelblue;color:white}
</style>
<center>
<form name='f1' action='<?print $PHP_SELF?>' method='post' onSubmit='return valid()'>
<table border=1 width=100%>
<tr><td><center>Customer : 
<?
$brres=mysqli_query($db,"select acname,code from customer order by acname");
print "<select name='to' onChange=getmsno()><option></option>";
$i=1;
 while($brrow=mysqli_fetch_array($brres))
 {
   print "\n<option value='$brrow[0]'>$brrow[0]</option>";
   $mres=mysqli_query($db,"select msno from minput where cust='$brrow[1]'");
   if(mysqli_num_rows($mres)>0)
   {
    $ms='';
   while($mrow=mysqli_fetch_array($mres))
   {
      $ms=$ms.' '.$mrow[0].',';
   }
$ms= substr($ms,0,strlen($ms)-1);
     print "\n <script> msn[$i]='$ms'; </script>";
	$i++;

   }
   else
   {
     print "\n <script> msn[$i]='-'; </script>";
	$i++;
   }
  
 }
$brres1=mysqli_query($db,"select distinct code,supname from sup_master order by supname");
 while($brrow1=mysqli_fetch_array($brres1))
 {
   print "\n<option value='$brrow1[0]'>$brrow1[1]</option>";
 }

print "</select>";
?>
&nbsp;MSNO:
<input type=text name="msno" size=35> <br>
<input type=submit value='Save & Print' name='save'>
</td></tr></table>
</form>

<br>
<?
$tbl='invotmp'.$brcode;
$sql="create table $tbl (partcode varchar(100),prtname varchar(200), qty double(14,2), sqty double(14,2), brcode varchar(100))";
//print "sql:".$sql;
//$sql="create table $tbl (partcode varchar(100),prtname varchar(200), qty double(14,2),sqty double(14,2), price double(14,2), discount double(14,2), hand double(14,2), cst double(14,2), tot double(14,2), brcode varchar(100))";

mysqli_query($db,$sql);

if(isset($_POST['add']))
{
$partcode=$_REQUEST['partcode'];

$prtname=$_REQUEST['prtname'];
$sqty=$_REQUEST['sqty'];
  $res1=mysqli_query($db,"select * from $tbl where partcode='$partcode'");
  if(mysqli_num_rows($res1)>0)
  {
    print "<script> alert('Duplicate Entry')</script>";
    return;
  }
$qty=exqty1($partcode,$brcode,$user);
if (is_numeric($qty))
$qty = $qty;
else
$qty = 0;
 $sql="insert into $tbl values('$partcode','$prtname','$qty','$sqty','$brcode')";

 mysqli_query($db,$sql) or die(mysqli_error($db));
}

$res=mysqli_query($db,"select * from $tbl order by partcode");
if(mysqli_num_rows($res)>0)
{
  $i=1;
  print "<table border=1 cellpadding=3> <th>No <th>Location<th>Item Code <th>Item Name <th>Ex Qty<th>Qty <th>Rate <th>Amount<th>Tax <th>Cess <th>Net <th> Del";
  while($row=mysqli_fetch_array($res))
  {
    $locres=mysqli_query($db,"select location from sparelocmast where brcode='$brcode' and itemcode='$row[partcode]'")or die(mysqli_error($db));
    $locrow=mysqli_fetch_array($locres);
$rate=getPrice($row[0]);
    print "\n<form action='$PHP_SELF' method='post'> <input type=hidden name='partcode' value='$row[0]'><tr><td>";
    print $i++."</td><td>$locrow[0]</td><td>$row[0]</td><td>$row[1]</td><td>$row[qty]</td><td>$row[sqty]</td>";
    print "<td>$rate</td>";
    $tqty+=$row[sqty];
    $amt=$rate*$row[sqty];
    print "<td align=right>$amt</td>";
    $tax=($amt*12.5/100);
    print "<td align=right>$tax</td>";
    $cess_amt=($tax*1/100);
    print "<td align=right>".putComa(round($cess_amt,3))."</td>";
    $netamt_1=$amt+$tax+$cess_amt;
    print "<td align=right>".putComa(round($netamt_1,2))."</td>";

    print "<td><input type=submit value='Del' name='Del'></td></tr></form>";
    $totamt+=$netamt_1;
  }
 print "<tr><td colspan=5><center> Total</td>";
 print "<td>".putComa(round($tqty,2))."</td><td>&nbsp;</td>";

 print "<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>";
 print "<td align=right>".putComa(round($totamt,2))."</td></tr>";
 print "</table>";
}


$Del=$_REQUEST['Del'];


if($Del)
{
$partcode=$_REQUEST['partcode'];
 mysqli_query($db,"delete from $tbl where partcode='$partcode'")or die('err'.mysqli_error($db));
 print "<script>document.location='invtemp2.php'</script>";
}
function getPrice($code)
{
 $prres=mysqli_query($db,"select lstprice from spareprmast where partcode='$code' order by effectfrom desc")or die('err'.mysqli_error($db));
 $prrow=mysqli_fetch_array($prres);
 return $prrow[0];
}
?>