diff --git a/app/models/counter.rb b/app/models/counter.rb new file mode 100644 index 000000000..01bec95e6 --- /dev/null +++ b/app/models/counter.rb @@ -0,0 +1,20 @@ +class Counter + def initialize value = 0 + @value = value + end + attr_accessor :value + def method_missing *args, &blk + @value.send(*args, &blk) + end + def to_s + @value.to_s + end + + # pre-increment ".+" when x not present + def next(x = 1) + @value += x + end + def prev(x = 1) + @value -= x + end +end \ No newline at end of file diff --git a/app/models/directo.rb b/app/models/directo.rb index 5a719d0a7..5cd480bf5 100644 --- a/app/models/directo.rb +++ b/app/models/directo.rb @@ -46,4 +46,62 @@ class Directo < ActiveRecord::Base Rails.logger.info("[DIRECTO] Invoice #{res.attributes["docid"].value} was pushed and return is #{res.as_json.to_h.inspect}") end end + + + def self.send_monthly_invoices + product_ids = {"ee" => "01EE", "com.ee" => "02COM", "pri.ee" => "03PRI", "fie.ee"=>"04FIE", "med.ee" => "05MED"} + month = Time.now - 1.month + invoices_until = month.end_of_month + # pochemu registrar has_many :accounts + + activity_scope = AccountActivity.where(activity_type: [CREATE, RENEW]) + Registrar.joins(:account).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) + + 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 + } + } + end + + end + end end + +=begin +RN - incremental +RR - grouping of rows - is same for rows where sum is devided to more than one year eg. 7€ - 7€ - 7€ for 3 year 21€ domains + + + + + + + + + + + + + + + + + + + + +=end