@@ -284,58 +284,12 @@ protected function calculate()
284284 return array ();
285285 }
286286
287- $ params = array ();
288- $ params ['senderId ' ] = $ this ->senderId ;
289-
290- $ params += $ this ->prepareAddress ();
291-
292- $ total_weight = $ this ->getTotalWeight ();
293- if (!empty ($ total_weight )) {
294- $ package_size = $ this ->getPackageSize ($ total_weight );
295- if (!empty ($ package_size )) {
296- $ params ['dimensions ' ] = [
297- 'weight ' => $ total_weight
298- ];
299- $ params ['dimensions ' ] += $ package_size ;
300- }
301- }
302- $ params ['shipment ' ]['warehouseId ' ] = $ this ->warehouseId ;
303-
304- /** @var string $departure_datetime SQL DATETIME */
305- $ departure_datetime = $ this ->getPackageProperty ('departure_datetime ' );
306- if ($ departure_datetime ) {
307- $ params ['shipment ' ]['date ' ] = date ('Y-m-d ' , strtotime ($ departure_datetime ));
308- }
309-
310- $ country_id = $ this ->getAddress ('country ' );
311- $ region_id = $ this ->getAddress ('region ' );
312- if (!empty ($ country_id ) && !empty ($ region_id )) {
313- if (is_numeric ($ region_id )) {
314- $ region_model = new waRegionModel ();
315- $ region_data = $ region_model ->getByField (array (
316- 'country_iso3 ' => $ country_id ,
317- 'code ' => $ region_id ,
318- ));
319- $ name_region = $ region_data ['name ' ];
320- } else {
321- // here region_id is the name of the region
322- $ name_region = $ region_id ;
323- }
324- $ to_location = $ params ['to ' ]['location ' ];
325- if (!empty ($ name_region )) {
326- $ params ['to ' ]['location ' ] = $ name_region . ', ' . $ to_location ;
327- }
328- $ zip = $ this ->getAddress ('zip ' );
329- if (!empty ($ zip )) {
330- $ params ['to ' ]['location ' ] .= ', ' . $ zip ;
331- } elseif (!empty ($ params ['to ' ]['geoId ' ])) {
332- $ params ['to ' ]['location ' ] .= ', ' . $ params ['to ' ]['geoId ' ];
333- }
334-
335- $ options = $ this ->getExactAddressesForMainSelector ($ params ['to ' ]['location ' ]);
287+ $ delivery_options = $ this ->getDeliveryOptionsParams ();
288+ if (isset ($ delivery_options ['simple_location ' ])) {
289+ $ options = $ this ->getExactAddressesForMainSelector ($ delivery_options ['params ' ]['to ' ]['location ' ]);
336290 $ addresses_do_not_match = true ;
337291 foreach ($ options as $ option ) {
338- if (mb_strpos (mb_strtolower ($ option ['value ' ]), mb_strtolower ($ to_location )) !== false ) {
292+ if (mb_strpos (mb_strtolower ($ option ['value ' ]), mb_strtolower ($ delivery_options [ ' simple_location ' ] )) !== false ) {
339293 $ addresses_do_not_match = false ;
340294 }
341295 }
@@ -350,12 +304,9 @@ protected function calculate()
350304 }
351305 }
352306
353- $ params ['cost ' ]['assessedValue ' ] = $ this ->getAssessedPrice ($ this ->insurance );
354- $ params ['cost ' ]['itemsSum ' ] = $ this ->getTotalPrice ();
355-
356307 try {
357308 $ callback = array ($ this , 'handleCalculateResponse ' );
358- $ services = $ this ->apiQuery ('searchDeliveryList ' , $ params , $ callback );
309+ $ services = $ this ->apiQuery ('searchDeliveryList ' , $ delivery_options [ ' params ' ] , $ callback );
359310
360311 return $ this ->handleCalculateResponse (null , $ services );
361312 } catch (waException $ ex ) {
@@ -447,6 +398,65 @@ private function getPackageSize($weight = null)
447398 return $ data ;
448399 }
449400
401+ /**
402+ * Add places or dimensions
403+ * @param $data
404+ * @param bool $check_empty
405+ * @throws waException
406+ */
407+ protected function addDimensions (&$ data , $ check_empty = true )
408+ {
409+ if ($ this ->size ['type ' ] != 'places ' ) {
410+ $ total_weight = $ this ->getTotalWeight ();
411+ if (!empty ($ total_weight )) {
412+ $ package_size = $ this ->getPackageSize ($ total_weight );
413+ if (!empty ($ package_size )) {
414+ $ data ['dimensions ' ] = [
415+ 'weight ' => $ total_weight
416+ ];
417+ $ data ['dimensions ' ] += $ package_size ;
418+ }
419+ }
420+ }
421+ if (empty ($ data ['dimensions ' ])) {
422+ foreach ($ this ->getItems () as $ item ) {
423+ if (!empty ($ item ['weight ' ])) {
424+ $ place = [
425+ 'dimensions ' => $ this ->getItemDimensions ($ item )
426+ ];
427+
428+ if (!isset ($ data ['places ' ])) {
429+ $ data ['places ' ] = [];
430+ }
431+
432+ $ data ['places ' ][] = $ place ;
433+ }
434+ }
435+ }
436+ if ($ check_empty && empty ($ data ['places ' ]) && empty ($ data ['dimensions ' ])) {
437+ $ data ['dimensions ' ] = [
438+ 'length ' => 1 ,
439+ 'width ' => 1 ,
440+ 'height ' => 1 ,
441+ 'weight ' => 1 ,
442+ ];
443+ }
444+ }
445+
446+ /**
447+ * @param $item
448+ * @return array
449+ */
450+ private function getItemDimensions ($ item )
451+ {
452+ return [
453+ 'weight ' => $ item ['weight ' ],
454+ 'length ' => empty ($ item ['length ' ]) || ceil ($ item ['length ' ]) <= 1 ? 1 : ceil ($ item ['length ' ]),
455+ 'width ' => empty ($ item ['width ' ]) || ceil ($ item ['width ' ]) <= 1 ? 1 : ceil ($ item ['width ' ]),
456+ 'height ' => empty ($ item ['height ' ]) || ceil ($ item ['height ' ]) <= 1 ? 1 : ceil ($ item ['height ' ]),
457+ ];
458+ }
459+
450460 private function getAssessedPrice ($ string )
451461 {
452462 $ cost = 0.0 ;
@@ -476,6 +486,66 @@ private function getAssessedPrice($string)
476486 return round (max (0.0 , $ cost ), 2 );
477487 }
478488
489+ /**
490+ * @return array|array[]
491+ * @throws waException
492+ */
493+ private function getDeliveryOptionsParams ()
494+ {
495+ $ params = [
496+ 'senderId ' => $ this ->senderId ,
497+ 'shipment ' => [
498+ 'warehouseId ' => $ this ->warehouseId
499+ ],
500+ 'cost ' => [
501+ 'assessedValue ' => $ this ->getAssessedPrice ($ this ->insurance ),
502+ 'itemsSum ' => $ this ->getTotalPrice (),
503+ ]
504+ ];
505+
506+ $ params += $ this ->prepareAddress ();
507+
508+ $ this ->addDimensions ($ params , false );
509+
510+ /** @var string $departure_datetime SQL DATETIME */
511+ $ departure_datetime = $ this ->getPackageProperty ('departure_datetime ' );
512+ if ($ departure_datetime ) {
513+ $ params ['shipment ' ]['date ' ] = date ('Y-m-d ' , strtotime ($ departure_datetime ));
514+ }
515+
516+ $ country_id = $ this ->getAddress ('country ' );
517+ $ region_id = $ this ->getAddress ('region ' );
518+ $ to_location = null ;
519+ if (!empty ($ country_id ) && !empty ($ region_id )) {
520+ if (is_numeric ($ region_id )) {
521+ $ region_model = new waRegionModel ();
522+ $ region_data = $ region_model ->getByField (array (
523+ 'country_iso3 ' => $ country_id ,
524+ 'code ' => $ region_id ,
525+ ));
526+ $ name_region = $ region_data ['name ' ];
527+ } else {
528+ // here region_id is the name of the region
529+ $ name_region = $ region_id ;
530+ }
531+ $ to_location = $ params ['to ' ]['location ' ];
532+ if (!empty ($ name_region )) {
533+ $ params ['to ' ]['location ' ] = $ name_region . ', ' . $ to_location ;
534+ }
535+ $ zip = $ this ->getAddress ('zip ' );
536+ if (!empty ($ zip )) {
537+ $ params ['to ' ]['location ' ] .= ', ' . $ zip ;
538+ } elseif (!empty ($ params ['to ' ]['geoId ' ])) {
539+ $ params ['to ' ]['location ' ] .= ', ' . $ params ['to ' ]['geoId ' ];
540+ }
541+ }
542+
543+ return [
544+ 'simple_location ' => $ to_location ,
545+ 'params ' => $ params
546+ ];
547+ }
548+
479549 public function handleCalculateResponse ($ net , $ result )
480550 {
481551 if ($ result instanceof waException) {
@@ -803,8 +873,6 @@ private static function calculateServiceCost($service, $options)
803873 $ required = array_intersect ($ payment_types , (array )$ options ['payment_types ' ]);
804874 if ($ required ) {
805875 $ cost += floatval ($ _service ['cost ' ]);
806- } else {
807- $ cost -= floatval ($ _service ['cost ' ]);
808876 }
809877 }
810878 }
@@ -1378,17 +1446,17 @@ protected function draftPackage(waOrder $order, $shipping_data = array())
13781446 $ delivery_type = strtoupper ($ delivery_type );
13791447 $ data += array (
13801448 'senderId ' => $ this ->senderId ,
1449+ 'comment ' => $ order ->comment ,
13811450 'deliveryType ' => $ delivery_type ,
1382- 'items ' => array (),
13831451 'cost ' => array (
1452+ 'manualDeliveryForCustomer ' => $ shipping_discount ,
13841453 'assessedValue ' => $ this ->getAssessedPrice ($ this ->insurance ),
1385- 'manualDeliveryForCustomer ' => empty ($ shipping_discount ) ? 0 : 1 ,
1386- 'fullyPrepaid ' => $ order ->paid_datetime ? true : false ,
1454+ 'fullyPrepaid ' => (bool )$ order ->paid_datetime ,
13871455 ),
13881456 'deliveryOption ' => array (
1389- 'delivery ' => number_format ($ order ->shipping - $ shipping_discount , 0 , '. ' , '' ),
1457+ 'delivery ' => max ( 0 , number_format ($ order ->shipping - $ shipping_discount , 0 , '. ' , '' ) ),
13901458 ),
1391- 'comment ' => $ order -> comment ,
1459+ 'recipient ' => [] ,
13921460 );
13931461
13941462 $ order_shipping_type = preg_replace ('@\..+$@ ' , '' , $ order ->shipping_rate_id );
@@ -1403,52 +1471,90 @@ protected function draftPackage(waOrder $order, $shipping_data = array())
14031471 );
14041472
14051473 $ data ['shipment ' ]['type ' ] = $ this ->shipping_type ;
1406- if ($ this -> shipping_type != 'import ' ) {
1474+ if (strtolower ( $ data [ ' shipment ' ][ ' type ' ]) != 'import ' ) {
14071475 $ data ['shipment ' ]['partnerTo ' ] = $ delivery [0 ];
14081476 $ data ['shipment ' ]['warehouseFrom ' ] = $ this ->warehouseId ;
14091477 }
14101478
1479+ // RECIPIENT
14111480 $ shipping_address = $ order ->shipping_address ;
1412- $ data ['recipient ' ]['address ' ] = array (
1413- 'country ' => ifset ($ shipping_address ['country_name ' ]),
1414- 'region ' => ifset ($ shipping_address ['region_name ' ]),
1415- 'locality ' => ifset ($ shipping_address ['city ' ]),
1416- 'street ' => ifset ($ shipping_address ['street ' ]),
1417- 'postalCode ' => ifset ($ shipping_address ['zip ' ]),
1418- );
1419-
1481+ $ data ['recipient ' ] += $ this ->getContactFields ($ shipping_address , $ order , 'recipient ' );
1482+ foreach (['country ' => 'country_name ' , 'region ' => 'region_name ' ,
1483+ 'locality ' => 'city ' , 'street ' => 'street ' , 'postalCode ' => 'zip ' ] as $ yad_field => $ wa_field
1484+ ) {
1485+ if (!empty ($ shipping_address [$ wa_field ])) {
1486+ if (!isset ($ data ['recipient ' ]['address ' ])) {
1487+ $ data ['recipient ' ]['address ' ] = [];
1488+ }
1489+ $ data ['recipient ' ]['address ' ][$ yad_field ] = $ shipping_address [$ wa_field ];
1490+ }
1491+ }
14201492 if (!empty ($ order ['shipping_params ' ]['geo_id_to ' ])) {
14211493 $ data ['recipient ' ]['address ' ]['geoId ' ] = (int )$ order ['shipping_params ' ]['geo_id_to ' ];
14221494 } elseif (!empty ($ order ['shipping_params_geo_id_to ' ])) {
14231495 $ data ['recipient ' ]['address ' ]['geoId ' ] = (int )$ order ['shipping_params_geo_id_to ' ];
14241496 }
14251497
1426- $ data ['recipient ' ] += $ this ->getContactFields ($ shipping_address , $ order , 'recipient ' );
1498+ // CONTACTS
1499+ $ contacts_fields = $ this ->getContactFields ($ shipping_address , $ order , 'contacts ' );
1500+ if ($ contacts_fields ) {
1501+ $ data ['contacts ' ][0 ]['type ' ] = 'RECIPIENT ' ;
1502+ $ data ['contacts ' ][0 ] += $ contacts_fields ;
1503+ }
1504+
1505+ $ this ->addDimensions ($ data );
1506+
1507+ // items are now deprecated
14271508 foreach ($ this ->getItems () as $ item ) {
14281509 $ item_params = array (
14291510 'externalId ' => $ item ['id ' ],
14301511 'name ' => $ item ['name ' ],
14311512 'count ' => $ item ['quantity ' ],
1432- 'price ' => round ($ item ['price ' ] - $ item ['discount ' ]),
1513+ 'price ' => max ( 0 , round ($ item ['price ' ] - $ item ['discount ' ]) ),
14331514 'tax ' => $ this ->getTaxType ($ item ),
14341515 );
14351516
1436- if (!empty (round ($ item ['weight ' ], 2 ))) {
1437- $ item_params ['dimensions ' ] = array (
1438- 'weight ' => round ($ item ['weight ' ], 2 ),
1439- 'length ' => empty (ceil ($ item ['length ' ])) ? 1 : ceil ($ item ['length ' ]),
1440- 'width ' => empty (ceil ($ item ['width ' ])) ? 1 : ceil ($ item ['width ' ]),
1441- 'height ' => empty (ceil ($ item ['height ' ])) ? 1 : ceil ($ item ['height ' ]),
1442- );
1517+ if (!empty ($ item ['weight ' ])) {
1518+ $ item_params ['dimensions ' ] = $ this ->getItemDimensions ($ item );
14431519 }
14441520
14451521 $ data ['items ' ][] = $ item_params ;
14461522 }
14471523
1448- $ contacts_fields = $ this ->getContactFields ($ shipping_address , $ order , 'contacts ' );
1449- if ($ contacts_fields ) {
1450- $ data ['contacts ' ][0 ]['type ' ] = 'RECIPIENT ' ;
1451- $ data ['contacts ' ][0 ] += $ contacts_fields ;
1524+ if (!empty ($ data ['recipient ' ]['pickupPointId ' ])) {
1525+ $ search_delivery_params = $ this ->getDeliveryOptionsParams ()['params ' ];
1526+ $ search_delivery_params ['to ' ]['pickupPointIds ' ] = [
1527+ $ data ['recipient ' ]['pickupPointId ' ]
1528+ ];
1529+ $ search_delivery_params ['cost ' ]['fullyPrepaid ' ] = (bool )$ order ->paid_datetime ;
1530+ $ search_delivery_params ['deliveryType ' ] = $ delivery_type ;
1531+ $ search_delivery_params ['cost ' ]['manualDeliveryForCustomer ' ] = $ shipping_discount ;
1532+
1533+ // Get missing data in the waOrder
1534+ $ delivery_list = $ this ->apiQuery ('searchDeliveryList ' , $ search_delivery_params );
1535+ foreach ($ delivery_list as $ item ) {
1536+ if (is_array ($ item ['pickupPointIds ' ]) && in_array ($ data ['recipient ' ]['pickupPointId ' ], $ item ['pickupPointIds ' ])) {
1537+ $ data ['deliveryOption ' ] += [
1538+ 'calculatedDeliveryDateMin ' => $ item ['delivery ' ]['calculatedDeliveryDateMin ' ],
1539+ 'calculatedDeliveryDateMax ' => $ item ['delivery ' ]['calculatedDeliveryDateMax ' ],
1540+ ];
1541+ foreach ($ item ['services ' ] as $ service ) {
1542+ if (!isset ($ data ['deliveryOption ' ]['services ' ])) {
1543+ $ data ['deliveryOption ' ]['services ' ] = [];
1544+ }
1545+ $ data ['deliveryOption ' ]['services ' ][] = [
1546+ 'code ' => $ service ['code ' ],
1547+ 'cost ' => $ service ['cost ' ],
1548+ 'customerPay ' => $ service ['customerPay ' ],
1549+ ];
1550+ }
1551+
1552+ $ delivery_for_customer = $ item ['cost ' ]['deliveryForCustomer ' ];
1553+ if (!empty ($ delivery_for_customer )) {
1554+ $ data ['deliveryOption ' ]['deliveryForCustomer ' ] = $ delivery_for_customer ;
1555+ }
1556+ }
1557+ }
14521558 }
14531559
14541560 try {
@@ -1543,7 +1649,7 @@ protected function correctItems()
15431649
15441650 $ this ->setItems ($ items );
15451651 if ($ total_discount > $ total ) {
1546- return $ total_discount - $ total ;
1652+ return $ total_discount ;
15471653 }
15481654 }
15491655
@@ -1932,7 +2038,13 @@ public static function settingSizeControl($name, $params = array())
19322038
19332039 $ radio_params = $ params ;
19342040 $ radio_params ['value ' ] = ifset ($ params ['value ' ]['type ' ]);
1935- $ radio_params ['options ' ] = array ();
2041+ $ radio_params ['options ' ] = array (
2042+ array (
2043+ 'value ' => 'places ' ,
2044+ 'title ' => 'Передавать габариты товаров отдельными позициями ' ,
2045+ 'description ' => '' ,
2046+ )
2047+ );
19362048 if (isset ($ params ['instance ' ])) {
19372049 /** @var yadShipping $instance */
19382050 $ instance = $ params ['instance ' ];
0 commit comments