<?php
if(basename($_SERVER['PHP_SELF']) == 'oper.php') 
  $kk=" class='current'";
include'header.php';
include'connection.php';
$dato=$datos=$Date;
$custo='';
$conde='';
$p=1;
		
if(isset($_POST['search']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$p=2;
		}

if(isset($_POST['open']))
		{
			$vous=$_POST['vous'];
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$p=3;
		}
		
	// Receive a given production to main store ********************************************************************
if(isset($_POST['receivepro']))
		{
			$n=$_POST['n'];
            $dato=$_POST['dato'];
	$rece=mysqli_query($cons, "SELECT `Voucher` FROM `stouse` WHERE `Voucher`!='0' ORDER BY `Voucher` DESC LIMIT 1");
				$re=mysqli_fetch_assoc($rece);
					$vou=$re['Voucher']+1;
					$vous=$re['Voucher']+2;

			while($n>0){
				$item=$_POST["item$n"];
				$qty=$_POST["rec$n"];
				$pri=$_POST["pri$n"];
				$prs=$_POST["prs$n"];
				$cost=$_POST["cost$n"];
				$voi=$_POST["voi$n"];
				$code=$_POST["code$n"];
				$req=$_POST["qty$n"];
				$produ=$_POST["produ$n"];
				if($qty!=''){
$so=mysqli_query($cons, "INSERT INTO `stouse` (`Number`, `Date`, `Time`, `User`, `Item`, `Cost`, `Quantity`, `Price`, `Action`, `Destin`, `Voucher`, `Invoice`, `Status`, `Upda`, `Produce`) VALUES (NULL, '$dato', '$Time', '$loge', '$item', '$cost', '$qty', '$pri', 'RECEIVE', '$produ', '$vou', 'NO INVOICE', '0', '1', '$voi')");

	$then=mysqli_query($cons, "UPDATE `stouse` SET `Upda`='1' WHERE `Number`='$code' AND `Action`='PRODUCE' ORDER BY `Number` DESC LIMIT 1");
$soso=mysqli_query($cons, "UPDATE `items` SET `Quantity`=`Quantity`+'$qty' WHERE `Number`='$item' ORDER BY `Number` ASC LIMIT 1");

/*			
	$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;
	$soso=mysqli_query($cons, "UPDATE `items` SET `Quantity`=`Quantity`-'$rqt' WHERE `Number`='$ingre' ORDER BY `Number` ASC LIMIT 1");

$so=mysqli_query($cons, "INSERT INTO `stouse` (`Number`, `Date`, `Time`, `User`, `Item`, `Cost`, `Quantity`, `Price`, `Action`, `Destin`, `Voucher`, `Invoice`, `Status`, `Upda`, `Person`, `Produce`) VALUES (NULL, '$dato', '$Time', '$loge', '$ingre', '$cost', '$rqt', '$prix', 'TAKEN', '$produ', '$vous', 'MAIN STORE', '0', '1', '$prs', '$voi')");
			}
			*/
				}
			$n--;
			}
		}

		$rece=mysqli_query($cons, "SELECT `Voucher` FROM `stouse` WHERE `Voucher`!='0' ORDER BY `Voucher` DESC LIMIT 1");
				$re=mysqli_fetch_assoc($rece);
					$vou=$re['Voucher']+1;
		
	if(isset($_POST['request']))
		{
			$dato=$_POST['dato'];
			$taker=$_POST['taker'];
			$produ=$_POST['produ'];
	$so=mysqli_query($cons, "UPDATE `stouse` SET `Date`='$dato', `Destin`='$produ', `Voucher`='$vou', `Person`='$taker' WHERE `Status`='0' AND `Voucher`='0' AND `Action`='PRODUCE' AND `Destin`='' AND `User`='$loge'");

	$rece=mysqli_query($cons, "SELECT `Voucher` FROM `requis` WHERE `Action`='INTERNAL' ORDER BY `Voucher` DESC LIMIT 1");
				$re=mysqli_fetch_assoc($rece);
					$vous=$re['Voucher']+1;

$so=mysqli_query($cons, "UPDATE `requis` SET `Date`='$dato', `Time`='$Time',  `Voucher`='$vous', `Produce`='$vou', `Requested`=`Quantity`, `Destin`='$produ', `Person`='$taker' WHERE `Status`='0' AND `Voucher`='0' AND `Action`='INTERNAL' AND `User`='$loge'");
		}

if($p=='1')
$do=mysqli_query($cons, "SELECT `User`, `Date`, `Voucher`, `Person`, COUNT(DISTINCT(`Item`)) AS 'Ito' FROM `stouse` WHERE `Status`='0' AND `Action`='PRODUCE' AND `Voucher`!='0' GROUP BY `Voucher` ORDER BY `Date` DESC, `Voucher` DESC LIMIT 15");
elseif($p=='2')
$do=mysqli_query($cons, "SELECT `User`, `Date`, `Voucher`, `Person`, COUNT(DISTINCT(`Item`)) AS 'Ito' FROM `stouse` WHERE `Status`='0' AND `Action`='PRODUCE' AND `Voucher`!='0' AND `Date` BETWEEN '$dato' AND '$datos' GROUP BY `Voucher` ORDER BY `Date` DESC, `Voucher` DESC LIMIT 1500");
else
$do=mysqli_query($cons, "SELECT *FROM `stouse` WHERE `Status`='0' AND `Action`='PRODUCE' AND `Voucher`='$vous' ORDER BY `Number` ASC LIMIT 100");
$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">
	  <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 active">
	  <a href="oper.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Receive Item
				<?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-3"> 
           <div class="input-group date datepicker">
      <input class="form-control form-center" id="from" name="dato" type="text" value="<?php echo $dato ?>" onkeypress='return isNumberKey(event)' required>
	  <span class="input-group-addon"><i class="lnr lnr-calendar-full"></i></span>
			</div></div>


		  <div class="col-lg-3"> 
           <div class="input-group date datepicker">	
      <input class="form-control form-center" id="to" name="datos" type="text" value="<?php echo $datos ?>" onkeypress='return isNumberKey(event)' required>
	  <span class="input-group-addon"><i class="lnr lnr-calendar-full"></i></span>
              </div></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($pfequo){
				?>
             <div class="row"><form method="post" action="">
            <div class="col-lg-12" style='margin-top:-20px;'>
             <span>Total Records Founds : <b><?php echo" $fo " ?></b></span>
            <div class="widget-container fluid-height clearfix">
            <div class="widget-content padded clearfix">
                
    <?php
    if($p<=2){
        
    echo"<table class='table table-striped table-hover'>     
                <thead>
    <tr role='row'><th class='text-center' width='5%'> Batch&nbsp;No </th>
		<th class='text-center'>&nbsp;&nbsp;Due&nbsp;Date&nbsp;&nbsp;</th>
					 <th> Done By </th><th> Requested By </th>
                       <th> Items </th><th> Quantity </th>
                       <th> Received </th><th> Balance </th>
    <th class='hidden-print' style='width:20px; text-align:center;'> # </th>
                     </tr></thead><tbody>";
                                        
            while($ro=mysqli_fetch_assoc($do)){
                $vous=$ro['Voucher'];
				$user=$ro['User'];
				$pers=$ro['Person'];
				$item=$ro['Ito'];
				$date=$ro['Date'];
				$stl="style='padding:0px;'";
				
	$prequoi=mysqli_query($cons, "SELECT SUM(`Quantity`) AS 'Req' FROM `stouse` WHERE `Voucher`='$vous' AND  `Status`='0' AND `Action`='PRODUCE' ORDER BY `Number` ASC");
	$requoi=mysqli_fetch_assoc($prequoi);
	    $req=$requoi['Req'];
	    
	    
    $prequo=mysqli_query($cons, "SELECT SUM(`Quantity`) AS 'Rec' FROM `stouse` WHERE `Voucher`!='0' AND `Produce`='$vous' AND `Status`='0' AND `Action`='RECEIVE' ORDER BY `Voucher` ASC");
    $requo=mysqli_fetch_assoc($prequo);
        $rec=$requo['Rec'];
	    
	    $bal=$req-$rec;
	print("<tr><form action='' method='post'>
	<td class='text-right' $stl> $vous </td>
	<td class='text-center' $stl> $date </td><td $stl> $user </td>
	<td $stl> $pers </td><td class='text-center' $stl> $item </td>
	<td class='text-center' $stl> $req </td><td class='text-center' $stl> $rec </td><td class='text-center' $stl> $bal </td><td class='hidden-print' align='right' style='width:20px; padding:2px 0px 0px 0px;'>
			<input type='hidden' name='vous' value='$vous'>
			<input type='hidden' name='dato' value='$dato'>
			<input type='hidden' name='datos' value='$datos'>
        <button type='submit' name='open' class='btn btn-xs btn-success hidden-print' style='height:18px; padding:0px;'>&nbsp;&nbsp;<i class='lnr lnr-download'></i>&nbsp;&nbsp;</button></td></form></tr>");
            }
            echo"</tbody></table>";
        
        
        
        
    }
    else{
        ?>
        <table class="table table-striped table-hover">     
                <thead>
                    <tr role="row">
                     <th class="hidden-xs"> No </th>
					 <th>&nbsp;&nbsp;Due&nbsp;Date&nbsp;&nbsp;</th>
					 <th> Producer </th>
                       <th> Item&nbsp;Name </th>
                        <th> Description </th>
						 <th> Price/Unit </th>
                      <th colspan='2' width='1%'><div align='center'> Requested </th><th><div align='center'> Received </th>
						<th><div align='center'> Produced </th><th><div align='center'> Balance </th>
                        <th class="hidden-xs hidden-print" style="width:20px; text-align:center;"> # </th>
                     </tr>
                    </thead>
                                        <tbody>
					<?php
								   $n=1;
	$prequoi=mysqli_query($cons, "SELECT *FROM `stouse` WHERE `Voucher`='$vous' AND  `Status`='0' AND `Action`='PRODUCE' GROUP BY `Destin` ORDER BY `Voucher` ASC");
	while($requoi=mysqli_fetch_assoc($prequoi)){
	    $destini=$requoi['Destin'];
	print("<tr><th class='hidden-xs' style='color:powderblue;'>  </th><th style='color:powderblue;' colspan='10'><b class='caret'></b> &nbsp; $destini &nbsp;&nbsp;  <span class='pull-right text-right'> $vous </span></th><th style='color:powderblue;' class='hidden-xs'>   </th></tr>");
    $prequo=mysqli_query($cons, "SELECT *FROM `stouse` WHERE `Voucher`='$vous' AND `Status`='0' AND `Action`='PRODUCE' AND `Destin`='$destini' ORDER BY `Voucher` ASC");
							
						while($ror=mysqli_fetch_assoc($prequo)){
						$code=$ror['Number'];
						$item=$ror['Item'];
						$sale=$ror['Price'];			
						$cost=$ror['Cost'];	
						$qty=$ror['Quantity'];
						$dte=$ror['Date'];
						$voi=$ror['Voucher'];
						$pers=$ror['Person'];
						$deso=$ror['Destin'];
						$prs=$ror['Person'];
			
	$theni=mysqli_query($cons, "UPDATE `items` SET `Cost`='$cost' WHERE `Number`='$item' LIMIT 1");
	$do=mysqli_query($cons, "SELECT *FROM `items` WHERE `Status`='0' AND `Number`='$item' ORDER BY `Number` DESC");
		$ro=mysqli_fetch_assoc($do);
			$iname=$ro['Iname'];
			$kin=$ro['Type'];
			$descri=$ro['Descri'];
			$uno=$ro['Unit'];
			$pri=$ro['Price'];

$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'];
							
	$prequoc=mysqli_query($cons, "SELECT SUM(`Quantity`) AS 'Rec' FROM `stouse` WHERE `Voucher`!='0' AND `Produce`='$vous' AND `Status`='0' AND `Action`='RECEIVE' AND `Item`='$item' ORDER BY `Voucher` ASC");
    $requoc=mysqli_fetch_assoc($prequoc);
        $rec=$requoc['Rec'];

		$costo=number_format($cost, 2);			$prio=number_format($pri, 2);

			$ite="$item|$qty";

			$doki=mysqli_query($cons, "SELECT *FROM `requis` WHERE `Produce`='$voi' AND `Status`='0' AND `Upda`='1' ORDER BY `Number` ASC");
					if($foki=mysqli_num_rows($doki)){
						$hr="";
						$bt='disabled';
						$tle="title='Not allowed at this moment' data-toggle='tooltip' data-placement='top'";
						$tli="title='Not allowed at this moment' data-toggle='tooltip' data-placement='top'";
					}
					else{
						$hr="<a href='oper.php?prodele=$code'>";
						$bt='';
						$tle="title='Remove this item' data-toggle='tooltip' data-placement='top'";
						$tli="";
					}

       $b=$n*10;
		print("<tr>
                        <td class=hidden-xs><div align='center'>$n&nbsp;&nbsp;</td>
						<td> $dte </td><td> $pers </td><td> $iname </td><td> $descri </td><td><div align='right'> $costo&nbsp;/&nbsp;$prio </td>
						
						<td><div align='right'><input name='qty$n' class='form-control' type='text' onkeyup='minusBy$n();' onkeypress='return isNumberKey(event)' style='text-align:right; width:40px; height:22px; margin:0px 0px 0px 0px; background-color:transparent; border:0px; padding:0px;' id='box$n' value='$qty' readonly='readonly'></td>
						
						<td style='padding:5px 0px 5px 0px;'><div align='left'><input name='qto' class='form-control' type='text' style='text-align:left; width:40px; height:22px; margin:0px 0px 0px 0px; background-color:transparent; border:0px; padding:0px;' value='$unit' readonly='readonly'></td>
						
						<td><div align='right'><input class='form-control' type='text' style='text-align:right; width:40px; height:22px; margin:0px 0px 0px 0px; background-color:transparent; border:0px; padding:0px; color:blue' value='$rec' readonly='readonly'></td>

						<td><div align='right'><input name='rec$n' class='form-control' type='text' onkeyup='minusBy$n();' onkeypress='return isNumberKey(event)' style='text-align:right; width:80px; height:22px;' id='box$b'></td>
						 
						 <td><div align='right' style='border:1px solid #cccccc; width:80px; height:22px; border-radius:5px; padding:0px 10px 5px 0px'><span class='dollars' style='text-align:right; width:80px; height:22px; font-size:18px; color:#66cccc; float:center; text-align:right;' id='result$n' onchange='format(this);'></span></div></td>
						
						  
						  <td class='hidden-xs hidden-print' align='right' style='width:40px; padding:0px;' $tli>
                          <input type='hidden' name='item$n' value='$item'><input type='hidden' name='pri$n' value='$pri'>
                          <input type='hidden' name='cost$n' value='$cost'><input type='hidden' name='voi$n' value='$voi'>
						  <input type='hidden' name='n' value='$n'><input type='hidden' name='code$n' value='$code'><input type='hidden' name='prs$n' value='$prs'>
						  <input type='hidden' name='produ$n' value='$deso'> $hr
                          <button type='button' class='btn btn-xs btn-danger hidden-print' style='height:18px; padding:0px;' $bt $tle>&nbsp;&nbsp;<i class='lnr lnr-trash'></i>&nbsp;&nbsp;</button></a></td></tr>");
						  $n++;
						}
					}
						$toto=number_format($tot);			$tco=number_format($tco);
						?>
						
                    </tbody>
                  </table>

                   <div class="row" style="padding-top:-20px; height:10px; margin-top:-20px;"><br>
                  <div class="col-lg-7"><hr></div><div class="col-lg-2">
        <div class="input-group date" data-provide="datepicker">
      <input class="form-control form-center" name="dato" type="text" value="<?php echo $Date ?>" onkeypress='return isNumberKey(event)' style="height:30px;" required>
	  <span class="input-group-addon"><i class="lnr lnr-calendar-full"></i></span>
			</div>          
                  </div><div class="col-lg-3">
                 <?php echo"<input type='hidden' name='n' value='$n'>"; ?>
                 <button class="btn btn-sm btn-block btn-success hidden-print" type="submit" name="receivepro" style="margin:0px 0px 0px -15px;">
				 <i class="lnr lnr-plus-circle"></i> RECEIVE </button>
              </div></div></form> 
			  
			  <?php
				}
					?>
                
              </div>
            </div></div>
                  </div>
      
   </div></div></div>  
   <?php
   include'footer.php';
   ?>
