mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2024-12-01 12:24:28 +01:00
af3def0e2e
Previously, the accounting data were built on demand, extracting the data from the invoices on-the-fly. This is intended to be used only once in a while, so there was no performance issue with that. Now, we want those data to be accessed from the OpenAPI, so building them on-the-fly would be very much intensive and resouces heavy. So we build them each nights using a scheduled worker and save them in the database
28 lines
1.1 KiB
Ruby
28 lines
1.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
# Asynchronously export the accounting data (AccountingLines) to an external accounting software
|
|
class AccountingExportWorker
|
|
include Sidekiq::Worker
|
|
|
|
def perform(export_id)
|
|
export = Export.find(export_id)
|
|
|
|
raise SecurityError, 'Not allowed to export' unless export.user.admin?
|
|
|
|
data = JSON.parse(export.query)
|
|
service = export.export_type == 'vat' ? Accounting::VatExportService : Accounting::AccountingExportService
|
|
service = service.new(
|
|
data['columns'],
|
|
encoding: data['encoding'], format: export.extension, separator: export.key
|
|
)
|
|
service.set_options(date_format: data['date_format'], decimal_separator: data['decimal_separator'],
|
|
label_max_length: data['label_max_length'], export_zeros: data['export_invoices_at_zero'])
|
|
|
|
service.export(data['start_date'], data['end_date'], export.file)
|
|
|
|
NotificationCenter.call type: :notify_admin_export_complete,
|
|
receiver: export.user,
|
|
attached_object: export
|
|
end
|
|
end
|