Merge pull request #2424 from internetee/send-monthly-reports

Created job for sending monthly invoices
This commit is contained in:
Timo Võhmar 2022-08-31 16:56:41 +03:00 committed by GitHub
commit dd34dceb51
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 1225 additions and 63 deletions

View file

@ -0,0 +1,10 @@
class DeleteMonthlyInvoicesJob < ApplicationJob
queue_as :default
def perform
@month = Time.zone.now - 1.month
invoices = Invoice.where(monthly_invoice: true, issue_date: @month.end_of_month.to_date,
in_directo: false, e_invoice_sent_at: nil)
invoices.delete_all
end
end

View file

@ -16,8 +16,9 @@ class SendEInvoiceJob < ApplicationJob
def need_to_process_invoice?(invoice:, payable:)
logger.info "Checking if need to process e-invoice #{invoice}, payable: #{payable}"
unprocessable = invoice.do_not_send_e_invoice? && (invoice.monthly_invoice ? true : payable)
return false if invoice.blank?
return false if invoice.do_not_send_e_invoice? && payable
return false if unprocessable
true
end

View file

@ -0,0 +1,147 @@
class SendMonthlyInvoicesJob < ApplicationJob # rubocop:disable Metrics/ClassLength
queue_as :default
def perform(dry: false)
@dry = dry
@month = Time.zone.now - 1.month
@directo_client = new_directo_client
@min_directo_num = Setting.directo_monthly_number_min.presence.try(:to_i)
@max_directo_num = Setting.directo_monthly_number_max.presence.try(:to_i)
send_monthly_invoices
end
def new_directo_client
DirectoApi::Client.new(ENV['directo_invoice_url'], Setting.directo_sales_agent,
Setting.directo_receipt_payment_term)
end
# rubocop:disable Metrics/MethodLength
def send_monthly_invoices
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
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}")
@directo_client = new_directo_client
directo_invoices = @directo_client.invoices.add_with_schema(invoice: summary,
schema: 'summary')
next unless directo_invoices.size.positive?
directo_invoices.last.number = invoice.number
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_later(invoice_id, payable: false)
end
def create_invoice(summary, registrar)
invoice = registrar.init_monthly_invoice(normalize(summary))
invoice.number = assign_monthly_number
return unless invoice.save!
update_monthly_invoice_number(num: invoice.number)
invoice
end
def sync_with_directo
invoices_xml = @directo_client.invoices.as_xml
Rails.logger.info("[Directo] - attempting to send following XML:\n #{invoices_xml}")
res = @directo_client.invoices.deliver(ssl_verify: false)
process_directo_response(res.body, invoices_xml)
rescue SocketError, Errno::ECONNREFUSED, Timeout::Error, Errno::EINVAL, Errno::ECONNRESET,
EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError
Rails.logger.info('[Directo] Failed to communicate via API')
end
def assign_monthly_number
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?(1, last_directo_num)
last_directo_num + 1
end
def directo_counter_exceedable?(invoices_count, last_directo_num)
return true if @max_directo_num && @max_directo_num < (last_directo_num + invoices_count)
false
end
def process_directo_response(body, req)
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_to_directo(res: res, req: req, invoice: inv)
end
end
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
invoice.update(in_directo: true)
directo_record.save!
end
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
end
private
# rubocop:disable Metrics/MethodLength
def normalize(summary, lines: [])
sum = summary.dup
line_map = Hash.new 0
sum['invoice_lines'].each { |l| line_map[l] += 1 }
line_map.each_key do |count|
count['quantity'] = line_map[count] unless count['unit'].nil?
regex = /Domeenide ettemaks|Domains prepayment/
count['quantity'] = -1 if count['description'].match?(regex)
lines << count
end
sum['invoice_lines'] = summarize_lines(lines)
sum
end
# rubocop:enable Metrics/MethodLength
def summarize_lines(invoice_lines, lines: [])
line_map = Hash.new 0
invoice_lines.each do |l|
hash = l.with_indifferent_access.except(:start_date, :end_date)
line_map[hash] += 1
end
line_map.each_key do |count|
count['price'] = (line_map[count] * count['price'].to_f).round(3) unless count['price'].nil?
lines << count
end
lines
end
end

View file

@ -4,6 +4,7 @@ class InvoiceMailer < ApplicationMailer
subject = default_i18n_subject(invoice_number: invoice.number)
subject << I18n.t('invoice.already_paid') if paid
subject << I18n.t('invoice.monthly_invoice') if invoice.monthly_invoice
attachments["invoice-#{invoice.number}.pdf"] = invoice.as_pdf
mail(to: recipient, subject: subject)
end

View file

@ -1,22 +1,27 @@
module Registrar::BookKeeping
module Registrar::BookKeeping # rubocop:disable Metrics/ModuleLength
extend ActiveSupport::Concern
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?
invoice = {
'number': 1,
'customer': compose_directo_customer,
'number': 1, 'customer': compose_directo_customer,
'language': language == 'en' ? 'ENG' : '', 'currency': activities.first.currency,
'date': month.end_of_month.strftime('%Y-%m-%d')
}.as_json
invoice['invoice_lines'] = prepare_invoice_lines(month: month, activities: activities)
invoice
end
@ -55,20 +60,25 @@ module Registrar::BookKeeping
.where(activity_type: [AccountActivity::CREATE, AccountActivity::RENEW])
end
def monthly_invoice(month:)
invoices.where(monthly_invoice: true, issue_date: month.end_of_month.to_date,
cancelled_at: nil).first
end
def new_monthly_invoice_line(activity:, duration: nil)
price = load_price(activity)
line = {
'product_id': DOMAIN_TO_PRODUCT[price.zone_name.to_sym],
'quantity': 1,
'unit': language == 'en' ? 'pc' : 'tk',
}
}.with_indifferent_access
finalize_invoice_line(line, price: price, duration: duration, activity: activity)
end
def finalize_invoice_line(line, price:, activity:, duration:)
yearly = price.duration.in_years.to_i >= 1
line['price'] = yearly ? (price.price.amount / price.duration.in_years.to_i) : price.price.amount
line['price'] = yearly ? (price.price.amount / price.duration.in_years.to_i).to_f : price.price.amount.to_f
line['description'] = description_in_language(price: price, yearly: yearly)
add_product_timeframe(line: line, activity: activity, duration: duration) if duration.present? && (duration > 1)
@ -79,15 +89,16 @@ module Registrar::BookKeeping
def add_product_timeframe(line:, activity:, duration:)
create_time = activity.created_at
line['start_date'] = (create_time + (duration - 1).year).end_of_month.strftime('%Y-%m-%d')
line['end_date'] = (create_time + (duration - 1).year + 1).end_of_month.strftime('%Y-%m-%d')
line['end_date'] = (create_time + duration.year).end_of_month.strftime('%Y-%m-%d')
end
def description_in_language(price:, yearly:)
timeframe_string = yearly ? 'yearly' : 'monthly'
locale_string = "registrar.invoice_#{timeframe_string}_product_description"
length = yearly ? price.duration.in_years.to_i : price.duration.in_months.to_i
I18n.with_locale(language == 'en' ? 'en' : 'et') do
I18n.t(locale_string, tld: ".#{price.zone_name}", length: price.duration.in_years.to_i)
I18n.t(locale_string, tld: ".#{price.zone_name}", length: length)
end
end

View file

@ -32,11 +32,14 @@ class Invoice < ApplicationRecord
# rubocop:enable Layout/LineLength
# rubocop:enable Style/MultilineBlockLayout
validates :due_date, :currency, :seller_name,
:seller_iban, :buyer_name, :items, presence: true
:seller_iban, :buyer_name, presence: true
validates :items, presence: true, unless: -> { monthly_invoice }
before_create :set_invoice_number
before_create :calculate_total, unless: :total?
before_create :apply_default_buyer_vat_no, unless: :buyer_vat_no?
skip_callback :create, :before, :set_invoice_number, if: -> { monthly_invoice }
skip_callback :create, :before, :calculate_total, if: -> { monthly_invoice }
attribute :vat_rate, ::Type::VatRate.new
@ -118,7 +121,7 @@ class Invoice < ApplicationRecord
end
def subtotal
items.map(&:item_sum_without_vat).reduce(:+)
items.map(&:item_sum_without_vat).reduce(:+) || 0
end
def vat_amount
@ -131,7 +134,11 @@ class Invoice < ApplicationRecord
end
def each(&block)
items.each(&block)
if monthly_invoice
metadata['items'].map { |el| OpenStruct.new(el) }.each(&block)
else
items.each(&block)
end
end
def as_pdf

View file

@ -41,22 +41,16 @@ class Invoice
e_invoice_invoice_items = []
invoice.each do |invoice_item|
e_invoice_invoice_item = EInvoice::InvoiceItem.new.tap do |i|
i.description = invoice_item.description
i.price = invoice_item.price
i.quantity = invoice_item.quantity
i.unit = invoice_item.unit
i.subtotal = invoice_item.subtotal
i.vat_rate = invoice_item.vat_rate
i.vat_amount = invoice_item.vat_amount
i.total = invoice_item.total
end
e_invoice_invoice_item = generate_invoice_item(invoice, invoice_item)
e_invoice_invoice_items << e_invoice_invoice_item
end
e_invoice_name_item = e_invoice_invoice_items.shift if invoice.monthly_invoice
e_invoice_invoice = EInvoice::Invoice.new.tap do |i|
i.seller = seller
i.buyer = buyer
i.name = e_invoice_name_item&.description
i.items = e_invoice_invoice_items
i.number = invoice.number
i.date = invoice.issue_date
@ -72,9 +66,33 @@ class Invoice
i.currency = invoice.currency
i.delivery_channel = %i[internet_bank portal]
i.payable = payable
i.monthly_invoice = invoice.monthly_invoice
end
EInvoice::EInvoice.new(date: Time.zone.today, invoice: e_invoice_invoice)
end
private
def generate_invoice_item(invoice, item)
EInvoice::InvoiceItem.new.tap do |i|
i.description = item.description
i.unit = item.unit
i.price = item.price
i.quantity = item.quantity
if invoice.monthly_invoice && item.price && item.quantity
i.product_id = item.product_id
i.vat_rate = invoice.vat_rate
i.subtotal = (item.price * item.quantity).round(3)
i.vat_amount = i.subtotal * (i.vat_rate / 100)
i.total = i.subtotal + i.vat_amount
else
i.subtotal = item.subtotal
i.vat_rate = item.vat_rate
i.vat_amount = item.vat_amount
i.total = item.total
end
end
end
end
end

View file

@ -14,7 +14,8 @@ class Invoice
private
def invoice_html
ApplicationController.render(template: 'invoice/pdf', assigns: { invoice: invoice })
template = invoice.monthly_invoice ? 'invoice/monthly_pdf' : 'invoice/pdf'
ApplicationController.render(template: template, assigns: { invoice: invoice })
end
end
end

View file

@ -1,4 +1,4 @@
class Registrar < ApplicationRecord
class Registrar < ApplicationRecord # rubocop:disable Metrics/ClassLength
include Versions # version/registrar_version.rb
include Registrar::BookKeeping
include EmailVerifable
@ -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

View file

@ -4,11 +4,12 @@
= @invoice
.col-sm-8
%h1.text-right.text-center-xs
- if @invoice.unpaid?
= link_to(t(:payment_received), new_admin_bank_statement_path(invoice_id: @invoice.id), class: 'btn btn-default')
- unless @invoice.monthly_invoice
- if @invoice.unpaid?
= link_to(t(:payment_received), new_admin_bank_statement_path(invoice_id: @invoice.id), class: 'btn btn-default')
- if @invoice.paid? and !@invoice.cancelled?
= link_to(t(:cancel_payment), cancel_paid_admin_invoices_path(invoice_id: @invoice.id), method: 'post', data: { confirm: t(:are_you_sure) }, class: 'btn btn-warning')
- if @invoice.paid? && !@invoice.cancelled?
= link_to(t(:cancel_payment), cancel_paid_admin_invoices_path(invoice_id: @invoice.id), method: 'post', data: { confirm: t(:are_you_sure) }, class: 'btn btn-warning')
= link_to(t('.download_btn'), download_admin_invoice_path(@invoice), class: 'btn btn-default')
= link_to(t('.deliver_btn'), new_admin_invoice_delivery_path(@invoice), class: 'btn btn-default')
@ -24,6 +25,9 @@
.col-md-6= render 'registrar/invoices/partials/seller'
.col-md-6= render 'registrar/invoices/partials/buyer'
.row
.col-md-12= render 'registrar/invoices/partials/items'
- if @invoice.monthly_invoice
.col-md-12= render 'registrar/invoices/partials/monthly_invoice_items'
- else
.col-md-12= render 'registrar/invoices/partials/items'
.row
.col-md-12= render 'registrar/invoices/partials/payment_orders'

View file

@ -0,0 +1,277 @@
%html{lang: I18n.locale.to_s}
%head
%meta{charset: "utf-8"}
:css
.container {
margin: auto;
font-size: 12px;
}
.col-md-12 {
}
.col-md-6 {
width: 49%;
display: inline-block;
}
.col-xs-4 {
width: 33%;
}
.col-xs-2 {
width: 16%;
}
.col-md-3 {
width: 24%;
display: inline-block;
}
.left {
float: left;
}
.left {
padding-right: 5px;
}
.right {
float: right;
}
.text-right {
text-align: right;
}
dt {
float: left;
width: 100px;
clear: left;
text-align: right;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-weight: bold;
line-height: 1.42857;
}
dd {
margin-left: 120px;
line-height: 1.42857;
}
table {
width: 100%;
border-collapse: collapse;
font-size: 12px;
}
th {
text-align: left;
border: 0px;
border-top: 1px solid #DDD;
padding: 6px;
}
thead th {
border-bottom: 2px solid #DDD;
border-top: 0px;
}
td {
border-top: 1px solid #DDD;
}
td {
padding: 6px;
}
.no-border {
border: 0px;
}
hr {
height: 1px;
border: 0;
color: #DDD;
background-color: #DDD;
}
.clear {
clear: both;
}
.pull-down {
margin-top: 50px;
}
#header {
position: relative;
min-height: 100px;
}
img {
width: 106px;
height: 102px;
}
#header-content {
position: absolute;
bottom: 0;
}
#footer {
position: absolute;
bottom: 0px;
width: 99%;
}
h1 {
margin-bottom: 5px;
}
%body
.container
#header.row
.col-sm-6.left
#header-content
%h1
= @invoice
.col-sm-6.right
%img{src: "#{Rails.root}/public/eis-logo-black-et.png"}
.clear
%hr
.row
.col-md-6.left
%h4
Details
%hr
%dl.dl-horizontal
%dt= t(:issue_date)
%dd= l @invoice.issue_date
- if @invoice.cancelled?
%dt= Invoice.human_attribute_name :cancelled_at
%dd= l @invoice.cancelled_at
%dt= t(:due_date)
- if @invoice.cancelled?
%dd= t(:cancelled)
- else
%dd= l @invoice.due_date
%dt= t(:issuer)
%dd= @invoice.seller_contact_name
- if @invoice.description.present?
%dt= t(:description)
%dd=@invoice.description
%dt= Invoice.human_attribute_name :reference_no
%dd= @invoice.reference_no
.col-md-6.right
%h4= t(:client)
%hr
%dl.dl-horizontal
%dt= t(:name)
%dd= @invoice.buyer_name
%dt= t(:reg_no)
%dd= @invoice.buyer_reg_no
- if @invoice.buyer_address.present?
%dt= Invoice.human_attribute_name :address
%dd= @invoice.buyer_address
- if @invoice.buyer_country.present?
%dt= t(:country)
%dd= @invoice.buyer_country
- if @invoice.buyer_phone.present?
%dt= t(:phone)
%dd= @invoice.buyer_phone
- if @invoice.buyer_url.present?
%dt= t(:url)
%dd= @invoice.buyer_url
- if @invoice.buyer_email.present?
%dt= t(:email)
%dd= @invoice.buyer_email
.clear
.row.pull-down
.col-md-12
.table-responsive
%table.table.table-hover.table-condensed
%thead
%tr
%th{class: 'col-xs-1'}= t(:code)
%th{class: 'col-xs-1'}= InvoiceItem.human_attribute_name :quantity
%th{class: 'col-xs-1'}= t(:unit)
%th{class: 'col-xs-5'}= t(:description)
%th{class: 'col-xs-2'}= t(:price)
%th{class: 'col-xs-2'}= t(:total)
%tbody
- @invoice.each do |invoice_item|
%tr
%td= invoice_item.product_id
%td= invoice_item.quantity
%td= invoice_item.unit
%td= invoice_item.description
- if invoice_item.price && invoice_item.quantity
%td= currency(invoice_item.price)
%td= "#{currency((invoice_item.price * invoice_item.quantity).round(3))} #{@invoice.currency}"
- else
%td= ''
%td= ''
%tfoot
%tr
%th{colspan: 4}
%th= Invoice.human_attribute_name :subtotal
%td= number_to_currency(0)
%tr
%th.no-border{colspan: 4}
%th= "VAT #{number_to_percentage(@invoice.vat_rate, precision: 1)}"
%td= number_to_currency(0)
%tr
%th.no-border{colspan: 4}
%th= t(:total)
%td= number_to_currency(0)
#footer
%hr
.row
.col-md-3.left
= @invoice.seller_name
%br
= @invoice.seller_address
%br
= @invoice.seller_country
%br
= "#{t('reg_no')} #{@invoice.seller_reg_no}"
%br
= "#{Registrar.human_attribute_name :vat_no} #{@invoice.seller_vat_no}"
.col-md-3.left
= @invoice.seller_phone
%br
= @invoice.seller_email
%br
= @invoice.seller_url
.col-md-3.text-right.left
= t(:bank)
%br
= t(:iban)
%br
= t(:swift)
.col-md-3.left
= @invoice.seller_bank
%br
= @invoice.seller_iban
%br
= @invoice.seller_swift

View file

@ -234,7 +234,7 @@
%td= invoice_item.unit
%td= invoice_item.quantity
%td= currency(invoice_item.price)
%td= "#{currency(invoice_item.item_sum_without_vat)} #{@invoice.currency}"
%td= "#{currency(invoice_item.item_sum_without_vat)} #{@invoice.currency}"
%tfoot
%tr
%th{colspan: 3}

View file

@ -0,0 +1,38 @@
%h4= t(:items)
%hr
.table-responsive
%table.table.table-hover.table-condensed
%thead
%tr
%th{class: 'col-xs-1'}= t(:code)
%th{class: 'col-xs-1'}= InvoiceItem.human_attribute_name :quantity
%th{class: 'col-xs-1'}= t(:unit)
%th{class: 'col-xs-5'}= t(:description)
%th{class: 'col-xs-2'}= t(:price)
%th{class: 'col-xs-2'}= t(:total)
%tbody
- @invoice.each do |invoice_item|
%tr
%td= invoice_item.product_id
%td= invoice_item.quantity
%td= invoice_item.unit
%td= invoice_item.description
- if invoice_item.price && invoice_item.quantity
%td= currency(invoice_item.price)
%td= "#{currency((invoice_item.price * invoice_item.quantity).round(3))} #{@invoice.currency}"
- else
%td= ''
%td= ''
%tfoot
%tr
%th{colspan: 4}
%th= Invoice.human_attribute_name :subtotal
%td= number_to_currency(0)
%tr
%th.no-border{colspan: 4}
%th= "VAT #{number_to_percentage(@invoice.vat_rate, precision: 1)}"
%td= number_to_currency(0)
%tr
%th.no-border{colspan: 4}
%th= t(:total)
%td= number_to_currency(0)

View file

@ -13,7 +13,10 @@
.col-md-6= render 'registrar/invoices/partials/seller'
.col-md-6= render 'registrar/invoices/partials/buyer'
.row
.col-md-12= render 'registrar/invoices/partials/items'
- if @invoice.monthly_invoice
.col-md-12= render 'registrar/invoices/partials/monthly_invoice_items'
- else
.col-md-12= render 'registrar/invoices/partials/items'
- if @invoice.payable?
.row.semifooter