PluginProbe
MBE eShip / trunk
MBE eShip vtrunk
2.8.1 trunk 1.0.0 1.1.0 1.1.3 1.2.1 1.2.2 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.6.0 1.7.0 1.7.1 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1.0 2.1.1 2.1.2 2.2.1 All 37 releases
mail-boxes-etc / lib / Model / Ws.php

Ws.php in MBE eShip trunk, at lib/Model/Ws.php

530 lines 19.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Mbe_Shipping_Model_Ws
4 {
5 private $helper;
6 protected $logger = null;
7 private $ws;
8 protected $wsUrl;
9 protected $wsUsername;
10 protected $wsPassword;
11 protected $system;
12 protected $customer;
13
14 public function __construct()
15 {
16 $this->helper = new Mbe_Shipping_Helper_Data();
17 $this->logger = new Mbe_Shipping_Helper_Logger();
18
19 $debug = $this->helper->debug();
20 $this->ws = new MbeWs($debug);
21 $this->wsUrl = $this->helper->getWsUrl();
22 $this->wsUsername = $this->helper->getWsUsername();
23 $this->wsPassword = $this->helper->getWsPassword();
24 $this->system = $this->helper->getCountry();
25 $this->customer = $this->getCustomer();
26
27 // if ($debug) {
28 // $this->helper->checkDir($this->helper->getMbeLogDir());
29 // }
30 }
31
32 public function getCustomer()
33 {
34 $cacheKey = md5($this->wsUrl . $this->wsUsername . $this->wsPassword . $this->system);
35 if (get_transient($cacheKey) !== false) {
36 return get_transient($cacheKey);
37 }
38 return $this->cacheCustomerData();
39 }
40
41 public function getCustomerPermission($permissionName)
42 {
43 $result = null;
44 if ($this->customer) {
45 $result = $this->customer->Permissions->$permissionName??null;
46 }
47
48 return $result;
49 }
50
51 public function getAvailableOptions()
52 {
53 $result = false;
54 if ($this->wsUrl && $this->wsUsername && $this->wsPassword) {
55 $result = $this->ws->getAvailableOptions($this->wsUrl, $this->wsUsername, $this->wsPassword);
56 }
57
58 return $result;
59 }
60
61 public function getAllowedShipmentServices($customer)
62 {
63
64
65 $result = array();
66 if ($this->wsUrl && $this->wsUsername && $this->wsPassword) {
67 //$customer = $this->ws->getCustomer($this->wsUrl, $this->wsUsername, $this->wsPassword, $this->system);
68 if ($customer && $customer->Enabled) {
69 if (isset($customer->Permissions->enabledServices)) {
70 $enabledServices = $customer->Permissions->enabledServices;
71 $enabledServicesDesc = $customer->Permissions->enabledServicesDesc;
72
73 $enabledServicesArray = explode(",", $enabledServices);
74 $enabledServicesDescArray = explode(",", $enabledServicesDesc);
75
76 for ($i = 0; $i < count($enabledServicesArray); $i++) {
77 $service = $enabledServicesArray[$i];
78 $serviceDesc = $enabledServicesDescArray[$i];
79
80
81 $serviceDesc .= ' (' . $service . ')';
82
83 $currentShippingType = array(
84 'value' => $service,
85 'label' => $serviceDesc,
86 );
87
88 if (!in_array($currentShippingType, $result)) {
89 array_push($result, $currentShippingType);
90 }
91
92 //SHIPPING WITH INSURANCE
93 if (isset($customer->Permissions->canSpecifyInsurance) && $customer->Permissions->canSpecifyInsurance) {
94 $currentShippingWithInsuranceType = array(
95 'value' => $this->helper->convertShippingCodeWithInsurance($service),
96 'label' => $this->helper->convertShippingLabelWithInsurance($serviceDesc),
97 );
98 if (!in_array($currentShippingWithInsuranceType, $result)) {
99 $result[] = $currentShippingWithInsuranceType;
100 }
101 }
102
103 //SHIPPING WITH Safe Value
104 if (isset($customer->Permissions->canSpecifyMBESafeValue) && $customer->Permissions->canSpecifyMBESafeValue) {
105 $currentShippingWithSafeValueType = array(
106 'value' => $this->helper->convertShippingCodeWithSafeValue($service),
107 'label' => $this->helper->convertShippingLabelWithSafeValue($serviceDesc),
108 );
109 $currentShippingWithSafeValueArtType = array(
110 'value' => $this->helper->convertShippingCodeWithSafeValueArt($service),
111 'label' => $this->helper->convertShippingLabelWithSafeValueArt($serviceDesc),
112 );
113 if (!in_array($currentShippingWithSafeValueType, $result)) {
114 $result[] = $currentShippingWithSafeValueType;
115 }
116 if (!in_array($currentShippingWithSafeValueArtType, $result)) {
117 $result[] = $currentShippingWithSafeValueArtType;
118 }
119 }
120
121 //SHIPPING WITH Safe Value 4 Business
122 if (isset($customer->Permissions->canSpecifyMBESafeValue4Business) && $customer->Permissions->canSpecifyMBESafeValue4Business) {
123 $currentShippingWithSafeValue4BusinessType = array(
124 'value' => $this->helper->convertShippingCodeWithSafeValue4Business($service),
125 'label' => $this->helper->convertShippingLabelWithSafeValue4Business($serviceDesc),
126 );
127 if (!in_array($currentShippingWithSafeValue4BusinessType, $result)) {
128 $result[] = $currentShippingWithSafeValue4BusinessType;
129 }
130 }
131
132 }
133 }
134 }
135 }
136 return $result;
137 }
138
139 public function getLabelFromShipmentType($shipmentCode)
140 {
141 $result = $shipmentCode;
142 $allowedShipmentServices = $this->getAllowedShipmentServices($this->customer);
143 foreach ($allowedShipmentServices as $allowedShipmentService) {
144 if ($allowedShipmentService["value"] == $shipmentCode) {
145 $result = $allowedShipmentService["label"];
146 break;
147 }
148 }
149 return $result;
150 }
151
152 public function getDepartmentsAddresses() {
153 $result=[];
154 if ($this->wsUrl && $this->wsUsername && $this->wsPassword) {
155 $result = $this->ws->getDepartmentsAddress($this->wsUrl, $this->wsUsername, $this->wsPassword);
156 }
157 return $result;
158 }
159
160 private function convertInsuranceShipping($shippingList, $insuranceValue)
161 {
162 $result = false;
163 if ($shippingList) {
164 $newShippingList = array();
165 $selectedInsuranceCode = $this->helper->getSelectedInsuranceCode();
166 $selectedInsuranceLabel = $this->helper->getSelectedInsuranceLabel();
167 $insuranceThreshold = $this->helper->getShipmentsCsvInsuranceMin();
168
169 switch ($selectedInsuranceCode) {
170 case Mbe_Shipping_Helper_Data::MBE_SHIPPING_WITH_SAFE_VALUE_CODE_SUFFIX:
171 case Mbe_Shipping_Helper_Data::MBE_SHIPPING_WITH_SAFE_VALUE_ART_CODE_SUFFIX:
172 $insuranceAvailable = 'MBESafeValueAvailable';
173 break;
174 case Mbe_Shipping_Helper_Data::MBE_SHIPPING_WITH_SAFE_VALUE_4B_CODE_SUFFIX:
175 $insuranceAvailable = 'MBESafeValue4BusinessAvailable';
176 break;
177 default:
178 $insuranceAvailable = 'InsuranceAvailable';
179 break;
180 }
181
182 foreach ($shippingList as $shipping) {
183
184 if ($shipping->$insuranceAvailable && $insuranceValue >= $insuranceThreshold ) {
185 $newShipping = $shipping;
186 $newShipping->Service = $newShipping->Service . $selectedInsuranceCode;
187 $newShipping->ServiceDesc = $newShipping->ServiceDesc . $selectedInsuranceLabel;
188 $newShippingList[] = $newShipping;
189 }
190 }
191 if (!empty($newShippingList)) {
192 $result = $newShippingList;
193 }
194 }
195 return $result;
196 }
197
198 public function estimateShipping($country, $region, $city, $postCode, $weight, $boxes, $insuranceValue, $products, $insuranceCode)
199 {
200 $this->logger->log('ESTIMATESHIPPING');
201 // $weight = $this->helper->convertWeight($weight);
202
203 $wsUrl = $this->helper->getWsUrl();
204 $wsUsername = $this->helper->getWsUsername();
205 $wsPassword = $this->helper->getWsPassword();
206 $system = $this->helper->getCountry();
207
208 $result = [];
209
210 if ($wsUrl && $wsUsername && $wsPassword) {
211 $items = $this->setItems($weight);
212
213 $shipmentType = $this->helper->getDefaultShipmentType();
214
215 $this->logger->logVar($items, 'ESTIMATESHIPPING ITEMS');
216
217 //Shipping without insurance
218 $resultWithoutInsurance = $this->ws->estimateShipping(
219 $wsUrl, $wsUsername, $wsPassword, $shipmentType, $system, $country, $region, $city, $postCode, $items, $products, false, $insuranceValue
220 );
221
222 //Shipping with insurance
223 $resultWithInsurance = $this->ws->estimateShipping(
224 $wsUrl, $wsUsername, $wsPassword, $shipmentType, $system, $country, $region, $city, $postCode, $items, $products, true, $insuranceValue, $insuranceCode
225 );
226 $resultWithInsurance = $this->convertInsuranceShipping($resultWithInsurance, $insuranceValue);
227
228 if ($resultWithInsurance && $resultWithoutInsurance) {
229 $result = array_merge($resultWithInsurance, $resultWithoutInsurance);
230 }
231 else {
232 if ($resultWithInsurance) {
233 $result = $resultWithInsurance;
234 }
235 if ($resultWithoutInsurance) {
236 $result = $resultWithoutInsurance;
237 }
238 }
239
240 // Remove multiple MOL delivery point services
241 foreach ( $result as $key=>$value ) {
242 if(in_array($value->Service, MBE_ESTIMATE_DELIVERY_POINT_SERVICES) ){
243 unset($result[$key]);
244 }
245 }
246 // Add common delivery point service
247 if(is_array($resultWithoutInsurance)) {
248 $result[] = $this->helper->getDeliveryPointServices($resultWithoutInsurance);
249 $result = array_values(array_filter($result)); //Clean empty array items and reindex (remove null deliverypointservices)
250 }
251 }
252
253 if (empty($result)) {$result = false;} // to keep result retro-compatible
254 return $result;
255 }
256
257 /**
258 * @throws \MbeExceptions\ApiRequestException
259 */
260 public function createShipping($country, $region, $postCode, $weight, $boxes, $products, $service, $subzone, $notes, $firstName, $lastName, $companyName, $address, $phone, $city, $email, $goodsValue = 0.0, $reference = "", $isCod = false, $codValue = 0.0, $insurance = false, $insuranceValue = 0.0, $isPickup = false, $senderInfo = [], $pickupData = [], $insuranceCode = null)
261 {
262 // $weight = $this->helper->convertWeight($weight, 'kg');
263 $this->logger->log('CREATE SHIPPING');
264 $this->logger->logVar($goodsValue, 'GOODS VALUE');
265 $this->logger->logVar(func_get_args(), 'CREATE SHIPPING ARGS');
266
267 $wsUrl = $this->helper->getWsUrl();
268 $wsUsername = $this->helper->getWsUsername();
269 $wsPassword = $this->helper->getWsPassword();
270 $system = $this->helper->getCountry();
271 $result = false;
272 if ($wsUrl && $wsUsername && $wsPassword) {
273 $items = $this->setItems($weight);
274
275 $shipmentType = $this->helper->getDefaultShipmentType();
276
277 $this->logger->logVar($items, 'CREATE SHIPPING ITEMS');
278
279 $shipperType = $this->getShipperType();
280 if ($isPickup) {
281 $result = $this->ws->createPickupShipping(
282 $wsUrl, $wsUsername, $wsPassword, $shipmentType, $service, $subzone, $system, $notes, $firstName, $lastName, $companyName,
283 $address, $phone, $city, $region, $country, $postCode, $email, $items, $products, $shipperType, $goodsValue, $reference,
284 $isCod, $codValue, $insurance, $insuranceValue, $senderInfo, $pickupData, $insuranceCode
285 );
286 } else {
287 $result = $this->ws->createShipping(
288 $wsUrl, $wsUsername, $wsPassword, $shipmentType, $service, $subzone, $system, $notes, $firstName, $lastName, $companyName,
289 $address, $phone, $city, $region, $country, $postCode, $email, $items, $products, $shipperType, $goodsValue, $reference,
290 $isCod, $codValue, $insurance, $insuranceValue, $insuranceCode
291 );
292 }
293 }
294 return $result;
295 }
296 public function getShipperType()
297 {
298 $shipperType = "MBE";
299 $canCreateCourierWaybill= $this->helper->getCanCreateCourierWaybill()??0;
300
301 if ($canCreateCourierWaybill) {
302 $shipperType = "COURIERLDV";
303 }
304
305 return $shipperType;
306 }
307
308 public function mustCloseShipments()
309 {
310 $canCreateCourierWaybill= $this->helper->getCanCreateCourierWaybill()??false;
311
312 return !$canCreateCourierWaybill;
313 }
314
315 public function getCustomerMaxParcelWeight()
316 {
317 return $this->customer->Permissions->maxParcelWeight;
318 }
319
320 public function getCustomerMaxShipmentWeight()
321 {
322 return $this->customer->Permissions->maxShipmentWeight;
323 }
324
325 public function isCustomerActive()
326 {
327 $result = false;
328 if ($this->customer) {
329 $result = $this->customer->Enabled;
330 }
331 return $result;
332 }
333
334 public function closeShipping(array $shipmentIds)
335 {
336 $trackingNumbers = array();
337 foreach ($shipmentIds as $shipmentId) {
338 $tracks = $this->helper->getTrackings($shipmentId);
339 foreach ($tracks as $track) {
340 array_push($trackingNumbers, $track);
341 }
342 }
343 $this->closeTrackingNumbers($trackingNumbers);
344
345 }
346
347 public function closeTrackingNumbers(array $trackingNumbers)
348 {
349 $this->logger->log('CLOSE SHIPPING');
350
351
352
353 $result = false;
354
355 if ($this->wsUrl && $this->wsUsername && $this->wsPassword) {
356 $result = $this->ws->closeShipping($this->wsUrl, $this->wsUsername, $this->wsPassword, $this->system, $trackingNumbers);
357
358 if ($result) {
359 foreach ($trackingNumbers as $trackingNumber) {
360 $filePath = $this->helper->getTrackingFilePath($trackingNumber);
361 file_put_contents($filePath, $result->Pdf);
362 }
363 }
364 }
365
366 return $result;
367 }
368
369 public function cacheCustomerData()
370 {
371 $result = false;
372 if ( $this->wsUrl && $this->wsUsername && $this->wsPassword ) {
373 $result = $this->ws->getCustomer( $this->wsUrl, $this->wsUsername, $this->wsPassword, $this->system );
374 if ( $result ) {
375 set_transient(
376 md5( $this->wsUrl . $this->wsUsername . $this->wsPassword . $this->system ),
377 $result,
378 12 * HOUR_IN_SECONDS
379 );
380 }
381 $this->logger->logVar( $result, 'WS getCustomer' );
382 }
383
384 return $result;
385 }
386
387 public function setItems($boxesWeight)
388 {
389 $items = [];
390 foreach ($boxesWeight as $box) {
391 foreach ($box['weight'] as $weight) {
392 $item = new \stdClass;
393 $item->Weight = $this->helper->convertWeight($weight);
394 $item->Dimensions = new \stdClass;
395 $item->Dimensions->Lenght = $box['dimensions']['length'];
396 $item->Dimensions->Height = $box['dimensions']['height'];
397 $item->Dimensions->Width = $box['dimensions']['width'];
398 $items[] = $item;
399 }
400 }
401 return $items;
402 }
403
404 public function returnShipping($toReturnIds)
405 {
406 $result = [];
407 foreach ($toReturnIds as $toReturnId) {
408 $tracks = $this->helper->getTrackings($toReturnId);
409 if (!empty($tracks)) {
410 // foreach ($tracks as $track) {
411 // array_push($trackingNumbers, $track);
412 // }
413 // return $this->returnTrackingNumbers($trackingNumbers);
414 $result[$toReturnId] = $this->returnTrackingNumbers([$tracks[0]]); // TODO : only manage the 1st order's shipment for now
415 }
416 }
417 return $result;
418 }
419
420 public function returnTrackingNumbers(array $trackingNumbers)
421 {
422 $this->logger->logVar( $trackingNumbers, 'RETURN SHIPPING ');
423
424 $returnTracking = [];
425
426 if ($this->wsUrl && $this->wsUsername && $this->wsPassword) {
427 foreach ( $trackingNumbers as $trackingNumber ) {
428 $result = $this->ws->returnShipping($this->wsUrl, $this->wsUsername, $this->wsPassword, $this->system, $trackingNumber);
429 if (!empty($result)) {
430 $returnTracking[$trackingNumber] = $result->MasterTrackingMBE;
431 } else {
432 // error on creation of the return shipment
433 $returnTracking[$trackingNumber] = false;
434 }
435 }
436 }
437 return $returnTracking;
438 }
439
440 /**
441 * @throws SoapFault
442 * @throws \MbeExceptions\ValidationException
443 * @throws \MbeExceptions\ApiRequestException
444 */
445 public function advancedReturnShipping($toReturnId, $returnData)
446 {
447 $tracking = $this->helper->getTrackings($toReturnId);
448 if (empty($tracking)) {
449 throw new \MbeExceptions\ValidationException(__('Please select only one shipped order' , 'mail-boxes-etc'));
450 }
451
452 return $this->ws->advancedReturnShipping($toReturnId, $this->wsUrl, $this->wsUsername, $this->wsPassword, $this->system, $this->getShipperType(), $returnData);
453 }
454
455 public function getShipmentItems( $toReturnId ) {
456 $result = [];
457 $tracking = $this->helper->getTrackings($toReturnId)[0];
458 if (!empty($tracking)) {
459 $result = $this->ws->getShipmentItems($this->wsUrl, $this->wsUsername, $this->wsPassword, $this->system, $tracking);
460 }
461
462 return $result;
463
464 }
465
466 public function getPickupDefaultData() {
467 return $this->ws->getPickupDefaultData($this->wsUrl, $this->wsUsername, $this->wsPassword, $this->system);
468 }
469
470 public function setPickupDefaultData(array $data) {
471 return $this->ws->setPickupDefaultData($this->wsUrl, $this->wsUsername, $this->wsPassword, $this->system, $data);
472 }
473
474 public function getPickupAddressesData() {
475 return $this->ws->getCustomerPickupAddresses($this->wsUrl, $this->wsUsername, $this->wsPassword, $this->system);
476 }
477
478 /**
479 * @throws \MbeExceptions\ApiRequestException
480 */
481 public function addPickupAddressData(array $address) {
482 return $this->ws->createPickupAddress($this->wsUrl, $this->wsUsername, $this->wsPassword, $this->system, $address);
483 }
484
485 /**
486 * @throws \MbeExceptions\ApiRequestException
487 */
488 public function updatePickupAddressData(array $address) {
489 return $this->ws->updatePickupAddress($this->wsUrl, $this->wsUsername, $this->wsPassword, $this->system, $address);
490 }
491
492 public function deletePickupAddressData($addressId) {
493 return $this->ws->deletePickupAddress($this->wsUrl, $this->wsUsername, $this->wsPassword, $this->system, $addressId);
494 }
495
496 public function getPickupManifest( $masterTrackingId ) {
497 return $this->ws->getPickupManifest($this->wsUrl, $this->wsUsername, $this->wsPassword, $this->system, $masterTrackingId);
498 }
499
500 public function getDefaultPickupAddress() {
501 return $this->ws->getDefaultPickupAddress($this->wsUrl, $this->wsUsername, $this->wsPassword, $this->system);
502 }
503
504 public function getPickupAddressById($id) {
505 return $this->ws->getPickupAddressById($this->wsUrl, $this->wsUsername, $this->wsPassword, $this->system, $id);
506 }
507
508 /**
509 * @throws \MbeExceptions\ApiRequestException
510 */
511 public function closePickupShippping($pickupBatchid, $pickupBatchData) {
512 return $this->ws->closePickupShippping($this->wsUrl, $this->wsUsername, $this->wsPassword, $this->system, $pickupBatchid, $pickupBatchData);
513 }
514
515 /**
516 * @throws \MbeExceptions\ShippingDocumentException
517 */
518 public function getDeliveryPointShippingDocument( $masterTrackingId ) {
519 return $this->ws->getShippingDocument($this->wsUrl, $this->wsUsername, $this->wsPassword, $this->system, $masterTrackingId);
520 }
521
522 /**
523 * @throws \MbeExceptions\ApiRequestException
524 * @throws SoapFault
525 */
526 public function sendRestApiCredentials( $apiKey ) {
527 return $this->ws->sendRestApiCredentials($this->wsUrl, $this->wsUsername, $this->wsPassword, $this->system, $apiKey, get_rest_url(null, MBE_WC_REST_API_NAMESPACE.MBE_REST_API_ENDPOINT_SET_SHIPPING_STATUS));
528 }
529
530 }