[guadec-web] Drop student info, add room type and calculate the price before storing it in the db instead of trus



commit e1046e398923be0ee27afbf021e4cf246ed21312
Author: Pascal Terjan <pterjan gmail com>
Date:   Sat Jul 12 19:51:07 2014 +0000

    Drop student info, add room type and calculate the price before storing it in the db instead of trusting 
the user

 guadec/confirm-payment.php   |   37 ++++++++++++++++--------
 guadec/js/calculate-total.js |   31 ++++++++++----------
 guadec/js/calculate.php      |   63 ++++++++++--------------------------------
 guadec/pricing.php           |   49 ++++++++++++++++++++++++++++++++
 4 files changed, 105 insertions(+), 75 deletions(-)
---
diff --git a/guadec/confirm-payment.php b/guadec/confirm-payment.php
index 9df401c..ee3bd76 100644
--- a/guadec/confirm-payment.php
+++ b/guadec/confirm-payment.php
@@ -27,7 +27,7 @@ $sql = "CREATE TABLE $table_name (
   irc text,
   gender text,
   country text,
-  student VARCHAR(10) DEFAULT 'NO',
+  room VARCHAR(7),
   payment VARCHAR(10) DEFAULT 'NoPayment',
   bday date,
   UNIQUE KEY id (id)
@@ -37,6 +37,7 @@ require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
 dbDelta( $sql );
 
 if (!empty($_POST)) {
+       require_once('pricing.php');
        $application_submitted = true;
        $errors = false;
 
@@ -48,11 +49,8 @@ if (!empty($_POST)) {
        $diet = (isset($_POST['diet']))?(trim(stripslashes($_POST['diet']))) : 'NA';
        
        $entry = (isset($_POST['entry-fee']))?(trim(stripslashes($_POST['entry-fee']))):'0';
-       $lamount = (isset($_POST['lfee']))?(trim(stripslashes($_POST['lfee']))):'0';
-       $aamount = (isset($_POST['afee']))?(trim(stripslashes($_POST['afee']))):'0';
-       $tamount = (isset($_POST['tfee']))?(trim(stripslashes($_POST['tfee']))):'0';
+
        $bday = (isset($_POST['bday']))?($_POST['bday']):'NA';
-       $student =  ($_POST['student'] == true)?"YES":"NA";
 
        $obfuscated_email = str_replace("@", " AT ", $email);
        //check if the email already registered
@@ -69,15 +67,27 @@ if (!empty($_POST)) {
        }
        if(!empty($repeat)){
                $errors = true;
-       }       
+       }
        if(!isset($_POST['accommodation'])){
                $arrive = "NA";
                $depart = "NA";
        }
        else{
+               if(!isset($_POST['room_type'])){
+                       $errors = true;
+               } else {
+                       $room_type = $_POST['room_type'];
+                       if ($room_type != 'single' && $room_type != 'double') {
+                               $errors = true;
+                       }
+               }
                $arrive = $_POST['arrival'];
                $depart = $_POST['departure'];
        }
+
+       $nights = dayParser($arrive, $depart);
+       $aamount = accomPrice($nights, $room_type);
+
        $lunch_days = "";
        $x = 0;
        if(isset($_POST['lunch'])){
@@ -86,6 +96,9 @@ if (!empty($_POST)) {
                        $x = $x + 1;
                }
        }
+       $lamount = lunchPrice($x);
+       $tamount = $aamount + $lamount + $entry;
+
        $sponsor_check = ($_POST['sponsored'] == true)?"YES":"NO";
        $payment = ($tamount > 0)?"Pending":"NoPayment";
        $accom = ($_POST['accommodation'] == true)?"YES":"NO";
@@ -110,6 +123,7 @@ if (!empty($_POST)) {
                                 'name' => $name,
                                 'email' => $email,
                                 'accom' => $accom,
+                                'room' => $room_type,
                                 'arrive' => $arrive,
                                 'depart' => $depart,
                                 'sponsored' => $sponsor_check,
@@ -123,8 +137,7 @@ if (!empty($_POST)) {
                                 'gender' => $gender,
                                 'country' => $country,
                                 'payment' => $payment,
-                                'student' => $student,
-                                'bday' => $bday));     
+                                'bday' => $bday));
        }
 }
        
@@ -156,6 +169,10 @@ if (!empty($_POST)) {
                <div class="col span_1_of_2"><?php echo $accom;?></div>
                </div>
                <div class="section group">
+               <div class="col span_1_of_2">Room Type</div>
+               <div class="col span_1_of_2"><?php echo $room_type;?></div>
+               </div>
+               <div class="section group">
                <div class="col span_1_of_2">Arrival</div>
                <div class="col span_1_of_2"><?php echo $arrive;?></div>
                </div>
@@ -172,10 +189,6 @@ if (!empty($_POST)) {
                <div class="col span_1_of_2"><?php echo $diet;?></div>
                </div>
                <div class="section group">
-               <div class="col span_1_of_2">Student</div>
-               <div class="col span_1_of_2"><?php echo $student;?></div>
-               </div>
-               <div class="section group">
                <div class="col span_1_of_2">Accommodation-Fee</div>
                <div class="col span_1_of_2">€<?php echo $aamount;?></div>
                </div>
diff --git a/guadec/js/calculate-total.js b/guadec/js/calculate-total.js
index 3833cc9..32931f4 100644
--- a/guadec/js/calculate-total.js
+++ b/guadec/js/calculate-total.js
@@ -7,7 +7,7 @@ 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") ]},
+       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=room]").val() ]},
        success:function(obj, status){
                result = new String(obj);
                result = result.trim();
@@ -46,7 +46,7 @@ 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")]},
+       data: {functionname : "updateAccomTotal", arguments : [$('#arrive').val(),$('#depart').val(), 
$("[value=accommodation]").prop("checked"), $("[value=room]").val()]},
        success:function(obj, status){
                result = new String(obj);
                result = result.trim();
@@ -66,21 +66,19 @@ function enableDisableA(obj) {
                $("[name=arrival]").prop("disabled", false);
                $("[name=departure]").prop("disabled", false);
                $('[name=bday]').prop("disabled", false);
-               $("[name=sponsored]").prop("disabled", false);
-               $("[name=student]").prop("disabled", false);
+               $("[name=sponsored]").prop("checked", false);
                $(".box-options-accom").removeClass("disabled");
-
        }
        else {
                $("[name=arrival]").prop("disabled", true);
                $("[name=departure]").prop("disabled", true);
                $("[name=bday]").prop("disabled", true);
-               $("[name=sponsored]").prop("disabled", true);
-               $("[name=student]").prop("disabled", true);
-               $("[name=sponsored]").prop("checked", false);
-               $("[name=student]").prop("checked", false);
                $(".box-options-accom").addClass("disabled");
-
+       }
+}
+function enableDisableS(obj) {
+       if ($(obj).is(":checked")) {
+               $('[name=accomodation]').prop("checked", false);
        }
 }
 
@@ -118,9 +116,6 @@ $(function() {
        $("[value=accommodation]").change(function(){
                callAccomCalculate();
        })
-       $("[value=sponsored]").change(function(){
-               callAccomCalculate();
-       })
        $("[value*='lunch_']").click(function(){
                callLunchCalculate();
        })
@@ -158,6 +153,12 @@ $(function() {
                        enableDisableL(this);   
                });
        });
+       $(document).ready(function() {
+               enableDisableS(this);
+               $("[name=sponsored]").click(function() {
+                       enableDisableS(this);
+               });
+       });
        // Enable Disable the submit button
        $('input[name=regsub]').attr('disabled','disabled');
 
@@ -166,7 +167,7 @@ $(function() {
            $('input[name=regsub]').removeAttr('disabled');
         }
         else {
-               $('input[name=regsub]').attr('disabled','disabled');
+           $('input[name=regsub]').attr('disabled','disabled');
         }
      });
 
@@ -177,7 +178,7 @@ $(function() {
            $('#entry-arb').removeAttr('disabled');
         }
         else {
-               $('#entry-arb').attr('disabled','disabled');
+           $('#entry-arb').attr('disabled','disabled');
         }
      });
 
diff --git a/guadec/js/calculate.php b/guadec/js/calculate.php
index 9b7dc19..899d833 100644
--- a/guadec/js/calculate.php
+++ b/guadec/js/calculate.php
@@ -1,36 +1,7 @@
 
 <?php
+    include("../pricing.php");
 
-    define('day_fee', 25);
-    define('lunch_fee', 8.5);
-
-    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;
-       }
-       return $diffDate;   
-    }
-    
     $aResult = "";
 
     if( !isset($_POST['functionname']) ) { $aResult = 'error'; }
@@ -40,6 +11,7 @@
     if( !($aResult == 'error')) {
        switch($_POST['functionname']) {
             case 'updateLunchTotal':
+               # nb_lunch, lunch_checked
                if( !is_array($_POST['arguments']) || (count($_POST['arguments']) < 2) ) {
                    $aResult = 'error';
                }
@@ -48,46 +20,41 @@
                    $aResult = 0;
                  }
                  else{
-                   $aResult = (int)($_POST['arguments'][0]) * lunch_fee;
+                   $aResult = lunchPrice((int)($_POST['arguments'][0]));
                  }
                }
                break;
             case 'updateAccomTotal' :
+               # arrive, depart, accommodation_checked, room_type
                if( !is_array($_POST['arguments']) || (count($_POST['arguments']) < 4) ) {
                    $aResult = 'error';
                }
                else {
-                   if (($_POST['arguments'][2] == 'false') || ($_POST['arguments'][3] == 'true')) {
+                   if ($_POST['arguments'][2] == 'false') {
                      $aResult = 0;
                    }
-                   else{ 
-                     $total_days = dayParser($_POST['arguments'][0], $_POST['arguments'][1]);
-                     $aResult = (int)($total_days) * day_fee;
-                     if ($total_days == "Incorrect dates"){
-                      $aResult = $total_days;
-                     }
-                     
+                   else{
+                     $aResult = accomPrice($_POST['arguments'][0], $_POST['arguments'][1], 
$_POST['arguments'][2]){
                   }
                }
                break;
-            case 'updateTotal' :
+           case 'updateTotal' :
+                # nb_lunch, arrive, depart, fee, lunch_checked, accommodation_checked, room_type
                 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;
+               else {
+                   $nights = dayParser($_POST['arguments'][1], $_POST['arguments'][2]);
+                  $lunch = lunchPrice((int)($_POST['arguments'][0]));
+                  $accom = accomPrice($nights, $_POST['arguments'][6]);
+                  $fee = (int)($_POST['arguments'][3]);
                    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]);
+                  $aResult = $accom + $lunch + $fee);
                   if ($total_days == "Incorrect dates"){
                       $aResult = $total_days;
                   }
diff --git a/guadec/pricing.php b/guadec/pricing.php
new file mode 100644
index 0000000..77c4b7b
--- /dev/null
+++ b/guadec/pricing.php
@@ -0,0 +1,49 @@
+<?php
+    define('single_fee', 30);
+    define('double_fee', 35);
+    define('lunch_fee', 8.5);
+
+    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;
+       }
+       return $diffDate;   
+    }
+    
+    function accomPrice($nights, $room_type){
+       if($nights == "Incorrect dates") {
+          return $nights;
+       }
+       if ($room_type == 'single') {
+          return single_fee * $nights;
+       } elseif ($room_type == 'double') {
+          return double_fee * $nights;
+       }
+       return "Invalid room type";
+    }
+
+
+    function lunchPrice($days){
+       return $days * lunch_fee;
+    }
+?>


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