diff --git a/data/definition/oms-01.xsd b/data/definition/oms-01.xsd index de0f64d1..46de37d1 100644 --- a/data/definition/oms-01.xsd +++ b/data/definition/oms-01.xsd @@ -42,6 +42,7 @@ + diff --git a/src/Spryker/Zed/Oms/Business/Checker/OrderChecker.php b/src/Spryker/Zed/Oms/Business/Checker/OrderChecker.php new file mode 100644 index 00000000..99bc9b1d --- /dev/null +++ b/src/Spryker/Zed/Oms/Business/Checker/OrderChecker.php @@ -0,0 +1,64 @@ +flagChecker = $flagChecker; + $this->omsRepository = $omsRepository; + } + + /** + * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer + * @param string $flag + * + * @return bool + */ + public function areOrderItemsSatisfiedByFlag(OrderTransfer $orderTransfer, string $flag): bool + { + $orderItemFilterTransfer = $this->createOrderItemFilterTransfer($orderTransfer); + $itemTransfers = $this->omsRepository->getOrderItems($orderItemFilterTransfer); + + return $this->flagChecker->hasOrderItemsFlag($itemTransfers, $flag); + } + + /** + * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer + * + * @return \Generated\Shared\Transfer\OrderItemFilterTransfer + */ + protected function createOrderItemFilterTransfer(OrderTransfer $orderTransfer): OrderItemFilterTransfer + { + $orderItemFilterTransfer = new OrderItemFilterTransfer(); + if ($orderTransfer->getOrderReference()) { + return $orderItemFilterTransfer->addOrderReference($orderTransfer->getOrderReferenceOrFail()); + } + + return $orderItemFilterTransfer->addSalesOrderId($orderTransfer->getIdSalesOrderOrFail()); + } +} diff --git a/src/Spryker/Zed/Oms/Business/Checker/OrderCheckerInterface.php b/src/Spryker/Zed/Oms/Business/Checker/OrderCheckerInterface.php new file mode 100644 index 00000000..81675627 --- /dev/null +++ b/src/Spryker/Zed/Oms/Business/Checker/OrderCheckerInterface.php @@ -0,0 +1,21 @@ +createFlagChecker(), + $this->getRepository(), + ); + } + /** * @return \Spryker\Zed\Oms\Dependency\Facade\OmsToMailInterface */ diff --git a/src/Spryker/Zed/Oms/Business/OmsFacade.php b/src/Spryker/Zed/Oms/Business/OmsFacade.php index 027eea43..cf73f86b 100644 --- a/src/Spryker/Zed/Oms/Business/OmsFacade.php +++ b/src/Spryker/Zed/Oms/Business/OmsFacade.php @@ -875,4 +875,21 @@ public function sendOrderStatusChangedMessage(int $idSalesOrder): void { $this->getFactory()->createOrderStatusChangedMessageSender()->sendMessage($idSalesOrder); } + + /** + * {@inheritDoc} + * + * @api + * + * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer + * @param string $flag + * + * @return bool + */ + public function areOrderItemsSatisfiedByFlag(OrderTransfer $orderTransfer, string $flag): bool + { + return $this->getFactory() + ->createOrderChecker() + ->areOrderItemsSatisfiedByFlag($orderTransfer, $flag); + } } diff --git a/src/Spryker/Zed/Oms/Business/OmsFacadeInterface.php b/src/Spryker/Zed/Oms/Business/OmsFacadeInterface.php index 6688d8d2..9c3f5a2f 100644 --- a/src/Spryker/Zed/Oms/Business/OmsFacadeInterface.php +++ b/src/Spryker/Zed/Oms/Business/OmsFacadeInterface.php @@ -805,4 +805,20 @@ public function setOrderIsCancellableByItemState(array $orderTransfers): array; * @return void */ public function sendOrderStatusChangedMessage(int $idSalesOrder): void; + + /** + * Specification: + * - Expects `OrderTransfer.orderReference` to be set. + * - Requires `OrderTransfer.idSalesOrder` to be set if `OrderTransfer.orderReference` is not provided. + * - Reads order items from Persistence. + * - Checks if all order items are satisfied by provided flag. + * + * @api + * + * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer + * @param string $flag + * + * @return bool + */ + public function areOrderItemsSatisfiedByFlag(OrderTransfer $orderTransfer, string $flag): bool; } diff --git a/tests/SprykerTest/Zed/Oms/Business/OmsFacade/AreOrderItemsSatisfiedByFlagTest.php b/tests/SprykerTest/Zed/Oms/Business/OmsFacade/AreOrderItemsSatisfiedByFlagTest.php new file mode 100644 index 00000000..7a14a1b5 --- /dev/null +++ b/tests/SprykerTest/Zed/Oms/Business/OmsFacade/AreOrderItemsSatisfiedByFlagTest.php @@ -0,0 +1,130 @@ +tester->configureTestStateMachine([OmsBusinessTester::DEFAULT_OMS_PROCESS_NAME]); + } + + /** + * @return void + */ + public function testShouldReturnTrueWhenOrderReferenceIsProvidedAndAllOrderItemsSatisfiedByFlag(): void + { + // Arrange + $orderTransfer = $this->tester->createOrderByStateMachineProcessName(OmsBusinessTester::DEFAULT_OMS_PROCESS_NAME); + $itemTransfer = $orderTransfer->getItems(); + $this->tester->setItemState($itemTransfer->offsetGet(0)->getIdSalesOrderItem(), static::ORDER_ITEM_STATUS_CANCELLED); + $this->tester->setItemState($itemTransfer->offsetGet(1)->getIdSalesOrderItem(), static::ORDER_ITEM_STATUS_CANCELLED); + + // Act + $result = $this->tester->getFacade()->areOrderItemsSatisfiedByFlag( + (new OrderTransfer())->setOrderReference($orderTransfer->getOrderReferenceOrFail()), + static::ORDER_ITEM_FLAGGED_EXCLUDE_FROM_CUSTOMER, + ); + + // Assert + $this->assertTrue($result); + } + + /** + * @return void + */ + public function testShouldReturnTrueWhenIdSalesOrderIsProvidedAndAllOrderItemsSatisfiedByFlag(): void + { + // Arrange + $orderTransfer = $this->tester->createOrderByStateMachineProcessName(OmsBusinessTester::DEFAULT_OMS_PROCESS_NAME); + $itemTransfer = $orderTransfer->getItems(); + $this->tester->setItemState($itemTransfer->offsetGet(0)->getIdSalesOrderItem(), static::ORDER_ITEM_STATUS_CANCELLED); + $this->tester->setItemState($itemTransfer->offsetGet(1)->getIdSalesOrderItem(), static::ORDER_ITEM_STATUS_CANCELLED); + + // Act + $result = $this->tester->getFacade()->areOrderItemsSatisfiedByFlag( + (new OrderTransfer())->setIdSalesOrder($orderTransfer->getIdSalesOrderOrFail()), + static::ORDER_ITEM_FLAGGED_EXCLUDE_FROM_CUSTOMER, + ); + + // Assert + $this->assertTrue($result); + } + + /** + * @return void + */ + public function testShouldReturnFalseWhenAtLeastOneOrderItemIsNotSatisfiedByFlag(): void + { + // Arrange + $orderTransfer = $this->tester->createOrderByStateMachineProcessName(OmsBusinessTester::DEFAULT_OMS_PROCESS_NAME); + $itemTransfer = $orderTransfer->getItems(); + $this->tester->setItemState($itemTransfer->offsetGet(0)->getIdSalesOrderItem(), static::ORDER_ITEM_STATUS_CANCELLED); + + // Act + $result = $this->tester->getFacade()->areOrderItemsSatisfiedByFlag( + $orderTransfer, + static::ORDER_ITEM_FLAGGED_EXCLUDE_FROM_CUSTOMER, + ); + + // Assert + $this->assertFalse($result); + } + + /** + * @return void + */ + public function testThrowsExceptionWhenOrderReferenceAndIdSalesOrderAreNotProvided(): void + { + // Assert + $this->expectException(NullValueException::class); + $this->expectExceptionMessage(sprintf('Property "idSalesOrder" of transfer `%s` is null.', OrderTransfer::class)); + + // Act + $result = $this->tester->getFacade()->areOrderItemsSatisfiedByFlag( + new OrderTransfer(), + static::ORDER_ITEM_FLAGGED_EXCLUDE_FROM_CUSTOMER, + ); + } +}