Compare commits
2 commits
demo
...
37_display
Author | SHA1 | Date | |
---|---|---|---|
43a41062e9 | |||
80031c9e20 |
9 changed files with 96 additions and 0 deletions
|
@ -53,4 +53,12 @@ module GroupOrdersHelper
|
||||||
return 'missing-many'
|
return 'missing-many'
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
59
app/lib/quantity_unit.rb
Normal file
59
app/lib/quantity_unit.rb
Normal 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
|
|
@ -77,6 +77,7 @@
|
||||||
%th{style: 'width:120px'}= heading_helper StockArticle, :supplier
|
%th{style: 'width:120px'}= heading_helper StockArticle, :supplier
|
||||||
%th{style: "width:13px;"}
|
%th{style: "width:13px;"}
|
||||||
%th{style: "width:4.5em;"}= t '.price'
|
%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
|
%th{style: "width:4.5em;"}= heading_helper Article, :unit
|
||||||
- unless @order.stockit?
|
- unless @order.stockit?
|
||||||
%th{style: "width:70px;"}= heading_helper OrderArticle, :missing_units, short: true
|
%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= truncate order_article.article.supplier.name, length: 15
|
||||||
%td= h order_article.article.origin
|
%td= h order_article.article.origin
|
||||||
%td= number_to_currency(@ordering_data[:order_articles][order_article.id][:price])
|
%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= order_article.article.unit
|
||||||
%td
|
%td
|
||||||
- if @order.stockit?
|
- if @order.stockit?
|
||||||
|
|
|
@ -1052,6 +1052,7 @@ de:
|
||||||
action_save: Bestellung speichern
|
action_save: Bestellung speichern
|
||||||
new_funds: Neuer Kontostand
|
new_funds: Neuer Kontostand
|
||||||
price: Preis
|
price: Preis
|
||||||
|
price_per_base_unit: Grundpreis
|
||||||
reset_article_search: Suche zurücksetzen
|
reset_article_search: Suche zurücksetzen
|
||||||
search_article: Artikel suchen...
|
search_article: Artikel suchen...
|
||||||
sum_amount: Gesamtbestellmenge bisher
|
sum_amount: Gesamtbestellmenge bisher
|
||||||
|
|
|
@ -1054,6 +1054,7 @@ en:
|
||||||
action_save: Save order
|
action_save: Save order
|
||||||
new_funds: New account balance
|
new_funds: New account balance
|
||||||
price: Price
|
price: Price
|
||||||
|
price_per_base_unit: Base price
|
||||||
reset_article_search: Reset search
|
reset_article_search: Reset search
|
||||||
search_article: Search for articles...
|
search_article: Search for articles...
|
||||||
sum_amount: Current amount
|
sum_amount: Current amount
|
||||||
|
|
|
@ -929,6 +929,7 @@ es:
|
||||||
action_save: Guardar pedido
|
action_save: Guardar pedido
|
||||||
new_funds: Nuevo balance de cuenta
|
new_funds: Nuevo balance de cuenta
|
||||||
price: Precio
|
price: Precio
|
||||||
|
price_per_base_unit: Precio de base
|
||||||
reset_article_search: Reinicia la búsqueda
|
reset_article_search: Reinicia la búsqueda
|
||||||
search_article: Busca artículos...
|
search_article: Busca artículos...
|
||||||
sum_amount: Cantidad actual
|
sum_amount: Cantidad actual
|
||||||
|
|
|
@ -678,6 +678,7 @@ fr:
|
||||||
action_save: Enregistrer ta commande
|
action_save: Enregistrer ta commande
|
||||||
new_funds: Nouveau solde
|
new_funds: Nouveau solde
|
||||||
price: Prix
|
price: Prix
|
||||||
|
price_per_base_unit: Prix de base
|
||||||
reset_article_search: Réinitialiser la recherche
|
reset_article_search: Réinitialiser la recherche
|
||||||
search_article: Rechercher des produits...
|
search_article: Rechercher des produits...
|
||||||
sum_amount: Quantité déjà commandée
|
sum_amount: Quantité déjà commandée
|
||||||
|
|
|
@ -1024,6 +1024,7 @@ nl:
|
||||||
action_save: Bestelling opslaan
|
action_save: Bestelling opslaan
|
||||||
new_funds: Nieuw tegoed
|
new_funds: Nieuw tegoed
|
||||||
price: Prijs
|
price: Prijs
|
||||||
|
price_per_base_unit: Basisprjis
|
||||||
reset_article_search: Alles tonen
|
reset_article_search: Alles tonen
|
||||||
search_article: Artikelen zoeken...
|
search_article: Artikelen zoeken...
|
||||||
sum_amount: Huidig totaalbedrag
|
sum_amount: Huidig totaalbedrag
|
||||||
|
|
22
spec/lib/quantity_unit_spec.rb
Normal file
22
spec/lib/quantity_unit_spec.rb
Normal 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
|
Loading…
Reference in a new issue