00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Adminhtml_Block_Report_Grid extends Mage_Adminhtml_Block_Widget_Grid
00035 {
00036 protected $_storeSwitcherVisibility = true;
00037
00038 protected $_dateFilterVisibility = true;
00039
00040 protected $_exportVisibility = true;
00041
00042 protected $_subtotalVisibility = false;
00043
00044 protected $_filters = array();
00045
00046 protected $_defaultFilters = array(
00047 'report_from' => '',
00048 'report_to' => '',
00049 'report_period' => 'day'
00050 );
00051
00052 protected $_subReportSize = 5;
00053
00054 protected $_grandTotals;
00055
00056 protected $_errors = array();
00057
00058
00059
00060
00061 protected $_currentCurrencyCode = null;
00062
00063 public function __construct()
00064 {
00065 parent::__construct();
00066 $this->setFilterVisibility(false);
00067 $this->setPagerVisibility(false);
00068 $this->setTemplate('report/grid.phtml');
00069 $this->setUseAjax(false);
00070 $this->setCountTotals(true);
00071 }
00072
00073 protected function _prepareLayout()
00074 {
00075 $this->setChild('store_switcher',
00076 $this->getLayout()->createBlock('adminhtml/store_switcher')
00077 ->setUseConfirm(false)
00078 ->setSwitchUrl($this->getUrl('*/*/*', array('store'=>null)))
00079 ->setTemplate('report/store/switcher.phtml')
00080 );
00081
00082 $this->setChild('refresh_button',
00083 $this->getLayout()->createBlock('adminhtml/widget_button')
00084 ->setData(array(
00085 'label' => Mage::helper('adminhtml')->__('Refresh'),
00086 'onclick' => $this->getRefreshButtonCallback(),
00087 'class' => 'task'
00088 ))
00089 );
00090 parent::_prepareLayout();
00091 return $this;
00092 }
00093
00094 protected function _prepareColumns()
00095 {
00096 foreach ($this->_columns as $_column) {
00097 $_column->setSortable(false);
00098 }
00099
00100 parent::_prepareColumns();
00101 }
00102
00103 protected function _prepareCollection()
00104 {
00105 $filter = $this->getParam($this->getVarNameFilter(), null);
00106
00107 if (is_null($filter)) {
00108 $filter = $this->_defaultFilter;
00109 }
00110
00111 if (is_string($filter)) {
00112 $data = array();
00113 $filter = base64_decode($filter);
00114 parse_str(urldecode($filter), $data);
00115
00116 if (!isset($data['report_from'])) {
00117
00118 $date = new Zend_Date(mktime(0,0,0,1,1,2001));
00119 $data['report_from'] = $date->toString($this->getLocale()->getDateFormat('short'));
00120 }
00121
00122 if (!isset($data['report_to'])) {
00123
00124 $date = new Zend_Date();
00125 $data['report_to'] = $date->toString($this->getLocale()->getDateFormat('short'));
00126 }
00127
00128 $this->_setFilterValues($data);
00129 } else if ($filter && is_array($filter)) {
00130 $this->_setFilterValues($filter);
00131 } else if(0 !== sizeof($this->_defaultFilter)) {
00132 $this->_setFilterValues($this->_defaultFilter);
00133 }
00134
00135 $collection = Mage::getResourceModel('reports/report_collection');
00136
00137 $collection->setPeriod($this->getFilter('report_period'));
00138
00139 if ($this->getFilter('report_from') && $this->getFilter('report_to')) {
00140
00141
00142
00143 try {
00144 $from = $this->getLocale()->date($this->getFilter('report_from'), Zend_Date::DATE_SHORT, null, false);
00145 $to = $this->getLocale()->date($this->getFilter('report_to'), Zend_Date::DATE_SHORT, null, false);
00146
00147 $collection->setInterval($from, $to);
00148 }
00149 catch (Exception $e) {
00150 $this->_errors[] = Mage::helper('reports')->__('Invalid date specified');
00151 }
00152 }
00153
00154
00155
00156
00157 if ($this->getRequest()->getParam('store')) {
00158 $storeIds = array($this->getParam('store'));
00159 } else if ($this->getRequest()->getParam('website')){
00160 $storeIds = Mage::app()->getWebsite($this->getRequest()->getParam('website'))->getStoreIds();
00161 } else if ($this->getRequest()->getParam('group')){
00162 $storeIds = Mage::app()->getGroup($this->getRequest()->getParam('group'))->getStoreIds();
00163 } else {
00164 $storeIds = array('');
00165 }
00166 $collection->setStoreIds($storeIds);
00167
00168 $collection->setPageSize($this->getSubReportSize());
00169
00170 $this->setCollection($collection);
00171
00172 Mage::dispatchEvent('adminhtml_widget_grid_filter_collection',
00173 array('collection' => $this->getCollection(), 'filter_values' => $this->_filterValues)
00174 );
00175 }
00176
00177 protected function _setFilterValues($data)
00178 {
00179 foreach ($data as $name => $value) {
00180
00181 $this->setFilter($name, $data[$name]);
00182
00183 }
00184 return $this;
00185 }
00186
00187
00188
00189
00190
00191
00192 public function setStoreSwitcherVisibility($visible=true)
00193 {
00194 $this->_storeSwitcherVisibility = $visible;
00195 }
00196
00197
00198
00199
00200
00201
00202 public function getStoreSwitcherVisibility()
00203 {
00204 return $this->_storeSwitcherVisibility;
00205 }
00206
00207
00208
00209
00210
00211
00212 public function getStoreSwitcherHtml()
00213 {
00214 return $this->getChildHtml('store_switcher');
00215 }
00216
00217
00218
00219
00220
00221
00222 public function setDateFilterVisibility($visible=true)
00223 {
00224 $this->_dateFilterVisibility = $visible;
00225 }
00226
00227
00228
00229
00230
00231
00232 public function getDateFilterVisibility()
00233 {
00234 return $this->_dateFilterVisibility;
00235 }
00236
00237
00238
00239
00240
00241
00242 public function setExportVisibility($visible=true)
00243 {
00244 $this->_exportVisibility = $visible;
00245 }
00246
00247
00248
00249
00250
00251
00252 public function getExportVisibility()
00253 {
00254 return $this->_exportVisibility;
00255 }
00256
00257
00258
00259
00260
00261
00262 public function setSubtotalVisibility($visible=true)
00263 {
00264 $this->_subtotalVisibility = $visible;
00265 }
00266
00267
00268
00269
00270
00271
00272 public function getSubtotalVisibility()
00273 {
00274 return $this->_subtotalVisibility;
00275 }
00276
00277 public function getPeriods()
00278 {
00279 return $this->getCollection()->getPeriods();
00280 }
00281
00282 public function getDateFormat()
00283 {
00284 return $this->getLocale()->getDateStrFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
00285 }
00286
00287
00288
00289
00290 public function getRefreshButtonHtml()
00291 {
00292 return $this->getChildHtml('refresh_button');
00293 }
00294
00295 public function setFilter($name, $value)
00296 {
00297 if ($name) {
00298 $this->_filters[$name] = $value;
00299 }
00300 }
00301
00302 public function getFilter($name)
00303 {
00304 if (isset($this->_filters[$name])) {
00305 return $this->_filters[$name];
00306 } else {
00307 return '';
00308 }
00309 }
00310
00311 public function setSubReportSize($size)
00312 {
00313 $this->_subReportSize = $size;
00314 }
00315
00316 public function getSubReportSize()
00317 {
00318 return $this->_subReportSize;
00319 }
00320
00321
00322
00323
00324
00325
00326 public function getLocale()
00327 {
00328 if (!$this->_locale) {
00329 $this->_locale = Mage::app()->getLocale();
00330 }
00331 return $this->_locale;
00332 }
00333
00334
00335
00336
00337
00338
00339
00340
00341 public function addExportType($url, $label)
00342 {
00343 $this->_exportTypes[] = new Varien_Object(
00344 array(
00345 'url' => $this->getUrl($url,
00346 array(
00347 '_current'=>true,
00348 'filter' => $this->getParam($this->getVarNameFilter(), null)
00349 )
00350 ),
00351 'label' => $label
00352 )
00353 );
00354 return $this;
00355 }
00356
00357 public function getReport($from, $to)
00358 {
00359 if ($from == '') {
00360 $from = $this->getFilter('report_from');
00361 }
00362 if ($to == '') {
00363 $to = $this->getFilter('report_to');
00364 }
00365 $totalObj = new Mage_Reports_Model_Totals();
00366 $this->setTotals($totalObj->countTotals($this, $from, $to));
00367 $this->addGrandTotals($this->getTotals());
00368 return $this->getCollection()->getReport($from, $to);
00369 }
00370
00371 public function addGrandTotals($total)
00372 {
00373 $totalData = $total->getData();
00374 foreach ($totalData as $key=>$value) {
00375 $_column = $this->getColumn($key);
00376 if ($_column->getTotal() != '') {
00377 $this->getGrandTotals()->setData($key, $this->getGrandTotals()->getData($key)+$value);
00378 }
00379 }
00380
00381
00382
00383 foreach ($this->getColumns() as $key=>$_column) {
00384 if (strpos($_column->getTotal(), '/') !== FALSE) {
00385 list($t1, $t2) = explode('/', $_column->getTotal());
00386 if ($this->getGrandTotals()->getData($t2) != 0) {
00387 $this->getGrandTotals()->setData(
00388 $key,
00389 (float)$this->getGrandTotals()->getData($t1)/$this->getGrandTotals()->getData($t2)
00390 );
00391 }
00392 }
00393 }
00394 }
00395
00396 public function getGrandTotals()
00397 {
00398 if (!$this->_grandTotals) {
00399 $this->_grandTotals = new Varien_Object();
00400 }
00401 return $this->_grandTotals;
00402 }
00403
00404 public function getPeriodText()
00405 {
00406 return $this->__('Period');
00407 }
00408
00409
00410
00411
00412
00413
00414 public function getCsv()
00415 {
00416 $csv = '';
00417 $this->_prepareGrid();
00418
00419 $data = array('"'.$this->__('Period').'"');
00420 foreach ($this->_columns as $column) {
00421 if (!$column->getIsSystem()) {
00422 $data[] = '"'.$column->getHeader().'"';
00423 }
00424 }
00425 $csv.= implode(',', $data)."\n";
00426
00427 foreach ($this->getCollection()->getIntervals() as $_index=>$_item) {
00428 $report = $this->getReport($_item['start'], $_item['end']);
00429 foreach ($report as $_subIndex=>$_subItem) {
00430 $data = array('"'.$_index.'"');
00431 foreach ($this->_columns as $column) {
00432 if (!$column->getIsSystem()) {
00433 $data[] = '"'.str_replace(array('"', '\\'), array('""', '\\\\'), $column->getRowField($_subItem)).'"';
00434 }
00435 }
00436 $csv.= implode(',', $data)."\n";
00437 }
00438 if ($this->getCountTotals() && $this->getSubtotalVisibility())
00439 {
00440 $data = array('"'.$_index.'"');
00441 $j = 0;
00442 foreach ($this->_columns as $column) {
00443 $j++;
00444 if (!$column->getIsSystem()) {
00445 $data[] = ($j==1)?'"'.$this->__('Subtotal').'"':'"'.str_replace('"', '""', $column->getRowField($this->getTotals())).'"';
00446 }
00447 }
00448 $csv.= implode(',', $data)."\n";
00449 }
00450 }
00451
00452 if ($this->getCountTotals())
00453 {
00454 $data = array('"'.$this->__('Total').'"');
00455 foreach ($this->_columns as $column) {
00456 if (!$column->getIsSystem()) {
00457 $data[] = '"'.str_replace('"', '""', $column->getRowField($this->getGrandTotals())).'"';
00458 }
00459 }
00460 $csv.= implode(',', $data)."\n";
00461 }
00462
00463 return $csv;
00464 }
00465
00466
00467
00468
00469
00470
00471 public function getExcel($filename = '')
00472 {
00473 $this->_prepareGrid();
00474
00475 $data = array();
00476 $row = array($this->__('Period'));
00477 foreach ($this->_columns as $column) {
00478 if (!$column->getIsSystem()) {
00479 $row[] = $column->getHeader();
00480 }
00481 }
00482 $data[] = $row;
00483
00484 foreach ($this->getCollection()->getIntervals() as $_index=>$_item) {
00485 $report = $this->getReport($_item['start'], $_item['end']);
00486 foreach ($report as $_subIndex=>$_subItem) {
00487 $row = array($_index);
00488 foreach ($this->_columns as $column) {
00489 if (!$column->getIsSystem()) {
00490 $row[] = $column->getRowField($_subItem);
00491 }
00492 }
00493 $data[] = $row;
00494 }
00495 if ($this->getCountTotals() && $this->getSubtotalVisibility())
00496 {
00497 $row = array($_index);
00498 $j = 0;
00499 foreach ($this->_columns as $column) {
00500 $j++;
00501 if (!$column->getIsSystem()) {
00502 $row[] = ($j==1)?$this->__('Subtotal'):$column->getRowField($this->getTotals());
00503 }
00504 }
00505 $data[] = $row;
00506 }
00507 }
00508
00509 if ($this->getCountTotals())
00510 {
00511 $row = array($this->__('Total'));
00512 foreach ($this->_columns as $column) {
00513 if (!$column->getIsSystem()) {
00514 $row[] = $column->getRowField($this->getGrandTotals());
00515 }
00516 }
00517 $data[] = $row;
00518 }
00519
00520 $xmlObj = new Varien_Convert_Parser_Xml_Excel();
00521 $xmlObj->setVar('single_sheet', $filename);
00522 $xmlObj->setData($data);
00523 $xmlObj->unparse();
00524
00525 return $xmlObj->getData();
00526 }
00527
00528 public function getSubtotalText()
00529 {
00530 return $this->__('Subtotal');
00531 }
00532
00533 public function getTotalText()
00534 {
00535 return $this->__('Total');
00536 }
00537
00538 public function getEmptyText()
00539 {
00540 return $this->__('No records found for this period.');
00541 }
00542
00543 public function getCountTotals()
00544 {
00545 $totals = $this->getGrandTotals()->getData();
00546 if (parent::getCountTotals() && count($totals)) {
00547 return true;
00548 } else {
00549 return false;
00550 }
00551 }
00552
00553
00554
00555
00556
00557
00558 public function getRefreshButtonCallback()
00559 {
00560 return "{$this->getJsObjectName()}.doFilter();";
00561 return "if ($('period_date_to').value == '' && $('period_date_from').value == '') {alert('".$this->__('Please specify at least start or end date.')."'); return false;}else {$this->getJsObjectName()}.doFilter();";
00562 }
00563
00564
00565
00566
00567
00568
00569 public function getErrors()
00570 {
00571 return $this->_errors;
00572 }
00573
00574
00575
00576
00577
00578
00579 public function getCurrentCurrencyCode()
00580 {
00581 if (is_null($this->_currentCurrencyCode)) {
00582 if ($this->getRequest()->getParam('store')) {
00583 $this->_currentCurrencyCode = Mage::app()->getStore($this->getRequest()->getParam('store'))->getBaseCurrencyCode();
00584 } else if ($this->getRequest()->getParam('website')){
00585 $this->_currentCurrencyCode = Mage::app()->getWebsite($this->getRequest()->getParam('website'))->getBaseCurrencyCode();
00586 } else if ($this->getRequest()->getParam('group')){
00587 $this->_currentCurrencyCode = Mage::app()->getGroup($this->getRequest()->getParam('group'))->getWebsite()->getBaseCurrencyCode();
00588 } else {
00589 $this->_currentCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
00590 }
00591 }
00592 return $this->_currentCurrencyCode;
00593 }
00594 }