Class: Spree::Kashflow::InvoicePayload
- Inherits:
-
Object
- Object
- Spree::Kashflow::InvoicePayload
- 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.
"Shipping"
Instance Method Summary collapse
-
#initialize(order, integration:) ⇒ InvoicePayload
constructor
A new instance of InvoicePayload.
-
#lines ⇒ Array<Hash{String => Object}>
The KashFlow
InvoiceLinestructures: one per Spree line item, plus one for shipping. -
#to_h ⇒ Hash{String => Object}
Assembles the KashFlow
Invoicestructure, guarding the arithmetic before returning it.
Constructor Details
#initialize(order, integration:) ⇒ InvoicePayload
Returns a new instance of InvoicePayload.
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
#lines ⇒ Array<Hash{String => Object}>
Returns 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_h ⇒ Hash{String => Object}
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.
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 |