Refactored code

This commit is contained in:
Sergei Tsõganov 2022-08-22 10:23:14 +03:00
parent d589aa1681
commit 72022aab8d
4 changed files with 78 additions and 69 deletions

View file

@ -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?

View file

@ -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