2019-04-03 17:57:21 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-04-21 17:38:06 +02:00
|
|
|
require 'integrity/checksum'
|
|
|
|
|
2019-04-03 17:57:21 +02:00
|
|
|
# Will generate a ZIP archive file containing all invoicing data for the given period.
|
|
|
|
# This file will be asynchronously generated by sidekiq and a notification will be sent to the requesting user when it's done.
|
|
|
|
class ArchiveWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
|
|
|
|
def perform(accounting_period_id)
|
|
|
|
period = AccountingPeriod.find(accounting_period_id)
|
2021-06-10 11:36:32 +02:00
|
|
|
invoices = period.invoices.includes(:invoice_items).order(created_at: :asc)
|
|
|
|
schedules = period.payment_schedules.includes(:payment_schedule_items, :payment_schedule_objects).order(created_at: :asc)
|
2019-04-03 17:57:21 +02:00
|
|
|
previous_file = period.previous_period&.archive_file
|
2021-04-21 17:38:06 +02:00
|
|
|
last_archive_checksum = previous_file ? Integrity::Checksum.file(previous_file) : nil
|
2021-06-10 11:36:32 +02:00
|
|
|
json_data = to_json_archive(period, invoices, schedules, previous_file, last_archive_checksum)
|
2021-04-21 17:38:06 +02:00
|
|
|
current_archive_checksum = Integrity::Checksum.text(json_data)
|
2023-02-14 13:10:58 +01:00
|
|
|
date = Time.current.iso8601
|
2021-04-21 17:38:06 +02:00
|
|
|
chained = Integrity::Checksum.text("#{current_archive_checksum}#{last_archive_checksum}#{date}")
|
2019-04-03 17:57:21 +02:00
|
|
|
|
|
|
|
Zip::OutputStream.open(period.archive_file) do |io|
|
|
|
|
io.put_next_entry(period.archive_json_file)
|
|
|
|
io.write(json_data)
|
|
|
|
io.put_next_entry('checksum.sha256')
|
|
|
|
io.write("#{current_archive_checksum}\t#{period.archive_json_file}")
|
|
|
|
io.put_next_entry('chained.sha256')
|
|
|
|
io.write("#{chained}\t#{date}")
|
|
|
|
end
|
|
|
|
|
|
|
|
NotificationCenter.call type: :notify_admin_archive_complete,
|
2023-01-09 10:20:19 +01:00
|
|
|
receiver: User.where(id: period.closed_by)&.first,
|
2019-04-03 17:57:21 +02:00
|
|
|
attached_object: period
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-06-10 11:36:32 +02:00
|
|
|
def to_json_archive(period, invoices, schedules, previous_file, last_checksum)
|
2021-04-21 17:38:06 +02:00
|
|
|
code_checksum = Integrity::Checksum.code
|
2019-04-03 17:57:21 +02:00
|
|
|
ApplicationController.new.view_context.render(
|
|
|
|
partial: 'archive/accounting',
|
|
|
|
locals: {
|
|
|
|
invoices: period.invoices_with_vat(invoices),
|
2021-06-10 11:36:32 +02:00
|
|
|
schedules: schedules,
|
2019-04-03 17:57:21 +02:00
|
|
|
period_total: period.period_total,
|
|
|
|
perpetual_total: period.perpetual_total,
|
|
|
|
period_footprint: period.footprint,
|
|
|
|
code_checksum: code_checksum,
|
|
|
|
last_archive_checksum: last_checksum,
|
|
|
|
previous_file: previous_file,
|
|
|
|
software_version: Version.current,
|
2023-02-14 13:10:58 +01:00
|
|
|
date: Time.current.iso8601
|
2019-04-03 17:57:21 +02:00
|
|
|
},
|
|
|
|
formats: [:json],
|
|
|
|
handlers: [:jbuilder]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|