mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
266 UK: Adding first UK report - Payment Methods Report - to find balances per ordercycle for multiple payment method options. Working, but not complete to spec yet
This commit is contained in:
@@ -4,6 +4,7 @@ require 'open_food_network/products_and_inventory_report'
|
||||
require 'open_food_network/group_buy_report'
|
||||
require 'open_food_network/order_grouper'
|
||||
require 'open_food_network/customers_report'
|
||||
require 'open_food_network/order_cycle_management_report'
|
||||
|
||||
Spree::Admin::ReportsController.class_eval do
|
||||
|
||||
@@ -21,11 +22,14 @@ Spree::Admin::ReportsController.class_eval do
|
||||
customers: [
|
||||
["Mailing List", :mailing_list],
|
||||
["Addresses", :addresses]
|
||||
],
|
||||
order_cycle_management: [
|
||||
["Payment Methods Report", :payment_methods_report]
|
||||
]
|
||||
}
|
||||
|
||||
# Fetches user's distributors, suppliers and order_cycles
|
||||
before_filter :load_data, only: [:customers, :products_and_inventory]
|
||||
before_filter :load_data, only: [:customers, :products_and_inventory, :order_cycle_management]
|
||||
|
||||
# Render a partial for orders and fulfillment description
|
||||
respond_override :index => { :html => { :success => lambda {
|
||||
@@ -35,6 +39,8 @@ Spree::Admin::ReportsController.class_eval do
|
||||
render_to_string(partial: 'products_and_inventory_description', layout: false, locals: {report_types: REPORT_TYPES[:products_and_inventory]}).html_safe
|
||||
@reports[:customers][:description] =
|
||||
render_to_string(partial: 'customers_description', layout: false, locals: {report_types: REPORT_TYPES[:customers]}).html_safe
|
||||
@reports[:order_cycle_management][:description] =
|
||||
render_to_string(partial: 'order_cycle_management_description', layout: false, locals: {report_types: REPORT_TYPES[:order_cycle_management]}).html_safe
|
||||
} } }
|
||||
|
||||
|
||||
@@ -53,6 +59,18 @@ Spree::Admin::ReportsController.class_eval do
|
||||
render_report(@report.header, @report.table, params[:csv], "customers.csv")
|
||||
end
|
||||
|
||||
def order_cycle_management
|
||||
@report_types = REPORT_TYPES[:order_cycle_management]
|
||||
@report_type = params[:report_type]
|
||||
@report = OpenFoodNetwork::OrderCycleManagementReport.new spree_current_user, params
|
||||
|
||||
@search = Spree::Order.complete.not_state(:canceled).managed_by(spree_current_user).search(params[:q])
|
||||
|
||||
@orders = @search.result
|
||||
|
||||
render_report(@report.header, @report.table, params[:csv], "customers.csv")
|
||||
end
|
||||
|
||||
def orders_and_distributors
|
||||
params[:q] = {} unless params[:q]
|
||||
|
||||
@@ -613,7 +631,8 @@ Spree::Admin::ReportsController.class_eval do
|
||||
:orders_and_fulfillment => {:name => "Orders & Fulfillment Reports", :description => ''},
|
||||
:customers => {:name => "Customers", :description => 'Customer details'},
|
||||
:products_and_inventory => {:name => "Products & Inventory", :description => ''},
|
||||
:sales_total => { :name => "Sales Total", :description => "Sales Total For All Orders" }
|
||||
:order_cycle_management => {:name => "UK Order Cycle Management", :description => ''}
|
||||
|
||||
}
|
||||
# Return only reports the user is authorized to view.
|
||||
reports.select { |action| can? action, :report }
|
||||
|
||||
@@ -7,5 +7,24 @@ module Spree
|
||||
[ "#{oc.name} (#{orders_open_at} - #{orders_close_at})".html_safe, oc.id ]
|
||||
end
|
||||
end
|
||||
|
||||
#lin-d-hop
|
||||
#Find the payment methods options for reporting.
|
||||
#I don't like that this is done in two loops, but redundant list entries
|
||||
# were created otherwise...
|
||||
def report_payment_method_options(orders)
|
||||
payment_method_list = {}
|
||||
orders.map do |o|
|
||||
payment_method_name = o.payments.first.payment_method.andand.name
|
||||
payment_method_id = o.payments.first.payment_method.andand.id
|
||||
payment_method_list[payment_method_name] = payment_method_id
|
||||
end
|
||||
payment_method_list.each do |key, value|
|
||||
[ "#{value}".html_safe, key]
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@@ -122,7 +122,7 @@ class AbilityDecorator
|
||||
end
|
||||
|
||||
# Reports page
|
||||
can [:admin, :index, :customers, :group_buys, :bulk_coop, :payments, :orders_and_distributors, :orders_and_fulfillment, :products_and_inventory], :report
|
||||
can [:admin, :index, :customers, :orders_and_distributors, :group_buys, :bulk_coop, :payments, :orders_and_fulfillment, :products_and_inventory, :order_cycle_management], :report
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
%ul{style: "margin-left: 12pt"}
|
||||
- report_types.each do |report_type|
|
||||
%li
|
||||
= link_to report_type[0], "#{order_cycle_management_admin_reports_url}?report_type=#{report_type[1]}"
|
||||
@@ -0,0 +1,38 @@
|
||||
= form_tag spree.order_cycle_management_admin_reports_url do |f|
|
||||
%br
|
||||
= label_tag nil, "Order Cycle: "
|
||||
= select_tag(:order_cycle_id,
|
||||
options_for_select(report_order_cycle_options(@order_cycles), params[:order_cycle_id]),
|
||||
include_blank: true)
|
||||
%br
|
||||
%br
|
||||
= label_tag nil, "Payment Methods (hold Ctrl to select multiple payment methods)"
|
||||
%br
|
||||
|
||||
= select_tag(:payment_method_id,
|
||||
options_for_select(report_payment_method_options(@orders), params[:payment_method_id]),
|
||||
multiple: true, include_blank: true)
|
||||
%br
|
||||
%br
|
||||
= check_box_tag :csv
|
||||
= label_tag :csv, "Download as csv"
|
||||
%br
|
||||
%br
|
||||
= button t(:search)
|
||||
|
||||
%br
|
||||
%br
|
||||
%table#listing_order_payment_methods.index
|
||||
%thead
|
||||
%tr{'data-hook' => "orders_header"}
|
||||
- @report.header.each do |heading|
|
||||
%th=heading
|
||||
%tbody
|
||||
- @report.table.each do |row|
|
||||
%tr
|
||||
- row.each do |column|
|
||||
%td= column
|
||||
- if @report.table.empty?
|
||||
%tr
|
||||
%td{:colspan => "2"}= t(:none)
|
||||
|
||||
@@ -112,6 +112,7 @@ end
|
||||
|
||||
Spree::Core::Engine.routes.prepend do
|
||||
match '/admin/reports/orders_and_distributors' => 'admin/reports#orders_and_distributors', :as => "orders_and_distributors_admin_reports", :via => [:get, :post]
|
||||
match '/admin/reports/order_cycle_management' => 'admin/reports#order_cycle_management', :as => "order_cycle_management_admin_reports", :via => [:get, :post]
|
||||
match '/admin/reports/group_buys' => 'admin/reports#group_buys', :as => "group_buys_admin_reports", :via => [:get, :post]
|
||||
match '/admin/reports/bulk_coop' => 'admin/reports#bulk_coop', :as => "bulk_coop_admin_reports", :via => [:get, :post]
|
||||
match '/admin/reports/payments' => 'admin/reports#payments', :as => "payments_admin_reports", :via => [:get, :post]
|
||||
|
||||
65
lib/open_food_network/order_cycle_management_report.rb
Normal file
65
lib/open_food_network/order_cycle_management_report.rb
Normal file
@@ -0,0 +1,65 @@
|
||||
module OpenFoodNetwork
|
||||
class OrderCycleManagementReport
|
||||
attr_reader :params
|
||||
def initialize(user, params = {})
|
||||
@params = params
|
||||
@user = user
|
||||
end
|
||||
|
||||
def header
|
||||
["First Name", "Last Name", "Email", "Phone", "Hub", "Payment Method", "Amount ", "Amount Paid"]
|
||||
|
||||
end
|
||||
|
||||
def table
|
||||
orders.map do |order|
|
||||
ba = order.billing_address
|
||||
da = order.distributor.andand.address
|
||||
[ba.firstname,
|
||||
ba.lastname,
|
||||
order.email,
|
||||
ba.phone,
|
||||
order.distributor.andand.name,
|
||||
order.payments.first.andand.payment_method.andand.name,
|
||||
order.payments.first.amount
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
def orders
|
||||
filter Spree::Order
|
||||
end
|
||||
|
||||
def filter(orders)
|
||||
filter_for_user filter_active filter_to_order_cycle filter_to_payment_method orders
|
||||
end
|
||||
|
||||
def filter_for_user (orders)
|
||||
orders.managed_by(@user).distributed_by_user(@user)
|
||||
end
|
||||
|
||||
def filter_active (orders)
|
||||
orders.complete.where("spree_orders.state != ?", :cancelled)
|
||||
end
|
||||
|
||||
def filter_to_payment_method (orders)
|
||||
if params.has_key? (:payment_method_id)
|
||||
orders.joins(:payments)
|
||||
.where(:spree_payments => {:payment_method_id => params[:payment_method_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
|
||||
|
||||
@@ -57,6 +57,22 @@ feature %q{
|
||||
end
|
||||
end
|
||||
|
||||
describe "Order cycle management report" do
|
||||
before do
|
||||
login_to_admin_section
|
||||
click_link "Reports"
|
||||
end
|
||||
|
||||
scenario "order payment method report" do
|
||||
click_link "Order Cycle Management"
|
||||
rows = find("table#listing_order_payment_method").all("thead tr")
|
||||
table = rows.map { |r| r.all("th").map { |c| c.text.strip } }
|
||||
table.sort.should == [
|
||||
["First Name", "Last Name", "Email", "Phone", "Hub", "Payment Method", "Amount", "Amount Paid"]
|
||||
].sort
|
||||
end
|
||||
end
|
||||
|
||||
scenario "orders and distributors report" do
|
||||
login_to_admin_section
|
||||
click_link 'Reports'
|
||||
|
||||
Reference in New Issue
Block a user