<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
include("../opendb.php");
include("../test.php");
$ordres=mysqli_query($db,"select max(orderno) from purorder_spare where brcode='$brcode'");
$ordrow=mysqli_fetch_row($ordres);
$ordnumber=$ordrow[0]+1;

//print $ordnumber;
?>
<link type="text/css" href="../jquery/jquery-ui.min.css" rel="stylesheet" />	
<script type="text/javascript" src="../jquery/jquery.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>

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

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

<link rel="stylesheet" href="../style.css" type="text/css" />
<style>

#tbl	{
      margin-top:70px;
    	}

.cont
	{




	background:url(images/yellow_back2.jpg) repeat;
  	width: 100%;position: absolute ;
	height:100% auto;
    	/* Optional - just for DIV)*/
    	//border: solid 1px #000000;
	-moz-box-shadow: 0px 5px 5px #888;
	-webkit-box-shadow: 0px 5px 5px #888;
	box-shadow: 0px 5px 10px #888;
    
	}
body
	{
	background: url(images/yellow_back2.jpg) repeat;
	}
#txt
	 {
	font-size:13px;
	}

	#popup td 
	{
	border-bottom:1px solid #CCCCCC;border-left:1px solid #CCCCCC;
	}
.del 	{
    	border: none;
   	background: url('img/delete.png') no-repeat ;
	padding: 0px 25px 20px 0;
    
	}
.edit {
   	 border: none;
    	background: url('img/edit.gif') no-repeat ;
	padding: 0px 25px 12px 0;
    
	}
.tb
	{
	-moz-box-shadow:    inset 1px 10px 10px grey;
  	 -webkit-box-shadow: inset 1px 10px 10px grey;
   	box-shadow:         inset 1px 10px 10px grey;
	}
.tb1
	{
	-moz-box-shadow:    inset 1px 10px 10px #565155;
   	-webkit-box-shadow: inset 1px 10px 10px #565155;
  	 box-shadow:         inset 1px 10px 10px #565155;
	width:100%;
	height:50px;
	font-size:11px;
	background:#2C2C30;
	}
  .tb1 a
	 {
  	text-decoration: none;
  	color:black;
	font-family: Arial, Helvetica;
    	}
</style>
<style>
	tr{background-color:white}
	th{background-color:steelblue;color:white}
</style>
<script type="text/javascript">
$(function() {

    $("#invoice_date").datepicker({
	dateFormat: "yy-mm-dd"
   });
 $("#supinvoice_date").datepicker({
	dateFormat: "yy-mm-dd"
   });

 });
  </script>
<script type="text/javascript">
var count1=1;
var count2=1;
var count3=1;
var pname='';
var pcode='';
var pname1=[];
var pcode1=[];
var empname=[];
var ndpcost=[];
$(function(){
$(".partcode").live("keydown.autocomplete",function(){
$partcode = $(this);
$row = $partcode.parent().parent();
$(this).autocomplete({
		source: function( request, response ) {

		$.ajax({
			url: "search2.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,
						
							
						
					}
				}));
			}
		});
	}
	
,
	minLength: 2,
	select: function( event, ui ) {
$row.find('.partcode').val(ui.item.value);
	$row.find('.prtname').val(ui.item.name);
	
	//alert(val(ui.item.rate));
	$row.find('.price').val(ui.item.rate);
	
	
	
	
	$row.find('.qty').show().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);
     };

					
});


//ch chk


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

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

		$.ajax({
			url: "searchch1.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


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

$partname = $(this);

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

		$.ajax({
			url: "search2.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);
     };

					
});
});





function sel(id1)
{

   document.getElementById(id1).select();
  
}




function showHint()
{
document.getElementById("popScroll2").innerHTML="<img src='images/loader.GIF' />";

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("popScroll2").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","purchviewinv1.php", true);
xmlhttp.send();
}



function viewinv(str)
{
document.getElementById("popScroll2").innerHTML="<img src='images/loader.GIF' />";


if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("popScroll2").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","purchviewinv.php?inno="+str,true);
xmlhttp.send();
}


/** code below for close popup div **/
function close_div(){
   var list = document.getElementsByClassName("pop-up2");
             for (var i = 0; i < list.length; i++) {
               list[i].style.display = 'none';
             }
    		var e = document.getElementById('pop2');
   		 if(e.style.display == 'block') {
       		 e.style.display = 'none';
   		 } else {
        	e.style.display = 'none';
              }

 }



 function check_empname(str,id1){

   var flg=0;
   for(var j=0;j<count1;j++){
alert(empname[j]);
       if(str==empname[j]){

         flg++;

       }//close of if cond.
  

   }//close of for loop.

   var len=str.length;

   if((!isNaN(str))&&len<=1){

   flg++;
   }
 
   if(flg<1){
    alert('Invalid employee');
    document.getElementById(id1).value='';
    document.getElementById(id1).focus();
    
   }
   
 }//function closed.

</script>

<style>
	tr{background-color:white}
	th{background-color:steelblue;color:white}
</style>
<script>
var pna=[];
var hand=[];
var price=[];
var disc=[];
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];

var act=parseFloat(document.f1.price.value)*parseFloat(document.f1.qty.value);
 var hnd=parseFloat(act)*document.f1.hand.value/100;

 var act1=act*parseFloat(document.f1.discount.value)/100;

 document.f1.aprice.value=parseFloat(act+hnd-act1).toFixed(2);
 act=document.f1.aprice.value;

 var act2=parseFloat(document.f1.aprice.value);
  var cst=parseFloat(act2)*document.f1.cst.value/100;
  var gtot=parseFloat(act2+cst).toFixed(2);
  document.f1.tot.value=gtot;
 }
}
function calcamt()
{
var act=parseFloat(document.f1.price.value)*parseFloat(document.f1.qty.value);
 var hnd=parseFloat(act)*document.f1.hand.value/100;

 var act1=act*parseFloat(document.f1.discount.value)/100;

 document.f1.aprice.value=parseFloat(act+hnd-act1).toFixed(2);
 act=document.f1.aprice.value;

 var act2=parseFloat(document.f1.aprice.value);
  var cst=parseFloat(act2)*document.f1.cst.value/100;
  var gtot=parseFloat(act2+cst).toFixed(2);
  document.f1.tot.value=gtot;
}
function valid()
{
 if(document.f1.prtname.value<=0)
 {
  alert('Invalid Part Name');
  return false;
 }
window.location.reload();
/*
var x=document.getElementById("mselect")
 document.f1.partcode1.value=x.options[x.selectedIndex].text
x.remove(x.selectedIndex)
*/
}
</script>
<body bgcolor='ivory'>
<center><br><br><h1>Purchase Order</h1>
<form name='f1' action='' method='POST' onSubmit='return valid()'>
<input type=hidden name='partcode1'>
<table>
<th colspan=10>ITEM PURCHASE ORDER (<?print $ordnumber?>)</th>
<tr>
<td>Part Code</td><td>
<input type=text name=partcode id=partcode  class='partcode' size=20 autocomplete='off' list='partcodelist' >


</td><td>Part Name </td><td COLSPAN=3>
<input type=text name=prtname id=prtname class='prtname' readonly size=30  autocomplete='off' list='partlist' onKeydown='return handleEnter(this, event)' >
</td></tr>
<tr><td>Chasis No Test </td><td><input type='text' name='chasis1' id='ordertoch' placeholder='Type chasis number Here' class='ordertoch' size=50></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></tr>
<tr><td>Qty</td><td>
<input type=text name='qty' class='qty' id=qty value='1' size=4 onKeydown='return handleEnter(this, event)' onKeyup='calc(this.value,this,this.id)'>
</td>
  <td>Price </td><td><input type=text  name='price' class='price' size=10></td>
  <td>Disc% </td><td><select name='discount'> 

    <option value='0'>0</option>
</select>


   Act Price <input type=text name='aprice' class='aprice' size=10>
</td></tr>
<tr><td>Hand Chrg%</td><td><input type=text name='hand' size=6 value='0'></td>
<?
// $cstres=mysqli_query($db,"select tax from taxmaster where type='cst'");
  //$cstrow=mysqli_fetch_row($cstres);
?>
<td>CST%</td><td><input type=text name='cst'  size=6 value='18'></td>
<td>Total</td><td><input type=text name='tot' class='tot' size=16></td></tr>


<tr><td colspan=6 align=center bgcolor='steelblue'><input type=submit value='Add' name='add' onclick=valid()></td></tr>
</table>
</form>
<?
if(isset($_POST['save']))
{

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

       while($ro=mysqli_fetch_array($re))
       {
             $sql="insert into purorder_spare (partcode,partname,qty,date,orderto,brcode,orderno) values('$ro[0]','$ro[1]','$ro[2]','$dt','$to','$brcode','$orderno')";
//echo $sql;
             mysqli_query($db,$sql) or die(mysqli_error($db));
        }
log_entry($brcode,$user,$REMOTE_ADDR,"Purchase Order Saved:$orderno,branch:$brcode",$db);

mysqli_query($db,"delete from $tbl");
print "<b>Purchase order ".$orderno." has been saved</B>";
include('pur1new.php');
}
?>
<form name='f1' action='<?echo $PHP_SELF?>' method='POST' onSubmit='return valid()'>
    
<table border=1 width=100%>
<tr><td><center>Order To : 
<?
$brres=mysqli_query($db,"select brcode,brname from branch where brcode!='$brcode' order by brname");
print "<select name='to'>";
while($brrow=mysqli_fetch_array($brres))
{
	print "<option value='$brrow[0]'>$brrow[1]</option>";
}
/*
$brres1=mysqli_query($db,"select distinct code,supname from sup_master order by supname");
while($brrow1=mysqli_fetch_array($brres1))
{
   print "<option value='$brrow1[0]'>$brrow1[1]</option>";
}
*/
print "</select>";
?>
&nbsp;
<input type=submit value='Save & Print' name='save'>
</td></tr></table>
</form>

<br>
<?
$tbl='suptmp'.$brcode.$user;
//print $tbl;
//mysqli_query($db,"drop table  $tbl");
$sql="CREATE TABLE IF NOT EXISTS $tbl (partcode varchar(100),prtname varchar(200), qty double(14,2), brcode varchar(100))";

//$sql="create table $tbl (partcode varchar(100),prtname varchar(200), qty 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)or die('err'.mysqli_error($db));

if($_REQUEST['add'])
{
    $partcode = $_REQUEST['partcode'];
    
    $res1=mysqli_query($db,"select * from $tbl where partcode='$partcode'");
    if(mysqli_num_rows($res1)>0)
    {
        print "<script> alert('Duplicate Entry')</script>";
        //return;
    }
    //print "select * from partmaster where partcode='$partcode'";
    else
    {
        $prtname = $_REQUEST['prtname'];
        $qty = $_REQUEST['qty'];
        $res2=mysqli_query($db,"select * from partmaster where partcode='$partcode'");
        $r=mysqli_fetch_array($res2);
        $prtname=$r['partname'];
        $sql="insert into $tbl values('$partcode','$prtname','$qty', '$brcode')" ;
        mysqli_query($db,$sql)or die('err'.mysqli_error($db));
    }
}

$res=mysqli_query($db,"select * from $tbl");
if(mysqli_num_rows($res)>=0)
{
  $i=1;
  print "<table border=1 cellpadding=3> <th>No <th>Item Code <th>Item Name <th>Qty <th> Del";
  while($row=mysqli_fetch_array($res))
  {
    print "\n<form action='$_SERVER[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>";
    print "<td><input type=submit value='Del' name='Del'></td></tr></form>";
  }
 print "</table>";
}

if(isset($_POST['Del']))
{
$partcode=$_REQUEST['partcode'];
 mysqli_query($db,"delete from $tbl where partcode='$partcode'")or die('err'.mysqli_error($db));
print "<script>alert('deleted...!!!');document.location='pur1new.php';</script>"; 
    
}
?>
<script>
 function valid1()
 {
  if(confirm('Do you want to save?'))
   return true;
  else
    return false;
 }
</script>
