Implemented order creation outside front-offcie context

This commit is contained in:
Franck Allimant
2014-01-23 19:54:16 +01:00
parent e5fea4bdd2
commit 3c16cac486
4 changed files with 343 additions and 39 deletions

View File

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

View File

@@ -0,0 +1,266 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Core\Event\Order;
use Thelia\Model\Order;
use Thelia\Model\Currency;
use Thelia\Model\Lang;
use Thelia\Model\Cart;
use Thelia\Model\Customer;
class OrderManualEvent extends OrderEvent
{
protected $currency = null;
protected $lang = null;
protected $cart = null;
protected $customer = null;
/**
* @param Order $order
*/
public function __construct(Order $order, Currency $currency, Lang $lang, Cart $cart, Customer $customer)
{
$this
->setOrder($order)
->setCurrency($currency)
->setLang($lang)
->setCart($cart)
->setCustomer($customer)
;
}
/**
* @param Order $order
*/
public function setOrder(Order $order)
{
$this->order = $order;
return $this;
}
/**
* @param Order $order
*/
public function setPlacedOrder(Order $order)
{
$this->placedOrder = $order;
return $this;
}
/**
* @param $address
*/
public function setInvoiceAddress($address)
{
$this->invoiceAddress = $address;
return $this;
}
/**
* @param $address
*/
public function setDeliveryAddress($address)
{
$this->deliveryAddress = $address;
return $this;
}
/**
* @param $module
*/
public function setDeliveryModule($module)
{
$this->deliveryModule = $module;
return $this;
}
/**
* @param $module
*/
public function setPaymentModule($module)
{
$this->paymentModule = $module;
return $this;
}
/**
* @param $postage
*/
public function setPostage($postage)
{
$this->postage = $postage;
return $this;
}
/**
* @param $ref
*/
public function setRef($ref)
{
$this->ref = $ref;
return $this;
}
/**
* @param $status
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* @param $deliveryRef
*/
public function setDeliveryRef($deliveryRef)
{
$this->deliveryRef = $deliveryRef;
}
/**
* @return null|Order
*/
public function getOrder()
{
return $this->order;
}
/**
* @return null|Order
*/
public function getPlacedOrder()
{
return $this->placedOrder;
}
/**
* @return null|int
*/
public function getInvoiceAddress()
{
return $this->invoiceAddress;
}
/**
* @return null|int
*/
public function getDeliveryAddress()
{
return $this->deliveryAddress;
}
/**
* @return null|int
*/
public function getDeliveryModule()
{
return $this->deliveryModule;
}
/**
* @return null|int
*/
public function getPaymentModule()
{
return $this->paymentModule;
}
/**
* @return null|int
*/
public function getPostage()
{
return $this->postage;
}
/**
* @return null|int
*/
public function getRef()
{
return $this->ref;
}
/**
* @return null|int
*/
public function getStatus()
{
return $this->status;
}
/**
* @return null|string
*/
public function getDeliveryRef()
{
return $this->deliveryRef;
}
public function getCurrency()
{
return $this->currency;
}
public function setCurrency($currency)
{
$this->currency = $currency;
return $this;
}
public function getLang()
{
return $this->lang;
}
public function setLang($lang)
{
$this->lang = $lang;
return $this;
}
public function getCart()
{
return $this->cart;
}
public function setCart($cart)
{
$this->cart = $cart;
return $this;
}
public function getCustomer()
{
return $this->customer;
}
public function setCustomer($customer)
{
$this->customer = $customer;
return $this;
}
}

View File

@@ -378,6 +378,8 @@ final class TheliaEvents
const ORDER_AFTER_CREATE = "action.order.afterCreate";
const ORDER_BEFORE_PAYMENT = "action.order.beforePayment";
const ORDER_CREATE_MANUAL = "action.order.createManual";
const ORDER_UPDATE_STATUS = "action.order.updateStatus";
const ORDER_UPDATE_DELIVERY_REF = "action.order.updateDeliveryRef";
const ORDER_UPDATE_ADDRESS = "action.order.updateAddress";

View File

@@ -5,6 +5,8 @@ AddDefaultCharset UTF-8
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /thelia2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d