1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-30 19:52:20 +01:00

(quality) moved accounting services to a decicated module

This commit is contained in:
Sylvain 2022-11-15 13:52:39 +01:00
parent 37f30e26a9
commit 462a764276
6 changed files with 37 additions and 28 deletions

View File

@ -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

View File

@ -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'

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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