mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Move duplicate code into its own module
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
require 'open_food_network/reports/bulk_coop_supplier_report'
|
||||
require 'open_food_network/reports/bulk_coop_allocation_report'
|
||||
require "open_food_network/reports/line_items"
|
||||
|
||||
module OpenFoodNetwork
|
||||
class BulkCoopReport
|
||||
@@ -45,26 +46,12 @@ module OpenFoodNetwork
|
||||
end
|
||||
|
||||
def search
|
||||
permissions.visible_orders.complete.not_state(:canceled).search(params[:q])
|
||||
OpenFoodNetwork::Reports::LineItems.search_orders(permissions, params)
|
||||
end
|
||||
|
||||
def table_items
|
||||
return [] unless @render_table
|
||||
orders = search.result
|
||||
|
||||
line_items = permissions.visible_line_items.merge(Spree::LineItem.where(order_id: orders))
|
||||
|
||||
line_items_with_hidden_details =
|
||||
permissions.editable_line_items.empty? ? line_items : line_items.where('"spree_line_items"."id" NOT IN (?)', permissions.editable_line_items)
|
||||
|
||||
line_items.select{ |li| line_items_with_hidden_details.include? li }.each do |line_item|
|
||||
# TODO We should really be hiding customer code here too, but until we
|
||||
# have an actual association between order and customer, it's a bit tricky
|
||||
line_item.order.bill_address.andand.assign_attributes(firstname: I18n.t('admin.reports.hidden'), lastname: "", phone: "", address1: "", address2: "", city: "", zipcode: "", state: nil)
|
||||
line_item.order.ship_address.andand.assign_attributes(firstname: I18n.t('admin.reports.hidden'), lastname: "", phone: "", address1: "", address2: "", city: "", zipcode: "", state: nil)
|
||||
line_item.order.assign_attributes(email: I18n.t('admin.reports.hidden'))
|
||||
end
|
||||
line_items
|
||||
OpenFoodNetwork::Reports::LineItems.list(permissions, params)
|
||||
end
|
||||
|
||||
def rules
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
require "open_food_network/reports/line_items"
|
||||
|
||||
include Spree::ReportsHelper
|
||||
|
||||
module OpenFoodNetwork
|
||||
@@ -46,28 +48,12 @@ module OpenFoodNetwork
|
||||
end
|
||||
|
||||
def search
|
||||
permissions.visible_orders.complete.not_state(:canceled).search(params[:q])
|
||||
OpenFoodNetwork::Reports::LineItems.search_orders(permissions, params)
|
||||
end
|
||||
|
||||
def table_items
|
||||
return [] unless @render_table
|
||||
orders = search.result
|
||||
|
||||
line_items = permissions.visible_line_items.merge(Spree::LineItem.where(order_id: orders))
|
||||
line_items = line_items.supplied_by_any(params[:supplier_id_in]) if params[:supplier_id_in].present?
|
||||
|
||||
# If empty array is passed in, the where clause will return all line_items, which is bad
|
||||
line_items_with_hidden_details =
|
||||
permissions.editable_line_items.empty? ? line_items : line_items.where('"spree_line_items"."id" NOT IN (?)', permissions.editable_line_items)
|
||||
|
||||
line_items.select{ |li| line_items_with_hidden_details.include? li }.each do |line_item|
|
||||
# TODO We should really be hiding customer code here too, but until we
|
||||
# have an actual association between order and customer, it's a bit tricky
|
||||
line_item.order.bill_address.andand.assign_attributes(firstname: I18n.t('admin.reports.hidden'), lastname: "", phone: "", address1: "", address2: "", city: "", zipcode: "", state: nil)
|
||||
line_item.order.ship_address.andand.assign_attributes(firstname: I18n.t('admin.reports.hidden'), lastname: "", phone: "", address1: "", address2: "", city: "", zipcode: "", state: nil)
|
||||
line_item.order.assign_attributes(email: I18n.t('admin.reports.hidden'))
|
||||
end
|
||||
line_items
|
||||
OpenFoodNetwork::Reports::LineItems.list(permissions, params)
|
||||
end
|
||||
|
||||
def rules
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
require "open_food_network/reports/line_items"
|
||||
|
||||
module OpenFoodNetwork
|
||||
class PackingReport
|
||||
attr_reader :params
|
||||
@@ -36,28 +38,12 @@ module OpenFoodNetwork
|
||||
end
|
||||
|
||||
def search
|
||||
permissions.visible_orders.complete.not_state(:canceled).search(params[:q])
|
||||
OpenFoodNetwork::Reports::LineItems.search_orders(permissions, params)
|
||||
end
|
||||
|
||||
def table_items
|
||||
return [] unless @render_table
|
||||
|
||||
orders = search.result
|
||||
|
||||
line_items = permissions.visible_line_items.merge(Spree::LineItem.where(order_id: orders))
|
||||
line_items = line_items.supplied_by_any(params[:supplier_id_in]) if params[:supplier_id_in].present?
|
||||
|
||||
line_items_with_hidden_details =
|
||||
permissions.editable_line_items.empty? ? line_items : line_items.where('"spree_line_items"."id" NOT IN (?)', permissions.editable_line_items)
|
||||
|
||||
line_items.select{ |li| line_items_with_hidden_details.include? li }.each do |line_item|
|
||||
# TODO We should really be hiding customer code here too, but until we
|
||||
# have an actual association between order and customer, it's a bit tricky
|
||||
line_item.order.bill_address.andand.assign_attributes(firstname: I18n.t('admin.reports.hidden'), lastname: "", phone: "", address1: "", address2: "", city: "", zipcode: "", state: nil)
|
||||
line_item.order.ship_address.andand.assign_attributes(firstname: I18n.t('admin.reports.hidden'), lastname: "", phone: "", address1: "", address2: "", city: "", zipcode: "", state: nil)
|
||||
line_item.order.assign_attributes(email: I18n.t('admin.reports.hidden'))
|
||||
end
|
||||
line_items
|
||||
OpenFoodNetwork::Reports::LineItems.list(permissions, params)
|
||||
end
|
||||
|
||||
def rules
|
||||
|
||||
30
lib/open_food_network/reports/line_items.rb
Normal file
30
lib/open_food_network/reports/line_items.rb
Normal file
@@ -0,0 +1,30 @@
|
||||
module OpenFoodNetwork
|
||||
module Reports
|
||||
# shared code to search and list line items
|
||||
module LineItems
|
||||
def self.search_orders(permissions, params)
|
||||
permissions.visible_orders.complete.not_state(:canceled).search(params[:q])
|
||||
end
|
||||
|
||||
def self.list(permissions, params)
|
||||
orders = search_orders(permissions, params).result
|
||||
|
||||
line_items = permissions.visible_line_items.merge(Spree::LineItem.where(order_id: orders))
|
||||
line_items = line_items.supplied_by_any(params[:supplier_id_in]) if params[:supplier_id_in].present?
|
||||
|
||||
# If empty array is passed in, the where clause will return all line_items, which is bad
|
||||
line_items_with_hidden_details =
|
||||
permissions.editable_line_items.empty? ? line_items : line_items.where('"spree_line_items"."id" NOT IN (?)', permissions.editable_line_items)
|
||||
|
||||
line_items.select{ |li| line_items_with_hidden_details.include? li }.each do |line_item|
|
||||
# TODO We should really be hiding customer code here too, but until we
|
||||
# have an actual association between order and customer, it's a bit tricky
|
||||
line_item.order.bill_address.andand.assign_attributes(firstname: I18n.t('admin.reports.hidden'), lastname: "", phone: "", address1: "", address2: "", city: "", zipcode: "", state: nil)
|
||||
line_item.order.ship_address.andand.assign_attributes(firstname: I18n.t('admin.reports.hidden'), lastname: "", phone: "", address1: "", address2: "", city: "", zipcode: "", state: nil)
|
||||
line_item.order.assign_attributes(email: I18n.t('admin.reports.hidden'))
|
||||
end
|
||||
line_items
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user