Added a way to create orders outside of the front-office context.

This commit is contained in:
Franck Allimant
2014-01-23 19:49:36 +01:00
parent c0f5002db9
commit f8460901b3

View File

@@ -42,6 +42,14 @@ use Thelia\Model\Map\OrderTableMap;
use Thelia\Model\OrderAddress; use Thelia\Model\OrderAddress;
use Thelia\Model\OrderStatusQuery; use Thelia\Model\OrderStatusQuery;
use Thelia\Tools\I18n; use Thelia\Tools\I18n;
use Thelia\Model\Currency;
use Thelia\Model\Lang;
use Thelia\Model\Country;
use Thelia\Model\Customer;
use Thelia\Core\Event\Order\OrderManualEvent;
use Thelia\Model\Cart as CartModel;
use Thelia\Model\Order as ModelOrder;
/** /**
* *
@@ -113,36 +121,23 @@ class Order extends BaseAction implements EventSubscriberInterface
$event->setOrder($order); $event->setOrder($order);
} }
/** protected function createOrder(ModelOrder $sessionOrder, Currency $currency, Lang $lang, CartModel $cart, Customer $customer)
* @param OrderEvent $event
*
* @throws \Thelia\Exception\TheliaProcessException
*/
public function create(OrderEvent $event)
{ {
$con = \Propel\Runtime\Propel::getConnection( $con = \Propel\Runtime\Propel::getConnection(
OrderTableMap::DATABASE_NAME OrderTableMap::DATABASE_NAME
); );
$con->beginTransaction(); $con->beginTransaction();
$sessionOrder = $event->getOrder();
/* use a copy to avoid errored reccord in session */ /* use a copy to avoid errored reccord in session */
$placedOrder = $sessionOrder->copy(); $placedOrder = $sessionOrder->copy();
$placedOrder->setDispatcher($this->getDispatcher()); $placedOrder->setDispatcher($this->getDispatcher());
$customer = $this->getSecurityContext()->getCustomerUser();
$currency = $this->getSession()->getCurrency();
$lang = $this->getSession()->getLang();
$deliveryAddress = AddressQuery::create()->findPk($sessionOrder->chosenDeliveryAddress); $deliveryAddress = AddressQuery::create()->findPk($sessionOrder->chosenDeliveryAddress);
$taxCountry = $deliveryAddress->getCountry(); $taxCountry = $deliveryAddress->getCountry();
$invoiceAddress = AddressQuery::create()->findPk($sessionOrder->chosenInvoiceAddress); $invoiceAddress = AddressQuery::create()->findPk($sessionOrder->chosenInvoiceAddress);
$cart = $this->getSession()->getCart();
$cartItems = $cart->getCartItems(); $cartItems = $cart->getCartItems();
$paymentModule = ModuleQuery::create()->findPk($placedOrder->getPaymentModuleId());
/* fulfill order */ /* fulfill order */
$placedOrder->setCustomerId($customer->getId()); $placedOrder->setCustomerId($customer->getId());
$placedOrder->setCurrencyId($currency->getId()); $placedOrder->setCurrencyId($currency->getId());
@@ -163,7 +158,7 @@ class Order extends BaseAction implements EventSubscriberInterface
->setCity($deliveryAddress->getCity()) ->setCity($deliveryAddress->getCity())
->setPhone($deliveryAddress->getPhone()) ->setPhone($deliveryAddress->getPhone())
->setCountryId($deliveryAddress->getCountryId()) ->setCountryId($deliveryAddress->getCountryId())
->save($con) ->save($con)
; ;
$invoiceOrderAddress = new OrderAddress(); $invoiceOrderAddress = new OrderAddress();
@@ -179,19 +174,19 @@ class Order extends BaseAction implements EventSubscriberInterface
->setCity($invoiceAddress->getCity()) ->setCity($invoiceAddress->getCity())
->setPhone($invoiceAddress->getPhone()) ->setPhone($invoiceAddress->getPhone())
->setCountryId($invoiceAddress->getCountryId()) ->setCountryId($invoiceAddress->getCountryId())
->save($con) ->save($con)
; ;
$placedOrder->setDeliveryOrderAddressId($deliveryOrderAddress->getId()); $placedOrder->setDeliveryOrderAddressId($deliveryOrderAddress->getId());
$placedOrder->setInvoiceOrderAddressId($invoiceOrderAddress->getId()); $placedOrder->setInvoiceOrderAddressId($invoiceOrderAddress->getId());
$placedOrder->setStatusId( $placedOrder->setStatusId(
OrderStatusQuery::create()->findOneByCode(OrderStatus::CODE_NOT_PAID)->getId() OrderStatusQuery::create()->findOneByCode(OrderStatus::CODE_NOT_PAID)->getId()
); );
/* memorize discount */ /* memorize discount */
$placedOrder->setDiscount( $placedOrder->setDiscount(
$cart->getDiscount() $cart->getDiscount()
); );
$placedOrder->save($con); $placedOrder->save($con);
@@ -202,7 +197,7 @@ class Order extends BaseAction implements EventSubscriberInterface
$product = $cartItem->getProduct(); $product = $cartItem->getProduct();
/* get translation */ /* get translation */
$productI18n = I18n::forceI18nRetrieving($this->getSession()->getLang()->getLocale(), 'Product', $product->getId()); $productI18n = I18n::forceI18nRetrieving($lang->getLocale(), 'Product', $product->getId());
$pse = $cartItem->getProductSaleElements(); $pse = $cartItem->getProductSaleElements();
@@ -213,19 +208,19 @@ class Order extends BaseAction implements EventSubscriberInterface
/* decrease stock */ /* decrease stock */
$pse->setQuantity( $pse->setQuantity(
$pse->getQuantity() - $cartItem->getQuantity() $pse->getQuantity() - $cartItem->getQuantity()
); );
$pse->save($con); $pse->save($con);
/* get tax */ /* get tax */
$taxRuleI18n = I18n::forceI18nRetrieving($this->getSession()->getLang()->getLocale(), 'TaxRule', $product->getTaxRuleId()); $taxRuleI18n = I18n::forceI18nRetrieving($lang->getLocale(), 'TaxRule', $product->getTaxRuleId());
$taxDetail = $product->getTaxRule()->getTaxDetail( $taxDetail = $product->getTaxRule()->getTaxDetail(
$product, $product,
$taxCountry, $taxCountry,
$cartItem->getPrice(), $cartItem->getPrice(),
$cartItem->getPromoPrice(), $cartItem->getPromoPrice(),
$this->getSession()->getLang()->getLocale() $lang->getLocale()
); );
$orderProduct = new OrderProduct(); $orderProduct = new OrderProduct();
@@ -246,9 +241,9 @@ class Order extends BaseAction implements EventSubscriberInterface
->setTaxRuleTitle($taxRuleI18n->getTitle()) ->setTaxRuleTitle($taxRuleI18n->getTitle())
->setTaxRuleDescription($taxRuleI18n->getDescription()) ->setTaxRuleDescription($taxRuleI18n->getDescription())
->setEanCode($pse->getEanCode()) ->setEanCode($pse->getEanCode())
->setDispatcher($this->getDispatcher())
->save($con)
; ;
$orderProduct->setDispatcher($this->getDispatcher());
$orderProduct->save($con);
/* fulfill order_product_tax */ /* fulfill order_product_tax */
foreach ($taxDetail as $tax) { foreach ($taxDetail as $tax) {
@@ -258,8 +253,8 @@ class Order extends BaseAction implements EventSubscriberInterface
/* fulfill order_attribute_combination and decrease stock */ /* fulfill order_attribute_combination and decrease stock */
foreach ($pse->getAttributeCombinations() as $attributeCombination) { foreach ($pse->getAttributeCombinations() as $attributeCombination) {
$attribute = I18n::forceI18nRetrieving($this->getSession()->getLang()->getLocale(), 'Attribute', $attributeCombination->getAttributeId()); $attribute = I18n::forceI18nRetrieving($lang->getLocale(), 'Attribute', $attributeCombination->getAttributeId());
$attributeAv = I18n::forceI18nRetrieving($this->getSession()->getLang()->getLocale(), 'AttributeAv', $attributeCombination->getAttributeAvId()); $attributeAv = I18n::forceI18nRetrieving($lang->getLocale(), 'AttributeAv', $attributeCombination->getAttributeAvId());
$orderAttributeCombination = new OrderProductAttributeCombination(); $orderAttributeCombination = new OrderProductAttributeCombination();
$orderAttributeCombination $orderAttributeCombination
@@ -272,28 +267,67 @@ class Order extends BaseAction implements EventSubscriberInterface
->setAttributeAvChapo($attributeAv->getChapo()) ->setAttributeAvChapo($attributeAv->getChapo())
->setAttributeAvDescription($attributeAv->getDescription()) ->setAttributeAvDescription($attributeAv->getDescription())
->setAttributeAvPostscriptum($attributeAv->getPostscriptum()) ->setAttributeAvPostscriptum($attributeAv->getPostscriptum())
; ->save($con);
$orderAttributeCombination->save($con);
} }
} }
$con->commit(); $con->commit();
return $placedOrder;
}
/**
* Create an order outside of the front-office context, e.g. manually from the back-office.
*/
public function createManual(OrderManualEvent $event) {
$placedOrder = $this->createOrder(
$event->getOrder(),
$event->getCurrency(),
$event->getLang(),
$event->getCart(),
$event->getCustomer()
);
}
/**
* @param OrderEvent $event
*
* @throws \Thelia\Exception\TheliaProcessException
*/
public function create(OrderEvent $event)
{
$session = $this->getSession();
$placedOrder = $this->createOrder(
$event->getOrder(),
$session->getCurrency(),
$session->getLang(),
$session->getCart(),
$this->getSecurityContext()->getCustomerUser()
);
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_BEFORE_PAYMENT, new OrderEvent($placedOrder)); $this->getDispatcher()->dispatch(TheliaEvents::ORDER_BEFORE_PAYMENT, new OrderEvent($placedOrder));
/* clear session */ /* clear session */
$session
->setProcessedOrder($placedOrder)
->setOrder(new \Thelia\Model\Order())
;
/* but memorize placed order */ /* but memorize placed order */
$sessionOrder = new \Thelia\Model\Order(); $event->setOrder(new \Thelia\Model\Order());
$event->setOrder($sessionOrder);
$event->setPlacedOrder($placedOrder); $event->setPlacedOrder($placedOrder);
$this->getSession()->setProcessedOrder($placedOrder);
$this->getSession()->setOrder(new \Thelia\Model\Order());
/* empty cart */ /* empty cart */
$this->getDispatcher()->dispatch(TheliaEvents::CART_CLEAR, new CartEvent($this->getCart($this->getRequest()))); $this->getDispatcher()->dispatch(TheliaEvents::CART_CLEAR, new CartEvent($this->getCart($this->getRequest())));
/* call pay method */ /* call pay method */
$paymentModule = ModuleQuery::create()->findPk($placedOrder->getPaymentModuleId());
$paymentModuleInstance = $this->container->get(sprintf('module.%s', $paymentModule->getCode())); $paymentModuleInstance = $this->container->get(sprintf('module.%s', $paymentModule->getCode()));
$paymentModuleInstance->pay($placedOrder); $paymentModuleInstance->pay($placedOrder);
} }
@@ -435,6 +469,7 @@ class Order extends BaseAction implements EventSubscriberInterface
TheliaEvents::ORDER_UPDATE_STATUS => array("updateStatus", 128), TheliaEvents::ORDER_UPDATE_STATUS => array("updateStatus", 128),
TheliaEvents::ORDER_UPDATE_DELIVERY_REF => array("updateDeliveryRef", 128), TheliaEvents::ORDER_UPDATE_DELIVERY_REF => array("updateDeliveryRef", 128),
TheliaEvents::ORDER_UPDATE_ADDRESS => array("updateAddress", 128), TheliaEvents::ORDER_UPDATE_ADDRESS => array("updateAddress", 128),
TheliaEvents::ORDER_CREATE_MANUAL => array("createManual", 128),
); );
} }