1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Commerce\Services; |
4
|
|
|
|
5
|
|
|
use Craft\Craft; |
6
|
|
|
use Craft\Commerce_TaxCategoryModel; |
7
|
|
|
use NerdsAndCompany\Schematic\Services\Base; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Schematic Commerce Tax Categories 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 TaxCategories extends Base |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Export taxCategories. |
24
|
|
|
* |
25
|
|
|
* @param TaxCategoryModel[] $taxCategories |
26
|
|
|
* |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
public function export(array $taxCategories = []) |
30
|
|
|
{ |
31
|
|
|
if (!count($taxCategories)) { |
32
|
|
|
$taxCategories = Craft::app()->commerce_taxCategories->getAllTaxCategories(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
Craft::log(Craft::t('Exporting Commerce Tax Categories')); |
36
|
|
|
|
37
|
|
|
$taxCategoryDefinitions = []; |
38
|
|
|
|
39
|
|
|
foreach ($taxCategories as $taxCategory) { |
40
|
|
|
$taxCategoryDefinitions[$taxCategory->handle] = $this->getTaxCategoryDefinition($taxCategory); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $taxCategoryDefinitions; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get tax categories definition. |
48
|
|
|
* |
49
|
|
|
* @param Commerce_TaxCategoryModel $taxCategory |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
private function getTaxCategoryDefinition(Commerce_TaxCategoryModel $taxCategory) |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
'name' => $taxCategory->name, |
57
|
|
|
'description' => $taxCategory->description, |
58
|
|
|
'default' => $taxCategory->default, |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Attempt to import tax categories. |
64
|
|
|
* |
65
|
|
|
* @param array $taxCategoryDefinitions |
66
|
|
|
* @param bool $force If set to true tax categories not included in the import will be deleted |
67
|
|
|
* |
68
|
|
|
* @return Result |
69
|
|
|
*/ |
70
|
|
|
public function import(array $taxCategoryDefinitions, $force = false) |
71
|
|
|
{ |
72
|
|
|
Craft::log(Craft::t('Importing Commerce Tax Categories')); |
73
|
|
|
|
74
|
|
|
$this->resetCraftTaxCategoriesServiceCache(); |
75
|
|
|
$taxCategories = Craft::app()->commerce_taxCategories->getAllTaxCategories('handle'); |
76
|
|
|
|
77
|
|
|
foreach ($taxCategoryDefinitions as $taxCategoryHandle => $taxCategoryDefinition) { |
78
|
|
|
$taxCategory = array_key_exists($taxCategoryHandle, $taxCategories) |
79
|
|
|
? $taxCategories[$taxCategoryHandle] |
80
|
|
|
: new Commerce_TaxCategoryModel(); |
81
|
|
|
|
82
|
|
|
unset($taxCategories[$taxCategoryHandle]); |
83
|
|
|
|
84
|
|
|
$this->populateTaxCategory($taxCategory, $taxCategoryDefinition, $taxCategoryHandle); |
85
|
|
|
|
86
|
|
|
if (!Craft::app()->commerce_taxCategories->saveTaxCategory($taxCategory)) { // Save taxcategory via craft |
87
|
|
|
$this->addErrors($taxCategory->getAllErrors()); |
88
|
|
|
|
89
|
|
|
continue; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if ($force) { |
94
|
|
|
foreach ($taxCategories as $taxCategory) { |
95
|
|
|
Craft::app()->commerce_taxCategories->deleteTaxCategoryById($taxCategory->id); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->getResultModel(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Populate taxcategory. |
104
|
|
|
* |
105
|
|
|
* @param Commerce_TaxCategoryModel $taxCategory |
106
|
|
|
* @param array $taxCategoryDefinition |
107
|
|
|
* @param string $taxCategoryHandle |
108
|
|
|
*/ |
109
|
|
|
private function populateTaxCategory(Commerce_TaxCategoryModel $taxCategory, array $taxCategoryDefinition, $taxCategoryHandle) |
110
|
|
|
{ |
111
|
|
|
$taxCategory->setAttributes([ |
112
|
|
|
'handle' => $taxCategoryHandle, |
113
|
|
|
'name' => $taxCategoryDefinition['name'], |
114
|
|
|
'description' => $taxCategoryDefinition['description'], |
115
|
|
|
'default' => $taxCategoryDefinition['default'], |
116
|
|
|
]); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Reset service cache using reflection. |
121
|
|
|
*/ |
122
|
|
|
private function resetCraftTaxCategoriesServiceCache() |
123
|
|
|
{ |
124
|
|
|
$obj = Craft::app()->commerce_taxCategories; |
125
|
|
|
$refObject = new \ReflectionObject($obj); |
126
|
|
|
if ($refObject->hasProperty('_fetchedAllTaxCategories')) { |
127
|
|
|
$refProperty = $refObject->getProperty('_fetchedAllTaxCategories'); |
128
|
|
|
$refProperty->setAccessible(true); |
129
|
|
|
$refProperty->setValue($obj, false); |
130
|
|
|
} |
131
|
|
|
if ($refObject->hasProperty('_taxCategoriesById')) { |
132
|
|
|
$refProperty = $refObject->getProperty('_taxCategoriesById'); |
133
|
|
|
$refProperty->setAccessible(true); |
134
|
|
|
$refProperty->setValue($obj, array()); |
135
|
|
|
} |
136
|
|
|
if ($refObject->hasProperty('_taxCategoriesByHandle')) { |
137
|
|
|
$refProperty = $refObject->getProperty('_taxCategoriesByHandle'); |
138
|
|
|
$refProperty->setAccessible(true); |
139
|
|
|
$refProperty->setValue($obj, array()); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|