Collection.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Tax report collection
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Tax\Model\ResourceModel\Report;
  12. class Collection extends \Magento\Sales\Model\ResourceModel\Report\Collection\AbstractCollection
  13. {
  14. /**
  15. * @var \Zend_Db_Expr
  16. */
  17. protected $_periodFormat;
  18. /**
  19. * Aggregated Data Table
  20. *
  21. * @var string
  22. */
  23. protected $_aggregationTable = 'tax_order_aggregated_created';
  24. /**
  25. * @var array
  26. */
  27. protected $_selectedColumns = [];
  28. /**
  29. * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  30. * @param \Psr\Log\LoggerInterface $logger
  31. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  32. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  33. * @param \Magento\Sales\Model\ResourceModel\Report $resource
  34. * @param mixed $connection
  35. */
  36. public function __construct(
  37. \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
  38. \Psr\Log\LoggerInterface $logger,
  39. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  40. \Magento\Framework\Event\ManagerInterface $eventManager,
  41. \Magento\Sales\Model\ResourceModel\Report $resource,
  42. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null
  43. ) {
  44. $resource->init($this->_aggregationTable);
  45. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $resource, $connection);
  46. }
  47. /**
  48. * @return array
  49. */
  50. protected function _getSelectedColumns()
  51. {
  52. if ('month' == $this->_period) {
  53. $this->_periodFormat = $this->getConnection()->getDateFormatSql('period', '%Y-%m');
  54. } elseif ('year' == $this->_period) {
  55. $this->_periodFormat = $this->getConnection()->getDateFormatSql('period', '%Y');
  56. } else {
  57. $this->_periodFormat = $this->getConnection()->getDateFormatSql('period', '%Y-%m-%d');
  58. }
  59. if (!$this->isTotals() && !$this->isSubTotals()) {
  60. $this->_selectedColumns = [
  61. 'period' => $this->_periodFormat,
  62. 'code' => 'code',
  63. 'percent' => 'percent',
  64. 'orders_count' => 'SUM(orders_count)',
  65. 'tax_base_amount_sum' => 'SUM(tax_base_amount_sum)',
  66. ];
  67. }
  68. if ($this->isTotals()) {
  69. $this->_selectedColumns = $this->getAggregatedColumns();
  70. }
  71. if ($this->isSubTotals()) {
  72. $this->_selectedColumns = $this->getAggregatedColumns() + ['period' => $this->_periodFormat];
  73. }
  74. return $this->_selectedColumns;
  75. }
  76. /**
  77. * Apply custom columns before load
  78. *
  79. * @return $this
  80. */
  81. protected function _beforeLoad()
  82. {
  83. $this->getSelect()->from($this->getResource()->getMainTable(), $this->_getSelectedColumns());
  84. if (!$this->isTotals() && !$this->isSubTotals()) {
  85. $this->getSelect()->group([$this->_periodFormat, 'code', 'percent']);
  86. }
  87. if ($this->isSubTotals()) {
  88. $this->getSelect()->group([$this->_periodFormat]);
  89. }
  90. return parent::_beforeLoad();
  91. }
  92. }