<?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">Production</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>

								Production

							</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 type="text/javascript">

	//global var

	var maxRowNum = 1;

	var maxOutNum = 1;

	var selectedId = 0;

	

function update_qt() {



  	  var count=0;

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

	  

	  var row = $(this).parents('.product-row');

	  

	  //alert(row);

	  

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

	  

	  //alert(opt);

	  

	  count=count+opt;

	  

	  });

	  

	  //alert(count);

	

  var row = $(this).parents('.item-row');	  

  

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

  

  //alert(pro);

  

  var qt = row.find('.percentage').val() / 100;

  

  //alert(qt);

  

  qt = qt*count;

  

  //alert(qt);

    

  qt = qt.toFixed(2);



  row.find('.aq').val(qt);

  row.find('.mq').val(qt);



}



function bind() {

$(".percentage").blur(update_qt);

}	

	

$(document).ready(function() {



  $('input').click(function(){

    $(this).select();

  });

     

$('#addrow').click(function(){

    	maxRowNum++;

        var htmlString = "";

        htmlString += '<tr class="item-row">';

		

		htmlString += '<td id="page" class="item-name" style="text-align:center; padding-top:10px;"><a class="delete" href="javascript:;" title="Remove row" style="color:#FF0000;">X</a> &nbsp; <input type="hidden" name="rno[]" class="rno" value="'+ maxRowNum + '" />';

        htmlString += '<select name="inputcategory['+ maxRowNum + ']" class="inputcategory" style="width:200px;"><option value="">Select Category</option> <?php $proc=mysql_query("select * from product_category order by category asc"); while($proc1=mysql_fetch_array($proc)) { echo'<option value="'.$proc1['ID'].'">'.$proc1['category'].'</option>'; } ?> </select>';

        htmlString += '</td>';

		

        htmlString += '<td style="text-align:center; padding-top:10px;"> <select name="inputproduct['+ maxRowNum + ']" class="inputproduct" style="width:220px;"><option value="" selected="selected">Select Product</option></select>';

		

		htmlString += '<td style="width:11%; padding-top:10px; text-align:center;"><input type="text" style="width:100px;" readonly="true" class="stock" readonly="true" /></td>';

		

		htmlString += '<td style="width:11%; padding-top:10px; text-align:center;"><input type="text" style="width:100px;" name="inputqty['+ maxRowNum + ']" class="inputqty" autocomplete="off" /></td>';

		

		htmlString += '</tr>';



     $(".item-row:last").after(htmlString);

	 

	if ($(".delete").length > 0) $(".delete").show();

    bind();

  });

  

  

$('#addoutput').click(function(){

    	maxOutNum++;

        var htmlString = "";

        htmlString += '<tr class="product-row">';

        htmlString += '<td style="padding-left:15px; padding-top:10px;"><a class="del" href="javascript:;" title="Remove row" style="color:#FF0000;">X</a> &nbsp; <input type="hidden" name="sn[]" class="rnno" value="'+ maxOutNum + '" /> <select name="outputcategory['+ maxOutNum +']" class="outputcategory" style="width:230px;"><option value="">Select Category</option> <?php $proca=mysql_query("select * from product_category order by category asc"); while($proca1=mysql_fetch_array($proca)) { echo'<option value="'.$proca1['ID'].'">'.$proca1['category'].'</option>'; } ?> </select></td>';

		

		htmlString += '<td style="padding-left:15px; padding-top:10px;"><select name="outputproduct['+ maxOutNum +']" class="outputproduct" style="width:230px;"><option value="">Select Product</option></select></td>';

		

		htmlString += '<td style="text-align:center; padding-top:10px;"><input type="text" style="width:80px;" class="op" name="outputqty['+ maxOutNum +']" autocomplete="off" /></td>';

		

		htmlString += '</tr>';



     $(".product-row:last").after(htmlString);

	 

	if ($(".del").length > 0) $(".del").show();

    bind();

  });  

  

  

  

  bind();

  

});

 

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

  

  var row = $(this).parents('.item-row');

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

		  

 var dataString = 'category='+ category;

 //alert(dataString);

    $.ajax

({

type: "GET",

url: "getproduct.php",

data: dataString,

cache: false,

success: function( resp )

{

	//alert(resp);

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



}

});

}); 





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

  

  var row = $(this).parents('.item-row');

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

  var factory=$('.factory').val();

		  

 var dataString = 'product='+ product +'&factory='+ factory;

 //alert(dataString);

    $.ajax

({

type: "GET",

url: "getstock.php",

data: dataString,

cache: false,

success: function( html )

{

	

	//alert(html);

	

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

	

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

}

});

}); 



  



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

  

  var row = $(this).parents('.product-row');

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

		  

 var dataString = 'category='+ category;

 //alert(dataString);

    $.ajax

({

type: "GET",

url: "getproduct.php",

data: dataString,

cache: false,

success: function( resp )

{

	//alert(resp);

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



}

});

});



   

 $(".delete").live('click',function(){

    $(this).parents('.item-row').remove();

    update_total();

    if ($(".delete").length < 2) $(".delete").hide();

  });

  

 $(".del").live('click',function(){

    $(this).parents('.product-row').remove();

    update_total();

    if ($(".del").length < 2) $(".del").hide();

  });  

  

  

//});



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

			};

		});

		

		return valid;

		

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

		

	});

	

});	



</script>

						

                        

<?php



if(isset($_POST['submit']))

{

	$factory=$_POST['factory'];

	$pno=$_POST['pno'];

	$inputproduct=$_POST['inputproduct'];

	$inputqty=$_POST['inputqty'];

	$outputproduct=$_POST['outputproduct'];

	$outputqty=$_POST['outputqty'];

	$date=$_POST['date'];

	$sno=$_POST['sno'];

	$rno=$_POST['rno'];

	$fpno=$_POST['fpno'];

	

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

	

	$chk=mysql_query("select COUNT(ID) from production where pno='$pno'");

	$chk1=mysql_result($chk,0);	

	if($chk1>0)

	{

		$new=mysql_query("select MAX(pno) as pno from production");

		$new1=mysql_fetch_array($new);

		

		$pno=$new1['pno']+1;

	}

	else

	{

		$pno=$pno;

	}

	

	

		$chek=mysql_query("select count(ID) from production where financial_pno='$fpno' and financial='$financial'");

		$chek1=mysql_result($chek,0);

		if($chek1>0)

		{

		$chck=mysql_query("select max(financial_pno) as fpno from production where financial='$financial'");

		$chck1=mysql_fetch_array($chck);

		$fpno=$chck1['fpno']+1;

		}

		else

		{

		$fpno=$fpno;

		}

	

	

		$v1=count($rno);

		$v2=count($sno);

		

		if($v1>$v2)

		{

		$rw=$rno;

		}

		elseif($v2>$v1)

		{

		$rw=$sno;

		}

		else

		{

		$rw=$rno;

		}

	

	

	foreach($rw as $no)

	{

		$inputpro=$inputproduct[$no];

		$inputq=$inputqty[$no];

		$outputpro=$outputproduct[$no];

		$outputq=$outputqty[$no];

				

		$ins=mysql_query("insert into production(date,factory,input_product,input_qty,output_product,output_qty,pno,financial_pno,financial) values('$date','$factory','$inputpro','$inputq','$outputpro','$outputq','$pno','$fpno','$financial')");

	

	}

	

	if($ins)

	{

		header("location:production.php?s=1");

	}

	else

	{

		header("location:production.php?s=2");

	}

	

}

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>

					Production Added Successfully.

			  </div>';

		 }

		 elseif($status=='2')

		 {

		 echo '<div class="alert alert-error">

				<a class="close" data-dismiss="alert">x</a>

					Values not inserted. Try again.

			  </div>';

		 }		

	}



$prod=mysql_query("select MAX(pno) as pno from production");

$prod1=mysql_fetch_array($prod);

$npno=$prod1['pno']+1;



$nmx=mysql_query("select max(financial_pno) as fpno from production where financial='$financial'");

$nmx1=mysql_fetch_array($nmx);

$neword=$nmx1['fpno']+1;



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



<table style='width:75%; margin:auto;'>



<tr><td valign='bottom'>



<table style='line-height:35px;'>



<tr><td>Factory <span class='red'>*</span></td><td style='padding:0 11px 0 11px;'>:</td><td><select name='factory' class='factory' id='required' title='*' 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>Pno <span class='red'>*</span></td><td style='padding:0 11px 0 11px;'>:</td><td>

<input type='hidden' name='pno' id='required' title='*' readonly='true' value='".$npno."' />

<input type='text' name='fpno' id='required' title='*' readonly='true' value='".$neword."' /></td></tr>



</table>



</td>





<td valign='bottom' style='padding-bottom:2px; text-align:center;'>



<table style='margin:auto;'>



<tr><td>Date <span class='red'>*</span></td><td style='padding:0 11px 0 11px;'>:</td><td><input type='text' name='date' id='required' title='*' class='datepicker' autocomplete='off' /></td></tr>



</table>



</td></tr>



</table><br />";



//products

		

echo'



<center><b style="color:#158F01;">Output Products</b></center>



<table style="width:60%; margin:7px auto; border-color:#CCC; border:1px solid #CCC;">



<tr style="background-color:#EAF5F8;">

<th style="width:40%; height:25px; border:1px solid #CCC; text-align:center; border-right:none;">Category</th>

<th style="width:40%; height:25px; border:1px solid #CCC; text-align:center; border-left:none; border-right:none;">Product</th>

<th style="width:20%; border:1px solid #CCC; text-align:center; border-left:none; border-right:none;">Output</th></tr>';



echo'<tr class="product-row">



<td style="padding-left:15px; padding-top:10px;">



<a href="javascript:;" title="Remove row" style="color:#FF0000;">X</a> &nbsp; <input type="hidden" name="sn[]" value="1" class="rnno" /> 



<select name="outputcategory[1]" class="outputcategory" style="width:230px;"><option value="">Select Category</option>';



$pro=mysql_query("select * from product_category order by category asc");

while($pro1=mysql_fetch_array($pro))

{

	echo"<option value='".$pro1['ID']."'>".$pro1['category']."</option>";

}



echo'</select>



</td>



<td style="padding-left:15px; padding-top:10px;">



<select name="outputproduct[1]" class="outputproduct" style="width:230px;"><option value="" selected="selected">Select Product</option></select></td>



<td style="text-align:center; padding-top:10px;"> <input type="text" style="width:80px; margin-top:8px;" class="op" name="outputqty[1]" autocomplete="off" /></td>



<tr><td colspan="3" style="padding-left:15px; padding-top:7px; padding-bottom:7px;"><a id="addoutput" href="javascript:;" title="Add a row" style="color:#003399;">Add Many</a></td></tr>';



echo'</table><br /><br />







<center><b style="color:#FF0000;">Input Products</b></center>

<table style="width:75%; margin:7px auto; border-color:#CCC; border:1px solid #CCC;">

<tr style="background-color:#EAF5F8;"><th style="width:23%; height:25px; text-align:center; border:1px solid #CCC; border-right:none;">Category</th>

<th style="width:23%; height:25px; border:1px solid #CCC; text-align:center; border-left:none; border-right:none;">Product</th>

<th style="width:11%; border:1px solid #CCC; border-left:none; text-align:center; border-right:none;">Stock</th>

<th style="width:11%; border:1px solid #CCC; border-left:none; text-align:center; border-right:none;">Input Qty</th></tr>';

?>





<tr class="item-row">



<td style="padding-top:10px; text-align:center;"><a href='javascript:;' title='Remove row' style='color:#FF0000;'>X</a> &nbsp; 

<select name="inputcategory[1]" class="inputcategory" style="width:200px;"><option value="">Select Category</option>



<?php



$proc=mysql_query("select * from product_category order by category asc");

while($proc1=mysql_fetch_array($proc))

{

	echo"<option value='".$proc1['ID']."'>".$proc1['category']."</option>";

}



?>



</select></td>



<td style="padding-top:10px; text-align:center;"><input type="hidden" class="rno" name="rno[]" value="1" />

 <select type="text" name="inputproduct[1]" class="inputproduct" style="width:220px;"><option value="" selected="selected">Select Product</option>

 </select></td>

<?php

echo"<td style='width:11%; text-align:center; padding-top:10px;'><input type='text' style='width:100px;' readonly='true' class='stock' /></td>

<td style='width:11%; padding-top:10px; text-align:center; border:none;'><input type='text' style='width:100px;' class='inputqty' name='inputqty[1]' autocomplete='off' /></td>";

?>

</tr>



<tr><td colspan="4" style="padding-left:30px; padding-top:7px; padding-bottom:7px;"><a id="addrow" href="javascript:;" title="Add a row" style="color:#003399;">Add Many</a></td></tr>



<?php



echo'</table><br /><br />



</td></tr></table><br />



<center><button type="submit" name="submit" value="submit" class="btn btn-sm btn-primary">Save</button></center>



</form>';



}



?>

                        		



								<!-- 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();

?>