mirror of
https://github.com/internetee/registry.git
synced 2025-07-21 10:16:01 +02:00
Reduce condition sizes for Directo job
This commit is contained in:
parent
6302462184
commit
6c328438e6
4 changed files with 37 additions and 22 deletions
|
@ -12,7 +12,7 @@ module BookKeeping
|
||||||
'number': 1,
|
'number': 1,
|
||||||
'customer_code': accounting_customer_code,
|
'customer_code': accounting_customer_code,
|
||||||
'language': language, 'currency': activities.first.currency,
|
'language': language, 'currency': activities.first.currency,
|
||||||
'date': month.end_of_month.strftime('%Y-%m-%d'),
|
'date': month.end_of_month.strftime('%Y-%m-%d')
|
||||||
}.as_json
|
}.as_json
|
||||||
|
|
||||||
inv['invoice_lines'] = prepare_invoice_lines(month: month, activities: activities)
|
inv['invoice_lines'] = prepare_invoice_lines(month: month, activities: activities)
|
||||||
|
@ -63,16 +63,22 @@ module BookKeeping
|
||||||
|
|
||||||
def new_monthly_invoice_line(activity:, duration: nil)
|
def new_monthly_invoice_line(activity:, duration: nil)
|
||||||
price = load_price(activity)
|
price = load_price(activity)
|
||||||
yearly = price.duration.include?('year')
|
|
||||||
line = {
|
line = {
|
||||||
'product_id': DOMAIN_TO_PRODUCT[price.zone_name.to_sym],
|
'product_id': DOMAIN_TO_PRODUCT[price.zone_name.to_sym],
|
||||||
'quantity': 1,
|
'quantity': 1,
|
||||||
'price': yearly ? (price.price.amount / price.duration.to_i) : price.price.amount,
|
|
||||||
'unit': language == 'en' ? 'pc' : 'tk',
|
'unit': language == 'en' ? 'pc' : 'tk',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
finalize_invoice_line(line, price: price, duration: duration, activity: activity)
|
||||||
|
end
|
||||||
|
|
||||||
|
def finalize_invoice_line(line, price:, activity:, duration:)
|
||||||
|
yearly = price.duration.include?('year')
|
||||||
|
|
||||||
|
line['price'] = yearly ? (price.price.amount / price.duration.to_i) : price.price.amount
|
||||||
line['description'] = description_in_language(price: price, yearly: yearly)
|
line['description'] = description_in_language(price: price, yearly: yearly)
|
||||||
if yearly && duration
|
|
||||||
|
if duration.present?
|
||||||
add_product_timeframe(line: line, activity: activity, duration: duration) if duration > 1
|
add_product_timeframe(line: line, activity: activity, duration: duration) if duration > 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -27,18 +27,23 @@ class DirectoInvoiceForwardJob < Que::Job
|
||||||
end
|
end
|
||||||
|
|
||||||
def send_monthly_invoices
|
def send_monthly_invoices
|
||||||
Registrar.where(test_registrar: false).find_each do |registrar|
|
Registrar.where.not(test_registrar: true).find_each do |registrar|
|
||||||
next unless registrar.cash_account
|
fetch_monthly_summary(registrar: registrar)
|
||||||
|
|
||||||
invoice = registrar.monthly_summary(month: @month)
|
|
||||||
@client.invoices.add_with_schema(invoice: invoice, schema: 'summary') unless invoice.nil?
|
|
||||||
end
|
end
|
||||||
|
|
||||||
assign_montly_numbers
|
return unless @client.invoices.count.positive?
|
||||||
|
|
||||||
sync_with_directo
|
sync_with_directo
|
||||||
end
|
end
|
||||||
|
|
||||||
def assign_montly_numbers
|
def fetch_monthly_summary(registrar:)
|
||||||
|
return unless registrar.cash_account
|
||||||
|
|
||||||
|
summary = registrar.monthly_summary(month: @month)
|
||||||
|
@client.invoices.add_with_schema(invoice: summary, schema: 'summary') unless summary.nil?
|
||||||
|
end
|
||||||
|
|
||||||
|
def assign_monthly_numbers
|
||||||
if directo_counter_exceedable?(@client.invoices.count)
|
if directo_counter_exceedable?(@client.invoices.count)
|
||||||
raise 'Directo Counter is going to be out of period!'
|
raise 'Directo Counter is going to be out of period!'
|
||||||
end
|
end
|
||||||
|
@ -66,9 +71,9 @@ class DirectoInvoiceForwardJob < Que::Job
|
||||||
|
|
||||||
def sync_with_directo
|
def sync_with_directo
|
||||||
Rails.logger.info("[Directo] - attempting to send following XML:\n #{@client.invoices.as_xml}")
|
Rails.logger.info("[Directo] - attempting to send following XML:\n #{@client.invoices.as_xml}")
|
||||||
|
|
||||||
return if @dry
|
return if @dry
|
||||||
|
|
||||||
|
assign_monthly_numbers if @month
|
||||||
res = @client.invoices.deliver(ssl_verify: false)
|
res = @client.invoices.deliver(ssl_verify: false)
|
||||||
update_invoice_directo_state(res.body, @client.invoices.as_xml) if res.code == '200'
|
update_invoice_directo_state(res.body, @client.invoices.as_xml) if res.code == '200'
|
||||||
rescue SocketError, Errno::ECONNREFUSED, Timeout::Error, Errno::EINVAL, Errno::ECONNRESET,
|
rescue SocketError, Errno::ECONNREFUSED, Timeout::Error, Errno::EINVAL, Errno::ECONNRESET,
|
||||||
|
|
|
@ -70,7 +70,7 @@ class Invoice < ApplicationRecord
|
||||||
Country.new(buyer_country_code)
|
Country.new(buyer_country_code)
|
||||||
end
|
end
|
||||||
|
|
||||||
# order is used for directo/banklink description
|
# order is used for directo/banklink description
|
||||||
def order
|
def order
|
||||||
"Order nr. #{number}"
|
"Order nr. #{number}"
|
||||||
end
|
end
|
||||||
|
@ -108,17 +108,18 @@ class Invoice < ApplicationRecord
|
||||||
inv['issue_date'] = issue_date.strftime('%Y-%m-%d')
|
inv['issue_date'] = issue_date.strftime('%Y-%m-%d')
|
||||||
inv['transaction_date'] = account_activity.bank_transaction&.paid_at&.strftime('%Y-%m-%d')
|
inv['transaction_date'] = account_activity.bank_transaction&.paid_at&.strftime('%Y-%m-%d')
|
||||||
inv['language'] = buyer.language
|
inv['language'] = buyer.language
|
||||||
inv['invoice_lines'] = [{
|
inv['invoice_lines'] = compose_directo_product
|
||||||
'product_id': Setting.directo_receipt_product_name,
|
|
||||||
'description': order,
|
|
||||||
'quantity': 1,
|
|
||||||
'price': ActionController::Base.helpers
|
|
||||||
.number_with_precision(subtotal, precision: 2, separator: '.'),
|
|
||||||
}].as_json
|
|
||||||
|
|
||||||
inv
|
inv
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def compose_directo_product
|
||||||
|
[{ 'product_id': Setting.directo_receipt_product_name, 'description': order,
|
||||||
|
'quantity': 1, 'price': ActionController::Base.helpers.number_with_precision(
|
||||||
|
subtotal, precision: 2, separator: '.'
|
||||||
|
) }].as_json
|
||||||
|
end
|
||||||
|
|
||||||
def do_not_send_e_invoice?
|
def do_not_send_e_invoice?
|
||||||
e_invoice_sent? || cancelled? || paid?
|
e_invoice_sent? || cancelled? || paid?
|
||||||
end
|
end
|
||||||
|
|
|
@ -37,8 +37,11 @@ class DirectoInvoiceForwardJobTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_fails_if_directo_bounds_exceedable
|
def test_fails_if_directo_bounds_exceedable
|
||||||
Setting.clear_cache
|
activity = account_activities(:one)
|
||||||
Setting.directo_monthly_number_max = 30990
|
price = billing_prices(:create_one_year)
|
||||||
|
activity.update!(activity_type: 'create', price: price)
|
||||||
|
|
||||||
|
Setting.directo_monthly_number_max = 30991
|
||||||
|
|
||||||
assert_raises 'RuntimeError' do
|
assert_raises 'RuntimeError' do
|
||||||
DirectoInvoiceForwardJob.run(monthly: true, dry: false)
|
DirectoInvoiceForwardJob.run(monthly: true, dry: false)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue