Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Invoiced.php
Go to the documentation of this file.
1 <?php
7 
13 class Invoiced 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->_aggregateByInvoiceCreatedAt($from, $to);
36 
37  $this->_setFlagData(\Magento\Reports\Model\Flag::REPORT_INVOICE_FLAG_CODE);
38  return $this;
39  }
40 
50  protected function _aggregateByInvoiceCreatedAt($from, $to)
51  {
52  $table = $this->getTable('sales_invoiced_aggregated');
53  $sourceTable = $this->getTable('sales_invoice');
54  $orderTable = $this->getTable('sales_order');
55  $connection = $this->getConnection();
56 
57  $connection->beginTransaction();
58 
59  try {
60  if ($from !== null || $to !== null) {
61  $subSelect = $this->_getTableDateRangeRelatedSelect(
62  $sourceTable,
63  $orderTable,
64  ['order_id' => 'entity_id'],
65  'created_at',
66  'updated_at',
67  $from,
68  $to
69  );
70  } else {
71  $subSelect = null;
72  }
73 
74  $this->_clearTableByDateRange($table, $from, $to, $subSelect);
75  // convert dates to current admin timezone
76  $periodExpr = $connection->getDatePartSql(
77  $this->getStoreTZOffsetQuery(
78  ['source_table' => $sourceTable],
79  'source_table.created_at',
80  $from,
81  $to
82  )
83  );
84  $columns = [
85  'period' => $periodExpr,
86  'store_id' => 'order_table.store_id',
87  'order_status' => 'order_table.status',
88  'orders_count' => new \Zend_Db_Expr('COUNT(order_table.entity_id)'),
89  'orders_invoiced' => new \Zend_Db_Expr('COUNT(order_table.entity_id)'),
90  'invoiced' => new \Zend_Db_Expr(
91  'SUM(order_table.base_total_invoiced * order_table.base_to_global_rate)'
92  ),
93  'invoiced_captured' => new \Zend_Db_Expr(
94  'SUM(order_table.base_total_paid * order_table.base_to_global_rate)'
95  ),
96  'invoiced_not_captured' => new \Zend_Db_Expr(
97  'SUM((order_table.base_total_invoiced - order_table.base_total_paid)' .
98  ' * order_table.base_to_global_rate)'
99  ),
100  ];
101 
102  $select = $connection->select();
103  $select->from(
104  ['source_table' => $sourceTable],
105  $columns
106  )->joinInner(
107  ['order_table' => $orderTable],
108  $connection->quoteInto(
109  'source_table.order_id = order_table.entity_id AND order_table.state <> ?',
110  \Magento\Sales\Model\Order::STATE_CANCELED
111  ),
112  []
113  );
114 
115  $filterSubSelect = $connection->select();
116  $filterSubSelect->from(
117  ['filter_source_table' => $sourceTable],
118  'MAX(filter_source_table.entity_id)'
119  )->where(
120  'filter_source_table.order_id = source_table.order_id'
121  );
122 
123  if ($subSelect !== null) {
124  $select->having($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
125  }
126 
127  $select->where('source_table.entity_id = (?)', new \Zend_Db_Expr($filterSubSelect));
128  unset($filterSubSelect);
129 
130  $select->group([$periodExpr, 'order_table.store_id', 'order_table.status']);
131 
132  $select->having('orders_count > 0');
133  $insertQuery = $select->insertFromSelect($table, array_keys($columns));
134  $connection->query($insertQuery);
135  $select->reset();
136 
137  $columns = [
138  'period' => 'period',
139  'store_id' => new \Zend_Db_Expr(\Magento\Store\Model\Store::DEFAULT_STORE_ID),
140  'order_status' => 'order_status',
141  'orders_count' => new \Zend_Db_Expr('SUM(orders_count)'),
142  'orders_invoiced' => new \Zend_Db_Expr('SUM(orders_invoiced)'),
143  'invoiced' => new \Zend_Db_Expr('SUM(invoiced)'),
144  'invoiced_captured' => new \Zend_Db_Expr('SUM(invoiced_captured)'),
145  'invoiced_not_captured' => new \Zend_Db_Expr('SUM(invoiced_not_captured)'),
146  ];
147 
148  $select->from($table, $columns)->where('store_id <> ?', \Magento\Store\Model\Store::DEFAULT_STORE_ID);
149 
150  if ($subSelect !== null) {
151  $select->where($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
152  }
153 
154  $select->group(['period', 'order_status']);
155  $insertQuery = $select->insertFromSelect($table, array_keys($columns));
156  $connection->query($insertQuery);
157  $connection->commit();
158  } catch (\Exception $e) {
159  $connection->rollBack();
160  throw $e;
161  }
162 
163  return $this;
164  }
165 
173  protected function _aggregateByOrderCreatedAt($from, $to)
174  {
175  $table = $this->getTable('sales_invoiced_aggregated_order');
176  $sourceTable = $this->getTable('sales_order');
177  $connection = $this->getConnection();
178 
179  if ($from !== null || $to !== null) {
180  $subSelect = $this->_getTableDateRangeSelect($sourceTable, 'created_at', 'updated_at', $from, $to);
181  } else {
182  $subSelect = null;
183  }
184 
185  $this->_clearTableByDateRange($table, $from, $to, $subSelect);
186  // convert dates to current admin timezone
187  $periodExpr = $connection->getDatePartSql($this->getStoreTZOffsetQuery($sourceTable, 'created_at', $from, $to));
188 
189  $columns = [
190  'period' => $periodExpr,
191  'store_id' => 'store_id',
192  'order_status' => 'status',
193  'orders_count' => new \Zend_Db_Expr('COUNT(base_total_invoiced)'),
194  'orders_invoiced' => new \Zend_Db_Expr(
195  sprintf('SUM(%s)', $connection->getCheckSql('base_total_invoiced > 0', 1, 0))
196  ),
197  'invoiced' => new \Zend_Db_Expr(
198  sprintf(
199  'SUM(%s * %s)',
200  $connection->getIfNullSql('base_total_invoiced', 0),
201  $connection->getIfNullSql('base_to_global_rate', 0)
202  )
203  ),
204  'invoiced_captured' => new \Zend_Db_Expr(
205  sprintf(
206  'SUM(%s * %s)',
207  $connection->getIfNullSql('base_total_paid', 0),
208  $connection->getIfNullSql('base_to_global_rate', 0)
209  )
210  ),
211  'invoiced_not_captured' => new \Zend_Db_Expr(
212  sprintf(
213  'SUM((%s - %s) * %s)',
214  $connection->getIfNullSql('base_total_invoiced', 0),
215  $connection->getIfNullSql('base_total_paid', 0),
216  $connection->getIfNullSql('base_to_global_rate', 0)
217  )
218  ),
219  ];
220 
221  $select = $connection->select();
222  $select->from($sourceTable, $columns)->where('state <> ?', \Magento\Sales\Model\Order::STATE_CANCELED);
223 
224  if ($subSelect !== null) {
225  $select->having($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
226  }
227 
228  $select->group([$periodExpr, 'store_id', 'status']);
229  $select->having('orders_count > 0');
230 
231  $insertQuery = $select->insertFromSelect($table, array_keys($columns));
232  $connection->query($insertQuery);
233  $select->reset();
234 
235  $columns = [
236  'period' => 'period',
237  'store_id' => new \Zend_Db_Expr(\Magento\Store\Model\Store::DEFAULT_STORE_ID),
238  'order_status' => 'order_status',
239  'orders_count' => new \Zend_Db_Expr('SUM(orders_count)'),
240  'orders_invoiced' => new \Zend_Db_Expr('SUM(orders_invoiced)'),
241  'invoiced' => new \Zend_Db_Expr('SUM(invoiced)'),
242  'invoiced_captured' => new \Zend_Db_Expr('SUM(invoiced_captured)'),
243  'invoiced_not_captured' => new \Zend_Db_Expr('SUM(invoiced_not_captured)'),
244  ];
245 
246  $select->from($table, $columns)->where('store_id <> ?', \Magento\Store\Model\Store::DEFAULT_STORE_ID);
247 
248  if ($subSelect !== null) {
249  $select->where($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
250  }
251 
252  $select->group(['period', 'order_status']);
253  $insertQuery = $select->insertFromSelect($table, array_keys($columns));
254  $connection->query($insertQuery);
255  return $this;
256  }
257 }
_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')