diff --git a/app/helpers/manage_products_helper.rb b/app/helpers/manage_products_helper.rb index 3ba46f3..7d85b54 100644 --- a/app/helpers/manage_products_helper.rb +++ b/app/helpers/manage_products_helper.rb @@ -277,9 +277,9 @@ module ManageProductsHelper prompt_msg = _('Insert the name of the new cost:') error_msg = _('Something went wrong. Please, try again') select_tag('price_details[][production_cost_id]', + '' + options_for_select(product.available_production_costs.map {|item| [truncate(item.name, 10, '...'), item.id]} + [[_('Other cost'), '']], selected), - {:include_blank => _('Select the cost'), - :class => 'production-cost-selection', + {:class => 'production-cost-selection', :onchange => "productionCostTypeChange(this, '#{url}', '#{prompt_msg}', '#{error_msg}')"}) end @@ -288,6 +288,6 @@ module ManageProductsHelper production_cost = args[:production_cost_value] || product.formatted_value(:total_production_cost) product_price = args[:product_price] || product.formatted_value(:price) - _("%{currency} %{production_cost} of %{currency} %{product_price}") % {:currency => currency, :production_cost => content_tag('span', production_cost, :class => '.production_cost'), :product_price => content_tag('span', product_price, :class => 'product_price')} + _("%{currency} %{production_cost} of %{currency} %{product_price}") % {:currency => currency, :production_cost => content_tag('span', production_cost, :class => 'production_cost'), :product_price => content_tag('span', product_price, :class => 'product_price')} end end diff --git a/app/models/input.rb b/app/models/input.rb index 541fbc4..92e8e3c 100644 --- a/app/models/input.rb +++ b/app/models/input.rb @@ -59,4 +59,9 @@ class Input < ActiveRecord::Base return 0 if self.amount_used.blank? || self.price_per_unit.blank? self.amount_used * self.price_per_unit end + + def cost + return 0 if self.amount_used.blank? || self.price_per_unit.blank? + self.amount_used * self.price_per_unit + end end diff --git a/app/views/manage_products/_edit_price_details.rhtml b/app/views/manage_products/_edit_price_details.rhtml index d70d4be..1b9141b 100644 --- a/app/views/manage_products/_edit_price_details.rhtml +++ b/app/views/manage_products/_edit_price_details.rhtml @@ -5,7 +5,7 @@ <%= link_to_remote(_('Remove'), :update => "price-detail-#{price_detail.id}", - :success => "jQuery('#manage-product-details-form input.submit').removeAttr('disabled').removeClass('disabled');", + :complete => "calculateValuesForBar();", :confirm => _('Are you sure that you want to remove this cost?'), :url => { :action => 'remove_price_detail', :id => price_detail, :product => @product }) %> diff --git a/app/views/manage_products/_inputs_cost.rhtml b/app/views/manage_products/_inputs_cost.rhtml index 9db5114..bae2240 100644 --- a/app/views/manage_products/_inputs_cost.rhtml +++ b/app/views/manage_products/_inputs_cost.rhtml @@ -1,4 +1,4 @@ -<%= float_to_currency(@product.inputs_cost) %> +<%= float_to_currency(@product.inputs_cost) %> + +<%= render :partial => 'shared/numbers_only_javascript' %> diff --git a/app/views/manage_products/_price_composition_bar.rhtml b/app/views/manage_products/_price_composition_bar.rhtml index a9887d7..d2395db 100644 --- a/app/views/manage_products/_price_composition_bar.rhtml +++ b/app/views/manage_products/_price_composition_bar.rhtml @@ -1,8 +1,9 @@ <% javascript_tag do %> - var value = <%= @product.price_description_percentage %> - var total_cost = <%= @product.total_production_cost %> - var price = <%= @product.price %> + var value = <%= @product.price_description_percentage %>; + var total_cost = <%= @product.total_production_cost %>; + var price = '<%= @product.formatted_value(:price) %>'; var described = false; + var currency_format = { separator : '<%= environment.currency_separator %>', delimiter : '<%= environment.currency_delimiter %>', unit : '<%= environment.currency_unit %>' }; if (<%= @product.price_described? %>) { var described = true; } diff --git a/public/javascripts/manage-products.js b/public/javascripts/manage-products.js index 72034fa..748d7fd 100644 --- a/public/javascripts/manage-products.js +++ b/public/javascripts/manage-products.js @@ -44,6 +44,7 @@ $(".cancel-new-cost").live('click', function() { $(this).parents('tr').remove(); + calculateValuesForBar(); return false; }); @@ -57,25 +58,12 @@ updatePriceCompositionBar(form); inputs_cost_update_url = $(form).find('#inputs-cost-update-url').val(); $.get(inputs_cost_update_url, function(data){ - $(".inputs-cost").html(data); + $(".inputs-cost span").html(data); }); return false; }); - $("#manage-product-details-form .price-details-price").live('keydown', function(data) { - $('.cancel-price-details').addClass('form-changed'); - var product_price = parseFloat($('form #product_price').val()); - var total_cost = parseFloat($('#product_inputs_cost').val()); - - $('form .price-details-price').each(function() { - total_cost = total_cost + parseFloat($(this).val()); - }); - enablePriceDetailSubmit(); - - var described = (product_price - total_cost) == 0; - var percentage = total_cost * 100 / product_price; - priceCompositionBar(percentage, described, total_cost, product_price); - }); + $("#manage-product-details-form .price-details-price").live('blur', function(data) { calculateValuesForBar(); }); function cancelPriceDetailsEdition() { $("#manage-product-details-button").show(); @@ -87,35 +75,84 @@ bar_url = $(form).find('.bar-update-url').val(); $.get(bar_url, function(data){ $("#price-composition-bar").html(data); + $('form #product_price').val(currencyToFloat($('#progressbar-text .product_price').html(), currency_format.separator, currency_format.delimiter)); + $('form #product_inputs_cost').val(currencyToFloat($('#display-product-price-details .inputs-cost span').html(), currency_format.separator, currency_format.delimiter, currency_format.unit)); + calculateValuesForBar(); }); }; - function enablePriceDetailSubmit() { - $('#manage-product-details-form input.submit').removeAttr("disabled").removeClass('disabled'); - }; - })(jQuery); +function enablePriceDetailSubmit() { + jQuery('#manage-product-details-form input.submit').removeAttr("disabled").removeClass('disabled'); +} + +function calculateValuesForBar() { + jQuery('.cancel-price-details').addClass('form-changed'); + var product_price = parseFloat(jQuery('form #product_price').val()); + var total_cost = parseFloat(jQuery('form #product_inputs_cost').val()); + + jQuery('form .price-details-price').each(function() { + var this_val = parseFloat(jQuery(this).val()) || 0; + total_cost = total_cost + this_val; + }); + enablePriceDetailSubmit(); + + var described = (product_price - total_cost) == 0; + var percentage = total_cost * 100 / product_price; + priceCompositionBar(percentage, described, total_cost, product_price); +} + +function addCommas(nStr) { + nStr += ''; + var x = nStr.split('.'); + var x1 = x[0]; + var x2 = x.length > 1 ? '.' + x[1] : ''; + var rgx = /(\d+)(\d{3})/; + while (rgx.test(x1)) { + x1 = x1.replace(rgx, '$1' + ',' + '$2'); + } + return x1 + x2; +} + +function floatToCurrency(value, sep, del, cur) { + var ret = ''; + if (cur) ret = cur + ' '; + if (!sep) sep = '.'; + if (!del) del = ','; + return ret + addCommas(parseFloat(value).toFixed(2).toString()).replace('.', '%sep%').replace(',', del).replace('%sep%', sep); +} + +function currencyToFloat(value, sep, del, cur) { + var val = value; + if (cur) val.replace(cur + ' ', ''); + if (!sep) sep = '.'; + if (!del) del = ','; + return parseFloat(val.replace(del, '').replace(sep, '.')); +} + function productionCostTypeChange(select, url, question, error_msg) { if (select.value == '') { var newType = prompt(question); - jQuery.ajax({ - url: url + "/" + newType, - dataType: 'json', - success: function(data, status, ajax){ - if (data.ok) { - var opt = jQuery(''); - opt.insertBefore(jQuery("option:last", select)); - select.selectedIndex = select.options.length - 2; - opt.clone().insertBefore('#new-cost-fields .production-cost-selection option:last'); - } else { - alert(data.error_msg); + if (newType) { + jQuery.ajax({ + url: url + "/" + newType, + dataType: 'json', + success: function(data, status, ajax){ + if (data.ok) { + var opt = jQuery(''); + opt.insertBefore(jQuery("option:last", select)); + select.selectedIndex = select.options.length - 2; + opt.clone().insertBefore('#new-cost-fields .production-cost-selection option:last'); + } else { + alert(data.error_msg); + } + }, + error: function(ajax, status, error){ + alert(error_msg); } - }, - error: function(ajax, status, error){ - alert(error_msg); - } - }); + }); + } } } @@ -125,8 +162,8 @@ function priceCompositionBar(value, described, total_cost, price) { $(bar_area).find('#progressbar').progressbar({ value: value }); - $(bar_area).find('.production-cost').html(total_cost.toFixed(2)); - $(bar_area).find('.product_price').html(price.toFixed(2)); + $(bar_area).find('.production_cost').html(floatToCurrency(total_cost, currency_format.separator, currency_format.delimiter)); + $(bar_area).find('.product_price').html(floatToCurrency(price, currency_format.separator, currency_format.delimiter)); if (described) { $(bar_area).find('#progressbar-icon').addClass('ui-icon-check'); $(bar_area).find('#progressbar-icon').attr('title', $('#price-described-message').html()); -- libgit2 0.21.2