1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Reports
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * Reports tax collection
30: *
31: * @category Mage
32: * @package Mage_Reports
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Reports_Model_Resource_Tax_Collection extends Mage_Sales_Model_Entity_Order_Collection
36: {
37: /**
38: * Set row identifier field name
39: *
40: */
41: public function _construct()
42: {
43: parent::_construct();
44: $this->setRowIdFieldName('tax_id');
45: }
46:
47: /**
48: * Set date range
49: *
50: * @param string $from
51: * @param string $to
52: * @return Mage_Reports_Model_Resource_Tax_Collection
53: */
54: public function setDateRange($from, $to)
55: {
56: $this->_reset();
57:
58: $this->addAttributeToFilter('created_at', array('from' => $from, 'to' => $to))
59: ->addExpressionAttributeToSelect('orders', 'COUNT(DISTINCT({{entity_id}}))', array('entity_id'))
60: ->getSelect()
61: ->join(
62: array('tax_table' => $this->getTable('sales/order_tax')),
63: 'e.entity_id = tax_table.order_id')
64: ->group('tax_table.code')
65: ->order(array('process', 'priority'));
66: /*
67: * Allow Analytic Functions Usage
68: */
69: $this->_useAnalyticFunction = true;
70:
71: return $this;
72: }
73:
74: /**
75: * Set store filter to collection
76: *
77: * @param array $storeIds
78: * @return Mage_Reports_Model_Resource_Tax_Collection
79: */
80: public function setStoreIds($storeIds)
81: {
82: if ($storeIds) {
83: $this->getSelect()
84: ->where('e.store_id IN(?)', (array)$storeIds)
85: ->columns(array('tax' => 'SUM(tax_table.base_real_amount)'));
86: } else {
87: $this->addExpressionAttributeToSelect(
88: 'tax',
89: 'SUM(tax_table.base_real_amount*{{base_to_global_rate}})',
90: array('base_to_global_rate')
91: );
92: }
93:
94: return $this;
95: }
96:
97: /**
98: * Get select count sql
99: *
100: * @return string
101: */
102: public function getSelectCountSql()
103: {
104: $countSelect = clone $this->getSelect();
105: $countSelect->reset(Zend_Db_Select::ORDER);
106: $countSelect->reset(Zend_Db_Select::LIMIT_COUNT);
107: $countSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
108: $countSelect->reset(Zend_Db_Select::COLUMNS);
109: $countSelect->reset(Zend_Db_Select::GROUP);
110: $countSelect->reset(Zend_Db_Select::HAVING);
111: $countSelect->columns("COUNT(DISTINCT e.entity_id)");
112: return $countSelect;
113: }
114: }
115: