mirror of
https://github.com/internetee/registry.git
synced 2025-07-28 05:26:17 +02:00
Refactored code
This commit is contained in:
parent
d589aa1681
commit
72022aab8d
4 changed files with 78 additions and 69 deletions
|
@ -7,4 +7,4 @@ class DeleteMonthlyInvoicesJob < ApplicationJob
|
|||
in_directo: false, e_invoice_sent_at: nil)
|
||||
invoices.delete_all
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -16,22 +16,17 @@ class SendMonthlyInvoicesJob < ApplicationJob
|
|||
Setting.directo_receipt_payment_term)
|
||||
end
|
||||
|
||||
# rubocop:disable Metrics/MethodLength
|
||||
def send_monthly_invoices
|
||||
Registrar.where.not(test_registrar: true).find_each do |registrar|
|
||||
next unless registrar.cash_account
|
||||
|
||||
Registrar.with_cash_accounts.find_each do |registrar|
|
||||
summary = registrar.monthly_summary(month: @month)
|
||||
next if summary.nil?
|
||||
|
||||
invoice = registrar.monthly_invoice(month: @month) || create_invoice(summary, registrar)
|
||||
next if invoice.nil? || @dry
|
||||
|
||||
InvoiceMailer.invoice_email(invoice: invoice,
|
||||
recipient: registrar.billing_email)
|
||||
.deliver_now
|
||||
|
||||
SendEInvoiceJob.set(wait: 1.minute).perform_now(invoice.id, payable: false)
|
||||
|
||||
send_email_to_registrar(invoice: invoice, registrar: registrar)
|
||||
send_e_invoice(invoice.id)
|
||||
next if invoice.in_directo
|
||||
|
||||
Rails.logger.info("[DIRECTO] Trying to send monthly invoice #{invoice.number}")
|
||||
|
@ -44,50 +39,24 @@ class SendMonthlyInvoicesJob < ApplicationJob
|
|||
sync_with_directo
|
||||
end
|
||||
end
|
||||
# rubocop:enable Metrics/MethodLength
|
||||
|
||||
def send_email_to_registrar(invoice:, registrar:)
|
||||
InvoiceMailer.invoice_email(invoice: invoice,
|
||||
recipient: registrar.billing_email)
|
||||
.deliver_now
|
||||
end
|
||||
|
||||
def send_e_invoice(invoice_id)
|
||||
SendEInvoiceJob.set(wait: 1.minute).perform_now(invoice_id, payable: false)
|
||||
end
|
||||
|
||||
def create_invoice(summary, registrar)
|
||||
vat_rate = ::Invoice::VatRateCalculator.new(registrar: registrar).calculate
|
||||
invoice = Invoice.new(
|
||||
number: assign_monthly_number,
|
||||
issue_date: summary['date'].to_date,
|
||||
due_date: summary['date'].to_date,
|
||||
currency: 'EUR',
|
||||
description: I18n.t('invoice.monthly_invoice_description'),
|
||||
seller_name: Setting.registry_juridical_name,
|
||||
seller_reg_no: Setting.registry_reg_no,
|
||||
seller_iban: Setting.registry_iban,
|
||||
seller_bank: Setting.registry_bank,
|
||||
seller_swift: Setting.registry_swift,
|
||||
seller_vat_no: Setting.registry_vat_no,
|
||||
seller_country_code: Setting.registry_country_code,
|
||||
seller_state: Setting.registry_state,
|
||||
seller_street: Setting.registry_street,
|
||||
seller_city: Setting.registry_city,
|
||||
seller_zip: Setting.registry_zip,
|
||||
seller_phone: Setting.registry_phone,
|
||||
seller_url: Setting.registry_url,
|
||||
seller_email: Setting.registry_email,
|
||||
seller_contact_name: Setting.registry_invoice_contact,
|
||||
buyer: registrar,
|
||||
buyer_name: registrar.name,
|
||||
buyer_reg_no: registrar.reg_no,
|
||||
buyer_country_code: registrar.address_country_code,
|
||||
buyer_state: registrar.address_state,
|
||||
buyer_street: registrar.address_street,
|
||||
buyer_city: registrar.address_city,
|
||||
buyer_zip: registrar.address_zip,
|
||||
buyer_phone: registrar.phone,
|
||||
buyer_url: registrar.website,
|
||||
buyer_email: registrar.email,
|
||||
reference_no: registrar.reference_no,
|
||||
vat_rate: vat_rate,
|
||||
monthly_invoice: true,
|
||||
metadata: { items: summary['invoice_lines'] },
|
||||
total: 0
|
||||
)
|
||||
invoice = registrar.init_monthly_invoice(summary)
|
||||
invoice.number = assign_monthly_number
|
||||
return unless invoice.save!
|
||||
|
||||
update_directo_number(num: invoice.number)
|
||||
update_monthly_invoice_number(num: invoice.number)
|
||||
invoice
|
||||
end
|
||||
|
||||
|
@ -103,19 +72,6 @@ class SendMonthlyInvoicesJob < ApplicationJob
|
|||
Rails.logger.info('[Directo] Failed to communicate via API')
|
||||
end
|
||||
|
||||
def assign_monthly_numbers
|
||||
invoices_count = @directo_client.invoices.count
|
||||
last_directo_num = [Setting.directo_monthly_number_last.presence.try(:to_i),
|
||||
@min_directo_num].compact.max || 0
|
||||
raise 'Directo Counter is out of period!' if directo_counter_exceedable?(invoices_count,
|
||||
last_directo_num)
|
||||
|
||||
@directo_client.invoices.each do |inv|
|
||||
last_directo_num += 1
|
||||
inv.number = last_directo_num
|
||||
end
|
||||
end
|
||||
|
||||
def assign_monthly_number
|
||||
last_directo_num = [Setting.directo_monthly_number_last.presence.try(:to_i),
|
||||
@min_directo_num].compact.max || 0
|
||||
|
@ -134,11 +90,11 @@ class SendMonthlyInvoicesJob < ApplicationJob
|
|||
Rails.logger.info "[Directo] - Responded with body: #{body}"
|
||||
Nokogiri::XML(body).css('Result').each do |res|
|
||||
inv = Invoice.find_by(number: res.attributes['docid'].value.to_i)
|
||||
mark_invoice_as_sent(res: res, req: req, invoice: inv)
|
||||
mark_invoice_as_sent_to_directo(res: res, req: req, invoice: inv)
|
||||
end
|
||||
end
|
||||
|
||||
def mark_invoice_as_sent(res:, req:, invoice: nil)
|
||||
def mark_invoice_as_sent_to_directo(res:, req:, invoice: nil)
|
||||
directo_record = Directo.new(response: res.as_json.to_h,
|
||||
request: req, invoice_number: res.attributes['docid'].value.to_i)
|
||||
directo_record.item = invoice
|
||||
|
@ -147,7 +103,7 @@ class SendMonthlyInvoicesJob < ApplicationJob
|
|||
directo_record.save!
|
||||
end
|
||||
|
||||
def update_directo_number(num:)
|
||||
def update_monthly_invoice_number(num:)
|
||||
return unless num.to_i > Setting.directo_monthly_number_last.to_i
|
||||
|
||||
Setting.directo_monthly_number_last = num.to_i
|
||||
|
|
|
@ -4,6 +4,15 @@ module Registrar::BookKeeping
|
|||
DOMAIN_TO_PRODUCT = { 'ee': '01EE', 'com.ee': '02COM', 'pri.ee': '03PRI',
|
||||
'fie.ee': '04FIE', 'med.ee': '05MED' }.freeze
|
||||
|
||||
included do
|
||||
scope :with_cash_accounts, (lambda do
|
||||
joins(:accounts)
|
||||
.where('accounts.account_type = ? AND test_registrar != ?',
|
||||
Account::CASH,
|
||||
true)
|
||||
end)
|
||||
end
|
||||
|
||||
def monthly_summary(month:)
|
||||
activities = monthly_activites(month)
|
||||
return unless activities.any?
|
||||
|
|
|
@ -56,9 +56,48 @@ class Registrar < ApplicationRecord
|
|||
end
|
||||
end
|
||||
|
||||
def issue_prepayment_invoice(amount, description = nil, payable: true)
|
||||
vat_rate = ::Invoice::VatRateCalculator.new(registrar: self).calculate
|
||||
# rubocop:disable Metrics/MethodLength
|
||||
def init_monthly_invoice(summary)
|
||||
Invoice.new(
|
||||
issue_date: summary['date'].to_date,
|
||||
due_date: summary['date'].to_date,
|
||||
currency: 'EUR',
|
||||
description: I18n.t('invoice.monthly_invoice_description'),
|
||||
seller_name: Setting.registry_juridical_name,
|
||||
seller_reg_no: Setting.registry_reg_no,
|
||||
seller_iban: Setting.registry_iban,
|
||||
seller_bank: Setting.registry_bank,
|
||||
seller_swift: Setting.registry_swift,
|
||||
seller_vat_no: Setting.registry_vat_no,
|
||||
seller_country_code: Setting.registry_country_code,
|
||||
seller_state: Setting.registry_state,
|
||||
seller_street: Setting.registry_street,
|
||||
seller_city: Setting.registry_city,
|
||||
seller_zip: Setting.registry_zip,
|
||||
seller_phone: Setting.registry_phone,
|
||||
seller_url: Setting.registry_url,
|
||||
seller_email: Setting.registry_email,
|
||||
seller_contact_name: Setting.registry_invoice_contact,
|
||||
buyer: self,
|
||||
buyer_name: name,
|
||||
buyer_reg_no: reg_no,
|
||||
buyer_country_code: address_country_code,
|
||||
buyer_state: address_state,
|
||||
buyer_street: address_street,
|
||||
buyer_city: address_city,
|
||||
buyer_zip: address_zip,
|
||||
buyer_phone: phone,
|
||||
buyer_url: website,
|
||||
buyer_email: email,
|
||||
reference_no: reference_no,
|
||||
vat_rate: calculate_vat_rate,
|
||||
monthly_invoice: true,
|
||||
metadata: { items: summary['invoice_lines'] },
|
||||
total: 0
|
||||
)
|
||||
end
|
||||
|
||||
def issue_prepayment_invoice(amount, description = nil, payable: true)
|
||||
invoice = invoices.create!(
|
||||
issue_date: Time.zone.today,
|
||||
due_date: (Time.zone.now + Setting.days_to_keep_invoices_active.days).to_date,
|
||||
|
@ -91,7 +130,7 @@ class Registrar < ApplicationRecord
|
|||
buyer_url: website,
|
||||
buyer_email: email,
|
||||
reference_no: reference_no,
|
||||
vat_rate: vat_rate,
|
||||
vat_rate: calculate_vat_rate,
|
||||
items_attributes: [
|
||||
{
|
||||
description: 'prepayment',
|
||||
|
@ -124,6 +163,7 @@ class Registrar < ApplicationRecord
|
|||
|
||||
invoice
|
||||
end
|
||||
# rubocop:enable Metrics/MethodLength
|
||||
|
||||
def cash_account
|
||||
accounts.find_by(account_type: Account::CASH)
|
||||
|
@ -265,4 +305,8 @@ class Registrar < ApplicationRecord
|
|||
def vat_liable_in_foreign_country?
|
||||
!vat_liable_locally?
|
||||
end
|
||||
|
||||
def calculate_vat_rate
|
||||
::Invoice::VatRateCalculator.new(registrar: self).calculate
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue