Skip to content

Commit

Permalink
Merge pull request #156 from Adyen/develop
Browse files Browse the repository at this point in the history
Release 1.7.2
  • Loading branch information
acampos1916 authored May 3, 2021
2 parents 595b818 + 0228612 commit d147d29
Show file tree
Hide file tree
Showing 37 changed files with 917 additions and 141 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @cyattilakiss @acampos1916 @msilvagarcia
* @cyattilakiss @acampos1916 @msilvagarcia @peterojo
93 changes: 93 additions & 0 deletions Basket/Restore/DetailAttributesRestorer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace AdyenPayment\Basket\Restore;

use AdyenPayment\Dbal\BasketDetailAttributes;
use AdyenPayment\Dbal\OrderDetailAttributes;
use Shopware\Components\Model\ModelManager;
use Zend_Db_Adapter_Exception;

class DetailAttributesRestorer
{
/**
* @var BasketDetailAttributes
*/
private $basketDetailAttributes;

/**
* @var ModelManager
*/
private $modelManager;

/**
* @var OrderDetailAttributes
*/
private $orderDetailAttributes;

public function __construct(
ModelManager $modelManager,
BasketDetailAttributes $basketDetailAttributes,
OrderDetailAttributes $orderDetailAttributes
) {
$this->modelManager = $modelManager;
$this->basketDetailAttributes = $basketDetailAttributes;
$this->orderDetailAttributes = $orderDetailAttributes;
}

/**
* Copies attributes from the supplied order detail article ID to a basket detail ID
*
* @param int $orderDetailId
* @param int $basketDetailId
* @throws Zend_Db_Adapter_Exception
*/
public function restore(int $orderDetailId, int $basketDetailId)
{
$orderDetailAttributes = $this->orderDetailAttributes->fetchByOrderDetailId($orderDetailId);
if (!count($orderDetailAttributes)) {
return;
}

$attributes = $this->provideFillableAttributeColumns();

if (!count($attributes)) {
return;
}

// Updating the basket attributes with the order attribute values
$attributeValues = [];
foreach ($attributes as $attribute) {
if (!empty($orderDetailAttributes[$attribute])) {
$attributeValues[$attribute] = $orderDetailAttributes[$attribute];
}
}

if (!count($attributeValues)) {
return;
}

if ($this->basketDetailAttributes->hasBasketDetails($basketDetailId)) {
$this->basketDetailAttributes->update($basketDetailId, $attributeValues);
} else {
$this->basketDetailAttributes->insert($basketDetailId, $attributeValues);
}
}

/**
* @return array
*/
private function provideFillableAttributeColumns(): array
{
// Getting order attributes columns to possibly fill
$basketAttributesColumns = $this->modelManager
->getClassMetadata('Shopware\Models\Attribute\OrderDetail')
->getColumnNames();

// These columns shouldn't be translated from the order detail to the basket detail
$columnsToSkip = [
'id',
'detailID'
];
return array_diff($basketAttributesColumns, $columnsToSkip);
}
}
170 changes: 134 additions & 36 deletions Components/BasketService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,29 @@

namespace AdyenPayment\Components;

use AdyenPayment\Basket\Restore\DetailAttributesRestorer;
use AdyenPayment\Models\Event;
use DateTime;
use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use Enlight_Components_Db_Adapter_Pdo_Mysql;
use Enlight_Components_Session_Namespace;
use Enlight_Event_Exception;
use Enlight_Exception;
use sBasket;
use Shopware\Components\ContainerAwareEventManager;
use Shopware\Components\Model\ModelManager;
use Shopware\Models\Order\Detail;
use Shopware\Models\Order\Order;
use Shopware\Models\Order\Status;
use Shopware\Models\Voucher\Code;
use Shopware\Models\Voucher\Repository;
use Shopware\Models\Voucher\Voucher;
use Zend_Db_Adapter_Exception;
use Zend_Db_Select_Exception;
use Zend_Db_Statement_Exception;

/**
* Class BasketService
Expand All @@ -24,7 +39,7 @@ class BasketService
const MODE_SURCHARGE_DISCOUNT = 4;

/**
* @var \sBasket
* @var sBasket
*/
private $sBasket;

Expand All @@ -39,53 +54,77 @@ class BasketService
private $modelManager;

/**
* @var \Doctrine\Common\Persistence\ObjectRepository|\Doctrine\ORM\EntityRepository
* @var ObjectRepository|EntityRepository
*/
private $statusRepository;

/**
* @var \Doctrine\Common\Persistence\ObjectRepository|\Doctrine\ORM\EntityRepository|\Shopware\Models\Order\Repository
* @var ObjectRepository|EntityRepository|\Shopware\Models\Order\Repository
*/
private $orderRepository;

/**
* @var \Doctrine\Common\Persistence\ObjectRepository|\Doctrine\ORM\EntityRepository|\Shopware\Models\Voucher\Repository
* @var ObjectRepository|EntityRepository|Repository
*/
private $voucherRepository;

/**
* @var \Doctrine\Common\Persistence\ObjectRepository|\Doctrine\ORM\EntityRepository
* @var ObjectRepository|EntityRepository
*/
private $voucherCodeRepository;

/**
* @var Enlight_Components_Db_Adapter_Pdo_Mysql
*/
private $db;

/**
* @var Enlight_Components_Session_Namespace
*/
private $session;

/**
* @var DetailAttributesRestorer
*/
private $detailAttributesRestorer;

/**
* BasketService constructor.
* @param ContainerAwareEventManager $events
* @param ModelManager $modelManager
* @param Enlight_Components_Db_Adapter_Pdo_Mysql $db
* @param Enlight_Components_Session_Namespace $session
* @param DetailAttributesRestorer $detailAttributesRestorer
*/
public function __construct(
ContainerAwareEventManager $events,
ModelManager $modelManager
ModelManager $modelManager,
Enlight_Components_Db_Adapter_Pdo_Mysql $db,
Enlight_Components_Session_Namespace $session,
DetailAttributesRestorer $detailAttributesRestorer
) {
$this->sBasket = Shopware()->Modules()->Basket();
$this->events = $events;
$this->modelManager = $modelManager;
$this->db = $db;
$this->session = $session;

$this->statusRepository = $modelManager->getRepository(Status::class);
$this->orderRepository = $modelManager->getRepository(Order::class);
$this->voucherRepository = $modelManager->getRepository(Voucher::class);
$this->voucherCodeRepository = $modelManager->getRepository(Code::class);
$this->detailAttributesRestorer = $detailAttributesRestorer;
}

/**
* @param int $orderNumber
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
* @throws \Enlight_Event_Exception
* @throws \Enlight_Exception
* @throws \Zend_Db_Adapter_Exception
* @param string $orderNumber
* @throws ORMException
* @throws OptimisticLockException
* @throws Enlight_Event_Exception
* @throws Enlight_Exception
* @throws Zend_Db_Adapter_Exception
*/
public function cancelAndRestoreByOrderNumber(int $orderNumber)
public function cancelAndRestoreByOrderNumber(string $orderNumber)
{
$order = $this->getOrderByOrderNumber($orderNumber);
if (!$order) {
Expand All @@ -97,19 +136,19 @@ public function cancelAndRestoreByOrderNumber(int $orderNumber)
}

/**
* @param int $orderNumber
* @return Order|null
* @param string $orderNumber
* @return Order|null|object
*/
public function getOrderByOrderNumber(int $orderNumber)
public function getOrderByOrderNumber(string $orderNumber): Order
{
return $this->orderRepository->findOneBy(['number' => $orderNumber]);
}

/**
* @param Order $order
* @throws \Enlight_Event_Exception
* @throws \Enlight_Exception
* @throws \Zend_Db_Adapter_Exception
* @throws Enlight_Event_Exception
* @throws Enlight_Exception
* @throws Zend_Db_Adapter_Exception
*/
public function restoreFromOrder(Order $order)
{
Expand All @@ -128,8 +167,8 @@ public function restoreFromOrder(Order $order)

/**
* @param Order $order
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
* @throws ORMException
* @throws OptimisticLockException
*/
public function cancelOrder(Order $order)
{
Expand All @@ -145,7 +184,7 @@ public function cancelOrder(Order $order)
/**
* @param Order $order
* @param Detail $orderDetail
* @throws \Enlight_Event_Exception
* @throws Enlight_Event_Exception
*/
private function processOrderDetail(Order $order, Detail $orderDetail)
{
Expand All @@ -166,6 +205,7 @@ private function processOrderDetail(Order $order, Detail $orderDetail)

switch ($orderDetailFiltered->getMode()) {
case self::MODE_PRODUCT:
case self::MODE_SURCHARGE_DISCOUNT:
$this->addArticle($orderDetailFiltered);
break;
case self::MODE_PREMIUM_PRODUCT:
Expand All @@ -175,7 +215,6 @@ private function processOrderDetail(Order $order, Detail $orderDetail)
$this->addVoucher($orderDetailFiltered);
break;
case self::MODE_REBATE:
case self::MODE_SURCHARGE_DISCOUNT:
break;
}

Expand All @@ -188,21 +227,80 @@ private function processOrderDetail(Order $order, Detail $orderDetail)

/**
* @param Detail $orderDetail
* @throws \Enlight_Event_Exception
* @throws \Enlight_Exception
* @throws \Zend_Db_Adapter_Exception
* @throws Enlight_Event_Exception
* @throws Enlight_Exception
* @throws Zend_Db_Adapter_Exception
* @throws Zend_Db_Select_Exception
* @throws Zend_Db_Statement_Exception
*/
private function addArticle(Detail $orderDetail)
{
$this->sBasket->sAddArticle(
$orderDetail->getArticleNumber(),
$orderDetail->getQuantity()
);
if (empty($orderDetail->getArticleNumber()) || !$this->isArticlesDetails($orderDetail->getArticleNumber())) {
// The order item doesn't have an article number or it isn't a regular Shopware article
// add it to the basket manually
$basketDetailId = $this->insertInToBasket($orderDetail);
} else {
$basketDetailId = $this->sBasket->sAddArticle(
$orderDetail->getArticleNumber(),
$orderDetail->getQuantity()
);
}

$this->detailAttributesRestorer->restore($orderDetail->getId(), $basketDetailId);
}

/**
* Searches in the s_articles_details table with the ordernumber column and returns true if an article is found
*
* @param string $articleDetailNumber
* @return bool
* @throws Zend_Db_Select_Exception
* @throws Zend_Db_Statement_Exception
*/
private function isArticlesDetails(string $articleDetailNumber): bool
{
$result = $this->db->select()
->from('s_articles_details')
->where('ordernumber=?', $articleDetailNumber)
->query()
->fetch();

return !empty($result);
}

/**
* Inserts data from a order detail row into a basket detail and returns the inserted ID
*
* @param Detail $optionData
* @return int
* @throws Zend_Db_Adapter_Exception
*/
private function insertInToBasket(Detail $optionData): int
{
$this->db->insert('s_order_basket', [
'sessionID' => $this->session->get('sessionId'),
'userID' => $this->session->get('sUserId') || 0,
'articlename' => $optionData->getArticleName(),
'articleID' => $optionData->getArticleId(),
'quantity' => $optionData->getQuantity(),
'price' => $optionData->getPrice(),
'netprice' => $optionData->getPrice() === null
? 0
: $optionData->getPrice() / (1 + ($optionData->getTaxRate() / 100)),
'tax_rate' => $optionData->getTaxRate(),
'modus' => $optionData->getMode(),
'esdarticle' => $optionData->getEsdArticle(),
'config' => $optionData->getConfig(),
'datum' => (new DateTime())->format('Y-m-d H:i:s'),
'currencyFactor' => Shopware()->Shop()->getCurrency()->getFactor()
]);

return $this->db->lastInsertId();
}

/**
* @param Detail $orderDetail
* @throws \Zend_Db_Adapter_Exception
* @throws Zend_Db_Adapter_Exception
*/
private function addPremium(Detail $orderDetail)
{
Expand All @@ -212,11 +310,11 @@ private function addPremium(Detail $orderDetail)

/**
* @param Detail $orderDetail
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
* @throws \Enlight_Event_Exception
* @throws \Enlight_Exception
* @throws \Zend_Db_Adapter_Exception
* @throws ORMException
* @throws OptimisticLockException
* @throws Enlight_Event_Exception
* @throws Enlight_Exception
* @throws Zend_Db_Adapter_Exception
*/
private function addVoucher(Detail $orderDetail)
{
Expand Down
Loading

0 comments on commit d147d29

Please sign in to comment.