Skip to content

Commit

Permalink
Added unit to article_prices
Browse files Browse the repository at this point in the history
  • Loading branch information
lentschi committed Apr 29, 2022
1 parent 4ecf7d4 commit b7f32d7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
29 changes: 20 additions & 9 deletions app/documents/order_fax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@ def title
end

def body
contact = FoodsoftConfig[:contact].symbolize_keys
from_paragraph

recipient_paragraph

articles_paragraph
rescue => exception
Rails.logger.info exception.backtrace
raise # always reraise
end

private

# From paragraph
def from_paragraph
contact = FoodsoftConfig[:contact].symbolize_keys
bounding_box [margin_box.right - 200, margin_box.top], width: 200 do
text FoodsoftConfig[:name], size: fontsize(9), align: :right
move_down 5
Expand All @@ -34,8 +45,9 @@ def body
text "#{Supplier.human_attribute_name :email}: #{contact[:email]}", size: fontsize(9), align: :right
end
end
end

# Recipient
def recipient_paragraph
bounding_box [margin_box.left, margin_box.top - 60], width: 200 do
text order.name
move_down 5
Expand All @@ -56,20 +68,21 @@ def body
text "#{Supplier.human_attribute_name :contact_person}: #{order.supplier[:contact_person]}"
move_down 10
end
end

# Articles
def articles_paragraph
total = 0
data = [I18n.t('documents.order_fax.rows')]
each_order_article do |oa|
price = oa.article.get_price(oa.price.supplier_order_unit)
price = oa.price.get_price(oa.price.supplier_order_unit)
subtotal = oa.units_to_order * price
total += subtotal
data << [oa.article.order_number,
oa.units_to_order,
oa.article.name,
# TODO-article-units: Why should we show the supplier the group order unit quantity?:
oa.article.convert_quantity(1, oa.price.supplier_order_unit, oa.price.group_order_unit),
format_supplier_article_unit(oa.article),
oa.price.convert_quantity(1, oa.price.supplier_order_unit, oa.price.group_order_unit),
format_supplier_article_unit(oa.price),
number_to_currency(price),
number_to_currency(subtotal)]
end
Expand All @@ -93,8 +106,6 @@ def body
# align: {0 => :left}
end

private

def order_articles
order.order_articles.ordered
.joins(:article)
Expand Down
3 changes: 2 additions & 1 deletion app/models/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ def update_price_history
:tax => tax,
:deposit => deposit,
:supplier_order_unit => supplier_order_unit,
:unit => unit,
:price_unit => price_unit,
:billing_unit => billing_unit,
:group_order_unit => group_order_unit,
Expand All @@ -293,7 +294,7 @@ def on_article_unit_ratios_change(some_change)
end

def price_changed?
changed.any? { |attr| attr == 'price' || 'tax' || 'deposit' || 'supplier_order_unit' || 'price_unit' || 'billing_unit' || 'group_order_unit' || 'group_order_granularity' || 'minimum_order_quantity' || 'article_unit_ratios' } \
changed.any? { |attr| attr == 'price' || 'tax' || 'deposit' || 'supplier_order_unit' || 'unit' || 'price_unit' || 'billing_unit' || 'group_order_unit' || 'group_order_granularity' || 'minimum_order_quantity' || 'article_unit_ratios' } \
|| @article_unit_ratios_changed \
|| article_unit_ratios.any?(&:changed?)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def up
t.column :group_order_unit, :string, length: 3
t.column :group_order_granularity, :float, null: false, default: 1
t.column :minimum_order_quantity, :float
t.column :unit, :string, null: true, default: nil
end

create_table :article_unit_ratios do |t|
Expand Down Expand Up @@ -66,6 +67,7 @@ def down
t.remove :group_order_unit
t.remove :group_order_granularity
t.remove :minimum_order_quantity
t.remove :unit
t.column :unit_quantity, :integer, null: false
end

Expand Down
1 change: 1 addition & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
t.string "group_order_unit"
t.float "group_order_granularity", default: 1.0, null: false
t.float "minimum_order_quantity"
t.string "unit"
t.index ["article_id"], name: "index_article_prices_on_article_id"
end

Expand Down
4 changes: 3 additions & 1 deletion lib/tasks/convert_units.rake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace :units do
task :convert do
task :convert => :environment do
base_unit_entries = []

un_ece_20_units = YAML.safe_load(ERB.new(File.read(File.expand_path('config/units-of-measure/un-ece-20.yml', Rails.root))).result)
Expand Down Expand Up @@ -37,6 +37,7 @@ namespace :units do
base_unit_entries << unit if is_base_unit
end

# rubocop:disable Style/CombinableLoops
un_ece_20_units.each do |unit|
next if unit['conversion']['unit'].blank?

Expand All @@ -47,6 +48,7 @@ namespace :units do
un_ece_20_units.each do |unit|
unit['conversion'].delete('unit')
end
# rubocop:enable Style/CombinableLoops

File.write(File.expand_path('config/units-of-measure/un-ece-20-remastered.yml', Rails.root), un_ece_20_units.to_yaml)
end
Expand Down

0 comments on commit b7f32d7

Please sign in to comment.