From bc747c90a26dedb06f60b13d5d794cb32dbcea1f Mon Sep 17 00:00:00 2001 From: Jonathan Rosenbaum Date: Sat, 5 Aug 2017 04:41:55 +0000 Subject: [PATCH] Creates new object when year changes. --- js/transaction.js | 56 ++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/js/transaction.js b/js/transaction.js index 4803bbf..1f0fa35 100644 --- a/js/transaction.js +++ b/js/transaction.js @@ -785,15 +785,14 @@ $(function() { var obj = $.parseJSON(data); var history = obj[obj.length - 1]; - // bug: need to check if qualifies to be a volunteer + // Check if individual has redeemed hours if (history.redeemed_hours !== "0.00") { $("#original_price").text(history.original_price).show(); $("#volunteer_hours").text(history.redeemed_hours).show(); } } }); - } - + } }); // show original price @@ -1028,22 +1027,14 @@ $(function() { var vhr; if ($("#redeemable_hours").val() === "") { vhr = "0.00"; + } else { + vhr = parseFloat($("#redeemable_hours").val()); } volunteer_benefits_history[year] = { volunteer_hours_redeemed: vhr, max_bike_earned: 0 }; - /* - var volunteer_benefits_history = { - [year] : - { - volunteer_hours_redeemed: parseFloat($("#redeemable_hours").val()), - max_bike_earned: 0 - } - }; - */ - // Volunteer History query $.post("json/transaction.php",{ volunteer_history_select: 1, contact_id: sold_to }, function(data) { @@ -1056,15 +1047,29 @@ $(function() { } else { // update redeemed hours volunteer_benefits_history = $.parseJSON(data); - if ($("#redeemable_hours").val().length) { - volunteer_benefits_history[year].volunteer_hours_redeemed = parseFloat(volunteer_benefits_history[year].volunteer_hours_redeemed) + - parseFloat($("#redeemable_hours").val()); - } - $.post("json/transaction.php",{ volunteer_history_update: 1, - contact_id: sold_to, - volunteer_history: volunteer_benefits_history, - more_than_one: 1 }); + // check if new year - will have to test + if (volunteer_benefits_history[year] === undefined) { + + volunteer_benefits_history[year] = { + volunteer_hours_redeemed: vhr, + max_bike_earned: 0 + }; + $.post("json/transaction.php",{ volunteer_history_update: 1, + contact_id: sold_to, + volunteer_history: volunteer_benefits_history }); + } else { + + if ($("#redeemable_hours").val().length) { + volunteer_benefits_history[year].volunteer_hours_redeemed = parseFloat(volunteer_benefits_history[year].volunteer_hours_redeemed) + + parseFloat($("#redeemable_hours").val()); + } + + $.post("json/transaction.php",{ volunteer_history_update: 1, + contact_id: sold_to, + volunteer_history: volunteer_benefits_history, + more_than_one: 1 }); + } // else same year } // update redeemed hours @@ -1093,7 +1098,12 @@ $(function() { // store the transaction's history - var transaction_history = []; + var transaction_history = [], rh; + if ($("#redeemable_hours").val() === "0" || $("#redeemable_hours").val() === "") { + rh = "0.00"; + } else { + rh = parseFloat($("#redeemable_hours").val()); + } var current_transaction = { transaction_id: transaction_id, @@ -1102,7 +1112,7 @@ $(function() { transaction_type: $("#transaction_type").val(), original_price: $("#original_price").text(), amount: $("#amount").val(), - redeemed_hours: parseFloat($("#redeemable_hours").val()) || parseFloat($("#volunteer_hours").text()), + redeemed_hours: rh || parseFloat($("#volunteer_hours").text()), description: $("#description").val(), sold_to: sold_to, sold_by: $("[name='sold_by']").val(),