Skip to content

Commit

Permalink
CC-34501: Initialisation Order Amendment Process (#11163)
Browse files Browse the repository at this point in the history
CC-34501 Initialisation Order Amendment Process
  • Loading branch information
dmiseev authored Nov 15, 2024
1 parent a903bfd commit 3c47c5b
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 0 deletions.
1 change: 1 addition & 0 deletions data/definition/oms-01.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<xsd:enumeration value="ready for invoice"/>
<xsd:enumeration value="waiting for export"/>
<xsd:enumeration value="cancellable"/>
<xsd:enumeration value="amendable"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
Expand Down
64 changes: 64 additions & 0 deletions src/Spryker/Zed/Oms/Business/Checker/OrderChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace Spryker\Zed\Oms\Business\Checker;

use Generated\Shared\Transfer\OrderItemFilterTransfer;
use Generated\Shared\Transfer\OrderTransfer;
use Spryker\Zed\Oms\Persistence\OmsRepositoryInterface;

class OrderChecker implements OrderCheckerInterface
{
/**
* @var \Spryker\Zed\Oms\Business\Checker\FlagCheckerInterface
*/
protected FlagCheckerInterface $flagChecker;

/**
* @var \Spryker\Zed\Oms\Persistence\OmsRepositoryInterface
*/
protected OmsRepositoryInterface $omsRepository;

/**
* @param \Spryker\Zed\Oms\Business\Checker\FlagCheckerInterface $flagChecker
* @param \Spryker\Zed\Oms\Persistence\OmsRepositoryInterface $omsRepository
*/
public function __construct(FlagCheckerInterface $flagChecker, OmsRepositoryInterface $omsRepository)
{
$this->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());
}
}
21 changes: 21 additions & 0 deletions src/Spryker/Zed/Oms/Business/Checker/OrderCheckerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace Spryker\Zed\Oms\Business\Checker;

use Generated\Shared\Transfer\OrderTransfer;

interface OrderCheckerInterface
{
/**
* @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
* @param string $flag
*
* @return bool
*/
public function areOrderItemsSatisfiedByFlag(OrderTransfer $orderTransfer, string $flag): bool;
}
13 changes: 13 additions & 0 deletions src/Spryker/Zed/Oms/Business/OmsBusinessFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Spryker\Zed\Kernel\Business\AbstractBusinessFactory;
use Spryker\Zed\Oms\Business\Checker\FlagChecker;
use Spryker\Zed\Oms\Business\Checker\FlagCheckerInterface;
use Spryker\Zed\Oms\Business\Checker\OrderChecker;
use Spryker\Zed\Oms\Business\Checker\OrderCheckerInterface;
use Spryker\Zed\Oms\Business\Expander\OrderAggregatedItemStateExpander;
use Spryker\Zed\Oms\Business\Expander\OrderAggregatedItemStateExpanderInterface;
use Spryker\Zed\Oms\Business\Expander\OrderExpander;
Expand Down Expand Up @@ -458,6 +460,17 @@ public function createOrderItemStateExpander(): OrderItemStateExpanderInterface
);
}

/**
* @return \Spryker\Zed\Oms\Business\Checker\OrderCheckerInterface
*/
public function createOrderChecker(): OrderCheckerInterface
{
return new OrderChecker(
$this->createFlagChecker(),
$this->getRepository(),
);
}

/**
* @return \Spryker\Zed\Oms\Dependency\Facade\OmsToMailInterface
*/
Expand Down
17 changes: 17 additions & 0 deletions src/Spryker/Zed/Oms/Business/OmsFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
16 changes: 16 additions & 0 deletions src/Spryker/Zed/Oms/Business/OmsFacadeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace SprykerTest\Zed\Oms\Business\OmsFacade;

use Codeception\Test\Unit;
use Generated\Shared\Transfer\OrderTransfer;
use Spryker\Shared\Kernel\Transfer\Exception\NullValueException;
use SprykerTest\Zed\Oms\OmsBusinessTester;

/**
* Auto-generated group annotations
*
* @group SprykerTest
* @group Zed
* @group Oms
* @group Business
* @group OmsFacade
* @group AreOrderItemsSatisfiedByFlagTest
* Add your own group annotations below this line
*/
class AreOrderItemsSatisfiedByFlagTest extends Unit
{
/**
* @var string
*/
protected const ORDER_ITEM_STATUS_CANCELLED = 'cancelled';

/**
* @var string
*/
protected const ORDER_ITEM_FLAGGED_EXCLUDE_FROM_CUSTOMER = 'exclude from customer';

/**
* @var \SprykerTest\Zed\Oms\OmsBusinessTester
*/
protected OmsBusinessTester $tester;

/**
* @return void
*/
protected function setUp(): void
{
parent::setUp();

$this->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,
);
}
}

0 comments on commit 3c47c5b

Please sign in to comment.