From 2018561e6a9e6e06a2e1e14664f0fd6bc604a575 Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Mon, 27 Apr 2009 13:57:52 -0700 Subject: [PATCH] [#1919] Moved the JavaScript for calculating the Budget to an assets file. --- app/views/deliverables/_form.html.erb | 117 -------------------------- app/views/deliverables/edit.html.erb | 1 + app/views/deliverables/index.html.erb | 1 + assets/javascripts/budget.js | 110 ++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 117 deletions(-) create mode 100644 assets/javascripts/budget.js diff --git a/app/views/deliverables/_form.html.erb b/app/views/deliverables/_form.html.erb index cab9d6d..4540139 100644 --- a/app/views/deliverables/_form.html.erb +++ b/app/views/deliverables/_form.html.erb @@ -109,120 +109,3 @@
-<% content_for :header_tags do %> - -<% end %> diff --git a/app/views/deliverables/edit.html.erb b/app/views/deliverables/edit.html.erb index 366599f..634a9a5 100644 --- a/app/views/deliverables/edit.html.erb +++ b/app/views/deliverables/edit.html.erb @@ -8,4 +8,5 @@ <% content_for :header_tags do %> <%= stylesheet_link_tag "budget.css", :plugin => "budget_plugin", :media => "screen" %> + <%= javascript_include_tag('budget', :plugin => 'budget_plugin') %> <% end %> diff --git a/app/views/deliverables/index.html.erb b/app/views/deliverables/index.html.erb index cf13f99..aff93a3 100644 --- a/app/views/deliverables/index.html.erb +++ b/app/views/deliverables/index.html.erb @@ -28,4 +28,5 @@ <% content_for :header_tags do %> <%= stylesheet_link_tag "budget.css", :plugin => "budget_plugin", :media => "screen" %> + <%= javascript_include_tag('budget', :plugin => 'budget_plugin') %> <% end %> diff --git a/assets/javascripts/budget.js b/assets/javascripts/budget.js new file mode 100644 index 0000000..e468923 --- /dev/null +++ b/assets/javascripts/budget.js @@ -0,0 +1,110 @@ +/* Used to calculate the Budget */ +var BudgetModule = Class.create(); +Object.extend(BudgetModule.prototype, { + initialize: function () {}, + + toAmount: function(value) { + var amount = value.replace(/[^1234567890.]/ig,''); + if (amount) { + return parseFloat(amount); + } else { + return 0; + } + }, + + updateAmounts: function() { + if ($('deliverable_type').checked) { + // Fixed cost + var cost = Budget.toAmount($('deliverable_fixed_cost').value); + Budget.updateAmount($('fixedCost'), cost); + } else { + // Variable cost + var perHour = Budget.toAmount($('deliverable_cost_per_hour').value); + var hours = Budget.toAmount($('deliverable_total_hours').value); + + var cost = perHour * hours; + Budget.updateAmount($('variableCost'), cost); + } + + + if ($('deliverable_overhead').value.match('%')) { + var overhead_subtotal = (Budget.toAmount($('deliverable_overhead').value) / 100) * cost; + } else { + var overhead_subtotal = Budget.toAmount($('deliverable_overhead').value); + } + + if ($('deliverable_materials').value.match('%')) { + var materials_subtotal = (Budget.toAmount($('deliverable_materials').value) / 100) * cost; + } else { + var materials_subtotal = Budget.toAmount($('deliverable_materials').value); + } + + // Profit uses labor cost and overhead + if ($('deliverable_profit').value.match('%')) { + var profit_subtotal = (Budget.toAmount($('deliverable_profit').value) / 100) * (cost + overhead_subtotal); + } else { + var profit_subtotal = Budget.toAmount($('deliverable_profit').value); + } + + // Amounts + Budget.updateAmount($('overhead_subtotal'), overhead_subtotal); + Budget.updateAmount($('materials_subtotal'), materials_subtotal); + Budget.updateAmount($('profit_subtotal'), profit_subtotal); + + var total = cost + overhead_subtotal + materials_subtotal + profit_subtotal; + $('deliverable_budget').value = total; + $('total-budget-calculation').innerHTML = Budget.number_to_currency(total); + }, + + updateAmount: function(element, value) { + if (element) { + element.innerHTML = Budget.number_to_currency(value); + } + }, + + changeType: function() { + if ($('deliverable_type').checked) { + // Fixed + $$('.budget-hourly').each(function(ele) { ele.hide(); }); + $$('.budget-fixed').each(function(ele) { ele.show(); }); + } else { + // Variable + $$('.budget-hourly').each(function(ele) { ele.show(); }); + $$('.budget-fixed').each(function(ele) { ele.hide(); }); + } + + Budget.updateAmounts(); + }, + + // Rails-like number_to_currency currency formatting + // http://snippets.dzone.com/posts/show/4646 + number_to_currency: function (number, options) { + try { + var options = options || {}; + var precision = options["precision"] || 2; + var unit = options["unit"] || "$"; + var separator = precision > 0 ? options["separator"] || "." : ""; + var delimiter = options["delimiter"] || ","; + + var parts = parseFloat(number).toFixed(precision).split('.'); + return unit + Budget.number_with_delimiter(parts[0], delimiter) + separator + parts[1].toString(); + } catch(e) { + return number + } + }, + + number_with_delimiter: function (number, delimiter, separator) { + try { + var delimiter = delimiter || ","; + var separator = separator || "."; + + var parts = number.toString().split('.'); + parts[0] = parts[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1" + delimiter); + return parts.join(separator); + } catch(e) { + return number + } + } +}); +Budget = new BudgetModule(); +