PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-sales/Model/ResourceModel/Report/Shipping.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 254 lines | 187 code | 27 blank | 40 comment | 10 complexity | 99779d96ed73dfc70ba5262f22071971 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\ResourceModel\Report;
  7. /**
  8. * Shipping report resource model
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class Shipping 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 Shipping 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->_aggregateByShippingCreatedAt($from, $to);
  34. $this->_setFlagData(\Magento\Reports\Model\Flag::REPORT_SHIPPING_FLAG_CODE);
  35. return $this;
  36. }
  37. /**
  38. * Aggregate shipping report by order create_at as period
  39. *
  40. * @param string|null $from
  41. * @param string|null $to
  42. * @return $this
  43. * @throws \Exception
  44. */
  45. protected function _aggregateByOrderCreatedAt($from, $to)
  46. {
  47. $table = $this->getTable('sales_shipping_aggregated_order');
  48. $sourceTable = $this->getTable('sales_order');
  49. $connection = $this->getConnection();
  50. $connection->beginTransaction();
  51. try {
  52. if ($from !== null || $to !== null) {
  53. $subSelect = $this->_getTableDateRangeSelect($sourceTable, 'created_at', 'updated_at', $from, $to);
  54. } else {
  55. $subSelect = null;
  56. }
  57. $this->_clearTableByDateRange($table, $from, $to, $subSelect);
  58. // convert dates to current admin timezone
  59. $periodExpr = $connection->getDatePartSql(
  60. $this->getStoreTZOffsetQuery($sourceTable, 'created_at', $from, $to)
  61. );
  62. $shippingCanceled = $connection->getIfNullSql('base_shipping_canceled', 0);
  63. $shippingRefunded = $connection->getIfNullSql('base_shipping_refunded', 0);
  64. $columns = [
  65. 'period' => $periodExpr,
  66. 'store_id' => 'store_id',
  67. 'order_status' => 'status',
  68. 'shipping_description' => 'shipping_description',
  69. 'orders_count' => new \Zend_Db_Expr('COUNT(entity_id)'),
  70. 'total_shipping' => new \Zend_Db_Expr(
  71. "SUM((base_shipping_amount - {$shippingCanceled}) * base_to_global_rate)"
  72. ),
  73. 'total_shipping_actual' => new \Zend_Db_Expr(
  74. "SUM((base_shipping_invoiced - {$shippingRefunded}) * base_to_global_rate)"
  75. ),
  76. ];
  77. $select = $connection->select();
  78. $select->from(
  79. $sourceTable,
  80. $columns
  81. )->where(
  82. 'state NOT IN (?)',
  83. [\Magento\Sales\Model\Order::STATE_PENDING_PAYMENT, \Magento\Sales\Model\Order::STATE_NEW]
  84. )->where(
  85. 'is_virtual = 0'
  86. );
  87. if ($subSelect !== null) {
  88. $select->having($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
  89. }
  90. $select->group([$periodExpr, 'store_id', 'status', 'shipping_description']);
  91. $select->having('orders_count > 0');
  92. $insertQuery = $select->insertFromSelect($table, array_keys($columns));
  93. $connection->query($insertQuery);
  94. $select->reset();
  95. $columns = [
  96. 'period' => 'period',
  97. 'store_id' => new \Zend_Db_Expr(\Magento\Store\Model\Store::DEFAULT_STORE_ID),
  98. 'order_status' => 'order_status',
  99. 'shipping_description' => 'shipping_description',
  100. 'orders_count' => new \Zend_Db_Expr('SUM(orders_count)'),
  101. 'total_shipping' => new \Zend_Db_Expr('SUM(total_shipping)'),
  102. 'total_shipping_actual' => new \Zend_Db_Expr('SUM(total_shipping_actual)'),
  103. ];
  104. $select->from($table, $columns)->where('store_id != ?', \Magento\Store\Model\Store::DEFAULT_STORE_ID);
  105. if ($subSelect !== null) {
  106. $select->where($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
  107. }
  108. $select->group(['period', 'order_status', 'shipping_description']);
  109. $insertQuery = $select->insertFromSelect($table, array_keys($columns));
  110. $connection->query($insertQuery);
  111. } catch (\Exception $e) {
  112. $connection->rollBack();
  113. throw $e;
  114. }
  115. $connection->commit();
  116. return $this;
  117. }
  118. /**
  119. * Aggregate shipping report by shipment create_at as period
  120. *
  121. * @param string|null $from
  122. * @param string|null $to
  123. * @return $this
  124. * @throws \Exception
  125. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  126. */
  127. protected function _aggregateByShippingCreatedAt($from, $to)
  128. {
  129. $table = $this->getTable('sales_shipping_aggregated');
  130. $sourceTable = $this->getTable('sales_invoice');
  131. $orderTable = $this->getTable('sales_order');
  132. $connection = $this->getConnection();
  133. $connection->beginTransaction();
  134. try {
  135. if ($from !== null || $to !== null) {
  136. $subSelect = $this->_getTableDateRangeRelatedSelect(
  137. $sourceTable,
  138. $orderTable,
  139. ['order_id' => 'entity_id'],
  140. 'created_at',
  141. 'updated_at',
  142. $from,
  143. $to
  144. );
  145. } else {
  146. $subSelect = null;
  147. }
  148. $this->_clearTableByDateRange($table, $from, $to, $subSelect);
  149. // convert dates to current admin timezone
  150. $periodExpr = $connection->getDatePartSql(
  151. $this->getStoreTZOffsetQuery(
  152. ['source_table' => $sourceTable],
  153. 'source_table.created_at',
  154. $from,
  155. $to
  156. )
  157. );
  158. $shippingCanceled = $connection->getIfNullSql('order_table.base_shipping_canceled', 0);
  159. $shippingRefunded = $connection->getIfNullSql('order_table.base_shipping_refunded', 0);
  160. $columns = [
  161. 'period' => $periodExpr,
  162. 'store_id' => 'order_table.store_id',
  163. 'order_status' => 'order_table.status',
  164. 'shipping_description' => 'order_table.shipping_description',
  165. 'orders_count' => new \Zend_Db_Expr('COUNT(order_table.entity_id)'),
  166. 'total_shipping' => new \Zend_Db_Expr(
  167. 'SUM((order_table.base_shipping_amount - ' .
  168. "{$shippingCanceled}) * order_table.base_to_global_rate)"
  169. ),
  170. 'total_shipping_actual' => new \Zend_Db_Expr(
  171. 'SUM((order_table.base_shipping_invoiced - ' .
  172. "{$shippingRefunded}) * order_table.base_to_global_rate)"
  173. ),
  174. ];
  175. $select = $connection->select();
  176. $select->from(
  177. ['source_table' => $sourceTable],
  178. $columns
  179. )->joinInner(
  180. ['order_table' => $orderTable],
  181. $connection->quoteInto(
  182. 'source_table.order_id = order_table.entity_id AND order_table.state != ?',
  183. \Magento\Sales\Model\Order::STATE_CANCELED
  184. ),
  185. []
  186. )->useStraightJoin();
  187. $filterSubSelect = $connection->select()->from(
  188. ['filter_source_table' => $sourceTable],
  189. 'MIN(filter_source_table.entity_id)'
  190. )->where(
  191. 'filter_source_table.order_id = source_table.order_id'
  192. );
  193. if ($subSelect !== null) {
  194. $select->having($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
  195. }
  196. $select->where('source_table.entity_id = (?)', new \Zend_Db_Expr($filterSubSelect));
  197. unset($filterSubSelect);
  198. $select->group(
  199. [$periodExpr, 'order_table.store_id', 'order_table.status', 'order_table.shipping_description']
  200. );
  201. $insertQuery = $select->insertFromSelect($table, array_keys($columns));
  202. $connection->query($insertQuery);
  203. $select->reset();
  204. $columns = [
  205. 'period' => 'period',
  206. 'store_id' => new \Zend_Db_Expr(\Magento\Store\Model\Store::DEFAULT_STORE_ID),
  207. 'order_status' => 'order_status',
  208. 'shipping_description' => 'shipping_description',
  209. 'orders_count' => new \Zend_Db_Expr('SUM(orders_count)'),
  210. 'total_shipping' => new \Zend_Db_Expr('SUM(total_shipping)'),
  211. 'total_shipping_actual' => new \Zend_Db_Expr('SUM(total_shipping_actual)'),
  212. ];
  213. $select->from($table, $columns)->where('store_id != ?', \Magento\Store\Model\Store::DEFAULT_STORE_ID);
  214. if ($subSelect !== null) {
  215. $select->where($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
  216. }
  217. $select->group(['period', 'order_status', 'shipping_description']);
  218. $insertQuery = $select->insertFromSelect($table, array_keys($columns));
  219. $connection->query($insertQuery);
  220. } catch (\Exception $e) {
  221. $connection->rollBack();
  222. throw $e;
  223. }
  224. $connection->commit();
  225. return $this;
  226. }
  227. }