Skip to content

Commit

Permalink
Merge pull request #30 from contardi/master
Browse files Browse the repository at this point in the history
feat: added address attributes
  • Loading branch information
nicolasmafraintelipost authored Jan 5, 2024
2 parents 13b11c6 + 53591b8 commit 3477650
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 13 deletions.
26 changes: 17 additions & 9 deletions Client/ShipmentOrder/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,26 @@

namespace Intelipost\Shipping\Client\ShipmentOrder;

use Intelipost\Shipping\Helper\Data;
use Magento\Sales\Model\ResourceModel\Order\Address\CollectionFactory;

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);

Expand All @@ -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');
Expand All @@ -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);
Expand Down
36 changes: 36 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
*/
Expand Down
32 changes: 32 additions & 0 deletions Model/Config/Source/Street.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Intelipost\Shipping\Model\Config\Source;

class Street implements \Magento\Framework\Data\OptionSourceInterface
{
/**
* @var array
*/
protected static $options;

/**
* @return array
*/
public function toOptionArray()
{
if (!self::$options) {
self::$options = [
['label' => __('Street 1'), 'value' => '1'],
['label' => __('Street 2'), 'value' => '2'],
['label' => __('Street 3'), 'value' => '3'],
['label' => __('Street 4'), 'value' => '4']
];
}
return self::$options;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
30 changes: 29 additions & 1 deletion etc/adminhtml/system/general.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>carriers/intelipost/active</config_path>
</field>
<field id="api_key" translate="label comment" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="api_key" translate="label comment" type="password" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>API Key</label>
<comment>Key for API</comment>
<config_path>intelipost_basic/settings/api_key</config_path>
Expand Down Expand Up @@ -48,6 +48,34 @@
<tooltip>Quotes older than 3 months will be deleted automattically</tooltip>
<comment>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</comment>
</field>
<group id="intelipost_address" translate="label" sortOrder="1000" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Address Attributes</label>
<frontend_model>Magento\Config\Block\System\Config\Form\Fieldset</frontend_model>

<field id="street" translate="label comment" type="select" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Street</label>
<source_model>Intelipost\Shipping\Model\Config\Source\Street</source_model>
<config_path>carriers/intelipost/street_attribute</config_path>
</field>

<field id="number" translate="label comment" type="select" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Number</label>
<source_model>Intelipost\Shipping\Model\Config\Source\Street</source_model>
<config_path>carriers/intelipost/number_attribute</config_path>
</field>

<field id="complement" translate="label" type="select" sortOrder="200" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Complement</label>
<source_model>Intelipost\Shipping\Model\Config\Source\Street</source_model>
<config_path>carriers/intelipost/complement_attribute</config_path>
</field>

<field id="district" translate="label" type="select" sortOrder="210" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>District</label>
<source_model>Intelipost\Shipping\Model\Config\Source\Street</source_model>
<config_path>carriers/intelipost/district_attribute</config_path>
</field>
</group>
</group>
</include>

4 changes: 4 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
</riskareamsg>
<street_attribute>1</street_attribute>
<number_attribute>2</number_attribute>
<complement_attribute>3</complement_attribute>
<district_attribute>4</district_attribute>
<height_attribute>height</height_attribute>
<freeshipping_text>Frete Grátis</freeshipping_text>
<scheduled_title>Entrega Agendada</scheduled_title>
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
-->
<config>
<module name="Intelipost_Shipping" setup_version="2.6.2">
<module name="Intelipost_Shipping" setup_version="2.7.0">
<sequence>
<module name="Magento_Quote"/>
</sequence>
Expand Down
5 changes: 5 additions & 0 deletions i18n/en_US.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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"
7 changes: 6 additions & 1 deletion i18n/pt_BR.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"

0 comments on commit 3477650

Please sign in to comment.