123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- /**
- * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
- * @author Mediotype https://www.mediotype.com/
- */
- namespace Vertex\Tax\Setup;
- use Magento\Customer\Model\Customer;
- use Magento\Eav\Api\AttributeRepositoryInterface;
- use Magento\Eav\Model\Config;
- use Magento\Framework\App\Cache\TypeListInterface;
- use Magento\Framework\Exception\LocalizedException;
- use Magento\Framework\Setup\ModuleContextInterface;
- use Magento\Framework\Setup\ModuleDataSetupInterface;
- use Magento\Framework\Setup\UpgradeDataInterface;
- /**
- * Data Upgrade Script
- *
- * @codeCoverageIgnore
- */
- class UpgradeData implements UpgradeDataInterface
- {
- /** @var AttributeRepositoryInterface */
- private $attributeRepository;
- /** @var TypeListInterface */
- private $cacheTypeList;
- /** @var Config */
- private $eavConfig;
- /**
- * @param Config $eavConfig
- * @param AttributeRepositoryInterface $attributeRepository
- * @param TypeListInterface $cacheTypeList
- */
- public function __construct(
- Config $eavConfig,
- AttributeRepositoryInterface $attributeRepository,
- TypeListInterface $cacheTypeList
- ) {
- $this->eavConfig = $eavConfig;
- $this->attributeRepository = $attributeRepository;
- $this->cacheTypeList = $cacheTypeList;
- }
- /**
- * {@inheritdoc}
- *
- * @throws LocalizedException
- */
- public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
- {
- if (version_compare($context->getVersion(), '100.1.0') < 0) {
- $this->migrateCustomAttributeToExtensionAttribute($setup);
- $this->deleteCustomAttribute();
- }
- if (version_compare($context->getVersion(), '100.2.1') < 0) {
- $this->migrateVertexCalculationSetting($setup);
- }
- }
- /**
- * Deletes the "customer_code" custom attribute, if created
- *
- * @throws \Magento\Framework\Exception\LocalizedException
- * @throws \Magento\Framework\Exception\StateException
- */
- private function deleteCustomAttribute()
- {
- $attribute = $this->getEntityAttribute(Customer::ENTITY, 'customer_code');
- if (!$attribute) {
- return;
- }
- $this->attributeRepository->delete($attribute);
- }
- /**
- * Retrieve an entity attribute
- *
- * @param string $entity
- * @param string $attributeCode
- * @return \Magento\Eav\Model\Entity\Attribute\AbstractAttribute|void
- * @throws LocalizedException
- */
- private function getEntityAttribute($entity, $attributeCode)
- {
- if (method_exists($this->eavConfig, 'getEntityAttributes')) {
- $attributes = $this->eavConfig->getEntityAttributes($entity);
- if (!isset($attributes[$attributeCode])) {
- return;
- }
- return $attributes[$attributeCode];
- }
- $attributeCodes = $this->eavConfig->getEntityAttributeCodes($entity);
- if (!in_array($attributeCode, $attributeCodes)) {
- return;
- }
- return $this->eavConfig->getAttribute($entity, $attributeCode);
- }
- /**
- * Perform migration of custom attributes to extension attributes
- *
- * @param ModuleDataSetupInterface $setup
- */
- private function migrateCustomAttributeToExtensionAttribute(ModuleDataSetupInterface $setup)
- {
- $db = $setup->getConnection();
- $attribute = $this->getEntityAttribute(Customer::ENTITY, 'customer_code');
- if (!$attribute) {
- return;
- }
- $select = $db->select()
- ->from($setup->getTable('customer_entity_varchar'), ['entity_id', 'value'])
- ->where('attribute_id = ?', $attribute->getId());
- $results = array_map(
- function ($rawResult) {
- return [
- 'customer_id' => $rawResult['entity_id'],
- 'customer_code' => $rawResult['value'],
- ];
- },
- $db->fetchAll($select)
- );
- if (!count($results)) {
- return;
- }
- $db->insertMultiple(
- $setup->getTable('vertex_customer_code'),
- $results
- );
- }
- /**
- * Remove any user settings where VERTEX was the tax calculation mode
- *
- * @param ModuleDataSetupInterface $setup
- * @return void
- */
- private function migrateVertexCalculationSetting(ModuleDataSetupInterface $setup)
- {
- $setup->getConnection()->delete(
- $setup->getTable('core_config_data'),
- [
- 'path = ?' => 'tax/calculation/algorithm',
- 'value IN (?)' => ['VERTEX_UNIT_BASE_CALCULATION', 'VERTEXSMB_UNIT_BASE_CALCULATION']
- ]
- );
- $this->cacheTypeList->invalidate('CONFIG');
- }
- }
|