1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Model\ResourceModel\Order;
use Magento\Framework\App\ResourceConnection;
use Psr\Log\LoggerInterface as LogWriter;
use Magento\Framework\Exception\LocalizedException;
use Magento\SalesSequence\Model\Manager;
use \Magento\Sales\Model\ResourceModel\EntityAbstract;
use \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot;
/**
* Order status resource model
*
* @author Magento Core Team <core@magentocommerce.com>
*/
class Status extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
/**
* Status labels table
*
* @var string
*/
protected $labelsTable;
/**
* Status state table
*
* @var string
*/
protected $stateTable;
/**
* Internal constructor
*
* @return void
*/
protected function _construct()
{
$this->_init('sales_order_status', 'status');
$this->_isPkAutoIncrement = false;
$this->labelsTable = $this->getTable('sales_order_status_label');
$this->stateTable = $this->getTable('sales_order_status_state');
}
/**
* Retrieve select object for load object data
*
* @param string $field
* @param mixed $value
* @param \Magento\Framework\Model\AbstractModel $object
* @return \Magento\Framework\DB\Select
*/
protected function _getLoadSelect($field, $value, $object)
{
if ($field == 'default_state') {
$select = $this->getConnection()->select()->from(
$this->getMainTable(),
['label']
)->join(
['state_table' => $this->stateTable],
$this->getMainTable() . '.status = state_table.status',
'status'
)->where(
'state_table.state = ?',
$value
)->order(
'state_table.is_default DESC'
)->limit(
1
);
} else {
$select = parent::_getLoadSelect($field, $value, $object);
}
return $select;
}
/**
* Store labels getter
*
* @param \Magento\Sales\Model\Order\Status $status
* @return array
*/
public function getStoreLabels(\Magento\Sales\Model\Order\Status $status)
{
$select = $this->getConnection()->select()
->from(['ssl' => $this->labelsTable], [])
->where('status = ?', $status->getStatus())
->columns([
'store_id',
'label',
]);
return $this->getConnection()->fetchPairs($select);
}
/**
* Save status labels per store
*
* @param \Magento\Framework\Model\AbstractModel $object
* @return $this
*/
protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
{
if ($object->hasStoreLabels()) {
$labels = $object->getStoreLabels();
$this->getConnection()->delete($this->labelsTable, ['status = ?' => $object->getStatus()]);
$data = [];
foreach ($labels as $storeId => $label) {
if (empty($label)) {
continue;
}
$data[] = ['status' => $object->getStatus(), 'store_id' => $storeId, 'label' => $label];
}
if (!empty($data)) {
$this->getConnection()->insertMultiple($this->labelsTable, $data);
}
}
return parent::_afterSave($object);
}
/**
* Assign order status to order state
*
* @param string $status
* @param string $state
* @param bool $isDefault
* @param bool $visibleOnFront
* @return $this
*/
public function assignState($status, $state, $isDefault, $visibleOnFront = false)
{
if ($isDefault) {
$this->getConnection()->update(
$this->stateTable,
['is_default' => 0],
['state = ?' => $state]
);
}
$this->getConnection()->insertOnDuplicate(
$this->stateTable,
[
'status' => $status,
'state' => $state,
'is_default' => (int)$isDefault,
'visible_on_front' => (int)$visibleOnFront
]
);
return $this;
}
/**
* Unassign order status from order state
*
* @param string $status
* @param string $state
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function unassignState($status, $state)
{
$this->getConnection()->beginTransaction();
try {
$isStateDefault = $this->checkIsStateDefault($state, $status);
$this->getConnection()->delete(
$this->stateTable,
[
'state = ?' => $state,
'status = ?' => $status
]
);
if ($isStateDefault) {
$newDefaultStatus = $this->getStatusByState($state);
if ($newDefaultStatus) {
$this->getConnection()->update(
$this->stateTable,
['is_default' => 1],
[
'state = ?' => $state,
'status = ?' => $newDefaultStatus
]
);
}
}
$this->getConnection()->commit();
} catch (\Exception $e) {
$this->getConnection()->rollBack();
throw new LocalizedException(__('The status needs to remain assigned to its state.'));
}
return $this;
}
/**
* Check is this state last
*
* @param string $state
* @return bool
*/
public function checkIsStateLast($state)
{
return (1 == $this->getConnection()->fetchOne(
$this->getConnection()->select()
->from(['sss' => $this->stateTable], [])
->where('state = ?', $state)
->columns([new\Zend_Db_Expr('COUNT(1)')])
));
}
/**
* Check is this status used in orders
*
* @param string $status
* @return bool
*/
public function checkIsStatusUsed($status)
{
return (bool)$this->getConnection()->fetchOne(
$this->getConnection()->select()
->from(['sfo' => $this->getTable('sales_order')], [])
->where('status = ?', $status)
->limit(1)
->columns([new \Zend_Db_Expr(1)])
);
}
/**
* Check is this pair of state and status default
*
* @param string $state
* @param string $status
* @return bool
*/
protected function checkIsStateDefault($state, $status)
{
return (bool)$this->getConnection()->fetchOne(
$this->getConnection()->select()
->from(['sss' => $this->stateTable], [])
->where('state = ?', $state)
->where('status = ?', $status)
->limit(1)
->columns(['is_default'])
);
}
/**
* Returns any possible status for state
*
* @param string $state
* @return string
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
protected function getStatusByState($state)
{
return (string)$this->getConnection()->fetchOne(
$select = $this->getConnection()->select()
->from(['sss' => $this->stateTable, []])
->where('state = ?', $state)
->limit(1)
->columns(['status'])
);
}
}