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: class Mage_Adminhtml_Block_Report_Grid extends Mage_Adminhtml_Block_Widget_Grid
35: {
36: protected $_storeSwitcherVisibility = true;
37:
38: protected $_dateFilterVisibility = true;
39:
40: protected $_exportVisibility = true;
41:
42: protected $_subtotalVisibility = false;
43:
44: protected $_filters = array();
45:
46: protected $_defaultFilters = array(
47: 'report_from' => '',
48: 'report_to' => '',
49: 'report_period' => 'day'
50: );
51:
52: protected $_subReportSize = 5;
53:
54: protected $_grandTotals;
55:
56: protected $_errors = array();
57:
58: 59: 60:
61: protected $_currentCurrencyCode = null;
62:
63: public function __construct()
64: {
65: parent::__construct();
66: $this->setFilterVisibility(false);
67: $this->setPagerVisibility(false);
68: $this->setTemplate('report/grid.phtml');
69: $this->setUseAjax(false);
70: $this->setCountTotals(true);
71: }
72:
73: protected function _prepareLayout()
74: {
75: $this->setChild('store_switcher',
76: $this->getLayout()->createBlock('adminhtml/store_switcher')
77: ->setUseConfirm(false)
78: ->setSwitchUrl($this->getUrl('*/*/*', array('store'=>null)))
79: ->setTemplate('report/store/switcher.phtml')
80: );
81:
82: $this->setChild('refresh_button',
83: $this->getLayout()->createBlock('adminhtml/widget_button')
84: ->setData(array(
85: 'label' => Mage::helper('adminhtml')->__('Refresh'),
86: 'onclick' => $this->getRefreshButtonCallback(),
87: 'class' => 'task'
88: ))
89: );
90: parent::_prepareLayout();
91: return $this;
92: }
93:
94: protected function _prepareColumns()
95: {
96: foreach ($this->_columns as $_column) {
97: $_column->setSortable(false);
98: }
99:
100: parent::_prepareColumns();
101: }
102:
103: protected function _prepareCollection()
104: {
105: $filter = $this->getParam($this->getVarNameFilter(), null);
106:
107: if (is_null($filter)) {
108: $filter = $this->_defaultFilter;
109: }
110:
111: if (is_string($filter)) {
112: $data = array();
113: $filter = base64_decode($filter);
114: parse_str(urldecode($filter), $data);
115:
116: if (!isset($data['report_from'])) {
117:
118: $date = new Zend_Date(mktime(0,0,0,1,1,2001));
119: $data['report_from'] = $date->toString($this->getLocale()->getDateFormat('short'));
120: }
121:
122: if (!isset($data['report_to'])) {
123:
124: $date = new Zend_Date();
125: $data['report_to'] = $date->toString($this->getLocale()->getDateFormat('short'));
126: }
127:
128: $this->_setFilterValues($data);
129: } else if ($filter && is_array($filter)) {
130: $this->_setFilterValues($filter);
131: } else if(0 !== sizeof($this->_defaultFilter)) {
132: $this->_setFilterValues($this->_defaultFilter);
133: }
134:
135: $collection = Mage::getResourceModel('reports/report_collection');
136:
137: $collection->setPeriod($this->getFilter('report_period'));
138:
139: if ($this->getFilter('report_from') && $this->getFilter('report_to')) {
140: 141: 142:
143: try {
144: $from = $this->getLocale()->date($this->getFilter('report_from'), Zend_Date::DATE_SHORT, null, false);
145: $to = $this->getLocale()->date($this->getFilter('report_to'), Zend_Date::DATE_SHORT, null, false);
146:
147: $collection->setInterval($from, $to);
148: }
149: catch (Exception $e) {
150: $this->_errors[] = Mage::helper('reports')->__('Invalid date specified.');
151: }
152: }
153:
154: 155: 156:
157: $storeIds = array();
158: if ($this->getRequest()->getParam('store')) {
159: $storeIds = array($this->getParam('store'));
160: } elseif ($this->getRequest()->getParam('website')){
161: $storeIds = Mage::app()->getWebsite($this->getRequest()->getParam('website'))->getStoreIds();
162: } elseif ($this->getRequest()->getParam('group')){
163: $storeIds = Mage::app()->getGroup($this->getRequest()->getParam('group'))->getStoreIds();
164: }
165:
166:
167: $allowedStoreIds = array_keys(Mage::app()->getStores());
168:
169: $storeIds = array_intersect($allowedStoreIds, $storeIds);
170:
171: if (empty($storeIds)) {
172: $storeIds = $allowedStoreIds;
173: }
174:
175: $storeIds = array_values($storeIds);
176:
177:
178: $collection->setStoreIds($storeIds);
179:
180: if ($this->getSubReportSize() !== null) {
181: $collection->setPageSize($this->getSubReportSize());
182: }
183:
184: $this->setCollection($collection);
185:
186: Mage::dispatchEvent('adminhtml_widget_grid_filter_collection',
187: array('collection' => $this->getCollection(), 'filter_values' => $this->_filterValues)
188: );
189: }
190:
191: protected function _setFilterValues($data)
192: {
193: foreach ($data as $name => $value) {
194:
195: $this->setFilter($name, $data[$name]);
196:
197: }
198: return $this;
199: }
200:
201: 202: 203: 204: 205:
206: public function setStoreSwitcherVisibility($visible=true)
207: {
208: $this->_storeSwitcherVisibility = $visible;
209: }
210:
211: 212: 213: 214: 215:
216: public function getStoreSwitcherVisibility()
217: {
218: return $this->_storeSwitcherVisibility;
219: }
220:
221: 222: 223: 224: 225:
226: public function getStoreSwitcherHtml()
227: {
228: return $this->getChildHtml('store_switcher');
229: }
230:
231: 232: 233: 234: 235:
236: public function setDateFilterVisibility($visible=true)
237: {
238: $this->_dateFilterVisibility = $visible;
239: }
240:
241: 242: 243: 244: 245:
246: public function getDateFilterVisibility()
247: {
248: return $this->_dateFilterVisibility;
249: }
250:
251: 252: 253: 254: 255:
256: public function setExportVisibility($visible=true)
257: {
258: $this->_exportVisibility = $visible;
259: }
260:
261: 262: 263: 264: 265:
266: public function getExportVisibility()
267: {
268: return $this->_exportVisibility;
269: }
270:
271: 272: 273: 274: 275:
276: public function setSubtotalVisibility($visible=true)
277: {
278: $this->_subtotalVisibility = $visible;
279: }
280:
281: 282: 283: 284: 285:
286: public function getSubtotalVisibility()
287: {
288: return $this->_subtotalVisibility;
289: }
290:
291: public function getPeriods()
292: {
293: return $this->getCollection()->getPeriods();
294: }
295:
296: public function getDateFormat()
297: {
298: return $this->getLocale()->getDateStrFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
299: }
300:
301: 302: 303:
304: public function getRefreshButtonHtml()
305: {
306: return $this->getChildHtml('refresh_button');
307: }
308:
309: public function setFilter($name, $value)
310: {
311: if ($name) {
312: $this->_filters[$name] = $value;
313: }
314: }
315:
316: public function getFilter($name)
317: {
318: if (isset($this->_filters[$name])) {
319: return $this->_filters[$name];
320: } else {
321: return ($this->getRequest()->getParam($name))
322: ?htmlspecialchars($this->getRequest()->getParam($name)):'';
323: }
324: }
325:
326: public function setSubReportSize($size)
327: {
328: $this->_subReportSize = $size;
329: }
330:
331: public function getSubReportSize()
332: {
333: return $this->_subReportSize;
334: }
335:
336: 337: 338: 339: 340:
341: public function getLocale()
342: {
343: if (!$this->_locale) {
344: $this->_locale = Mage::app()->getLocale();
345: }
346: return $this->_locale;
347: }
348:
349: 350: 351: 352: 353: 354: 355:
356: public function addExportType($url, $label)
357: {
358: $this->_exportTypes[] = new Varien_Object(
359: array(
360: 'url' => $this->getUrl($url,
361: array(
362: '_current'=>true,
363: 'filter' => $this->getParam($this->getVarNameFilter(), null)
364: )
365: ),
366: 'label' => $label
367: )
368: );
369: return $this;
370: }
371:
372: public function getReport($from, $to)
373: {
374: if ($from == '') {
375: $from = $this->getFilter('report_from');
376: }
377: if ($to == '') {
378: $to = $this->getFilter('report_to');
379: }
380: $totalObj = Mage::getModel('reports/totals');
381: $this->setTotals($totalObj->countTotals($this, $from, $to));
382: $this->addGrandTotals($this->getTotals());
383: return $this->getCollection()->getReport($from, $to);
384: }
385:
386: public function addGrandTotals($total)
387: {
388: $totalData = $total->getData();
389: foreach ($totalData as $key=>$value) {
390: $_column = $this->getColumn($key);
391: if ($_column->getTotal() != '') {
392: $this->getGrandTotals()->setData($key, $this->getGrandTotals()->getData($key)+$value);
393: }
394: }
395: 396: 397:
398: foreach ($this->getColumns() as $key=>$_column) {
399: if (strpos($_column->getTotal(), '/') !== FALSE) {
400: list($t1, $t2) = explode('/', $_column->getTotal());
401: if ($this->getGrandTotals()->getData($t2) != 0) {
402: $this->getGrandTotals()->setData(
403: $key,
404: (float)$this->getGrandTotals()->getData($t1)/$this->getGrandTotals()->getData($t2)
405: );
406: }
407: }
408: }
409: }
410:
411: public function getGrandTotals()
412: {
413: if (!$this->_grandTotals) {
414: $this->_grandTotals = new Varien_Object();
415: }
416: return $this->_grandTotals;
417: }
418:
419: public function getPeriodText()
420: {
421: return $this->__('Period');
422: }
423:
424: 425: 426: 427: 428:
429: public function getCsv()
430: {
431: $csv = '';
432: $this->_prepareGrid();
433:
434: $data = array('"'.$this->__('Period').'"');
435: foreach ($this->_columns as $column) {
436: if (!$column->getIsSystem()) {
437: $data[] = '"'.$column->getHeader().'"';
438: }
439: }
440: $csv.= implode(',', $data)."\n";
441:
442: foreach ($this->getCollection()->getIntervals() as $_index=>$_item) {
443: $report = $this->getReport($_item['start'], $_item['end']);
444: foreach ($report as $_subIndex=>$_subItem) {
445: $data = array('"'.$_index.'"');
446: foreach ($this->_columns as $column) {
447: if (!$column->getIsSystem()) {
448: $data[] = '"' . str_replace(
449: array('"', '\\'),
450: array('""', '\\\\'),
451: $column->getRowField($_subItem)
452: ) . '"';
453: }
454: }
455: $csv.= implode(',', $data)."\n";
456: }
457: if ($this->getCountTotals() && $this->getSubtotalVisibility())
458: {
459: $data = array('"'.$_index.'"');
460: $j = 0;
461: foreach ($this->_columns as $column) {
462: $j++;
463: if (!$column->getIsSystem()) {
464: $data[] = ($j == 1) ?
465: '"' . $this->__('Subtotal') . '"' :
466: '"'.str_replace('"', '""', $column->getRowField($this->getTotals())).'"';
467: }
468: }
469: $csv.= implode(',', $data)."\n";
470: }
471: }
472:
473: if ($this->getCountTotals())
474: {
475: $data = array('"'.$this->__('Total').'"');
476: foreach ($this->_columns as $column) {
477: if (!$column->getIsSystem()) {
478: $data[] = '"'.str_replace('"', '""', $column->getRowField($this->getGrandTotals())).'"';
479: }
480: }
481: $csv.= implode(',', $data)."\n";
482: }
483:
484: return $csv;
485: }
486:
487: 488: 489: 490: 491:
492: public function getExcel($filename = '')
493: {
494: $this->_prepareGrid();
495:
496: $data = array();
497: $row = array($this->__('Period'));
498: foreach ($this->_columns as $column) {
499: if (!$column->getIsSystem()) {
500: $row[] = $column->getHeader();
501: }
502: }
503: $data[] = $row;
504:
505: foreach ($this->getCollection()->getIntervals() as $_index=>$_item) {
506: $report = $this->getReport($_item['start'], $_item['end']);
507: foreach ($report as $_subIndex=>$_subItem) {
508: $row = array($_index);
509: foreach ($this->_columns as $column) {
510: if (!$column->getIsSystem()) {
511: $row[] = $column->getRowField($_subItem);
512: }
513: }
514: $data[] = $row;
515: }
516: if ($this->getCountTotals() && $this->getSubtotalVisibility())
517: {
518: $row = array($_index);
519: $j = 0;
520: foreach ($this->_columns as $column) {
521: $j++;
522: if (!$column->getIsSystem()) {
523: $row[] = ($j==1)?$this->__('Subtotal'):$column->getRowField($this->getTotals());
524: }
525: }
526: $data[] = $row;
527: }
528: }
529:
530: if ($this->getCountTotals())
531: {
532: $row = array($this->__('Total'));
533: foreach ($this->_columns as $column) {
534: if (!$column->getIsSystem()) {
535: $row[] = $column->getRowField($this->getGrandTotals());
536: }
537: }
538: $data[] = $row;
539: }
540:
541: $xmlObj = new Varien_Convert_Parser_Xml_Excel();
542: $xmlObj->setVar('single_sheet', $filename);
543: $xmlObj->setData($data);
544: $xmlObj->unparse();
545:
546: return $xmlObj->getData();
547: }
548:
549: public function getSubtotalText()
550: {
551: return $this->__('Subtotal');
552: }
553:
554: public function getTotalText()
555: {
556: return $this->__('Total');
557: }
558:
559: public function getEmptyText()
560: {
561: return $this->__('No records found for this period.');
562: }
563:
564: public function getCountTotals()
565: {
566: $totals = $this->getGrandTotals()->getData();
567: if (parent::getCountTotals() && count($totals)) {
568: return true;
569: } else {
570: return false;
571: }
572: }
573:
574: 575: 576: 577: 578:
579: public function getRefreshButtonCallback()
580: {
581: return "{$this->getJsObjectName()}.doFilter();";
582: }
583:
584: 585: 586: 587: 588:
589: public function getErrors()
590: {
591: return $this->_errors;
592: }
593:
594: 595: 596: 597: 598:
599: public function getCurrentCurrencyCode()
600: {
601: if (is_null($this->_currentCurrencyCode)) {
602: if ($this->getRequest()->getParam('store')) {
603: $store = $this->getRequest()->getParam('store');
604: $this->_currentCurrencyCode = Mage::app()->getStore($store)->getBaseCurrencyCode();
605: } else if ($this->getRequest()->getParam('website')){
606: $website = $this->getRequest()->getParam('website');
607: $this->_currentCurrencyCode = Mage::app()->getWebsite($website)->getBaseCurrencyCode();
608: } else if ($this->getRequest()->getParam('group')){
609: $group = $this->getRequest()->getParam('group');
610: $this->_currentCurrencyCode = Mage::app()->getGroup($group)->getWebsite()->getBaseCurrencyCode();
611: } else {
612: $this->_currentCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
613: }
614: }
615: return $this->_currentCurrencyCode;
616: }
617:
618: 619: 620: 621: 622: 623:
624: public function getRate($toCurrency)
625: {
626: return Mage::app()->getStore()->getBaseCurrency()->getRate($toCurrency);
627: }
628: }
629: