LD: Adding additional functionality for order cycle management reports - adding hub code model, adding temp_controlled bool, adding delivery report

This commit is contained in:
Lynne Davis
2015-02-24 13:43:00 +10:00
parent f83ceae5d1
commit a3df4bf026
7 changed files with 114 additions and 24 deletions

View File

@@ -27,7 +27,8 @@ Spree::Admin::ReportsController.class_eval do
["Addresses", :addresses]
],
order_cycle_management: [
["Payment Methods Report", :payment_methods_report]
["Payment Methods Report", :payment_methods],
["Delivery Report", :delivery]
]
}
@@ -58,7 +59,6 @@ Spree::Admin::ReportsController.class_eval do
@report_types = REPORT_TYPES[:customers]
@report_type = params[:report_type]
@report = OpenFoodNetwork::CustomersReport.new spree_current_user, params
render_report(@report.header, @report.table, params[:csv], "customers_#{timestamp}.csv")
end
@@ -68,7 +68,6 @@ Spree::Admin::ReportsController.class_eval do
@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], "order_cycle_management_#{timestamp}.csv")

2
app/models/customer.rb Normal file
View File

@@ -0,0 +1,2 @@
class Customer < ActiveRecord::Base
end

View File

@@ -0,0 +1,13 @@
class CreateCustomers < ActiveRecord::Migration
def change
create_table :customers do |t|
t.string :email
t.references :enterprise
t.string :code
t.timestamps
end
add_index :customers, [:enterprise_id, :code], unique: true
add_index :customers, :email
end
end

View File

@@ -0,0 +1,5 @@
class AddTemperatureControlledToSpreeShippingCategories < ActiveRecord::Migration
def change
add_column :spree_shipping_categories, :temperature_controlled, :boolean
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20141219034321) do
ActiveRecord::Schema.define(:version => 20150216075336) do
create_table "adjustment_metadata", :force => true do |t|
t.integer "adjustment_id"
@@ -155,6 +155,17 @@ ActiveRecord::Schema.define(:version => 20141219034321) do
add_index "coordinator_fees", ["enterprise_fee_id"], :name => "index_coordinator_fees_on_enterprise_fee_id"
add_index "coordinator_fees", ["order_cycle_id"], :name => "index_coordinator_fees_on_order_cycle_id"
create_table "customers", :force => true do |t|
t.string "email"
t.integer "enterprise_id"
t.string "code"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "customers", ["email"], :name => "index_customers_on_email"
add_index "customers", ["enterprise_id", "code"], :name => "index_customers_on_enterprise_id_and_code", :unique => true
create_table "distributors_payment_methods", :id => false, :force => true do |t|
t.integer "distributor_id"
t.integer "payment_method_id"
@@ -574,9 +585,9 @@ ActiveRecord::Schema.define(:version => 20141219034321) do
t.string "email"
t.text "special_instructions"
t.integer "distributor_id"
t.integer "order_cycle_id"
t.string "currency"
t.string "last_ip_address"
t.integer "order_cycle_id"
t.integer "cart_id"
end
@@ -837,8 +848,9 @@ ActiveRecord::Schema.define(:version => 20141219034321) do
create_table "spree_shipping_categories", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.boolean "temperature_controlled"
end
create_table "spree_shipping_methods", :force => true do |t|

View File

@@ -1,3 +1,5 @@
require 'open_food_network/user_balance_calculator'
module OpenFoodNetwork
class OrderCycleManagementReport
attr_reader :params
@@ -7,23 +9,19 @@ module OpenFoodNetwork
end
def header
["First Name", "Last Name", "Email", "Phone", "Hub", "Shipping Method", "Payment Method", "Amount"]
if is_payment_methods?
["First Name", "Last Name", "Hub", "Hub Code", "Email", "Phone", "Shipping Method", "Payment Method", "Amount", "Balance"]
else
["First Name", "Last Name", "Hub", "Hub Code", "Delivery Address", "Delivery Postcode", "Phone", "Shipping Method", "Payment Method", "Amount", "Balance", "Temp Controlled Items?", "Special Instructions"]
end
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.shipping_method.andand.name,
order.payments.first.andand.payment_method.andand.name,
order.payments.first.amount
]
end
if is_payment_methods?
orders.map { |o| payment_method_row o }
else
orders.map { |o| delivery_row o }
end
end
def orders
@@ -34,6 +32,44 @@ module OpenFoodNetwork
filter_to_order_cycle filter_to_payment_method filter_to_shipping_method orders
end
private
def payment_method_row (order)
ba = order.billing_address
da = order.distributor.andand.address
[ba.firstname,
ba.lastname,
order.distributor.andand.name,
customer_code(order.email),
order.email,
ba.phone,
order.shipping_method.andand.name,
order.payments.first.andand.payment_method.andand.name,
order.payments.first.amount,
OpenFoodNetwork::UserBalanceCalculator.new(order.user, order.distributor).balance
]
end
def delivery_row (order)
ba = order.billing_address
da = order.distributor.andand.address
[ba.firstname,
ba.lastname,
order.distributor.andand.name,
customer_code(order.email),
"#{ba.address1} #{ba.address2} #{ba.city}",
ba.zipcode,
ba.phone,
order.shipping_method.andand.name,
order.payments.first.andand.payment_method.andand.name,
order.payments.first.amount,
OpenFoodNetwork::UserBalanceCalculator.new(order.user, order.distributor).balance,
has_temperature_controlled_items?(order),
order.special_instructions
]
end
def filter_to_payment_method(orders)
if params[:payment_method_name].present?
orders.with_payment_method_name(params[:payment_method_name])
@@ -57,5 +93,19 @@ module OpenFoodNetwork
orders
end
end
def has_temperature_controlled_items?(order)
order.line_items.any? { |line_item| line_item.product.shipping_category.nil? ?
false : line_item.product.shipping_category.temperature_controlled? }
end
def is_payment_methods?
params[:report_type] == "payment_methods"
end
def customer_code (email)
customer = Customer.where(email: email).first
customer.nil? ? "" : customer.code
end
end
end

View File

@@ -65,12 +65,21 @@ feature %q{
click_link "Reports"
end
scenario "order payment method report" do
click_link "Order Cycle Management"
scenario "payment method report" do
click_link "Payment Methods Report"
rows = find("table#listing_order_payment_methods").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", "Shipping Method", "Payment Method", "Amount"]
["First Name", "Last Name", "Hub", "Hub Code", "Email", "Phone", "Shipping Method", "Payment Method", "Amount", "Balance"]
].sort
end
scenario "delivery report" do
click_link "Delivery Report"
rows = find("table#listing_order_payment_methods").all("thead tr")
table = rows.map { |r| r.all("th").map { |c| c.text.strip } }
table.sort.should == [
["First Name", "Last Name", "Hub", "Hub Code", "Delivery Address", "Delivery Postcode", "Phone", "Shipping Method", "Payment Method", "Amount", "Balance", "Temp Controlled Items?", "Special Instructions"]
].sort
end
end