Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Shipping.php
Go to the documentation of this file.
1 <?php
7 
13 class Shipping extends AbstractReport
14 {
20  protected function _construct()
21  {
22  $this->_setResource('sales');
23  }
24 
32  public function aggregate($from = null, $to = null)
33  {
34  $this->_aggregateByOrderCreatedAt($from, $to);
35  $this->_aggregateByShippingCreatedAt($from, $to);
36  $this->_setFlagData(\Magento\Reports\Model\Flag::REPORT_SHIPPING_FLAG_CODE);
37  return $this;
38  }
39 
48  protected function _aggregateByOrderCreatedAt($from, $to)
49  {
50  $table = $this->getTable('sales_shipping_aggregated_order');
51  $sourceTable = $this->getTable('sales_order');
52  $connection = $this->getConnection();
53  $connection->beginTransaction();
54 
55  try {
56  if ($from !== null || $to !== null) {
57  $subSelect = $this->_getTableDateRangeSelect($sourceTable, 'created_at', 'updated_at', $from, $to);
58  } else {
59  $subSelect = null;
60  }
61 
62  $this->_clearTableByDateRange($table, $from, $to, $subSelect);
63  // convert dates to current admin timezone
64  $periodExpr = $connection->getDatePartSql(
65  $this->getStoreTZOffsetQuery($sourceTable, 'created_at', $from, $to)
66  );
67  $shippingCanceled = $connection->getIfNullSql('base_shipping_canceled', 0);
68  $shippingRefunded = $connection->getIfNullSql('base_shipping_refunded', 0);
69  $columns = [
70  'period' => $periodExpr,
71  'store_id' => 'store_id',
72  'order_status' => 'status',
73  'shipping_description' => 'shipping_description',
74  'orders_count' => new \Zend_Db_Expr('COUNT(entity_id)'),
75  'total_shipping' => new \Zend_Db_Expr(
76  "SUM((base_shipping_amount - {$shippingCanceled}) * base_to_global_rate)"
77  ),
78  'total_shipping_actual' => new \Zend_Db_Expr(
79  "SUM((base_shipping_invoiced - {$shippingRefunded}) * base_to_global_rate)"
80  ),
81  ];
82 
83  $select = $connection->select();
84  $select->from(
85  $sourceTable,
86  $columns
87  )->where(
88  'state NOT IN (?)',
90  )->where(
91  'is_virtual = 0'
92  );
93 
94  if ($subSelect !== null) {
95  $select->having($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
96  }
97 
98  $select->group([$periodExpr, 'store_id', 'status', 'shipping_description']);
99  $select->having('orders_count > 0');
100  $insertQuery = $select->insertFromSelect($table, array_keys($columns));
101  $connection->query($insertQuery);
102  $select->reset();
103 
104  $columns = [
105  'period' => 'period',
106  'store_id' => new \Zend_Db_Expr(\Magento\Store\Model\Store::DEFAULT_STORE_ID),
107  'order_status' => 'order_status',
108  'shipping_description' => 'shipping_description',
109  'orders_count' => new \Zend_Db_Expr('SUM(orders_count)'),
110  'total_shipping' => new \Zend_Db_Expr('SUM(total_shipping)'),
111  'total_shipping_actual' => new \Zend_Db_Expr('SUM(total_shipping_actual)'),
112  ];
113 
114  $select->from($table, $columns)->where('store_id != ?', \Magento\Store\Model\Store::DEFAULT_STORE_ID);
115 
116  if ($subSelect !== null) {
117  $select->where($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
118  }
119 
120  $select->group(['period', 'order_status', 'shipping_description']);
121  $insertQuery = $select->insertFromSelect($table, array_keys($columns));
122  $connection->query($insertQuery);
123  } catch (\Exception $e) {
124  $connection->rollBack();
125  throw $e;
126  }
127 
128  $connection->commit();
129  return $this;
130  }
131 
141  protected function _aggregateByShippingCreatedAt($from, $to)
142  {
143  $table = $this->getTable('sales_shipping_aggregated');
144  $sourceTable = $this->getTable('sales_invoice');
145  $orderTable = $this->getTable('sales_order');
146  $connection = $this->getConnection();
147  $connection->beginTransaction();
148 
149  try {
150  if ($from !== null || $to !== null) {
151  $subSelect = $this->_getTableDateRangeRelatedSelect(
152  $sourceTable,
153  $orderTable,
154  ['order_id' => 'entity_id'],
155  'created_at',
156  'updated_at',
157  $from,
158  $to
159  );
160  } else {
161  $subSelect = null;
162  }
163 
164  $this->_clearTableByDateRange($table, $from, $to, $subSelect);
165  // convert dates to current admin timezone
166  $periodExpr = $connection->getDatePartSql(
167  $this->getStoreTZOffsetQuery(
168  ['source_table' => $sourceTable],
169  'source_table.created_at',
170  $from,
171  $to
172  )
173  );
174  $shippingCanceled = $connection->getIfNullSql('order_table.base_shipping_canceled', 0);
175  $shippingRefunded = $connection->getIfNullSql('order_table.base_shipping_refunded', 0);
176  $columns = [
177  'period' => $periodExpr,
178  'store_id' => 'order_table.store_id',
179  'order_status' => 'order_table.status',
180  'shipping_description' => 'order_table.shipping_description',
181  'orders_count' => new \Zend_Db_Expr('COUNT(order_table.entity_id)'),
182  'total_shipping' => new \Zend_Db_Expr(
183  'SUM((order_table.base_shipping_amount - ' .
184  "{$shippingCanceled}) * order_table.base_to_global_rate)"
185  ),
186  'total_shipping_actual' => new \Zend_Db_Expr(
187  'SUM((order_table.base_shipping_invoiced - ' .
188  "{$shippingRefunded}) * order_table.base_to_global_rate)"
189  ),
190  ];
191 
192  $select = $connection->select();
193  $select->from(
194  ['source_table' => $sourceTable],
195  $columns
196  )->joinInner(
197  ['order_table' => $orderTable],
198  $connection->quoteInto(
199  'source_table.order_id = order_table.entity_id AND order_table.state != ?',
200  \Magento\Sales\Model\Order::STATE_CANCELED
201  ),
202  []
203  )->useStraightJoin();
204 
205  $filterSubSelect = $connection->select()->from(
206  ['filter_source_table' => $sourceTable],
207  'MIN(filter_source_table.entity_id)'
208  )->where(
209  'filter_source_table.order_id = source_table.order_id'
210  );
211 
212  if ($subSelect !== null) {
213  $select->having($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
214  }
215 
216  $select->where('source_table.entity_id = (?)', new \Zend_Db_Expr($filterSubSelect));
217  unset($filterSubSelect);
218 
219  $select->group(
220  [$periodExpr, 'order_table.store_id', 'order_table.status', 'order_table.shipping_description']
221  );
222 
223  $insertQuery = $select->insertFromSelect($table, array_keys($columns));
224  $connection->query($insertQuery);
225  $select->reset();
226 
227  $columns = [
228  'period' => 'period',
229  'store_id' => new \Zend_Db_Expr(\Magento\Store\Model\Store::DEFAULT_STORE_ID),
230  'order_status' => 'order_status',
231  'shipping_description' => 'shipping_description',
232  'orders_count' => new \Zend_Db_Expr('SUM(orders_count)'),
233  'total_shipping' => new \Zend_Db_Expr('SUM(total_shipping)'),
234  'total_shipping_actual' => new \Zend_Db_Expr('SUM(total_shipping_actual)'),
235  ];
236 
237  $select->from($table, $columns)->where('store_id != ?', \Magento\Store\Model\Store::DEFAULT_STORE_ID);
238 
239  if ($subSelect !== null) {
240  $select->where($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
241  }
242 
243  $select->group(['period', 'order_status', 'shipping_description']);
244  $insertQuery = $select->insertFromSelect($table, array_keys($columns));
245  $connection->query($insertQuery);
246  } catch (\Exception $e) {
247  $connection->rollBack();
248  throw $e;
249  }
250 
251  $connection->commit();
252  return $this;
253  }
254 }
_makeConditionFromDateRangeSelect($select, $periodColumn, $connection=null)
getStoreTZOffsetQuery( $table, $column, $from=null, $to=null, $store=null, $connection=null)
_clearTableByDateRange( $table, $from=null, $to=null, $subSelect=null, $doNotUseTruncate=false, $connection=null)
$columns
Definition: default.phtml:15
_getTableDateRangeSelect( $table, $column, $whereColumn, $from=null, $to=null, $additionalWhere=[], $alias='date_range_table')
$connection
Definition: bulk.php:13
$table
Definition: trigger.php:14
_getTableDateRangeRelatedSelect( $table, $relatedTable, $joinCondition, $column, $whereColumn, $from=null, $to=null, $additionalWhere=[], $alias='date_range_table', $relatedAlias='related_date_range_table')