Report Refactor 3: Xero Invoices

This one as actually not really been refactored, too much work to rewrite it so I just encapsulated the result within the new methods definitions
This commit is contained in:
Sebastian Castro
2022-04-06 20:29:02 +02:00
committed by Jean-Baptiste Bellet
parent 42da443901
commit cd30012334
8 changed files with 97 additions and 57 deletions

View File

@@ -128,7 +128,7 @@ Layout/LineLength:
- 'lib/reporting/reports/payments/payments_report.rb'
- 'lib/reporting/reports/products_and_inventory/lettuce_share_report.rb'
- 'lib/reporting/reports/sales_tax/sales_tax_report.rb'
- 'lib/reporting/reports/xero_invoices/xero_invoices_report.rb'
- 'lib/reporting/reports/xero_invoices/base.rb'
- 'lib/spree/localized_number.rb'
- 'lib/tasks/data.rake'
- 'lib/tasks/enterprises.rake'
@@ -536,7 +536,7 @@ Metrics/ClassLength:
- 'lib/reporting/reports/order_cycle_management/order_cycle_management_report.rb'
- 'lib/open_food_network/order_cycle_permissions.rb'
- 'lib/reporting/reports/payments/payments_report.rb'
- 'lib/reporting/reports/xero_invoices/xero_invoices_report.rb'
- 'lib/reporting/reports/xero_invoices/base.rb'
- 'lib/reporting/report_grouper.rb'
# Offense count: 39
@@ -571,7 +571,7 @@ Metrics/CyclomaticComplexity:
- 'lib/reporting/reports/customers/customers_report.rb'
- 'lib/reporting/reports/orders_and_fulfillment/customer_totals_report.rb'
- 'lib/reporting/reports/payments/payments_report.rb'
- 'lib/reporting/reports/xero_invoices/xero_invoices_report.rb'
- 'lib/reporting/reports/xero_invoices/base.rb'
- 'lib/spree/core/controller_helpers/order.rb'
- 'lib/spree/core/controller_helpers/respond_with.rb'
- 'lib/spree/localized_number.rb'
@@ -600,7 +600,7 @@ Metrics/MethodLength:
- 'lib/reporting/reports/order_cycle_management/order_cycle_management_report.rb'
- 'lib/open_food_network/order_cycle_permissions.rb'
- 'lib/reporting/reports/payments/payments_report.rb'
- 'lib/reporting/reports/xero_invoices/xero_invoices_report.rb'
- 'lib/reporting/reports/xero_invoices/base.rb'
- 'lib/tasks/sample_data/product_factory.rb'
# Offense count: 54
@@ -668,7 +668,7 @@ Metrics/ParameterLists:
Exclude:
- 'app/helpers/angular_form_builder.rb'
- 'app/models/product_import/entry_processor.rb'
- 'lib/reporting/reports/xero_invoices/xero_invoices_report.rb'
- 'lib/reporting/reports/xero_invoices/base.rb'
- 'spec/support/controller_requests_helper.rb'
- 'spec/system/admin/reports_spec.rb'

View File

@@ -9,15 +9,17 @@
options_for_select(report_order_cycle_options(@data.order_cycles), params.dig(:q, :order_cycle_id_eq)),
{:include_blank => true}, {:class => "select2 fullwidth light"})
.row
.two.columns.alpha= label_tag :initial_invoice_number, t(:initial_invoice_number)
.fourteen.columns.omega= text_field_tag :initial_invoice_number, params[:initial_invoice_number]
.row
.two.columns.alpha= label_tag :invoice_date, t(:invoice_date)
.fourteen.columns.omega= text_field_tag :invoice_date, params[:invoice_date], class: 'datetimepicker'
.row
.two.columns.alpha= label_tag :due_date, t(:due_date)
.fourteen.columns.omega= text_field_tag :due_date, params[:due_date], class: 'datetimepicker'
.row
.two.columns.alpha= label_tag :account_code, t(:account_code)
.fourteen.columns.omega= text_field_tag :account_code, params[:account_code]
%fieldset.no-border-bottom.print-hidden{ style: "padding-bottom: 0" }
%legend{ align: 'center'}= t(:report_xero_configuration)
.row
.two.columns.alpha= label_tag :initial_invoice_number, t(:initial_invoice_number)
.fourteen.columns.omega= number_field_tag :initial_invoice_number, params[:initial_invoice_number]
.row
.two.columns.alpha= label_tag :invoice_date, t(:invoice_date)
.fourteen.columns.omega= text_field_tag :invoice_date, params[:invoice_date], class: 'datetimepicker'
.row
.two.columns.alpha= label_tag :due_date, t(:due_date)
.fourteen.columns.omega= text_field_tag :due_date, params[:due_date], class: 'datetimepicker'
.row
.two.columns.alpha= label_tag :account_code, t(:account_code)
.fourteen.columns.omega= text_field_tag :account_code, params[:account_code]

View File

@@ -2767,10 +2767,11 @@ See the %{link} to find out more about %{sitename}'s features and to start using
report_header_transaction_fee: Transaction Fee (no tax)
report_header_total_untaxable_admin: Total untaxable admin adjustments (no tax)
report_header_total_taxable_admin: Total taxable admin adjustments (tax inclusive)
initial_invoice_number: "Initial invoice number:"
invoice_date: "Invoice date:"
due_date: "Due date:"
account_code: "Account code:"
report_xero_configuration: Xero Configuration
initial_invoice_number: "Initial invoice number"
invoice_date: "Invoice date"
due_date: "Due date"
account_code: "Account code"
equals: "Equals"
contains: "contains"
discount: "Discount"

View File

@@ -3,23 +3,36 @@
module Reporting
module Reports
module XeroInvoices
class XeroInvoicesReport < ReportObjectTemplate
class Base < ReportObjectTemplate
def initialize(user, params = {})
params.reverse_merge!(report_subtype: 'summary',
invoice_date: Time.zone.today,
due_date: Time.zone.today + 1.month,
account_code: 'food sales')
super(user, params)
@params = @params.
symbolize_keys.
reject { |_k, v| v.blank? }.
reverse_merge( report_subtype: 'summary',
invoice_date: Time.zone.today,
due_date: Time.zone.today + 1.month,
account_code: 'food sales' )
end
def table_headers
# NOTE: These are NOT to be translated, they need to be in this exact format to work with Xero
def xero_columns
# These are NOT to be translated, they need to be in this exact format to work with Xero
%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 Paid?)
POCity PORegion POPostalCode POCountry *InvoiceNumber Reference *InvoiceDate *DueDate
InventoryItemCode *Description *Quantity *UnitAmount Discount *AccountCode *TaxType
TrackingName1 TrackingOption1 TrackingName2 TrackingOption2 Currency
BrandingTheme Paid?)
end
def custom_headers
xero_columns.index_by(&:to_sym)
end
# This report calculate data in a specific way, so instead of refactoring it
# we just encapsulate the result in the columns method
def columns
result = {}
xero_columns.each_with_index do |header, id|
result[header.to_sym] = proc { |row| row[id] }
end
result
end
def search
@@ -27,14 +40,15 @@ module Reporting
permissions.editable_orders.complete.not_state(:canceled).ransack(params[:q])
end
def orders
search.result.reorder('id DESC')
end
# In the new way of managing reports, query_result should be an ActiveRecordRelation
# Here we directly transform the ActiveRecordRelation into table_rows without using the
# new ReportGrouper, so we can keep the old report without refactoring it
def query_result
search_result = search.result.reorder('id DESC')
def table_rows
rows = []
orders.each_with_index do |order, i|
search_result.each_with_index do |order, i|
invoice_number = invoice_number_for(order, i)
rows += detail_rows_for_order(order, invoice_number, params) if detail?
rows += summary_rows_for_order(order, invoice_number, params)
@@ -137,6 +151,7 @@ module Reporting
row order, '', description, '1', amount, invoice_number, tax_type, opts
end
# rubocop:disable Metrics/AbcSize
def row(order, sku, description, quantity, amount, invoice_number, tax_type, opts = {})
return nil if amount == 0
@@ -147,13 +162,13 @@ module Reporting
'',
'',
order.bill_address&.city,
order.bill_address&.state,
order.bill_address&.state.to_s,
order.bill_address&.zipcode,
order.bill_address&.country&.name,
invoice_number,
order.number,
opts[:invoice_date],
opts[:due_date],
opts[:invoice_date].to_date.to_s,
opts[:due_date].to_date.to_s,
sku,
description,
quantity,
@@ -169,6 +184,7 @@ module Reporting
'',
order.paid? ? I18n.t(:y) : I18n.t(:n)]
end
# rubocop:enable Metrics/AbcSize
def admin_adjustments(order)
order.adjustments.admin
@@ -179,7 +195,11 @@ module Reporting
end
def invoice_number_for(order, idx)
params[:initial_invoice_number] ? params[:initial_invoice_number].to_i + idx : order.number
if params[:initial_invoice_number].present?
params[:initial_invoice_number].to_i + idx
else
order.number
end
end
def total_untaxable_products(order)

View File

@@ -0,0 +1,10 @@
# frozen_string_literal: true
module Reporting
module Reports
module XeroInvoices
class Detailed < Base
end
end
end
end

View File

@@ -0,0 +1,10 @@
# frozen_string_literal: true
module Reporting
module Reports
module XeroInvoices
class Summary < Base
end
end
end
end

View File

@@ -5,31 +5,28 @@ require 'spec_helper'
module Reporting
module Reports
module XeroInvoices
describe XeroInvoicesReport do
subject { XeroInvoicesReport.new user, {} }
describe Base do
subject { Base.new user, {} }
let(:user) { create(:user) }
describe "option defaults" do
let(:report) {
XeroInvoicesReport.new user, initial_invoice_number: '', invoice_date: '', due_date: '',
account_code: ''
}
let(:report) { Base.new user }
around { |example| Timecop.travel(Time.zone.local(2015, 5, 5, 14, 0, 0)) { example.run } }
it "uses defaults when blank params are passed" do
expect(report.instance_variable_get(:@params)).to eq(invoice_date: Date.civil(2015, 5, 5),
due_date: Date.civil(2015, 6, 5),
account_code: 'food sales',
report_subtype: 'summary' )
expect(report.params).to eq(invoice_date: Date.civil(2015, 5, 5),
due_date: Date.civil(2015, 6, 5),
account_code: 'food sales',
report_subtype: 'summary' )
end
end
describe "summary rows" do
let(:report) {
XeroInvoicesReport.new user, initial_invoice_number: '', invoice_date: '', due_date: '',
account_code: ''
Base.new user, initial_invoice_number: '', invoice_date: '', due_date: '',
account_code: ''
}
let(:order) { double(:order) }
let(:summary_rows) { report.__send__(:summary_rows_for_order, order, 1, {}) }
@@ -86,7 +83,7 @@ module Reporting
end
describe "when an initial invoice number is given" do
subject { XeroInvoicesReport.new user, initial_invoice_number: '123' }
subject { Base.new user, initial_invoice_number: '123' }
it "increments the number by the index" do
expect(subject.send(:invoice_number_for, order, 456)).to eq(579)

View File

@@ -582,8 +582,8 @@ describe '
fill_in 'account_code', with: 'abc123'
click_button 'Go'
opts = { invoice_number: '5', invoice_date: '2021-02-12 00:00',
due_date: '2021-03-12 00:00', account_code: 'abc123' }
opts = { invoice_number: '5', invoice_date: '2021-02-12',
due_date: '2021-03-12', account_code: 'abc123' }
expect(xero_invoice_table).to match_table [
xero_invoice_header,