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: class Mage_Adminhtml_Model_Sales_Order_Random
33: {
34: 35: 36: 37: 38:
39: protected $_quote;
40:
41: 42: 43: 44: 45:
46: protected $_order;
47: protected $_store;
48: protected $_customer;
49: protected $_productCollection;
50:
51: protected static $_storeCollection;
52: protected static $_customerCollection;
53:
54: public function __construct()
55: {
56: $this->_quote = Mage::getModel('sales/quote')->save();
57: $this->_order = Mage::getModel('sales/order');
58: }
59:
60: protected function _getStores()
61: {
62: if (!self::$_storeCollection) {
63: self::$_storeCollection = Mage::getResourceModel('core/store_collection')
64: ->load();
65: }
66: return self::$_storeCollection->getItems();
67: }
68:
69: protected function _getCustomers()
70: {
71: if (!self::$_customerCollection) {
72: self::$_customerCollection = Mage::getResourceModel('customer/customer_collection')
73: ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'inner')
74: ->joinAttribute('shipping_country_id', 'customer_address/country_id', 'default_shipping', null, 'inner')
75: ->load();
76: }
77: return self::$_customerCollection->getItems();
78: }
79:
80: protected function _getProducts()
81: {
82: if (!$this->_productCollection) {
83: $this->_productCollection= Mage::getResourceModel('catalog/product_collection');
84:
85: Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($this->_productCollection);
86: Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($this->_productCollection);
87: $this->_productCollection->addAttributeToSelect('name')
88: ->addAttributeToSelect('sku')
89: ->addAttributeToFilter('type_id', Mage_Catalog_Model_Product_Type::TYPE_SIMPLE)
90: ->load();
91: }
92: return $this->_productCollection->getItems();
93: }
94:
95: 96: 97: 98: 99:
100: protected function _getCustomer()
101: {
102: if (!$this->_customer) {
103: $items = $this->_getCustomers();
104: $randKey = array_rand($items);
105: $this->_customer = $items[$randKey];
106: }
107: return $this->_customer;
108: }
109:
110: protected function _getRandomProduct()
111: {
112: $items = $this->_getProducts();
113: $randKey = array_rand($items);
114: return isset($items[$randKey]) ? $items[$randKey] : false;
115: }
116:
117: protected function _getStore()
118: {
119: if (!$this->_store) {
120: $items = $this->_getStores();
121: $randKey = array_rand($items);
122: $this->_store = $items[$randKey];
123: }
124: return $this->_store;
125: }
126:
127: public function render()
128: {
129: $customer = $this->_getCustomer();
130: $this->_quote->setStore($this->_getStore())
131: ->setCustomer($customer);
132: $this->_quote->getBillingAddress()->importCustomerAddress($customer->getDefaultBillingAddress());
133: $this->_quote->getShippingAddress()->importCustomerAddress($customer->getDefaultShippingAddress());
134:
135: $productCount = rand(3, 10);
136: for ($i=0; $i<$productCount; $i++){
137: $product = $this->_getRandomProduct();
138: if ($product) {
139: $product->setQuoteQty(1);
140: $this->_quote->addCatalogProduct($product);
141: }
142: }
143: $this->_quote->getPayment()->setMethod('checkmo');
144:
145: $this->_quote->getShippingAddress()->setShippingMethod('freeshipping_freeshipping');
146: $this->_quote->getShippingAddress()->setCollectShippingRates(true);
147: $this->_quote->collectTotals()
148: ->save();
149: $this->_quote->save();
150: return $this;
151: }
152:
153: protected function _getRandomDate()
154: {
155: $timestamp = mktime(rand(0,23), rand(0,59), 0, rand(1,11), rand(1,28), rand(2006, 2007));
156: return date('Y-m-d H:i:s', $timestamp);
157: }
158:
159: public function save()
160: {
161: $this->_order->setStoreId($this->_getStore()->getId());
162: $this->_order->createFromQuoteAddress($this->_quote->getShippingAddress());
163: $this->_order->validate();
164: $this->_order->setInitialStatus();
165: $this->_order->save();
166: $this->_order->setCreatedAt($this->_getRandomDate());
167: $this->_order->save();
168:
169: $this->_quote->setIsActive(false);
170: $this->_quote->save();
171: return $this;
172: }
173: }
174: