From 53591b8b6c3e18027aa72851f1532c1fd4f431af Mon Sep 17 00:00:00 2001 From: Thiago Contardi Date: Fri, 29 Dec 2023 17:19:31 -0300 Subject: [PATCH] feat: added address attributes for store that uses address attributes in a different order --- Client/ShipmentOrder/Customer.php | 26 ++++++++++++++-------- Helper/Data.php | 36 +++++++++++++++++++++++++++++++ Model/Config/Source/Street.php | 32 +++++++++++++++++++++++++++ composer.json | 2 +- etc/adminhtml/system/general.xml | 30 +++++++++++++++++++++++++- etc/config.xml | 4 ++++ etc/module.xml | 2 +- i18n/en_US.csv | 5 +++++ i18n/pt_BR.csv | 7 +++++- 9 files changed, 131 insertions(+), 13 deletions(-) create mode 100644 Model/Config/Source/Street.php diff --git a/Client/ShipmentOrder/Customer.php b/Client/ShipmentOrder/Customer.php index 09f9d57..ddff368 100644 --- a/Client/ShipmentOrder/Customer.php +++ b/Client/ShipmentOrder/Customer.php @@ -8,6 +8,7 @@ namespace Intelipost\Shipping\Client\ShipmentOrder; +use Intelipost\Shipping\Helper\Data; use Magento\Sales\Model\ResourceModel\Order\Address\CollectionFactory; class Customer @@ -15,12 +16,18 @@ class Customer /** @var CollectionFactory */ protected $addressCollectionFactory; - public function __construct(CollectionFactory $addressCollectionFactory) - { + /** @var Data */ + protected $helperData; + + public function __construct( + CollectionFactory $addressCollectionFactory, + Data $helperData + ) { $this->addressCollectionFactory = $addressCollectionFactory; + $this->helperData = $helperData; } - public function getInformation($orderIncrementId, $taxVat) + public function getInformation($orderIncrementId, $taxVat): \stdClass { $collection = $this->getAddressCollection($orderIncrementId); @@ -33,10 +40,11 @@ public function getInformation($orderIncrementId, $taxVat) $customer->phone = $addressModel->getData('telephone'); $customer->is_company = false; $customer->federal_tax_payer_id = $taxVat; - $customer->shipping_address = $addressModel->getStreetLine(1); - $customer->shipping_number = $addressModel->getStreetLine(2) ?: $this->getAddressNumber($addressModel); - $customer->shipping_additional = $addressModel->getStreetLine(3); - $customer->shipping_quarter = $addressModel->getStreetLine(4); + $customer->shipping_address = $addressModel->getStreetLine($this->helperData->getStreetAttribute()); + $customer->shipping_number = $addressModel->getStreetLine($this->helperData->getNumberAttribute()) + ?: $this->getAddressNumber($addressModel); + $customer->shipping_additional = $addressModel->getStreetLine($this->helperData->getComplementAttribute()); + $customer->shipping_quarter = $addressModel->getStreetLine($this->helperData->getDistrictAttribute()); $customer->shipping_city = $addressModel->getData('city'); $customer->shipping_state = $addressModel->getData('region'); $customer->shipping_zip_code = $addressModel->getData('postcode'); @@ -61,9 +69,9 @@ public function getAddressCollection($orderIncrementId) * @param \Magento\Sales\Model\Order\Address $addressModel * @return string */ - public function getAddressNumber($addressModel) + public function getAddressNumber($addressModel): string { - $number = trim((string) $addressModel->getStreetLine(1)); + $number = trim((string) $addressModel->getStreetLine($this->helperData->getStreetAttribute())); $shippingNumber = "s/n"; if ($number) { $number = explode(',', $number); diff --git a/Helper/Data.php b/Helper/Data.php index abb8655..d69102a 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -154,6 +154,42 @@ public function __construct( parent::__construct($context); } + public function getStreetAttribute(): int + { + $streetAttribute = (int) $this->getConfig('street_attribute'); + if (!$streetAttribute) { + $streetAttribute = 1; + } + return $streetAttribute; + } + + public function getNumberAttribute(): int + { + $numberAttribute = (int) $this->getConfig('number_attribute'); + if (!$numberAttribute) { + $numberAttribute = 2; + } + return $numberAttribute; + } + + public function getComplementAttribute(): int + { + $complementAttribute = (int) $this->getConfig('complement_attribute'); + if (!$complementAttribute) { + $complementAttribute = 3; + } + return $complementAttribute; + } + + public function getDistrictAttribute(): int + { + $districtAttribute = (int) $this->getConfig('district_attribute'); + if (!$districtAttribute) { + $districtAttribute = 4; + } + return $districtAttribute; + } + /** * @return string[] */ diff --git a/Model/Config/Source/Street.php b/Model/Config/Source/Street.php new file mode 100644 index 0000000..7677451 --- /dev/null +++ b/Model/Config/Source/Street.php @@ -0,0 +1,32 @@ + __('Street 1'), 'value' => '1'], + ['label' => __('Street 2'), 'value' => '2'], + ['label' => __('Street 3'), 'value' => '3'], + ['label' => __('Street 4'), 'value' => '4'] + ]; + } + return self::$options; + } +} diff --git a/composer.json b/composer.json index 4b29fe5..be0fa2b 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "intelipost/magento2", "description": "Intelipost Shipping", "type": "magento2-module", - "version": "2.6.2", + "version": "2.7.0", "require": { "guzzlehttp/guzzle": ">=6.3.3" }, diff --git a/etc/adminhtml/system/general.xml b/etc/adminhtml/system/general.xml index 8d4bc8f..ee308bf 100644 --- a/etc/adminhtml/system/general.xml +++ b/etc/adminhtml/system/general.xml @@ -15,7 +15,7 @@ Magento\Config\Model\Config\Source\Yesno carriers/intelipost/active - + Key for API intelipost_basic/settings/api_key @@ -48,6 +48,34 @@ Quotes older than 3 months will be deleted automattically If enabled, it can use a huge amount of disk space because it'll save all quotes in a database table, it's only recommended for testing purposes + + + Magento\Config\Block\System\Config\Form\Fieldset + + + + Intelipost\Shipping\Model\Config\Source\Street + carriers/intelipost/street_attribute + + + + + Intelipost\Shipping\Model\Config\Source\Street + carriers/intelipost/number_attribute + + + + + Intelipost\Shipping\Model\Config\Source\Street + carriers/intelipost/complement_attribute + + + + + Intelipost\Shipping\Model\Config\Source\Street + carriers/intelipost/district_attribute + + diff --git a/etc/config.xml b/etc/config.xml index a22893d..08784f1 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -37,6 +37,10 @@ Nesses casos, é necessário monitorar a entrega e o processo de pedido para verificar a unidade de entrega mais próxima para retirada. Você receberá a notificação de quando sua encomenda estará disponível para retirada. + 1 + 2 + 3 + 4 height Frete Grátis Entrega Agendada diff --git a/etc/module.xml b/etc/module.xml index 426e992..243807b 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -7,7 +7,7 @@ */ --> - + diff --git a/i18n/en_US.csv b/i18n/en_US.csv index f6fe80b..b1b5c50 100644 --- a/i18n/en_US.csv +++ b/i18n/en_US.csv @@ -180,3 +180,8 @@ Description,Description "Each 15 minutes","Each 15 minutes" "Hourly","Hourly" "Each 2 hours","Each 2 hours" +"Address Attributes","Address Attributes" +"Street","Street" +"Number","Number" +"Complement","Complement" +"District","District" diff --git a/i18n/pt_BR.csv b/i18n/pt_BR.csv index bf1a29e..0dd865d 100644 --- a/i18n/pt_BR.csv +++ b/i18n/pt_BR.csv @@ -77,7 +77,7 @@ Title,Título "Default Height","Altura Padrão" "Width Attribute","Atributo de Largura" "Default Width","Largura Padrão" -"Length Attribute","Comprimento Padrão" +"Length Attribute","Atributo de Comprimento" "Default Length","Comprimento Padrão" "Free Shipping Conditions","Condições de Frete Grátis" "Free Shipping Method","Método para Frete Grátis" @@ -180,3 +180,8 @@ Description,Descrição "Each 15 minutes","De 15 em 15 minutos" "Hourly","De hora em hora" "Each 2 hours","De 2 em 2 horas" +"Address Attributes","Atributos de Endereço" +"Street","Rua" +"Number","Número" +"Complement","Complemento" +"District","Bairro"