Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Original namespace #12

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"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.7",
"keywords": [
"craft",
"cms",
Expand Down
63 changes: 25 additions & 38 deletions src/Avatax.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Avatax plugin for Craft CMS 3.x
*
Expand Down Expand Up @@ -107,36 +108,36 @@ function (PluginEvent $event) {

// Register the commerce order tax adjuster
Event::on(
OrderAdjustments::class,
OrderAdjustments::EVENT_REGISTER_ORDER_ADJUSTERS,
function(RegisterComponentTypesEvent $event) {
OrderAdjustments::class,
OrderAdjustments::EVENT_REGISTER_ORDER_ADJUSTERS,
function (RegisterComponentTypesEvent $event) {
$event->types[] = AvataxTaxAdjuster::class;
}
);

// Register order complete listener
Event::on(
Order::class,
Order::EVENT_BEFORE_COMPLETE_ORDER,
function(Event $event) {
Order::class,
Order::EVENT_BEFORE_COMPLETE_ORDER,
function (Event $event) {
$this->onBeforeOrderComplete($event);
}
);

// Register address save event listener
Event::on(
Addresses::class,
Addresses::EVENT_BEFORE_SAVE_ADDRESS,
function(AddressEvent $event) {
Addresses::class,
Addresses::EVENT_BEFORE_SAVE_ADDRESS,
function (AddressEvent $event) {
$this->onBeforeSaveAddress($event);
}
);

// Register order refund listener
Event::on(
Payments::class,
Payments::EVENT_AFTER_REFUND_TRANSACTION,
function(RefundTransactionEvent $event) {
Payments::class,
Payments::EVENT_AFTER_REFUND_TRANSACTION,
function (RefundTransactionEvent $event) {
$this->onRefundTransaction($event);
}
);
Expand Down Expand Up @@ -173,7 +174,7 @@ public function onBeforeSaveAddress(AddressEvent $event)
// @var AddressEvent $address
$address = $event->address;

if(Craft::$app->getRequest()->getIsSiteRequest()) {
if (Craft::$app->getRequest()->getIsSiteRequest()) {
$this->SalesTaxService->validateAddress($address);
}
}
Expand All @@ -190,8 +191,7 @@ public function onRefundTransaction(RefundTransactionEvent $event)
// @var Transaction $transaction
$transaction = $event->transaction;

if($transaction->status == 'success')
{
if ($transaction->status == 'success') {
$this->SalesTaxService->handleRefund($amount, $transaction);
}
}
Expand All @@ -207,21 +207,17 @@ public function onAfterInstall()
// Create an "avatax" tax category
$category = $commerce->TaxCategories->getTaxCategoryByHandle('avatax');

if(!$category)
{
if (!$category) {
$model = new TaxCategory();

$model->name = 'Avatax';
$model->handle = 'avatax';
$model->description = 'Calculate tax rates using Avalara AvaTax';
$model->default = FALSE;

if ( $commerce->TaxCategories->saveTaxCategory($model) )
{
if ($commerce->TaxCategories->saveTaxCategory($model)) {
Craft::info('Avatax tax category created successfully.', 'avatax');
}
else
{
} else {
Craft::warning('Could not save the Avatax tax category.', 'avatax');
}
}
Expand All @@ -230,8 +226,7 @@ public function onAfterInstall()
$group = new FieldGroup();
$group->name = 'Avatax';

if( Craft::$app->fields->saveGroup($group) )
{
if (Craft::$app->fields->saveGroup($group)) {
Craft::info('Avatax field group created successfully.', 'avatax');

// Create avataxTaxCode field
Expand All @@ -249,12 +244,9 @@ public function onAfterInstall()
]
]);

if (Craft::$app->fields->saveField($field))
{
if (Craft::$app->fields->saveField($field)) {
Craft::info('Avatax Tax Code field created successfully.', 'avatax');
}
else
{
} else {
Craft::warning('Could not save the Avatax Tax Code field.', 'avatax');
}

Expand Down Expand Up @@ -290,17 +282,12 @@ public function onAfterInstall()
]
]);

if (Craft::$app->fields->saveField($field))
{
if (Craft::$app->fields->saveField($field)) {
Craft::info('Avatax Customer Usage Type field created successfully.', 'Avatax');
}
else
{
} else {
Craft::warning('Could not save the Avatax Customer Usage Type field.', 'Avatax');
}
}
else
{
} else {
Craft::warning('Could not save the Avatax field group. ', 'Avatax');
}

Expand Down
19 changes: 8 additions & 11 deletions src/adjusters/AvataxTaxAdjuster.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php

namespace surprisehighway\avatax\adjusters;

Expand All @@ -12,29 +12,26 @@

class AvataxTaxAdjuster extends Component implements AdjusterInterface
{

public function adjust(Order $order): array
{
$adjustments = [];

if($order->shippingAddress !== NULL && sizeof($order->getLineItems()) > 0)
{
$taxService = new SalesTaxService;
if ($order->shippingAddress !== null && sizeof($order->getLineItems()) > 0) {
$taxService = new SalesTaxService();

$salesTax = $taxService->createSalesOrder($order);

$adjustment = new OrderAdjustment;
$adjustment = new OrderAdjustment();

$adjustment->type = 'tax';
$adjustment->name = 'Sales Tax';
$adjustment->description = 'Adds $'.$salesTax.' of tax to the order';
$adjustment->sourceSnapshot = [ 'avatax' => $salesTax];
$adjustment->description = 'Adds $' . $salesTax . ' of tax to the order';
$adjustment->sourceSnapshot = ['avatax' => $salesTax];
$adjustment->amount = +$salesTax;
$adjustment->setOrder($order);
$adjustments[] = $adjustment;
}

return $adjustments;
}

}
}
3 changes: 2 additions & 1 deletion src/assetbundles/avatax/AvataxAsset.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Avatax plugin for Craft CMS 3.x
*
Expand All @@ -8,7 +9,7 @@
* @copyright Copyright (c) 2019 Surprise Highway
*/

namespace surprisehighway\avatax\assetbundles\Avatax;
namespace surprisehighway\avatax\assetbundles\avatax;

use Craft;
use craft\web\AssetBundle;
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/BaseController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Avatax plugin for Craft CMS 3.x
*
Expand Down Expand Up @@ -47,5 +48,4 @@ public function actionSettings()
'settings' => $settings,
]);
}

}
13 changes: 5 additions & 8 deletions src/controllers/JsonController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Avatax plugin for Craft CMS 3.x
*
Expand Down Expand Up @@ -71,8 +72,7 @@ public function actionValidateAddress()
'stateValue'
];

foreach ($attributes as $attr)
{
foreach ($attributes as $attr) {
$address->$attr = Craft::$app->getRequest()->getParam($attr);
}

Expand All @@ -82,21 +82,18 @@ public function actionValidateAddress()

$response = $taxService->getValidateAddress($address);

if(!empty($response->validatedAddresses) || isset($response->coordinates))
{
if (!empty($response->validatedAddresses) || isset($response->coordinates)) {
return $this->asJson([
'success' => true,
'response' => $response
]);
}
else
{
} else {
return $this->asJson([
'success' => false,
'error' => Craft::t('avatax', 'Invalid Address.'),
'response' => $response,
]);
}
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/controllers/UtilityController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Avatax plugin for Craft CMS 3.x
*
Expand Down
Loading