mirror of
https://github.com/internetee/registry.git
synced 2025-08-05 17:28:18 +02:00
Story#105855968 - Prepare xml for Directo
This commit is contained in:
parent
ca3e28f841
commit
e954c212c7
7 changed files with 96 additions and 21 deletions
|
@ -10,6 +10,10 @@ class Counter
|
|||
@value.to_s
|
||||
end
|
||||
|
||||
def now
|
||||
@value
|
||||
end
|
||||
|
||||
# pre-increment ".+" when x not present
|
||||
def next(x = 1)
|
||||
@value += x
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
class Directo < ActiveRecord::Base
|
||||
DOMAIN_TO_PRODUCT = {"ee" => "01EE", "com.ee" => "02COM", "pri.ee" => "03PRI", "fie.ee"=>"04FIE", "med.ee" => "05MED"}.freeze
|
||||
belongs_to :item, polymorphic: true
|
||||
|
||||
def self.send_receipts
|
||||
|
@ -49,36 +50,84 @@ class Directo < ActiveRecord::Base
|
|||
|
||||
|
||||
def self.send_monthly_invoices
|
||||
product_ids = {"ee" => "01EE", "com.ee" => "02COM", "pri.ee" => "03PRI", "fie.ee"=>"04FIE", "med.ee" => "05MED"}
|
||||
I18n.locale = :et
|
||||
month = Time.now - 1.month
|
||||
invoices_until = month.end_of_month
|
||||
# pochemu registrar has_many :accounts
|
||||
date_format = "%Y-%m-%d"
|
||||
|
||||
activity_scope = AccountActivity.where(activity_type: [CREATE, RENEW])
|
||||
Registrar.joins(:account).find_each do |registrar|
|
||||
Registrar.find_each do |registrar|
|
||||
next unless registrar.cash_account
|
||||
counter = Counter.new
|
||||
builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
|
||||
activity_scope.where(account_id: registrar.cash_account)
|
||||
counter = Counter.new(1)
|
||||
items = {}
|
||||
registrar_activities = AccountActivity.where(account_id: registrar.account_ids).where("created_at BETWEEN ? AND ?",month.beginning_of_month, month.end_of_month)
|
||||
|
||||
xml.invoices{
|
||||
xml.invoice("Number"=>"13980",
|
||||
"InvoiceDate"=>invoices_until.strftime("%Y-%m-%d"),
|
||||
"PaymentTerm"=>"E",
|
||||
"CustomerCode"=>registrar.directo_handle,
|
||||
"Language"=>"",
|
||||
"Currency"=>"EUR",
|
||||
"SalesAgent"=>Setting.directo_sales_agent){
|
||||
xml.line("RN" => counter.next, "RR"=>1, "ProductName"=> "Domeenide registreerimine - Juuli 2015")
|
||||
activity_scope.where(account_id: registrar.account_ids).each do |activity|
|
||||
xml.line("RN"=>counter.next, "RR"=>"2", "ProductID"=>"01EE", "Quantity"=>"1911", "Unit"=>"tk", "ProductName"=>".ee registreerimine: 1 aasta", "UnitPriceWoVAT"=>"9.00")
|
||||
end
|
||||
# adding domains items
|
||||
registrar_activities.where(activity_type: [AccountActivity::CREATE, AccountActivity::RENEW]).each do |activity|
|
||||
pricelist = load_activity_pricelist(activity)
|
||||
next unless pricelist
|
||||
|
||||
pricelist.years_amount.times do |i|
|
||||
year = i+1
|
||||
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.year).strftime(date_format) if year > 1
|
||||
hash["EndDate"] = (activity.created_at + year.year + 1).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
|
||||
end
|
||||
|
||||
#adding prepaiments
|
||||
registrar_activities.where(activity_type: [AccountActivity::ADD_CREDIT]).each do |activity|
|
||||
hash = {"ProductID" => Setting.directo_receipt_product_name, "Unit" => "tk", "ProductName" => "Domeenide ettemaks", "UnitPriceWoVAT"=>activity.sum}
|
||||
items[hash] = {"RN"=>counter.next, "RR" => counter.now, "Quantity"=> -1}
|
||||
end
|
||||
|
||||
# generating XML
|
||||
if items.any?
|
||||
builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
|
||||
xml.invoices{
|
||||
xml.invoice("Number" =>"13980",
|
||||
"InvoiceDate" =>invoices_until.strftime(date_format),
|
||||
"PaymentTerm" =>"E",
|
||||
"CustomerCode"=>registrar.directo_handle,
|
||||
"Language" =>"",
|
||||
"Currency" =>registrar_activities.first.currency,
|
||||
"SalesAgent" =>Setting.directo_sales_agent){
|
||||
xml.line("RN" => 1, "RR"=>1, "ProductName"=> "Domeenide registreerimine - #{I18n.l(invoices_until, format: "%B %Y").titleize}")
|
||||
items.each do |line, val|
|
||||
xml.line(val.merge(line))
|
||||
end
|
||||
}
|
||||
}
|
||||
end
|
||||
puts builder.to_xml
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
def self.load_activity_pricelist activity
|
||||
@pricelists ||= {}
|
||||
return @pricelists[activity.log_pricelist_id] if @pricelists.has_key?(activity.log_pricelist_id)
|
||||
|
||||
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
|
||||
|
||||
=begin
|
||||
|
|
|
@ -452,6 +452,7 @@ class Domain < ActiveRecord::Base
|
|||
period_i ||= period
|
||||
unit ||= period_unit
|
||||
|
||||
# TODO: test if name.scan(/\.(.+)\z/).first.first is faster
|
||||
zone = name.split('.').drop(1).join('.')
|
||||
|
||||
p = period_i / 365 if unit == 'd'
|
||||
|
|
|
@ -8,6 +8,8 @@ class Pricelist < ActiveRecord::Base
|
|||
)
|
||||
}
|
||||
|
||||
scope :valid_at, ->(time){ where("valid_from IS NULL OR valid_from <= ?", time).where("valid_to IS NULL OR valid_to >= ?", time) }
|
||||
|
||||
monetize :price_cents
|
||||
|
||||
validates :price_cents, :price_currency, :price,
|
||||
|
@ -27,6 +29,14 @@ class Pricelist < ActiveRecord::Base
|
|||
"#{operation_category} #{category}"
|
||||
end
|
||||
|
||||
def years_amount
|
||||
duration.to_i
|
||||
end
|
||||
|
||||
def price_decimal
|
||||
price_cents / BigDecimal.new('100')
|
||||
end
|
||||
|
||||
class << self
|
||||
def pricelist_for(zone, operation, period)
|
||||
lists = valid.where(category: zone, operation_category: operation, duration: period)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue