Fix directo

#475
This commit is contained in:
Artur Beljajev 2017-06-12 03:11:31 +03:00
parent c6e286662b
commit b1bf725e27

View file

@ -75,61 +75,72 @@ class Directo < ActiveRecord::Base
end end
directo_next = last_directo directo_next = last_directo
Registrar.where.not(test_registrar: true).find_each do |registrar|
Registrar.where(test_registrar: false).each do |registrar| unless registrar.cash_account
Rails.logger.info("[DIRECTO] Monthly invoice for registrar #{registrar.id} has been skipped as it doesn't has cash_account")
next
end
counter = Counter.new(1) counter = Counter.new(1)
items = {} items = {}
registrar_activities = AccountActivity registrar_activities = AccountActivity.where(account_id: registrar.account_ids).where("created_at BETWEEN ? AND ?",month.beginning_of_month, month.end_of_month)
.where(account_id: registrar.account_ids)
.where(created_at: month.beginning_of_month..month.end_of_month)
.where(activity_type: [AccountActivity::CREATE, AccountActivity::RENEW])
# Adding domains items # adding domains items
registrar_activities.each do |account_activity| registrar_activities.where(activity_type: [AccountActivity::CREATE, AccountActivity::RENEW]).each do |activity|
price = load_price(account_activity) price = load_price(activity)
quantity = price.account_activities
.where(account_id: registrar.account_ids)
.where(created_at: month.beginning_of_month..month.end_of_month)
.where(activity_type: [AccountActivity::CREATE, AccountActivity::RENEW])
.count
if price.duration.include?('years') if price.duration.include?('year')
line_count = price.duration.to_i price.duration.to_i.times do |i|
year = i+1
hash = {
"ProductID" => DOMAIN_TO_PRODUCT[price.zone_name],
"Unit" => "tk",
"ProductName" => ".#{price.zone_name} registreerimine: #{price.duration.to_i} aasta",
"UnitPriceWoVAT" => price.price.amount / price.duration.to_i
}
hash["StartDate"] = (activity.created_at + (year-1).year).end_of_month.strftime(date_format) if year > 1
hash["EndDate"] = (activity.created_at + (year-1).year + 1).end_of_month.strftime(date_format) if year > 1
if items.has_key?(hash)
items[hash]["Quantity"] += 1
else
items[hash] = { "RN" => counter.next, "RR" => counter.now - i, "Quantity" => 1 }
end
end
else else
line_count = 1 1.times do |i|
end quantity = price.account_activities
.where(account_id: registrar.account_ids)
.where(created_at: month.beginning_of_month..month.end_of_month)
.where(activity_type: [AccountActivity::CREATE, AccountActivity::RENEW])
.count
localized_duration = price.duration hash = {
localized_duration.sub!(/mons/, 'kuud') "ProductID" => DOMAIN_TO_PRODUCT[price.zone_name],
localized_duration.sub!(/years/, 'aastat') "Unit" => "tk",
localized_duration.sub!(/year/, 'aasta') "ProductName" => ".#{price.zone_name} registreerimine: #{price.duration.to_i} kuud",
"UnitPriceWoVAT" => price.price.amount,
}
line_count.times do |i| if items.has_key?(hash)
year = i + 1 #items[hash]["Quantity"] += 1
hash = { else
"ProductID" => DOMAIN_TO_PRODUCT[price.zone_name], items[hash] = { "RN" => counter.next, "RR" => counter.now - i, "Quantity" => quantity }
"Unit" => "tk", end
"ProductName" => ".#{price.zone_name} registreerimine: #{price.duration}",
"UnitPriceWoVAT" => price.price,
}
hash["StartDate"] = (account_activity.created_at + (year-1).year).end_of_month.strftime(date_format) if year > 1
hash["EndDate"] = (account_activity.created_at + (year-1).year + 1).end_of_month.strftime(date_format) if year > 1
unless items.has_key?(hash)
items[hash] = {"RN"=>counter.next, "RR" => counter.now - i, "Quantity"=> quantity}
end end
end end
end end
# Adding prepayments #adding prepaiments
if items.any? if items.any?
total = Money.new(0) total = 0
items.each{ |key, val| total += val["Quantity"] * key["UnitPriceWoVAT"] } items.each{ |key, val| total += val["Quantity"] * key["UnitPriceWoVAT"] }
hash = {"ProductID" => Setting.directo_receipt_product_name, "Unit" => "tk", "ProductName" => "Domeenide ettemaks", "UnitPriceWoVAT"=>total} hash = {"ProductID" => Setting.directo_receipt_product_name, "Unit" => "tk", "ProductName" => "Domeenide ettemaks", "UnitPriceWoVAT"=>total}
items[hash] = {"RN"=>counter.next, "RR" => counter.now, "Quantity"=> -1} items[hash] = {"RN"=>counter.next, "RR" => counter.now, "Quantity"=> -1}
end end
# Generating XML # generating XML
if items.any? if items.any?
directo_next += 1 directo_next += 1
invoice_counter.next invoice_counter.next
@ -145,7 +156,6 @@ class Directo < ActiveRecord::Base
"SalesAgent" =>Setting.directo_sales_agent){ "SalesAgent" =>Setting.directo_sales_agent){
xml.line("RN" => 1, "RR"=>1, "ProductName"=> "Domeenide registreerimine - #{I18n.l(invoices_until, format: "%B %Y").titleize}") xml.line("RN" => 1, "RR"=>1, "ProductName"=> "Domeenide registreerimine - #{I18n.l(invoices_until, format: "%B %Y").titleize}")
items.each do |line, val| items.each do |line, val|
line['UnitPriceWoVAT'] = line['UnitPriceWoVAT'].amount.to_s
xml.line(val.merge(line)) xml.line(val.merge(line))
end end
} }