Class: Recurring

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/fastbill/recurring.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth = nil) ⇒ Recurring

Returns a new instance of Recurring.



7
8
9
10
# File 'lib/fastbill/recurring.rb', line 7

def initialize(auth = nil)
  @auth = auth
  @is_new = true
end

Instance Attribute Details

#currency_codeObject

Returns the value of attribute currency_code.



5
6
7
# File 'lib/fastbill/recurring.rb', line 5

def currency_code
  @currency_code
end

#customer_costcenter_idObject

Returns the value of attribute customer_costcenter_id.



5
6
7
# File 'lib/fastbill/recurring.rb', line 5

def customer_costcenter_id
  @customer_costcenter_id
end

#customer_idObject

Returns the value of attribute customer_id.



5
6
7
# File 'lib/fastbill/recurring.rb', line 5

def customer_id
  @customer_id
end

#delivery_dateObject

Returns the value of attribute delivery_date.



5
6
7
# File 'lib/fastbill/recurring.rb', line 5

def delivery_date
  @delivery_date
end

#email_notifyObject

Returns the value of attribute email_notify.



5
6
7
# File 'lib/fastbill/recurring.rb', line 5

def email_notify
  @email_notify
end

#eu_deliveryObject

Returns the value of attribute eu_delivery.



5
6
7
# File 'lib/fastbill/recurring.rb', line 5

def eu_delivery
  @eu_delivery
end

#frequencyObject

Returns the value of attribute frequency.



5
6
7
# File 'lib/fastbill/recurring.rb', line 5

def frequency
  @frequency
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/fastbill/recurring.rb', line 5

def id
  @id
end

#introtextObject

Returns the value of attribute introtext.



5
6
7
# File 'lib/fastbill/recurring.rb', line 5

def introtext
  @introtext
end

#invoice_itemsObject

Returns the value of attribute invoice_items.



5
6
7
# File 'lib/fastbill/recurring.rb', line 5

def invoice_items
  @invoice_items
end

#invoice_numberObject

Returns the value of attribute invoice_number.



5
6
7
# File 'lib/fastbill/recurring.rb', line 5

def invoice_number
  @invoice_number
end

#invoice_typeObject

Returns the value of attribute invoice_type.



5
6
7
# File 'lib/fastbill/recurring.rb', line 5

def invoice_type
  @invoice_type
end

#is_newObject

Returns the value of attribute is_new.



5
6
7
# File 'lib/fastbill/recurring.rb', line 5

def is_new
  @is_new
end

#occurencesObject

Returns the value of attribute occurences.



5
6
7
# File 'lib/fastbill/recurring.rb', line 5

def occurences
  @occurences
end

#output_typeObject

Returns the value of attribute output_type.



5
6
7
# File 'lib/fastbill/recurring.rb', line 5

def output_type
  @output_type
end

#start_dateObject

Returns the value of attribute start_date.



5
6
7
# File 'lib/fastbill/recurring.rb', line 5

def start_date
  @start_date
end

#template_idObject

Returns the value of attribute template_id.



5
6
7
# File 'lib/fastbill/recurring.rb', line 5

def template_id
  @template_id
end

#vat_itemsObject

Returns the value of attribute vat_items.



5
6
7
# File 'lib/fastbill/recurring.rb', line 5

def vat_items
  @vat_items
end

Instance Method Details

#delete!Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/fastbill/recurring.rb', line 84

def delete!
  options = {
    :basic_auth => @auth,
    :headers => {
      "Content-Type" => "application/xml"
    },
    :body => '<?xml version="1.0" encoding="utf-8"?><FBAPI><SERVICE>recurring.delete</SERVICE><DATA><INVOICE_ID>' + @id + '</INVOICE_ID></DATA></FBAPI>'
  }
  r = self.class.post('/api/0.1/api.php', options)
  body = Crack::XML.parse r.body
  if !body['FBAPI']["RESPONSE"]["STATUS"].nil? && body['FBAPI']["RESPONSE"]["STATUS"] == "success"
    true
  else
    false
  end    
end

#get(id = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fastbill/recurring.rb', line 12

def get(id = nil)
  body = '<?xml version="1.0" encoding="utf-8"?><FBAPI><SERVICE>recurring.get</SERVICE>'
  if id
    body = body + "<FILTER><INVOICE_ID>#{id}</INVOICE_ID></FILTER>"
  end
  body = body + "</FBAPI>"
  options = {
    :basic_auth => @auth,
    :headers => {
      "Content-Type" => "application/xml"
    },
    :body => body
  }
  r = self.class.post('/api/0.1/api.php', options)
  body = Crack::XML.parse r.body
  invoices = []
  if body['FBAPI']["RESPONSE"]["INVOICES"]["INVOICE"].class.to_s == 'Hash'
    inv = Recurring.new(@auth)
    inv.hydrate(body['FBAPI']["RESPONSE"]["INVOICES"]["INVOICE"])
    invoices.push inv
  else
    for invoice in body['FBAPI']["RESPONSE"]["INVOICES"]["INVOICE"].each
      inv = Recurring.new(@auth)
      inv.hydrate(invoice)
      invoices.push inv
    end
  end
  invoices
end

#hydrate(body) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/fastbill/recurring.rb', line 154

def hydrate(body)
  @is_new = false
  @id = body["INVOICE_ID"]
  @invoice_type = body["INVOICE_TYPE"]
  @customer_id = body["CUSTOMER_ID"]
  @customer_costcenter_id = body["CUSTOMER_COSTCENTER_ID"]
  @currency_code = body["CURRENCY_CODE"]
  @template_id = body["TEMPLATE_ID"]
  @occurances = body["OCCURANCES"]
  @frequency = body["FREQUENCY"]
  @start_date = parse_date body["START_DATE"]
  @email_notify = body["EMAIL_NOTIFY"]
  @output_type = body["OUTPUT_TYPE"]
  @introtext = body["INTROTEXT"]
  #@delivery_date = parse_date body["DELIVERY_DATE"]
  @sub_total = body["SUB_TOTAL"]
  @vat_total = body["VAT_TOTAL"]
  #@eu_delivery = body["EU_DELIVERY"] == "1" ? true : false
  @vat_items = []
  @total = body["TOTAL"]
  for vat_item in body["VAT_ITEMS"].each
    @vat_items.push InvoiceVatItem.new vat_item.last
  end
  @invoice_items = []
  for item in body["ITEMS"].each
    begin
      i =  InvoiceItem.new(@auth)
      i.hydrate(item.last)
      @invoice_items.push i
    rescue
    end
  end
end

#parse_date(date) ⇒ Object



187
188
189
190
191
192
193
# File 'lib/fastbill/recurring.rb', line 187

def parse_date(date)
  if date != nil && date != "0000-00-00 00:00:00"
    Time.parse date
  else
    false
  end
end

#save!Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fastbill/recurring.rb', line 41

def save!
  if @is_new
    #create
    options = {
      :basic_auth => @auth,
      :headers => {
        "Content-Type" => "application/xml"
      },
      :body => '<?xml version="1.0" encoding="utf-8"?><FBAPI><SERVICE>recurring.create</SERVICE><DATA>' + self.to_xml + '</DATA></FBAPI>'
    }
    r = self.class.post('/api/0.1/api.php', options)
    body = Crack::XML.parse r.body
    if !body['FBAPI']["RESPONSE"]["STATUS"].nil? && body['FBAPI']["RESPONSE"]["STATUS"] == "success"
      unless body['FBAPI']["RESPONSE"]["STATUS"]["INVOICE_ID"].nil?
        @id = body['FBAPI']["RESPONSE"]["STATUS"]["INVOICE_ID"]
      end
      @is_new = false
      self
    else
      false
    end
  else
    #update
    options = {
      :basic_auth => @auth,
      :headers => {
        "Content-Type" => "application/xml"
      },
      :body => '<?xml version="1.0" encoding="utf-8"?><FBAPI><SERVICE>recurring.update</SERVICE><DATA>' + self.to_xml + '</DATA></FBAPI>'
    }
    r = self.class.post('/api/0.1/api.php', options)
    body = Crack::XML.parse r.body
    if !body['FBAPI']["RESPONSE"]["STATUS"].nil? && body['FBAPI']["RESPONSE"]["STATUS"] == "success"
      unless body['FBAPI']["RESPONSE"]["STATUS"]["INVOICE_ID"].nil?
        @id = body['FBAPI']["RESPONSE"]["STATUS"]["INVOICE_ID"]
      end
      @is_new = false
      self
    else
      false
    end
  end
end

#to_xmlObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/fastbill/recurring.rb', line 100

def to_xml
  xml = ""
  unless @id.nil?
    xml = xml + "<INVOICE_ID>#{@id}</INVOICE_ID>"
  end
  unless @customer_id.nil?
    xml = xml + "<CUSTOMER_ID>#{@customer_id}</CUSTOMER_ID>"
  end
  unless @customer_costcenter_id.nil?
    xml = xml + "<CUSTOMER_COSTCENTER_ID>#{@customer_costcenter_id}</CUSTOMER_COSTCENTER_ID>"
  end
  unless @currency_code.nil?
    xml = xml + "<CURRENCY_CODE>#{@currency_code}</CURRENCY_CODE>"
  end
  unless @template_id.nil?
    xml = xml + "<TEMPLATE_ID>#{@template_id}</TEMPLATE_ID>"
  end
  unless @invoice_number.nil?
    xml = xml + "<INVOICE_NUMBER>#{@invoice_number}</INVOICE_NUMBER>"
  end
  unless @introtext.nil?
    xml = xml + "<INTROTEXT>#{@introtext}</INTROTEXT>"
  end
  if @start_date
    xml = xml + "<START_DATE>#{@start_date.strftime("%Y-%m-%d")}</START_DATE>"
  end
  unless @frequency.nil?
    xml = xml + "<FREQUENCY>#{@frequency}</FREQUENCY>"
  end
  unless @occurrences.nil?
    xml = xml + "<OCCURENCES>#{@occurences}</OCCURENCES>"
  end
  unless @output_type.nil?
    xml = xml + "<OUTPUT_TYPE>#{@output_type}</OUTPUT_TYPE>"
  end
  unless @email_notify.nil?
    xml = xml + "<EMAIL_NOTIFY>#{@email_notify}</EMAIL_NOTIFY>"
  end
  unless @delivery_date.nil?
    xml = xml + "<DELIVERY_DATE>#{@delivery_date.strftime("%Y-%m-%d")}</DELIVERY_DATE>"
  end
  unless @eu_delivery.nil?
    eu = @eu_delivery ? 1 : 0
    xml = xml + "<EU_DELIVERY>#{@eu}</EU_DELIVERY>"
  end
  unless @invoice_items.length == 0
    xml = xml + "<ITEMS>"
    for item in @invoice_items.each
      xml = xml + item.to_xml
    end
    xml = xml + "</ITEMS>"
  end
  xml
end