1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Sales_Model_Convert_Quote extends Varien_Object
35: {
36:
37: 38: 39: 40: 41: 42:
43: public function toOrder(Mage_Sales_Model_Quote $quote, $order=null)
44: {
45: if (!($order instanceof Mage_Sales_Model_Order)) {
46: $order = Mage::getModel('sales/order');
47: }
48:
49:
50: $order->setIncrementId($quote->getReservedOrderId())
51: ->setStoreId($quote->getStoreId())
52: ->setQuoteId($quote->getId())
53: ->setQuote($quote)
54: ->setCustomer($quote->getCustomer());
55:
56: Mage::helper('core')->copyFieldset('sales_convert_quote', 'to_order', $quote, $order);
57: Mage::dispatchEvent('sales_convert_quote_to_order', array('order'=>$order, 'quote'=>$quote));
58: return $order;
59: }
60:
61: 62: 63: 64: 65: 66:
67: public function addressToOrder(Mage_Sales_Model_Quote_Address $address, $order=null)
68: {
69: if (!($order instanceof Mage_Sales_Model_Order)) {
70: $order = $this->toOrder($address->getQuote());
71: }
72:
73: Mage::helper('core')->copyFieldset('sales_convert_quote_address', 'to_order', $address, $order);
74:
75: Mage::dispatchEvent('sales_convert_quote_address_to_order', array('address'=>$address, 'order'=>$order));
76: return $order;
77: }
78:
79: 80: 81: 82: 83: 84:
85: public function addressToOrderAddress(Mage_Sales_Model_Quote_Address $address)
86: {
87: $orderAddress = Mage::getModel('sales/order_address')
88: ->setStoreId($address->getStoreId())
89: ->setAddressType($address->getAddressType())
90: ->setCustomerId($address->getCustomerId())
91: ->setCustomerAddressId($address->getCustomerAddressId());
92:
93: Mage::helper('core')->copyFieldset('sales_convert_quote_address', 'to_order_address', $address, $orderAddress);
94:
95: Mage::dispatchEvent('sales_convert_quote_address_to_order_address',
96: array('address' => $address, 'order_address' => $orderAddress));
97:
98: return $orderAddress;
99: }
100:
101: 102: 103: 104: 105: 106:
107: public function paymentToOrderPayment(Mage_Sales_Model_Quote_Payment $payment)
108: {
109: $orderPayment = Mage::getModel('sales/order_payment')
110: ->setStoreId($payment->getStoreId())
111: ->setCustomerPaymentId($payment->getCustomerPaymentId());
112: Mage::helper('core')->copyFieldset('sales_convert_quote_payment', 'to_order_payment', $payment, $orderPayment);
113:
114: Mage::dispatchEvent('sales_convert_quote_payment_to_order_payment',
115: array('order_payment' => $orderPayment, 'quote_payment' => $payment));
116:
117: return $orderPayment;
118: }
119:
120: 121: 122: 123: 124: 125:
126: public function itemToOrderItem(Mage_Sales_Model_Quote_Item_Abstract $item)
127: {
128: $orderItem = Mage::getModel('sales/order_item')
129: ->setStoreId($item->getStoreId())
130: ->setQuoteItemId($item->getId())
131: ->setQuoteParentItemId($item->getParentItemId())
132: ->setProductId($item->getProductId())
133: ->setProductType($item->getProductType())
134: ->setQtyBackordered($item->getBackorders())
135: ->setProduct($item->getProduct())
136: ->setBaseOriginalPrice($item->getBaseOriginalPrice())
137: ;
138:
139: $options = $item->getProductOrderOptions();
140: if (!$options) {
141: $options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
142: }
143: $orderItem->setProductOptions($options);
144: Mage::helper('core')->copyFieldset('sales_convert_quote_item', 'to_order_item', $item, $orderItem);
145:
146: if ($item->getParentItem()) {
147: $orderItem->setQtyOrdered($orderItem->getQtyOrdered()*$item->getParentItem()->getQty());
148: }
149:
150: if (!$item->getNoDiscount()) {
151: Mage::helper('core')->copyFieldset('sales_convert_quote_item', 'to_order_item_discount', $item, $orderItem);
152: }
153:
154: Mage::dispatchEvent('sales_convert_quote_item_to_order_item',
155: array('order_item'=>$orderItem, 'item'=>$item)
156: );
157: return $orderItem;
158: }
159: }
160: