Invoiced.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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;
  7. /**
  8. * Invoice report resource model
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class Invoiced extends AbstractReport
  13. {
  14. /**
  15. * Model initialization
  16. *
  17. * @return void
  18. */
  19. protected function _construct()
  20. {
  21. $this->_setResource('sales');
  22. }
  23. /**
  24. * Aggregate Invoiced data
  25. *
  26. * @param string|int|\DateTime|array|null $from
  27. * @param string|int|\DateTime|array|null $to
  28. * @return $this
  29. */
  30. public function aggregate($from = null, $to = null)
  31. {
  32. $this->_aggregateByOrderCreatedAt($from, $to);
  33. $this->_aggregateByInvoiceCreatedAt($from, $to);
  34. $this->_setFlagData(\Magento\Reports\Model\Flag::REPORT_INVOICE_FLAG_CODE);
  35. return $this;
  36. }
  37. /**
  38. * Aggregate Invoiced data by invoice created_at as period
  39. *
  40. * @param string|null $from
  41. * @param string|null $to
  42. * @return $this
  43. * @throws \Exception
  44. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  45. */
  46. protected function _aggregateByInvoiceCreatedAt($from, $to)
  47. {
  48. $table = $this->getTable('sales_invoiced_aggregated');
  49. $sourceTable = $this->getTable('sales_invoice');
  50. $orderTable = $this->getTable('sales_order');
  51. $connection = $this->getConnection();
  52. $connection->beginTransaction();
  53. try {
  54. if ($from !== null || $to !== null) {
  55. $subSelect = $this->_getTableDateRangeRelatedSelect(
  56. $sourceTable,
  57. $orderTable,
  58. ['order_id' => 'entity_id'],
  59. 'created_at',
  60. 'updated_at',
  61. $from,
  62. $to
  63. );
  64. } else {
  65. $subSelect = null;
  66. }
  67. $this->_clearTableByDateRange($table, $from, $to, $subSelect);
  68. // convert dates to current admin timezone
  69. $periodExpr = $connection->getDatePartSql(
  70. $this->getStoreTZOffsetQuery(
  71. ['source_table' => $sourceTable],
  72. 'source_table.created_at',
  73. $from,
  74. $to
  75. )
  76. );
  77. $columns = [
  78. 'period' => $periodExpr,
  79. 'store_id' => 'order_table.store_id',
  80. 'order_status' => 'order_table.status',
  81. 'orders_count' => new \Zend_Db_Expr('COUNT(order_table.entity_id)'),
  82. 'orders_invoiced' => new \Zend_Db_Expr('COUNT(order_table.entity_id)'),
  83. 'invoiced' => new \Zend_Db_Expr(
  84. 'SUM(order_table.base_total_invoiced * order_table.base_to_global_rate)'
  85. ),
  86. 'invoiced_captured' => new \Zend_Db_Expr(
  87. 'SUM(order_table.base_total_paid * order_table.base_to_global_rate)'
  88. ),
  89. 'invoiced_not_captured' => new \Zend_Db_Expr(
  90. 'SUM((order_table.base_total_invoiced - order_table.base_total_paid)' .
  91. ' * order_table.base_to_global_rate)'
  92. ),
  93. ];
  94. $select = $connection->select();
  95. $select->from(
  96. ['source_table' => $sourceTable],
  97. $columns
  98. )->joinInner(
  99. ['order_table' => $orderTable],
  100. $connection->quoteInto(
  101. 'source_table.order_id = order_table.entity_id AND order_table.state <> ?',
  102. \Magento\Sales\Model\Order::STATE_CANCELED
  103. ),
  104. []
  105. );
  106. $filterSubSelect = $connection->select();
  107. $filterSubSelect->from(
  108. ['filter_source_table' => $sourceTable],
  109. 'MAX(filter_source_table.entity_id)'
  110. )->where(
  111. 'filter_source_table.order_id = source_table.order_id'
  112. );
  113. if ($subSelect !== null) {
  114. $select->having($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
  115. }
  116. $select->where('source_table.entity_id = (?)', new \Zend_Db_Expr($filterSubSelect));
  117. unset($filterSubSelect);
  118. $select->group([$periodExpr, 'order_table.store_id', 'order_table.status']);
  119. $select->having('orders_count > 0');
  120. $insertQuery = $select->insertFromSelect($table, array_keys($columns));
  121. $connection->query($insertQuery);
  122. $select->reset();
  123. $columns = [
  124. 'period' => 'period',
  125. 'store_id' => new \Zend_Db_Expr(\Magento\Store\Model\Store::DEFAULT_STORE_ID),
  126. 'order_status' => 'order_status',
  127. 'orders_count' => new \Zend_Db_Expr('SUM(orders_count)'),
  128. 'orders_invoiced' => new \Zend_Db_Expr('SUM(orders_invoiced)'),
  129. 'invoiced' => new \Zend_Db_Expr('SUM(invoiced)'),
  130. 'invoiced_captured' => new \Zend_Db_Expr('SUM(invoiced_captured)'),
  131. 'invoiced_not_captured' => new \Zend_Db_Expr('SUM(invoiced_not_captured)'),
  132. ];
  133. $select->from($table, $columns)->where('store_id <> ?', \Magento\Store\Model\Store::DEFAULT_STORE_ID);
  134. if ($subSelect !== null) {
  135. $select->where($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
  136. }
  137. $select->group(['period', 'order_status']);
  138. $insertQuery = $select->insertFromSelect($table, array_keys($columns));
  139. $connection->query($insertQuery);
  140. $connection->commit();
  141. } catch (\Exception $e) {
  142. $connection->rollBack();
  143. throw $e;
  144. }
  145. return $this;
  146. }
  147. /**
  148. * Aggregate Invoiced data by order created_at as period
  149. *
  150. * @param string|null $from
  151. * @param string|null $to
  152. * @return $this
  153. */
  154. protected function _aggregateByOrderCreatedAt($from, $to)
  155. {
  156. $table = $this->getTable('sales_invoiced_aggregated_order');
  157. $sourceTable = $this->getTable('sales_order');
  158. $connection = $this->getConnection();
  159. if ($from !== null || $to !== null) {
  160. $subSelect = $this->_getTableDateRangeSelect($sourceTable, 'created_at', 'updated_at', $from, $to);
  161. } else {
  162. $subSelect = null;
  163. }
  164. $this->_clearTableByDateRange($table, $from, $to, $subSelect);
  165. // convert dates to current admin timezone
  166. $periodExpr = $connection->getDatePartSql($this->getStoreTZOffsetQuery($sourceTable, 'created_at', $from, $to));
  167. $columns = [
  168. 'period' => $periodExpr,
  169. 'store_id' => 'store_id',
  170. 'order_status' => 'status',
  171. 'orders_count' => new \Zend_Db_Expr('COUNT(base_total_invoiced)'),
  172. 'orders_invoiced' => new \Zend_Db_Expr(
  173. sprintf('SUM(%s)', $connection->getCheckSql('base_total_invoiced > 0', 1, 0))
  174. ),
  175. 'invoiced' => new \Zend_Db_Expr(
  176. sprintf(
  177. 'SUM(%s * %s)',
  178. $connection->getIfNullSql('base_total_invoiced', 0),
  179. $connection->getIfNullSql('base_to_global_rate', 0)
  180. )
  181. ),
  182. 'invoiced_captured' => new \Zend_Db_Expr(
  183. sprintf(
  184. 'SUM(%s * %s)',
  185. $connection->getIfNullSql('base_total_paid', 0),
  186. $connection->getIfNullSql('base_to_global_rate', 0)
  187. )
  188. ),
  189. 'invoiced_not_captured' => new \Zend_Db_Expr(
  190. sprintf(
  191. 'SUM((%s - %s) * %s)',
  192. $connection->getIfNullSql('base_total_invoiced', 0),
  193. $connection->getIfNullSql('base_total_paid', 0),
  194. $connection->getIfNullSql('base_to_global_rate', 0)
  195. )
  196. ),
  197. ];
  198. $select = $connection->select();
  199. $select->from($sourceTable, $columns)->where('state <> ?', \Magento\Sales\Model\Order::STATE_CANCELED);
  200. if ($subSelect !== null) {
  201. $select->having($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
  202. }
  203. $select->group([$periodExpr, 'store_id', 'status']);
  204. $select->having('orders_count > 0');
  205. $insertQuery = $select->insertFromSelect($table, array_keys($columns));
  206. $connection->query($insertQuery);
  207. $select->reset();
  208. $columns = [
  209. 'period' => 'period',
  210. 'store_id' => new \Zend_Db_Expr(\Magento\Store\Model\Store::DEFAULT_STORE_ID),
  211. 'order_status' => 'order_status',
  212. 'orders_count' => new \Zend_Db_Expr('SUM(orders_count)'),
  213. 'orders_invoiced' => new \Zend_Db_Expr('SUM(orders_invoiced)'),
  214. 'invoiced' => new \Zend_Db_Expr('SUM(invoiced)'),
  215. 'invoiced_captured' => new \Zend_Db_Expr('SUM(invoiced_captured)'),
  216. 'invoiced_not_captured' => new \Zend_Db_Expr('SUM(invoiced_not_captured)'),
  217. ];
  218. $select->from($table, $columns)->where('store_id <> ?', \Magento\Store\Model\Store::DEFAULT_STORE_ID);
  219. if ($subSelect !== null) {
  220. $select->where($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
  221. }
  222. $select->group(['period', 'order_status']);
  223. $insertQuery = $select->insertFromSelect($table, array_keys($columns));
  224. $connection->query($insertQuery);
  225. return $this;
  226. }
  227. }