cleaning up order report

This commit is contained in:
Andrew Spinks
2012-07-19 22:48:42 +10:00
parent 58e9cd3eeb
commit 7e9a84087e
8 changed files with 115 additions and 68 deletions

View File

@@ -56,5 +56,6 @@ group :test, :development do
gem 'database_cleaner', '0.7.1', :require => false
gem 'spork', '~> 1.0rc'
gem 'pry'
gem 'awesome_print'
end

View File

@@ -82,6 +82,7 @@ GEM
andand (1.3.3)
ansi (1.4.2)
arel (3.0.2)
awesome_print (1.0.2)
aws-s3 (0.6.3)
builder
mime-types
@@ -322,6 +323,7 @@ PLATFORMS
DEPENDENCIES
andand
awesome_print
aws-s3
capybara
coffee-rails (~> 3.2.1)

View File

@@ -1,10 +1,11 @@
require 'csv'
require 'open_food_web/order_and_distributor_report'
Spree::Admin::ReportsController.class_eval do
Spree::Admin::ReportsController::AVAILABLE_REPORTS.merge!({:orders => {:name => "Orders", :description => "Orders with distributor details"}})
Spree::Admin::ReportsController::AVAILABLE_REPORTS.merge!({:orders_and_distributors => {:name => "Orders And Distributors", :description => "Orders with distributor details"}})
def orders
def orders_and_distributors
params[:q] = {} unless params[:q]
if params[:q][:created_at_gt].blank?
@@ -19,20 +20,15 @@ Spree::Admin::ReportsController.class_eval do
params[:q][:meta_sort] ||= "created_at.desc"
@search = Spree::Order.complete.search(params[:q])
@orders = @search.result
orders = @search.result
@report = OpenFoodWeb::OrderAndDistributorReport.new orders
if(!params[:csv])
render :html => @orders
render :html => @report
else
csv_string = CSV.generate do |csv|
csv << ["Order date", "Order Id", "Name","Email", "SKU", "Item cost", "Quantity", "Cost", "Shipping cost", "Distributor", "Distributor address", "Distributor city", "Distributor postcode"]
@orders.each do |order|
order.line_items.each do |line_item|
csv << [order.created_at, order.id, order.bill_address.full_name, order.user.email,
line_item.product.sku, line_item.product.name, line_item.quantity, line_item.price * line_item.quantity, line_item.itemwise_shipping_cost,
order.distributor.name, order.distributor.pickup_address.address1, order.distributor.pickup_address.city, order.distributor.pickup_address.zipcode ]
end
end
csv << @report.header
@report.table.each { |row| csv << row }
end
send_data csv_string
end

View File

@@ -1,55 +0,0 @@
%table#listing_orders.index
%thead
%tr{'data-hook' => "orders_header"}
%th Order Date
%th Order Id
%th Name
%th Email
%th SKU
%th Item name
%th Item cost
%th Quantity
%th Shipping cost
%th Distributor
%th Distributor address
%th Distributor city
%th Distributor postcode
%tbody
- @orders.each do |order|
- order.line_items.each do |line_item|
%tr
%td= order.created_at
%td= order.id
%td= order.bill_address.full_name
%td= order.user.email
%td= line_item.product.sku
%td= line_item.product.name
%td= line_item.quantity
%td= line_item.price * line_item.quantity
%td= line_item.itemwise_shipping_cost
%td= order.distributor.name
%td= order.distributor.pickup_address.address1
%td= order.distributor.pickup_address.city
%td= order.distributor.pickup_address.zipcode
- if @orders.empty?
%tr
%td{:colspan => "2"}= t(:none)
- content_for :sidebar do
= form_for @search, :url => spree.orders_admin_reports_path do |s|
= label_tag nil, t(:date_range)
%br
.date-range-filter
%div{"class" => "left sub-field"}
= s.text_field :created_at_gt, :class => 'datepicker'
%br
= label_tag nil, t(:start), :class => 'sub'
%div{"class" => "right sub-field"}
= s.text_field :created_at_lt, :class => 'datepicker'
%br
= label_tag nil, t(:stop)
= check_box_tag :csv
= label_tag :csv, "Download as csv"
%br
= button t(:search)

View File

@@ -0,0 +1,32 @@
%table#listing_orders.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)
- content_for :sidebar do
= form_for @search, :url => spree.orders_and_distributors_admin_reports_path do |s|
= label_tag nil, t(:date_range)
%br
.date-range-filter
%div{"class" => "left sub-field"}
= s.text_field :created_at_gt, :class => 'datepicker'
%br
= label_tag nil, t(:start), :class => 'sub'
%div{"class" => "right sub-field"}
= s.text_field :created_at_lt, :class => 'datepicker'
%br
= label_tag nil, t(:stop)
= check_box_tag :csv
= label_tag :csv, "Download as csv"
%br
= button t(:search)

View File

@@ -18,5 +18,5 @@ Spree::Core::Engine.routes.prepend do
resources :suppliers
end
match '/admin/reports/orders' => 'admin/reports#orders', :as => "orders_admin_reports", :via => [:get, :post]
match '/admin/reports/orders_and_distributors' => 'admin/reports#orders_and_distributors', :as => "orders_and_distributors_admin_reports", :via => [:get, :post]
end

View File

@@ -0,0 +1,24 @@
module OpenFoodWeb
class OrderAndDistributorReport
def initialize orders
@orders = orders
end
def header
["Order date", "Order Id", "Name","Email", "SKU", "Item cost", "Quantity", "Cost", "Shipping cost", "Distributor", "Distributor address", "Distributor city", "Distributor postcode"]
end
def table
order_and_distributor_details = []
@orders.each do |order|
order.line_items.each do |line_item|
order_and_distributor_details << [order.created_at, order.id, order.bill_address.full_name, order.user.email,
line_item.product.sku, line_item.product.name, line_item.quantity, line_item.price * line_item.quantity, line_item.itemwise_shipping_cost,
order.distributor.name, order.distributor.pickup_address.address1, order.distributor.pickup_address.city, order.distributor.pickup_address.zipcode ]
end
end
order_and_distributor_details
end
end
end

View File

@@ -0,0 +1,47 @@
require 'spec_helper'
module OpenFoodWeb
describe OrderAndDistributorReport do
describe "orders and distributors report" do
let(:bill_address) { create(:address) }
let(:distributor_address) { create(:address, :address1 => "distributor address", :city => 'The Shire', :zipcode => "1234") }
let(:distributor) { create(:distributor, :pickup_address => distributor_address) }
let(:product) do
product = create(:product)
product_distribution = create(:product_distribution, :product => product, :distributor => distributor, :shipping_method => create(:shipping_method))
product
end
let(:order) do
create(:order, :distributor => distributor, :bill_address => bill_address)
end
let(:line_item) do
line_item = create(:line_item, :product => product, :order => order)
order.line_items << line_item
line_item
end
it "should return a header row describing the report" do
subject = OrderAndDistributorReport.new [order]
header = subject.header
header.should == ["Order date", "Order Id", "Name","Email", "SKU", "Item cost", "Quantity", "Cost", "Shipping cost", "Distributor", "Distributor address", "Distributor city", "Distributor postcode"]
end
it "should denormalise order and distributor details for display as csv" do
subject = OrderAndDistributorReport.new [order]
table = subject.table
table[0].should == [order.created_at, order.id, bill_address.full_name, order.user.email,
line_item.product.sku, line_item.product.name, line_item.quantity, line_item.price * line_item.quantity, line_item.itemwise_shipping_cost,
distributor.name, distributor.pickup_address.address1, distributor.pickup_address.city, distributor.pickup_address.zipcode ]
end
it "should include breakdown an order into each line item"
end
end
end