Reduce condition sizes for Directo job

This commit is contained in:
Karl Erik Õunapuu 2020-02-27 17:06:25 +02:00
parent 6302462184
commit 6c328438e6
4 changed files with 37 additions and 22 deletions

View file

@ -12,7 +12,7 @@ module BookKeeping
'number': 1,
'customer_code': accounting_customer_code,
'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
inv['invoice_lines'] = prepare_invoice_lines(month: month, activities: activities)
@ -63,16 +63,22 @@ module BookKeeping
def new_monthly_invoice_line(activity:, duration: nil)
price = load_price(activity)
yearly = price.duration.include?('year')
line = {
'product_id': DOMAIN_TO_PRODUCT[price.zone_name.to_sym],
'quantity': 1,
'price': yearly ? (price.price.amount / price.duration.to_i) : price.price.amount,
'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)
if yearly && duration
if duration.present?
add_product_timeframe(line: line, activity: activity, duration: duration) if duration > 1
end

View file

@ -27,18 +27,23 @@ class DirectoInvoiceForwardJob < Que::Job
end
def send_monthly_invoices
Registrar.where(test_registrar: false).find_each do |registrar|
next unless registrar.cash_account
invoice = registrar.monthly_summary(month: @month)
@client.invoices.add_with_schema(invoice: invoice, schema: 'summary') unless invoice.nil?
Registrar.where.not(test_registrar: true).find_each do |registrar|
fetch_monthly_summary(registrar: registrar)
end
assign_montly_numbers
return unless @client.invoices.count.positive?
sync_with_directo
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)
raise 'Directo Counter is going to be out of period!'
end
@ -66,9 +71,9 @@ class DirectoInvoiceForwardJob < Que::Job
def sync_with_directo
Rails.logger.info("[Directo] - attempting to send following XML:\n #{@client.invoices.as_xml}")
return if @dry
assign_monthly_numbers if @month
res = @client.invoices.deliver(ssl_verify: false)
update_invoice_directo_state(res.body, @client.invoices.as_xml) if res.code == '200'
rescue SocketError, Errno::ECONNREFUSED, Timeout::Error, Errno::EINVAL, Errno::ECONNRESET,

View file

@ -70,7 +70,7 @@ class Invoice < ApplicationRecord
Country.new(buyer_country_code)
end
# order is used for directo/banklink description
# order is used for directo/banklink description
def order
"Order nr. #{number}"
end
@ -108,17 +108,18 @@ class Invoice < ApplicationRecord
inv['issue_date'] = issue_date.strftime('%Y-%m-%d')
inv['transaction_date'] = account_activity.bank_transaction&.paid_at&.strftime('%Y-%m-%d')
inv['language'] = buyer.language
inv['invoice_lines'] = [{
'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['invoice_lines'] = compose_directo_product
inv
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?
e_invoice_sent? || cancelled? || paid?
end

View file

@ -37,8 +37,11 @@ class DirectoInvoiceForwardJobTest < ActiveSupport::TestCase
end
def test_fails_if_directo_bounds_exceedable
Setting.clear_cache
Setting.directo_monthly_number_max = 30990
activity = account_activities(:one)
price = billing_prices(:create_one_year)
activity.update!(activity_type: 'create', price: price)
Setting.directo_monthly_number_max = 30991
assert_raises 'RuntimeError' do
DirectoInvoiceForwardJob.run(monthly: true, dry: false)