Class: HarvestToInfakt::Harvest::Invoice
- Defined in:
- lib/harvest_to_infakt/harvest/invoice.rb
Class Method Summary collapse
- .all ⇒ Object
- .basic_auth_options ⇒ Object
- .fetch_all ⇒ Object
- .fetch_one(id) ⇒ Object
- .find(id) ⇒ Object
Instance Method Summary collapse
- #client ⇒ Object
-
#initialize(data) ⇒ Invoice
constructor
A new instance of Invoice.
- #to_infakt ⇒ Object
Methods inherited from Resource
Constructor Details
#initialize(data) ⇒ Invoice
Returns a new instance of Invoice.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/harvest_to_infakt/harvest/invoice.rb', line 35 def initialize(data) @data = data if @data['csv_line_items'] csv_line_items = FasterCSV.parse(@data['csv_line_items']) keys = csv_line_items.shift @data['items'] = csv_line_items.inject([]) do |items, array| item = {} keys.each do |key| item[key] = array.shift end items << item end @data.delete('csv_line_items') end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class HarvestToInfakt::Resource
Class Method Details
.all ⇒ Object
11 12 13 |
# File 'lib/harvest_to_infakt/harvest/invoice.rb', line 11 def all @all ||= fetch_all end |
.basic_auth_options ⇒ Object
7 8 9 |
# File 'lib/harvest_to_infakt/harvest/invoice.rb', line 7 def { :username => HarvestToInfakt.configuration.harvest[:username], :password => HarvestToInfakt.configuration.harvest[:password]} end |
.fetch_all ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/harvest_to_infakt/harvest/invoice.rb', line 20 def fetch_all response = get('/invoices.xml', :basic_auth => ) response.parsed_response['invoices'].inject([]) do |invoices, data| invoices << new(data) invoices end end |
.fetch_one(id) ⇒ Object
29 30 31 32 |
# File 'lib/harvest_to_infakt/harvest/invoice.rb', line 29 def fetch_one(id) response = get("/invoices/#{id}.xml", :basic_auth => ) new(response.parsed_response['invoice']) end |
.find(id) ⇒ Object
15 16 17 18 |
# File 'lib/harvest_to_infakt/harvest/invoice.rb', line 15 def find(id) @find ||= {} @find[id.to_s] ||= fetch_one(id) end |
Instance Method Details
#client ⇒ Object
54 55 56 |
# File 'lib/harvest_to_infakt/harvest/invoice.rb', line 54 def client Client.find(client_id) end |
#to_infakt ⇒ Object
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 |
# File 'lib/harvest_to_infakt/harvest/invoice.rb', line 58 def to_infakt now = Time.now data = { 'zaplacono' => 0, 'waluta' => 'PLN', 'uwagi' => '', 'data_wystawienia' => now.strftime("%Y-%m-%d"), 'data_sprzedazy' => now.strftime("%Y-%m-%d"), 'rodzaj_faktury' => 'Faktura VAT', 'termin_zaplaty' => (now + 2*7*24*60*60).strftime("%Y-%m-%d") # +2 weeks } data['services'] = items.inject([]) do |items, item| hash = { 'symbol' => '', 'nazwa' => item['description'].sub(/(\[.+\].+) - (\d{4}\/\d{2}\/\d{2}) - (.+):/, "#{$3}:"), 'ilosc' => item['quantity'].to_f, 's_vat' => 23, 'cena_netto' => item['unit_price'].to_f, 'jm' => '1 godz.' } items << hash items end HarvestToInfakt::Infakt::Invoice.new(data) end |