<?
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("exqty.php");
?>
<link href="css/form.css" type="text/css" rel="stylesheet" media="screen"  />
<link type="text/css" href="jquery/jquery-ui.min.css" rel="stylesheet" />	
<script type="text/javascript" src="jquery/jquery-1.7.min.js"></script>
<script type="text/javascript" src="jquery/jquery-ui.min.js"></script>
<script type="text/javascript" src="jquery/jquery.jqEasyCharCounter.min.js"></script>
    <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>
<script type="text/javascript">
$(function(){
    
//ch chk


$(".ordertoch").live("keydown.autocomplete",function(){

$ordertoch = $(this);
$row = $ordertoch.parent().parent();
$(this).autocomplete({
		source: function( request, response ) {

		$.ajax({
			url: "searchch2.php",
			dataType: "json",
			data: "key="+$ordertoch.val(),
			success: function( data ) {
				response( $.map( data, function( item ) {
					return {
						label: item.ordertoch ,
						value: item.ordertoch,
						code:item.partname,
							
					}
				}));
			}
		});
	}
	
,
	minLength: 2,
	select: function( event, ui ) {
	$row.find('.ordertoch').val(ui.item.ordertoch);
	//$row.find('.msno').val(ui.item.partname);
	//document.f1.msno.value=ui.item.code;
	//document.f1.addr.value=ui.item.adr;
	//document.f1.tin.value=ui.item.gst;
	//document.f1.msno.focus();
	/*log( ui.item ?
	"Selected: " + ui.item.label :
	"Nothing selected, input was " + this.value);
	*/
	 }
	
	});

					
});
//ch chk end
});
</script>
<script>
 function validqty(x)
 {
   var qtyele=x;
   var stkele='stk'+x;
   var ordele='ordqty'+x;
   var prele='price'+x;
   var totprele='totprice'+x;
  var tpar=parseFloat(document.getElementById(prele).value);
   var ordq=parseFloat(document.getElementById(ordele).value);
   var supq=parseFloat(document.getElementById(qtyele).value);
   
   var totp=tpar*supq;
   document.getElementById(totprele).value=totp;
   
   if(supq>ordq)
   { 
     alert('Cannot supply more than ordered');
      document.getElementById(qtyele).focus();
     document.getElementById(qtyele).value=0;
      document.getElementById(qtyele).focus();

     return false;

   }
   exstock=parseFloat(document.getElementById(stkele).value);
   if(parseFloat(document.getElementById(qtyele).value)>exstock)
   {
     alert('Insufficient Stock');
      document.getElementById(qtyele).focus();
     document.getElementById(qtyele).value=0;
      document.getElementById(qtyele).focus();

     return false;
   }
   
}

function validqtyp(x)
 {
     ///alert(x);
   var qtyele=x;
   var stkele='stk'+x;
   var ordele='ordqty'+x;
   var prele='price'+x;
   var totprele='totprice'+x;
  var tpar=parseFloat(document.getElementById(prele).value);
   var ordq=parseFloat(document.getElementById(ordele).value);
   var supq=parseFloat(document.getElementById(qtyele).value);
   
   var totp=tpar*supq;
   document.getElementById(totprele).value=totp;
   
   
}

</script>

<title>Spare Transfer Out </title>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
	th{background-color:steelblue;color:white}
	.right{text-align:right}
</style>
<script>
 function valid()
 {
  if(document.f1.d.value.length<=0)
   {
     alert('Invalid Date');
	   return false;
   }
   
   if(document.f1.saprefno.value.length<=0)
   {
     alert('Invalid Chasis');
	   return false;
   }
   
   if(confirm('Save ?'))
     {return true;}
   else
     {return false;}
 }
</script>

<center>
<h3>SPARE TRANSFER OUT</H3>
</CENTER>
<?
$show=$_REQUEST['show'];
$brn=$_REQUEST['brn'];
if($show)
{
//include("calcsparestock.php");
if($brn=='All')
{
$sql="select a.brcode,b.brname from purorder_spare a, branch b where a.orderto='$brcode' and a.brcode=b.brcode and a.qty>a.sup_qty and a.closed='N' group by a.brcode";
}
else
{
$sql="select a.brcode,b.brname from purorder_spare a, branch b where a.orderto='$brcode' and a.brcode=b.brcode and a.qty>a.sup_qty and a.brcode='$brn' and a.closed='N' group by a.brcode";
}

$result=mysqli_query($db,$sql)or die(mysqli_error($db));

while($row1=mysqli_fetch_array($result))
{
 print "<fieldset><legend>Sale Order From: <b>$row1[1]</b></legend>";
?>


<center>
<form name='f1' action='svsparetroutnew.php' method=post onSubmit="return valid()" target='n'>
<input type=hidden name='to_branch' value='<?print $row1[0]?>'>
<table>
<tr><td>
Transfer Date </td><td>: dd <input type=text name='d'  size=2>
	 mm <input type=text name='m' value='<?print date("m")?>' size=2>
	 yy <input type=text name='y' value='<?print date("Y")?>' size=4></td>
</tr>
<tr><td>
Entry Date </td><td>: dd <input type=text name='d1' value='<?print date("d")?>' size=2>
	 mm <input type=text name='m1' value='<?print date("m")?>' size=2>
	 yy <input type=text name='y1' value='<?print date("Y")?>' size=4>
</tr></td>
<td>Chasis No </td><td><input type='text' name='chasis1' id='ordertoch' placeholder='Type chasis number Here' class='ordertoch' size=30></td><td>Type of Work </td><td><select name='worktype'> 

    <option value='GPS'>GPS</option>
    <option value='YELLOW PAINTING CHARGE'>YELLOW PAINTING CHARGE</option>
    <option value='NUMBER BOARD FITTING'>NUMBER BOARD FITTING</option>
    <option value='HSRP NUMBER PALTE FIXING'>HSRP NUMBER PALTE FIXING</option>
    <option value='2055 BRAKE LINER FITEMNT WITH CYLINDER'>2055 BRAKE LINER FITEMNT WITH CYLINDER</option>
    <option value='2055 BRAKE LINER FITEMNT '>2055 BRAKE LINER FITEMNT </option>
    <option value='AC FITMENT'>AC FITMENT</option>
    <option value='CAMERA AND STERIO'>CAMERA AND STERIO</option>
    <option value='ABS FITMENT (GAIL VEH)'>ABS FITMENT (GAIL VEH)</option>
    <option value='PAINTING CHARGE (GAIL VEH)'>PAINTING CHARGE (GAIL VEH)</option>
    <option value='YELLOW PAINTING CHARGE (HD)'>YELLOW PAINTING CHARGE (HD)</option>
    <option value='CUSSION 6000 SERIES'>CUSSION 6000 SERIES</option>
    
    <option value='STERIO AND CAMERA 3000 SERIES'>STERIO AND CAMERA 3000 SERIES</option>
    <option value='STERIO AND CAMERA 6000 SERIES'>STERIO AND CAMERA 6000 SERIES</option>
    <option value='CONSUMABLES 3000 SERIES'>CONSUMABLES 3000 SERIES</option>
    <option value='CONSUMABLES 6000 SERIES'>CONSUMABLES 6000 SERIES</option>
    
    
</select></td>
</table>
<br>
<table border=1 cellspacing=1 cellpadding=3 >
<th>No.<th>Part Code <th>Partname<th>Ord <br>No<th>Order <br>Qty<th>Sup <br>Qty
<th>ExQty <th>PSN <BR>Trans Qty
<th>Price <br>
<th>Total Price
<th>Ord date
<?
$ss1="select distinct(partcode) from purorder_spare where brcode='$row1[0]' and orderto='$brcode' and qty>sup_qty and closed='N' order by orderno desc";

$ree=mysqli_query($db,$ss1);
$i=1;
//$tbl="sparetmp".$brcode.$user;
while($row2=mysqli_fetch_array($ree))
{


$s="select * from purorder_spare where brcode='$row1[0]' and orderto='$brcode' and qty>sup_qty and closed='N' and partcode='$row2[0]'order by orderno desc limit 1";
$re=mysqli_query($db,$s);

//$tbl="sparetmp".$brcode.$user;
while($row=mysqli_fetch_array($re))
{
/* $stkres=mysqli_query($db,"select supqty from $tbl where partcode='$row[partcode]'");
 $stkrow=mysqli_fetch_array($stkres);
 if($stkrow[0]<=0)
  $stk=0;
 else
  $stk=$stkrow[0];
*/
//print $row[partcode];
$ex_stk=exqty1($row['partcode'],$brcode,$user,$db);
//print $user;

 if($ex_stk<=0)
  $stk=0;
 else
  $stk=$ex_stk;
print "<tr><td>".$i."<td><input type=text readonly name='partcode[]' value='$row[partcode]' size=9> </td>";
print "\n<td><textarea name='partname[]' rows=1 readonly>$row[partname]</textarea></td>";
print "\n<td><center><input type=text class=right readonly name='pord_no[]' value='$row[orderno]' size=3></td>";
print "\n<td align=center><input type=text lass=right readonly id='ordqty$i' value='".($row['qty'])."' size=4></td>";
print "\n<td align=center>".($row['sup_qty'])."</td>";
print "<td><input type=text name='exqty' readonly size=4 id='stk$i' value='$stk'></td>";

print "\n<td><center><input type=text name='qty[]' id='$i' class=right size=4 onKeyUp='validqty(this.id)'></td>";

$sql12=mysqli_query($db,"select price from sparepurchase where brcode='$brcode' and partcode='$row[partcode]' order by entrydate desc");
if(mysqli_num_rows($sql12)<=0)
{
$sql12=mysqli_query($db,"select lstprice from spareprmast where partcode='$row[partcode]'");
}
$re12=mysqli_fetch_row($sql12);

if($re12[0]=='')
$re12[0]=0;

print "\n<td><center><input type=text name='price[]'  id='price$i' value='$re12[0]' class=right onKeyUp='validqtyp(this.id)' size=4 ></td>";

print "\n<td><center><input type=text  id='totprice$i' value=0 class=right size=4 ></td>";
print "<td>".datetohtml($row['date'])." </td>";
print "</tr>";
$i++;
}
}
print "<tr><th colspan=10><input type=submit value='Save' name='sv'> ".
"<input type=submit value='Preview' name='pv'> ";
print "</table>\n</form></fieldset>\n<br>";
}
}// endif show

?>
<center>
 
<form action='<?print $PHP_SELF?>' method='POST'>
<table border=0>
<tr><td>Branch</td><td>
<select name='brn'>
<?
 $brr=mysqli_query($db,"select brcode,brname from branch order by brname");
 while($brrr=mysqli_fetch_array($brr))
  {
    print "<option value='$brrr[0]'>$brrr[1]</option>";
  }
 print "<option>All</option>";
?>
</select></td></tr>
<tr><td colspan=2 align=center bgcolor='steelblue'><input type=submit value='Show' name='show'></td></tr>
</table>
</form>


