/** * Apply custom columns before load * * @return $this */ protected function _beforeLoad() { $this->getSelect()->from($this->getResource()->getMainTable(), $this->_getSelectedColumns()); if (!$this->isTotals()) { $this->getSelect()->group($this->_periodFormat); } return parent::_beforeLoad(); }
/** * @return $this */ protected function _beforeLoad() { $this->getSelect()->columns($this->_getSelectedColumns()); if (!$this->isTotals()) { $this->getSelect()->group($this->_periodFormat); $this->getSelect()->having('SUM(orders_count) > 0'); } return parent::_beforeLoad(); }
/** * Apply custom columns before load * * @return $this */ protected function _beforeLoad() { $this->getSelect()->from($this->getResource()->getMainTable(), $this->_getSelectedColumns()); if (!$this->isTotals() && !$this->isSubTotals()) { $this->getSelect()->group([$this->_periodFormat, 'code', 'percent']); } if ($this->isSubTotals()) { $this->getSelect()->group([$this->_periodFormat]); } return parent::_beforeLoad(); }
/** * Redeclare parent method for applying filters after parent method * but before adding unions and calculating totals * * @return $this|\Magento\Framework\Model\ModelResource\Db\Collection\AbstractCollection * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function _beforeLoad() { parent::_beforeLoad(); $this->_applyStoresFilter(); if ($this->_period) { $selectUnions = []; // apply date boundaries (before calling $this->_applyDateRangeFilter()) $periodFrom = !is_null($this->_from) ? new \DateTime($this->_from) : null; $periodTo = !is_null($this->_to) ? new \DateTime($this->_to) : null; if ('year' == $this->_period) { if ($periodFrom) { // not the first day of the year if ($periodFrom->format('m') != 1 || $periodFrom->format('d') != 1) { $dtFrom = clone $periodFrom; // last day of the year $dtTo = clone $periodFrom; $dtTo->setDate($dtTo->format('Y'), 12, 31); if (!$periodTo || $dtTo < $periodTo) { $selectUnions[] = $this->_makeBoundarySelect($dtFrom->format('Y-m-d'), $dtTo->format('Y-m-d')); // first day of the next year $this->_from = clone $periodFrom; $this->_from->modify('+1 year'); $this->_from->setDate($this->_from->format('Y'), 1, 1); $this->_from = $this->_from->format('Y-m-d'); } } } if ($periodTo) { // not the last day of the year if ($periodTo->format('m') != 12 || $periodTo->format('d') != 31) { $dtFrom = clone $periodTo; $dtFrom->setDate($dtFrom->format('Y'), 1, 1); // first day of the year $dtTo = clone $periodTo; if (!$periodFrom || $dtFrom > $periodFrom) { $selectUnions[] = $this->_makeBoundarySelect($dtFrom->format('Y-m-d'), $dtTo->format('Y-m-d')); // last day of the previous year $this->_to = clone $periodTo; $this->_to->modify('-1 year'); $this->_to->setDate($this->_to->format('Y'), 12, 31); $this->_to = $this->_to->format('Y-m-d'); } } } if ($periodFrom && $periodTo) { // the same year if ($periodTo->format('Y') == $periodFrom->format('Y')) { $dtFrom = clone $periodFrom; $dtTo = clone $periodTo; $selectUnions[] = $this->_makeBoundarySelect($dtFrom->format('Y-m-d'), $dtTo->format('Y-m-d')); $this->getSelect()->where('1<>1'); } } } elseif ('month' == $this->_period) { if ($periodFrom) { // not the first day of the month if ($periodFrom->format('d') != 1) { $dtFrom = clone $periodFrom; // last day of the month $dtTo = clone $periodFrom; $dtTo->modify('+1 month'); $dtTo->setDate($dtTo->format('Y'), $dtTo->format('m'), 1); $dtTo->modify('-1 day'); if (!$periodTo || $dtTo < $periodTo) { $selectUnions[] = $this->_makeBoundarySelect($dtFrom->format('Y-m-d'), $dtTo->format('Y-m-d')); // first day of the next month $this->_from = clone $periodFrom; $this->_from->modify('+1 month'); $this->_from->setDate($this->_from->format('Y'), $this->_from->format('m'), 1); $this->_from = $this->_from->format('Y-m-d'); } } } if ($periodTo) { // not the last day of the month if ($periodTo->format('d') != $periodTo->format('t')) { $dtFrom = clone $periodTo; $dtFrom->setDate($dtFrom->format('Y'), $dtFrom->format('m'), 1); // first day of the month $dtTo = clone $periodTo; if (!$periodFrom || $dtFrom > $periodFrom) { $selectUnions[] = $this->_makeBoundarySelect($dtFrom->format('Y-m-d'), $dtTo->format('Y-m-d')); // last day of the previous month $this->_to = clone $periodTo; $this->_to->setDate($this->_to->format('Y'), $this->_to->format('m'), 1); $this->_to->modify('-1 day'); $this->_to = $this->_to->format('Y-m-d'); } } } if ($periodFrom && $periodTo) { // the same month if ($periodTo->format('Y') == $periodFrom->format('Y') && $periodTo->format('m') == $periodFrom->format('m')) { $dtFrom = clone $periodFrom; $dtTo = clone $periodTo; $selectUnions[] = $this->_makeBoundarySelect($dtFrom->format('Y-m-d'), $dtTo->format('Y-m-d')); $this->getSelect()->where('1<>1'); } } } $this->_applyDateRangeFilter(); // add unions to select if ($selectUnions) { $unionParts = []; $cloneSelect = clone $this->getSelect(); $unionParts[] = '(' . $cloneSelect . ')'; foreach ($selectUnions as $union) { $unionParts[] = '(' . $union . ')'; } $this->getSelect()->reset()->union($unionParts, \Magento\Framework\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(['period ASC', $this->getOrderedField() . ' DESC']); } } return $this; }
/** * Apply collection custom filter * * @return \Magento\Sales\Model\ResourceModel\Report\Collection\AbstractCollection */ protected function _applyCustomFilter() { $this->_applyRulesFilter(); return parent::_applyCustomFilter(); }