2012-08-24 11:11:40 +02:00
|
|
|
module GroupOrdersHelper
|
2011-06-19 15:30:33 +02:00
|
|
|
def data_to_js(ordering_data)
|
2011-06-19 19:56:04 +02:00
|
|
|
ordering_data[:order_articles].map { |id, data|
|
|
|
|
[id, data[:price], data[:unit], data[:total_price], data[:others_quantity], data[:others_tolerance], data[:used_quantity], data[:quantity_available]]
|
|
|
|
}.map { |row|
|
|
|
|
"addData(#{row.join(', ')});"
|
|
|
|
}.join("\n")
|
2011-06-19 15:30:33 +02:00
|
|
|
end
|
|
|
|
|
2013-11-25 10:04:44 +01:00
|
|
|
# Returns a link to the page where a group_order can be edited.
|
|
|
|
# If the option :show is true, the link is for showing the group_order.
|
2013-12-11 22:58:45 +01:00
|
|
|
def link_to_ordering(order, options = {}, &block)
|
2013-11-25 10:04:44 +01:00
|
|
|
group_order = order.group_order(current_user.ordergroup)
|
2015-01-14 21:15:08 +01:00
|
|
|
path = if options[:show] && group_order
|
2021-03-01 15:27:26 +01:00
|
|
|
group_order_path(group_order)
|
|
|
|
elsif group_order
|
|
|
|
edit_group_order_path(group_order, :order_id => order.id)
|
|
|
|
else
|
|
|
|
new_group_order_path(:order_id => order.id)
|
|
|
|
end
|
2013-11-25 10:04:44 +01:00
|
|
|
options.delete(:show)
|
2013-12-11 22:58:45 +01:00
|
|
|
name = block_given? ? capture(&block) : order.name
|
2013-11-25 10:04:44 +01:00
|
|
|
path ? link_to(name, path, options) : name
|
2013-11-13 16:29:30 +01:00
|
|
|
end
|
|
|
|
|
2011-06-19 15:30:33 +02:00
|
|
|
# Return css class names for order result table
|
2011-06-19 19:56:04 +02:00
|
|
|
|
2011-06-19 15:30:33 +02:00
|
|
|
def order_article_class_name(quantity, tolerance, result)
|
|
|
|
if (quantity + tolerance > 0)
|
|
|
|
result > 0 ? 'success' : 'failed'
|
|
|
|
else
|
|
|
|
'ignored'
|
|
|
|
end
|
|
|
|
end
|
2012-12-16 16:50:09 +01:00
|
|
|
|
|
|
|
def get_order_results(order_article, group_order_id)
|
|
|
|
goa = order_article.group_order_articles.detect { |goa| goa.group_order_id == group_order_id }
|
|
|
|
quantity, tolerance, result, sub_total = if goa.present?
|
2021-03-01 15:27:26 +01:00
|
|
|
[goa.quantity, goa.tolerance, goa.result, goa.total_price(order_article)]
|
|
|
|
else
|
|
|
|
[0, 0, 0, 0]
|
|
|
|
end
|
2012-12-16 16:50:09 +01:00
|
|
|
|
2021-03-01 15:27:26 +01:00
|
|
|
{ group_order_article: goa, quantity: quantity, tolerance: tolerance, result: result, sub_total: sub_total }
|
2012-12-16 16:50:09 +01:00
|
|
|
end
|
2015-05-03 20:05:39 +02:00
|
|
|
|
|
|
|
def get_missing_units_css_class(quantity_missing)
|
2021-03-01 15:27:26 +01:00
|
|
|
if (quantity_missing == 1)
|
2015-05-03 20:05:39 +02:00
|
|
|
return 'missing-few';
|
2021-03-01 15:27:26 +01:00
|
|
|
elsif (quantity_missing == 0)
|
2015-05-03 20:05:39 +02:00
|
|
|
return ''
|
|
|
|
else
|
|
|
|
return 'missing-many'
|
|
|
|
end
|
|
|
|
end
|
2023-02-24 18:25:50 +01:00
|
|
|
|
|
|
|
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
|
2013-11-13 16:29:30 +01:00
|
|
|
end
|