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_Random
00033 {
00034
00035
00036
00037
00038
00039 protected $_quote;
00040
00041
00042
00043
00044
00045
00046 protected $_order;
00047 protected $_store;
00048 protected $_customer;
00049 protected $_productCollection;
00050
00051 protected static $_storeCollection;
00052 protected static $_customerCollection;
00053
00054 public function __construct()
00055 {
00056 $this->_quote = Mage::getModel('sales/quote')->save();
00057 $this->_order = Mage::getModel('sales/order');
00058 }
00059
00060 protected function _getStores()
00061 {
00062 if (!self::$_storeCollection) {
00063 self::$_storeCollection = Mage::getResourceModel('core/store_collection')
00064 ->load();
00065 }
00066 return self::$_storeCollection->getItems();
00067 }
00068
00069 protected function _getCustomers()
00070 {
00071 if (!self::$_customerCollection) {
00072 self::$_customerCollection = Mage::getResourceModel('customer/customer_collection')
00073 ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'inner')
00074 ->joinAttribute('shipping_country_id', 'customer_address/country_id', 'default_shipping', null, 'inner')
00075 ->load();
00076 }
00077 return self::$_customerCollection->getItems();
00078 }
00079
00080 protected function _getProducts()
00081 {
00082 if (!$this->_productCollection) {
00083 $this->_productCollection= Mage::getResourceModel('catalog/product_collection');
00084
00085 Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($this->_productCollection);
00086 Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($this->_productCollection);
00087 $this->_productCollection->addAttributeToSelect('name')
00088 ->addAttributeToSelect('sku')
00089 ->addAttributeToFilter('type_id', Mage_Catalog_Model_Product_Type::TYPE_SIMPLE)
00090 ->load();
00091 }
00092 return $this->_productCollection->getItems();
00093 }
00094
00095
00096
00097
00098
00099
00100 protected function _getCustomer()
00101 {
00102 if (!$this->_customer) {
00103 $items = $this->_getCustomers();
00104 $randKey = array_rand($items);
00105 $this->_customer = $items[$randKey];
00106 }
00107 return $this->_customer;
00108 }
00109
00110 protected function _getRandomProduct()
00111 {
00112 $items = $this->_getProducts();
00113 $randKey = array_rand($items);
00114 return isset($items[$randKey]) ? $items[$randKey] : false;
00115 }
00116
00117 protected function _getStore()
00118 {
00119 if (!$this->_store) {
00120 $items = $this->_getStores();
00121 $randKey = array_rand($items);
00122 $this->_store = $items[$randKey];
00123 }
00124 return $this->_store;
00125 }
00126
00127 public function render()
00128 {
00129 $customer = $this->_getCustomer();
00130 $this->_quote->setStore($this->_getStore())
00131 ->setCustomer($customer);
00132 $this->_quote->getBillingAddress()->importCustomerAddress($customer->getDefaultBillingAddress());
00133 $this->_quote->getShippingAddress()->importCustomerAddress($customer->getDefaultShippingAddress());
00134
00135 $productCount = rand(3, 10);
00136 for ($i=0; $i<$productCount; $i++){
00137 $product = $this->_getRandomProduct();
00138 if ($product) {
00139 $product->setQuoteQty(1);
00140 $this->_quote->addCatalogProduct($product);
00141 }
00142 }
00143 $this->_quote->getPayment()->setMethod('checkmo');
00144
00145 $this->_quote->getShippingAddress()->setShippingMethod('freeshipping_freeshipping');
00146 $this->_quote->getShippingAddress()->setCollectShippingRates(true);
00147 $this->_quote->collectTotals()
00148 ->save();
00149 $this->_quote->save();
00150 return $this;
00151 }
00152
00153 protected function _getRandomDate()
00154 {
00155 $timestamp = mktime(rand(0,23), rand(0,59), 0, rand(1,11), rand(1,28), rand(2006, 2007));
00156 return date('Y-m-d H:i:s', $timestamp);
00157 }
00158
00159 public function save()
00160 {
00161 $this->_order->setStoreId($this->_getStore()->getId());
00162 $this->_order->createFromQuoteAddress($this->_quote->getShippingAddress());
00163 $this->_order->validate();
00164 $this->_order->setInitialStatus();
00165 $this->_order->save();
00166 $this->_order->setCreatedAt($this->_getRandomDate());
00167 $this->_order->save();
00168
00169 $this->_quote->setIsActive(false);
00170 $this->_quote->save();
00171 return $this;
00172 }
00173 }