InstallOrderStatusesAndInitialSalesConfig.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Setup\Patch\Data;
  7. use Magento\Sales\Setup\SalesSetupFactory;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\Framework\Setup\Patch\DataPatchInterface;
  10. use Magento\Framework\Setup\Patch\PatchVersionInterface;
  11. /**
  12. * Class InstallOrderStatusesAndInitialSalesConfig
  13. * @package Magento\Sales\Setup\Patch
  14. */
  15. class InstallOrderStatusesAndInitialSalesConfig implements DataPatchInterface, PatchVersionInterface
  16. {
  17. /**
  18. * @var \Magento\Framework\Setup\ModuleDataSetupInterface
  19. */
  20. private $moduleDataSetup;
  21. /**
  22. * @var SalesSetupFactory
  23. */
  24. private $salesSetupFactory;
  25. /**
  26. * InstallOrderStatusesAndInitialSalesConfig constructor.
  27. * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
  28. * @param SalesSetupFactory $salesSetupFactory
  29. */
  30. public function __construct(
  31. \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,
  32. SalesSetupFactory $salesSetupFactory
  33. ) {
  34. $this->moduleDataSetup = $moduleDataSetup;
  35. $this->salesSetupFactory = $salesSetupFactory;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  40. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  41. */
  42. public function apply()
  43. {
  44. /** @var \Magento\Sales\Setup\SalesSetup $salesSetup */
  45. $salesSetup = $this->salesSetupFactory->create(['setup' => $this->moduleDataSetup]);
  46. /**
  47. * Install eav entity types to the eav/entity_type table
  48. */
  49. $salesSetup->installEntities();
  50. /**
  51. * Install order statuses from config
  52. */
  53. $data = [];
  54. $statuses = [
  55. 'pending' => __('Pending'),
  56. 'pending_payment' => __('Pending Payment'),
  57. 'processing' => __('Processing'),
  58. 'holded' => __('On Hold'),
  59. 'complete' => __('Complete'),
  60. 'closed' => __('Closed'),
  61. 'canceled' => __('Canceled'),
  62. 'fraud' => __('Suspected Fraud'),
  63. 'payment_review' => __('Payment Review'),
  64. ];
  65. foreach ($statuses as $code => $info) {
  66. $data[] = ['status' => $code, 'label' => $info];
  67. }
  68. $this->moduleDataSetup->getConnection()->insertArray(
  69. $this->moduleDataSetup->getTable('sales_order_status'),
  70. ['status', 'label'],
  71. $data
  72. );
  73. /**
  74. * Install order states from config
  75. */
  76. $data = [];
  77. $states = [
  78. 'new' => [
  79. 'label' => __('New'),
  80. 'statuses' => ['pending' => ['default' => '1']],
  81. 'visible_on_front' => true,
  82. ],
  83. 'pending_payment' => [
  84. 'label' => __('Pending Payment'),
  85. 'statuses' => ['pending_payment' => ['default' => '1']],
  86. ],
  87. 'processing' => [
  88. 'label' => __('Processing'),
  89. 'statuses' => ['processing' => ['default' => '1'], 'fraud' => []],
  90. 'visible_on_front' => true,
  91. ],
  92. 'complete' => [
  93. 'label' => __('Complete'),
  94. 'statuses' => ['complete' => ['default' => '1']],
  95. 'visible_on_front' => true,
  96. ],
  97. 'closed' => [
  98. 'label' => __('Closed'),
  99. 'statuses' => ['closed' => ['default' => '1']],
  100. 'visible_on_front' => true,
  101. ],
  102. 'canceled' => [
  103. 'label' => __('Canceled'),
  104. 'statuses' => ['canceled' => ['default' => '1']],
  105. 'visible_on_front' => true,
  106. ],
  107. 'holded' => [
  108. 'label' => __('On Hold'),
  109. 'statuses' => ['holded' => ['default' => '1']],
  110. 'visible_on_front' => true,
  111. ],
  112. 'payment_review' => [
  113. 'label' => __('Payment Review'),
  114. 'statuses' => ['payment_review' => ['default' => '1'], 'fraud' => []],
  115. 'visible_on_front' => true,
  116. ],
  117. ];
  118. foreach ($states as $code => $info) {
  119. if (isset($info['statuses'])) {
  120. foreach ($info['statuses'] as $status => $statusInfo) {
  121. $data[] = [
  122. 'status' => $status,
  123. 'state' => $code,
  124. 'is_default' => is_array($statusInfo) && isset($statusInfo['default']) ? 1 : 0,
  125. ];
  126. }
  127. }
  128. }
  129. $this->moduleDataSetup->getConnection()->insertArray(
  130. $this->moduleDataSetup->getTable('sales_order_status_state'),
  131. ['status', 'state', 'is_default'],
  132. $data
  133. );
  134. $entitiesToAlter = ['order_address'];
  135. $attributes = [
  136. 'vat_id' => ['type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT],
  137. 'vat_is_valid' => ['type' => \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT],
  138. 'vat_request_id' => ['type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT],
  139. 'vat_request_date' => ['type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT],
  140. 'vat_request_success' => ['type' => \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT],
  141. ];
  142. foreach ($entitiesToAlter as $entityName) {
  143. foreach ($attributes as $attributeCode => $attributeParams) {
  144. $salesSetup->addAttribute($entityName, $attributeCode, $attributeParams);
  145. }
  146. }
  147. /** Update visibility for states */
  148. $states = ['new', 'processing', 'complete', 'closed', 'canceled', 'holded', 'payment_review'];
  149. foreach ($states as $state) {
  150. $this->moduleDataSetup->getConnection()->update(
  151. $this->moduleDataSetup->getTable('sales_order_status_state'),
  152. ['visible_on_front' => 1],
  153. ['state = ?' => $state]
  154. );
  155. }
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. public static function getDependencies()
  161. {
  162. return [];
  163. }
  164. /**
  165. * {@inheritdoc}
  166. */
  167. public static function getVersion()
  168. {
  169. return '2.0.0';
  170. }
  171. /**
  172. * {@inheritdoc}
  173. */
  174. public function getAliases()
  175. {
  176. return [];
  177. }
  178. }