-
Notifications
You must be signed in to change notification settings - Fork 0
/
AvataxTaxAdjusterPlugin.php
457 lines (408 loc) · 17 KB
/
AvataxTaxAdjusterPlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<?php
/**
* AvaTax Tax Adjuster plugin for Craft Commerce
*
* Calculate tax using Avalara's Avatax
*
*
* @author Rob Knecht
* @author Mike Kroll
* @copyright Copyright (c) 2017 Surprise Highway
* @link https://github.com/surprisehighway
* @package AvataxTaxAdjuster
* @since 0.0.1
*/
namespace Craft;
require('adjusters/AvataxTaxAdjuster.php');
class AvataxTaxAdjusterPlugin extends BasePlugin
{
/**
* Called after the plugin class is instantiated; do any one-time initialization here such as hooks and events:
*
* craft()->on('entries.saveEntry', function(Event $event) {
* // ...
* });
*
* or loading any third party Composer packages via:
*
* require_once __DIR__ . '/vendor/autoload.php';
*
* @return mixed
*/
public function init()
{
parent::init();
require __DIR__.'/vendor/autoload.php';
craft()->on('commerce_orders.onBeforeOrderComplete', [$this, 'onBeforeOrderComplete']);
craft()->on('commerce_addresses.onBeforeSaveAddress', [$this, 'onBeforeSaveAddress']);
craft()->on('commerce_payments.onRefundTransaction', [$this, 'onRefundTransaction']);
}
/**
* Raised before a cart is completed and becomes an order.
* Create a sales invoice in avatax.
*/
public function onBeforeOrderComplete(Event $event)
{
/** @var Commerce_OrderModel $order */
$order = $event->params['order'];
craft()->avataxTaxAdjuster_salesTax->createSalesInvoice($order);
}
/**
* Raised before address has been saved.
* Validate an address in avatax.
*/
public function onBeforeSaveAddress(Event $event)
{
/** @var Commerce_AddressModel $address */
$address = $event->params['address'];
craft()->avataxTaxAdjuster_salesTax->validateAddress($address);
}
/**
* Raised after a transaction was attempted to be refunded.
* Void a transaction in.
*/
public function onRefundTransaction(Event $event)
{
/** @var Commerce_TransactionModel $transaction */
$transaction = $event->params['transaction'];
if($transaction->status == 'success')
{
craft()->avataxTaxAdjuster_salesTax->refundTransaction($transaction->order);
}
}
/**
* Returns the user-facing name.
*
* @return mixed
*/
public function getName()
{
return Craft::t('AvaTax Tax Adjuster');
}
/**
* Plugins can have descriptions of themselves displayed on the Plugins page by adding a getDescription() method
* on the primary plugin class:
*
* @return mixed
*/
public function getDescription()
{
return Craft::t('Calculates tax rates with Avalara AvaTax');
}
/**
* Plugins can have links to their documentation on the Plugins page by adding a getDocumentationUrl() method on
* the primary plugin class:
*
* @return string
*/
public function getDocumentationUrl()
{
return 'https://github.com/surprisehighway/craft-avataxtaxadjuster/blob/master/README.md';
}
/**
* Plugins can now take part in Craft’s update notifications, and display release notes on the Updates page, by
* providing a JSON feed that describes new releases, and adding a getReleaseFeedUrl() method on the primary
* plugin class.
*
* @return string
*/
public function getReleaseFeedUrl()
{
return 'https://raw.githubusercontent.com/surprisehighway/craft-avataxtaxadjuster/master/releases.json';
}
/**
* Returns the version number.
*
* @return string
*/
public function getVersion()
{
return '1.0.8';
}
/**
* As of Craft 2.5, Craft no longer takes the whole site down every time a plugin’s version number changes, in
* case there are any new migrations that need to be run. Instead plugins must explicitly tell Craft that they
* have new migrations by returning a new (higher) schema version number with a getSchemaVersion() method on
* their primary plugin class:
*
* @return string
*/
public function getSchemaVersion()
{
return '1.0.0';
}
/**
* Returns the developer’s name.
*
* @return string
*/
public function getDeveloper()
{
return 'Surprise Highway';
}
/**
* Returns the developer’s website URL.
*
* @return string
*/
public function getDeveloperUrl()
{
return 'https://github.com/surprisehighway';
}
/**
* Returns whether the plugin should get its own tab in the CP header.
*
* @return bool
*/
public function hasCpSection()
{
return false;
}
/**
* Called right before your plugin’s row gets stored in the plugins database table, and tables have been created
* for it based on its records.
*/
public function onBeforeInstall()
{
}
/**
* Called right after your plugin’s row has been stored in the plugins database table, and tables have been
* created for it based on its records.
*/
public function onAfterInstall()
{
// Create an "avatax" tax category
$avataxTaxCategory = craft()->commerce_taxCategories->getTaxCategoryByHandle('avatax');
if(!$avataxTaxCategory)
{
$newTaxCategoryModel = Commerce_TaxCategoryModel::populateModel( array(
'name' => 'Avatax',
'handle' => 'avatax',
'description' => 'Calculate tax rates using Avalara AvaTax',
'default' => FALSE,
) );
/** @param Commerce_TaxCategoryModel $model **/
if ( craft()->commerce_taxCategories->saveTaxCategory($newTaxCategoryModel) )
{
Craft::log('Avatax tax category created successfully.');
}
else
{
Craft::log('Could not save the Avatax tax category.', LogLevel::Warning);
}
}
// Create an "avatax field group"
$avataxFieldGroupModel = new FieldGroupModel();
$avataxFieldGroupModel->name = 'Avatax';
if( craft()->fields->saveGroup($avataxFieldGroupModel) )
{
Craft::log('Avatax field group created successfully.', LogLevel::Info);
// Create avataxTaxCode field
$avataxTaxCodeModel = new FieldModel();
$avataxTaxCodeModel->groupId = $avataxFieldGroupModel->id;
$avataxTaxCodeModel->name = 'AvaTax Tax Code';
$avataxTaxCodeModel->handle = 'avataxTaxCode';
$avataxTaxCodeModel->translatable = true;
$avataxTaxCodeModel->type = 'PlainText';
$avataxTaxCodeModel->instructions = 'Specify an [Avalara Tax Code](https://taxcode.avatax.avalara.com) to use for this product.';
$avataxTaxCodeModel->settings = array(
'placeholder' => '',
'multiline' => '',
'initialRows' => '4',
'maxLength' => ''
);
if (craft()->fields->saveField($avataxTaxCodeModel))
{
Craft::log('Avatax Tax Code field created successfully.');
}
else
{
Craft::log('Could not save the Avatax Tax Code field.', LogLevel::Warning);
}
// Create avataxCustomerUsageType field
$avataxCustomerUsageTypeModel = new FieldModel();
$avataxCustomerUsageTypeModel->groupId = $avataxFieldGroupModel->id;
$avataxCustomerUsageTypeModel->name = 'AvaTax Customer Usage Type';
$avataxCustomerUsageTypeModel->handle = 'avataxCustomerUsageType';
$avataxCustomerUsageTypeModel->translatable = true;
$avataxCustomerUsageTypeModel->type = 'Dropdown';
$avataxCustomerUsageTypeModel->instructions = 'Select an [Entity/Use Code](https://help.avalara.com/000_Avalara_AvaTax/Exemption_Reason_Matrices_for_US_and_Canada) to exempt this customer from tax.';
$avataxCustomerUsageTypeModel->settings = array(
'options' => array(
array('label' => '', 'value' => '', 'default' => ''),
array('label' => 'A. Federal government (United States)', 'value' => 'A', 'default' => ''),
array('label' => 'B. State government (United States)', 'value' => 'B', 'default' => ''),
array('label' => 'C. Tribe / Status Indian / Indian Band (both)', 'value' => 'C', 'default' => ''),
array('label' => 'D. Foreign diplomat (both)', 'value' => 'D', 'default' => ''),
array('label' => 'E. Charitable or benevolent org (both)', 'value' => 'E', 'default' => ''),
array('label' => 'F. Religious or educational org (both)', 'value' => 'F', 'default' => ''),
array('label' => 'G. Resale (both)', 'value' => 'G', 'default' => ''),
array('label' => 'H. Commercial agricultural production (both)', 'value' => 'H', 'default' => ''),
array('label' => 'I. Industrial production / manufacturer (both)', 'value' => 'I', 'default' => ''),
array('label' => 'J. Direct pay permit (United States)', 'value' => 'J', 'default' => ''),
array('label' => 'K. Direct mail (United States)', 'value' => 'K', 'default' => ''),
array('label' => 'L. Other (both)', 'value' => 'L', 'default' => ''),
array('label' => 'M. Not Used', 'value' => 'M', 'default' => ''),
array('label' => 'N. Local government (United States)', 'value' => 'N', 'default' => ''),
array('label' => 'O. Not Used', 'value' => 'O', 'default' => ''),
array('label' => 'P. Commercial aquaculture (Canada)', 'value' => 'P', 'default' => ''),
array('label' => 'Q. Commercial Fishery (Canada)', 'value' => 'Q', 'default' => ''),
array('label' => 'R. Non-resident (Canada)', 'value' => 'R', 'default' => ''),
)
);
if (craft()->fields->saveField($avataxCustomerUsageTypeModel))
{
Craft::log('Avatax Customer Usage Type field created successfully.');
}
else
{
Craft::log('Could not save the Avatax Customer Usage Type field.', LogLevel::Warning);
}
}
else
{
Craft::log('Could not save the Avatax field group. ', LogLevel::Warning);
}
craft()->request->redirect('/admin/avataxtaxadjuster/settings');
}
/**
* Called right before your plugin’s record-based tables have been deleted, and its row in the plugins table
* has been deleted.
*/
public function onBeforeUninstall()
{
// Note: We can't remove the tax category as some prducts may only have the Avatax Category selected.
// Remove the avatax tax category
// $avataxTaxCategory = craft()->commerce_taxCategories->getTaxCategoryByHandle('avatax');
// if($avataxTaxCategory)
// {
// /** @param Commerce_TaxCategoryModel $model **/
// if ( craft()->commerce_taxCategories->deleteTaxCategoryById($avataxTaxCategory->id) )
// {
// Craft::log('Avatax tax category deleted successfully.');
// }
// else
// {
// Craft::log('Could not delete the Avatax tax category.', LogLevel::Warning);
// }
// }
}
/**
* Called right after your plugin’s record-based tables have been deleted, and its row in the plugins table
* has been deleted.
*/
public function onAfterUninstall()
{
}
/**
* Defines the attributes that model your plugin’s available settings.
*
* @return array
*/
protected function defineSettings()
{
return array(
'environment' => array( AttributeType::String, 'label' => 'Environment', 'default' => 'sandbox', 'required' => true),
'accountId' => array( AttributeType::String, 'label' => 'Account ID', 'default' => '', 'required' => true),
'licenseKey' => array( AttributeType::String, 'label' => 'License Key', 'default' => '', 'required' => true),
'companyCode' => array( AttributeType::String, 'label' => 'Company Code', 'default' => '', 'required' => false),
'sandboxAccountId' => array( AttributeType::String, 'label' => 'Account ID', 'default' => '', 'required' => false),
'sandboxLicenseKey' => array( AttributeType::String, 'label' => 'License Key', 'default' => '', 'required' => false),
'sandboxCompanyCode' => array( AttributeType::String, 'label' => 'Company Code', 'default' => '', 'required' => false),
'shipFromName' => array( AttributeType::String, 'label' => 'Name', 'default' => '', 'required' => true),
'shipFromStreet1' => array( AttributeType::String, 'label' => 'Street 1', 'default' => '', 'required' => true),
'shipFromStreet2' => array( AttributeType::String, 'label' => 'Street 1', 'default' => '', 'required' => false),
'shipFromStreet3' => array( AttributeType::String, 'label' => 'Street 3', 'default' => '', 'required' => false),
'shipFromCity' => array( AttributeType::String, 'label' => 'City', 'default' => '', 'required' => true),
'shipFromState' => array( AttributeType::String, 'label' => 'State/Province', 'default' => '', 'required' => true),
'shipFromZipCode' => array( AttributeType::String, 'label' => 'Postal Code', 'default' => '', 'required' => true),
'shipFromCountry' => array( AttributeType::String, 'label' => 'Country', 'default' => '', 'required' => true),
'enableTaxCalculation' => array( AttributeType::Bool, 'label' => 'Enable Tax Calculation', 'default' => true, 'required' => false),
'enableCommitting' => array( AttributeType::Bool, 'label' => 'Enable Document Committing', 'default' => true, 'required' => false),
'enableAddressValidation' => array( AttributeType::Bool, 'label' => 'Enable Address Validation', 'default' => true, 'required' => false),
'defaultTaxCode' => array( AttributeType::String, 'label' => 'Default Tax Code', 'default' => 'P0000000', 'required' => true),
'defaultShippingCode' => array( AttributeType::String, 'label' => 'Default Shipping Code', 'default' => 'FR', 'required' => true),
'debug' => array( AttributeType::Bool, 'label' => 'Debug', 'default' => false, 'required' => false),
);
}
/**
* Returns a URL to your plugin’s settings.
*
* @return string
*/
public function getSettingsUrl()
{
return 'avataxtaxadjuster/settings';
}
/**
* If you need to do any processing on your settings’ post data before they’re saved to the database, you can
* do it with the prepSettings() method:
*
* @param mixed $settings The Widget's settings
*
* @return mixed
*/
public function prepSettings($settings)
{
// Modify $settings here...
return $settings;
}
/**
* @param array|BaseModel $values
*/
public function setSettings($values)
{
if (!$values)
{
$values = array();
}
if (is_array($values))
{
// Merge in any values that are stored in craft/config/avataxtaxadjuster.php
foreach ($this->getSettings() as $key => $value)
{
if(substr($key, 0, 8) === 'shipFrom')
{
$shipFrom = craft()->config->get('shipFrom', 'avataxtaxadjuster');
$shipFromKey = lcfirst(str_replace('shipFrom', '', $key));
if(!empty($shipFrom[ $shipFromKey]))
{
$values[$key] = $shipFrom[$shipFromKey];
}
}
else
{
$configValue = craft()->config->get($key, 'avataxtaxadjuster');
if ($configValue !== null)
{
$values[$key] = $configValue;
}
}
}
}
parent::setSettings($values);
}
/**
* Define custom CP routes.
*
* @return array
*/
public function registerCpRoutes()
{
return array(
'avataxtaxadjuster/settings' => array('action' => 'avataxTaxAdjuster/settings'),
'avataxtaxadjuster/logs' => array('action' => 'avataxTaxAdjuster_Utilities/logs')
);
}
/**
* Register our tax adjuster.
* https://craftcommerce.com/docs/adjusters#ordering-adjustments
*
* @return array
*/
public function commerce_registerOrderAdjusters(){
return [
601 => new \Commerce\Adjusters\AvataxTaxAdjuster
];
}
}