<?
session_start();
$brcode = $_SESSION['brcode'];
$user = $_SESSION['user'];
if(strlen($brcode)<=0)
{
 	print "<script> alert('Session timed out'); parent.document.location='../index.php'; </script>";
	return;
}
include("../opendb.php");
include("../test.php");
$ordres=mysqli_query($db,"select max(orderno) from saleorder_spare where brcode='$brcode'");
$ordrow=mysqli_fetch_row($ordres);
$ordnumber=$ordrow[0]+1;
?>
<title>ZF</title>
<script type="text/javascript" src="../scripts/jquery-latest.js"></script>
<link type="text/css" href="../includes/style.css" rel="stylesheet" />
<link href="../css/form.css" type="text/css" rel="stylesheet" media="screen" />
<script type="text/javascript">
	   $(function() {
          if ($.browser.msie && $.browser.version.substr(0,1)<7)
          {
			$('li').has('ul').mouseover(function(){
				$(this).children('ul').show();
				}).mouseout(function(){
				$(this).children('ul').hide();
				})
          }
        });        
    </script>
<link rel="stylesheet" href="../style.css" type="text/css" />
<body background="../back_ground.jpg">
<?php include('logo1.php'); ?>

<?php include('top.php'); ?>
<br><br>

<?
$tbl="sparetmp".$brcode.$user;
//include("calcsparestock.php");
?>

<script>
var pna=[];
var qty=[];
var hand=[];
var price=[];
var disc=[];

function validqty()
{
if(parseFloat(document.f1.qty.value)<parseFloat(document.f1.sqty.value))
{
alert('Insufficient Quantity');
document.f1.sqty.value=0;
document.f1.sqty.focus();
return;
}
}
function getname()
{
 if(document.f1.partcode.selectedIndex<=0)
 {
 document.f1.prtname.value="";
 document.f1.price.value="";
 document.f1.discount.value="";
 document.f1.hand.value="";
 }
 else{
 document.f1.prtname.value=pna[document.f1.partcode.selectedIndex];
 document.f1.price.value=price[document.f1.partcode.selectedIndex];
 document.f1.discount.value=disc[document.f1.partcode.selectedIndex];
 document.f1.hand.value=hand[document.f1.partcode.selectedIndex];
/* if(parseFloat(qty[document.f1.partcode.selectedIndex])>=0)
  document.f1.qty.value=qty[document.f1.partcode.selectedIndex];
 else
  document.f1.qty.value=0;
*/
 var act=parseFloat(document.f1.price.value)*parseFloat(document.f1.sqty.value);
 var act1=act*parseFloat(document.f1.discount.value)/100;

 document.f1.aprice.value=parseFloat(act-act1).toFixed(2);
 var act2=parseFloat(document.f1.aprice.value);
  var hnd=parseFloat(act2)*document.f1.hand.value/100;
  var cst=parseFloat(act2)*document.f1.cst.value/100;
  var gtot=parseFloat(act2+cst+hnd).toFixed(2);
  document.f1.tot.value=gtot;
 }
}
function valid()
{
 if(document.f1.partcode.selectedIndex<=0)
 {
  alert('Invalid Item code');spareprmast
  return false;
 }
if(parseFloat(document.f1.sqty.value)<=0)
{
alert('Invalid Quantity');
document.f1.sqty.value=0;
document.f1.sqty.focus();
return false;
}
/*
var x=document.getElementById("mselect")
 document.f1.partcode1.value=x.options[x.selectedIndex].text
x.remove(x.selectedIndex)
*/
}
</script>
<body bgcolor='ivory'>
<center>
<form name='f1' action='<?print $PHP_SELF?>' method='POST' onSubmit='return valid()'>
<input type=hidden name='partcode1'>
<table class=form>
<th colspan=10><font color='black'<h3>SPARE INVOICE ORDER - (<?print $ordnumber?>)</h3></th>
<tr>
<td>Part Code</td><td><select name='partcode' id='mselect' onChange=getname();><option></option>
<?
$i=1;
$re=mysqli_query($db,"select * from partmaster order by partcode");
while($row=mysqli_fetch_array($re))
{
	print "<option value='$row[0]'>$row[0]</option>";
	
	$ker=mysqli_query($db,"select brstate from branch where brcode='$brcode'");
	$kar=mysqli_fetch_array($ker);
	if($kar[0]=='Kerala') 
	{
		$disres=mysqli_query($db,"select lstprice,discount from spareprmast where partcode='$row[0]' and brcode='ERA153' order by effectfrom desc")or die('errr'.mysqli_error($db));
		$disrow=mysqli_fetch_array($disres);
	}
	else
	{
		$disres=mysqli_query($db,"select lstprice,discount from spareprmast where partcode='$row[0]' and brcode='BAN682' order by effectfrom desc")or die('errr'.mysqli_error($db));
		$disrow=mysqli_fetch_array($disres);
	}
	print "<script>pna[$i]='$row[partname]'; price[$i]='$disrow[0]'; disc[$i]='0'; </script>";
	$disres1=mysqli_query($db,"select hand_charge from partmaster where partcode='$row[0]'");
	$disrow1=mysqli_fetch_array($disres1);
	print "<script>hand[$i]='$disrow1[0]';</script>";
	$i++;
}
?>
</select></td><td>Part Name </td><td COLSPAN=3><input type=text readonly  name='prtname' size=30></td></tr>
<tr><td>
<!-- Ex Qty</td><td><input type=text name='qty' size=6 readonly> -->
<td>
Order qty <input type=text name='sqty' size=6 onChange='getname()'></td>
  <td>Price </td><td><input type=text readonly name='price' size=10></td>
  <td>Disc% </td><td><input type=text  name='discount' size=6 onChange='getname()'> 
   Act Price <input type=text name='aprice' size=10>
</td></tr>
<tr><td>Hand Chrg%</td><td><input type=text readonly  name='hand' size=6></td>
<?
 $cstres=mysqli_query($db,"select tax from taxmaster where type='Local'");
  $cstrow=mysqli_fetch_row($cstres);
?>
<td>VAT%</td><td><input type=text name='cst'  readonly size=6 value='<?print $cstrow[0]?>'></td>
<td>Total</td><td><input type=text name='tot' readonly  size=16></td></tr>
<tr><td colspan=6 align=center bgcolor='steelblue'><input type=submit value='Add' name='add'></td></tr>
</table>
</form>
<?

if(isset($_POST[Del]))
{
$partcode=$_REQUEST['partcode'];
$tbl='invotmp'.$brcode.$user;
 mysqli_query($db,"delete from $tbl where partcode='$partcode'")or die('err'.mysqli_error($db));
 print "<script>alert('Data Has been Deleted');document.location='newspare.php'</script>";
}

if(isset($_POST[add]))
{
  $partcode=$_REQUEST['partcode'];
$prtname=$_REQUEST['prtname'];
$sqty=$_REQUEST['sqty'];
include("../util/exqty.php");
$tbl='invotmp'.$brcode.$user;
//echo $tbl;
$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);


  $res1=mysqli_query($db,"select * from $tbl where partcode='$partcode'");
  //print "select * from $tbl where partcode='$partcode'";
  if(mysqli_num_rows($res1)>0)
  {
    print "<script> alert('Duplicate Entry');document.location='newspare.php';</script>";
    
  }
  else
  {
$qty=exqty1($partcode,$brcode,$user);
if($sqty=='')
{
$sqty=0;
}
 $sql="insert into $tbl values('$partcode','$prtname','$qty','$sqty','$brcode')";
//print "insert into $tbl values('$partcode','$prtname','$qty','$sqty','$brcode')";
 mysqli_query($db,$sql) or die(mysqli_error($db));
  }
}

function getPrice($code)
{
if($kk=='Kerala') 
{
 $prres=mysqli_query($db,"select lstprice from spareprmast where partcode='$code' and brcode='ERA153' order by effectfrom desc")or die('err'.mysqli_error($db));
 $prrow=mysqli_fetch_array($prres);
 return $prrow[0];
}
else
{
 $prres=mysqli_query($db,"select lstprice from spareprmast where partcode='$code' and brcode='BAN682' order by effectfrom desc")or die('err'.mysqli_error($db));
 $prrow=mysqli_fetch_array($prres);
 return $prrow[0];
}
}



?>


<center>
<form name='f2' action='<?print $PHP_SELF?>' onSubmit='return valid()' method='POST'>
<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>





<?

$tbl='invotmp'.$brcode.$user;

$res=mysqli_query($db,"select * from $tbl order by partcode");
//print "select * from $tbl order by partcode";
if(mysqli_num_rows($res)>0)
{

  $i=1;
  print "<table border=1 cellpadding=3 class=report> <th>No<th>Item Code <th>Item Name <th>Ex Qty<th>Qty <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));
 // print "select location from sparelocmast where brcode='$brcode' and itemcode='$row[partcode]'".'<br>';
   $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>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td>";
    
 
 $tqty+=$row[3];
    
 
   


    
    

    print "<td><input type=submit value='Del' name='Del'></td></tr></form>";
    



 }






 print "<tr><td colspan=4><center> Total</td>";
 print "<td>".putComa(round($tqty,2))."</td><td>&nbsp;</td>";

 
 print "</tr>";
 print "</table>";
}




if(isset($_POST[save]))
{
$msno=$_REQUEST['msno'];
$to=$_REQUEST['to'];

$tbl='invotmp'.$brcode.$user;
$re=mysqli_query($db,"select * from $tbl");
$dt=date("Y-m-d");
$pres=mysqli_query($db,"select max(orderno) from saleorder_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 saleorder_spare (partcode,partname,qty,date,orderto,brcode,sup_purno,sup_qty,sup_date,orderno,trans_type,sqty,msno) values('$ro[0]','$ro[1]','$ro[3]','$dt','$to','$brcode','$orderno',0,'0000-00-00','$orderno','','$ro[3]','$msno')";

 mysqli_query($db,$sql) or die(mysqli_error($db));
}
mysqli_query($db,"delete from $tbl");
print "<script> window.open('prnsaleorderspare1.php?orderno=$orderno'); </script>";
print "<script> document.location='newspare.php' </script>";
}

?>

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