Skip to content

Commit

Permalink
Fixes parts of #44:
Browse files Browse the repository at this point in the history
- accounting menu: group order amounts (see posting above)
- group order result (of a closed or settled order)
- manage order menu: sorted in articles / sorted in groups
  • Loading branch information
lentschi committed Feb 4, 2024
1 parent 4644497 commit 3d54764
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 33 deletions.
3 changes: 3 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ $(function() {
// trigger timeout to submit form when value was changed
clearTimeout(input.data('submit-timeout-id'));
input.data('submit-timeout-id', setTimeout(function() {
if (input.data('multiply-before-submit')) {
input.parents('form').append(`<input type="hidden" name="${input.attr('name')}" value="${input.val() * input.data('multiply-before-submit')}" />`);
}
if (input.val() != input.data('old-value')) input.parents('form').submit();
input.removeData('submit-timeout-id');
input.removeData('old-value');
Expand Down
54 changes: 32 additions & 22 deletions app/helpers/articles_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,55 @@ def row_classes(article)
classes.join(" ")
end

def format_supplier_article_unit(article)
return ArticleUnit.as_hash[article.supplier_order_unit][:name] unless article.supplier_order_unit.nil?
def format_unit(unit_property, article)
unit_code = article.send(unit_property)
return article.unit if unit_code.nil?

article.unit
end

def format_group_order_unit(article)
return article.unit if article.group_order_unit.nil?

unit = ArticleUnitsLib.units.to_h[article.group_order_unit]
unit = ArticleUnitsLib.units.to_h[unit_code]
unit[:symbol] || unit[:name]
end

def format_supplier_order_unit_with_ratios(article)
base = format_supplier_article_unit(article)
return base if ArticleUnitsLib.unit_is_si_convertible(article.supplier_order_unit)
def format_supplier_order_unit(article)
format_unit(:supplier_order_unit, article)
end

first_si_convertible_unit = article.article_unit_ratios.map(&:unit)
.find { |unit| ArticleUnitsLib.unit_is_si_convertible(unit) }
return base if first_si_convertible_unit.nil?
def format_group_order_unit(article)
format_unit(:group_order_unit, article)
end

quantity = article.convert_quantity(1, article.supplier_order_unit, first_si_convertible_unit)
"#{base} (#{format_number(quantity)}\u00a0#{ArticleUnitsLib.units.to_h[first_si_convertible_unit][:symbol]})"
def format_billing_unit(article)
format_unit(:billing_unit, article)
end

def format_group_order_unit_with_ratios(article)
base = format_group_order_unit(article)
return base if ArticleUnitsLib.unit_is_si_convertible(article.group_order_unit)
def format_unit_with_ratios(unit_property, article)
base = format_unit(unit_property, article)
unit_code = article.send(unit_property)
return base if ArticleUnitsLib.unit_is_si_convertible(unit_code)

first_si_convertible_unit = [article.article_unit_ratios.map(&:unit), article.supplier_order_unit]
relevant_units = [article.article_unit_ratios.map(&:unit)]
relevant_units << article.supplier_order_unit unless unit_property == :supplier_order_unit
first_si_convertible_unit = relevant_units
.flatten
.find { |unit| ArticleUnitsLib.unit_is_si_convertible(unit) }

return base if first_si_convertible_unit.nil?

quantity = article.convert_quantity(1, article.group_order_unit, first_si_convertible_unit)
quantity = article.convert_quantity(1, unit_code, first_si_convertible_unit)
"#{base} (#{format_number(quantity)}\u00a0#{ArticleUnitsLib.units.to_h[first_si_convertible_unit][:symbol]})"
end

def format_supplier_order_unit_with_ratios(article)
format_unit_with_ratios(:supplier_order_unit, article)
end

def format_group_order_unit_with_ratios(article)
format_unit_with_ratios(:group_order_unit, article)
end

def format_billing_unit_with_ratios(article)
format_unit_with_ratios(:billing_unit, article)
end

def field_with_preset_value_and_errors(options)
form, field, value, field_errors, input_html = options.values_at(:form, :field, :value, :errors, :input_html)
form.input field, label: false, wrapper_html: { class: field_errors.blank? ? '' : 'error' }, input_html: input_html do
Expand Down
12 changes: 8 additions & 4 deletions app/helpers/group_order_articles_helper.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
module GroupOrderArticlesHelper
# return an edit field for a GroupOrderArticle result
def group_order_article_edit_result(goa)
def group_order_article_edit_result(goa, convert_to_billing_unit = true)

Check failure on line 3 in app/helpers/group_order_articles_helper.rb

View workflow job for this annotation

GitHub Actions / test

Style/OptionalBooleanParameter: Prefer keyword arguments for arguments with a boolean default value; use `convert_to_billing_unit: true` instead of `convert_to_billing_unit = true`.
result = number_with_precision goa.result, strip_insignificant_zeros: true
unless goa.group_order.order.finished? && current_user.role_finance?
result
else
order_article = goa.order_article
article_version = order_article.article_version
simple_form_for goa, remote: true, html: { 'data-submit-onchange' => 'changed', class: 'delta-input' } do |f|
order_article = goa.order_article
quantity_data = ratio_quantity_data(order_article, order_article.article_version.group_order_unit)
f.input_field :result, as: :delta, class: 'input-nano', data: { min: 0 }.merge(quantity_data), id: "r_#{goa.id}", value: result
quantity_data = ratio_quantity_data(order_article, order_article.article_version.billing_unit)
converted_value = convert_to_billing_unit ? article_version.convert_quantity(goa.result, article_version.group_order_unit, article_version.billing_unit) : result
input_data = { min: 0 }.merge(quantity_data)
input_data = input_data.merge('multiply-before-submit': article_version.convert_quantity(1, article_version.billing_unit, article_version.group_order_unit)) if convert_to_billing_unit
f.input_field(:result, as: :delta, class: 'input-nano', data: input_data, id: "r_#{goa.id}", value: converted_value)
end
end
end
Expand Down
1 change: 1 addition & 0 deletions app/models/concerns/price_calculation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def convert_quantity(quantity, input_unit, output_unit)

def group_order_price(value = nil)
value ||= price
# price is always stored in supplier_order_unit:
value / convert_quantity(1, supplier_order_unit, group_order_unit)
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/deliveries/_stock_article_for_adding.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
%tr{:id => "stock_article_#{article.id}", :class => css_class}
%td= article.name
%td{:data => {:toggle => :tooltip, :title => "#{render(:partial => 'shared/article_version_info', :locals => {:article => article})}"}}= number_to_currency article.price
%td= format_supplier_article_unit(article)
%td= format_supplier_order_unit(article)
%td= article.article_category.name
%td
= link_to t('.action_edit'), edit_stock_article_path(article), remote: true, class: 'btn btn-mini'
Expand Down
2 changes: 1 addition & 1 deletion app/views/deliveries/_stock_change_fields.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
%span.stock_article_name= stock_change.stock_article.name
= f.association :stock_article, :as => :hidden
%td.price{:data => {:toggle => :tooltip, :title => "#{render(:partial => 'shared/article_version_info', :locals => {:article => stock_article})}"}}= number_to_currency stock_article.price
%td.unit= format_supplier_article_unit(stock_change.stock_article)
%td.unit= format_supplier_order_unit(stock_change.stock_article)
%td= f.input :quantity, :wrapper => :intable, :input_html => {:class => 'stock-change-quantity', :autocomplete => :off}
%td= stock_change_remove_link f
2 changes: 1 addition & 1 deletion app/views/deliveries/add_stock_change.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

$('#stock_changes tr').removeClass('success');

var quantity = w.prompt('<%= j(t('.how_many_units', :unit => format_supplier_article_unit(@stock_change.stock_article), :name => @stock_change.stock_article.name)) %>');
var quantity = w.prompt('<%= j(t('.how_many_units', :unit => format_supplier_order_unit(@stock_change.stock_article), :name => @stock_change.stock_article.name)) %>');
if(null === quantity) {
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion app/views/finance/balancing/_group_order_articles.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
= link_to t('ui.delete'), group_order_article_path(group_order_article),
method: :delete, remote: true, class: 'btn btn-mini btn-danger'
%td
- totals[:result] += group_order_article.result
- article_version = order_article.article_version
- totals[:result] += article_version.convert_quantity(group_order_article.result, article_version.group_order_unit, article_version.billing_unit)
%tfoot
%tr
%td
Expand Down
2 changes: 1 addition & 1 deletion app/views/finance/balancing/_order_article.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
= pkg_helper(order_article.article_version, unit: order_article.article_version.billing_unit)
- if s=ordered_quantities_different_from_group_orders?(order_article)
%span{:style => "color:red;font-weight: bold"}= s
%td #{format_group_order_unit_with_ratios(order_article.article_version)}
%td #{format_billing_unit_with_ratios(order_article.article_version)}
%td
= number_to_currency(order_article.article_version.price, :unit => "")
:plain
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/articles_by/_article_single_goa.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%tr{class: if goa.result == 0 then 'unavailable' end, id: "goa_#{goa.id}"}
%td{:style => "width:70%"}= goa.group_order.ordergroup_name
%td.center= "#{goa.quantity} + #{goa.tolerance}"
%td.center.input-delta= group_order_article_edit_result(goa)
%td.center.input-delta= group_order_article_edit_result(goa, false)
%td.price{data: {value: goa.total_price}}= number_to_currency(goa.total_price)
2 changes: 1 addition & 1 deletion app/views/stockit/_stock_article.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
%td= stock_article.quantity
%td= stock_article.quantity_ordered
%td.main_info= stock_article.quantity_available
%td= format_supplier_article_unit(stock_article)
%td= format_supplier_order_unit(stock_article)
%td= stock_article.price
%td= number_to_percentage stock_article.tax
%td= link_to stock_article.supplier.name, stock_article.supplier
Expand Down

0 comments on commit 3d54764

Please sign in to comment.