Files
openfoodnetwork/lib/reporting/reports/customers/base.rb
Maikel Linke 4c3d619260 Report customers in deterministic order
The result could be random before which led to a flaky spec. I could
have adjusted the spec but I thought that it may be better UX as well to
be deterministic, especially when exporting into spreadsheets.
2023-08-23 17:03:30 +10:00

51 lines
1.3 KiB
Ruby

# frozen_string_literal: true
module Reporting
module Reports
module Customers
class Base < ReportTemplate
def query_result
filter Spree::Order.managed_by(@user)
.distributed_by_user(@user)
.complete.not_state(:canceled)
.order(:id)
end
def filter(orders)
filter_to_supplier filter_to_distributor filter_to_order_cycle orders
end
def filter_to_supplier(orders)
if params[:supplier_id].to_i > 0
orders.select do |order|
order.line_items.includes(:product)
.where("spree_products.supplier_id = ?", params[:supplier_id].to_i)
.references(:product)
.count
.positive?
end
else
orders
end
end
def filter_to_distributor(orders)
if params[:distributor_id].to_i > 0
orders.where(distributor_id: params[:distributor_id])
else
orders
end
end
def filter_to_order_cycle(orders)
if params[:order_cycle_id].to_i > 0
orders.where(order_cycle_id: params[:order_cycle_id])
else
orders
end
end
end
end
end
end