Class: Spree::Kashflow::InvoicePayload

Inherits:
Object
  • Object
show all
Defined in:
app/presenters/spree/kashflow/invoice_payload.rb

Overview

Maps a Spree order onto the shape KashFlow's Invoice complex type expects.

TKF prices are VAT-inclusive; KashFlow wants net (VAT-exclusive) per-unit rates. Because a per-unit Rate is rounded to 4 decimal places, Rate * Quantity is not guaranteed to reconcile back to the line's net total, or the assembled invoice back to the order's total. #to_h asserts both reconciliations before returning and raises TotalMismatchError rather than post a silently-wrong invoice. Pure object: reads the order and its associations, performs no network calls, and writes nothing back to Spree.

Constant Summary collapse

SHIPPING_DESCRIPTION =

Returns the description used for the shipping line.

Returns:

  • (String)

    the description used for the shipping line

"Shipping"

Instance Method Summary collapse

Constructor Details

#initialize(order, integration:) ⇒ InvoicePayload

Returns a new instance of InvoicePayload.

Parameters:

  • order (Spree::Order)

    a completed order to post as an invoice

  • integration (Spree::Integrations::Kashflow)

    the integration whose nominal codes decide which ledger accounts the lines post to



25
26
27
28
# File 'app/presenters/spree/kashflow/invoice_payload.rb', line 25

def initialize(order, integration:)
  @order = order
  @integration = integration
end

Instance Method Details

#linesArray<Hash{String => Object}>

Note:

Unreconciled. Only #to_h runs the correctness guard; a caller that needs the guarantee that these lines actually add up to the order's total (Task 6's credit notes, for instance) must go through #to_h, not call this directly.

Returns the KashFlow InvoiceLine structures: one per Spree line item, plus one for shipping.

Returns:

  • (Array<Hash{String => Object}>)

    the KashFlow InvoiceLine structures: one per Spree line item, plus one for shipping



38
39
40
# File 'app/presenters/spree/kashflow/invoice_payload.rb', line 38

def lines
  line_entries.map { |entry| entry[:line] }
end

#to_hHash{String => Object}

Note:

Known limitation: orders with exclusive tax (additional_tax_total, added on top of the price rather than included in it) are not reconciled by this arithmetic and will always raise here — refused rather than mis-booked, but not otherwise handled by this mapper.

Assembles the KashFlow Invoice structure, guarding the arithmetic before returning it.

Returns:

  • (Hash{String => Object})

    a KashFlow Invoice structure

Raises:

  • (Spree::Kashflow::TotalMismatchError)

    when a line's Rate * Quantity fails to reconcile against its net total, or the assembled invoice fails to reconcile against the order's total



55
56
57
58
59
60
61
62
63
64
65
# File 'app/presenters/spree/kashflow/invoice_payload.rb', line 55

def to_h
  entries = line_entries
  assert_totals_reconcile!(entries)

  {
    "CurrencyCode" => order.currency,
    "Lines" => entries.map { |entry| entry[:line] },
    "NetAmount" => entries.sum { |entry| entry[:net_total] }.round(2),
    "VATAmount" => entries.sum { |entry| entry[:line]["VatAmount"] }
  }
end