/app/code/local/AW/Advancedreports/Model/Mysql4/Report/Collection.php

https://bitbucket.org/spenna/alexoo_produzione · PHP · 197 lines · 142 code · 18 blank · 37 comment · 19 complexity · cdbfbd858dfdcacd0b4590aa520afc31 MD5 · raw file

  1. <?php
  2. /**
  3. * aheadWorks Co.
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the EULA
  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://ecommerce.aheadworks.com/AW-LICENSE-COMMUNITY.txt
  11. *
  12. * =================================================================
  13. * MAGENTO EDITION USAGE NOTICE
  14. * =================================================================
  15. * This package designed for Magento COMMUNITY edition
  16. * aheadWorks does not guarantee correct work of this extension
  17. * on any other Magento edition except Magento COMMUNITY edition.
  18. * aheadWorks does not provide extension support in case of
  19. * incorrect edition usage.
  20. * =================================================================
  21. *
  22. * @category AW
  23. * @package AW_Advancedreports
  24. * @copyright Copyright (c) 2010-2011 aheadWorks Co. (http://www.aheadworks.com)
  25. * @license http://ecommerce.aheadworks.com/AW-LICENSE-COMMUNITY.txt
  26. */?>
  27. <?php
  28. class AW_Advancedreports_Model_Mysql4_Report_Collection extends Mage_Reports_Model_Mysql4_Report_Collection
  29. {
  30. /**
  31. * Retrives helper
  32. * @return AW_Advancedreports_Helper_Data
  33. */
  34. protected function _helper()
  35. {
  36. return Mage::helper('advancedreports');
  37. }
  38. /**
  39. * Overrides standard periods
  40. * @return array
  41. */
  42. public function getPeriods()
  43. {
  44. return array(
  45. 'day'=>Mage::helper('advancedreports')->__('Day'),
  46. 'week'=>Mage::helper('advancedreports')->__('Week'),
  47. 'month'=>Mage::helper('advancedreports')->__('Month'),
  48. 'quarter'=>Mage::helper('advancedreports')->__('Quarter'),
  49. 'year'=>Mage::helper('advancedreports')->__('Year')
  50. );
  51. }
  52. protected function processIntervals()
  53. {
  54. foreach ($this->_intervals as $key=>&$interval){
  55. $interval['start'] = $this->_helper()->timezoneFactory($interval['start']);
  56. $interval['end'] = $this->_helper()->timezoneFactory($interval['end']);
  57. }
  58. }
  59. /**
  60. * Overrides standard getIntervals
  61. * @return array
  62. */
  63. public function getIntervals()
  64. {
  65. if (!$this->_intervals){
  66. $this->_intervals = array();
  67. if (!$this->_from && !$this->_to)
  68. {
  69. return $this->_intervals;
  70. }
  71. $t = array();
  72. $diff = 0;
  73. if ($this->_period == 'week'){
  74. $firstWeekDay = Mage::getStoreConfig('general/locale/firstday');
  75. $dateStart = new Zend_Date($this->_from);
  76. $curWeekDay = $dateStart->toString('e');
  77. if ($curWeekDay > $firstWeekDay){
  78. $firstWeekDay += 7;
  79. }
  80. $diff = abs($curWeekDay - $firstWeekDay);
  81. }
  82. if ($this->_period == 'week' && ($diff > 0)){
  83. $dateStart = new Zend_Date($this->_from);
  84. $dateStart2 = new Zend_Date($this->_from);
  85. $dateStart2->addDay($diff);
  86. $t['title'] = $dateStart->toString(Mage::app()->getLocale()->getDateFormat());
  87. $t['start'] = $dateStart->toString('yyyy-MM-dd 00:00:00');
  88. $dateStart->addDay($diff)->subDay(1);
  89. $t['title'] .= ' - '.$dateStart->toString(Mage::app()->getLocale()->getDateFormat());
  90. $t['end'] = $dateStart->toString('yyyy-MM-dd 23:59:59');
  91. $dateStart->addDay(1);
  92. if (isset($t['title'])){
  93. $this->_intervals[$t['title']] = $t;
  94. }
  95. $dateStart2 = new Zend_Date($this->_from);
  96. $dateEnd = new Zend_Date($this->_to);
  97. } else {
  98. $dateStart = new Zend_Date($this->_from);
  99. $dateStart2 = new Zend_Date($this->_from);
  100. $dateEnd = new Zend_Date($this->_to);
  101. }
  102. while ($dateStart->compare($dateEnd)<=0) {
  103. switch ($this->_period) {
  104. case 'day' :
  105. $t['title'] = $dateStart->toString(Mage::app()->getLocale()->getDateFormat());
  106. $t['start'] = $dateStart->toString('yyyy-MM-dd HH:mm:ss');
  107. $t['end'] = $dateStart->toString('yyyy-MM-dd 23:59:59');
  108. $dateStart->addDay(1);
  109. break;
  110. case 'week':
  111. $t['title'] = $dateStart->toString(Mage::app()->getLocale()->getDateFormat());
  112. $t['start'] = $dateStart->toString('yyyy-MM-dd 00:00:00');
  113. $dateStart->addWeek(1)->subDay(1);
  114. $t['title'] .= ' - '.$dateStart->toString(Mage::app()->getLocale()->getDateFormat());
  115. $t['end'] = $dateStart->toString('yyyy-MM-dd 23:59:59');
  116. $dateStart->addDay(1);
  117. break;
  118. case 'month':
  119. $t['title'] = $dateStart->toString('MM/yyyy');
  120. $t['start'] = $dateStart->toString('yyyy-MM-01 00:00:00');
  121. $t['end'] = $dateStart->toString('yyyy-MM-'.date('t', $dateStart->getTimestamp()).' 23:59:59');
  122. $dateStart->addMonth(1);
  123. break;
  124. case 'quarter':
  125. $month = (integer)$dateStart->toString('MM');
  126. $num = round($month / 3) + 1;
  127. $t['title'] = Mage::helper('advancedreports')->__('Q').$num.$dateStart->toString('/yyyy');
  128. $t['start'] = $dateStart->toString('yyyy-MM-01 00:00:00');
  129. $dateStart->addMonth(2);
  130. $t['end'] = $dateStart->toString('yyyy-MM-'.date('t', $dateStart->getTimestamp()).' 23:59:59');
  131. $dateStart->addMonth(1);
  132. break;
  133. case 'year':
  134. $t['title'] = $dateStart->toString('yyyy');
  135. $t['start'] = $dateStart->toString('yyyy-01-01 00:00:00');
  136. $t['end'] = $dateStart->toString('yyyy-12-31 23:59:59');
  137. $dateStart->addYear(1);
  138. break;
  139. default:
  140. Mage::throwException("Report tried to get intervals without a period.");
  141. }
  142. if (isset($t['title'])){
  143. $this->_intervals[$t['title']] = $t;
  144. }
  145. // echo $t['start'].' - '.$t['end'].'<hr>';
  146. }
  147. if ($this->_period != 'day')
  148. {
  149. $titles = array_keys($this->_intervals);
  150. if (count($titles) > 0)
  151. {
  152. $this->_intervals[$titles[0]]['start'] = $dateStart2->toString('yyyy-MM-dd 00:00:00');
  153. $this->_intervals[$titles[count($titles)-1]]['end'] = $dateEnd->toString('yyyy-MM-dd 23:59:59');
  154. if ($this->_period == 'week')
  155. {
  156. $t = $this->_intervals[$titles[count($titles)-1]];
  157. unset( $this->_intervals[$titles[count($titles)-1]] );
  158. $date = new Zend_Date( $t['start'], 'yyyy-MM-dd 00:00:00');
  159. $t['title'] = $date->toString(Mage::app()->getLocale()->getDateFormat());
  160. unset($date);
  161. $date = new Zend_Date( $t['end'], 'yyyy-MM-dd 23:59:59');
  162. $t['title'] .= ' - '.$date->toString(Mage::app()->getLocale()->getDateFormat());
  163. $this->_intervals[$t['title']] = $t;
  164. }
  165. }
  166. }
  167. $this->processIntervals();
  168. }
  169. return $this->_intervals;
  170. }
  171. public function setPageSize($size)
  172. {
  173. return $this;
  174. }
  175. public function setCurPage($page)
  176. {
  177. return $this;
  178. }
  179. }