From 7350dbb75e5da61bc172a352f4963771475dde8d Mon Sep 17 00:00:00 2001 From: Lynne Davis Date: Wed, 16 Sep 2015 16:09:51 +0100 Subject: [PATCH 1/3] Adding tweaks that have been missed from merges --- .../admin/reports_controller_decorator.rb | 26 ++++++-- .../reports/order_cycle_management.html.haml | 63 +++++++++---------- .../order_cycle_management_report.rb | 23 ++++--- lib/open_food_network/packing_report.rb | 4 +- spec/features/admin/reports_spec.rb | 4 +- 5 files changed, 69 insertions(+), 51 deletions(-) diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index 50762a97e1..e5fa85fe0b 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -77,14 +77,27 @@ Spree::Admin::ReportsController.class_eval do end def order_cycle_management + prepare_date_params params + + # -- Prepare form options + my_distributors = Enterprise.is_distributor.managed_by(spree_current_user) + my_suppliers = Enterprise.is_primary_producer.managed_by(spree_current_user) + + # My distributors and any distributors distributing products I supply + @distributors = my_distributors | Enterprise.with_distributed_products_outer.merge(Spree::Product.in_any_supplier(my_suppliers)) + # My suppliers and any suppliers supplying products I distribute + @suppliers = my_suppliers | my_distributors.map { |d| Spree::Product.in_distributor(d) }.flatten.map(&:supplier).uniq + @order_cycles = OrderCycle.active_or_complete.accessible_by(spree_current_user).order('orders_close_at DESC') + @report_types = REPORT_TYPES[:order_cycle_management] @report_type = params[:report_type] + + # -- Build Report with Order Grouper @report = OpenFoodNetwork::OrderCycleManagementReport.new spree_current_user, params + @table = @report.table_items + csv_file_name = "#{params[:report_type]}_#{timestamp}.csv" - @search = Spree::Order.complete.not_state(:canceled).managed_by(spree_current_user).search(params[:q]) - @orders = @search.result - - render_report(@report.header, @report.table, params[:csv], "order_cycle_management_#{timestamp}.csv") + render_report(@report.header, @table, params[:csv], "order_cycle_management_#{timestamp}.csv") end def packing @@ -294,9 +307,10 @@ Spree::Admin::ReportsController.class_eval do :sales_total => { :name => "Sales Total", :description => "Sales Total For All Orders" }, :users_and_enterprises => { :name => "Users & Enterprises", :description => "Enterprise Ownership & Status" }, :order_cycle_management => {:name => "Order Cycle Management", :description => ''}, - :packing => {:name => "Packing Reports", :description => ''}, :sales_tax => { :name => "Sales Tax", :description => "Sales Tax For Orders" }, - :xero_invoices => { :name => "Xero Invoices", :description => 'Invoices for import into Xero' } + :xero_invoices => { :name => "Xero Invoices", :description => 'Invoices for import into Xero' }, + :packing => { :name => "Packing Reports", :description => '' }, + :sales_tax => { :name => "Sales Tax", :description => "Sales Tax For Orders" } } # Return only reports the user is authorized to view. reports.select { |action| can? action, :report } diff --git a/app/views/spree/admin/reports/order_cycle_management.html.haml b/app/views/spree/admin/reports/order_cycle_management.html.haml index 418cba8b75..5ab379a669 100644 --- a/app/views/spree/admin/reports/order_cycle_management.html.haml +++ b/app/views/spree/admin/reports/order_cycle_management.html.haml @@ -1,47 +1,46 @@ -= form_tag spree.order_cycle_management_admin_reports_url do |f| - %br - = label_tag nil, "Order Cycle: " - = select_tag(:order_cycle_id, - options_for_select(report_order_cycle_options(@order_cycles), params[:order_cycle_id]), - include_blank: true) - %br - %br - = label_tag nil, "Payment Methods (hold Ctrl to select multiple payment methods)" - %br += form_for @report.search, :url => spree.order_cycle_management_admin_reports_path do |f| + = render 'date_range_form', f: f - = select_tag(:payment_method_name, - options_for_select(report_payment_method_options(@orders), params[:payment_method_name]), - multiple: true, include_blank: true, size: 10) - %br - %br - = label_tag nil, "Shipping Method: " - = select_tag(:shipping_method_name, - options_for_select(report_shipping_method_options(@orders), params[:shipping_method_name]), - include_blank: true) - %br - %br - = label_tag nil, "Report Type: " - = select_tag(:report_type, options_for_select(@report_types, @report_type)) - %br - %br - = check_box_tag :csv - = label_tag :csv, "Download as csv" - %br - %br + .row + .alpha.two.columns= label_tag nil, "Hubs: " + .omega.fourteen.columns= f.collection_select(:distributor_id_in, @distributors, :id, :name, {}, {class: "select2 fullwidth", multiple: true}) + + .row + .alpha.two.columns= label_tag nil, "Order Cycles: " + .omega.fourteen.columns + = f.select(:order_cycle_id_in, report_order_cycle_options(@order_cycles), {selected: params[:q][:order_cycle_id_in]}, {class: "select2 fullwidth", multiple: true}) + + .row + .alpha.two.columns= label_tag nil, "Payment Methods: " + .omega.fourteen.columns= select_tag(:payment_method_in, options_for_select(report_payment_method_options(@report.orders), params[:payment_method_in]), {class: "select2 fullwidth", multiple: true}) + + .row + .alpha.two.columns= label_tag nil, "Shipping Methods: " + .omega.fourteen.columns= select_tag(:shipping_method_in, options_for_select(report_shipping_method_options(@report.orders), params[:shipping_method_in]), {class: "select2 fullwidth", multiple: true}) + + .row + .alpha.two.columns= label_tag nil, "Report Type: " + .omega.fourteen.columns= select_tag(:report_type, options_for_select(@report_types, @report_type)) + + .row + = check_box_tag :csv + = label_tag :csv, "Download as csv" + + .row = button t(:search) %br %br -%table#listing_order_payment_methods.index +%table#listing_ocm_orders.index %thead %tr{'data-hook' => "orders_header"} - @report.header.each do |heading| %th=heading %tbody - - @report.table.each do |row| + - @table.each do |row| %tr - row.each do |column| %td= column - - if @report.table.empty? + - if @table.empty? %tr %td{:colspan => "2"}= t(:none) diff --git a/lib/open_food_network/order_cycle_management_report.rb b/lib/open_food_network/order_cycle_management_report.rb index b6e65f0a61..ea238f7792 100644 --- a/lib/open_food_network/order_cycle_management_report.rb +++ b/lib/open_food_network/order_cycle_management_report.rb @@ -16,24 +16,27 @@ module OpenFoodNetwork end end - def table + def search + Spree::Order.complete.where("spree_orders.state != ?", :canceled).distributed_by_user(@user).managed_by(@user).search(params[:q]) + end + + def orders + filter search.result + end + + def table_items if is_payment_methods? orders.map { |o| payment_method_row o } else orders.map { |o| delivery_row o } - end + end end - def orders - filter Spree::Order.managed_by(@user).distributed_by_user(@user).complete.where("spree_orders.state != ?", :canceled) + def filter(search_result) + filter_to_payment_method filter_to_shipping_method filter_to_order_cycle search_result end - def filter(orders) - filter_to_order_cycle filter_to_payment_method filter_to_shipping_method orders - end - - - private + private def payment_method_row(order) ba = order.billing_address diff --git a/lib/open_food_network/packing_report.rb b/lib/open_food_network/packing_report.rb index fcc5df5e88..38ef5735d9 100644 --- a/lib/open_food_network/packing_report.rb +++ b/lib/open_food_network/packing_report.rb @@ -71,7 +71,9 @@ module OpenFoodNetwork proc { |line_items| line_items.sum { |li| li.quantity } }, proc { |line_items| "" } ] }, { group_by: proc { |line_item| line_item.variant }, - sort_by: proc { |variant| variant.product.name } } ] + sort_by: proc { |variant| variant.product.name } }, + { group_by: proc { |line_item| line_item.order.bill_address.lastname }, + sort_by: proc { |lastname| lastname } } ] end end diff --git a/spec/features/admin/reports_spec.rb b/spec/features/admin/reports_spec.rb index 9cefc7ba26..ad0ad08df0 100644 --- a/spec/features/admin/reports_spec.rb +++ b/spec/features/admin/reports_spec.rb @@ -67,7 +67,7 @@ feature %q{ scenario "payment method report" do click_link "Payment Methods Report" - rows = find("table#listing_order_payment_methods").all("thead tr") + rows = find("table#listing_ocm_orders").all("thead tr") table = rows.map { |r| r.all("th").map { |c| c.text.strip } } table.sort.should == [ ["First Name", "Last Name", "Hub", "Hub Code", "Email", "Phone", "Shipping Method", "Payment Method", "Amount", "Balance"] @@ -76,7 +76,7 @@ feature %q{ scenario "delivery report" do click_link "Delivery Report" - rows = find("table#listing_order_payment_methods").all("thead tr") + rows = find("table#listing_ocm_orders").all("thead tr") table = rows.map { |r| r.all("th").map { |c| c.text.strip } } table.sort.should == [ ["First Name", "Last Name", "Hub", "Hub Code", "Delivery Address", "Delivery Postcode", "Phone", "Shipping Method", "Payment Method", "Amount", "Balance", "Temp Controlled Items?", "Special Instructions"] From 015f3eb9acdec59075591e42f3032c20c89a7fe1 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 30 Oct 2015 14:18:55 +1100 Subject: [PATCH 2/3] Display summary rows for admin adjustments --- app/models/spree/adjustment_decorator.rb | 10 ++++++---- lib/open_food_network/xero_invoices_report.rb | 14 +++++++++++++ spec/features/admin/reports_spec.rb | 20 +++++++++++++++---- .../xero_invoices_report_spec.rb | 10 ++++++++++ 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/app/models/spree/adjustment_decorator.rb b/app/models/spree/adjustment_decorator.rb index d27b5d760b..5f3847e218 100644 --- a/app/models/spree/adjustment_decorator.rb +++ b/app/models/spree/adjustment_decorator.rb @@ -5,11 +5,13 @@ module Spree # AdjustmentMetadata has no destroy logic itself. has_one :metadata, class_name: 'AdjustmentMetadata' - scope :enterprise_fee, where(originator_type: 'EnterpriseFee') + scope :enterprise_fee, where(originator_type: 'EnterpriseFee') scope :billable_period, where(source_type: 'BillablePeriod') - scope :included_tax, where(originator_type: 'Spree::TaxRate', adjustable_type: 'Spree::LineItem') - scope :with_tax, where('spree_adjustments.included_tax > 0') - scope :without_tax, where('spree_adjustments.included_tax = 0') + scope :admin, where(source_type: nil, originator_type: nil) + scope :included_tax, where(originator_type: 'Spree::TaxRate', adjustable_type: 'Spree::LineItem') + + scope :with_tax, where('spree_adjustments.included_tax > 0') + scope :without_tax, where('spree_adjustments.included_tax = 0') attr_accessible :included_tax diff --git a/lib/open_food_network/xero_invoices_report.rb b/lib/open_food_network/xero_invoices_report.rb index cf546efc90..92cfd0b205 100644 --- a/lib/open_food_network/xero_invoices_report.rb +++ b/lib/open_food_network/xero_invoices_report.rb @@ -82,6 +82,7 @@ module OpenFoodNetwork rows += produce_summary_rows(order, invoice_number, opts) unless detail? rows += fee_summary_rows(order, invoice_number, opts) unless detail? && order.account_invoice? rows += shipping_summary_rows(order, invoice_number, opts) + rows += admin_adjustment_summary_rows(order, invoice_number, opts) unless detail? rows end @@ -100,6 +101,11 @@ module OpenFoodNetwork [summary_row(order, 'Delivery Shipping Cost (tax inclusive)', total_shipping(order), invoice_number, tax_on_shipping_s(order), opts)] end + def admin_adjustment_summary_rows(order, invoice_number, opts) + [summary_row(order, 'Total untaxable admin adjustments (no tax)', total_untaxable_admin_adjustments(order), invoice_number, 'GST Free Income', opts), + summary_row(order, 'Total taxable admin adjustments (tax inclusive)', total_taxable_admin_adjustments(order), invoice_number, 'GST on Income', opts)] + end + def summary_row(order, description, amount, invoice_number, tax_type, opts={}) row order, '', description, '1', amount, invoice_number, tax_type, opts end @@ -173,6 +179,14 @@ module OpenFoodNetwork tax_on_shipping ? 'GST on Income' : 'GST Free Income' end + def total_untaxable_admin_adjustments(order) + order.adjustments.admin.without_tax.sum &:amount + end + + def total_taxable_admin_adjustments(order) + order.adjustments.admin.with_tax.sum &:amount + end + def detail? @opts[:report_type] == 'detailed' end diff --git a/spec/features/admin/reports_spec.rb b/spec/features/admin/reports_spec.rb index ad0ad08df0..acaf8bce84 100644 --- a/spec/features/admin/reports_spec.rb +++ b/spec/features/admin/reports_spec.rb @@ -397,6 +397,8 @@ feature %q{ let!(:adj_shipping) { create(:adjustment, adjustable: order1, label: "Shipping", originator: shipping_method, amount: 100.55, included_tax: 10.06) } let!(:adj_fee1) { create(:adjustment, adjustable: order1, originator: enterprise_fee1, label: "Enterprise fee untaxed", amount: 10, included_tax: 0) } let!(:adj_fee2) { create(:adjustment, adjustable: order1, originator: enterprise_fee2, label: "Enterprise fee taxed", amount: 20, included_tax: 2) } + let!(:adj_manual1) { create(:adjustment, adjustable: order1, originator: nil, source: nil, label: "Manual adjustment", amount: 30, included_tax: 0) } + let!(:adj_manual2) { create(:adjustment, adjustable: order1, originator: nil, source: nil, label: "Manual adjustment", amount: 40, included_tax: 3) } before do @@ -422,7 +424,9 @@ feature %q{ xero_invoice_summary_row('Total taxable produce (tax inclusive)', 1500.45, 'GST on Income'), xero_invoice_summary_row('Total untaxable fees (no tax)', 10.0, 'GST Free Income'), xero_invoice_summary_row('Total taxable fees (tax inclusive)', 20.0, 'GST on Income'), - xero_invoice_summary_row('Delivery Shipping Cost (tax inclusive)', 100.55, 'GST on Income') + xero_invoice_summary_row('Delivery Shipping Cost (tax inclusive)', 100.55, 'GST on Income'), + xero_invoice_summary_row('Total untaxable admin adjustments (no tax)', 30.0, 'GST Free Income'), + xero_invoice_summary_row('Total taxable admin adjustments (tax inclusive)', 40.0, 'GST on Income') ] end @@ -441,7 +445,9 @@ feature %q{ xero_invoice_summary_row('Total taxable produce (tax inclusive)', 1500.45, 'GST on Income', opts), xero_invoice_summary_row('Total untaxable fees (no tax)', 10.0, 'GST Free Income', opts), xero_invoice_summary_row('Total taxable fees (tax inclusive)', 20.0, 'GST on Income', opts), - xero_invoice_summary_row('Delivery Shipping Cost (tax inclusive)', 100.55, 'GST on Income', opts) + xero_invoice_summary_row('Delivery Shipping Cost (tax inclusive)', 100.55, 'GST on Income', opts), + xero_invoice_summary_row('Total untaxable admin adjustments (no tax)', 30.0, 'GST Free Income', opts), + xero_invoice_summary_row('Total taxable admin adjustments (tax inclusive)', 40.0, 'GST on Income', opts) ] end @@ -455,6 +461,8 @@ feature %q{ xero_invoice_header, xero_invoice_li_row(line_item1), xero_invoice_li_row(line_item2), + xero_invoice_adjustment_row(adj_manual1), + xero_invoice_adjustment_row(adj_manual2), xero_invoice_summary_row('Total untaxable fees (no tax)', 10.0, 'GST Free Income', opts), xero_invoice_summary_row('Total taxable fees (tax inclusive)', 20.0, 'GST on Income', opts), xero_invoice_summary_row('Delivery Shipping Cost (tax inclusive)', 100.55, 'GST on Income', opts) @@ -511,12 +519,16 @@ feature %q{ xero_invoice_row line_item.product.sku, line_item.variant.product_and_variant_name, line_item.price.to_s, line_item.quantity.to_s, tax_type, opts end - def xero_invoice_account_invoice_row(adjustment, opts={}) - opts.reverse_merge!({customer_name: '', address1: '', city: '', state: '', zipcode: '', country: '', invoice_number: account_invoice_order.number, order_number: account_invoice_order.number}) + def xero_invoice_adjustment_row(adjustment, opts={}) tax_type = adjustment.has_tax? ? 'GST on Income' : 'GST Free Income' xero_invoice_row('', adjustment.label, adjustment.amount, '1', tax_type, opts) end + def xero_invoice_account_invoice_row(adjustment, opts={}) + opts.reverse_merge!({customer_name: '', address1: '', city: '', state: '', zipcode: '', country: '', invoice_number: account_invoice_order.number, order_number: account_invoice_order.number}) + xero_invoice_adjustment_row(adjustment, opts) + end + def xero_invoice_row(sku, description, amount, quantity, tax_type, opts={}) opts.reverse_merge!({customer_name: 'Customer Name', address1: 'customer l1', city: 'customer city', state: 'Victoria', zipcode: '1234', country: country.name, invoice_number: order1.number, order_number: order1.number, invoice_date: '2015-04-26', due_date: '2015-05-10', account_code: 'food sales'}) diff --git a/spec/lib/open_food_network/xero_invoices_report_spec.rb b/spec/lib/open_food_network/xero_invoices_report_spec.rb index 5326d31149..ed981b706f 100644 --- a/spec/lib/open_food_network/xero_invoices_report_spec.rb +++ b/spec/lib/open_food_network/xero_invoices_report_spec.rb @@ -26,6 +26,7 @@ module OpenFoodNetwork report.stub(:produce_summary_rows) { ['produce'] } report.stub(:fee_summary_rows) { ['fee'] } report.stub(:shipping_summary_rows) { ['shipping'] } + report.stub(:admin_adjustment_summary_rows) { ['admin'] } order.stub(:account_invoice?) { false } end @@ -60,6 +61,15 @@ module OpenFoodNetwork it "always displays shipping summary rows" do summary_rows.should include 'shipping' end + + it "displays admin adjustment summary rows when summary report" do + summary_rows.should include 'admin' + end + + it "does not display admin adjustment summary rows when detail report" do + report.stub(:detail?) { true } + summary_rows.should_not include 'admin' + end end describe "finding account invoice adjustments" do From 9227df54b6b5c7eb03961a7e9c8a1a922ed37925 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 30 Oct 2015 14:47:01 +1100 Subject: [PATCH 3/3] Display detail rows for admin adjustments --- lib/open_food_network/xero_invoices_report.rb | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/open_food_network/xero_invoices_report.rb b/lib/open_food_network/xero_invoices_report.rb index 92cfd0b205..a05b1cc616 100644 --- a/lib/open_food_network/xero_invoices_report.rb +++ b/lib/open_food_network/xero_invoices_report.rb @@ -34,10 +34,7 @@ module OpenFoodNetwork rows = [] rows += line_item_detail_rows(order, invoice_number, opts) - - if order.account_invoice? - rows += adjustment_detail_rows(order, invoice_number, opts) - end + rows += adjustment_detail_rows(order, invoice_number, opts) rows end @@ -60,13 +57,13 @@ module OpenFoodNetwork end def adjustment_detail_rows(order, invoice_number, opts) - account_invoice_adjustments(order).map do |adjustment| + adjustments(order).map do |adjustment| adjustment_detail_row(adjustment, invoice_number, opts) end end def adjustment_detail_row(adjustment, invoice_number, opts) - row(adjustment.source.andand.account_invoice.andand.order, + row(adjustment_order(adjustment), '', adjustment.label, 1, @@ -144,12 +141,21 @@ module OpenFoodNetwork ] end + def adjustments(order) + account_invoice_adjustments(order) + order.adjustments.admin + end + def account_invoice_adjustments(order) order.adjustments. billable_period. select { |a| a.source.present? } end + def adjustment_order(adjustment) + adjustment.source.andand.account_invoice.andand.order || + (adjustment.adjustable.is_a?(Spree::Order) ? adjustment.adjustable : nil) + end + def invoice_number_for(order, i) @opts[:initial_invoice_number] ? @opts[:initial_invoice_number].to_i+i : order.number end