<!DOCTYPE html>
<?php 
$newid=$_GET['id'];
?>
<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>
    
    <head>
        <title>Tables</title>
        <!-- Bootstrap -->
        <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">
        <!--[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>
        <?php include '../includes/header.php'; ?>
        <div class="container-fluid">
            <div class="row-fluid">
                <?php include '../includes/sidebar.php'; ?>
                <!--/span-->
                <div class="span5" id="content">

                        <div class="row-fluid">
                        <!-- block -->
                        <div class="block">
                            <div class="navbar navbar-inner block-header">
                                <div class="muted pull-left">Orders</div>
                                <div class="muted pull-right"><button class="btn btn-primary"> Process Order</button></div>
                            </div>
                            <div class="block-content collapse in">
                                <div class="span12">
                                    <?php
include '../includes/connection.php';


$sql = "SELECT id, (select product_name from products where id=getstock.product_id)as product_name, quantity, unitprice,expiry_date,batch_no from getstock where branch_id='$branch'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
   ?>
  									<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
										<thead>
											<tr>
												<th>Id</th>
												<th>Product Name</th>
												<th>Quantity</th>
												<th>Unit Price</th>
                        <th>Total Price</th>
												
                                                
											</tr>
										</thead>
										<tbody>
                                        <?php
                                         // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        ?>
        <tr class="odd gradeX">
        
                                                <td><?php echo $row['id']; ?></td>
                                                <td><?php echo $row['product_name']; ?></td>
                                                <td><?php echo $row['quantity']; ?></td>
                                                <td class="center"> <?php echo $row['unitprice']; ?></td>
                                                <td class="center"><?php echo number_format($row['unitprice']*$row['quantity']); ?></td>
                                              
                                                
                                            </tr>
                                            <?php
    }
} else {
    echo "0 results";
}

mysqli_close($conn);
?>
											
											
										</tbody>
									</table>
                                </div>
                            </div>
                        </div>
                        <!-- /block -->
                    </div>

         
                </div>
                <div class="span4">
                <?php include 'edit_values.php'; ?>
                                     <form class="form-horizontal" action="" method="POST"> 
                                      <fieldset>
                                        <legend>Add new Order</legend>
                                        <div class="control-group">
                                          <label class="control-label" for="selectError">Product</label>
                                          <div class="controls">
                                          <input class="input-xlarge focused" name="product" id="focusedInput" type="text" value="<?php echo $prod_name; ?>" readonly>
                                          <input type="hidden" name="product_id" value="<?php echo $prod_id; ?>">

                                          </div>
                                        </div>
                                        <div class="control-group">
                                          <label class="control-label"  for="focusedInput">Quantity</label>
                                          <div class="controls">
                                            <input class="input-xlarge focused" name="quantity" id="focusedInput" type="number" value="<?php echo $qty; ?>">
                                          </div>
                                        </div>
                                         <div class="control-group">
                                          <label class="control-label"  for="focusedInput">Unit price</label>
                                          <div class="controls">
                                            <input onkeypress="return CheckNumeric()" onkeyup="FormatCurrency(this)" class="input-xlarge focused" name="unitprice" id="focusedInput" type="text" value="<?php echo $unitprice; ?>" place holder="Enter unit buying price" required="required">
                                          </div>
                                        </div>
                                        <div class="control-group">
                                          <label class="control-label"  for="focusedInput">Batch Number</label>
                                          <div class="controls">
                                            <input  class="input-xlarge focused" name="batch_no" id="focusedInput" type="text" value="<?php echo $batch_no; ?>"  >
                                          </div>
                                        </div>
                                         <div class="control-group">
                                          <label class="control-label"  for="focusedInput">Expiry Date</label>
                                          <div class="controls">
                                            <input  class="input-xlarge focused" name="expiry_date" id="focusedInput" type="date" value="<?php echo $expiry_date; ?>"  >
                                          </div>
                                        </div>
                                       
                                        
                                        <div class="form-actions">
                                          <button type="submit" class="btn btn-primary" name="update"><i class="icon-plus "></i>Update</button>
                                          <a href="new_purchase.php"><button type="submit" class="btn">Cancel</button></a>
                                        </div>
                                      </fieldset>
                                    </form>
                                    <?php
                                        include '../includes/connection.php';

                                        if(isset($_POST['update'])){
                                            $productid=$_POST['product_id'];
                                            $product=$_POST['product'];
                                            $quantity=$_POST['quantity'];
                                            $batch_no=$_POST['batch_no'];
                                            $expiry_date=$_POST['expiry_date'];
                                              $bad_symbols = array(",");
                                             $unitprice=$_POST['unitprice'];
                                             $unitprice= str_replace($bad_symbols, "", $unitprice);
                                           
                                            // Create connection
                                       

                                        $sql = " UPDATE getstock set quantity='$quantity',unitprice='$unitprice', expiry_date='$expiry_date',batch_no='$batch_no' where id='$id'";
                                    

                                        if (mysqli_query($conn, $sql)) {
                                           ?><meta http-equiv="refresh" content="0;URL='new_purchase.php'" /><?php
                                        } else {
                                            echo "Error: " . $sql . "<br>" . mysqli_error($conn);
                                        }

                                        mysqli_close($conn);
                                        }
                                        
                                        ?>

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

        <script src="../vendors/jquery-1.9.1.js"></script>
        <script src="../bootstrap/js/bootstrap.min.js"></script>
        <script src="../vendors/datatables/js/jquery.dataTables.min.js"></script>


        <script src="../assets/scripts.js"></script>
        <script src="../assets/DT_bootstrap.js"></script>
        <script>
        $(function() {
            
        });
        </script>
    </body>

</html>