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

if(isset($_POST['delo']))
		{
			$rowid=$_POST['rowid'];
			$then=mysqli_query($cons, "DELETE FROM `stouse` WHERE `Number`='$rowid' LIMIT 1");
			$ite=$_POST['ite'];
			$and=mysqli_query($cons, "DELETE FROM `requis` WHERE `Citem`='$ite' AND `Voucher`='0' AND `Destin` LIKE '%PRODUCTION%' LIMIT 1000");
		}

		// edit an item from a given chart
if(isset($_POST['edit']))
		{
			$rowid=$_POST['rowid'];
			$qty=$_POST['qty'];
				$qty=str_replace(',', '', $qty);
			$then=mysqli_query($cons, "UPDATE `stouse` SET `Quantity`='$qty' WHERE `Number`='$rowid' LIMIT 1");
			$ite=$_POST['ite'];
			$and=mysqli_query($cons, "UPDATE `requis` SET `Cqty`='$qty', `Quantity`=`Count`*'$qty' WHERE `Citem`='$ite' AND `Voucher`='0' AND `Destin` LIKE '%PRODUCTION%' LIMIT 1000");
		}

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

		if(isset($_POST['addo']))
		{
			$n=$_POST['n'];
			while($n>0){
				$item=$_POST["item$n"];
				$qty=str_replace(',', '', $_POST["qty$n"]);
				$pri=str_replace(',', '', $_POST["pri$n"]);
				$cost=str_replace(',', '', $_POST["cost$n"]);
				$prod=$_POST["prod$n"];
				if($qty){
		$so=mysqli_query($cons, "INSERT INTO `stouse` (`Number`, `Date`, `User`, `Item`, `Cost`, `Quantity`, `Price`, `Action`, `Destin`, `Voucher`, `Status`) VALUES (NULL, '$Date', '$loge', '$item', '$cost', '$qty', '$pri', 'PRODUCE', '', '0', '0')");

		$singe=mysqli_query($cons, "SELECT `iconfig`.`Ingre`, `iconfig`.`Quantity`, `items`.`Cost`, `items`.`Price` FROM `iconfig` INNER JOIN `items` ON `iconfig`.`Ingre` = `items`.`Number` WHERE `iconfig`.`Item`='$item' ORDER BY `iconfig`.`Number` ASC");
			while($ringe=mysqli_fetch_assoc($singe)){
				$ingre=$ringe['Ingre'];
				$qtl=$ringe['Quantity'];
				$cost=$ringe['Cost'];
				$prix=$ringe['Price'];
				$rqt=$qty*$qtl;
		$so=mysqli_query($cons, "INSERT INTO `requis` (`Number`, `Date`, `User`, `Item`, `Count`, `Quantity`, `Price`, `Action`, `Destin`, `Voucher`, `Status`, `Citem`, `Cost`, `Cqty`, `Production`) VALUES (NULL, '$Date', '$loge', '$ingre', '$qtl', '$rqt', '$prix', 'INTERNAL', 'PRODUCTION', '0', '0', '$item', '$cost', '$qty', 'PRODUCTION')");
			}
				}
			$n--;
			}
		}

	if($custo){
			$conde="AND (`Iname` LIKE '%$custo%' OR `Descri` LIKE '%$custo%')";
			$lim=100;
		}
		else{
			$conde='';
			$lim=140;
		}
		
		$do=mysqli_query($cons, "SELECT *FROM `items` WHERE `Produ`='1' AND `Status`='0' $conde ORDER BY `Number` DESC LIMIT $lim");
				$fo=mysqli_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 active">
	  <a href="mpro.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Make a Production
                </p>
              </a></li> 

	 <li class="list-group-item">
	  <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-4"> </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>
               <?php
			   if($t==1){
		$do=mysqli_query($cons, "SELECT *FROM `items` WHERE `Produ`='1' AND `Status`='0' $conde ORDER BY `Number` DESC LIMIT $lim");
				if($fo=mysqli_num_rows($do)){
				   ?>
                 
             <div class="row">
            <div class="col-lg-12" style='margin-top:-20px;'>
             <span> &nbsp;&nbsp;  Total Records Found : <b><?php echo" $fo " ?></b></span>
            <div class="widget-container fluid-height clearfix">
            <div class="widget-content padded clearfix">

								 <form action="" method="post" class="form-horizontal "> 
				<table class="table table-striped">     
                                      <thead>
                    <tr role="row">
                     <th class="hidden-xs"> No </th>
                       <th> Item&nbsp;Name </th>
                        <th> Description </th>
                       <th> Item&nbsp;Type </th>
                       <th>&nbsp;&nbsp;Qty&nbsp;In&nbsp;&nbsp;</th>
                       <th> Count&nbsp;Unit </th>
						 <th> Cost&nbsp;Price </th><th> Sales&nbsp;Price </th>
                        <th><div align='center'> Quantity </th>
                     </tr>
                    </thead>
                                        <tbody>
					<?php
					$n=1;		
						while($ro=mysqli_fetch_assoc($do)){
				$code=$ro['Number'];
				$iname=$ro['Iname'];
			$kin=$ro['Type'];
			$descri=$ro['Descri'];
			$uno=$ro['Unit'];		
			$sale=$ro['Price'];			
			$cost=0;			$sales=number_format($sale, 2);
			$qt=$ro['Quantity'];				$qty=number_format($qt, 2);
			$prod=$ro['Production'];

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

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

		$dou=mysqli_query($cons, "SELECT *FROM `iconfig` WHERE `Item`='$code' ORDER BY `Number` ASC");
					$fou=mysqli_num_rows($dou);
						while($rou=mysqli_fetch_assoc($dou)){
							$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=mysqli_fetch_assoc($dop);
					$cos=$rop['Cost'];
			$cost+=($qti*$cos)/$count;
						}
						$costo=number_format($cost, 2);
  
  if($cost==0)
	  $rb="xreadonly";
  else{
	  $rb="";
	  $doc=mysqli_query($cons, "UPDATE `items` SET `Cost` = '$cost' WHERE `Number` = '$code'");
  }
		print("<tr>
                        <td class=hidden-xs><div align='center'>$n&nbsp;&nbsp;</td>
						<td> $iname </td><td> $descri </td><td> $type </td><td><div align='right'> $qty </td><td> $unit </td>
						<td><div align='right'>&nbsp;$costo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='hidden' name='cost$n' value='$cost'><input type='hidden' name='prod$n' value='$prod'></td>
						<td><div align='right'>&nbsp;$sales&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='hidden' name='item$n' value='$code'></td>
						<td><input name='qty$n' class='form-control' type='text' onkeyup='format(this);' onkeypress='return isNumberKey(event)' style='text-align:right; width:120px; height:24px; margin:0px 0px 0px 0px' id='box$n' $rb></td></tr>");
						  $n++;
						}
						$toto=number_format($tot);			$tco=number_format($tco);
						?>
						
                    </tbody>
                  </table>

				   <div class="row"><br>
                  <div class="col-lg-10"><hr></div><div class="col-lg-2">
                 <?php echo"<input type='hidden' name='n' value='$n'>"; ?>
            <button class="btn btn-sm btn-block btn-primary hidden-print" type="submit" name="addo" style="margin:0px 0px 0px -15px;">
				 <i class="lnr lnr-plus-circle"></i> Add to Cart </button>
              </div></div></form>
				  <?php
				}
				 else{
						echo"<div class='row'>
            <div class='col-lg-12' style='margin-top:-20px;'>
             <span> &nbsp;&nbsp; Total Records Found : <b> $fo </b></span>
            <div class='widget-container fluid-height clearfix'>
            <div class='widget-content padded clearfix'><br><br><br><br><br><br>
						<div style='text-align:center; font-size:24px; color:#ff3333'> Item not found for your search [$custo] </div><br><br><br><br><br><br><br>";
					}  
			}
				   else{
		$dor=mysqli_query($cons, "SELECT *FROM `stouse` WHERE `Status`='0' AND `Voucher`='0' AND `Action`='PRODUCE' AND `User`='$loge' ORDER BY `Number` DESC");
				if($for=mysqli_num_rows($dor)){
					//$dok=mysqli_query($cons, "UPDATE `iconfig` SET `Produce` = '0' WHERE `Produce` != '0'");
?>

             <div class="row">
            <div class="col-lg-12" style='margin-top:-20px;'>
             <span> &nbsp;&nbsp;  Total Records Found : <b><?php echo" $for " ?></b></span>
            <div class="widget-container fluid-height clearfix">
            <div class="widget-content padded clearfix">
	
	<table width='100%'><tr><td width='44%' valign='top'>
	<table class="table table-striped">     
                                      <thead>
                    <tr role="row">
                     <th class="hidden-xs"> NO </th>
                       <th> Item&nbsp;Name </th>
                        <th> Description </th>
                        <th><div align='center'> Quantity </th>
                        <th class="hidden-xs hidden-print" style="width:40px; text-align:center;" colspan='2'> Options </th>
                     </tr>
                    </thead>
                                        <tbody>
										<?php
	$n=1;			$tot=0;
					while($ror=mysqli_fetch_assoc($dor)){
						$code=$ror['Number'];
						$item=$ror['Item'];
						$sale=$ror['Price'];			
						$cost=$ror['Cost'];	
						$qt=$ror['Quantity'];
	$do=mysqli_query($cons, "SELECT *FROM `items` WHERE `Status`='0' AND `Number`='$item' ORDER BY `Number` DESC LIMIT $lim");
		$ro=mysqli_fetch_assoc($do);
			$iname=$ro['Iname'];
			$kin=$ro['Type'];
			$descri=$ro['Descri'];
			$uno=$ro['Unit'];	
			$qin=$ro['Quantity'];

	//$dok=mysqli_query($cons, "UPDATE `iconfig` SET `Produce` = '1' WHERE `Item` = '$item'");
			
			$qty=number_format($qt, 2);			$costo=number_format($cost, 2);			$sales=number_format($sale, 2);

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

				$dox=mysqli_query($cons, "SELECT *FROM `unit` WHERE `Number`='$uno' ORDER BY `Number` ASC");
					$rox=mysqli_fetch_assoc($dox);
							$unit=$rox['Unit'];
							if($cost>$sale)
								$arr="<i class='lnr lnr-arrow-down'></i>";
							else
								$arr="<i class='lnr lnr-arrow-up'></i>";
          $to=$sale*$qty;				$too=number_format($to, 2);			$qino=number_format($qin, 2);
		print("<tr><form method=post action=''>
                        <td class=hidden-xs><div align='center'>$n&nbsp;&nbsp;</td>
						<td> $iname </td><td> $descri </td><td><div align='right'>
						 <input name='qty' class='form-control' type='text' onChange=this.style.color='#ff3366' onkeyup='format(this);' onkeypress='return isNumberKey(event)' style='text-align:right; width:80px; height:20px; margin:0px 0px 0px 0px; padding:0px 10px 0px 0px;' value='$qty'></td>						
						
						<td class='hidden-xs hidden-print' align='right' style='width:40px; padding:0px;'>
                              <input type='hidden' name='rowid' value='$code'> <input type='hidden' name='ite' value='$item'>
                          <button type='submit' class='btn btn-xs btn-warning hidden-print' name='edit' style='height:18px; padding:0px; margin:0px;' title='Edit' data-toggle='tooltip' data-placement='top'>
						  &nbsp;&nbsp;<i class='lnr lnr-pencil'></i>&nbsp;&nbsp;</button></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='ite' value='$item'>
                          <button type='submit' class='btn btn-xs btn-danger hidden-print' name='delo' style='height:18px; padding:0px; margin:0px;' title='Delete' data-toggle='tooltip' data-placement='top'>
						  &nbsp;&nbsp;<i class='lnr lnr-trash'></i>&nbsp;&nbsp;</button></td></form></tr>");
						  $n++;				$tot+=$to;
						}
						$toto=number_format($tot, 2);			
						?>
						
                    </tbody></table></td>

			<td width='4%'>  </td><td valign='top'>
			<?php
				$dou=mysqli_query($cons, "SELECT `Item` FROM `requis` WHERE `Voucher`='0' AND `User`='$loge' AND `Destin`='PRODUCTION' GROUP BY `Item` ORDER BY `Number` ASC");
			if($fou=mysqli_num_rows($dou)){
		
		 echo"<div style='border:0px solid blue; overflow:auto; height:340px; margin-bottom: -20px;'>
		 <table class='table table-striped table-hover'>     
                                      <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></tr>";
			$n=1;			$tot=$tq=0;
						while($rou=mysqli_fetch_assoc($dou)){
							$ingre=$rou['Item'];
				$dop=mysqli_query($cons, "SELECT *FROM `items` WHERE `Number`='$ingre' ORDER BY `Number` DESC LIMIT 1");
				$rop=mysqli_fetch_assoc($dop);
					$iname=$rop['Iname'];
					$cost=$rop['Cost'];
					$uno=$rop['Unit'];
					$desc=$rop['Descri'];
					$dire=$rop['Direct'];
			$dox=mysqli_query($cons, "SELECT *FROM `unit` WHERE `Number`='$uno' ORDER BY `Number` ASC");
					$rox=mysqli_fetch_assoc($dox);
							$unit=$rox['Unit'];
			$qti=$amo=0;
$dous=mysqli_query($cons, "SELECT *FROM `requis` WHERE `Voucher`='0' AND `Item`='$ingre' AND `User`='$loge' AND `Destin`='PRODUCTION' ORDER BY `Number` ASC");
	while($rous=mysqli_fetch_assoc($dous)){
		$code=$rous['Number'];
		$qz=$rous['Quantity'];
		$ct=$rous['Count'];
		$qti+=$qz;
	}
	                        $tq+=$qti;

	$then=mysqli_query($cons, "UPDATE `requis` SET `Price`='$cost',`Direct`='$dire' WHERE `Number`='$code' LIMIT 1");
	
					$amo=($qti*$cost);
			$costo=number_format($cost, 2);				$amoo=number_format($amo, 2);				$tot+=$amo;
						$qtio=number_format($qti, 2);					$qzo=$qz;
		echo"<tr><td class=hidden-xs><div align='center'>$n&nbsp;&nbsp;</td>
			<td>$iname $desc</td><td><div align='right'>$costo</td>
			<td><div align='right'><b>$qtio&nbsp;</b>$unit</td><td><div align='right'>$amoo</td></tr>";
				$n++;
						}
						$toto=number_format($tot, 2);
							$tq=number_format($tq, 2);
		echo"<tr><th colspan='3'><div align='center'>Total Amount</th>
				<th><div align='right'>$tq KG</td>
					<th><div align='right'>$toto</td></tr></table>";
			}
			?>
			</td></tr></table></div>
			 <div class="col-lg-12"><hr></div>

				  <label class="control-label col-md-4"> </label>

	<form method='post' action='oper.php'><label class="control-label col-md-2">
	<div align='right'><select class="form-control" name="produ" required>			
		<option value='PRODUCTION' selected='selected'> PRODUCTION </option>
</select></div></label>

	<label class="control-label col-md-2" title='Production to be done by' data-toggle='tooltip' data-placement='top'>
<input list="take" name="taker" class="form-control" placeholder="DONE BY" OnKeyup="return cUpper(this);" required></label>
<datalist id="take">
 <?php
		$top=mysqli_query($cons, "SELECT `Person` FROM `Stouse` WHERE `Status`='0' AND `Person`!='' AND `Action`='PRODUCE' GROUP BY `Person` ORDER BY `Date` DESC LIMIT 20");
						while($rop=mysqli_fetch_assoc($top)){
							$sup=$rop['Person'];
				if($pers==$sup)
					$t='selected';
				else
					$t='';
			echo"<option value='$sup' $t>";
						}
						?>
</datalist>
		</label>


	<label class="control-label col-md-2"><input name="dato" id="from" class="form-control sm" type="text" style="text-align:center;" VALUE="<?php echo $Date ?>" onclick="return pageScroll()"></label>

			 <label class="control-label col-md-2">
			 <button class="btn btn-md btn-block btn-info" type="submit" name="request"><i class="lnr lnr-menu-circle"></i> REQUEST </button>
			 </label></form>

		<?php
				}
					else{
						echo"<div class='row'>
            <div class='col-lg-12' style='margin-top:-20px;'>
             <span> &nbsp;&nbsp; Total Records Found : <b> $fo </b></span>
            <div class='widget-container fluid-height clearfix'>
            <div class='widget-content padded clearfix'><br><br><br><br><br><br>
						<div style='text-align:center; font-size:24px; color:#ff9999'>Load items to be produced </div><br><br><br><br><br><br><br>";
					}
			}
					?>
                                      
                
              </div>
            </div></div>
                  </div>
      
   </div></div></div>  
   <?php
   include'footer.php';
   ?>
