diff --git a/app/jobs/send_monthly_invoices_job.rb b/app/jobs/send_monthly_invoices_job.rb index c89e71b00..cd553237f 100644 --- a/app/jobs/send_monthly_invoices_job.rb +++ b/app/jobs/send_monthly_invoices_job.rb @@ -1,4 +1,4 @@ -class SendMonthlyInvoicesJob < ApplicationJob # rubocop:disable Metrics/ClassLength +class SendMonthlyInvoicesJob < ApplicationJob queue_as :default discard_on StandardError @@ -62,6 +62,7 @@ class SendMonthlyInvoicesJob < ApplicationJob # rubocop:disable Metrics/ClassLen private def handle_assign_numbers_response_errors(response) + raise 'ASSIGN NUMBER RESPONSE ERROR: Not Found' if response['error'] == 'Not Found' raise 'INVOICE NUMBER LIMIT REACHED, COULD NOT GENERATE INVOICE' if response['code'] == '403' raise 'PROBLEM WITH TOKEN' if response['error'] == 'out of range' end diff --git a/app/models/concerns/registrar/book_keeping.rb b/app/models/concerns/registrar/book_keeping.rb index a70b09c08..746bff331 100644 --- a/app/models/concerns/registrar/book_keeping.rb +++ b/app/models/concerns/registrar/book_keeping.rb @@ -1,4 +1,4 @@ -module Registrar::BookKeeping # rubocop:disable Metrics/ModuleLength +module Registrar::BookKeeping extend ActiveSupport::Concern DOMAIN_TO_PRODUCT = { 'ee': '01EE', 'com.ee': '02COM', 'pri.ee': '03PRI', @@ -39,7 +39,8 @@ module Registrar::BookKeeping # rubocop:disable Metrics/ModuleLength end def find_or_init_monthly_invoice(month:, overwrite:) - invoice = invoices.find_by(monthly_invoice: true, issue_date: month.end_of_month.to_date) + invoice = invoices.find_by(monthly_invoice: true, issue_date: month.end_of_month.to_date, + cancelled_at: nil) return invoice if invoice && !overwrite summary = monthly_summary(month: month) diff --git a/test/jobs/send_monthly_invoices_job_test.rb b/test/jobs/send_monthly_invoices_job_test.rb index 92b2af6be..dad481477 100644 --- a/test/jobs/send_monthly_invoices_job_test.rb +++ b/test/jobs/send_monthly_invoices_job_test.rb @@ -50,14 +50,14 @@ class SendMonthlyInvoicesJobTest < ActiveSupport::TestCase end def test_monthly_summary_is_delivered_if_invoice_already_exists - @monthly_invoice = invoices(:one) - @monthly_invoice.update(number: 309_902, monthly_invoice: true, - issue_date: @date.last_month.end_of_month, - due_date: @date.last_month.end_of_month, - metadata: metadata, - in_directo: false, - sent_at: nil, - e_invoice_sent_at: nil) + monthly_invoice = invoices(:one) + monthly_invoice.update(number: 309_902, monthly_invoice: true, + issue_date: @date.last_month.end_of_month, + due_date: @date.last_month.end_of_month, + metadata: metadata, + in_directo: false, + sent_at: nil, + e_invoice_sent_at: nil) activity = account_activities(:one) price = billing_prices(:create_one_year) @@ -77,9 +77,49 @@ class SendMonthlyInvoicesJobTest < ActiveSupport::TestCase SendMonthlyInvoicesJob.perform_now end end - @monthly_invoice.reload + monthly_invoice.reload - assert_not_nil @monthly_invoice.sent_at + assert_not_nil monthly_invoice.sent_at + assert_emails 1 + end + + def test_new_monthly_summary_is_delivered_if_invoice_cancelled + monthly_invoice = invoices(:one) + monthly_invoice.update(number: 309_902, monthly_invoice: true, + issue_date: @date.last_month.end_of_month, + due_date: @date.last_month.end_of_month, + metadata: metadata, + in_directo: false, + sent_at: nil, + cancelled_at: Time.now, + e_invoice_sent_at: nil) + + activity = account_activities(:one) + price = billing_prices(:create_one_year) + activity.update!(activity_type: 'create', price: price) + @user.update(language: 'et') + + stub_request(:post, @monthly_invoice_numbers_generator_url) + .to_return(status: :ok, body: { invoice_numbers: [309_903] }.to_json, headers: {}) + + stub_request(:post, @directo_url).with do |request| + body = CGI.unescape(request.body) + + (body.include? '.test registreerimine: 1 aasta(t)') && + (body.include? 'Domeenide ettemaks') && + (body.include? '309903') + end.to_return(status: 200, body: @response) + + assert_enqueued_jobs 1, only: SendEInvoiceJob do + assert_difference('Invoice.count', 1) do + SendMonthlyInvoicesJob.perform_now + end + end + monthly_invoice.reload + last_invoice = Invoice.last + assert_not_nil last_invoice.sent_at + assert_equal last_invoice.issue_date, monthly_invoice.issue_date + assert_nil monthly_invoice.sent_at assert_emails 1 end