diff --git a/app/helpers/orders_helper.rb b/app/helpers/orders_helper.rb index 24c1d3c5..54233d05 100644 --- a/app/helpers/orders_helper.rb +++ b/app/helpers/orders_helper.rb @@ -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 diff --git a/app/models/concerns/price_calculation.rb b/app/models/concerns/price_calculation.rb index 1a0c2c7c..d9339133 100644 --- a/app/models/concerns/price_calculation.rb +++ b/app/models/concerns/price_calculation.rb @@ -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] @@ -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 diff --git a/app/models/order.rb b/app/models/order.rb index 7eb79739..def3d19d 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -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] } diff --git a/lib/article_units_lib.rb b/lib/article_units_lib.rb index 481a74b1..5183c764 100644 --- a/lib/article_units_lib.rb +++ b/lib/article_units_lib.rb @@ -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) @@ -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)