Merge branch 'ordercyclemanagementreports2' of https://github.com/lin-d-hop/openfoodnetwork into lin-d-hop-ordercyclemanagementreports2

This commit is contained in:
Rohan Mitchell
2015-03-12 12:15:22 +11:00
11 changed files with 144 additions and 23 deletions

View File

@@ -28,7 +28,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]
]
}
@@ -59,7 +60,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
@@ -69,7 +69,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")

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

@@ -0,0 +1,7 @@
class Customer < ActiveRecord::Base
belongs_to :enterprise
validates :code, presence: true, uniqueness: {scope: :enterprise_id}
validates :email, presence: true
validates :enterprise_id, presence: true
end

View File

@@ -0,0 +1,3 @@
Spree::ShippingCategory.class_eval do
attr_accessible :temperature_controlled
end

View File

@@ -0,0 +1,5 @@
/ insert_bottom "div[data-hook='admin_shipping_category_form_fields']"
%div.field.align-center{data-hook => "name"}
= f.label :temperature_controlled, t(:temperature_controlled)
= f.check_box :temperature_controlled

View File

@@ -0,0 +1,5 @@
/ insert_after "[data-hook='category_row'] td:first-child"
%td.align-center
= shipping_category.temperature_controlled
%br/

View File

@@ -0,0 +1,4 @@
/ insert_after "[data-hook='categories_header'] th:first-child"
%th
Temperature Controlled

View File

@@ -0,0 +1,18 @@
class CreateCustomers < ActiveRecord::Migration
def change
create_table :customers do |t|
t.string :email, null: false
t.references :enterprise, null: false
t.string :code, null: false
t.references :user
t.timestamps
end
add_index :customers, [:enterprise_id, :code], unique: true
add_index :customers, :email
add_index :customers, :user_id
add_foreign_key :customers, :enterprises, column: :enterprise_id
add_foreign_key :customers, :spree_users, column: :user_id
end
end

View File

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

View File

@@ -155,6 +155,19 @@ ActiveRecord::Schema.define(:version => 20150225232938) 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", :null => false
t.integer "enterprise_id", :null => false
t.string "code", :null => false
t.integer "user_id"
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
add_index "customers", ["user_id"], :name => "index_customers_on_user_id"
create_table "distributors_payment_methods", :id => false, :force => true do |t|
t.integer "distributor_id"
t.integer "payment_method_id"
@@ -588,9 +601,9 @@ ActiveRecord::Schema.define(:version => 20150225232938) 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
@@ -851,8 +864,9 @@ ActiveRecord::Schema.define(:version => 20150225232938) 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", :null => false
end
create_table "spree_shipping_methods", :force => true do |t|
@@ -1084,6 +1098,9 @@ ActiveRecord::Schema.define(:version => 20150225232938) do
add_foreign_key "coordinator_fees", "enterprise_fees", name: "coordinator_fees_enterprise_fee_id_fk"
add_foreign_key "coordinator_fees", "order_cycles", name: "coordinator_fees_order_cycle_id_fk"
add_foreign_key "customers", "enterprises", name: "customers_enterprise_id_fk"
add_foreign_key "customers", "spree_users", name: "customers_user_id_fk", column: "user_id"
add_foreign_key "distributors_payment_methods", "enterprises", name: "distributors_payment_methods_distributor_id_fk", column: "distributor_id"
add_foreign_key "distributors_payment_methods", "spree_payment_methods", name: "distributors_payment_methods_payment_method_id_fk", column: "payment_method_id"

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,18 @@ module OpenFoodNetwork
orders
end
end
def has_temperature_controlled_items?(order)
order.line_items.any? { |line_item| line_item.product.shipping_category.andand.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