Createdat.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Tax report resource model with aggregation by created at
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Tax\Model\ResourceModel\Report\Tax;
  12. /**
  13. * Class for tax report resource model with aggregation by created at
  14. */
  15. class Createdat extends \Magento\Reports\Model\ResourceModel\Report\AbstractReport
  16. {
  17. /**
  18. * Resource initialization
  19. *
  20. * @return void
  21. */
  22. protected function _construct()
  23. {
  24. $this->_init('tax_order_aggregated_created', 'id');
  25. }
  26. /**
  27. * Aggregate Tax data by order created at
  28. *
  29. * @param mixed $from
  30. * @param mixed $to
  31. * @return $this
  32. */
  33. public function aggregate($from = null, $to = null)
  34. {
  35. return $this->_aggregateByOrder('created_at', $from, $to);
  36. }
  37. /**
  38. * Aggregate Tax data by orders
  39. *
  40. * @param string $aggregationField
  41. * @param mixed $from
  42. * @param mixed $to
  43. * @return $this
  44. * @throws \Exception
  45. */
  46. protected function _aggregateByOrder($aggregationField, $from, $to)
  47. {
  48. $connection = $this->getConnection();
  49. $salesAdapter = $this->_resources->getConnection('sales');
  50. $connection->beginTransaction();
  51. try {
  52. if ($from !== null || $to !== null) {
  53. $subSelect = $this->_getTableDateRangeSelect(
  54. $this->getTable('sales_order'),
  55. 'created_at',
  56. 'updated_at',
  57. $from,
  58. $to
  59. );
  60. } else {
  61. $subSelect = null;
  62. }
  63. $this->_clearTableByDateRange($this->getMainTable(), $from, $to, $subSelect, false, $salesAdapter);
  64. // convert dates to current admin timezone
  65. $periodExpr = $connection->getDatePartSql(
  66. $this->getStoreTZOffsetQuery(
  67. ['e' => $this->getTable('sales_order')],
  68. 'e.' . $aggregationField,
  69. $from,
  70. $to,
  71. null,
  72. $salesAdapter
  73. )
  74. );
  75. $columns = [
  76. 'period' => $periodExpr,
  77. 'store_id' => 'e.store_id',
  78. 'code' => 'tax.code',
  79. 'order_status' => 'e.status',
  80. 'percent' => 'MAX(tax.' . $connection->quoteIdentifier('percent') . ')',
  81. 'orders_count' => 'COUNT(DISTINCT e.entity_id)',
  82. 'tax_base_amount_sum' => 'SUM(tax.base_real_amount * e.base_to_global_rate)',
  83. ];
  84. $select = $connection->select()->from(
  85. ['tax' => $this->getTable('sales_order_tax')],
  86. $columns
  87. )->joinInner(
  88. ['e' => $this->getTable('sales_order')],
  89. 'e.entity_id = tax.order_id',
  90. []
  91. )->useStraightJoin()->where(
  92. 'e.state NOT IN (?)',
  93. [\Magento\Sales\Model\Order::STATE_PENDING_PAYMENT, \Magento\Sales\Model\Order::STATE_NEW]
  94. );
  95. if ($subSelect !== null) {
  96. $select->having($this->_makeConditionFromDateRangeSelect($subSelect, 'period', $salesAdapter));
  97. }
  98. $select->group([$periodExpr, 'e.store_id', 'code', 'tax.percent', 'e.status']);
  99. $aggregatedData = $salesAdapter->fetchAll($select);
  100. if ($aggregatedData) {
  101. $connection->insertArray($this->getMainTable(), array_keys($columns), $aggregatedData);
  102. }
  103. $columns = [
  104. 'period' => 'period',
  105. 'store_id' => new \Zend_Db_Expr(\Magento\Store\Model\Store::DEFAULT_STORE_ID),
  106. 'code' => 'code',
  107. 'order_status' => 'order_status',
  108. 'percent' => 'MAX(' . $connection->quoteIdentifier('percent') . ')',
  109. 'orders_count' => 'SUM(orders_count)',
  110. 'tax_base_amount_sum' => 'SUM(tax_base_amount_sum)',
  111. ];
  112. $select->reset()->from($this->getMainTable(), $columns)->where('store_id <> ?', 0);
  113. if ($subSelect !== null) {
  114. $select->where($this->_makeConditionFromDateRangeSelect($subSelect, 'period', $salesAdapter));
  115. }
  116. $select->group(['period', 'code', 'percent', 'order_status']);
  117. $insertQuery = $connection->insertFromSelect($select, $this->getMainTable(), array_keys($columns));
  118. $connection->query($insertQuery);
  119. $connection->commit();
  120. } catch (\Exception $e) {
  121. $connection->rollBack();
  122. throw $e;
  123. }
  124. return $this;
  125. }
  126. }