<?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));

	

		$v1=count($rno);

		$v2=count($sno);

		

		if($v1>$v2)

		{

		$rw=$rno;

		}

		elseif($v2>$v1)

		{

		$rw=$sno;

		}

		else

		{

		$rw=$rno;

		}

		

		$del=mysql_query("delete from production where pno='$pno'");	

	

	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:viewproduction.php?s=3");

	}

	else

	{

		header("location:viewproduction.php?s=4");

	}

	

}

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

		 }		

	}



$id=$_GET['id'];



$prodc=mysql_query("select * from production where pno='$id'");

$prodc1=mysql_fetch_array($prodc);



echo"<form action='editproduction.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))

{



$pfac="<option value='".$fac1['ID']."'>".$fac1['factory']."</option>";

$pfac=str_replace("<option value='".$prodc1['factory']."'","<option value='".$prodc1['factory']."' selected",$pfac);

echo $pfac;

	

}



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='".$id."' />

<input type='text' name='fpno' id='required' title='*' readonly='true' value='".$prodc1['financial_pno']."' /></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' value='".date('d-m-Y',strtotime($prodc1['date']))."' /></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>';



$sno=0;

$det=mysql_query("select * from production where pno='$id'");

while($det1=mysql_fetch_array($det))

{

$sno=$sno+1;



$opq=mysql_query("select * from product where ID='$det1[output_product]'");

$opq1=mysql_fetch_array($opq);



$opcy=$opq1['category'];



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="sno['.$sno.']" value="'.$sno.'" class="rnno" /> 



<select name="outputcategory['.$sno.']" 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))

{

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

	$opd=str_replace("<option value='".$opcy."'","<option value='".$opcy."' selected",$opd);

	echo $opd;

}



echo'</select>



</td>



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



<select name="outputproduct['.$sno.']" class="outputproduct" style="width:230px;"><option value="" selected="selected">Select Product</option>';



$prodn=mysql_query("select * from product where category='$opcy'");

while($prodn1=mysql_fetch_array($prodn))

{

	$dprod="<option value='".$prodn1['ID']."'>".$prodn1['product']."</option>";

	$dprod=str_replace("<option value='".$det1['output_product']."'","<option value='".$det1['output_product']."' selected",$dprod);

	echo $dprod;

}



echo'</select></td>



<td style="text-align:center; padding-top:10px;"> <input type="text" style="width:80px; margin-top:8px;" class="op" name="outputqty['.$sno.']" value="'.$det1['output_qty'].'" autocomplete="off" /></td>



</tr>';



}



?>



				  <script>

		

				  maxOutNum ="<?PHP echo ($sno); ?>";

		

				  </script>



<?php



echo'<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>';



$ssno=0;

$details=mysql_query("select * from production where pno='$id'");

while($details1=mysql_fetch_array($details))

{

$ssno=$ssno+1;	



$ipq=mysql_query("select * from product where ID='$details1[input_product]'");

$ipq1=mysql_fetch_array($ipq);



$ipcy=$ipq1['category'];





$pur=mysql_query("select sum(qty) as qty from purchase where product='$details1[input_product]' and factory='$prodc1[factory]'");

$pur1=mysql_fetch_array($pur);



$sal=mysql_query("select sum(qty) as qty from invoice where product='$details1[input_product]' and factory='$prodc1[factory]'");

$sal1=mysql_fetch_array($sal);



$prodc=mysql_query("select sum(input_qty) as qty from production where input_product='$details1[input_product]' and factory='$prodc1[factory]'");

$prodc1=mysql_fetch_array($prodc);



$prodct=mysql_query("select sum(output_qty) as qty from production where output_product='$details1[input_product]' and factory='$prodc1[factory]'");

$prodct1=mysql_fetch_array($prodct);



$bal=$pur1['qty']-$sal1['qty']-$prodc1['qty']+$prodct1['qty'];



$bal=$bal+$details1['input_qty'];





echo'<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['.$ssno.']" class="inputcategory" style="width:200px;"><option value="">Select Category</option>';



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

while($proc1=mysql_fetch_array($proc))

{

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

	$ipd=str_replace("<option value='".$ipcy."'","<option value='".$ipcy."' selected",$ipd);

	echo $ipd;

}



echo'</select></td>



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

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

 

 $spro=mysql_query("select * from product where category='$ipcy'");

 while($spro1=mysql_fetch_array($spro))

 {

	 $detlpr="<option value='".$spro1['ID']."'>".$spro1['product']."</option>";

	 $detlpr=str_replace("<option value='".$details1['input_product']."'","<option value='".$details1['input_product']."' selected",$detlpr);

	 echo $detlpr;

 }

 

 echo'</select></td>

 

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



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



</tr>';



}



?>



				  <script>

		

				  maxRowNum ="<?PHP echo ($ssno); ?>";

		

				  </script>



<?php





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





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

?>