2017-11-15 19:22:55 +01:00
|
|
|
class OrderPdf < RenderPDF
|
|
|
|
attr_reader :order
|
2012-10-02 02:50:48 +02:00
|
|
|
|
|
|
|
def initialize(order, options = {})
|
|
|
|
@order = order
|
2017-11-15 19:22:55 +01:00
|
|
|
@orders = order
|
|
|
|
super(options)
|
2012-10-02 02:50:48 +02:00
|
|
|
end
|
|
|
|
|
2017-11-15 19:22:55 +01:00
|
|
|
def nice_table(name, data, dimrows = [])
|
2021-03-01 15:27:26 +01:00
|
|
|
name_options = { size: 10, style: :bold }
|
2021-02-08 01:07:23 +01:00
|
|
|
name_height = height_of name, name_options
|
2021-03-01 15:27:26 +01:00
|
|
|
made_table = make_table data, width: bounds.width, cell_style: { size: 8, overflow: :shrink_to_fit } do |table|
|
2017-11-15 19:22:55 +01:00
|
|
|
# borders
|
|
|
|
table.cells.borders = [:bottom]
|
|
|
|
table.cells.padding_top = 2
|
|
|
|
table.cells.padding_bottom = 4
|
|
|
|
table.cells.border_color = 'dddddd'
|
|
|
|
table.rows(0).border_color = '666666'
|
|
|
|
|
|
|
|
# dim rows which were ordered but not received
|
|
|
|
dimrows.each do |ri|
|
|
|
|
table.row(ri).text_color = '999999'
|
|
|
|
table.row(ri).columns(0..-1).font_style = nil
|
2015-01-03 00:06:44 +01:00
|
|
|
end
|
2017-11-15 19:22:55 +01:00
|
|
|
|
|
|
|
yield table if block_given?
|
2012-10-02 02:50:48 +02:00
|
|
|
end
|
2021-02-08 01:07:23 +01:00
|
|
|
|
|
|
|
if name_height + made_table.height < cursor
|
|
|
|
down_or_page 15
|
|
|
|
else
|
|
|
|
start_new_page
|
|
|
|
end
|
|
|
|
|
|
|
|
text name, name_options
|
|
|
|
made_table.draw
|
2017-11-15 19:22:55 +01:00
|
|
|
end
|
2012-10-02 02:50:48 +02:00
|
|
|
|
2017-11-15 19:22:55 +01:00
|
|
|
protected
|
2012-10-02 02:50:48 +02:00
|
|
|
|
2017-11-15 19:22:55 +01:00
|
|
|
# Return price for order_article.
|
|
|
|
#
|
|
|
|
# This is a separate method so that plugins can override it.
|
|
|
|
#
|
|
|
|
# @param article [OrderArticle]
|
|
|
|
# @return [Number] Price to show
|
|
|
|
# @see https://github.com/foodcoops/foodsoft/issues/445
|
|
|
|
def order_article_price(order_article)
|
|
|
|
order_article.price.fc_price
|
2012-10-02 02:50:48 +02:00
|
|
|
end
|
|
|
|
|
2017-11-15 19:22:55 +01:00
|
|
|
def order_article_price_per_unit(order_article)
|
|
|
|
"#{number_to_currency(order_article_price(order_article))} / #{order_article.article.unit}"
|
2012-10-02 02:50:48 +02:00
|
|
|
end
|
2014-03-15 15:09:16 +01:00
|
|
|
|
2017-11-15 19:22:55 +01:00
|
|
|
def group_order_article_quantity_with_tolerance(goa)
|
|
|
|
goa.tolerance > 0 ? "#{goa.quantity} + #{goa.tolerance}" : "#{goa.quantity}"
|
2014-03-15 15:09:16 +01:00
|
|
|
end
|
2014-03-19 14:01:51 +01:00
|
|
|
|
2020-03-23 12:53:21 +01:00
|
|
|
def group_order_article_result(goa)
|
|
|
|
number_with_precision goa.result, strip_insignificant_zeros: true
|
|
|
|
end
|
|
|
|
|
2017-11-15 19:22:55 +01:00
|
|
|
def group_order_articles(ordergroup)
|
2021-03-01 15:27:26 +01:00
|
|
|
GroupOrderArticle
|
|
|
|
.includes(:group_order)
|
|
|
|
.where(group_orders: { order_id: @orders, ordergroup_id: ordergroup })
|
2014-03-19 14:01:51 +01:00
|
|
|
end
|
|
|
|
|
2017-11-15 19:22:55 +01:00
|
|
|
def order_articles
|
2021-03-01 15:27:26 +01:00
|
|
|
OrderArticle
|
|
|
|
.ordered
|
|
|
|
.includes(article: :supplier)
|
|
|
|
.includes(group_order_articles: { group_order: :ordergroup })
|
|
|
|
.where(order: @orders)
|
|
|
|
.order('suppliers.name, articles.name, groups.name')
|
|
|
|
.preload(:article_price)
|
2017-11-15 19:22:55 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def ordergroups(offset = nil, limit = nil)
|
2021-03-01 15:27:26 +01:00
|
|
|
result = GroupOrder
|
|
|
|
.ordered
|
|
|
|
.where(order: @orders)
|
|
|
|
.group('groups.id')
|
|
|
|
.offset(offset)
|
|
|
|
.limit(limit)
|
|
|
|
.pluck('groups.name', 'SUM(group_orders.price)', 'ordergroup_id', 'SUM(group_orders.transport)')
|
2017-11-15 19:22:55 +01:00
|
|
|
|
|
|
|
result.map do |item|
|
|
|
|
[item.first || stock_ordergroup_name] + item[1..-1]
|
2014-03-19 14:01:51 +01:00
|
|
|
end
|
|
|
|
end
|
2014-08-28 08:10:57 +02:00
|
|
|
|
2017-11-15 19:22:55 +01:00
|
|
|
def each_order_article(&block)
|
|
|
|
order_articles.each(&block)
|
|
|
|
end
|
2014-08-28 08:10:57 +02:00
|
|
|
|
2017-11-15 19:22:55 +01:00
|
|
|
def each_ordergroup(&block)
|
|
|
|
ordergroups.each(&block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def each_ordergroup_batch(batch_size)
|
|
|
|
offset = 0
|
|
|
|
|
|
|
|
while true
|
|
|
|
go_records = ordergroups(offset, batch_size)
|
|
|
|
|
|
|
|
break unless go_records.any?
|
|
|
|
|
|
|
|
group_ids = go_records.map(&:third)
|
|
|
|
|
|
|
|
# get quantity for each article and ordergroup
|
|
|
|
goa_records = group_order_articles(group_ids)
|
2021-03-01 15:27:26 +01:00
|
|
|
.group('group_order_articles.order_article_id, group_orders.ordergroup_id')
|
|
|
|
.pluck('group_order_articles.order_article_id', 'group_orders.ordergroup_id', 'SUM(COALESCE(group_order_articles.result, group_order_articles.quantity))')
|
2017-11-15 19:22:55 +01:00
|
|
|
|
|
|
|
# transform the flat list of results in a hash (with the article as key), which contains an array for all ordergroups
|
|
|
|
results = goa_records.group_by(&:first).transform_values do |value|
|
|
|
|
grouped_value = value.group_by(&:second)
|
|
|
|
group_ids.map do |group_id|
|
2020-03-23 12:53:21 +01:00
|
|
|
number_with_precision grouped_value[group_id].try(:first).try(:third), strip_insignificant_zeros: true
|
2017-11-15 19:22:55 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
yield go_records, results
|
|
|
|
offset += batch_size
|
2014-08-28 08:10:57 +02:00
|
|
|
end
|
|
|
|
end
|
2015-06-07 18:46:22 +02:00
|
|
|
|
2017-11-15 19:22:55 +01:00
|
|
|
def each_group_order_article_for_order_article(order_article, &block)
|
|
|
|
order_article.group_order_articles.each(&block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def each_group_order_article_for_ordergroup(ordergroup, &block)
|
|
|
|
group_order_articles(ordergroup)
|
2021-03-01 15:27:26 +01:00
|
|
|
.includes(order_article: { article: [:supplier] })
|
2017-11-15 19:22:55 +01:00
|
|
|
.order('suppliers.name, articles.name')
|
|
|
|
.preload(order_article: [:article_price, :order])
|
|
|
|
.each(&block)
|
2015-06-07 18:46:22 +02:00
|
|
|
end
|
2017-11-15 19:22:55 +01:00
|
|
|
|
|
|
|
def stock_ordergroup_name
|
2021-03-01 15:27:26 +01:00
|
|
|
users = GroupOrder.stock
|
|
|
|
.eager_load(:updated_by)
|
|
|
|
.where(order: @orders)
|
|
|
|
.map(&:updated_by)
|
|
|
|
.map { |u| u.try(&:name) || '?' }
|
2017-11-15 19:22:55 +01:00
|
|
|
|
|
|
|
I18n.t('model.group_order.stock_ordergroup_name', user: users.uniq.sort.join(', '))
|
|
|
|
end
|
2013-05-20 17:14:42 +02:00
|
|
|
end
|