
<?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>
    
    <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_exchange_rate'] != 1){
?>
<script>window.location="../main/dashboard.php"</script>
<?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">Exchanges</div>
                                
                                <div class="muted pull-right" >&nbsp; &nbsp; &nbsp;</div>
                                <div class="muted pull-right"><!-- <a href="crates.php"><button class="bt btn-primary">Crates</button> </a> --> </div>
                                
                            </div>
                            <div class="block-content collapse in">
                                <div class="span12">
                                    <?php
                                    include '../includes/connection.php';
                                    $sql = mysqli_query($con,"SELECT * from exchanges where business_id='$business' && branch_id='$branch'");
                                    if (mysqli_num_rows($sql) > 0) {
                                       ?>
                    <table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
                    <thead>
                      <tr>
                        <th>S/NO</th>
                        <th>Date</th>
                        <th>Total Sales</th>
                        <th>Exchange rate</th>
                        <th>Exchange amount(USD)</th>
                        
                        <th>Action</th>
                      </tr>
                    </thead>
                    <tbody>
                                        <?php
                                        
                                         // output data of each row
                                        $nnnn=0;
                while($row = mysqli_fetch_assoc($sql)) {
                        $nnnn++;
                    ?>
                    <tr class="odd gradeX">

        
                                                <td><?php echo $nnnn; ?></td>
                                                <td><?php echo $row['date']; ?></td>
                                                <td><?php echo $row['total_sales']; ?></td>
                                                <td><?php echo $row['exchange_rate']; ?></td>
                                                <td><?php echo $row['total_sales']/$row['exchange_rate']; ?></td>
                                                
                                                <td class="center">
                                                    <a href="#myModal<?php echo $row['id']; ?>" data-toggle="modal" class="btn btn-primary"><i class="icon-edit icon-white"></i>&nbsp;</a>
                                                    <a onclick = "confirmationDelete(this); return false;" href="delete_exchange.php?id=<?php echo $row['id']; ?>"><button class="btn btn-danger"><i class="icon-trash icon-white"></i></button></a>
                                                    
                                                </td>
                                            <div id="myModal<?php echo $row['id']; ?>" class="modal hide">
                                            <div class="modal-header" style="background-color: #08c; color: #ffff">
                                                <button data-dismiss="modal" class="close" type="button">&times;</button>
                                                <h3>Edit Exchange details</h3>
                                            </div>
                                            <div class="modal-body">
                                            <form  action="" method="POST"> 
                                      <fieldset>
                                      <div class="control-group" >
                                          <label class="control-label" for="selectError" style="font-size: 20px;">Total sales</label>
                                          <div class="controls">
                                            <input type="text" class="form-control" name="tsales" required="required" value="<?php echo $row['total_sales'];?>" style="width: 80%; font-size: 20px;" />
                                            
                                          </div>
                                        </div>
                                        <div class="control-group" >
                                          <label class="control-label" for="selectError" style="font-size: 20px;">Exchange Rate</label>
                                          <div class="controls">
                                          
                                            <input type="hidden" class="form-control" name="editid" required="required" value="<?php echo $row['id'];?>" style="width: 80%; font-size: 20px;" />
                                            <input type="text" class="form-control" name="exchangerate" required="required" value="<?php echo $row['exchange_rate'];?>" style="width: 80%; font-size: 20px;" />
                                            
                                          </div>
                                        </div>
                                        
                                        
                                        
                                        <div class="form-actions">
                                          <button type="submit" class="btn btn-primary" name="editexchange"><i class="icon-save "></i>Save changes</button>
                                       
                                        </div>
                                      
                                        
                                      </fieldset>
                                    </form>
                                            </div>
                                            </div>
                                             <?php
                                        include '../includes/connection.php';

                                        if(isset($_POST['editexchange'])){
                                            $editid=$_POST['editid'];
                                            $exchangerate=$_POST['exchangerate'];
                                            $tsales=$_POST['tsales'];
                                            
                                           
                                              $sql2 = mysqli_query($con,"UPDATE  exchanges set exchange_rate='$exchangerate', total_sales='$tsales' where id='$editid'");
                                            
                                        

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

                                        }
                                        
                                        ?>


                                            </tr>
                                            <?php
    }?>
    </tbody>
                                    </table>

                                    
                                    
                                    
            
                               
                                    <?php 
} else {
    echo "";
}

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

         
                </div>
                <div class="span3">
                                     <form  action="" method="POST"> 
                                      <fieldset>
                                        <legend style="text-align: center;">Add Exchange</legend>
                                        <div class="control-group" >
                                          <label class="control-label" for="selectError" style="font-size: 20px;">Date:</label>
                                          <div class="controls">
                                          
                                            <input type="date" id="datesale" onchange="gettotalsales(this.value);" class="form-control" name="date" required="required" style="width: 80%; font-size: 20px;" />
                                            
                                          </div>
                                        </div>
                                        <script type="text/javascript">

    function gettotalsales(idd){
          var xhttp = new XMLHttpRequest();
          
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      var response=this.responseText;
      document.getElementById("totalsales").value = response;
      document.getElementById("exchange_rate").value ="";
      document.getElementById("exchangeamount").value = "";

      if(response==0){
        document.getElementById("txtt").innerHTML =" ! No sales For that date!";
       document.getElementById("txtt").style.color="red";
       document.getElementById("approved").style.display="none";
       //document.getElementById("approved").style.disabled=true;


      }else{
       document.getElementById("approved").style.display="block";
        document.getElementById("txtt").innerHTML ="";
       
      }
      
      
    }
  };
  xhttp.open("GET", "getsalefordate.php?id="+idd, true);
  xhttp.send();

    }

</script>
                                        <div id="txtt"></div>
                                         
                                        <div id="approved">
                                        <div class="control-group">
                                          <label class="control-label"  for="focusedInput" style=" font-size: 20px;">Total Sales</label>
                                          <div class="controls">
                                            <input class="input-xlarge focused" id="totalsales" name="total_sales" type="text"  readonly style=" font-size: 20px;">
                                          </div>
                                        </div>
                                        
                                        <div class="control-group" >
                                          <label class="control-label" id="exrate" for="selectError" style="font-size: 20px;">Exchange rate(USD Value)</label>
                                          <div class="controls">
                                          
                                            <input type="number" class="form-control" id="exchange_rate" name="exchange_rate" onkeyup="calcutaexamount(this.value);" required="required" style="width: 80%; font-size: 20px;" step="any" />
                                            
                                          </div>
                                        </div>
                                         <script type="text/javascript">

    function calcutaexamount(exchange){
        
       var tsales=document.getElementById("totalsales").value;  
       var examount= tsales/exchange;
       if (exchange.length<1) {
         document.getElementById('submitbtn').disabled=true;
        
       }else{
         document.getElementById('exchangeamount').value=examount;
         document.getElementById('submitbtn').disabled=false;
       }
       
      

    
    }

</script>
                                        
                                         
                                        <div class="control-group">
                                          <label class="control-label"  for="focusedInput" style=" font-size: 20px;">Exchange Amount(USD)</label>
                                          <div class="controls">
                                            <input class="input-xlarge focused" id="exchangeamount" name="examount" type="text"  readonly style=" font-size: 20px;">
                                          </div>
                                        </div>
                                        

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

                                        if(isset($_POST['add'])){
                                            $date=$_POST['date'];
                                            $total_sales=$_POST['total_sales'];
                                            $exchange_rate=$_POST['exchange_rate'];
                                            $ck=mysqli_query($con,"SELECT * from exchanges where date='$date' && branch_id='$branch'");
                                            if (mysqli_num_rows($ck)>0) {
                                              echo "<script>alert('Failed! The exchanges for date   $date already exiists'),history.go(-1)</script>";
                                            }else{
                                              
                                              $sql2 = mysqli_query($con,"INSERT INTO exchanges(date, total_sales,exchange_rate,business_id,branch_id,added_by)
                                        VALUES ('$date', '$total_sales','$exchange_rate','$business','$branch','$id_user')");
                                            
                                        

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

                                        }}
                                        
                                        ?>

                                </div>
            </div>

<script type="text/javascript">
  function confirmationDelete(anchor){
    var conf = confirm("Are you sure you want to delete this Exhange rate?");
    if(conf){
        window.location = anchor.attr("href");
    }
}
</script>

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