Class: Spree::Invoice

Inherits:
Base
  • Object
show all
Defined in:
app/models/spree/invoice.rb

Instance Method Summary collapse

Instance Method Details

#add_sunat_line(sunat_invoice, resource) ⇒ Object

add a new ‘SunatInvoice::Item` to the given sunat invoice sunat_invoice => `SunatInvoice::Invoice` instance to add lines resource => an order’s line_item or shipment



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/spree/invoice.rb', line 48

def add_sunat_line(sunat_invoice, resource)
  # TODO: handle items with taxes different than vat
  tax_amount = resource.adjustments.tax.sum(:amount)
  tax = SunatInvoice::Tax.new(amount: tax_amount, tax_type: :igv)
  quantity = resource.respond_to?(:quantity) ? resource.quantity : 1
  item_attrs = {
    quantity: quantity,
    price: resource.price_excluding_vat,
    price_code: '01',
    taxes: [tax]
  }
  sunat_invoice.lines << SunatInvoice::Item.new(item_attrs)
end

#build_sunat_invoiceObject

return a ‘SunatInvoice::Invoice`



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/spree/invoice.rb', line 23

def build_sunat_invoice
  # generate SunatInvoice::Invoice
  sunat_invoice = SunatInvoice::Invoice.new(sunat_attributes)

  # add invoice lines from line_items
  order.line_items.map do |line_item|
    add_sunat_line(sunat_invoice, line_item)
  end

  # add invoice lines from shipment
  order.shipments.map do |shipment|
    next unless shipment.amount.positive?

    add_sunat_line(sunat_invoice, shipment)
  end

  # store xml
  store_xml(sunat_invoice.xml) if respond_to?(:store_xml)

  sunat_invoice
end

#sunat_attributesObject



15
16
17
18
19
20
# File 'app/models/spree/invoice.rb', line 15

def sunat_attributes
  attrs = (preferred_sunat_attributes || {}).dup
  provider = SunatInvoice::Provider.new(attrs[:provider] || {})
  attrs[:provider] = provider
  attrs
end