Skip to content

Commit

Permalink
MOL-969: update pr
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalij Mik committed Sep 27, 2023
1 parent e600e7d commit 3701291
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\EntityExtension;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\CascadeDelete;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToOneAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\OneToManyAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\OneToOneAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;

final class OrderLineItemExtension extends EntityExtension
class OrderLineItemExtension extends EntityExtension
{
const ORDER_LINE_ITEM_PROPERTY_NAME = 'refundLineItem';
const ORDER_LINE_ITEM_PROPERTY_NAME = 'mollieRefundLineItems';

/**
* @return string
Expand All @@ -28,6 +30,6 @@ public function getDefinitionClass(): string
*/
public function extendFields(FieldCollection $collection): void
{
$collection->add(new OneToOneAssociationField(self::ORDER_LINE_ITEM_PROPERTY_NAME, 'id', 'order_line_item_id', RefundItemDefinition::class));
$collection->add(new OneToManyAssociationField(self::ORDER_LINE_ITEM_PROPERTY_NAME, RefundItemDefinition::class, 'order_line_item_id'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ protected function defineFields(): FieldCollection
(new FkField('refund_id', 'refundId', RefundDefinition::class))->addFlags(new ApiAware()),
(new ManyToOneAssociationField('refund', 'refund_id', RefundDefinition::class))->addFlags(new ApiAware()),
(new IntField('quantity', 'quantity'))->addFlags(new Required(), new ApiAware()),
(new StringField('reference', 'reference'))->addFlags(new ApiAware()),
(new StringField('label', 'label'))->addFlags(new ApiAware()),
(new FloatField('amount', 'amount'))->addFlags(new Required(), new ApiAware()),
(new StringField('mollie_line_id', 'mollieLineId'))->addFlags(new Required(), new ApiAware()),
(new FkField('order_line_item_id', 'orderLineItemId', OrderLineItemDefinition::class))->addFlags(new ApiAware()),
new ReferenceVersionField(OrderLineItemDefinition::class, 'order_line_item_version_id'),
(new OneToOneAssociationField('orderLineItem', 'order_line_item_id', 'id', OrderLineItemDefinition::class, false))->addFlags(new ApiAware()),
(new ManyToOneAssociationField('orderLineItem', 'order_line_item_id', OrderLineItemDefinition::class))->addFlags(new ApiAware()),
]);
}
}
16 changes: 10 additions & 6 deletions src/Components/RefundManager/DAL/RefundItem/RefundItemEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ final class RefundItemEntity extends Entity
* @var string
*/
protected $refundId;

/**
* @var null|RefundEntity
*/
protected $refund;

/**
* @var string
*/
protected $reference;
protected $label;

/**
* @var string
*/
Expand All @@ -43,6 +46,7 @@ final class RefundItemEntity extends Entity
* @var float
*/
protected $amount;

/**
* @var null|string
*/
Expand Down Expand Up @@ -189,17 +193,17 @@ public function setRefund(?RefundEntity $refund): void
/**
* @return string
*/
public function getReference(): string
public function getLabel(): string
{
return $this->reference;
return $this->label;
}

/**
* @param string $reference
* @param string $label
*/
public function setReference(string $reference): void
public function setLabel(string $label): void
{
$this->reference = $reference;
$this->label = $label;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Components/RefundManager/RefundManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ private function convertToRepositoryArray(array $serviceItems, string $type): ar
'orderLineItemId' => $item->getShopwareLineID(),
'orderLineItemVersionId' => $item->getShopwareLineVersionId(),
'mollieLineId' => $item->getMollieLineID(),
'reference' => (string)$item->getShopwareReference(),
'label' => (string)$item->getShopwareReference(),
'quantity' => $item->getQuantity(),
'amount' => $item->getAmount(),
];
Expand Down
2 changes: 1 addition & 1 deletion src/Hydrator/RefundHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function hydrate(Refund $refund, OrderEntity $order): array
'swLineId' => (string)$refundLineItem->getOrderLineItemId(),
'swLineVersionId' => (string)$refundLineItem->getOrderLineItemVersionId(),
'mollieLineId' => $refundLineItem->getMollieLineId(),
'swReference' => $refundLineItem->getReference(),
'swReference' => $refundLineItem->getLabel(),
'quantity' => $refundLineItem->getQuantity(),
'amount' => $refundLineItem->getAmount()
];
Expand Down
2 changes: 1 addition & 1 deletion src/Migration/Migration1672671475RefundItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function update(Connection $connection): void
`order_line_item_version_id` binary(16) NULL,
`mollie_line_id` VARCHAR(255),
`type` VARCHAR(255),
`reference` VARCHAR(255),
`label` VARCHAR(255),
`refund_id` binary(16) NOT NULL,
`quantity` INT(10),
`amount` DOUBLE,
Expand Down
7 changes: 4 additions & 3 deletions src/Resources/config/services/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@
<argument type="service" id="Kiener\MolliePayments\Service\OrderService"/>
<argument type="service" id="Kiener\MolliePayments\Hydrator\RefundHydrator"/>
<argument type="service" id="Kiener\MolliePayments\Gateway\Mollie\MollieGateway"/>
<argument type="service" id="Kiener\MolliePayments\Service\Refund\CompositionRepairService"/>
<argument type="service" id="Kiener\MolliePayments\Service\Refund\CompositionMigrationService"/>
</service>

<service id="Kiener\MolliePayments\Service\Refund\CompositionRepairService">

<service id="Kiener\MolliePayments\Service\Refund\CompositionMigrationService">
<argument type="service" id="mollie_refund_item.repository"/>
</service>

Expand Down Expand Up @@ -184,7 +185,7 @@
<tag name="mollie_payments.mail.attachment_generator"/>
</service>

<service id="Kiener\MolliePayments\Service\Refund\Mollie\DataCompressor"/>



</services>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;

class CompositionRepairService implements CompositionRepairServiceInterface
class CompositionMigrationService implements CompositionMigrationServiceInterface
{
/**
* @var EntityRepository
Expand Down Expand Up @@ -63,14 +63,20 @@ public function updateRefundItems(Refund $refund, OrderEntity $order, Context $c
}

$refundLineItems = $shopwareRefund->getRefundItems()->getElements();

/**
* Exit criteria, if we have refund line items, then we exit here
*/
if (count($refundLineItems) > 0) {
return $order;
}

/** @var ?OrderLineItemCollection $orderLineItems */
$orderLineItems = $order->getLineItems();
if ($orderLineItems === null) {
return $order;
}

$dataToSave = [];
foreach ($oldCompositions as $composition) {
$reference = $composition->swReference;
Expand All @@ -81,14 +87,17 @@ public function updateRefundItems(Refund $refund, OrderEntity $order, Context $c
$row = [
'type' => $oldMetadata->type,
'mollieLineId' => $composition->mollieLineId,
'reference' => $reference,
'label' => $reference,
'quantity' => $composition->quantity,
'amount' => $composition->amount,
'refundId' => $shopwareRefund->getId(),
'oderLineItemId' => null,
'oderLineItemVersionId' => null,
];


$orderLineItem = $this->filterByMollieId($orderLineItems, $composition->mollieLineId);

if ($orderLineItem instanceof OrderLineItemEntity) {
$row['orderLineItemId'] = $orderLineItem->getId();
$row['orderLineItemVersionId'] = $orderLineItem->getVersionId();
Expand All @@ -97,7 +106,13 @@ public function updateRefundItems(Refund $refund, OrderEntity $order, Context $c

$dataToSave[] = $row;
}

$entityWrittenContainerEvent = $this->refundItemRepository->create($dataToSave, $context);

/**
* get the new inserted data from the written container event and create a new refund items collection and assign it to the refund.
* php is using here copy by reference so the order will have the new line items inside the refund and we do not need to reload the order entity again
*/
$refundItems = $this->createEntitiesByEvent($entityWrittenContainerEvent);
$shopwareRefund->setRefundItems($refundItems);
return $order;
Expand All @@ -119,7 +134,7 @@ private function createEntitiesByEvent(EntityWrittenContainerEvent $event): Refu
$entity->setId($result->getProperty('id'));
$entity->setUniqueIdentifier($result->getProperty('id'));
$entity->setQuantity($result->getProperty('quantity'));
$entity->setReference($result->getProperty('reference'));
$entity->setLabel($result->getProperty('label'));
$entity->setAmount($result->getProperty('amount'));
$entity->setRefundId($result->getProperty('refundId'));
$entity->setMollieLineId($result->getProperty('mollieLineId'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\Context;

interface CompositionRepairServiceInterface
interface CompositionMigrationServiceInterface
{
public function updateRefundItems(Refund $refund, OrderEntity $order, Context $context): OrderEntity;
}
37 changes: 0 additions & 37 deletions src/Service/Refund/Mollie/DataCompressor.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Service/Refund/RefundService.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class RefundService implements RefundServiceInterface
private $gwMollie;

/**
* @var CompositionRepairServiceInterface
* @var CompositionMigrationServiceInterface
*/
private $compositionRepairService;

Expand All @@ -59,7 +59,7 @@ class RefundService implements RefundServiceInterface
* @param RefundHydrator $refundHydrator
* @param MollieGatewayInterface $gwMollie
*/
public function __construct(Order $mollie, OrderService $orders, RefundHydrator $refundHydrator, MollieGatewayInterface $gwMollie, CompositionRepairServiceInterface $compositionRepairService)
public function __construct(Order $mollie, OrderService $orders, RefundHydrator $refundHydrator, MollieGatewayInterface $gwMollie, CompositionMigrationServiceInterface $compositionRepairService)
{
$this->mollie = $mollie;
$this->orders = $orders;
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPUnit/Hydrator/RefundHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private function getExpectedData(?float $amount, ?float $settlementAmount, strin
'isProcessing' => $status == RefundStatus::STATUS_PROCESSING,
'isQueued' => $status == RefundStatus::STATUS_QUEUED,
'isTransferred' => $status == RefundStatus::STATUS_REFUNDED,
'metadata' => null,
'metadata' => new \stdClass(),
];
}

Expand Down
53 changes: 0 additions & 53 deletions tests/PHPUnit/Service/Refund/Mollie/DataCompressorTest.php

This file was deleted.

0 comments on commit 3701291

Please sign in to comment.