-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCustomDelivery.php
More file actions
executable file
·302 lines (256 loc) · 10.3 KB
/
Copy pathCustomDelivery.php
File metadata and controls
executable file
·302 lines (256 loc) · 10.3 KB
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
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace CustomDelivery;
use CustomDelivery\Model\CustomDeliverySlice;
use CustomDelivery\Model\CustomDeliverySliceQuery;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\Connection\ConnectionInterface;
use Symfony\Component\DependencyInjection\Loader\Configurator\ServicesConfigurator;
use Thelia\Core\Translation\Translator;
use Thelia\Core\Install\Database;
use Thelia\Model\Base\TaxRuleQuery;
use Thelia\Model\Cart;
use Thelia\Model\ConfigQuery;
use Thelia\Model\Country;
use Thelia\Model\CountryAreaQuery;
use Thelia\Model\Currency;
use Thelia\Model\LangQuery;
use Thelia\Model\Map\CountryAreaTableMap;
use Thelia\Model\Message;
use Thelia\Model\MessageQuery;
use Thelia\Model\OrderPostage;
use Thelia\Model\State;
use Thelia\Module\AbstractDeliveryModuleWithState;
use Thelia\Module\BaseModule;
use Thelia\Module\DeliveryModuleInterface;
use Thelia\Module\Exception\DeliveryException;
use Thelia\Domain\Taxation\TaxEngine\Calculator;
use Thelia\Tools\I18n;
class CustomDelivery extends AbstractDeliveryModuleWithState
{
const MESSAGE_DOMAIN = "customdelivery";
const CONFIG_TRACKING_URL = 'custom_delivery_tracking_url';
const CONFIG_PICKING_METHOD = 'custom_delivery_picking_method';
const CONFIG_TAX_RULE_ID = 'custom_delivery_taxe_rule';
const DEFAULT_TRACKING_URL = '%ID%';
const DEFAULT_PICKING_METHOD = 0;
const METHOD_PRICE_WEIGHT = 0;
const METHOD_PRICE = 1;
const METHOD_WEIGHT = 2;
/** @var Translator */
protected $translator;
public static function getConfig()
{
$config = [
'url' => (
ConfigQuery::read(self::CONFIG_TRACKING_URL, self::DEFAULT_TRACKING_URL)
),
'method' => (
intval(ConfigQuery::read(self::CONFIG_PICKING_METHOD, self::DEFAULT_PICKING_METHOD))
),
'tax' => (
intval(ConfigQuery::read(self::CONFIG_TAX_RULE_ID))
)
];
return $config;
}
public function postActivation(?ConnectionInterface $con = null): void
{
if (!$this->getConfigValue('is_initialized', false)) {
$database = new Database($con);
$database->insertSql(null, array(__DIR__ . '/Config/thelia.sql'));
$this->setConfigValue('is_initialized', true);
}
// register config variables
if (null === ConfigQuery::read(self::CONFIG_TRACKING_URL, null)) {
ConfigQuery::write(self::CONFIG_TRACKING_URL, self::DEFAULT_TRACKING_URL);
}
if (null === ConfigQuery::read(self::CONFIG_PICKING_METHOD, null)) {
ConfigQuery::write(self::CONFIG_PICKING_METHOD, self::DEFAULT_PICKING_METHOD);
}
// create new message
if (null === MessageQuery::create()->findOneByName('mail_custom_delivery')) {
$message = new Message();
$message
->setName('mail_custom_delivery')
->setHtmlTemplateFileName('custom-delivery-shipping.html')
->setHtmlLayoutFileName('')
->setTextTemplateFileName('custom-delivery-shipping.txt')
->setTextLayoutFileName('')
->setSecured(0);
$languages = LangQuery::create()->find();
foreach ($languages as $language) {
$locale = $language->getLocale();
$message->setLocale($locale);
$message->setTitle(
$this->trans('Custom delivery shipping message', [], $locale)
);
$message->setSubject(
$this->trans('Your order {$order_ref} has been shipped', [], $locale)
);
}
$message->save();
}
}
/**
* This method is called by the Delivery loop, to check if the current module has to be displayed to the customer.
* Override it to implements your delivery rules/
*
* If you return true, the delivery method will de displayed to the customer
* If you return false, the delivery method will not be displayed
*
* @param Country $country the country to deliver to.
* @param State $state the state to deliver to.
*
* @return boolean
*/
public function isValidDelivery(Country $country, ?State $state = null): bool
{
// Retrieve the cart
$cart = $this->getRequest()->getSession()->getSessionCart($this->getDispatcher());
/** @var CustomDeliverySlice $slice */
$slice = $this->getSlicePostage($cart, $country, $state);
return null !== $slice;
}
/**
* Calculate and return delivery price in the shop's default currency
*
* @param Country $country the country to deliver to.
* @param State $state the state to deliver to.
*
* @return OrderPostage the delivery price
* @throws DeliveryException if the postage price cannot be calculated.
*/
public function getPostage(Country $country, ?State $state = null): OrderPostage|float
{
$cart = $this->getRequest()->getSession()->getSessionCart($this->getDispatcher());
/** @var CustomDeliverySlice $slice */
$postage = $this->getSlicePostage($cart, $country, $state);
if (null === $postage) {
throw new DeliveryException();
}
return $postage;
}
/**
*
* This method return true if your delivery manages virtual product delivery.
*
* @return bool
*/
public function handleVirtualProductDelivery(): bool
{
return false;
}
protected function trans($id, array $parameters = [], $locale = null)
{
if (null === $this->translator) {
$this->translator = Translator::getInstance();
}
return $this->translator->trans($id, $parameters, CustomDelivery::MESSAGE_DOMAIN, $locale);
}
public function getDeliveryMode()
{
return 'delivery';
}
/**
* If a state is given and has slices, use them.
* If state is given but has no slices, check if the country has slices.
* If the country has slices, use them.
* If the country has no slices, the module is not valid for delivery
*
* @param Cart $cart
* @param Country $country
* @param State $state
* @return OrderPostage|null
*/
protected function getSlicePostage(Cart $cart, Country $country, ?State $state = null)
{
$config = self::getConfig();
$currency = $cart->getCurrency();
/** @var CustomDeliverySlice $slice */
$slice = null;
if (null !== $state && null !== $areas = CountryAreaQuery::create()
->filterByStateId($state->getId())
->select([CountryAreaTableMap::COL_AREA_ID])
->find()
) {
$slice = $this->getAreaSlice($areas, $cart, $currency, $config);
}
if (null === $slice && null !== $areas = CountryAreaQuery::create()
->filterByCountryId($country->getId())
->filterByStateId(null)
->select([CountryAreaTableMap::COL_AREA_ID])
->find()
) {
$slice = $this->getAreaSlice($areas, $cart, $currency, $config);
}
if ($slice === null) {
return null;
}
return $this->getAreaPostage($slice, $currency, $country, $config);
}
/**
* @param $areas
* @param Cart $cart
* @param Currency $currency
* @param $config
* @return CustomDeliverySlice
*/
protected function getAreaSlice($areas, Cart $cart, Currency $currency, $config)
{
$query = CustomDeliverySliceQuery::create()->filterByAreaId($areas, Criteria::IN);
if ($config['method'] != CustomDelivery::METHOD_PRICE) {
$query->filterByWeightMax($cart->getWeight(), Criteria::GREATER_THAN);
$query->orderByWeightMax(Criteria::ASC);
}
if ($config['method'] != CustomDelivery::METHOD_WEIGHT) {
$total = $cart->getTotalAmount();
// convert amount to the default currency
if (0 == $currency->getByDefault()) {
$total = $total / $currency->getRate();
}
$query->filterByPriceMax($total, Criteria::GREATER_THAN);
$query->orderByPriceMax(Criteria::ASC);
}
return $query->findOne();
}
/**
* @param CustomDeliverySlice $slice
* @param Currency $currency
* @param Country $country
* @param $config
* @return OrderPostage
*/
protected function getAreaPostage(CustomDeliverySlice $slice, Currency $currency, Country $country, $config)
{
if (0 == $currency->getByDefault()) {
$untaxedPostage = $slice->getPrice() * $currency->getRate();
} else {
$untaxedPostage = $slice->getPrice();
}
$untaxedPostage = round($untaxedPostage, 2);
$locale = $this->getRequest()->getSession()->getLang()->getLocale();
return $this->buildOrderPostage($untaxedPostage, $country, $locale, $config['tax']);
}
/**
* Defines how services are loaded in your modules
*
* @param ServicesConfigurator $servicesConfigurator
*/
public static function configureServices(ServicesConfigurator $servicesConfigurator): void
{
$servicesConfigurator->load(self::getModuleCode().'\\', __DIR__)
->exclude([__DIR__.'/I18n/*', __DIR__.'/Config/**/*.php', __DIR__.'/Tests/*', __DIR__.'/CustomDelivery.php'])
->autowire(true)
->autoconfigure(true);
}
}