1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Commerce\Services; |
4
|
|
|
|
5
|
|
|
use Craft\Craft; |
6
|
|
|
use Craft\Commerce_TaxZoneModel; |
7
|
|
|
use NerdsAndCompany\Schematic\Services\Base; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Schematic Commerce Tax Zones 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 TaxZones extends Base |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Export taxZones. |
24
|
|
|
* |
25
|
|
|
* @param TaxZoneModel[] $taxZones |
26
|
|
|
* |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
public function export(array $taxZones = []) |
30
|
|
|
{ |
31
|
|
|
if (!count($taxZones)) { |
32
|
|
|
$taxZones = Craft::app()->commerce_taxZones->getAllTaxZones(true); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
Craft::log(Craft::t('Exporting Commerce Tax Zones')); |
36
|
|
|
|
37
|
|
|
$taxZoneDefinitions = []; |
38
|
|
|
|
39
|
|
|
foreach ($taxZones as $taxZone) { |
40
|
|
|
$taxZoneDefinitions[$taxZone->name] = $this->getTaxZoneDefinition($taxZone); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $taxZoneDefinitions; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get tax zones definition. |
48
|
|
|
* |
49
|
|
|
* @param Commerce_TaxZoneModel $taxZone |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
private function getTaxZoneDefinition(Commerce_TaxZoneModel $taxZone) |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
'name' => $taxZone->name, |
57
|
|
|
'description' => $taxZone->description, |
58
|
|
|
'countryBased' => $taxZone->countryBased, |
59
|
|
|
'default' => $taxZone->default, |
60
|
|
|
'countries' => $this->getCountryDefinitions($taxZone->getCountries()), |
61
|
|
|
'states' => $this->getStateDefinitions($taxZone->getStates()), |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get country definitions. |
67
|
|
|
* |
68
|
|
|
* @param Commerce_CountryModel[] $countries |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
private function getCountryDefinitions(array $countries) |
73
|
|
|
{ |
74
|
|
|
$countryDefinitions = []; |
75
|
|
|
|
76
|
|
|
foreach ($countries as $country) { |
77
|
|
|
$countryDefinitions[] = $country->iso; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $countryDefinitions; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get state definitions. |
85
|
|
|
* |
86
|
|
|
* @param Commerce_StateModel[] $states |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
private function getStateDefinitions(array $states) |
91
|
|
|
{ |
92
|
|
|
$stateDefinitions = []; |
93
|
|
|
|
94
|
|
|
foreach ($states as $state) { |
95
|
|
|
$stateDefinitions[] = $state->abbreviation; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $stateDefinitions; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Attempt to import tax zones. |
103
|
|
|
* |
104
|
|
|
* @param array $taxZoneDefinitions |
105
|
|
|
* @param bool $force If set to true tax zones not included in the import will be deleted |
106
|
|
|
* |
107
|
|
|
* @return Result |
108
|
|
|
*/ |
109
|
|
|
public function import(array $taxZoneDefinitions, $force = false) |
110
|
|
|
{ |
111
|
|
|
Craft::log(Craft::t('Importing Commerce Tax Zones')); |
112
|
|
|
|
113
|
|
|
$taxZones = array(); |
114
|
|
|
foreach (Craft::app()->commerce_taxZones->getAllTaxZones() as $taxZone) { |
115
|
|
|
$taxZones[$taxZone->name] = $taxZone; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
foreach ($taxZoneDefinitions as $taxZoneDefinition) { |
119
|
|
|
$taxZoneHandle = $taxZoneDefinition['name']; |
120
|
|
|
$taxZone = array_key_exists($taxZoneHandle, $taxZones) |
121
|
|
|
? $taxZones[$taxZoneHandle] |
122
|
|
|
: new Commerce_TaxZoneModel(); |
123
|
|
|
|
124
|
|
|
unset($taxZones[$taxZoneHandle]); |
125
|
|
|
|
126
|
|
|
$this->populateTaxZone($taxZone, $taxZoneDefinition, $taxZoneHandle); |
127
|
|
|
|
128
|
|
|
$countryIds = $this->getCountryIds($taxZone); |
129
|
|
|
$stateIds = $this->getStateIds($taxZone); |
130
|
|
|
|
131
|
|
|
if (!Craft::app()->commerce_taxZones->saveTaxZone($taxZone, $countryIds, $stateIds)) { // Save taxzone via craft |
132
|
|
|
$this->addErrors($taxZone->getAllErrors()); |
133
|
|
|
|
134
|
|
|
continue; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if ($force) { |
139
|
|
|
foreach ($taxZones as $taxZone) { |
140
|
|
|
Craft::app()->commerce_taxZones->deleteTaxZoneById($taxZone->id); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this->getResultModel(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Populate taxZone. |
149
|
|
|
* |
150
|
|
|
* @param Commerce_TaxZoneModel $taxZone |
151
|
|
|
* @param array $taxZoneDefinition |
152
|
|
|
* @param string $taxZoneHandle |
153
|
|
|
*/ |
154
|
|
|
private function populateTaxZone(Commerce_TaxZoneModel $taxZone, array $taxZoneDefinition, $taxZoneHandle) |
155
|
|
|
{ |
156
|
|
|
$taxZone->setAttributes([ |
157
|
|
|
'name' => $taxZoneHandle, |
158
|
|
|
'description' => $taxZoneDefinition['description'], |
159
|
|
|
'countryBased' => $taxZoneDefinition['countryBased'], |
160
|
|
|
'default' => $taxZoneDefinition['default'], |
161
|
|
|
]); |
162
|
|
|
|
163
|
|
|
$countries = array(); |
164
|
|
|
foreach ($taxZoneDefinition['countries'] as $iso) { |
165
|
|
|
$countries[] = Craft::app()->commerce_countries->getCountryByAttributes(array('iso' => $iso)); |
166
|
|
|
} |
167
|
|
|
$taxZone->setCountries($countries); |
168
|
|
|
|
169
|
|
|
$states = array(); |
170
|
|
|
foreach ($taxZoneDefinition['states'] as $abbreviation) { |
171
|
|
|
$states[] = Craft::app()->commerce_states->getStateByAttributes(array('abbreviation' => $abbreviation)); |
172
|
|
|
} |
173
|
|
|
$taxZone->setStates($states); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get country ids. |
178
|
|
|
* |
179
|
|
|
* @param Commerce_TaxZoneModel $taxZone |
180
|
|
|
* |
181
|
|
|
* @return array |
182
|
|
|
*/ |
183
|
|
|
private function getCountryIds(Commerce_TaxZoneModel $taxZone) |
184
|
|
|
{ |
185
|
|
|
$countryIds = array(); |
186
|
|
|
foreach ($taxZone->getCountries() as $country) { |
187
|
|
|
$countryIds[] = $country->id; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $countryIds; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get state ids. |
195
|
|
|
* |
196
|
|
|
* @param Commerce_TaxZoneModel $taxZone |
197
|
|
|
* |
198
|
|
|
* @return array |
199
|
|
|
*/ |
200
|
|
|
private function getStateIds(Commerce_TaxZoneModel $taxZone) |
201
|
|
|
{ |
202
|
|
|
$stateIds = array(); |
203
|
|
|
foreach ($taxZone->getStates() as $state) { |
204
|
|
|
$stateIds[] = $state->id; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $stateIds; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|