Skip to content
This repository has been archived by the owner on Apr 22, 2019. It is now read-only.

Commit

Permalink
Merge branch 'release/1.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Sisco committed Dec 29, 2017
2 parents 1e7e6cf + 2a5c31b commit 892eafb
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 1.3.0 (2017-12-29)

* Add the ability to use any custom customer attribute as customer code [#99](https://github.com/classyllama/ClassyLlama_AvaTax/issues/99)

### 1.2.7 (2017-12-14)

* Refactor code to remove abstract class for conditionally loading new class in parent constructor
Expand Down
2 changes: 1 addition & 1 deletion Framework/AppInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ interface AppInterface
/**
* If this is updated it must also be updated in composer.json
*/
const APP_VERSION = '1.2.7';
const APP_VERSION = '1.3.0';
}
25 changes: 23 additions & 2 deletions Framework/Interaction/Tax.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,16 @@ public function getTaxService(
*/
protected function getCustomerCode($data)
{
switch ($this->config->getCustomerCodeFormat($data->getStoreId())) {
// Retrieve the customer code configuration value
$customerCode = $this->config->getCustomerCodeFormat($data->getStoreId());
switch ($customerCode) {
case Config::CUSTOMER_FORMAT_OPTION_EMAIL:
// Use email address
$email = $data->getCustomerEmail();
return $email ?: Config::CUSTOMER_MISSING_EMAIL;
break;
case Config::CUSTOMER_FORMAT_OPTION_NAME_ID:
// Use name and ID
$customer = $this->getCustomerById($data->getCustomerId());
if ($customer && $customer->getId()) {
$name = $customer->getFirstname() . ' ' . $customer->getLastname();
Expand All @@ -349,9 +353,26 @@ protected function getCustomerCode($data)
return sprintf(Config::CUSTOMER_FORMAT_NAME_ID, $name, $id);
break;
case Config::CUSTOMER_FORMAT_OPTION_ID:
default:
// Use customer ID
return $data->getCustomerId() ?: strtolower(Config::CUSTOMER_GUEST_ID) . '-' . $data->getId();
break;
default:
// Use custom customer attribute
if (!$data->getCustomerId()) {
// This is a guest so no attribute value exists and neither does a customer ID
return strtolower(Config::CUSTOMER_GUEST_ID) . '-' . $data->getId();
}
// Retrieve customer by ID
$customer = $this->getCustomerById($data->getCustomerId());
// Retrieve attribute using provided attribute code
$attribute = $customer->getCustomAttribute($customerCode);
if (!is_null($attribute)) {
// Customer has value defined for provided attribute code
return $attribute->getValue();
}
// No value set for provided attribute code, but this is not a guest so use customer ID
return $data->getCustomerId();
break;
}
}

Expand Down
47 changes: 43 additions & 4 deletions Model/Config/Source/CustomerCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,57 @@
namespace ClassyLlama\AvaTax\Model\Config\Source;

use ClassyLlama\AvaTax\Helper\Config;
use Magento\Customer\Model\Customer;

class CustomerCode implements \Magento\Framework\Option\ArrayInterface
{
/**
* @var Customer
*/
protected $customer;

/**
* CustomerCode constructor.
*
* @param Customer $customer
*/
public function __construct(Customer $customer)
{
$this->customer = $customer;
}

/**
* @return array
*/
public function toOptionArray()
{
return [
['value' => Config::CUSTOMER_FORMAT_OPTION_ID, 'label' => __('ID')],
['value' => Config::CUSTOMER_FORMAT_OPTION_EMAIL, 'label' => __('Email')],
['value' => Config::CUSTOMER_FORMAT_OPTION_NAME_ID, 'label' => __('Name (ID)')],
// Define attributes array with default config values to include
$attributesArray[Config::CUSTOMER_FORMAT_OPTION_ID] = [
'value' => Config::CUSTOMER_FORMAT_OPTION_ID, 'label' => __('ID')
];
$attributesArray[Config::CUSTOMER_FORMAT_OPTION_EMAIL] = [
'value' => Config::CUSTOMER_FORMAT_OPTION_EMAIL, 'label' => __('Email')
];
$attributesArray[Config::CUSTOMER_FORMAT_OPTION_NAME_ID] = [
'value' => Config::CUSTOMER_FORMAT_OPTION_NAME_ID, 'label' => __('Name (ID)')
];

// Retrieve all customer attributes
$customerAttributes = $this->customer->getAttributes();
foreach ($customerAttributes as $attribute){
$label = $attribute->getDefaultFrontendLabel();
if (!is_null($label) && $attribute->getIsUserDefined()) {
// Only add custom attribute codes that have a frontend label value defined
$attributesArray[$attribute->getAttributeCode()] = [
'value' => $attribute->getAttributeCode(),
'label' => __($label)
];
}
}

// Sort alphabetically for ease of use
sort($attributesArray);

return $attributesArray;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "classyllama/module-avatax",
"type": "magento2-module",
"version": "1.2.7",
"version": "1.3.0",
"license": "OSL-3.0",
"require": {
"magento/framework": "^100.1.0|101.0.*",
Expand Down
2 changes: 1 addition & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
<field id="customer_code_format" translate="label comment" type="select" sortOrder="3010" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Customer Code Format</label>
<source_model>ClassyLlama\AvaTax\Model\Config\Source\CustomerCode</source_model>
<comment>Select a format for customers to be identified in AvaTax. Once the integration is live, this setting should not be changed.</comment>
<comment><![CDATA[Select a format for customers to be identified in AvaTax. Available options will include any <em>custom</em> customer attributes that have been created in Magento. If the customer doesn't have a value defined for the selected attribute when their transaction is processed and sent to Avalara, their customer (or guest) ID will be used instead. Once the integration is live, this setting should not be changed.]]></comment>
<depends>
<field id="enabled">1</field>
</depends>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<queue_failed_lifetime>180</queue_failed_lifetime>
<queue_admin_notification_enabled>1</queue_admin_notification_enabled>
<queue_failure_notification_enabled>1</queue_failure_notification_enabled>
<customer_code_format>id</customer_code_format>
</avatax>
</tax>
</default>
Expand Down

0 comments on commit 892eafb

Please sign in to comment.