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)
|
in_directo: false, e_invoice_sent_at: nil)
|
||||||
invoices.delete_all
|
invoices.delete_all
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -16,22 +16,17 @@ class SendMonthlyInvoicesJob < ApplicationJob
|
||||||
Setting.directo_receipt_payment_term)
|
Setting.directo_receipt_payment_term)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# rubocop:disable Metrics/MethodLength
|
||||||
def send_monthly_invoices
|
def send_monthly_invoices
|
||||||
Registrar.where.not(test_registrar: true).find_each do |registrar|
|
Registrar.with_cash_accounts.find_each do |registrar|
|
||||||
next unless registrar.cash_account
|
|
||||||
|
|
||||||
summary = registrar.monthly_summary(month: @month)
|
summary = registrar.monthly_summary(month: @month)
|
||||||
next if summary.nil?
|
next if summary.nil?
|
||||||
|
|
||||||
invoice = registrar.monthly_invoice(month: @month) || create_invoice(summary, registrar)
|
invoice = registrar.monthly_invoice(month: @month) || create_invoice(summary, registrar)
|
||||||
next if invoice.nil? || @dry
|
next if invoice.nil? || @dry
|
||||||
|
|
||||||
InvoiceMailer.invoice_email(invoice: invoice,
|
send_email_to_registrar(invoice: invoice, registrar: registrar)
|
||||||
recipient: registrar.billing_email)
|
send_e_invoice(invoice.id)
|
||||||
.deliver_now
|
|
||||||
|
|
||||||
SendEInvoiceJob.set(wait: 1.minute).perform_now(invoice.id, payable: false)
|
|
||||||
|
|
||||||
next if invoice.in_directo
|
next if invoice.in_directo
|
||||||
|
|
||||||
Rails.logger.info("[DIRECTO] Trying to send monthly invoice #{invoice.number}")
|
Rails.logger.info("[DIRECTO] Trying to send monthly invoice #{invoice.number}")
|
||||||
|
@ -44,50 +39,24 @@ class SendMonthlyInvoicesJob < ApplicationJob
|
||||||
sync_with_directo
|
sync_with_directo
|
||||||
end
|
end
|
||||||
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)
|
def create_invoice(summary, registrar)
|
||||||
vat_rate = ::Invoice::VatRateCalculator.new(registrar: registrar).calculate
|
invoice = registrar.init_monthly_invoice(summary)
|
||||||
invoice = Invoice.new(
|
invoice.number = assign_monthly_number
|
||||||
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
|
|
||||||
)
|
|
||||||
return unless invoice.save!
|
return unless invoice.save!
|
||||||
|
|
||||||
update_directo_number(num: invoice.number)
|
update_monthly_invoice_number(num: invoice.number)
|
||||||
invoice
|
invoice
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -103,19 +72,6 @@ class SendMonthlyInvoicesJob < ApplicationJob
|
||||||
Rails.logger.info('[Directo] Failed to communicate via API')
|
Rails.logger.info('[Directo] Failed to communicate via API')
|
||||||
end
|
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
|
def assign_monthly_number
|
||||||
last_directo_num = [Setting.directo_monthly_number_last.presence.try(:to_i),
|
last_directo_num = [Setting.directo_monthly_number_last.presence.try(:to_i),
|
||||||
@min_directo_num].compact.max || 0
|
@min_directo_num].compact.max || 0
|
||||||
|
@ -134,11 +90,11 @@ class SendMonthlyInvoicesJob < ApplicationJob
|
||||||
Rails.logger.info "[Directo] - Responded with body: #{body}"
|
Rails.logger.info "[Directo] - Responded with body: #{body}"
|
||||||
Nokogiri::XML(body).css('Result').each do |res|
|
Nokogiri::XML(body).css('Result').each do |res|
|
||||||
inv = Invoice.find_by(number: res.attributes['docid'].value.to_i)
|
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
|
||||||
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,
|
directo_record = Directo.new(response: res.as_json.to_h,
|
||||||
request: req, invoice_number: res.attributes['docid'].value.to_i)
|
request: req, invoice_number: res.attributes['docid'].value.to_i)
|
||||||
directo_record.item = invoice
|
directo_record.item = invoice
|
||||||
|
@ -147,7 +103,7 @@ class SendMonthlyInvoicesJob < ApplicationJob
|
||||||
directo_record.save!
|
directo_record.save!
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_directo_number(num:)
|
def update_monthly_invoice_number(num:)
|
||||||
return unless num.to_i > Setting.directo_monthly_number_last.to_i
|
return unless num.to_i > Setting.directo_monthly_number_last.to_i
|
||||||
|
|
||||||
Setting.directo_monthly_number_last = num.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',
|
DOMAIN_TO_PRODUCT = { 'ee': '01EE', 'com.ee': '02COM', 'pri.ee': '03PRI',
|
||||||
'fie.ee': '04FIE', 'med.ee': '05MED' }.freeze
|
'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:)
|
def monthly_summary(month:)
|
||||||
activities = monthly_activites(month)
|
activities = monthly_activites(month)
|
||||||
return unless activities.any?
|
return unless activities.any?
|
||||||
|
|
|
@ -56,9 +56,48 @@ class Registrar < ApplicationRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def issue_prepayment_invoice(amount, description = nil, payable: true)
|
# rubocop:disable Metrics/MethodLength
|
||||||
vat_rate = ::Invoice::VatRateCalculator.new(registrar: self).calculate
|
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!(
|
invoice = invoices.create!(
|
||||||
issue_date: Time.zone.today,
|
issue_date: Time.zone.today,
|
||||||
due_date: (Time.zone.now + Setting.days_to_keep_invoices_active.days).to_date,
|
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_url: website,
|
||||||
buyer_email: email,
|
buyer_email: email,
|
||||||
reference_no: reference_no,
|
reference_no: reference_no,
|
||||||
vat_rate: vat_rate,
|
vat_rate: calculate_vat_rate,
|
||||||
items_attributes: [
|
items_attributes: [
|
||||||
{
|
{
|
||||||
description: 'prepayment',
|
description: 'prepayment',
|
||||||
|
@ -124,6 +163,7 @@ class Registrar < ApplicationRecord
|
||||||
|
|
||||||
invoice
|
invoice
|
||||||
end
|
end
|
||||||
|
# rubocop:enable Metrics/MethodLength
|
||||||
|
|
||||||
def cash_account
|
def cash_account
|
||||||
accounts.find_by(account_type: Account::CASH)
|
accounts.find_by(account_type: Account::CASH)
|
||||||
|
@ -265,4 +305,8 @@ class Registrar < ApplicationRecord
|
||||||
def vat_liable_in_foreign_country?
|
def vat_liable_in_foreign_country?
|
||||||
!vat_liable_locally?
|
!vat_liable_locally?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def calculate_vat_rate
|
||||||
|
::Invoice::VatRateCalculator.new(registrar: self).calculate
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue