<?
include_once('includes/top.php');
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
//$br=$_SESSION['branch'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
?>
<link href="css/form.css" type="text/css" rel="stylesheet" media="screen"  />
<html>
<body >
<?
if(isset($_POST['save']))
{
	

    $damqty=$_REQUEST['damqty'];
    $shopsoilqty=$_POST['shopsoilqty'];
    $cutpiecqty=$_POST['cutpiecqty'];
        $norqty=$_POST['norqty'];
        $itcode=$_POST['partcode'];
	$curqty=$_POST['curqty'];
        $curdt=date('Y-m-d');
	$sumqty=$damqty+$shopsoilqty+$cutpiecqty;
	
	if($norqty<=$curqty)
	{
	
	if($norqty==$sumqty)
		{
			$sql="insert into qtytransf(itemcode,nor_qty,dam_qty,shpsoil_qty,cuppic_qty,brcode,dataexchdt) values('$itcode','$norqty','$damqty','$shopsoilqty','$cutpiecqty','$brcode','$curdt')";			
			mysqli_query($db,$sql) or die('Insert Err:'.mysqli_error($db));

                        
			$sql1="update sparestock set curstk=(curstk-'$sumqty'),curstk_dam=(curstk_dam+'$damqty'),curstk_soil=(curstk_soil+'$shopsoilqty'),curstk_cut=(curstk_cut+'$cutpiecqty') where itemcode='$itcode' and brcode='$brcode'";
			//echo $sql1;
			mysqli_query($db,$sql1) or die('Insert Err:'.mysqli_error($db));
			print "<script> document.location='qtytransfer.php'</script>";
			return;
		}
	else
		{
			print "<script>alert('qty mismatch error!')</script>";	
		}

	}
else
	{
		
		print "<script>alert('qty mismatch error!')</script>";	
	}
}
?>
<title>Demo</title>
<link type="text/css" href="jquery/jquery-ui.min.css" rel="stylesheet" />	
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="functions_work.js"></script>
<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>
<link rel="stylesheet" href="style.css" type="text/css" />
<link type="text/css" href="includes/style1.css" rel="stylesheet" />
<script type="text/javascript">
$(function(){

$(".partcode").live("keydown.autocomplete",function(){
$partcode = $(this);

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

		$.ajax({
			url: "search5_con.php",
			dataType: "json",
			data: "key="+$partcode.val(),
			success: function( data ) {
				response( $.map( data, function( item ) {
					
					
					return { 
						label: '<span class="la-code">'+item.partcode+' </span><span class="la-name">'+  item.partname + '</span><span class="la-stok"> '+ item.stock +'</span><span class="la-rate"> '+item.rate +'</span>'    ,
						value: item.partcode,
						name:item.partname,
						tax:item.tax,
						
						stock:item.stock,
						rate:item.rate,
						reduct:item.reduct,
							
						
					}
				}));
			}
		});
	}
	
,
	minLength: 2,
	select: function( event, ui ) {
	$row.find('.partname').val(ui.item.name);
	$row.find('.stok').val(ui.item.stock);
	$row.find('.unitprice').val(ui.item.rate);
	document.getElementById("curqty").value=ui.item.stock;
	document.getElementById("norqty").focus()	;
	/*log( ui.item ?
	"Selected: " + ui.item.label :
	"Nothing selected, input was " + this.value);
	*/
	 }
	
	}).data("ui-autocomplete")._renderItem = function (ul, item) {
         return $("<li></li>")
             .data("item.autocomplete", item)
             .append( item.label )
             .appendTo(ul);
     };

					
});




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

$partname = $(this);

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

		$.ajax({
			url: "search5_con.php?type=name",
			dataType: "json",
			data: "key="+$partname.val(),
			success: function( data ) {
				response( $.map( data, function( item ) {
					return { 
						label: '<span class="la-code">'+item.partcode+' </span><span class="la-name">'+  item.partname + '</span><span class="la-stok"> '+ item.stock +'</span><span class="la-rate"> '+item.rate +'</span>'    ,
						code: item.partcode,
						value:item.partname,
						tax:item.tax,
						
						stock:item.stock,
						rate:item.rate,
						rate2:item.rate2,
						reduct:item.reduct,	
						
						
					}
	
				}));
			}
		});
	}
	
,
	minLength: 2,
	select: function( event, ui ) {

	$row.find('.partcode').val(ui.item.code);
	$row.find('.stok').val(ui.item.stock);
	$row.find('.unitprice').val(ui.item.rate);
	$row.find('.pur_rate').val(ui.item.rate2);
	$row.find('.tax').val(ui.item.tax);
	$row.find('.itmvl').val(ui.item.reduct);
	$row.find('.qty').focus();
		
	/*log( ui.item ?
	"Selected: " + ui.item.label :
	"Nothing selected, input was " + this.value);
	*/
	 }
	
	}).data("ui-autocomplete")._renderItem = function (ul, item) {
         return $("<li></li>")
             .data("item.autocomplete", item)
             .append( item.label )
             .appendTo(ul);
     };

					
});



});

</script>
<script type="text/javascript">

/***********************************************
* Disable "Enter" key in Form script- By Nurul Fadilah(nurul@REMOVETHISvolmedia.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
                
function handleEnter (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} 
		else
		return true;
	}      

</script>
<style>
th{background-color:steelblue;color:white}
table.form{
font-weight: normal;border:1px;
} 
</style>
<style>
#txt{
font-family: Tahoma,Verdana,Geneva,sans-serif !important;
background-color: #F2F2F2;

}
</style>
<body>
<?php //include('includes/top.php'); ?>
<form name='f1' action='<?print $PHP_SELF?>' onSubmit="return valid()" method='post'> 
<table class="container"><tr><td>
<font style="font-family: 'Trebuchet MS',Verdana,Helvetica,sans-serif; font-size:15px;">  &nbsp;&nbsp;Master >> Stock Conversion</font>
<br>

<center>
<table cellspacing=1 cellpadding=1 class=form>
<tr><td align=center bgcolor='white'>
<form name='f1' action='<?print $PHP_SELF?>' method='post' onSubmit="return valid()">
<table cellspacing=1 cellpadding=3 width='450'>
<th colspan=6><font size=3>QTY TRANSFER</th>

<tr>
<td>Item Code</td><td><input type=text name='partcode' id='partcode' class='partcode'></td>
</tr>
<tr>
<td>Current Stock</td><td><input type=text name='curqty' id='curqty' value='' readonly onKeyPress="return handleEnter(this, event)"></td>
</tr>
<tr>
<td>Conversion Qty</td><td><input type=text name='norqty' id='norqty' onKeyPress="return handleEnter(this, event)"></td>
</tr>
<tr>
<td>Damaged Qty</td><td><input type=text name='damqty' onKeyPress="return handleEnter(this, event)"></td>
</tr>
<tr>
<td>Shop Soiled Qty</td><td><input type=text name='shopsoilqty' onKeyPress="return handleEnter(this, event)"></td>
</tr>
<tr>
<td>CutPiece Qty</td><td><input type=text name='cutpiecqty' onKeyPress="return handleEnter(this, event)"></td>
</tr>
<th colspan=6><input type=submit value='Save' name='save'></td></tr>
</table>
</form>
</td></tr>
</table>
<br>
<table cellspacing=1 cellpadding=5 class=report>
<th>No <th>Item Code<th>Normal Qty<th>Damaged Qty<th>Shop Soiled Qty<th>CutPiece Qty<th>Del
<?
$i=1;
$res=mysqli_query($db,"select * from qtytransf  where brcode='$brcode' order by itemcode");
while($row=mysqli_fetch_array($res))
{
?>
<form name='form2' id='form2' method='POST'>
<?
 print "<tr bgcolor='white' onMouseOver=bgColor='b8d7b4'; onMouseOut=bgColor='white'>";
 print "<td>".$i++."</td><td>".$row[itemcode]."</td><td>".$row[nor_qty]."</td><td>".$row[dam_qty]."</td><td>".$row[shpsoil_qty]."</td><td>".$row[cuppic_qty]."</td>";
  //print "<td><input type=button value='Edit' name='ed' onClick=edtfun('$row[id]')></td>";
  print "<td><input type=button value='Del' name='del' onClick=delfun('$row[itemcode]','$row[dam_qty]','$row[shpsoil_qty]','$row[cuppic_qty]','$row[id]')></td></tr>";
}
?>
</form>
</table>
<script>
 function edtfun(x)
 {
   document.location='editqtytrns.php?code='+x;
 }
 function delfun(x,y,z,w,s)
 {
  if (confirm('do you want to delete '+x+' ?'))
      //var url='delqtytrns.php?code='+x+'&&dmqt='+y+'&&shpsl='+z+'&&ctpc='+w;

   document.location='delqtytrns.php?code='+x+'&&dmqt='+y+'&&shpsl='+z+'&&ctpc='+w+'&&rdid='+s;
 }
  
</script>
