Skip to content

Commit

Permalink
Merge pull request #1701 from roadster31/order-creation-fix
Browse files Browse the repository at this point in the history
Order::createOrder() improvement to allow order duplication.
  • Loading branch information
bibich committed Jan 4, 2016
2 parents 917e1fb + 3c23ccd commit 7822fca
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 226 deletions.
120 changes: 71 additions & 49 deletions core/lib/Thelia/Action/Order.php
Expand Up @@ -33,9 +33,10 @@
use Thelia\Model\Lang as LangModel;
use Thelia\Model\Map\OrderTableMap;
use Thelia\Model\ModuleQuery;
use Thelia\Model\Order as OrderModel;
use Thelia\Model\Order as ModelOrder;
use Thelia\Model\Order as OrderModel;
use Thelia\Model\OrderAddress;
use Thelia\Model\OrderAddressQuery;
use Thelia\Model\OrderProduct;
use Thelia\Model\OrderProductAttributeCombination;
use Thelia\Model\OrderProductTax;
Expand Down Expand Up @@ -153,6 +154,7 @@ public function setPaymentModule(OrderEvent $event)
* @param CartModel $cart
* @param UserInterface $customer
* @param bool $manageStock decrement stock when order is created if true
* @param bool $useOrderDefinedAddresses if true, the delivery and invoice OrderAddresses will be used instead of creating new OrderAdresses using Order::getChoosenXXXAddress()
* @return ModelOrder
* @throws \Exception
* @throws \Propel\Runtime\Exception\PropelException
Expand All @@ -164,7 +166,8 @@ protected function createOrder(
LangModel $lang,
CartModel $cart,
UserInterface $customer,
$manageStock
$manageStock,
$useOrderDefinedAddresses = false
) {
$con = Propel::getConnection(
OrderTableMap::DATABASE_NAME
Expand All @@ -173,58 +176,76 @@ protected function createOrder(
$con->beginTransaction();

$placedOrder = $sessionOrder->copy();

// Be sure to create a brand new order, as copy raises the modified flag for all fields
// and will also copy order reference and id.
$placedOrder->setId(null)->setRef(null)->setNew(true);

// Dates should be marked as not updated so that Propel will update them.
$placedOrder->resetModified(OrderTableMap::CREATED_AT);
$placedOrder->resetModified(OrderTableMap::UPDATED_AT);
$placedOrder->resetModified(OrderTableMap::VERSION_CREATED_AT);

$placedOrder->setDispatcher($dispatcher);

$deliveryAddress = AddressQuery::create()->findPk($sessionOrder->getChoosenDeliveryAddress());
$taxCountry = $deliveryAddress->getCountry();
$invoiceAddress = AddressQuery::create()->findPk($sessionOrder->getChoosenInvoiceAddress());
$cartItems = $cart->getCartItems();

/* fulfill order */
$placedOrder->setCustomerId($customer->getId());
$placedOrder->setCurrencyId($currency->getId());
$placedOrder->setCurrencyRate($currency->getRate());
$placedOrder->setLangId($lang->getId());

/* hard save the delivery and invoice addresses */
$deliveryOrderAddress = new OrderAddress();
$deliveryOrderAddress
->setCustomerTitleId($deliveryAddress->getTitleId())
->setCompany($deliveryAddress->getCompany())
->setFirstname($deliveryAddress->getFirstname())
->setLastname($deliveryAddress->getLastname())
->setAddress1($deliveryAddress->getAddress1())
->setAddress2($deliveryAddress->getAddress2())
->setAddress3($deliveryAddress->getAddress3())
->setZipcode($deliveryAddress->getZipcode())
->setCity($deliveryAddress->getCity())
->setPhone($deliveryAddress->getPhone())
->setCellphone($deliveryAddress->getCellphone())
->setCountryId($deliveryAddress->getCountryId())
->setStateId($deliveryAddress->getStateId())
->save($con)
;

$invoiceOrderAddress = new OrderAddress();
$invoiceOrderAddress
->setCustomerTitleId($invoiceAddress->getTitleId())
->setCompany($invoiceAddress->getCompany())
->setFirstname($invoiceAddress->getFirstname())
->setLastname($invoiceAddress->getLastname())
->setAddress1($invoiceAddress->getAddress1())
->setAddress2($invoiceAddress->getAddress2())
->setAddress3($invoiceAddress->getAddress3())
->setZipcode($invoiceAddress->getZipcode())
->setCity($invoiceAddress->getCity())
->setPhone($invoiceAddress->getPhone())
->setCellphone($invoiceAddress->getCellphone())
->setCountryId($invoiceAddress->getCountryId())
->setStateId($deliveryAddress->getStateId())
->save($con)
;

$placedOrder->setDeliveryOrderAddressId($deliveryOrderAddress->getId());
$placedOrder->setInvoiceOrderAddressId($invoiceOrderAddress->getId());

if ($useOrderDefinedAddresses) {
$taxCountry =
OrderAddressQuery::create()
->findPk($placedOrder->getDeliveryOrderAddressId())
->getCountry()
;
} else {
$deliveryAddress = AddressQuery::create()->findPk($sessionOrder->getChoosenDeliveryAddress());
$invoiceAddress = AddressQuery::create()->findPk($sessionOrder->getChoosenInvoiceAddress());

/* hard save the delivery and invoice addresses */
$deliveryOrderAddress = new OrderAddress();
$deliveryOrderAddress
->setCustomerTitleId($deliveryAddress->getTitleId())
->setCompany($deliveryAddress->getCompany())
->setFirstname($deliveryAddress->getFirstname())
->setLastname($deliveryAddress->getLastname())
->setAddress1($deliveryAddress->getAddress1())
->setAddress2($deliveryAddress->getAddress2())
->setAddress3($deliveryAddress->getAddress3())
->setZipcode($deliveryAddress->getZipcode())
->setCity($deliveryAddress->getCity())
->setPhone($deliveryAddress->getPhone())
->setCellphone($deliveryAddress->getCellphone())
->setCountryId($deliveryAddress->getCountryId())
->setStateId($deliveryAddress->getStateId())
->save($con);

$invoiceOrderAddress = new OrderAddress();
$invoiceOrderAddress
->setCustomerTitleId($invoiceAddress->getTitleId())
->setCompany($invoiceAddress->getCompany())
->setFirstname($invoiceAddress->getFirstname())
->setLastname($invoiceAddress->getLastname())
->setAddress1($invoiceAddress->getAddress1())
->setAddress2($invoiceAddress->getAddress2())
->setAddress3($invoiceAddress->getAddress3())
->setZipcode($invoiceAddress->getZipcode())
->setCity($invoiceAddress->getCity())
->setPhone($invoiceAddress->getPhone())
->setCellphone($invoiceAddress->getCellphone())
->setCountryId($invoiceAddress->getCountryId())
->setStateId($deliveryAddress->getStateId())
->save($con);

$placedOrder->setDeliveryOrderAddressId($deliveryOrderAddress->getId());
$placedOrder->setInvoiceOrderAddressId($invoiceOrderAddress->getId());

$taxCountry = $deliveryAddress->getCountry();
}

$placedOrder->setStatusId(
OrderStatusQuery::getNotPaidStatus()->getId()
Expand Down Expand Up @@ -361,6 +382,7 @@ protected function createOrder(

/**
* Create an order outside of the front-office context, e.g. manually from the back-office.
* @param OrderManualEvent $event
*/
public function createManual(OrderManualEvent $event)
{
Expand All @@ -377,7 +399,8 @@ public function createManual(OrderManualEvent $event)
$event->getLang(),
$event->getCart(),
$event->getCustomer(),
$paymentModuleInstance->manageStockOnCreation()
$paymentModuleInstance->manageStockOnCreation(),
$event->getUseOrderDefinedAddresses()
)
);

Expand Down Expand Up @@ -502,9 +525,8 @@ public function updateStatus(OrderEvent $event)
}

/**
* @param ModelOrder $order
* @param $newStatus
* @param $canceledStatus
* @param ModelOrder $order
* @param $newStatus $newStatus the new status ID
* @throws \Thelia\Exception\TheliaProcessException
*/
public function updateQuantity(ModelOrder $order, $newStatus)
Expand Down

0 comments on commit 7822fca

Please sign in to comment.