From 71775378e86573a3776c40262895770334466f6a Mon Sep 17 00:00:00 2001 From: Sylvain Date: Thu, 10 Jun 2021 11:36:32 +0200 Subject: [PATCH] include payment schedules in accounting archives --- app/models/accounting_period.rb | 4 +++ app/views/archive/_accounting.json.jbuilder | 32 +++++++++++++++++++++ app/workers/archive_worker.rb | 9 +++--- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/app/models/accounting_period.rb b/app/models/accounting_period.rb index d0274cc5f..d068d5624 100644 --- a/app/models/accounting_period.rb +++ b/app/models/accounting_period.rb @@ -27,6 +27,10 @@ class AccountingPeriod < ApplicationRecord Invoice.where('created_at >= :start_date AND CAST(created_at AS DATE) <= :end_date', start_date: start_at, end_date: end_at) end + def payment_schedules + PaymentSchedule.where('created_at >= :start_date AND CAST(created_at AS DATE) <= :end_date', start_date: start_at, end_date: end_at) + end + def invoices_with_vat(invoices) vat_service = VatHistoryService.new invoices.map do |i| diff --git a/app/views/archive/_accounting.json.jbuilder b/app/views/archive/_accounting.json.jbuilder index 7491b3f53..48b024c44 100644 --- a/app/views/archive/_accounting.json.jbuilder +++ b/app/views/archive/_accounting.json.jbuilder @@ -32,12 +32,44 @@ json.invoices do json.object do json.type item.object_type json.id item.object_id + json.main item.main end json.partial! 'archive/vat', price: item.amount, vat_rate: invoice[:vat_rate] end end end +json.payment_schedules do + json.array!(schedules) do |schedule| + json.extract! schedule, :id, :payment_method, :created_at, :reference, :footprint + json.payment_gateway_objects schedule.payment_gateway_objects do |object| + json.id object.gateway_object_id + json.type object.gateway_object_type + end + json.total number_to_currency(schedule.total / 100.0) + json.user do + json.extract! schedule.invoicing_profile, :user_id, :email, :first_name, :last_name + json.address schedule.invoicing_profile&.address&.address + json.invoicing_profile_id schedule.invoicing_profile.id + if schedule.invoicing_profile.organization + json.organization do + json.extract! schedule.invoicing_profile.organization, :name, :id + json.address schedule.invoicing_profile.organization&.address&.address + end + end + end + json.deadlines schedule.payment_schedule_items do |item| + json.extract! item, :id, :due_date, :state, :details, :invoice_id, :footprint, :created_at + json.amount number_to_currency(item.amount / 100.0) + end + json.objects schedule.payment_schedule_objects do |object| + json.type object.object_type + json.id object.object_id + json.main object.main + end + end +end + json.totals do json.period_total number_to_currency(period_total / 100.0) json.perpetual_total number_to_currency(perpetual_total / 100.0) diff --git a/app/workers/archive_worker.rb b/app/workers/archive_worker.rb index 7b53b7843..c7fb99124 100644 --- a/app/workers/archive_worker.rb +++ b/app/workers/archive_worker.rb @@ -9,11 +9,11 @@ class ArchiveWorker def perform(accounting_period_id) period = AccountingPeriod.find(accounting_period_id) - - data = period.invoices.includes(:invoice_items).order(id: :asc) + 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) previous_file = period.previous_period&.archive_file last_archive_checksum = previous_file ? Integrity::Checksum.file(previous_file) : nil - json_data = to_json_archive(period, data, previous_file, last_archive_checksum) + json_data = to_json_archive(period, invoices, schedules, previous_file, last_archive_checksum) current_archive_checksum = Integrity::Checksum.text(json_data) date = DateTime.iso8601 chained = Integrity::Checksum.text("#{current_archive_checksum}#{last_archive_checksum}#{date}") @@ -34,12 +34,13 @@ class ArchiveWorker private - def to_json_archive(period, invoices, previous_file, last_checksum) + def to_json_archive(period, invoices, schedules, previous_file, last_checksum) code_checksum = Integrity::Checksum.code ApplicationController.new.view_context.render( partial: 'archive/accounting', locals: { invoices: period.invoices_with_vat(invoices), + schedules: schedules, period_total: period.period_total, perpetual_total: period.perpetual_total, period_footprint: period.footprint,