Collection.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Customers by totals Report collection
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Reports\Model\ResourceModel\Customer\Totals;
  12. /**
  13. * @SuppressWarnings(PHPMD.DepthOfInheritance)
  14. * @api
  15. * @since 100.0.2
  16. */
  17. class Collection extends \Magento\Reports\Model\ResourceModel\Order\Collection
  18. {
  19. /**
  20. * Join fields
  21. *
  22. * @param string $fromDate
  23. * @param string $toDate
  24. * @return $this
  25. */
  26. protected function _joinFields($fromDate = '', $toDate = '')
  27. {
  28. $this->joinCustomerName()->groupByCustomer()->addOrdersCount()->addAttributeToFilter(
  29. 'created_at',
  30. ['from' => $fromDate, 'to' => $toDate, 'datetime' => true]
  31. );
  32. return $this;
  33. }
  34. /**
  35. * Set date range
  36. *
  37. * @param string $fromDate
  38. * @param string $toDate
  39. * @return $this
  40. */
  41. public function setDateRange($fromDate, $toDate)
  42. {
  43. $this->_reset()->_joinFields($fromDate, $toDate);
  44. return $this;
  45. }
  46. /**
  47. * Set store filter collection
  48. *
  49. * @param array $storeIds
  50. * @return $this
  51. */
  52. public function setStoreIds($storeIds)
  53. {
  54. if ($storeIds) {
  55. $this->addAttributeToFilter('store_id', ['in' => (array)$storeIds]);
  56. $this->addSumAvgTotals(1)->orderByTotalAmount();
  57. } else {
  58. $this->addSumAvgTotals()->orderByTotalAmount();
  59. }
  60. return $this;
  61. }
  62. }