From ddb01e4ccb59063b4218b2ed546eb29606fb3808 Mon Sep 17 00:00:00 2001 From: Rob H Date: Sun, 21 Oct 2012 13:12:48 +1100 Subject: [PATCH 01/52] WIP: Create report grouper, write reports for bulk co-ops and payments --- .../admin/reports_controller_decorator.rb | 136 +++++++- .../spree/admin/reports/bulk_coop.html.haml | 41 +++ .../spree/admin/reports/payments.html.haml | 41 +++ lib/open_food_web/order_grouper.rb | 56 ++++ spec/lib/open_food_web/order_grouper_spec.rb | 298 ++++++++++++++++++ 5 files changed, 571 insertions(+), 1 deletion(-) create mode 100644 app/views/spree/admin/reports/bulk_coop.html.haml create mode 100644 app/views/spree/admin/reports/payments.html.haml create mode 100644 lib/open_food_web/order_grouper.rb create mode 100644 spec/lib/open_food_web/order_grouper_spec.rb diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index 10c09b7bdb..ed2d7a0c7c 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -1,11 +1,14 @@ require 'csv' require 'open_food_web/order_and_distributor_report' require 'open_food_web/group_buy_report' +require 'open_food_web/order_grouper' Spree::Admin::ReportsController.class_eval do Spree::Admin::ReportsController::AVAILABLE_REPORTS.merge!({:orders_and_distributors => {:name => "Orders And Distributors", :description => "Orders with distributor details"}}) Spree::Admin::ReportsController::AVAILABLE_REPORTS.merge!({:group_buys => {:name => "Group Buys", :description => "Orders by supplier and variant"}}) + Spree::Admin::ReportsController::AVAILABLE_REPORTS.merge!({:bulk_coop => {:name => "Bulk Co-Op", :description => "Reports for Bulk Co-Op orders"}}) + Spree::Admin::ReportsController::AVAILABLE_REPORTS.merge!({:payments => {:name => "Payment Reports", :description => "Reports for Payments"}}) def orders_and_distributors params[:q] = {} unless params[:q] @@ -52,7 +55,7 @@ Spree::Admin::ReportsController.class_eval do @search = Spree::Order.complete.search(params[:q]) orders = @search.result - + @distributors = Spree::Distributor.all @report = OpenFoodWeb::GroupBuyReport.new orders @@ -67,4 +70,135 @@ Spree::Admin::ReportsController.class_eval do end end + def bulk_coop + params[:q] = {} unless params[:q] + + if params[:q][:created_at_gt].blank? + params[:q][:created_at_gt] = Time.zone.now.beginning_of_month + else + params[:q][:created_at_gt] = Time.zone.parse(params[:q][:created_at_gt]).beginning_of_day rescue Time.zone.now.beginning_of_month + end + + if params[:q] && !params[:q][:created_at_lt].blank? + params[:q][:created_at_lt] = Time.zone.parse(params[:q][:created_at_lt]).end_of_day rescue "" + end + params[:q][:meta_sort] ||= "created_at.desc" + + @search = Spree::Order.complete.search(params[:q]) + orders = @search.result.select { |o| o.completed? } # Only select complete orders + line_items = orders.map { |o| o.line_items }.flatten + + # Ignore supplier conditions if "All" selected + #if params[:supplier_id] && params[:supplier_id] != "All" + # line_items = line_items.select { |li| li.variant.product.supplier_id == params[:supplier_id] } + #end + + #[Distributor.new(:id => nil, :name => 'All')]+ + @distributors = Spree::Distributor.all + @report_type = params[:report_type] + + case params[:report_type] + when "bulk_coop_supplier_report" + rules = [ { group_by: Proc.new { |li| li.variant.product.supplier }, sort_by: Proc.new { |supplier| supplier.name } }, { group_by: Proc.new { |li| li.variant.product }, sort_by: Proc.new { |product| product.name }, summary_columns: [ Proc.new { |lis| lis.first.variant.product.supplier.name }, Proc.new { |lis| lis.first.variant.product.name }, Proc.new { |lis| "UNIT SIZE" }, Proc.new { |lis| "" }, Proc.new { |lis| "" }, Proc.new { |lis| lis.sum { |li| li.quantity * li.variant.weight } }, Proc.new { |lis| lis.sum { |li| (li.max_quantity || 0) * li.variant.weight } } ] }, { group_by: Proc.new { |li| li.variant }, sort_by: Proc.new { |variant| variant.options_text } } ] + columns = [ Proc.new { |lis| lis.first.variant.product.supplier.name }, Proc.new { |lis| lis.first.variant.product.name }, Proc.new { |lis| "UNIT SIZE" }, Proc.new { |lis| lis.first.variant.options_text }, Proc.new { |lis| lis.first.variant.weight }, Proc.new { |lis| lis.sum { |li| li.quantity } }, Proc.new { |lis| lis.sum { |li| li.max_quantity || 0 } } ] + header = ["Supplier","Product","Unit Size","Variant","Weight","Sum Total","Sum Max Total"] + when "bulk_coop_allocation" + rules = [ { group_by: Proc.new { |li| li.variant.product }, sort_by: Proc.new { |product| product.name } }, { group_by: Proc.new { |li| li.variant }, sort_by: Proc.new { |variant| variant.options_text } }, { group_by: Proc.new { |li| li.order.user }, sort_by: Proc.new { |user| user.to_s } } ] + columns = [ Proc.new { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, Proc.new { |lis| lis.first.variant.product.name }, Proc.new { |lis| "UNIT SIZE" }, Proc.new { |lis| lis.first.variant.options_text }, Proc.new { |lis| lis.first.variant.weight }, Proc.new { |lis| lis.sum { |li| li.quantity } }, Proc.new { |lis| lis.sum { |li| li.max_quantity || 0 } } ] + header = ["Customer","Product","Unit Size","Variant","Weight","Sum Total","Sum Max Total"] + when "bulk_coop_packing_sheets" + rules = [ { group_by: Proc.new { |li| li.variant.product }, sort_by: Proc.new { |product| product.name } }, { group_by: Proc.new { |li| li.variant }, sort_by: Proc.new { |variant| variant.options_text } }, { group_by: Proc.new { |li| li.order.user }, sort_by: Proc.new { |user| user.to_s } } ] + columns = [ Proc.new { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, Proc.new { |lis| lis.first.variant.product.name }, Proc.new { |lis| lis.first.variant.options_text }, Proc.new { |lis| lis.sum { |li| li.quantity } } ] + header = ["Customer","Product","Variant","Sum Total"] + when "bulk_coop_customer_payments" + rules = [ { group_by: Proc.new { |li| li.order.user }, sort_by: Proc.new { |user| user.to_s } }, { group_by: Proc.new { |li| li.order }, sort_by: Proc.new { |order| order.created_at } } ] + columns = [ Proc.new { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, Proc.new { |lis| lis.first.order.created_at.to_s }, Proc.new { |lis| lis.map { |li| li.order }.to_set.to_a.sum { |o| o.total } }, Proc.new { |lis| lis.map { |li| li.order }.to_set.to_a.sum { |o| o.outstanding_balance } }, Proc.new { |lis| lis.map { |li| li.order }.to_set.to_a.sum { |o| o.payment_total } } ] + header = ["Customer","Date of Order","Total Cost","Amount Owing","Amount Paid"] + else # List all line items + rules = [ { group_by: Proc.new { |li| li.variant.product.supplier }, sort_by: Proc.new { |supplier| supplier.name } }, { group_by: Proc.new { |li| li.variant.product }, sort_by: Proc.new { |product| product.name } }, { group_by: Proc.new { |li| li.variant }, sort_by: Proc.new { |variant| variant.options_text } } ] + columns = [ Proc.new { |lis| lis.first.variant.product.supplier.name }, Proc.new { |lis| lis.first.variant.product.name }, Proc.new { |lis| "UNIT SIZE" }, Proc.new { |lis| lis.first.variant.options_text }, Proc.new { |lis| lis.first.variant.weight }, Proc.new { |lis| lis.sum { |li| li.quantity } }, Proc.new { |lis| lis.sum { |li| li.max_quantity || 0 } } ] + header = ["Supplier","Product","Unit Size","Variant","Weight","Sum Total","Sum Max Total"] + end + + order_grouper = OpenFoodWeb::OrderGrouper.new rules, columns + + @header = header + @table = order_grouper.table(line_items) + + unless params[:csv] + render :html => @table + else + csv_string = CSV.generate do |csv| + csv << @header + @table.each { |row| csv << row } + end + send_data csv_string, :filename => "bulk_coop.csv" + end + end + + def payments + params[:q] = {} unless params[:q] + + if params[:q][:created_at_gt].blank? + params[:q][:created_at_gt] = Time.zone.now.beginning_of_month + else + params[:q][:created_at_gt] = Time.zone.parse(params[:q][:created_at_gt]).beginning_of_day rescue Time.zone.now.beginning_of_month + end + + if params[:q] && !params[:q][:created_at_lt].blank? + params[:q][:created_at_lt] = Time.zone.parse(params[:q][:created_at_lt]).end_of_day rescue "" + end + params[:q][:meta_sort] ||= "created_at.desc" + + @search = Spree::Order.complete.search(params[:q]) + orders = @search.result.select { |o| o.complete? } # Only select complete orders + payments = orders.map { |o| o.payments.select { |payment| payment.completed? } }.flatten # Only select completed payments + + # Ignore supplier conditions if "All" selected + #if params[:supplier_id] && params[:supplier_id] != "All" + # line_items = line_items.select { |li| li.variant.product.supplier_id == params[:supplier_id] } + #end + + #[Distributor.new(:id => nil, :name => 'All')]+ + @distributors = Spree::Distributor.all + @report_type = params[:report_type] + + case params[:report_type] + when "payments_by_payment_type" + table_items = payments + rules = [ { group_by: Proc.new { |payment| payment.order.payment_state }, sort_by: Proc.new { |payment_state| payment_state } }, { group_by: Proc.new { |payment| payment.order.distributor }, sort_by: Proc.new { |distributor| distributor.name } }, { group_by: Proc.new { |payment| payment.payment_method }, sort_by: Proc.new { |method| method.name } } ] + columns = [ Proc.new { |payments| payments.first.order.payment_state }, Proc.new { |payments| payments.first.order.distributor.name }, Proc.new { |payments| payments.first.payment_method.name }, Proc.new { |payments| payments.sum { |payment| payment.amount } } ] + header = ["Payment State", "Distributor", "Payment Type", "Total ($)"] + when "itemised_payment_totals" + table_items = orders + rules = [ { group_by: Proc.new { |order| order.payment_state }, sort_by: Proc.new { |payment_state| payment_state } }, { group_by: Proc.new { |order| order.distributor }, sort_by: Proc.new { |distributor| distributor.name } } ] + columns = [ Proc.new { |orders| orders.first.payment_state }, Proc.new { |orders| orders.first.distributor.name }, Proc.new { |orders| orders.sum { |o| o.item_total } }, Proc.new { |orders| orders.sum { |o| o.ship_total } }, Proc.new { |orders| orders.sum { |o| o.outstanding_balance } }, Proc.new { |orders| orders.sum { |o| o.total } } ] + header = ["Payment State", "Distributor", "Product Total ($)", "Shipping Total ($)", "Outstanding Balance ($)", "Total ($)"] + when "payment_totals" + table_items = orders + rules = [ { group_by: Proc.new { |order| order.payment_state }, sort_by: Proc.new { |payment_state| payment_state } }, { group_by: Proc.new { |order| order.distributor }, sort_by: Proc.new { |distributor| distributor.name } } ] + columns = [ Proc.new { |orders| orders.first.payment_state }, Proc.new { |orders| orders.first.distributor.name }, Proc.new { |orders| orders.sum { |o| o.item_total } }, Proc.new { |orders| orders.sum { |o| o.ship_total } }, Proc.new { |orders| orders.sum { |o| o.total } }, Proc.new { |orders| orders.sum { |o| o.payments.select { |payment| payment.completed? && (payment.payment_method.name.to_s.include? "EFT") }.sum { |payment| payment.amount } } }, Proc.new { |orders| orders.sum { |o| o.payments.select { |payment| payment.completed? && (payment.payment_method.name.to_s.include? "PayPal") }.sum{ |payment| payment.amount } } }, Proc.new { |orders| orders.sum { |o| o.outstanding_balance } } ] + header = ["Payment State", "Distributor", "Product Total ($)", "Shipping Total ($)", "Total ($)", "EFT ($)", "PayPal ($)", "Outstanding Balance ($)"] + else + table_items = payments + rules = [ { group_by: Proc.new { |payment| payment.order.payment_state }, sort_by: Proc.new { |payment_state| payment_state } }, { group_by: Proc.new { |payment| payment.order.distributor }, sort_by: Proc.new { |distributor| distributor.name } }, { group_by: Proc.new { |payment| payment.payment_method }, sort_by: Proc.new { |method| method.name } } ] + columns = [ Proc.new { |payments| payments.first.order.payment_state }, Proc.new { |payments| payments.first.order.distributor.name }, Proc.new { |payments| payments.first.payment_method.name }, Proc.new { |payments| payments.sum { |payment| payment.amount } } ] + header = ["Payment State", "Distributor", "Payment Type", "Total ($)"] + end + + order_grouper = OpenFoodWeb::OrderGrouper.new rules, columns + + @header = header + @table = order_grouper.table(table_items) + + unless params[:csv] + render :html => @table + else + csv_string = CSV.generate do |csv| + csv << @header + @table.each { |row| csv << row } + end + send_data csv_string, :filename => "payments.csv" + end + end end \ No newline at end of file diff --git a/app/views/spree/admin/reports/bulk_coop.html.haml b/app/views/spree/admin/reports/bulk_coop.html.haml new file mode 100644 index 0000000000..b1b4824d1a --- /dev/null +++ b/app/views/spree/admin/reports/bulk_coop.html.haml @@ -0,0 +1,41 @@ += form_for @search, :url => spree.bulk_coop_admin_reports_path do |f| + = label_tag nil, t(:date_range) + %br + .date-range-filter + %div{"class" => "left sub-field"} + = f.text_field :created_at_gt, :class => 'datepicker' + %br + = label_tag nil, t(:start), :class => 'sub' + %div{"class" => "right sub-field"} + = f.text_field :created_at_lt, :class => 'datepicker' + %br + = label_tag nil, t(:stop) + %br + = label_tag nil, "Distributor: " + = f.collection_select(:distributor_id_eq, @distributors, :id, :name, :include_blank => 'All') + /= label_tag nil, "Supplier: " + /= collection_select(:supplier, :supplier_id, @suppliers, :id, :name, :prompt => 'All') + %br + = label_tag nil, "Report Type: " + = select_tag(:report_type, options_for_select([['Bulk Co-op - Totals by Supplier',:bulk_coop_supplier_report],['Bulk Co-op - Allocation',:bulk_coop_allocation],['Bulk Co-op - Packing Sheets',:bulk_coop_packing_sheets],['Bulk Co-op - Customer Payments',:bulk_coop_customer_payments]], @report_type)) + %br + %br + = check_box_tag :csv + = label_tag :csv, "Download as csv" + %br + = button t(:search) +%br +%br +%table#listing_orders.index + %thead + %tr{'data-hook' => "orders_header"} + - @header.each do |heading| + %th=heading + %tbody + - @table.each do |row| + %tr + - row.each do |column| + %td= column + - if @table.empty? + %tr + %td{:colspan => "2"}= t(:none) \ No newline at end of file diff --git a/app/views/spree/admin/reports/payments.html.haml b/app/views/spree/admin/reports/payments.html.haml new file mode 100644 index 0000000000..559e456a8f --- /dev/null +++ b/app/views/spree/admin/reports/payments.html.haml @@ -0,0 +1,41 @@ += form_for @search, :url => spree.payments_admin_reports_path do |f| + = label_tag nil, t(:date_range) + %br + .date-range-filter + %div{"class" => "left sub-field"} + = f.text_field :created_at_gt, :class => 'datepicker' + %br + = label_tag nil, t(:start), :class => 'sub' + %div{"class" => "right sub-field"} + = f.text_field :created_at_lt, :class => 'datepicker' + %br + = label_tag nil, t(:stop) + %br + = label_tag nil, "Distributor: " + = f.collection_select(:distributor_id_eq, @distributors, :id, :name, :include_blank => 'All') + /= label_tag nil, "Supplier: " + /= collection_select(:supplier, :supplier_id, @suppliers, :id, :name, :prompt => 'All') + %br + = label_tag nil, "Report Type: " + = select_tag(:report_type, options_for_select([['Payments By Type',:payments_by_payment_type],['Itemised Payment Totals',:itemised_payment_totals],['Payment Totals',:payment_totals]], @report_type)) + %br + %br + = check_box_tag :csv + = label_tag :csv, "Download as csv" + %br + = button t(:search) +%br +%br +%table#listing_orders.index + %thead + %tr{'data-hook' => "orders_header"} + - @header.each do |heading| + %th=heading + %tbody + - @table.each do |row| + %tr + - row.each do |column| + %td= column + - if @table.empty? + %tr + %td{:colspan => "2"}= t(:none) \ No newline at end of file diff --git a/lib/open_food_web/order_grouper.rb b/lib/open_food_web/order_grouper.rb new file mode 100644 index 0000000000..43587ae83b --- /dev/null +++ b/lib/open_food_web/order_grouper.rb @@ -0,0 +1,56 @@ +module OpenFoodWeb + + class OrderGrouper + def initialize rules, column_constructors + @rules = rules + @column_constructors = column_constructors + end + + def build_tree items, remaining_rules + rules = remaining_rules.clone + unless rules.empty? + rule = rules.delete_at(0) # Remove current rule for subsequent groupings + group_and_sort(rule, rules, items) + else + items + end + end + + def group_and_sort rule, remaining_rules, items + branch = {} + groups = items.group_by { |item| rule[:group_by].call(item) } + sorted_groups = groups.sort_by { |key, value| rule[:sort_by].call(key) } + sorted_groups.each do |property,items_by_property| + branch[property] = build_tree(items_by_property, remaining_rules) + branch[property][:summary_row] = { items: items_by_property, columns: rule[:summary_columns] } unless rule[:summary_columns] == nil || branch[property].class == Array + end + branch + end + + def build_table groups + rows = [] + unless groups.class == Array + groups.each do |key, group| + unless key == :summary_row + build_table(group).each { |g| rows << g } + else + row = Array.new + group[:columns].each { |cols| row << cols.call(group[:items]) } + rows << row + end + end + else + row = Array.new + @column_constructors.each { |column_constructor| row << column_constructor.call(groups) } + rows << row + end + rows + end + + def table items + tree = build_tree(items,@rules) + table = build_table(tree) + table + end + end +end \ No newline at end of file diff --git a/spec/lib/open_food_web/order_grouper_spec.rb b/spec/lib/open_food_web/order_grouper_spec.rb new file mode 100644 index 0000000000..010fd319e2 --- /dev/null +++ b/spec/lib/open_food_web/order_grouper_spec.rb @@ -0,0 +1,298 @@ +#require 'spec_helper' + +#module OpenFoodWeb +# describe OrderGrouper do +# +# before(:each) do +# @orders = [] +# distributor_address = create(:address, :address1 => "distributor address", :city => 'The Shire', :zipcode => "1234") +# distributor = create(:distributor, :pickup_address => distributor_address) +# +# @supplier1 = create(:supplier) +# @variant1 = create(:variant) +# @variant1.product.supplier = @supplier1 +# @variant1.product.save! +# shipping_method = create(:shipping_method) +# product_distribution = create(:product_distribution, :product => @variant1.product, :distributor => distributor, :shipping_method => create(:shipping_method)) +# shipping_instructions = "pick up on thursday please!" +# +# bill_address1 = create(:address) +# order1 = create(:order, :distributor => distributor, :bill_address => bill_address1, :special_instructions => shipping_instructions) +# line_item11 = create(:line_item, :variant => @variant1, :order => order1) +# order1.line_items << line_item11 +# @orders << order1 +# +# bill_address2 = create(:address) +# order2 = create(:order, :distributor => distributor, :bill_address => bill_address2, :special_instructions => shipping_instructions) +# line_item21 = create(:line_item, :variant => @variant1, :order => order2) +# order2.line_items << line_item21 +# +# @variant2 = create(:variant) +# @variant2.product.supplier = @supplier1 +# @variant2.product.save! +# product_distribution = create(:product_distribution, :product => @variant2.product, :distributor => distributor, :shipping_method => create(:shipping_method)) +# +# line_item22 = create(:line_item, :variant => @variant2, :order => order2) +# order2.line_items << line_item22 +# @orders << order2 +# +# @supplier2 = create(:supplier) +# @variant3 = create(:variant) +# @variant3.product.supplier = @supplier2 +# @variant3.product.save! +# product_distribution = create(:product_distribution, :product => @variant3.product, :distributor => distributor, :shipping_method => create(:shipping_method)) + +# bill_address3 = create(:address) +# order3 = create(:order, :distributor => distributor, :bill_address => bill_address3, :special_instructions => shipping_instructions) +# line_item31 = create(:line_item, :variant => @variant3, :order => order3) +# order3.line_items << line_item31 +# @orders << order3 +# end + +# context "when grouping by supplier, then product, then variant" do +# group_rules = [ { group_by: Proc.new { |li| li.variant.product.supplier }, sort_by: Proc.new { |supplier| supplier.name } }, { group_by: Proc.new { |li| li.variant.product }, sort_by: Proc.new { |product| product.name } }, { group_by: Proc.new { |li| li.variant }, sort_by: Proc.new { |variant| variant.options_text } } ] +# column_properties = [ Proc.new { |lis| lis.first.variant.product.supplier.name }, Proc.new { |lis| lis.first.variant.product.name }, Proc.new { |lis| "UNIT SIZE" }, Proc.new { |lis| lis.first.variant.options_text }, Proc.new { |lis| lis.first.variant.weight }, Proc.new { |lis| lis.sum { |li| li.quantity } }, Proc.new { |lis| lis.sum { |li| li.max_quantity || 0 } } ] + +# it "should return a Hash with one key for each supplier represented by the orders" do +# subject = OrderGrouper.new group_rules, column_properties + +# line_items = @orders.map { |o| o.line_items }.flatten + +# groups = subject.grouper_sorter(line_items, group_rules) +# groups.class.should == Hash +# groups.length.should == 2 +# end + +# it "should group items over multiple levels according to group by rules" do +# subject = OrderGrouper.new group_rules, column_properties + +# line_items = @orders.map { |o| o.line_items }.flatten + +# groups = subject.grouper_sorter(line_items, group_rules) +# groups[@supplier1].length.should == 2 +# groups[@supplier2].length.should == 1 +# end + +# it "should return a table as an array" do +# subject = OrderGrouper.new group_rules, column_properties +# +# line_items = @orders.map { |o| o.line_items }.flatten + +# subject.table(line_items).class.should == Array +# end +# end + +# context "when grouping by customers" do +# group_rules = [ { group_by: Proc.new { |li| li.variant.product }, sort_by: Proc.new { |product| product.name } }, { group_by: Proc.new { |li| li.variant }, sort_by: Proc.new { |variant| variant.options_text } }, { group_by: Proc.new { |li| li.order.bill_address }, sort_by: Proc.new { |bill_address| bill_address.firstname + " " + bill_address.lastname } } ] +# column_properties = [ Proc.new { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, Proc.new { |lis| lis.first.variant.product.name }, Proc.new { |lis| "UNIT SIZE" }, Proc.new { |lis| lis.first.variant.options_text }, Proc.new { |lis| lis.first.variant.weight }, Proc.new { |lis| lis.sum { |li| li.quantity } }, Proc.new { |lis| lis.sum { |li| li.max_quantity || 0 } } ] + +# it "should return a table as an array" do +# subject = OrderGrouper.new group_rules, column_properties + +# line_items = @orders.map { |o| o.line_items }.flatten + +# subject.table(line_items).class.should == Array +# end +# end +# end +#end + +require 'spec_helper' + +module OpenFoodWeb + describe OrderGrouper do + + before(:each) do + @items = [1, 2, 3, 4] + end + + context "constructing the table" do + it "should build a tree then build a table" do + rules = [ { group_by: Proc.new { |sentence| sentence.paragraph.chapter }, sort_by: Proc.new { |chapter| chapter.name }, summary_columns: [Proc.new { |is| is.first.paragraph.chapter.name }, Proc.new { |is| "TOTAL" }, Proc.new { |is| "" }, Proc.new { |is| is.sum {|i| i.property1 } } ] }, + { group_by: Proc.new { |sentence| sentence.paragraph }, sort_by: Proc.new { |paragraph| paragraph.name } } ] + columns = [Proc.new { |is| is.first.paragraph.chapter.name }, Proc.new { |is| is.first.paragraph.name }, Proc.new { |is| is.first.name }, Proc.new { |is| is.sum {|i| i.property1 } }] + + subject = OrderGrouper.new rules, columns + + tree = double(:tree) + subject.should_receive(:build_tree).with(@items, rules).and_return(tree) + subject.should_receive(:build_table).with(tree) + + subject.table(@items) + end + + end + + context "grouping items without rules" do + it "returns the original array when no rules are provided" do + rules = [] + column1 = double(:col1) + column2 = double(:col2) + columns = [column1, column2] + subject = OrderGrouper.new rules, columns + + rules.should_receive(:clone).and_return(rules) + subject.build_tree(@items, rules).should == @items + end + end + + context "grouping items with rules" do + it "builds branches by removing a rule from \"rules\" and running group_and_sort" do + rule1 = double(:rule1) + rule2 = double(:rule2) + rules = [rule1, rule2] + column1 = double(:col1) + column2 = double(:col2) + columns = [column1, column2] + + subject = OrderGrouper.new rules, columns + + #rules = [ { group_by: Proc.new { |sentence| sentence.paragraph.chapter }, sort_by: Proc.new { |chapter| chapter.name }, summary_columns: [Proc.new { |is| is.first.paragraph.chapter.name }, Proc.new { |is| "TOTAL" }, Proc.new { |is| "" }, Proc.new { |is| is.sum {|i| i.property1 } } ] }, + #{ group_by: Proc.new { |sentence| sentence.paragraph }, sort_by: Proc.new { |paragraph| paragraph.name } } ] + #columns = [Proc.new { |is| is.first.paragraph.chapter.name }, Proc.new { |is| is.first.paragraph.name }, Proc.new { |is| is.first.name }, Proc.new { |is| is.sum {|i| i.property1 } }] + rules.should_receive(:clone).and_return(rules) + rules.should_receive(:delete_at).with(0) + grouped_tree = double(:grouped_tree) + subject.should_receive(:group_and_sort).and_return(grouped_tree) + + subject.build_tree(@items, rules).should == grouped_tree + end + + it "separates the first rule from rules before sending to group_and_sort" do + rule1 = double(:rule1) + rule2 = double(:rule2) + rules = [rule1, rule2] + column1 = double(:col1) + column2 = double(:col2) + columns = [column1, column2] + + subject = OrderGrouper.new rules, columns + + grouped_tree = double(:grouped_tree) + subject.should_receive(:group_and_sort).with(rule1, rules[1..-1], @items).and_return(grouped_tree) + + subject.build_tree(@items, rules).should == grouped_tree + end + + it "should group, then sort, send each group to build_tree, and return a branch" do + rule1 = double(:rule1) + rule2 = double(:rule2) + rules = [rule1, rule2] + remaining_rules = [rule2] + column1 = double(:col1) + column2 = double(:col2) + columns = [column1, column2] + + summary_columns_object = double(:summary_columns) + rule1.stub(:[]).with(:summary_columns) { summary_columns_object } + + subject = OrderGrouper.new rules, columns + + number_of_categories = 3 + groups = double(:groups) + @items.should_receive(:group_by).and_return(groups) + sorted_groups = {} + 1.upto(number_of_categories) { |i| sorted_groups[i] = double(:group, name: "Group "+ i.to_s ) } + groups.should_receive(:sort_by).and_return(sorted_groups) + group = { group1: 1, group2: 2, group3: 3 } + subject.should_receive(:build_tree).exactly(number_of_categories).times.and_return(group) + + group_tree = {} + 1.upto(number_of_categories) { |i| group_tree[i] = group } + 1.upto(number_of_categories) { |i| group_tree[i][:summary_row] = summary_columns_object } + subject.group_and_sort(rule1, remaining_rules, @items).should == group_tree + end + end + + context "building the table Array" do + it "should return columns when given an Array" do + rule1 = double(:rule1) + rule2 = double(:rule2) + rules = [rule1, rule2] + column1 = double(:col1) + column2 = double(:col2) + columns = [column1, column2] + column1.stub(:call) { "Column1" } + column2.stub(:call) { "Column2" } + + item1 = double(:item1) + item2 = double(:item2) + item3 = double(:item3) + items1 = [item1, item2] + items2 = [item2, item3] + groups = { items1: items1, items2: items1 } + + subject = OrderGrouper.new rules, columns + + column1.should_receive(:call) + column2.should_receive(:call) + + subject.build_table(items1).should == [["Column1", "Column2"]] + end + + it "should return a row for each key-value pair when given a Hash" do + rule1 = double(:rule1) + rule2 = double(:rule2) + rules = [rule1, rule2] + column1 = double(:col1) + column2 = double(:col2) + columns = [column1, column2] + column1.stub(:call) { "Column1" } + column2.stub(:call) { "Column2" } + + item1 = double(:item1) + item2 = double(:item2) + item3 = double(:item3) + items1 = [item1, item2] + items2 = [item2, item3] + items3 = [item3, item1] + groups = { items1: items1, items2: items2, items3: items3 } + + subject = OrderGrouper.new rules, columns + + #subject.should_receive(:build_table).exactly(2).times + + expected_return = [] + groups.length.times { expected_return << ["Column1", "Column2"] } + subject.build_table(groups).should == expected_return + end + + it "should return an extra row when a :summary_row key appears in a given Hash" do + rule1 = double(:rule1) + rule2 = double(:rule2) + rules = [rule1, rule2] + column1 = double(:col1) + column2 = double(:col2) + columns = [column1, column2] + column1.stub(:call) { "Column1" } + column2.stub(:call) { "Column2" } + + sumcol1 = double(:sumcol1) + sumcol2 = double(:sumcol2) + sumcols = [sumcol1, sumcol2] + sumcol1.stub(:call) { "SumColumn1" } + sumcol2.stub(:call) { "SumColumn2" } + + item1 = double(:item1) + item2 = double(:item2) + item3 = double(:item3) + items1 = [item1, item2] + items2 = [item2, item3] + items3 = [item3, item1] + groups = { items1: items1, items2: items2, items3: items3, summary_row: { items: { items2: items2, items3: items3 }, columns: sumcols } } + + subject = OrderGrouper.new rules, columns + + expected_return = [] + groups.each do |key, group| + if key == :summary_row + expected_return << ["SumColumn1", "SumColumn2"] + else + expected_return << ["Column1", "Column2"] + end + end + subject.build_table(groups).should == expected_return + end + end + end +end \ No newline at end of file From eb7d69fe6bb7ec04dda2b4e96e2f6f5b01788ebb Mon Sep 17 00:00:00 2001 From: Rob H Date: Mon, 22 Oct 2012 11:35:48 +1100 Subject: [PATCH 02/52] commit routes.rb file to prevent overwriting --- config/routes.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/routes.rb b/config/routes.rb index 0befa2892e..7fde8d7b66 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,5 +20,7 @@ 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/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] end From 81b33772fe6aa427dbe461b676a8301a08f59dc5 Mon Sep 17 00:00:00 2001 From: Rob H Date: Wed, 24 Oct 2012 15:21:16 +1100 Subject: [PATCH 03/52] Fixes to syntax and formatting for group-reports --- .../admin/reports_controller_decorator.rb | 211 +++++++++++++----- .../spree/admin/reports/bulk_coop.html.haml | 2 - .../spree/admin/reports/payments.html.haml | 2 - db/schema.rb | 156 ++++++------- lib/open_food_web/order_grouper.rb | 28 +-- spec/lib/open_food_web/order_grouper_spec.rb | 181 ++------------- 6 files changed, 276 insertions(+), 304 deletions(-) diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index ed2d7a0c7c..67fe169317 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -85,55 +85,114 @@ Spree::Admin::ReportsController.class_eval do params[:q][:meta_sort] ||= "created_at.desc" @search = Spree::Order.complete.search(params[:q]) - orders = @search.result.select { |o| o.completed? } # Only select complete orders + orders = @search.result line_items = orders.map { |o| o.line_items }.flatten - # Ignore supplier conditions if "All" selected - #if params[:supplier_id] && params[:supplier_id] != "All" - # line_items = line_items.select { |li| li.variant.product.supplier_id == params[:supplier_id] } - #end - - #[Distributor.new(:id => nil, :name => 'All')]+ @distributors = Spree::Distributor.all @report_type = params[:report_type] case params[:report_type] when "bulk_coop_supplier_report" - rules = [ { group_by: Proc.new { |li| li.variant.product.supplier }, sort_by: Proc.new { |supplier| supplier.name } }, { group_by: Proc.new { |li| li.variant.product }, sort_by: Proc.new { |product| product.name }, summary_columns: [ Proc.new { |lis| lis.first.variant.product.supplier.name }, Proc.new { |lis| lis.first.variant.product.name }, Proc.new { |lis| "UNIT SIZE" }, Proc.new { |lis| "" }, Proc.new { |lis| "" }, Proc.new { |lis| lis.sum { |li| li.quantity * li.variant.weight } }, Proc.new { |lis| lis.sum { |li| (li.max_quantity || 0) * li.variant.weight } } ] }, { group_by: Proc.new { |li| li.variant }, sort_by: Proc.new { |variant| variant.options_text } } ] - columns = [ Proc.new { |lis| lis.first.variant.product.supplier.name }, Proc.new { |lis| lis.first.variant.product.name }, Proc.new { |lis| "UNIT SIZE" }, Proc.new { |lis| lis.first.variant.options_text }, Proc.new { |lis| lis.first.variant.weight }, Proc.new { |lis| lis.sum { |li| li.quantity } }, Proc.new { |lis| lis.sum { |li| li.max_quantity || 0 } } ] - header = ["Supplier","Product","Unit Size","Variant","Weight","Sum Total","Sum Max Total"] + + header = ["Supplier", "Product", "Unit Size", "Variant", "Weight", "Sum Total", "Sum Max Total"] + + columns = [ proc { |lis| lis.first.variant.product.supplier.name }, + proc { |lis| lis.first.variant.product.name }, + proc { |lis| "UNIT SIZE" }, + proc { |lis| lis.first.variant.options_text }, + proc { |lis| lis.first.variant.weight }, + proc { |lis| lis.sum { |li| li.quantity } }, + proc { |lis| lis.sum { |li| li.max_quantity || 0 } } ] + + rules = [ { group_by: proc { |li| li.variant.product.supplier }, + sort_by: proc { |supplier| supplier.name } }, + { group_by: proc { |li| li.variant.product }, + sort_by: proc { |product| product.name }, + summary_columns: [ proc { |lis| lis.first.variant.product.supplier.name }, + proc { |lis| lis.first.variant.product.name }, proc { |lis| "UNIT SIZE" }, + proc { |lis| "" }, proc { |lis| "" }, + proc { |lis| lis.sum { |li| li.quantity * li.variant.weight } }, + proc { |lis| lis.sum { |li| (li.max_quantity || 0) * li.variant.weight } } ] }, + { group_by: proc { |li| li.variant }, + sort_by: proc { |variant| variant.options_text } } ] + when "bulk_coop_allocation" - rules = [ { group_by: Proc.new { |li| li.variant.product }, sort_by: Proc.new { |product| product.name } }, { group_by: Proc.new { |li| li.variant }, sort_by: Proc.new { |variant| variant.options_text } }, { group_by: Proc.new { |li| li.order.user }, sort_by: Proc.new { |user| user.to_s } } ] - columns = [ Proc.new { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, Proc.new { |lis| lis.first.variant.product.name }, Proc.new { |lis| "UNIT SIZE" }, Proc.new { |lis| lis.first.variant.options_text }, Proc.new { |lis| lis.first.variant.weight }, Proc.new { |lis| lis.sum { |li| li.quantity } }, Proc.new { |lis| lis.sum { |li| li.max_quantity || 0 } } ] - header = ["Customer","Product","Unit Size","Variant","Weight","Sum Total","Sum Max Total"] + + header = ["Customer", "Product", "Unit Size", "Variant", "Weight", "Sum Total", "Sum Max Total"] + + columns = [ proc { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, + proc { |lis| lis.first.variant.product.name }, + proc { |lis| "UNIT SIZE" }, + proc { |lis| lis.first.variant.options_text }, + proc { |lis| lis.first.variant.weight }, + proc { |lis| lis.sum { |li| li.quantity } }, + proc { |lis| lis.sum { |li| li.max_quantity || 0 } } ] + + rules = [ { group_by: proc { |li| li.variant.product }, + sort_by: proc { |product| product.name } }, + { group_by: proc { |li| li.variant }, + sort_by: proc { |variant| variant.options_text } }, + { group_by: proc { |li| li.order.user }, + sort_by: proc { |user| user.to_s } } ] + when "bulk_coop_packing_sheets" - rules = [ { group_by: Proc.new { |li| li.variant.product }, sort_by: Proc.new { |product| product.name } }, { group_by: Proc.new { |li| li.variant }, sort_by: Proc.new { |variant| variant.options_text } }, { group_by: Proc.new { |li| li.order.user }, sort_by: Proc.new { |user| user.to_s } } ] - columns = [ Proc.new { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, Proc.new { |lis| lis.first.variant.product.name }, Proc.new { |lis| lis.first.variant.options_text }, Proc.new { |lis| lis.sum { |li| li.quantity } } ] - header = ["Customer","Product","Variant","Sum Total"] + + header = ["Customer", "Product", "Variant", "Sum Total"] + + columns = [ proc { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, + proc { |lis| lis.first.variant.product.name }, + proc { |lis| lis.first.variant.options_text }, + proc { |lis| lis.sum { |li| li.quantity } } ] + + rules = [ { group_by: proc { |li| li.variant.product }, + sort_by: proc { |product| product.name } }, + { group_by: proc { |li| li.variant }, + sort_by: proc { |variant| variant.options_text } }, + { group_by: proc { |li| li.order.user }, + sort_by: proc { |user| user.to_s } } ] + when "bulk_coop_customer_payments" - rules = [ { group_by: Proc.new { |li| li.order.user }, sort_by: Proc.new { |user| user.to_s } }, { group_by: Proc.new { |li| li.order }, sort_by: Proc.new { |order| order.created_at } } ] - columns = [ Proc.new { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, Proc.new { |lis| lis.first.order.created_at.to_s }, Proc.new { |lis| lis.map { |li| li.order }.to_set.to_a.sum { |o| o.total } }, Proc.new { |lis| lis.map { |li| li.order }.to_set.to_a.sum { |o| o.outstanding_balance } }, Proc.new { |lis| lis.map { |li| li.order }.to_set.to_a.sum { |o| o.payment_total } } ] - header = ["Customer","Date of Order","Total Cost","Amount Owing","Amount Paid"] + + header = ["Customer", "Date of Order", "Total Cost", "Amount Owing", "Amount Paid"] + + columns = [ proc { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, + proc { |lis| lis.first.order.created_at.to_s }, + proc { |lis| lis.map { |li| li.order }.uniq.sum { |o| o.total } }, + proc { |lis| lis.map { |li| li.order }.uniq.sum { |o| o.outstanding_balance } }, + proc { |lis| lis.map { |li| li.order }.uniq.sum { |o| o.payment_total } } ] + + rules = [ { group_by: proc { |li| li.order.user }, + sort_by: proc { |user| user.to_s } }, + { group_by: proc { |li| li.order }, + sort_by: proc { |order| order.created_at } } ] + else # List all line items - rules = [ { group_by: Proc.new { |li| li.variant.product.supplier }, sort_by: Proc.new { |supplier| supplier.name } }, { group_by: Proc.new { |li| li.variant.product }, sort_by: Proc.new { |product| product.name } }, { group_by: Proc.new { |li| li.variant }, sort_by: Proc.new { |variant| variant.options_text } } ] - columns = [ Proc.new { |lis| lis.first.variant.product.supplier.name }, Proc.new { |lis| lis.first.variant.product.name }, Proc.new { |lis| "UNIT SIZE" }, Proc.new { |lis| lis.first.variant.options_text }, Proc.new { |lis| lis.first.variant.weight }, Proc.new { |lis| lis.sum { |li| li.quantity } }, Proc.new { |lis| lis.sum { |li| li.max_quantity || 0 } } ] - header = ["Supplier","Product","Unit Size","Variant","Weight","Sum Total","Sum Max Total"] + + header = ["Supplier", "Product", "Unit Size", "Variant", "Weight", "Sum Total", "Sum Max Total"] + + columns = [ proc { |lis| lis.first.variant.product.supplier.name }, + proc { |lis| lis.first.variant.product.name }, + proc { |lis| "UNIT SIZE" }, + proc { |lis| lis.first.variant.options_text }, + proc { |lis| lis.first.variant.weight }, + proc { |lis| lis.sum { |li| li.quantity } }, + proc { |lis| lis.sum { |li| li.max_quantity || 0 } } ] + + rules = [ { group_by: proc { |li| li.variant.product.supplier }, + sort_by: proc { |supplier| supplier.name } }, + { group_by: proc { |li| li.variant.product }, + sort_by: proc { |product| product.name } }, + { group_by: proc { |li| li.variant }, + sort_by: proc { |variant| variant.options_text } } ] end order_grouper = OpenFoodWeb::OrderGrouper.new rules, columns @header = header @table = order_grouper.table(line_items) + csv_file_name = "bulk_coop.csv" - unless params[:csv] - render :html => @table - else - csv_string = CSV.generate do |csv| - csv << @header - @table.each { |row| csv << row } - end - send_data csv_string, :filename => "bulk_coop.csv" - end + generate_report(@header, @table, params[:csv], csv_file_name) end def payments @@ -151,54 +210,104 @@ Spree::Admin::ReportsController.class_eval do params[:q][:meta_sort] ||= "created_at.desc" @search = Spree::Order.complete.search(params[:q]) - orders = @search.result.select { |o| o.complete? } # Only select complete orders + orders = @search.result payments = orders.map { |o| o.payments.select { |payment| payment.completed? } }.flatten # Only select completed payments - # Ignore supplier conditions if "All" selected - #if params[:supplier_id] && params[:supplier_id] != "All" - # line_items = line_items.select { |li| li.variant.product.supplier_id == params[:supplier_id] } - #end - - #[Distributor.new(:id => nil, :name => 'All')]+ @distributors = Spree::Distributor.all @report_type = params[:report_type] case params[:report_type] when "payments_by_payment_type" table_items = payments - rules = [ { group_by: Proc.new { |payment| payment.order.payment_state }, sort_by: Proc.new { |payment_state| payment_state } }, { group_by: Proc.new { |payment| payment.order.distributor }, sort_by: Proc.new { |distributor| distributor.name } }, { group_by: Proc.new { |payment| payment.payment_method }, sort_by: Proc.new { |method| method.name } } ] - columns = [ Proc.new { |payments| payments.first.order.payment_state }, Proc.new { |payments| payments.first.order.distributor.name }, Proc.new { |payments| payments.first.payment_method.name }, Proc.new { |payments| payments.sum { |payment| payment.amount } } ] + header = ["Payment State", "Distributor", "Payment Type", "Total ($)"] + + columns = [ proc { |payments| payments.first.order.payment_state }, + proc { |payments| payments.first.order.distributor.name }, + proc { |payments| payments.first.payment_method.name }, + proc { |payments| payments.sum { |payment| payment.amount } } ] + + rules = [ { group_by: proc { |payment| payment.order.payment_state }, + sort_by: proc { |payment_state| payment_state } }, + { group_by: proc { |payment| payment.order.distributor }, + sort_by: proc { |distributor| distributor.name } }, + { group_by: proc { |payment| payment.payment_method }, + sort_by: proc { |method| method.name } } ] + when "itemised_payment_totals" table_items = orders - rules = [ { group_by: Proc.new { |order| order.payment_state }, sort_by: Proc.new { |payment_state| payment_state } }, { group_by: Proc.new { |order| order.distributor }, sort_by: Proc.new { |distributor| distributor.name } } ] - columns = [ Proc.new { |orders| orders.first.payment_state }, Proc.new { |orders| orders.first.distributor.name }, Proc.new { |orders| orders.sum { |o| o.item_total } }, Proc.new { |orders| orders.sum { |o| o.ship_total } }, Proc.new { |orders| orders.sum { |o| o.outstanding_balance } }, Proc.new { |orders| orders.sum { |o| o.total } } ] + header = ["Payment State", "Distributor", "Product Total ($)", "Shipping Total ($)", "Outstanding Balance ($)", "Total ($)"] + + columns = [ proc { |orders| orders.first.payment_state }, + proc { |orders| orders.first.distributor.name }, + proc { |orders| orders.sum { |o| o.item_total } }, + proc { |orders| orders.sum { |o| o.ship_total } }, + proc { |orders| orders.sum { |o| o.outstanding_balance } }, + proc { |orders| orders.sum { |o| o.total } } ] + + rules = [ { group_by: proc { |order| order.payment_state }, + sort_by: proc { |payment_state| payment_state } }, + { group_by: proc { |order| order.distributor }, + sort_by: proc { |distributor| distributor.name } } ] + when "payment_totals" table_items = orders - rules = [ { group_by: Proc.new { |order| order.payment_state }, sort_by: Proc.new { |payment_state| payment_state } }, { group_by: Proc.new { |order| order.distributor }, sort_by: Proc.new { |distributor| distributor.name } } ] - columns = [ Proc.new { |orders| orders.first.payment_state }, Proc.new { |orders| orders.first.distributor.name }, Proc.new { |orders| orders.sum { |o| o.item_total } }, Proc.new { |orders| orders.sum { |o| o.ship_total } }, Proc.new { |orders| orders.sum { |o| o.total } }, Proc.new { |orders| orders.sum { |o| o.payments.select { |payment| payment.completed? && (payment.payment_method.name.to_s.include? "EFT") }.sum { |payment| payment.amount } } }, Proc.new { |orders| orders.sum { |o| o.payments.select { |payment| payment.completed? && (payment.payment_method.name.to_s.include? "PayPal") }.sum{ |payment| payment.amount } } }, Proc.new { |orders| orders.sum { |o| o.outstanding_balance } } ] + header = ["Payment State", "Distributor", "Product Total ($)", "Shipping Total ($)", "Total ($)", "EFT ($)", "PayPal ($)", "Outstanding Balance ($)"] + + columns = [ proc { |orders| orders.first.payment_state }, + proc { |orders| orders.first.distributor.name }, + proc { |orders| orders.sum { |o| o.item_total } }, + proc { |orders| orders.sum { |o| o.ship_total } }, + proc { |orders| orders.sum { |o| o.total } }, + proc { |orders| orders.sum { |o| o.payments.select { |payment| payment.completed? && (payment.payment_method.name.to_s.include? "EFT") }.sum { |payment| payment.amount } } }, + proc { |orders| orders.sum { |o| o.payments.select { |payment| payment.completed? && (payment.payment_method.name.to_s.include? "PayPal") }.sum{ |payment| payment.amount } } }, + proc { |orders| orders.sum { |o| o.outstanding_balance } } ] + + rules = [ { group_by: proc { |order| order.payment_state }, + sort_by: proc { |payment_state| payment_state } }, + { group_by: proc { |order| order.distributor }, + sort_by: proc { |distributor| distributor.name } } ] + else table_items = payments - rules = [ { group_by: Proc.new { |payment| payment.order.payment_state }, sort_by: Proc.new { |payment_state| payment_state } }, { group_by: Proc.new { |payment| payment.order.distributor }, sort_by: Proc.new { |distributor| distributor.name } }, { group_by: Proc.new { |payment| payment.payment_method }, sort_by: Proc.new { |method| method.name } } ] - columns = [ Proc.new { |payments| payments.first.order.payment_state }, Proc.new { |payments| payments.first.order.distributor.name }, Proc.new { |payments| payments.first.payment_method.name }, Proc.new { |payments| payments.sum { |payment| payment.amount } } ] + header = ["Payment State", "Distributor", "Payment Type", "Total ($)"] + + columns = [ proc { |payments| payments.first.order.payment_state }, + proc { |payments| payments.first.order.distributor.name }, + proc { |payments| payments.first.payment_method.name }, + proc { |payments| payments.sum { |payment| payment.amount } } ] + + rules = [ { group_by: proc { |payment| payment.order.payment_state }, + sort_by: proc { |payment_state| payment_state } }, + { group_by: proc { |payment| payment.order.distributor }, + sort_by: proc { |distributor| distributor.name } }, + { group_by: proc { |payment| payment.payment_method }, + sort_by: proc { |method| method.name } } ] + end order_grouper = OpenFoodWeb::OrderGrouper.new rules, columns @header = header @table = order_grouper.table(table_items) + csv_file_name = "payments.csv" + + generate_report(@header, @table, params[:csv], csv_file_name) - unless params[:csv] - render :html => @table + end + + def generate_report (header, table, create_csv, csv_file_name) + unless create_csv + render :html => table else csv_string = CSV.generate do |csv| - csv << @header - @table.each { |row| csv << row } + csv << header + table.each { |row| csv << row } end - send_data csv_string, :filename => "payments.csv" + send_data csv_string, :filename => csv_file_name end end end \ No newline at end of file diff --git a/app/views/spree/admin/reports/bulk_coop.html.haml b/app/views/spree/admin/reports/bulk_coop.html.haml index b1b4824d1a..8c570e8de1 100644 --- a/app/views/spree/admin/reports/bulk_coop.html.haml +++ b/app/views/spree/admin/reports/bulk_coop.html.haml @@ -13,8 +13,6 @@ %br = label_tag nil, "Distributor: " = f.collection_select(:distributor_id_eq, @distributors, :id, :name, :include_blank => 'All') - /= label_tag nil, "Supplier: " - /= collection_select(:supplier, :supplier_id, @suppliers, :id, :name, :prompt => 'All') %br = label_tag nil, "Report Type: " = select_tag(:report_type, options_for_select([['Bulk Co-op - Totals by Supplier',:bulk_coop_supplier_report],['Bulk Co-op - Allocation',:bulk_coop_allocation],['Bulk Co-op - Packing Sheets',:bulk_coop_packing_sheets],['Bulk Co-op - Customer Payments',:bulk_coop_customer_payments]], @report_type)) diff --git a/app/views/spree/admin/reports/payments.html.haml b/app/views/spree/admin/reports/payments.html.haml index 559e456a8f..898bba70ef 100644 --- a/app/views/spree/admin/reports/payments.html.haml +++ b/app/views/spree/admin/reports/payments.html.haml @@ -13,8 +13,6 @@ %br = label_tag nil, "Distributor: " = f.collection_select(:distributor_id_eq, @distributors, :id, :name, :include_blank => 'All') - /= label_tag nil, "Supplier: " - /= collection_select(:supplier, :supplier_id, @suppliers, :id, :name, :prompt => 'All') %br = label_tag nil, "Report Type: " = select_tag(:report_type, options_for_select([['Payments By Type',:payments_by_payment_type],['Itemised Payment Totals',:itemised_payment_totals],['Payment Totals',:payment_totals]], @report_type)) diff --git a/db/schema.rb b/db/schema.rb index e23f9e59a8..09dc89c12e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -140,8 +140,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "abn" t.string "acn" t.string "description" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.integer "pickup_address_id" t.string "next_collection_at" t.text "long_description" @@ -183,8 +183,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "alternative_phone" t.integer "state_id" t.integer "country_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.string "company" end @@ -193,12 +193,12 @@ ActiveRecord::Schema.define(:version => 20121018002907) do create_table "spree_adjustments", :force => true do |t| t.integer "source_id" - t.decimal "amount", :precision => 8, :scale => 2, :default => 0.0 + t.decimal "amount", :precision => 8, :scale => 2 t.string "label" t.string "source_type" t.integer "adjustable_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.boolean "mandatory" t.boolean "locked" t.integer "originator_id" @@ -230,15 +230,15 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "type" t.integer "calculable_id", :null => false t.string "calculable_type", :null => false - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "spree_configurations", :force => true do |t| t.string "name" t.string "type", :limit => 50 - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end add_index "spree_configurations", ["name", "type"], :name => "index_configurations_on_name_and_type" @@ -276,8 +276,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "environment", :default => "development" t.string "server", :default => "test" t.boolean "test_mode", :default => true - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "spree_inventory_units", :force => true do |t| @@ -285,8 +285,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "state" t.integer "variant_id" t.integer "order_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.integer "shipment_id" t.integer "return_authorization_id" end @@ -300,8 +300,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.integer "variant_id" t.integer "quantity", :null => false t.decimal "price", :precision => 8, :scale => 2, :null => false - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.integer "max_quantity" t.integer "shipping_method_id" end @@ -313,22 +313,22 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.integer "source_id" t.string "source_type" t.text "details" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "spree_mail_methods", :force => true do |t| t.string "environment" t.boolean "active", :default => true - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "spree_option_types", :force => true do |t| t.string "name", :limit => 100 t.string "presentation", :limit => 100 - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.integer "position", :default => 0, :null => false end @@ -342,8 +342,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "name" t.string "presentation" t.integer "option_type_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "spree_option_values_variants", :id => false, :force => true do |t| @@ -362,8 +362,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.decimal "adjustment_total", :precision => 8, :scale => 2, :default => 0.0, :null => false t.decimal "credit_total", :precision => 8, :scale => 2, :default => 0.0, :null => false t.integer "user_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.datetime "completed_at" t.integer "bill_address_id" t.integer "ship_address_id" @@ -384,8 +384,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.text "description" t.boolean "active", :default => true t.string "environment", :default => "development" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.datetime "deleted_at" t.string "display_on" end @@ -393,8 +393,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do create_table "spree_payments", :force => true do |t| t.decimal "amount", :precision => 8, :scale => 2, :default => 0.0, :null => false t.integer "order_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.integer "source_id" t.string "source_type" t.integer "payment_method_id" @@ -423,8 +423,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.integer "owner_id" t.string "owner_type" t.text "value" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.string "key" t.string "value_type" end @@ -449,16 +449,16 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.integer "position" t.integer "product_id" t.integer "option_type_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "spree_product_properties", :force => true do |t| t.string "value" t.integer "product_id" t.integer "property_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end add_index "spree_product_properties", ["product_id"], :name => "index_product_properties_on_product_id" @@ -482,8 +482,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "meta_keywords" t.integer "tax_category_id" t.integer "shipping_category_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.integer "count_on_hand", :default => 0, :null => false t.integer "supplier_id" t.boolean "group_buy" @@ -528,8 +528,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.integer "user_id" t.integer "product_group_id" t.string "type" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end add_index "spree_promotion_rules", ["product_group_id"], :name => "index_promotion_rules_on_product_group_id" @@ -546,8 +546,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do create_table "spree_properties", :force => true do |t| t.string "name" t.string "presentation", :null => false - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "spree_properties_prototypes", :id => false, :force => true do |t| @@ -557,8 +557,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do create_table "spree_prototypes", :force => true do |t| t.string "name" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "spree_return_authorizations", :force => true do |t| @@ -567,8 +567,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.decimal "amount", :precision => 8, :scale => 2, :default => 0.0, :null => false t.integer "order_id" t.text "reason" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "spree_roles", :force => true do |t| @@ -591,8 +591,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.integer "order_id" t.integer "shipping_method_id" t.integer "address_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.string "state" end @@ -600,15 +600,15 @@ ActiveRecord::Schema.define(:version => 20121018002907) do create_table "spree_shipping_categories", :force => true do |t| t.string "name" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "spree_shipping_methods", :force => true do |t| t.string "name" t.integer "zone_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.string "display_on" t.integer "shipping_category_id" t.boolean "match_none" @@ -624,8 +624,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.integer "transaction_id" t.integer "customer_id" t.string "payment_type" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "spree_state_changes", :force => true do |t| @@ -633,8 +633,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "previous_state" t.integer "stateful_id" t.integer "user_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.string "stateful_type" t.string "next_state" end @@ -648,8 +648,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do create_table "spree_tax_categories", :force => true do |t| t.string "name" t.string "description" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.boolean "is_default", :default => false t.datetime "deleted_at" end @@ -658,15 +658,15 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.decimal "amount", :precision => 8, :scale => 5 t.integer "zone_id" t.integer "tax_category_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.boolean "included_in_price", :default => false end create_table "spree_taxonomies", :force => true do |t| t.string "name", :null => false - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "spree_taxons", :force => true do |t| @@ -675,8 +675,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "name", :null => false t.string "permalink" t.integer "taxonomy_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.integer "lft" t.integer "rgt" t.string "icon_file_name" @@ -694,8 +694,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.integer "permissable_id" t.string "permissable_type" t.string "token" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end add_index "spree_tokenized_permissions", ["permissable_id", "permissable_type"], :name => "index_tokenized_name_and_type" @@ -704,8 +704,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "environment" t.string "analytics_id" t.boolean "active", :default => true - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "spree_users", :force => true do |t| @@ -726,8 +726,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "login" t.integer "ship_address_id" t.integer "bill_address_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.string "authentication_token" t.string "unlock_token" t.datetime "locked_at" @@ -750,7 +750,7 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.boolean "is_master", :default => false t.integer "product_id" t.integer "count_on_hand", :default => 0, :null => false - t.decimal "cost_price", :precision => 8, :scale => 2, :default => 0.0 + t.decimal "cost_price", :precision => 8, :scale => 2 t.integer "position" end @@ -760,15 +760,15 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.integer "zoneable_id" t.string "zoneable_type" t.integer "zone_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "spree_zones", :force => true do |t| t.string "name" t.string "description" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.boolean "default_tax", :default => false t.integer "zone_members_count", :default => 0 end @@ -779,8 +779,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "email" t.string "twitter" t.string "website" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.integer "address_id" t.text "long_description" end diff --git a/lib/open_food_web/order_grouper.rb b/lib/open_food_web/order_grouper.rb index 43587ae83b..d222a7cc13 100644 --- a/lib/open_food_web/order_grouper.rb +++ b/lib/open_food_web/order_grouper.rb @@ -1,12 +1,12 @@ module OpenFoodWeb class OrderGrouper - def initialize rules, column_constructors + def initialize(rules, column_constructors) @rules = rules @column_constructors = column_constructors end - def build_tree items, remaining_rules + def build_tree(items, remaining_rules) rules = remaining_rules.clone unless rules.empty? rule = rules.delete_at(0) # Remove current rule for subsequent groupings @@ -16,41 +16,43 @@ module OpenFoodWeb end end - def group_and_sort rule, remaining_rules, items + def group_and_sort(rule, remaining_rules, items) branch = {} groups = items.group_by { |item| rule[:group_by].call(item) } sorted_groups = groups.sort_by { |key, value| rule[:sort_by].call(key) } sorted_groups.each do |property,items_by_property| branch[property] = build_tree(items_by_property, remaining_rules) - branch[property][:summary_row] = { items: items_by_property, columns: rule[:summary_columns] } unless rule[:summary_columns] == nil || branch[property].class == Array + branch[property][:summary_row] = { items: items_by_property, columns: rule[:summary_columns] } unless rule[:summary_columns] == nil || is_leaf_node(branch[property]) end branch end - def build_table groups + def build_table(groups) rows = [] - unless groups.class == Array + unless is_leaf_node(groups) groups.each do |key, group| unless key == :summary_row build_table(group).each { |g| rows << g } else - row = Array.new - group[:columns].each { |cols| row << cols.call(group[:items]) } - rows << row + rows << group[:columns].map { |cols| cols.call(group[:items]) } end end else - row = Array.new - @column_constructors.each { |column_constructor| row << column_constructor.call(groups) } - rows << row + rows << @column_constructors.map { |column_constructor| column_constructor.call(groups) } end rows end - def table items + def table(items) tree = build_tree(items,@rules) table = build_table(tree) table end + + private + + def is_leaf_node(node) + node.is_a? Array + end end end \ No newline at end of file diff --git a/spec/lib/open_food_web/order_grouper_spec.rb b/spec/lib/open_food_web/order_grouper_spec.rb index 010fd319e2..3acd0b49ad 100644 --- a/spec/lib/open_food_web/order_grouper_spec.rb +++ b/spec/lib/open_food_web/order_grouper_spec.rb @@ -1,102 +1,3 @@ -#require 'spec_helper' - -#module OpenFoodWeb -# describe OrderGrouper do -# -# before(:each) do -# @orders = [] -# distributor_address = create(:address, :address1 => "distributor address", :city => 'The Shire', :zipcode => "1234") -# distributor = create(:distributor, :pickup_address => distributor_address) -# -# @supplier1 = create(:supplier) -# @variant1 = create(:variant) -# @variant1.product.supplier = @supplier1 -# @variant1.product.save! -# shipping_method = create(:shipping_method) -# product_distribution = create(:product_distribution, :product => @variant1.product, :distributor => distributor, :shipping_method => create(:shipping_method)) -# shipping_instructions = "pick up on thursday please!" -# -# bill_address1 = create(:address) -# order1 = create(:order, :distributor => distributor, :bill_address => bill_address1, :special_instructions => shipping_instructions) -# line_item11 = create(:line_item, :variant => @variant1, :order => order1) -# order1.line_items << line_item11 -# @orders << order1 -# -# bill_address2 = create(:address) -# order2 = create(:order, :distributor => distributor, :bill_address => bill_address2, :special_instructions => shipping_instructions) -# line_item21 = create(:line_item, :variant => @variant1, :order => order2) -# order2.line_items << line_item21 -# -# @variant2 = create(:variant) -# @variant2.product.supplier = @supplier1 -# @variant2.product.save! -# product_distribution = create(:product_distribution, :product => @variant2.product, :distributor => distributor, :shipping_method => create(:shipping_method)) -# -# line_item22 = create(:line_item, :variant => @variant2, :order => order2) -# order2.line_items << line_item22 -# @orders << order2 -# -# @supplier2 = create(:supplier) -# @variant3 = create(:variant) -# @variant3.product.supplier = @supplier2 -# @variant3.product.save! -# product_distribution = create(:product_distribution, :product => @variant3.product, :distributor => distributor, :shipping_method => create(:shipping_method)) - -# bill_address3 = create(:address) -# order3 = create(:order, :distributor => distributor, :bill_address => bill_address3, :special_instructions => shipping_instructions) -# line_item31 = create(:line_item, :variant => @variant3, :order => order3) -# order3.line_items << line_item31 -# @orders << order3 -# end - -# context "when grouping by supplier, then product, then variant" do -# group_rules = [ { group_by: Proc.new { |li| li.variant.product.supplier }, sort_by: Proc.new { |supplier| supplier.name } }, { group_by: Proc.new { |li| li.variant.product }, sort_by: Proc.new { |product| product.name } }, { group_by: Proc.new { |li| li.variant }, sort_by: Proc.new { |variant| variant.options_text } } ] -# column_properties = [ Proc.new { |lis| lis.first.variant.product.supplier.name }, Proc.new { |lis| lis.first.variant.product.name }, Proc.new { |lis| "UNIT SIZE" }, Proc.new { |lis| lis.first.variant.options_text }, Proc.new { |lis| lis.first.variant.weight }, Proc.new { |lis| lis.sum { |li| li.quantity } }, Proc.new { |lis| lis.sum { |li| li.max_quantity || 0 } } ] - -# it "should return a Hash with one key for each supplier represented by the orders" do -# subject = OrderGrouper.new group_rules, column_properties - -# line_items = @orders.map { |o| o.line_items }.flatten - -# groups = subject.grouper_sorter(line_items, group_rules) -# groups.class.should == Hash -# groups.length.should == 2 -# end - -# it "should group items over multiple levels according to group by rules" do -# subject = OrderGrouper.new group_rules, column_properties - -# line_items = @orders.map { |o| o.line_items }.flatten - -# groups = subject.grouper_sorter(line_items, group_rules) -# groups[@supplier1].length.should == 2 -# groups[@supplier2].length.should == 1 -# end - -# it "should return a table as an array" do -# subject = OrderGrouper.new group_rules, column_properties -# -# line_items = @orders.map { |o| o.line_items }.flatten - -# subject.table(line_items).class.should == Array -# end -# end - -# context "when grouping by customers" do -# group_rules = [ { group_by: Proc.new { |li| li.variant.product }, sort_by: Proc.new { |product| product.name } }, { group_by: Proc.new { |li| li.variant }, sort_by: Proc.new { |variant| variant.options_text } }, { group_by: Proc.new { |li| li.order.bill_address }, sort_by: Proc.new { |bill_address| bill_address.firstname + " " + bill_address.lastname } } ] -# column_properties = [ Proc.new { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, Proc.new { |lis| lis.first.variant.product.name }, Proc.new { |lis| "UNIT SIZE" }, Proc.new { |lis| lis.first.variant.options_text }, Proc.new { |lis| lis.first.variant.weight }, Proc.new { |lis| lis.sum { |li| li.quantity } }, Proc.new { |lis| lis.sum { |li| li.max_quantity || 0 } } ] - -# it "should return a table as an array" do -# subject = OrderGrouper.new group_rules, column_properties - -# line_items = @orders.map { |o| o.line_items }.flatten - -# subject.table(line_items).class.should == Array -# end -# end -# end -#end - require 'spec_helper' module OpenFoodWeb @@ -137,7 +38,7 @@ module OpenFoodWeb end context "grouping items with rules" do - it "builds branches by removing a rule from \"rules\" and running group_and_sort" do + it "builds branches by removing a rule from 'rules' and running group_and_sort" do rule1 = double(:rule1) rule2 = double(:rule2) rules = [rule1, rule2] @@ -147,9 +48,6 @@ module OpenFoodWeb subject = OrderGrouper.new rules, columns - #rules = [ { group_by: Proc.new { |sentence| sentence.paragraph.chapter }, sort_by: Proc.new { |chapter| chapter.name }, summary_columns: [Proc.new { |is| is.first.paragraph.chapter.name }, Proc.new { |is| "TOTAL" }, Proc.new { |is| "" }, Proc.new { |is| is.sum {|i| i.property1 } } ] }, - #{ group_by: Proc.new { |sentence| sentence.paragraph }, sort_by: Proc.new { |paragraph| paragraph.name } } ] - #columns = [Proc.new { |is| is.first.paragraph.chapter.name }, Proc.new { |is| is.first.paragraph.name }, Proc.new { |is| is.first.name }, Proc.new { |is| is.sum {|i| i.property1 } }] rules.should_receive(:clone).and_return(rules) rules.should_receive(:delete_at).with(0) grouped_tree = double(:grouped_tree) @@ -205,50 +103,38 @@ module OpenFoodWeb end context "building the table Array" do - it "should return columns when given an Array" do + before(:each) do rule1 = double(:rule1) rule2 = double(:rule2) - rules = [rule1, rule2] - column1 = double(:col1) - column2 = double(:col2) - columns = [column1, column2] - column1.stub(:call) { "Column1" } - column2.stub(:call) { "Column2" } + @rules = [rule1, rule2] + @column1 = double(:col1, :call => "Column1") + @column2 = double(:col2, :call => "Column2") + @columns = [@column1, @column2] + + sumcol1 = double(:sumcol1, :call => "SumColumn1") + sumcol2 = double(:sumcol2, :call => "SumColumn2") + @sumcols = [sumcol1, sumcol2] item1 = double(:item1) item2 = double(:item2) item3 = double(:item3) - items1 = [item1, item2] - items2 = [item2, item3] - groups = { items1: items1, items2: items1 } + @items1 = [item1, item2] + @items2 = [item2, item3] + @items3 = [item3, item1] + end + it "should return columns when given an Array" do + subject = OrderGrouper.new @rules, @columns - subject = OrderGrouper.new rules, columns + @column1.should_receive(:call) + @column2.should_receive(:call) - column1.should_receive(:call) - column2.should_receive(:call) - - subject.build_table(items1).should == [["Column1", "Column2"]] + subject.build_table(@items1).should == [["Column1", "Column2"]] end it "should return a row for each key-value pair when given a Hash" do - rule1 = double(:rule1) - rule2 = double(:rule2) - rules = [rule1, rule2] - column1 = double(:col1) - column2 = double(:col2) - columns = [column1, column2] - column1.stub(:call) { "Column1" } - column2.stub(:call) { "Column2" } + groups = { items1: @items1, items2: @items2, items3: @items3 } - item1 = double(:item1) - item2 = double(:item2) - item3 = double(:item3) - items1 = [item1, item2] - items2 = [item2, item3] - items3 = [item3, item1] - groups = { items1: items1, items2: items2, items3: items3 } - - subject = OrderGrouper.new rules, columns + subject = OrderGrouper.new @rules, @columns #subject.should_receive(:build_table).exactly(2).times @@ -258,30 +144,9 @@ module OpenFoodWeb end it "should return an extra row when a :summary_row key appears in a given Hash" do - rule1 = double(:rule1) - rule2 = double(:rule2) - rules = [rule1, rule2] - column1 = double(:col1) - column2 = double(:col2) - columns = [column1, column2] - column1.stub(:call) { "Column1" } - column2.stub(:call) { "Column2" } + groups = { items1: @items1, items2: @items2, items3: @items3, summary_row: { items: { items2: @items2, items3: @items3 }, columns: @sumcols } } - sumcol1 = double(:sumcol1) - sumcol2 = double(:sumcol2) - sumcols = [sumcol1, sumcol2] - sumcol1.stub(:call) { "SumColumn1" } - sumcol2.stub(:call) { "SumColumn2" } - - item1 = double(:item1) - item2 = double(:item2) - item3 = double(:item3) - items1 = [item1, item2] - items2 = [item2, item3] - items3 = [item3, item1] - groups = { items1: items1, items2: items2, items3: items3, summary_row: { items: { items2: items2, items3: items3 }, columns: sumcols } } - - subject = OrderGrouper.new rules, columns + subject = OrderGrouper.new @rules, @columns expected_return = [] groups.each do |key, group| From 41d547ac2d1e8e34c79c7387c1652c63408c0c9b Mon Sep 17 00:00:00 2001 From: Rob H Date: Wed, 24 Oct 2012 16:13:23 +1100 Subject: [PATCH 04/52] Minor formatting changes to Order Group Spec --- spec/lib/open_food_web/order_grouper_spec.rb | 49 ++++++++------------ 1 file changed, 19 insertions(+), 30 deletions(-) diff --git a/spec/lib/open_food_web/order_grouper_spec.rb b/spec/lib/open_food_web/order_grouper_spec.rb index 3acd0b49ad..d734b78c7b 100644 --- a/spec/lib/open_food_web/order_grouper_spec.rb +++ b/spec/lib/open_food_web/order_grouper_spec.rb @@ -38,53 +38,42 @@ module OpenFoodWeb end context "grouping items with rules" do - it "builds branches by removing a rule from 'rules' and running group_and_sort" do - rule1 = double(:rule1) + + before(:each) do + @rule1 = double(:rule1) rule2 = double(:rule2) - rules = [rule1, rule2] + @rules = [@rule1, rule2] + @remaining_rules = [rule2] column1 = double(:col1) column2 = double(:col2) - columns = [column1, column2] + @columns = [column1, column2] + end + + it "builds branches by removing a rule from 'rules' and running group_and_sort" do + subject = OrderGrouper.new @rules, @columns - subject = OrderGrouper.new rules, columns - - rules.should_receive(:clone).and_return(rules) - rules.should_receive(:delete_at).with(0) + @rules.should_receive(:clone).and_return(@rules) + @rules.should_receive(:delete_at).with(0) grouped_tree = double(:grouped_tree) subject.should_receive(:group_and_sort).and_return(grouped_tree) - subject.build_tree(@items, rules).should == grouped_tree + subject.build_tree(@items, @rules).should == grouped_tree end it "separates the first rule from rules before sending to group_and_sort" do - rule1 = double(:rule1) - rule2 = double(:rule2) - rules = [rule1, rule2] - column1 = double(:col1) - column2 = double(:col2) - columns = [column1, column2] - - subject = OrderGrouper.new rules, columns + subject = OrderGrouper.new @rules, @columns grouped_tree = double(:grouped_tree) - subject.should_receive(:group_and_sort).with(rule1, rules[1..-1], @items).and_return(grouped_tree) + subject.should_receive(:group_and_sort).with(@rule1, @rules[1..-1], @items).and_return(grouped_tree) - subject.build_tree(@items, rules).should == grouped_tree + subject.build_tree(@items, @rules).should == grouped_tree end it "should group, then sort, send each group to build_tree, and return a branch" do - rule1 = double(:rule1) - rule2 = double(:rule2) - rules = [rule1, rule2] - remaining_rules = [rule2] - column1 = double(:col1) - column2 = double(:col2) - columns = [column1, column2] - summary_columns_object = double(:summary_columns) - rule1.stub(:[]).with(:summary_columns) { summary_columns_object } + @rule1.stub(:[]).with(:summary_columns) { summary_columns_object } - subject = OrderGrouper.new rules, columns + subject = OrderGrouper.new @rules, @columns number_of_categories = 3 groups = double(:groups) @@ -98,7 +87,7 @@ module OpenFoodWeb group_tree = {} 1.upto(number_of_categories) { |i| group_tree[i] = group } 1.upto(number_of_categories) { |i| group_tree[i][:summary_row] = summary_columns_object } - subject.group_and_sort(rule1, remaining_rules, @items).should == group_tree + subject.group_and_sort(@rule1, @remaining_rules, @items).should == group_tree end end From a362dd51613830ec0132d0af0155733824316fff Mon Sep 17 00:00:00 2001 From: Rob H Date: Wed, 24 Oct 2012 17:01:50 +1100 Subject: [PATCH 05/52] Change reports controller: generate_report to render_report --- app/controllers/spree/admin/reports_controller_decorator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index 67fe169317..a8cbd23f08 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -299,7 +299,7 @@ Spree::Admin::ReportsController.class_eval do end - def generate_report (header, table, create_csv, csv_file_name) + def render_report (header, table, create_csv, csv_file_name) unless create_csv render :html => table else From 09b60908b8763a9f800c2356a02d2d86eff3e731 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 25 Oct 2012 12:20:20 +1100 Subject: [PATCH 06/52] Fix links to admin distributor edit and delete --- app/views/admin/distributors/index.html.erb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/admin/distributors/index.html.erb b/app/views/admin/distributors/index.html.erb index 6259efa15c..86eef01337 100644 --- a/app/views/admin/distributors/index.html.erb +++ b/app/views/admin/distributors/index.html.erb @@ -25,8 +25,8 @@ <%= distributor_form.text_field :next_collection_at %> <%= distributor.description %> - <%= link_to_edit [main_app, distributor], :class => 'edit' %>   - <%= link_to_delete [main_app, distributor] %> + <%= link_to_edit distributor, :class => 'edit' %>   + <%= link_to_delete distributor %> <% end %> From d74591afe61a9b2a874236d86f3056cdd272364a Mon Sep 17 00:00:00 2001 From: Rob H Date: Fri, 26 Oct 2012 13:44:13 +1100 Subject: [PATCH 07/52] Fixes to reports controller and addition of order cycles reports --- .../admin/reports_controller_decorator.rb | 435 +++++++++++++----- .../admin/reports/order_cycles.html.haml | 39 ++ config/routes.rb | 1 + 3 files changed, 348 insertions(+), 127 deletions(-) create mode 100644 app/views/spree/admin/reports/order_cycles.html.haml diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index a8cbd23f08..110a93dfd9 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -9,6 +9,7 @@ Spree::Admin::ReportsController.class_eval do Spree::Admin::ReportsController::AVAILABLE_REPORTS.merge!({:group_buys => {:name => "Group Buys", :description => "Orders by supplier and variant"}}) Spree::Admin::ReportsController::AVAILABLE_REPORTS.merge!({:bulk_coop => {:name => "Bulk Co-Op", :description => "Reports for Bulk Co-Op orders"}}) Spree::Admin::ReportsController::AVAILABLE_REPORTS.merge!({:payments => {:name => "Payment Reports", :description => "Reports for Payments"}}) + Spree::Admin::ReportsController::AVAILABLE_REPORTS.merge!({:order_cycles => {:name => "Order Cycle Reports", :description => "Reports for Order Cycles"}}) def orders_and_distributors params[:q] = {} unless params[:q] @@ -93,96 +94,98 @@ Spree::Admin::ReportsController.class_eval do case params[:report_type] when "bulk_coop_supplier_report" - + header = ["Supplier", "Product", "Unit Size", "Variant", "Weight", "Sum Total", "Sum Max Total"] - - columns = [ proc { |lis| lis.first.variant.product.supplier.name }, - proc { |lis| lis.first.variant.product.name }, - proc { |lis| "UNIT SIZE" }, - proc { |lis| lis.first.variant.options_text }, - proc { |lis| lis.first.variant.weight }, - proc { |lis| lis.sum { |li| li.quantity } }, + + columns = [ proc { |lis| lis.first.variant.product.supplier.name }, + proc { |lis| lis.first.variant.product.name }, + proc { |lis| "UNIT SIZE" }, + proc { |lis| lis.first.variant.options_text }, + proc { |lis| lis.first.variant.weight }, + proc { |lis| lis.sum { |li| li.quantity } }, proc { |lis| lis.sum { |li| li.max_quantity || 0 } } ] - - rules = [ { group_by: proc { |li| li.variant.product.supplier }, + + rules = [ { group_by: proc { |li| li.variant.product.supplier }, sort_by: proc { |supplier| supplier.name } }, - { group_by: proc { |li| li.variant.product }, - sort_by: proc { |product| product.name }, - summary_columns: [ proc { |lis| lis.first.variant.product.supplier.name }, - proc { |lis| lis.first.variant.product.name }, proc { |lis| "UNIT SIZE" }, - proc { |lis| "" }, proc { |lis| "" }, - proc { |lis| lis.sum { |li| li.quantity * li.variant.weight } }, + { group_by: proc { |li| li.variant.product }, + sort_by: proc { |product| product.name }, + summary_columns: [ proc { |lis| lis.first.variant.product.supplier.name }, + proc { |lis| lis.first.variant.product.name }, + proc { |lis| "UNIT SIZE" }, + proc { |lis| "" }, + proc { |lis| "" }, + proc { |lis| lis.sum { |li| li.quantity * li.variant.weight } }, proc { |lis| lis.sum { |li| (li.max_quantity || 0) * li.variant.weight } } ] }, - { group_by: proc { |li| li.variant }, + { group_by: proc { |li| li.variant }, sort_by: proc { |variant| variant.options_text } } ] - + when "bulk_coop_allocation" - + header = ["Customer", "Product", "Unit Size", "Variant", "Weight", "Sum Total", "Sum Max Total"] - - columns = [ proc { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, - proc { |lis| lis.first.variant.product.name }, - proc { |lis| "UNIT SIZE" }, - proc { |lis| lis.first.variant.options_text }, - proc { |lis| lis.first.variant.weight }, - proc { |lis| lis.sum { |li| li.quantity } }, - proc { |lis| lis.sum { |li| li.max_quantity || 0 } } ] - - rules = [ { group_by: proc { |li| li.variant.product }, - sort_by: proc { |product| product.name } }, - { group_by: proc { |li| li.variant }, - sort_by: proc { |variant| variant.options_text } }, - { group_by: proc { |li| li.order.user }, - sort_by: proc { |user| user.to_s } } ] - - when "bulk_coop_packing_sheets" - - header = ["Customer", "Product", "Variant", "Sum Total"] - - columns = [ proc { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, - proc { |lis| lis.first.variant.product.name }, - proc { |lis| lis.first.variant.options_text }, - proc { |lis| lis.sum { |li| li.quantity } } ] - - rules = [ { group_by: proc { |li| li.variant.product }, - sort_by: proc { |product| product.name } }, - { group_by: proc { |li| li.variant }, - sort_by: proc { |variant| variant.options_text } }, - { group_by: proc { |li| li.order.user }, - sort_by: proc { |user| user.to_s } } ] - - when "bulk_coop_customer_payments" - - header = ["Customer", "Date of Order", "Total Cost", "Amount Owing", "Amount Paid"] - - columns = [ proc { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, - proc { |lis| lis.first.order.created_at.to_s }, - proc { |lis| lis.map { |li| li.order }.uniq.sum { |o| o.total } }, - proc { |lis| lis.map { |li| li.order }.uniq.sum { |o| o.outstanding_balance } }, - proc { |lis| lis.map { |li| li.order }.uniq.sum { |o| o.payment_total } } ] - - rules = [ { group_by: proc { |li| li.order.user }, - sort_by: proc { |user| user.to_s } }, - { group_by: proc { |li| li.order }, - sort_by: proc { |order| order.created_at } } ] - - else # List all line items - - header = ["Supplier", "Product", "Unit Size", "Variant", "Weight", "Sum Total", "Sum Max Total"] - - columns = [ proc { |lis| lis.first.variant.product.supplier.name }, - proc { |lis| lis.first.variant.product.name }, - proc { |lis| "UNIT SIZE" }, - proc { |lis| lis.first.variant.options_text }, - proc { |lis| lis.first.variant.weight }, - proc { |lis| lis.sum { |li| li.quantity } }, + + columns = [ proc { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, + proc { |lis| lis.first.variant.product.name }, + proc { |lis| "UNIT SIZE" }, + proc { |lis| lis.first.variant.options_text }, + proc { |lis| lis.first.variant.weight }, + proc { |lis| lis.sum { |li| li.quantity } }, proc { |lis| lis.sum { |li| li.max_quantity || 0 } } ] - - rules = [ { group_by: proc { |li| li.variant.product.supplier }, - sort_by: proc { |supplier| supplier.name } }, - { group_by: proc { |li| li.variant.product }, - sort_by: proc { |product| product.name } }, - { group_by: proc { |li| li.variant }, + + rules = [ { group_by: proc { |li| li.variant.product }, + sort_by: proc { |product| product.name } }, + { group_by: proc { |li| li.variant }, + sort_by: proc { |variant| variant.options_text } }, + { group_by: proc { |li| li.order.user }, + sort_by: proc { |user| user.to_s } } ] + + when "bulk_coop_packing_sheets" + + header = ["Customer", "Product", "Variant", "Sum Total"] + + columns = [ proc { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, + proc { |lis| lis.first.variant.product.name }, + proc { |lis| lis.first.variant.options_text }, + proc { |lis| lis.sum { |li| li.quantity } } ] + + rules = [ { group_by: proc { |li| li.variant.product }, + sort_by: proc { |product| product.name } }, + { group_by: proc { |li| li.variant }, + sort_by: proc { |variant| variant.options_text } }, + { group_by: proc { |li| li.order.user }, + sort_by: proc { |user| user.to_s } } ] + + when "bulk_coop_customer_payments" + + header = ["Customer", "Date of Order", "Total Cost", "Amount Owing", "Amount Paid"] + + columns = [ proc { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, + proc { |lis| lis.first.order.created_at.to_s }, + proc { |lis| lis.map { |li| li.order }.uniq.sum { |o| o.total } }, + proc { |lis| lis.map { |li| li.order }.uniq.sum { |o| o.outstanding_balance } }, + proc { |lis| lis.map { |li| li.order }.uniq.sum { |o| o.payment_total } } ] + + rules = [ { group_by: proc { |li| li.order.user }, + sort_by: proc { |user| user.to_s } }, + { group_by: proc { |li| li.order }, + sort_by: proc { |order| order.created_at } } ] + + else # List all line items + + header = ["Supplier", "Product", "Unit Size", "Variant", "Weight", "Sum Total", "Sum Max Total"] + + columns = [ proc { |lis| lis.first.variant.product.supplier.name }, + proc { |lis| lis.first.variant.product.name }, + proc { |lis| "UNIT SIZE" }, + proc { |lis| lis.first.variant.options_text }, + proc { |lis| lis.first.variant.weight }, + proc { |lis| lis.sum { |li| li.quantity } }, + proc { |lis| lis.sum { |li| li.max_quantity || 0 } } ] + + rules = [ { group_by: proc { |li| li.variant.product.supplier }, + sort_by: proc { |supplier| supplier.name } }, + { group_by: proc { |li| li.variant.product }, + sort_by: proc { |product| product.name } }, + { group_by: proc { |li| li.variant }, sort_by: proc { |variant| variant.options_text } } ] end @@ -192,7 +195,7 @@ Spree::Admin::ReportsController.class_eval do @table = order_grouper.table(line_items) csv_file_name = "bulk_coop.csv" - generate_report(@header, @table, params[:csv], csv_file_name) + render_report(@header, @table, params[:csv], csv_file_name) end def payments @@ -219,72 +222,72 @@ Spree::Admin::ReportsController.class_eval do case params[:report_type] when "payments_by_payment_type" table_items = payments - + header = ["Payment State", "Distributor", "Payment Type", "Total ($)"] - columns = [ proc { |payments| payments.first.order.payment_state }, - proc { |payments| payments.first.order.distributor.name }, - proc { |payments| payments.first.payment_method.name }, + columns = [ proc { |payments| payments.first.order.payment_state }, + proc { |payments| payments.first.order.distributor.name }, + proc { |payments| payments.first.payment_method.name }, proc { |payments| payments.sum { |payment| payment.amount } } ] - rules = [ { group_by: proc { |payment| payment.order.payment_state }, - sort_by: proc { |payment_state| payment_state } }, - { group_by: proc { |payment| payment.order.distributor }, - sort_by: proc { |distributor| distributor.name } }, - { group_by: proc { |payment| payment.payment_method }, + rules = [ { group_by: proc { |payment| payment.order.payment_state }, + sort_by: proc { |payment_state| payment_state } }, + { group_by: proc { |payment| payment.order.distributor }, + sort_by: proc { |distributor| distributor.name } }, + { group_by: proc { |payment| payment.payment_method }, sort_by: proc { |method| method.name } } ] - + when "itemised_payment_totals" table_items = orders - + header = ["Payment State", "Distributor", "Product Total ($)", "Shipping Total ($)", "Outstanding Balance ($)", "Total ($)"] - - columns = [ proc { |orders| orders.first.payment_state }, - proc { |orders| orders.first.distributor.name }, - proc { |orders| orders.sum { |o| o.item_total } }, - proc { |orders| orders.sum { |o| o.ship_total } }, - proc { |orders| orders.sum { |o| o.outstanding_balance } }, + + columns = [ proc { |orders| orders.first.payment_state }, + proc { |orders| orders.first.distributor.name }, + proc { |orders| orders.sum { |o| o.item_total } }, + proc { |orders| orders.sum { |o| o.ship_total } }, + proc { |orders| orders.sum { |o| o.outstanding_balance } }, proc { |orders| orders.sum { |o| o.total } } ] - - rules = [ { group_by: proc { |order| order.payment_state }, - sort_by: proc { |payment_state| payment_state } }, - { group_by: proc { |order| order.distributor }, + + rules = [ { group_by: proc { |order| order.payment_state }, + sort_by: proc { |payment_state| payment_state } }, + { group_by: proc { |order| order.distributor }, sort_by: proc { |distributor| distributor.name } } ] - + when "payment_totals" table_items = orders header = ["Payment State", "Distributor", "Product Total ($)", "Shipping Total ($)", "Total ($)", "EFT ($)", "PayPal ($)", "Outstanding Balance ($)"] - columns = [ proc { |orders| orders.first.payment_state }, - proc { |orders| orders.first.distributor.name }, - proc { |orders| orders.sum { |o| o.item_total } }, - proc { |orders| orders.sum { |o| o.ship_total } }, - proc { |orders| orders.sum { |o| o.total } }, - proc { |orders| orders.sum { |o| o.payments.select { |payment| payment.completed? && (payment.payment_method.name.to_s.include? "EFT") }.sum { |payment| payment.amount } } }, - proc { |orders| orders.sum { |o| o.payments.select { |payment| payment.completed? && (payment.payment_method.name.to_s.include? "PayPal") }.sum{ |payment| payment.amount } } }, + columns = [ proc { |orders| orders.first.payment_state }, + proc { |orders| orders.first.distributor.name }, + proc { |orders| orders.sum { |o| o.item_total } }, + proc { |orders| orders.sum { |o| o.ship_total } }, + proc { |orders| orders.sum { |o| o.total } }, + proc { |orders| orders.sum { |o| o.payments.select { |payment| payment.completed? && (payment.payment_method.name.to_s.include? "EFT") }.sum { |payment| payment.amount } } }, + proc { |orders| orders.sum { |o| o.payments.select { |payment| payment.completed? && (payment.payment_method.name.to_s.include? "PayPal") }.sum{ |payment| payment.amount } } }, proc { |orders| orders.sum { |o| o.outstanding_balance } } ] - rules = [ { group_by: proc { |order| order.payment_state }, - sort_by: proc { |payment_state| payment_state } }, - { group_by: proc { |order| order.distributor }, + rules = [ { group_by: proc { |order| order.payment_state }, + sort_by: proc { |payment_state| payment_state } }, + { group_by: proc { |order| order.distributor }, sort_by: proc { |distributor| distributor.name } } ] else table_items = payments - + header = ["Payment State", "Distributor", "Payment Type", "Total ($)"] - columns = [ proc { |payments| payments.first.order.payment_state }, - proc { |payments| payments.first.order.distributor.name }, - proc { |payments| payments.first.payment_method.name }, + columns = [ proc { |payments| payments.first.order.payment_state }, + proc { |payments| payments.first.order.distributor.name }, + proc { |payments| payments.first.payment_method.name }, proc { |payments| payments.sum { |payment| payment.amount } } ] - rules = [ { group_by: proc { |payment| payment.order.payment_state }, - sort_by: proc { |payment_state| payment_state } }, - { group_by: proc { |payment| payment.order.distributor }, - sort_by: proc { |distributor| distributor.name } }, - { group_by: proc { |payment| payment.payment_method }, + rules = [ { group_by: proc { |payment| payment.order.payment_state }, + sort_by: proc { |payment_state| payment_state } }, + { group_by: proc { |payment| payment.order.distributor }, + sort_by: proc { |distributor| distributor.name } }, + { group_by: proc { |payment| payment.payment_method }, sort_by: proc { |method| method.name } } ] end @@ -294,11 +297,189 @@ Spree::Admin::ReportsController.class_eval do @header = header @table = order_grouper.table(table_items) csv_file_name = "payments.csv" - - generate_report(@header, @table, params[:csv], csv_file_name) + + render_report(@header, @table, params[:csv], csv_file_name) end - + + def order_cycles + params[:q] = {} unless params[:q] + + if params[:q][:created_at_gt].blank? + params[:q][:created_at_gt] = Time.zone.now.beginning_of_month + else + params[:q][:created_at_gt] = Time.zone.parse(params[:q][:created_at_gt]).beginning_of_day rescue Time.zone.now.beginning_of_month + end + + if params[:q] && !params[:q][:created_at_lt].blank? + params[:q][:created_at_lt] = Time.zone.parse(params[:q][:created_at_lt]).end_of_day rescue "" + end + params[:q][:meta_sort] ||= "created_at.desc" + + @search = Spree::Order.complete.search(params[:q]) + orders = @search.result + line_items = orders.map { |o| o.line_items }.flatten + #payments = orders.map { |o| o.payments.select { |payment| payment.completed? } }.flatten # Only select completed payments + + @distributors = Spree::Distributor.all + @report_type = params[:report_type] + + case params[:report_type] + when "order_cycle_supplier_totals" + table_items = line_items + @include_blank = 'All' + + header = ["Supplier", "Product", "Variant", "Amount", "Cost per Unit", "Total Cost", "Status", "Incoming Transport"] + + columns = [ proc { |line_items| line_items.first.variant.product.supplier.name }, + proc { |line_items| line_items.first.variant.product.name }, + proc { |line_items| line_items.first.variant.options_text }, + proc { |line_items| line_items.sum { |li| li.quantity } }, + proc { |line_items| line_items.first.variant.price }, + proc { |line_items| line_items.sum { |li| li.quantity * li.variant.price } }, + proc { |line_items| "status" }, + proc { |line_items| "incoming transport" } ] + + rules = [ { group_by: proc { |line_item| line_item.variant.product.supplier }, + sort_by: proc { |supplier| supplier.name } }, + { group_by: proc { |line_item| line_item.variant.product }, + sort_by: proc { |product| product.name } }, + { group_by: proc { |line_item| line_item.variant }, + sort_by: proc { |variant| variant.options_text } } ] + + when "order_cycle_supplier_totals_by_distributor" + table_items = line_items + @include_blank = 'All' + + header = ["Supplier", "Product", "Variant", "To Distributor", "Amount", "Cost per Unit", "Total Cost", "Shipping Method"] + + columns = [ proc { |line_items| line_items.first.variant.product.supplier.name }, + proc { |line_items| line_items.first.variant.product.name }, + proc { |line_items| line_items.first.variant.options_text }, + proc { |line_items| line_items.first.order.distributor.name }, + proc { |line_items| line_items.sum { |li| li.quantity } }, + proc { |line_items| line_items.first.variant.price }, + proc { |line_items| line_items.sum { |li| li.quantity * li.variant.price } }, + proc { |line_items| "shipping method" } ] + + rules = [ { group_by: proc { |line_item| line_item.variant.product.supplier }, + sort_by: proc { |supplier| supplier.name } }, + { group_by: proc { |line_item| line_item.variant.product }, + sort_by: proc { |product| product.name } }, + { group_by: proc { |line_item| line_item.variant }, + sort_by: proc { |variant| variant.options_text }, + summary_columns: [ proc { |line_items| line_items.first.variant.product.supplier.name }, + proc { |line_items| line_items.first.variant.product.name }, + proc { |line_items| line_items.first.variant.options_text }, + proc { |line_items| "TOTAL" }, + proc { |line_items| line_items.sum { |li| li.quantity } }, + proc { |line_items| line_items.first.variant.price }, + proc { |line_items| line_items.sum { |li| li.quantity * li.variant.price } }, + proc { |line_items| "" } ] }, + { group_by: proc { |line_item| line_item.order.distributor }, + sort_by: proc { |distributor| distributor.name } } ] + + when "order_cycle_distributor_totals_by_supplier" + table_items = line_items + @include_blank = 'All' + + header = ["Distributor", "Supplier", "Product", "Variant", "Amount", "Cost per Unit", "Total Cost", "Total Shipping Cost", "Shipping Method"] + + columns = [ proc { |line_items| line_items.first.order.distributor.name }, + proc { |line_items| line_items.first.variant.product.supplier.name }, + proc { |line_items| line_items.first.variant.product.name }, + proc { |line_items| line_items.first.variant.options_text }, + proc { |line_items| line_items.sum { |li| li.quantity } }, + proc { |line_items| line_items.first.variant.price }, + proc { |line_items| line_items.sum { |li| li.quantity * li.variant.price } }, + proc { |line_items| "" }, + proc { |line_items| "shipping method" } ] + + rules = [ { group_by: proc { |line_item| line_item.order.distributor }, + sort_by: proc { |distributor| distributor.name }, + summary_columns: [ proc { |line_items| line_items.first.order.distributor.name }, + proc { |line_items| "TOTAL" }, + proc { |line_items| "" }, + proc { |line_items| "" }, + proc { |line_items| "" }, + proc { |line_items| "" }, + proc { |line_items| line_items.sum { |li| li.quantity * li.variant.price } }, + proc { |line_items| "total shipping cost" }, + proc { |line_items| "" } ] }, + { group_by: proc { |line_item| line_item.variant.product.supplier }, + sort_by: proc { |supplier| supplier.name } }, + { group_by: proc { |line_item| line_item.variant.product }, + sort_by: proc { |product| product.name } }, + { group_by: proc { |line_item| line_item.variant }, + sort_by: proc { |variant| variant.options_text } } ] + + when "order_cycle_customer_totals" + table_items = line_items + @include_blank = false + + header = ["Customer", "Email", "Phone", "Product", "Variant", "Amount", "Total Cost", "Paid?", "Packed?", "Shipped?"] + + columns = [ proc { |line_items| line_items.first.order.bill_address.firstname + " " + line_items.first.order.bill_address.lastname }, + proc { |line_items| line_items.first.order.email }, + proc { |line_items| line_items.first.order.bill_address.phone }, + proc { |line_items| line_items.first.variant.product.name }, + proc { |line_items| line_items.first.variant.options_text }, + proc { |line_items| line_items.sum { |li| li.quantity } }, + proc { |line_items| line_items.sum { |li| li.quantity * li.variant.price } }, + proc { |line_items| "" }, + proc { |line_items| "" }, + proc { |line_items| "" } ] + + rules = [ { group_by: proc { |line_item| line_item.order.user }, + sort_by: proc { |user| user.to_s }, + summary_columns: [ proc { |line_items| line_items.first.order.bill_address.firstname + " " + line_items.first.order.bill_address.lastname }, + proc { |line_items| line_items.first.order.email }, + proc { |line_items| line_items.first.order.bill_address.phone }, + proc { |line_items| "TOTAL" }, + proc { |line_items| "" }, + proc { |line_items| "" }, + proc { |line_items| line_items.sum { |li| li.quantity * li.variant.price } }, + proc { |line_items| "work out whether paid or not" }, + proc { |line_items| "" }, + proc { |line_items| "" } ] }, + { group_by: proc { |line_item| line_item.variant.product }, + sort_by: proc { |product| product.name } }, + { group_by: proc { |line_item| line_item.variant }, + sort_by: proc { |variant| variant.options_text } } ] + + else + table_items = line_items + + header = ["Supplier", "Product", "Variant", "Amount", "Cost per Unit", "Total Cost", "Status", "Incoming Transport"] + + columns = [ proc { |line_items| line_items.first.variant.product.supplier.name }, + proc { |line_items| line_items.first.variant.product.name }, + proc { |line_items| line_items.first.variant.options_text }, + proc { |line_items| line_items.sum { |li| li.quantity } }, + proc { |line_items| line_items.first.variant.price }, + proc { |line_items| line_items.sum { |li| li.quantity * li.variant.price } }, + proc { |line_items| "status" }, + proc { |line_items| "incoming transport" } ] + + rules = [ { group_by: proc { |line_item| line_item.variant.product.supplier }, + sort_by: proc { |supplier| supplier.name } }, + { group_by: proc { |line_item| line_item.variant.product }, + sort_by: proc { |product| product.name } }, + { group_by: proc { |line_item| line_item.variant }, + sort_by: proc { |variant| variant.options_text } } ] + + end + + order_grouper = OpenFoodWeb::OrderGrouper.new rules, columns + + @header = header + @table = order_grouper.table(table_items) + csv_file_name = "order_cycles.csv" + + render_report(@header, @table, params[:csv], csv_file_name) + + end + def render_report (header, table, create_csv, csv_file_name) unless create_csv render :html => table diff --git a/app/views/spree/admin/reports/order_cycles.html.haml b/app/views/spree/admin/reports/order_cycles.html.haml new file mode 100644 index 0000000000..33bd97cb06 --- /dev/null +++ b/app/views/spree/admin/reports/order_cycles.html.haml @@ -0,0 +1,39 @@ += form_for @search, :url => spree.order_cycles_admin_reports_path do |f| + = label_tag nil, t(:date_range) + %br + .date-range-filter + %div{"class" => "left sub-field"} + = f.text_field :created_at_gt, :class => 'datepicker' + %br + = label_tag nil, t(:start), :class => 'sub' + %div{"class" => "right sub-field"} + = f.text_field :created_at_lt, :class => 'datepicker' + %br + = label_tag nil, t(:stop) + %br + = label_tag nil, "Distributor: " + = f.collection_select(:distributor_id_eq, @distributors, :id, :name, :include_blank => @include_blank) + %br + = label_tag nil, "Report Type: " + = select_tag(:report_type, options_for_select([['Order Cycle Supplier Totals',:order_cycle_supplier_totals], ['Order Cycle Supplier Totals by Distributor',:order_cycle_supplier_totals_by_distributor], ['Order Cycle Distributor Totals by Supplier',:order_cycle_distributor_totals_by_supplier], ['Order Cycle Customer Totals',:order_cycle_customer_totals]], @report_type)) + %br + %br + = check_box_tag :csv + = label_tag :csv, "Download as csv" + %br + = button t(:search) +%br +%br +%table#listing_orders.index + %thead + %tr{'data-hook' => "orders_header"} + - @header.each do |heading| + %th=heading + %tbody + - @table.each do |row| + %tr + - row.each do |column| + %td= column + - if @table.empty? + %tr + %td{:colspan => "2"}= t(:none) \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 7fde8d7b66..354e2d0dbd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -22,5 +22,6 @@ Spree::Core::Engine.routes.prepend do 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] + match '/admin/reports/order_cycles' => 'admin/reports#order_cycles', :as => "order_cycles_admin_reports", :via => [:get, :post] end From 4dd5e129680e2115c12fe20bc4c8b04b9c2ae72f Mon Sep 17 00:00:00 2001 From: Rob H Date: Fri, 26 Oct 2012 13:49:39 +1100 Subject: [PATCH 08/52] Fix reports controller, previous changes to fix change generate_report were incomplete --- .../admin/reports_controller_decorator.rb | 254 +++++++++--------- 1 file changed, 127 insertions(+), 127 deletions(-) diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index a8cbd23f08..d5bb667845 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -93,96 +93,96 @@ Spree::Admin::ReportsController.class_eval do case params[:report_type] when "bulk_coop_supplier_report" - + header = ["Supplier", "Product", "Unit Size", "Variant", "Weight", "Sum Total", "Sum Max Total"] - - columns = [ proc { |lis| lis.first.variant.product.supplier.name }, - proc { |lis| lis.first.variant.product.name }, - proc { |lis| "UNIT SIZE" }, - proc { |lis| lis.first.variant.options_text }, - proc { |lis| lis.first.variant.weight }, - proc { |lis| lis.sum { |li| li.quantity } }, + + columns = [ proc { |lis| lis.first.variant.product.supplier.name }, + proc { |lis| lis.first.variant.product.name }, + proc { |lis| "UNIT SIZE" }, + proc { |lis| lis.first.variant.options_text }, + proc { |lis| lis.first.variant.weight }, + proc { |lis| lis.sum { |li| li.quantity } }, proc { |lis| lis.sum { |li| li.max_quantity || 0 } } ] - - rules = [ { group_by: proc { |li| li.variant.product.supplier }, + + rules = [ { group_by: proc { |li| li.variant.product.supplier }, sort_by: proc { |supplier| supplier.name } }, - { group_by: proc { |li| li.variant.product }, - sort_by: proc { |product| product.name }, - summary_columns: [ proc { |lis| lis.first.variant.product.supplier.name }, - proc { |lis| lis.first.variant.product.name }, proc { |lis| "UNIT SIZE" }, - proc { |lis| "" }, proc { |lis| "" }, - proc { |lis| lis.sum { |li| li.quantity * li.variant.weight } }, + { group_by: proc { |li| li.variant.product }, + sort_by: proc { |product| product.name }, + summary_columns: [ proc { |lis| lis.first.variant.product.supplier.name }, + proc { |lis| lis.first.variant.product.name }, proc { |lis| "UNIT SIZE" }, + proc { |lis| "" }, proc { |lis| "" }, + proc { |lis| lis.sum { |li| li.quantity * li.variant.weight } }, proc { |lis| lis.sum { |li| (li.max_quantity || 0) * li.variant.weight } } ] }, - { group_by: proc { |li| li.variant }, + { group_by: proc { |li| li.variant }, sort_by: proc { |variant| variant.options_text } } ] - + when "bulk_coop_allocation" - + header = ["Customer", "Product", "Unit Size", "Variant", "Weight", "Sum Total", "Sum Max Total"] - - columns = [ proc { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, - proc { |lis| lis.first.variant.product.name }, - proc { |lis| "UNIT SIZE" }, - proc { |lis| lis.first.variant.options_text }, - proc { |lis| lis.first.variant.weight }, - proc { |lis| lis.sum { |li| li.quantity } }, - proc { |lis| lis.sum { |li| li.max_quantity || 0 } } ] - - rules = [ { group_by: proc { |li| li.variant.product }, - sort_by: proc { |product| product.name } }, - { group_by: proc { |li| li.variant }, - sort_by: proc { |variant| variant.options_text } }, - { group_by: proc { |li| li.order.user }, - sort_by: proc { |user| user.to_s } } ] - - when "bulk_coop_packing_sheets" - - header = ["Customer", "Product", "Variant", "Sum Total"] - - columns = [ proc { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, - proc { |lis| lis.first.variant.product.name }, - proc { |lis| lis.first.variant.options_text }, - proc { |lis| lis.sum { |li| li.quantity } } ] - - rules = [ { group_by: proc { |li| li.variant.product }, - sort_by: proc { |product| product.name } }, - { group_by: proc { |li| li.variant }, - sort_by: proc { |variant| variant.options_text } }, - { group_by: proc { |li| li.order.user }, - sort_by: proc { |user| user.to_s } } ] - - when "bulk_coop_customer_payments" - - header = ["Customer", "Date of Order", "Total Cost", "Amount Owing", "Amount Paid"] - - columns = [ proc { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, - proc { |lis| lis.first.order.created_at.to_s }, - proc { |lis| lis.map { |li| li.order }.uniq.sum { |o| o.total } }, - proc { |lis| lis.map { |li| li.order }.uniq.sum { |o| o.outstanding_balance } }, - proc { |lis| lis.map { |li| li.order }.uniq.sum { |o| o.payment_total } } ] - - rules = [ { group_by: proc { |li| li.order.user }, - sort_by: proc { |user| user.to_s } }, - { group_by: proc { |li| li.order }, - sort_by: proc { |order| order.created_at } } ] - - else # List all line items - - header = ["Supplier", "Product", "Unit Size", "Variant", "Weight", "Sum Total", "Sum Max Total"] - - columns = [ proc { |lis| lis.first.variant.product.supplier.name }, - proc { |lis| lis.first.variant.product.name }, - proc { |lis| "UNIT SIZE" }, - proc { |lis| lis.first.variant.options_text }, - proc { |lis| lis.first.variant.weight }, - proc { |lis| lis.sum { |li| li.quantity } }, + + columns = [ proc { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, + proc { |lis| lis.first.variant.product.name }, + proc { |lis| "UNIT SIZE" }, + proc { |lis| lis.first.variant.options_text }, + proc { |lis| lis.first.variant.weight }, + proc { |lis| lis.sum { |li| li.quantity } }, proc { |lis| lis.sum { |li| li.max_quantity || 0 } } ] - - rules = [ { group_by: proc { |li| li.variant.product.supplier }, - sort_by: proc { |supplier| supplier.name } }, - { group_by: proc { |li| li.variant.product }, - sort_by: proc { |product| product.name } }, - { group_by: proc { |li| li.variant }, + + rules = [ { group_by: proc { |li| li.variant.product }, + sort_by: proc { |product| product.name } }, + { group_by: proc { |li| li.variant }, + sort_by: proc { |variant| variant.options_text } }, + { group_by: proc { |li| li.order.user }, + sort_by: proc { |user| user.to_s } } ] + + when "bulk_coop_packing_sheets" + + header = ["Customer", "Product", "Variant", "Sum Total"] + + columns = [ proc { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, + proc { |lis| lis.first.variant.product.name }, + proc { |lis| lis.first.variant.options_text }, + proc { |lis| lis.sum { |li| li.quantity } } ] + + rules = [ { group_by: proc { |li| li.variant.product }, + sort_by: proc { |product| product.name } }, + { group_by: proc { |li| li.variant }, + sort_by: proc { |variant| variant.options_text } }, + { group_by: proc { |li| li.order.user }, + sort_by: proc { |user| user.to_s } } ] + + when "bulk_coop_customer_payments" + + header = ["Customer", "Date of Order", "Total Cost", "Amount Owing", "Amount Paid"] + + columns = [ proc { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, + proc { |lis| lis.first.order.created_at.to_s }, + proc { |lis| lis.map { |li| li.order }.uniq.sum { |o| o.total } }, + proc { |lis| lis.map { |li| li.order }.uniq.sum { |o| o.outstanding_balance } }, + proc { |lis| lis.map { |li| li.order }.uniq.sum { |o| o.payment_total } } ] + + rules = [ { group_by: proc { |li| li.order.user }, + sort_by: proc { |user| user.to_s } }, + { group_by: proc { |li| li.order }, + sort_by: proc { |order| order.created_at } } ] + + else # List all line items + + header = ["Supplier", "Product", "Unit Size", "Variant", "Weight", "Sum Total", "Sum Max Total"] + + columns = [ proc { |lis| lis.first.variant.product.supplier.name }, + proc { |lis| lis.first.variant.product.name }, + proc { |lis| "UNIT SIZE" }, + proc { |lis| lis.first.variant.options_text }, + proc { |lis| lis.first.variant.weight }, + proc { |lis| lis.sum { |li| li.quantity } }, + proc { |lis| lis.sum { |li| li.max_quantity || 0 } } ] + + rules = [ { group_by: proc { |li| li.variant.product.supplier }, + sort_by: proc { |supplier| supplier.name } }, + { group_by: proc { |li| li.variant.product }, + sort_by: proc { |product| product.name } }, + { group_by: proc { |li| li.variant }, sort_by: proc { |variant| variant.options_text } } ] end @@ -192,7 +192,7 @@ Spree::Admin::ReportsController.class_eval do @table = order_grouper.table(line_items) csv_file_name = "bulk_coop.csv" - generate_report(@header, @table, params[:csv], csv_file_name) + render_report(@header, @table, params[:csv], csv_file_name) end def payments @@ -219,72 +219,72 @@ Spree::Admin::ReportsController.class_eval do case params[:report_type] when "payments_by_payment_type" table_items = payments - + header = ["Payment State", "Distributor", "Payment Type", "Total ($)"] - columns = [ proc { |payments| payments.first.order.payment_state }, - proc { |payments| payments.first.order.distributor.name }, - proc { |payments| payments.first.payment_method.name }, + columns = [ proc { |payments| payments.first.order.payment_state }, + proc { |payments| payments.first.order.distributor.name }, + proc { |payments| payments.first.payment_method.name }, proc { |payments| payments.sum { |payment| payment.amount } } ] - rules = [ { group_by: proc { |payment| payment.order.payment_state }, - sort_by: proc { |payment_state| payment_state } }, - { group_by: proc { |payment| payment.order.distributor }, - sort_by: proc { |distributor| distributor.name } }, - { group_by: proc { |payment| payment.payment_method }, + rules = [ { group_by: proc { |payment| payment.order.payment_state }, + sort_by: proc { |payment_state| payment_state } }, + { group_by: proc { |payment| payment.order.distributor }, + sort_by: proc { |distributor| distributor.name } }, + { group_by: proc { |payment| payment.payment_method }, sort_by: proc { |method| method.name } } ] - + when "itemised_payment_totals" table_items = orders - + header = ["Payment State", "Distributor", "Product Total ($)", "Shipping Total ($)", "Outstanding Balance ($)", "Total ($)"] - - columns = [ proc { |orders| orders.first.payment_state }, - proc { |orders| orders.first.distributor.name }, - proc { |orders| orders.sum { |o| o.item_total } }, - proc { |orders| orders.sum { |o| o.ship_total } }, - proc { |orders| orders.sum { |o| o.outstanding_balance } }, + + columns = [ proc { |orders| orders.first.payment_state }, + proc { |orders| orders.first.distributor.name }, + proc { |orders| orders.sum { |o| o.item_total } }, + proc { |orders| orders.sum { |o| o.ship_total } }, + proc { |orders| orders.sum { |o| o.outstanding_balance } }, proc { |orders| orders.sum { |o| o.total } } ] - - rules = [ { group_by: proc { |order| order.payment_state }, - sort_by: proc { |payment_state| payment_state } }, - { group_by: proc { |order| order.distributor }, + + rules = [ { group_by: proc { |order| order.payment_state }, + sort_by: proc { |payment_state| payment_state } }, + { group_by: proc { |order| order.distributor }, sort_by: proc { |distributor| distributor.name } } ] - + when "payment_totals" table_items = orders header = ["Payment State", "Distributor", "Product Total ($)", "Shipping Total ($)", "Total ($)", "EFT ($)", "PayPal ($)", "Outstanding Balance ($)"] - columns = [ proc { |orders| orders.first.payment_state }, - proc { |orders| orders.first.distributor.name }, - proc { |orders| orders.sum { |o| o.item_total } }, - proc { |orders| orders.sum { |o| o.ship_total } }, - proc { |orders| orders.sum { |o| o.total } }, - proc { |orders| orders.sum { |o| o.payments.select { |payment| payment.completed? && (payment.payment_method.name.to_s.include? "EFT") }.sum { |payment| payment.amount } } }, - proc { |orders| orders.sum { |o| o.payments.select { |payment| payment.completed? && (payment.payment_method.name.to_s.include? "PayPal") }.sum{ |payment| payment.amount } } }, + columns = [ proc { |orders| orders.first.payment_state }, + proc { |orders| orders.first.distributor.name }, + proc { |orders| orders.sum { |o| o.item_total } }, + proc { |orders| orders.sum { |o| o.ship_total } }, + proc { |orders| orders.sum { |o| o.total } }, + proc { |orders| orders.sum { |o| o.payments.select { |payment| payment.completed? && (payment.payment_method.name.to_s.include? "EFT") }.sum { |payment| payment.amount } } }, + proc { |orders| orders.sum { |o| o.payments.select { |payment| payment.completed? && (payment.payment_method.name.to_s.include? "PayPal") }.sum{ |payment| payment.amount } } }, proc { |orders| orders.sum { |o| o.outstanding_balance } } ] - rules = [ { group_by: proc { |order| order.payment_state }, - sort_by: proc { |payment_state| payment_state } }, - { group_by: proc { |order| order.distributor }, + rules = [ { group_by: proc { |order| order.payment_state }, + sort_by: proc { |payment_state| payment_state } }, + { group_by: proc { |order| order.distributor }, sort_by: proc { |distributor| distributor.name } } ] else table_items = payments - + header = ["Payment State", "Distributor", "Payment Type", "Total ($)"] - columns = [ proc { |payments| payments.first.order.payment_state }, - proc { |payments| payments.first.order.distributor.name }, - proc { |payments| payments.first.payment_method.name }, + columns = [ proc { |payments| payments.first.order.payment_state }, + proc { |payments| payments.first.order.distributor.name }, + proc { |payments| payments.first.payment_method.name }, proc { |payments| payments.sum { |payment| payment.amount } } ] - rules = [ { group_by: proc { |payment| payment.order.payment_state }, - sort_by: proc { |payment_state| payment_state } }, - { group_by: proc { |payment| payment.order.distributor }, - sort_by: proc { |distributor| distributor.name } }, - { group_by: proc { |payment| payment.payment_method }, + rules = [ { group_by: proc { |payment| payment.order.payment_state }, + sort_by: proc { |payment_state| payment_state } }, + { group_by: proc { |payment| payment.order.distributor }, + sort_by: proc { |distributor| distributor.name } }, + { group_by: proc { |payment| payment.payment_method }, sort_by: proc { |method| method.name } } ] end @@ -294,11 +294,11 @@ Spree::Admin::ReportsController.class_eval do @header = header @table = order_grouper.table(table_items) csv_file_name = "payments.csv" - - generate_report(@header, @table, params[:csv], csv_file_name) + + render_report(@header, @table, params[:csv], csv_file_name) end - + def render_report (header, table, create_csv, csv_file_name) unless create_csv render :html => table From f960cc44ba6c2cc5b20aa54b354d79f7508ccb9b Mon Sep 17 00:00:00 2001 From: Rob H Date: Fri, 26 Oct 2012 17:27:08 +1100 Subject: [PATCH 09/52] Fixed calculations when variant weight is nil --- .../spree/admin/reports_controller_decorator.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index 60ed1a4e03..58343bf4eb 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -100,7 +100,7 @@ Spree::Admin::ReportsController.class_eval do proc { |lis| lis.first.variant.product.name }, proc { |lis| "UNIT SIZE" }, proc { |lis| lis.first.variant.options_text }, - proc { |lis| lis.first.variant.weight }, + proc { |lis| lis.first.variant.weight || 0 }, proc { |lis| lis.sum { |li| li.quantity } }, proc { |lis| lis.sum { |li| li.max_quantity || 0 } } ] @@ -111,8 +111,8 @@ Spree::Admin::ReportsController.class_eval do summary_columns: [ proc { |lis| lis.first.variant.product.supplier.name }, proc { |lis| lis.first.variant.product.name }, proc { |lis| "UNIT SIZE" }, proc { |lis| "" }, proc { |lis| "" }, - proc { |lis| lis.sum { |li| li.quantity * li.variant.weight } }, - proc { |lis| lis.sum { |li| (li.max_quantity || 0) * li.variant.weight } } ] }, + proc { |lis| lis.sum { |li| li.quantity * li.variant.weight || 0 } }, + proc { |lis| lis.sum { |li| (li.max_quantity || 0) * li.variant.weight || 0 } } ] }, { group_by: proc { |li| li.variant }, sort_by: proc { |variant| variant.options_text } } ] @@ -124,7 +124,7 @@ Spree::Admin::ReportsController.class_eval do proc { |lis| lis.first.variant.product.name }, proc { |lis| "UNIT SIZE" }, proc { |lis| lis.first.variant.options_text }, - proc { |lis| lis.first.variant.weight }, + proc { |lis| lis.first.variant.weight || 0 }, proc { |lis| lis.sum { |li| li.quantity } }, proc { |lis| lis.sum { |li| li.max_quantity || 0 } } ] @@ -174,7 +174,7 @@ Spree::Admin::ReportsController.class_eval do proc { |lis| lis.first.variant.product.name }, proc { |lis| "UNIT SIZE" }, proc { |lis| lis.first.variant.options_text }, - proc { |lis| lis.first.variant.weight }, + proc { |lis| lis.first.variant.weight || 0 }, proc { |lis| lis.sum { |li| li.quantity } }, proc { |lis| lis.sum { |li| li.max_quantity || 0 } } ] From 13317347bb8db1f9e5ef715cab087098d253e6a3 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Sat, 27 Oct 2012 12:19:44 +1100 Subject: [PATCH 10/52] Display an explanation message on product page when no distributor is selected --- .../stylesheets/store/openfoodweb.css.scss | 10 ++++++++++ .../add_distributor_details_to_product.rb | 4 ++++ app/views/distributors/_details.html.haml | 2 ++ spec/requests/consumer/product_spec.rb | 19 +++++++++++++++++++ 4 files changed, 35 insertions(+) create mode 100644 app/overrides/add_distributor_details_to_product.rb create mode 100644 app/views/distributors/_details.html.haml diff --git a/app/assets/stylesheets/store/openfoodweb.css.scss b/app/assets/stylesheets/store/openfoodweb.css.scss index a3cb0a29f1..06979bf3ea 100644 --- a/app/assets/stylesheets/store/openfoodweb.css.scss +++ b/app/assets/stylesheets/store/openfoodweb.css.scss @@ -144,6 +144,16 @@ ul.product-listing { } +/* Distributor details on product details page */ +#product-distributor-details { + float: right; +} + +#product-variants { + float: none; +} + + /* Add to cart form on product details page */ #cart-form { .error-distributor { diff --git a/app/overrides/add_distributor_details_to_product.rb b/app/overrides/add_distributor_details_to_product.rb new file mode 100644 index 0000000000..b107869e19 --- /dev/null +++ b/app/overrides/add_distributor_details_to_product.rb @@ -0,0 +1,4 @@ +Deface::Override.new(:virtual_path => "spree/products/show", + :insert_before => "[data-hook='cart_form']", + :partial => "distributors/details", + :name => "product_distributor_details") diff --git a/app/views/distributors/_details.html.haml b/app/views/distributors/_details.html.haml new file mode 100644 index 0000000000..6646588ef4 --- /dev/null +++ b/app/views/distributors/_details.html.haml @@ -0,0 +1,2 @@ +#product-distributor-details.columns.five.omega + When you select a distributor for your order, their address and pickup times will be displayed here. diff --git a/spec/requests/consumer/product_spec.rb b/spec/requests/consumer/product_spec.rb index 97712d1e13..368452ab54 100644 --- a/spec/requests/consumer/product_spec.rb +++ b/spec/requests/consumer/product_spec.rb @@ -22,4 +22,23 @@ feature %q{ page.should have_selector 'td', :text => d.name end + describe "viewing distributor details" do + context "without Javascript" do + it "displays a holding message when no distributor is selected" do + p = create(:product) + + visit spree.product_path p + + page.should have_selector '#product-distributor-details', :text => 'When you select a distributor for your order, their address and pickup times will be displayed here.' + end + + it "displays distributor details when one is selected" + end + + context "with Javascript" do + it "changes distributor details when the distributor is changed" + end + end + + end From 773adca838931c945c3db35c1130bdbd1aa1e94e Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Sat, 27 Oct 2012 12:49:57 +1100 Subject: [PATCH 11/52] Display distributor details on product page when distributor is selected --- .../stylesheets/store/openfoodweb.css.scss | 5 +++- .../add_distributor_details_to_product.rb | 2 +- app/views/distributors/_details.html.haml | 25 +++++++++++++++-- .../spree/checkout/_distributor.html.haml | 25 +---------------- .../products/_distributor_details.html.haml | 7 +++++ spec/requests/consumer/product_spec.rb | 28 ++++++++++++++++++- 6 files changed, 63 insertions(+), 29 deletions(-) create mode 100644 app/views/spree/products/_distributor_details.html.haml diff --git a/app/assets/stylesheets/store/openfoodweb.css.scss b/app/assets/stylesheets/store/openfoodweb.css.scss index 06979bf3ea..fa4d16689f 100644 --- a/app/assets/stylesheets/store/openfoodweb.css.scss +++ b/app/assets/stylesheets/store/openfoodweb.css.scss @@ -145,8 +145,11 @@ ul.product-listing { /* Distributor details on product details page */ -#product-distributor-details { +fieldset#product-distributor-details { float: right; + margin-top: 0; + width: 250px; + @extend #shipping; } #product-variants { diff --git a/app/overrides/add_distributor_details_to_product.rb b/app/overrides/add_distributor_details_to_product.rb index b107869e19..6671ed40b5 100644 --- a/app/overrides/add_distributor_details_to_product.rb +++ b/app/overrides/add_distributor_details_to_product.rb @@ -1,4 +1,4 @@ Deface::Override.new(:virtual_path => "spree/products/show", :insert_before => "[data-hook='cart_form']", - :partial => "distributors/details", + :partial => "spree/products/distributor_details", :name => "product_distributor_details") diff --git a/app/views/distributors/_details.html.haml b/app/views/distributors/_details.html.haml index 6646588ef4..2a354be1db 100644 --- a/app/views/distributors/_details.html.haml +++ b/app/views/distributors/_details.html.haml @@ -1,2 +1,23 @@ -#product-distributor-details.columns.five.omega - When you select a distributor for your order, their address and pickup times will be displayed here. +%h2= distributor.name +%p + %strong Address: + %br/ + = render 'spree/shared/address', :address => distributor.pickup_address +%p + %strong Next collection time: + %br/ + = distributor.next_collection_at +%p + %strong Regular collection times: + %br/ + = distributor.pickup_times +%p + %strong Contact: + %br/ + = distributor.contact + %br/ + = "Phone: #{distributor.phone}" + %br/ + = "Email: #{distributor.email}" +%p= distributor.description +%p= link_to distributor.url, distributor.url if distributor.url diff --git a/app/views/spree/checkout/_distributor.html.haml b/app/views/spree/checkout/_distributor.html.haml index 9460e3a94b..b2eac11c71 100644 --- a/app/views/spree/checkout/_distributor.html.haml +++ b/app/views/spree/checkout/_distributor.html.haml @@ -1,27 +1,4 @@ .columns.omega.six %fieldset#shipping %legend Distributor - %h2= @order.distributor.name - %p - %strong Address: - %br/ - = render 'spree/shared/address', :address => @order.distributor.pickup_address - %p - %strong Next collection time: - %br/ - = @order.distributor.next_collection_at - %p - %strong Regular collection times: - %br/ - = @order.distributor.pickup_times - %p - %strong Contact: - %br/ - = @order.distributor.contact - %br/ - = "Phone: #{@order.distributor.phone}" - %br/ - = "Email: #{@order.distributor.email}" - %p= @order.distributor.description - %p= link_to @order.distributor.url, @order.distributor.url if @order.distributor.url - + = render 'distributors/details', :distributor => @order.distributor diff --git a/app/views/spree/products/_distributor_details.html.haml b/app/views/spree/products/_distributor_details.html.haml new file mode 100644 index 0000000000..808468f806 --- /dev/null +++ b/app/views/spree/products/_distributor_details.html.haml @@ -0,0 +1,7 @@ +%fieldset#product-distributor-details.columns.five.omega + %legend Distributor + - order = current_order(false) + - if order.andand.distributor.present? + = render 'distributors/details', :distributor => order.distributor + - else + When you select a distributor for your order, their address and pickup times will be displayed here. diff --git a/spec/requests/consumer/product_spec.rb b/spec/requests/consumer/product_spec.rb index 368452ab54..76a647d074 100644 --- a/spec/requests/consumer/product_spec.rb +++ b/spec/requests/consumer/product_spec.rb @@ -32,7 +32,33 @@ feature %q{ page.should have_selector '#product-distributor-details', :text => 'When you select a distributor for your order, their address and pickup times will be displayed here.' end - it "displays distributor details when one is selected" + it "displays distributor details when one is selected" do + d = create(:distributor) + p = create(:product, :distributors => [d]) + + visit spree.root_path + click_link d.name + visit spree.product_path p + + within '#product-distributor-details' do + [d.name, + d.pickup_address.address1, + d.pickup_address.city, + d.pickup_address.zipcode, + d.pickup_address.state_text, + d.pickup_address.country.name, + d.pickup_times, + d.next_collection_at, + d.contact, + d.phone, + d.email, + d.description, + d.url].each do |value| + + page.should have_content value + end + end + end end context "with Javascript" do From c5941d6cb5068cefeafd23b41045f1ce1dd25408 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Sat, 27 Oct 2012 18:50:09 +1100 Subject: [PATCH 12/52] Update distributor details when distributor is changed --- app/assets/javascripts/store/products.js | 9 ++++++++ app/controllers/distributors_controller.rb | 10 +++++++++ .../add_distributor_details_to_product.rb | 5 +++++ app/views/distributors/index.js.erb | 1 + .../products/_distributor_details.html.haml | 11 +++++----- spec/requests/consumer/product_spec.rb | 21 +++++++++++++++---- 6 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 app/views/distributors/index.js.erb diff --git a/app/assets/javascripts/store/products.js b/app/assets/javascripts/store/products.js index 3e972161a6..0dabff5f4d 100644 --- a/app/assets/javascripts/store/products.js +++ b/app/assets/javascripts/store/products.js @@ -10,6 +10,15 @@ $(document).ready(function() { // Product page with master price only $(".add-to-cart input.title:not(#quantity):not(.max_quantity)").change(products_update_price_without_variant).change(); + + // Product page other + $("#distributor_id").change(function() { + var distributor_html = distributors[$(this).val()]; + if(!distributor_html) { + distributor_html = 'When you select a distributor for your order, their address and pickup times will be displayed here.'; + } + $("#product-distributor-details .distributor-details").html(distributor_html); + }); }); diff --git a/app/controllers/distributors_controller.rb b/app/controllers/distributors_controller.rb index 97c6d81691..2dc50f4af6 100644 --- a/app/controllers/distributors_controller.rb +++ b/app/controllers/distributors_controller.rb @@ -1,4 +1,14 @@ class DistributorsController < BaseController + def index + @distributors = Distributor.all + + respond_to do |format| + format.js do + @distributor_details = Hash[@distributors.map { |d| [d.id, render_to_string(:partial => 'distributors/details', :locals => {:distributor => d})] }] + end + end + end + def show options = {:distributor_id => params[:id]} options.merge(params.reject { |k,v| k == :id }) diff --git a/app/overrides/add_distributor_details_to_product.rb b/app/overrides/add_distributor_details_to_product.rb index 6671ed40b5..4d1b1d344f 100644 --- a/app/overrides/add_distributor_details_to_product.rb +++ b/app/overrides/add_distributor_details_to_product.rb @@ -2,3 +2,8 @@ Deface::Override.new(:virtual_path => "spree/products/show", :insert_before => "[data-hook='cart_form']", :partial => "spree/products/distributor_details", :name => "product_distributor_details") + +Deface::Override.new(:virtual_path => "spree/products/show", + :insert_after => "[data-hook='product_show']", + :text => "<%= javascript_include_tag main_app.distributors_path(:format => :js) %>", + :name => "product_distributor_details_js") diff --git a/app/views/distributors/index.js.erb b/app/views/distributors/index.js.erb new file mode 100644 index 0000000000..aed01164da --- /dev/null +++ b/app/views/distributors/index.js.erb @@ -0,0 +1 @@ +distributors = <%= @distributor_details.to_json.html_safe %>; \ No newline at end of file diff --git a/app/views/spree/products/_distributor_details.html.haml b/app/views/spree/products/_distributor_details.html.haml index 808468f806..e68b6c49ca 100644 --- a/app/views/spree/products/_distributor_details.html.haml +++ b/app/views/spree/products/_distributor_details.html.haml @@ -1,7 +1,8 @@ %fieldset#product-distributor-details.columns.five.omega %legend Distributor - - order = current_order(false) - - if order.andand.distributor.present? - = render 'distributors/details', :distributor => order.distributor - - else - When you select a distributor for your order, their address and pickup times will be displayed here. + .distributor-details + - order = current_order(false) + - if order.andand.distributor.present? + = render 'distributors/details', :distributor => order.distributor + - else + When you select a distributor for your order, their address and pickup times will be displayed here. diff --git a/spec/requests/consumer/product_spec.rb b/spec/requests/consumer/product_spec.rb index 76a647d074..40b65cace8 100644 --- a/spec/requests/consumer/product_spec.rb +++ b/spec/requests/consumer/product_spec.rb @@ -61,10 +61,23 @@ feature %q{ end end - context "with Javascript" do - it "changes distributor details when the distributor is changed" + context "with Javascript", js: true do + it "changes distributor details when the distributor is changed" do + d1 = create(:distributor) + d2 = create(:distributor) + d3 = create(:distributor) + p = create(:product, :distributors => [d1, d2, d3]) + + visit spree.product_path p + + [d1, d2, d3].each do |d| + select d.name, :from => 'distributor_id' + + within '#product-distributor-details' do + page.should have_selector 'h2', :text => d.name + end + end + end end end - - end From 03610c93de31febc1fd35b9f7501738286d2b459 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Sun, 28 Oct 2012 12:28:55 +1100 Subject: [PATCH 13/52] Upgrade capybara and selenium-webdriver for compatibility with Firefox 16 --- Gemfile.lock | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index a79ae3a26e..83954e36f3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -129,7 +129,7 @@ GEM i18n (~> 0.6) multi_json (~> 1.0) acts_as_list (0.1.4) - addressable (2.2.8) + addressable (2.3.2) andand (1.3.3) ansi (1.4.2) arel (3.0.2) @@ -152,8 +152,8 @@ GEM rack-test (>= 0.5.4) selenium-webdriver (~> 2.0) xpath (~> 0.1.4) - childprocess (0.3.2) - ffi (~> 1.0.6) + childprocess (0.3.6) + ffi (~> 1.0, >= 1.0.6) cocaine (0.4.2) coderay (1.0.7) coffee-rails (3.2.2) @@ -196,7 +196,7 @@ GEM faker (1.0.1) i18n (~> 0.4) ffaker (1.12.1) - ffi (1.0.11) + ffi (1.1.5) haml (3.1.6) highline (1.6.11) hike (1.2.1) @@ -215,7 +215,7 @@ GEM railties (>= 3.0.0) kgio (2.7.4) libv8 (3.3.10.4) - libwebsocket (0.1.3) + libwebsocket (0.1.5) addressable mail (2.4.4) i18n (>= 0.4.0) @@ -295,15 +295,14 @@ GEM activesupport (>= 3.0) railties (>= 3.0) rspec (~> 2.10.0) - rubyzip (0.9.8) + rubyzip (0.9.9) sass (3.1.19) sass-rails (3.2.5) railties (~> 3.2.0) sass (>= 3.1.10) tilt (~> 1.3) - selenium-webdriver (2.22.2) + selenium-webdriver (2.25.0) childprocess (>= 0.2.5) - ffi (~> 1.0) libwebsocket (~> 0.1.3) multi_json (~> 1.0) rubyzip From ff2414919542711ad50ebca272d05c99a9a1b0f9 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Sun, 28 Oct 2012 12:59:50 +1100 Subject: [PATCH 14/52] Combine suppliers and distributors into enterprises table --- ...liers_and_distributors_into_enterprises.rb | 49 +++++++++++++++++++ db/schema.rb | 27 ++++++++-- 2 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20121025012233_combine_suppliers_and_distributors_into_enterprises.rb diff --git a/db/migrate/20121025012233_combine_suppliers_and_distributors_into_enterprises.rb b/db/migrate/20121025012233_combine_suppliers_and_distributors_into_enterprises.rb new file mode 100644 index 0000000000..e9c4a18c09 --- /dev/null +++ b/db/migrate/20121025012233_combine_suppliers_and_distributors_into_enterprises.rb @@ -0,0 +1,49 @@ +class CombineSuppliersAndDistributorsIntoEnterprises < ActiveRecord::Migration + class Supplier < ActiveRecord::Base; end + class Distributor < ActiveRecord::Base; end + class Enterprise < ActiveRecord::Base; end + + def up + # Create enterprises table + create_table :enterprises do |t| + t.string :name + t.string :description + t.text :long_description + t.boolean :is_primary_producer + t.boolean :is_distributor + t.string :contact + t.string :phone + t.string :email + t.string :website + t.string :twitter + t.string :abn + t.string :acn + t.integer :address_id + t.string :pickup_times + t.integer :pickup_address_id + t.string :next_collection_at + t.timestamps + end + + # Copy suppliers to enterprises table with primary producer flag set + Supplier.all.each do |s| + attrs = s.attributes + attrs.reject! { |k| k == 'id' } + attrs.merge! is_primary_producer: true, is_distributor: false + Enterprise.create! attrs + end + + # Copy distributors to enterprises table with distributor flag set + Distributor.all.each do |d| + attrs = d.attributes + attrs['website'] = attrs['url'] + attrs.reject! { |k| ['id', 'url'].include? k } + attrs.merge! is_primary_producer: false, is_distributor: true + Enterprise.create! attrs + end + end + + def down + drop_table :enterprises + end +end diff --git a/db/schema.rb b/db/schema.rb index e23f9e59a8..b1555598df 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 => 20121018002907) do +ActiveRecord::Schema.define(:version => 20121025012233) do create_table "cms_blocks", :force => true do |t| t.integer "page_id", :null => false @@ -147,6 +147,27 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.text "long_description" end + create_table "enterprises", :force => true do |t| + t.string "name" + t.string "description" + t.text "long_description" + t.boolean "is_primary_producer" + t.boolean "is_distributor" + t.string "contact" + t.string "phone" + t.string "email" + t.string "website" + t.string "twitter" + t.string "abn" + t.string "acn" + t.integer "address_id" + t.string "pickup_times" + t.integer "pickup_address_id" + t.string "next_collection_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + create_table "product_distributions", :force => true do |t| t.integer "product_id" t.integer "distributor_id" @@ -779,10 +800,10 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "email" t.string "twitter" t.string "website" - t.datetime "created_at" - t.datetime "updated_at" t.integer "address_id" t.text "long_description" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end end From c700d9e71b6ea4f08866a398928bc5ab38f11280 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Sun, 28 Oct 2012 13:24:26 +1100 Subject: [PATCH 15/52] Create enterprise model, integrate functionality from supplier --- app/models/enterprise.rb | 32 +++++++++++++++++++++++ app/models/spree/product_decorator.rb | 2 +- spec/factories.rb | 13 ++++++++++ spec/models/enterprises_spec.rb | 37 +++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 app/models/enterprise.rb create mode 100644 spec/models/enterprises_spec.rb diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb new file mode 100644 index 0000000000..d0a40c3826 --- /dev/null +++ b/app/models/enterprise.rb @@ -0,0 +1,32 @@ +class Enterprise < ActiveRecord::Base + has_many :supplied_products, :class_name => 'Spree::Product', :foreign_key => 'supplier_id' + belongs_to :address, :class_name => 'Spree::Address' + + accepts_nested_attributes_for :address + + validates_presence_of :name, :address + validates_associated :address + + after_initialize :initialize_country + before_validation :set_unused_address_fields + + def has_supplied_products_on_hand? + self.supplied_products.where('count_on_hand > 0').present? + end + + def to_param + "#{id}-#{name.parameterize}" + end + + + private + + def initialize_country + self.address ||= Spree::Address.new + self.address.country = Spree::Country.find_by_id(Spree::Config[:default_country_id]) if self.address.new_record? + end + + def set_unused_address_fields + address.firstname = address.lastname = address.phone = 'unused' if address.present? + end +end diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 91ed6e57ee..58a7c93346 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -1,5 +1,5 @@ Spree::Product.class_eval do - belongs_to :supplier + belongs_to :supplier, :class_name => 'Enterprise' has_many :product_distributions, :dependent => :destroy has_many :distributors, :through => :product_distributions diff --git a/spec/factories.rb b/spec/factories.rb index bd2a08d09f..75cdcec18a 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -2,6 +2,19 @@ require 'faker' require 'spree/core/testing_support/factories' FactoryGirl.define do + factory :enterprise, :class => Enterprise do + sequence(:name) { |n| "Enterprise #{n}" } + description 'enterprise' + long_description '

Hello, world!

This is a paragraph.

' + email 'enterprise@example.com' + address { Spree::Address.first || FactoryGirl.create(:address) } + end + + factory :supplier_enterprise, :parent => :enterprise do + is_primary_producer true + is_distributor false + end + factory :supplier, :class => Supplier do sequence(:name) { |n| "Supplier #{n}" } description 'supplier' diff --git a/spec/models/enterprises_spec.rb b/spec/models/enterprises_spec.rb new file mode 100644 index 0000000000..6e7930e355 --- /dev/null +++ b/spec/models/enterprises_spec.rb @@ -0,0 +1,37 @@ +require 'spec_helper' + +describe Enterprise do + + describe "associations" do + it { should have_many(:supplied_products) } + it { should belong_to(:address) } + end + + describe "validations" do + it { should validate_presence_of(:name) } + end + + it "should default country to system country" do + subject.address.country.should == Spree::Country.find_by_id(Spree::Config[:default_country_id]) + end + + context "has_supplied_products_on_hand?" do + before :each do + @supplier = create(:supplier_enterprise) + end + + it "returns false when no products" do + @supplier.should_not have_supplied_products_on_hand + end + + it "returns false when the product is out of stock" do + create(:product, :supplier => @supplier, :on_hand => 0) + @supplier.should_not have_supplied_products_on_hand + end + + it "returns true when the product is in stock" do + create(:product, :supplier => @supplier, :on_hand => 1) + @supplier.should have_supplied_products_on_hand + end + end +end From abe3feb9963db8ec2709eb358b5c2f76fd63c6de Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Sun, 28 Oct 2012 17:46:45 +1100 Subject: [PATCH 16/52] Integrate functionality from distributor into enterprise --- app/models/enterprise.rb | 12 +++++++++++- app/models/product_distribution.rb | 2 +- spec/factories.rb | 7 ++++++- spec/models/enterprises_spec.rb | 18 +++++++++++++++++- 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index d0a40c3826..0d66f83f8e 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -1,15 +1,25 @@ class Enterprise < ActiveRecord::Base has_many :supplied_products, :class_name => 'Spree::Product', :foreign_key => 'supplier_id' + has_many :distributed_orders, :class_name => 'Spree::Order', :foreign_key => 'distributor_id' belongs_to :address, :class_name => 'Spree::Address' + has_many :product_distributions, :foreign_key => 'distributor_id', :dependent => :destroy + has_many :distributed_products, :through => :product_distributions, :source => :product accepts_nested_attributes_for :address - validates_presence_of :name, :address + validates_presence_of :name + validates_presence_of :address validates_associated :address after_initialize :initialize_country before_validation :set_unused_address_fields + scope :by_name, order('name') + scope :is_supplier, where(:is_primary_producer => true) + scope :is_distributor, where(:is_distributor => true) + scope :with_distributed_active_products_on_hand, lambda { joins(:distributed_products).where('spree_products.deleted_at IS NULL AND spree_products.available_on <= ? AND spree_products.count_on_hand > 0', Time.now).select('distinct(enterprises.*)') } + + def has_supplied_products_on_hand? self.supplied_products.where('count_on_hand > 0').present? end diff --git a/app/models/product_distribution.rb b/app/models/product_distribution.rb index 19305c2685..026322afd6 100644 --- a/app/models/product_distribution.rb +++ b/app/models/product_distribution.rb @@ -1,6 +1,6 @@ class ProductDistribution < ActiveRecord::Base belongs_to :product, :class_name => 'Spree::Product' - belongs_to :distributor + belongs_to :distributor, :class_name => 'Enterprise' belongs_to :shipping_method, :class_name => 'Spree::ShippingMethod' validates_presence_of :product_id, :on => :update diff --git a/spec/factories.rb b/spec/factories.rb index 75cdcec18a..eded25b43c 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -15,6 +15,11 @@ FactoryGirl.define do is_distributor false end + factory :distributor_enterprise, :parent => :enterprise do + is_primary_producer false + is_distributor true + end + factory :supplier, :class => Supplier do sequence(:name) { |n| "Supplier #{n}" } description 'supplier' @@ -59,7 +64,7 @@ FactoryGirl.modify do # When this fix has been merged into a version of Spree that we're using, this line can be removed. sequence(:name) { |n| "Product ##{n} - #{Kernel.rand(9999)}" } - supplier { Supplier.first || FactoryGirl.create(:supplier) } + supplier { Enterprise.is_supplier.first || FactoryGirl.create(:supplier_enterprise) } on_hand 3 # before(:create) do |product, evaluator| diff --git a/spec/models/enterprises_spec.rb b/spec/models/enterprises_spec.rb index 6e7930e355..051533144e 100644 --- a/spec/models/enterprises_spec.rb +++ b/spec/models/enterprises_spec.rb @@ -4,17 +4,33 @@ describe Enterprise do describe "associations" do it { should have_many(:supplied_products) } + it { should have_many(:distributed_orders) } it { should belong_to(:address) } + it { should have_many(:product_distributions) } end describe "validations" do it { should validate_presence_of(:name) } end - it "should default country to system country" do + it "should default address country to system country" do subject.address.country.should == Spree::Country.find_by_id(Spree::Config[:default_country_id]) end + describe "scopes" do + it "returns distributors with products in stock" do + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) + d3 = create(:distributor_enterprise) + d4 = create(:distributor_enterprise) + create(:product, :distributors => [d1, d2], :on_hand => 5) + create(:product, :distributors => [d1], :on_hand => 5) + create(:product, :distributors => [d3], :on_hand => 0) + + Enterprise.with_distributed_active_products_on_hand.sort.should == [d1, d2] + end + end + context "has_supplied_products_on_hand?" do before :each do @supplier = create(:supplier_enterprise) From 4c2debba7e4079f86d439989217740f342b66abe Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Sun, 28 Oct 2012 18:00:15 +1100 Subject: [PATCH 17/52] Model specs pass --- app/models/spree/order_decorator.rb | 4 +-- spec/factories.rb | 2 +- spec/models/distributors_spec.rb | 36 ---------------------- spec/models/order_spec.rb | 10 +++--- spec/models/product_distribution_spec.rb | 2 +- spec/models/product_spec.rb | 2 +- spec/models/suppliers_spec.rb | 39 ------------------------ 7 files changed, 10 insertions(+), 85 deletions(-) delete mode 100644 spec/models/distributors_spec.rb delete mode 100644 spec/models/suppliers_spec.rb diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index 4cfbd1ddcb..449640a4b6 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -1,5 +1,5 @@ Spree::Order.class_eval do - belongs_to :distributor + belongs_to :distributor, :class_name => 'Enterprise' before_validation :shipping_address_from_distributor after_create :set_default_shipping_method @@ -57,7 +57,7 @@ Spree::Order.class_eval do def shipping_address_from_distributor if distributor - self.ship_address = distributor.pickup_address.clone + self.ship_address = distributor.address.clone if bill_address self.ship_address.firstname = bill_address.firstname diff --git a/spec/factories.rb b/spec/factories.rb index eded25b43c..7ecae25065 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -43,7 +43,7 @@ FactoryGirl.define do factory :product_distribution, :class => ProductDistribution do product { |pd| Spree::Product.first || FactoryGirl.create(:product) } - distributor { |pd| Distributor.first || FactoryGirl.create(:distributor) } + distributor { |pd| Enterprise.is_distributor.first || FactoryGirl.create(:distributor_enterprise) } shipping_method { |pd| Spree::ShippingMethod.where("name != 'Delivery'").first || FactoryGirl.create(:shipping_method) } end diff --git a/spec/models/distributors_spec.rb b/spec/models/distributors_spec.rb deleted file mode 100644 index 64fa93b585..0000000000 --- a/spec/models/distributors_spec.rb +++ /dev/null @@ -1,36 +0,0 @@ -require 'spec_helper' - -module Spree - describe Distributor do - - describe "associations" do - it { should belong_to(:pickup_address) } - it { should have_many(:product_distributions) } - it { should have_many(:orders) } - end - - describe "validations" do - it { should validate_presence_of(:name) } - end - - it "should default country to system country" do - distributor = Distributor.new - distributor.pickup_address.country.should == Country.find_by_id(Config[:default_country_id]) - end - - describe "scopes" do - it "returns distributors with products in stock" do - d1 = create(:distributor) - d2 = create(:distributor) - d3 = create(:distributor) - d4 = create(:distributor) - create(:product, :distributors => [d1, d2], :on_hand => 5) - create(:product, :distributors => [d1], :on_hand => 5) - create(:product, :distributors => [d3], :on_hand => 0) - - Distributor.with_active_products_on_hand.sort.should == [d1, d2] - end - end - - end -end diff --git a/spec/models/order_spec.rb b/spec/models/order_spec.rb index aaa3d70c36..335c67a574 100644 --- a/spec/models/order_spec.rb +++ b/spec/models/order_spec.rb @@ -15,7 +15,7 @@ describe Spree::Order do end it "reveals permission for changing distributor" do - d = create(:distributor) + d = create(:distributor_enterprise) p = create(:product, :distributors => [d]) subject.distributor = d @@ -27,7 +27,7 @@ describe Spree::Order do end it "raises an exception if distributor is changed without permission" do - d = create(:distributor) + d = create(:distributor_enterprise) p = create(:product, :distributors => [d]) subject.distributor = d subject.save! @@ -41,8 +41,8 @@ describe Spree::Order do end it "reveals permission for adding products to the cart" do - d1 = create(:distributor) - d2 = create(:distributor) + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) p_first = create(:product, :distributors => [d1]) p_subsequent_same_dist = create(:product, :distributors => [d1]) @@ -67,7 +67,7 @@ describe Spree::Order do end it "sets attributes on line items for variants" do - d = create(:distributor) + d = create(:distributor_enterprise) p = create(:product, :distributors => [d]) subject.distributor = d diff --git a/spec/models/product_distribution_spec.rb b/spec/models/product_distribution_spec.rb index bcfd131dea..51b5b9a8a2 100644 --- a/spec/models/product_distribution_spec.rb +++ b/spec/models/product_distribution_spec.rb @@ -6,7 +6,7 @@ describe ProductDistribution do pd1.should be_valid new_product = create(:product) - new_distributor = create(:distributor) + new_distributor = create(:distributor_enterprise) pd2 = build(:product_distribution, :product => pd1.product, :distributor => pd1.distributor) pd2.should_not be_valid diff --git a/spec/models/product_spec.rb b/spec/models/product_spec.rb index b78a08c6f0..dd0d8490b9 100644 --- a/spec/models/product_spec.rb +++ b/spec/models/product_spec.rb @@ -22,7 +22,7 @@ describe Spree::Product do context "finders" do it "finds the shipping method for a particular distributor" do shipping_method = create(:shipping_method) - distributor = create(:distributor) + distributor = create(:distributor_enterprise) product = create(:product) product_distribution = create(:product_distribution, :product => product, :distributor => distributor, :shipping_method => shipping_method) product.shipping_method_for_distributor(distributor).should == shipping_method diff --git a/spec/models/suppliers_spec.rb b/spec/models/suppliers_spec.rb deleted file mode 100644 index 7ddc4f8cab..0000000000 --- a/spec/models/suppliers_spec.rb +++ /dev/null @@ -1,39 +0,0 @@ -require 'spec_helper' - -module Spree - describe Supplier do - - describe "associations" do - it { should have_many(:products) } - it { should belong_to(:address) } - end - - describe "validations" do - it { should validate_presence_of(:name) } - end - - it "should default country to system country" do - subject.address.country.should == Country.find_by_id(Config[:default_country_id]) - end - - context "has_products_on_hand?" do - before :each do - @supplier = create(:supplier) - end - - it "returns false when no products" do - @supplier.should_not have_products_on_hand - end - - it "returns false when the product is out of stock" do - create(:product, :supplier => @supplier, :on_hand => 0) - @supplier.should_not have_products_on_hand - end - - it "returns true when the product is in stock" do - create(:product, :supplier => @supplier, :on_hand => 1) - @supplier.should have_products_on_hand - end - end - end -end From 04d8648c6db0802d1bc4613c9591d30803859645 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Sun, 28 Oct 2012 18:03:19 +1100 Subject: [PATCH 18/52] Remove pickup address and use single address field for supplier and distributor roles within enterprise --- .../20121028070200_remove_pickup_address_from_enterprises.rb | 5 +++++ db/schema.rb | 3 +-- 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20121028070200_remove_pickup_address_from_enterprises.rb diff --git a/db/migrate/20121028070200_remove_pickup_address_from_enterprises.rb b/db/migrate/20121028070200_remove_pickup_address_from_enterprises.rb new file mode 100644 index 0000000000..2aa38222b4 --- /dev/null +++ b/db/migrate/20121028070200_remove_pickup_address_from_enterprises.rb @@ -0,0 +1,5 @@ +class RemovePickupAddressFromEnterprises < ActiveRecord::Migration + def change + remove_column :enterprises, :pickup_address_id + end +end diff --git a/db/schema.rb b/db/schema.rb index b1555598df..7863a818d1 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 => 20121025012233) do +ActiveRecord::Schema.define(:version => 20121028070200) do create_table "cms_blocks", :force => true do |t| t.integer "page_id", :null => false @@ -162,7 +162,6 @@ ActiveRecord::Schema.define(:version => 20121025012233) do t.string "acn" t.integer "address_id" t.string "pickup_times" - t.integer "pickup_address_id" t.string "next_collection_at" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false From 077098e6c1a157be92752dbbe8abb041f521d9da Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Mon, 29 Oct 2012 14:57:03 +1100 Subject: [PATCH 19/52] Refactor libs to use enterprise instead of supplier and distributor --- lib/open_food_web/order_and_distributor_report.rb | 2 +- lib/spree/product_filters.rb | 6 +++--- spec/lib/open_food_web/group_buy_report_spec.rb | 10 +++++----- .../open_food_web/order_and_distributor_report_spec.rb | 4 ++-- spec/lib/open_food_web/searcher_spec.rb | 8 ++++---- .../split_products_by_distributor_spec.rb | 4 ++-- spec/lib/spree/product_filters_spec.rb | 4 ++-- 7 files changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/open_food_web/order_and_distributor_report.rb b/lib/open_food_web/order_and_distributor_report.rb index a477fd0edd..bcd7aba08d 100644 --- a/lib/open_food_web/order_and_distributor_report.rb +++ b/lib/open_food_web/order_and_distributor_report.rb @@ -22,7 +22,7 @@ module OpenFoodWeb order.bill_address.full_name, order.email, order.bill_address.phone, order.bill_address.city, line_item.product.sku, line_item.product.name, line_item.variant.options_text, line_item.quantity, line_item.max_quantity, line_item.price * line_item.quantity, line_item.itemwise_shipping_cost, order.payments.first.payment_method.andand.name, - order.distributor.andand.name, order.distributor.pickup_address.address1, order.distributor.pickup_address.city, order.distributor.pickup_address.zipcode, order.special_instructions ] + order.distributor.andand.name, order.distributor.address.address1, order.distributor.address.city, order.distributor.address.zipcode, order.special_instructions ] end end order_and_distributor_details diff --git a/lib/spree/product_filters.rb b/lib/spree/product_filters.rb index b7e3b0d6db..407d7e07f5 100644 --- a/lib/spree/product_filters.rb +++ b/lib/spree/product_filters.rb @@ -1,6 +1,6 @@ module Spree module ProductFilters - if Distributor.table_exists? + if Enterprise.table_exists? Spree::Product.scope :distributor_any, lambda {|*opts| conds = opts.map {|o| ProductFilters.distributor_filter[:conds][o]}.reject {|c| c.nil?} @@ -8,8 +8,8 @@ module Spree } def ProductFilters.distributor_filter - distributors = Distributor.all.map(&:name).compact.uniq - conds = Hash[*distributors.map { |d| [d, "#{Distributor.table_name}.name = '#{d}'"] }.flatten] + distributors = Enterprise.is_distributor.map(&:name).compact.uniq + conds = Hash[*distributors.map { |d| [d, "#{Enterprise.table_name}.name = '#{d}'"] }.flatten] { :name => "Group", :scope => :distributor_any, :conds => conds, diff --git a/spec/lib/open_food_web/group_buy_report_spec.rb b/spec/lib/open_food_web/group_buy_report_spec.rb index c3ed851d55..c77d774856 100644 --- a/spec/lib/open_food_web/group_buy_report_spec.rb +++ b/spec/lib/open_food_web/group_buy_report_spec.rb @@ -3,13 +3,13 @@ require 'spec_helper' module OpenFoodWeb describe GroupBuyReport do - before(:each) do + before(:all) do @orders = [] bill_address = create(:address) distributor_address = create(:address, :address1 => "distributor address", :city => 'The Shire', :zipcode => "1234") - distributor = create(:distributor, :pickup_address => distributor_address) + distributor = create(:distributor_enterprise, :address => distributor_address) - @supplier1 = create(:supplier) + @supplier1 = create(:supplier_enterprise) @variant1 = create(:variant) @variant1.product.supplier = @supplier1 @variant1.product.save! @@ -35,7 +35,7 @@ module OpenFoodWeb order2.line_items << line_item22 @orders << order2 - @supplier2 = create(:supplier) + @supplier2 = create(:supplier_enterprise) @variant3 = create(:variant) @variant3.product.supplier = @supplier2 @variant3.product.save! @@ -94,4 +94,4 @@ module OpenFoodWeb product_groups.length.should == 3 end end -end \ No newline at end of file +end diff --git a/spec/lib/open_food_web/order_and_distributor_report_spec.rb b/spec/lib/open_food_web/order_and_distributor_report_spec.rb index 26a04a8cae..8081319391 100644 --- a/spec/lib/open_food_web/order_and_distributor_report_spec.rb +++ b/spec/lib/open_food_web/order_and_distributor_report_spec.rb @@ -9,7 +9,7 @@ module OpenFoodWeb before(:each) do @bill_address = create(:address) @distributor_address = create(:address, :address1 => "distributor address", :city => 'The Shire', :zipcode => "1234") - @distributor = create(:distributor, :pickup_address => @distributor_address) + @distributor = create(:distributor_enterprise, :address => @distributor_address) product = create(:product) product_distribution = create(:product_distribution, :product => product, :distributor => @distributor, :shipping_method => create(:shipping_method)) @shipping_instructions = "pick up on thursday please!" @@ -41,7 +41,7 @@ module OpenFoodWeb @bill_address.full_name, @order.email, @bill_address.phone, @bill_address.city, @line_item.product.sku, @line_item.product.name, @line_item.variant.options_text, @line_item.quantity, @line_item.max_quantity, @line_item.price * @line_item.quantity, @line_item.itemwise_shipping_cost, @payment_method.name, - @distributor.name, @distributor.pickup_address.address1, @distributor.pickup_address.city, @distributor.pickup_address.zipcode, @shipping_instructions ] + @distributor.name, @distributor.address.address1, @distributor.address.city, @distributor.address.zipcode, @shipping_instructions ] end end end diff --git a/spec/lib/open_food_web/searcher_spec.rb b/spec/lib/open_food_web/searcher_spec.rb index 0dc3ec7477..2617778abc 100644 --- a/spec/lib/open_food_web/searcher_spec.rb +++ b/spec/lib/open_food_web/searcher_spec.rb @@ -5,8 +5,8 @@ module OpenFoodWeb describe Searcher do it "searches by supplier" do # Given products under two suppliers - s1 = create(:supplier) - s2 = create(:supplier) + s1 = create(:supplier_enterprise) + s2 = create(:supplier_enterprise) p1 = create(:product, :supplier => s1) p2 = create(:product, :supplier => s2) @@ -18,8 +18,8 @@ module OpenFoodWeb it "searches by distributor" do # Given products under two distributors - d1 = create(:distributor) - d2 = create(:distributor) + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) p1 = create(:product, :distributors => [d1]) p2 = create(:product, :distributors => [d2]) diff --git a/spec/lib/open_food_web/split_products_by_distributor_spec.rb b/spec/lib/open_food_web/split_products_by_distributor_spec.rb index 4924ac1061..b4a7a7b145 100644 --- a/spec/lib/open_food_web/split_products_by_distributor_spec.rb +++ b/spec/lib/open_food_web/split_products_by_distributor_spec.rb @@ -16,8 +16,8 @@ describe OpenFoodWeb::SplitProductsByDistributor do end it "splits products when a distributor is selected" do - d1 = build(:distributor) - d2 = build(:distributor) + d1 = build(:distributor_enterprise) + d2 = build(:distributor_enterprise) orig_products = [build(:product, :distributors => [d1]), build(:product, :distributors => [d2])] diff --git a/spec/lib/spree/product_filters_spec.rb b/spec/lib/spree/product_filters_spec.rb index eb879cfe11..5335683423 100644 --- a/spec/lib/spree/product_filters_spec.rb +++ b/spec/lib/spree/product_filters_spec.rb @@ -3,8 +3,8 @@ require 'spec_helper' describe Spree::ProductFilters do context "distributor filter" do it "provides filtering for all distributors" do - 3.times { create(:distributor) } - Spree::ProductFilters.distributor_filter[:labels].should == Distributor.all.map { |d| [d.name, d.name] } + 3.times { create(:distributor_enterprise) } + Spree::ProductFilters.distributor_filter[:labels].should == Enterprise.is_distributor.map { |d| [d.name, d.name] } end end end From 9b32579bd69d7023befc84b6d71d90d9499981ad Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 31 Oct 2012 10:37:40 +1100 Subject: [PATCH 20/52] Group buy report handles nil variant weight gracefully --- lib/open_food_web/group_buy_report.rb | 4 ++-- spec/lib/open_food_web/group_buy_report_spec.rb | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/open_food_web/group_buy_report.rb b/lib/open_food_web/group_buy_report.rb index f8582f88a1..3234d1078c 100644 --- a/lib/open_food_web/group_buy_report.rb +++ b/lib/open_food_web/group_buy_report.rb @@ -38,8 +38,8 @@ module OpenFoodWeb end # Sum quantities for each product (Total line) - sum_quantities = line_items_by_product.sum { |li| li.variant.weight * li.quantity } - sum_max_quantities = line_items_by_product.sum { |li| li.variant.weight * (li.max_quantity || 0) } + sum_quantities = line_items_by_product.sum { |li| (li.variant.weight || 0) * li.quantity } + sum_max_quantities = line_items_by_product.sum { |li| (li.variant.weight || 0) * (li.max_quantity || 0) } variants_and_quantities << GroupBuyProductRow.new(product, sum_quantities, sum_max_quantities) end end diff --git a/spec/lib/open_food_web/group_buy_report_spec.rb b/spec/lib/open_food_web/group_buy_report_spec.rb index c3ed851d55..0513c235c7 100644 --- a/spec/lib/open_food_web/group_buy_report_spec.rb +++ b/spec/lib/open_food_web/group_buy_report_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' module OpenFoodWeb describe GroupBuyReport do - before(:each) do + before(:all) do @orders = [] bill_address = create(:address) distributor_address = create(:address, :address1 => "distributor address", :city => 'The Shire', :zipcode => "1234") @@ -36,7 +36,7 @@ module OpenFoodWeb @orders << order2 @supplier2 = create(:supplier) - @variant3 = create(:variant) + @variant3 = create(:variant, :weight => nil) @variant3.product.supplier = @supplier2 @variant3.product.save! product_distribution = create(:product_distribution, :product => @variant3.product, :distributor => distributor, :shipping_method => create(:shipping_method)) @@ -82,16 +82,16 @@ module OpenFoodWeb table_row_objects = subject.variants_and_quantities - variant_rows = table_row_objects.select{ |r| r.class == OpenFoodWeb::GroupBuyVariantRow } - product_rows = table_row_objects.select{ |r| r.class == OpenFoodWeb::GroupBuyProductRow } + variant_rows = table_row_objects.select { |r| r.class == OpenFoodWeb::GroupBuyVariantRow } + product_rows = table_row_objects.select { |r| r.class == OpenFoodWeb::GroupBuyProductRow } supplier_groups = variant_rows.group_by { |r| r.variant.product.supplier } - variant_groups = variant_rows.group_by{ |r| r.variant } - product_groups = product_rows.group_by{ |r| r.product } + variant_groups = variant_rows.group_by { |r| r.variant } + product_groups = product_rows.group_by { |r| r.product } supplier_groups.length.should == 2 variant_groups.length.should == 3 product_groups.length.should == 3 end end -end \ No newline at end of file +end From 926440a978e0cb99402b17c6a8fec3a923fd8a8c Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 31 Oct 2012 11:13:55 +1100 Subject: [PATCH 21/52] Fix namespacing of Distributor in reports controller --- app/controllers/spree/admin/reports_controller_decorator.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index 081be1be5a..04bba767ae 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -89,7 +89,7 @@ Spree::Admin::ReportsController.class_eval do orders = @search.result line_items = orders.map { |o| o.line_items }.flatten - @distributors = Spree::Distributor.all + @distributors = Distributor.all @report_type = params[:report_type] case params[:report_type] @@ -216,7 +216,7 @@ Spree::Admin::ReportsController.class_eval do orders = @search.result payments = orders.map { |o| o.payments.select { |payment| payment.completed? } }.flatten # Only select completed payments - @distributors = Spree::Distributor.all + @distributors = Distributor.all @report_type = params[:report_type] case params[:report_type] @@ -321,7 +321,7 @@ Spree::Admin::ReportsController.class_eval do line_items = orders.map { |o| o.line_items }.flatten #payments = orders.map { |o| o.payments.select { |payment| payment.completed? } }.flatten # Only select completed payments - @distributors = Spree::Distributor.all + @distributors = Distributor.all @report_type = params[:report_type] case params[:report_type] From 5dcba7403b006228a68f41477e2d06fa6050b970 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 31 Oct 2012 13:54:14 +1100 Subject: [PATCH 22/52] Avoid contaminating test data --- spec/lib/open_food_web/group_buy_report_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/lib/open_food_web/group_buy_report_spec.rb b/spec/lib/open_food_web/group_buy_report_spec.rb index 0513c235c7..ed21aba6d2 100644 --- a/spec/lib/open_food_web/group_buy_report_spec.rb +++ b/spec/lib/open_food_web/group_buy_report_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' module OpenFoodWeb describe GroupBuyReport do - before(:all) do + before(:each) do @orders = [] bill_address = create(:address) distributor_address = create(:address, :address1 => "distributor address", :city => 'The Shire', :zipcode => "1234") From 1d1c27701dd5d41a2d895be3d034c48699a35c91 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 31 Oct 2012 14:25:00 +1100 Subject: [PATCH 23/52] Add admin interface for enterprises --- .../admin/enterprises_controller.rb | 30 +++++ .../{distributor_set.rb => enterprise_set.rb} | 16 +-- app/overrides/enterprises_admin_tab.rb | 4 + app/views/admin/enterprises/_form.html.haml | 54 ++++++++ app/views/admin/enterprises/edit.html.erb | 6 + app/views/admin/enterprises/index.html.erb | 40 ++++++ app/views/admin/enterprises/new.html.erb | 6 + app/views/admin/enterprises/show.html.haml | 51 ++++++++ config/routes.rb | 3 + db/schema.rb | 8 +- spec/requests/admin/enterprises_spec.rb | 122 ++++++++++++++++++ 11 files changed, 328 insertions(+), 12 deletions(-) create mode 100644 app/controllers/admin/enterprises_controller.rb rename app/models/{distributor_set.rb => enterprise_set.rb} (55%) create mode 100644 app/overrides/enterprises_admin_tab.rb create mode 100644 app/views/admin/enterprises/_form.html.haml create mode 100644 app/views/admin/enterprises/edit.html.erb create mode 100644 app/views/admin/enterprises/index.html.erb create mode 100644 app/views/admin/enterprises/new.html.erb create mode 100644 app/views/admin/enterprises/show.html.haml create mode 100644 spec/requests/admin/enterprises_spec.rb diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb new file mode 100644 index 0000000000..bc9b39ef35 --- /dev/null +++ b/app/controllers/admin/enterprises_controller.rb @@ -0,0 +1,30 @@ +module Admin + class EnterprisesController < ResourceController + before_filter :load_enterprise_set, :only => :index + before_filter :load_countries, :except => :index + + helper 'spree/products' + + def bulk_update + @enterprise_set = EnterpriseSet.new(params[:enterprise_set]) + if @enterprise_set.save + redirect_to main_app.admin_enterprises_path, :notice => 'Distributor collection times updated.' + else + render :index + end + end + + private + def load_enterprise_set + @enterprise_set = EnterpriseSet.new :enterprises => collection + end + + def load_countries + @countries = Spree::Country.order(:name) + end + + def collection + super.order(:name) + end + end +end diff --git a/app/models/distributor_set.rb b/app/models/enterprise_set.rb similarity index 55% rename from app/models/distributor_set.rb rename to app/models/enterprise_set.rb index 04fbd02db4..b741167dfe 100644 --- a/app/models/distributor_set.rb +++ b/app/models/enterprise_set.rb @@ -1,30 +1,30 @@ -# Tableless model to handle updating multiple distributors at once from a +# Tableless model to handle updating multiple enterprises at once from a # single form. Used to update next_collection_at field for all distributors in # admin backend. -class DistributorSet +class EnterpriseSet include ActiveModel::Conversion extend ActiveModel::Naming - attr_accessor :distributors + attr_accessor :enterprises def initialize(attributes={}) - @distributors = Distributor.all + @enterprises = Enterprise.all attributes.each do |name, value| send("#{name}=", value) end end - def distributors_attributes=(attributes) + def enterprises_attributes=(attributes) attributes.each do |k, attributes| # attributes == {:id => 123, :next_collection_at => '...'} - d = @distributors.detect { |d| d.id.to_s == attributes[:id].to_s } - d.assign_attributes(attributes.except(:id)) + e = @enterprises.detect { |e| e.id.to_s == attributes[:id].to_s } + e.assign_attributes(attributes.except(:id)) end end def save - distributors.all?(&:save) + enterprises.all?(&:save) end def persisted? diff --git a/app/overrides/enterprises_admin_tab.rb b/app/overrides/enterprises_admin_tab.rb new file mode 100644 index 0000000000..254f9d7d25 --- /dev/null +++ b/app/overrides/enterprises_admin_tab.rb @@ -0,0 +1,4 @@ +Deface::Override.new(:virtual_path => "spree/layouts/admin", + :name => "enterprises_admin_tabs", + :insert_bottom => "[data-hook='admin_tabs'], #admin_tabs[data-hook]", + :text => "<%= tab(:enterprises, :url => main_app.admin_enterprises_path) %>") diff --git a/app/views/admin/enterprises/_form.html.haml b/app/views/admin/enterprises/_form.html.haml new file mode 100644 index 0000000000..1cd6efa5e0 --- /dev/null +++ b/app/views/admin/enterprises/_form.html.haml @@ -0,0 +1,54 @@ +- content_for :head do + = render 'shared/cms_elrte_head' + +%table{"data-hook" => "distributors"} + %tr{"data-hook" => "name"} + %td Name: + %td= f.text_field :name + %tr{"data-hook" => "description"} + %td Description: + %td= f.text_field :description + %tr{'data-hook' => "long_description"} + %td Extended Description: + %td= f.text_area :long_description, :class => 'rich_text' + %tr{'data-hook' => "is_primary_producer"} + %td Primary Producer? + %td= f.check_box :is_primary_producer + %tr{'data-hook' => "is_distributor"} + %td Distributor? + %td= f.check_box :is_distributor + %tr{"data-hook" => "contact"} + %td Contact Person: + %td= f.text_field :contact + %tr{"data-hook" => "phone"} + %td Phone: + %td= f.text_field :phone + %tr{"data-hook" => "email"} + %td Email: + %td= f.text_field :email + %tr{"data-hook" => "website"} + %td Website: + %td= f.text_field :website + %tr{"data-hook" => "twitter"} + %td Twitter: + %td= f.text_field :twitter + %tr{"data-hook" => "abn"} + %td ABN: + %td= f.text_field :abn + %tr{"data-hook" => "acn"} + %td ACN: + %td= f.text_field :acn +%fieldset + %legend Address + %table + = f.fields_for :address do |address_form| + = render 'spree/admin/shared/address_form', :f => address_form +%fieldset + %legend Pickup details + %table{"data-hook" => "distributors_pickup_details"} + %tr{"data-hook" => "next_collection_at"} + %td Next collection date/time: + %td= f.text_field :next_collection_at + %tr{"data-hook" => "pickup_times"} + %td Regular pickup times: + %td= f.text_field :pickup_times diff --git a/app/views/admin/enterprises/edit.html.erb b/app/views/admin/enterprises/edit.html.erb new file mode 100644 index 0000000000..f4302f4ef9 --- /dev/null +++ b/app/views/admin/enterprises/edit.html.erb @@ -0,0 +1,6 @@ +<%= render :partial => 'spree/shared/error_messages', :locals => { :target => @enterprise } %> + +<%= form_for [main_app, :admin, @enterprise] do |f| %> + <%= render :partial => 'form', :locals => { :f => f } %> + <%= render :partial => 'spree/admin/shared/edit_resource_links' %> +<% end %> diff --git a/app/views/admin/enterprises/index.html.erb b/app/views/admin/enterprises/index.html.erb new file mode 100644 index 0000000000..d31be7b2b8 --- /dev/null +++ b/app/views/admin/enterprises/index.html.erb @@ -0,0 +1,40 @@ +
+
    +
  • + <%= button_link_to "New Enterprise", main_app.new_admin_enterprise_path, :icon => 'add', :id => 'admin_new_enterprise_link' %> +
  • +
+
+
+ +<%= form_for @enterprise_set, :url => main_app.bulk_update_admin_enterprises_path do |f| %> + + + + + + + + + + + <%= f.fields_for :enterprises do |enterprise_form| %> + <% enterprise = enterprise_form.object %> + + + + + + + <% end %> + <% if @enterprises.empty? %> + + <% end %> + +
NameNext Collection Date/TimeDescription
<%= link_to enterprise.name, main_app.admin_enterprise_path(enterprise) %><%= enterprise_form.text_field :next_collection_at %><%= enterprise.description %> + <%= link_to_edit enterprise, :class => 'edit' %>   + <%= link_to_delete enterprise %> +
<%= t(:none) %>
+ + <%= f.submit 'Update' %> +<% end %> diff --git a/app/views/admin/enterprises/new.html.erb b/app/views/admin/enterprises/new.html.erb new file mode 100644 index 0000000000..92682f64d8 --- /dev/null +++ b/app/views/admin/enterprises/new.html.erb @@ -0,0 +1,6 @@ +<%= render :partial => 'spree/shared/error_messages', :locals => { :target => @enterprise } %> + +<%= form_for [main_app, :admin, @enterprise] do |f| %> + <%= render :partial => 'form', :locals => { :f => f } %> + <%= render :partial => 'spree/admin/shared/new_resource_links' %> +<% end %> diff --git a/app/views/admin/enterprises/show.html.haml b/app/views/admin/enterprises/show.html.haml new file mode 100644 index 0000000000..a2eb9027fa --- /dev/null +++ b/app/views/admin/enterprises/show.html.haml @@ -0,0 +1,51 @@ +%h1 Enterprise +%table + %tr + %th Name: + %td= @enterprise.name + %tr + %th Description: + %td= @enterprise.description + %tr + %th Extended Description: + %td= @enterprise.long_description.andand.html_safe + %tr + %th Primary producer? + %td= @enterprise.is_primary_producer ? 'Yes' : 'No' + %tr + %th Distributor? + %td= @enterprise.is_distributor ? 'Yes' : 'No' + %tr + %th Contact person: + %td= @enterprise.contact + %tr + %th Phone number: + %td= @enterprise.phone + %tr + %th Email: + %td= @enterprise.email + %tr + %th Website: + %td= @enterprise.website + %tr + %th Twitter: + %td= @enterprise.twitter + %tr + %th ABN: + %td= @enterprise.abn + %tr + %th ACN: + %td= @enterprise.acn + %tr + %th Address: + %td= render 'spree/shared/address', :address => @enterprise.address + %tr + %th Regular pickup times: + %td= @enterprise.pickup_times + %tr + %th Next collection date/time: + %td= @enterprise.next_collection_at +%p + = link_to :Edit, main_app.edit_admin_enterprise_path(@enterprise), :class => 'edit_enterprise' + = t(:or) + = link_to t(:back), main_app.admin_enterprises_path diff --git a/config/routes.rb b/config/routes.rb index 3d5a29e0bb..7fb03b6950 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,6 +8,9 @@ Openfoodweb::Application.routes.draw do end namespace :admin do + resources :enterprises do + post :bulk_update, :on => :collection, :as => :bulk_update + end resources :distributors do post :bulk_update, :on => :collection, :as => :bulk_update end diff --git a/db/schema.rb b/db/schema.rb index 7863a818d1..6267fefdd9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -282,8 +282,8 @@ ActiveRecord::Schema.define(:version => 20121028070200) do t.string "start_year" t.string "issue_number" t.integer "address_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" t.string "gateway_customer_profile_id" t.string "gateway_payment_profile_id" end @@ -799,10 +799,10 @@ ActiveRecord::Schema.define(:version => 20121028070200) do t.string "email" t.string "twitter" t.string "website" + t.datetime "created_at" + t.datetime "updated_at" t.integer "address_id" t.text "long_description" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false end end diff --git a/spec/requests/admin/enterprises_spec.rb b/spec/requests/admin/enterprises_spec.rb new file mode 100644 index 0000000000..19edb6d772 --- /dev/null +++ b/spec/requests/admin/enterprises_spec.rb @@ -0,0 +1,122 @@ +require "spec_helper" + +feature %q{ + As an administration + I want manage enterprises +} do + include AuthenticationWorkflow + include WebHelper + + + scenario "listing enterprises" do + e = create(:enterprise) + + login_to_admin_section + click_link 'Enterprises' + + page.should have_content e.name + end + + scenario "viewing an enterprise" do + e = create(:enterprise) + + login_to_admin_section + click_link 'Enterprises' + click_link e.name + + page.should have_content e.name + end + + scenario "creating a new enterprise" do + login_to_admin_section + + click_link 'Enterprises' + click_link 'New Enterprise' + + fill_in 'enterprise_name', :with => 'Eaterprises' + fill_in 'enterprise_description', :with => 'Connecting farmers and eaters' + fill_in 'enterprise_long_description', :with => 'Zombie ipsum reversus ab viral inferno, nam rick grimes malum cerebro.' + + uncheck 'enterprise_is_primary_producer' + check 'enterprise_is_distributor' + + fill_in 'enterprise_contact', :with => 'Kirsten or Ren' + fill_in 'enterprise_phone', :with => '0413 897 321' + fill_in 'enterprise_email', :with => 'info@eaterprises.com.au' + fill_in 'enterprise_website', :with => 'http://eaterprises.com.au' + fill_in 'enterprise_twitter', :with => '@eaterprises' + fill_in 'enterprise_abn', :with => '09812309823' + fill_in 'enterprise_acn', :with => '' + + fill_in 'enterprise_address_attributes_address1', :with => '35 Ballantyne St' + fill_in 'enterprise_address_attributes_city', :with => 'Thornbury' + fill_in 'enterprise_address_attributes_zipcode', :with => '3072' + select('Australia', :from => 'enterprise_address_attributes_country_id') + select('Victoria', :from => 'enterprise_address_attributes_state_id') + + fill_in 'enterprise_pickup_times', :with => 'Thursday, 22nd Feb, 6 - 9 PM. Friday, 23nd Feb, 6 - 9 PM' + fill_in 'enterprise_next_collection_at', :with => 'Thursday, 22nd Feb, 6 - 9 PM' + + click_button 'Create' + + flash_message.should == 'Enterprise "Eaterprises" has been successfully created!' + end + + scenario "editing an existing enterprise" do + @enterprise = create(:enterprise) + + login_to_admin_section + + click_link 'Enterprises' + click_link 'Edit' + + fill_in 'enterprise_name', :with => 'Eaterprises' + fill_in 'enterprise_description', :with => 'Connecting farmers and eaters' + fill_in 'enterprise_long_description', :with => 'Zombie ipsum reversus ab viral inferno, nam rick grimes malum cerebro.' + + uncheck 'enterprise_is_primary_producer' + check 'enterprise_is_distributor' + + fill_in 'enterprise_contact', :with => 'Kirsten or Ren' + fill_in 'enterprise_phone', :with => '0413 897 321' + fill_in 'enterprise_email', :with => 'info@eaterprises.com.au' + fill_in 'enterprise_website', :with => 'http://eaterprises.com.au' + fill_in 'enterprise_twitter', :with => '@eaterprises' + fill_in 'enterprise_abn', :with => '09812309823' + fill_in 'enterprise_acn', :with => '' + + fill_in 'enterprise_address_attributes_address1', :with => '35 Ballantyne St' + fill_in 'enterprise_address_attributes_city', :with => 'Thornbury' + fill_in 'enterprise_address_attributes_zipcode', :with => '3072' + select('Australia', :from => 'enterprise_address_attributes_country_id') + select('Victoria', :from => 'enterprise_address_attributes_state_id') + + fill_in 'enterprise_pickup_times', :with => 'Thursday, 22nd Feb, 6 - 9 PM. Friday, 23nd Feb, 6 - 9 PM' + fill_in 'enterprise_next_collection_at', :with => 'Thursday, 22nd Feb, 6 - 9 PM' + + click_button 'Update' + + flash_message.should == 'Enterprise "Eaterprises" has been successfully updated!' + end + + + scenario "updating many distributor next collection times at once" do + # Given three distributors + 3.times { create(:distributor_enterprise) } + + # When I go to the enterprises page + login_to_admin_section + click_link 'Enterprises' + + # And I fill in some new collection times and save them + fill_in 'enterprise_set_enterprises_attributes_0_next_collection_at', :with => 'One' + fill_in 'enterprise_set_enterprises_attributes_1_next_collection_at', :with => 'Two' + fill_in 'enterprise_set_enterprises_attributes_2_next_collection_at', :with => 'Three' + click_button 'Update' + + # Then my times should have been saved + flash_message.should == 'Distributor collection times updated.' + Enterprise.is_distributor.map { |d| d.next_collection_at }.should == %w(One Two Three) + end + +end From b9a072b61ab5a0a2b8ff1b3fc67e2813c35aff95 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 31 Oct 2012 14:32:47 +1100 Subject: [PATCH 24/52] Make sure enterprises have a valid address after pickup address field has been removed --- ...0_remove_pickup_address_from_enterprises.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/db/migrate/20121028070200_remove_pickup_address_from_enterprises.rb b/db/migrate/20121028070200_remove_pickup_address_from_enterprises.rb index 2aa38222b4..d1c7fd0d56 100644 --- a/db/migrate/20121028070200_remove_pickup_address_from_enterprises.rb +++ b/db/migrate/20121028070200_remove_pickup_address_from_enterprises.rb @@ -1,5 +1,21 @@ class RemovePickupAddressFromEnterprises < ActiveRecord::Migration - def change + class Enterprise < ActiveRecord::Base; end + + def up + Enterprise.all.each do |e| + e.address_id ||= e.pickup_address_id + e.save! + end + remove_column :enterprises, :pickup_address_id end + + def down + add_column :enterprises, :pickup_address_id, :integer + + Enterprise.all.each do |e| + e.pickup_address_id ||= e.address_id + e.save! + end + end end From f03d9b66f05716dad0569aee3295b68c8ec88ce2 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 31 Oct 2012 15:17:05 +1100 Subject: [PATCH 25/52] Update spree_paypal_express to fail gracefully when setting address --- Gemfile.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 83954e36f3..21adee8e4d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -23,7 +23,7 @@ GIT GIT remote: git://github.com/eaterprises/spree_paypal_express.git - revision: ec7fd298f117fe6c598070dc0e7a38887b9bcbc4 + revision: 7dd46e6e549d82c4d603f685a7cb7fbb4af0d973 branch: 1-1-stable specs: spree_paypal_express (1.1.0) @@ -320,13 +320,13 @@ GEM libv8 (~> 3.3.10) thor (0.16.0) tilt (1.3.3) - treetop (1.4.11) + treetop (1.4.12) polyglot polyglot (>= 0.3.1) truncate_html (0.5.5) turn (0.8.3) ansi - tzinfo (0.3.33) + tzinfo (0.3.34) uglifier (1.2.4) execjs (>= 0.3.0) multi_json (>= 1.0.2) From a8f41788940830b4062fc0728a01ad00b27e0831 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 31 Oct 2012 16:33:38 +1100 Subject: [PATCH 26/52] Update orders controller for enterprises --- .../spree/orders_controller_decorator.rb | 2 +- spec/controllers/orders_controller_spec.rb | 22 +++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/app/controllers/spree/orders_controller_decorator.rb b/app/controllers/spree/orders_controller_decorator.rb index 6b9a708d1c..90b851fbaf 100644 --- a/app/controllers/spree/orders_controller_decorator.rb +++ b/app/controllers/spree/orders_controller_decorator.rb @@ -3,7 +3,7 @@ Spree::OrdersController.class_eval do after_filter :populate_variant_attributes, :only => :populate def populate_order_distributor - @distributor = params[:distributor_id].present? ? Distributor.find(params[:distributor_id]) : nil + @distributor = params[:distributor_id].present? ? Enterprise.is_distributor.find(params[:distributor_id]) : nil if populate_valid? @distributor order = current_order(true) diff --git a/spec/controllers/orders_controller_spec.rb b/spec/controllers/orders_controller_spec.rb index 772a3bfdf0..ded96678dd 100644 --- a/spec/controllers/orders_controller_spec.rb +++ b/spec/controllers/orders_controller_spec.rb @@ -11,7 +11,7 @@ describe Spree::OrdersController do context "adding the first product to the cart" do it "does not add the product if the user does not specify a distributor" do - create(:distributor) + create(:distributor_enterprise) p = create(:product) expect do @@ -20,8 +20,8 @@ describe Spree::OrdersController do end it "does not add the product if the user specifies a distributor that the product is not available at" do - distributor_product = create(:distributor) - distributor_no_product = create(:distributor) + distributor_product = create(:distributor_enterprise) + distributor_no_product = create(:distributor_enterprise) p = create(:product, :distributors => [distributor_product]) expect do @@ -30,8 +30,8 @@ describe Spree::OrdersController do end it "adds the product and sets the distributor even if the order has a different distributor set" do - distributor_product = create(:distributor) - distributor_no_product = create(:distributor) + distributor_product = create(:distributor_enterprise) + distributor_no_product = create(:distributor_enterprise) p = create(:product, :distributors => [distributor_product]) order = current_order(true) @@ -47,7 +47,7 @@ describe Spree::OrdersController do it "sets the order's distributor" do # Given a product in a distributor - d = create(:distributor) + d = create(:distributor_enterprise) p = create(:product, :distributors => [d]) # When we add the product to our cart @@ -61,7 +61,7 @@ describe Spree::OrdersController do context "adding a subsequent product to the cart" do before(:each) do # Given a product and a distributor - @distributor = create(:distributor) + @distributor = create(:distributor_enterprise) @product = create(:product, :distributors => [@distributor]) # And the product is in the cart @@ -72,7 +72,7 @@ describe Spree::OrdersController do it "does not add the product if the product is not available at the order's distributor" do # Given a product at another distributor - d2 = create(:distributor) + d2 = create(:distributor_enterprise) p2 = create(:product, :distributors => [d2]) # When I attempt to add the product to the cart @@ -85,7 +85,7 @@ describe Spree::OrdersController do it "does not add the product if the product is not available at the given distributor" do # Given a product at another distributor - d2 = create(:distributor) + d2 = create(:distributor_enterprise) p2 = create(:product, :distributors => [d2]) # When I attempt to add the product to the cart with a fake distributor_id @@ -98,7 +98,7 @@ describe Spree::OrdersController do it "does not add the product if the chosen distributor is different from the order's distributor" do # Given a product that's available at the chosen distributor and another distributor - d2 = create(:distributor) + d2 = create(:distributor_enterprise) p2 = create(:product, :distributors => [@distributor, d2]) # When I attempt to add the product to the cart with the alternate distributor @@ -112,7 +112,7 @@ describe Spree::OrdersController do context "adding a group buy product to the cart" do it "sets a variant attribute for the max quantity" do - distributor_product = create(:distributor) + distributor_product = create(:distributor_enterprise) p = create(:product, :distributors => [distributor_product], :group_buy => true) order = current_order(true) From 9d5c127f0db214298ff8a6758d5580a74cb4faf8 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 31 Oct 2012 16:47:01 +1100 Subject: [PATCH 27/52] Add enterprises controller that supercedes suppliers and distributors controllers. Add to cart consumer spec passes. --- .../stylesheets/store/openfoodweb.css.scss | 4 +- app/controllers/application_controller.rb | 4 +- app/controllers/enterprises_controller.rb | 43 ++++++++++ app/views/enterprises/index.html.haml | 12 +++ app/views/enterprises/show.html.haml | 7 ++ app/views/enterprises/suppliers.html.haml | 12 +++ .../spree/products/_source_sidebar.html.haml | 8 +- config/routes.rb | 11 +++ db/schema.rb | 4 +- .../enterprises_controller_spec.rb | 83 +++++++++++++++++++ spec/requests/consumer/add_to_cart_spec.rb | 28 +++---- 11 files changed, 192 insertions(+), 24 deletions(-) create mode 100644 app/controllers/enterprises_controller.rb create mode 100644 app/views/enterprises/index.html.haml create mode 100644 app/views/enterprises/show.html.haml create mode 100644 app/views/enterprises/suppliers.html.haml create mode 100644 spec/controllers/enterprises_controller_spec.rb diff --git a/app/assets/stylesheets/store/openfoodweb.css.scss b/app/assets/stylesheets/store/openfoodweb.css.scss index a3cb0a29f1..d183ffd10b 100644 --- a/app/assets/stylesheets/store/openfoodweb.css.scss +++ b/app/assets/stylesheets/store/openfoodweb.css.scss @@ -128,8 +128,8 @@ ul.product-listing { } -/* Supplier and distributor description */ -.supplier-description, .distributor-description { +/* Enterprise description */ +.enterprise-description { margin-bottom: 2em; } diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 73238eaef2..1bfb4a7d42 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -11,8 +11,8 @@ class ApplicationController < ActionController::Base def load_data_for_sidebar - @suppliers = Supplier.all - @distributors = Distributor.with_active_products_on_hand.by_name + @suppliers = Enterprise.is_supplier.all + @distributors = Enterprise.is_distributor.with_distributed_active_products_on_hand.by_name end end diff --git a/app/controllers/enterprises_controller.rb b/app/controllers/enterprises_controller.rb new file mode 100644 index 0000000000..3528746df4 --- /dev/null +++ b/app/controllers/enterprises_controller.rb @@ -0,0 +1,43 @@ +class EnterprisesController < BaseController + def index + @enterprises = Enterprise.all + end + + def suppliers + @suppliers = Enterprise.is_supplier + end + + def show + options = {:enterprise_id => params[:id]} + options.merge(params.reject { |k,v| k == :id }) + + @enterprise = Enterprise.find params[:id] + + @searcher = Spree::Config.searcher_class.new(options) + @products = @searcher.retrieve_products + end + + def select_distributor + distributor = Enterprise.is_distributor.find params[:id] + + order = current_order(true) + + if order.can_change_distributor? + order.distributor = distributor + order.save! + end + + redirect_to distributor + end + + def deselect_distributor + order = current_order(true) + + if order.can_change_distributor? + order.distributor = nil + order.save! + end + + redirect_to root_path + end +end diff --git a/app/views/enterprises/index.html.haml b/app/views/enterprises/index.html.haml new file mode 100644 index 0000000000..a1e07dfc04 --- /dev/null +++ b/app/views/enterprises/index.html.haml @@ -0,0 +1,12 @@ +- content_for :sidebar do + %div{'data-hook' => "homepage_sidebar_navigation"} + = render 'spree/sidebar' + + +%h1 Enterprises + += cms_page_content(:content, Cms::Page.find_by_full_path('/enterprises')) + +%ul.enterprises + - @enterprises.each do |enterprise| + %li= link_to enterprise.name, enterprise diff --git a/app/views/enterprises/show.html.haml b/app/views/enterprises/show.html.haml new file mode 100644 index 0000000000..b5f8284b33 --- /dev/null +++ b/app/views/enterprises/show.html.haml @@ -0,0 +1,7 @@ +%h2= @enterprise.name + +.enterprise-description= @enterprise.long_description.andand.html_safe + +%h3 Available Now + += render :template => 'spree/products/index' diff --git a/app/views/enterprises/suppliers.html.haml b/app/views/enterprises/suppliers.html.haml new file mode 100644 index 0000000000..c67a7bc987 --- /dev/null +++ b/app/views/enterprises/suppliers.html.haml @@ -0,0 +1,12 @@ +- content_for :sidebar do + %div{'data-hook' => "homepage_sidebar_navigation"} + = render 'spree/sidebar' + + +%h1 Suppliers + += cms_page_content(:content, Cms::Page.find_by_full_path('/enterprises/suppliers')) + +%ul.enterprises + - @suppliers.each do |supplier| + %li= link_to supplier.name, supplier diff --git a/app/views/spree/products/_source_sidebar.html.haml b/app/views/spree/products/_source_sidebar.html.haml index 88e1ed1cf5..bd25db53a0 100644 --- a/app/views/spree/products/_source_sidebar.html.haml +++ b/app/views/spree/products/_source_sidebar.html.haml @@ -2,9 +2,9 @@ %h6.filter_name Shop by Supplier %ul.filter_choices - @suppliers.each do |supplier| - - if supplier.has_products_on_hand? + - if supplier.has_supplied_products_on_hand? %li.nowrap= link_to supplier.name, [main_app, supplier] - = button_to 'Browse All Suppliers', main_app.suppliers_path, :method => :get + = button_to 'Browse All Suppliers', main_app.suppliers_enterprises_path, :method => :get %h6.filter_name Shop by Distributor %ul.filter_choices @@ -12,10 +12,10 @@ - @distributors.each do |distributor| %li.nowrap - if order.nil? || order.can_change_distributor? - = link_to distributor.name, main_app.select_distributor_path(distributor) + = link_to distributor.name, main_app.select_distributor_enterprise_path(distributor) - elsif order.distributor == distributor = link_to distributor.name, [main_app, distributor] - else %span.inactive= distributor.name - if current_distributor && order.can_change_distributor? - = button_to 'Browse All Distributors', main_app.deselect_distributors_path, :method => :get + = button_to 'Browse All Distributors', main_app.deselect_distributor_enterprises_path, :method => :get diff --git a/config/routes.rb b/config/routes.rb index 7fb03b6950..67bd610c69 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,15 @@ Openfoodweb::Application.routes.draw do root :to => 'spree/home#index' + resources :enterprises do + get :suppliers, :on => :collection + get :select_distributor, :on => :member + get :deselect_distributor, :on => :collection + end + + # Deprecated resources :suppliers + # Deprecated resources :distributors do get :select, :on => :member get :deselect, :on => :collection @@ -11,9 +19,12 @@ Openfoodweb::Application.routes.draw do resources :enterprises do post :bulk_update, :on => :collection, :as => :bulk_update end + + # Deprecated resources :distributors do post :bulk_update, :on => :collection, :as => :bulk_update end + # Deprecated resources :suppliers end diff --git a/db/schema.rb b/db/schema.rb index 6267fefdd9..c03b5d21f4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -282,8 +282,8 @@ ActiveRecord::Schema.define(:version => 20121028070200) do t.string "start_year" t.string "issue_number" t.integer "address_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.string "gateway_customer_profile_id" t.string "gateway_payment_profile_id" end diff --git a/spec/controllers/enterprises_controller_spec.rb b/spec/controllers/enterprises_controller_spec.rb new file mode 100644 index 0000000000..1554984dfb --- /dev/null +++ b/spec/controllers/enterprises_controller_spec.rb @@ -0,0 +1,83 @@ +require 'spec_helper' +require 'spree/core/current_order' + +describe EnterprisesController do + include Spree::Core::CurrentOrder + + before :each do + stub!(:before_save_new_order) + stub!(:after_save_new_order) + + create(:itemwise_shipping_method) + end + + it "displays suppliers" do + s = create(:supplier_enterprise) + d = create(:distributor_enterprise) + + spree_get :suppliers + + assigns(:suppliers).should == [s] + end + + it "selects distributors" do + d = create(:distributor_enterprise) + + spree_get :select_distributor, :id => d.id + response.should be_redirect + + order = current_order(false) + order.distributor.should == d + end + + it "deselects distributors" do + d = create(:distributor_enterprise) + order = current_order(true) + order.distributor = d + order.save! + + spree_get :deselect_distributor + response.should be_redirect + + order.reload + order.distributor.should be_nil + end + + context "when a product has been added to the cart" do + it "does not allow selecting another distributor" do + # Given some distributors and an order with a product + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) + p = create(:product, :distributors => [d1]) + o = current_order(true) + + o.distributor = d1 + o.save! + o.add_variant(p.master, 1) + + # When I attempt to select a distributor + spree_get :select_distributor, :id => d2.id + + # Then my distributor should remain unchanged + o.reload + o.distributor.should == d1 + end + + it "does not allow deselecting distributors" do + # Given a distributor and an order with a product + d = create(:distributor_enterprise) + p = create(:product, :distributors => [d]) + o = current_order(true) + o.distributor = d + o.save! + o.add_variant(p.master, 1) + + # When I attempt to deselect the distributor + spree_get :deselect_distributor + + # Then my distributor should remain unchanged + o.reload + o.distributor.should == d + end + end +end diff --git a/spec/requests/consumer/add_to_cart_spec.rb b/spec/requests/consumer/add_to_cart_spec.rb index d528866647..de9ccce8da 100644 --- a/spec/requests/consumer/add_to_cart_spec.rb +++ b/spec/requests/consumer/add_to_cart_spec.rb @@ -10,8 +10,8 @@ feature %q{ scenario "adding a product to the cart with no distributor chosen" do # Given a product and some distributors - d1 = create(:distributor) - d2 = create(:distributor) + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) p = create(:product, :distributors => [d1]) create(:product, :distributors => [d2]) @@ -30,8 +30,8 @@ feature %q{ create(:itemwise_shipping_method) # Given a product, some distributors and a defined shipping cost - d1 = create(:distributor) - d2 = create(:distributor) + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) create(:product, :distributors => [d2]) p = create(:product, :price => 12.34) create(:product_distribution, :product => p, :distributor => d1, :shipping_method => create(:shipping_method)) @@ -67,8 +67,8 @@ feature %q{ it "does not allow the user to change distributor after a product has been added to the cart" do # Given a product and some distributors - d1 = create(:distributor) - d2 = create(:distributor) + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) p = create(:product, :distributors => [d1]) # When I add a product to my cart (which sets my distributor) @@ -87,7 +87,7 @@ feature %q{ context "adding a subsequent product to the cart" do it "does not allow the user to choose a distributor" do # Given a product under a distributor - d = create(:distributor) + d = create(:distributor_enterprise) p = create(:product, :distributors => [d]) # And a product in my cart @@ -103,8 +103,8 @@ feature %q{ it "does not allow the user to add a product from another distributor" do # Given two products, each at a different distributor - d1 = create(:distributor) - d2 = create(:distributor) + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) p1 = create(:product, :distributors => [d1]) p2 = create(:product, :distributors => [d2]) @@ -123,7 +123,7 @@ feature %q{ it "adds products with valid distributors" do # Given two products, each at the same distributor - d = create(:distributor) + d = create(:distributor_enterprise) p1 = create(:product, :distributors => [d]) p2 = create(:product, :distributors => [d]) @@ -146,7 +146,7 @@ feature %q{ context "group buys" do scenario "adding a product to the cart for a group buy" do # Given a group buy product and a distributor - d = create(:distributor) + d = create(:distributor_enterprise) p = create(:product, :distributors => [d], :group_buy => true) # When I add the item to my cart @@ -166,7 +166,7 @@ feature %q{ scenario "adding a product with variants to the cart for a group buy" do # Given a group buy product with variants and a distributor - d = create(:distributor) + d = create(:distributor_enterprise) p = create(:product, :distributors => [d], :group_buy => true) create(:variant, :product => p) @@ -187,7 +187,7 @@ feature %q{ scenario "adding a product to cart that is not a group buy does not show max quantity field" do # Given a group buy product and a distributor - d = create(:distributor) + d = create(:distributor_enterprise) p = create(:product, :distributors => [d], :group_buy => false) # When I view the add to cart form, there should not be a max quantity field @@ -198,7 +198,7 @@ feature %q{ scenario "adding a product with a max quantity less than quantity results in max_quantity==quantity" do # Given a group buy product and a distributor - d = create(:distributor) + d = create(:distributor_enterprise) p = create(:product, :distributors => [d], :group_buy => true) # When I add the item to my cart From 309179096bb137a30a204e02be28a74df17fe58a Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 31 Oct 2012 16:56:35 +1100 Subject: [PATCH 28/52] Update supplier_id and distributor_id to point to newly created enterprises --- ...suppliers_and_distributors_into_enterprises.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/db/migrate/20121025012233_combine_suppliers_and_distributors_into_enterprises.rb b/db/migrate/20121025012233_combine_suppliers_and_distributors_into_enterprises.rb index e9c4a18c09..1d02ba1595 100644 --- a/db/migrate/20121025012233_combine_suppliers_and_distributors_into_enterprises.rb +++ b/db/migrate/20121025012233_combine_suppliers_and_distributors_into_enterprises.rb @@ -2,6 +2,10 @@ class CombineSuppliersAndDistributorsIntoEnterprises < ActiveRecord::Migration class Supplier < ActiveRecord::Base; end class Distributor < ActiveRecord::Base; end class Enterprise < ActiveRecord::Base; end + class ProductDistribution < ActiveRecord::Base; end + class Spree::Product < ActiveRecord::Base; end + class Spree::Order < ActiveRecord::Base; end + def up # Create enterprises table @@ -30,7 +34,10 @@ class CombineSuppliersAndDistributorsIntoEnterprises < ActiveRecord::Migration attrs = s.attributes attrs.reject! { |k| k == 'id' } attrs.merge! is_primary_producer: true, is_distributor: false - Enterprise.create! attrs + e = Enterprise.create! attrs + + # Update supplier_id on product to point at the new enterprise + Spree::Product.update_all("supplier_id=#{e.id}", "supplier_id=#{s.id}") end # Copy distributors to enterprises table with distributor flag set @@ -39,7 +46,11 @@ class CombineSuppliersAndDistributorsIntoEnterprises < ActiveRecord::Migration attrs['website'] = attrs['url'] attrs.reject! { |k| ['id', 'url'].include? k } attrs.merge! is_primary_producer: false, is_distributor: true - Enterprise.create! attrs + e = Enterprise.create! attrs + + # Update distributor_id on product distribution and order to point at the new enterprise + ProductDistribution.update_all("distributor_id=#{e.id}", "distributor_id=#{d.id}") + Spree::Order.update_all("distributor_id=#{e.id}", "distributor_id=#{d.id}") end end From 1b89331aa4a8ba05ae88cb253356054239199ee8 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 31 Oct 2012 17:09:26 +1100 Subject: [PATCH 29/52] Update capybara and selenium-webdriver for compatibility with firefox 16 --- Gemfile.lock | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index a79ae3a26e..da143e47ed 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -129,7 +129,7 @@ GEM i18n (~> 0.6) multi_json (~> 1.0) acts_as_list (0.1.4) - addressable (2.2.8) + addressable (2.3.2) andand (1.3.3) ansi (1.4.2) arel (3.0.2) @@ -145,15 +145,15 @@ GEM multi_json (~> 1.0) builder (3.0.4) cancan (1.6.7) - capybara (1.1.2) + capybara (1.1.3) mime-types (>= 1.16) nokogiri (>= 1.3.3) rack (>= 1.0.0) rack-test (>= 0.5.4) selenium-webdriver (~> 2.0) xpath (~> 0.1.4) - childprocess (0.3.2) - ffi (~> 1.0.6) + childprocess (0.3.6) + ffi (~> 1.0, >= 1.0.6) cocaine (0.4.2) coderay (1.0.7) coffee-rails (3.2.2) @@ -196,7 +196,7 @@ GEM faker (1.0.1) i18n (~> 0.4) ffaker (1.12.1) - ffi (1.0.11) + ffi (1.1.5) haml (3.1.6) highline (1.6.11) hike (1.2.1) @@ -215,7 +215,7 @@ GEM railties (>= 3.0.0) kgio (2.7.4) libv8 (3.3.10.4) - libwebsocket (0.1.3) + libwebsocket (0.1.5) addressable mail (2.4.4) i18n (>= 0.4.0) @@ -295,15 +295,14 @@ GEM activesupport (>= 3.0) railties (>= 3.0) rspec (~> 2.10.0) - rubyzip (0.9.8) + rubyzip (0.9.9) sass (3.1.19) sass-rails (3.2.5) railties (~> 3.2.0) sass (>= 3.1.10) tilt (~> 1.3) - selenium-webdriver (2.22.2) + selenium-webdriver (2.25.0) childprocess (>= 0.2.5) - ffi (~> 1.0) libwebsocket (~> 0.1.3) multi_json (~> 1.0) rubyzip From 7dd42b7feb124f1a5e9e7a7f4417e9087f2a9a1c Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 31 Oct 2012 17:10:21 +1100 Subject: [PATCH 30/52] Fix use of pickup_address and url fields with enterprises, checkout spec passes --- .../spree/checkout/_distributor.html.haml | 5 ++-- .../spree/order_mailer/confirm_email.text.erb | 2 +- spec/requests/consumer/checkout_spec.rb | 26 +++++++++---------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/app/views/spree/checkout/_distributor.html.haml b/app/views/spree/checkout/_distributor.html.haml index 9460e3a94b..eaaf31217b 100644 --- a/app/views/spree/checkout/_distributor.html.haml +++ b/app/views/spree/checkout/_distributor.html.haml @@ -5,7 +5,7 @@ %p %strong Address: %br/ - = render 'spree/shared/address', :address => @order.distributor.pickup_address + = render 'spree/shared/address', :address => @order.distributor.address %p %strong Next collection time: %br/ @@ -23,5 +23,4 @@ %br/ = "Email: #{@order.distributor.email}" %p= @order.distributor.description - %p= link_to @order.distributor.url, @order.distributor.url if @order.distributor.url - + %p= link_to @order.distributor.website, @order.distributor.website if @order.distributor.website diff --git a/app/views/spree/order_mailer/confirm_email.text.erb b/app/views/spree/order_mailer/confirm_email.text.erb index db3022529e..1502123479 100644 --- a/app/views/spree/order_mailer/confirm_email.text.erb +++ b/app/views/spree/order_mailer/confirm_email.text.erb @@ -20,7 +20,7 @@ Delivery Details ============================================================ Address: <%= @order.distributor.name %> - <% address = @order.distributor.pickup_address %> + <% address = @order.distributor.address %> <%= address.address1 %> <%= ",\n #{address.address2}" unless address.address2.blank? %> <%= [address.city, address.state_text, address.zipcode, address.country.name].compact.join ', ' %> Colection time: diff --git a/spec/requests/consumer/checkout_spec.rb b/spec/requests/consumer/checkout_spec.rb index cfaaee8bee..2a0bce8c5d 100644 --- a/spec/requests/consumer/checkout_spec.rb +++ b/spec/requests/consumer/checkout_spec.rb @@ -9,13 +9,13 @@ feature %q{ include WebHelper background do - @distributor = create(:distributor, :name => 'Edible garden', - :pickup_address => create(:address, - :address1 => '12 Bungee Rd', - :city => 'Carion', - :zipcode => 3056, - :state => Spree::State.find_by_name('Victoria'), - :country => Spree::Country.find_by_name('Australia')), + @distributor = create(:distributor_enterprise, :name => 'Edible garden', + :address => create(:address, + :address1 => '12 Bungee Rd', + :city => 'Carion', + :zipcode => 3056, + :state => Spree::State.find_by_name('Victoria'), + :country => Spree::Country.find_by_name('Australia')), :pickup_times => 'Tuesday, 4 PM') @shipping_method_1 = create(:shipping_method, :name => 'Shipping Method One') @@ -94,18 +94,18 @@ feature %q{ # Distributor details should be displayed within('fieldset#shipping') do [@distributor.name, - @distributor.pickup_address.address1, - @distributor.pickup_address.city, - @distributor.pickup_address.zipcode, - @distributor.pickup_address.state_text, - @distributor.pickup_address.country.name, + @distributor.address.address1, + @distributor.address.city, + @distributor.address.zipcode, + @distributor.address.state_text, + @distributor.address.country.name, @distributor.pickup_times, @distributor.next_collection_at, @distributor.contact, @distributor.phone, @distributor.email, @distributor.description, - @distributor.url].each do |value| + @distributor.website].each do |value| page.should have_content value end From f8c2c8bbedeb8c567a7a3c31896c0d7838e06551 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 1 Nov 2012 08:02:11 +1100 Subject: [PATCH 31/52] Convert group buy unit size field from string to float --- ...roup_buy_unit_size_from_string_to_float.rb | 21 +++ db/schema.rb | 160 +++++++++--------- spec/requests/admin/product_spec.rb | 4 +- 3 files changed, 103 insertions(+), 82 deletions(-) create mode 100644 db/migrate/20121031203807_change_group_buy_unit_size_from_string_to_float.rb diff --git a/db/migrate/20121031203807_change_group_buy_unit_size_from_string_to_float.rb b/db/migrate/20121031203807_change_group_buy_unit_size_from_string_to_float.rb new file mode 100644 index 0000000000..44caf3beaa --- /dev/null +++ b/db/migrate/20121031203807_change_group_buy_unit_size_from_string_to_float.rb @@ -0,0 +1,21 @@ +class ChangeGroupBuyUnitSizeFromStringToFloat < ActiveRecord::Migration + class Spree::Product < ActiveRecord::Base; end + + def up + add_column :spree_products, :group_buy_unit_size_f, :float + Spree::Product.reset_column_information + + Spree::Product.all.each do |product| + product.group_buy_unit_size_f = product.group_buy_unit_size.to_f + product.save! + end + + remove_column :spree_products, :group_buy_unit_size + rename_column :spree_products, :group_buy_unit_size_f, :group_buy_unit_size + end + + def down + change_column :spree_products, :group_buy_unit_size, :string + end + +end diff --git a/db/schema.rb b/db/schema.rb index 09dc89c12e..4a0fe39ee3 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 => 20121018002907) do +ActiveRecord::Schema.define(:version => 20121031203807) do create_table "cms_blocks", :force => true do |t| t.integer "page_id", :null => false @@ -140,8 +140,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "abn" t.string "acn" t.string "description" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" t.integer "pickup_address_id" t.string "next_collection_at" t.text "long_description" @@ -183,8 +183,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "alternative_phone" t.integer "state_id" t.integer "country_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" t.string "company" end @@ -193,12 +193,12 @@ ActiveRecord::Schema.define(:version => 20121018002907) do create_table "spree_adjustments", :force => true do |t| t.integer "source_id" - t.decimal "amount", :precision => 8, :scale => 2 + t.decimal "amount", :precision => 8, :scale => 2, :default => 0.0 t.string "label" t.string "source_type" t.integer "adjustable_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" t.boolean "mandatory" t.boolean "locked" t.integer "originator_id" @@ -230,15 +230,15 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "type" t.integer "calculable_id", :null => false t.string "calculable_type", :null => false - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" end create_table "spree_configurations", :force => true do |t| t.string "name" t.string "type", :limit => 50 - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" end add_index "spree_configurations", ["name", "type"], :name => "index_configurations_on_name_and_type" @@ -276,8 +276,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "environment", :default => "development" t.string "server", :default => "test" t.boolean "test_mode", :default => true - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" end create_table "spree_inventory_units", :force => true do |t| @@ -285,8 +285,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "state" t.integer "variant_id" t.integer "order_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" t.integer "shipment_id" t.integer "return_authorization_id" end @@ -300,8 +300,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.integer "variant_id" t.integer "quantity", :null => false t.decimal "price", :precision => 8, :scale => 2, :null => false - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" t.integer "max_quantity" t.integer "shipping_method_id" end @@ -313,22 +313,22 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.integer "source_id" t.string "source_type" t.text "details" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" end create_table "spree_mail_methods", :force => true do |t| t.string "environment" t.boolean "active", :default => true - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" end create_table "spree_option_types", :force => true do |t| t.string "name", :limit => 100 t.string "presentation", :limit => 100 - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" t.integer "position", :default => 0, :null => false end @@ -342,8 +342,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "name" t.string "presentation" t.integer "option_type_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" end create_table "spree_option_values_variants", :id => false, :force => true do |t| @@ -362,8 +362,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.decimal "adjustment_total", :precision => 8, :scale => 2, :default => 0.0, :null => false t.decimal "credit_total", :precision => 8, :scale => 2, :default => 0.0, :null => false t.integer "user_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" t.datetime "completed_at" t.integer "bill_address_id" t.integer "ship_address_id" @@ -384,8 +384,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.text "description" t.boolean "active", :default => true t.string "environment", :default => "development" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" t.datetime "deleted_at" t.string "display_on" end @@ -393,8 +393,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do create_table "spree_payments", :force => true do |t| t.decimal "amount", :precision => 8, :scale => 2, :default => 0.0, :null => false t.integer "order_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" t.integer "source_id" t.string "source_type" t.integer "payment_method_id" @@ -423,8 +423,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.integer "owner_id" t.string "owner_type" t.text "value" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" t.string "key" t.string "value_type" end @@ -449,16 +449,16 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.integer "position" t.integer "product_id" t.integer "option_type_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" end create_table "spree_product_properties", :force => true do |t| t.string "value" t.integer "product_id" t.integer "property_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" end add_index "spree_product_properties", ["product_id"], :name => "index_product_properties_on_product_id" @@ -482,12 +482,12 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "meta_keywords" t.integer "tax_category_id" t.integer "shipping_category_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" t.integer "count_on_hand", :default => 0, :null => false t.integer "supplier_id" t.boolean "group_buy" - t.string "group_buy_unit_size" + t.float "group_buy_unit_size" end add_index "spree_products", ["available_on"], :name => "index_products_on_available_on" @@ -528,8 +528,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.integer "user_id" t.integer "product_group_id" t.string "type" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" end add_index "spree_promotion_rules", ["product_group_id"], :name => "index_promotion_rules_on_product_group_id" @@ -546,8 +546,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do create_table "spree_properties", :force => true do |t| t.string "name" t.string "presentation", :null => false - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" end create_table "spree_properties_prototypes", :id => false, :force => true do |t| @@ -557,8 +557,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do create_table "spree_prototypes", :force => true do |t| t.string "name" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" end create_table "spree_return_authorizations", :force => true do |t| @@ -567,8 +567,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.decimal "amount", :precision => 8, :scale => 2, :default => 0.0, :null => false t.integer "order_id" t.text "reason" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" end create_table "spree_roles", :force => true do |t| @@ -591,8 +591,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.integer "order_id" t.integer "shipping_method_id" t.integer "address_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" t.string "state" end @@ -600,15 +600,15 @@ ActiveRecord::Schema.define(:version => 20121018002907) 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" + t.datetime "updated_at" end create_table "spree_shipping_methods", :force => true do |t| t.string "name" t.integer "zone_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" t.string "display_on" t.integer "shipping_category_id" t.boolean "match_none" @@ -624,8 +624,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.integer "transaction_id" t.integer "customer_id" t.string "payment_type" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" end create_table "spree_state_changes", :force => true do |t| @@ -633,8 +633,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "previous_state" t.integer "stateful_id" t.integer "user_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" t.string "stateful_type" t.string "next_state" end @@ -648,8 +648,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do create_table "spree_tax_categories", :force => true do |t| t.string "name" t.string "description" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" t.boolean "is_default", :default => false t.datetime "deleted_at" end @@ -658,15 +658,15 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.decimal "amount", :precision => 8, :scale => 5 t.integer "zone_id" t.integer "tax_category_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" t.boolean "included_in_price", :default => false end create_table "spree_taxonomies", :force => true do |t| t.string "name", :null => false - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" end create_table "spree_taxons", :force => true do |t| @@ -675,8 +675,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "name", :null => false t.string "permalink" t.integer "taxonomy_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" t.integer "lft" t.integer "rgt" t.string "icon_file_name" @@ -694,8 +694,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.integer "permissable_id" t.string "permissable_type" t.string "token" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" end add_index "spree_tokenized_permissions", ["permissable_id", "permissable_type"], :name => "index_tokenized_name_and_type" @@ -704,8 +704,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "environment" t.string "analytics_id" t.boolean "active", :default => true - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" end create_table "spree_users", :force => true do |t| @@ -726,8 +726,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "login" t.integer "ship_address_id" t.integer "bill_address_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" t.string "authentication_token" t.string "unlock_token" t.datetime "locked_at" @@ -750,7 +750,7 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.boolean "is_master", :default => false t.integer "product_id" t.integer "count_on_hand", :default => 0, :null => false - t.decimal "cost_price", :precision => 8, :scale => 2 + t.decimal "cost_price", :precision => 8, :scale => 2, :default => 0.0 t.integer "position" end @@ -760,15 +760,15 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.integer "zoneable_id" t.string "zoneable_type" t.integer "zone_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" end create_table "spree_zones", :force => true do |t| t.string "name" t.string "description" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" t.boolean "default_tax", :default => false t.integer "zone_members_count", :default => 0 end @@ -779,8 +779,8 @@ ActiveRecord::Schema.define(:version => 20121018002907) do t.string "email" t.string "twitter" t.string "website" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at" + t.datetime "updated_at" t.integer "address_id" t.text "long_description" end diff --git a/spec/requests/admin/product_spec.rb b/spec/requests/admin/product_spec.rb index 37d9935dd2..bb707ef681 100644 --- a/spec/requests/admin/product_spec.rb +++ b/spec/requests/admin/product_spec.rb @@ -49,14 +49,14 @@ feature %q{ fill_in 'product_price', :with => '19.99' select 'New supplier', :from => 'product_supplier_id' choose 'product_group_buy_1' - fill_in 'Group buy unit size', :with => '10 kg' + fill_in 'Group buy unit size', :with => '10' click_button 'Create' flash_message.should == 'Product "A new product !!!" has been successfully created!' product = Spree::Product.find_by_name('A new product !!!') product.group_buy.should be_true - product.group_buy_unit_size.should == '10 kg' + product.group_buy_unit_size.should == 10.0 end end From fb9f59213fddf9259576169328b0a0afbbcb13b0 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 1 Nov 2012 08:15:50 +1100 Subject: [PATCH 32/52] Distributors, product, suppliers and taxonomy specs pass --- spec/requests/consumer/distributors_spec.rb | 26 ++++++++++----------- spec/requests/consumer/product_spec.rb | 4 ++-- spec/requests/consumer/suppliers_spec.rb | 16 ++++++------- spec/requests/consumer/taxonomy_spec.rb | 4 ++-- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/spec/requests/consumer/distributors_spec.rb b/spec/requests/consumer/distributors_spec.rb index 1139ace1cd..aaaf78e618 100644 --- a/spec/requests/consumer/distributors_spec.rb +++ b/spec/requests/consumer/distributors_spec.rb @@ -10,9 +10,9 @@ feature %q{ scenario "viewing a list of distributors" do # Given some distributors - d1 = create(:distributor) - d2 = create(:distributor) - d3 = create(:distributor) + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) + d3 = create(:distributor_enterprise) # And some of those distributors have a product create(:product, :distributors => [d1, d2]) @@ -29,7 +29,7 @@ feature %q{ context "when a distributor is selected" do it "displays the distributor's details" do # Given a distributor with a product - d = create(:distributor, :name => 'Melb Uni Co-op', :description => '

Hello, world!

') + d = create(:distributor_enterprise, :name => 'Melb Uni Co-op', :description => '

Hello, world!

') create(:product, :distributors => [d]) # When I select the distributor @@ -40,12 +40,12 @@ feature %q{ page.should have_selector 'h2', :text => 'Melb Uni Co-op' # And I should see the distributor's long description - page.should have_selector 'div.distributor-description', :text => 'Hello, world!' + page.should have_selector 'div.enterprise-description', :text => 'Hello, world!' end it "displays the distributor's name on the home page" do # Given a distributor with a product - d = create(:distributor, :name => 'Melb Uni Co-op', :description => '

Hello, world!

') + d = create(:distributor_enterprise, :name => 'Melb Uni Co-op', :description => '

Hello, world!

') create(:product, :distributors => [d]) # When I select the distributor @@ -63,8 +63,8 @@ feature %q{ taxonomy = Spree::Taxonomy.find_by_name('Products') || create(:taxonomy, :name => 'Products') taxonomy_root = taxonomy.root taxon = create(:taxon, :name => 'Taxon one', :parent_id => taxonomy_root.id) - d1 = create(:distributor) - d2 = create(:distributor) + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) p1 = create(:product, :distributors => [d1], :taxons => [taxon]) p2 = create(:product, :distributors => [d2], :taxons => [taxon]) @@ -90,7 +90,7 @@ feature %q{ it "allows the user to leave the distributor" do # Given a distributor with a product - d = create(:distributor, :name => 'Melb Uni Co-op') + d = create(:distributor_enterprise, :name => 'Melb Uni Co-op') create(:product, :distributors => [d]) # When I select the distributor and then leave it @@ -105,7 +105,7 @@ feature %q{ context "viewing a product, it provides a choice of distributor when adding to cart" do it "works when no distributor is chosen" do # Given a distributor and a product under it - distributor = create(:distributor) + distributor = create(:distributor_enterprise) product = create(:product, :distributors => [distributor]) # When we view the product @@ -118,7 +118,7 @@ feature %q{ it "displays the local distributor as the default choice when available for the current product" do # Given a distributor and a product under it - distributor = create(:distributor) + distributor = create(:distributor_enterprise) product = create(:product, :distributors => [distributor]) # When we select the distributor and view the product @@ -132,8 +132,8 @@ feature %q{ it "works when viewing a product from a remote distributor" do # Given two distributors and our product under one - distributor_product = create(:distributor) - distributor_no_product = create(:distributor) + distributor_product = create(:distributor_enterprise) + distributor_no_product = create(:distributor_enterprise) product = create(:product, :distributors => [distributor_product]) create(:product, :distributors => [distributor_no_product]) diff --git a/spec/requests/consumer/product_spec.rb b/spec/requests/consumer/product_spec.rb index 97712d1e13..f86b3bb409 100644 --- a/spec/requests/consumer/product_spec.rb +++ b/spec/requests/consumer/product_spec.rb @@ -10,8 +10,8 @@ feature %q{ scenario "viewing a product shows its supplier and distributor" do # Given a product with a supplier and distributor - s = create(:supplier) - d = create(:distributor) + s = create(:supplier_enterprise) + d = create(:distributor_enterprise) p = create(:product, :supplier => s, :distributors => [d]) # When I view the product diff --git a/spec/requests/consumer/suppliers_spec.rb b/spec/requests/consumer/suppliers_spec.rb index 1c88ffd64f..ab1f90a493 100644 --- a/spec/requests/consumer/suppliers_spec.rb +++ b/spec/requests/consumer/suppliers_spec.rb @@ -10,9 +10,9 @@ feature %q{ scenario "viewing a list of suppliers in the sidebar" do # Given some suppliers - s1 = create(:supplier) - s2 = create(:supplier) - s3 = create(:supplier) + s1 = create(:supplier_enterprise) + s2 = create(:supplier_enterprise) + s3 = create(:supplier_enterprise) # And some of those suppliers have a product create(:product, :supplier => s1) @@ -29,9 +29,9 @@ feature %q{ scenario "viewing a list of all suppliers" do # Given some suppliers - s1 = create(:supplier) - s2 = create(:supplier) - s3 = create(:supplier) + s1 = create(:supplier_enterprise) + s2 = create(:supplier_enterprise) + s3 = create(:supplier_enterprise) # And some of those suppliers have a product create(:product, :supplier => s1) @@ -49,7 +49,7 @@ feature %q{ scenario "viewing products provided by a supplier" do # Given a supplier with a product - s = create(:supplier, :name => 'Murrnong', :long_description => "

Hello, world!

") + s = create(:supplier_enterprise, :name => 'Murrnong', :long_description => "

Hello, world!

") p = create(:product, :supplier => s) # When I select the supplier @@ -58,7 +58,7 @@ feature %q{ # Then I should see the supplier details page.should have_selector 'h2', :text => s.name - page.should have_selector 'div.supplier-description', :text => 'Hello, world!' + page.should have_selector 'div.enterprise-description', :text => 'Hello, world!' # And I should see the product page.should have_content p.name diff --git a/spec/requests/consumer/taxonomy_spec.rb b/spec/requests/consumer/taxonomy_spec.rb index 2b606a9406..9a2190c1a0 100644 --- a/spec/requests/consumer/taxonomy_spec.rb +++ b/spec/requests/consumer/taxonomy_spec.rb @@ -40,8 +40,8 @@ feature %q{ taxon_two = create(:taxon, :name => 'Taxon two', :parent_id => taxonomy_root.id) taxon_three = create(:taxon, :name => 'Taxon three', :parent_id => taxonomy_root.id) - my_distributor = create(:distributor, :name => 'My Distributor') - other_distributor = create(:distributor, :name => 'Other Distributor') + my_distributor = create(:distributor_enterprise, :name => 'My Distributor') + other_distributor = create(:distributor_enterprise, :name => 'Other Distributor') 1.times { create(:product, :taxons => [taxon_one], :distributors => [other_distributor]) } 2.times { create(:product, :taxons => [taxon_two], :distributors => [other_distributor]) } From 58d227e76ad5f85a349cb7eae7467de98c13ae2f Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 1 Nov 2012 08:28:40 +1100 Subject: [PATCH 33/52] Home controller and product model specs pass --- spec/controllers/home_controller_spec.rb | 4 ++-- spec/models/product_spec.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/controllers/home_controller_spec.rb b/spec/controllers/home_controller_spec.rb index 9fcbd3dd2e..b52dcae435 100644 --- a/spec/controllers/home_controller_spec.rb +++ b/spec/controllers/home_controller_spec.rb @@ -11,8 +11,8 @@ describe Spree::HomeController do it "splits products by local/remote distributor when distributor is selected" do # Given two distributors with a product under each - d1 = create(:distributor) - d2 = create(:distributor) + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) p1 = create(:product, :distributors => [d1]) p2 = create(:product, :distributors => [d2]) diff --git a/spec/models/product_spec.rb b/spec/models/product_spec.rb index dd0d8490b9..cc74db8638 100644 --- a/spec/models/product_spec.rb +++ b/spec/models/product_spec.rb @@ -29,7 +29,7 @@ describe Spree::Product do end it "raises an error if distributor is not found" do - distributor = create(:distributor) + distributor = create(:distributor_enterprise) product = create(:product) expect do product.shipping_method_for_distributor(distributor) From d927906934de0de69733c87bb55d6947542c3cc4 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 1 Nov 2012 08:36:40 +1100 Subject: [PATCH 34/52] Update admin views, tests pass --- app/models/spree/product_decorator.rb | 2 +- app/views/spree/admin/products/_supplier_form.html.haml | 2 +- spec/requests/admin/product_spec.rb | 4 ++-- spec/requests/admin/shipping_methods_spec.rb | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 58a7c93346..e59f15f85b 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -23,7 +23,7 @@ Spree::Product.class_eval do # Build a product distribution for each distributor def build_product_distributions - Distributor.all.each do |distributor| + Enterprise.is_distributor.each do |distributor| unless self.product_distributions.find_by_distributor_id distributor.id self.product_distributions.build(:distributor => distributor) end diff --git a/app/views/spree/admin/products/_supplier_form.html.haml b/app/views/spree/admin/products/_supplier_form.html.haml index e4cd8280a7..9bfc0bec4a 100644 --- a/app/views/spree/admin/products/_supplier_form.html.haml +++ b/app/views/spree/admin/products/_supplier_form.html.haml @@ -1,5 +1,5 @@ = f.field_container :supplier do = f.label :supplier %br - = f.collection_select(:supplier_id, Supplier.all, :id, :name, :include_blank => true) + = f.collection_select(:supplier_id, Enterprise.is_supplier, :id, :name, :include_blank => true) = f.error_message_on :supplier diff --git a/spec/requests/admin/product_spec.rb b/spec/requests/admin/product_spec.rb index 37d9935dd2..fff5417726 100644 --- a/spec/requests/admin/product_spec.rb +++ b/spec/requests/admin/product_spec.rb @@ -8,8 +8,8 @@ feature %q{ include WebHelper background do - @supplier = create(:supplier, :name => 'New supplier') - @distributors = (1..3).map { create(:distributor) } + @supplier = create(:supplier_enterprise, :name => 'New supplier') + @distributors = (1..3).map { create(:distributor_enterprise) } @shipping_method = create(:shipping_method, :name => 'My shipping method') end diff --git a/spec/requests/admin/shipping_methods_spec.rb b/spec/requests/admin/shipping_methods_spec.rb index a72cff3e3a..d794e382c2 100644 --- a/spec/requests/admin/shipping_methods_spec.rb +++ b/spec/requests/admin/shipping_methods_spec.rb @@ -27,7 +27,7 @@ feature 'shipping methods' do scenario "deleting a shipping method referenced by a product distribution" do p = create(:product) - d = create(:distributor) + d = create(:distributor_enterprise) create(:product_distribution, product: p, distributor: d, shipping_method: @sm) visit_delete spree.admin_shipping_method_path(@sm) @@ -38,7 +38,7 @@ feature 'shipping methods' do scenario "deleting a shipping method referenced by a line item" do sm2 = create(:shipping_method) - d = create(:distributor) + d = create(:distributor_enterprise) p = create(:product) create(:product_distribution, product: p, distributor: d, shipping_method: sm2) From 34588e6141fcbeda949c8461e151ae3217a17dd4 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 1 Nov 2012 09:55:45 +1100 Subject: [PATCH 35/52] Remove old supplier and distributor routes, models, controllers, views, specs --- .../admin/distributors_controller.rb | 28 ------- app/controllers/admin/suppliers_controller.rb | 16 ---- app/controllers/distributors_controller.rb | 35 -------- app/controllers/suppliers_controller.rb | 15 ---- app/models/distributor.rb | 31 ------- app/models/supplier.rb | 33 -------- app/overrides/distributors_admin_tab.rb | 11 --- app/views/admin/distributors/_form.html.haml | 42 ---------- app/views/admin/distributors/edit.html.erb | 6 -- app/views/admin/distributors/index.html.erb | 40 ---------- app/views/admin/distributors/new.html.erb | 7 -- app/views/admin/distributors/show.html.haml | 42 ---------- app/views/admin/suppliers/_form.html.haml | 24 ------ app/views/admin/suppliers/edit.html.haml | 5 -- app/views/admin/suppliers/index.html.haml | 22 ----- app/views/admin/suppliers/new.html.haml | 5 -- app/views/admin/suppliers/show.html.haml | 27 ------- app/views/distributors/show.html.haml | 7 -- app/views/suppliers/index.html.haml | 12 --- app/views/suppliers/show.html.haml | 7 -- config/routes.rb | 15 ---- ...22403_remove_suppliers_and_distributors.rb | 37 +++++++++ db/schema.rb | 31 +------ .../distributors_controller_spec.rb | 75 ----------------- spec/factories.rb | 21 ----- spec/requests/admin/distributors_spec.rb | 80 ------------------- spec/requests/admin/suppliers_spec.rb | 56 ------------- 27 files changed, 38 insertions(+), 692 deletions(-) delete mode 100644 app/controllers/admin/distributors_controller.rb delete mode 100644 app/controllers/admin/suppliers_controller.rb delete mode 100644 app/controllers/distributors_controller.rb delete mode 100644 app/controllers/suppliers_controller.rb delete mode 100644 app/models/distributor.rb delete mode 100644 app/models/supplier.rb delete mode 100644 app/overrides/distributors_admin_tab.rb delete mode 100644 app/views/admin/distributors/_form.html.haml delete mode 100644 app/views/admin/distributors/edit.html.erb delete mode 100644 app/views/admin/distributors/index.html.erb delete mode 100644 app/views/admin/distributors/new.html.erb delete mode 100644 app/views/admin/distributors/show.html.haml delete mode 100644 app/views/admin/suppliers/_form.html.haml delete mode 100644 app/views/admin/suppliers/edit.html.haml delete mode 100644 app/views/admin/suppliers/index.html.haml delete mode 100644 app/views/admin/suppliers/new.html.haml delete mode 100644 app/views/admin/suppliers/show.html.haml delete mode 100644 app/views/distributors/show.html.haml delete mode 100644 app/views/suppliers/index.html.haml delete mode 100644 app/views/suppliers/show.html.haml create mode 100644 db/migrate/20121031222403_remove_suppliers_and_distributors.rb delete mode 100644 spec/controllers/distributors_controller_spec.rb delete mode 100644 spec/requests/admin/distributors_spec.rb delete mode 100644 spec/requests/admin/suppliers_spec.rb diff --git a/app/controllers/admin/distributors_controller.rb b/app/controllers/admin/distributors_controller.rb deleted file mode 100644 index 21d561a1ea..0000000000 --- a/app/controllers/admin/distributors_controller.rb +++ /dev/null @@ -1,28 +0,0 @@ -module Admin - class DistributorsController < ResourceController - before_filter :load_distributor_set, :only => :index - before_filter :load_countries, :except => :index - - def bulk_update - @distributor_set = DistributorSet.new(params[:distributor_set]) - if @distributor_set.save - redirect_to main_app.admin_distributors_path, :notice => 'Distributor collection times updated.' - else - render :index - end - end - - private - def load_distributor_set - @distributor_set = DistributorSet.new :distributors => collection - end - - def load_countries - @countries = Spree::Country.order(:name) - end - - def collection - super.order(:name) - end - end -end diff --git a/app/controllers/admin/suppliers_controller.rb b/app/controllers/admin/suppliers_controller.rb deleted file mode 100644 index 63eb594bf3..0000000000 --- a/app/controllers/admin/suppliers_controller.rb +++ /dev/null @@ -1,16 +0,0 @@ -module Admin - class SuppliersController < ResourceController - before_filter :load_data, :except => [:index] - - helper 'spree/products' - - private - def load_data - @countries = Spree::Country.order(:name) - end - - def collection - super.order(:name) - end - end -end diff --git a/app/controllers/distributors_controller.rb b/app/controllers/distributors_controller.rb deleted file mode 100644 index 97c6d81691..0000000000 --- a/app/controllers/distributors_controller.rb +++ /dev/null @@ -1,35 +0,0 @@ -class DistributorsController < BaseController - def show - options = {:distributor_id => params[:id]} - options.merge(params.reject { |k,v| k == :id }) - - @distributor = Distributor.find params[:id] - - @searcher = Spree::Config.searcher_class.new(options) - @products = @searcher.retrieve_products - end - - def select - distributor = Distributor.find params[:id] - - order = current_order(true) - - if order.can_change_distributor? - order.distributor = distributor - order.save! - end - - redirect_to distributor - end - - def deselect - order = current_order(true) - - if order.can_change_distributor? - order.distributor = nil - order.save! - end - - redirect_to root_path - end -end diff --git a/app/controllers/suppliers_controller.rb b/app/controllers/suppliers_controller.rb deleted file mode 100644 index 3b76225449..0000000000 --- a/app/controllers/suppliers_controller.rb +++ /dev/null @@ -1,15 +0,0 @@ -class SuppliersController < BaseController - def index - @suppliers = Supplier.all - end - - def show - options = {:supplier_id => params[:id]} - options.merge(params.reject { |k,v| k == :id }) - - @supplier = Supplier.find params[:id] - - @searcher = Spree::Config.searcher_class.new(options) - @products = @searcher.retrieve_products - end -end diff --git a/app/models/distributor.rb b/app/models/distributor.rb deleted file mode 100644 index 1bc2d274c5..0000000000 --- a/app/models/distributor.rb +++ /dev/null @@ -1,31 +0,0 @@ -class Distributor < ActiveRecord::Base - belongs_to :pickup_address, :foreign_key => 'pickup_address_id', :class_name => 'Spree::Address' - has_many :orders, :class_name => 'Spree::Order' - - has_many :product_distributions, :dependent => :destroy - has_many :products, :through => :product_distributions - - accepts_nested_attributes_for :pickup_address - - validates_presence_of :name, :pickup_address - validates_associated :pickup_address - - scope :by_name, order('name') - scope :with_active_products_on_hand, lambda { joins(:products).where('spree_products.deleted_at IS NULL AND spree_products.available_on <= ? AND spree_products.count_on_hand > 0', Time.now).select('distinct(distributors.*)') } - - after_initialize :initialize_country - before_validation :set_unused_address_fields - - def initialize_country - self.pickup_address ||= Spree::Address.new - self.pickup_address.country = Spree::Country.find_by_id(Spree::Config[:default_country_id]) if self.pickup_address.new_record? - end - - def set_unused_address_fields - pickup_address.firstname = pickup_address.lastname = pickup_address.phone = 'unused' if pickup_address.present? - end - - def to_param - "#{id}-#{name.parameterize}" - end -end diff --git a/app/models/supplier.rb b/app/models/supplier.rb deleted file mode 100644 index 76518a5f34..0000000000 --- a/app/models/supplier.rb +++ /dev/null @@ -1,33 +0,0 @@ -class Supplier < ActiveRecord::Base - has_many :products, :class_name => 'Spree::Product' - belongs_to :address, :class_name => 'Spree::Address' - - accepts_nested_attributes_for :address - - validates_presence_of :name, :address - validates_associated :address - - after_initialize :initialize_country - before_validation :set_unused_address_fields - - def has_products_on_hand? - self.products.where('count_on_hand > 0').present? - end - - def to_param - "#{id}-#{name.parameterize}" - end - - - private - - def initialize_country - self.address ||= Spree::Address.new - self.address.country = Spree::Country.find_by_id(Spree::Config[:default_country_id]) if self.address.new_record? - end - - def set_unused_address_fields - address.firstname = address.lastname = address.phone = 'unused' if address.present? - end - -end diff --git a/app/overrides/distributors_admin_tab.rb b/app/overrides/distributors_admin_tab.rb deleted file mode 100644 index b0b71bfcea..0000000000 --- a/app/overrides/distributors_admin_tab.rb +++ /dev/null @@ -1,11 +0,0 @@ -Deface::Override.new(:virtual_path => "spree/layouts/admin", - :name => "distributors_admin_tabs", - :insert_bottom => "[data-hook='admin_tabs'], #admin_tabs[data-hook]", - :text => "<%= tab(:distributors, :url => main_app.admin_distributors_path) %>", - :disabled => false) - -Deface::Override.new(:virtual_path => "spree/layouts/admin", - :name => "suppliers_admin_tabs", - :insert_bottom => "[data-hook='admin_tabs'], #admin_tabs[data-hook]", - :text => "<%= tab(:suppliers, :url => main_app.admin_suppliers_path) %>", - :disabled => false) diff --git a/app/views/admin/distributors/_form.html.haml b/app/views/admin/distributors/_form.html.haml deleted file mode 100644 index efc0dd37f0..0000000000 --- a/app/views/admin/distributors/_form.html.haml +++ /dev/null @@ -1,42 +0,0 @@ -- content_for :head do - = render 'shared/cms_elrte_head' - -%table{"data-hook" => "distributors"} - %tr{"data-hook" => "name"} - %td Name: - %td= f.text_field :name - %tr{"data-hook" => "description"} - %td Description: - %td= f.text_field :description - %tr{'data-hook' => "long_description"} - %td Extended Description: - %td= f.text_area :long_description, :class => 'rich_text' - %tr{"data-hook" => "contact"} - %td Contact: - %td= f.text_field :contact - %tr{"data-hook" => "phone"} - %td Phone: - %td= f.text_field :phone - %tr{"data-hook" => "email"} - %td Email: - %td= f.text_field :email - %tr{"data-hook" => "url"} - %td URL: - %td= f.text_field :url - %tr{"data-hook" => "abn"} - %td ABN: - %td= f.text_field :abn - %tr{"data-hook" => "acn"} - %td ACN: - %td= f.text_field :acn -%fieldset - %legend Pickup details - %table{"data-hook" => "distributors_pickup_details"} - %tr{"data-hook" => "next_collection_at"} - %td Next collection date/time: - %td= f.text_field :next_collection_at - %tr{"data-hook" => "pickup_times"} - %td Regular pickup times: - %td= f.text_field :pickup_times - = f.fields_for :pickup_address do |pickup_address_form| - = render 'spree/admin/shared/address_form', :f => pickup_address_form diff --git a/app/views/admin/distributors/edit.html.erb b/app/views/admin/distributors/edit.html.erb deleted file mode 100644 index a67c215206..0000000000 --- a/app/views/admin/distributors/edit.html.erb +++ /dev/null @@ -1,6 +0,0 @@ - -<%= render :partial => 'spree/shared/error_messages', :locals => { :target => @distributor } %> -<%= form_for [:admin, @distributor] do |f| %> - <%= render :partial => 'form', :locals => { :f => f } %> - <%= render :partial => 'spree/admin/shared/edit_resource_links' %> -<% end %> \ No newline at end of file diff --git a/app/views/admin/distributors/index.html.erb b/app/views/admin/distributors/index.html.erb deleted file mode 100644 index 86eef01337..0000000000 --- a/app/views/admin/distributors/index.html.erb +++ /dev/null @@ -1,40 +0,0 @@ -
-
    -
  • - <%= button_link_to "New Distributor", main_app.new_admin_distributor_path, :icon => 'add', :id => 'admin_new_distributor_link' %> -
  • -
-
-
- -<%= form_for @distributor_set, :url => main_app.bulk_update_admin_distributors_path do |f| %> - - - - - - - - - - - <%= f.fields_for :distributors do |distributor_form| %> - <% distributor = distributor_form.object %> - - - - - - - <% end %> - <% if @distributors.empty? %> - - <% end %> - -
NameNext Collection Date/TimeDescription
<%= link_to distributor.name, main_app.admin_distributor_path(distributor) %><%= distributor_form.text_field :next_collection_at %><%= distributor.description %> - <%= link_to_edit distributor, :class => 'edit' %>   - <%= link_to_delete distributor %> -
<%= t(:none) %>
- - <%= f.submit 'Update' %> -<% end %> diff --git a/app/views/admin/distributors/new.html.erb b/app/views/admin/distributors/new.html.erb deleted file mode 100644 index df6b32cbbe..0000000000 --- a/app/views/admin/distributors/new.html.erb +++ /dev/null @@ -1,7 +0,0 @@ - -<%= render :partial => 'spree/shared/error_messages', :locals => { :target => @distributor } %> - -<%= form_for [main_app, :admin, @distributor] do |f| %> - <%= render :partial => 'form', :locals => { :f => f } %> - <%= render :partial => 'spree/admin/shared/new_resource_links' %> -<% end %> diff --git a/app/views/admin/distributors/show.html.haml b/app/views/admin/distributors/show.html.haml deleted file mode 100644 index 1f1357457a..0000000000 --- a/app/views/admin/distributors/show.html.haml +++ /dev/null @@ -1,42 +0,0 @@ -%h1 Distributor -%table - %tr - %th Name: - %td= @distributor.name - %tr - %th Description: - %td= @distributor.description - %tr - %th Extended Description: - %td= @distributor.long_description.andand.html_safe - %tr - %th Contact person: - %td= @distributor.contact - %tr - %th Phone number: - %td= @distributor.phone - %tr - %th Email: - %td= @distributor.email - %tr - %th Pickup address: - %td= render 'spree/shared/address', :address => @distributor.pickup_address - %tr - %th Next collection date/time: - %td= @distributor.next_collection_at - %tr - %th Regular pickup times: - %td= @distributor.pickup_times - %tr - %th ABN: - %td= @distributor.abn - %tr - %th ACN: - %td= @distributor.acn - %tr - %th URL: - %td= @distributor.url -%p - = link_to :Edit, main_app.edit_admin_distributor_path(@distributor), :class => 'edit_distributor' - = t(:or) - = link_to t(:back), main_app.admin_distributors_path diff --git a/app/views/admin/suppliers/_form.html.haml b/app/views/admin/suppliers/_form.html.haml deleted file mode 100644 index 2e145f44d0..0000000000 --- a/app/views/admin/suppliers/_form.html.haml +++ /dev/null @@ -1,24 +0,0 @@ -- content_for :head do - = render 'shared/cms_elrte_head' - -%table{'data-hook' => "suppliers"} - %tr{'data-hook' => "name"} - %td Name: - %td= f.text_field :name - %tr{'data-hook' => "description"} - %td Description: - %td= f.text_field :description - %tr{'data-hook' => "long_description"} - %td Extended Description: - %td= f.text_area :long_description, :class => 'rich_text' - = f.fields_for :address do |address_form| - = render 'spree/admin/shared/address_form', :f => address_form - %tr{'data-hook' => "email"} - %td Email: - %td= f.text_field :email - %tr{'data-hook' => "website"} - %td Website: - %td= f.text_field :website - %tr{'data-hook' => "twitter"} - %td Twitter: - %td= f.text_field :twitter diff --git a/app/views/admin/suppliers/edit.html.haml b/app/views/admin/suppliers/edit.html.haml deleted file mode 100644 index b51781dede..0000000000 --- a/app/views/admin/suppliers/edit.html.haml +++ /dev/null @@ -1,5 +0,0 @@ - -= render :partial => 'spree/shared/error_messages', :locals => { :target => @supplier } -= form_for [:admin, @supplier] do |f| - = render :partial => 'form', :locals => { :f => f } - = render :partial => 'spree/admin/shared/edit_resource_links' diff --git a/app/views/admin/suppliers/index.html.haml b/app/views/admin/suppliers/index.html.haml deleted file mode 100644 index 8eb54a6d8d..0000000000 --- a/app/views/admin/suppliers/index.html.haml +++ /dev/null @@ -1,22 +0,0 @@ -.toolbar{'data-hook' => "suppliers"} - %ul.actions - %li= button_link_to "New Supplier", main_app.new_admin_supplier_path, :icon => 'add', :id => 'admin_new_supplier_link' - %br.clear -%table#listing_suppliers.index - %thead - %tr{'data-hook' => "suppliers_header"} - %th Name - %th Description - %th - %tbody - - @suppliers.each do |supplier| - %tr - %td= link_to supplier.name, main_app.admin_supplier_path(supplier) - %td= supplier.description - %td{'data-hook' => "admin_supplier_index_row_actions"} - = link_to_edit supplier, :class => 'edit' -   - = link_to_delete supplier - - if @suppliers.empty? - %tr - %td{:colspan => "2"}= t(:none) diff --git a/app/views/admin/suppliers/new.html.haml b/app/views/admin/suppliers/new.html.haml deleted file mode 100644 index cbdf66570a..0000000000 --- a/app/views/admin/suppliers/new.html.haml +++ /dev/null @@ -1,5 +0,0 @@ -= render :partial => 'spree/shared/error_messages', :locals => { :target => @supplier } - -= form_for [main_app, :admin, @supplier] do |f| - = render :partial => 'form', :locals => { :f => f } - = render :partial => 'spree/admin/shared/new_resource_links' diff --git a/app/views/admin/suppliers/show.html.haml b/app/views/admin/suppliers/show.html.haml deleted file mode 100644 index c6ac3b8e24..0000000000 --- a/app/views/admin/suppliers/show.html.haml +++ /dev/null @@ -1,27 +0,0 @@ -%h1 Supplier -%table - %tr - %th Name: - %td= @supplier.name - %tr - %th Description: - %td= @supplier.description - %tr - %th Extended Description: - %td= @supplier.long_description.andand.html_safe - %tr - %th Address: - %td= render 'spree/shared/address', :address => @supplier.address - %tr - %th Email: - %td= @supplier.email - %tr - %th Website: - %td= @supplier.website - %tr - %th Twitter: - %td= @supplier.twitter -%p - = link_to :Edit, main_app.edit_admin_supplier_path(@supplier), :class => 'edit_supplier' - = t(:or) - = link_to t(:back), main_app.admin_suppliers_path diff --git a/app/views/distributors/show.html.haml b/app/views/distributors/show.html.haml deleted file mode 100644 index da898156e1..0000000000 --- a/app/views/distributors/show.html.haml +++ /dev/null @@ -1,7 +0,0 @@ -%h2= @distributor.name - -.distributor-description= @distributor.long_description.andand.html_safe - -%h3 Available Now - -= render :template => 'spree/products/index' diff --git a/app/views/suppliers/index.html.haml b/app/views/suppliers/index.html.haml deleted file mode 100644 index 04bd95375c..0000000000 --- a/app/views/suppliers/index.html.haml +++ /dev/null @@ -1,12 +0,0 @@ -- content_for :sidebar do - %div{'data-hook' => "homepage_sidebar_navigation"} - = render 'spree/sidebar' - - -%h1 Suppliers - -= cms_page_content(:content, Cms::Page.find_by_full_path('/suppliers')) - -%ul.suppliers - - @suppliers.each do |supplier| - %li= link_to supplier.name, supplier diff --git a/app/views/suppliers/show.html.haml b/app/views/suppliers/show.html.haml deleted file mode 100644 index 9605d5db1a..0000000000 --- a/app/views/suppliers/show.html.haml +++ /dev/null @@ -1,7 +0,0 @@ -%h2= @supplier.name - -.supplier-description= @supplier.long_description.andand.html_safe - -%h3 Available Now - -= render :template => 'spree/products/index' diff --git a/config/routes.rb b/config/routes.rb index 67bd610c69..1a018ba3ae 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,25 +7,10 @@ Openfoodweb::Application.routes.draw do get :deselect_distributor, :on => :collection end - # Deprecated - resources :suppliers - # Deprecated - resources :distributors do - get :select, :on => :member - get :deselect, :on => :collection - end - namespace :admin do resources :enterprises do post :bulk_update, :on => :collection, :as => :bulk_update end - - # Deprecated - resources :distributors do - post :bulk_update, :on => :collection, :as => :bulk_update - end - # Deprecated - resources :suppliers end # Mount Spree's routes diff --git a/db/migrate/20121031222403_remove_suppliers_and_distributors.rb b/db/migrate/20121031222403_remove_suppliers_and_distributors.rb new file mode 100644 index 0000000000..ece31be171 --- /dev/null +++ b/db/migrate/20121031222403_remove_suppliers_and_distributors.rb @@ -0,0 +1,37 @@ +class RemoveSuppliersAndDistributors < ActiveRecord::Migration + def up + drop_table :suppliers + drop_table :distributors + end + + def down + create_table "distributors" do |t| + t.string :name + t.string :contact + t.string :phone + t.string :email + t.string :pickup_times + t.string :url + t.string :abn + t.string :acn + t.string :description + t.datetime :created_at + t.datetime :updated_at + t.integer :pickup_address_id + t.string :next_collection_at + t.text :long_description + end + + create_table "suppliers" do |t| + t.string :name + t.string :description + t.string :email + t.string :twitter + t.string :website + t.datetime :created_at + t.datetime :updated_at + t.integer :address_id + t.text :long_description + end + end +end diff --git a/db/schema.rb b/db/schema.rb index c03b5d21f4..197f21417b 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 => 20121028070200) do +ActiveRecord::Schema.define(:version => 20121031222403) do create_table "cms_blocks", :force => true do |t| t.integer "page_id", :null => false @@ -130,23 +130,6 @@ ActiveRecord::Schema.define(:version => 20121028070200) do add_index "cms_snippets", ["site_id", "identifier"], :name => "index_cms_snippets_on_site_id_and_identifier", :unique => true add_index "cms_snippets", ["site_id", "position"], :name => "index_cms_snippets_on_site_id_and_position" - create_table "distributors", :force => true do |t| - t.string "name" - t.string "contact" - t.string "phone" - t.string "email" - t.string "pickup_times" - t.string "url" - t.string "abn" - t.string "acn" - t.string "description" - t.datetime "created_at" - t.datetime "updated_at" - t.integer "pickup_address_id" - t.string "next_collection_at" - t.text "long_description" - end - create_table "enterprises", :force => true do |t| t.string "name" t.string "description" @@ -793,16 +776,4 @@ ActiveRecord::Schema.define(:version => 20121028070200) do t.integer "zone_members_count", :default => 0 end - create_table "suppliers", :force => true do |t| - t.string "name" - t.string "description" - t.string "email" - t.string "twitter" - t.string "website" - t.datetime "created_at" - t.datetime "updated_at" - t.integer "address_id" - t.text "long_description" - end - end diff --git a/spec/controllers/distributors_controller_spec.rb b/spec/controllers/distributors_controller_spec.rb deleted file mode 100644 index 488ee24b39..0000000000 --- a/spec/controllers/distributors_controller_spec.rb +++ /dev/null @@ -1,75 +0,0 @@ -require 'spec_helper' -require 'spree/core/current_order' - -describe DistributorsController do - include Spree::Core::CurrentOrder - - before :each do - stub!(:before_save_new_order) - stub!(:after_save_new_order) - - create(:itemwise_shipping_method) - end - - - it "selects distributors" do - d = create(:distributor) - - spree_get :select, :id => d.id - response.should be_redirect - - order = current_order(false) - order.distributor.should == d - end - - it "deselects distributors" do - d = create(:distributor) - order = current_order(true) - order.distributor = d - order.save! - - spree_get :deselect - response.should be_redirect - - order.reload - order.distributor.should be_nil - end - - context "when a product has been added to the cart" do - it "does not allow selecting another distributor" do - # Given some distributors and an order with a product - d1 = create(:distributor) - d2 = create(:distributor) - p = create(:product, :distributors => [d1]) - o = current_order(true) - - o.distributor = d1 - o.save! - o.add_variant(p.master, 1) - - # When I attempt to select a distributor - spree_get :select, :id => d2.id - - # Then my distributor should remain unchanged - o.reload - o.distributor.should == d1 - end - - it "does not allow deselecting distributors" do - # Given a distributor and an order with a product - d = create(:distributor) - p = create(:product, :distributors => [d]) - o = current_order(true) - o.distributor = d - o.save! - o.add_variant(p.master, 1) - - # When I attempt to deselect the distributor - spree_get :deselect - - # Then my distributor should remain unchanged - o.reload - o.distributor.should == d - end - end -end diff --git a/spec/factories.rb b/spec/factories.rb index 7ecae25065..79d26f5731 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -20,27 +20,6 @@ FactoryGirl.define do is_distributor true end - factory :supplier, :class => Supplier do - sequence(:name) { |n| "Supplier #{n}" } - description 'supplier' - long_description '

Hello, world!

This is a paragraph.

' - email 'supplier@example.com' - address { Spree::Address.first || FactoryGirl.create(:address) } - end - - factory :distributor, :class => Distributor do - sequence(:name) { |n| "Distributor #{n}" } - contact 'Mr Turing' - phone '1000100100' - description 'The creator' - long_description '

Hello, world!

This is a paragraph.

' - email 'alan@somewhere.com' - url 'http://example.com' - pickup_times "Whenever you're free" - next_collection_at 'Thursday 10am' - pickup_address { Spree::Address.first || FactoryGirl.create(:address) } - end - factory :product_distribution, :class => ProductDistribution do product { |pd| Spree::Product.first || FactoryGirl.create(:product) } distributor { |pd| Enterprise.is_distributor.first || FactoryGirl.create(:distributor_enterprise) } diff --git a/spec/requests/admin/distributors_spec.rb b/spec/requests/admin/distributors_spec.rb deleted file mode 100644 index d409eba3ff..0000000000 --- a/spec/requests/admin/distributors_spec.rb +++ /dev/null @@ -1,80 +0,0 @@ -require "spec_helper" - -feature %q{ - As an administration - I want manage the distributors of products -} do - include AuthenticationWorkflow - include WebHelper - - - scenario "listing distributors" do - d = create(:distributor) - - login_to_admin_section - click_link 'Distributors' - - page.should have_content d.name - end - - scenario "viewing a distributor" do - d = create(:distributor) - - login_to_admin_section - click_link 'Distributors' - click_link d.name - - page.should have_content d.name - end - - scenario "creating a new distributor" do - login_to_admin_section - - click_link 'Distributors' - click_link 'New Distributor' - - fill_in 'distributor_name', :with => 'Eaterprises' - fill_in 'distributor_description', :with => 'Connecting farmers and eaters' - fill_in 'distributor_long_description', :with => 'Zombie ipsum reversus ab viral inferno, nam rick grimes malum cerebro.' - fill_in 'distributor_contact', :with => 'Kirsten or Ren' - fill_in 'distributor_phone', :with => '0413 897 321' - - fill_in 'distributor_pickup_address_attributes_address1', :with => '35 Ballantyne St' - fill_in 'distributor_pickup_address_attributes_city', :with => 'Thornbury' - fill_in 'distributor_pickup_address_attributes_zipcode', :with => '3072' - select('Australia', :from => 'distributor_pickup_address_attributes_country_id') - select('Victoria', :from => 'distributor_pickup_address_attributes_state_id') - - fill_in 'distributor_next_collection_at', :with => 'Thursday, 22nd Feb, 6 - 9 PM' - fill_in 'distributor_pickup_times', :with => 'Thursday, 22nd Feb, 6 - 9 PM. Friday, 23nd Feb, 6 - 9 PM' - fill_in 'distributor_email', :with => 'info@eaterprises.com.au' - fill_in 'distributor_url', :with => 'http://eaterprises.com.au' - fill_in 'distributor_abn', :with => '09812309823' - fill_in 'distributor_acn', :with => '' - - click_button 'Create' - - flash_message.should == 'Distributor "Eaterprises" has been successfully created!' - end - - - scenario "updating many distributor next collection times at once" do - # Given three distributors - 3.times { create(:distributor) } - - # When I go to the distributors page - login_to_admin_section - click_link 'Distributors' - - # And I fill in some new collection times and save them - fill_in 'distributor_set_distributors_attributes_0_next_collection_at', :with => 'One' - fill_in 'distributor_set_distributors_attributes_1_next_collection_at', :with => 'Two' - fill_in 'distributor_set_distributors_attributes_2_next_collection_at', :with => 'Three' - click_button 'Update' - - # Then my times should have been saved - flash_message.should == 'Distributor collection times updated.' - Distributor.all.map { |d| d.next_collection_at }.should == %w(One Two Three) - end - -end diff --git a/spec/requests/admin/suppliers_spec.rb b/spec/requests/admin/suppliers_spec.rb deleted file mode 100644 index acd91568be..0000000000 --- a/spec/requests/admin/suppliers_spec.rb +++ /dev/null @@ -1,56 +0,0 @@ -require "spec_helper" - -feature %q{ - As an administration - I want manage the suppliers of products -} do - include AuthenticationWorkflow - include WebHelper - - background do - end - - scenario "listing suppliers" do - s = create(:supplier) - - login_to_admin_section - click_link 'Suppliers' - - page.should have_content s.name - end - - scenario "viewing a supplier" do - s = create(:supplier) - - login_to_admin_section - click_link 'Suppliers' - click_link s.name - - page.should have_content s.name - end - - scenario "creating a new supplier" do - login_to_admin_section - - click_link 'Suppliers' - click_link 'New Supplier' - - fill_in 'supplier_name', :with => 'David Arnold' - fill_in 'supplier_description', :with => 'A farmer with a difference' - fill_in 'supplier_long_description', :with => 'Zombie ipsum reversus ab viral inferno, nam rick grimes malum cerebro.' - - fill_in 'supplier_address_attributes_address1', :with => '35 Byron Ave' - fill_in 'supplier_address_attributes_city', :with => 'Ararat' - fill_in 'supplier_address_attributes_zipcode', :with => '1112' - select('Australia', :from => 'supplier_address_attributes_country_id') - select('Victoria', :from => 'supplier_address_attributes_state_id') - - fill_in 'supplier_email', :with => 'david@here.com' - fill_in 'supplier_website', :with => 'http://somewhere.com' - fill_in 'supplier_twitter', :with => 'davida' - - click_button 'Create' - - flash_message.should == 'Supplier "David Arnold" has been successfully created!' - end -end From 682c09f5164076eca362c3307b54081a795d0542 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 1 Nov 2012 10:05:39 +1100 Subject: [PATCH 36/52] Remove use of old Distributor model, fix before all not cleaning db --- app/views/spree/products/_add_to_cart.html.haml | 3 +-- spec/lib/open_food_web/group_buy_report_spec.rb | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/views/spree/products/_add_to_cart.html.haml b/app/views/spree/products/_add_to_cart.html.haml index 34796f83a3..e934cebe21 100644 --- a/app/views/spree/products/_add_to_cart.html.haml +++ b/app/views/spree/products/_add_to_cart.html.haml @@ -17,11 +17,10 @@ - order = current_order(false) - if order.nil? || order.can_change_distributor? %p Distributor - = select_tag "distributor_id", options_from_collection_for_select([Distributor.new]+@product.distributors, "id", "name", current_distributor.andand.id) + = select_tag "distributor_id", options_from_collection_for_select([Enterprise.new]+@product.distributors, "id", "name", current_distributor.andand.id) - else = hidden_field_tag "distributor_id", order.distributor.id .distributor-fixed= "Your distributor for this order is #{order.distributor.name}" %br/ = button_tag :class => 'large primary', :id => 'add-to-cart-button', :type => :submit do = t(:add_to_cart) - diff --git a/spec/lib/open_food_web/group_buy_report_spec.rb b/spec/lib/open_food_web/group_buy_report_spec.rb index c77d774856..0dae0d3d47 100644 --- a/spec/lib/open_food_web/group_buy_report_spec.rb +++ b/spec/lib/open_food_web/group_buy_report_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' module OpenFoodWeb describe GroupBuyReport do - before(:all) do + before(:each) do @orders = [] bill_address = create(:address) distributor_address = create(:address, :address1 => "distributor address", :city => 'The Shire', :zipcode => "1234") From 78bdbcbe6f9fe184fd5aaa3f21862ad7cb7deb70 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 1 Nov 2012 10:07:55 +1100 Subject: [PATCH 37/52] Rename DistributorsHelper to EnterprisesHelper --- app/controllers/spree/home_controller_decorator.rb | 2 +- app/controllers/spree/products_controller_decorator.rb | 2 +- app/controllers/spree/taxons_controller_decorator.rb | 2 +- app/helpers/{distributors_helper.rb => enterprises_helper.rb} | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename app/helpers/{distributors_helper.rb => enterprises_helper.rb} (80%) diff --git a/app/controllers/spree/home_controller_decorator.rb b/app/controllers/spree/home_controller_decorator.rb index f82db2ff50..7eb3e3e351 100644 --- a/app/controllers/spree/home_controller_decorator.rb +++ b/app/controllers/spree/home_controller_decorator.rb @@ -1,7 +1,7 @@ require 'open_food_web/split_products_by_distributor' Spree::HomeController.class_eval do - include DistributorsHelper + include EnterprisesHelper include OpenFoodWeb::SplitProductsByDistributor respond_override :index => { :html => { :success => lambda { diff --git a/app/controllers/spree/products_controller_decorator.rb b/app/controllers/spree/products_controller_decorator.rb index 17d134c2d9..913b7f4dee 100644 --- a/app/controllers/spree/products_controller_decorator.rb +++ b/app/controllers/spree/products_controller_decorator.rb @@ -1,7 +1,7 @@ require 'open_food_web/split_products_by_distributor' Spree::ProductsController.class_eval do - include DistributorsHelper + include EnterprisesHelper include OpenFoodWeb::SplitProductsByDistributor respond_override :index => { :html => { :success => lambda { diff --git a/app/controllers/spree/taxons_controller_decorator.rb b/app/controllers/spree/taxons_controller_decorator.rb index 0fb7517995..1d0fe7a7a5 100644 --- a/app/controllers/spree/taxons_controller_decorator.rb +++ b/app/controllers/spree/taxons_controller_decorator.rb @@ -1,7 +1,7 @@ require 'open_food_web/split_products_by_distributor' Spree::TaxonsController.class_eval do - include DistributorsHelper + include EnterprisesHelper include OpenFoodWeb::SplitProductsByDistributor respond_override :show => { :html => { :success => lambda { diff --git a/app/helpers/distributors_helper.rb b/app/helpers/enterprises_helper.rb similarity index 80% rename from app/helpers/distributors_helper.rb rename to app/helpers/enterprises_helper.rb index c641807e99..fb8a9a13bc 100644 --- a/app/helpers/distributors_helper.rb +++ b/app/helpers/enterprises_helper.rb @@ -1,4 +1,4 @@ -module DistributorsHelper +module EnterprisesHelper def current_distributor @current_distributor ||= current_order(false).andand.distributor end From d89945bbb2979e9b0eb49949fd31565cc92610dc Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 1 Nov 2012 10:20:06 +1100 Subject: [PATCH 38/52] Add smoke test for reports, fix use of old Distributor model --- .../admin/reports_controller_decorator.rb | 2 +- spec/requests/admin/enterprises_spec.rb | 2 +- spec/requests/admin/reports_spec.rb | 27 +++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 spec/requests/admin/reports_spec.rb diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index b4d6585ec2..88424d31c7 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -53,7 +53,7 @@ Spree::Admin::ReportsController.class_eval do @search = Spree::Order.complete.search(params[:q]) orders = @search.result - @distributors = Distributor.all + @distributors = Enterprise.is_distributor @report = OpenFoodWeb::GroupBuyReport.new orders unless params[:csv] diff --git a/spec/requests/admin/enterprises_spec.rb b/spec/requests/admin/enterprises_spec.rb index 19edb6d772..dae560df69 100644 --- a/spec/requests/admin/enterprises_spec.rb +++ b/spec/requests/admin/enterprises_spec.rb @@ -1,7 +1,7 @@ require "spec_helper" feature %q{ - As an administration + As an administrator I want manage enterprises } do include AuthenticationWorkflow diff --git a/spec/requests/admin/reports_spec.rb b/spec/requests/admin/reports_spec.rb new file mode 100644 index 0000000000..e3e2d8fcd2 --- /dev/null +++ b/spec/requests/admin/reports_spec.rb @@ -0,0 +1,27 @@ +require "spec_helper" + +feature %q{ + As an administrator + I want numbers, all the numbers! +} do + include AuthenticationWorkflow + include WebHelper + + + scenario "orders and distributors report" do + login_to_admin_section + click_link 'Reports' + click_link 'Orders And Distributors' + + page.should have_content 'Order date' + end + + scenario "group buys report" do + login_to_admin_section + click_link 'Reports' + click_link 'Group Buys' + + page.should have_content 'Supplier' + end + +end From 13d6f7213e51b3f0d751399d069f0b34986fecba Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 1 Nov 2012 10:21:55 +1100 Subject: [PATCH 39/52] Fix db seeds --- db/seeds.rb | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/db/seeds.rb b/db/seeds.rb index e786ca7a1a..9a566de0ce 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -59,12 +59,12 @@ unless Spree::Taxonomy.find_by_name 'Products' end -# -- Suppliers and distributors -unless Supplier.count > 0 - puts "[db:seed] Seeding suppliers and distributors" +# -- Enterprises +unless Enterprise.count > 0 + puts "[db:seed] Seeding enterprises" - 3.times { FactoryGirl.create(:supplier) } - 3.times { FactoryGirl.create(:distributor) } + 3.times { FactoryGirl.create(:supplier_enterprise) } + 3.times { FactoryGirl.create(:distributor_enterprise) } end @@ -74,19 +74,19 @@ unless Spree::Product.count > 0 FactoryGirl.create(:product, :name => 'Garlic', :price => 20.00, - :supplier => Supplier.all[0], - :distributors => [Distributor.all[0]], + :supplier => Enterprise.is_supplier[0], + :distributors => [Enterprise.is_distributor[0]], :taxons => [Spree::Taxon.find_by_name('Vegetables')]) FactoryGirl.create(:product, :name => 'Fuji Apple', :price => 5.00, - :supplier => Supplier.all[1], - :distributors => Distributor.all, + :supplier => Enterprise.is_supplier[1], + :distributors => Enterprise.is_distributor, :taxons => [Spree::Taxon.find_by_name('Fruit')]) FactoryGirl.create(:product, :name => 'Beef - 5kg Trays', :price => 50.00, - :supplier => Supplier.all[2], - :distributors => [Distributor.all[2]], + :supplier => Enterprise.is_supplier[2], + :distributors => [Enterprise.is_distributor[2]], :taxons => [Spree::Taxon.find_by_name('Meat and Fish')]) end From 8748a65031b15a6a578e313c9dfe5ad3ac9fb661 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 1 Nov 2012 11:22:13 +1100 Subject: [PATCH 40/52] Show role on admin enterprises listing page --- app/controllers/admin/enterprises_controller.rb | 2 +- app/views/admin/enterprises/index.html.erb | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index bc9b39ef35..08f6e2133f 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -24,7 +24,7 @@ module Admin end def collection - super.order(:name) + super.order('is_primary_producer DESC, is_distributor ASC, name') end end end diff --git a/app/views/admin/enterprises/index.html.erb b/app/views/admin/enterprises/index.html.erb index d31be7b2b8..75dad0f85f 100644 --- a/app/views/admin/enterprises/index.html.erb +++ b/app/views/admin/enterprises/index.html.erb @@ -12,6 +12,7 @@ Name + Role Next Collection Date/Time Description @@ -22,6 +23,7 @@ <% enterprise = enterprise_form.object %> <%= link_to enterprise.name, main_app.admin_enterprise_path(enterprise) %> + <%= 'PP ' if enterprise.is_primary_producer %><%= 'D' if enterprise.is_distributor %> <%= enterprise_form.text_field :next_collection_at %> <%= enterprise.description %> From a7a8b8490b98c1330a376eacc5931de3929f5dc0 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 1 Nov 2012 11:31:08 +1100 Subject: [PATCH 41/52] Add test for viewing a CMS page --- spec/requests/consumer/cms_spec.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/spec/requests/consumer/cms_spec.rb b/spec/requests/consumer/cms_spec.rb index 93a6a87958..7cd085f1e4 100644 --- a/spec/requests/consumer/cms_spec.rb +++ b/spec/requests/consumer/cms_spec.rb @@ -31,7 +31,6 @@ feature %q{ page.should_not have_content 'Home page content' end - scenario "viewing the menu of CMS pages" do # Given some CMS pages home_page = create(:cms_page, content: 'Home') @@ -48,4 +47,19 @@ feature %q{ page.should have_selector 'ul#main-nav-bar li', :text => 'Three' end + scenario "viewing a page from the CMS menu" do + # Given some CMS pages + home_page = create(:cms_page, content: 'Home') + create(:cms_page, parent: home_page, label: 'One') + create(:cms_page, parent: home_page, label: 'Two', content: 'This is the page') + create(:cms_page, parent: home_page, label: 'Three') + + # When I go to one of the pages + visit spree.root_path + click_link 'Two' + + # Then I should see the page + page.should have_content 'This is the page' + end + end From c2133d70ac5fc3c2bef59a17c7e42987a541544e Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 1 Nov 2012 15:02:57 +1100 Subject: [PATCH 42/52] Rename Enterprise is_supplier scope to is_primary_producer --- app/controllers/application_controller.rb | 3 +-- app/controllers/enterprises_controller.rb | 2 +- app/models/enterprise.rb | 2 +- app/views/spree/admin/products/_supplier_form.html.haml | 2 +- db/seeds.rb | 6 +++--- spec/factories.rb | 2 +- 6 files changed, 8 insertions(+), 9 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 1bfb4a7d42..b5bcef894e 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -9,9 +9,8 @@ class ApplicationController < ActionController::Base @cms_site = Cms::Site.where(:identifier => 'open-food-web').first end - def load_data_for_sidebar - @suppliers = Enterprise.is_supplier.all + @suppliers = Enterprise.is_primary_producer @distributors = Enterprise.is_distributor.with_distributed_active_products_on_hand.by_name end diff --git a/app/controllers/enterprises_controller.rb b/app/controllers/enterprises_controller.rb index 11b3a34beb..1b05eaf1c3 100644 --- a/app/controllers/enterprises_controller.rb +++ b/app/controllers/enterprises_controller.rb @@ -4,7 +4,7 @@ class EnterprisesController < BaseController end def suppliers - @suppliers = Enterprise.is_supplier + @suppliers = Enterprise.is_primary_producer end def distributors diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 0d66f83f8e..d4679f6424 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -15,7 +15,7 @@ class Enterprise < ActiveRecord::Base before_validation :set_unused_address_fields scope :by_name, order('name') - scope :is_supplier, where(:is_primary_producer => true) + scope :is_primary_producer, where(:is_primary_producer => true) scope :is_distributor, where(:is_distributor => true) scope :with_distributed_active_products_on_hand, lambda { joins(:distributed_products).where('spree_products.deleted_at IS NULL AND spree_products.available_on <= ? AND spree_products.count_on_hand > 0', Time.now).select('distinct(enterprises.*)') } diff --git a/app/views/spree/admin/products/_supplier_form.html.haml b/app/views/spree/admin/products/_supplier_form.html.haml index 9bfc0bec4a..aafb9175e1 100644 --- a/app/views/spree/admin/products/_supplier_form.html.haml +++ b/app/views/spree/admin/products/_supplier_form.html.haml @@ -1,5 +1,5 @@ = f.field_container :supplier do = f.label :supplier %br - = f.collection_select(:supplier_id, Enterprise.is_supplier, :id, :name, :include_blank => true) + = f.collection_select(:supplier_id, Enterprise.is_primary_producer, :id, :name, :include_blank => true) = f.error_message_on :supplier diff --git a/db/seeds.rb b/db/seeds.rb index 9a566de0ce..dd6be815e2 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -74,19 +74,19 @@ unless Spree::Product.count > 0 FactoryGirl.create(:product, :name => 'Garlic', :price => 20.00, - :supplier => Enterprise.is_supplier[0], + :supplier => Enterprise.is_primary_producer[0], :distributors => [Enterprise.is_distributor[0]], :taxons => [Spree::Taxon.find_by_name('Vegetables')]) FactoryGirl.create(:product, :name => 'Fuji Apple', :price => 5.00, - :supplier => Enterprise.is_supplier[1], + :supplier => Enterprise.is_primary_producer[1], :distributors => Enterprise.is_distributor, :taxons => [Spree::Taxon.find_by_name('Fruit')]) FactoryGirl.create(:product, :name => 'Beef - 5kg Trays', :price => 50.00, - :supplier => Enterprise.is_supplier[2], + :supplier => Enterprise.is_primary_producer[2], :distributors => [Enterprise.is_distributor[2]], :taxons => [Spree::Taxon.find_by_name('Meat and Fish')]) end diff --git a/spec/factories.rb b/spec/factories.rb index 79d26f5731..8348f98afd 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -43,7 +43,7 @@ FactoryGirl.modify do # When this fix has been merged into a version of Spree that we're using, this line can be removed. sequence(:name) { |n| "Product ##{n} - #{Kernel.rand(9999)}" } - supplier { Enterprise.is_supplier.first || FactoryGirl.create(:supplier_enterprise) } + supplier { Enterprise.is_primary_producer.first || FactoryGirl.create(:supplier_enterprise) } on_hand 3 # before(:create) do |product, evaluator| From 3a116dc13d25fbad97342967abb19477a43a81de Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 1 Nov 2012 16:37:12 +1100 Subject: [PATCH 43/52] Fix product filtering on supplier and distributor pages --- app/models/spree/product_decorator.rb | 4 +++ lib/open_food_web/searcher.rb | 2 ++ spec/lib/open_food_web/searcher_spec.rb | 20 +++++++++++++ spec/models/product_spec.rb | 33 ++++++++++++++++++++- spec/requests/consumer/distributors_spec.rb | 21 +++++++++++++ spec/requests/consumer/suppliers_spec.rb | 19 +++++++----- 6 files changed, 91 insertions(+), 8 deletions(-) diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index e59f15f85b..bd6626d296 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -12,6 +12,10 @@ Spree::Product.class_eval do scope :in_supplier, lambda { |supplier| where(:supplier_id => supplier) } scope :in_distributor, lambda { |distributor| joins(:product_distributions).where('product_distributions.distributor_id = ?', (distributor.respond_to?(:id) ? distributor.id : distributor.to_i)) } + scope :in_supplier_or_distributor, lambda { |enterprise| joins('LEFT OUTER JOIN product_distributions ON product_distributions.product_id=spree_products.id'). + where('supplier_id=? OR product_distributions.distributor_id=?', + enterprise.respond_to?(:id) ? enterprise.id : enterprise.to_i, + enterprise.respond_to?(:id) ? enterprise.id : enterprise.to_i) } def shipping_method_for_distributor(distributor) diff --git a/lib/open_food_web/searcher.rb b/lib/open_food_web/searcher.rb index 65aced9c58..faaa10893e 100644 --- a/lib/open_food_web/searcher.rb +++ b/lib/open_food_web/searcher.rb @@ -14,6 +14,7 @@ module OpenFoodWeb def get_base_scope base_scope = super + base_scope = base_scope.in_supplier_or_distributor(enterprise_id) if enterprise_id base_scope = base_scope.in_supplier(supplier_id) if supplier_id base_scope = base_scope.in_distributor(distributor_id) if distributor_id @@ -23,6 +24,7 @@ module OpenFoodWeb def prepare(params) super(params) + @properties[:enterprise_id] = params[:enterprise_id] @properties[:supplier_id] = params[:supplier_id] @properties[:distributor_id] = params[:distributor_id] end diff --git a/spec/lib/open_food_web/searcher_spec.rb b/spec/lib/open_food_web/searcher_spec.rb index 2617778abc..5c18b51f15 100644 --- a/spec/lib/open_food_web/searcher_spec.rb +++ b/spec/lib/open_food_web/searcher_spec.rb @@ -28,5 +28,25 @@ module OpenFoodWeb products = searcher.retrieve_products products.should == [p1] end + + it "searches by supplier or distributor" do + # Given products under some suppliers and distributors + s0 = create(:supplier_enterprise) + s1 = create(:supplier_enterprise) + d1 = create(:distributor_enterprise) + p1 = create(:product, :supplier => s1) + p2 = create(:product, :distributors => [d1]) + p3 = create(:product) + + # When we search by the supplier enterprise, we should see the supplied products + searcher = Searcher.new(:enterprise_id => s1.id.to_s) + products = searcher.retrieve_products + products.should == [p1] + + # When we search by the distributor enterprise, we should see the distributed products + searcher = Searcher.new(:enterprise_id => d1.id.to_s) + products = searcher.retrieve_products + products.should == [p2] + end end end diff --git a/spec/models/product_spec.rb b/spec/models/product_spec.rb index cc74db8638..5943218037 100644 --- a/spec/models/product_spec.rb +++ b/spec/models/product_spec.rb @@ -19,7 +19,38 @@ describe Spree::Product do end end - context "finders" do + describe "scopes" do + describe "in_supplier_or_distributor" do + it "finds supplied products" do + s0 = create(:supplier_enterprise) + s1 = create(:supplier_enterprise) + p0 = create(:product, :supplier => s0) + p1 = create(:product, :supplier => s1) + + Spree::Product.in_supplier_or_distributor(s1).should == [p1] + end + + it "finds distributed products" do + d0 = create(:distributor_enterprise) + d1 = create(:distributor_enterprise) + p0 = create(:product, :distributors => [d0]) + p1 = create(:product, :distributors => [d1]) + + Spree::Product.in_supplier_or_distributor(d1).should == [p1] + end + + it "finds products supplied and distributed by the same enterprise" do + s = create(:supplier_enterprise) + d = create(:distributor_enterprise) + p = create(:product, :supplier => s, :distributors => [d]) + + Spree::Product.in_supplier_or_distributor(s).should == [p] + Spree::Product.in_supplier_or_distributor(d).should == [p] + end + end + end + + describe "finders" do it "finds the shipping method for a particular distributor" do shipping_method = create(:shipping_method) distributor = create(:distributor_enterprise) diff --git a/spec/requests/consumer/distributors_spec.rb b/spec/requests/consumer/distributors_spec.rb index aaaf78e618..32e642edf5 100644 --- a/spec/requests/consumer/distributors_spec.rb +++ b/spec/requests/consumer/distributors_spec.rb @@ -26,6 +26,27 @@ feature %q{ page.should_not have_selector 'a', :text => d3.name end + scenario "viewing a distributor" do + # Given some distributors with products + d1 = create(:distributor_enterprise, :long_description => "

Hello, world!

") + d2 = create(:distributor_enterprise) + p1 = create(:product, :distributors => [d1]) + p2 = create(:product, :distributors => [d2]) + + # When I go to the first distributor page + visit spree.root_path + click_link d1.name + + # Then I should see the distributor details + page.should have_selector 'h2', :text => d1.name + page.should have_selector 'div.enterprise-description', :text => 'Hello, world!' + + # And I should see the first, but not the second product + page.should have_content p1.name + page.should_not have_content p2.name + end + + context "when a distributor is selected" do it "displays the distributor's details" do # Given a distributor with a product diff --git a/spec/requests/consumer/suppliers_spec.rb b/spec/requests/consumer/suppliers_spec.rb index ab1f90a493..c52e3c6f86 100644 --- a/spec/requests/consumer/suppliers_spec.rb +++ b/spec/requests/consumer/suppliers_spec.rb @@ -49,18 +49,23 @@ feature %q{ scenario "viewing products provided by a supplier" do # Given a supplier with a product - s = create(:supplier_enterprise, :name => 'Murrnong', :long_description => "

Hello, world!

") - p = create(:product, :supplier => s) + s1 = create(:supplier_enterprise, :name => 'Murrnong', :long_description => "

Hello, world!

") + p1 = create(:product, :supplier => s1) - # When I select the supplier + # And a different supplier with another product + s2 = create(:supplier_enterprise, :name => 'Red Herring') + p2 = create(:product, :supplier => s2) + + # When I select the first supplier visit spree.root_path - click_link s.name + click_link s1.name # Then I should see the supplier details - page.should have_selector 'h2', :text => s.name + page.should have_selector 'h2', :text => s1.name page.should have_selector 'div.enterprise-description', :text => 'Hello, world!' - # And I should see the product - page.should have_content p.name + # And I should see the first, but not the second product + page.should have_content p1.name + page.should_not have_content p2.name end end From 80159e1ea776471d22b622ec1ef1b44c5cc083fd Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 2 Nov 2012 15:34:02 +1100 Subject: [PATCH 45/52] Do not munge foreign keys when migrating to enterprises --- ...suppliers_and_distributors_into_enterprises.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/db/migrate/20121025012233_combine_suppliers_and_distributors_into_enterprises.rb b/db/migrate/20121025012233_combine_suppliers_and_distributors_into_enterprises.rb index 1d02ba1595..7eff3e6961 100644 --- a/db/migrate/20121025012233_combine_suppliers_and_distributors_into_enterprises.rb +++ b/db/migrate/20121025012233_combine_suppliers_and_distributors_into_enterprises.rb @@ -30,6 +30,7 @@ class CombineSuppliersAndDistributorsIntoEnterprises < ActiveRecord::Migration end # Copy suppliers to enterprises table with primary producer flag set + updated_product_ids = [-1] Supplier.all.each do |s| attrs = s.attributes attrs.reject! { |k| k == 'id' } @@ -37,10 +38,14 @@ class CombineSuppliersAndDistributorsIntoEnterprises < ActiveRecord::Migration e = Enterprise.create! attrs # Update supplier_id on product to point at the new enterprise - Spree::Product.update_all("supplier_id=#{e.id}", "supplier_id=#{s.id}") + product_ids = Spree::Product.where(:supplier_id => s.id).pluck(:id) + Spree::Product.update_all("supplier_id=#{e.id}", "supplier_id=#{s.id} AND id NOT IN (#{updated_product_ids.join(', ')})") + updated_product_ids += product_ids end # Copy distributors to enterprises table with distributor flag set + updated_product_distribution_ids = [-1] + updated_order_ids = [-1] Distributor.all.each do |d| attrs = d.attributes attrs['website'] = attrs['url'] @@ -49,8 +54,12 @@ class CombineSuppliersAndDistributorsIntoEnterprises < ActiveRecord::Migration e = Enterprise.create! attrs # Update distributor_id on product distribution and order to point at the new enterprise - ProductDistribution.update_all("distributor_id=#{e.id}", "distributor_id=#{d.id}") - Spree::Order.update_all("distributor_id=#{e.id}", "distributor_id=#{d.id}") + product_distribution_ids = ProductDistribution.where(:distributor_id => d.id).pluck(:id) + order_ids = Spree::Order.where(:distributor_id => d.id).pluck(:id) + ProductDistribution.update_all("distributor_id=#{e.id}", "distributor_id=#{d.id} AND id NOT IN (#{updated_product_distribution_ids.join(', ')})") + Spree::Order.update_all("distributor_id=#{e.id}", "distributor_id=#{d.id} AND id NOT IN (#{updated_order_ids.join(', ')})") + updated_product_distribution_ids += product_distribution_ids + updated_order_ids += order_ids end end From 9e4edd5da0cd5eab305a42159d577c09b1c027a7 Mon Sep 17 00:00:00 2001 From: Rob H Date: Fri, 2 Nov 2012 16:41:32 +1100 Subject: [PATCH 46/52] Change the basis of line_item grouping for Bulk Co-op and Order Cycle reports from User to Order --- .../admin/reports_controller_decorator.rb | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index b65b68fad1..551ffb914a 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -99,7 +99,7 @@ Spree::Admin::ReportsController.class_eval do columns = [ proc { |lis| lis.first.variant.product.supplier.name }, proc { |lis| lis.first.variant.product.name }, - proc { |lis| "UNIT SIZE" }, + proc { |lis| lis.first.variant.product.group_buy_unit_size }, proc { |lis| lis.first.variant.options_text }, proc { |lis| lis.first.variant.weight || 0 }, proc { |lis| lis.sum { |li| li.quantity } }, @@ -111,7 +111,7 @@ Spree::Admin::ReportsController.class_eval do sort_by: proc { |product| product.name }, summary_columns: [ proc { |lis| lis.first.variant.product.supplier.name }, proc { |lis| lis.first.variant.product.name }, - proc { |lis| "UNIT SIZE" }, + proc { |lis| lis.first.variant.product.group_buy_unit_size }, proc { |lis| "" }, proc { |lis| "" }, proc { |lis| lis.sum { |li| li.quantity * li.variant.weight || 0 } }, @@ -125,7 +125,7 @@ Spree::Admin::ReportsController.class_eval do columns = [ proc { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, proc { |lis| lis.first.variant.product.name }, - proc { |lis| "UNIT SIZE" }, + proc { |lis| lis.first.variant.product.group_buy_unit_size }, proc { |lis| lis.first.variant.options_text }, proc { |lis| lis.first.variant.weight || 0 }, proc { |lis| lis.sum { |li| li.quantity } }, @@ -135,8 +135,8 @@ Spree::Admin::ReportsController.class_eval do sort_by: proc { |product| product.name } }, { group_by: proc { |li| li.variant }, sort_by: proc { |variant| variant.options_text } }, - { group_by: proc { |li| li.order.user }, - sort_by: proc { |user| user.to_s } } ] + { group_by: proc { |li| li.order }, + sort_by: proc { |order| order.to_s } } ] when "bulk_coop_packing_sheets" @@ -151,22 +151,20 @@ Spree::Admin::ReportsController.class_eval do sort_by: proc { |product| product.name } }, { group_by: proc { |li| li.variant }, sort_by: proc { |variant| variant.options_text } }, - { group_by: proc { |li| li.order.user }, - sort_by: proc { |user| user.to_s } } ] + { group_by: proc { |li| li.order }, + sort_by: proc { |order| order.to_s } } ] when "bulk_coop_customer_payments" header = ["Customer", "Date of Order", "Total Cost", "Amount Owing", "Amount Paid"] columns = [ proc { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, - proc { |lis| lis.first.order.created_at.to_s }, + proc { |lis| lis.first.order.completed_at.to_s }, proc { |lis| lis.map { |li| li.order }.uniq.sum { |o| o.total } }, proc { |lis| lis.map { |li| li.order }.uniq.sum { |o| o.outstanding_balance } }, proc { |lis| lis.map { |li| li.order }.uniq.sum { |o| o.payment_total } } ] - rules = [ { group_by: proc { |li| li.order.user }, - sort_by: proc { |user| user.to_s } }, - { group_by: proc { |li| li.order }, + rules = [ { group_by: proc { |li| li.order }, sort_by: proc { |order| order.created_at } } ] else # List all line items @@ -175,7 +173,7 @@ Spree::Admin::ReportsController.class_eval do columns = [ proc { |lis| lis.first.variant.product.supplier.name }, proc { |lis| lis.first.variant.product.name }, - proc { |lis| "UNIT SIZE" }, + proc { |lis| lis.first.variant.product.group_buy_unit_size }, proc { |lis| lis.first.variant.options_text }, proc { |lis| lis.first.variant.weight || 0 }, proc { |lis| lis.sum { |li| li.quantity } }, @@ -415,11 +413,12 @@ Spree::Admin::ReportsController.class_eval do when "order_cycle_customer_totals" table_items = line_items - @include_blank = false + @include_blank = 'All' - header = ["Customer", "Email", "Phone", "Product", "Variant", "Amount", "Total Cost", "Paid?", "Packed?", "Shipped?"] + header = ["Distributor", "Customer", "Email", "Phone", "Product", "Variant", "Amount", "Total Cost", "Paid?", "Packed?", "Shipped?"] - columns = [ proc { |line_items| line_items.first.order.bill_address.firstname + " " + line_items.first.order.bill_address.lastname }, + columns = [ proc { |line_items| line_items.first.order.distributor.name }, + proc { |line_items| line_items.first.order.bill_address.firstname + " " + line_items.first.order.bill_address.lastname }, proc { |line_items| line_items.first.order.email }, proc { |line_items| line_items.first.order.bill_address.phone }, proc { |line_items| line_items.first.variant.product.name }, @@ -430,16 +429,19 @@ Spree::Admin::ReportsController.class_eval do proc { |line_items| "" }, proc { |line_items| "" } ] - rules = [ { group_by: proc { |line_item| line_item.order.user }, - sort_by: proc { |user| user.to_s }, - summary_columns: [ proc { |line_items| line_items.first.order.bill_address.firstname + " " + line_items.first.order.bill_address.lastname }, + rules = [ { group_by: proc { |line_item| line_item.order.distributor }, + sort_by: proc { |distributor| distributor.name } }, + { group_by: proc { |line_item| line_item.order }, + sort_by: proc { |order| order.completed_at }, + summary_columns: [ proc { |line_items| line_items.first.order.distributor.name }, + proc { |line_items| line_items.first.order.bill_address.firstname + " " + line_items.first.order.bill_address.lastname }, proc { |line_items| line_items.first.order.email }, proc { |line_items| line_items.first.order.bill_address.phone }, proc { |line_items| "TOTAL" }, proc { |line_items| "" }, proc { |line_items| "" }, proc { |line_items| line_items.sum { |li| li.quantity * li.variant.price } }, - proc { |line_items| "work out whether paid or not" }, + proc { |line_items| line_items.map { |li| li.order.paid? }.all? { |paid| paid == true } ? "Yes" : "No" }, proc { |line_items| "" }, proc { |line_items| "" } ] }, { group_by: proc { |line_item| line_item.variant.product }, @@ -449,6 +451,7 @@ Spree::Admin::ReportsController.class_eval do else table_items = line_items + @include_blank = 'All' header = ["Supplier", "Product", "Variant", "Amount", "Cost per Unit", "Total Cost", "Status", "Incoming Transport"] From 5f07df93168fa3878bd6f359d8ec3ca967c5226b Mon Sep 17 00:00:00 2001 From: Rob H Date: Sat, 3 Nov 2012 11:36:17 +1100 Subject: [PATCH 47/52] Add calculation fields to bulk co-op and order cycle reports --- .../admin/reports_controller_decorator.rb | 59 ++++++++++++++----- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index 551ffb914a..9ae942f731 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -95,7 +95,7 @@ Spree::Admin::ReportsController.class_eval do case params[:report_type] when "bulk_coop_supplier_report" - header = ["Supplier", "Product", "Unit Size", "Variant", "Weight", "Sum Total", "Sum Max Total"] + header = ["Supplier", "Product", "Unit Size", "Variant", "Weight", "Sum Total", "Sum Max Total", "Units Required", "Remainder"] columns = [ proc { |lis| lis.first.variant.product.supplier.name }, proc { |lis| lis.first.variant.product.name }, @@ -103,7 +103,9 @@ Spree::Admin::ReportsController.class_eval do proc { |lis| lis.first.variant.options_text }, proc { |lis| lis.first.variant.weight || 0 }, proc { |lis| lis.sum { |li| li.quantity } }, - proc { |lis| lis.sum { |li| li.max_quantity || 0 } } ] + proc { |lis| lis.sum { |li| li.max_quantity || 0 } }, + proc { |lis| "" }, + proc { |lis| "" } ] rules = [ { group_by: proc { |li| li.variant.product.supplier }, sort_by: proc { |supplier| supplier.name } }, @@ -115,13 +117,15 @@ Spree::Admin::ReportsController.class_eval do proc { |lis| "" }, proc { |lis| "" }, proc { |lis| lis.sum { |li| li.quantity * li.variant.weight || 0 } }, - proc { |lis| lis.sum { |li| (li.max_quantity || 0) * li.variant.weight || 0 } } ] }, + proc { |lis| lis.sum { |li| (li.max_quantity || 0) * li.variant.weight || 0 } }, + proc { |lis| ( lis.first.variant.product.group_buy_unit_size.zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity, li.quantity].max || 0 ) * li.variant.weight || 0 } / lis.first.variant.product.group_buy_unit_size ) ).floor }, + proc { |lis| lis.sum { |li| ( [li.max_quantity, li.quantity].max || 0 ) * li.variant.weight || 0 } - ( ( lis.first.variant.product.group_buy_unit_size.zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity, li.quantity].max || 0 ) * li.variant.weight || 0 } / lis.first.variant.product.group_buy_unit_size ) ).floor * lis.first.variant.product.group_buy_unit_size ) } ] }, { group_by: proc { |li| li.variant }, sort_by: proc { |variant| variant.options_text } } ] when "bulk_coop_allocation" - header = ["Customer", "Product", "Unit Size", "Variant", "Weight", "Sum Total", "Sum Max Total"] + header = ["Customer", "Product", "Unit Size", "Variant", "Weight", "Sum Total", "Sum Max Total", "Total Allocated", "Remainder"] columns = [ proc { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, proc { |lis| lis.first.variant.product.name }, @@ -129,10 +133,21 @@ Spree::Admin::ReportsController.class_eval do proc { |lis| lis.first.variant.options_text }, proc { |lis| lis.first.variant.weight || 0 }, proc { |lis| lis.sum { |li| li.quantity } }, - proc { |lis| lis.sum { |li| li.max_quantity || 0 } } ] + proc { |lis| lis.sum { |li| li.max_quantity || 0 } }, + proc { |lis| "" }, + proc { |lis| "" } ] rules = [ { group_by: proc { |li| li.variant.product }, - sort_by: proc { |product| product.name } }, + sort_by: proc { |product| product.name }, + summary_columns: [ proc { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, + proc { |lis| lis.first.variant.product.name }, + proc { |lis| lis.first.variant.product.group_buy_unit_size }, + proc { |lis| "" }, + proc { |lis| "" }, + proc { |lis| lis.sum { |li| li.quantity * li.variant.weight || 0 } }, + proc { |lis| lis.sum { |li| (li.max_quantity || 0) * li.variant.weight || 0 } }, + proc { |lis| ( lis.first.variant.product.group_buy_unit_size.zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity, li.quantity].max || 0 ) * li.variant.weight || 0 } / lis.first.variant.product.group_buy_unit_size ) ).floor * lis.first.variant.product.group_buy_unit_size }, + proc { |lis| lis.sum { |li| ( [li.max_quantity, li.quantity].max || 0 ) * li.variant.weight || 0 } - ( ( lis.first.variant.product.group_buy_unit_size.zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity, li.quantity].max || 0 ) * li.variant.weight || 0 } / lis.first.variant.product.group_buy_unit_size ) ).floor * lis.first.variant.product.group_buy_unit_size ) } ] }, { group_by: proc { |li| li.variant }, sort_by: proc { |variant| variant.options_text } }, { group_by: proc { |li| li.order }, @@ -169,7 +184,7 @@ Spree::Admin::ReportsController.class_eval do else # List all line items - header = ["Supplier", "Product", "Unit Size", "Variant", "Weight", "Sum Total", "Sum Max Total"] + header = ["Supplier", "Product", "Unit Size", "Variant", "Weight", "Sum Total", "Sum Max Total", "Units Required", "Remainder"] columns = [ proc { |lis| lis.first.variant.product.supplier.name }, proc { |lis| lis.first.variant.product.name }, @@ -177,14 +192,26 @@ Spree::Admin::ReportsController.class_eval do proc { |lis| lis.first.variant.options_text }, proc { |lis| lis.first.variant.weight || 0 }, proc { |lis| lis.sum { |li| li.quantity } }, - proc { |lis| lis.sum { |li| li.max_quantity || 0 } } ] + proc { |lis| lis.sum { |li| li.max_quantity || 0 } }, + proc { |lis| "" }, + proc { |lis| "" } ] rules = [ { group_by: proc { |li| li.variant.product.supplier }, sort_by: proc { |supplier| supplier.name } }, { group_by: proc { |li| li.variant.product }, - sort_by: proc { |product| product.name } }, + sort_by: proc { |product| product.name }, + summary_columns: [ proc { |lis| lis.first.variant.product.supplier.name }, + proc { |lis| lis.first.variant.product.name }, + proc { |lis| lis.first.variant.product.group_buy_unit_size }, + proc { |lis| "" }, + proc { |lis| "" }, + proc { |lis| lis.sum { |li| li.quantity * li.variant.weight || 0 } }, + proc { |lis| lis.sum { |li| (li.max_quantity || 0) * li.variant.weight || 0 } }, + proc { |lis| ( lis.first.variant.product.group_buy_unit_size.zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity, li.quantity].max || 0 ) * li.variant.weight || 0 } / lis.first.variant.product.group_buy_unit_size ) ).floor }, + proc { |lis| lis.sum { |li| ( [li.max_quantity, li.quantity].max || 0 ) * li.variant.weight || 0 } - ( ( lis.first.variant.product.group_buy_unit_size.zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity, li.quantity].max || 0 ) * li.variant.weight || 0 } / lis.first.variant.product.group_buy_unit_size ) ).floor * lis.first.variant.product.group_buy_unit_size ) } ] }, { group_by: proc { |li| li.variant }, sort_by: proc { |variant| variant.options_text } } ] + end order_grouper = OpenFoodWeb::OrderGrouper.new rules, columns @@ -335,7 +362,7 @@ Spree::Admin::ReportsController.class_eval do proc { |line_items| line_items.sum { |li| li.quantity } }, proc { |line_items| line_items.first.variant.price }, proc { |line_items| line_items.sum { |li| li.quantity * li.variant.price } }, - proc { |line_items| "status" }, + proc { |line_items| "" }, proc { |line_items| "incoming transport" } ] rules = [ { group_by: proc { |line_item| line_item.variant.product.supplier }, @@ -402,7 +429,7 @@ Spree::Admin::ReportsController.class_eval do proc { |line_items| "" }, proc { |line_items| "" }, proc { |line_items| line_items.sum { |li| li.quantity * li.variant.price } }, - proc { |line_items| "total shipping cost" }, + proc { |line_items| line_items.map { |li| li.order }.uniq.sum { |o| o.ship_total } }, proc { |line_items| "" } ] }, { group_by: proc { |line_item| line_item.variant.product.supplier }, sort_by: proc { |supplier| supplier.name } }, @@ -415,7 +442,7 @@ Spree::Admin::ReportsController.class_eval do table_items = line_items @include_blank = 'All' - header = ["Distributor", "Customer", "Email", "Phone", "Product", "Variant", "Amount", "Total Cost", "Paid?", "Packed?", "Shipped?"] + header = ["Distributor", "Customer", "Email", "Phone", "Product", "Variant", "Amount", "Item ($)", "Ship ($)", "Total ($)", "Paid?", "Packed?", "Shipped?"] columns = [ proc { |line_items| line_items.first.order.distributor.name }, proc { |line_items| line_items.first.order.bill_address.firstname + " " + line_items.first.order.bill_address.lastname }, @@ -427,12 +454,14 @@ Spree::Admin::ReportsController.class_eval do proc { |line_items| line_items.sum { |li| li.quantity * li.variant.price } }, proc { |line_items| "" }, proc { |line_items| "" }, + proc { |line_items| "" }, + proc { |line_items| "" }, proc { |line_items| "" } ] rules = [ { group_by: proc { |line_item| line_item.order.distributor }, sort_by: proc { |distributor| distributor.name } }, { group_by: proc { |line_item| line_item.order }, - sort_by: proc { |order| order.completed_at }, + sort_by: proc { |order| order.bill_address.lastname + " " + order.bill_address.firstname }, summary_columns: [ proc { |line_items| line_items.first.order.distributor.name }, proc { |line_items| line_items.first.order.bill_address.firstname + " " + line_items.first.order.bill_address.lastname }, proc { |line_items| line_items.first.order.email }, @@ -441,6 +470,8 @@ Spree::Admin::ReportsController.class_eval do proc { |line_items| "" }, proc { |line_items| "" }, proc { |line_items| line_items.sum { |li| li.quantity * li.variant.price } }, + proc { |line_items| line_items.map { |li| li.order }.uniq.sum { |o| o.ship_total } }, + proc { |line_items| line_items.map { |li| li.order }.uniq.sum { |o| o.total } }, proc { |line_items| line_items.map { |li| li.order.paid? }.all? { |paid| paid == true } ? "Yes" : "No" }, proc { |line_items| "" }, proc { |line_items| "" } ] }, @@ -461,7 +492,7 @@ Spree::Admin::ReportsController.class_eval do proc { |line_items| line_items.sum { |li| li.quantity } }, proc { |line_items| line_items.first.variant.price }, proc { |line_items| line_items.sum { |li| li.quantity * li.variant.price } }, - proc { |line_items| "status" }, + proc { |line_items| "" }, proc { |line_items| "incoming transport" } ] rules = [ { group_by: proc { |line_item| line_item.variant.product.supplier }, From a15f96aa686f72322a77fe31af5d4c2db385e95a Mon Sep 17 00:00:00 2001 From: Rob H Date: Wed, 7 Nov 2012 18:34:54 +1100 Subject: [PATCH 48/52] Fix for case where no group_buy_size_has been set. ie. = nil --- .../admin/reports_controller_decorator.rb | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index 9ae942f731..93d9168ae8 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -99,7 +99,7 @@ Spree::Admin::ReportsController.class_eval do columns = [ proc { |lis| lis.first.variant.product.supplier.name }, proc { |lis| lis.first.variant.product.name }, - proc { |lis| lis.first.variant.product.group_buy_unit_size }, + proc { |lis| lis.first.variant.product.group_buy_unit_size || 0.0 }, proc { |lis| lis.first.variant.options_text }, proc { |lis| lis.first.variant.weight || 0 }, proc { |lis| lis.sum { |li| li.quantity } }, @@ -113,13 +113,13 @@ Spree::Admin::ReportsController.class_eval do sort_by: proc { |product| product.name }, summary_columns: [ proc { |lis| lis.first.variant.product.supplier.name }, proc { |lis| lis.first.variant.product.name }, - proc { |lis| lis.first.variant.product.group_buy_unit_size }, + proc { |lis| lis.first.variant.product.group_buy_unit_size || 0.0 }, proc { |lis| "" }, proc { |lis| "" }, proc { |lis| lis.sum { |li| li.quantity * li.variant.weight || 0 } }, proc { |lis| lis.sum { |li| (li.max_quantity || 0) * li.variant.weight || 0 } }, - proc { |lis| ( lis.first.variant.product.group_buy_unit_size.zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity, li.quantity].max || 0 ) * li.variant.weight || 0 } / lis.first.variant.product.group_buy_unit_size ) ).floor }, - proc { |lis| lis.sum { |li| ( [li.max_quantity, li.quantity].max || 0 ) * li.variant.weight || 0 } - ( ( lis.first.variant.product.group_buy_unit_size.zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity, li.quantity].max || 0 ) * li.variant.weight || 0 } / lis.first.variant.product.group_buy_unit_size ) ).floor * lis.first.variant.product.group_buy_unit_size ) } ] }, + proc { |lis| ( (lis.first.variant.product.group_buy_unit_size || 0).zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * li.variant.weight || 0 } / lis.first.variant.product.group_buy_unit_size ) ).floor }, + proc { |lis| lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max) * li.variant.weight || 0 } - ( ( (lis.first.variant.product.group_buy_unit_size || 0).zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max) * li.variant.weight || 0 } / lis.first.variant.product.group_buy_unit_size ) ).floor * (lis.first.variant.product.group_buy_unit_size || 0) ) } ] }, { group_by: proc { |li| li.variant }, sort_by: proc { |variant| variant.options_text } } ] @@ -129,7 +129,7 @@ Spree::Admin::ReportsController.class_eval do columns = [ proc { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, proc { |lis| lis.first.variant.product.name }, - proc { |lis| lis.first.variant.product.group_buy_unit_size }, + proc { |lis| lis.first.variant.product.group_buy_unit_size || 0.0 }, proc { |lis| lis.first.variant.options_text }, proc { |lis| lis.first.variant.weight || 0 }, proc { |lis| lis.sum { |li| li.quantity } }, @@ -141,13 +141,13 @@ Spree::Admin::ReportsController.class_eval do sort_by: proc { |product| product.name }, summary_columns: [ proc { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, proc { |lis| lis.first.variant.product.name }, - proc { |lis| lis.first.variant.product.group_buy_unit_size }, + proc { |lis| lis.first.variant.product.group_buy_unit_size || 0.0 }, proc { |lis| "" }, proc { |lis| "" }, proc { |lis| lis.sum { |li| li.quantity * li.variant.weight || 0 } }, proc { |lis| lis.sum { |li| (li.max_quantity || 0) * li.variant.weight || 0 } }, - proc { |lis| ( lis.first.variant.product.group_buy_unit_size.zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity, li.quantity].max || 0 ) * li.variant.weight || 0 } / lis.first.variant.product.group_buy_unit_size ) ).floor * lis.first.variant.product.group_buy_unit_size }, - proc { |lis| lis.sum { |li| ( [li.max_quantity, li.quantity].max || 0 ) * li.variant.weight || 0 } - ( ( lis.first.variant.product.group_buy_unit_size.zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity, li.quantity].max || 0 ) * li.variant.weight || 0 } / lis.first.variant.product.group_buy_unit_size ) ).floor * lis.first.variant.product.group_buy_unit_size ) } ] }, + proc { |lis| ( (lis.first.variant.product.group_buy_unit_size || 0).zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * li.variant.weight || 0 } / lis.first.variant.product.group_buy_unit_size ) ).floor * (lis.first.variant.product.group_buy_unit_size || 0) }, + proc { |lis| lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * li.variant.weight || 0 } - ( ( (lis.first.variant.product.group_buy_unit_size || 0).zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * li.variant.weight || 0 } / lis.first.variant.product.group_buy_unit_size ) ).floor * (lis.first.variant.product.group_buy_unit_size || 0) ) } ] }, { group_by: proc { |li| li.variant }, sort_by: proc { |variant| variant.options_text } }, { group_by: proc { |li| li.order }, @@ -188,7 +188,7 @@ Spree::Admin::ReportsController.class_eval do columns = [ proc { |lis| lis.first.variant.product.supplier.name }, proc { |lis| lis.first.variant.product.name }, - proc { |lis| lis.first.variant.product.group_buy_unit_size }, + proc { |lis| lis.first.variant.product.group_buy_unit_size || 0.0 }, proc { |lis| lis.first.variant.options_text }, proc { |lis| lis.first.variant.weight || 0 }, proc { |lis| lis.sum { |li| li.quantity } }, @@ -202,13 +202,13 @@ Spree::Admin::ReportsController.class_eval do sort_by: proc { |product| product.name }, summary_columns: [ proc { |lis| lis.first.variant.product.supplier.name }, proc { |lis| lis.first.variant.product.name }, - proc { |lis| lis.first.variant.product.group_buy_unit_size }, + proc { |lis| lis.first.variant.product.group_buy_unit_size || 0.0 }, proc { |lis| "" }, proc { |lis| "" }, proc { |lis| lis.sum { |li| li.quantity * li.variant.weight || 0 } }, proc { |lis| lis.sum { |li| (li.max_quantity || 0) * li.variant.weight || 0 } }, - proc { |lis| ( lis.first.variant.product.group_buy_unit_size.zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity, li.quantity].max || 0 ) * li.variant.weight || 0 } / lis.first.variant.product.group_buy_unit_size ) ).floor }, - proc { |lis| lis.sum { |li| ( [li.max_quantity, li.quantity].max || 0 ) * li.variant.weight || 0 } - ( ( lis.first.variant.product.group_buy_unit_size.zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity, li.quantity].max || 0 ) * li.variant.weight || 0 } / lis.first.variant.product.group_buy_unit_size ) ).floor * lis.first.variant.product.group_buy_unit_size ) } ] }, + proc { |lis| ( (lis.first.variant.product.group_buy_unit_size || 0).zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * li.variant.weight || 0 } / lis.first.variant.product.group_buy_unit_size ) ).floor }, + proc { |lis| lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * li.variant.weight || 0 } - ( ( (lis.first.variant.product.group_buy_unit_size || 0).zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * li.variant.weight || 0 } / lis.first.variant.product.group_buy_unit_size ) ).floor * (lis.first.variant.product.group_buy_unit_size || 0) ) } ] }, { group_by: proc { |li| li.variant }, sort_by: proc { |variant| variant.options_text } } ] From 9a9bef5304c8a6a012fa819d3ad37d729efe3243 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 9 Nov 2012 20:31:08 +1100 Subject: [PATCH 49/52] Report filtering on completion date, not creation date --- .../admin/reports_controller_decorator.rb | 62 +++++++++---------- .../spree/admin/reports/bulk_coop.html.haml | 6 +- .../spree/admin/reports/group_buys.html.haml | 4 +- .../admin/reports/order_cycles.html.haml | 6 +- .../reports/orders_and_distributors.html.haml | 4 +- .../spree/admin/reports/payments.html.haml | 6 +- 6 files changed, 44 insertions(+), 44 deletions(-) diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index 93d9168ae8..046fd05525 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -14,16 +14,16 @@ Spree::Admin::ReportsController.class_eval do def orders_and_distributors params[:q] = {} unless params[:q] - if params[:q][:created_at_gt].blank? - params[:q][:created_at_gt] = Time.zone.now.beginning_of_month + if params[:q][:completed_at_gt].blank? + params[:q][:completed_at_gt] = Time.zone.now.beginning_of_month else - params[:q][:created_at_gt] = Time.zone.parse(params[:q][:created_at_gt]).beginning_of_day rescue Time.zone.now.beginning_of_month + params[:q][:completed_at_gt] = Time.zone.parse(params[:q][:completed_at_gt]).beginning_of_day rescue Time.zone.now.beginning_of_month end - if params[:q] && !params[:q][:created_at_lt].blank? - params[:q][:created_at_lt] = Time.zone.parse(params[:q][:created_at_lt]).end_of_day rescue "" + if params[:q] && !params[:q][:completed_at_lt].blank? + params[:q][:completed_at_lt] = Time.zone.parse(params[:q][:completed_at_lt]).end_of_day rescue "" end - params[:q][:meta_sort] ||= "created_at.desc" + params[:q][:meta_sort] ||= "completed_at.desc" @search = Spree::Order.complete.search(params[:q]) orders = @search.result @@ -43,16 +43,16 @@ Spree::Admin::ReportsController.class_eval do def group_buys params[:q] = {} unless params[:q] - if params[:q][:created_at_gt].blank? - params[:q][:created_at_gt] = Time.zone.now.beginning_of_month + if params[:q][:completed_at_gt].blank? + params[:q][:completed_at_gt] = Time.zone.now.beginning_of_month else - params[:q][:created_at_gt] = Time.zone.parse(params[:q][:created_at_gt]).beginning_of_day rescue Time.zone.now.beginning_of_month + params[:q][:completed_at_gt] = Time.zone.parse(params[:q][:completed_at_gt]).beginning_of_day rescue Time.zone.now.beginning_of_month end - if params[:q] && !params[:q][:created_at_lt].blank? - params[:q][:created_at_lt] = Time.zone.parse(params[:q][:created_at_lt]).end_of_day rescue "" + if params[:q] && !params[:q][:completed_at_lt].blank? + params[:q][:completed_at_lt] = Time.zone.parse(params[:q][:completed_at_lt]).end_of_day rescue "" end - params[:q][:meta_sort] ||= "created_at.desc" + params[:q][:meta_sort] ||= "completed_at.desc" @search = Spree::Order.complete.search(params[:q]) orders = @search.result @@ -74,16 +74,16 @@ Spree::Admin::ReportsController.class_eval do def bulk_coop params[:q] = {} unless params[:q] - if params[:q][:created_at_gt].blank? - params[:q][:created_at_gt] = Time.zone.now.beginning_of_month + if params[:q][:completed_at_gt].blank? + params[:q][:completed_at_gt] = Time.zone.now.beginning_of_month else - params[:q][:created_at_gt] = Time.zone.parse(params[:q][:created_at_gt]).beginning_of_day rescue Time.zone.now.beginning_of_month + params[:q][:completed_at_gt] = Time.zone.parse(params[:q][:completed_at_gt]).beginning_of_day rescue Time.zone.now.beginning_of_month end - if params[:q] && !params[:q][:created_at_lt].blank? - params[:q][:created_at_lt] = Time.zone.parse(params[:q][:created_at_lt]).end_of_day rescue "" + if params[:q] && !params[:q][:completed_at_lt].blank? + params[:q][:completed_at_lt] = Time.zone.parse(params[:q][:completed_at_lt]).end_of_day rescue "" end - params[:q][:meta_sort] ||= "created_at.desc" + params[:q][:meta_sort] ||= "completed_at.desc" @search = Spree::Order.complete.search(params[:q]) orders = @search.result @@ -180,7 +180,7 @@ Spree::Admin::ReportsController.class_eval do proc { |lis| lis.map { |li| li.order }.uniq.sum { |o| o.payment_total } } ] rules = [ { group_by: proc { |li| li.order }, - sort_by: proc { |order| order.created_at } } ] + sort_by: proc { |order| order.completed_at } } ] else # List all line items @@ -226,16 +226,16 @@ Spree::Admin::ReportsController.class_eval do def payments params[:q] = {} unless params[:q] - if params[:q][:created_at_gt].blank? - params[:q][:created_at_gt] = Time.zone.now.beginning_of_month + if params[:q][:completed_at_gt].blank? + params[:q][:completed_at_gt] = Time.zone.now.beginning_of_month else - params[:q][:created_at_gt] = Time.zone.parse(params[:q][:created_at_gt]).beginning_of_day rescue Time.zone.now.beginning_of_month + params[:q][:completed_at_gt] = Time.zone.parse(params[:q][:completed_at_gt]).beginning_of_day rescue Time.zone.now.beginning_of_month end - if params[:q] && !params[:q][:created_at_lt].blank? - params[:q][:created_at_lt] = Time.zone.parse(params[:q][:created_at_lt]).end_of_day rescue "" + if params[:q] && !params[:q][:completed_at_lt].blank? + params[:q][:completed_at_lt] = Time.zone.parse(params[:q][:completed_at_lt]).end_of_day rescue "" end - params[:q][:meta_sort] ||= "created_at.desc" + params[:q][:meta_sort] ||= "completed_at.desc" @search = Spree::Order.complete.search(params[:q]) orders = @search.result @@ -330,16 +330,16 @@ Spree::Admin::ReportsController.class_eval do def order_cycles params[:q] = {} unless params[:q] - if params[:q][:created_at_gt].blank? - params[:q][:created_at_gt] = Time.zone.now.beginning_of_month + if params[:q][:completed_at_gt].blank? + params[:q][:completed_at_gt] = Time.zone.now.beginning_of_month else - params[:q][:created_at_gt] = Time.zone.parse(params[:q][:created_at_gt]).beginning_of_day rescue Time.zone.now.beginning_of_month + params[:q][:completed_at_gt] = Time.zone.parse(params[:q][:completed_at_gt]).beginning_of_day rescue Time.zone.now.beginning_of_month end - if params[:q] && !params[:q][:created_at_lt].blank? - params[:q][:created_at_lt] = Time.zone.parse(params[:q][:created_at_lt]).end_of_day rescue "" + if params[:q] && !params[:q][:completed_at_lt].blank? + params[:q][:completed_at_lt] = Time.zone.parse(params[:q][:completed_at_lt]).end_of_day rescue "" end - params[:q][:meta_sort] ||= "created_at.desc" + params[:q][:meta_sort] ||= "completed_at.desc" @search = Spree::Order.complete.search(params[:q]) orders = @search.result diff --git a/app/views/spree/admin/reports/bulk_coop.html.haml b/app/views/spree/admin/reports/bulk_coop.html.haml index 8c570e8de1..adde173e98 100644 --- a/app/views/spree/admin/reports/bulk_coop.html.haml +++ b/app/views/spree/admin/reports/bulk_coop.html.haml @@ -3,11 +3,11 @@ %br .date-range-filter %div{"class" => "left sub-field"} - = f.text_field :created_at_gt, :class => 'datepicker' + = f.text_field :completed_at_gt, :class => 'datepicker' %br = label_tag nil, t(:start), :class => 'sub' %div{"class" => "right sub-field"} - = f.text_field :created_at_lt, :class => 'datepicker' + = f.text_field :completed_at_lt, :class => 'datepicker' %br = label_tag nil, t(:stop) %br @@ -36,4 +36,4 @@ %td= column - if @table.empty? %tr - %td{:colspan => "2"}= t(:none) \ No newline at end of file + %td{:colspan => "2"}= t(:none) diff --git a/app/views/spree/admin/reports/group_buys.html.haml b/app/views/spree/admin/reports/group_buys.html.haml index f593483d57..bdda9a043d 100644 --- a/app/views/spree/admin/reports/group_buys.html.haml +++ b/app/views/spree/admin/reports/group_buys.html.haml @@ -3,11 +3,11 @@ %br .date-range-filter %div{"class" => "left sub-field"} - = f.text_field :created_at_gt, :class => 'datepicker' + = f.text_field :completed_at_gt, :class => 'datepicker' %br = label_tag nil, t(:start), :class => 'sub' %div{"class" => "right sub-field"} - = f.text_field :created_at_lt, :class => 'datepicker' + = f.text_field :completed_at_lt, :class => 'datepicker' %br = label_tag nil, t(:stop) %br diff --git a/app/views/spree/admin/reports/order_cycles.html.haml b/app/views/spree/admin/reports/order_cycles.html.haml index 33bd97cb06..4468f1d183 100644 --- a/app/views/spree/admin/reports/order_cycles.html.haml +++ b/app/views/spree/admin/reports/order_cycles.html.haml @@ -3,11 +3,11 @@ %br .date-range-filter %div{"class" => "left sub-field"} - = f.text_field :created_at_gt, :class => 'datepicker' + = f.text_field :completed_at_gt, :class => 'datepicker' %br = label_tag nil, t(:start), :class => 'sub' %div{"class" => "right sub-field"} - = f.text_field :created_at_lt, :class => 'datepicker' + = f.text_field :completed_at_lt, :class => 'datepicker' %br = label_tag nil, t(:stop) %br @@ -36,4 +36,4 @@ %td= column - if @table.empty? %tr - %td{:colspan => "2"}= t(:none) \ No newline at end of file + %td{:colspan => "2"}= t(:none) diff --git a/app/views/spree/admin/reports/orders_and_distributors.html.haml b/app/views/spree/admin/reports/orders_and_distributors.html.haml index 08fba94e7e..26137158b0 100644 --- a/app/views/spree/admin/reports/orders_and_distributors.html.haml +++ b/app/views/spree/admin/reports/orders_and_distributors.html.haml @@ -3,11 +3,11 @@ %br .date-range-filter %div{"class" => "left sub-field"} - = s.text_field :created_at_gt, :class => 'datepicker' + = s.text_field :completed_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' + = s.text_field :completed_at_lt, :class => 'datepicker' %br = label_tag nil, t(:stop) = check_box_tag :csv diff --git a/app/views/spree/admin/reports/payments.html.haml b/app/views/spree/admin/reports/payments.html.haml index 898bba70ef..66da52ad4b 100644 --- a/app/views/spree/admin/reports/payments.html.haml +++ b/app/views/spree/admin/reports/payments.html.haml @@ -3,11 +3,11 @@ %br .date-range-filter %div{"class" => "left sub-field"} - = f.text_field :created_at_gt, :class => 'datepicker' + = f.text_field :completed_at_gt, :class => 'datepicker' %br = label_tag nil, t(:start), :class => 'sub' %div{"class" => "right sub-field"} - = f.text_field :created_at_lt, :class => 'datepicker' + = f.text_field :completed_at_lt, :class => 'datepicker' %br = label_tag nil, t(:stop) %br @@ -36,4 +36,4 @@ %td= column - if @table.empty? %tr - %td{:colspan => "2"}= t(:none) \ No newline at end of file + %td{:colspan => "2"}= t(:none) From 580486a34770689a7f9c52c2273f8cbcfce2fb3a Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Sat, 10 Nov 2012 12:26:41 +1100 Subject: [PATCH 50/52] Do not show duplicate products with in_supplier_or_distributor scope --- app/models/spree/product_decorator.rb | 3 ++- spec/models/product_spec.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index bd6626d296..cd809d880e 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -12,7 +12,8 @@ Spree::Product.class_eval do scope :in_supplier, lambda { |supplier| where(:supplier_id => supplier) } scope :in_distributor, lambda { |distributor| joins(:product_distributions).where('product_distributions.distributor_id = ?', (distributor.respond_to?(:id) ? distributor.id : distributor.to_i)) } - scope :in_supplier_or_distributor, lambda { |enterprise| joins('LEFT OUTER JOIN product_distributions ON product_distributions.product_id=spree_products.id'). + scope :in_supplier_or_distributor, lambda { |enterprise| select('distinct spree_products.*'). + joins('LEFT OUTER JOIN product_distributions ON product_distributions.product_id=spree_products.id'). where('supplier_id=? OR product_distributions.distributor_id=?', enterprise.respond_to?(:id) ? enterprise.id : enterprise.to_i, enterprise.respond_to?(:id) ? enterprise.id : enterprise.to_i) } diff --git a/spec/models/product_spec.rb b/spec/models/product_spec.rb index 5943218037..ef777ef5c7 100644 --- a/spec/models/product_spec.rb +++ b/spec/models/product_spec.rb @@ -47,6 +47,18 @@ describe Spree::Product do Spree::Product.in_supplier_or_distributor(s).should == [p] Spree::Product.in_supplier_or_distributor(d).should == [p] end + + it "shows each product once when it is distributed by many distributors" do + s = create(:supplier_enterprise) + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) + d3 = create(:distributor_enterprise) + p = create(:product, :supplier => s, :distributors => [d1, d2, d3]) + + [s, d1, d2, d3].each do |enterprise| + Spree::Product.in_supplier_or_distributor(enterprise).should == [p] + end + end end end From 076c64f7b07b46631132d18a340f5f8670da2974 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Sat, 10 Nov 2012 12:53:03 +1100 Subject: [PATCH 51/52] Brackets make everything better --- .../spree/admin/reports_controller_decorator.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index 046fd05525..2d8324b0a6 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -205,10 +205,10 @@ Spree::Admin::ReportsController.class_eval do proc { |lis| lis.first.variant.product.group_buy_unit_size || 0.0 }, proc { |lis| "" }, proc { |lis| "" }, - proc { |lis| lis.sum { |li| li.quantity * li.variant.weight || 0 } }, - proc { |lis| lis.sum { |li| (li.max_quantity || 0) * li.variant.weight || 0 } }, - proc { |lis| ( (lis.first.variant.product.group_buy_unit_size || 0).zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * li.variant.weight || 0 } / lis.first.variant.product.group_buy_unit_size ) ).floor }, - proc { |lis| lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * li.variant.weight || 0 } - ( ( (lis.first.variant.product.group_buy_unit_size || 0).zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * li.variant.weight || 0 } / lis.first.variant.product.group_buy_unit_size ) ).floor * (lis.first.variant.product.group_buy_unit_size || 0) ) } ] }, + proc { |lis| lis.sum { |li| li.quantity * (li.variant.weight || 0) } }, + proc { |lis| lis.sum { |li| (li.max_quantity || 0) * (li.variant.weight || 0) } }, + proc { |lis| ( (lis.first.variant.product.group_buy_unit_size || 0).zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * (li.variant.weight || 0) } / lis.first.variant.product.group_buy_unit_size ) ).floor }, + proc { |lis| lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * (li.variant.weight || 0) } - ( ( (lis.first.variant.product.group_buy_unit_size || 0).zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * (li.variant.weight || 0) } / lis.first.variant.product.group_buy_unit_size ) ).floor * (lis.first.variant.product.group_buy_unit_size || 0) ) } ] }, { group_by: proc { |li| li.variant }, sort_by: proc { |variant| variant.options_text } } ] From 9cb5ac95ff1ee378095dd0f58e06c9271a3140bd Mon Sep 17 00:00:00 2001 From: Rob H Date: Sun, 11 Nov 2012 15:30:01 +1100 Subject: [PATCH 52/52] More brackets make everything even better --- .../spree/admin/reports_controller_decorator.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index 2d8324b0a6..ed5e9f5b8f 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -116,10 +116,10 @@ Spree::Admin::ReportsController.class_eval do proc { |lis| lis.first.variant.product.group_buy_unit_size || 0.0 }, proc { |lis| "" }, proc { |lis| "" }, - proc { |lis| lis.sum { |li| li.quantity * li.variant.weight || 0 } }, - proc { |lis| lis.sum { |li| (li.max_quantity || 0) * li.variant.weight || 0 } }, - proc { |lis| ( (lis.first.variant.product.group_buy_unit_size || 0).zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * li.variant.weight || 0 } / lis.first.variant.product.group_buy_unit_size ) ).floor }, - proc { |lis| lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max) * li.variant.weight || 0 } - ( ( (lis.first.variant.product.group_buy_unit_size || 0).zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max) * li.variant.weight || 0 } / lis.first.variant.product.group_buy_unit_size ) ).floor * (lis.first.variant.product.group_buy_unit_size || 0) ) } ] }, + proc { |lis| lis.sum { |li| (li.quantity || 0) * (li.variant.weight || 0) } }, + proc { |lis| lis.sum { |li| (li.max_quantity || 0) * (li.variant.weight || 0) } }, + proc { |lis| ( (lis.first.variant.product.group_buy_unit_size || 0).zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * (li.variant.weight || 0) } / lis.first.variant.product.group_buy_unit_size ) ).floor }, + proc { |lis| lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max) * (li.variant.weight || 0) } - ( ( (lis.first.variant.product.group_buy_unit_size || 0).zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max) * (li.variant.weight || 0) } / lis.first.variant.product.group_buy_unit_size ) ).floor * (lis.first.variant.product.group_buy_unit_size || 0) ) } ] }, { group_by: proc { |li| li.variant }, sort_by: proc { |variant| variant.options_text } } ] @@ -144,10 +144,10 @@ Spree::Admin::ReportsController.class_eval do proc { |lis| lis.first.variant.product.group_buy_unit_size || 0.0 }, proc { |lis| "" }, proc { |lis| "" }, - proc { |lis| lis.sum { |li| li.quantity * li.variant.weight || 0 } }, - proc { |lis| lis.sum { |li| (li.max_quantity || 0) * li.variant.weight || 0 } }, - proc { |lis| ( (lis.first.variant.product.group_buy_unit_size || 0).zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * li.variant.weight || 0 } / lis.first.variant.product.group_buy_unit_size ) ).floor * (lis.first.variant.product.group_buy_unit_size || 0) }, - proc { |lis| lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * li.variant.weight || 0 } - ( ( (lis.first.variant.product.group_buy_unit_size || 0).zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * li.variant.weight || 0 } / lis.first.variant.product.group_buy_unit_size ) ).floor * (lis.first.variant.product.group_buy_unit_size || 0) ) } ] }, + proc { |lis| lis.sum { |li| li.quantity * (li.variant.weight || 0) } }, + proc { |lis| lis.sum { |li| (li.max_quantity || 0) * (li.variant.weight || 0) } }, + proc { |lis| ( (lis.first.variant.product.group_buy_unit_size || 0).zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * (li.variant.weight || 0) } / lis.first.variant.product.group_buy_unit_size ) ).floor * (lis.first.variant.product.group_buy_unit_size || 0) }, + proc { |lis| lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * (li.variant.weight || 0) } - ( ( (lis.first.variant.product.group_buy_unit_size || 0).zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * (li.variant.weight || 0) } / lis.first.variant.product.group_buy_unit_size ) ).floor * (lis.first.variant.product.group_buy_unit_size || 0) ) } ] }, { group_by: proc { |li| li.variant }, sort_by: proc { |variant| variant.options_text } }, { group_by: proc { |li| li.order },