Collection.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\ResourceModel\Report\Order;
  7. /**
  8. * Report order collection
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class Collection extends \Magento\Sales\Model\ResourceModel\Report\Collection\AbstractCollection
  13. {
  14. /**
  15. * Period format
  16. *
  17. * @var string
  18. */
  19. protected $_periodFormat;
  20. /**
  21. * Aggregated Data Table
  22. *
  23. * @var string
  24. */
  25. protected $_aggregationTable = 'sales_order_aggregated_created';
  26. /**
  27. * Selected columns
  28. *
  29. * @var array
  30. */
  31. protected $_selectedColumns = [];
  32. /**
  33. * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  34. * @param \Psr\Log\LoggerInterface $logger
  35. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  36. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  37. * @param \Magento\Sales\Model\ResourceModel\Report $resource
  38. * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
  39. */
  40. public function __construct(
  41. \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
  42. \Psr\Log\LoggerInterface $logger,
  43. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  44. \Magento\Framework\Event\ManagerInterface $eventManager,
  45. \Magento\Sales\Model\ResourceModel\Report $resource,
  46. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null
  47. ) {
  48. $resource->init($this->_aggregationTable);
  49. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $resource, $connection);
  50. }
  51. /**
  52. * Get selected columns
  53. *
  54. * @return array
  55. */
  56. protected function _getSelectedColumns()
  57. {
  58. $connection = $this->getConnection();
  59. if ('month' == $this->_period) {
  60. $this->_periodFormat = $connection->getDateFormatSql('period', '%Y-%m');
  61. } elseif ('year' == $this->_period) {
  62. $this->_periodFormat = $connection->getDateExtractSql(
  63. 'period',
  64. \Magento\Framework\DB\Adapter\AdapterInterface::INTERVAL_YEAR
  65. );
  66. } else {
  67. $this->_periodFormat = $connection->getDateFormatSql('period', '%Y-%m-%d');
  68. }
  69. if (!$this->isTotals()) {
  70. $this->_selectedColumns = [
  71. 'period' => $this->_periodFormat,
  72. 'orders_count' => 'SUM(orders_count)',
  73. 'total_qty_ordered' => 'SUM(total_qty_ordered)',
  74. 'total_qty_invoiced' => 'SUM(total_qty_invoiced)',
  75. 'total_income_amount' => 'SUM(total_income_amount)',
  76. 'total_revenue_amount' => 'SUM(total_revenue_amount)',
  77. 'total_profit_amount' => 'SUM(total_profit_amount)',
  78. 'total_invoiced_amount' => 'SUM(total_invoiced_amount)',
  79. 'total_canceled_amount' => 'SUM(total_canceled_amount)',
  80. 'total_paid_amount' => 'SUM(total_paid_amount)',
  81. 'total_refunded_amount' => 'SUM(total_refunded_amount)',
  82. 'total_tax_amount' => 'SUM(total_tax_amount)',
  83. 'total_tax_amount_actual' => 'SUM(total_tax_amount_actual)',
  84. 'total_shipping_amount' => 'SUM(total_shipping_amount)',
  85. 'total_shipping_amount_actual' => 'SUM(total_shipping_amount_actual)',
  86. 'total_discount_amount' => 'SUM(total_discount_amount)',
  87. 'total_discount_amount_actual' => 'SUM(total_discount_amount_actual)',
  88. ];
  89. }
  90. if ($this->isTotals()) {
  91. $this->_selectedColumns = $this->getAggregatedColumns();
  92. }
  93. return $this->_selectedColumns;
  94. }
  95. /**
  96. * Apply custom columns before load
  97. *
  98. * @return $this
  99. */
  100. protected function _beforeLoad()
  101. {
  102. $this->getSelect()->from($this->getResource()->getMainTable(), $this->_getSelectedColumns());
  103. if (!$this->isTotals()) {
  104. $this->getSelect()->group($this->_periodFormat);
  105. }
  106. return parent::_beforeLoad();
  107. }
  108. }