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:
35: class Mage_Tax_Model_Resource_Report_Tax_Createdat extends Mage_Reports_Model_Resource_Report_Abstract
36: {
37: 38: 39:
40: protected function _construct()
41: {
42: $this->_init('tax/tax_order_aggregated_created', 'id');
43: }
44:
45: 46: 47: 48: 49: 50: 51:
52: public function aggregate($from = null, $to = null)
53: {
54: return $this->_aggregateByOrder('created_at', $from, $to);
55: }
56:
57: 58: 59: 60: 61: 62: 63: 64: 65:
66: protected function _aggregateByOrder($aggregationField, $from, $to)
67: {
68:
69: $from = $this->_dateToUtc($from);
70: $to = $this->_dateToUtc($to);
71:
72: $this->_checkDates($from, $to);
73: $writeAdapter = $this->_getWriteAdapter();
74: $writeAdapter->beginTransaction();
75:
76: try {
77: if ($from !== null || $to !== null) {
78: $subSelect = $this->_getTableDateRangeSelect(
79: $this->getTable('sales/order'),
80: 'created_at', 'updated_at', $from, $to
81: );
82: } else {
83: $subSelect = null;
84: }
85:
86: $this->_clearTableByDateRange($this->getMainTable(), $from, $to, $subSelect);
87:
88: $periodExpr = $writeAdapter->getDatePartSql(
89: $this->getStoreTZOffsetQuery(
90: array('e' => $this->getTable('sales/order')),
91: 'e.' . $aggregationField,
92: $from, $to
93: )
94: );
95:
96: $columns = array(
97: 'period' => $periodExpr,
98: 'store_id' => 'e.store_id',
99: 'code' => 'tax.code',
100: 'order_status' => 'e.status',
101: 'percent' => 'MAX(tax.' . $writeAdapter->quoteIdentifier('percent') .')',
102: 'orders_count' => 'COUNT(DISTINCT e.entity_id)',
103: 'tax_base_amount_sum' => 'SUM(tax.base_amount * e.base_to_global_rate)'
104: );
105:
106: $select = $writeAdapter->select();
107: $select->from(array('tax' => $this->getTable('tax/sales_order_tax')), $columns)
108: ->joinInner(array('e' => $this->getTable('sales/order')), 'e.entity_id = tax.order_id', array())
109: ->useStraightJoin();
110:
111: $select->where('e.state NOT IN (?)', array(
112: Mage_Sales_Model_Order::STATE_PENDING_PAYMENT,
113: Mage_Sales_Model_Order::STATE_NEW
114: ));
115:
116: if ($subSelect !== null) {
117: $select->having($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
118: }
119:
120: $select->group(array($periodExpr, 'e.store_id', 'code', 'tax.percent', 'e.status'));
121:
122: $insertQuery = $writeAdapter->insertFromSelect($select, $this->getMainTable(), array_keys($columns));
123: $writeAdapter->query($insertQuery);
124:
125: $select->reset();
126:
127: $columns = array(
128: 'period' => 'period',
129: 'store_id' => new Zend_Db_Expr(Mage_Core_Model_App::ADMIN_STORE_ID),
130: 'code' => 'code',
131: 'order_status' => 'order_status',
132: 'percent' => 'MAX(' . $writeAdapter->quoteIdentifier('percent') . ')',
133: 'orders_count' => 'SUM(orders_count)',
134: 'tax_base_amount_sum' => 'SUM(tax_base_amount_sum)'
135: );
136:
137: $select
138: ->from($this->getMainTable(), $columns)
139: ->where('store_id <> ?', 0);
140:
141: if ($subSelect !== null) {
142: $select->where($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
143: }
144:
145: $select->group(array('period', 'code', 'percent', 'order_status'));
146: $insertQuery = $writeAdapter->insertFromSelect($select, $this->getMainTable(), array_keys($columns));
147: $writeAdapter->query($insertQuery);
148: $writeAdapter->commit();
149: } catch (Exception $e) {
150: $writeAdapter->rollBack();
151: throw $e;
152: }
153:
154: return $this;
155: }
156: }
157: