From 80c4057b8fcf2b2cc8fa315d7b681b5c0c22d0ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergei=20Ts=C3=B5ganov?= Date: Wed, 31 Aug 2022 14:22:01 +0300 Subject: [PATCH] Corrected monthly invoices for sending to omniva --- app/jobs/send_monthly_invoices_job.rb | 36 ++++++++++++++++++- app/models/concerns/registrar/book_keeping.rb | 7 ++-- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/app/jobs/send_monthly_invoices_job.rb b/app/jobs/send_monthly_invoices_job.rb index 647bd0752..00a003444 100644 --- a/app/jobs/send_monthly_invoices_job.rb +++ b/app/jobs/send_monthly_invoices_job.rb @@ -39,6 +39,7 @@ class SendMonthlyInvoicesJob < ApplicationJob sync_with_directo end end + # rubocop:enable Metrics/MethodLength def send_email_to_registrar(invoice:, registrar:) @@ -52,7 +53,7 @@ class SendMonthlyInvoicesJob < ApplicationJob end def create_invoice(summary, registrar) - invoice = registrar.init_monthly_invoice(summary) + invoice = registrar.init_monthly_invoice(normalize(summary)) invoice.number = assign_monthly_number return unless invoice.save! @@ -108,4 +109,37 @@ class SendMonthlyInvoicesJob < ApplicationJob Setting.directo_monthly_number_last = num.to_i end + + private + + 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 + + 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 diff --git a/app/models/concerns/registrar/book_keeping.rb b/app/models/concerns/registrar/book_keeping.rb index 13574f3c2..c74ae3987 100644 --- a/app/models/concerns/registrar/book_keeping.rb +++ b/app/models/concerns/registrar/book_keeping.rb @@ -17,14 +17,11 @@ module Registrar::BookKeeping 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 @@ -92,7 +89,7 @@ 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:)