Skip to content

Commit 2967a0a

Browse files
authored
Merge pull request #6 from dpdconnect/1.1.3
1.1.3
2 parents 9f3ef75 + 81cd60e commit 2967a0a

File tree

19 files changed

+742
-555
lines changed

19 files changed

+742
-555
lines changed

classes/DpdCarrier.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function createCarriers()
9595
$carrier->name = $info['name'];
9696
$carrier->delay[Configuration::get('PS_LANG_DEFAULT')] = (string)$info['description'];
9797
$carrier->active = 0;
98-
$carrier->deleted = 1;
98+
$carrier->deleted = 0;
9999
$carrier->shipping_handling = 1;
100100
$carrier->range_behavior = 0;
101101
$carrier->shipping_external = 0;
@@ -105,29 +105,13 @@ public function createCarriers()
105105
Configuration::updateValue($this->dpdPrefix . strtolower($prefix), $carrier->id);
106106

107107
// copy(dirname(__DIR__) . '/../logo.png', _PS_SHIP_IMG_DIR_ . '/' . (int)$carrier->id . '.jpg'); //assign carrier logo
108-
}
109-
}
110-
$this->setCarrierForAccountType();
111-
return true;
112-
}
113-
114-
public function setCarrierForAccountType()
115-
{
116-
$accountType = Configuration::get($this->dpdPrefix . 'account_type');
117-
118-
foreach ($this->carrierNames as $prefix => $info) {
119-
$configCarrierId = Configuration::get($this->dpdPrefix . $prefix);
120-
121-
$carrierId = $this->getLatestCarrierByReferenceId($configCarrierId, false);
122-
if ($info['type'] == $accountType) {
123-
$this->unDeleteCarrier($carrierId);
124108
} else {
125-
$this->softDeleteCarriers($carrierId);
109+
$this->unDeleteCarrier(Configuration::get($this->dpdPrefix . strtolower($prefix)));
126110
}
127111
}
112+
return true;
128113
}
129114

130-
131115
public function deleteCarriers()
132116
{
133117
foreach ($this->carrierNames as $prefix => $info) {

classes/DpdHelper.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ public function displayConfigurationForm($module, $formAccountSettings, $formAdr
6666
$helperForm->fields_value['dpdconnect_password'] = Configuration::get('dpdconnect_connect_password') ?: Tools::getValue('dpdconnect_password');
6767
$helperForm->fields_value['dpdconnect_depot'] = Configuration::get('dpdconnect_depot') ?: Tools::getValue('dpdconnect_depot');
6868
$helperForm->fields_value['company'] = Configuration::get('dpdconnect_company') ?: Tools::getValue('company');
69-
$helperForm->fields_value['account_type'] = Configuration::get('dpdconnect_account_type') ?: Tools::getValue('account_type');
7069
$helperForm->fields_value['street'] = Configuration::get('dpdconnect_street') ?: Tools::getValue('street');
7170
$helperForm->fields_value['postalcode'] = Configuration::get('dpdconnect_postalcode') ?: Tools::getValue('postalcode');
7271
$helperForm->fields_value['place'] = Configuration::get('dpdconnect_place') ?: Tools::getValue('place');
@@ -75,8 +74,8 @@ public function displayConfigurationForm($module, $formAccountSettings, $formAdr
7574
$helperForm->fields_value['vatnumber'] = Configuration::get('dpdconnect_vatnumber') ?: Tools::getValue('vatnumber');
7675
$helperForm->fields_value['eorinumber'] = Configuration::get('dpdconnect_eorinumber') ?: Tools::getValue('eorinumber');
7776
$helperForm->fields_value['spr'] = Configuration::get('dpdconnect_spr') ?: Tools::getValue('spr');
78-
$helperForm->fields_value['gmaps_client_key'] = Configuration::get('gmaps_client_key') ?: Tools::getValue('gmaps_client_key');
79-
$helperForm->fields_value['gmaps_server_key'] = Configuration::get('gmaps_server_key') ?: Tools::getValue('gmaps_server_key');
77+
$helperForm->fields_value['gmaps_key'] = Configuration::get('gmaps_key') ?: Tools::getValue('gmaps_key');
78+
$helperForm->fields_value['use_gmaps_key'] = Configuration::get('use_gmaps_key') ?: Tools::getValue('use_gmaps_key');
8079
$helperForm->fields_value['default_product_hcs'] = Configuration::get('dpdconnect_default_product_hcs') ?: Tools::getValue('default_product_hcs');
8180
$helperForm->fields_value['default_product_weight'] = Configuration::get('dpdconnect_default_product_weight') ?: Tools::getValue('default_product_weight');
8281
$helperForm->fields_value['default_product_country_of_origin'] = Configuration::get('dpdconnect_default_product_country_of_origin') ?: Tools::getValue('default_product_country_of_origin');

classes/DpdLabelGenerator.php

Lines changed: 89 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -88,33 +88,40 @@ public function __construct()
8888
$this->dpdParcelPredict = new DpdParcelPredict();
8989
}
9090

91-
public function generateLabel($orderIds, $parcelCount, $return)
91+
public function generateLabel($orderIds, $parcelCount, $return, $freshFreezeData = array())
9292
{
9393
if (isset($this->errors['LOGIN_8'])) {
9494
$this->errors['LOGIN_8'] = $this->dpdError->get('LOGIN_8');
9595
}
9696
$labelsForDirectDownload = [];
9797
$labelRequests = [];
9898

99-
foreach ($orderIds as $orderId) {
99+
$bundledOrders = FreshFreezeHelper::bundleOrders($orderIds);
100+
101+
// Create shipment for every dpd shipping type
102+
foreach ($bundledOrders as $orderId => $orders) {
100103
if (!$this->dpdParcelPredict->checkIfDpdSending($orderId)) {
101104
continue;
102105
}
103106

104107
$result = $this->getLabelOutOfDb($orderId, $return);
105108
if ($result) {
106-
$labelsForDirectDownload[] = [
107-
'pdf' => $result[0]['label'],
108-
'mpsId' => $result[0]['mps_id'],
109-
];
109+
foreach ($result as $label) {
110+
$labelsForDirectDownload[] = [
111+
'pdf' => $label['label'],
112+
'mpsId' => $label['mps_id'],
113+
];
114+
}
110115
continue;
111116
}
112117

113-
try {
114-
$labelRequests[] = $this->generateShipmentInfo($orderId, $parcelCount, $return);
115-
} catch (InvalidRequestException $e) {
116-
$this->errors['VALIDATION'] = 'Multiple parcels is only allowed for countries inside EU.';
117-
return;
118+
foreach ($orders as $shippingType => $orderProducts) {
119+
try {
120+
$labelRequests[] = $this->generateShipmentInfo($orderId, $orderProducts, $parcelCount, $return, $freshFreezeData);
121+
} catch (InvalidRequestException $e) {
122+
$this->errors['VALIDATION'] = 'Multiple parcels is only allowed for countries inside EU.';
123+
return;
124+
}
118125
}
119126
}
120127

@@ -132,21 +139,22 @@ public function generateLabel($orderIds, $parcelCount, $return)
132139
}
133140
}
134141

135-
if (count($orderIds) > 1) {
142+
if (count($labelsForDirectDownload) > 1) {
136143
return $this->redirectToZipDownload($labelsForDirectDownload);
137144
}
138145

139146
return $this->redirectToPdfDownload($labelsForDirectDownload[0]);
140147
}
141148

142-
public function generateShipmentInfo($orderId, $parcelCount, $return)
149+
public function generateShipmentInfo($orderId, $orderProducts, $parcelCount, $return, $freshFreezeData)
143150
{
144151
if ($parcelCount === null || $parcelCount === false) {
145152
$parcelCount = 1;
146153
}
147154
$tempOrder = new Order($orderId);
148-
$orderDetails = OrderDetail::getList($orderId);
155+
// $orderDetails = OrderDetail::getList($orderId);
149156
$address = new Address((int)$tempOrder->id_address_delivery);
157+
$dpdShippingType = $orderProducts[0]['dpd_shipping_product'];
150158

151159
$country = new Country($address->id_country);
152160
$multipleParcelsAllowed = $this->isPartOfSingleMarket($country->iso_code);
@@ -166,11 +174,11 @@ public function generateShipmentInfo($orderId, $parcelCount, $return)
166174
$saturdayDelivery = false;
167175
$orderType = 'consignment';
168176

169-
foreach ($orderDetails as $orderDetail) {
170-
if ($orderDetail['product_weight'] == 0) {
171-
$orderDetail['product_weight'] = 5;
177+
foreach ($orderProducts as $orderProduct) {
178+
if ((float)$orderProduct['product_weight'] <= 0) {
179+
$orderProduct['product_weight'] = '5.0';
172180
}
173-
$weightTotal += $orderDetail['product_weight'] * $orderDetail['product_quantity'];
181+
$weightTotal += ((float)$orderProduct['product_weight'] / 100) * (int)$orderProduct['product_quantity'];
174182
}
175183
$weightTotal *= 100;
176184
if (($this->dpdParcelPredict->checkIfSaturdayCarrier($orderId) || $this->dpdParcelPredict->checkIfClassicSaturdayCarrier($orderId)) && !$return) {
@@ -223,8 +231,17 @@ public function generateShipmentInfo($orderId, $parcelCount, $return)
223231
$productCode = 'RETURN';
224232
}
225233

234+
if (!$return && $dpdShippingType === FreshFreezeHelper::TYPE_FRESH) {
235+
$productCode = strtoupper(FreshFreezeHelper::TYPE_FRESH);
236+
}
237+
238+
if (!$return && $dpdShippingType === FreshFreezeHelper::TYPE_FREEZE) {
239+
$productCode = strtoupper(FreshFreezeHelper::TYPE_FREEZE);
240+
}
241+
242+
226243
$shipment = [
227-
'orderId' => $orderId,
244+
'orderId' => (string)$orderId,
228245
'sendingDepot' => Configuration::get('dpdconnect_depot'),
229246
'sender' => [
230247
'name1' => Configuration::get('dpdconnect_company'),
@@ -245,6 +262,7 @@ public function generateShipmentInfo($orderId, $parcelCount, $return)
245262
'postalcode' => $address->postcode, // No spaces in zipCode!
246263
'city' => $address->city,
247264
'phoneNumber' => $phone,
265+
'email' => $customer->email,
248266
'commercialAddress' => false,
249267
'vatnumber' => (Configuration::get('dpdconnect_vatnumber') === null) ? 'null' : Configuration::get('dpdconnect_vatnumber'),
250268
'eorinumber' => (Configuration::get('dpdconnect_eorinumber') === null) ? 'null' : Configuration::get('dpdconnect_eorinumber'),
@@ -276,19 +294,31 @@ public function generateShipmentInfo($orderId, $parcelCount, $return)
276294

277295
$shipment['parcels'] = [];
278296

279-
for ($x = 1; $x <= $parcelCount; $x++) {
280-
$parcelInfo = [
281-
'customerReferenceNumber1' => $orderId,
282-
'weight' => (int) ceil($weightTotal / $parcelCount),
283-
];
297+
if (!$return && FreshFreezeHelper::shippingTypeIsFreshOrFreeze($dpdShippingType)) {
298+
$shipment['parcels'] = $this->generateFreshFreezeParcels(
299+
$orderProducts,
300+
ceil($weightTotal / $parcelCount) * 100,
301+
$freshFreezeData,
302+
$parcelCount
303+
);
304+
} else {
305+
for ($x = 1; $x <= $parcelCount; $x++) {
306+
$parcelInfo = [
307+
'customerReferences' => [
308+
(string)$orderId
309+
],
310+
'weight' => (int) ceil($weightTotal / $parcelCount) * 100,
311+
];
284312

285-
if ((boolean)$return) {
286-
$parcelInfo['returns'] = true;
287-
}
313+
if ((boolean)$return) {
314+
$parcelInfo['returns'] = true;
315+
}
288316

289-
array_push($shipment['parcels'], $parcelInfo);
317+
array_push($shipment['parcels'], $parcelInfo);
318+
}
290319
}
291320

321+
292322
$currency = new Currency($tempOrder->id_currency);
293323
$shipment['customs'] = [
294324
'terms' => 'DAP',
@@ -297,26 +327,25 @@ public function generateShipmentInfo($orderId, $parcelCount, $return)
297327

298328
$totalAmount = 0;
299329

300-
$rows = $tempOrder->getWsOrderRows();
301330
$customsLines = [];
302331

303-
foreach ($rows as $row) {
304-
$product = new Product($row['product_id']);
332+
foreach ($orderProducts as $orderProduct) {
333+
$product = new Product($orderProduct['product_id']);
305334
$hsCode = $this->getHsCode($product);
306335
$customsValue = $this->getCustomsValue($product);
307336
$originCountry = $this->getCountryOfOrigin($product);
308337
$weight = (int) ceil($product->weight) * 100; // Default weight is 0.000000
309338
if ($weight === 0) {
310339
$weight = Configuration::get('dpdconnect_default_product_weight');
311340
}
312-
$amount = $customsValue * $row['product_quantity'];
341+
$amount = $customsValue * $orderProduct['product_quantity'];
313342
$totalAmount += $amount;
314343

315344
$customsLines[] = [
316-
'description' => mb_strcut($row['product_name'], 0, 35),
345+
'description' => mb_strcut($orderProduct['product_name'], 0, 35),
317346
'harmonizedSystemCode' => $hsCode ? $hsCode : "",
318347
'originCountry' => $originCountry ? $originCountry : "",
319-
'quantity' => (int) $row['product_quantity'],
348+
'quantity' => (int) $orderProduct['product_quantity'],
320349
'netWeight' => (int) $weight,
321350
'grossWeight' => (int) $weight,
322351
'totalAmount' => (float) ($amount),
@@ -384,7 +413,7 @@ public static function countLabels($orderId, $return = false)
384413
public static function deleteLabelFromDb($ordersId, $return)
385414
{
386415
foreach ($ordersId as $orderId) {
387-
Db::getInstance()->delete('dpdshipment_label', 'order_id=' . pSQL($orderId) . ' AND retour=' . pSQL((int)$return), 1);
416+
Db::getInstance()->delete('dpdshipment_label', 'order_id=' . pSQL($orderId) . ' AND retour=' . pSQL((int)$return));
388417

389418
$tempOrder = new Order($orderId);
390419
// so it empty the shipping number.
@@ -625,4 +654,29 @@ private function lookupCountry($iso2)
625654
$countries = $this->dpdClient->getCountries()->getList();
626655
return array_search(strtoupper($iso2), array_column($countries, 'country'), true);
627656
}
657+
658+
private function generateFreshFreezeParcels($orderProducts, $weight, $freshFreezeData, $parcelCount)
659+
{
660+
$parcels = [];
661+
662+
foreach ($orderProducts as $orderProduct) {
663+
$orderId = (int)$orderProduct['id_order'];
664+
$expirationDate = $freshFreezeData[$orderId][$orderProduct['product_id']]['expiration_date'];
665+
$expirationDate = str_replace('-', '', $expirationDate);
666+
667+
for ($i = 0; $i < $parcelCount; $i++) {
668+
$parcels[] = [
669+
'customerReferences' => [
670+
(string)$orderProduct['id_order'],
671+
$orderProduct['reference']
672+
],
673+
'weight' => (int) $weight,
674+
'goodsExpirationDate' => (int)$expirationDate,
675+
'goodsDescription' => mb_strcut($freshFreezeData[$orderId][$orderProduct['product_id']]['carrier_description'], 0, 34)
676+
];
677+
}
678+
}
679+
680+
return $parcels;
681+
}
628682
}

classes/FreshFreezeHelper.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace DpdConnect\classes;
4+
5+
use OrderCore;
6+
use Order;
7+
use Product;
8+
9+
class FreshFreezeHelper
10+
{
11+
const TYPE_DEFAULT = 'default';
12+
const TYPE_FRESH = 'fresh';
13+
const TYPE_FREEZE = 'freeze';
14+
15+
public static function ordersContainFreshFreezeProducts($orderIds)
16+
{
17+
foreach ($orderIds as $orderId) {
18+
/** @var OrderCore $order */
19+
$order = new Order($orderId);
20+
21+
/** @var array $orderProduct */
22+
foreach ($order->getProducts() as $orderProduct) {
23+
$dpdShippingProduct = $orderProduct['dpd_shipping_product'];
24+
25+
if (in_array(strtolower($dpdShippingProduct), [self::TYPE_FRESH, self::TYPE_FREEZE])) {
26+
return true;
27+
}
28+
}
29+
}
30+
31+
return false;
32+
}
33+
34+
public static function bundleOrders($orderIds)
35+
{
36+
$bundledOrders = [];
37+
38+
foreach ($orderIds as $orderId) {
39+
/** @var OrderCore $order */
40+
$order = new Order($orderId);
41+
42+
/** @var array $orderProduct */
43+
foreach ($order->getProducts() as $orderProduct) {
44+
$dpdShippingProduct = $orderProduct['dpd_shipping_product'];
45+
46+
$bundledOrders[$order->id][$dpdShippingProduct][] = $orderProduct;
47+
}
48+
}
49+
50+
return $bundledOrders;
51+
}
52+
53+
// Default date is current date + 5 weekdays
54+
public static function getDefaultDate()
55+
{
56+
return date('Y-m-d', strtotime("+5 weekday"));
57+
}
58+
59+
public static function collectFreshFreezeData()
60+
{
61+
$data = [];
62+
63+
foreach ($_POST as $key => $value) {
64+
// Check if key starts with 'dpd_'. If not, skip this iteration
65+
if (strpos($key, 'dpd_') !== 0) {
66+
continue;
67+
}
68+
69+
// Extract expiration date (yyyy-mm-dd) from key
70+
if (strpos($key, 'dpd_expiration_date') === 0) {
71+
$explodedString = explode('_', $key);
72+
73+
$orderId = $explodedString[3];
74+
$orderProductId = $explodedString[4];
75+
76+
$data[$orderId][$orderProductId]['expiration_date'] = $value;
77+
}
78+
79+
// Extract carrier description from key
80+
if (strpos($key, 'dpd_carrier_description') === 0) {
81+
$explodedString = explode('_', $key);
82+
83+
$orderId = $explodedString[3];
84+
$orderProductId = $explodedString[4];
85+
86+
$data[$orderId][$orderProductId]['carrier_description'] = $value;
87+
}
88+
}
89+
90+
return $data;
91+
}
92+
93+
public static function shippingTypeIsFreshOrFreeze($shippingType)
94+
{
95+
return in_array(strtolower($shippingType), [self::TYPE_FRESH, self::TYPE_FREEZE]);
96+
}
97+
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"license": "GPL-3.0-or-later",
55
"minimum-stability": "dev",
66
"require": {
7-
"dpdconnect/php-sdk": "^1.0"
7+
"dpdconnect/php-sdk": "^1.1"
88
},
99
"autoload": {
1010
"psr-4": {

0 commit comments

Comments
 (0)