Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SalesSetup.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Sales\Setup;
7 
9 use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;
16 
23 class SalesSetup extends EavSetup
24 {
29 
34 
39 
44 
48  protected $config;
49 
53  protected $encryptor;
54 
58  private static $connectionName = 'sales';
59 
69  public function __construct(
71  Context $context,
72  CacheInterface $cache,
73  CollectionFactory $attrGroupCollectionFactory,
75  ) {
76  $this->config = $config;
77  $this->encryptor = $context->getEncryptor();
78  parent::__construct($setup, $context, $cache, $attrGroupCollectionFactory);
79  }
80 
86  protected $_flatEntityTables = [
87  'order' => 'sales_order',
88  'order_payment' => 'sales_order_payment',
89  'order_item' => 'sales_order_item',
90  'order_address' => 'sales_order_address',
91  'order_status_history' => 'sales_order_status_history',
92  'invoice' => 'sales_invoice',
93  'invoice_item' => 'sales_invoice_item',
94  'invoice_comment' => 'sales_invoice_comment',
95  'creditmemo' => 'sales_creditmemo',
96  'creditmemo_item' => 'sales_creditmemo_item',
97  'creditmemo_comment' => 'sales_creditmemo_comment',
98  'shipment' => 'sales_shipment',
99  'shipment_item' => 'sales_shipment_item',
100  'shipment_track' => 'sales_shipment_track',
101  'shipment_comment' => 'sales_shipment_comment',
102  ];
103 
109  protected $_flatEntitiesGrid = ['order', 'invoice', 'shipment', 'creditmemo'];
110 
117  protected function _flatTableExist($table)
118  {
119  $tablesList = $this->getConnection()
120  ->listTables();
121  return in_array(
122  strtoupper($this->getTable($table)),
123  array_map('strtoupper', $tablesList)
124  );
125  }
126 
135  public function addAttribute($entityTypeId, $code, array $attr)
136  {
137  if (isset(
138  $this->_flatEntityTables[$entityTypeId]
139  ) && $this->_flatTableExist(
140  $this->_flatEntityTables[$entityTypeId]
141  )
142  ) {
143  $this->_addFlatAttribute($this->_flatEntityTables[$entityTypeId], $code, $attr);
144  $this->_addGridAttribute($this->_flatEntityTables[$entityTypeId], $code, $attr, $entityTypeId);
145  } else {
146  parent::addAttribute($entityTypeId, $code, $attr);
147  }
148  return $this;
149  }
150 
159  protected function _addFlatAttribute($table, $attribute, $attr)
160  {
161  $tableInfo = $this->getConnection()
162  ->describeTable($this->getTable($table));
163  if (isset($tableInfo[$attribute])) {
164  return $this;
165  }
166  $columnDefinition = $this->_getAttributeColumnDefinition($attribute, $attr);
167  $this->getConnection()->addColumn(
168  $this->getTable($table),
169  $attribute,
170  $columnDefinition
171  );
172  return $this;
173  }
174 
185  {
186  if (in_array($entityTypeId, $this->_flatEntitiesGrid) && !empty($attr['grid'])) {
187  $columnDefinition = $this->_getAttributeColumnDefinition($attribute, $attr);
188  $this->getConnection()->addColumn(
189  $this->getTable($table . '_grid'),
190  $attribute,
191  $columnDefinition
192  );
193  }
194  return $this;
195  }
196 
207  {
208  // Convert attribute type to column info
209  $data['type'] = isset($data['type']) ? $data['type'] : 'varchar';
210  $type = null;
211  $length = null;
212  switch ($data['type']) {
213  case 'timestamp':
215  break;
216  case 'datetime':
218  break;
219  case 'decimal':
221  $length = '12,4';
222  break;
223  case 'int':
225  break;
226  case 'text':
228  $length = 65536;
229  break;
230  case 'char':
231  case 'varchar':
233  $length = 255;
234  break;
235  }
236  if ($type !== null) {
237  $data['type'] = $type;
238  $data['length'] = $length;
239  }
240 
241  $data['nullable'] = isset($data['required']) ? !$data['required'] : true;
242  $data['comment'] = isset($data['comment']) ? $data['comment'] : ucwords(str_replace('_', ' ', $code));
243  return $data;
244  }
245 
249  public function getDefaultEntities()
250  {
251  $entities = [
252  'order' => [
253  'entity_type_id' => self::ORDER_ENTITY_TYPE_ID,
254  'entity_model' => \Magento\Sales\Model\ResourceModel\Order::class,
255  'table' => 'sales_order',
256  'increment_model' => \Magento\Eav\Model\Entity\Increment\NumericValue::class,
257  'increment_per_store' => true,
258  'attributes' => [],
259  ],
260  'invoice' => [
261  'entity_type_id' => self::INVOICE_PRODUCT_ENTITY_TYPE_ID,
262  'entity_model' => \Magento\Sales\Model\ResourceModel\Order\Invoice::class,
263  'table' => 'sales_invoice',
264  'increment_model' => \Magento\Eav\Model\Entity\Increment\NumericValue::class,
265  'increment_per_store' => true,
266  'attributes' => [],
267  ],
268  'creditmemo' => [
269  'entity_type_id' => self::CREDITMEMO_PRODUCT_ENTITY_TYPE_ID,
270  'entity_model' => \Magento\Sales\Model\ResourceModel\Order\Creditmemo::class,
271  'table' => 'sales_creditmemo',
272  'increment_model' => \Magento\Eav\Model\Entity\Increment\NumericValue::class,
273  'increment_per_store' => true,
274  'attributes' => [],
275  ],
276  'shipment' => [
277  'entity_type_id' => self::SHIPMENT_PRODUCT_ENTITY_TYPE_ID,
278  'entity_model' => \Magento\Sales\Model\ResourceModel\Order\Shipment::class,
279  'table' => 'sales_shipment',
280  'increment_model' => \Magento\Eav\Model\Entity\Increment\NumericValue::class,
281  'increment_per_store' => true,
282  'attributes' => [],
283  ],
284  ];
285  return $entities;
286  }
287 
293  public function getConfigModel()
294  {
295  return $this->config;
296  }
297 
301  public function getEncryptor()
302  {
303  return $this->encryptor;
304  }
305 
309  public function getConnection()
310  {
311  return $this->getSetup()->getConnection(self::$connectionName);
312  }
313 
320  public function getTable($table)
321  {
322  return $this->getSetup()->getTable($table, self::$connectionName);
323  }
324 
330  public function updateEntityTypes()
331  {
332  $this->updateEntityType(
333  \Magento\Sales\Model\Order::ENTITY,
334  'entity_model',
335  \Magento\Sales\Model\ResourceModel\Order::class
336  );
337  $this->updateEntityType(
338  \Magento\Sales\Model\Order::ENTITY,
339  'increment_model',
340  \Magento\Eav\Model\Entity\Increment\NumericValue::class
341  );
342  $this->updateEntityType(
343  'invoice',
344  'entity_model',
345  \Magento\Sales\Model\ResourceModel\Order::class
346  );
347  $this->updateEntityType(
348  'invoice',
349  'increment_model',
350  \Magento\Eav\Model\Entity\Increment\NumericValue::class
351  );
352  $this->updateEntityType(
353  'creditmemo',
354  'entity_model',
355  \Magento\Sales\Model\ResourceModel\Order\Creditmemo::class
356  );
357  $this->updateEntityType(
358  'creditmemo',
359  'increment_model',
360  \Magento\Eav\Model\Entity\Increment\NumericValue::class
361  );
362  $this->updateEntityType(
363  'shipment',
364  'entity_model',
365  \Magento\Sales\Model\ResourceModel\Order\Shipment::class
366  );
367  $this->updateEntityType(
368  'shipment',
369  'increment_model',
370  \Magento\Eav\Model\Entity\Increment\NumericValue::class
371  );
372  }
373 }
$attr
Definition: text.phtml:8
_addFlatAttribute($table, $attribute, $attr)
Definition: SalesSetup.php:159
addAttribute($entityTypeId, $code, array $attr)
Definition: SalesSetup.php:135
$type
Definition: item.phtml:13
__construct(ModuleDataSetupInterface $setup, Context $context, CacheInterface $cache, CollectionFactory $attrGroupCollectionFactory, ScopeConfigInterface $config)
Definition: SalesSetup.php:69
updateEntityType($code, $field, $value=null)
Definition: EavSetup.php:231
_addGridAttribute($table, $attribute, $attr, $entityTypeId)
Definition: SalesSetup.php:184
_getAttributeColumnDefinition($code, $data)
Definition: SalesSetup.php:206
$setup
Definition: trigger.php:12
$table
Definition: trigger.php:14
$code
Definition: info.phtml:12