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: * Totals Class
29: *
30: * @category Mage
31: * @package Mage_Reports
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Reports_Model_Totals
35: {
36: /**
37: * Retrieve count totals
38: *
39: * @param Mage_Adminhtml_Block_Report_Grid $grid
40: * @param string $from
41: * @param string $to
42: * @return Varien_Object
43: */
44: public function countTotals($grid, $from, $to)
45: {
46: $columns = array();
47: foreach ($grid->getColumns() as $col) {
48: $columns[$col->getIndex()] = array("total" => $col->getTotal(), "value" => 0);
49: }
50:
51: $count = 0;
52: $report = $grid->getCollection()->getReportFull($from, $to);
53: foreach ($report as $item) {
54: if ($grid->getSubReportSize() && $count >= $grid->getSubReportSize()) {
55: continue;
56: }
57: $data = $item->getData();
58:
59: foreach ($columns as $field=>$a) {
60: if ($field !== '') {
61: $columns[$field]['value'] = $columns[$field]['value'] + (isset($data[$field]) ? $data[$field] : 0);
62: }
63: }
64: $count++;
65: }
66: $data = array();
67: foreach ($columns as $field => $a) {
68: if ($a['total'] == 'avg') {
69: if ($field !== '') {
70: if ($count != 0) {
71: $data[$field] = $a['value']/$count;
72: } else {
73: $data[$field] = 0;
74: }
75: }
76: } else if ($a['total'] == 'sum') {
77: if ($field !== '') {
78: $data[$field] = $a['value'];
79: }
80: } else if (strpos($a['total'], '/') !== FALSE) {
81: if ($field !== '') {
82: $data[$field] = 0;
83: }
84: }
85: }
86:
87: $totals = new Varien_Object();
88: $totals->setData($data);
89:
90: return $totals;
91: }
92: }
93: