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 $installer = $this;
00028
00029
00030 $installer->startSetup();
00031
00032 $installer->run("
00033
00034 /*Table structure for table `sales_order` */
00035
00036 DROP TABLE IF EXISTS {$this->getTable('sales_order')};
00037 CREATE TABLE {$this->getTable('sales_order')} (
00038 `entity_id` int(10) unsigned NOT NULL auto_increment,
00039 `entity_type_id` smallint(5) unsigned NOT NULL default '0',
00040 `attribute_set_id` smallint(5) unsigned NOT NULL default '0',
00041 `increment_id` varchar(50) NOT NULL default '',
00042 `parent_id` int(10) unsigned NOT NULL default '0',
00043 `store_id` smallint(5) unsigned default NULL,
00044 `created_at` datetime NOT NULL default '0000-00-00 00:00:00',
00045 `updated_at` datetime NOT NULL default '0000-00-00 00:00:00',
00046 `is_active` tinyint(1) unsigned NOT NULL default '1',
00047 `customer_id` int(11),
00048 `tax_amount` decimal(12,4) NOT NULL default '0.0000',
00049 `shipping_amount` decimal(12,4) NOT NULL default '0.0000',
00050 `discount_amount` decimal(12,4) NOT NULL default '0.0000',
00051 `subtotal` decimal(12,4) NOT NULL default '0.0000',
00052 `grand_total` decimal(12,4) NOT NULL default '0.0000',
00053 `total_paid` decimal(12,4) NOT NULL default '0.0000',
00054 `total_refunded` decimal(12,4) NOT NULL default '0.0000',
00055 `total_qty_ordered` decimal(12,4) NOT NULL default '0.0000',
00056 `total_canceled` decimal(12,4) NOT NULL default '0.0000',
00057 `total_invoiced` decimal(12,4) NOT NULL default '0.0000',
00058 `total_online_refunded` decimal(12,4) NOT NULL default '0.0000',
00059 `total_offline_refunded` decimal(12,4) NOT NULL default '0.0000',
00060 `base_tax_amount` decimal(12,4) NOT NULL default '0.0000',
00061 `base_shipping_amount` decimal(12,4) NOT NULL default '0.0000',
00062 `base_discount_amount` decimal(12,4) NOT NULL default '0.0000',
00063 `base_subtotal` decimal(12,4) NOT NULL default '0.0000',
00064 `base_grand_total` decimal(12,4) NOT NULL default '0.0000',
00065 `base_total_paid` decimal(12,4) NOT NULL default '0.0000',
00066 `base_total_refunded` decimal(12,4) NOT NULL default '0.0000',
00067 `base_total_qty_ordered` decimal(12,4) NOT NULL default '0.0000',
00068 `base_total_canceled` decimal(12,4) NOT NULL default '0.0000',
00069 `base_total_invoiced` decimal(12,4) NOT NULL default '0.0000',
00070 `base_total_online_refunded` decimal(12,4) NOT NULL default '0.0000',
00071 `base_total_offline_refunded` decimal(12,4) NOT NULL default '0.0000',
00072 PRIMARY KEY (`entity_id`),
00073 KEY `FK_sales_order_type` (`entity_type_id`),
00074 KEY `FK_sales_order_store` (`store_id`),
00075 CONSTRAINT `FK_SALE_ORDER_STORE` FOREIGN KEY (`store_id`) REFERENCES `{$this->getTable('core_store')}` (`store_id`) ON DELETE SET NULL ON UPDATE CASCADE,
00076 CONSTRAINT `FK_SALE_ORDER_TYPE` FOREIGN KEY (`entity_type_id`) REFERENCES `{$this->getTable('eav_entity_type')}` (`entity_type_id`) ON DELETE CASCADE ON UPDATE CASCADE
00077 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
00078
00079
00080 DROP TABLE IF EXISTS {$this->getTable('sales_order')}_datetime;
00081 CREATE TABLE `{$this->getTable('sales_order')}_datetime` (
00082 `value_id` int(11) NOT NULL auto_increment,
00083 `entity_type_id` smallint(5) unsigned NOT NULL default '0',
00084 `attribute_id` smallint(5) unsigned NOT NULL default '0',
00085 `entity_id` int(10) unsigned NOT NULL default '0',
00086 `value` datetime NOT NULL default '0000-00-00 00:00:00',
00087 PRIMARY KEY (`value_id`),
00088 KEY `FK_sales_order_datetime_entity_type` (`entity_type_id`),
00089 KEY `FK_sales_order_datetime_attribute` (`attribute_id`),
00090 KEY `FK_sales_order_datetime` (`entity_id`),
00091 CONSTRAINT `FK_sales_order_datetime` FOREIGN KEY (`entity_id`) REFERENCES `{$this->getTable('sales_order')}` (`entity_id`) ON DELETE CASCADE ON UPDATE CASCADE,
00092 CONSTRAINT `FK_sales_order_datetime_attribute` FOREIGN KEY (`attribute_id`) REFERENCES `{$this->getTable('eav_attribute')}` (`attribute_id`) ON DELETE CASCADE ON UPDATE CASCADE,
00093 CONSTRAINT `FK_sales_order_datetime_entity_type` FOREIGN KEY (`entity_type_id`) REFERENCES `{$this->getTable('eav_entity_type')}` (`entity_type_id`) ON DELETE CASCADE ON UPDATE CASCADE
00094 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
00095
00096 DROP TABLE IF EXISTS {$this->getTable('sales_order')}_decimal;
00097 CREATE TABLE `{$this->getTable('sales_order')}_decimal` (
00098 `value_id` int(11) NOT NULL auto_increment,
00099 `entity_type_id` smallint(5) unsigned NOT NULL default '0',
00100 `attribute_id` smallint(5) unsigned NOT NULL default '0',
00101 `entity_id` int(10) unsigned NOT NULL default '0',
00102 `value` decimal(12,4) NOT NULL default '0.0000',
00103 PRIMARY KEY (`value_id`),
00104 KEY `FK_sales_order_decimal_entity_type` (`entity_type_id`),
00105 KEY `FK_sales_order_decimal_attribute` (`attribute_id`),
00106 KEY `FK_sales_order_decimal` (`entity_id`),
00107 CONSTRAINT `FK_sales_order_decimal` FOREIGN KEY (`entity_id`) REFERENCES `{$this->getTable('sales_order')}` (`entity_id`) ON DELETE CASCADE ON UPDATE CASCADE,
00108 CONSTRAINT `FK_sales_order_decimal_attribute` FOREIGN KEY (`attribute_id`) REFERENCES `{$this->getTable('eav_attribute')}` (`attribute_id`) ON DELETE CASCADE ON UPDATE CASCADE,
00109 CONSTRAINT `FK_sales_order_decimal_entity_type` FOREIGN KEY (`entity_type_id`) REFERENCES `{$this->getTable('eav_entity_type')}` (`entity_type_id`) ON DELETE CASCADE ON UPDATE CASCADE
00110 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
00111
00112 DROP TABLE IF EXISTS {$this->getTable('sales_order')}_int;
00113 CREATE TABLE `{$this->getTable('sales_order')}_int` (
00114 `value_id` int(11) NOT NULL auto_increment,
00115 `entity_type_id` smallint(5) unsigned NOT NULL default '0',
00116 `attribute_id` smallint(5) unsigned NOT NULL default '0',
00117 `entity_id` int(10) unsigned NOT NULL default '0',
00118 `value` int(11) NOT NULL default '0',
00119 PRIMARY KEY (`value_id`),
00120 KEY `FK_sales_order_int_entity_type` (`entity_type_id`),
00121 KEY `FK_sales_order_int_attribute` (`attribute_id`),
00122 KEY `FK_sales_order_int` (`entity_id`),
00123 CONSTRAINT `FK_sales_order_int` FOREIGN KEY (`entity_id`) REFERENCES `{$this->getTable('sales_order')}` (`entity_id`) ON DELETE CASCADE ON UPDATE CASCADE,
00124 CONSTRAINT `FK_sales_order_int_attribute` FOREIGN KEY (`attribute_id`) REFERENCES `{$this->getTable('eav_attribute')}` (`attribute_id`) ON DELETE CASCADE ON UPDATE CASCADE,
00125 CONSTRAINT `FK_sales_order_int_entity_type` FOREIGN KEY (`entity_type_id`) REFERENCES `{$this->getTable('eav_entity_type')}` (`entity_type_id`) ON DELETE CASCADE ON UPDATE CASCADE
00126 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
00127
00128 DROP TABLE IF EXISTS {$this->getTable('sales_order')}_text;
00129 CREATE TABLE `{$this->getTable('sales_order')}_text` (
00130 `value_id` int(11) NOT NULL auto_increment,
00131 `entity_type_id` smallint(5) unsigned NOT NULL default '0',
00132 `attribute_id` smallint(5) unsigned NOT NULL default '0',
00133 `entity_id` int(10) unsigned NOT NULL default '0',
00134 `value` text NOT NULL,
00135 PRIMARY KEY (`value_id`),
00136 KEY `FK_sales_order_text_entity_type` (`entity_type_id`),
00137 KEY `FK_sales_order_text_attribute` (`attribute_id`),
00138 KEY `FK_sales_order_text` (`entity_id`),
00139 CONSTRAINT `FK_sales_order_text` FOREIGN KEY (`entity_id`) REFERENCES `{$this->getTable('sales_order')}` (`entity_id`) ON DELETE CASCADE ON UPDATE CASCADE,
00140 CONSTRAINT `FK_sales_order_text_attribute` FOREIGN KEY (`attribute_id`) REFERENCES `{$this->getTable('eav_attribute')}` (`attribute_id`) ON DELETE CASCADE ON UPDATE CASCADE,
00141 CONSTRAINT `FK_sales_order_text_entity_type` FOREIGN KEY (`entity_type_id`) REFERENCES `{$this->getTable('eav_entity_type')}` (`entity_type_id`) ON DELETE CASCADE ON UPDATE CASCADE
00142 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
00143
00144 DROP TABLE IF EXISTS {$this->getTable('sales_order')}_varchar;
00145 CREATE TABLE `{$this->getTable('sales_order')}_varchar` (
00146 `value_id` int(11) NOT NULL auto_increment,
00147 `entity_type_id` smallint(5) unsigned NOT NULL default '0',
00148 `attribute_id` smallint(5) unsigned NOT NULL default '0',
00149 `entity_id` int(10) unsigned NOT NULL default '0',
00150 `value` varchar(255) NOT NULL default '',
00151 PRIMARY KEY (`value_id`),
00152 KEY `FK_sales_order_varchar_entity_type` (`entity_type_id`),
00153 KEY `FK_sales_order_varchar_attribute` (`attribute_id`),
00154 KEY `FK_sales_order_varchar` (`entity_id`),
00155 CONSTRAINT `FK_sales_order_varchar` FOREIGN KEY (`entity_id`) REFERENCES `{$this->getTable('sales_order')}` (`entity_id`) ON DELETE CASCADE ON UPDATE CASCADE,
00156 CONSTRAINT `FK_sales_order_varchar_attribute` FOREIGN KEY (`attribute_id`) REFERENCES `{$this->getTable('eav_attribute')}` (`attribute_id`) ON DELETE CASCADE ON UPDATE CASCADE,
00157 CONSTRAINT `FK_sales_order_varchar_entity_type` FOREIGN KEY (`entity_type_id`) REFERENCES `{$this->getTable('eav_entity_type')}` (`entity_type_id`) ON DELETE CASCADE ON UPDATE CASCADE
00158 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
00159
00160 ");
00161
00162 $installer->endSetup();
00163
00164 $orderEntityTypeId = $this->getEntityTypeId('order');
00165
00166 $attributes = array(
00167 'customer_id' => array(),
00168 'tax_amount' => array(),
00169 'shipping_amount' => array(),
00170 'discount_amount' => array(),
00171 'subtotal' => array(),
00172 'grand_total' => array(),
00173 'total_paid' => array(),
00174 'total_refunded' => array(),
00175 'total_qty_ordered' => array(),
00176 'total_canceled' => array(),
00177 'total_invoiced' => array(),
00178 'total_online_refunded' => array(),
00179 'total_offline_refunded' => array(),
00180 'base_tax_amount' => array(),
00181 'base_shipping_amount' => array(),
00182 'base_discount_amount' => array(),
00183 'base_subtotal' => array(),
00184 'base_grand_total' => array(),
00185 'base_total_paid' => array(),
00186 'base_total_refunded' => array(),
00187 'base_total_qty_ordered' => array(),
00188 'base_total_canceled' => array(),
00189 'base_total_invoiced' => array(),
00190 'base_total_online_refunded' => array(),
00191 'base_total_offline_refunded' => array()
00192 );
00193
00194 $select = new Zend_Db_Select($installer->getConnection());
00195 $select->from(array('e' => $this->getTable('sales_order_entity')));
00196
00197
00198
00199 $attributeIds = array();
00200 foreach ($attributes as $code => $params) {
00201 $attributes[$code] = $installer->getAttribute($orderEntityTypeId, $code);
00202 if ($attributes[$code]['backend_type'] != 'static') {
00203 $select->joinLeft(array("_table_{$code}" => "{$this->getTable('sales_order_entity')}_{$attributes[$code]['backend_type']}"),
00204 "_table_{$code}.attribute_id = {$attributes[$code]['attribute_id']} AND _table_{$code}.entity_id = e.entity_id",
00205 array($code => 'value'));
00206 $select->join(
00207 array("_eav_atr_{$code}" => $this->getTable('eav/attribute')),
00208 "_eav_atr_{$code}.attribute_id = {$attributes[$code]['attribute_id']}",
00209 array()
00210 );
00211 $attributeIds[] = $attributes[$code]['attribute_id'];
00212 }
00213 }
00214
00215 $select->where("e.entity_type_id = {$orderEntityTypeId}");
00216
00217 $orders = $installer->getConnection()->fetchAll($select);
00218
00219 foreach ($orders as $order) {
00220 $old_entity_id = $order['entity_id'];
00221 unset($order['entity_id']);
00222 unset($order['parent_id']);
00223 foreach ($order as $key => $field) {
00224 if ($field == '') {
00225 unset($order[$key]);
00226 }
00227 }
00228 $installer->getConnection()->insert($this->getTable('sales_order'), $order);
00229 $new_entity_id = $installer->getConnection()->lastInsertId();
00230
00231 $installer->run("UPDATE {$this->getTable('sales_order_entity')} SET parent_id={$new_entity_id} WHERE parent_id={$old_entity_id}");
00232
00233 $tables = array("varchar", "int", "datetime", "text", "decimal");
00234 foreach ($tables as $table) {
00235 $delete = array();
00236 $attrs = $installer->getConnection()->fetchAll("SELECT tt.* FROM {$this->getTable('sales_order_entity')}_{$table} tt JOIN eav_attribute on eav_attribute.attribute_id = tt.attribute_id WHERE entity_id={$old_entity_id}");
00237 foreach($attrs as $attr ) {
00238 if (!in_array($attr['attribute_id'], $attributeIds)) {
00239 unset($attr['value_id']);
00240 $attr['entity_id'] = $new_entity_id;
00241 $installer->getConnection()->insert("{$this->getTable('sales_order')}_{$table}", $attr);
00242 }
00243 }
00244 }
00245 }
00246
00247 $installer->run("DELETE FROM {$this->getTable('sales_order_entity')} WHERE parent_id=0");
00248
00249 $installer->installEntities();