@@ -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}
0 commit comments