Class: Sendregning::Invoice

Inherits:
Object
  • Object
show all
Defined in:
lib/sendregning/invoice.rb

Constant Summary collapse

OPTIONAL_ATTRIBUTES =
[
  # Request
  :invoiceType, :creditedId, :orderNo, :invoiceDate, :orderNo,
  :invoiceDate, :dueDate, :orderDate, :recipientNo, :address1, :address2,
  :country, :email, :ourRef, :yourRef, :comment, :invoiceText, :printDunningInfo,
  :itemTaxInclude,

  # Response
  :tax, :dueDate, :dunningFee, :invoiceNo, :total, :accountNo, :orgNrSuffix, :kid, :orgNo, :interestRate, :state
]
SHIPMENT_ATTRIBUTES =
[
  :shipment, :emailaddresses, :copyaddresses
]
SHIPMENT_MODES =
{
  :paper           => 'PAPER',
  :email           => 'EMAIL',
  :paper_and_email => 'PAPER_AND_EMAIL'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Invoice

Returns a new instance of Invoice.



31
32
33
34
# File 'lib/sendregning/invoice.rb', line 31

def initialize(attributes={})
  self.update(attributes)
  @lines = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (protected)



135
136
137
138
139
140
141
142
143
# File 'lib/sendregning/invoice.rb', line 135

def method_missing(method, *args)
  if OPTIONAL_ATTRIBUTES.include?(method)
    @optional[method]
  elsif SHIPPING_ATTRIBUTES.include?(method)
    @shipping[method]
  else
    super
  end
end

Instance Attribute Details

#cityObject

Returns the value of attribute city.



27
28
29
# File 'lib/sendregning/invoice.rb', line 27

def city
  @city
end

#clientObject

Returns the value of attribute client.



26
27
28
# File 'lib/sendregning/invoice.rb', line 26

def client
  @client
end

#linesObject

Returns the value of attribute lines.



29
30
31
# File 'lib/sendregning/invoice.rb', line 29

def lines
  @lines
end

#nameObject

Returns the value of attribute name.



27
28
29
# File 'lib/sendregning/invoice.rb', line 27

def name
  @name
end

#optionalObject

Returns the value of attribute optional.



28
29
30
# File 'lib/sendregning/invoice.rb', line 28

def optional
  @optional
end

#shipmentObject

Returns the value of attribute shipment.



28
29
30
# File 'lib/sendregning/invoice.rb', line 28

def shipment
  @shipment
end

#zipObject

Returns the value of attribute zip.



27
28
29
# File 'lib/sendregning/invoice.rb', line 27

def zip
  @zip
end

Instance Method Details

#add_line(line) ⇒ Object



45
46
47
48
49
# File 'lib/sendregning/invoice.rb', line 45

def add_line(line)
  line = Sendregning::Line.new(line) unless line.kind_of?(Sendregning::Line)
  @lines << line
  line
end

#paid?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/sendregning/invoice.rb', line 56

def paid?
  state == 'paid'
end

#send!Object

Sends an invoice



52
53
54
# File 'lib/sendregning/invoice.rb', line 52

def send!
  self.client.send_invoice(self)
end

#to_xml(options = {}) ⇒ Object

Renders invoice to XML



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/sendregning/invoice.rb', line 61

def to_xml(options={})
  if options[:builder]
    xml = options[:builder]
  else
    xml = Builder::XmlMarkup.new(:indent=>2)
    xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
  end
  xml.invoices do |invoices|
    invoices.invoice do |invoice|
      invoice.name @name
      invoice.zip  @zip
      invoice.city @city
      
      # Lines
      if @lines.length > 0
        invoice.lines do |line_builder|
          @lines.each{|l| l.to_xml(:builder => line_builder)}
        end
      end

      # Optional attributes
      if @optional.length > 0
        invoice.optional do |optional|
          @optional.each do |key, value|
            key = key.to_sym
            if value.kind_of?(Date) || value.kind_of?(Time)
              value = value.strftime("%d.%m.%y")
            end
            optional.tag! key, value
          end
        end
      end

      # Shipment attributes
      if @shipment.length > 0
        invoice.shipment do |shipment|
          shipment_mode = (@shipment[:shipment] || :paper).to_sym
          raise 'Invalid shipment mode!' unless SHIPMENT_MODES.keys.include?(shipment_mode)
          shipment_mode = SHIPMENT_MODES[shipment_mode]

          shipment.text! shipment_mode
          @shipment.each do |key, values|
            key = key.to_sym
            unless key == :shipment
              values = [values] unless values.kind_of?(Enumerable)
              shipment.tag! key do |emails|
                values.each{|v| emails.email v}
              end
            end
          end
        end
      end
    end
  end
end

#update(attributes = {}) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/sendregning/invoice.rb', line 36

def update(attributes={})
  @client  = attributes[:client] if attributes[:client]
  @name    = attributes[:name]   if attributes[:name]
  @zip     = attributes[:zip]    if attributes[:zip]
  @city    = attributes[:city]   if attributes[:city]
  self.optional = attributes
  self.shipment = attributes
end