UpgradeData.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Setup;
  7. use Magento\Customer\Model\Customer;
  8. use Magento\Eav\Api\AttributeRepositoryInterface;
  9. use Magento\Eav\Model\Config;
  10. use Magento\Framework\App\Cache\TypeListInterface;
  11. use Magento\Framework\Exception\LocalizedException;
  12. use Magento\Framework\Setup\ModuleContextInterface;
  13. use Magento\Framework\Setup\ModuleDataSetupInterface;
  14. use Magento\Framework\Setup\UpgradeDataInterface;
  15. /**
  16. * Data Upgrade Script
  17. *
  18. * @codeCoverageIgnore
  19. */
  20. class UpgradeData implements UpgradeDataInterface
  21. {
  22. /** @var AttributeRepositoryInterface */
  23. private $attributeRepository;
  24. /** @var TypeListInterface */
  25. private $cacheTypeList;
  26. /** @var Config */
  27. private $eavConfig;
  28. /**
  29. * @param Config $eavConfig
  30. * @param AttributeRepositoryInterface $attributeRepository
  31. * @param TypeListInterface $cacheTypeList
  32. */
  33. public function __construct(
  34. Config $eavConfig,
  35. AttributeRepositoryInterface $attributeRepository,
  36. TypeListInterface $cacheTypeList
  37. ) {
  38. $this->eavConfig = $eavConfig;
  39. $this->attributeRepository = $attributeRepository;
  40. $this->cacheTypeList = $cacheTypeList;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. *
  45. * @throws LocalizedException
  46. */
  47. public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
  48. {
  49. if (version_compare($context->getVersion(), '100.1.0') < 0) {
  50. $this->migrateCustomAttributeToExtensionAttribute($setup);
  51. $this->deleteCustomAttribute();
  52. }
  53. if (version_compare($context->getVersion(), '100.2.1') < 0) {
  54. $this->migrateVertexCalculationSetting($setup);
  55. }
  56. }
  57. /**
  58. * Deletes the "customer_code" custom attribute, if created
  59. *
  60. * @throws \Magento\Framework\Exception\LocalizedException
  61. * @throws \Magento\Framework\Exception\StateException
  62. */
  63. private function deleteCustomAttribute()
  64. {
  65. $attribute = $this->getEntityAttribute(Customer::ENTITY, 'customer_code');
  66. if (!$attribute) {
  67. return;
  68. }
  69. $this->attributeRepository->delete($attribute);
  70. }
  71. /**
  72. * Retrieve an entity attribute
  73. *
  74. * @param string $entity
  75. * @param string $attributeCode
  76. * @return \Magento\Eav\Model\Entity\Attribute\AbstractAttribute|void
  77. * @throws LocalizedException
  78. */
  79. private function getEntityAttribute($entity, $attributeCode)
  80. {
  81. if (method_exists($this->eavConfig, 'getEntityAttributes')) {
  82. $attributes = $this->eavConfig->getEntityAttributes($entity);
  83. if (!isset($attributes[$attributeCode])) {
  84. return;
  85. }
  86. return $attributes[$attributeCode];
  87. }
  88. $attributeCodes = $this->eavConfig->getEntityAttributeCodes($entity);
  89. if (!in_array($attributeCode, $attributeCodes)) {
  90. return;
  91. }
  92. return $this->eavConfig->getAttribute($entity, $attributeCode);
  93. }
  94. /**
  95. * Perform migration of custom attributes to extension attributes
  96. *
  97. * @param ModuleDataSetupInterface $setup
  98. */
  99. private function migrateCustomAttributeToExtensionAttribute(ModuleDataSetupInterface $setup)
  100. {
  101. $db = $setup->getConnection();
  102. $attribute = $this->getEntityAttribute(Customer::ENTITY, 'customer_code');
  103. if (!$attribute) {
  104. return;
  105. }
  106. $select = $db->select()
  107. ->from($setup->getTable('customer_entity_varchar'), ['entity_id', 'value'])
  108. ->where('attribute_id = ?', $attribute->getId());
  109. $results = array_map(
  110. function ($rawResult) {
  111. return [
  112. 'customer_id' => $rawResult['entity_id'],
  113. 'customer_code' => $rawResult['value'],
  114. ];
  115. },
  116. $db->fetchAll($select)
  117. );
  118. if (!count($results)) {
  119. return;
  120. }
  121. $db->insertMultiple(
  122. $setup->getTable('vertex_customer_code'),
  123. $results
  124. );
  125. }
  126. /**
  127. * Remove any user settings where VERTEX was the tax calculation mode
  128. *
  129. * @param ModuleDataSetupInterface $setup
  130. * @return void
  131. */
  132. private function migrateVertexCalculationSetting(ModuleDataSetupInterface $setup)
  133. {
  134. $setup->getConnection()->delete(
  135. $setup->getTable('core_config_data'),
  136. [
  137. 'path = ?' => 'tax/calculation/algorithm',
  138. 'value IN (?)' => ['VERTEX_UNIT_BASE_CALCULATION', 'VERTEXSMB_UNIT_BASE_CALCULATION']
  139. ]
  140. );
  141. $this->cacheTypeList->invalidate('CONFIG');
  142. }
  143. }