Module: Einvoicing::Invoiceable

Defined in:
lib/einvoicing/invoiceable.rb

Overview

ActiveSupport::Concern that adds e-invoicing capabilities to an ActiveRecord model.

The model must respond to the following methods (columns or Ruby methods):

invoice_number, issue_date, due_date, currency,
einvoicing_seller, einvoicing_buyer, einvoicing_lines

Examples:

class Invoice < ApplicationRecord
  include Einvoicing::Invoiceable

  def einvoicing_seller
    Einvoicing::Party.new(
      name:       company.name,
      siren:      company.siren,
      vat_number: company.vat_number,
      street:     company.street,
      city:       company.city,
      postal_code: company.postal_code
    )
  end

  def einvoicing_buyer
    Einvoicing::Party.new(name: client.name, siren: client.siren)
  end

  def einvoicing_lines
    line_items.map do |li|
      Einvoicing::LineItem.new(
        description: li.description,
        quantity:    li.quantity,
        unit_price:  li.unit_price,
        vat_rate:    li.vat_rate
      )
    end
  end
end

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



42
43
44
# File 'lib/einvoicing/invoiceable.rb', line 42

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#einvoicing_errorsArray<Hash>

Validate the invoice using the configured validator.

Returns:

  • (Array<Hash>)

    list of error hashes ({ field:, error:, message: })



99
100
101
# File 'lib/einvoicing/invoiceable.rb', line 99

def einvoicing_errors
  self.class.einvoicing_validator.validate(to_einvoice)
end

#einvoicing_valid?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/einvoicing/invoiceable.rb', line 104

def einvoicing_valid?
  einvoicing_errors.empty?
end

#to_cii_xmlString

Generate CII D16B XML string.

Returns:

  • (String)


79
80
81
# File 'lib/einvoicing/invoiceable.rb', line 79

def to_cii_xml
  Einvoicing::Formats::CII.generate(to_einvoice)
end

#to_einvoiceEinvoicing::Invoice

Build an Einvoicing::Invoice from this record.

Returns:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/einvoicing/invoiceable.rb', line 61

def to_einvoice
  has_due_date = self.class.respond_to?(:column_names) \
    ? self.class.column_names.include?("due_date") \
    : respond_to?(:due_date)

  Einvoicing::Invoice.new(
    invoice_number: invoice_number,
    issue_date:     issue_date,
    due_date:       has_due_date ? due_date : nil,
    currency:       respond_to?(:currency) ? (currency || "EUR") : "EUR",
    seller:         einvoicing_seller,
    buyer:          einvoicing_buyer,
    lines:          einvoicing_lines
  )
end

#to_facturx(pdf_data) ⇒ String

Generate Factur-X PDF by embedding CII XML into an existing PDF blob.

Parameters:

  • pdf_data (String)

    original PDF binary

Returns:

  • (String)

    Factur-X PDF binary



92
93
94
95
# File 'lib/einvoicing/invoiceable.rb', line 92

def to_facturx(pdf_data)
  xml = to_cii_xml
  Einvoicing::Formats::FacturX.embed(pdf_data, xml)
end

#to_ubl_xmlString

Generate UBL 2.1 XML string.

Returns:

  • (String)


85
86
87
# File 'lib/einvoicing/invoiceable.rb', line 85

def to_ubl_xml
  Einvoicing::Formats::UBL.generate(to_einvoice)
end

#transmit!(adapter: nil) ⇒ Object

Stub: override in your model or configure an adapter. Returns a hash with :status and optionally :reference.

Raises:

  • (NotImplementedError)


115
116
117
118
# File 'lib/einvoicing/invoiceable.rb', line 115

def transmit!(adapter: nil)
  raise NotImplementedError,
        "Configure a transmission adapter or override #transmit! in #{self.class}"
end

#validate_einvoice!Object

Raise ValidationError unless valid.



109
110
111
# File 'lib/einvoicing/invoiceable.rb', line 109

def validate_einvoice!
  self.class.einvoicing_validator.validate!(to_einvoice)
end