<!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");
		?>
    <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="span9" id="content">
					

                     <div class="row-fluid">
                        <!-- block -->
                        <div class="block">
						
                            <div class="navbar navbar-inner block-header">
                               
                                <div class="muted pull-left" > &nbsp&nbsp&nbsp<br> <h4>Stock Value</h4></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>
                                   <div class="table-toolbar">
                                   
                  

                                      <!--Model begins here -->
                        
										<!--Model Ends here -->
                 
                                   <?php
include '../includes/connection.php';
     if( $_SESSION['role']=="Super Admin"){
      $sql = mysqli_query($con,"SELECT id, (SELECT product_name from products where id=stock.product_name && business_id='$business')as product_name, quantity, buying_price, selling_price, (select symbol from units where UnitID=(select Units from products where id=stock.product_name) && business_id='$business')as unitf, min_quantity from stock where status=1 && business_id='$business'");
     }else{
$sql = mysqli_query($con,"SELECT id, (SELECT product_name from products where id=stock.product_name && branch_id='$branch')as product_name, quantity, buying_price, selling_price, (select symbol from units where UnitID=(select Units from products where id=stock.product_name) && branch_id='$branch')as unitf, min_quantity from stock where status=1 && branch_id='$branch'");
     }                              // Create connection

//$result = mysqli_query($con, $sql);

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


                                   ?>

        </div>
                                <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>S/NO</th>
                                                <th>Product Name</th>
                                               
                                                <th>Quantity</th>

                                                
                                                 <th>Selling Price</th>
                                                  <th>Total Value</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['product_name']; ?></td>
                                                <td><?php echo $row['quantity']." ".$row['unitf']; ?></td>
                                                
                                                <td><?php echo number_format($row['selling_price']); ?>/=</td>
                                                <td><?php echo number_format($row['selling_price']*$row['quantity']); ?>/=</td>
                                               
                                                
                                                
                                                
                      
                                            </tr>
                                            
                                            <?php


    }
     if( $_SESSION['role']=="Super Admin"){
      $sqltt= mysqli_fetch_array(mysqli_query($con,"SELECT sum(selling_price*quantity) as value from stock where business_id='$business'"));
     }else{
$sqltt= mysqli_fetch_array(mysqli_query($con,"SELECT sum(selling_price*quantity) as value from stock where branch_id='$branch'"));
     }
    
    ?>
    <tr><td colspan="4"><h4>Total Stock Value</h4></td><td ><h4><?php echo number_format($sqltt['value']); ?>/=</h4></td></tr>
    <?php

} else {
    echo "0 results";
}

mysqli_close($con);


                                        ?>
                                        <?php
                                        if(isset($_POST['next'])){
  include '../includes/connection.php';
  $bad_symbols = array(",", ".");
    $sprice=mysqli_real_escape_string($con,$_POST['sprice']);
    $sprice= str_replace($bad_symbols, "", $sprice);


$bprice=mysqli_real_escape_string($con,$_POST['bprice']);
$id=$_POST['id'];
 $up=mysqli_fetch_array(mysqli_query($con,"select * from stock where id='$id'"));
 $pname=$up['product_name'];

if ($sprice<=$bprice) {
 echo"<script>alert('Failed: The selling price should be greater than the buying price'),history.go(-1);</script>";
}else{
$sqlsp = mysqli_query($con,"update stock set selling_price='$sprice' where id='$id' ");
$sqlps = mysqli_query($con,"update products set price='$sprice' where id='$pname' ");

if ($sqlsp) {
  $id_user=$_SESSION['id'];
  $activity="Edited selling price for ". $podct;
 $log_sqlp=mysqli_query($con,"INSERT INTO logs(user_id, activity) VALUES 
('$id_user','$activity')");

if ($log_sqlp) {
 //alert('Updated a selling price for  $podct succesfully');
    echo "<script>
    
    window.location='stock';
    </script>";
} else {
    echo "Error: Failed to add log <br>" . mysqli_error($con);
}
  ?>


    <meta http-equiv="refresh" />  
    <?php
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($con);
}

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======-->
       
   


<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>
