diff --git a/app/helpers/group_orders_helper.rb b/app/helpers/group_orders_helper.rb index c5e27c66..4f1d352f 100644 --- a/app/helpers/group_orders_helper.rb +++ b/app/helpers/group_orders_helper.rb @@ -53,4 +53,12 @@ module GroupOrdersHelper return 'missing-many' end end + + def price_per_base_unit(article:, price:) + quantity_unit = QuantityUnit.parse(article.unit) + return nil unless quantity_unit.present? + + scaled_price, base_unit = quantity_unit.scale_price_to_base_unit(price) + "#{number_to_currency(scaled_price)}/#{base_unit}" + end end diff --git a/app/lib/quantity_unit.rb b/app/lib/quantity_unit.rb new file mode 100644 index 00000000..0a910f87 --- /dev/null +++ b/app/lib/quantity_unit.rb @@ -0,0 +1,59 @@ +class QuantityUnit + def initialize(quantity, unit) + @quantity = quantity + @unit = unit + end + + def self.parse(number_with_unit) + # remove whitespace + number_with_unit = number_with_unit.gsub(/\s+/, '') + # to lowercase + number_with_unit = number_with_unit.downcase + # remove numerical part + number = number_with_unit.gsub(/[^0-9.,]/, '') + # remove unit part + unit = number_with_unit.gsub(/[^a-zA-Z]/, '') + # convert comma to dot + number = number.gsub(',', '.') + # convert to float + number = number.to_f + + return nil unless unit.in?(%w[g kg l ml]) + + QuantityUnit.new(number, unit) + end + + def scale_price_to_base_unit(price) + return nil unless price.is_a?(Numeric) + + factor = if @unit == 'kg' || @unit == 'l' + 1 + elsif @unit == 'g' || @unit == 'ml' + 1000 + end + + scaled_price = price / @quantity * factor + scaled_price.round(2) + + base_unit = if @unit == 'kg' || @unit == 'g' + 'kg' + elsif @unit == 'l' || @unit == 'ml' + 'L' + end + + [scaled_price, base_unit] + end + + + def to_s + "#{@quantity} #{@unit}" + end + + def quantity + @quantity + end + + def unit + @unit + end +end \ No newline at end of file diff --git a/config/locales/de.yml b/config/locales/de.yml index 89a69005..d6254d84 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -1067,6 +1067,7 @@ de: action_save: Bestellung speichern new_funds: Neuer Kontostand price: Preis + price_per_base_unit: Grundpreis reset_article_search: Suche zurücksetzen search_article: Artikel suchen... sum_amount: Gesamtbestellmenge bisher diff --git a/config/locales/en.yml b/config/locales/en.yml index cb7a54c1..ea65f309 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1069,6 +1069,7 @@ en: action_save: Save order new_funds: New account balance price: Price + price_per_base_unit: Base price reset_article_search: Reset search search_article: Search for articles... sum_amount: Current amount diff --git a/config/locales/es.yml b/config/locales/es.yml index 4004b5c5..1f594ead 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -930,6 +930,7 @@ es: action_save: Guardar pedido new_funds: Nuevo balance de cuenta price: Precio + price_per_base_unit: Precio de base reset_article_search: Reinicia la búsqueda search_article: Busca artículos... sum_amount: Cantidad actual diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 304a7b9d..04dc03fc 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -678,6 +678,7 @@ fr: action_save: Enregistrer ta commande new_funds: Nouveau solde price: Prix + price_per_base_unit: Prix de base reset_article_search: Réinitialiser la recherche search_article: Rechercher des produits... sum_amount: Quantité déjà commandée diff --git a/config/locales/nl.yml b/config/locales/nl.yml index f441d15d..dd41b666 100644 --- a/config/locales/nl.yml +++ b/config/locales/nl.yml @@ -1026,6 +1026,7 @@ nl: action_save: Bestelling opslaan new_funds: Nieuw tegoed price: Prijs + price_per_base_unit: Basisprjis reset_article_search: Alles tonen search_article: Artikelen zoeken... sum_amount: Huidig totaalbedrag diff --git a/spec/lib/quantity_unit_spec.rb b/spec/lib/quantity_unit_spec.rb new file mode 100644 index 00000000..bbe3d546 --- /dev/null +++ b/spec/lib/quantity_unit_spec.rb @@ -0,0 +1,22 @@ +require_relative '../spec_helper' + +describe QuantityUnit do + it "parses a string correctly" do + qu = QuantityUnit.parse("1.5 k g"); expect([qu.quantity, qu.unit]).to eq([1.5, "kg"]) + qu = QuantityUnit.parse(" 1,5 kg"); expect([qu.quantity, qu.unit]).to eq([1.5, "kg"]) + qu = QuantityUnit.parse("1500 g"); expect([qu.quantity, qu.unit]).to eq([1500, "g"]) + qu = QuantityUnit.parse("1.5L "); expect([qu.quantity, qu.unit]).to eq([1.5, "l"]) + qu = QuantityUnit.parse("2400mL"); expect([qu.quantity, qu.unit]).to eq([2400, "ml"]) + end + + it "scales prices correctly" do + qu = QuantityUnit.new(1.5, "kg") + expect(qu.scale_price_to_base_unit(12.34)).to eq([8.23, "kg"]) + qu = QuantityUnit.new(1500, "g") + expect(qu.scale_price_to_base_unit(12.34)).to eq([8.23, "kg"]) + qu = QuantityUnit.new(1.5, "l") + expect(qu.scale_price_to_base_unit(12.34)).to eq([8.23, "L"]) + qu = QuantityUnit.new(2400, "ml") + expect(qu.scale_price_to_base_unit(12.34)).to eq([5.14, "L"]) + end +end \ No newline at end of file