Class: HarvestToInfakt::Harvest::Invoice

Inherits:
Resource
  • Object
show all
Defined in:
lib/harvest_to_infakt/harvest/invoice.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#id, #method_missing

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

.allObject



11
12
13
# File 'lib/harvest_to_infakt/harvest/invoice.rb', line 11

def all
  @all ||= fetch_all
end

.basic_auth_optionsObject



7
8
9
# File 'lib/harvest_to_infakt/harvest/invoice.rb', line 7

def basic_auth_options
  { :username => HarvestToInfakt.configuration.harvest[:username], :password => HarvestToInfakt.configuration.harvest[:password]}
end

.fetch_allObject



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 => basic_auth_options)

  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 => basic_auth_options)
  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

#clientObject



54
55
56
# File 'lib/harvest_to_infakt/harvest/invoice.rb', line 54

def client
  Client.find(client_id)
end

#to_infaktObject



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