Skip to content

Commit

Permalink
Merge branch 'release/2.0.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
imagehat committed Sep 6, 2019
2 parents 252d18e + e9e7edf commit 5c3e58e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).


## 2.0.5 - 2019-09-06
### Added
- Added support for overriding the Customer Code sent to Avalara based on the value of a User or Order field ([more](https://github.com/surprisehighway/craft-avatax#customer-code))

## 2.0.4 - 2019-09-06
### Fixed
- Fixed deprecation error (pull request [#8](https://github.com/surprisehighway/craft-avatax/pull/8))
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ Craft Commerce supports refunds for completed transactions if the [payment gatew

As of Commerce 2.0 partial refunds can be initiated multiple times from the admin control panel. Triggering a full refund for the exact amount of the original order will issue a new Return Invoice for full amount of the corresponding AvaTax transaction. Be aware that triggering a partial refund will issue a new Return Invoice for the partial amount to the corresponding AvaTax *customer* but is not tied to the original transaction. This is because AvaTax only issues refunds on a transaction in full, for specific line items, or as a percentage.

## Customer Code

By default the plugin will send Avalara the order email address as the customer code. With this "hidden" feature you can optionally override this behavior to use the value saved in a User or Order field with a specific handle. This field is **not**created automatically when you install the plugin, you must manually create it.

#### To set up the field:

1. Create either a User or Order field named "Avatax Customer Number", with the handle `avataxCustomerCode`. The handle must match exactly and is case sensitive.

Note that it is up to you how you save or validate the field value. The plugin will simply use the field value if available, or default to the order email address if the field is empty or does not exist.


## Config Overrides

Expand Down
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": "surprisehighway/craft-avatax",
"description": "Calculate and add sales tax to an order's base tax using Avalara's Avatax service.",
"type": "craft-plugin",
"version": "2.0.4",
"version": "2.0.5",
"keywords": [
"craft",
"cms",
Expand Down
51 changes: 44 additions & 7 deletions src/services/SalesTaxService.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,9 @@ public function refundPartialTransaction($amount, Transaction $transaction)
// add entity/use code if set for the customer
if(!is_null($order->customer->user))
{
if(isset($order->customer->user->avataxCustomerUsageType)
&& !empty($order->customer->user->avataxCustomerUsageType->value))
if($this->getFieldValue('avataxCustomerUsageType', $order->customer->user))
{
$tb = $tb->withEntityUseCode($order->customer->user->avataxCustomerUsageType->value);
$tb = $tb->withEntityUseCode($this->getFieldValue('avataxCustomerUsageType', $order->customer->user));
}
}

Expand Down Expand Up @@ -409,7 +408,46 @@ private function getCompanyCode()
*/
private function getCustomerCode($order)
{
return (!empty($order->email)) ? $order->email : 'GUEST';

$customerCode = (!empty($order->email)) ? $order->email : 'GUEST';

// Override value from a logged-in User field if available
if(!is_null($order->customer->user))
{
if($this->getFieldValue('avataxCustomerCode', $order->customer->user))
{
$customerCode = $this->getFieldValue('avataxCustomerCode', $order->customer->user);
}
}

// Override value from an order field if available
if($this->getFieldValue('avataxCustomerCode', $order))
{
$customerCode = $this->getFieldValue('avataxCustomerCode', $order);
}

return $customerCode;
}

/**
* @return string|null
*
* Check for override value in a plaintext or dropdown field.
*/
private function getFieldValue($handle, $element)
{
if($element->fieldValues !== null && array_key_exists($handle, $element->fieldValues))
{
$field = $element->fieldValues[$handle];
$value = isset($field->value) ? $field->value : $field;

if(is_string($value) && !empty($value))
{
return $value;
}
}

return null;
}

/**
Expand Down Expand Up @@ -592,10 +630,9 @@ private function getTotalTax($order, $transaction)
// add entity/use code if set for a logged-in User
if(!is_null($order->customer->user))
{
if(isset($order->customer->user->avataxCustomerUsageType)
&& !empty($order->customer->user->avataxCustomerUsageType->value))
if($this->getFieldValue('avataxCustomerUsageType', $order->customer->user))
{
$t = $t->withEntityUseCode($order->customer->user->avataxCustomerUsageType->value);
$t = $t->withEntityUseCode($this->getFieldValue('avataxCustomerUsageType', $order->customer->user));
}
}

Expand Down

0 comments on commit 5c3e58e

Please sign in to comment.