00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 class Mage_Adminhtml_Model_Sales_Order_Create extends Varien_Object
00033 {
00034
00035
00036
00037
00038
00039 protected $_session;
00040
00041
00042
00043
00044
00045
00046 protected $_wishlist;
00047 protected $_cart;
00048 protected $_compareList;
00049
00050 protected $_needCollect;
00051
00052
00053
00054
00055 protected $_customer;
00056
00057 public function __construct()
00058 {
00059 $this->_session = Mage::getSingleton('adminhtml/session_quote');
00060 }
00061
00062
00063
00064
00065
00066
00067
00068 protected function _getQuoteItem($item)
00069 {
00070 if ($item instanceof Mage_Sales_Model_Quote_Item) {
00071 return $item;
00072 }
00073 elseif (is_numeric($item)) {
00074 return $this->getSession()->getQuote()->getItemById($item);
00075 }
00076 return false;
00077 }
00078
00079
00080
00081
00082
00083
00084 public function initRuleData()
00085 {
00086 Mage::register('rule_data', new Varien_Object(array(
00087 'store_id' => $this->_session->getStore()->getId(),
00088 'website_id' => $this->_session->getStore()->getWebsiteId(),
00089 'customer_group_id' => $this->getCustomerGroupId(),
00090 )));
00091 return $this;
00092 }
00093
00094
00095
00096
00097
00098
00099
00100 public function setRecollect($flag)
00101 {
00102 $this->_needCollect = $flag;
00103 return $this;
00104 }
00105
00106
00107
00108
00109
00110
00111 public function saveQuote()
00112 {
00113 if (!$this->getQuote()->getId()) {
00114 return $this;
00115 }
00116
00117 if ($this->_needCollect) {
00118 $this->getQuote()->collectTotals();
00119 }
00120 $this->getQuote()->save();
00121 return $this;
00122 }
00123
00124
00125
00126
00127
00128
00129 public function getSession()
00130 {
00131 return $this->_session;
00132 }
00133
00134
00135
00136
00137
00138
00139 public function getQuote()
00140 {
00141 return $this->getSession()->getQuote();
00142 }
00143
00144
00145
00146
00147
00148
00149
00150 public function initFromOrder(Mage_Sales_Model_Order $order)
00151 {
00152 if (!$order->getReordered()) {
00153 $this->getSession()->setOrderId($order->getId());
00154 } else {
00155 $this->getSession()->setReordered($order->getId());
00156 }
00157
00158 $this->getSession()->setCurrencyId($order->getOrderCurrencyCode());
00159 $this->getSession()->setCustomerId($order->getCustomerId());
00160 $this->getSession()->setStoreId($order->getStoreId());
00161
00162 foreach ($order->getItemsCollection(
00163 array_keys(Mage::getConfig()->getNode('adminhtml/sales/order/create/available_product_types')->asArray()),
00164 true
00165 ) as $orderItem) {
00166
00167 if (!$orderItem->getParentItem()) {
00168 if ($order->getReordered()) {
00169 $qty = $orderItem->getQtyOrdered();
00170 }
00171 else {
00172 $qty = $orderItem->getQtyOrdered() - $orderItem->getQtyShipped() - $orderItem->getQtyInvoiced();
00173 }
00174
00175 if ($qty > 0) {
00176 $item = $this->initFromOrderItem($orderItem, $qty);
00177 if (is_string($item)) {
00178 Mage::throwException($item);
00179 }
00180 }
00181 }
00182 }
00183
00184 $this->_initBillingAddressFromOrder($order);
00185 $this->_initShippingAddressFromOrder($order);
00186
00187 $this->setShippingMethod($order->getShippingMethod());
00188 $this->getQuote()->getShippingAddress()->setShippingDescription($order->getShippingDescription());
00189
00190 $this->getQuote()->getPayment()->addData($order->getPayment()->getData());
00191
00192 if ($this->getQuote()->getCouponCode()) {
00193 $this->getQuote()->collectTotals();
00194 }
00195
00196 Mage::helper('core')->copyFieldset(
00197 'sales_copy_order',
00198 'to_edit',
00199 $order,
00200 $this->getQuote()
00201 );
00202
00203 Mage::dispatchEvent('sales_convert_order_to_quote', array(
00204 'order' => $order,
00205 'quote' => $this->getQuote()
00206 ));
00207
00208 if (!$order->getCustomerId()) {
00209 $this->getQuote()->setCustomerIsGuest(true);
00210 }
00211
00212 if ($this->getSession()->getUseOldShippingMethod(true)) {
00213
00214
00215
00216
00217
00218 $this->collectShippingRates();
00219 } else {
00220
00221
00222
00223
00224 $this->collectRates();
00225 }
00226
00227
00228
00229
00230
00231 $this->getQuote()->save();
00232
00233 return $this;
00234 }
00235
00236 protected function _initBillingAddressFromOrder(Mage_Sales_Model_Order $order)
00237 {
00238 $this->getQuote()->getBillingAddress()->setCustomerAddressId('');
00239 Mage::helper('core')->copyFieldset(
00240 'sales_copy_order_billing_address',
00241 'to_order',
00242 $order->getBillingAddress(),
00243 $this->getQuote()->getBillingAddress()
00244 );
00245 }
00246
00247 protected function _initShippingAddressFromOrder(Mage_Sales_Model_Order $order)
00248 {
00249 $this->getQuote()->getShippingAddress()->setCustomerAddressId('');
00250 Mage::helper('core')->copyFieldset(
00251 'sales_copy_order_shipping_address',
00252 'to_order',
00253 $order->getShippingAddress(),
00254 $this->getQuote()->getShippingAddress()
00255 );
00256 }
00257
00258
00259
00260
00261
00262
00263
00264 public function initFromOrderItem(Mage_Sales_Model_Order_Item $orderItem, $qty = 1)
00265 {
00266 if (!$orderItem->getId()) {
00267 return $this;
00268 }
00269
00270 $product = Mage::getModel('catalog/product')
00271 ->setStoreId($this->getSession()->getStoreId())
00272 ->load($orderItem->getProductId());
00273
00274 if ($product->getId()) {
00275 $info = $orderItem->getProductOptionByCode('info_buyRequest');
00276 $info = new Varien_Object($info);
00277 $product->setSkipCheckRequiredOption(true);
00278 $item = $this->getQuote()->addProduct($product,$info);
00279 if (is_string($item)) {
00280 return $item;
00281 }
00282 $item->setQty($qty);
00283 if ($additionalOptions = $orderItem->getProductOptionByCode('additional_options')) {
00284 $item->addOption(new Varien_Object(
00285 array(
00286 'product' => $item->getProduct(),
00287 'code' => 'additional_options',
00288 'value' => serialize($additionalOptions)
00289 )
00290 ));
00291 }
00292 Mage::dispatchEvent('sales_convert_order_item_to_quote_item', array(
00293 'order_item' => $orderItem,
00294 'quote_item' => $item
00295 ));
00296
00297 return $item;
00298 }
00299
00300 return $this;
00301 }
00302
00303
00304
00305
00306
00307
00308 public function getCustomerWishlist()
00309 {
00310 if (!is_null($this->_wishlist)) {
00311 return $this->_wishlist;
00312 }
00313
00314 if ($this->getSession()->getCustomer()->getId()) {
00315 $this->_wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer(
00316 $this->getSession()->getCustomer(), true
00317 );
00318 $this->_wishlist->setStore($this->getSession()->getStore())
00319 ->setSharedStoreIds($this->getSession()->getStore()->getWebsite()->getStoreIds());
00320 }
00321 else {
00322 $this->_wishlist = false;
00323 }
00324
00325 return $this->_wishlist;
00326 }
00327
00328
00329
00330
00331
00332
00333 public function getCustomerCart()
00334 {
00335 if (!is_null($this->_cart)) {
00336 return $this->_cart;
00337 }
00338
00339 $this->_cart = Mage::getModel('sales/quote');
00340
00341 if ($this->getSession()->getCustomer()->getId()) {
00342 $this->_cart->setStore($this->getSession()->getStore())
00343 ->loadByCustomer($this->getSession()->getCustomer()->getId());
00344 if (!$this->_cart->getId()) {
00345 $this->_cart->assignCustomer($this->getSession()->getCustomer());
00346 $this->_cart->save();
00347 }
00348 }
00349
00350 return $this->_cart;
00351 }
00352
00353
00354
00355
00356
00357
00358 public function getCustomerCompareList()
00359 {
00360 if (!is_null($this->_compareList)) {
00361 return $this->_compareList;
00362 }
00363
00364 if ($this->getSession()->getCustomer()->getId()) {
00365 $this->_compareList = Mage::getModel('catalog/product_compare_list');
00366 }
00367 else {
00368 $this->_compareList = false;
00369 }
00370 return $this->_compareList;
00371 }
00372
00373 public function getCustomerGroupId()
00374 {
00375 $groupId = $this->getQuote()->getCustomerGroupId();
00376 if (!$groupId) {
00377 $groupId = $this->getSession()->getCustomerGroupId();
00378 }
00379 return $groupId;
00380 }
00381
00382
00383
00384
00385
00386
00387
00388
00389 public function moveQuoteItem($item, $moveTo, $qty)
00390 {
00391 if ($item = $this->_getQuoteItem($item)) {
00392 switch ($moveTo) {
00393 case 'order':
00394 $info = array();
00395 if ($info = $item->getOptionByCode('info_buyRequest')) {
00396 $info = new Varien_Object(
00397 unserialize($info->getValue())
00398 );
00399 $info->setOptions($this->_prepareOptionsForRequest($item));
00400 }
00401
00402 $product = Mage::getModel('catalog/product')
00403 ->setStoreId($this->getQuote()->getStoreId())
00404 ->load($item->getProduct()->getId());
00405
00406 $product->setSkipCheckRequiredOption(true);
00407
00408 $newItem = $this->getQuote()->addProduct($product, $info);
00409 if (is_string($newItem)) {
00410 Mage::throwException($newItem);
00411 }
00412 $product->unsSkipCheckRequiredOption();
00413 $newItem->checkData();
00414 $newItem->setQty($qty);
00415 $this->getQuote()->collectTotals()
00416 ->save();
00417 break;
00418 case 'cart':
00419 if (($cart = $this->getCustomerCart()) && is_null($item->getOptionByCode('additional_options'))) {
00420
00421 $product = Mage::getModel('catalog/product')
00422 ->setStoreId($this->getQuote()->getStoreId())
00423 ->load($item->getProduct()->getId());
00424 $product->setSkipCheckRequiredOption(true);
00425
00426 $info = array();
00427 if ($info = $item->getOptionByCode('info_buyRequest')) {
00428 $info = new Varien_Object(
00429 unserialize($info->getValue())
00430 );
00431 $info->setOptions($this->_prepareOptionsForRequest($item));
00432 } else {
00433 $info = new Varien_Object(array(
00434 'product_id' => $product->getId(),
00435 'qty' => $qty,
00436 'options' => $this->_prepareOptionsForRequest($item)
00437 ));
00438 }
00439
00440 $cartItem = $cart->addProduct($product, $info);
00441 if (is_string($cartItem)) {
00442 Mage::throwException($cartItem);
00443 }
00444 $product->unsSkipCheckRequiredOption();
00445 $cartItem->setQty($qty);
00446 $cartItem->setPrice($item->getProduct()->getPrice());
00447 $cart->collectTotals()
00448 ->save();
00449 }
00450 break;
00451 case 'wishlist':
00452 if ($wishlist = $this->getCustomerWishlist()) {
00453 $wishlist->addNewItem($item->getProduct()->getId());
00454 }
00455 break;
00456 case 'comparelist':
00457
00458 break;
00459 default:
00460 break;
00461 }
00462 $this->getQuote()->removeItem($item->getId());
00463 $this->setRecollect(true);
00464 }
00465 return $this;
00466 }
00467
00468 public function applySidebarData($data)
00469 {
00470 if (isset($data['add'])) {
00471 foreach ($data['add'] as $productId => $qty) {
00472 $this->addProduct($productId, $qty);
00473 }
00474 }
00475 if (isset($data['reorder'])) {
00476 foreach ($data['reorder'] as $orderItemId=>$value) {
00477 $orderItem = Mage::getModel('sales/order_item')->load($orderItemId);
00478 $item = $this->initFromOrderItem($orderItem);
00479 if (is_string($item)) {
00480 Mage::throwException($item);
00481 }
00482 }
00483 }
00484 if (isset($data['cartItem'])) {
00485 foreach ($data['cartItem'] as $itemId => $qty) {
00486 if ($item = $this->getCustomerCart()->getItemById($itemId)) {
00487 $this->moveQuoteItem($item, 'order', $qty);
00488
00489 }
00490 }
00491 }
00492 if (isset($data['remove'])) {
00493 foreach ($data['remove'] as $itemId => $from) {
00494 $this->removeItem($itemId, $from);
00495 }
00496 }
00497 return $this;
00498 }
00499
00500
00501
00502
00503
00504
00505
00506
00507 public function removeItem($itemId, $from)
00508 {
00509 switch ($from) {
00510 case 'quote':
00511 $this->removeQuoteItem($itemId);
00512 break;
00513 case 'cart':
00514 if ($cart = $this->getCustomerCart()) {
00515 $cart->removeItem($itemId);
00516 $cart->collectTotals()
00517 ->save();
00518 }
00519 break;
00520 case 'wishlist':
00521 if ($wishlist = $this->getCustomerWishlist()) {
00522 $item = Mage::getModel('wishlist/item')->load($itemId);
00523 $item->delete();
00524 }
00525 break;
00526 case 'compared':
00527 $item = Mage::getModel('catalog/product_compare_item')
00528 ->load($itemId)
00529 ->delete();
00530 break;
00531 }
00532 return $this;
00533 }
00534
00535
00536
00537
00538
00539
00540
00541 public function removeQuoteItem($item)
00542 {
00543 $this->getQuote()->removeItem($item);
00544 $this->setRecollect(true);
00545 return $this;
00546 }
00547
00548
00549
00550
00551
00552
00553
00554
00555 public function addProduct($product, $qty=1)
00556 {
00557 $qty = (int) $qty;
00558 if (!($product instanceof Mage_Catalog_Model_Product)) {
00559 $productId = $product;
00560 $product = Mage::getModel('catalog/product')
00561 ->setStore($this->getSession()->getStore())
00562 ->setStoreId($this->getSession()->getStoreId())
00563 ->load($product);
00564 if (!$product->getId()) {
00565 Mage::throwException(Mage::helper('adminhtml')->__('Failed to add a product to cart by id "%s"', $productId));
00566 }
00567 }
00568
00569 if ($item = $this->getQuote()->getItemByProduct($product)) {
00570 $item->setQty($item->getQty()+$qty);
00571 }
00572 else {
00573 $product->setSkipCheckRequiredOption(true);
00574 $item = $this->getQuote()->addProduct($product, $qty);
00575 $product->unsSkipCheckRequiredOption();
00576 $item->checkData();
00577 }
00578
00579 $this->setRecollect(true);
00580 return $this;
00581 }
00582
00583
00584
00585
00586
00587
00588
00589 public function addProducts(array $products)
00590 {
00591 foreach ($products as $productId => $data) {
00592 $qty = isset($data['qty']) ? (int)$data['qty'] : 1;
00593 try {
00594 $this->addProduct($productId, $qty);
00595 }
00596 catch (Mage_Core_Exception $e){
00597 $this->getSession()->addError($e->getMessage());
00598 }
00599 catch (Exception $e){
00600 return $e;
00601 }
00602 }
00603 return $this;
00604 }
00605
00606
00607
00608
00609
00610
00611
00612 public function updateQuoteItems($data)
00613 {
00614 if (is_array($data)) {
00615 foreach ($data as $itemId => $info) {
00616 $itemQty = (int) $info['qty'];
00617 $itemQty = $itemQty>0 ? $itemQty : 1;
00618 if (isset($info['custom_price'])) {
00619 $itemPrice = $this->_parseCustomPrice($info['custom_price']);
00620 }
00621 else {
00622 $itemPrice = null;
00623 }
00624 $noDiscount = !isset($info['use_discount']);
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641 if (empty($info['action'])) {
00642 if ($item = $this->getQuote()->getItemById($itemId)) {
00643
00644 $item->setQty($itemQty);
00645 $item->setCustomPrice($itemPrice);
00646 $item->setNoDiscount($noDiscount);
00647 $item->getProduct()->setIsSuperMode(true);
00648
00649 $this->_assignOptionsToItem(
00650 $item,
00651 $this->_parseOptions($item, $info['options'])
00652 );
00653 $item->checkData();
00654 }
00655 }
00656 else {
00657 $this->moveQuoteItem($itemId, $info['action'], $itemQty);
00658 }
00659 }
00660 $this->setRecollect(true);
00661 }
00662 return $this;
00663 }
00664
00665
00666
00667
00668
00669
00670
00671 protected function _parseOptions(Mage_Sales_Model_Quote_Item $item, $additionalOptions)
00672 {
00673 $productOptions = Mage::getSingleton('catalog/product_option_type_default')
00674 ->setProduct($item->getProduct())
00675 ->getProductOptions();
00676
00677 $newOptions = array();
00678 $newAdditionalOptions = array();
00679
00680 foreach (explode("\n", $additionalOptions) as $_additionalOption) {
00681 if (strlen(trim($_additionalOption))) {
00682 try {
00683 list($label,$value) = explode(':', $_additionalOption, 2);
00684 } catch (Exception $e) {
00685 Mage::throwException(Mage::helper('adminhtml')->__('One of options row has error'));
00686 }
00687 $label = trim($label);
00688 $value = trim($value);
00689 if (empty($value)) {
00690 die($label);
00691 continue;
00692 }
00693
00694 if (array_key_exists($label, $productOptions)) {
00695 $optionId = $productOptions[$label]['option_id'];
00696 $option = $item->getProduct()->getOptionById($optionId);
00697
00698 $group = Mage::getSingleton('catalog/product_option')->groupFactory($option->getType())
00699 ->setOption($option)
00700 ->setProduct($item->getProduct());
00701
00702 $parsedValue = $group->parseOptionValue($value, $productOptions[$label]['values']);
00703
00704 if ($parsedValue !== null) {
00705 $newOptions[$optionId] = $parsedValue;
00706 } else {
00707 $newAdditionalOptions[] = array(
00708 'label' => $label,
00709 'value' => $value
00710 );
00711 }
00712 } else {
00713 $newAdditionalOptions[] = array(
00714 'label' => $label,
00715 'value' => $value
00716 );
00717 }
00718 }
00719 }
00720
00721 return array(
00722 'options' => $newOptions,
00723 'additional_options' => $newAdditionalOptions
00724 );
00725 }
00726
00727
00728
00729
00730
00731
00732
00733 protected function _assignOptionsToItem(Mage_Sales_Model_Quote_Item $item, $options)
00734 {
00735 if ($optionIds = $item->getOptionByCode('option_ids')) {
00736 foreach (explode(',', $optionIds->getValue()) as $optionId) {
00737 $item->removeOption('option_'.$optionId);
00738 }
00739 $item->removeOption('option_ids');
00740 }
00741 if ($item->getOptionByCode('additional_options')) {
00742 $item->removeOption('additional_options');
00743 }
00744 $item->save();
00745 if (!empty($options['options'])) {
00746 $item->addOption(new Varien_Object(
00747 array(
00748 'product' => $item->getProduct(),
00749 'code' => 'option_ids',
00750 'value' => implode(',', array_keys($options['options']))
00751 )
00752 ));
00753
00754 foreach ($options['options'] as $optionId => $optionValue) {
00755 $item->addOption(new Varien_Object(
00756 array(
00757 'product' => $item->getProduct(),
00758 'code' => 'option_'.$optionId,
00759 'value' => $optionValue
00760 )
00761 ));
00762 }
00763 }
00764 if (!empty($options['additional_options'])) {
00765 $item->addOption(new Varien_Object(
00766 array(
00767 'product' => $item->getProduct(),
00768 'code' => 'additional_options',
00769 'value' => serialize($options['additional_options'])
00770 )
00771 ));
00772 }
00773
00774 return $this;
00775 }
00776
00777
00778
00779
00780
00781
00782
00783 protected function _prepareOptionsForRequest($item)
00784 {
00785 $newInfoOptions = array();
00786 if ($optionIds = $item->getOptionByCode('option_ids')) {
00787 foreach (explode(',', $optionIds->getValue()) as $optionId) {
00788 $option = $item->getProduct()->getOptionById($optionId);
00789 $optionValue = $item->getOptionByCode('option_'.$optionId)->getValue();
00790
00791 $group = Mage::getSingleton('catalog/product_option')->groupFactory($option->getType())
00792 ->setOption($option)
00793 ->setQuoteItem($item);
00794
00795 $newInfoOptions[$optionId] = $group->prepareOptionValueForRequest($optionValue);
00796 }
00797 }
00798 return $newInfoOptions;
00799 }
00800
00801 protected function _parseCustomPrice($price)
00802 {
00803 $price = Mage::app()->getLocale()->getNumber($price);
00804 $price = $price>0 ? $price : 0;
00805 return $price;
00806 }
00807
00808
00809
00810
00811
00812
00813 public function getShippingAddress()
00814 {
00815 return $this->getQuote()->getShippingAddress();
00816 }
00817
00818 public function setShippingAddress($address)
00819 {
00820 if (is_array($address)) {
00821 $address['save_in_address_book'] = isset($address['save_in_address_book']) ? (empty($address['save_in_address_book']) ? 0 : 1) : 0;
00822 $shippingAddress = Mage::getModel('sales/quote_address')
00823 ->setData($address);
00824 $shippingAddress->implodeStreetAddress();
00825 }
00826 if ($address instanceof Mage_Sales_Model_Quote_Address) {
00827 $shippingAddress = $address;
00828 }
00829
00830 $this->setRecollect(true);
00831 $this->getQuote()->setShippingAddress($shippingAddress);
00832 return $this;
00833 }
00834
00835 public function setShippingAsBilling($flag)
00836 {
00837 if ($flag) {
00838 $tmpAddress = clone $this->getBillingAddress();
00839 $tmpAddress->unsAddressId()
00840 ->unsAddressType();
00841 $this->getShippingAddress()->addData($tmpAddress->getData());
00842 }
00843 $this->getShippingAddress()->setSameAsBilling($flag);
00844 $this->setRecollect(true);
00845 return $this;
00846 }
00847
00848
00849
00850
00851
00852
00853 public function getBillingAddress()
00854 {
00855 return $this->getQuote()->getBillingAddress();
00856 }
00857
00858 public function setBillingAddress($address)
00859 {
00860 if (is_array($address)) {
00861 $address['save_in_address_book'] = isset($address['save_in_address_book']) ? 1 : 0;
00862 $billingAddress = Mage::getModel('sales/quote_address')
00863 ->setData($address);
00864 $billingAddress->implodeStreetAddress();
00865 }
00866
00867 if ($this->getShippingAddress()->getSameAsBilling()) {
00868 $shippingAddress = clone $billingAddress;
00869 $shippingAddress->setSameAsBilling(true);
00870 $shippingAddress->setSaveInAddressBook(false);
00871 $address['save_in_address_book'] = 0;
00872 $this->setShippingAddress($address);
00873 }
00874
00875 $this->getQuote()->setBillingAddress($billingAddress);
00876 return $this;
00877 }
00878
00879 public function setShippingMethod($method)
00880 {
00881 $this->getShippingAddress()->setShippingMethod($method);
00882 $this->setRecollect(true);
00883 return $this;
00884 }
00885
00886 public function resetShippingMethod()
00887 {
00888 $this->getShippingAddress()->setShippingMethod(false);
00889 $this->getShippingAddress()->removeAllShippingRates();
00890 return $this;
00891 }
00892
00893 public function collectShippingRates()
00894 {
00895 $this->collectRates();
00896 $this->getQuote()->getShippingAddress()->setCollectShippingRates(true);
00897 $this->getQuote()->getShippingAddress()->collectShippingRates();
00898 return $this;
00899 }
00900
00901 public function collectRates()
00902 {
00903 $this->getQuote()->collectTotals();
00904 }
00905
00906 public function setPaymentMethod($method)
00907 {
00908 $this->getQuote()->getPayment()->setMethod($method);
00909 return $this;
00910 }
00911
00912 public function setPaymentData($data)
00913 {
00914 if (!isset($data['method'])) {
00915 $data['method'] = $this->getQuote()->getPayment()->getMethod();
00916 }
00917 $this->getQuote()->getPayment()->importData($data);
00918 return $this;
00919 }
00920
00921 public function applyCoupon($code)
00922 {
00923 $code = trim((string)$code);
00924 $this->getQuote()->setCouponCode($code);
00925 $this->setRecollect(true);
00926 return $this;
00927 }
00928
00929 public function setAccountData($accountData)
00930 {
00931 $data = array();
00932 foreach ($accountData as $key => $value) {
00933 $data['customer_'.$key] = $value;
00934 }
00935
00936 if (isset($data['customer_group_id'])) {
00937 $groupModel = Mage::getModel('customer/group')->load($data['customer_group_id']);
00938 $data['customer_tax_class_id'] = $groupModel->getTaxClassId();
00939 $this->setRecollect(true);
00940 }
00941
00942 $this->getQuote()->addData($data);
00943 return $this;
00944 }
00945
00946
00947
00948
00949
00950
00951
00952 public function importPostData($data)
00953 {
00954 if (is_array($data)) {
00955 $this->addData($data);
00956 }
00957 else {
00958 return $this;
00959 }
00960
00961 if (isset($data['account'])) {
00962 $this->setAccountData($data['account']);
00963 }
00964
00965 if (isset($data['comment'])) {
00966 $this->getQuote()->addData($data['comment']);
00967 }
00968
00969 if (isset($data['billing_address'])) {
00970 $this->setBillingAddress($data['billing_address']);
00971 }
00972
00973 if (isset($data['shipping_address'])) {
00974 $this->setShippingAddress($data['shipping_address']);
00975 }
00976
00977 if (isset($data['shipping_method'])) {
00978 $this->setShippingMethod($data['shipping_method']);
00979 }
00980
00981 if (isset($data['payment_method'])) {
00982 $this->setPaymentMethod($data['payment_method']);
00983 }
00984
00985 if (isset($data['coupon']['code'])) {
00986 $this->applyCoupon($data['coupon']['code']);
00987 }
00988
00989 return $this;
00990 }
00991
00992
00993
00994
00995
00996
00997 public function createOrder()
00998 {
00999 $this->_validate();
01000
01001 if (!$this->getQuote()->getCustomerIsGuest()) {
01002 $this->_putCustomerIntoQuote();
01003 }
01004
01005 $quoteConvert = Mage::getModel('sales/convert_quote');
01006
01007
01008
01009 $quote = $this->getQuote();
01010 if (!$this->getSession()->getOrder()->getId()) {
01011 $quote->reserveOrderId();
01012 }
01013
01014 if ($this->getQuote()->getIsVirtual()) {
01015 $order = $quoteConvert->addressToOrder($quote->getBillingAddress());
01016 }
01017 else {
01018 $order = $quoteConvert->addressToOrder($quote->getShippingAddress());
01019 }
01020 $order->setBillingAddress($quoteConvert->addressToOrderAddress($quote->getBillingAddress()))
01021 ->setPayment($quoteConvert->paymentToOrderPayment($quote->getPayment()));
01022 if (!$this->getQuote()->getIsVirtual()) {
01023 $order->setShippingAddress($quoteConvert->addressToOrderAddress($quote->getShippingAddress()));
01024 }
01025
01026 if (!$this->getQuote()->getIsVirtual()) {
01027 foreach ($quote->getShippingAddress()->getAllItems() as $item) {
01028
01029 $orderItem = $quoteConvert->itemToOrderItem($item);
01030 $options = array();
01031 if ($productOptions = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct())) {
01032 $productOptions['info_buyRequest']['options'] = $this->_prepareOptionsForRequest($item);
01033 $options = $productOptions;
01034 }
01035 if ($addOptions = $item->getOptionByCode('additional_options')) {
01036 $options['additional_options'] = unserialize($addOptions->getValue());
01037 }
01038 if ($options) {
01039 $orderItem->setProductOptions($options);
01040 }
01041
01042 if ($item->getParentItem()) {
01043 $orderItem->setParentItem($order->getItemByQuoteItemId($item->getParentItem()->getId()));
01044 }
01045
01046 $order->addItem($orderItem);
01047 }
01048 }
01049 if ($this->getQuote()->hasVirtualItems()) {
01050 foreach ($quote->getBillingAddress()->getAllItems() as $item) {
01051
01052 $orderItem = $quoteConvert->itemToOrderItem($item);
01053 $options = array();
01054 if ($productOptions = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct())) {
01055 $productOptions['info_buyRequest']['options'] = $this->_prepareOptionsForRequest($item);
01056 $options = $productOptions;
01057 }
01058 if ($addOptions = $item->getOptionByCode('additional_options')) {
01059 $options['additional_options'] = unserialize($addOptions->getValue());
01060 }
01061 if ($options) {
01062 $orderItem->setProductOptions($options);
01063 }
01064
01065 if ($item->getParentItem()) {
01066 $orderItem->setParentItem($order->getItemByQuoteItemId($item->getParentItem()->getId()));
01067 }
01068
01069 $order->addItem($orderItem);
01070 }
01071 }
01072
01073 if ($this->getSendConfirmation()) {
01074 $order->setEmailSent(true);
01075 }
01076
01077 if ($this->getSession()->getOrder()->getId()) {
01078 $oldOrder = $this->getSession()->getOrder();
01079
01080 $originalId = $oldOrder->getOriginalIncrementId() ? $oldOrder->getOriginalIncrementId() : $oldOrder->getIncrementId();
01081 $order->setOriginalIncrementId($originalId);
01082 $order->setRelationParentId($oldOrder->getId());
01083 $order->setRelationParentRealId($oldOrder->getIncrementId());
01084 $order->setEditIncrement($oldOrder->getEditIncrement()+1);
01085 $order->setIncrementId($originalId.'-'.$order->getEditIncrement());
01086 }
01087
01088 $order->place();
01089 $this->_saveCustomerAfterOrder($order);
01090 $order->save();
01091
01092 if ($this->getSession()->getOrder()->getId()) {
01093 $oldOrder = $this->getSession()->getOrder();
01094
01095 $this->getSession()->getOrder()->setRelationChildId($order->getId());
01096 $this->getSession()->getOrder()->setRelationChildRealId($order->getIncrementId());
01097 $this->getSession()->getOrder()->cancel()
01098 ->save();
01099 $order->save();
01100 }
01101
01102 if ($this->getSendConfirmation()) {
01103 $order->sendNewOrderEmail();
01104 }
01105
01106 return $order;
01107 }
01108
01109
01110
01111
01112
01113
01114 protected function _validate()
01115 {
01116 $customerId = $this->getSession()->getCustomerId();
01117 if (is_null($customerId)) {
01118 Mage::throwException(Mage::helper('adminhtml')->__('Please select a custmer'));
01119 }
01120
01121 if (!$this->getSession()->getStore()->getId()) {
01122 Mage::throwException(Mage::helper('adminhtml')->__('Please select a store'));
01123 }
01124 $items = $this->getQuote()->getAllItems();
01125
01126 $errors = array();
01127 if (count($items) == 0) {
01128 $errors[] = Mage::helper('adminhtml')->__('You need to specify order items');
01129 }
01130
01131 if (!$this->getQuote()->isVirtual()) {
01132 if (!$this->getQuote()->getShippingAddress()->getShippingMethod()) {
01133 $errors[] = Mage::helper('adminhtml')->__('Shipping method must be specified');
01134 }
01135
01136 if (!$this->getQuote()->getPayment()->getMethod()) {
01137 $errors[] = Mage::helper('adminhtml')->__('Payment method must be specified');
01138 }
01139 }
01140
01141 if (!empty($errors)) {
01142 foreach ($errors as $error) {
01143 $this->getSession()->addError($error);
01144 }
01145 Mage::throwException('');
01146 }
01147 return $this;
01148 }
01149
01150
01151
01152
01153 protected function _putCustomerIntoQuote()
01154 {
01155 if (!$this->getSession()->getCustomer()->getId()) {
01156 $customer = Mage::getModel('customer/customer');
01157
01158
01159 $billingAddress = $this->getBillingAddress()->exportCustomerAddress();
01160
01161 $customer->addData($billingAddress->getData())
01162 ->addData($this->getData('account'))
01163 ->setPassword($customer->generatePassword())
01164 ->setWebsiteId($this->getSession()->getStore()->getWebsiteId())
01165 ->setStoreId($this->getSession()->getStore()->getId())
01166 ->addAddress($billingAddress);
01167
01168 if (!$this->getShippingAddress()->getSameAsBilling()) {
01169 $shippingAddress = $this->getShippingAddress()->exportCustomerAddress();
01170 $customer->addAddress($shippingAddress);
01171 }
01172 else {
01173 $shippingAddress = $billingAddress;
01174 }
01175
01176 $customer->setEmail($this->_getNewCustomerEmail($customer))
01177 ->setDefaultBilling($billingAddress->getId())
01178 ->setDefaultShipping($shippingAddress->getId());
01179 }
01180 else {
01181 $customer = $this->getSession()->getCustomer();
01182 $customer->addData($this->getData('account'));
01183 }
01184 $this->getQuote()->setCustomer($customer);
01185 $this->_customer = $customer;
01186 }
01187
01188
01189
01190
01191
01192
01193 protected function _saveCustomerAfterOrder($order)
01194 {
01195 if ($this->_customer) {
01196 if (!$this->_customer->getId()) {
01197 $this->_customer->save();
01198 $order->setCustomerId($this->_customer->getId());
01199 $this->getBillingAddress()->setCustomerId($this->_customer->getId());
01200 $this->getShippingAddress()->setCustomerId($this->_customer->getId());
01201 $this->_customer->sendNewAccountEmail();
01202 }
01203 else {
01204 $saveCusstomerAddress = false;
01205
01206 if ($this->getBillingAddress()->getSaveInAddressBook()) {
01207 $billingAddress = $this->getBillingAddress()->exportCustomerAddress();
01208 if ($this->getBillingAddress()->getCustomerAddressId()) {
01209 $billingAddress->setId($this->getBillingAddress()->getCustomerAddressId());
01210 }
01211 $this->_customer->addAddress($billingAddress);
01212 $saveCusstomerAddress = true;
01213 }
01214 if ($this->getShippingAddress()->getSaveInAddressBook()) {
01215 $shippingAddress = $this->getShippingAddress()->exportCustomerAddress();
01216 if ($this->getShippingAddress()->getCustomerAddressId()) {
01217 $shippingAddress->setId($this->getShippingAddress()->getCustomerAddressId());
01218 }
01219 $this->_customer->addAddress($shippingAddress);
01220 $saveCusstomerAddress = true;
01221 }
01222 if ($saveCusstomerAddress) {
01223 $this->_customer->save();
01224 }
01225 }
01226 }
01227 }
01228
01229
01230
01231
01232
01233
01234 protected function _saveCustomer()
01235 {
01236 if (!$this->getSession()->getCustomer()->getId()) {
01237 $customer = Mage::getModel('customer/customer');
01238
01239
01240 $billingAddress = $this->getBillingAddress()->exportCustomerAddress();
01241
01242 $customer->addData($billingAddress->getData())
01243 ->addData($this->getData('account'))
01244 ->setPassword($customer->generatePassword())
01245 ->setWebsiteId($this->getSession()->getStore()->getWebsiteId())
01246 ->setStoreId($this->getSession()->getStore()->getId())
01247 ->addAddress($billingAddress);
01248
01249 if (!$this->getShippingAddress()->getSameAsBilling()) {
01250 $shippingAddress = $this->getShippingAddress()->exportCustomerAddress();
01251 $customer->addAddress($shippingAddress);
01252 }
01253 else {
01254 $shippingAddress = $billingAddress;
01255 }
01256 $customer->save();
01257
01258
01259 $customer->setEmail($this->_getNewCustomerEmail($customer))
01260 ->setDefaultBilling($billingAddress->getId())
01261 ->setDefaultShipping($shippingAddress->getId())
01262 ->save();
01263
01264 $this->getBillingAddress()->setCustomerId($customer->getId());
01265 $this->getShippingAddress()->setCustomerId($customer->getId());
01266
01267 $customer->sendNewAccountEmail();
01268 }
01269 else {
01270 $customer = $this->getSession()->getCustomer();
01271
01272 $saveCusstomerAddress = false;
01273
01274 if ($this->getBillingAddress()->getSaveInAddressBook()) {
01275 $billingAddress = $this->getBillingAddress()->exportCustomerAddress();
01276 if ($this->getBillingAddress()->getCustomerAddressId()) {
01277 $billingAddress->setId($this->getBillingAddress()->getCustomerAddressId());
01278 }
01279 $customer->addAddress($billingAddress);
01280 $saveCusstomerAddress = true;
01281 }
01282 if ($this->getShippingAddress()->getSaveInAddressBook()) {
01283 $shippingAddress = $this->getShippingAddress()->exportCustomerAddress();
01284 if ($this->getShippingAddress()->getCustomerAddressId()) {
01285 $shippingAddress->setId($this->getShippingAddress()->getCustomerAddressId());
01286 }
01287 $customer->addAddress($shippingAddress);
01288 $saveCusstomerAddress = true;
01289 }
01290 if ($saveCusstomerAddress) {
01291 $customer->save();
01292 }
01293
01294 $customer->addData($this->getData('account'));
01295
01296
01297
01298
01299 }
01300 $this->getQuote()->setCustomer($customer);
01301 return $this;
01302 }
01303
01304
01305
01306
01307
01308
01309
01310 protected function _getNewCustomerEmail($customer)
01311 {
01312 $email = $this->getData('account/email');
01313 if (empty($email)) {
01314 $host = $this->getSession()->getStore()->getConfig(Mage_Customer_Model_Customer::XML_PATH_DEFAULT_EMAIL_DOMAIN);
01315 $account = $customer->getIncrementId() ? $customer->getIncrementId() : time();
01316 $email = $account.'@'. $host;
01317 }
01318 return $email;
01319 }
01320 }