<?PHP

ob_start();

session_start();

include 'connect.php';

if(isset($_SESSION['adminemail']))

{

$user=$_SESSION['adminemail'];



$gtp=mysqli_query($conn,"select * from admin where username='$user'");

$gtp1=mysqli_fetch_assoc($gtp);



include 'top.php';

?>






<div id="page-wrapper" >
          <div id="page-inner">
			<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">Stock</li>

						</ul><!-- /.breadcrumb -->

					</div>



					<div class="page-content">

			



						<div class="page-header">

                        

<?php



$aca=mysqli_query($conn,"select * from financial where user='$user' order by ID desc");

$aca1=mysqli_fetch_assoc($aca);



$financial=$aca1['financial'];



?>

                        

                        <table style="width:100%;">

                        

                        <tr><td style="width:50%;">

                        

							<h1>

								Stock

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



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

			};

		});

		

		return valid;

	});

	

});	



</script>







<?php



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

{

$factory=$_POST['factory'];



$fac=mysqli_query($conn,"select * from factory where ID='$factory'");

$fac1=mysqli_fetch_assoc($fac);



echo"<center><a href='printstock.php?factory=".$factory."' style='color:#333;' target='_blank'><button type='button' class='btn btn-sm btn-primary' style='margin:7px 0 7px 0;'>Print</button></a><br />



<b>Factory : ".$fac1['factory']."</b><br /></center>



<table id='dynamic-table' class='table table-bordered table-condensed'>

<thead><tr><th style='width:20%;'>Category</th>

<th style='width:25%;'>Product</th><th style='width:15%;'>Purchase / Production</th>

<th style='width:15%;'>Sales</th><th style='width:15%;'>Balance</th></tr></thead>";



$prod=mysqli_query($conn,"select * from product order by product asc");

while($prod1=mysqli_fetch_assoc($prod))

{



$pur=mysqli_query($conn,"select sum(qty) as qty from purchase where product='$prod1[ID]' and factory='$factory'");

$pur1=mysqli_fetch_assoc($pur);



$sal=mysqli_query($conn,"select sum(qty) as qty from invoice where product='$prod1[ID]' and factory='$factory'");

$sal1=mysqli_fetch_assoc($sal);



$prodc=mysqli_query($conn,"select sum(input_qty) as qty from production where input_product='$prod1[ID]' and factory='$factory'");

$prodc1=mysqli_fetch_assoc($prodc);



$prodct=mysqli_query($conn,"select sum(output_qty) as qty from production where output_product='$prod1[ID]' and factory='$factory'");

$prodct1=mysqli_fetch_assoc($prodct);



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



$purq=$pur1['qty']+$prodct1['qty'];

$salq=$sal1['qty']+$prodc1['qty'];



$pct=mysqli_query($conn,"select * from product_category where ID='$prod1[category]'");

$pct1=mysqli_fetch_assoc($pct);



echo"<tr><td>".$pct1['category']."</td>

<td>".$prod1['product']."</td>

<td style='text-align:right;'>".$purq."</td>

<td style='text-align:right;'>".$salq."</td>

<td style='text-align:right;'>".$bal."</td></tr>";



}



echo"</table><br />



<center><a href='stock.php'>Back</a></center>";



}



else

{



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



<br />



<table style='margin:auto;'>



<tr><td>Factory <span class='red'></span></td><td style='padding:0 11px 0 11px;'></td><td><select class='form-control' name='factory' id='required' title='*' style='width:220px;'><option value=''>Select Factory</option>";



$fc=mysqli_query($conn,"select * from factory order by ID asc");

while($fc1=mysqli_fetch_assoc($fc))

{



echo"<option value='".$fc1['ID']."'>".$fc1['factory']."</option>";

	

}



echo"</select></td></tr>



<tr><td colspan='3' style='text-align:center; padding-top:7px;'><button type='submit' name='submit' value='submit' class='btn btn btn-primary'>Submit</button></td></tr>



</table>



</form>";

	

}



?>









										<!-- PAGE CONTENT ENDS -->

									</div><!-- /.col -->

								</div><!-- /.row -->

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

						</div>

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





<?php

include 'bottom.php';

}

else

{



 header("location:login.php");

}

ob_flush();

?>