-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathDpdClassic.php
253 lines (203 loc) · 8.49 KB
/
DpdClassic.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
<?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 DpdClassic;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Propel;
use Symfony\Component\DependencyInjection\Loader\Configurator\ServicesConfigurator;
use Thelia\Exception\OrderException;
use Thelia\Install\Database;
use Thelia\Model\Country;
use Thelia\Model\OrderPostage;
use Thelia\Model\State;
use Thelia\Module\AbstractDeliveryModuleWithState;
use Thelia\Module\Exception\DeliveryException;
use PDO;
class DpdClassic extends AbstractDeliveryModuleWithState
{
const DOMAIN_NAME = 'dpdclassic';
const DELIVERY_REF_COLUMN = 17;
const ORDER_REF_COLUMN = 18;
const STATUS_PAID = 2;
const STATUS_PROCESSING = 3;
const STATUS_SENT = 4;
const NO_CHANGE = 'nochange';
const PROCESS = 'processing';
const SEND = 'sent';
const JSON_PRICE_RESOURCE = "/Config/prices.json";
const DPD_CLASSIC_TAX_RULE_ID = 'dpd_classic_tax_rule_id';
protected $request;
protected $dispatcher;
private static $prices = null;
public function postActivation(ConnectionInterface $con = null): void
{
$database = new Database($con->getWrappedConnection());
if (!self::getConfigValue(self::DPD_CLASSIC_TAX_RULE_ID)) {
self::setConfigValue(self::DPD_CLASSIC_TAX_RULE_ID, null);
}
$database->insertSql(null, array(__DIR__ . '/Config/thelia.sql'));
}
/**
* 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
* @param State|null $state
* @return boolean
*/
public function isValidDelivery(Country $country, State $state = null)
{
$cartWeight = $this->getRequest()->getSession()->getSessionCart($this->getDispatcher())->getWeight();
$areaIds = $this->getAllAreasForCountry($country);
$prices = self::getPrices();
foreach ($areaIds as $areaId) {
/* Check if DpdClassic delivers the asked area */
if (isset($prices[$areaId]) && isset($prices[$areaId]["slices"])) {
$areaPrices = $prices[$areaId]["slices"];
ksort($areaPrices);
/* check this weight is not too much */
end($areaPrices);
$maxWeight = key($areaPrices);
if ($cartWeight <= $maxWeight) {
return true;
}
}
}
return false;
}
public static function getPrices()
{
if (null === self::$prices) {
if (is_readable(sprintf('%s/%s', __DIR__, self::JSON_PRICE_RESOURCE))) {
self::$prices = json_decode(
file_get_contents(sprintf('%s/%s', __DIR__, self::JSON_PRICE_RESOURCE)),
true
);
} else {
self::$prices = null;
}
}
return self::$prices;
}
/**
* Calculate and return delivery price in the shop's default currency
*
*
* @param Country $country
* @param State|null $state
* @return OrderPostage|float the delivery price
* @throws DeliveryException if the postage price cannot be calculated.
* @throws \Propel\Runtime\Exception\PropelException
*/
public function getPostage(Country $country, State $state = null)
{
$cart = $this->getRequest()->getSession()->getSessionCart($this->getDispatcher());
$postage = $this->getOrderPostage(
$country,
$cart->getWeight(),
$this->getRequest()->getSession()->getLang()->getLocale(),
$cart->getTaxedAmount($country)
);
return $postage;
}
public function getOrderPostage($country, $weight, $locale, $cartAmount = 0): OrderPostage
{
$freeShipping = Dpdclassic::getConfigValue('freeshipping');
$postage=0;
$areasIds = $this->getAllAreasForCountry($country);
$foundArea = false;
if (!$freeShipping) {
$freeShippingAmount = (float) self::getFreeShippingAmount();
//If a min price for freeShipping is defined and the amount of cart reach this amount return 0
//Be careful ! Thelia cartAmount is a decimal with 6 in precision ! That's why we must round cart amount
if ($freeShippingAmount > 0 && $freeShippingAmount <= round($cartAmount, 2)) {
return 0;
}
$prices = self::getPrices();
foreach ($areasIds as $areaId) {
/* check if DpdClassic delivers the asked area */
if (!isset($prices[$areaId]) || !isset($prices[$areaId]["slices"])) {
continue;
}
$foundArea = true;
$areaPrices = $prices[$areaId]["slices"];
ksort($areaPrices);
/* check this weight is not too much */
end($areaPrices);
$maxWeight = key($areaPrices);
if ($weight > $maxWeight) {
throw new DeliveryException(
sprintf("DPD Classic delivery unavailable for this cart weight (%s kg)", $weight),
OrderException::DELIVERY_MODULE_UNAVAILABLE
);
}
$postage = current($areaPrices);
while (prev($areaPrices)) {
if ($weight > key($areaPrices)) {
break;
}
$postage = current($areaPrices);
}
break;
}
if (!$foundArea) {
throw new DeliveryException(
"DPD Classic delivery unavailable for the chosen delivery country",
OrderException::DELIVERY_MODULE_UNAVAILABLE
);
}
}
return $this->buildOrderPostage($postage, $country, $locale, self::getConfigValue(self::DPD_CLASSIC_TAX_RULE_ID));
}
public static function getFreeShippingAmount()
{
if (!null !== $amount = self::getConfigValue('free_shipping_amount')) {
return (float) $amount;
}
return 0;
}
public static function setFreeShippingAmount($amount)
{
self::setConfigValue('free_shipping_amount', $amount);
}
public static function configureServices(ServicesConfigurator $servicesConfigurator): void
{
$servicesConfigurator->load(self::getModuleCode().'\\', __DIR__)
->exclude([THELIA_MODULE_DIR . ucfirst(self::getModuleCode()). "/I18n/*"])
->autowire(true)
->autoconfigure(true);
}
/**
* 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;
}
}