|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Commerce\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Craft\Craft; |
|
6
|
|
|
use Craft\Commerce_TaxRateModel; |
|
7
|
|
|
use NerdsAndCompany\Schematic\Services\Base; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Schematic Commerce Tax Rates Service. |
|
11
|
|
|
* |
|
12
|
|
|
* Sync Craft Setups. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Nerds & Company |
|
15
|
|
|
* @copyright Copyright (c) 2015-2017, Nerds & Company |
|
16
|
|
|
* @license MIT |
|
17
|
|
|
* |
|
18
|
|
|
* @see http://www.nerds.company |
|
19
|
|
|
*/ |
|
20
|
|
|
class TaxRates extends Base |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Export taxRates. |
|
24
|
|
|
* |
|
25
|
|
|
* @param TaxRateModel[] $taxRates |
|
26
|
|
|
* |
|
27
|
|
|
* @return array |
|
28
|
|
|
*/ |
|
29
|
|
|
public function export(array $taxRates = []) |
|
30
|
|
|
{ |
|
31
|
|
|
if (!count($taxRates)) { |
|
32
|
|
|
$taxRates = Craft::app()->commerce_taxRates->getAllTaxRates(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
Craft::log(Craft::t('Exporting Commerce Tax Rates')); |
|
36
|
|
|
|
|
37
|
|
|
$taxRateDefinitions = []; |
|
38
|
|
|
|
|
39
|
|
|
foreach ($taxRates as $taxRate) { |
|
40
|
|
|
$taxRateDefinitions[$taxRate->name] = $this->getTaxRateDefinition($taxRate); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $taxRateDefinitions; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get tax rates definition. |
|
48
|
|
|
* |
|
49
|
|
|
* @param Commerce_TaxRateModel $taxRate |
|
50
|
|
|
* |
|
51
|
|
|
* @return array |
|
52
|
|
|
*/ |
|
53
|
|
|
private function getTaxRateDefinition(Commerce_TaxRateModel $taxRate) |
|
54
|
|
|
{ |
|
55
|
|
|
return [ |
|
56
|
|
|
'name' => $taxRate->name, |
|
57
|
|
|
'rate' => $taxRate->rate, |
|
58
|
|
|
'include' => $taxRate->include, |
|
59
|
|
|
'isVat' => $taxRate->isVat, |
|
60
|
|
|
'taxable' => $taxRate->taxable, |
|
61
|
|
|
'taxCategory' => $taxRate->getTaxCategory()->handle, |
|
62
|
|
|
'taxZone' => $taxRate->getTaxZone()->name, |
|
63
|
|
|
]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Attempt to import tax rates. |
|
68
|
|
|
* |
|
69
|
|
|
* @param array $taxRateDefinitions |
|
70
|
|
|
* @param bool $force If set to true tax rates not included in the import will be deleted |
|
71
|
|
|
* |
|
72
|
|
|
* @return Result |
|
73
|
|
|
*/ |
|
74
|
|
|
public function import(array $taxRateDefinitions, $force = false) |
|
75
|
|
|
{ |
|
76
|
|
|
Craft::log(Craft::t('Importing Commerce Tax Rates')); |
|
77
|
|
|
|
|
78
|
|
|
$taxRates = array(); |
|
79
|
|
|
foreach (Craft::app()->commerce_taxRates->getAllTaxRates() as $taxRate) { |
|
80
|
|
|
$taxRates[$taxRate->name] = $taxRate; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
foreach ($taxRateDefinitions as $taxRateDefinition) { |
|
84
|
|
|
$taxRateHandle = $taxRateDefinition['name']; |
|
85
|
|
|
$taxRate = array_key_exists($taxRateHandle, $taxRates) |
|
86
|
|
|
? $taxRates[$taxRateHandle] |
|
87
|
|
|
: new Commerce_TaxRateModel(); |
|
88
|
|
|
|
|
89
|
|
|
unset($taxRates[$taxRateHandle]); |
|
90
|
|
|
|
|
91
|
|
|
$this->populateTaxRate($taxRate, $taxRateDefinition, $taxRateHandle); |
|
92
|
|
|
|
|
93
|
|
|
if (!Craft::app()->commerce_taxRates->saveTaxRate($taxRate)) { // Save taxrate via craft |
|
94
|
|
|
$this->addErrors($taxRate->getAllErrors()); |
|
95
|
|
|
|
|
96
|
|
|
continue; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if ($force) { |
|
101
|
|
|
foreach ($taxRates as $taxRate) { |
|
102
|
|
|
Craft::app()->commerce_taxRates->deleteTaxRateById($taxRate->id); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $this->getResultModel(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Populate taxRate. |
|
111
|
|
|
* |
|
112
|
|
|
* @param Commerce_TaxRateModel $taxRate |
|
113
|
|
|
* @param array $taxRateDefinition |
|
114
|
|
|
* @param string $taxRateHandle |
|
115
|
|
|
*/ |
|
116
|
|
|
private function populateTaxRate(Commerce_TaxRateModel $taxRate, array $taxRateDefinition, $taxRateHandle) |
|
117
|
|
|
{ |
|
118
|
|
|
$taxCategory = Craft::app()->commerce_taxCategories->getTaxCategoryByHandle($taxRateDefinition['taxCategory']); |
|
119
|
|
|
$taxZone = null; |
|
120
|
|
|
foreach (Craft::app()->commerce_taxZones->getAllTaxZones() as $zone) { |
|
121
|
|
|
if ($zone->name == $taxRateDefinition['taxZone']) { |
|
122
|
|
|
$taxZone = $zone->id; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$taxRate->setAttributes([ |
|
127
|
|
|
'name' => $taxRateHandle, |
|
128
|
|
|
'rate' => $taxRateDefinition['rate'], |
|
129
|
|
|
'include' => $taxRateDefinition['include'], |
|
130
|
|
|
'isVat' => $taxRateDefinition['isVat'], |
|
131
|
|
|
'taxable' => $taxRateDefinition['taxable'], |
|
132
|
|
|
'taxCategoryId' => $taxCategory->id, |
|
133
|
|
|
'taxZoneId' => $taxZone, |
|
134
|
|
|
]); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|