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->getConnection()->addColumn($this->getTable('sales/order'), 'discount_refunded', 'decimal(12,4) default NULL AFTER `subtotal_canceled`');
00031 $installer->getConnection()->addColumn($this->getTable('sales/order'), 'discount_canceled', 'decimal(12,4) default NULL AFTER `discount_refunded`');
00032 $installer->getConnection()->addColumn($this->getTable('sales/order'), 'discount_invoiced', 'decimal(12,4) default NULL AFTER `discount_canceled`');
00033 $installer->getConnection()->addColumn($this->getTable('sales/order'), 'base_discount_refunded', 'decimal(12,4) default NULL AFTER `base_subtotal_canceled`');
00034 $installer->getConnection()->addColumn($this->getTable('sales/order'), 'base_discount_canceled', 'decimal(12,4) default NULL AFTER `base_discount_refunded`');
00035 $installer->getConnection()->addColumn($this->getTable('sales/order'), 'base_discount_invoiced', 'decimal(12,4) default NULL AFTER `base_discount_canceled`');
00036
00037 $installer->addAttribute('order', 'discount_refunded', array('type'=>'static'));
00038 $installer->addAttribute('order', 'discount_canceled', array('type'=>'static'));
00039 $installer->addAttribute('order', 'discount_invoiced', array('type'=>'static'));
00040 $installer->addAttribute('order', 'base_discount_refunded', array('type'=>'static'));
00041 $installer->addAttribute('order', 'base_discount_canceled', array('type'=>'static'));
00042 $installer->addAttribute('order', 'base_discount_invoiced', array('type'=>'static'));
00043
00044 $sql = "
00045 SELECT `e_int`.`value` AS `order_id`,
00046 SUM(e_decimal.value) AS `order_discount`,
00047 SUM(e_decimal_base.value) AS `order_base_discount`
00048 FROM `%s` AS `e_int`
00049 INNER JOIN `%s` AS `e_decimal`
00050 ON e_int.entity_id=e_decimal.entity_id AND e_decimal.attribute_id='%d'
00051 INNER JOIN `%s` AS `e_decimal_base`
00052 ON e_int.entity_id=e_decimal_base.entity_id AND e_decimal_base.attribute_id='%d'
00053 WHERE e_int.entity_type_id='%d' AND e_int.attribute_id='%d'
00054 GROUP BY `e_int`.`value`
00055 ";
00056
00057 $ordersEntity = $installer->getEntityType('order');
00058 $ordersTable = $installer->getTable($ordersEntity['entity_table']);
00059
00060
00061 $entityTypeId = $installer->getEntityTypeId('creditmemo');
00062 $orderAttributeId = $installer->getAttributeId($entityTypeId, 'order_id');
00063 $orderAttributeTable = $installer->getAttributeTable($entityTypeId, 'order_id');
00064 $discountAttributeId = $installer->getAttributeId($entityTypeId, 'discount_amount');
00065 $discountAttributeTable = $installer->getAttributeTable($entityTypeId, 'discount_amount');
00066 $baseDiscountAttributeId = $installer->getAttributeId($entityTypeId, 'base_discount_amount');
00067 $baseDiscountAttributeTable = $installer->getAttributeTable($entityTypeId, 'base_discount_amount');
00068
00069 $preparedSql = sprintf($sql,
00070 $orderAttributeTable,
00071 $discountAttributeTable,
00072 $discountAttributeId,
00073 $baseDiscountAttributeTable,
00074 $baseDiscountAttributeId,
00075 $entityTypeId,
00076 $orderAttributeId
00077 );
00078
00079 $stmt = $installer->getConnection()->query($preparedSql);
00080 while($row = $stmt->fetch()) {
00081 $data = array(
00082 'discount_refunded' => $row['order_discount'],
00083 'base_discount_refunded' => $row['order_base_discount']
00084 );
00085 $installer->getConnection()->update(
00086 $ordersTable,
00087 $data,
00088 $installer->getConnection()->quoteInto('entity_id=?', $row['order_id'])
00089 );
00090 }
00091
00092
00093 $entityTypeId = $installer->getEntityTypeId('invoice');
00094 $orderAttributeId = $installer->getAttributeId($entityTypeId, 'order_id');
00095 $orderAttributeTable = $installer->getAttributeTable($entityTypeId, 'order_id');
00096 $discountAttributeId = $installer->getAttributeId($entityTypeId, 'discount_amount');
00097 $discountAttributeTable = $installer->getAttributeTable($entityTypeId, 'discount_amount');
00098 $baseDiscountAttributeId = $installer->getAttributeId($entityTypeId, 'base_discount_amount');
00099 $baseDiscountAttributeTable = $installer->getAttributeTable($entityTypeId, 'base_discount_amount');
00100
00101 $preparedSql = sprintf($sql,
00102 $orderAttributeTable,
00103 $discountAttributeTable,
00104 $discountAttributeId,
00105 $baseDiscountAttributeTable,
00106 $baseDiscountAttributeId,
00107 $entityTypeId,
00108 $orderAttributeId
00109 );
00110
00111 $stmt = $installer->getConnection()->query($preparedSql);
00112 while($row = $stmt->fetch()) {
00113 $data = array(
00114 'discount_invoiced' => $row['order_discount'],
00115 'base_discount_invoiced' => $row['order_base_discount']
00116 );
00117 $installer->getConnection()->update(
00118 $ordersTable,
00119 $data,
00120 $installer->getConnection()->quoteInto('entity_id=?', $row['order_id'])
00121 );
00122 }
00123
00124
00125 $statusAttributeId = $installer->getAttributeId($ordersEntity['entity_type_id'], 'status');
00126 $statusAttributeTable = $installer->getAttributeTable($ordersEntity['entity_type_id'], 'status');
00127 $select = $installer->getConnection()->select()
00128 ->from(
00129 array('s' => $statusAttributeTable),
00130 array('order_id' => 's.entity_id')
00131 )
00132 ->where('s.attribute_id=?', $statusAttributeId)
00133 ->where('s.entity_type_id=?', $ordersEntity['entity_type_id'])
00134 ->where('s.value=?', Mage_Sales_Model_Order::STATE_CANCELED);
00135
00136 $stmt = $installer->getConnection()->query($select);
00137 while($row = $stmt->fetch()) {
00138 $entityId = $row['order_id'];
00139 $installer->run("
00140 UPDATE `{$ordersTable}` SET
00141 `discount_canceled`=`discount_amount`-`discount_invoiced`,
00142 `base_discount_canceled`=`base_discount_amount`-`base_discount_invoiced`
00143 WHERE `entity_id`='{$entityId}'
00144 ");
00145 }