mirror of
https://github.com/internetee/registry.git
synced 2025-05-17 17:59:47 +02:00
parent
885f28e34e
commit
fd1f475925
2 changed files with 48 additions and 54 deletions
|
@ -52,24 +52,16 @@ module Billing
|
||||||
"#{operation_category} #{zone_name}"
|
"#{operation_category} #{zone_name}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def years_amount
|
|
||||||
duration.to_i
|
|
||||||
end
|
|
||||||
|
|
||||||
def price_decimal
|
|
||||||
price_cents / BigDecimal.new('100')
|
|
||||||
end
|
|
||||||
|
|
||||||
def zone_name
|
def zone_name
|
||||||
zone.origin
|
zone.origin
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def to_partial_path
|
def to_partial_path
|
||||||
'price'
|
'price'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
def init_values
|
def init_values
|
||||||
return unless new_record?
|
return unless new_record?
|
||||||
self.valid_from = Time.zone.now.beginning_of_year unless valid_from
|
self.valid_from = Time.zone.now.beginning_of_year unless valid_from
|
||||||
|
|
|
@ -61,7 +61,6 @@ class Directo < ActiveRecord::Base
|
||||||
|
|
||||||
|
|
||||||
def self.send_monthly_invoices(debug: false)
|
def self.send_monthly_invoices(debug: false)
|
||||||
@debug = debug
|
|
||||||
I18n.locale = :et
|
I18n.locale = :et
|
||||||
month = Time.now - 1.month
|
month = Time.now - 1.month
|
||||||
invoices_until = month.end_of_month
|
invoices_until = month.end_of_month
|
||||||
|
@ -76,51 +75,61 @@ class Directo < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
directo_next = last_directo
|
directo_next = last_directo
|
||||||
Registrar.where.not(test_registrar: true).find_each do |registrar|
|
|
||||||
unless registrar.cash_account
|
Registrar.where(test_registrar: false).each do |registrar|
|
||||||
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.where(account_id: registrar.account_ids).where("created_at BETWEEN ? AND ?",month.beginning_of_month, month.end_of_month)
|
registrar_activities = AccountActivity
|
||||||
|
.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.where(activity_type: [AccountActivity::CREATE, AccountActivity::RENEW]).each do |activity|
|
registrar_activities.each do |account_activity|
|
||||||
pricelist = load_activity_pricelist(activity)
|
price = load_price(account_activity)
|
||||||
unless pricelist
|
quantity = price.account_activities
|
||||||
Rails.logger.error("[DIRECTO] Skipping activity #{activity.id} as pricelist not found")
|
.where(account_id: registrar.account_ids)
|
||||||
next
|
.where(created_at: month.beginning_of_month..month.end_of_month)
|
||||||
end
|
.where(activity_type: [AccountActivity::CREATE, AccountActivity::RENEW])
|
||||||
|
.count
|
||||||
|
|
||||||
pricelist.years_amount.times do |i|
|
if price.duration.include?('years')
|
||||||
year = i+1
|
line_count = price.duration.to_i
|
||||||
hash = {
|
|
||||||
"ProductID" => DOMAIN_TO_PRODUCT[pricelist.category],
|
|
||||||
"Unit" => "tk",
|
|
||||||
"ProductName" => ".#{pricelist.category} registreerimine: #{pricelist.years_amount} aasta",
|
|
||||||
"UnitPriceWoVAT" => pricelist.price_decimal/pricelist.years_amount
|
|
||||||
}
|
|
||||||
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
|
else
|
||||||
items[hash] = {"RN"=>counter.next, "RR" => counter.now - i, "Quantity"=> 1}
|
line_count = 1
|
||||||
|
end
|
||||||
|
|
||||||
|
localized_duration = price.duration
|
||||||
|
localized_duration.sub!(/mons/, 'kuud')
|
||||||
|
localized_duration.sub!(/years/, 'aastat')
|
||||||
|
localized_duration.sub!(/year/, 'aasta')
|
||||||
|
|
||||||
|
line_count.times do |i|
|
||||||
|
year = i + 1
|
||||||
|
hash = {
|
||||||
|
"ProductID" => DOMAIN_TO_PRODUCT[price.zone_name],
|
||||||
|
"Unit" => "tk",
|
||||||
|
"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 prepaiments
|
# Adding prepayments
|
||||||
if items.any?
|
if items.any?
|
||||||
total = 0
|
total = Money.new(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
|
||||||
|
@ -136,6 +145,7 @@ 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
|
||||||
}
|
}
|
||||||
|
@ -143,10 +153,11 @@ class Directo < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
data = builder.to_xml.gsub("\n",'')
|
data = builder.to_xml.gsub("\n",'')
|
||||||
response = RestClient::Request.execute(url: ENV['directo_invoice_url'], method: :post, payload: {put: "1", what: "invoice", xmldata: data}, verify_ssl: false).to_s
|
|
||||||
if @debug
|
if debug
|
||||||
STDOUT << "#{Time.zone.now.utc} - Directo xml had to be sent #{data}\n"
|
STDOUT << "#{Time.zone.now.utc} - Directo xml had to be sent #{data}\n"
|
||||||
else
|
else
|
||||||
|
response = RestClient::Request.execute(url: ENV['directo_invoice_url'], method: :post, payload: {put: "1", what: "invoice", xmldata: data}, verify_ssl: false).to_s
|
||||||
Setting.directo_monthly_number_last = directo_next
|
Setting.directo_monthly_number_last = directo_next
|
||||||
Nokogiri::XML(response).css("Result").each do |res|
|
Nokogiri::XML(response).css("Result").each do |res|
|
||||||
Directo.create!(request: data, response: res.as_json.to_h, invoice_number: directo_next)
|
Directo.create!(request: data, response: res.as_json.to_h, invoice_number: directo_next)
|
||||||
|
@ -161,19 +172,10 @@ class Directo < ActiveRecord::Base
|
||||||
STDOUT << "#{Time.zone.now.utc} - Directo invoices sending finished. #{invoice_counter.now} are sent\n"
|
STDOUT << "#{Time.zone.now.utc} - Directo invoices sending finished. #{invoice_counter.now} are sent\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.load_price(account_activity)
|
||||||
def self.load_activity_pricelist activity
|
|
||||||
@pricelists ||= {}
|
@pricelists ||= {}
|
||||||
return @pricelists[activity.log_pricelist_id] if @pricelists.has_key?(activity.log_pricelist_id)
|
return @pricelists[account_activity.price_id] if @pricelists.has_key?(account_activity.price_id)
|
||||||
|
@pricelists[account_activity.price_id] = account_activity.price
|
||||||
pricelist = Pricelist.find_by(id: activity.log_pricelist_id) || PricelistVersion.find_by(item_id: activity.log_pricelist_id).try(:reify)
|
|
||||||
unless pricelist
|
|
||||||
@pricelists[activity.log_pricelist_id] = nil
|
|
||||||
Rails.logger.info("[DIRECTO] AccountActivity #{activity.id} cannot be sent as pricelist wasn't found #{activity.log_pricelist_id}")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
@pricelists[activity.log_pricelist_id] = pricelist.version_at(activity.created_at) || pricelist
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue