Skip to content

Commit

Permalink
Merge pull request #1985 from bibich/delivery-payment-events
Browse files Browse the repository at this point in the history
Delivery payment events
  • Loading branch information
gillesbourgeat committed Mar 17, 2016
2 parents 019f488 + 8baea69 commit d62769d
Show file tree
Hide file tree
Showing 20 changed files with 1,070 additions and 209 deletions.
66 changes: 66 additions & 0 deletions core/lib/Thelia/Action/Delivery.php
@@ -0,0 +1,66 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/


namespace Thelia\Action;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\Delivery\DeliveryPostageEvent;
use Thelia\Core\Event\TheliaEvents;

/**
* Class Delivery
* @package Thelia\Action
* @author Julien Chanséaume <julien@thelia.net>
*/
class Delivery implements EventSubscriberInterface
{
/**
* Get postage from module using the classical module functions
*
* @param DeliveryPostageEvent $event
*/
public function getPostage(DeliveryPostageEvent $event, $eventName, EventDispatcherInterface $dispatcher)
{
$module = $event->getModule();

// dispatch event to target specific module
$dispatcher->dispatch(
TheliaEvents::getModuleEvent(
TheliaEvents::MODULE_DELIVERY_GET_POSTAGE,
$module->getCode()
),
$event
);

if ($event->isPropagationStopped()) {
return;
}

// call legacy module method
$event->setValidModule($module->isValidDelivery($event->getCountry()));
if ($event->isValidModule()) {
$event->setPostage($module->getPostage($event->getCountry()));
}
}

/**
* @inheritdoc
*/
public static function getSubscribedEvents()
{
return [
TheliaEvents::MODULE_DELIVERY_GET_POSTAGE => ['getPostage', 128]
];
}
}
56 changes: 48 additions & 8 deletions core/lib/Thelia/Action/Order.php
Expand Up @@ -13,13 +13,15 @@
namespace Thelia\Action;

use Propel\Runtime\Propel;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Thelia\Core\Event\Order\OrderAddressEvent;
use Thelia\Core\Event\Order\OrderEvent;
use Thelia\Core\Event\Order\OrderManualEvent;
use Thelia\Core\Event\Order\OrderPaymentEvent;
use Thelia\Core\Event\Payment\ManageStockOnCreationEvent;
use Thelia\Core\Event\Product\VirtualProductOrderHandleEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\Security\SecurityContext;
Expand All @@ -45,6 +47,7 @@
use Thelia\Model\ProductSaleElements;
use Thelia\Model\ProductSaleElementsQuery;
use Thelia\Model\TaxRuleI18n;
use Thelia\Module\PaymentModuleInterface;
use Thelia\Tools\I18n;

/**
Expand Down Expand Up @@ -397,7 +400,10 @@ public function createManual(OrderManualEvent $event, $eventName, EventDispatche
$event->getLang(),
$event->getCart(),
$event->getCustomer(),
$paymentModuleInstance->manageStockOnCreation(),
$this->isModuleManageStockOnCreation(
$dispatcher,
$paymentModuleInstance
),
$event->getUseOrderDefinedAddresses()
)
);
Expand Down Expand Up @@ -429,7 +435,10 @@ public function create(OrderEvent $event, $eventName, EventDispatcherInterface $
$session->getLang(),
$session->getSessionCart($dispatcher),
$this->securityContext->getCustomerUser(),
$paymentModuleInstance->manageStockOnCreation()
$this->isModuleManageStockOnCreation(
$dispatcher,
$paymentModuleInstance
)
);

$dispatcher->dispatch(TheliaEvents::ORDER_BEFORE_PAYMENT, new OrderEvent($placedOrder));
Expand Down Expand Up @@ -514,12 +523,20 @@ public function sendNotificationEmail(OrderEvent $event)
/**
* @param OrderEvent $event
*/
public function updateStatus(OrderEvent $event)
public function updateStatus(OrderEvent $event, $eventName, EventDispatcherInterface $dispatcher)
{
$order = $event->getOrder();
$newStatus = $event->getStatus();
$paymentModule = ModuleQuery::create()->findPk($order->getPaymentModuleId());
/** @var PaymentModuleInterface $paymentModuleInstance */
$paymentModuleInstance = $paymentModule->createInstance();

$this->updateQuantity($order, $newStatus);
$manageStockOnCreation = $this->isModuleManageStockOnCreation(
$dispatcher,
$paymentModuleInstance
);

$this->updateQuantity($order, $newStatus, $manageStockOnCreation);

$order->setStatusId($newStatus);
$order->save();
Expand All @@ -532,14 +549,14 @@ public function updateStatus(OrderEvent $event)
* @param $newStatus $newStatus the new status ID
* @throws \Thelia\Exception\TheliaProcessException
*/
public function updateQuantity(ModelOrder $order, $newStatus)
public function updateQuantity(ModelOrder $order, $newStatus, $manageStockOnCreation = true)
{
$canceledStatus = OrderStatusQuery::getCancelledStatus()->getId();
$paidStatus = OrderStatusQuery::getPaidStatus()->getId();
if ($newStatus == $canceledStatus || $order->isCancelled()) {
$this->updateQuantityForCanceledOrder($order, $newStatus, $canceledStatus);
} elseif ($paidStatus == $newStatus && $order->isNotPaid() && $order->getVersion() == 1) {
$this->updateQuantityForPaidOrder($order);
$this->updateQuantityForPaidOrder($order, $manageStockOnCreation);
}
}

Expand All @@ -548,14 +565,14 @@ public function updateQuantity(ModelOrder $order, $newStatus)
* @throws \Exception
* @throws \Propel\Runtime\Exception\PropelException
*/
protected function updateQuantityForPaidOrder(ModelOrder $order)
protected function updateQuantityForPaidOrder(ModelOrder $order, $manageStockOnCreation)
{
$paymentModule = ModuleQuery::create()->findPk($order->getPaymentModuleId());

/** @var \Thelia\Module\PaymentModuleInterface $paymentModuleInstance */
$paymentModuleInstance = $paymentModule->createInstance();

if (false === $paymentModuleInstance->manageStockOnCreation()) {
if (false === $manageStockOnCreation) {
$orderProductList = $order->getOrderProducts();

/** @var OrderProduct $orderProduct */
Expand Down Expand Up @@ -652,6 +669,29 @@ public function updateAddress(OrderAddressEvent $event)
$event->setOrderAddress($orderAddress);
}

/**
* Check if a payment module manage stock on creation
*
* @param EventDispatcher $dispatcher
* @param PaymentModuleInterface $module
* @return bool if the module manage stock on creation, false otherwise
*/
protected function isModuleManageStockOnCreation(EventDispatcherInterface $dispatcher, PaymentModuleInterface $module)
{
$event = new ManageStockOnCreationEvent($module);

$dispatcher->dispatch(
TheliaEvents::getModuleEvent(
TheliaEvents::MODULE_PAYMENT_MANAGE_STOCK,
$module->getCode()
)
);

return (null !== $event->getManageStock())
? $event->getManageStock()
: $module->manageStockOnCreation();
}

/**
* {@inheritdoc}
*/
Expand Down
63 changes: 63 additions & 0 deletions core/lib/Thelia/Action/Payment.php
@@ -0,0 +1,63 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/


namespace Thelia\Action;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\Payment\IsValidPaymentEvent;
use Thelia\Core\Event\TheliaEvents;

/**
* Class Payment
* @package Thelia\Action
* @author Julien Chanséaume <julien@thelia.net>
*/
class Payment implements EventSubscriberInterface
{
/**
* Check if a module is valid
*
* @param IsValidPaymentEvent $event
*/
public function isValid(IsValidPaymentEvent $event, $eventName, EventDispatcherInterface $dispatcher)
{
$module = $event->getModule();

// dispatch event to target specific module
$dispatcher->dispatch(
TheliaEvents::getModuleEvent(
TheliaEvents::MODULE_PAYMENT_IS_VALID,
$module->getCode()
),
$event
);

if ($event->isPropagationStopped()) {
return;
}

// call legacy module method
$event->setValidModule($module->isValidPayment());
}

/**
* @inheritdoc
*/
public static function getSubscribedEvents()
{
return [
TheliaEvents::MODULE_PAYMENT_IS_VALID => ['isValid', 128],
];
}
}
8 changes: 8 additions & 0 deletions core/lib/Thelia/Config/Resources/action.xml
Expand Up @@ -225,6 +225,14 @@
<tag name="kernel.event_subscriber"/>
</service>

<service id="thelia.action.module.delivery" class="Thelia\Action\Delivery">
<tag name="kernel.event_subscriber"/>
</service>

<service id="thelia.action.module.payment" class="Thelia\Action\Payment">
<tag name="kernel.event_subscriber"/>
</service>

</services>

</config>

0 comments on commit d62769d

Please sign in to comment.