<!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>
    
    <head>
       <?php   include("../includes/header-links.php");?>
        <!-- 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';
         $total=0; ?>
        <div class="container-fluid">
            <div class="row-fluid">
                <?php include '../includes/sidebar.php'; ?>
                <!--/span-->
           <div class="span7" id="content">

                        <div class="row-fluid">
                        <!-- block -->
                        <div class="block">
                            <div class="navbar navbar-inner block-header">
                                <div class="muted pull-left">Purchases</div>
                                
                                <div class="muted pull-right" >&nbsp &nbsp &nbsp</div>
                                <div class="muted pull-right"><a href="../stock/stock.php"><button class="bt btn-primary">Stock</button> </a></div>
                                
                            </div>
                            <div class="block-content collapse in">
                                <div class="span12">
                                    <?php
                                    include '../includes/connection.php';
                                    $sql0 = mysqli_query($con,"SELECT * from getstock where user_id='$id_user'");
                                    $row0 = mysqli_fetch_array($sql0);
                                    $bran = $row0['branch_id'];
                                    $sql = mysqli_query($con,"SELECT id, (select product_name from products where id=getstock.product_id)as product_name, quantity, unitprice,expiry_date,batch_no from getstock where business_id='$business' and user_id='$id_user'");
                                    if (mysqli_num_rows($sql) > 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>
                        <th>BatchNo</th>
												<th>Expiry Date</th>
                          <th>Action</th>
											</tr>
										</thead>
										<tbody>
                                        <?php
                                        
                                         // output data of each row
                                        $nnnn=0;
                while($row = mysqli_fetch_assoc($sql)) {
                        $total=$total+$row['unitprice']*$row['quantity'];
                        $nnnn++;
                    ?>
                    <tr class="odd gradeX">

        
                                                <td><?php echo $nnnn; ?></td>
                                                <td><?php echo $row['product_name']; ?></td>
                                                <td><?php echo $row['quantity']; ?></td>
                                                <td class="center"> <?php $row['unitprice']; ?></td>
                                                <td class="center"><?php echo number_format($row['unitprice']*$row['quantity']); ?></td>
                                                <td class="center"><?php echo $row['batch_no']; ?></td>
                                                <td class="center"><?php echo $row['expiry_date']; ?></td>
                                                <td class="center">
                                                    <a href="edit_purchase2.php?id=<?php echo $row['id']; ?>"><button class="btn btn-primary"><i class="icon-edit icon-white"></i></button></a>
                                                    <a href="deletepurchase2.php?id=<?php echo $row['id']; ?>"><button class="btn btn-danger"><i class="icon-trash icon-white"></i></button></a>
                                                </td>
                                            </tr>
                                            <?php
    }?>
    </tbody>
                                    </table>

                                    <h3><center>TOTAL: Ushs. <?php echo number_format($total); ?> </center></h3>
                                    <?php
                                    $sqlsup=mysqli_fetch_array(mysqli_query($con,"SELECT distinct supplier from getstock where business_id='$business' && user_id='$id_user'"));
                                    $supplierName=$sqlsup['supplier'];
                                    $fromsup=mysqli_fetch_array(mysqli_query($con,"select name, credit_limit from suppliers where supplier_id='$supplierName'"));
                                    $ssname=$fromsup['name'];
                                    $climit=$fromsup['credit_limit'];
                                    ?>
                                    <h4><center>Supplier:  <?php echo $ssname; ?>&nbsp; <a href="#myModal" data-toggle='modal'><i class="icon-pencil icon-large"></i></a><br>Credit Limit <?php echo number_format($climit);?>/=</center></h4>
                                    
            <form action="process_purchase2.php?total=<?php echo $total; ?>" method="POST">
    <div class="muted pull-left">
        
        Amount Paid: <input type="number"  name="amount_paid" class="form-control" value="<?php echo $total; ?>" min="<?php echo $total-$climit; ?>" max="<?php echo $total?>">
    
    </div>
    <div class="muted pull-right">
    

                                <button type="submit" name="process_order" class="btn btn-primary"> Save purchase</button></div>
    
                                </form>
                               
                                    <?php 
} else {
    echo " No Purchase Have been Added Yet!";
}

//mysqli_close($conn);
?>
					 <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>Edit Supplier</h3>
            
        </div>
        <div class="modal-body">
            <div>
                <form action="" method="POST">
                    <select name="updatevalue" id=""  required="required" class="span12">
       <option value="<?php echo $supplierName; ?>" ><?php echo $ssname; ?></option>
       <?php 
       include '../includes/connection.php';
       $supl=mysqli_query($con,"SELECT * from suppliers where status=1 && suspend=1 && branch_id='$branch'");
       while ($results=mysqli_fetch_array($supl)) {

    $supid=$results['supplier_id'];
    $sname=$results['name'];
    ?>
    <option value="<?php echo $supid; ?>"><?php echo $sname; ?></option>
    <?php
       }
        ?>
  </select>
    <button type="submit" class="btn btn-large btn-block btn-primary" name="save_changes">Save Changes</button>
                </form>
                <?php
if(isset($_POST['save_changes'])){
    $newsup=mysqli_real_escape_string($con,ucwords($_POST['updatevalue']));
    $sql1 = "UPDATE getstock SET supplier='$newsup'";

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

}
                ?>
            </div>
        </div>
    </div>						
											
								
                                </div>
                            </div>
                        </div>
                        <!-- /block -->
                    </div>

         
                </div>
                <div class="span3">
                                     <form  action="" method="POST">
                    <?php
                    $sql3 = mysqli_query($con,"SELECT * from getstock where business_id='$business' and user_id='$id_user'");
                    $num3 = mysqli_num_rows($sql3);
                    $row3 = mysqli_fetch_array($sql3);
                    $branchid = $row3['branch_id'];
                    ?> 
                                      <fieldset>
                                        <legend style="text-align: center;">Add product</legend>
                                        <div class="control-group" >
                                        <div class="control-group">
                          <label style=" font-size: 20px; ">Branch</label>
                        <?php if($num3>0){
                          ?>
                          <select class="input-xlarge focused" name="branch" id="focusedInput"   required="required"  readonly style=" font-size: 20px;">
                          <option disabled selected="" value="">Select Branch</option>
                          <!-- <option value="all">All</option> -->
                         <?php 
                         $cat=mysqli_query($con,"SELECT * from branches where status=1 && business_id='$business'");
                         while ( $row1=mysqli_fetch_array($cat)) {
                           ?>
                           <option <?php if($row1['branch_id']==$branchid){ echo"selected";}?> value="<?php echo $row1['branch_id'];?>"><?php echo $row1['branch_name'];?></option>
                           <?php
                         }
                          ?>
                          </select>
                          <?php
                        }
                          else{
                          ?>
                    <select name="branch" class="input-xlarge focused" required style="width: 80%; height: 30px;font-size: 20px;">
                          <option disabled selected="" value="">Select Branch</option>
                          <!-- <option value="all">All</option> -->
                         <?php 
                         $cat=mysqli_query($con,"SELECT * from branches where status=1 && business_id='$business'");
                         while ( $row1=mysqli_fetch_array($cat)) {
                           ?>
                           <option value="<?php echo $row1['branch_id'];?>"><?php echo $row1['branch_name'];?></option>
                           <?php
                         }
                          ?>
                          </select>
                          <?php
                          }
                          ?>
                        
                      </div>
                                          <label class="control-label" for="selectError" style="font-size: 20px;">Product</label>
                                          <div class="controls">
                                            <select id="select01" class="chzn-select" name="product" required="required" style="width: 80%; font-size: 18px; height: 35px;" >
                                            <option disabled selected value="" >Select Product</option>
                                              <?php include 'listproducts2.php'; ?>
                                            
                                            </select>
                                          </div>
                                        </div>
                                        <div class="control-group">
                                          <label class="control-label"  for="focusedInput" style=" font-size: 20px;">Quantity</label>
                                          <div class="controls">
                                            <input class="input-xlarge focused" name="quantity" id="focusedInput" type="number" value="Enter Quantity" required="required" style=" font-size: 20px;">
                                          </div>
                                        </div>
                                        <div class="control-group">
                                          <label class="control-label"  for="focusedInput" style=" font-size: 20px;">Unit buying price</label>
                                          <div class="controls">
                                            <input class="input-xlarge focused" name="unitprice" id="focusedInput" type="text" onkeypress="return CheckNumeric()" onkeyup="FormatCurrency(this)" required="required" style=" font-size: 20px;">
                                          </div>
                                        </div>
                                        <div class="control-group">
                                          <label class="control-label"  for="focusedInput" style=" font-size: 20px;">Batch Number</label>
                                          <div class="controls">
                                            <input class="input-xlarge focused" name="batch_no" id="focusedInput" type="text" style=" font-size: 20px;">
                                          </div>
                                        </div>
                                        <div class="control-group">
                                          <label class="control-label"  for="focusedInput" style=" font-size: 20px;">Expiry Date</label>
                                          <div class="controls">
                                            <input class="input-xlarge focused" name="expiry_date" id="focusedInput" type="date"  style=" font-size: 20px;">
                                          </div>
                                        </div>
                                        <div class="control-group">
                                          <label class="control-label"  for="focusedInput" style=" font-size: 20px;">supplier</label>
                                          <div class="controls">
    <?php
$sql4 = mysqli_query($con,"SELECT * from getstock where business_id='$business' and user_id='$id_user'");
$num4 = mysqli_num_rows($sql4);
$row4 = mysqli_fetch_array($sql4);
$sup = $row4['supplier'];
if($num4>0){
?>
<select class="input-xlarge focused" name="supplier" id="focusedInput"  readonly style=" font-size: 20px;">
<?php 
       include '../includes/connection.php';
       $supl=mysqli_query($con,"SELECT * from suppliers where status=1 && suspend=1 && business_id='$business'");
       while ($results=mysqli_fetch_array($supl)) {

    $supid=$results['supplier_id'];
    $sname=$results['name'];
    ?>
    <option <?php if($supid==$sup){echo"selected";}?> value="<?php echo $supid; ?>"><?php echo $sname; ?></option>
    <?php
       }
        ?>
</select>
<?php
}else{
  ?>
  <select class="input-xlarge focused" name="supplier" id="focusedInput" style=" font-size: 20px;">
       <option  selected value="">Select supplier</option>
       <?php 
       include '../includes/connection.php';
       $supl=mysqli_query($con,"SELECT * from suppliers where status=1 && suspend=1 && business_id='$business'");
       while ($results=mysqli_fetch_array($supl)) {

    $supid=$results['supplier_id'];
    $sname=$results['name'];
    ?>
    <option value="<?php echo $supid; ?>"><?php echo $sname; ?></option>
    <?php
       }
        ?>
  </select>
  <?php   
}
    ?>
                                           
                                          </div>

                                        
                                       
                                        
                                        <div class="form-actions">
                                          <button type="submit" class="btn btn-primary" name="add"><i class="icon-plus "></i>Add</button>
                                       
                                        </div>
                                      </fieldset>
                                    </form>
                                    <?php
                                        include '../includes/connection.php';

                                        if(isset($_POST['add'])){
                                            $branch=$_POST['branch'];
                                            $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);

                                            $supplier=mysqli_real_escape_string($con,ucwords($_POST['supplier']));
                                        if(isset($expiry_date)){
                                              $expiry_date=$_POST['expiry_date'];
                                              $sql2 = mysqli_query($con,"INSERT INTO getstock(product_id, quantity,unitprice,supplier,branch_id,business_id,batch_no,expiry_date,user_id)
                                        VALUES ('$product', '$quantity','$unitprice','$supplier','$branch','$business','$batch_no','$expiry_date','$id_user')");
                                            }else{
                                              
                                              $sql2 = mysqli_query($con,"INSERT INTO getstock(product_id, quantity,unitprice,supplier,branch_id,business_id,batch_no,expiry_date,user_id)
                                        VALUES ('$product', '$quantity','$unitprice','$supplier','$branch','$business','$batch_no',null,'$id_user')");
                                            }

                                        if ($sql2) {
                                           ?><meta http-equiv="refresh" content="0;URL='new_purchase_admin.php'" /><?php
                                        } else {
                                            echo "Error: " . $sql2 . mysqli_error($con);
                                        }

                                        mysqli_close($con);
                                        }
                                        
                                        ?>

                                </div>
            </div>



<div class="row-fluid">
    
</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>


        <link href="../vendors/datepicker.css" rel="stylesheet" media="screen">
        <link href="../vendors/uniform.default.css" rel="stylesheet" media="screen">
        <link href="../vendors/chosen.min.css" rel="stylesheet" media="screen">

        <link href="../vendors/wysiwyg/bootstrap-wysihtml5.css" rel="stylesheet" media="screen">

        <script src="../vendors/jquery-1.9.1.js"></script>
        <script src="../vendors/jquery.uniform.min.js"></script>
        <script src="../vendors/chosen.jquery.min.js"></script>
        <script src="../vendors/bootstrap-datepicker.js"></script>

        <script src="../vendors/wysiwyg/wysihtml5-0.3.0.js"></script>
        <script src="../vendors/wysiwyg/bootstrap-wysihtml5.js"></script>

        <script src="../vendors/wizard/jquery.bootstrap.wizard.min.js"></script>
        <script type="text/javascript" src="../vendors/jquery-validation/dist/jquery.validate.min.js"></script>
    <script src="../assets/form-validation.js"></script>
        
    <script src="../assets/scripts.js"></script>
        <script>
        $(function() {
            
        });
        </script>
        <script>

    jQuery(document).ready(function() {   
       FormValidation.init();
    });
    

        $(function() {
            $(".datepicker").datepicker();
            $(".uniform_on").uniform();
            $(".chzn-select").chosen();
            $(".chzn-select1").chosen();
            $('.textarea').wysihtml5();

            $('#rootwizard').bootstrapWizard({onTabShow: function(tab, navigation, index) {
                var $total = navigation.find('li').length;
                var $current = index+1;
                var $percent = ($current/$total) * 100;
                $('#rootwizard').find('.bar').css({width:$percent+'%'});
                // If it's the last tab then hide the last button and show the finish instead
                if($current >= $total) {
                    $('#rootwizard').find('.pager .next').hide();
                    $('#rootwizard').find('.pager .finish').show();
                    $('#rootwizard').find('.pager .finish').removeClass('disabled');
                } else {
                    $('#rootwizard').find('.pager .next').show();
                    $('#rootwizard').find('.pager .finish').hide();
                }
            }});
            $('#rootwizard .finish').click(function() {
                alert('Finished!, Starting over!');
                $('#rootwizard').find("a[href*='tab1']").trigger('click');
            });
        });
        </script>

    </body>

</html>