Class: Einvoicing::Invoice

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

Overview

Core invoice model. All monetary values are in the invoice currency.

Examples:

seller = Einvoicing::Party.new(name: "Acme SAS", siren: "123456789", vat_number: "FR12123456789")
buyer  = Einvoicing::Party.new(name: "Client SA", siren: "987654321")
line   = Einvoicing::LineItem.new(description: "Consulting", quantity: 1, unit_price: 1000.00)

invoice = Einvoicing::Invoice.new(
  invoice_number: "INV-2024-001",
  issue_date: Date.today,
  seller: seller,
  buyer: buyer,
  lines: [line]
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(invoice_number:, issue_date:, seller:, buyer:, lines:, due_date: nil, currency: "EUR", tax_currency: nil, tax_breakdown: nil, payment_reference: nil, note: nil, payment_means_code: nil, iban: nil, bic: nil, document_type: :invoice, original_invoice_number: nil, original_invoice_date: nil, prepaid_amount: BigDecimal(0)) ⇒ Invoice

Returns a new instance of Invoice.



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
# File 'lib/einvoicing/invoice.rb', line 41

def initialize(invoice_number:, issue_date:, seller:, buyer:, lines:,
               due_date: nil, currency: "EUR", tax_currency: nil, tax_breakdown: nil,
               payment_reference: nil, note: nil,
               payment_means_code: nil, iban: nil, bic: nil,
               document_type: :invoice, original_invoice_number: nil, original_invoice_date: nil,
               prepaid_amount: BigDecimal(0))
  computed_breakdown = tax_breakdown || compute_tax_breakdown(lines)
  super(
    invoice_number: invoice_number,
    issue_date: issue_date,
    due_date: due_date,
    currency: currency,
    tax_currency: tax_currency,
    seller: seller,
    buyer: buyer,
    lines: lines,
    tax_breakdown: computed_breakdown,
    payment_reference: payment_reference,
    note: note,
    payment_means_code: payment_means_code,
    iban: iban,
    bic: bic,
    document_type: document_type,
    original_invoice_number: original_invoice_number,
    original_invoice_date: original_invoice_date,
    prepaid_amount: prepaid_amount.nil? ? BigDecimal(0) : BigDecimal(prepaid_amount.to_s)
  )
end

Instance Attribute Details

#bicObject (readonly)

Returns the value of attribute bic

Returns:

  • (Object)

    the current value of bic



21
22
23
# File 'lib/einvoicing/invoice.rb', line 21

def bic
  @bic
end

#buyerObject (readonly)

Returns the value of attribute buyer

Returns:

  • (Object)

    the current value of buyer



21
22
23
# File 'lib/einvoicing/invoice.rb', line 21

def buyer
  @buyer
end

#currencyObject (readonly)

Returns the value of attribute currency

Returns:

  • (Object)

    the current value of currency



21
22
23
# File 'lib/einvoicing/invoice.rb', line 21

def currency
  @currency
end

#document_typeObject (readonly)

Returns the value of attribute document_type

Returns:

  • (Object)

    the current value of document_type



21
22
23
# File 'lib/einvoicing/invoice.rb', line 21

def document_type
  @document_type
end

#due_dateObject (readonly)

Returns the value of attribute due_date

Returns:

  • (Object)

    the current value of due_date



21
22
23
# File 'lib/einvoicing/invoice.rb', line 21

def due_date
  @due_date
end

#ibanObject (readonly)

Returns the value of attribute iban

Returns:

  • (Object)

    the current value of iban



21
22
23
# File 'lib/einvoicing/invoice.rb', line 21

def iban
  @iban
end

#invoice_numberObject (readonly)

Returns the value of attribute invoice_number

Returns:

  • (Object)

    the current value of invoice_number



21
22
23
# File 'lib/einvoicing/invoice.rb', line 21

def invoice_number
  @invoice_number
end

#issue_dateObject (readonly)

Returns the value of attribute issue_date

Returns:

  • (Object)

    the current value of issue_date



21
22
23
# File 'lib/einvoicing/invoice.rb', line 21

def issue_date
  @issue_date
end

#linesObject (readonly)

Returns the value of attribute lines

Returns:

  • (Object)

    the current value of lines



21
22
23
# File 'lib/einvoicing/invoice.rb', line 21

def lines
  @lines
end

#noteObject (readonly)

Returns the value of attribute note

Returns:

  • (Object)

    the current value of note



21
22
23
# File 'lib/einvoicing/invoice.rb', line 21

def note
  @note
end

#original_invoice_dateObject (readonly)

Returns the value of attribute original_invoice_date

Returns:

  • (Object)

    the current value of original_invoice_date



21
22
23
# File 'lib/einvoicing/invoice.rb', line 21

def original_invoice_date
  @original_invoice_date
end

#original_invoice_numberObject (readonly)

Returns the value of attribute original_invoice_number

Returns:

  • (Object)

    the current value of original_invoice_number



21
22
23
# File 'lib/einvoicing/invoice.rb', line 21

def original_invoice_number
  @original_invoice_number
end

#payment_means_codeObject (readonly)

Returns the value of attribute payment_means_code

Returns:

  • (Object)

    the current value of payment_means_code



21
22
23
# File 'lib/einvoicing/invoice.rb', line 21

def payment_means_code
  @payment_means_code
end

#payment_referenceObject (readonly)

Returns the value of attribute payment_reference

Returns:

  • (Object)

    the current value of payment_reference



21
22
23
# File 'lib/einvoicing/invoice.rb', line 21

def payment_reference
  @payment_reference
end

#prepaid_amountObject (readonly)

Returns the value of attribute prepaid_amount

Returns:

  • (Object)

    the current value of prepaid_amount



21
22
23
# File 'lib/einvoicing/invoice.rb', line 21

def prepaid_amount
  @prepaid_amount
end

#sellerObject (readonly)

Returns the value of attribute seller

Returns:

  • (Object)

    the current value of seller



21
22
23
# File 'lib/einvoicing/invoice.rb', line 21

def seller
  @seller
end

#tax_breakdownObject (readonly)

Returns the value of attribute tax_breakdown

Returns:

  • (Object)

    the current value of tax_breakdown



21
22
23
# File 'lib/einvoicing/invoice.rb', line 21

def tax_breakdown
  @tax_breakdown
end

#tax_currencyObject (readonly)

Returns the value of attribute tax_currency

Returns:

  • (Object)

    the current value of tax_currency



21
22
23
# File 'lib/einvoicing/invoice.rb', line 21

def tax_currency
  @tax_currency
end

Instance Method Details

#due_amountObject

Amount due after deducting any retained/prepaid amount (BT-113). VAT remains due on the full gross_total — only the payable balance is reduced (EN 16931 BR-CO-16: DuePayableAmount = GrandTotal − TotalPrepaidAmount).



89
90
91
# File 'lib/einvoicing/invoice.rb', line 89

def due_amount
  gross_total - prepaid_amount
end

#gross_totalObject

Grand total including VAT — computed from per-line gross amounts to avoid double-rounding through already-rounded net_total/tax_total (EN 16931 BR-CO-13).



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

def gross_total
  lines.sum(BigDecimal("0"), &:gross_amount).round(2, :half_up)
end

#net_totalObject

Sum of all line net amounts (excl. VAT).



71
72
73
# File 'lib/einvoicing/invoice.rb', line 71

def net_total
  lines.sum(BigDecimal("0"), &:net_amount).round(2, :half_up)
end

#tax_totalObject

Total VAT across all lines.



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

def tax_total
  tax_breakdown.sum(BigDecimal("0"), &:tax_amount).round(2, :half_up)
end