Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ gem 'omniauth-tara', github: 'internetee/omniauth-tara'
# gem 'omniauth-tara', path: 'vendor/gems/omniauth-tara'

gem 'airbrake'
gem 'company_register', github: 'internetee/company_register', branch: :master
gem 'company_register', github: 'internetee/company_register', branch: 'master'
gem 'directo', github: 'internetee/directo', branch: 'master'
gem 'domain_name'
gem 'dry-struct'
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GIT
remote: https://github.com/internetee/company_register.git
revision: 2c31da54c57db13324161eeb8db7e9f81af81987
revision: d83597c5cc06c1909637e56d937a5b3bcb6286c6
branch: master
specs:
company_register (0.1.0)
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/repp/v1/accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def details
api_users: serialized_users(current_user.api_users),
white_ips: serialized_ips(registrar.white_ips),
balance_auto_reload: type,
accept_pdf_invoices: registrar.accept_pdf_invoices,
min_deposit: Setting.minimum_deposit },
roles: ApiUser::ROLES,
interfaces: WhiteIp::INTERFACES }
Expand Down Expand Up @@ -117,7 +118,7 @@ def balance
private

def account_params
params.require(:account).permit(:billing_email, :iban, :new_user_id)
params.require(:account).permit(:billing_email, :iban, :new_user_id, :accept_pdf_invoices)
end

def index_params
Expand Down
28 changes: 25 additions & 3 deletions app/models/contact/company_register.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,43 @@ def return_company_status
end

def return_company_data
return unless org?
return unless is_contact_estonian_org?

company_register.simple_data(registration_number: ident.to_s)
rescue CompanyRegister::NotAvailableError
[]
end

def return_company_details
return unless org?
return unless is_contact_estonian_org?

company_register.company_details(registration_number: ident.to_s)
rescue CompanyRegister::NotAvailableError
[]
end

def e_invoice_recipients
return unless is_contact_estonian_org?

company_register.e_invoice_recipients(registration_numbers: ident.to_s)
rescue CompanyRegister::SOAPFaultError => e
Rails.logger.error("SOAP Fault getting company details for #{ident}: #{e.message}")
raise e
rescue CompanyRegister::NotAvailableError
[]
end

def org_contact_accept_e_invoice?
return unless is_contact_estonian_org?

result = e_invoice_recipients.first
result.status == 'OK'
end

def is_contact_estonian_org?
org? && ident_country_code == 'EE'
end

def company_register
@company_register ||= CompanyRegister::Client.new
end
Expand Down
16 changes: 15 additions & 1 deletion app/models/registrar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def issue_prepayment_invoice(amount, description = nil, payable: true)
]
)

unless payable
unless payable && (accepts_e_invoices? && !accept_pdf_invoices?)
InvoiceMailer.invoice_email(invoice: invoice, recipient: billing_email, paid: !payable)
.deliver_later(wait: 1.minute)
end
Expand Down Expand Up @@ -307,6 +307,20 @@ def billing_email
self[:billing_email]
end

def accepts_e_invoices?
return false unless address_country_code == 'EE'

result = company_register.e_invoice_recipients(registration_numbers: reg_no).first
result.status == 'OK'
rescue CompanyRegister::NotAvailableError, CompanyRegister::SOAPFaultError => e
Rails.logger.error("Error checking e-invoice status for #{reg_no}: #{e.message}")
false
end

def company_register
@company_register ||= CompanyRegister::Client.new
end

private

def domain_not_updatable?(hostname:, domain:)
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20251230104312_accept_pdf_invoices.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AcceptPdfInvoices < ActiveRecord::Migration[6.1]
def change
add_column :registrars, :accept_pdf_invoices, :boolean, default: true
end
end
21 changes: 15 additions & 6 deletions db/structure.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
\restrict manNM7YktJCYjflsdAWd12PkDkR5VB5nDAhRVyBVVHwcDScSXNtcviWOHPpPk1Q

-- Dumped from database version 13.4 (Debian 13.4-4.pgdg110+1)
-- Dumped by pg_dump version 13.22 (Debian 13.22-0+deb11u1)

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
Expand Down Expand Up @@ -2641,7 +2646,8 @@ CREATE TABLE public.registrars (
settings jsonb DEFAULT '{}'::jsonb NOT NULL,
legaldoc_optout boolean DEFAULT false NOT NULL,
legaldoc_optout_comment text,
email_history character varying
email_history character varying,
accept_pdf_invoices boolean DEFAULT true
);


Expand Down Expand Up @@ -5279,6 +5285,8 @@ ALTER TABLE ONLY public.users
-- PostgreSQL database dump complete
--

\unrestrict manNM7YktJCYjflsdAWd12PkDkR5VB5nDAhRVyBVVHwcDScSXNtcviWOHPpPk1Q

SET search_path TO "$user", public;

INSERT INTO "schema_migrations" (version) VALUES
Expand Down Expand Up @@ -5767,10 +5775,13 @@ INSERT INTO "schema_migrations" (version) VALUES
('20230707084741'),
('20230710120154'),
('20230711083811'),
('20240722085530'),
('20240723110208'),
('20240816091049'),
('20240816092636'),
('20240924103554'),
('20241015071505'),
('20241022121525'),
('20241030095636'),
('20241104104620'),
('20241112093540'),
Expand All @@ -5779,12 +5790,10 @@ INSERT INTO "schema_migrations" (version) VALUES
('20241206085817'),
('20250204094550'),
('20250219102811'),
('20250313122119'),
('20250319104749'),
('20250310133151'),
('20250313122119'),
('20250314133357'),
('20240722085530'),
('20240723110208'),
('20241022121525');
('20250319104749'),
('20251230104312');


2 changes: 2 additions & 0 deletions test/models/contact/company_register_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
class CompanyRegisterTest < ActiveSupport::TestCase
def setup
@acme_ltd = contacts(:acme_ltd)
@acme_ltd.update!(ident_country_code: 'EE', ident_type: 'org', ident: '12345678')

@john = contacts(:john)
@company_register_stub = CompanyRegister::Client.new
end
Expand Down
55 changes: 55 additions & 0 deletions test/models/registrar/invoice_email_cancellation_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'test_helper'

class RegistrarInvoiceEmailCancellationTest < ActiveJob::TestCase
setup do
@registrar = registrars(:bestnames)
@registrar.update!(address_country_code: 'EE', reference_no: '1232', vat_rate: 24)
@invoice_params = 100

stub_request(:post, "https://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator").
to_return(status: 200, body: "{\"invoice_number\":\"123456\"}", headers: {})

stub_request(:post, "https://eis_billing_system:3000/api/v1/invoice_generator/invoice_generator").
to_return(status: 200, body: "{\"everypay_link\":\"http://link.test\"}", headers: {})

stub_request(:put, "https://registry:3000/eis_billing/e_invoice_response").
to_return(status: 200, body: "{\"invoice_number\":\"123456\"}, {\"date\":\"#{Time.zone.now}\"}", headers: {})

stub_request(:post, "https://eis_billing_system:3000/api/v1/e_invoice/e_invoice").
to_return(status: 200, body: "", headers: {})
end

def test_sends_email_when_registrar_does_not_accept_e_invoices_and_pdf_opt_in_is_true
# Name does not contain 'einvoice', so stub returns 'MR' (not OK)
@registrar.update!(name: 'simple-registrar', accept_pdf_invoices: true)

assert_enqueued_jobs 1, only: ActionMailer::MailDeliveryJob do
@registrar.issue_prepayment_invoice(@invoice_params)
end
end

def test_sends_email_when_registrar_does_not_accept_e_invoices_and_pdf_opt_in_is_false
# Name does not contain 'einvoice', so stub returns 'MR' (not OK)
@registrar.update!(name: 'simple-registrar', accept_pdf_invoices: false)

assert_enqueued_jobs 1, only: ActionMailer::MailDeliveryJob do
@registrar.issue_prepayment_invoice(@invoice_params)
end
end

def test_skips_email_when_registrar_accepts_e_invoices_and_pdf_opt_in_is_false
@registrar.update!(name: 'einvoice-registrar', accept_pdf_invoices: false)

assert_enqueued_jobs 0, only: ActionMailer::MailDeliveryJob do
@registrar.issue_prepayment_invoice(@invoice_params)
end
end

def test_sends_email_when_registrar_accepts_e_invoices_BUT_pdf_opt_in_is_true
@registrar.update!(name: 'einvoice-registrar', accept_pdf_invoices: true)

assert_enqueued_jobs 1, only: ActionMailer::MailDeliveryJob do
@registrar.issue_prepayment_invoice(@invoice_params)
end
end
end
9 changes: 9 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ def simple_data(registration_number:)
def company_details(registration_number:)
[]
end

def e_invoice_recipients(registration_numbers:)
if ::Registrar.exists?(reg_no: registration_numbers)
registrar = ::Registrar.find_by(reg_no: registration_numbers)
status = registrar.name.include?('einvoice') ? 'OK' : 'MR'
return [Struct.new(:status).new(status)]
end
[Struct.new(:status).new('OK')]
end
end

CompanyRegister::Client = CompanyRegisterClientStub
Expand Down
Loading