From c5b618b1f4589f8243da2455e509fc012c2ebc09 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 14 May 2015 12:25:48 +1000 Subject: [PATCH] Admin can customise some fields on Xero invoices report Add require for xero invoices report spec --- .../admin/reports_controller_decorator.rb | 6 ++- .../admin/reports/xero_invoices.html.haml | 33 +++++++++++++++ lib/open_food_network/xero_invoices_report.rb | 32 +++++++++------ spec/features/admin/reports_spec.rb | 40 +++++++++++++++---- .../xero_invoices_report_spec.rb | 25 ++++++++++++ 5 files changed, 115 insertions(+), 21 deletions(-) create mode 100644 app/views/spree/admin/reports/xero_invoices.html.haml create mode 100644 spec/lib/open_food_network/xero_invoices_report_spec.rb diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index 3ef32aa074..15fab60d89 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -683,7 +683,7 @@ Spree::Admin::ReportsController.class_eval do def xero_invoices @search = Spree::Order.complete.managed_by(spree_current_user).search(params[:q]) orders = @search.result - @report = OpenFoodNetwork::XeroInvoicesReport.new orders + @report = OpenFoodNetwork::XeroInvoicesReport.new orders, params render_report(@report.header, @report.table, params[:csv], "xero_invoices_#{timestamp}.csv") end @@ -725,7 +725,9 @@ 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 => ''}, - :sales_tax => { :name => "Sales Tax", :description => "Sales Tax For Orders" } + :sales_tax => { :name => "Sales Tax", :description => "Sales Tax For Orders" }, + :xero_invoices => { :name => "Xero Invoices", :description => 'Invoices for import into Xero' } + } # Return only reports the user is authorized to view. reports.select { |action| can? action, :report } diff --git a/app/views/spree/admin/reports/xero_invoices.html.haml b/app/views/spree/admin/reports/xero_invoices.html.haml new file mode 100644 index 0000000000..8c11e5f543 --- /dev/null +++ b/app/views/spree/admin/reports/xero_invoices.html.haml @@ -0,0 +1,33 @@ += form_tag spree.xero_invoices_admin_reports_path do + .row + .four.columns.alpha= label_tag :initial_invoice_number, "Initial invoice number:" + .twelve.columns.omega= text_field_tag :initial_invoice_number + .row + .four.columns.alpha= label_tag :invoice_date, "Invoice date:" + .twelve.columns.omega= text_field_tag :invoice_date, '', class: 'datetimepicker' + .row + .four.columns.alpha= label_tag :due_date, "Due date:" + .twelve.columns.omega= text_field_tag :due_date, '', class: 'datetimepicker' + .row + .four.columns.alpha= label_tag :account_code, "Account code:" + .twelve.columns.omega= text_field_tag :account_code + .row + .four.columns.alpha= label_tag :csv, "Download as CSV:" + .twelve.columns.omega= check_box_tag :csv + .row + .four.columns.alpha= button t(:search) + + +%table#listing_invoices.index + %thead + %tr + - @report.header.each do |header| + %th= header + %tbody + - @report.table.each do |row| + %tr + - row.each do |column| + %td= column + - if @report.table.empty? + %tr + %td{:colspan => "2"}= t(:none) diff --git a/lib/open_food_network/xero_invoices_report.rb b/lib/open_food_network/xero_invoices_report.rb index 18c95073d3..784550a4b4 100644 --- a/lib/open_food_network/xero_invoices_report.rb +++ b/lib/open_food_network/xero_invoices_report.rb @@ -1,7 +1,10 @@ module OpenFoodNetwork class XeroInvoicesReport - def initialize(orders) + def initialize(orders, opts={}) @orders = orders + @opts = opts.reverse_merge({invoice_date: Date.today, + due_date: 2.weeks.from_now.to_date, + account_code: 'food sales'}) end def header @@ -11,12 +14,13 @@ module OpenFoodNetwork def table rows = [] - @orders.each do |order| - rows << summary_row(order, 'Total untaxable produce (no tax)', 0, 'GST Free Income') - rows << summary_row(order, 'Total taxable produce (tax inclusive)', 0, 'GST on Income') - rows << summary_row(order, 'Total untaxable fees (no tax)', 0, 'GST Free Income') - rows << summary_row(order, 'Total taxable fees (tax inclusive)', 0, 'GST on Income') - rows << summary_row(order, 'Delivery Shipping Cost (tax inclusive)', 0, 'Tax or No Tax - depending on enterprise setting') + @orders.each_with_index do |order, i| + invoice_number = invoice_number_for(order, i) + rows << summary_row(order, 'Total untaxable produce (no tax)', 0, invoice_number, 'GST Free Income', @opts) + rows << summary_row(order, 'Total taxable produce (tax inclusive)', 0, invoice_number, 'GST on Income', @opts) + rows << summary_row(order, 'Total untaxable fees (no tax)', 0, invoice_number, 'GST Free Income', @opts) + rows << summary_row(order, 'Total taxable fees (tax inclusive)', 0, invoice_number, 'GST on Income', @opts) + rows << summary_row(order, 'Delivery Shipping Cost (tax inclusive)', 0, invoice_number, 'Tax or No Tax - depending on enterprise setting', @opts) end rows @@ -25,7 +29,11 @@ module OpenFoodNetwork private - def summary_row(order, description, amount, tax_type) + def invoice_number_for(order, i) + @opts[:initial_invoice_number] ? @opts[:initial_invoice_number].to_i+i : order.number + end + + def summary_row(order, description, amount, invoice_number, tax_type, opts={}) [order.bill_address.full_name, order.email, order.bill_address.address1, @@ -36,16 +44,16 @@ module OpenFoodNetwork order.bill_address.state, order.bill_address.zipcode, order.bill_address.country.andand.name, - order.number, # To customise + invoice_number, order.number, - Date.today, # To customise - 2.weeks.from_now.to_date, # To customise + opts[:invoice_date], + opts[:due_date], '', description, '1', amount, '', - 'food sales', # To customise + opts[:account_code], tax_type, '', '', diff --git a/spec/features/admin/reports_spec.rb b/spec/features/admin/reports_spec.rb index 70e7711759..49a0979406 100644 --- a/spec/features/admin/reports_spec.rb +++ b/spec/features/admin/reports_spec.rb @@ -327,7 +327,7 @@ feature %q{ login_to_admin_section click_link 'Reports' - click_link 'Xero invoices' + click_link 'Xero Invoices' end around do |example| @@ -337,10 +337,7 @@ feature %q{ end it "shows Xero invoices report" do - rows = find("table#listing_invoices").all("tr") - table = rows.map { |r| r.all("th,td").map { |c| c.text.strip } } - - table.should == [ + xero_invoice_table.should match_table [ %w(*ContactName EmailAddress POAddressLine1 POAddressLine2 POAddressLine3 POAddressLine4 POCity PORegion POPostalCode POCountry *InvoiceNumber Reference *InvoiceDate *DueDate InventoryItemCode *Description *Quantity *UnitAmount Discount *AccountCode *TaxType TrackingName1 TrackingOption1 TrackingName2 TrackingOption2 Currency BrandingTheme), xero_invoice_row('Total untaxable produce (no tax)', 0, 'GST Free Income'), xero_invoice_row('Total taxable produce (tax inclusive)', 0, 'GST on Income'), @@ -350,11 +347,40 @@ feature %q{ ] end + it "can customise a number of fields" do + fill_in 'initial_invoice_number', with: '5' + fill_in 'invoice_date', with: '2015-02-12' + fill_in 'due_date', with: '2015-03-12' + fill_in 'account_code', with: 'abc123' + click_button 'Search' + + opts = {invoice_number: '5', invoice_date: '2015-02-12', due_date: '2015-03-12', account_code: 'abc123'} + + xero_invoice_table.should match_table [ + %w(*ContactName EmailAddress POAddressLine1 POAddressLine2 POAddressLine3 POAddressLine4 POCity PORegion POPostalCode POCountry *InvoiceNumber Reference *InvoiceDate *DueDate InventoryItemCode *Description *Quantity *UnitAmount Discount *AccountCode *TaxType TrackingName1 TrackingOption1 TrackingName2 TrackingOption2 Currency BrandingTheme), + xero_invoice_row('Total untaxable produce (no tax)', 0, 'GST Free Income', opts), + xero_invoice_row('Total taxable produce (tax inclusive)', 0, 'GST on Income', opts), + xero_invoice_row('Total untaxable fees (no tax)', 0, 'GST Free Income', opts), + xero_invoice_row('Total taxable fees (tax inclusive)', 0, 'GST on Income', opts), + xero_invoice_row('Delivery Shipping Cost (tax inclusive)', 0, 'Tax or No Tax - depending on enterprise setting', opts) + ] + + # TODO: + # - Amounts + # - Tax specification for shipping + end + private - def xero_invoice_row(description, amount, tax_type) - ['Customer Name', 'customer@email.com', 'customer l1', '', '', '', 'customer city', 'Victoria', '1234', country.name, order1.number, order1.number, '2015-04-26', '2015-05-10', '', description, '1', amount.to_s, '', 'food sales', tax_type, '', '', '', '', Spree::Config.currency, ''] + def xero_invoice_table + find("table#listing_invoices") + end + + def xero_invoice_row(description, amount, tax_type, opts={}) + opts.reverse_merge!({invoice_number: order1.number, invoice_date: '2015-04-26', due_date: '2015-05-10', account_code: 'food sales'}) + + ['Customer Name', 'customer@email.com', 'customer l1', '', '', '', 'customer city', 'Victoria', '1234', country.name, opts[:invoice_number], order1.number, opts[:invoice_date], opts[:due_date], '', description, '1', amount.to_s, '', opts[:account_code], tax_type, '', '', '', '', Spree::Config.currency, ''] end end diff --git a/spec/lib/open_food_network/xero_invoices_report_spec.rb b/spec/lib/open_food_network/xero_invoices_report_spec.rb new file mode 100644 index 0000000000..9756629f0e --- /dev/null +++ b/spec/lib/open_food_network/xero_invoices_report_spec.rb @@ -0,0 +1,25 @@ +require 'open_food_network/xero_invoices_report' + +module OpenFoodNetwork + describe XeroInvoicesReport do + subject { XeroInvoicesReport.new [] } + + describe "generating invoice numbers" do + let(:order) { double(:order, number: 'R731032860') } + + describe "when no initial invoice number is given" do + it "returns the order number" do + subject.send(:invoice_number_for, order, 123).should == 'R731032860' + end + end + + describe "when an initial invoice number is given" do + subject { XeroInvoicesReport.new [], {initial_invoice_number: '123'} } + + it "increments the number by the index" do + subject.send(:invoice_number_for, order, 456).should == 579 + end + end + end + end +end