From a3df4bf0262154d8dfa3667f6e20d09fc8dca77c Mon Sep 17 00:00:00 2001 From: Lynne Davis Date: Tue, 24 Feb 2015 13:43:00 +1000 Subject: [PATCH] LD: Adding additional functionality for order cycle management reports - adding hub code model, adding temp_controlled bool, adding delivery report --- .../admin/reports_controller_decorator.rb | 5 +- app/models/customer.rb | 2 + db/migrate/20150122145607_create_customers.rb | 13 ++++ ...controlled_to_spree_shipping_categories.rb | 5 ++ db/schema.rb | 20 ++++- .../order_cycle_management_report.rb | 78 +++++++++++++++---- spec/features/admin/reports_spec.rb | 15 +++- 7 files changed, 114 insertions(+), 24 deletions(-) create mode 100644 app/models/customer.rb create mode 100644 db/migrate/20150122145607_create_customers.rb create mode 100644 db/migrate/20150216075336_add_temperature_controlled_to_spree_shipping_categories.rb diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index 655d1778dc..50954950e0 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -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") diff --git a/app/models/customer.rb b/app/models/customer.rb new file mode 100644 index 0000000000..047dcb0c04 --- /dev/null +++ b/app/models/customer.rb @@ -0,0 +1,2 @@ +class Customer < ActiveRecord::Base +end diff --git a/db/migrate/20150122145607_create_customers.rb b/db/migrate/20150122145607_create_customers.rb new file mode 100644 index 0000000000..7993f9a4b6 --- /dev/null +++ b/db/migrate/20150122145607_create_customers.rb @@ -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 diff --git a/db/migrate/20150216075336_add_temperature_controlled_to_spree_shipping_categories.rb b/db/migrate/20150216075336_add_temperature_controlled_to_spree_shipping_categories.rb new file mode 100644 index 0000000000..d939d5fdf3 --- /dev/null +++ b/db/migrate/20150216075336_add_temperature_controlled_to_spree_shipping_categories.rb @@ -0,0 +1,5 @@ +class AddTemperatureControlledToSpreeShippingCategories < ActiveRecord::Migration + def change + add_column :spree_shipping_categories, :temperature_controlled, :boolean + end +end diff --git a/db/schema.rb b/db/schema.rb index 2d8f28fbdd..3c77da926a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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| diff --git a/lib/open_food_network/order_cycle_management_report.rb b/lib/open_food_network/order_cycle_management_report.rb index 06a90bfe25..ac98b062c4 100644 --- a/lib/open_food_network/order_cycle_management_report.rb +++ b/lib/open_food_network/order_cycle_management_report.rb @@ -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 diff --git a/spec/features/admin/reports_spec.rb b/spec/features/admin/reports_spec.rb index 0524a6769e..0956f268d3 100644 --- a/spec/features/admin/reports_spec.rb +++ b/spec/features/admin/reports_spec.rb @@ -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