Class: PolishInvoicer::Invoice

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

Constant Summary collapse

AVAILABLE_PARAMS =
[
  :number,            # numer faktury (string)
  :create_date,       # data wystawienia faktury (date)
  :trade_date,        # data sprzedaży (date)
  :seller,            # adres sprzedawcy (tablica stringów)
  :seller_nip,        # NIP sprzedawcy (string)
  :buyer,             # adres nabywcy (tablica stringów)
  :buyer_nip,         # NIP nabywcy (string)
  :recipient,         # odbiorca faktury (tablica stringów)
  :item_name,         # nazwa usługi (string)
  :price,             # cena w złotych (float)
  :price_paid,        # kwota częściowego opłacenia faktury w złotych
  :gross_price,       # znacznik rodzaju ceny (netto/brutto), domyślnie: true (boolean)
  :vat,               # stawka vat, domyślnie: 23 (integer)
  :pkwiu,             # numer PKWiU (string)
  :payment_type,      # rodzaj płatności, domyślnie: 'Przelew' (string)
  :payment_date,      # termin płatności (date)
  :comments,          # uwagi (string lub tablica stringów)
  :paid,              # znacznik opłacenia faktury, domyślnie: true (boolean)
  :footer,            # treść umieszczana w stopce faktury (string)
  :proforma,          # znacznik faktury pro-forma, domyślnie: false (boolean)
  :no_vat_reason,     # podstawa prawna zwolnienia z VAT (string)
  :foreign_buyer,     # nabywcą jest firma spoza Polski, domyślnie: false (boolean)
  :lang,              # język na fakturze, domyślnie: zależny od ustawienia foreign_buyer
                      # foreign_buyer = false => lang = 'pl'
                      # foreign_buyer = true => lang = 'pl_en'
                      # możliwe wartości: pl | pl_en | en | es
  :reverse_charge,    # faktura z odwrotnym obciążeniem VAT
  :currency,          # waluta rozliczeniowa, domyślnie: PLN (string)
  :exchange_rate      # kurs waluty rozliczeniowej, domyślnie: 1.0000 (float)
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Invoice

Returns a new instance of Invoice.



39
40
41
42
43
44
45
46
47
# File 'lib/polish_invoicer/invoice.rb', line 39

def initialize(params = {})
  set_defaults
  params.each do |k, v|
    raise "Nierozpoznany parametr #{k}" unless AVAILABLE_PARAMS.include?(k)

    send("#{k}=", v)
  end
  @validator = PolishInvoicer::Validator.new(self)
end

Instance Method Details

#errorsObject



49
50
51
# File 'lib/polish_invoicer/invoice.rb', line 49

def errors
  @validator.errors
end

#exchanged_taxObject



91
92
93
# File 'lib/polish_invoicer/invoice.rb', line 91

def exchanged_tax
  (vat_value * exchange_rate)
end

#gross_valueObject

cena/wartość brutto



70
71
72
73
74
# File 'lib/polish_invoicer/invoice.rb', line 70

def gross_value
  return price if gross_price

  price + (price * Vat.to_i(vat) / 100.0)
end

#net_valueObject

cena/wartość netto



58
59
60
61
62
# File 'lib/polish_invoicer/invoice.rb', line 58

def net_value
  return price unless gross_price

  price / (1 + (Vat.to_i(vat) / 100.0))
end


99
100
101
# File 'lib/polish_invoicer/invoice.rb', line 99

def paid_value
  paid ? total_to_pay_value : price_paid.to_f
end

#save_to_html(path) ⇒ Object



76
77
78
79
# File 'lib/polish_invoicer/invoice.rb', line 76

def save_to_html(path)
  validate!
  Writer.new(self).save_to_html(path)
end

#save_to_pdf(path) ⇒ Object



81
82
83
84
# File 'lib/polish_invoicer/invoice.rb', line 81

def save_to_pdf(path)
  validate!
  Writer.new(self).save_to_pdf(path)
end

#template_fileObject



111
112
113
# File 'lib/polish_invoicer/invoice.rb', line 111

def template_file
  proforma ? "proforma-#{template_lang}.slim" : "invoice-#{template_lang}.slim"
end

#template_langObject



107
108
109
# File 'lib/polish_invoicer/invoice.rb', line 107

def template_lang
  lang || (foreign_buyer ? 'pl_en' : 'pl')
end

#to_hashObject

Wszystkie dane w postaci hash-a



87
88
89
# File 'lib/polish_invoicer/invoice.rb', line 87

def to_hash
  Presenter.new(self).data
end

#to_pay_valueObject



103
104
105
# File 'lib/polish_invoicer/invoice.rb', line 103

def to_pay_value
  paid ? 0 : (total_to_pay_value - price_paid.to_f)
end

#total_to_pay_valueObject



95
96
97
# File 'lib/polish_invoicer/invoice.rb', line 95

def total_to_pay_value
  reverse_charge ? net_value : gross_value
end

#valid?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/polish_invoicer/invoice.rb', line 53

def valid?
  @validator.valid?
end

#vat_valueObject

kwota VAT



65
66
67
# File 'lib/polish_invoicer/invoice.rb', line 65

def vat_value
  (gross_value * Vat.to_i(vat)) / (100.0 + Vat.to_i(vat))
end