<?PHP
ob_start();
session_start();
include 'connect.php';
if(isset($_SESSION['adminemail']))
{
$user=$_SESSION['adminemail'];

$gtp=mysql_query("select * from admin where username='$user'");
$gtp1=mysql_fetch_array($gtp);

include 'top.php';
?>



<div class="main-content">
				<div class="main-content-inner">
					<div class="breadcrumbs ace-save-state" id="breadcrumbs">
						<ul class="breadcrumb">
							<li>
								<i class="ace-icon fa fa-home home-icon"></i>
								<a href="#">Home</a>
							</li>
							<li class="active">Purchase Order</li>
						</ul><!-- /.breadcrumb -->
					</div>

					<div class="page-content">
			

						<div class="page-header">
                        
<?php

$aca=mysql_query("select * from financial where user='$user' order by ID desc");
$aca1=mysql_fetch_array($aca);

$financial=$aca1['financial'];

?>                        
                        
							<table style="width:100%;">
                        
                        <tr><td style="width:50%;">
                        
							<h1>
								Purchase Order
							</h1>
                            
                            </td>
                            
                            <td style="width:50%; text-align:right; padding-right:15px; font-size:15px;">
                            
                            <i class="fa fa-calendar"></i>&nbsp; <?php echo $financial; ?>
                            
                            </td></tr></table>
                            
						</div><!-- /.page-header -->
<br />

						<div class="row">
							<div class="col-xs-12">
                            
								<!-- PAGE CONTENT BEGINS -->
                                
<script src="assets/js/jquery.js"></script>

<script>
	
jQuery(function($){
		   
	// simple jQuery validation script
	$('#work').submit(function(){
		
		var valid = true;
		var errormsg = 'This field is required!';
		var errorcn = 'error';
		
		$('.' + errorcn, this).remove();			
		
		$('#required', this).each(function(){
			var parent = $(this).parent();
			if( $(this).val() == '' ){
				var msg = $(this).attr('title');
				msg = (msg != '') ? msg : errormsg;
				$('<span style="font-size:13px;color:#FF0000; padding-left:3px;" class="'+ errorcn +'">'+ msg +'</span>')
					.appendTo(parent)
					.fadeIn('fast')
					.click(function(){ $(this).remove(); });
				valid = false;
			};
		});
		
		
		//All Fields Empty

	

	  var len=$('.item-row').length;

	  var r=0;

	  $('.row').each(function(i){

	  

	  var row = $(this).parents('.worker_row');

	  var category = row.find('.category').val();
	  
	  var product = row.find('.product').val();

	  var qty = row.find('.qty').val()*1;

	  var rate = row.find('.rate').val()*1;

	  

	  //alert(rn);

	  
	  if(category=='')

	  {

	  alert('Select Category');

	  

	  valid=false;

	  

	  }

	  else if(product=='')

	  {

	  alert('Select Product');

	  

	  valid=false;

	  

	  }

	  else if(qty=='')

	  {

	  alert('Enter qty');

	  

	  valid=false;

	  

	  }

	  else if(rate=='')

	  {

	  alert('Enter rate');

	  

	  valid=false;	  

	  }

		

	  });
		
		
		return valid;
		
		$('button:submit').prop('disabled', true);
		
	});
		
});	

</script>

<script type="text/javascript">

	//global var
	var maxRowNum = 1;
	var selectedId = 0;

function update_total() {

  var total = 0;

  $('.price').each(function(i){

    price = $(this).html().replace("Rs.","");

    if (!isNaN(price)) total += Number(price);

  });



  total = total.toFixed(2);

  //alert(total);

  $('.samount').html("Rs."+total);
  

}

function update_price() {

  var row = $(this).parents('.worker_row');

  

  var price = row.find('.rate').val().replace("Rs.","") * row.find('.qty').val();

  

  //alert(price);

  

  var cgst=row.find('.cgst').val()*1;

  var sgst=row.find('.sgst').val()*1;

  var igst=row.find('.igst').val()*1;

   

  var p1=price*cgst/100;

  var p2=price*sgst/100;

  var p3=price*igst/100;

  

  //alert(p1);

  

  var totalpr=price+p1+p2+p3;

  

  totalpr=totalpr.toFixed(2);

  

  isNaN(totalpr) ? row.find('.price').html("N/A") : row.find('.price').html(totalpr);

  update_total();

}


function bind() {

  $(".rate").blur(update_price);

  $(".qty").blur(update_price);

  $(".cgst").blur(update_price);

  $(".sgst").blur(update_price);

  $(".igst").blur(update_price);
  
}

$(document).ready(function() {


bind();


$("#addrow").click(function(){
maxRowNum++;
//alert($max);
    $(".worker_row:last").after("<tr align='center' class='worker_row'><td id='page' class='item-name' style='padding-top:10px;'><input type='hidden' name='row["+ maxRowNum +"]' value='"+ maxRowNum +"' class='row'><div class='delete-wpr'>&nbsp;<a class='delete' href='javascript:;' title='Remove row'>X </a></div></td><td style='padding-top:10px;'><select name='category["+ maxRowNum +"]' class='category' style='width:170px;'><option value=''>Select Category</option><?php $cate=mysql_query("select * from product_category order by category asc"); while($cate1=mysql_fetch_array($cate)){ echo"<option value='".$cate1['ID']."'>".$cate1['category']."</option>"; } ?></select></td><td style='padding-top:10px;'><select name='product["+ maxRowNum +"]' class='product' style='width:170px;'><option value='' selected='selected'>Select Product</option></select></td><td style='padding-top:10px;'><input type='text' name='unit["+ maxRowNum +"]' class='unit' style='width:50px;' placeholder='Unit' autocomplete='off' readonly='true' /></td><td style='padding-top:10px;'><input type='text' name='qty["+ maxRowNum +"]' class='qty' style='width:50px;' placeholder='Qty' autocomplete='off' /></td><td style='padding-top:10px;'><input type='text' name='rate["+ maxRowNum +"]' class='rate' style='width:50px;' placeholder='Rate' autocomplete='off' /></td><td style='padding-top:10px;'><input type='text' name='cgst["+ maxRowNum +"]' style='width:45px;' class='cgst' placeholder='CGST' autocomplete='off' /></td><td style='padding-top:10px;'><input type='text' name='sgst["+ maxRowNum +"]' style='width:45px;' class='sgst' placeholder='SGST' autocomplete='off' /></td><td style='padding-top:10px;'><input type='text' name='igst["+ maxRowNum +"]' style='width:45px;' class='igst' placeholder='IGST' autocomplete='off' /></td><td style='padding-top:10px;'><span class='price'>0.00</span></td></tr>");
	
    if ($(".delete").length > 0) $(".delete").show();
	    bind();
		
		//alert("ds");
		
		  });
		  
		  
bind();

});


$(".category").live('change',function(){  

  var row = $(this).parents('.worker_row');

  var category=row.find('.category').val();		  

 var dataString = 'category='+ category;

 //alert(dataString);

    $.ajax

({

type: "GET",

url: "getproduct.php",

data: dataString,

cache: false,

success: function( resp )

{

	//alert(resp);

	row.find('.product').html($('#material',resp).html());

}

});

});


    $(".product").live('change',function(){

var row = $(this).parents('.worker_row');

 var product=row.find('.product').val();

 var dataString = 'product='+ product;

//alert(dataString);

   $.ajax

({

type: "GET",

url: "getrate.php",

data: dataString,

cache: false,

success: function(html)

{

//alert(html);	

var unit=$(html).filter('#unit').text();

row.find('.unit').val(unit);

}

});

});


$(".delete").live('click',function(){
    $(this).parents('.worker_row').remove();
    update_total();
    if ($(".delete").length < 2) $(".delete").hide();
  });

</script>
								
<?php
	
if(isset($_POST['submit']))
{
		$date=$_POST['date'];

		$supplier=$_POST['supplier'];

		$orderno=$_POST['orderno'];
		
		$ordno=$_POST['ordno'];

		$product=$_POST['product'];
		
		$code=$_POST['code'];

		$hsn=$_POST['hsn'];

		$rate=$_POST['rate'];

		$qty=$_POST['qty'];

		$discount=$_POST['discount'];

		$cgst=$_POST['cgst'];

		$sgst=$_POST['sgst'];

		$igst=$_POST['igst'];

		$row=$_POST['row'];
		
		$factory=$_POST['factory'];

		$date=date('Y-m-d',strtotime($date));


		$csk=mysql_query("select count(ID) from purchase_order where orderno='$orderno'");
		$csk1=mysql_result($csk,0);
		if($csk1>0)
		{
		$chk=mysql_query("select max(orderno) as orderno from purchase");
		$chk1=mysql_fetch_array($chk);
		$orderno=$chk1['orderno']+1;
		}
		else
		{
		$orderno=$orderno;
		}
		
		
		
		$chek=mysql_query("select count(ID) from purchase where ordno='$ordno' and financial='$financial'");
		$chek1=mysql_result($chek,0);
		if($chek1>0)
		{
		$chck=mysql_query("select max(ordno) as ordno from purchase where financial='$financial'");
		$chck1=mysql_fetch_array($chck);
		$ordno=$chck1['ordno']+1;
		}
		else
		{
		$ordno=$ordno;
		}


if($supplier)
{

foreach($row as $rw)
{

				$prod=$product[$rw];

				$cos=$rate[$rw];

				$qt=$qty[$rw];

				$dis=$discount[$rw];

				$cgs=$cgst[$rw];

				$sgs=$sgst[$rw];

				$igs=$igst[$rw];

				

				$totalrate=$cos*$qt;

				

				$totalrate=$totalrate;

				

				$cgsam=$totalrate*$cgs/100;

				$sgsam=$totalrate*$sgs/100;

				$igsam=$totalrate*$igs/100;

				

				$finalrate=$totalrate+$cgsam+$sgsam+$igsam;


$insert="INSERT into purchase_order(date,supplier,product,rate,qty,total,orderno,cgst,sgst,igst,cgst_amt,sgst_amt,igst_amt,ordno,financial,factory)

values('$date','$supplier','$prod','$cos','$qt','$finalrate','$orderno','$cgs','$sgs','$igs','$cgsam','$sgsam','$igsam','$ordno','$financial','$factory')";

mysql_query($insert) or die(mysql_error());

}

}


if($insert){
	
header("location:viewpurchaseorder.php?id=".$orderno."&s=1" . urlencode($Message));

}

else{

echo "<script type='text/javascript'>alert('Field Missing.');</script><META HTTP-EQUIV = 'Refresh' Content = '1; URL =purchaseorder.php'>";

}

}

else
{
if (isset($_GET['s'])) 
	{
	 $status=$_GET['s'];
		 if($status=='1')
		 {
		 echo '<div class="alert alert-success">
				<a class="close" data-dismiss="alert">x</a>
					<strong>Purchase Created Successfully !</strong>
			  </div>';
		}
	}
	
$mx=mysql_query("select MAX(orderno) as orderno from purchase_order") or die(mysql_error());
$mx1=mysql_fetch_array($mx);
$neworder=$mx1['orderno']+1;

$nmx=mysql_query("select MAX(ordno) as ordno from purchase_order where financial='$financial'") or die(mysql_error());
$nmx1=mysql_fetch_array($nmx);
$neword=$nmx1['ordno']+1;

echo '<form action="purchaseorder.php" method="post" id="work">

<center><table width="85%" id="page" style="line-height:35px;">

<tr><td style="width:13%;">Order No. <span class="red">*</span></td><td>: <input type="hidden" name="orderno" id="required" title="*" value="'.$neworder.'" readonly="true" /><input type="text" name="ordno" id="required" title="*" value="'.$neword.'" readonly="true" />

</td><td style="width:75px;"></td><td>Date <span class="red">*</span></td><td>: <input type="text" name="date" id="required" title="*" class="datepicker" autocomplete="off" /></td></tr>

<tr><td colspan="3">';

echo'</td><td style="width:55px;"></td><td></td></tr>

<tr><td>Supplier <span class="red">*</span></td><td>: <select name="supplier" class="supplier chosen" id="required" title="*" style="width:220px;"><option value="">Select Supplier</option>';

$sup=mysql_query("select * from supplier order by name asc");
while($sup1=mysql_fetch_array($sup))
{
	echo"<option value='".$sup1['ID']."'>".$sup1['name']."</option>";
}

echo'</select></td><td style="width:55px;"></td><td>Factory</td><td>: <select name="factory" style="width:220px;"><option value="">Select Factory</option>';

$fac=mysql_query("select * from factory order by ID asc");
while($fac1=mysql_fetch_array($fac))
{
	echo"<option value='".$fac1['ID']."'>".$fac1['factory']."</option>";
}

echo'</select></td></tr>

<tr><td style="width:13%;"></td><td></td>

<td style="width:75px;"></td><td></td><td></td></tr>

</table><br />

<br />

<br><table width="90%" style="line-height:25px; border:1px solid #dddddd; font-size:12px;">
			<tr height="25" bgcolor="#eaf5f8" border="1">
			<th width="3%"></th>
	
			<th style="width:10%; text-align:center;">Category</th>
		
			<th style="width:15%; text-align:center;">Product</th>
			
			<th style="width:7%; text-align:center;">Unit</th>
			
			<th style="width:7%; text-align:center;">Qty</th>
		
			<th style="width:7%; text-align:center;">Rate</th>
		
			<th style="width:7%; text-align:center;">CGST (%)</th>
		
			<th style="width:7%; text-align:center;">SGST (%)</th>
		
			<th style="width:7%; text-align:center;">IGST (%)</th>
		
			<th style="width:7%; text-align:center;">Amount</th>
			
			</tr>
			
			  <tr class="worker_row" align="center" style="margin-top:10px;">

		        <td id="page" style="padding-top:11px;"><a class="delete" href="javascript:;" title="Remove row">X</a></td>

				<td id="page" style="padding-top:11px;"><input type="hidden" name="row[1]" class="row" value="1" />
				
				<select name="category[1]" class="category" style="width:170px;"><option value="">Select Category</option>';
				
				$cate=mysql_query("select * from product_category order by category asc");
				while($cate1=mysql_fetch_array($cate))
				{
					echo"<option value='".$cate1['ID']."'>".$cate1['category']."</option>";
				}
				
				echo'</select></td>
		
				<td id="page" style="padding-top:11px;"><select name="product[1]" class="product" style="width:170px;"><option value="" selected="selected">Select Product</option></select></td>
				
				<td id="page" style="padding-top:11px;"><input type="text" name="unit[1]" class="unit" style="width:50px;" placeholder="Unit" autocomplete="off" readonly="true" /></td>
				
				<td id="page" style="padding-top:11px;"><input type="text" name="qty[1]" class="qty" style="width:50px;" placeholder="Qty" autocomplete="off" /></td>
		
				<td id="page" style="padding-top:11px;"><input type="text" name="rate[1]" class="rate" style="width:50px;" placeholder="Rate" autocomplete="off" /></td>
				
				<td id="page" style="padding-top:11px;"><input type="text" name="cgst[1]" style="width:45px;" class="cgst" placeholder="CGST" autocomplete="off" /></td>
		
				<td id="page" style="padding-top:11px;"><input type="text" name="sgst[1]" style="width:45px;" class="sgst" placeholder="SGST" autocomplete="off" /></td>
		
				<td id="page" style="padding-top:11px;"><input type="text" name="igst[1]" style="width:45px;" class="igst" placeholder="IGST" autocomplete="off" /></td>
		
				<td id="page" style="padding-top:11px;"><span class="price">0.00</span></td>
			  
			  </tr>

			  <tr id="hiderow">
			  
		      <td id="page" colspan="9"><a id="addrow" href="javascript:;" title="Add a row">Add Row</a></td>
			  
		      </tr>
			 
			  </table>
			  
			  <br /><br />
			  
			  <table style="width:85%; margin:auto;">
			  
			  <tr><td style="width:80%;">
			  
			  
			  </td>
			  
			  <td style="width:20%;">
			  
			  <table style="line-height:35px;">
			  
			  <tr><td>Total</td><td style="padding:0 7px 0 7px;">:</td><td><div class="samount"></div></td></tr>
			  
			  <tr><td colspan="3" style="text-align:center;"><button id="save" type="submit" class="btn btn-sm btn-primary" name="submit" value="Add">Save</button></td></tr>
			  
			  </table>
			  
			  </td></tr></table>
			  
</center>

<br />

</form>';

}
	
?>                             
                               
							<br>
<br>
	

								<!-- PAGE CONTENT ENDS -->
							</div><!-- /.col -->
						</div><!-- /.row -->
					

</div><!-- /.page-content -->
				</div>
			</div><!-- /.main-content -->


<?php
include 'bottom.php';
}
else
{

 header("location:login.php");
}
ob_flush();
?>