Rule.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Model\ResourceModel\Report;
  7. /**
  8. * Rule report resource model
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class Rule extends \Magento\Reports\Model\ResourceModel\Report\AbstractReport
  13. {
  14. /**
  15. * @var \Magento\SalesRule\Model\ResourceModel\Report\Rule\CreatedatFactory
  16. */
  17. protected $_createdatFactory;
  18. /**
  19. * @var \Magento\SalesRule\Model\ResourceModel\Report\Rule\UpdatedatFactory
  20. */
  21. protected $_updatedatFactory;
  22. /**
  23. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  24. * @param \Psr\Log\LoggerInterface $logger
  25. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  26. * @param \Magento\Reports\Model\FlagFactory $reportsFlagFactory
  27. * @param \Magento\Framework\Stdlib\DateTime\Timezone\Validator $timezoneValidator
  28. * @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
  29. * @param \Magento\SalesRule\Model\ResourceModel\Report\Rule\CreatedatFactory $createdatFactory
  30. * @param \Magento\SalesRule\Model\ResourceModel\Report\Rule\UpdatedatFactory $updatedatFactory
  31. * @param string $connectionName
  32. */
  33. public function __construct(
  34. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  35. \Psr\Log\LoggerInterface $logger,
  36. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
  37. \Magento\Reports\Model\FlagFactory $reportsFlagFactory,
  38. \Magento\Framework\Stdlib\DateTime\Timezone\Validator $timezoneValidator,
  39. \Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
  40. \Magento\SalesRule\Model\ResourceModel\Report\Rule\CreatedatFactory $createdatFactory,
  41. \Magento\SalesRule\Model\ResourceModel\Report\Rule\UpdatedatFactory $updatedatFactory,
  42. $connectionName = null
  43. ) {
  44. parent::__construct(
  45. $context,
  46. $logger,
  47. $localeDate,
  48. $reportsFlagFactory,
  49. $timezoneValidator,
  50. $dateTime,
  51. $connectionName
  52. );
  53. $this->_createdatFactory = $createdatFactory;
  54. $this->_updatedatFactory = $updatedatFactory;
  55. }
  56. /**
  57. * Resource Report Rule constructor
  58. *
  59. * @return void
  60. */
  61. protected function _construct()
  62. {
  63. $this->_setResource('salesrule');
  64. }
  65. /**
  66. * Aggregate Coupons data
  67. *
  68. * @param mixed|null $from
  69. * @param mixed|null $to
  70. * @return $this
  71. */
  72. public function aggregate($from = null, $to = null)
  73. {
  74. $this->_createdatFactory->create()->aggregate($from, $to);
  75. $this->_updatedatFactory->create()->aggregate($from, $to);
  76. $this->_setFlagData(\Magento\Reports\Model\Flag::REPORT_COUPONS_FLAG_CODE);
  77. return $this;
  78. }
  79. /**
  80. * Get all unique Rule Names from aggregated coupons usage data
  81. *
  82. * @return array
  83. */
  84. public function getUniqRulesNamesList()
  85. {
  86. $connection = $this->getConnection();
  87. $tableName = $this->getTable('salesrule_coupon_aggregated');
  88. $select = $connection->select()->from(
  89. $tableName,
  90. new \Zend_Db_Expr('DISTINCT rule_name')
  91. )->where(
  92. 'rule_name IS NOT NULL'
  93. )->where(
  94. 'rule_name <> ?',
  95. ''
  96. )->order(
  97. 'rule_name ASC'
  98. );
  99. $rulesNames = $connection->fetchAll($select);
  100. $result = [];
  101. foreach ($rulesNames as $row) {
  102. $result[] = $row['rule_name'];
  103. }
  104. return $result;
  105. }
  106. }