Feature: #918 - Create cancellations and returns and change their status 

See Issue 918

Description 

Cancellations and returns (see Feature: #916 - Cancellations and returns with lines) are created and changed by services in \Extcode\Cart\Domain\Order\Fulfillment, like shipments (see Feature: #905 - Create shipments and derive the shipping status). The backend UI and the restocking follow separately.

OrderReturnCreationService::create() stores a cancellation or a return with its products in one database transaction. The order is locked while the quantities are checked:

  • Only products of the order with a count greater than 0 can be added, a cancellation or a return can not be created as rejected. Otherwise an \InvalidArgumentException is thrown.
  • A cancellation must not exceed the ordered count minus the counts in shipments which are not canceled and in cancellations which are not rejected.
  • A return must not exceed the shipped count minus the counts in returns which are not rejected.
  • If a count is exceeded, nothing is stored and the outcome is OrderReturnCreationOutcome::QuantityExceeded.
  • The creation is written to the order history with the type cancellation_status or return_status, the subject return:<uid> and the products and counts as data.

Listeners of the new event \Extcode\Cart\Event\Order\BeforeOrderReturnCreationEvent can prevent the creation with veto(). EXT:cart vetoes products which are not cancelable or not returnable. If the request comes from the backend and the cancellation or return has an override reason, it is created anyway; the veto reason and the override reason are written to the order history. A frontend or API request never overrides a veto (OrderReturnCreationOutcome::Vetoed). Listeners of the new event \Extcode\Cart\Event\Order\AfterOrderReturnCreationEvent react to the creation.

OrderReturnStatusChangeService::change() changes the status of a cancellation or a return atomically, with the new events BeforeOrderReturnStatusChangeEvent (with veto) and AfterOrderReturnStatusChangeEvent. accepted and rejected are final, a change from them is StatusChangeOutcome::NotAllowed. A return gets the received date when it becomes received without one.

OrderProductQuantities contain per ordered product the counts in cancellations and returns which are not rejected, the canceled count (accepted cancellations) and the returned count (accepted returns), and whether the product is cancelable and returnable. Cancellations which are not rejected reduce the count available for a shipment.

After a cancellation is created or its status is changed, the shipping status of the order is derived again:

  • canceled: all ordered quantities of shippable products are canceled.
  • shipped: all quantities of shippable products which are not canceled are shipped.

Returns do not change the shipping status of the order.

The order history in the order details of the backend module shows the details of a change in a new column: the shipment, cancellation or return it belongs to, the products and counts, the reason of a return and the veto and override reasons.

Impact 

Without the feature toggle cart.orderFulfillment nothing changes for existing shops.

OrderProductQuantityCalculator needs the OrderReturnRepository as new constructor argument.