Skip to content

Commit

Permalink
Update order modifiers to fix data updates (#32)
Browse files Browse the repository at this point in the history
* Update order modifiers to fix data updates

* Fix coding standard issues

* Optimize order item configuration

* Add variables to reduce cyclomatic complexity
  • Loading branch information
ArjenMiedema authored Jul 19, 2024
1 parent 69181d4 commit 96eeb94
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 39 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/2.0.0.

## [Unreleased]

## [1.6.1] - 2024-07-09

### Fixed
- Order data of an existing order was overwritten
- Order items are only updated if they are not set yet

## [1.6.0] - 2024-04-18

### Changed
Expand Down
2 changes: 1 addition & 1 deletion src/Modifiers/Order/AbstractModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(

public function supports(mixed $entity): bool
{
return $entity instanceof ExternalOrderInterface;
return $entity instanceof ExternalOrderInterface && !$entity->getOrderId();
}

protected function getOrderEntity(ExternalOrderInterface $order): ?OrderInterface
Expand Down
5 changes: 5 additions & 0 deletions src/Modifiers/Order/AddOrderAttachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public function __construct(
);
}

public function supports(mixed $entity): bool
{
return $entity instanceof ExternalOrderInterface;
}

/**
* @param ExternalOrderInterface $model
* @param OrderInterface $result
Expand Down
5 changes: 5 additions & 0 deletions src/Modifiers/Order/CreateOrFetchOrderModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public function __construct(
);
}

public function supports(mixed $entity): bool
{
return $entity instanceof ExternalOrderInterface;
}

/**
* @param ExternalOrderInterface $model
* @param OrderInterface|null $result
Expand Down
8 changes: 6 additions & 2 deletions src/Modifiers/Order/SetCustomerData.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public function __construct(
);
}

public function supports(mixed $entity): bool
{
return parent::supports($entity) && !$entity->getOrderId();
}

/**
* @param ExternalOrderInterface $model
* @param OrderInterface $result
Expand All @@ -39,8 +44,7 @@ public function process(mixed $model, mixed $result): mixed
$customer = $this->customerRepository
->getById($model->getMagentoCustomerId());

$result->setExtCustomerId($model->getExternalCustomerId())
->setCustomerId($customer->getId())
$result->setCustomerId($customer->getId())
->setCustomerEmail($customer->getEmail())
->setCustomerPrefix($customer->getPrefix())
->setCustomerFirstname($customer->getFirstname())
Expand Down
43 changes: 43 additions & 0 deletions src/Modifiers/Order/SetExternalOrderData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* Copyright Youwe. All rights reserved.
* https://www.youweagency.com
*/

declare(strict_types=1);

namespace JcElectronics\ExactOrders\Modifiers\Order;

use JcElectronics\ExactOrders\Api\Data\ExternalOrderInterface;
use Magento\Sales\Api\Data\OrderInterface;

class SetExternalOrderData extends AbstractModifier
{
/**
* @param ExternalOrderInterface $entity
*/
public function supports(mixed $entity): bool
{
return $entity instanceof ExternalOrderInterface &&
$entity->getExtOrderId();
}

/**
* @param ExternalOrderInterface $model
* @param OrderInterface $result
*
* @return OrderInterface
*/
public function process(mixed $model, mixed $result): mixed
{
if ($result->getExtOrderId()) {
return $result;
}

$result->setExtOrderId($model->getExtOrderId())
->setExtCustomerId($model->getExternalCustomerId());

return $result;
}
}
96 changes: 60 additions & 36 deletions src/Modifiers/Order/SetOrderItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,17 @@ public function __construct(
);
}

// phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
public function supports(mixed $entity): bool
{
return $entity instanceof ExternalOrderInterface && $entity->getItems();
}

/**
* @param ExternalOrderInterface $model
* @param OrderInterface&Order $result
*
* @return OrderInterface
* @throws LocalizedException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function process(mixed $model, mixed $result): mixed
{
Expand All @@ -69,28 +70,12 @@ public function process(mixed $model, mixed $result): mixed
continue;
}

/** @var OrderItemInterface $orderItem */
$orderItem = $this->itemFactory->create();
$orderItem->setItemId($this->getOrderItemId($item, (int) $result->getEntityId()))
->setProductId($product->getId())
->setName($item->getName())
->setSku($item->getSku())
->setProductType($product->getTypeId())
->setQtyOrdered($item->getQty())
->setBaseDiscountAmount($item->getBaseDiscountAmount() ?: $item->getDiscountAmount() ?: 0)
->setBasePrice($item->getBasePrice() ?: $item->getPrice())
->setBaseOriginalPrice($orderItem->getBasePrice())
->setBasePriceInclTax($orderItem->getBasePrice())
->setBaseRowTotal($item->getBaseRowTotal() ?: $item->getRowTotal() ?: $orderItem->getBasePrice() * $orderItem->getQtyOrdered())
->setBaseRowTotalInclTax($orderItem->getBaseRowTotal())
->setBaseTaxAmount($item->getBaseTaxAmount() ?: $item->getTaxAmount() ?: 0)
->setDiscountAmount(0)
->setOriginalPrice($item->getPrice())
->setPrice($item->getPrice())
->setPriceInclTax($item->getPrice())
->setRowTotal($item->getRowTotal() ?: $orderItem->getPrice() * $orderItem->getQtyOrdered())
->setRowTotalInclTax($orderItem->getRowTotal())
->setTaxAmount($item->getTaxAmount() ?: 0);
$orderItem = $this->getOrderItemFromExternalOrder($item, (int) $result->getEntityId()) ?? $this->itemFactory->create();

if (!$orderItem->getItemId()) {
$this->setBasicOrderItemData($orderItem, $product, $item)
->setOrderItemPriceData($orderItem, $product, $item);
}

// phpcs:disable Magento2.Performance.ForeachArrayMerge.ForeachArrayMerge
$additionalData = array_reduce(
Expand All @@ -102,13 +87,12 @@ public function process(mixed $model, mixed $result): mixed
[]
);
// phpcs:enable

$extensionAttributes = $orderItem->getExtensionAttributes() ?: $this->extensionFactory->create();
$extensionAttributes->setExpectedDeliveryDate($additionalData['expected_delivery_date'] ?? null)
->setSerialNumber($additionalData['serial_number'] ?? null);

$orderItem->setExtensionAttributes($extensionAttributes);

$result->addItem($orderItem);
}

Expand All @@ -119,9 +103,51 @@ public function process(mixed $model, mixed $result): mixed
return $result;
}

// phpcs:enable
private function setBasicOrderItemData(
OrderItemInterface|Order\Item $orderItem,
ProductInterface $product,
ItemInterface $item
): self {
$orderItem->setProductId($product->getId())
->setName($item->getName())
->setSku($item->getSku())
->setProductType($product->getTypeId())
->setQtyOrdered($item->getQty());

return $this;
}

private function setOrderItemPriceData(
OrderItemInterface|Order\Item $orderItem,
ProductInterface $product,
ItemInterface $item
): self {
$price = $item->getPrice();
$basePrice = $item->getBasePrice() ?: $price;
$rowTotal = $item->getRowTotal() ?: $price * $orderItem->getQtyOrdered();
$baseRowTotal = $item->getBaseRowTotal() ?: $rowTotal;
$taxAmount = $item->getTaxAmount() ?: 0;
$baseTaxAmount = $item->getBaseTaxAmount() ?: $taxAmount;

$orderItem->setBaseDiscountAmount($item->getBaseDiscountAmount() ?: $item->getDiscountAmount() ?: 0)
->setBasePrice($basePrice)
->setBaseOriginalPrice($basePrice)
->setBasePriceInclTax($basePrice)
->setBaseRowTotal($baseRowTotal)
->setBaseRowTotalInclTax($baseRowTotal)
->setBaseTaxAmount($baseTaxAmount)
->setDiscountAmount(0)
->setOriginalPrice($price)
->setPrice($price)
->setPriceInclTax($price)
->setRowTotal($rowTotal)
->setRowTotalInclTax($rowTotal)
->setTaxAmount($taxAmount);

return $this;
}

private function getOrderItemId(ItemInterface $item, ?int $orderId): ?int
private function getOrderItemFromExternalOrder(ItemInterface $item, ?int $orderId): ?OrderItemInterface
{
$searchCriteria = $this->searchCriteriaBuilder
->addFilter(OrderItemInterface::SKU, $item->getSku())
Expand All @@ -132,13 +158,11 @@ private function getOrderItemId(ItemInterface $item, ?int $orderId): ?int
$searchCriteria->addFilter(OrderItemInterface::ORDER_ID, $orderId);
}

$orderItem = current(
$this->orderItemRepository->getList(
$searchCriteria->create()
)->getItems()
);

return $orderItem ? (int) $orderItem->getItemId() : null;
return current(
$this->orderItemRepository
->getList($searchCriteria->create())
->getItems()
) ?: null;
}

private function getProductBySku(string $sku): ?ProductInterface
Expand Down
1 change: 1 addition & 0 deletions src/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<item name="Order::CreateOrFetchOrderModel" sortOrder="90" xsi:type="object">JcElectronics\ExactOrders\Modifiers\Order\CreateOrFetchOrderModel</item>
<item name="Order::SetBaseOrderData" sortOrder="100" xsi:type="object">JcElectronics\ExactOrders\Modifiers\Order\SetBaseOrderData</item>
<item name="Order::SetCustomerData" sortOrder="200" xsi:type="object">JcElectronics\ExactOrders\Modifiers\Order\SetCustomerData</item>
<item name="Order::SetExternalOrderData" sortOrder="250" xsi:type="object">JcElectronics\ExactOrders\Modifiers\Order\SetExternalOrderData</item>
<item name="Order::SetPaymentInformation" sortOrder="300" xsi:type="object">JcElectronics\ExactOrders\Modifiers\Order\SetPaymentInformation</item>
<item name="Order::SetShippingInformation" sortOrder="400" xsi:type="object">JcElectronics\ExactOrders\Modifiers\Order\SetShippingInformation</item>
<item name="Order::SetBillingAddress" sortOrder="500" xsi:type="object">JcElectronics\ExactOrders\Modifiers\Order\SetBillingAddress</item>
Expand Down

0 comments on commit 96eeb94

Please sign in to comment.