diff --git a/src/Spryker/Shared/Oms/Transfer/oms.transfer.xml b/src/Spryker/Shared/Oms/Transfer/oms.transfer.xml index 5e582592..c51aa46d 100644 --- a/src/Spryker/Shared/Oms/Transfer/oms.transfer.xml +++ b/src/Spryker/Shared/Oms/Transfer/oms.transfer.xml @@ -288,4 +288,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Spryker/Zed/Oms/Business/OmsFacade.php b/src/Spryker/Zed/Oms/Business/OmsFacade.php index ad44d454..a98c264c 100644 --- a/src/Spryker/Zed/Oms/Business/OmsFacade.php +++ b/src/Spryker/Zed/Oms/Business/OmsFacade.php @@ -12,6 +12,8 @@ use Generated\Shared\Transfer\OmsCheckTimeoutsQueryCriteriaTransfer; use Generated\Shared\Transfer\OmsStateCollectionTransfer; use Generated\Shared\Transfer\OrderItemFilterTransfer; +use Generated\Shared\Transfer\OrderMatrixCollectionTransfer; +use Generated\Shared\Transfer\OrderMatrixCriteriaTransfer; use Generated\Shared\Transfer\OrderTransfer; use Generated\Shared\Transfer\ReservationRequestTransfer; use Generated\Shared\Transfer\ReservationResponseTransfer; @@ -469,6 +471,8 @@ public function triggerEventForOneItem($eventId, $orderItem, array $logContext, * * @api * + * @deprecated Use {@link \Spryker\Zed\Oms\Business\OmsFacadeInterface::getOrderMatrixCollection()} instead. + * * @return array */ public function getOrderItemMatrix() @@ -476,6 +480,32 @@ public function getOrderItemMatrix() return $this->getFactory()->createUtilOrderItemMatrix()->getMatrix(); } + /** + * {@inheritDoc} + * + * @api + * + * @param \Generated\Shared\Transfer\OrderMatrixCriteriaTransfer $orderMatrixCriteriaTransfer + * + * @return \Generated\Shared\Transfer\OrderMatrixCollectionTransfer + */ + public function getOrderMatrixCollection(OrderMatrixCriteriaTransfer $orderMatrixCriteriaTransfer): OrderMatrixCollectionTransfer + { + return $this->getRepository()->getOrderMatrixCollection($orderMatrixCriteriaTransfer); + } + + /** + * {@inheritDoc} + * + * @api + * + * @return array + */ + public function getProcessNamesIndexedByIdOmsOrderProcess(): array + { + return $this->getRepository()->getProcessNamesIndexedByIdOmsOrderProcess(); + } + /** * {@inheritDoc} * diff --git a/src/Spryker/Zed/Oms/Business/OmsFacadeInterface.php b/src/Spryker/Zed/Oms/Business/OmsFacadeInterface.php index 0c4fc618..456351f6 100644 --- a/src/Spryker/Zed/Oms/Business/OmsFacadeInterface.php +++ b/src/Spryker/Zed/Oms/Business/OmsFacadeInterface.php @@ -12,6 +12,8 @@ use Generated\Shared\Transfer\OmsCheckTimeoutsQueryCriteriaTransfer; use Generated\Shared\Transfer\OmsStateCollectionTransfer; use Generated\Shared\Transfer\OrderItemFilterTransfer; +use Generated\Shared\Transfer\OrderMatrixCollectionTransfer; +use Generated\Shared\Transfer\OrderMatrixCriteriaTransfer; use Generated\Shared\Transfer\OrderTransfer; use Generated\Shared\Transfer\ReservationRequestTransfer; use Generated\Shared\Transfer\ReservationResponseTransfer; @@ -454,10 +456,34 @@ public function triggerEventForOneItem($eventId, $orderItem, array $logContext, * * @api * + * @deprecated Use {@link \Spryker\Zed\Oms\Business\OmsFacadeInterface::getOrderMatrixCollection()} instead. + * * @return array */ public function getOrderItemMatrix(); + /** + * Specification: + * - Retrieves a batch of matrix order items based on provided criteria filter. + * + * @api + * + * @param \Generated\Shared\Transfer\OrderMatrixCriteriaTransfer $orderMatrixCriteriaTransfer + * + * @return \Generated\Shared\Transfer\OrderMatrixCollectionTransfer + */ + public function getOrderMatrixCollection(OrderMatrixCriteriaTransfer $orderMatrixCriteriaTransfer): OrderMatrixCollectionTransfer; + + /** + * Specification: + * - Retrieves all active processes from the database. + * + * @api + * + * @return array + */ + public function getProcessNamesIndexedByIdOmsOrderProcess(): array; + /** * Specification: * - Reads all order manual event from persistence diff --git a/src/Spryker/Zed/Oms/Business/Util/OrderItemMatrix.php b/src/Spryker/Zed/Oms/Business/Util/OrderItemMatrix.php index 14c1548f..7b41aa6d 100644 --- a/src/Spryker/Zed/Oms/Business/Util/OrderItemMatrix.php +++ b/src/Spryker/Zed/Oms/Business/Util/OrderItemMatrix.php @@ -12,6 +12,9 @@ use Spryker\Zed\Oms\Persistence\OmsQueryContainerInterface; use Spryker\Zed\Oms\Persistence\OmsRepositoryInterface; +/** + * @deprecated Will be removed without replacement. + */ class OrderItemMatrix { /** diff --git a/src/Spryker/Zed/Oms/Persistence/OmsPersistenceFactory.php b/src/Spryker/Zed/Oms/Persistence/OmsPersistenceFactory.php index 88573537..95d0e870 100644 --- a/src/Spryker/Zed/Oms/Persistence/OmsPersistenceFactory.php +++ b/src/Spryker/Zed/Oms/Persistence/OmsPersistenceFactory.php @@ -19,6 +19,8 @@ use Orm\Zed\Sales\Persistence\SpySalesOrderItemQuery; use Spryker\Zed\Kernel\Persistence\AbstractPersistenceFactory; use Spryker\Zed\Oms\OmsDependencyProvider; +use Spryker\Zed\Oms\Persistence\Propel\Indexer\ProcessIndexer; +use Spryker\Zed\Oms\Persistence\Propel\Indexer\ProcessIndexerInterface; use Spryker\Zed\Oms\Persistence\Propel\Mapper\OmsMapper; use Spryker\Zed\Oms\Persistence\Propel\Mapper\OrderItemMapper; use Spryker\Zed\Oms\Persistence\Propel\Mapper\OrderItemMapperInterface; @@ -128,8 +130,6 @@ public function getSalesQueryContainer() } /** - * @deprecated Use {@link \Spryker\Zed\Oms\Persistence\OmsPersistenceFactory::getSalesQueryContainer()} to get the required query instead. - * * @return \Orm\Zed\Sales\Persistence\SpySalesOrderItemQuery */ public function getSalesOrderItemPropelQuery(): SpySalesOrderItemQuery @@ -144,4 +144,12 @@ public function createOmsMapper(): OmsMapper { return new OmsMapper(); } + + /** + * @return \Spryker\Zed\Oms\Persistence\Propel\Indexer\ProcessIndexerInterface + */ + public function createProcessIndexer(): ProcessIndexerInterface + { + return new ProcessIndexer(); + } } diff --git a/src/Spryker/Zed/Oms/Persistence/OmsQueryContainer.php b/src/Spryker/Zed/Oms/Persistence/OmsQueryContainer.php index 37428cda..b10d9346 100644 --- a/src/Spryker/Zed/Oms/Persistence/OmsQueryContainer.php +++ b/src/Spryker/Zed/Oms/Persistence/OmsQueryContainer.php @@ -559,6 +559,8 @@ public function querySalesOrderById($idOrder) * * @api * + * @deprecated Will be removed without replacement. + * * @param array $activeProcesses * * @return \Orm\Zed\Oms\Persistence\SpyOmsOrderProcessQuery @@ -576,6 +578,8 @@ public function queryActiveProcesses(array $activeProcesses) * * @api * + * @deprecated Will be removed without replacement. + * * @param array $orderItemStates * * @return \Orm\Zed\Oms\Persistence\SpyOmsOrderItemStateQuery diff --git a/src/Spryker/Zed/Oms/Persistence/OmsQueryContainerInterface.php b/src/Spryker/Zed/Oms/Persistence/OmsQueryContainerInterface.php index 6cdea53c..53b1a0f3 100644 --- a/src/Spryker/Zed/Oms/Persistence/OmsQueryContainerInterface.php +++ b/src/Spryker/Zed/Oms/Persistence/OmsQueryContainerInterface.php @@ -198,6 +198,8 @@ public function querySalesOrderById($idOrder); * * @api * + * @deprecated Will be removed without replacement. + * * @param array $activeProcesses * * @return \Orm\Zed\Oms\Persistence\SpyOmsOrderProcessQuery @@ -210,6 +212,8 @@ public function queryActiveProcesses(array $activeProcesses); * * @api * + * @deprecated Will be removed without replacement. + * * @param array $orderItemStates * * @return \Orm\Zed\Oms\Persistence\SpyOmsOrderItemStateQuery diff --git a/src/Spryker/Zed/Oms/Persistence/OmsRepository.php b/src/Spryker/Zed/Oms/Persistence/OmsRepository.php index ade956b6..49e34717 100644 --- a/src/Spryker/Zed/Oms/Persistence/OmsRepository.php +++ b/src/Spryker/Zed/Oms/Persistence/OmsRepository.php @@ -7,8 +7,11 @@ namespace Spryker\Zed\Oms\Persistence; +use DateTime; use Generated\Shared\Transfer\OmsProductReservationTransfer; use Generated\Shared\Transfer\OrderItemFilterTransfer; +use Generated\Shared\Transfer\OrderMatrixCollectionTransfer; +use Generated\Shared\Transfer\OrderMatrixCriteriaTransfer; use Generated\Shared\Transfer\ReservationRequestTransfer; use Generated\Shared\Transfer\ReservationResponseTransfer; use Generated\Shared\Transfer\SalesOrderItemStateAggregationTransfer; @@ -27,12 +30,69 @@ */ class OmsRepository extends AbstractRepository implements OmsRepositoryInterface { + /** + * @var string + */ + protected const ITEMS_COUNT = 'itemsCount'; + + /** + * @var string + */ + protected const DATE_WINDOW = 'dateWindow'; + /** * @var string */ protected const COL_PRODUCT_RESERVATION_TOTAL_QUANTITY = 'productReservationTotalQuantity'; /** + * @var string + */ + protected const STATE_NAME = 'stateName'; + + /** + * @var string + */ + protected const PROCESS_NAME = 'processName'; + + /** + * @var string + */ + protected const DATE_CASE_EXPRESSION = "(CASE WHEN %s > '%s' THEN 'day' WHEN %s > '%s' THEN 'week' ELSE 'other' END)"; + + /** + * @var string + */ + protected const DATE_FORMAT = 'Y-m-d H:i:s'; + + /** + * @var string + */ + protected const FK_OMS_ORDER_ITEM_STATE = 'fk_oms_order_item_state'; + + /** + * @var string + */ + protected const FK_OMS_ORDER_PROCESS = 'fk_oms_order_process'; + + /** + * @var string + */ + protected const SUBQUERY_ALIAS = 'sub'; + + /** + * @var string + */ + protected const SUBQUERY_FIELD_PLACEHOLDER = '%s.%s'; + + /** + * @var string + */ + protected const COUNT_ALL_KEYWORD = 'COUNT(*)'; + + /** + * @deprecated Use {@link \Spryker\Zed\Oms\Persistence\OmsRepositoryInterface::getOrderMatrixCollection()} instead. + * * @param array $processIds * @param array $stateBlackList * @@ -49,6 +109,104 @@ public function getMatrixOrderItems(array $processIds, array $stateBlackList): a ->mapOrderItemMatrix($orderItemsMatrixResult->getArrayCopy()); } + /** + * @param \Generated\Shared\Transfer\OrderMatrixCriteriaTransfer $orderMatrixCriteriaTransfer + * + * @return \Generated\Shared\Transfer\OrderMatrixCollectionTransfer + */ + public function getOrderMatrixCollection(OrderMatrixCriteriaTransfer $orderMatrixCriteriaTransfer): OrderMatrixCollectionTransfer + { + $subQuery = $this->getOrderMatrixSubquery($orderMatrixCriteriaTransfer); + + $query = $this->getFactory() + ->getSalesOrderItemPropelQuery() + ->addSelectQuery($subQuery, static::SUBQUERY_ALIAS) + ->select([ + sprintf(static::SUBQUERY_FIELD_PLACEHOLDER, static::SUBQUERY_ALIAS, static::FK_OMS_ORDER_ITEM_STATE), + sprintf(static::SUBQUERY_FIELD_PLACEHOLDER, static::SUBQUERY_ALIAS, static::FK_OMS_ORDER_PROCESS), + ]) + ->withColumn(static::COUNT_ALL_KEYWORD, static::ITEMS_COUNT) + ->withColumn(sprintf(static::SUBQUERY_FIELD_PLACEHOLDER, static::SUBQUERY_ALIAS, static::DATE_WINDOW), static::DATE_WINDOW) + ->withColumn(sprintf(static::SUBQUERY_FIELD_PLACEHOLDER, static::SUBQUERY_ALIAS, static::PROCESS_NAME), static::PROCESS_NAME) + ->withColumn(sprintf(static::SUBQUERY_FIELD_PLACEHOLDER, static::SUBQUERY_ALIAS, static::STATE_NAME), static::STATE_NAME) + ->groupBy([ + sprintf(static::SUBQUERY_FIELD_PLACEHOLDER, static::SUBQUERY_ALIAS, static::FK_OMS_ORDER_ITEM_STATE), + sprintf(static::SUBQUERY_FIELD_PLACEHOLDER, static::SUBQUERY_ALIAS, static::FK_OMS_ORDER_PROCESS), + ]) + ->addGroupByColumn(sprintf(static::SUBQUERY_FIELD_PLACEHOLDER, static::SUBQUERY_ALIAS, static::DATE_WINDOW)); + + $orderItemEntities = $query->find()->getArrayCopy(); + + return $this->getFactory() + ->createOrderItemMapper() + ->mapSalesOrderItemEntitiesToOrderMatrixCollectionTransfer($orderItemEntities, new OrderMatrixCollectionTransfer()); + } + + /** + * @return array + */ + public function getProcessNamesIndexedByIdOmsOrderProcess(): array + { + $activeProcesses = $this->getFactory() + ->getConfig() + ->getActiveProcesses(); + $query = $this->getFactory() + ->createOmsOrderProcessQuery(); + + $processEntities = $query->filterByName($activeProcesses, Criteria::IN) + ->find() + ->getArrayCopy(); + + return $this->getFactory() + ->createProcessIndexer() + ->getProcessNamesIndexedByIdOmsOrderProcess($processEntities); + } + + /** + * @param \Generated\Shared\Transfer\OrderMatrixCriteriaTransfer $orderMatrixCriteriaTransfer + * + * @return \Orm\Zed\Sales\Persistence\SpySalesOrderItemQuery + */ + protected function getOrderMatrixSubquery(OrderMatrixCriteriaTransfer $orderMatrixCriteriaTransfer): SpySalesOrderItemQuery + { + /** @var \Orm\Zed\Sales\Persistence\SpySalesOrderItemQuery $subQuery */ + $subQuery = $this->getFactory() + ->getSalesOrderItemPropelQuery(); + + /** @var \Orm\Zed\Sales\Persistence\SpySalesOrderItemQuery $subQuery */ + $subQuery = $subQuery->filterByFkOmsOrderProcess($orderMatrixCriteriaTransfer->getOrderMatrixConditions()->getProcessIds(), Criteria::IN) + ->useStateQuery() + ->withColumn(SpyOmsOrderItemStateTableMap::COL_NAME, static::STATE_NAME) + ->endUse(); + + $subQuery->useProcessQuery() + ->withColumn(SpyOmsOrderProcessTableMap::COL_NAME, static::PROCESS_NAME) + ->endUse() + ->withColumn(sprintf( + static::DATE_CASE_EXPRESSION, + SpySalesOrderItemTableMap::COL_LAST_STATE_CHANGE, + (new DateTime('-1 day'))->format(static::DATE_FORMAT), + SpySalesOrderItemTableMap::COL_LAST_STATE_CHANGE, + (new DateTime('-7 day'))->format(static::DATE_FORMAT), + ), static::DATE_WINDOW) + ->withColumn(SpySalesOrderItemTableMap::COL_FK_OMS_ORDER_PROCESS, static::FK_OMS_ORDER_PROCESS) + ->withColumn(SpySalesOrderItemTableMap::COL_FK_OMS_ORDER_ITEM_STATE, static::FK_OMS_ORDER_ITEM_STATE); + + if ($orderMatrixCriteriaTransfer->getPagination() && $orderMatrixCriteriaTransfer->getPagination()->getLimit()) { + $subQuery->limit($orderMatrixCriteriaTransfer->getPagination()->getLimit()) + ->offset($orderMatrixCriteriaTransfer->getPagination()->getOffset()); + } + $stateBlackList = $this->getFactory()->getConfig()->getStateBlacklist(); + + if ($stateBlackList) { + $subQuery->useStateQuery() + ->filterByName($stateBlackList, Criteria::NOT_IN) + ->endUse(); + } + + return $subQuery; + } + /** * @param array $stateNames * @param string $sku diff --git a/src/Spryker/Zed/Oms/Persistence/OmsRepositoryInterface.php b/src/Spryker/Zed/Oms/Persistence/OmsRepositoryInterface.php index 80d07bbc..b0b3f38e 100644 --- a/src/Spryker/Zed/Oms/Persistence/OmsRepositoryInterface.php +++ b/src/Spryker/Zed/Oms/Persistence/OmsRepositoryInterface.php @@ -9,6 +9,8 @@ use Generated\Shared\Transfer\OmsProductReservationTransfer; use Generated\Shared\Transfer\OrderItemFilterTransfer; +use Generated\Shared\Transfer\OrderMatrixCollectionTransfer; +use Generated\Shared\Transfer\OrderMatrixCriteriaTransfer; use Generated\Shared\Transfer\ReservationRequestTransfer; use Generated\Shared\Transfer\StoreTransfer; use Spryker\DecimalObject\Decimal; @@ -16,6 +18,8 @@ interface OmsRepositoryInterface { /** + * @deprecated Use {@link \Spryker\Zed\Oms\Persistence\OmsRepositoryInterface::getOrderMatrixCollection()} instead. + * * @param array $processIds * @param array $stateBlackList * @@ -23,6 +27,18 @@ interface OmsRepositoryInterface */ public function getMatrixOrderItems(array $processIds, array $stateBlackList): array; + /** + * @param \Generated\Shared\Transfer\OrderMatrixCriteriaTransfer $orderMatrixCriteriaTransfer + * + * @return \Generated\Shared\Transfer\OrderMatrixCollectionTransfer + */ + public function getOrderMatrixCollection(OrderMatrixCriteriaTransfer $orderMatrixCriteriaTransfer): OrderMatrixCollectionTransfer; + + /** + * @return array + */ + public function getProcessNamesIndexedByIdOmsOrderProcess(): array; + /** * @param array $stateNames * @param string $sku diff --git a/src/Spryker/Zed/Oms/Persistence/Propel/Indexer/ProcessIndexer.php b/src/Spryker/Zed/Oms/Persistence/Propel/Indexer/ProcessIndexer.php new file mode 100644 index 00000000..66effdcd --- /dev/null +++ b/src/Spryker/Zed/Oms/Persistence/Propel/Indexer/ProcessIndexer.php @@ -0,0 +1,26 @@ + $omsOrderProcessEntities + * + * @return array + */ + public function getProcessNamesIndexedByIdOmsOrderProcess(array $omsOrderProcessEntities): array + { + $processes = []; + foreach ($omsOrderProcessEntities as $omsOrderProcessEntity) { + $processes[$omsOrderProcessEntity->getIdOmsOrderProcess()] = $omsOrderProcessEntity->getName(); + } + + return $processes; + } +} diff --git a/src/Spryker/Zed/Oms/Persistence/Propel/Indexer/ProcessIndexerInterface.php b/src/Spryker/Zed/Oms/Persistence/Propel/Indexer/ProcessIndexerInterface.php new file mode 100644 index 00000000..f8647126 --- /dev/null +++ b/src/Spryker/Zed/Oms/Persistence/Propel/Indexer/ProcessIndexerInterface.php @@ -0,0 +1,18 @@ + $omsOrderProcessEntities + * + * @return array + */ + public function getProcessNamesIndexedByIdOmsOrderProcess(array $omsOrderProcessEntities): array; +} diff --git a/src/Spryker/Zed/Oms/Persistence/Propel/Mapper/OrderItemMapper.php b/src/Spryker/Zed/Oms/Persistence/Propel/Mapper/OrderItemMapper.php index db1ddd48..61cc3cf4 100644 --- a/src/Spryker/Zed/Oms/Persistence/Propel/Mapper/OrderItemMapper.php +++ b/src/Spryker/Zed/Oms/Persistence/Propel/Mapper/OrderItemMapper.php @@ -9,6 +9,8 @@ use Generated\Shared\Transfer\ItemStateTransfer; use Generated\Shared\Transfer\ItemTransfer; +use Generated\Shared\Transfer\OrderMatrixCollectionTransfer; +use Generated\Shared\Transfer\OrderMatrixTransfer; use Orm\Zed\Sales\Persistence\Map\SpySalesOrderItemTableMap; use Propel\Runtime\Collection\Collection; use Spryker\Zed\Oms\Persistence\OmsQueryContainer; @@ -82,4 +84,38 @@ public function mapSalesOrderItemEntityCollectionToOrderItemTransfers( return $itemTransfers; } + + /** + * @param array> $orderItemEntities + * @param \Generated\Shared\Transfer\OrderMatrixCollectionTransfer $orderMatrixCollectionTransfer + * + * @return \Generated\Shared\Transfer\OrderMatrixCollectionTransfer + */ + public function mapSalesOrderItemEntitiesToOrderMatrixCollectionTransfer( + array $orderItemEntities, + OrderMatrixCollectionTransfer $orderMatrixCollectionTransfer + ): OrderMatrixCollectionTransfer { + foreach ($orderItemEntities as $orderItemEntity) { + $orderMatrixTransfer = $this->mapSalesOrderItemEntityToOrderMatrixTransfer($orderItemEntity, new OrderMatrixTransfer()); + $orderMatrixCollectionTransfer->addOrderMatrix($orderMatrixTransfer); + } + + return $orderMatrixCollectionTransfer; + } + + /** + * @param array $orderItemEntity + * @param \Generated\Shared\Transfer\OrderMatrixTransfer $orderMatrixTransfer + * + * @return \Generated\Shared\Transfer\OrderMatrixTransfer + */ + protected function mapSalesOrderItemEntityToOrderMatrixTransfer(array $orderItemEntity, OrderMatrixTransfer $orderMatrixTransfer): OrderMatrixTransfer + { + return $orderMatrixTransfer->setProcessName((string)$orderItemEntity['processName']) + ->setStateName((string)$orderItemEntity['stateName']) + ->setDateWindow((string)$orderItemEntity['dateWindow']) + ->setItemsCount((int)$orderItemEntity['itemsCount']) + ->setIdProcess((int)$orderItemEntity['sub.fk_oms_order_process']) + ->setIdState((int)$orderItemEntity['sub.fk_oms_order_item_state']); + } } diff --git a/src/Spryker/Zed/Oms/Persistence/Propel/Mapper/OrderItemMapperInterface.php b/src/Spryker/Zed/Oms/Persistence/Propel/Mapper/OrderItemMapperInterface.php index 5f394c2d..f0b67536 100644 --- a/src/Spryker/Zed/Oms/Persistence/Propel/Mapper/OrderItemMapperInterface.php +++ b/src/Spryker/Zed/Oms/Persistence/Propel/Mapper/OrderItemMapperInterface.php @@ -7,6 +7,7 @@ namespace Spryker\Zed\Oms\Persistence\Propel\Mapper; +use Generated\Shared\Transfer\OrderMatrixCollectionTransfer; use Propel\Runtime\Collection\Collection; interface OrderItemMapperInterface @@ -35,4 +36,15 @@ public function mapOmsOrderItemStateHistoryEntityCollectionToItemStateHistoryTra public function mapSalesOrderItemEntityCollectionToOrderItemTransfers( Collection $salesOrderItemEntityCollection ): array; + + /** + * @param array> $orderItemEntities + * @param \Generated\Shared\Transfer\OrderMatrixCollectionTransfer $orderMatrixCollectionTransfer + * + * @return \Generated\Shared\Transfer\OrderMatrixCollectionTransfer + */ + public function mapSalesOrderItemEntitiesToOrderMatrixCollectionTransfer( + array $orderItemEntities, + OrderMatrixCollectionTransfer $orderMatrixCollectionTransfer + ): OrderMatrixCollectionTransfer; } diff --git a/tests/SprykerTest/Zed/Oms/Business/OmsFacade/GetOrderMatrixCollectionTest.php b/tests/SprykerTest/Zed/Oms/Business/OmsFacade/GetOrderMatrixCollectionTest.php new file mode 100644 index 00000000..aee1caa8 --- /dev/null +++ b/tests/SprykerTest/Zed/Oms/Business/OmsFacade/GetOrderMatrixCollectionTest.php @@ -0,0 +1,159 @@ +tester::DEFAULT_OMS_PROCESS_NAME; + $this->tester->createOrderByStateMachineProcessName($testStateMachineProcessName); + $orderProcessEntity = SpyOmsOrderProcessQuery::create()->filterByName($testStateMachineProcessName)->findOne(); + $orderMatrixCriteriaTransfer = (new OrderMatrixCriteriaTransfer()) + ->setOrderMatrixConditions((new OrderMatrixConditionsTransfer())->addProcessId($orderProcessEntity->getIdOmsOrderProcess())); + + // Act + $result = $this->omsFacade->getOrderMatrixCollection($orderMatrixCriteriaTransfer); + + // Assert + $this->tester->assertCount(1, $result->getOrderMatrices()); + } + + /** + * @return void + */ + public function testGetOrderMatrixCollectionShouldReturnEmptyArrayWhenNoItemsMatchCriteria(): void + { + // Arrange + $orderMatrixCriteriaTransfer = (new OrderMatrixCriteriaTransfer()) + ->setPagination((new PaginationTransfer())->setLimit(10)) + ->setOrderMatrixConditions(new OrderMatrixConditionsTransfer()); + + // Act + $result = $this->omsFacade->getOrderMatrixCollection($orderMatrixCriteriaTransfer); + + // Assert + $this->assertEmpty($result->getOrderMatrices()); + } + + /** + * @return void + */ + public function testGetOrderMatrixCollectionShouldCorrectlyApplyBlackListStates(): void + { + // Arrange + $testStateMachineProcessName = $this->tester::DEFAULT_OMS_PROCESS_NAME; + $firstOrder = $this->tester->createOrderByStateMachineProcessName($testStateMachineProcessName); + $this->tester->createOrderByStateMachineProcessName($testStateMachineProcessName); + $firstOrderItems = SpySalesOrderItemQuery::create()->filterByFkSalesOrder($firstOrder->getIdSalesOrder())->find(); + $cancelledState = SpyOmsOrderItemStateQuery::create()->filterByName('cancelled')->findOne(); + if (!$cancelledState) { + $cancelledState = new SpyOmsOrderItemState(); + $cancelledState->setName('cancelled'); + $cancelledState->save(); + } + foreach ($firstOrderItems as $item) { + $item->setFkOmsOrderItemState($cancelledState->getIdOmsOrderItemState()); + $item->save(); + } + $orderProcessEntity = SpyOmsOrderProcessQuery::create()->filterByName($testStateMachineProcessName)->findOne(); + $orderMatrixCriteriaTransfer = new OrderMatrixCriteriaTransfer(); + $orderMatrixConditionsTransfer = (new OrderMatrixConditionsTransfer()) + ->addProcessId($orderProcessEntity->getIdOmsOrderProcess()); + $configMock = $this->createMock(OmsConfig::class); + $configMock->method('getStateBlacklist')->willReturn([$cancelledState->getName()]); + $factory = new OmsPersistenceFactory(); + $factory->setConfig($configMock); + $repository = new OmsRepository(); + $repository->setFactory($factory); + $omsFacade = new OmsFacade(); + $omsFacade->setRepository($repository); + $orderMatrixCriteriaTransfer->setOrderMatrixConditions($orderMatrixConditionsTransfer); + // Act + $result = $omsFacade->getOrderMatrixCollection($orderMatrixCriteriaTransfer); + + // Assert + $this->tester->assertCount(1, $result->getOrderMatrices()); + } + + /** + * @return void + */ + public function testGetProcessNamesIndexedByIdOmsOrderProcessShouldReturnArrayOfProcessesWithExpectedAmount(): void + { + // Arrange + $testStateMachineProcessName = $this->tester::DEFAULT_OMS_PROCESS_NAME; + $this->tester->createOrderByStateMachineProcessName($testStateMachineProcessName); + + // Act + $result = $this->omsFacade->getProcessNamesIndexedByIdOmsOrderProcess(); + + // Assert + $this->tester->assertIsArray($result); + $this->tester->assertContains($testStateMachineProcessName, $result); + } + + /** + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + $this->omsFacade = $this->tester->getFacade(); + $this->tester->resetReservedStatesCache(); + $this->tester->configureTestStateMachine(['Test01']); + } + + /** + * @return void + */ + protected function tearDown(): void + { + $this->tester->resetReservedStatesCache(); + } +}