From 462a76427611e13993972dff51af25541a55d159 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 15 Nov 2022 13:52:39 +0100 Subject: [PATCH] (quality) moved accounting services to a decicated module --- .../api/accounting_periods_controller.rb | 7 +++--- .../accounting_export_service.rb | 7 ++++-- .../accounting/accounting_period_service.rb | 22 +++++++++++++++++++ .../{ => accounting}/vat_export_service.rb | 9 +++++--- app/services/accounting_period_service.rb | 18 --------------- app/workers/accounting_export_worker.rb | 2 +- 6 files changed, 37 insertions(+), 28 deletions(-) rename app/services/{ => accounting}/accounting_export_service.rb (98%) create mode 100644 app/services/accounting/accounting_period_service.rb rename app/services/{ => accounting}/vat_export_service.rb (94%) delete mode 100644 app/services/accounting_period_service.rb diff --git a/app/controllers/api/accounting_periods_controller.rb b/app/controllers/api/accounting_periods_controller.rb index e154ef76e..d52cacb26 100644 --- a/app/controllers/api/accounting_periods_controller.rb +++ b/app/controllers/api/accounting_periods_controller.rb @@ -2,12 +2,11 @@ # API Controller for resources of AccountingPeriod class API::AccountingPeriodsController < API::ApiController - before_action :authenticate_user! before_action :set_period, only: %i[show download_archive] def index - @accounting_periods = AccountingPeriodService.all_periods_with_users + @accounting_periods = Accounting::AccountingPeriodService.all_periods_with_users end def show; end @@ -24,7 +23,7 @@ class API::AccountingPeriodsController < API::ApiController def last_closing_end authorize AccountingPeriod - last_period = AccountingPeriodService.find_last_period + last_period = Accounting::AccountingPeriodService.find_last_period if last_period.nil? invoice = Invoice.order(:created_at).first @last_end = invoice.created_at if invoice @@ -35,7 +34,7 @@ class API::AccountingPeriodsController < API::ApiController def download_archive authorize AccountingPeriod - send_file File.join(Rails.root, @accounting_period.archive_file), type: 'application/json', disposition: 'attachment' + send_file Rails.root.join(@accounting_period.archive_file), type: 'application/json', disposition: 'attachment' end private diff --git a/app/services/accounting_export_service.rb b/app/services/accounting/accounting_export_service.rb similarity index 98% rename from app/services/accounting_export_service.rb rename to app/services/accounting/accounting_export_service.rb index bdc1033f8..f77ae13c9 100644 --- a/app/services/accounting_export_service.rb +++ b/app/services/accounting/accounting_export_service.rb @@ -1,7 +1,10 @@ # frozen_string_literal: false +# module definition +module Accounting; end + # Provides the routine to export the accounting data to an external accounting software -class AccountingExportService +class Accounting::AccountingExportService include ActionView::Helpers::NumberHelper attr_reader :encoding, :format, :separator, :journal_code, :date_format, :columns, :decimal_separator, :label_max_length, @@ -119,7 +122,7 @@ class AccountingExportService columns.each do |column| case column when 'journal_code' - row << journal_code + row << journal_code.to_s when 'date' row << invoice.created_at&.strftime(date_format) when 'account_code' diff --git a/app/services/accounting/accounting_period_service.rb b/app/services/accounting/accounting_period_service.rb new file mode 100644 index 000000000..511ccbd16 --- /dev/null +++ b/app/services/accounting/accounting_period_service.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +# module definition +module Accounting; end + +# Provides methods for accessing AccountingPeriods properties +class Accounting::AccountingPeriodService + class << self + def find_last_period + AccountingPeriod.where(end_at: AccountingPeriod.select('max(end_at)')).first + end + + def all_periods_with_users + AccountingPeriod.joins("INNER JOIN #{User.arel_table.name} ON users.id = accounting_periods.closed_by + INNER JOIN #{Profile.arel_table.name} ON profiles.user_id = users.id") + .select("#{AccountingPeriod.arel_table.name}.*, + #{Profile.arel_table.name}.first_name, + #{Profile.arel_table.name}.last_name") + .order('start_at DESC') + end + end +end diff --git a/app/services/vat_export_service.rb b/app/services/accounting/vat_export_service.rb similarity index 94% rename from app/services/vat_export_service.rb rename to app/services/accounting/vat_export_service.rb index 1da48f745..c95aa2fe9 100644 --- a/app/services/vat_export_service.rb +++ b/app/services/accounting/vat_export_service.rb @@ -1,7 +1,10 @@ # frozen_string_literal: false +# module definition +module Accounting; end + # Provides the routine to export the collected VAT data to a CSV file. -class VatExportService +class Accounting::VatExportService include ActionView::Helpers::NumberHelper attr_reader :encoding, :format, :separator, :date_format, :columns, :decimal_separator @@ -15,7 +18,7 @@ class VatExportService @columns = columns end - def set_options(decimal_separator: ',', date_format: '%d/%m/%Y', label_max_length: nil, export_zeros: nil) + def set_options(decimal_separator: ',', date_format: '%d/%m/%Y') @decimal_separator = decimal_separator @date_format = date_format end @@ -66,7 +69,7 @@ class VatExportService vat_total.push service.invoice_vat(i) end - vat_total.map(&:values).flatten.group_by { |tot| tot[:vat_rate] }.map { |k, v| [k, v.map { |t| t[:total_vat] }.reduce(:+)] }.to_h + vat_total.map(&:values).flatten.group_by { |tot| tot[:vat_rate] }.transform_values { |v| v.pluck(:total_vat).reduce(:+) } end # Generate a row of the export, filling the configured columns with the provided values diff --git a/app/services/accounting_period_service.rb b/app/services/accounting_period_service.rb deleted file mode 100644 index 4f37945ab..000000000 --- a/app/services/accounting_period_service.rb +++ /dev/null @@ -1,18 +0,0 @@ -# frozen_string_literal: true - -# Provides methods for accessing AccountingPeriods properties -class AccountingPeriodService - - def self.find_last_period - AccountingPeriod.where(end_at: AccountingPeriod.select('max(end_at)')).first - end - - def self.all_periods_with_users - AccountingPeriod.joins("INNER JOIN #{User.arel_table.name} ON users.id = accounting_periods.closed_by - INNER JOIN #{Profile.arel_table.name} ON profiles.user_id = users.id") - .select("#{AccountingPeriod.arel_table.name}.*, - #{Profile.arel_table.name}.first_name, - #{Profile.arel_table.name}.last_name") - .order('start_at DESC') - end -end diff --git a/app/workers/accounting_export_worker.rb b/app/workers/accounting_export_worker.rb index 2fe5b217a..a9e433b9c 100644 --- a/app/workers/accounting_export_worker.rb +++ b/app/workers/accounting_export_worker.rb @@ -10,7 +10,7 @@ class AccountingExportWorker raise SecurityError, 'Not allowed to export' unless export.user.admin? data = JSON.parse(export.query) - service = export.export_type == 'vat' ? VatExportService : AccountingExportService + service = export.export_type == 'vat' ? Accounting::VatExportService : Accounting::AccountingExportService service = service.new( data['columns'], encoding: data['encoding'], format: export.extension, separator: export.key