Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Collection.php
Go to the documentation of this file.
1 <?php
7 
12 use Psr\Log\LoggerInterface as Logger;
13 
18 {
24  protected $_totals = ['lifetime' => 0, 'base_lifetime' => 0, 'base_avgsale' => 0, 'num_orders' => 0];
25 
31  protected $_customerId;
32 
38  protected $_state = null;
39 
45  protected $_orderStateCondition = null;
46 
51 
55  protected $_storeManager;
56 
65  public function __construct(
66  EntityFactory $entityFactory,
67  Logger $logger,
68  FetchStrategyInterface $fetchStrategy,
69  ManagerInterface $eventManager,
70  \Magento\Store\Model\ResourceModel\Store\CollectionFactory $storeCollectionFactory,
72  ) {
73  $this->_storeCollectionFactory = $storeCollectionFactory;
74  $this->_storeManager = $storeManager;
75  parent::__construct(
76  $entityFactory,
77  $logger,
78  $fetchStrategy,
79  $eventManager
80  );
81  }
82 
86  protected function _construct()
87  {
88  parent::_construct();
89  $this->_init(\Magento\Sales\Model\Order::class, \Magento\Sales\Model\ResourceModel\Order::class);
90  }
91 
99  {
100  $this->_customerId = (int)$customerId;
101  return $this;
102  }
103 
110  public function addStoreFilter($storeIds)
111  {
112  return $this->addFieldToFilter('store_id', ['in' => $storeIds]);
113  }
114 
122  public function setOrderStateFilter($state, $exclude = false)
123  {
124  $this->_orderStateCondition = $exclude ? 'NOT IN' : 'IN';
125  $this->_state = !is_array($state) ? [$state] : $state;
126  return $this;
127  }
128 
134  protected function _beforeLoad()
135  {
136  $this->getSelect()
137  ->columns(
138  [
139  'store_id',
140  'lifetime' => new \Zend_Db_Expr('SUM(base_grand_total)'),
141  'base_lifetime' => new \Zend_Db_Expr('SUM(base_grand_total * base_to_global_rate)'),
142  'avgsale' => new \Zend_Db_Expr('AVG(base_grand_total)'),
143  'base_avgsale' => new \Zend_Db_Expr('AVG(base_grand_total * base_to_global_rate)'),
144  'num_orders' => new \Zend_Db_Expr('COUNT(base_grand_total)')
145  ]
146  )
147  ->group('store_id');
148 
149  if ($this->_customerId) {
150  $this->addFieldToFilter('customer_id', $this->_customerId);
151  }
152 
153  if ($this->_state !== null) {
154  $condition = '';
155  switch ($this->_orderStateCondition) {
156  case 'IN':
157  $condition = 'in';
158  break;
159  case 'NOT IN':
160  $condition = 'nin';
161  break;
162  }
163  $this->addFieldToFilter('state', [$condition => $this->_state]);
164  }
165 
166  $this->_eventManager->dispatch('sales_sale_collection_query_before', ['collection' => $this]);
167  return $this;
168  }
169 
177  public function load($printQuery = false, $logQuery = false)
178  {
179  if ($this->isLoaded()) {
180  return $this;
181  }
182 
183  $this->_beforeLoad();
184 
185  $this->_renderFilters()->_renderOrders()->_renderLimit();
186 
187  $this->printLogQuery($printQuery, $logQuery);
188 
189  $data = $this->getData();
190  $this->resetData();
191 
192  $stores = $this->_storeCollectionFactory->create()->setWithoutDefaultFilter()->load()->toOptionHash();
193  $this->_items = [];
194  foreach ($data as $v) {
195  $storeObject = new \Magento\Framework\DataObject($v);
196  $storeId = $v['store_id'];
197  $storeName = isset($stores[$storeId]) ? $stores[$storeId] : null;
198  $storeObject->setStoreName(
199  $storeName
200  )->setWebsiteId(
201  $this->_storeManager->getStore($storeId)->getWebsiteId()
202  )->setAvgNormalized(
203  $v['avgsale'] * $v['num_orders']
204  );
205  $this->_items[$storeId] = $storeObject;
206  foreach (array_keys($this->_totals) as $key) {
207  $this->_totals[$key] += $storeObject->getData($key);
208  }
209  }
210 
211  if ($this->_totals['num_orders']) {
212  $this->_totals['avgsale'] = $this->_totals['base_lifetime'] / $this->_totals['num_orders'];
213  }
214 
215  $this->_setIsLoaded();
216  $this->_afterLoad();
217  return $this;
218  }
219 
225  public function getTotals()
226  {
227  return new \Magento\Framework\DataObject($this->_totals);
228  }
229 }
load($printQuery=false, $logQuery=false)
Definition: Collection.php:177
printLogQuery($printQuery=false, $logQuery=false, $sql=null)
Definition: AbstractDb.php:731
$storeManager
$logger
__construct(EntityFactory $entityFactory, Logger $logger, FetchStrategyInterface $fetchStrategy, ManagerInterface $eventManager, \Magento\Store\Model\ResourceModel\Store\CollectionFactory $storeCollectionFactory, StoreManagerInterface $storeManager)
Definition: Collection.php:65
$storeName
Definition: logo.phtml:13