Skip to content

Commit

Permalink
Fixes #101 (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
lentschi authored Oct 12, 2024
1 parent 6a1437e commit 151f060
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/helpers/orders_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def ratio_quantity_data(order_article, default_unit = nil)
data['supplier-order-unit'] = order_article.article_version.supplier_order_unit
data['default-unit'] = default_unit
data['custom-unit'] = order_article.article_version.unit
order_article.article_version.article_unit_ratios.all.each_with_index do |ratio, index|
order_article.article_version.article_unit_ratios.each_with_index do |ratio, index|
data["ratio-quantity-#{index}"] = ratio.quantity
data["ratio-unit-#{index}"] = ratio.unit
end
Expand Down
12 changes: 11 additions & 1 deletion app/models/concerns/price_calculation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def fc_price
def get_unit_ratio_quantity(unit)
return 1 if unit == supplier_order_unit

ratio = new_record? ? article_unit_ratios.detect { |ratio| ratio[:unit] == unit } : article_unit_ratios.find_by_unit(unit)
ratio = new_record? ? article_unit_ratios.detect { |ratio| ratio[:unit] == unit } : find_ratio_by_unit(unit)
return ratio.quantity unless ratio.nil?

unit_hash = ArticleUnit.as_hash[unit]
Expand Down Expand Up @@ -77,4 +77,14 @@ def fc_group_order_price
def add_percent(value, percent)
(value * ((percent * 0.01) + 1)).round(2)
end

def find_ratio_by_unit(unit)
begin
return article_unit_ratios.detect { |ratio| ratio.unit == unit } if association(:article_unit_ratios).loaded?
rescue StandardError
# continue
end

article_unit_ratios.find_by_unit(unit)
end
end
4 changes: 2 additions & 2 deletions app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ def stock_group_order
# e.g: [["drugs",[teethpaste, toiletpaper]], ["fruits" => [apple, banana, lemon]]]
def articles_grouped_by_category
@articles_grouped_by_category ||= order_articles
.includes([:article_version, :group_order_articles,
{ article_version: :article_category }])
.includes([:group_order_articles,
{ article_version: %i[article_category article_unit_ratios] }])
.order('article_versions.name')
.group_by { |oa| oa.article_version.article_category.name }
.sort { |a, b| a[0] <=> b[0] }
Expand Down
4 changes: 2 additions & 2 deletions lib/article_units_lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def self.unit_translations
def self.units
@units = {} if @units.nil?
units_cached_in_current_locale = @units[I18n.locale]
return units_cached_in_current_locale unless @units_cached_in_current_locale.nil?
return units_cached_in_current_locale unless units_cached_in_current_locale.nil?

@units[I18n.locale] = untranslated_units.to_h do |code, untranslated_unit|
translated_name = ArticleUnitsLib.get_translated_name_for_code(code, default_nil: true)
Expand All @@ -66,7 +66,7 @@ def self.units
end

def self.unit_is_si_convertible(code)
!units.to_h[code]&.dig(:conversionFactor).nil?
!units[code]&.dig(:conversionFactor).nil?
end

def self.human_readable_unit(unit_code)
Expand Down

0 comments on commit 151f060

Please sign in to comment.