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_Sales_Model_Resource_Report_Order_Createdat extends Mage_Sales_Model_Resource_Report_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('sales/order_aggregated_created', 'id');
44: }
45:
46: 47: 48: 49: 50: 51: 52:
53: public function aggregate($from = null, $to = null)
54: {
55: return $this->_aggregateByField('created_at', $from, $to);
56: }
57:
58: 59: 60: 61: 62: 63: 64: 65: 66:
67: protected function _aggregateByField($aggregationField, $from, $to)
68: {
69:
70: $from = $this->_dateToUtc($from);
71: $to = $this->_dateToUtc($to);
72:
73: $this->_checkDates($from, $to);
74: $adapter = $this->_getWriteAdapter();
75:
76: $adapter->beginTransaction();
77: try {
78:
79: if ($from !== null || $to !== null) {
80: $subSelect = $this->_getTableDateRangeSelect(
81: $this->getTable('sales/order'),
82: 'created_at', 'updated_at', $from, $to
83: );
84: } else {
85: $subSelect = null;
86: }
87: $this->_clearTableByDateRange($this->getMainTable(), $from, $to, $subSelect);
88:
89: $periodExpr = $adapter->getDatePartSql($this->getStoreTZOffsetQuery(
90: array('o' => $this->getTable('sales/order')),
91: 'o.' . $aggregationField,
92: $from, $to
93: ));
94:
95: $columns = array(
96:
97: 'period' => $periodExpr,
98: 'store_id' => 'o.store_id',
99: 'order_status' => 'o.status',
100: 'orders_count' => new Zend_Db_Expr('COUNT(o.entity_id)'),
101: 'total_qty_ordered' => new Zend_Db_Expr('SUM(oi.total_qty_ordered)'),
102: 'total_qty_invoiced' => new Zend_Db_Expr('SUM(oi.total_qty_invoiced)'),
103: 'total_income_amount' => new Zend_Db_Expr(
104: sprintf('SUM((%s - %s) * %s)',
105: $adapter->getIfNullSql('o.base_grand_total', 0),
106: $adapter->getIfNullSql('o.base_total_canceled',0),
107: $adapter->getIfNullSql('o.base_to_global_rate',0)
108: )
109: ),
110: 'total_revenue_amount' => new Zend_Db_Expr(
111: sprintf('SUM((%s - %s - %s - (%s - %s - %s)) * %s)',
112: $adapter->getIfNullSql('o.base_total_invoiced', 0),
113: $adapter->getIfNullSql('o.base_tax_invoiced', 0),
114: $adapter->getIfNullSql('o.base_shipping_invoiced', 0),
115: $adapter->getIfNullSql('o.base_total_refunded', 0),
116: $adapter->getIfNullSql('o.base_tax_refunded', 0),
117: $adapter->getIfNullSql('o.base_shipping_refunded', 0),
118: $adapter->getIfNullSql('o.base_to_global_rate', 0)
119: )
120: ),
121: 'total_profit_amount' => new Zend_Db_Expr(
122: sprintf('SUM((%s - %s - %s - %s - %s) * %s)',
123: $adapter->getIfNullSql('o.base_total_paid', 0),
124: $adapter->getIfNullSql('o.base_total_refunded', 0),
125: $adapter->getIfNullSql('o.base_tax_invoiced', 0),
126: $adapter->getIfNullSql('o.base_shipping_invoiced', 0),
127: $adapter->getIfNullSql('o.base_total_invoiced_cost', 0),
128: $adapter->getIfNullSql('o.base_to_global_rate', 0)
129: )
130: ),
131: 'total_invoiced_amount' => new Zend_Db_Expr(
132: sprintf('SUM(%s * %s)',
133: $adapter->getIfNullSql('o.base_total_invoiced', 0),
134: $adapter->getIfNullSql('o.base_to_global_rate', 0)
135: )
136: ),
137: 'total_canceled_amount' => new Zend_Db_Expr(
138: sprintf('SUM(%s * %s)',
139: $adapter->getIfNullSql('o.base_total_canceled', 0),
140: $adapter->getIfNullSql('o.base_to_global_rate', 0)
141: )
142: ),
143: 'total_paid_amount' => new Zend_Db_Expr(
144: sprintf('SUM(%s * %s)',
145: $adapter->getIfNullSql('o.base_total_paid', 0),
146: $adapter->getIfNullSql('o.base_to_global_rate', 0)
147: )
148: ),
149: 'total_refunded_amount' => new Zend_Db_Expr(
150: sprintf('SUM(%s * %s)',
151: $adapter->getIfNullSql('o.base_total_refunded', 0),
152: $adapter->getIfNullSql('o.base_to_global_rate', 0)
153: )
154: ),
155: 'total_tax_amount' => new Zend_Db_Expr(
156: sprintf('SUM((%s - %s) * %s)',
157: $adapter->getIfNullSql('o.base_tax_amount', 0),
158: $adapter->getIfNullSql('o.base_tax_canceled', 0),
159: $adapter->getIfNullSql('o.base_to_global_rate', 0)
160: )
161: ),
162: 'total_tax_amount_actual' => new Zend_Db_Expr(
163: sprintf('SUM((%s -%s) * %s)',
164: $adapter->getIfNullSql('o.base_tax_invoiced', 0),
165: $adapter->getIfNullSql('o.base_tax_refunded', 0),
166: $adapter->getIfNullSql('o.base_to_global_rate', 0)
167: )
168: ),
169: 'total_shipping_amount' => new Zend_Db_Expr(
170: sprintf('SUM((%s - %s) * %s)',
171: $adapter->getIfNullSql('o.base_shipping_amount', 0),
172: $adapter->getIfNullSql('o.base_shipping_canceled', 0),
173: $adapter->getIfNullSql('o.base_to_global_rate', 0)
174: )
175: ),
176: 'total_shipping_amount_actual' => new Zend_Db_Expr(
177: sprintf('SUM((%s - %s) * %s)',
178: $adapter->getIfNullSql('o.base_shipping_invoiced', 0),
179: $adapter->getIfNullSql('o.base_shipping_refunded', 0),
180: $adapter->getIfNullSql('o.base_to_global_rate', 0)
181: )
182: ),
183: 'total_discount_amount' => new Zend_Db_Expr(
184: sprintf('SUM((ABS(%s) - %s) * %s)',
185: $adapter->getIfNullSql('o.base_discount_amount', 0),
186: $adapter->getIfNullSql('o.base_discount_canceled', 0),
187: $adapter->getIfNullSql('o.base_to_global_rate', 0)
188: )
189: ),
190: 'total_discount_amount_actual' => new Zend_Db_Expr(
191: sprintf('SUM((%s - %s) * %s)',
192: $adapter->getIfNullSql('o.base_discount_invoiced', 0),
193: $adapter->getIfNullSql('o.base_discount_refunded', 0),
194: $adapter->getIfNullSql('o.base_to_global_rate', 0)
195: )
196: )
197: );
198:
199: $select = $adapter->select();
200: $selectOrderItem = $adapter->select();
201:
202: $qtyCanceledExpr = $adapter->getIfNullSql('qty_canceled', 0);
203: $cols = array(
204: 'order_id' => 'order_id',
205: 'total_qty_ordered' => new Zend_Db_Expr("SUM(qty_ordered - {$qtyCanceledExpr})"),
206: 'total_qty_invoiced' => new Zend_Db_Expr('SUM(qty_invoiced)'),
207: );
208: $selectOrderItem->from($this->getTable('sales/order_item'), $cols)
209: ->where('parent_item_id IS NULL')
210: ->group('order_id');
211:
212: $select->from(array('o' => $this->getTable('sales/order')), $columns)
213: ->join(array('oi' => $selectOrderItem), 'oi.order_id = o.entity_id', array())
214: ->where('o.state NOT IN (?)', array(
215: Mage_Sales_Model_Order::STATE_PENDING_PAYMENT,
216: Mage_Sales_Model_Order::STATE_NEW
217: ));
218:
219: if ($subSelect !== null) {
220: $select->having($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
221: }
222:
223: $select->group(array(
224: $periodExpr,
225: 'o.store_id',
226: 'o.status',
227: ));
228:
229: $adapter->query($select->insertFromSelect($this->getMainTable(), array_keys($columns)));
230:
231:
232: foreach ($columns as $k => $v) {
233: $columns[$k] = new Zend_Db_Expr('SUM(' . $k . ')');
234: }
235: $columns['period'] = 'period';
236: $columns['store_id'] = new Zend_Db_Expr(Mage_Core_Model_App::ADMIN_STORE_ID);
237: $columns['order_status'] = 'order_status';
238:
239: $select->reset();
240: $select->from($this->getMainTable(), $columns)
241: ->where('store_id <> 0');
242:
243: if ($subSelect !== null) {
244: $select->where($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
245: }
246:
247: $select->group(array(
248: 'period',
249: 'order_status'
250: ));
251: $adapter->query($select->insertFromSelect($this->getMainTable(), array_keys($columns)));
252: $adapter->commit();
253: } catch (Exception $e) {
254: $adapter->rollBack();
255: throw $e;
256: }
257:
258: return $this;
259: }
260: }
261: