From 87e3ba125f7be97780aca6c550f099858124cbe0 Mon Sep 17 00:00:00 2001 From: oleghasjanov Date: Tue, 16 Jun 2026 11:38:12 +0300 Subject: [PATCH 1/2] Disabled company registrar blocked invoice creation in billing side --- app/models/registrar.rb | 47 +++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/app/models/registrar.rb b/app/models/registrar.rb index afe304242f..f3d7459148 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -107,7 +107,31 @@ def init_monthly_invoice(summary) end def issue_prepayment_invoice(amount, description = nil, payable: true) - invoice = invoices.create!( + # Invoice creation and the billing push must be atomic: if the deposit + # cannot be registered in the billing system, the registry invoice (and the + # invoice number reserved for it) must be rolled back, otherwise the billing + # number sequence drifts and later invoices collide on `unique_number`. + invoice = transaction do + new_invoice = create_prepayment_invoice!(amount, description) + send_prepayment_invoice_to_billing(new_invoice) + new_invoice + end + + # Side effects below are best-effort and intentionally kept OUTSIDE the + # transaction: an external e-invoice/Business Registry hiccup must never roll + # back an already-registered invoice nor hold the DB transaction open. + unless payable && (accepts_e_invoices? && !accept_pdf_invoices?) + InvoiceMailer.invoice_email(invoice: invoice, recipient: billing_email, paid: !payable) + .deliver_later(wait: 1.minute) + end + + SendEInvoiceJob.set(wait: 1.minute).perform_now(invoice.id, payable: payable) + + invoice + end + + def create_prepayment_invoice!(amount, description) + invoices.create!( issue_date: Time.zone.today, due_date: (Time.zone.now + Setting.days_to_keep_invoices_active.days).to_date, description: description, @@ -150,21 +174,13 @@ def issue_prepayment_invoice(amount, description = nil, payable: true) }, ] ) + end - unless payable && (accepts_e_invoices? && !accept_pdf_invoices?) - InvoiceMailer.invoice_email(invoice: invoice, recipient: billing_email, paid: !payable) - .deliver_later(wait: 1.minute) - end - - add_invoice_instance = EisBilling::AddDeposits.new(invoice) - result = add_invoice_instance.call - + def send_prepayment_invoice_to_billing(invoice) + result = EisBilling::AddDeposits.new(invoice).call link = JSON.parse(result.body)['everypay_link'] - invoice.update(payment_link: link) - SendEInvoiceJob.set(wait: 1.minute).perform_now(invoice.id, payable: payable) - - invoice + invoice.update!(payment_link: link) end # rubocop:enable Metrics/MethodLength @@ -318,7 +334,10 @@ def accepts_e_invoices? result = company_register.e_invoice_recipients(registration_numbers: reg_no).first result.status == 'OK' - rescue CompanyRegister::NotAvailableError, CompanyRegister::SOAPFaultError => e + rescue StandardError => e + # Any failure to determine e-invoice status (Business Registry SOAP fault, + # network/SSL error, unexpected response) must degrade safely to "send PDF" + # and never break invoice creation. Rails.logger.error("Error checking e-invoice status for #{reg_no}: #{e.message}") false end From 7ee15389ddaf354e4755af72db94a3b0d77504c5 Mon Sep 17 00:00:00 2001 From: oleghasjanov Date: Tue, 16 Jun 2026 12:32:06 +0300 Subject: [PATCH 2/2] remove comments --- app/models/registrar.rb | 7 ------- 1 file changed, 7 deletions(-) diff --git a/app/models/registrar.rb b/app/models/registrar.rb index f3d7459148..2ea75ccb2d 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -107,19 +107,12 @@ def init_monthly_invoice(summary) end def issue_prepayment_invoice(amount, description = nil, payable: true) - # Invoice creation and the billing push must be atomic: if the deposit - # cannot be registered in the billing system, the registry invoice (and the - # invoice number reserved for it) must be rolled back, otherwise the billing - # number sequence drifts and later invoices collide on `unique_number`. invoice = transaction do new_invoice = create_prepayment_invoice!(amount, description) send_prepayment_invoice_to_billing(new_invoice) new_invoice end - # Side effects below are best-effort and intentionally kept OUTSIDE the - # transaction: an external e-invoice/Business Registry hiccup must never roll - # back an already-registered invoice nor hold the DB transaction open. unless payable && (accepts_e_invoices? && !accept_pdf_invoices?) InvoiceMailer.invoice_email(invoice: invoice, recipient: billing_email, paid: !payable) .deliver_later(wait: 1.minute)