-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathChronopostPickupPoint.php
518 lines (433 loc) · 18.7 KB
/
ChronopostPickupPoint.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
<?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 ChronopostPickupPoint;
use ChronopostPickupPoint\Config\ChronopostPickupPointConst;
use ChronopostPickupPoint\Model\ChronopostPickupPointAreaFreeshippingQuery;
use ChronopostPickupPoint\Model\ChronopostPickupPointDeliveryMode;
use ChronopostPickupPoint\Model\ChronopostPickupPointDeliveryModeQuery;
use ChronopostPickupPoint\Model\ChronopostPickupPointPriceQuery;
use PDO;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Exception\PropelException;
use Propel\Runtime\Propel;
use Symfony\Component\DependencyInjection\Loader\Configurator\ServicesConfigurator;
use Symfony\Component\Finder\Finder;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Core\HttpFoundation\Session\Session;
use Thelia\Install\Database;
use Thelia\Model\Country;
use Thelia\Model\CountryArea;
use Thelia\Model\Lang;
use Thelia\Model\LangQuery;
use Thelia\Model\Message;
use Thelia\Model\MessageQuery;
use Thelia\Model\OrderPostage;
use Thelia\Model\State;
use Thelia\Module\AbstractDeliveryModuleWithState;
use Thelia\Module\Exception\DeliveryException;
class ChronopostPickupPoint extends AbstractDeliveryModuleWithState
{
/** @var string */
const DOMAIN_NAME = 'chronopostpickuppoint';
const CHRONOPOST_CONFIRMATION_MESSAGE_NAME = 'chronopost_pickup_point_confirmation_message_name';
const CHRONOPOST_TAX_RULE_ID = 'chronopost_pickup_point_tax_rule_id';
/**
* @param ConnectionInterface|null $con
* @throws PropelException
*/
public function postActivation(ConnectionInterface $con = null): void
{
try {
/** Security to not erase user configuration on reactivation */
ChronopostPickupPointDeliveryModeQuery::create()->findOne();
ChronopostPickupPointAreaFreeshippingQuery::create()->findOne();
} catch (\Exception $e) {
$database = new Database($con->getWrappedConnection());
$database->insertSql(null, array(__DIR__ . '/Config/TheliaMain.sql'));
}
$langs = LangQuery::create()->filterByActive(1)->find();
/** Default config values */
$defaultConfig = [
ChronopostPickupPointConst::CHRONOPOST_PICKUP_POINT_CODE_CLIENT => null,
ChronopostPickupPointConst::CHRONOPOST_PICKUP_POINT_PASSWORD => null,
];
/** Defaults the delivery types status as false */
foreach (ChronopostPickupPointConst::getDeliveryTypesStatusKeys() as $statusKey) {
$defaultConfig[$statusKey] = false;
}
/** Set the default config values in the DB table if it doesn't exists yet */
foreach ($defaultConfig as $key => $value) {
if (null === self::getConfigValue($key, null)) {
self::setConfigValue($key, $value);
}
}
/** Set the delivery types as not enabled in the ChronopostPickupPointDeliveryMode table
* when activating the module for the first time
*/
foreach (ChronopostPickupPointConst::CHRONOPOST_PICKUP_POINT_DELIVERY_CODES as $title => $code) {
if (null === $this->isDeliveryTypeSet($code)) {
$this->setDeliveryType($code, $title, $langs);
}
}
if (null === MessageQuery::create()->findOneByName(self::CHRONOPOST_CONFIRMATION_MESSAGE_NAME)) {
$message = new Message();
$message
->setName(self::CHRONOPOST_CONFIRMATION_MESSAGE_NAME)
->setHtmlLayoutFileName('order_shipped.html')
->setTextLayoutFileName('order_shipped.txt')
->setLocale('en_US')
->setTitle('Order send confirmation')
->setSubject('Order send confirmation')
->setLocale('fr_FR')
->setTitle('Confirmation d\'envoi de commande')
->setSubject('Confirmation d\'envoi de commande')
->save()
;
}
if (!self::getConfigValue(self::CHRONOPOST_TAX_RULE_ID)) {
self::setConfigValue(self::CHRONOPOST_TAX_RULE_ID, null);
}
}
public function update($currentVersion, $newVersion, ConnectionInterface $con = null):void
{
$sqlToExecute = [];
$finder = new Finder();
$sort = function (\SplFileInfo $a, \SplFileInfo $b) {
$a = strtolower(substr($a->getRelativePathname(), 0, -4));
$b = strtolower(substr($b->getRelativePathname(), 0, -4));
return version_compare($a, $b);
};
$files = $finder->name('*.sql')
->in(__DIR__ . "/Config/Update/")
->sort($sort);
foreach ($files as $file) {
if (version_compare($file->getFilename(), $currentVersion, ">")) {
$sqlToExecute[$file->getFilename()] = $file->getRealPath();
}
}
$database = new Database($con);
foreach ($sqlToExecute as $version => $sql) {
$database->insertSql(null, [$sql]);
}
$langs = LangQuery::create()->filterByActive(1)->find();
// Add missing delivery types when updating
foreach (ChronopostPickupPointConst::CHRONOPOST_PICKUP_POINT_DELIVERY_CODES as $title => $code) {
if (null === $this->isDeliveryTypeSet($code)) {
$this->setDeliveryType($code, $title, $langs);
}
}
}
/**
* Check if a given delivery type exists in the ChronopostPickupPointDeliveryMode table
*
* @param $code
* @return ChronopostPickupPointDeliveryMode
*/
public function isDeliveryTypeSet($code)
{
return ChronopostPickupPointDeliveryModeQuery::create()->findOneByCode($code);
}
/**
* Add a delivery type to the ChronopostPickupPointDeliveryMode table
*
* @param $code
* @param $title
*/
public function setDeliveryType($code, $title, $langs)
{
$newDeliveryType = new ChronopostPickupPointDeliveryMode();
try {
$newDeliveryType
->setCode($code)
->setFreeshippingActive(false)
->setFreeshippingFrom(null)
->save();
/** @var Lang $lang */
foreach ($langs as $lang) {
$newDeliveryType
->setLocale($lang->getLocale())
->setTitle($title)
->save();
}
} catch (\Exception $e) {
}
}
/**
* Verify if the area asked by the user is in the list of areas added to the shipping zones
* and has correctly defined price slices
*
* @param Country $country
* @param State $state the state to deliver to.
* @return bool
* @throws \Propel\Runtime\Exception\PropelException
*/
public function isValidDelivery(Country $country, State $state = null)
{
if (empty($this->getAllAreasForCountry($country))) {
return false;
}
$countryAreas = $country->getCountryAreas();
$areasArray = [];
/** @var CountryArea $countryArea */
foreach ($countryAreas as $countryArea) {
$areasArray[] = $countryArea->getAreaId();
}
$prices = ChronopostPickupPointPriceQuery::create()
->filterByAreaId($areasArray)
->findOne()
;
$freeShipping = ChronopostPickupPointDeliveryModeQuery::create()
->filterByFreeshippingActive(true)
->findOne()
;
/** Check if Chronopost delivers in the asked area */
if (null !== $prices || null !== $freeShipping) {
try {
$this->getPostage($country, $state);
}catch (\Exception $exception){
return false;
}
return true;
}
return false;
}
/**
* Return the delivery type of an ongoing order.
*
* @param Request|Session $request
* @return null|string
*/
public function getDeliveryType($request)
{
$deliveryMode = $request->get('deliveryModuleOptionCode');
$deliveryCodes = array_change_key_case(ChronopostPickupPointConst::CHRONOPOST_PICKUP_POINT_DELIVERY_CODES, CASE_LOWER);
if (array_key_exists(strtolower($deliveryMode),$deliveryCodes)) {
return $deliveryCodes[strtolower($deliveryMode)];
}
if(in_array($deliveryMode, ChronopostPickupPointConst::CHRONOPOST_PICKUP_POINT_DELIVERY_CODES, true)){
return $deliveryMode;
}
return null;
}
/**
* Return the postage price for a given area, cart weight, cart amount, and delivery type
*
* @param $areaId
* @param $weight
* @param int $cartAmount
* @param null $deliveryCode
* @return int
* @throws \Exception
*/
public static function getPostageAmount($areaId, $weight, $cartAmount = 0, $deliveryCode = null)
{
if (null === $deliveryType = ChronopostPickupPointDeliveryModeQuery::create()->findOneByCode($deliveryCode)) {
throw new \Exception("The delivery code given is not supported by the module.");
}
/** Check if freeshipping is activated for this delivery type */
try {
$freeShipping = $deliveryType->getFreeshippingActive();
} catch (\Exception $e) {
$freeShipping = false;
}
/** Get the total cart price needed to have a free shipping for all areas, if it exists */
try {
$freeShippingFrom = $deliveryType->getFreeshippingFrom();
} catch (\Exception $er) {
$freeShippingFrom = null;
}
/** Set the initial postage price as 0 */
$postage = 0;
/** If free shipping is enabled, skip and return 0 */
if (!$freeShipping) {
/** If a minimum price for free shipping is defined and the amount of the cart reach this limit, return 0. */
if (null !== $freeShippingFrom && $freeShippingFrom <= $cartAmount) {
return $postage;
}
/** Get the minimum price for free shipping in the area of the order */
$cartAmountFreeShipping = ChronopostPickupPointAreaFreeshippingQuery::create()
->filterByAreaId($areaId)
->filterByDeliveryModeId($deliveryType->getId())
->findOne();
if (null !== $cartAmountFreeShipping) {
$cartAmountFreeShipping = $cartAmountFreeShipping->getCartAmount();
}
/** If the cart price is superior to the minimum price for free shipping in the area of the order,
* return the postage as free.
*/
if ($cartAmountFreeShipping !== null && $cartAmountFreeShipping <= $cartAmount) {
return 0;
}
/** Search the list of prices and order it in ascending order */
$areaPrices = ChronopostPickupPointPriceQuery::create()
->filterByDeliveryModeId($deliveryType->getId())
->filterByAreaId($areaId)
->filterByWeightMax($weight, Criteria::GREATER_EQUAL)
->_or()
->filterByWeightMax(null)
->filterByPriceMax($cartAmount, Criteria::GREATER_EQUAL)
->_or()
->filterByPriceMax(null)
->orderByWeightMax()
->orderByPriceMax();
/** Find the correct postage price for the cart weight and price according to the area and delivery mode in $areaPrices*/
$firstPrice = $areaPrices
->find()
->getFirst()
;
/** If no price was found, return null */
if (null === $firstPrice) {
return null;
}
$postage = $firstPrice->getPrice();
}
return $postage;
}
/**
* Return the minimum postage price of a list of areas, for a given cart weight, price, and delivery type.
*
* @param $country
* @param $cartWeight
* @param $cartAmount
* @param $deliveryType
* @return OrderPostage
*/
public function getMinPostage($country, $cartWeight, $cartAmount, $deliveryType, $locale)
{
$minPostage = null;
/** Check what areas are covered in the shipping zones defined by the admin */
$areaIdArray = $this->getAllAreasForCountry($country);
if (empty($areaIdArray)) {
throw new DeliveryException("Your delivery country is not covered by Chronopost");
}
foreach ($areaIdArray as $areaId) {
try {
$postage = self::getPostageAmount($areaId, $cartWeight, $cartAmount, $deliveryType);
if (null === $postage) {
continue ;
}
if ($minPostage === null || $postage < $minPostage) {
$minPostage = $postage;
if ($minPostage === 0) {
break;
}
}
} catch (\Exception $ex) {
throw new DeliveryException($ex->getMessage()); //todo : Make a better catch, duh
}
}
return $this->buildOrderPostage($minPostage, $country, $locale, self::getConfigValue(self::CHRONOPOST_TAX_RULE_ID));
}
/**
* Return an array of the codes of all delivery types that have been activated
* in the backOffice configuration page.
*
* @return array
*/
public static function getActivatedDeliveryTypes()
{
$config = ChronopostPickupPointConst::getConfig();
$deliveryTypes = ChronopostPickupPointConst::CHRONOPOST_PICKUP_POINT_DELIVERY_CODES;
$activatedDeliveryTypes = [];
foreach (ChronopostPickupPointConst::getDeliveryTypesStatusKeys() as $deliveryTypeName => $statusKey) {
if (true === (bool)$config[$statusKey]) {
$activatedDeliveryTypes[] = $deliveryTypes[$deliveryTypeName];
}
}
return $activatedDeliveryTypes;
}
/**
* Return the postage of an ongoing order, or the minimum expected postage before the user chooses what delivery types he wants.
*
* @param Country $country
* @param null $deliveryType
* @param State $state the state to deliver to.
* @return float|int|\Thelia\Model\OrderPostage
* @throws \Propel\Runtime\Exception\PropelException
*/
public function getPostage(Country $country, State $state = null)
{
$request = $this->getRequest();
$cartWeight = $request->getSession()->getSessionCart($this->getDispatcher())->getWeight();
$cartAmount = $request->getSession()->getSessionCart($this->getDispatcher())->getTaxedAmount($country);
/** Get the delivery type of an ongoing order by looking at the request */
$deliveryType = $this->getDeliveryType($request);
/** If no delivery type was found, search again in the session. */
if (null === $deliveryType) {
$deliveryType = $this->getDeliveryType($request->getSession());
}
$deliveryArray[] = $deliveryType;
/** If still no delivery type is found, create an array of all activated delivery types to search
* which one has the lowest delivery price.
*/
if (null === $deliveryType) {
$deliveryArray = $this->getActivatedDeliveryTypes();
}
$postage = null;
/** If no delivery type was given, the loop should continue until the postage for each delivery types was
* found, then return the minimum one. Otherwise, the loop should stop after the first iteration.
*/
if ($deliveryArray !== null) {
$y = 0;
$postage = $this->getMinPostage($country, $cartWeight, $cartAmount, $deliveryArray[$y], $request->getSession()->getLang()->getLocale());
while (isset($deliveryArray[$y]) && !empty($deliveryArray[$y]) && null !== $deliveryArray[$y]) {
if ($postage > ($minPost = $this->getMinPostage($country, $cartWeight, $cartAmount, $deliveryArray[$y], $request->getSession()->getLang()->getLocale())) && $minPost !== null) {
$postage = $minPost;
}
$y++;
}
}
/** If no postage was found, we throw an exception */
if (null === $postage) {
throw new DeliveryException("Chronopost delivery unavailable for your cart weight or delivery country");
}
/** Get the postage for the shipping zones we've just got */
///** If delivery is free, set it to a minimal number so the price will still appear. It will be rounded up to 0 */
//if (0 == $postage) {
// $postage = 0.000001;
//}
return $postage;
}
/**
* Returns ids of area containing this country and covered by this module
* @param Country $country
* @return array Area ids
*/
public function getAllAreasForCountry(Country $country)
{
$areaArray = [];
$sql = "SELECT ca.area_id as area_id FROM country_area ca
INNER JOIN area_delivery_module adm ON (ca.area_id = adm.area_id AND adm.delivery_module_id = :p0)
WHERE ca.country_id = :p1";
$con = Propel::getConnection();
$stmt = $con->prepare($sql);
$stmt->bindValue(':p0', $this->getModuleModel()->getId(), PDO::PARAM_INT);
$stmt->bindValue(':p1', $country->getId(), PDO::PARAM_INT);
$stmt->execute();
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
$areaArray[] = $row['area_id'];
}
return $areaArray;
}
public static function configureServices(ServicesConfigurator $servicesConfigurator): void
{
$servicesConfigurator->load(self::getModuleCode().'\\', __DIR__)
->exclude([THELIA_MODULE_DIR . ucfirst(self::getModuleCode()). "/I18n/*"])
->autowire(true)
->autoconfigure(true);
}
public function getDeliveryMode()
{
return "pickup";
}
}