Class: Delivery::Transitions::ToInvoicedHandler

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/delivery/transitions/to_invoiced_handler.rb

Overview

Handles all business logic for transitioning a delivery to the 'invoiced' state.

This is a BEFORE_TRANSITION handler - it runs before the state is saved.
If any step fails and raises an exception, the state transition is aborted
and the delivery remains in its previous state ('shipped').

== Execution Order

  1. set_cogs - Calculate and lock in cost of goods sold
  2. delete_serial_number_reservations - Release any held serial numbers
  3. check_payments_status - Update order payment state
  4. mark_store_transfer_po_as_shipped - Update linked PO for store transfers
  5. handle_invoicing - Create invoice OR ledger entries (mutually exclusive)
  6. cleanup_shipments - Remove temporary shipment records

== Why Before Transition?

Invoice creation happens in before_transition because if it fails,
we want the delivery to remain in 'shipped' state for retry.
The invoice creation is the critical, potentially-failing operation.

Examples:

Delivery::Transitions::ToInvoicedHandler.new(delivery).process

Instance Method Summary collapse

Constructor Details

#initialize(delivery) ⇒ ToInvoicedHandler

Returns a new instance of ToInvoicedHandler.



28
29
30
31
32
# File 'app/services/delivery/transitions/to_invoiced_handler.rb', line 28

def initialize(delivery)
  super()
  @delivery = delivery
  @order = delivery.order
end

Instance Method Details

#processObject



34
35
36
37
38
39
40
41
42
43
# File 'app/services/delivery/transitions/to_invoiced_handler.rb', line 34

def process
  logger.tagged("Delivery #{@delivery.id} ToInvoicedHandler") do
    set_cogs
    delete_serial_number_reservations
    check_payments_status
    mark_store_transfer_po_as_shipped
    handle_invoicing
    cleanup_shipments
  end
end