adding poster form calculations
authorAnthony Talarico <talarico@gaslightmedia.com>
Mon, 24 Apr 2017 18:06:19 +0000 (14:06 -0400)
committerAnthony Talarico <talarico@gaslightmedia.com>
Mon, 24 Apr 2017 18:06:19 +0000 (14:06 -0400)
the poster form has 4 new additions of posters. had to remove the option to buy all 4 posters
for a set price since there are now 8 posters. had to create calculations to allow a mix and
match pricing system.

js/app.js
js/custom/pageSetup.js

index 8ea7077..9a0768b 100644 (file)
--- a/js/app.js
+++ b/js/app.js
@@ -56,6 +56,65 @@ $(document).ready(function () {
     pdf_form.insertAfter(".gform_confirmation_message");
     pdf_form.css("display", "block");
     
+    var poster_form     = page.find("#gform_2");
+    var poster_input    = poster_form.find('.ginput_quantity');
+    var prod_values     = [];
+    var price_total;
+    var poster_array = [3,19,6,7,16,18,17,5];
+    var quantity = 0;
+    
+    // poster form calculations //////////////////////////////////
+    function calculate_poster(){
+        price_total = 0;
+        var qty_total = 0;
+        for(var i = 3; i < 20; i++){
+            if( $.inArray(i, poster_array ) !== -1 ){
+                if( $('#ginput_quantity_2_' + i).val() !== undefined && $('#ginput_quantity_2_' + i).val() !== '' ){
+                    qty_total += parseInt($('#ginput_quantity_2_' + i).val(), 10);
+                }
+            }
+        }
+
+       if(qty_total > 4){
+           price_total = (qty_total - 4) * 10 + 30;
+       } else if( qty_total === 4){
+           price_total = 30;
+       } else if( qty_total < 4){
+           price_total = qty_total * 10;
+       }
+
+       $('.ginput_total_2').text('$' + price_total + '.00');
+       $('#input_2_15').val(price_total);
+    }
+  
+    $(window).on("load", function(){
+        calculate_poster();
+    });
+
+    poster_input.each(function(index){
+        if( $(this).val() !== '') {
+            quantity += parseInt($(this).val());
+        }
+       $(document).on("change",$(this),calculate_poster);
+        if( $(this).val() ){
+            prod_values.push($(this));
+        }
+    });
+
+    if(quantity >= 4){
+        $.each(prod_values.slice(0,1), function(){
+            if( $(this).val() === '1'){
+                $(this).prev().prev().val('0');
+            } else {
+                var price_calc =  parseInt( $(this).val() ) * 10 - 10;
+                var new_price =  ( price_calc / parseInt( $(this).val() ) );
+                $(this).prev().prev().val( new_price );
+            }
+        }); 
+    }
+    //end poster form calculations ///////////////////////////////////
+
+    // token order form
     $(document).on("change, keyup", "#ginput_quantity_4_36", function(){
         
         if( $(this).val() > 0){
@@ -73,7 +132,6 @@ $(document).ready(function () {
         for(var i = 12; i < 36; i++){
             if( $.inArray(i, field_array ) == -1 ){
                 if( $('#ginput_quantity_4_' + i).val() !== undefined && $('#ginput_quantity_4_' + i).val() !== '' ){
-                    console.log($('#ginput_quantity_4_' + i).val());
                     qty_total += parseInt($('#ginput_quantity_4_' + i).val(), 10);
                 }
             }
index da096b6..bdc89ec 100644 (file)
@@ -47,6 +47,65 @@ $(document).ready(function () {
     pdf_form.insertAfter(".gform_confirmation_message");
     pdf_form.css("display", "block");
     
+    var poster_form     = page.find("#gform_2");
+    var poster_input    = poster_form.find('.ginput_quantity');
+    var prod_values     = [];
+    var price_total;
+    var poster_array = [3,19,6,7,16,18,17,5];
+    var quantity = 0;
+    
+    // poster form calculations //////////////////////////////////
+    function calculate_poster(){
+        price_total = 0;
+        var qty_total = 0;
+        for(var i = 3; i < 20; i++){
+            if( $.inArray(i, poster_array ) !== -1 ){
+                if( $('#ginput_quantity_2_' + i).val() !== undefined && $('#ginput_quantity_2_' + i).val() !== '' ){
+                    qty_total += parseInt($('#ginput_quantity_2_' + i).val(), 10);
+                }
+            }
+        }
+
+       if(qty_total > 4){
+           price_total = (qty_total - 4) * 10 + 30;
+       } else if( qty_total === 4){
+           price_total = 30;
+       } else if( qty_total < 4){
+           price_total = qty_total * 10;
+       }
+
+       $('.ginput_total_2').text('$' + price_total + '.00');
+       $('#input_2_15').val(price_total);
+    }
+  
+    $(window).on("load", function(){
+        calculate_poster();
+    });
+
+    poster_input.each(function(index){
+        if( $(this).val() !== '') {
+            quantity += parseInt($(this).val());
+        }
+       $(document).on("change",$(this),calculate_poster);
+        if( $(this).val() ){
+            prod_values.push($(this));
+        }
+    });
+
+    if(quantity >= 4){
+        $.each(prod_values.slice(0,1), function(){
+            if( $(this).val() === '1'){
+                $(this).prev().prev().val('0');
+            } else {
+                var price_calc =  parseInt( $(this).val() ) * 10 - 10;
+                var new_price =  ( price_calc / parseInt( $(this).val() ) );
+                $(this).prev().prev().val( new_price );
+            }
+        }); 
+    }
+    //end poster form calculations ///////////////////////////////////
+
+    // token order form
     $(document).on("change, keyup", "#ginput_quantity_4_36", function(){
         
         if( $(this).val() > 0){
@@ -64,7 +123,6 @@ $(document).ready(function () {
         for(var i = 12; i < 36; i++){
             if( $.inArray(i, field_array ) == -1 ){
                 if( $('#ginput_quantity_4_' + i).val() !== undefined && $('#ginput_quantity_4_' + i).val() !== '' ){
-                    console.log($('#ginput_quantity_4_' + i).val());
                     qty_total += parseInt($('#ginput_quantity_4_' + i).val(), 10);
                 }
             }