Compare commits

...

2 commits

Author SHA1 Message Date
FGU
43a41062e9 add base price to group order form 2023-02-24 18:25:50 +01:00
FGU
80031c9e20 WIP on kilo price 2023-02-24 18:12:45 +01:00
9 changed files with 96 additions and 0 deletions

View file

@ -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

59
app/lib/quantity_unit.rb Normal file
View file

@ -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

View file

@ -77,6 +77,7 @@
%th{style: 'width:120px'}= heading_helper StockArticle, :supplier
%th{style: "width:13px;"}
%th{style: "width:4.5em;"}= t '.price'
%th{style: "width:4.5em;"}= t '.price_per_base_unit'
%th{style: "width:4.5em;"}= heading_helper Article, :unit
- unless @order.stockit?
%th{style: "width:70px;"}= heading_helper OrderArticle, :missing_units, short: true
@ -100,6 +101,7 @@
%td= truncate order_article.article.supplier.name, length: 15
%td= h order_article.article.origin
%td= number_to_currency(@ordering_data[:order_articles][order_article.id][:price])
%td= price_per_base_unit(article: order_article.article, price: @ordering_data[:order_articles][order_article.id][:price])
%td= order_article.article.unit
%td
- if @order.stockit?

View file

@ -1052,6 +1052,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

View file

@ -1054,6 +1054,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

View file

@ -929,6 +929,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

View file

@ -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

View file

@ -1024,6 +1024,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

View file

@ -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