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_Reports_Model_Resource_Invoiced_Collection extends Mage_Sales_Model_Entity_Order_Collection
36: {
37: 38: 39: 40: 41: 42: 43:
44: public function setDateRange($from, $to)
45: {
46: $orderInvoicedExpr = $this->getConnection()->getCheckSql('{{base_total_invoiced}} > 0', 1, 0);
47: $this->_reset()
48: ->addAttributeToSelect('*')
49: ->addAttributeToFilter('created_at', array('from' => $from, 'to' => $to))
50: ->addExpressionAttributeToSelect('orders',
51: 'COUNT({{base_total_invoiced}})',
52: array('base_total_invoiced'))
53: ->addExpressionAttributeToSelect('orders_invoiced',
54: "SUM({$orderInvoicedExpr})",
55: array('base_total_invoiced'))
56: ->addAttributeToFilter('state', array('neq' => Mage_Sales_Model_Order::STATE_CANCELED))
57: ->getSelect()
58: ->group('entity_id')
59: ->having('orders > ?', 0);
60: 61: 62:
63: $this->_useAnalyticFunction = true;
64:
65: return $this;
66: }
67:
68: 69: 70: 71: 72: 73:
74: public function setStoreIds($storeIds)
75: {
76: if ($storeIds) {
77: $this->addAttributeToFilter('store_id', array('in' => (array)$storeIds))
78: ->addExpressionAttributeToSelect(
79: 'invoiced',
80: 'SUM({{base_total_invoiced}})',
81: array('base_total_invoiced'))
82: ->addExpressionAttributeToSelect(
83: 'invoiced_captured',
84: 'SUM({{base_total_paid}})',
85: array('base_total_paid'))
86: ->addExpressionAttributeToSelect(
87: 'invoiced_not_captured',
88: 'SUM({{base_total_invoiced}}-{{base_total_paid}})',
89: array('base_total_invoiced', 'base_total_paid'));
90: } else {
91: $this->addExpressionAttributeToSelect(
92: 'invoiced',
93: 'SUM({{base_total_invoiced}}*{{base_to_global_rate}})',
94: array('base_total_invoiced', 'base_to_global_rate'))
95: ->addExpressionAttributeToSelect(
96: 'invoiced_captured',
97: 'SUM({{base_total_paid}}*{{base_to_global_rate}})',
98: array('base_total_paid', 'base_to_global_rate'))
99: ->addExpressionAttributeToSelect(
100: 'invoiced_not_captured',
101: 'SUM(({{base_total_invoiced}}-{{base_total_paid}})*{{base_to_global_rate}})',
102: array('base_total_invoiced', 'base_to_global_rate', 'base_total_paid'));
103: }
104:
105: return $this;
106: }
107:
108: 109: 110: 111: 112:
113: public function getSelectCountSql()
114: {
115: $countSelect = clone $this->getSelect();
116: $countSelect->reset(Zend_Db_Select::ORDER);
117: $countSelect->reset(Zend_Db_Select::LIMIT_COUNT);
118: $countSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
119: $countSelect->reset(Zend_Db_Select::COLUMNS);
120: $countSelect->reset(Zend_Db_Select::GROUP);
121: $countSelect->reset(Zend_Db_Select::HAVING);
122: $countSelect->columns("COUNT(*)");
123:
124: return $countSelect;
125: }
126: }
127: