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_Order extends Varien_Object
35: {
36: 37: 38: 39: 40: 41:
42: public function toQuote(Mage_Sales_Model_Order $order, $quote=null)
43: {
44: if (!($quote instanceof Mage_Sales_Model_Quote)) {
45: $quote = Mage::getModel('sales/quote');
46: }
47:
48: $quote->setStoreId($order->getStoreId())
49: ->setOrderId($order->getId());
50:
51: Mage::helper('core')->copyFieldset('sales_convert_order', 'to_quote', $order, $quote);
52:
53: Mage::dispatchEvent('sales_convert_order_to_quote', array('order'=>$order, 'quote'=>$quote));
54: return $quote;
55: }
56:
57: 58: 59: 60: 61: 62:
63: public function toQuoteShippingAddress(Mage_Sales_Model_Order $order)
64: {
65: $address = $this->addressToQuoteAddress($order->getShippingAddress());
66:
67: Mage::helper('core')->copyFieldset('sales_convert_order', 'to_quote_address', $order, $address);
68: return $address;
69: }
70:
71: 72: 73: 74: 75: 76:
77: public function addressToQuoteAddress(Mage_Sales_Model_Order_Address $address)
78: {
79: $quoteAddress = Mage::getModel('sales/quote_address')
80: ->setStoreId($address->getStoreId())
81: ->setAddressType($address->getAddressType())
82: ->setCustomerId($address->getCustomerId())
83: ->setCustomerAddressId($address->getCustomerAddressId());
84:
85: Mage::helper('core')->copyFieldset('sales_convert_order_address', 'to_quote_address', $address, $quoteAddress);
86: return $quoteAddress;
87: }
88:
89: 90: 91: 92: 93: 94:
95: public function paymentToQuotePayment(Mage_Sales_Model_Order_Payment $payment, $quotePayment=null)
96: {
97: if (!($quotePayment instanceof Mage_Sales_Model_Quote_Payment)) {
98: $quotePayment = Mage::getModel('sales/quote_payment');
99: }
100:
101: $quotePayment->setStoreId($payment->getStoreId())
102: ->setCustomerPaymentId($payment->getCustomerPaymentId());
103:
104: Mage::helper('core')->copyFieldset('sales_convert_order_payment', 'to_quote_payment', $payment, $quotePayment);
105: return $quotePayment;
106: }
107:
108: 109: 110: 111: 112: 113:
114: public function itemToQuoteItem(Mage_Sales_Model_Order_Item $item)
115: {
116: $quoteItem = Mage::getModel('sales/quote_item')
117: ->setStoreId($item->getOrder()->getStoreId())
118: ->setQuoteItemId($item->getId())
119: ->setProductId($item->getProductId())
120: ->setParentProductId($item->getParentProductId());
121:
122: Mage::helper('core')->copyFieldset('sales_convert_order_item', 'to_quote_item', $item, $quoteItem);
123: return $quoteItem;
124: }
125:
126: 127: 128: 129: 130: 131:
132: public function toInvoice(Mage_Sales_Model_Order $order)
133: {
134: $invoice = Mage::getModel('sales/order_invoice');
135: $invoice->setOrder($order)
136: ->setStoreId($order->getStoreId())
137: ->setCustomerId($order->getCustomerId())
138: ->setBillingAddressId($order->getBillingAddressId())
139: ->setShippingAddressId($order->getShippingAddressId());
140:
141: Mage::helper('core')->copyFieldset('sales_convert_order', 'to_invoice', $order, $invoice);
142: return $invoice;
143: }
144:
145: 146: 147: 148: 149: 150:
151: public function itemToInvoiceItem(Mage_Sales_Model_Order_Item $item)
152: {
153: $invoiceItem = Mage::getModel('sales/order_invoice_item');
154: $invoiceItem->setOrderItem($item)
155: ->setProductId($item->getProductId());
156:
157: Mage::helper('core')->copyFieldset('sales_convert_order_item', 'to_invoice_item', $item, $invoiceItem);
158: return $invoiceItem;
159: }
160:
161: 162: 163: 164: 165: 166:
167: public function toShipment(Mage_Sales_Model_Order $order)
168: {
169: $shipment = Mage::getModel('sales/order_shipment');
170: $shipment->setOrder($order)
171: ->setStoreId($order->getStoreId())
172: ->setCustomerId($order->getCustomerId())
173: ->setBillingAddressId($order->getBillingAddressId())
174: ->setShippingAddressId($order->getShippingAddressId());
175:
176: Mage::helper('core')->copyFieldset('sales_convert_order', 'to_shipment', $order, $shipment);
177: return $shipment;
178: }
179:
180: 181: 182: 183: 184: 185:
186: public function itemToShipmentItem(Mage_Sales_Model_Order_Item $item)
187: {
188: $shipmentItem = Mage::getModel('sales/order_shipment_item');
189: $shipmentItem->setOrderItem($item)
190: ->setProductId($item->getProductId());
191:
192: Mage::helper('core')->copyFieldset('sales_convert_order_item', 'to_shipment_item', $item, $shipmentItem);
193: return $shipmentItem;
194: }
195:
196: 197: 198: 199: 200: 201:
202: public function toCreditmemo(Mage_Sales_Model_Order $order)
203: {
204: $creditmemo = Mage::getModel('sales/order_creditmemo');
205: $creditmemo->setOrder($order)
206: ->setStoreId($order->getStoreId())
207: ->setCustomerId($order->getCustomerId())
208: ->setBillingAddressId($order->getBillingAddressId())
209: ->setShippingAddressId($order->getShippingAddressId());
210:
211: Mage::helper('core')->copyFieldset('sales_convert_order', 'to_cm', $order, $creditmemo);
212: return $creditmemo;
213: }
214:
215: 216: 217: 218: 219: 220:
221: public function itemToCreditmemoItem(Mage_Sales_Model_Order_Item $item)
222: {
223: $creditmemoItem = Mage::getModel('sales/order_creditmemo_item');
224: $creditmemoItem->setOrderItem($item)
225: ->setProductId($item->getProductId());
226:
227: Mage::helper('core')->copyFieldset('sales_convert_order_item', 'to_cm_item', $item, $creditmemoItem);
228: return $creditmemoItem;
229: }
230: }
231: