<?php
if(basename($_SERVER['PHP_SELF']) == 'ctaken.php') 
  $cs=" class='current'";
include'header.php';
$custo='';
$conde='';
$t=0;

// delete item from a given chart
if(isset($_POST['delo']))
		{
			$rowid=$_POST['rowid'];
			$then=mysqli_query($conn, "DELETE FROM `cstouse` WHERE `Number`='$rowid' LIMIT 1");
		}

// delete all items from a given chart
if(isset($_POST['delox']))
		{
			$then=mysqli_query($conn, "DELETE FROM `cstouse` WHERE `Action`='TAKEN' AND `Voucher`='0' LIMIT 100");
		}

// edit an item from a given chart
if(isset($_POST['edit']))
		{
			$rowid=$_POST['rowid'];
			$qty=$_POST['qty'];
				$qty=str_replace(',', '', $qty);
			$then=mysqli_query($conn, "UPDATE `cstouse` SET `Quantity`='$qty' WHERE `Number`='$rowid' LIMIT 1");
		}

// Search for an item to be added to the chart
if(isset($_POST['search']))
		{
			$custo=$_POST['custo'];
			$t=1;
		}

// Add found item to the chart
		if(isset($_POST['addo']))
		{
			$n=$_POST['n'];
			while($n>0){
				$item=$_POST["item$n"];
				$qty=$_POST["qty$n"];
				$pri=$_POST["pri$n"];
				$cost=$_POST["cost$n"];
				if($qty)
		$so=mysqli_query($conn, "INSERT INTO `cstouse` (`Number`, `Date`, `Time`, `User`, `Item`, `Cost`, `Quantity`, `Price`, `Action`, `Destin`, `Voucher`, `Status`) VALUES (NULL, '$Date', '$Time', '$loge', '$item', '$cost', '$qty', '$pri', 'TAKEN', '', '0', '0')");
			$n--;
			}
		}

		// edit a given voucher from consumption report
if(isset($_POST['edivo']))
		{
			$vous=$_POST['vous'];

			// Update quantity for items that were taken *****************************************************
   $rece=mysqli_query($conn, "SELECT *FROM `cstouse` WHERE `Action`='TAKEN' AND `Voucher`='$vous' AND `Upda`='1' AND `Status`='0' ORDER BY `Number` DESC");
   if($fece=mysqli_num_rows($rece)){
				while($re=mysqli_fetch_assoc($rece)){
					$nuo=$re['Number'];
					$ito=$re['Item'];
					$qto=$re['Quantity'];
					$dest=$re['Destin'];
					
	$do=mysqli_query($conn, "UPDATE `citems` SET `Quantity`=`Quantity`+'$qto' WHERE `Number`='$ito' ORDER BY `Number` ASC LIMIT 1");
		
	$then=mysqli_query($conn, "UPDATE `cstouse` SET `Voucher`='0', `Upda`='0', `Closing`='0' WHERE `Number`='$nuo' ORDER BY `Number` ASC LIMIT 1");
				}
   }
		}

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

		$rece=mysqli_query($conn, "SELECT `Voucher` FROM `cstouse` WHERE `Voucher`!='0' ORDER BY `Voucher` DESC LIMIT 1");
				$re=mysqli_fetch_assoc($rece);
					$vou=$re['Voucher']+1;
					$vous=$re['Voucher']+1;

// Close the current chart
		if(isset($_POST['receive']))
		{
			$supplier=$_POST['supplier'];
			$dato=$_POST['dato'];
			$taker=$_POST['taker'];
			$otype=$_POST['otype'];
			$comment=$_POST['comment'];
			$comment=str_replace("'", "`", $comment);
	$so=mysqli_query($conn, "UPDATE `cstouse` SET `Date`='$dato', `Time`='$Time', `Destin`='$supplier', `Voucher`='$vou', `Invoice`='MAIN STORE', `Person`='$taker', `Comment`='$comment', `Otype`='$otype' WHERE `Status`='0' AND `Voucher`='0' AND `Action`='TAKEN'");

	$see=mysqli_query($conn, "SELECT *FROM `cstouse` WHERE `Voucher`='$vou' AND `Action`='TAKEN' AND `Status`='0' AND `Date`='$dato'");
	    while($ree=mysqli_fetch_assoc($see)){
	        $num=$ree['Number'];
	        $item=$ree['Item'];
	        $cost=$ree['Cost'];
	        $qty=$ree['Quantity'];
	        $pri=$ree['Price'];
	        $sale=$ree['Price'];
	    
	$itms=mysqli_query($conn, "SELECT `citems`.`Price`, `itype`.`Type`, `items`.`Iname`, `items`.`Descri` FROM `items` INNER JOIN `itype` ON `items`.`Type`=`itype`.`Number` WHERE `items`.`Number`='$item'");
	    $rims=mysqli_fetch_assoc($itms);
	        $itype=$rims['Type'];
	        $iprix=$rims['Price'];
	        $inam=$rims['Iname'];
	        $idesc=$rims['Descri'];
    
   
    $then=mysqli_query($conn, "UPDATE `cstouse` SET `Paid`='1' WHERE `Number`='$num' ORDER BY `Number` LIMIT 1");
	    }
			}
			
			
			
				// Update quantity when there is any stock taken *****************************************************
   $recex=mysqli_query($conn, "SELECT *FROM `cstouse` WHERE `Action`='TAKEN' AND `Voucher`!='0' AND `Upda`='0' AND `Status`='0' ORDER BY `Voucher` DESC LIMIT 60");
   if($fecex=mysqli_num_rows($recex)){
				while($rex=mysqli_fetch_assoc($recex)){
					$nuo=$rex['Number'];
					$ito=$rex['Item'];
					$qto=$rex['Quantity'];

$dow=mysqli_query($conn, "UPDATE `citems` SET `Quantity`=`Quantity`-'$qto', `Star`=`Star`+'1' WHERE `Number`='$ito' ORDER BY `Number` ASC LIMIT 1");
	
	$dova=mysqli_query($conn, "SELECT *FROM `citems` WHERE `Number`='$ito' ORDER BY `Number` DESC LIMIT 1");
			$rova=mysqli_fetch_assoc($dova);
				$qty=$rova['Quantity'];
	$thent=mysqli_query($conn, "UPDATE `cstouse` SET `Upda`='1', `Closing`='$qty' WHERE `Number`='$nuo' ORDER BY `Number` ASC LIMIT 1");
				}
   }

		
?>
<div class="container-fluid main-content">
        <div class="page-title">
          <h2 style='margin-top:-20px; margin-bottom: 5px; color:#ffcc33'>
         Construction
          </h2>
                 </div>

<style type="text/css" media="screen">
 .hiddenDiv {
 display: none;
 }
 .visibleDiv {
 display: block;
 border: 1px grey solid;
 margin-top: 5px;
  margin-bottom: 0px;
   padding-top: 5px;
    padding-bottom: 5px;
 }
 </style>
     
        <div class="row">
  <div class="col-lg-2 hidden-print">
   
   <ul class="list-group">

    <li class="list-group-item">
	  <a href="cmainsto.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Store Status
                </p>
              </a></li> 

	   <li class="list-group-item">
              <a href="ccrete.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Create New Items
                </p>
              </a></li>  
                  
			  <li class="list-group-item">
              <a href="creceive.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Receive Items
                </p>
              </a></li> 
			  
			  <li class="list-group-item active">
              <a href="ctaken.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Store Consumption
                </p>
              </a></li>    
<?php
if($_SESSION['Spay']){
    ?>
			  <li class="list-group-item">
              <a href="cbillpay.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Supplier Payment
                </p>
              </a></li> 
              <?php
}
?>
                       
            </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="csearchs" 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($conn, "SELECT *FROM `citems` WHERE `Store`<='2' 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;Type </th>
                       <th> Item&nbsp;Name </th>
                        <th> Description </th>
             <th colspan='2'>&nbsp;&nbsp;Qty&nbsp;In&nbsp;/&nbsp;Unit </th>
					<th> Cost&nbsp;Price </th>
                        <th><div align='center'> Quantity </th>
						<th class="hidden-xs hidden-print">Total&nbsp;Amount</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'];
$store=$ro['Store'];			
if($store=='1')		
	$cost=$ro['Cost'];
else
	$cost=$ro['Price'];
	
$costo=number_format($cost, 2);

			$qt=$ro['Quantity'];				$qty=number_format($qt, 2);

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

				$dox=mysqli_query($conn, "SELECT *FROM `cunit` WHERE `Number`='$uno' ORDER BY `Number` ASC");
					$rox=mysqli_fetch_assoc($dox);
							$unit=$rox['Unit'];
          $b=$n*10;
          		
				    $costo=number_format($cost, 2);
				    $id="id='result$n'";
				    $dsa="";
				    $typ="text";
				
		print("<tr><td class=hidden-xs><div align='center'>$n&nbsp;&nbsp;</td><td> $type </td><td> $iname </td><td> $descri </td><td><div align='right'> $qty </td><td> $unit </td>
				<td><input name='pri$n' class='form-control' type='$typ' onkeyup='multiplyBy$n();' onkeypress='return isNumberKey(event)' style='text-align:right; width:130px; height:24px;' id='box$b' value='$cost' $dsa></td>
				<td><input name='qty$n' class='form-control' type='text' onkeyup='multiplyBy$n();' onkeypress='return isNumberKey(event)' style='text-align:right; width:120px; height:24px; margin:0px 0px 0px 0px' id='box$n'></td>
				<td class='hidden-xs hidden-print' width='10%'><div align='right' style='border:1px solid #cccccc; width:120px; height:24px; border-radius:5px; padding:0px 10px 0px 0px;'><input type='hidden' name='item$n' value='$code'>
						<span class='dollars' style='text-align:right; width:130px; height:24px; font-size:18px; color:#66cccc; float:center; text-align:right;' $id onchange='format(this);'></span></div><input type='hidden' name='cost$n' value='$cost'></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; Consumption Voucher No : <b> $vou </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 found for your search [$custo] </div><br><br><br><br><br><br><br>";
					}  
			}
				   else{
		$dor=mysqli_query($conn, "SELECT *FROM `cstouse` WHERE `Status`='0' AND `Voucher`='0' AND `Action`='TAKEN' ORDER BY `Number` DESC");
				if($for=mysqli_num_rows($dor)){
			  $click=0;
?>
 <div class="row">
            <div class="col-lg-12" style='margin-top:-20px;'>
             <span> &nbsp;&nbsp; Consumption Voucher No : <b><?php echo" $vou " ?></b></span>
            <div class="widget-container fluid-height clearfix">
            <div class="widget-content padded clearfix">

<table class="table table-striped">     
                                      <thead>
                    <tr role="row">
                     <th class="hidden-xs"> No </th>
                       <th> Item&nbsp;Type </th>
                       <th> Item&nbsp;Name </th>
                        <th> Description </th>
        <th colspan='2'>&nbsp;&nbsp;Qty&nbsp;In&nbsp;/&nbsp;Unit</th>
						 <th> Cost&nbsp;Price </th>
                        <th><div align='center'> Quantity </th>
						<th>Total&nbsp;Amount</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'];
						$dst=$ror['Destin'];
						$dte=$ror['Date'];
						$pers=$ror['Person'];
$pri=$ror['Price'];
	$do=mysqli_query($conn, "SELECT *FROM `citems` 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'];
			
			$qty=number_format($qt, 2);			$costo=number_format($cost, 2);				$prio=number_format($pri, 2);			

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

				$dox=mysqli_query($conn, "SELECT *FROM `cunit` WHERE `Number`='$uno' ORDER BY `Number` ASC");
					$rox=mysqli_fetch_assoc($dox);
							$unit=$rox['Unit'];
							if($cost==$sale)
								$arr="";
							elseif($cost>$sale)
								$arr="<i class='lnr lnr-arrow-down'></i>";
							else
								$arr="<i class='lnr lnr-arrow-up'></i>";
          $to=$pri*$qt;				$too=number_format($to, 2);			$qino=number_format($qin, 2);

		  if($qin<$qt){
			  $stl="style='color:#ff3333;'";
			  $click++;
		  }
		  else{
			  $stl='';
		  }
		  
	     $prio=number_format($pri, 2);
	     $too=number_format($to, 2);	 
	     
		print("<tr><form action='' method='post'>
                        <td $stl class=hidden-xs><div align='center'>$n&nbsp;&nbsp;</td><td $stl> $type </td>
						<td $stl> $iname </td><td $stl> $descri </td><td $stl><div align='right'> $qino </td>
						<td $stl> $unit </td><td $stl><div align='right'>&nbsp;$prio&nbsp;&nbsp;&nbsp;&nbsp;</td>
						<td $stl><div align='right'><input name='qty' class='form-control' type='text' onkeyup='format(this);' onkeypress='return isNumberKey(event)' onChange=this.style.color='#ff3366' style='text-align:right; width:80px; height:24px; margin:0px 0px 0px 0px;' value='$qty'></td><td $stl><div align='right'>$too</td>
						
						
						<td class='hidden-xs hidden-print' align='right' style='width:40px; padding:0px;'>
                              <input type='hidden' name='rowid' value='$code'>
                          <button type='submit' class='btn btn-xs btn-warning hidden-print' name='edit' style='height:18px; padding:0px;' title='Edit' data-toggle='tooltip' data-placement='top'>
						  &nbsp;&nbsp;<i class='lnr lnr-pencil'></i>&nbsp;&nbsp;</button></td></form>
						  
						  <form method=post action=''><td class='hidden-xs hidden-print' align='right' style='width:40px; padding:0px;'>
                              <input type='hidden' name='rowid' value='$code'>
                          <button type='submit' class='btn btn-xs btn-danger hidden-print' name='delo' style='height:18px; padding: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>

					 <thead>
					<tr><th class="hidden-xs"> </th><th colspan='5'><div align='center'> Grand Total </th>
					<th colspan='3'><div align='right'><?php echo $toto ?></th><th colspan='2'><div align='center'> -- </th></tr>
                  </table><br>
		
			<label class="control-label col-md-1"> </label>

				  <form method='post' action=''><label class="control-label col-md-3">
			  <select class="form-control" name="supplier" onchange='showDiv(this.value);' onclick="return pageScroll()" required>			
			 <?php
				if($dst=='DAMAGED/EXPIRED')
					$d='selected';
				else
					$d='';
					
				if($dst=='CONSTRUCTION')
					$u='selected';
				else
					$u='';
					
				if($dst=='LOGISTICS')
					$l='selected';
				else
					$l='';

				if($dst=='')
					$dte=$Date;

				echo"<option value='' selected='selected'> SELECT DESTINATION </option>
				<option value='DAMAGED/EXPIRED' $d> DAMAGED/EXPIRED </option>
				<option value='CONSTRUCTION' $u> CONSTRUCTION </option>
			<option value='LOGISTICS' $l> LOGISTICS </option>";
	
			?>			    
            </select></label>

	<label class="control-label col-md-3">
<input list="take" name="taker" class="form-control" placeholder="TAKEN BY" OnKeyup="return cUpper(this);" required></label>
<datalist id="take">
 <?php
			 $top=mysqli_query($conn, "SELECT `Person` FROM `cstouse` WHERE `Status`='0' AND `Person`!='' GROUP BY `Person` ORDER BY `Date` DESC LIMIT 30");
						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 $dte ?>" onclick="return pageScroll()"></label>
<?php
		if($click=='0'){
			$btl='submit';
$tle="title='Save' data-toggle='tooltip' data-placement='top'";
}
	else{
		$btl="button";
		$tle="title='Some items are missing in your store' data-toggle='tooltip' data-placement='top'";
	}
	?>
			 <label class="control-label col-md-2">
			 <button class="btn btn-md btn-block btn-info" type="<?php echo $btl ?>" name="receive" <?php echo $tle ?>>
			 <i class="lnr lnr-plus-circle"></i> CONSUME </button>
			 </label>
			 
			  <label class="control-label col-md-1" style="text-align:right;">
			    <?php
				  if($n>3){
				  echo"<button type='submit' class='btn btn-md btn-danger hidden-print' name='delox' title='Remove All' data-toggle='tooltip' data-placement='top'>&nbsp;&nbsp;<i class='lnr lnr-trash'></i>&nbsp;&nbsp;</button>";
			  }
			  ?>
			  </label>

		<?php
				}
					else{
						echo"<div class='row'>
            <div class='col-lg-12' style='margin-top:-20px;'>
             <span> &nbsp;&nbsp; Consumption Voucher No : <b> $vou </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 consumed </div><br><br><br><br><br><br><br>";
					}
			}
					?>
                                      
                
              </div>
            </div></div>
                  </div>

				  <div id="DAMAGED/EXPIRED" class="hiddenDiv" style="border:0px; height:32px; margin:0px; padding:0px; text-align='right'"><br>
		 <label class="control-label col-md-2"> </label>

		 <label class="control-label col-md-2 pull-right"><br><br><div class="form-check"> 
		 <input type="radio" name="otype" value="DAMAGED" class="form-check-input">&nbsp;<font color='#ff0033'>Damaged</font>
		 <br><br><input type="radio" name="otype" value="EXPIRED" class="form-check-input" checked>&nbsp;<font color='#ff66cc'>Expired</font>
		 </div></label>

		  <label class="control-label col-md-8"> &nbsp;&nbsp;&nbsp;&nbsp; <font color="#0033ff">Write comment here &nbsp; &#9660;</font>
		<textarea  class="form-control" name="comment" rows="4" cols="80"> </textarea>
			</label>
				  </div>
      </form>
   </div></div></div>  
   <?php
   include'footer.php';
   ?>
