From 03778ee8424e2f5339fd053e1089f28cd8739b7e Mon Sep 17 00:00:00 2001 From: Sergei Tsoganov Date: Thu, 24 Nov 2022 13:49:54 +0200 Subject: [PATCH 1/4] Added overwrite feature to SendMonthlyInvoicesJob --- app/jobs/send_monthly_invoices_job.rb | 5 +++-- app/models/concerns/registrar/book_keeping.rb | 17 ++++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/app/jobs/send_monthly_invoices_job.rb b/app/jobs/send_monthly_invoices_job.rb index 2c6e34831..c89e71b00 100644 --- a/app/jobs/send_monthly_invoices_job.rb +++ b/app/jobs/send_monthly_invoices_job.rb @@ -2,8 +2,9 @@ class SendMonthlyInvoicesJob < ApplicationJob # rubocop:disable Metrics/ClassLen queue_as :default discard_on StandardError - def perform(dry: false, months_ago: 1) + def perform(dry: false, months_ago: 1, overwrite: false) @dry = dry + @overwrite = overwrite @month = Time.zone.now - months_ago.month @directo_data = [] @@ -48,7 +49,7 @@ class SendMonthlyInvoicesJob < ApplicationJob # rubocop:disable Metrics/ClassLen def find_or_init_monthly_invoices(invoices: []) Registrar.with_cash_accounts.find_each do |registrar| - invoice = registrar.find_or_init_monthly_invoice(month: @month) + invoice = registrar.find_or_init_monthly_invoice(month: @month, overwrite: @overwrite) invoices << invoice unless invoice.nil? end invoices diff --git a/app/models/concerns/registrar/book_keeping.rb b/app/models/concerns/registrar/book_keeping.rb index 86eede779..a70b09c08 100644 --- a/app/models/concerns/registrar/book_keeping.rb +++ b/app/models/concerns/registrar/book_keeping.rb @@ -38,14 +38,25 @@ module Registrar::BookKeeping # rubocop:disable Metrics/ModuleLength lines.as_json end - def find_or_init_monthly_invoice(month:) + def find_or_init_monthly_invoice(month:, overwrite:) invoice = invoices.find_by(monthly_invoice: true, issue_date: month.end_of_month.to_date) - return invoice if invoice + return invoice if invoice && !overwrite summary = monthly_summary(month: month) return unless summary - init_monthly_invoice(summary) + new_invoice = init_monthly_invoice(summary) + return overwrite_invoice(invoice, new_invoice) if invoice && overwrite + + new_invoice + end + + def overwrite_invoice(original_invoice, new_invoice) + params_to_scrub = %i[created_at updated_at id number sent_at + e_invoice_sent_at in_directo cancelled_at payment_link] + attrs = new_invoice.attributes.with_indifferent_access.except(*params_to_scrub) + original_invoice.update(attrs) + original_invoice end def title_for_summary(date) From df332e40127784a84d45ff7f384938fece8433f5 Mon Sep 17 00:00:00 2001 From: Sergei Tsoganov Date: Thu, 24 Nov 2022 14:00:12 +0200 Subject: [PATCH 2/4] Fixed codeclimate issue --- app/models/concerns/registrar/book_keeping.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/models/concerns/registrar/book_keeping.rb b/app/models/concerns/registrar/book_keeping.rb index a70b09c08..571a64247 100644 --- a/app/models/concerns/registrar/book_keeping.rb +++ b/app/models/concerns/registrar/book_keeping.rb @@ -38,6 +38,7 @@ module Registrar::BookKeeping # rubocop:disable Metrics/ModuleLength lines.as_json end + # rubocop:disable Metrics/CyclomaticComplexity def find_or_init_monthly_invoice(month:, overwrite:) invoice = invoices.find_by(monthly_invoice: true, issue_date: month.end_of_month.to_date) return invoice if invoice && !overwrite @@ -50,6 +51,7 @@ module Registrar::BookKeeping # rubocop:disable Metrics/ModuleLength new_invoice end + # rubocop:enable Metrics/CyclomaticComplexity def overwrite_invoice(original_invoice, new_invoice) params_to_scrub = %i[created_at updated_at id number sent_at From 591dc5808302db10922cb0e9ace74618d7b1cb9d Mon Sep 17 00:00:00 2001 From: Sergei Tsoganov Date: Thu, 24 Nov 2022 14:09:02 +0200 Subject: [PATCH 3/4] Fixed codeclimate issue --- app/models/concerns/registrar/book_keeping.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/concerns/registrar/book_keeping.rb b/app/models/concerns/registrar/book_keeping.rb index 571a64247..8a35f7bbd 100644 --- a/app/models/concerns/registrar/book_keeping.rb +++ b/app/models/concerns/registrar/book_keeping.rb @@ -38,7 +38,7 @@ module Registrar::BookKeeping # rubocop:disable Metrics/ModuleLength lines.as_json end - # rubocop:disable Metrics/CyclomaticComplexity + # rubocop:disable Metrics/CognitiveComplexity def find_or_init_monthly_invoice(month:, overwrite:) invoice = invoices.find_by(monthly_invoice: true, issue_date: month.end_of_month.to_date) return invoice if invoice && !overwrite @@ -51,7 +51,7 @@ module Registrar::BookKeeping # rubocop:disable Metrics/ModuleLength new_invoice end - # rubocop:enable Metrics/CyclomaticComplexity + # rubocop:enable Metrics/CognitiveComplexity def overwrite_invoice(original_invoice, new_invoice) params_to_scrub = %i[created_at updated_at id number sent_at From 9a080915723dcdecf2aaf720fc0e54fe8cc4486b Mon Sep 17 00:00:00 2001 From: Sergei Tsoganov Date: Thu, 24 Nov 2022 14:10:55 +0200 Subject: [PATCH 4/4] Fixed codeclimate issue --- app/models/concerns/registrar/book_keeping.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/models/concerns/registrar/book_keeping.rb b/app/models/concerns/registrar/book_keeping.rb index 8a35f7bbd..a70b09c08 100644 --- a/app/models/concerns/registrar/book_keeping.rb +++ b/app/models/concerns/registrar/book_keeping.rb @@ -38,7 +38,6 @@ module Registrar::BookKeeping # rubocop:disable Metrics/ModuleLength lines.as_json end - # rubocop:disable Metrics/CognitiveComplexity def find_or_init_monthly_invoice(month:, overwrite:) invoice = invoices.find_by(monthly_invoice: true, issue_date: month.end_of_month.to_date) return invoice if invoice && !overwrite @@ -51,7 +50,6 @@ module Registrar::BookKeeping # rubocop:disable Metrics/ModuleLength new_invoice end - # rubocop:enable Metrics/CognitiveComplexity def overwrite_invoice(original_invoice, new_invoice) params_to_scrub = %i[created_at updated_at id number sent_at