Class: Delivery::Transitions::ToInvoicedHandler
- Inherits:
-
BaseService
- Object
- BaseService
- Delivery::Transitions::ToInvoicedHandler
- 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
- set_cogs - Calculate and lock in cost of goods sold
- delete_serial_number_reservations - Release any held serial numbers
- check_payments_status - Update order payment state
- mark_store_transfer_po_as_shipped - Update linked PO for store transfers
- handle_invoicing - Create invoice OR ledger entries (mutually exclusive)
- 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.
Instance Method Summary collapse
-
#initialize(delivery) ⇒ ToInvoicedHandler
constructor
A new instance of ToInvoicedHandler.
- #process ⇒ Object
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
#process ⇒ Object
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 |