/** * Initialize custom resource model * */ public function __construct() { parent::_construct(); $this->setModel('adminhtml/report_item'); $this->_resource = Mage::getResourceModel('sales/report')->init($this->_aggregationTable); $this->setConnection($this->getResource()->getReadConnection()); }
/** * Initialize custom resource model * */ public function __construct() { parent::_construct(); $this->setModel('Mage_Adminhtml_Model_Report_Item'); $this->_resource = Mage::getResourceModel('Mage_Sales_Model_Resource_Report')->init('sales_refunded_aggregated_order'); $this->setConnection($this->getResource()->getReadConnection()); }
/** * Redeclare parent method for applying filters after parent method * but before adding unions and calculating totals * * @return Mage_Sales_Model_Resource_Report_Bestsellers_Collection */ protected function _beforeLoad() { parent::_beforeLoad(); $this->_applyStoresFilter(); if ($this->_period) { // $selectUnions = array(); // apply date boundaries (before calling $this->_applyDateRangeFilter()) $dtFormat = Varien_Date::DATE_INTERNAL_FORMAT; $periodFrom = !is_null($this->_from) ? new Zend_Date($this->_from, $dtFormat) : null; $periodTo = !is_null($this->_to) ? new Zend_Date($this->_to, $dtFormat) : null; if ('year' == $this->_period) { if ($periodFrom) { // not the first day of the year if ($periodFrom->toValue(Zend_Date::MONTH) != 1 || $periodFrom->toValue(Zend_Date::DAY) != 1) { $dtFrom = $periodFrom->getDate(); // last day of the year $dtTo = $periodFrom->getDate()->setMonth(12)->setDay(31); if (!$periodTo || $dtTo->isEarlier($periodTo)) { $selectUnions[] = $this->_makeBoundarySelect($dtFrom->toString($dtFormat), $dtTo->toString($dtFormat)); // first day of the next year $this->_from = $periodFrom->getDate()->addYear(1)->setMonth(1)->setDay(1)->toString($dtFormat); } } } if ($periodTo) { // not the last day of the year if ($periodTo->toValue(Zend_Date::MONTH) != 12 || $periodTo->toValue(Zend_Date::DAY) != 31) { $dtFrom = $periodTo->getDate()->setMonth(1)->setDay(1); // first day of the year $dtTo = $periodTo->getDate(); if (!$periodFrom || $dtFrom->isLater($periodFrom)) { $selectUnions[] = $this->_makeBoundarySelect($dtFrom->toString($dtFormat), $dtTo->toString($dtFormat)); // last day of the previous year $this->_to = $periodTo->getDate()->subYear(1)->setMonth(12)->setDay(31)->toString($dtFormat); } } } if ($periodFrom && $periodTo) { // the same year if ($periodFrom->toValue(Zend_Date::YEAR) == $periodTo->toValue(Zend_Date::YEAR)) { $dtFrom = $periodFrom->getDate(); $dtTo = $periodTo->getDate(); $selectUnions[] = $this->_makeBoundarySelect($dtFrom->toString($dtFormat), $dtTo->toString($dtFormat)); $this->getSelect()->where('1<>1'); } } } else { if ('month' == $this->_period) { if ($periodFrom) { // not the first day of the month if ($periodFrom->toValue(Zend_Date::DAY) != 1) { $dtFrom = $periodFrom->getDate(); // last day of the month $dtTo = $periodFrom->getDate()->addMonth(1)->setDay(1)->subDay(1); if (!$periodTo || $dtTo->isEarlier($periodTo)) { $selectUnions[] = $this->_makeBoundarySelect($dtFrom->toString($dtFormat), $dtTo->toString($dtFormat)); // first day of the next month $this->_from = $periodFrom->getDate()->addMonth(1)->setDay(1)->toString($dtFormat); } } } if ($periodTo) { // not the last day of the month if ($periodTo->toValue(Zend_Date::DAY) != $periodTo->toValue(Zend_Date::MONTH_DAYS)) { $dtFrom = $periodTo->getDate()->setDay(1); // first day of the month $dtTo = $periodTo->getDate(); if (!$periodFrom || $dtFrom->isLater($periodFrom)) { $selectUnions[] = $this->_makeBoundarySelect($dtFrom->toString($dtFormat), $dtTo->toString($dtFormat)); // last day of the previous month $this->_to = $periodTo->getDate()->setDay(1)->subDay(1)->toString($dtFormat); } } } if ($periodFrom && $periodTo) { // the same month if ($periodFrom->toValue(Zend_Date::YEAR) == $periodTo->toValue(Zend_Date::YEAR) && $periodFrom->toValue(Zend_Date::MONTH) == $periodTo->toValue(Zend_Date::MONTH)) { $dtFrom = $periodFrom->getDate(); $dtTo = $periodTo->getDate(); $selectUnions[] = $this->_makeBoundarySelect($dtFrom->toString($dtFormat), $dtTo->toString($dtFormat)); $this->getSelect()->where('1<>1'); } } } } $this->_applyDateRangeFilter(); // add unions to select if ($selectUnions) { $unionParts = array(); $cloneSelect = clone $this->getSelect(); $helper = Mage::getResourceHelper('core'); $unionParts[] = '(' . $cloneSelect . ')'; foreach ($selectUnions as $union) { $query = $helper->getQueryUsingAnalyticFunction($union); $unionParts[] = '(' . $query . ')'; } $this->getSelect()->reset()->union($unionParts, Zend_Db_Select::SQL_UNION_ALL); } if ($this->isTotals()) { // calculate total $cloneSelect = clone $this->getSelect(); $this->getSelect()->reset()->from($cloneSelect, $this->getAggregatedColumns()); } else { // add sorting $this->getSelect()->order(array('period ASC', 'qty_ordered DESC')); } } return $this; }
/** * Apply collection custom filter * * @return Mage_Sales_Model_Resource_Report_Collection_Abstract */ protected function _applyCustomFilter() { $this->_applyRulesFilter(); return parent::_applyCustomFilter(); }
/** * Load * * @param boolean $printQuery * @param boolean $logQuery * @return Mage_Tax_Model_Resource_Report_Updatedat_Collection */ public function load($printQuery = false, $logQuery = false) { if ($this->isLoaded()) { return $this; } $this->_initSelect(); $this->setApplyFilters(false); return parent::load($printQuery, $logQuery); }