
<?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>
    
    <head>
        <?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_settings_empties'] != 1){
?>
<script>window.location="../main/dashboard.php"</script>
<?php
}
?>
    ?>
    <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
        <link href="../bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
        <link href="../assets/styles.css" rel="stylesheet" media="screen">
        <link href="../assets/DT_bootstrap.css" rel="stylesheet" media="screen">
        <link rel="stylesheet" href="../data-table-css/bootstrap-table.css">
    <link rel="stylesheet" href="../data-table-css/bootstrap-editable.css">
        <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="vendors/flot/excanvas.min.js"></script><![endif]-->
        <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
        <!--[if lt IE 9]>
            <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <script src="../vendors/modernizr-2.6.2-respond-1.1.0.min.js"></script>
    </head>
    
    <body>
        <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.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">Empty Crate Tracking</div>
                                <div class="muted pull-right">
                                    <?php 
                        if( $permissions['empties_customer'] == 1){
                            ?>
                                   
                                    <a href="customers.php"><button class="btn btn-primary">Customers </button></a>
                                    <?php
                         }?>
                           <?php 
                        if( $permissions['empties_categories'] == 1){
                            ?>
                                     <a href="categories.php"><button class="btn btn-primary">Categories </button></a> 
                                     <?php
                         }?>
                         <?php 
                        if( $permissions['add_taken_crates'] == 1){
                            ?>
                                     <a href="#myModal" data-toggle="modal" class="btn btn-primary"><i class="icon-plus icon-white"></i>&nbsp;Add taken crates</a>
                                      <?php
                         }?>

                                 </div>
                                
                            </div>
                            <div id="myModal" class="modal hide">
                                            <div class="modal-header" style="background-color: #08c; color: #ffff">
                                                <button data-dismiss="modal" class="close" type="button">&times;</button>
                                                <h3>Record Taken Crates</h3>
                                            </div>
                                            <div class="modal-body">
                                                <form action="" method="POST">
                                               <div class="form-group">
                                            <label class="control-label" for="select01">Customer</label>
                                          
                                            <select id="select01" class="chzn-select" name="customer" style="width: 100%;" required="required">
                                              <option disabled selected value="">select customer</option>
<?php
$sqlcustomer = "SELECT * FROM customers where business_id='$business' && branch_id='$branch'";
$resultcustomer = mysqli_query($conn, $sqlcustomer);

if (mysqli_num_rows($resultcustomer) > 0) {
    // output data of each row
    while($rowc = mysqli_fetch_assoc($resultcustomer)) {
        ?>
<option value="<?php echo $rowc['id']; ?>"><?php echo $rowc['name']; ?></option>
        <?php
    }
} else {
    echo "0 results";
}

?>
</select>
</div>
<div class="form-group">
                                            <label class="control-label" for="select01">Category</label>
                                          
                                            <select id="select01" class="chzn-select" name="category" style="width: 100%;" required="required">
                                              <option disabled selected value="">select category</option>
<?php
$sqlcategor = "SELECT * FROM empty_categories where business_id='$business' && branch_id='$branch'";
$resultcategory = mysqli_query($conn, $sqlcategor);

if (mysqli_num_rows($resultcategory) > 0) {
    // output data of each row
    while($rowcat = mysqli_fetch_assoc($resultcategory)) {
        ?>
<option value="<?php echo $rowcat['id']; ?>"><?php echo $rowcat['name']; ?></option>
        <?php
    }
} else {
    echo "0 results";
}

?>
</select>
</div>
                                                <label>Crates Taken</label>
                                                <input type="number" name="crates_taken" class="input-xlarge focused" required="" style="width: 100%;">
                                                <label>Description</label>
                                                <textarea name="description" class="input-xlarge focused" required="required" style="width: 100%;"></textarea>
                                                
                                                
                                                <div class="form-actions">
                                          <button type="submit" name="save_crates" class="btn btn-primary">Add</button>
                                          
                                        </div>  
                                                </form>
                                                </div>
                                                </div>
                                                <?php
if (isset($_POST['save_crates'])) {
    $customer=$_POST['customer'];
    $category=$_POST['category'];
    $crates_taken=$_POST['crates_taken'];
    $description=$_POST['description'];
    $insercrates = mysqli_query($con,"INSERT INTO empty_crates(customer_id,category_id,crates_taken,description,branch_id,business_id,added_by) VALUES('$customer','$category','$crates_taken','$description','$branch','$business','$id_user') ");
    if ($insercrates) {
        ?>
        <meta http-equiv="refresh" content="0;URL='crates.php'" />
        <?php
    }else{
        echo "Error".mysqli_error($con);
    }

}
?>
                            <div class="block-content collapse in">
                                <div class="span12">
                                   
                                 
                                        <!--Model Ends here -->
                                                 <?php
                                    include '../includes/connection.php';
                                                                       // Create connection
                                    $sql = mysqli_query($con,"SELECT *,date_format(addedon,'%Y-%m-%d')as date,(SELECT name from customers where id=empty_crates.customer_id) as customer,(SELECT name from empty_categories where id=empty_crates.category_id) as category  from empty_crates where  business_id='$business' && branch_id='$branch' order by crates_taken-crates_returned DESC");
                                    //$result = mysqli_query($con, $sql);

                                    if (mysqli_num_rows($sql) > 0) {
                                     ?>
                                    <?php 
                        if( $permissions['export_crates'] == 1){
                            ?>
                                    <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>
                                    <?php
                         }?>
                                    <table  id="table" data-toggle="table" data-pagination="true" data-search="true" data-show-columns="true" data-show-pagination-switch="true" data-show-refresh="false" data-key-events="true" data-show-toggle="false" data-resizable="false" 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>S/NO</th>
                                                <th>Date</th>
                                                <th>Customer</th>
                                                <th>Category</th>
                                                <th>Description</th>
                                                <th>Number taken</th>
                                                <th>Number returned</th>
                                                <th>Number Remaining</th>
                                                <th>Add returned</th>

                                            </tr>
                                        </thead>
                                        <tbody>
                                        
                                        <?php
                                        $n=0;
while($row = mysqli_fetch_assoc($sql)) {
  $pid=$row['id'];
  $n++;
 
    ?>
<tr class="gradeU">
   


                                                <td><?php echo $n; ?></td>
                                                <td><?php echo $row['addedon']; ?></td>
                                                <td><?php echo $row['customer']; ?></td>
                                                <td><?php echo $row['category']; ?></td>
                                                <td> <?php echo $row['description'];?></td>
                                                <td><?php echo $row['crates_taken']; ?></td>
                                                <td><?php echo $row['crates_returned']; ?></td>
                                                <td><?php echo $row['crates_taken']-$row['crates_returned']; ?></td>
                                                
                                                    
                                                <td>
                                                    <form method="POST" action="">
                                                     <input type="hidden" value="<?php echo $row['crates_returned'];?>" name="actual" >

                                                    <input type="hidden" name="rid" value="<?php echo $pid;?>" required="required">
                                             <input type="number"  name="addm" max="<?php echo $row['crates_taken']-$row['crates_returned']; ?>">
                                                <button class="btn btn-primary" type="submit" name="save">Add </button> 
                                            </form>
                                            </td>
                                                
                                                
                                                
                                              
                                       </tr>
                                            
                                            <?php


    }
    ?>
                                        </tbody>
                                    </table>
                                    <?php                              
} else {
    echo "0 results";
}


?>
<div class="form-actions">
                                        <!-- <a href="save_added.php">  <button  class="btn btn-primary">Save Changes </button></a> -->
                                          
                                        </div>  

<?php
if (isset($_POST['save'])) {
    $rid=$_POST['rid'];
    $actual=$_POST['actual'];
    $addm=$_POST['addm'];
    $newactual=(int)$actual+(int)$addm;
    $updateactual = mysqli_query($con,"UPDATE empty_crates set crates_returned='$newactual' where id='$rid' ");
    if ($updateactual) {
        ?>
        <meta http-equiv="refresh" content="0;URL='crates.php'" />
        <?php
    }else{
        echo "Error".mysqli_error($con);
    }

}
?>
                                        
                                </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======-->
   <script type = "text/javascript">
    function confirmationDelete(anchor){
        var conf = confirm("Are you sure you want to delete this record?");
        if(conf){
            window.location = anchor.attr("href");
        }
    } 
    function confirmationSuspend(anchor){
        var conf1 = confirm("Are you sure you want to Suspend this supplier?");
        if(conf1){
            window.location = anchor.attr("href");
        }
    }  
    function confirmationReturn(anchor){
        var conf2 = confirm("Are you sure you want to Return this Supplier?");
        if(conf2){
            window.location = anchor.attr("href");
        }
    }      
</script>
<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>

