[guadec-web] form calculations from Saumya Dwivedi



commit 2a67b0e687acd4bc95b114ff2992ed77d44b26fb
Author: Andreas Nilsson <anilsson redhat com>
Date:   Mon Jun 30 22:58:04 2014 +0200

    form calculations from Saumya Dwivedi

 guadec/calculate-total.js |  104 +++++++++++++++++++++++++++++++++++++++++++++
 guadec/calculate.php      |  100 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 204 insertions(+), 0 deletions(-)
---
diff --git a/guadec/calculate-total.js b/guadec/calculate-total.js
new file mode 100644
index 0000000..f7c559e
--- /dev/null
+++ b/guadec/calculate-total.js
@@ -0,0 +1,104 @@
+/*calculate-total.js*/
+/*Jquery file to calculate net amount during registration */
+
+function callTotalCalculate() {
+       $.ajax({
+       type: "POST",   
+       url:"wp-content/themes/guadec/js/calculate.php",
+       data: {functionname: "updateTotal", arguments : [$("form input[value*='lunch_']:checked").size(), 
$('#arrive').val(), $('#depart').val(), $('input:radio[name=entry-fee]:checked').val(), 
$("[value=lunch]").prop("checked"), $("[value=accommodation]").prop("checked"), 
$("[value=sponsored]").prop("checked") ]},
+       success:function(obj, status){
+               result = new String(obj);
+               result = result.trim();
+       if(result != 'error' ) {
+               $(".total").html(result);
+        }
+        else {
+            console.log(result);
+               }          
+       }
+       });
+}
+
+function callLunchCalculate() {
+       $.ajax({
+       type: "POST",   
+       url:"wp-content/themes/guadec/js/calculate.php",
+       data: {functionname : "updateLunchTotal", arguments : [$("form 
input[value*='lunch_']:checked").size(),  $("[value=lunch]").prop("checked")]},
+       success:function(obj, status){
+               result = new String(obj);
+               result = result.trim();
+        if(result != 'error' ) {
+               $(".lunchfee").html(result);
+        }
+        else {
+            console.log(result);
+               }          
+       }
+       });
+       callTotalCalculate();
+}
+
+function callAccomCalculate() {
+       $.ajax({
+       type: "POST",   
+       url:"wp-content/themes/guadec/js/calculate.php",
+       data: {functionname : "updateAccomTotal", arguments : [$('#arrive').val(),$('#depart').val(), 
$("[value=accommodation]").prop("checked"), $("[value=sponsored]").prop("checked")]},
+       success:function(obj, status){
+               result = new String(obj);
+               result = result.trim();
+        if(result != 'error' ) {
+               $(".accomfee").html(result);
+        }
+        else {
+            console.log(result);
+               }          
+       }
+       });     
+       callTotalCalculate();
+}
+$(function() {
+       
+       /*Dropdown triggered event*/
+       $('#arrive').on('change' , function(){
+               callAccomCalculate();
+       })
+       $('#depart').on('change' , function(){
+               callAccomCalculate();
+       })
+
+        /* Checkboxes call events */
+       $("[value=lunch]").change(function(){
+               callLunchCalculate();
+       })
+       $("[value=accommodation]").change(function(){
+               callAccomCalculate();
+       })
+       $("[value=sponsored]").change(function(){
+               callAccomCalculate();
+       })
+       $("[value*='lunch_']").click(function(){
+               callLunchCalculate();
+       })
+       
+
+       /*Radio triggered event */
+       $('input:radio[name=entry-fee]').click(function() {
+               callTotalCalculate();
+       })
+       
+       $('#entry-arb').focusout(function(){
+               if($('input:radio[id=entry-fee-arb]').is(':checked')){
+                       $('input:radio[id=entry-fee-arb]').prop('value', $('#entry-arb').val()); 
+                       callTotalCalculate();
+               }
+       })
+       /* To force entry of numbers in money textbox */
+       $('#entry-arb').on('change keyup', function() {
+         // Remove invalid characters
+         var sanitized = $(this).val().replace(/[^0-9]/g, '');
+         // Update value
+          $(this).val(sanitized);
+
+       });
+});
+
diff --git a/guadec/calculate.php b/guadec/calculate.php
new file mode 100644
index 0000000..4511a38
--- /dev/null
+++ b/guadec/calculate.php
@@ -0,0 +1,100 @@
+<?php
+
+    define('day_fee', 2);
+    define('lunch_fee', 3);
+
+    function dayParser($arrive_string, $depart_string){
+       $result = ""; 
+       $a = explode('-', $arrive_string);
+       $b = explode('-', $depart_string);
+       $aDate = substr($a[1],3);
+       $bDate = substr($b[1],3);
+       $aMon = substr($a[1],0,3);
+       $bMon = substr($b[1],0,3);
+
+       $aDate = (int)$aDate;
+       $bDate = (int)$bDate;
+       
+       if ($aMon == $bMon){
+        if ($aDate > $bDate){
+          $result = "Incorrect dates";
+          return $result;
+        }
+        else {
+          $diffDate = ($bDate - $aDate);
+        }
+       }
+       else{
+         $diffDate = (31 - $aDate) + 1;
+       }
+      if ($diffDate == 0){
+        $diffDate = 1;
+      }
+       return $diffDate;   
+    }
+    
+    $aResult = "";
+
+    if( !isset($_POST['functionname']) ) { $aResult = 'error'; }
+
+    if( !isset($_POST['arguments']) ) { $aResult = 'error'; }
+
+    if( !($aResult == 'error')) {
+       switch($_POST['functionname']) {
+            case 'updateLunchTotal':
+               if( !is_array($_POST['arguments']) || (count($_POST['arguments']) < 2) ) {
+                   $aResult = 'error';
+               }
+               else {
+                  if ($_POST['arguments'][1] == 'false'){
+                   $aResult = 0;
+                 }
+                 else{
+                   $aResult = (int)($_POST['arguments'][0]) * lunch_fee;
+                 }
+               }
+               break;
+            case 'updateAccomTotal' :
+               if( !is_array($_POST['arguments']) || (count($_POST['arguments']) < 4) ) {
+                   $aResult = 'error';
+               }
+               else {
+                   if (($_POST['arguments'][2] == 'false') || ($_POST['arguments'][3] == 'true')) {
+                     $aResult = 0;
+                   }
+                   else{ 
+                     $total_days = dayParser($_POST['arguments'][0], $_POST['arguments'][1]);
+                     $aResult = (int)($total_days) * day_fee;
+                     
+                  }
+               }
+               break;
+            case 'updateTotal' :
+                if( !is_array($_POST['arguments']) || (count($_POST['arguments']) < 7) ) {
+                    $aResult = 'error';
+                }
+                else {
+                   $total_days = dayParser($_POST['arguments'][1], $_POST['arguments'][2]);
+                   $lunch = (int)($_POST['arguments'][0]) * lunch_fee;
+                   $accom = (int)($total_days) * day_fee;
+                   if ($_POST['arguments'][4] == 'false'){
+                     $lunch = 0;
+                   }
+                   if ($_POST['arguments'][5] == 'false'){
+                     $accom = 0;
+                   }
+                   if(($_POST['arguments'][6] == 'true')){ //&& ($_POST['arguments'][5] == 'true')) {
+                     $accom = 0;
+                   }
+                  $aResult = $accom + $lunch + (int)($_POST['arguments'][3]);
+               }
+               break;
+            
+            default:
+               $aResult = 'error';
+               break;
+        }
+
+    }
+    echo $aResult;
+?>
\ No newline at end of file


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]