<?php
if(basename($_SERVER['PHP_SELF']) == 'iconfig.php') 
  $kk=" class='current'";
include'header.php';
include'connection.php';
$custo='';
$conde='';
$rowid=0;

if(isset($_POST['delo']))
		{
			$rowid=$_POST['rowid'];
			$ico=$_POST['ico'];
			$then=mysqli_query($cons, "DELETE FROM `iconfig` WHERE `Number`='$ico' LIMIT 1");
		}

	if(isset($_POST['config']))
		{
			$rowid=$_POST['rowid'];
			$ingred=$_POST['ingred'];
			$qts=$_POST['qts'];
			$then=mysqli_query($cons, "INSERT INTO `iconfig` (`Number`, `Date`, `User`, `Item`, `Count`, `Ingre`, `Quantity`, `Status`, `Pro`) VALUES (NULL, '$Date', '$loge', '$rowid', '1', '$ingred', '$qts', '0', '2')");
		}

if(isset($_POST['open']))
		{
			$rowid=$_POST['rowid'];			
		}

if(isset($_POST['search']))
		{
			$custo=$_POST['custo'];
		}

		
		if($custo){
			$conde="AND (`Iname` LIKE '%$custo%' OR `Descri` LIKE '%$custo%')";
			$lim=999999999;
		}
		else{
			$conde='';
			$lim=20;
		}

	if($rowid)
$do=mysqli_query($cons, "SELECT *FROM `items` WHERE `Produ`='1' AND `Status`='0' AND `Number`='$rowid' ORDER BY `Number` DESC LIMIT $lim");
	else
$do=mysqli_query($cons, "SELECT *FROM `items` WHERE `Produ`='1' AND `Status`='0' $conde ORDER BY `Iname` ASC LIMIT $lim");
$fo=mysql_num_rows($do);
?>
<div class="container-fluid main-content">
        <div class="page-title">
          <h2 style='margin-top:-20px; margin-bottom: 5px; color:#ffcc33'>
         Production
          </h2>
                 </div>
     
        <div class="row">
  <div class="col-lg-2 hidden-print">
   
   <ul class="list-group">

	 <li class="list-group-item">
	  <a href="mpro.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Make a Production
                </p>
              </a></li>

	 <li class="list-group-item active">
	  <a href="iconfig.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Item Configuration
                </p>
              </a></li>   

    <li class="list-group-item">
	  <a href="oper.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Receive Items
				<?php
				if($pfequo)
					echo"<span class='badge' style='float:right; font-size:12px; margin-right:0px; margin-top:0px; height:18px; background-color:#5CF7CE; width:auto;'> $pfequo </span>";
					?>
                </p>
              </a></li>  

	   <li class="list-group-item">
              <a href="requis.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Requisitions
				<?php
				if($fequo)
					echo"<span class='badge' style='float:right; font-size:12px; margin-right:0px; margin-top:0px; height:18px; background-color:#ff66cc; width:auto;'> $fequo </span>";
					?>
                </p>
              </a></li> 
                       
           
                       
            </ul>
  </div>        
                    
           
           
       
        <div class="col-lg-10">
                  <div class="row">
         
           <div class="col-lg-1"> </div><div class="col-lg-2"> 
<?php 
if($rowid!='0')
echo"<a href='iconfig.php'>
<button class='btn  btn-success btn-block' type='button' name='search'><i class='lnr lnr-chevron-left-circle'></i> Go Back </button></a>";
?></div><div class="col-lg-1">  </div>
          
         
           

        <form action="" method="post" class="form-horizontal ">                  
                       <div class="col-lg-8 no-print"><div class="col-lg-3"> 					
					   
					   </div>
            <div class="col-lg-6"> 
      <input class="form-control"  name="custo" type="text" id="searchs" autofocus="autofocus" required>
			</div>                      
                       
                       <div class="col-lg-2">
                        <button class="btn  btn-primary btn-block" type="submit" name="search"><i class="lnr lnr-magnifier"></i> Search</button>
                   
					  </div>
                         </div> 
                      
                     
                  
            </form> 
             
               
            </div>
             <div class="row">
            <div class="col-lg-12" style='margin-top:-20px;'>
             <span>Total Records Found : <b><?php echo" $fo " ?></b></span>
            <div class="widget-container fluid-height clearfix">
            <div class="widget-content padded clearfix">
               
                               <table class="table table-striped table-hover">     
                                      <thead>
                    <tr role="row">
                     <th class="hidden-xs"> No </th>
                       <th> Item&nbsp;Name </th>
                        <th> Description </th>
                       <th> Item&nbsp;Type </th>
						 <th> Price/Unit </th>
                       <th> Count&nbsp;Unit </th>
                        <th><div align='center'> Ingredients </th><th><div align='center'> Total&nbsp;Cost </th>
			<th> Percentage </th><th class="hidden-xs hidden-print">&nbsp;&nbsp;</th>
                        <th class="hidden-xs hidden-print" style="width:40px; text-align:center;" colspan='2'> Config </th>
                     </tr>
                    </thead>
                                        <tbody>
					<?php
					$n=1;		
						while($ro=mysql_fetch_assoc($do)){
				$code=$ro['Number'];
				$iname=$ro['Iname'];
			$kin=$ro['Type'];
			$descri=$ro['Descri'];
			$uno=$ro['Unit'];			
			$sale=$ro['Price'];			$sales=number_format($sale, 2);
			$cost=0;
			$qt=$ro['Quanity'];				$qty=number_format($qt, 2);

$doi=mysqli_query($cons, "SELECT *FROM `itype` WHERE `Number`='$kin' ORDER BY `Number` ASC");
		$roi=mysql_fetch_assoc($doi);
				$type=$roi['Type'];

				$dox=mysqli_query($cons, "SELECT *FROM `unit` WHERE `Number`='$uno' ORDER BY `Number` ASC");
					$rox=mysql_fetch_assoc($dox);
							$unit=$rox['Unit'];

				$dou=mysqli_query($cons, "SELECT *FROM `iconfig` WHERE `Item`='$code' ORDER BY `Number` ASC");
					$fou=mysql_num_rows($dou);
						while($rou=mysql_fetch_assoc($dou)){
$nose=$rou['Number'];
							$ingre=$rou['Ingre'];
							$count=$rou['Count'];
							$qti=$rou['Quantity'];
			$dop=mysqli_query($cons, "SELECT *FROM `items` WHERE `Number`='$ingre' ORDER BY `Number` DESC LIMIT 1");
				$rop=mysql_fetch_assoc($dop);
					$cos=$rop['Cost'];
			$cost+=($qti*$cos)/$count;
$upda=mysqli_query($cons, "UPDATE `iconfig` SET `Pro`='2' WHERE `Number`='$nose'");
						}
$per=$cost/$sale*100;
				$pero=number_format($per, 2);	
						$costo=number_format($cost, 2);

						if($cost==0)
				$stl="padding:1px; style='color:#0033cc;'";
			else
				$stl="padding:1px;";
		print("<tr>
                        <td $stl class=hidden-xs><div align='center'>$n&nbsp;&nbsp;</td>
						<td $stl> $iname </td><td $stl> $descri </td>
                        <td $stl> $type </td><td $stl><div align='right'> $sales </td>
						<td $stl><center> 1$unit </td><td $stl><div align='center'> $fou </td>
						<td $stl><div align='right'> $costo </td><td $stl><div align='right'> $pero% </td> 
						<td class='hidden-xs hidden-print'>&nbsp;&nbsp;</td>
						   <form method='post' action=''><td colspan='2' class='hidden-xs hidden-print' align='right' padding:0px;'>
                              <input type='hidden' name='rowid' value='$code'>
                          <button type='submit' class='btn btn-xs btn-info hidden-print' name='open' style='height:20px; padding:0px; margin:0px;'>
						  &nbsp;&nbsp;&nbsp;<i class='lnr lnr-cog'></i>&nbsp;&nbsp;Open&nbsp;&nbsp;&nbsp;</button></td></form></tr>");
						  $n++;

						   $then=mysqli_query($cons, "UPDATE `items` SET `Cost`='$cost' WHERE `Number`='$code' LIMIT 1");
						}
						$toto=number_format($tot);			$tco=number_format($tco);
						?>
						
                    </tbody>
                  </table>
                   
                  <div class="col-lg-12" style="margin-top:-10px;"><hr></div>
				  
				  <?php
				  if($rowid){
		echo"<table width='100%'><tr><td width='50%' valign='top'><br><br>
		<div style='position:fixed'><form action='' method='post'>
		<label class='control-label col-md-6'><select class='form-control' name='ingred' style='width:240px;' required>			
			<option value='' selected='selected'> SELECT AN ITEM </option>";
		
	$do=mysqli_query($cons, "SELECT *FROM `items` WHERE `Status`='0' AND `Produ`>='0' ORDER BY `Iname` ASC LIMIT 2000");
		while($ro=mysql_fetch_assoc($do)){
			$nuo=$ro['Number'];
			$iname=$ro['Iname'];
			$cost=number_format($ro['Cost']);
			$descri=$ro['Descri'];
			$uno=$ro['Unit'];
	$dox=mysqli_query($cons, "SELECT *FROM `unit` WHERE `Number`='$uno' ORDER BY `Number` ASC");
					$rox=mysql_fetch_assoc($dox);
							$unit=$rox['Unit'];
		$dou=mysqli_query($cons, "SELECT *FROM `iconfig` WHERE `Item`='$code' AND `Ingre`='$nuo' ORDER BY `Number` ASC");
			if(!$fou=mysql_num_rows($dou))
	echo"<option value='$nuo'> $iname $descri &nbsp; $cost/$unit </option>";
		}
			
	echo"</select><input type='hidden' name='rowid' value='$code'></label>

	<label class='control-label col-md-3'><input name='qts' class='form-control sm' type='text' style='text-align:center;' placeholder='Portion (Qty)'></label>

			 <label class='control-label col-md-3'>
			 <button class='btn btn-md btn-block btn-info' type='submit' name='config'><i class='lnr lnr-plus-circle'></i> ADD </button>
			 </label><br><br></form></div>		
		
		</td><td valign='top' style='padding-left:20px;'><div align='right'>";
		
		$dou=mysqli_query($cons, "SELECT *FROM `iconfig` WHERE `Item`='$code' ORDER BY `Number` ASC");
			if($fou=mysql_num_rows($dou)){
				 if($_SESSION['Access']=='5'){
			 $dbutn='submit';
			 $disa='';
		 }
		 else{
			 $dbutn='button';
			 $disa='disabled';
		 }
		 echo"<div style='border:0px solid blue; overflow:auto; height:340px; margin:-21px 0px -10px 0px; position:absolute; float:right; width:46%; text-align:right;'>
		 <fieldset style='border:1px solid powderblue; border-radius:5px; padding:0px 10px 20px 10px; background-color:#ffffff;'><table class='table table-striped'>     
                                      <thead>
                    <tr role='row'>
                     <th class='hidden-xs'> No </th>
                       <th> Ingredient </th>
                        <th><div align='center'> Cost </th>
                       <th><div align='center'> Quantity </th>
				   <th><div align='center'> Amount </th><th><div align='center'> # </th></tr>";
			$n=1;				$tot=0;
						while($rou=mysql_fetch_assoc($dou)){
							$ico=$rou['Number'];
							$ingre=$rou['Ingre'];
							$count=$rou['Count'];
							$qti=$rou['Quantity'];
				$dop=mysqli_query($cons, "SELECT *FROM `items` WHERE `Number`='$ingre' ORDER BY `Number` DESC LIMIT 1");
				$rop=mysql_fetch_assoc($dop);
					$iname=$rop['Iname'];
					$cost=$rop['Cost'];
					$amo=($qti*$cost);
			$costo=number_format($cost, 2);				$amoo=number_format($amo, 2);
		echo"<tr><td class=hidden-xs><div align='center'>$n&nbsp;&nbsp;</td>
			<td><div align='left'>$iname</div></td><td><div align='right'>$costo</td>
			<td><div align='right'>$qti</td><td><div align='right'>$amoo</td>
			
			<form method='post' action=''><td class='hidden-xs hidden-print' align='right' style='width:40px; padding:0px;'>
                              <input type='hidden' name='rowid' value='$code'><input type='hidden' name='ico' value='$ico'>
                          <button type='$dbutn' class='btn btn-xs btn-danger hidden-print' name='delo' style='height:18px; padding:0px;' $disa>
						  &nbsp;&nbsp;<i class='lnr lnr-trash'></i>&nbsp;&nbsp;</button></td></form></tr>";

				$n++;				$tot+=$amo;
						}
						$toto=number_format($tot, 2);
					   echo"<tr><th colspan='3'>&nbsp;&nbsp;&nbsp;&nbsp; Total Cost </th>
					  <th colspan='2'><div align='right'> $toto </th><th> </th></tr></table></fieldset><br><br><br>.<br>";
			}
			echo"</td></tr></table></div>";
					}
					else{
					?>
					<div class="col-lg-12">
                  <div class="pull-right">
                 <button class="btn btn-primary btn-success hidden-print" onclick="return window.print()"><i class="lnr lnr-printer"></i> Print</button>
              </div></div>
			  <?php
					}
					?>
                
              </div>
            </div></div>
                  </div>
      
   </div></div></div>  
   <?php
   include'footer.php';
   ?>
