
<?php
session_start();
error_reporting(0);
@ini_set('display_errors', 0);
$id_user=$_SESSION['id'];
$branch=$_SESSION['branch_id'];
$business=$_SESSION['business_id'];
?>
<!DOCTYPE html>
<html>
    <script type="text/javascript">
    function FormatCurrency(ctrl) {
                  //Check if arrow keys are pressed - we want to allow navigation around textbox using arrow keys
                  if (event.keyCode == 37 || event.keyCode == 38 || event.keyCode == 39 || event.keyCode == 40) {
                      return;
                  }

                  var val = ctrl.value;

                  val = val.replace(/,/g, "")
                  ctrl.value = "";
                  val += '';
                  x = val.split('.');
                  x1 = x[0];
                  x2 = x.length > 1 ? '.' + x[1] : '';

                  var rgx = /(\d+)(\d{3})/;

                  while (rgx.test(x1)) {
                      x1 = x1.replace(rgx, '$1' + ',' + '$2');
                  }

                  ctrl.value = x1 + x2;
              }

              function CheckNumeric() {
                  return event.keyCode >= 48 && event.keyCode <= 57 || event.keyCode == 46;
              }
</script>
<?php
    include("../includes/header-links.php");
    
$permissions = mysqli_fetch_array(mysqli_query($con,"SELECT * FROM  
  permissions,users WHERE  permissions.role_id = users.role 
  AND users.id='$id_user' AND users.business_id ='$business' AND users.branch_id ='$branch'"));

if($permissions['nav_products'] != 1){
?>
<script>window.location="../main/dashboard.php"</script>
<?php
}
?>
    
    <head>

    
    <link rel="stylesheet" href="../data-table-css/bootstrap-table.css">
    <link rel="stylesheet" href="../data-table-css/bootstrap-editable.css">
        
    </head>
    
    <body>
        <?php include '../includes/header.php'; ?>
        
        <div class="container-fluid">
    
            <div class="row-fluid">
                <?php include '../includes/sidebar.php'; ?>
                <!--/span-->
                <div class="span10" id="content">
          

                     <div class="row-fluid">
                        <!-- block -->
                        <div class="block">
            
                            <div class="navbar navbar-inner block-header">
                                <div class="muted pull-left"> Consumables</div>
                            </div>
                            <div class="block-content collapse in">
              
                                <div class="span12">
<div class="row">
  <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">

  </div>
  <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
  </div>     
</div>
                                   
        
                                    
                            

                
                                   <?php 
                                   //if($_SESSION['role']=="admin"|| $_SESSION['role']=="Super Admin"){
                                   if( $permissions['add_product'] == 1){
                                    ?>
                                            <div class="btn-group">
                                         <a href="#myModal" data-toggle="modal" class="btn btn-success"><i class="icon-plus icon-white"></i>&nbsp;Add New</a>
                                      </div>
                                          <div class="btn-group">
                                         <a href="#ConsumableModal" data-toggle="modal" class="btn btn-primary"><i class="icon-briefcase icon-white"></i>&nbsp;consumable stocking</a>
                                      </div>
                                      <div class="btn-group">
                                       <a href="#ConsumableUsageModal" data-toggle="modal" ><button class="btn btn-warning"> <i class="icon-indent-left"></i> Consumable usage</button></a>
                                     </div>
                                      <div class="btn-group">
                                       <a href="stocking_report.php" ><button class="btn btn-info"> <i class="icon-indent-left"></i>Stocking report</button></a>
                                     </div>
                                     <div class="btn-group">
                                       <a href="usage_report.php" ><button class="btn btn-info"> <i class="icon-indent-left"></i>Usage report</button></a>
                                     </div>
                                     

                                    <?php
                                   }
                                   ?>
                                      

                                      <!--Model begins here -->
                                      <div id="myModal" class="modal hide">
                      <div class="modal-header" style="background-color: #08c; color: white;">
                        <button data-dismiss="modal" class="close" type="button">&times;</button>
                        <h3>Add new consumable item</h3>
                      </div>
                      <div class="modal-body" style="max-height: 550px;">
                        <form action="" method="POST" enctype="multipart/form-data"> 

                         
                      <div align="center" class="12">
                        <label>Item Name</label>
                        
                        <input type="text" name="name" class="form-control" placeholder="Item Name" style="width: 94%;" required="required">
                      </div>
                      
                      
                       
                      <div align="center" class="span12">
                        <label>Units</label>
                        <select name="units" class="form-control" required style="width: 95%;">
                          <option value="">Select units</option >
                          <?php
                          include("../includes/connection.php");
    
                          $query = mysqli_query($con,"select * from units where branch_id='$branch'");
                          while($row0=mysqli_fetch_array($query)){
                          ?>
                          <option value="<?php echo $row0['UnitID'];?>"><?php echo$row0['Symbol'];?></option>
                          <?php
                          
                          }
                          ?>
                          
                        </select>
                      </div>
                      <div align="center" id="PcodeMin" class="span12">
                       <div align="center" class="span12">
                         <label>Description</label>
                        
                        <textarea  name="description" class="form-control" placeholder="Item description" style="width: 95%;"></textarea>
                      </div>

                      
                      </div>
                      <div align="center" id="description" class="span12">
                       
                     
                      
                      
                        <div class="form-actions">
                                          <button type="submit" name="add" class="btn btn-primary">Add Item</button>
                                          <a href="#" data-dismiss="modal" class="btn btn-danger">Cancel</a>
                                        </div> 
                                        </div> 
                        </form>
                      </div>
                      </div>
                    </div>
                    <!--Model Ends here -->

                     <!--Model stock begins here -->
                                      <div id="ConsumableModal" class="modal hide">
                      <div class="modal-header" style="background-color: #08c; color: white;">
                        <button data-dismiss="modal" class="close" type="button">&times;</button>
                        <h3>Consumable stocking</h3>
                      </div>
                      <div class="modal-body" style="max-height: 550px;">
                        <form action="" method="POST" enctype="multipart/form-data"> 

                         
                      <div align="center" class="12">
                        <label>Item Name</label>
                        
                        <select id="select02" class="form-control chzn-select" name="item"  required="required" style="width: 80%; font-size: 18px; height: 35px;" >
                          <option disabled selected value="" >Select Item</option>
                           <?php
                          include("../includes/connection.php");
    
                          $query = mysqli_query($con,"SELECT * from consumable_items where branch_id='$branch' && status=1");
                          while($row0=mysqli_fetch_array($query)){
                          ?>
                          <option value="<?php echo $row0['id'];?>"><?php echo $row0['name'];?></option>
                          <?php
                          
                          }
                          ?>
                                      
                          </select>
                                            
                                            
                                          
                      </div>
                      
                       <div align="center" class="span12">
                         <label>Quantity</label>
                         <input class="form-control" name="quantity" id="quantitya" type="number" value="Enter Quantity" required="required" step="any" style="width: 80%; font-size: 18px; height: 30px;">
                        
                        
                      </div>
                      <div align="center" class="span12">
                         <label>Unit price</label>
                         <input class="input-xlarge focused" name="unitprice" id="focusedInput" type="text" onkeypress="return CheckNumeric()" onkeyup="FormatCurrency(this)" required="required"  style="width: 80%; font-size: 18px; height: 30px;">
                        
                      </div>

                      
                      <div align="center" class="span12">
                      
                        <div class="form-actions" style="width: 80%">
                                          <button type="submit" name="additemstock" class="btn btn-primary">Add stock</button>
                                          <a href="#" data-dismiss="modal" class="btn btn-danger">Cancel</a>
                                        </div> 
                        </div> 
                        </form>
                      </div>
                      </div>
                    <!--Model stock Ends here -->
                    <!--Model usage begins here -->
                                      <div id="ConsumableUsageModal" class="modal hide">
                      <div class="modal-header" style="background-color: #08c; color: white;">
                        <button data-dismiss="modal" class="close" type="button">&times;</button>
                        <h3>Record Consumables Used</h3>
                      </div>
                      <div class="modal-body" style="max-height: 550px;">
                        <form action="" method="POST" enctype="multipart/form-data"> 

                         
                      <div align="center" class="12">
                        <label>Item Name</label>
                        
                        <select id="select02" class="form-control chzn-select" name="item"  required="required" style="width: 80%; font-size: 18px; height: 35px;" >
                          <option disabled selected value="" >Select Item</option>
                           <?php
                          include("../includes/connection.php");
    
                          $query = mysqli_query($con,"SELECT * from consumable_items where branch_id='$branch' && status=1");
                          while($row0=mysqli_fetch_array($query)){
                          ?>
                          <option value="<?php echo $row0['id'];?>"><?php echo $row0['name']." (".$row0['quantity']." in stock )";?></option>
                          <?php
                          
                          }
                          ?>
                                      
                          </select>
                                            
                                            
                                          
                      </div>
                      
                       <div align="center" class="span12">
                         <label>Quantity</label>
                         <input class="form-control" name="quantity" id="quantitya" type="number" value="Enter Quantity" required="required" step="any" style="width: 80%; font-size: 18px; height: 30px;">
                        
                        
                      </div>
                      <div align="center" class="span12">
                         <label>Taken By</label>
                         <input class="input-xlarge focused" name="takenby" id="focusedInput" type="text"  style="width: 80%; font-size: 18px; height: 30px;">
                        
                      </div>
                      <div align="center" class="span12">
                         <label>Date</label>
                         <input type="date"class="input-xlarge focused" name="date"   value="<?php echo date('Y-m-d'); ?>"  style="width: 80%; font-size: 18px; height: 30px;">
                        
                      </div>

                      
                      <div align="center" class="span12">
                      
                        <div class="form-actions" style="width: 80%">
                                          <button type="submit" name="recordusage" class="btn btn-primary">Add stock</button>
                                          <a href="#" data-dismiss="modal" class="btn btn-danger">Cancel</a>
                                        </div> 
                        </div> 
                        </form>
                      </div>
                      </div>
                    <!--Model stock Ends here -->
                                   
                                   <?php
include '../includes/connection.php';
       // if($_SESSION['role']=="Super Admin"){

                  
             $sql = mysqli_query($con,"SELECT *,(SELECT Name from units where UnitID=consumable_items.unit_id) as unit FROM consumable_items where status=1  && branch_id='$branch'");

              if (mysqli_num_rows($sql) > 0) {
                // output data of each row

                                   ?>
                                    <div class="datatable-dashv1-list custom-datatable-overright">
                                    <div id="toolbar">
                                        <!-- <select class="form-control">
                                                <option value="">Export Basic</option>
                                                <option value="all">Export All</option>
                                                <option value="selected">Export Selected</option>
                                            </select> -->
                                    </div>
                                    <table  id="table" data-toggle="table" data-pagination="true" data-search="true" data-show-columns="true" data-show-pagination-switch="true" data-show-refresh="true" data-key-events="true" data-show-toggle="true" data-resizable="true" data-cookie="true"
                                        data-cookie-id-table="saveId" data-show-export="true" data-click-to-select="true" data-toolbar="#toolbar" data-export-options='{"fileName": "Testing_filename"}'>
                                        <thead>
                                            <tr>
                                                <th>ID</th>
                                                <th>Item</th>
                                                <th>Quantity</th>
                                                <th>Units</th>
                                                <th>Description</th>
                                                <th>Action</th>
                                                
                                             </tr>
                                        </thead>
                                        <tbody>
                                        
                                        <?php
                                        $no=0;
while($row = mysqli_fetch_assoc($sql)) {
   $no++;

        ?>
<tr class="gradeU">
                                                <td><?php echo $no; ?></td>
                                                
                                                <td><?php echo $row['name']; ?></td>
                                                <td><?php echo $row['quantity']; ?></td>
                                               
                                                <td><?php echo $row['unit']; ?></td>
                                                <td><?php echo $row['description']; ?></td>
                                                
                                                <td class="center">
                                             
                                                
                                                  
                                                  <a href="#<?php echo $row['id']?>myModal" data-toggle="modal" class="btn btn-primary"><i class="icon-edit icon-white"></i></a>
                                                  <a href="delete_item.php?id=<?php echo base64_encode($row['id']); ?>"><button class="btn btn-danger"><i class="icon-trash icon-white"></i></button></a>
                                                  
                  
                                          
                                                </td>
                                                           <div id="<?php echo $row['id']?>myModal" class="modal hide">
                      <div class="modal-header" style="background-color: #08c; color: white;">
                        <button data-dismiss="modal" class="close" type="button">&times;</button>
                        <h3>Update Details of Item <?php echo $row['name']; ?> </h3>
                      </div>
                      <div class="modal-body" style="max-height: 550px;">
                        <form action="" method="POST" enctype="multipart/form-data"> 

                         
                      <div align="center" class="12">
                        <label>Item Name</label>
                        <input type="hidden" name="editid" value="<?php echo $row['id']; ?>">
                        <input type="text" name="name" class="form-control" value="<?php echo $row['name']; ?>" style="width: 94%;" required="required">
                      </div>
                      
                      
                       
                      <div align="center" class="span12">
                        <label>Units</label>
                        <select name="units" class="form-control" required style="width: 95%;">
                          <option value="<?php echo $row['unit_id']; ?>"><?php echo $row['unit']; ?></option >
                          <?php
                          include("../includes/connection.php");
    
                          $query = mysqli_query($con,"select * from units where branch_id='$branch'");
                          while($row0=mysqli_fetch_array($query)){
                          ?>
                          <option value="<?php echo $row0['UnitID'];?>"><?php echo$row0['Symbol'];?></option>
                          <?php
                          
                          }
                          ?>
                          
                        </select>
                      </div>
                      <div align="center" id="PcodeMin" class="span12">
                       <div align="center" class="span12">
                         <label>Description</label>
                        
                        <textarea  name="description" class="form-control" placeholder="Item description" style="width: 95%;"><?php echo $row['description']; ?></textarea>
                      </div>

                      
                      </div>
                      <div align="center" id="description" class="span12">
                       
                     
                      
                      
                        <div class="form-actions">
                                          <button type="submit" name="updateitem" class="btn btn-primary">Save changes</button>
                                          <a href="" data-dismiss="modal" class="btn btn-danger">Cancel</a>
                                        </div> 
                                        </div> 
                        </form>
                      </div>
                      </div>
                                                
                                            </tr>
                                            
                                            <?php


    }
} else {
    echo "No Items added yet!";
}

mysqli_close($con);

 ?>
                                            
                                        </tbody>
                                    </table>
                                  </div>
                                    
                                </div>
                            </div>
                        </div>
                        <!-- /block -->
                    </div>
                </div>
            </div>
            <hr>
            <?php include '../includes/footer.php'; ?>
        </div>
        <!--/.fluid-container-->

        <!--==========Footer js======-->
    <?php require_once("../includes/footerjs.php");?>
        <!--==========Footer js======-->
       
 


<?php


if(isset($_POST['add'])){
  include '../includes/connection.php';
$name=mysqli_real_escape_string($con,ucwords($_POST['name']));
$description=mysqli_real_escape_string($con,$_POST['description']);
$sql0 = mysqli_query($con,"SELECT * from consumable_items where name='$name' && branch_id='$branch'");
if(mysqli_num_rows($sql0)>0){
  echo"<script>alert('Failed! Item name already exists'),history.go(-1)</script>";
}else{ 
      
$id_user=$_SESSION['id'];
  
$unit=$_POST['units'];
if (strlen($unit)<1) {
  $units=0;
}else{
  $units=$unit;
}
//  $bad_symbols = array(",", ".");


// $unitprice= str_replace($bad_symbols, "", $unitprice);
$sql = mysqli_query($con,"INSERT INTO consumable_items (name,description,unit_id,branch_id,business_id,AddedBy)
VALUES ('$name','$description','$units','$branch','$business','$id_user')");

if ($sql) {


    $id_user=$_SESSION['id'];
  $activity="Added a new consumable item ".$name;
     

 $log_sql=mysqli_query($con,"INSERT INTO logs(user_id, activity,branch_id,business_id) VALUES 
('$id_user','$activity','$branch','$business')");

if ($log_sql) {

  ?>
    <meta http-equiv="refresh" content="0;URL='consumables.php'" />  
    <?php


} else {
    echo "Error: Failed to add log <br>" . mysqli_error($con);
}
    
    
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
}
mysqli_close($con);
}

?>
<?php


if(isset($_POST['updateitem'])){
  include '../includes/connection.php';
$editid=$_POST['editid'];
$name=mysqli_real_escape_string($con,ucwords($_POST['name']));
$description=mysqli_real_escape_string($con,$_POST['description']);
$sql0 = mysqli_query($con,"SELECT * from consumable_items where name='$name' && branch_id='$branch' && id!='$editid'");
if(mysqli_num_rows($sql0)>0){
  echo"<script>alert('Failed! Item name already exists'),history.go(-1)</script>";
}else{ 
      
$id_user=$_SESSION['id'];
  
$unit=$_POST['units'];
if (strlen($unit)<1) {
  $units=0;
}else{
  $units=$unit;
}
//  $bad_symbols = array(",", ".");


// $unitprice= str_replace($bad_symbols, "", $unitprice);
$sql = mysqli_query($con,"UPDATE consumable_items SET name='$name',description='$description',unit_id='$units' where id='$editid'");

if ($sql) {


    $id_user=$_SESSION['id'];
  $activity="Update details of a consumable item ".$name;
     

 $log_sql=mysqli_query($con,"INSERT INTO logs(user_id, activity,branch_id,business_id) VALUES 
('$id_user','$activity','$branch','$business')");

if ($log_sql) {


  ?>
    <meta http-equiv="refresh" content="0;URL='consumables.php'" />  
    <?php


} else {
    echo "Error: Failed to add log <br>" . mysqli_error($con);
}
    
    
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
}
mysqli_close($con);
}

?>
<?php


if(isset($_POST['additemstock'])){
  include '../includes/connection.php';
$item=mysqli_real_escape_string($con,ucwords($_POST['item']));
$quantity=mysqli_real_escape_string($con,$_POST['quantity']);
$unitp=mysqli_real_escape_string($con,$_POST['unitprice']);
      
$id_user=$_SESSION['id'];
  

$bad_symbols = array(",", ".");

$unitprice= str_replace($bad_symbols, "", $unitp);


$sql = mysqli_query($con,"INSERT INTO consumable_stocking (item_id,quantity,unit_price,branch_id,business_id,AddedBy)
VALUES ('$item','$quantity','$unitprice','$branch','$business','$id_user')");

if ($sql) {
 mysqli_query($con,"UPDATE consumable_items SET quantity=quantity+$quantity where id='$item'");


    $id_user=$_SESSION['id'];
  $activity="Added  stock for consumable item ".$item;
     

 $log_sql=mysqli_query($con,"INSERT INTO logs(user_id, activity,branch_id,business_id) VALUES 
('$id_user','$activity','$branch','$business')");

if ($log_sql) {

  ?>
    <meta http-equiv="refresh" content="0;URL='consumables.php'" />  
    <?php


} else {
    echo "Error: Failed to add log <br>" . mysqli_error($con);
}
    
    
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($con);
}


mysqli_close($con);
}

?>
<?php


if(isset($_POST['recordusage'])){
  include '../includes/connection.php';
$item=mysqli_real_escape_string($con,ucwords($_POST['item']));
$quantity=mysqli_real_escape_string($con,$_POST['quantity']);
$takenby=mysqli_real_escape_string($con,ucwords($_POST['takenby']));
$date=mysqli_real_escape_string($con,$_POST['date']);
      
$id_user=$_SESSION['id'];


$sql = mysqli_query($con,"INSERT INTO consumable_usage (item_id,quantity,given_to,date,branch_id,business_id,AddedBy)
VALUES ('$item','$quantity','$takenby','$date','$branch','$business','$id_user')");

if ($sql) {
 mysqli_query($con,"UPDATE consumable_items SET quantity=quantity-$quantity where id='$item'");


    $id_user=$_SESSION['id'];
  $activity="Record usage  consumable item ".$item;
     

 $log_sql=mysqli_query($con,"INSERT INTO logs(user_id, activity,branch_id,business_id) VALUES 
('$id_user','$activity','$branch','$business')");

if ($log_sql) {

  ?>
    <meta http-equiv="refresh" content="0;URL='consumables.php'" />  
    <?php


} else {
    echo "Error: Failed to add log <br>" . mysqli_error($con);
}
    
    
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($con);
}


mysqli_close($con);
}

?>
<script src="../data-table-js/bootstrap-table.js"></script>
    <script src="../data-table-js/tableExport.js"></script>
    <script src="../data-table-js/data-table-active.js"></script>
    <script src="../data-table-js/bootstrap-table-editable.js"></script>
    <script src="../data-table-js/bootstrap-editable.js"></script>
    <script src="../data-table-js/bootstrap-table-resizable.js"></script>
    <script src="../data-table-js/colResizable-1.5.source.js"></script>
    <script src="../data-table-js/bootstrap-table-export.js"></script>
    </body>
</html>
