-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDpnOneoffCosts.php
192 lines (173 loc) · 5.77 KB
/
DpnOneoffCosts.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
<?php
namespace DpnOneoffCosts;
/**
* Copyright notice
*
* (c) Björn Fromme <fromme@dreipunktnull.com>, dreipunktnull
*
* All rights reserved
*/
use Shopware\Bundle\AttributeBundle\Service\CrudServiceInterface;
use Shopware\Bundle\AttributeBundle\Service\TypeMapping;
use Shopware\Components\Model\ModelManager;
use Shopware\Components\Plugin;
use Shopware\Components\Plugin\Context\ActivateContext;
use Shopware\Components\Plugin\Context\DeactivateContext;
use Shopware\Components\Plugin\Context\InstallContext;
use Shopware\Components\Plugin\Context\UninstallContext;
use Shopware\Components\Plugin\Context\UpdateContext;
class DpnOneoffCosts extends Plugin
{
/**
* @param InstallContext $content
*/
public function install(InstallContext $context)
{
try {
$this->createOrUpdateAttributes();
}
catch (\Exception $e) {
$context->scheduleMessage($e->getMessage());
}
/** @var ModelManager $modelManager */
$modelManager = $this->container->get('models');
$modelManager->generateAttributeModels(['s_articles_attributes']);
}
public function update(UpdateContext $context)
{
try {
$this->createOrUpdateAttributes();
}
catch (\Exception $e) {
$context->scheduleMessage($e->getMessage());
}
/** @var ModelManager $modelManager */
$modelManager = $this->container->get('models');
$modelManager->generateAttributeModels(['s_articles_attributes']);
$context->scheduleClearCache(InstallContext::CACHE_LIST_DEFAULT);
}
/**
* @param ActivateContext $context
*/
public function activate(ActivateContext $context)
{
$context->scheduleClearCache(ActivateContext::CACHE_LIST_DEFAULT);
}
/**
* @param UninstallContext $context
*/
public function uninstall(UninstallContext $context)
{
if (!$context->keepUserData()) {
$this->removeAttributes($context);
}
$context->scheduleClearCache(UninstallContext::CACHE_LIST_DEFAULT);
}
/**
* @param DeactivateContext $context
*/
public function deactivate(DeactivateContext $context)
{
$context->scheduleClearCache(InstallContext::CACHE_LIST_DEFAULT);
}
protected function createOrUpdateAttributes()
{
/** @var CrudServiceInterface $crudService */
$crudService = $this->container->get('shopware_attribute.crud_service');
$crudService->update(
's_articles_attributes',
'oneoff_costs_price',
TypeMapping::TYPE_FLOAT,
[
'displayInBackend' => true,
'position' => 300,
'custom' => false,
'translatable' => false,
'label' => 'Price',
'helpText' => 'One-off costs to be added to this article independent from amount',
]
);
$crudService->update(
's_articles_attributes',
'oneoff_costs_price_net',
TypeMapping::TYPE_BOOLEAN,
[
'displayInBackend' => true,
'position' => 301,
'custom' => false,
'translatable' => false,
'label' => 'Price net',
'helpText' => 'One-off costs price is entered net',
]
);
$crudService->update(
's_articles_attributes',
'oneoff_costs_tax',
TypeMapping::TYPE_SINGLE_SELECTION,
[
'displayInBackend' => true,
'allowBlank' => true,
'position' => 302,
'custom' => false,
'translatable' => false,
'entity' => 'Shopware\Models\Tax\Tax',
'label' => 'Tax',
'helpText' => 'Tax to be applied to one-off costs (article\'s tax rule is used by default)',
]
);
$crudService->update(
's_articles_attributes',
'oneoff_costs_label',
TypeMapping::TYPE_STRING,
[
'displayInBackend' => true,
'position' => 303,
'custom' => false,
'translatable' => true,
'label' => 'Label',
'helpText' => 'One-off costs label shown in basket',
]
);
$crudService->update(
's_articles_attributes',
'oneoff_costs_ordernum',
TypeMapping::TYPE_STRING,
[
'displayInBackend' => true,
'position' => 304,
'custom' => false,
'translatable' => false,
'label' => 'Order number',
'helpText' => 'One-off costs order number',
]
);
}
/**
* @param UninstallContext $context
*/
protected function removeAttributes(UninstallContext $context)
{
/** @var CrudServiceInterface $crudService */
$crudService = $this->container->get('shopware_attribute.crud_service');
$attributes = [
'oneoff_costs_price',
'oneoff_costs_label',
'oneoff_costs_tax',
'oneoff_costs_ordernum'
];
if ($context->assertMinimumVersion('1.2')) {
$attributes[] = 'oneoff_costs_price_net';
}
try {
foreach ($attributes as $attribute) {
$crudService->delete('s_articles_attributes', $attribute);
}
}
catch (\Exception $e) {
$context->scheduleMessage($e->getMessage());
}
/** @var ModelManager $modelManager */
$modelManager = $this->container->get('models');
$modelManager->generateAttributeModels(['s_articles_attributes']);
}
}