<?
session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}

include("../opendb.php");
include("../test.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;
 }
 if(confirm('Do you want to save?'))
   return true;
 else
   return false;
}
</script>
<?
if($save)
{
$tbl='qinvotmp'.$brcode;
$re=mysqli_query($db,"select * from $tbl");
$dt=date("Y-m-d");
$pres=mysqli_query($db,"select max(orderno) from q_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 q_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','$terms')";
 mysqli_query($db,$sql) or die(mysqli_error());
}
mysqli_query($db,"delete from $tbl");
print "<script> window.open('prnqspare.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>
<h3>QUOTATION</H3>
<form name='f1' action='<?print $PHP_SELF?>' 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>";
?>
<br>MSNO:
<input type=text name="msno" size=35> <br>
Terms: 
<?
 $ter=mysqli_query($db,"select tname from termsmaster")or die(mysqli_error());
  print "<select name='terms' style={width:450}>";
 while($trow=mysqli_fetch_array($ter))
 {
   print "<option>$trow[0]</option>";
 }
print "</select>";
?>
<br>
<input type=submit value='Save & Print' name='save'>
</td></tr></table>
</form>

<br>
<?
$tbl='qinvotmp'.$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($add)
{
  $res1=mysqli_query($db,"select * from $tbl where partcode='$partcode'");
  if(mysqli_num_rows($res1)>0)
  {
    print "<script> alert('Duplicate Entry')</script>";
    return;
  }

 $sql="insert into $tbl values('$partcode','$prtname','$qty','$sqty','$brcode')";
//print $sql;
 mysqli_query($db,$sql) or die(mysqli_error());
}

$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>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());
    $locrow=mysqli_fetch_array($locres);
$rate=getPrice($row[0]);
    print "\n<form action='$PHP_SELF'> <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[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=4><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>";
}




if($Del)
{
 mysqli_query($db,"delete from $tbl where partcode='$partcode'")or die('err'.mysqli_error());
 print "<script>document.location='qtemp.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());
 $prrow=mysqli_fetch_array($prres);
 return $prrow[0];
}
?>
