diff --git a/Classes/Domain/Order/Fulfillment/FeatureToggle.php b/Classes/Domain/Order/Fulfillment/FeatureToggle.php new file mode 100644 index 00000000..a61a8f67 --- /dev/null +++ b/Classes/Domain/Order/Fulfillment/FeatureToggle.php @@ -0,0 +1,21 @@ + + */ + public array $products = [], + public string $carrier = '', + public string $trackingNumber = '', + public string $trackingUrl = '', + public ?DateTimeImmutable $shippedDate = null, + public string $note = '', + /** + * A replacement may exceed the ordered count by the returned count. + */ + public bool $isReplacement = false, + public int $uid = 0, + ) { + } +} diff --git a/Classes/Domain/Order/Fulfillment/ShipmentProduct.php b/Classes/Domain/Order/Fulfillment/ShipmentProduct.php new file mode 100644 index 00000000..12d8fe50 --- /dev/null +++ b/Classes/Domain/Order/Fulfillment/ShipmentProduct.php @@ -0,0 +1,22 @@ +findPidOfOrderItem($shipment->orderItemUid); + $timestamp = $this->getCurrentTimestamp(); + + return $this->connectionPool->getConnectionForTable(self::ORDER_SHIPMENT_TABLE_NAME)->transactional( + static function (Connection $connection) use ($shipment, $orderItemPid, $timestamp): int { + $connection->insert( + self::ORDER_SHIPMENT_TABLE_NAME, + [ + 'pid' => $orderItemPid, + 'tstamp' => $timestamp, + 'crdate' => $timestamp, + 'item' => $shipment->orderItemUid, + 'status' => $shipment->status, + 'carrier' => $shipment->carrier, + 'tracking_number' => $shipment->trackingNumber, + 'tracking_url' => $shipment->trackingUrl, + 'shipped_date' => $shipment->shippedDate?->getTimestamp() ?? 0, + 'note' => $shipment->note, + 'replacement' => (int) $shipment->isReplacement, + 'products' => count($shipment->products), + ], + [ + 'pid' => Connection::PARAM_INT, + 'tstamp' => Connection::PARAM_INT, + 'crdate' => Connection::PARAM_INT, + 'item' => Connection::PARAM_INT, + 'shipped_date' => Connection::PARAM_INT, + 'replacement' => Connection::PARAM_INT, + 'products' => Connection::PARAM_INT, + ] + ); + $shipmentUid = (int) $connection->lastInsertId(); + + foreach ($shipment->products as $shipmentProduct) { + $connection->insert( + self::ORDER_SHIPMENT_PRODUCT_TABLE_NAME, + [ + 'pid' => $orderItemPid, + 'tstamp' => $timestamp, + 'crdate' => $timestamp, + 'shipment' => $shipmentUid, + 'order_product' => $shipmentProduct->orderProductUid, + 'count' => $shipmentProduct->count, + ], + [ + 'pid' => Connection::PARAM_INT, + 'tstamp' => Connection::PARAM_INT, + 'crdate' => Connection::PARAM_INT, + 'shipment' => Connection::PARAM_INT, + 'order_product' => Connection::PARAM_INT, + 'count' => Connection::PARAM_INT, + ] + ); + } + + $queryBuilder = $connection->createQueryBuilder(); + $queryBuilder + ->update(self::ORDER_ITEM_TABLE_NAME) + ->set('shipments', $queryBuilder->quoteIdentifier('shipments') . ' + 1', false) + ->where( + $queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($shipment->orderItemUid, Connection::PARAM_INT)) + ) + ->executeStatement() + ; + + return $shipmentUid; + } + ); + } + + public function findByUid(int $shipmentUid): ?Shipment + { + $queryBuilder = $this->connectionPool->getQueryBuilderForTable(self::ORDER_SHIPMENT_TABLE_NAME); + $shipmentRow = $queryBuilder + ->select('*') + ->from(self::ORDER_SHIPMENT_TABLE_NAME) + ->where( + $queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($shipmentUid, Connection::PARAM_INT)) + ) + ->executeQuery() + ->fetchAssociative() + ; + + if ($shipmentRow === false) { + return null; + } + + $shipmentProducts = $this->findShipmentProductsByShipmentUids([$shipmentUid]); + + return self::mapRowToShipment($shipmentRow, $shipmentProducts[$shipmentUid] ?? []); + } + + /** + * @return list oldest first + */ + public function findByOrderItem(int $orderItemUid): array + { + $queryBuilder = $this->connectionPool->getQueryBuilderForTable(self::ORDER_SHIPMENT_TABLE_NAME); + $shipmentRows = $queryBuilder + ->select('*') + ->from(self::ORDER_SHIPMENT_TABLE_NAME) + ->where( + $queryBuilder->expr()->eq('item', $queryBuilder->createNamedParameter($orderItemUid, Connection::PARAM_INT)) + ) + ->orderBy('uid', 'ASC') + ->executeQuery() + ->fetchAllAssociative() + ; + + $shipmentUids = array_map(static fn (array $shipmentRow): int => self::toInt($shipmentRow['uid'] ?? 0), $shipmentRows); + $shipmentProducts = $this->findShipmentProductsByShipmentUids($shipmentUids); + + return array_map( + static fn (array $shipmentRow): Shipment => self::mapRowToShipment( + $shipmentRow, + $shipmentProducts[self::toInt($shipmentRow['uid'] ?? 0)] ?? [] + ), + $shipmentRows + ); + } + + /** + * @param list $shipmentUids + * + * @return array> by shipment uid + */ + private function findShipmentProductsByShipmentUids(array $shipmentUids): array + { + if ($shipmentUids === []) { + return []; + } + + $queryBuilder = $this->connectionPool->getQueryBuilderForTable(self::ORDER_SHIPMENT_PRODUCT_TABLE_NAME); + $shipmentProductRows = $queryBuilder + ->select('*') + ->from(self::ORDER_SHIPMENT_PRODUCT_TABLE_NAME) + ->where( + $queryBuilder->expr()->in( + 'shipment', + $queryBuilder->createNamedParameter($shipmentUids, Connection::PARAM_INT_ARRAY) + ) + ) + ->orderBy('uid', 'ASC') + ->executeQuery() + ->fetchAllAssociative() + ; + + $shipmentProductsByShipmentUid = []; + foreach ($shipmentProductRows as $shipmentProductRow) { + $shipmentUid = self::toInt($shipmentProductRow['shipment'] ?? 0); + $shipmentProductsByShipmentUid[$shipmentUid][] = new ShipmentProduct( + orderProductUid: self::toInt($shipmentProductRow['order_product'] ?? 0), + count: self::toInt($shipmentProductRow['count'] ?? 0), + uid: self::toInt($shipmentProductRow['uid'] ?? 0), + ); + } + + return $shipmentProductsByShipmentUid; + } + + private function findPidOfOrderItem(int $orderItemUid): int + { + $queryBuilder = $this->connectionPool->getQueryBuilderForTable(self::ORDER_ITEM_TABLE_NAME); + $orderItemPid = $queryBuilder + ->select('pid') + ->from(self::ORDER_ITEM_TABLE_NAME) + ->where( + $queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($orderItemUid, Connection::PARAM_INT)) + ) + ->executeQuery() + ->fetchOne() + ; + + if ($orderItemPid === false) { + throw new InvalidArgumentException('The order item ' . $orderItemUid . ' does not exist.', 1795347001); + } + + return self::toInt($orderItemPid); + } + + /** + * @param array $shipmentRow + * @param list $shipmentProducts + */ + private static function mapRowToShipment(array $shipmentRow, array $shipmentProducts): Shipment + { + $shippedTimestamp = self::toInt($shipmentRow['shipped_date'] ?? 0); + + return new Shipment( + orderItemUid: self::toInt($shipmentRow['item'] ?? 0), + status: self::toString($shipmentRow['status'] ?? ''), + products: $shipmentProducts, + carrier: self::toString($shipmentRow['carrier'] ?? ''), + trackingNumber: self::toString($shipmentRow['tracking_number'] ?? ''), + trackingUrl: self::toString($shipmentRow['tracking_url'] ?? ''), + shippedDate: $shippedTimestamp > 0 ? new DateTimeImmutable('@' . $shippedTimestamp) : null, + note: self::toString($shipmentRow['note'] ?? ''), + isReplacement: (bool) self::toInt($shipmentRow['replacement'] ?? 0), + uid: self::toInt($shipmentRow['uid'] ?? 0), + ); + } + + private function getCurrentTimestamp(): int + { + $now = $this->context->getPropertyFromAspect('date', 'full'); + + return ($now instanceof DateTimeImmutable ? $now : new DateTimeImmutable())->getTimestamp(); + } + + private static function toInt(mixed $value): int + { + return is_numeric($value) ? (int) $value : 0; + } + + private static function toString(mixed $value): string + { + return is_scalar($value) ? (string) $value : ''; + } +} diff --git a/Classes/Domain/Order/Fulfillment/ShipmentStatus.php b/Classes/Domain/Order/Fulfillment/ShipmentStatus.php new file mode 100644 index 00000000..d7e916a0 --- /dev/null +++ b/Classes/Domain/Order/Fulfillment/ShipmentStatus.php @@ -0,0 +1,28 @@ + $_LLL . ':tx_cart_domain_model_order_shipping.status.in_process', 'after' => [ShippingStatus::ON_HOLD], ], + ShippingStatus::PARTIALLY_SHIPPED => [ + 'label' => $_LLL . ':tx_cart_domain_model_order_shipping.status.partially_shipped', + 'after' => [ShippingStatus::IN_PROCESS], + 'before' => [ShippingStatus::SHIPPED], + ], ShippingStatus::SHIPPED => [ 'label' => $_LLL . ':tx_cart_domain_model_order_shipping.status.shipped', 'after' => [ShippingStatus::IN_PROCESS], ], + ShippingStatus::NOT_REQUIRED => [ + 'label' => $_LLL . ':tx_cart_domain_model_order_shipping.status.not_required', + 'after' => [ShippingStatus::SHIPPED], + ], + ], + 'shipment' => [ + ShipmentStatus::OPEN => [ + 'label' => $_LLL . ':tx_cart_domain_model_order_shipment.status.open', + ], + ShipmentStatus::IN_PROCESS => [ + 'label' => $_LLL . ':tx_cart_domain_model_order_shipment.status.in_process', + 'after' => [ShipmentStatus::OPEN], + ], + ShipmentStatus::SHIPPED => [ + 'label' => $_LLL . ':tx_cart_domain_model_order_shipment.status.shipped', + 'after' => [ShipmentStatus::IN_PROCESS], + ], + ShipmentStatus::DELIVERED => [ + 'label' => $_LLL . ':tx_cart_domain_model_order_shipment.status.delivered', + 'after' => [ShipmentStatus::SHIPPED], + ], + ShipmentStatus::CANCELED => [ + 'label' => $_LLL . ':tx_cart_domain_model_order_shipment.status.canceled', + 'after' => [ShipmentStatus::DELIVERED], + ], ], ]; diff --git a/Configuration/TCA/tx_cart_domain_model_order_item.php b/Configuration/TCA/tx_cart_domain_model_order_item.php index ae823619..c9975af5 100644 --- a/Configuration/TCA/tx_cart_domain_model_order_item.php +++ b/Configuration/TCA/tx_cart_domain_model_order_item.php @@ -39,6 +39,7 @@ discounts, payment, shipping, + shipments, --palette--;' . $_LLL . ':tx_cart_domain_model_order_item.palettes.documents;pdfs', ], ], @@ -453,6 +454,29 @@ ], ], ], + 'shipments' => [ + 'exclude' => 0, + 'label' => $_LLL . ':tx_cart_domain_model_order_item.shipments', + 'config' => [ + 'type' => 'inline', + 'readOnly' => 1, + 'foreign_table' => 'tx_cart_domain_model_order_shipment', + 'foreign_field' => 'item', + 'maxitems' => 9999, + 'appearance' => [ + 'collapseAll' => 1, + 'enabledControls' => [ + 'info' => false, + 'new' => false, + 'dragdrop' => false, + 'sort' => false, + 'hide' => false, + 'delete' => false, + 'localize' => false, + ], + ], + ], + ], 'discounts' => [ 'exclude' => 0, 'label' => $_LLL . ':tx_cart_domain_model_order_item.discounts', diff --git a/Configuration/TCA/tx_cart_domain_model_order_shipment.php b/Configuration/TCA/tx_cart_domain_model_order_shipment.php new file mode 100644 index 00000000..34a9f88f --- /dev/null +++ b/Configuration/TCA/tx_cart_domain_model_order_shipment.php @@ -0,0 +1,124 @@ + [ + 'title' => $_LLL . ':tx_cart_domain_model_order_shipment', + 'label' => 'uid', + 'label_alt' => 'status, tracking_number', + 'label_alt_force' => true, + 'tstamp' => 'tstamp', + 'crdate' => 'crdate', + + 'versioningWS' => true, + 'hideTable' => true, + 'delete' => 'deleted', + 'enablecolumns' => [], + 'iconfile' => 'EXT:cart/Resources/Public/Icons/Order/Shipping.svg', + ], + 'types' => [ + '1' => [ + 'showitem' => 'status, replacement, carrier, tracking_number, tracking_url, shipped_date, note, products', + ], + ], + 'columns' => [ + 'status' => [ + 'label' => $_LLL . ':tx_cart_domain_model_order_shipment.status', + 'config' => [ + 'type' => 'select', + 'renderType' => 'selectSingle', + 'readOnly' => 1, + 'items' => [], + 'itemsProcFunc' => OrderStatusItemsProcFunc::class . '->addItems', + 'itemsProcConfig' => [ + 'serviceType' => ServiceType::Shipment->value, + ], + 'size' => 1, + 'maxitems' => 1, + ], + ], + 'replacement' => [ + 'label' => $_LLL . ':tx_cart_domain_model_order_shipment.replacement', + 'config' => [ + 'type' => 'check', + 'readOnly' => 1, + ], + ], + 'carrier' => [ + 'label' => $_LLL . ':tx_cart_domain_model_order_shipment.carrier', + 'config' => [ + 'type' => 'input', + 'readOnly' => 1, + 'size' => 30, + ], + ], + 'tracking_number' => [ + 'label' => $_LLL . ':tx_cart_domain_model_order_shipment.tracking_number', + 'config' => [ + 'type' => 'input', + 'readOnly' => 1, + 'size' => 30, + ], + ], + 'tracking_url' => [ + 'label' => $_LLL . ':tx_cart_domain_model_order_shipment.tracking_url', + 'config' => [ + 'type' => 'input', + 'readOnly' => 1, + 'size' => 30, + ], + ], + 'shipped_date' => [ + 'label' => $_LLL . ':tx_cart_domain_model_order_shipment.shipped_date', + 'config' => [ + 'type' => 'datetime', + 'readOnly' => 1, + 'default' => 0, + ], + ], + 'note' => [ + 'label' => $_LLL . ':tx_cart_domain_model_order_shipment.note', + 'config' => [ + 'type' => 'text', + 'readOnly' => 1, + 'cols' => 40, + 'rows' => 5, + ], + ], + 'products' => [ + 'label' => $_LLL . ':tx_cart_domain_model_order_shipment.products', + 'config' => [ + 'type' => 'inline', + 'readOnly' => 1, + 'foreign_table' => 'tx_cart_domain_model_order_shipment_product', + 'foreign_field' => 'shipment', + 'maxitems' => 9999, + 'appearance' => [ + 'collapseAll' => 1, + 'enabledControls' => [ + 'info' => false, + 'new' => false, + 'dragdrop' => false, + 'sort' => false, + 'hide' => false, + 'delete' => false, + 'localize' => false, + ], + ], + ], + ], + 'item' => [ + 'config' => [ + 'type' => 'passthrough', + ], + ], + ], +]; diff --git a/Configuration/TCA/tx_cart_domain_model_order_shipment_product.php b/Configuration/TCA/tx_cart_domain_model_order_shipment_product.php new file mode 100644 index 00000000..82c8eec0 --- /dev/null +++ b/Configuration/TCA/tx_cart_domain_model_order_shipment_product.php @@ -0,0 +1,57 @@ + [ + 'title' => $_LLL . ':tx_cart_domain_model_order_shipment_product', + 'label' => 'order_product', + 'label_alt' => 'count', + 'label_alt_force' => true, + 'tstamp' => 'tstamp', + 'crdate' => 'crdate', + + 'versioningWS' => true, + 'hideTable' => true, + 'delete' => 'deleted', + 'enablecolumns' => [], + 'iconfile' => 'EXT:cart/Resources/Public/Icons/Order/Product.svg', + ], + 'types' => [ + '1' => [ + 'showitem' => 'order_product, count', + ], + ], + 'columns' => [ + 'order_product' => [ + 'label' => $_LLL . ':tx_cart_domain_model_order_shipment_product.order_product', + 'config' => [ + 'type' => 'select', + 'renderType' => 'selectSingle', + 'readOnly' => 1, + 'foreign_table' => 'tx_cart_domain_model_order_product', + 'size' => 1, + 'minitems' => 0, + 'maxitems' => 1, + ], + ], + 'count' => [ + 'label' => $_LLL . ':tx_cart_domain_model_order_shipment_product.count', + 'config' => [ + 'type' => 'number', + 'readOnly' => 1, + 'size' => 8, + 'default' => 0, + ], + ], + 'shipment' => [ + 'config' => [ + 'type' => 'passthrough', + ], + ], + ], +]; diff --git a/Documentation/Changelog/12.9/Feature-902-OrderShipments.rst b/Documentation/Changelog/12.9/Feature-902-OrderShipments.rst new file mode 100644 index 00000000..7a63b97d --- /dev/null +++ b/Documentation/Changelog/12.9/Feature-902-OrderShipments.rst @@ -0,0 +1,53 @@ +.. include:: ../../Includes.rst.txt + +===================================== +Feature: #902 - Shipments with lines +===================================== + +See `Issue 902 `__ + +Description +=========== + +An order can store several shipments (partial deliveries). This change adds +the data model only; creating shipments, deriving the shipping status of the +order and the backend UI follow in later versions. + +* ``tx_cart_domain_model_order_shipment``: order item, status, carrier, + tracking number, tracking URL, shipped date, note and whether the shipment + is a replacement for returned products. +* ``tx_cart_domain_model_order_shipment_product``: the products of a shipment, + the shipped count of an ordered product. + +Shipments are no Extbase models. They are only written by services in +database transactions, so integrators cannot change them by accident. The +readonly objects ``\Extcode\Cart\Domain\Order\Fulfillment\Shipment`` and +``\Extcode\Cart\Domain\Order\Fulfillment\ShipmentProduct`` are read and stored by +``\Extcode\Cart\Domain\Order\Fulfillment\ShipmentRepository``. The TCA of both +tables is read only, the shipments are shown in the order item record. + +New status (see :ref:`developer-order-status`): + +* The new type ``shipment`` with the status ``open``, ``in_process``, + ``shipped``, ``delivered`` and ``canceled`` + (``\Extcode\Cart\Domain\Order\Fulfillment\ShipmentStatus``). Extensions + register further shipment status in :file:`Configuration/OrderStatus.php`. +* The shipping status ``partially_shipped`` and ``not_required`` (no ordered + product is shippable). + +The new feature toggle ``cart.orderFulfillment`` is disabled by default. It +will enable the backend UI for shipments and the derived shipping status of +the order; for now it has no effect. + +Impact +====== + +The database schema has to be updated. Nothing changes for existing orders. + +The select of the shipping status in the backend module offers the new status +``partially_shipped`` and ``not_required``. + +``\Extcode\Cart\Domain\Order\Status\ServiceType`` has the new case +``Shipment``. Code that matches all cases of the enum has to handle it. + +.. index:: Backend, Database, PHP-API diff --git a/Documentation/Changelog/12.9/Index.rst b/Documentation/Changelog/12.9/Index.rst new file mode 100644 index 00000000..6b85e89a --- /dev/null +++ b/Documentation/Changelog/12.9/Index.rst @@ -0,0 +1,24 @@ +.. include:: ../../Includes.rst.txt + +12.9 Changes +============ + +**Table of contents** + +.. contents:: + :local: + :depth: 1 + +12.9.0 +------ + +Features +~~~~~~~~ + +* :doc:`#902 - Shipments with lines ` + +.. toctree:: + :hidden: + :glob: + + *-* diff --git a/Documentation/Changelog/Index.rst b/Documentation/Changelog/Index.rst index 32898e25..6366a417 100644 --- a/Documentation/Changelog/Index.rst +++ b/Documentation/Changelog/Index.rst @@ -10,6 +10,7 @@ ChangeLog :maxdepth: 5 :titlesonly: + 12.9/Index 12.8/Index 12.7/Index 12.6/Index diff --git a/Documentation/Developer/OrderStatus/Index.rst b/Documentation/Developer/OrderStatus/Index.rst index d28ca808..6dc78a8f 100644 --- a/Documentation/Developer/OrderStatus/Index.rst +++ b/Documentation/Developer/OrderStatus/Index.rst @@ -6,19 +6,23 @@ Payment and shipping status =========================== -Every order has a payment status and a shipping status. EXT:cart registers -these status: +Every order has a payment status and a shipping status, every shipment +(partial delivery) of an order has its own status. EXT:cart registers these +status: -========= ================================================= -Service Status in their order -========= ================================================= +========= ================================================================== +Type Status in their order +========= ================================================================== payment ``open``, ``pending``, ``paid``, ``canceled`` -shipping ``open``, ``on_hold``, ``in_process``, ``shipped`` -========= ================================================= +shipping ``open``, ``on_hold``, ``in_process``, ``partially_shipped``, + ``shipped``, ``not_required`` +shipment ``open``, ``in_process``, ``shipped``, ``delivered``, ``canceled`` +========= ================================================================== The identifiers of these status are available as constants of -``\Extcode\Cart\Domain\Order\Status\PaymentStatus`` and -``\Extcode\Cart\Domain\Order\Status\ShippingStatus``, e.g. +``\Extcode\Cart\Domain\Order\Status\PaymentStatus``, +``\Extcode\Cart\Domain\Order\Status\ShippingStatus`` and +``\Extcode\Cart\Domain\Order\Fulfillment\ShipmentStatus``, e.g. ``PaymentStatus::PAID``. The constants only name the status of EXT:cart. The payment and the shipping of an order tell whether they have one of the @@ -44,7 +48,7 @@ Register own status =================== Add a file :file:`Configuration/OrderStatus.php` to your extension. It -returns the status by service type (``payment`` or ``shipping``) and +returns the status by type (``payment``, ``shipping`` or ``shipment``) and identifier. The identifier is stored in the database, it consists of lower case letters, digits and underscores and starts with a letter. diff --git a/Resources/Private/Language/locallang_db.xlf b/Resources/Private/Language/locallang_db.xlf index c90f0c86..4e96bd66 100644 --- a/Resources/Private/Language/locallang_db.xlf +++ b/Resources/Private/Language/locallang_db.xlf @@ -174,6 +174,9 @@ Products + + Shipments + Discounts @@ -423,13 +426,78 @@ in process + + partially shipped + shipped + + not required + Note + + Shipment + + + Order + + + Status + + + open + + + in process + + + shipped + + + delivered + + + canceled + + + Carrier + + + Tracking Number + + + Tracking URL + + + Shipped Date + + + Note + + + Replacement + + + Products + + + + Shipped Product + + + Shipment + + + Ordered Product + + + Count + + Payment diff --git a/Tests/Functional/Controller/Backend/Order/OrderStatusTest.php b/Tests/Functional/Controller/Backend/Order/OrderStatusTest.php index 66301997..39059ca3 100644 --- a/Tests/Functional/Controller/Backend/Order/OrderStatusTest.php +++ b/Tests/Functional/Controller/Backend/Order/OrderStatusTest.php @@ -33,7 +33,9 @@ final class OrderStatusTest extends AbstractOrderStatusTestCase ShippingStatus::OPEN => 'open', ShippingStatus::ON_HOLD => 'on hold', ShippingStatus::IN_PROCESS => 'in process', + ShippingStatus::PARTIALLY_SHIPPED => 'partially shipped', ShippingStatus::SHIPPED => 'shipped', + ShippingStatus::NOT_REQUIRED => 'not required', ]; #[Test] diff --git a/Tests/Functional/Controller/Backend/Order/OwnOrderStatusTest.php b/Tests/Functional/Controller/Backend/Order/OwnOrderStatusTest.php index 0f07c171..83c9f97a 100644 --- a/Tests/Functional/Controller/Backend/Order/OwnOrderStatusTest.php +++ b/Tests/Functional/Controller/Backend/Order/OwnOrderStatusTest.php @@ -41,8 +41,10 @@ final class OwnOrderStatusTest extends AbstractOrderStatusTestCase ShippingStatus::ON_HOLD => 'on hold', ShippingStatus::IN_PROCESS => 'in process', 'packed' => 'Packed', + ShippingStatus::PARTIALLY_SHIPPED => 'partially shipped', ShippingStatus::SHIPPED => 'shipped', 'delivered' => 'Delivered', + ShippingStatus::NOT_REQUIRED => 'not required', ]; protected array $orderStatusExtensionsToLoad = [ diff --git a/Tests/Functional/Controller/Backend/Order/TcaOrderStatusTest.php b/Tests/Functional/Controller/Backend/Order/TcaOrderStatusTest.php index 07cbaa73..3f764fd5 100644 --- a/Tests/Functional/Controller/Backend/Order/TcaOrderStatusTest.php +++ b/Tests/Functional/Controller/Backend/Order/TcaOrderStatusTest.php @@ -38,7 +38,9 @@ final class TcaOrderStatusTest extends AbstractOrderStatusTestCase ShippingStatus::OPEN => 'open', ShippingStatus::ON_HOLD => 'on hold', ShippingStatus::IN_PROCESS => 'in process', + ShippingStatus::PARTIALLY_SHIPPED => 'partially shipped', ShippingStatus::SHIPPED => 'shipped', + ShippingStatus::NOT_REQUIRED => 'not required', 'delivered_via_tca' => 'Delivered via TCA', ]; diff --git a/Tests/Functional/Domain/Order/Fulfillment/ShipmentRepositoryTest.php b/Tests/Functional/Domain/Order/Fulfillment/ShipmentRepositoryTest.php new file mode 100644 index 00000000..48cbba8e --- /dev/null +++ b/Tests/Functional/Domain/Order/Fulfillment/ShipmentRepositoryTest.php @@ -0,0 +1,296 @@ +importPHPDataSet(__DIR__ . '/../../../../Fixtures/BaseDatabase.php'); + (new PhpDataSet())->import([ + 'tx_cart_domain_model_order_item' => [ + ['uid' => 1, 'pid' => 105, 'order_number' => 'O-1', 'products' => 2], + ['uid' => 2, 'pid' => 105, 'order_number' => 'O-2', 'products' => 1], + ], + 'tx_cart_domain_model_order_product' => [ + ['uid' => 11, 'pid' => 105, 'item' => 1, 'sku' => 'book', 'count' => 3], + ['uid' => 12, 'pid' => 105, 'item' => 1, 'sku' => 'mug', 'count' => 1], + ['uid' => 21, 'pid' => 105, 'item' => 2, 'sku' => 'book', 'count' => 1], + ], + ]); + + $context = GeneralUtility::makeInstance(Context::class); + $context->setAspect('date', new DateTimeAspect(new DateTimeImmutable(self::CURRENT_DATE))); + + $this->subject = new ShipmentRepository($this->getConnectionPool(), $context); + } + + #[Test] + public function addStoresTheShipmentWithItsProductsOnThePageOfTheOrder(): void + { + $shipmentUid = $this->subject->add(new Shipment( + orderItemUid: 1, + status: ShipmentStatus::SHIPPED, + products: [ + new ShipmentProduct(orderProductUid: 11, count: 2), + new ShipmentProduct(orderProductUid: 12, count: 1), + ], + carrier: 'DHL', + trackingNumber: '00340434161094042557', + trackingUrl: 'https://example.org/tracking/00340434161094042557', + shippedDate: new DateTimeImmutable('2026-11-23 15:00:00'), + note: 'first parcel', + )); + + $currentTimestamp = (new DateTimeImmutable(self::CURRENT_DATE))->getTimestamp(); + self::assertSame( + [ + [ + 'uid' => $shipmentUid, + 'pid' => 105, + 'tstamp' => $currentTimestamp, + 'crdate' => $currentTimestamp, + 'item' => 1, + 'status' => ShipmentStatus::SHIPPED, + 'replacement' => 0, + 'carrier' => 'DHL', + 'tracking_number' => '00340434161094042557', + 'tracking_url' => 'https://example.org/tracking/00340434161094042557', + 'shipped_date' => (new DateTimeImmutable('2026-11-23 15:00:00'))->getTimestamp(), + 'note' => 'first parcel', + 'products' => 2, + ], + ], + $this->fetchRows( + self::ORDER_SHIPMENT_TABLE_NAME, + ['uid', 'pid', 'tstamp', 'crdate', 'item', 'status', 'replacement', 'carrier', 'tracking_number', 'tracking_url', 'shipped_date', 'note', 'products'] + ) + ); + self::assertSame( + [ + ['pid' => 105, 'shipment' => $shipmentUid, 'order_product' => 11, 'count' => 2], + ['pid' => 105, 'shipment' => $shipmentUid, 'order_product' => 12, 'count' => 1], + ], + $this->fetchRows(self::ORDER_SHIPMENT_PRODUCT_TABLE_NAME, ['pid', 'shipment', 'order_product', 'count']) + ); + } + + #[Test] + public function addCountsTheShipmentsOfTheOrderItem(): void + { + $this->subject->add(new Shipment(orderItemUid: 1, status: ShipmentStatus::OPEN)); + $this->subject->add(new Shipment(orderItemUid: 1, status: ShipmentStatus::OPEN)); + + self::assertSame( + [ + ['uid' => 1, 'shipments' => 2], + ['uid' => 2, 'shipments' => 0], + ], + $this->fetchRows('tx_cart_domain_model_order_item', ['uid', 'shipments']) + ); + } + + #[Test] + public function addThrowsAnExceptionForAnUnknownOrderItem(): void + { + $this->expectExceptionObject(new InvalidArgumentException('The order item 3 does not exist.', 1795347001)); + + $this->subject->add(new Shipment(orderItemUid: 3, status: ShipmentStatus::OPEN)); + } + + #[Test] + public function findByUidReturnsTheStoredShipmentWithItsProducts(): void + { + $shipment = new Shipment( + orderItemUid: 1, + status: ShipmentStatus::IN_PROCESS, + products: [ + new ShipmentProduct(orderProductUid: 11, count: 1), + ], + carrier: 'DPD', + note: 'replacement for a damaged book', + isReplacement: true, + ); + $shipmentUid = $this->subject->add($shipment); + + $foundShipment = $this->subject->findByUid($shipmentUid); + + self::assertInstanceOf(Shipment::class, $foundShipment); + self::assertSame($shipmentUid, $foundShipment->uid); + self::assertSame(1, $foundShipment->orderItemUid); + self::assertSame(ShipmentStatus::IN_PROCESS, $foundShipment->status); + self::assertSame('DPD', $foundShipment->carrier); + self::assertSame('', $foundShipment->trackingNumber); + self::assertSame('', $foundShipment->trackingUrl); + self::assertNull($foundShipment->shippedDate); + self::assertSame('replacement for a damaged book', $foundShipment->note); + self::assertTrue($foundShipment->isReplacement); + self::assertCount(1, $foundShipment->products); + self::assertSame(11, $foundShipment->products[0]->orderProductUid); + self::assertSame(1, $foundShipment->products[0]->count); + self::assertGreaterThan(0, $foundShipment->products[0]->uid); + } + + #[Test] + public function findByUidReturnsTheShippedDate(): void + { + $shipmentUid = $this->subject->add(new Shipment( + orderItemUid: 1, + status: ShipmentStatus::SHIPPED, + shippedDate: new DateTimeImmutable('2026-11-23 15:00:00'), + )); + + self::assertSame( + (new DateTimeImmutable('2026-11-23 15:00:00'))->getTimestamp(), + $this->subject->findByUid($shipmentUid)?->shippedDate?->getTimestamp() + ); + } + + #[Test] + public function findByUidReturnsNullForAnUnknownOrDeletedShipment(): void + { + $shipmentUid = $this->subject->add(new Shipment(orderItemUid: 1, status: ShipmentStatus::OPEN)); + $this->getConnectionPool() + ->getConnectionForTable(self::ORDER_SHIPMENT_TABLE_NAME) + ->update(self::ORDER_SHIPMENT_TABLE_NAME, ['deleted' => 1], ['uid' => $shipmentUid]) + ; + + self::assertNull($this->subject->findByUid($shipmentUid)); + self::assertNull($this->subject->findByUid($shipmentUid + 1)); + } + + #[Test] + public function findByOrderItemReturnsTheShipmentsOfTheOrderInTheOrderTheyWereCreated(): void + { + $firstShipmentUid = $this->subject->add(new Shipment( + orderItemUid: 1, + status: ShipmentStatus::SHIPPED, + products: [new ShipmentProduct(orderProductUid: 11, count: 2)], + )); + $this->subject->add(new Shipment( + orderItemUid: 2, + status: ShipmentStatus::SHIPPED, + products: [new ShipmentProduct(orderProductUid: 21, count: 1)], + )); + $secondShipmentUid = $this->subject->add(new Shipment( + orderItemUid: 1, + status: ShipmentStatus::OPEN, + products: [ + new ShipmentProduct(orderProductUid: 11, count: 1), + new ShipmentProduct(orderProductUid: 12, count: 1), + ], + )); + + $shipments = $this->subject->findByOrderItem(1); + + self::assertSame([$firstShipmentUid, $secondShipmentUid], array_map(static fn (Shipment $shipment): int => $shipment->uid, $shipments)); + self::assertSame( + [[11 => 2], [11 => 1, 12 => 1]], + array_map( + static function (Shipment $shipment): array { + $countsByOrderProductUid = []; + foreach ($shipment->products as $shipmentProduct) { + $countsByOrderProductUid[$shipmentProduct->orderProductUid] = $shipmentProduct->count; + } + + return $countsByOrderProductUid; + }, + $shipments + ) + ); + } + + #[Test] + public function findByOrderItemSkipsDeletedShipmentsAndShipmentProducts(): void + { + $deletedShipmentUid = $this->subject->add(new Shipment(orderItemUid: 1, status: ShipmentStatus::OPEN)); + $keptShipmentUid = $this->subject->add(new Shipment( + orderItemUid: 1, + status: ShipmentStatus::OPEN, + products: [ + new ShipmentProduct(orderProductUid: 11, count: 1), + new ShipmentProduct(orderProductUid: 12, count: 1), + ], + )); + $connectionPool = $this->getConnectionPool(); + $connectionPool->getConnectionForTable(self::ORDER_SHIPMENT_TABLE_NAME) + ->update(self::ORDER_SHIPMENT_TABLE_NAME, ['deleted' => 1], ['uid' => $deletedShipmentUid]) + ; + $connectionPool->getConnectionForTable(self::ORDER_SHIPMENT_PRODUCT_TABLE_NAME) + ->update(self::ORDER_SHIPMENT_PRODUCT_TABLE_NAME, ['deleted' => 1], ['order_product' => 12]) + ; + + $shipments = $this->subject->findByOrderItem(1); + + self::assertCount(1, $shipments); + self::assertSame($keptShipmentUid, $shipments[0]->uid); + self::assertCount(1, $shipments[0]->products); + self::assertSame(11, $shipments[0]->products[0]->orderProductUid); + } + + #[Test] + public function findByOrderItemReturnsAnEmptyListForAnOrderWithoutShipments(): void + { + self::assertSame([], $this->subject->findByOrderItem(2)); + } + + /** + * @param list $fields + * + * @return list> + */ + private function fetchRows(string $table, array $fields): array + { + $queryBuilder = $this->getConnectionPool()->getQueryBuilderForTable($table); + $queryBuilder->getRestrictions()->removeAll(); + + return $queryBuilder + ->select(...$fields) + ->from($table) + ->orderBy('uid') + ->executeQuery() + ->fetchAllAssociative() + ; + } +} diff --git a/Tests/Functional/Hooks/OrderStatusItemsProcFuncTest.php b/Tests/Functional/Hooks/OrderStatusItemsProcFuncTest.php index 0109657d..7a9936dc 100644 --- a/Tests/Functional/Hooks/OrderStatusItemsProcFuncTest.php +++ b/Tests/Functional/Hooks/OrderStatusItemsProcFuncTest.php @@ -11,6 +11,7 @@ * LICENSE file that was distributed with this source code. */ +use Extcode\Cart\Domain\Order\Fulfillment\ShipmentStatus; use Extcode\Cart\Domain\Order\Status\PaymentStatus; use Extcode\Cart\Domain\Order\Status\ShippingStatus; use Extcode\Cart\Hooks\OrderStatusItemsProcFunc; @@ -22,7 +23,7 @@ use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; /** - * The status field of the payment and the shipping in the TCA (e.g. the list + * The status field of the payment, the shipping and the shipment in the TCA (e.g. the list * module) contains the registered status of cart and of other extensions, * see Tests/Fixtures/cart_order_status/Configuration/OrderStatus.php. */ @@ -91,8 +92,20 @@ public static function tableDataProvider(): iterable ['label' => $cartLLL . 'tx_cart_domain_model_order_shipping.status.on_hold', 'value' => ShippingStatus::ON_HOLD], ['label' => $cartLLL . 'tx_cart_domain_model_order_shipping.status.in_process', 'value' => ShippingStatus::IN_PROCESS], ['label' => $ownLLL . 'shipping.status.packed', 'value' => 'packed'], + ['label' => $cartLLL . 'tx_cart_domain_model_order_shipping.status.partially_shipped', 'value' => ShippingStatus::PARTIALLY_SHIPPED], ['label' => $cartLLL . 'tx_cart_domain_model_order_shipping.status.shipped', 'value' => ShippingStatus::SHIPPED], ['label' => $ownLLL . 'shipping.status.delivered', 'value' => 'delivered'], + ['label' => $cartLLL . 'tx_cart_domain_model_order_shipping.status.not_required', 'value' => ShippingStatus::NOT_REQUIRED], + ], + ]; + yield 'shipment' => [ + 'table' => 'tx_cart_domain_model_order_shipment', + 'expectedItems' => [ + ['label' => $cartLLL . 'tx_cart_domain_model_order_shipment.status.open', 'value' => ShipmentStatus::OPEN], + ['label' => $cartLLL . 'tx_cart_domain_model_order_shipment.status.in_process', 'value' => ShipmentStatus::IN_PROCESS], + ['label' => $cartLLL . 'tx_cart_domain_model_order_shipment.status.shipped', 'value' => ShipmentStatus::SHIPPED], + ['label' => $cartLLL . 'tx_cart_domain_model_order_shipment.status.delivered', 'value' => ShipmentStatus::DELIVERED], + ['label' => $cartLLL . 'tx_cart_domain_model_order_shipment.status.canceled', 'value' => ShipmentStatus::CANCELED], ], ]; } diff --git a/Tests/Unit/Domain/Order/Status/OrderStatusConfigurationBuilderTest.php b/Tests/Unit/Domain/Order/Status/OrderStatusConfigurationBuilderTest.php index fb2824fe..95f1b0ae 100644 --- a/Tests/Unit/Domain/Order/Status/OrderStatusConfigurationBuilderTest.php +++ b/Tests/Unit/Domain/Order/Status/OrderStatusConfigurationBuilderTest.php @@ -24,7 +24,9 @@ final class OrderStatusConfigurationBuilderTest extends UnitTestCase { private const CART_PAYMENT_STATUS = ['open', 'pending', 'paid', 'canceled']; - private const CART_SHIPPING_STATUS = ['open', 'on_hold', 'in_process', 'shipped']; + private const CART_SHIPPING_STATUS = ['open', 'on_hold', 'in_process', 'partially_shipped', 'shipped', 'not_required']; + + private const CART_SHIPMENT_STATUS = ['open', 'in_process', 'shipped', 'delivered', 'canceled']; private OrderStatusConfigurationBuilder $subject; @@ -42,6 +44,7 @@ public function buildReturnsTheStatusOfCartInTheirOrder(): void self::assertSame(self::CART_PAYMENT_STATUS, array_keys($result['payment'])); self::assertSame(self::CART_SHIPPING_STATUS, array_keys($result['shipping'])); + self::assertSame(self::CART_SHIPMENT_STATUS, array_keys($result['shipment'])); self::assertSame( 'LLL:EXT:cart/Resources/Private/Language/locallang_db.xlf:tx_cart_domain_model_order_payment.status.paid', $result['payment']['paid'] @@ -49,10 +52,10 @@ public function buildReturnsTheStatusOfCartInTheirOrder(): void } #[Test] - public function buildReturnsBothServiceTypesWithoutAnyConfiguration(): void + public function buildReturnsAllServiceTypesWithoutAnyConfiguration(): void { self::assertSame( - ['payment' => [], 'shipping' => []], + ['payment' => [], 'shipping' => [], 'shipment' => []], $this->subject->build([]) ); } @@ -122,7 +125,7 @@ public static function ownStatusDataProvider(): iterable ], ], 'expectedPaymentStatus' => self::CART_PAYMENT_STATUS, - 'expectedShippingStatus' => ['ordered', 'open', 'on_hold', 'in_process', 'shipped'], + 'expectedShippingStatus' => ['ordered', 'open', 'on_hold', 'in_process', 'partially_shipped', 'shipped', 'not_required'], ]; yield 'shipping status between two status' => [ 'configurations' => [ @@ -133,7 +136,7 @@ public static function ownStatusDataProvider(): iterable ], ], 'expectedPaymentStatus' => self::CART_PAYMENT_STATUS, - 'expectedShippingStatus' => ['open', 'on_hold', 'in_process', 'packed', 'shipped'], + 'expectedShippingStatus' => ['open', 'on_hold', 'in_process', 'packed', 'partially_shipped', 'shipped', 'not_required'], ]; yield 'shipping status after the last status' => [ 'configurations' => [ @@ -144,7 +147,7 @@ public static function ownStatusDataProvider(): iterable ], ], 'expectedPaymentStatus' => self::CART_PAYMENT_STATUS, - 'expectedShippingStatus' => ['open', 'on_hold', 'in_process', 'shipped', 'delivered'], + 'expectedShippingStatus' => ['open', 'on_hold', 'in_process', 'partially_shipped', 'shipped', 'delivered', 'not_required'], ]; yield 'status at all positions of both service types' => [ 'configurations' => [ @@ -162,7 +165,7 @@ public static function ownStatusDataProvider(): iterable ], ], 'expectedPaymentStatus' => ['new', 'open', 'pending', 'paid', 'refunded', 'canceled', 'chargeback'], - 'expectedShippingStatus' => ['ordered', 'open', 'on_hold', 'in_process', 'packed', 'shipped', 'delivered'], + 'expectedShippingStatus' => ['ordered', 'open', 'on_hold', 'in_process', 'packed', 'partially_shipped', 'shipped', 'delivered', 'not_required'], ]; yield 'two status between the same two status' => [ 'configurations' => [ @@ -290,7 +293,7 @@ public static function invalidConfigurationDataProvider(): iterable yield 'unknown service type' => [ 'configurations' => ['my_ext' => ['invoice' => []]], 'expectedException' => new UnexpectedValueException( - 'The Configuration/OrderStatus.php of the package "my_ext" contains the unknown service type "invoice", allowed are "payment", "shipping".', + 'The Configuration/OrderStatus.php of the package "my_ext" contains the unknown service type "invoice", allowed are "payment", "shipping", "shipment".', 1795068607 ), ]; diff --git a/ext_localconf.php b/ext_localconf.php index 5b07419e..2de104fc 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -14,6 +14,7 @@ use Extcode\Cart\Controller\Cart\ProductController; use Extcode\Cart\Controller\Cart\ShippingController; use Extcode\Cart\Domain\Log\DatabaseWriter; +use Extcode\Cart\Domain\Order\Fulfillment\FeatureToggle as FulfillmentFeatureToggle; use Extcode\Cart\EventListener\Order\Finish\Email as FinishEmail; use Extcode\Cart\EventListener\Order\StatusChange\Mail as StatusChangeMail; use TYPO3\CMS\Core\Log\LogLevel; @@ -80,6 +81,13 @@ $GLOBALS['TYPO3_CONF_VARS'] = ArrayUtility::setValueByPath($configuration, $featurePath, false); } + // disabled by default, see FulfillmentFeatureToggle + $configuration = $GLOBALS['TYPO3_CONF_VARS']; + $featurePath = ['SYS', 'features', FulfillmentFeatureToggle::NAME]; + if (is_array($configuration) && !ArrayUtility::isValidPath($configuration, $featurePath)) { + $GLOBALS['TYPO3_CONF_VARS'] = ArrayUtility::setValueByPath($configuration, $featurePath, false); + } + // configure plugins ExtensionUtility::configurePlugin( 'Cart', diff --git a/ext_tables.sql b/ext_tables.sql index 9350655c..efbe671a 100644 --- a/ext_tables.sql +++ b/ext_tables.sql @@ -34,6 +34,7 @@ CREATE TABLE tx_cart_domain_model_order_item ( products int(11) unsigned DEFAULT '0' NOT NULL, discounts int(11) unsigned DEFAULT '0' NOT NULL, shipping int(11) unsigned DEFAULT '0', + shipments int(11) unsigned DEFAULT '0' NOT NULL, payment int(11) unsigned DEFAULT '0', comment text, @@ -318,3 +319,34 @@ CREATE TABLE tx_cart_domain_model_order_change ( PRIMARY KEY (uid), KEY item (item,type), ); + +# +# Table structure for table 'tx_cart_domain_model_order_shipment' +# +CREATE TABLE tx_cart_domain_model_order_shipment ( + item int(11) unsigned DEFAULT '0' NOT NULL, + + status varchar(255) DEFAULT '' NOT NULL, + replacement tinyint(4) unsigned DEFAULT '0' NOT NULL, + carrier varchar(255) DEFAULT '' NOT NULL, + tracking_number varchar(255) DEFAULT '' NOT NULL, + tracking_url varchar(2048) DEFAULT '' NOT NULL, + shipped_date int(11) unsigned DEFAULT '0' NOT NULL, + note text, + + products int(11) unsigned DEFAULT '0' NOT NULL, + + KEY item (item), +); + +# +# Table structure for table 'tx_cart_domain_model_order_shipment_product' +# +CREATE TABLE tx_cart_domain_model_order_shipment_product ( + shipment int(11) unsigned DEFAULT '0' NOT NULL, + order_product int(11) unsigned DEFAULT '0' NOT NULL, + count int(11) unsigned DEFAULT '0' NOT NULL, + + KEY shipment (shipment), + KEY order_product (order_product), +);