1: <?php
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: class Eabi_DpdEE_Model_Post extends Eabi_Postoffice_Model_Carrier_Abstract {
50:
51: protected $_code = Eabi_DpdEE_Model_Config::SHIPPING_METHOD_CODE_PARCEL_TERMINAL;
52:
53: 54: 55:
56: const = '-----EABI_DPDEE-----';
57:
58:
59: 60: 61: 62:
63: protected $_tracking_url = 'https://tracking.dpd.de/cgi-bin/delistrack?typ=1&lang=en&pknr=%s';
64:
65:
66:
67: 68: 69: 70: 71: 72:
73: protected function _isAvailable(Mage_Shipping_Model_Rate_Request $request) {
74: $productItem = true;
75: if ($this->getConfigData('checkitems') == 1) {
76: $productItem = true;
77: if ($request->getAllItems()) {
78: foreach ($request->getAllItems() as $item) {
79: $custom = Mage::getModel('catalog/product')->load($item->getProduct()->getId());
80: $desc = $custom->getShortDescription();
81: if (stristr($desc, '<!-- no dpd_ee_module -->')) {
82: $productItem = false;
83: break;
84: }
85: if (stristr($desc, '<!-- no dpd_ee_parcelstore -->')) {
86: $productItem = false;
87: break;
88: }
89: }
90: }
91: }
92: if (!$productItem) {
93: return false;
94: }
95: return true;
96: }
97:
98:
99: 100: 101: 102: 103: 104: 105: 106:
107: public function _calculateAdditionalShippingPrice(Mage_Shipping_Model_Rate_Request $request, $price) {
108: $shippingMatrix = $this->_decodeShippingMatrix($this->getConfigDataForThis('handling_fee_country'));
109: if ($request->getDestCountryId() && isset($shippingMatrix[$request->getDestCountryId()])) {
110:
111: if ($shippingMatrix[$request->getDestCountryId()]['free_shipping_from'] !== '') {
112: if ($request->getPackageValueWithDiscount() >= $shippingMatrix[$request->getDestCountryId()]['free_shipping_from']) {
113: return 0;
114: }
115: }
116:
117: $packageWeight = $request->getPackageWeight() - 0.000001;
118: $weightSet = 10;
119:
120:
121:
122:
123: $extraWeightCost = max(floor($packageWeight / $weightSet) * $shippingMatrix[$request->getDestCountryId()]['kg_price'], 0);
124:
125: $handlingFee = $shippingMatrix[$request->getDestCountryId()]['base_price'];
126: if ($this->getConfigData('handling_action') == 'P') {
127: $handlingFee = ($this->_getOfficeHelper()->getNumberOfPackagesFromItemWeights($request->getEabiProductWeights(), $this->getConfigData('max_package_weight')) - $this->getFreeBoxes()) * $handlingFee;
128: }
129: $handlingFee += $extraWeightCost;
130:
131: return $handlingFee;
132: }
133: return $price;
134: }
135:
136:
137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147:
148: protected function _decodeShippingMatrix($input) {
149: $shippingMatrix = @unserialize($input);
150: $result = array();
151: if (!is_array($shippingMatrix)) {
152: return $result;
153: }
154: foreach ($shippingMatrix as $countryDefinition) {
155: $result[$countryDefinition['country_id']] = $countryDefinition;
156: }
157: return $result;
158: }
159:
160:
161:
162: 163: 164: 165: 166:
167: protected function _getDescription($parcelT) {
168: if (!isset($parcelT['Pudo_worktime']) || !$parcelT['Pudo_worktime']) {
169: return trim($parcelT['Sh_street'] . ' ' . $parcelT['Sh_city'] . ' ' . $parcelT['Sh_postal']. ', ' . $parcelT['Sh_country']. ' '.$parcelT['Sh_phone']);
170: } else {
171: return trim($parcelT['Sh_street'] . ' ' . $parcelT['Sh_city'] . ' ' . $parcelT['Sh_postal']. ', ' . $parcelT['Sh_country']. ' '.$parcelT['Sh_phone']
172: .' '.$this->_getDpdHelper()->getOpeningsDescriptionFromTerminal($parcelT['Pudo_worktime'], Zend_Locale::getLocaleToTerritory(strtoupper($parcelT['Sh_country']))));
173: }
174: }
175:
176:
177:
178:
179: 180: 181: 182: 183:
184: public function isBarcodeFunctionAvailable() {
185:
186: return $this->isAutoSendAvailable();
187: }
188:
189: 190: 191: 192: 193: 194:
195: public function autoSendData(Mage_Sales_Model_Order $order, $selectedOfficeId) {
196: $shippingAddress = $order->getShippingAddress();
197: $selectedOffice = $this->getTerminal($selectedOfficeId);
198: $requestData = array(
199: 'Sh_name' => $shippingAddress->getName(),
200: 'Sh_company' => $shippingAddress->getCompany(),
201: 'Sh_street' => $this->_getStreetFromDescription($selectedOffice),
202: 'Sh_postal' => $selectedOffice->getZipCode(),
203: 'Sh_country' => strtolower($selectedOffice->getCountry()),
204: 'Sh_city' => $selectedOffice->getCity(),
205: 'Sh_contact' => $selectedOffice->getName(),
206: 'Sh_phone' => $this->_getPhoneFromDescription($selectedOffice),
207: 'Po_remark' => $this->_getRemark($order),
208: 'Sh_remark' => '',
209: 'Sh_pudo' => 'true',
210: 'Sh_pudo_id' => $selectedOfficeId,
211: 'Sh_parcel_qty' => $this->_getNumberOfPackagesForOrder($order),
212: 'Sh_cust_reference' => $order->getIncrementId(),
213: );
214:
215:
216: if ($order->getTotalDue() > 0) {
217: $requestData['Sh_cod_amount'] = number_format(round($order->getTotalDue(), 2), 2, '.', '');
218: $requestData['Sh_cod_currency'] = $order->getCurrency();
219: }
220:
221: $phoneNumbers = $this->_getDialCodeHelper()->separatePhoneNumberFromCountryCode($shippingAddress->getTelephone(), $shippingAddress->getCountryId());
222: $requestData['Sh_notify_phone_code'] = $phoneNumbers['dial_code'];
223: $requestData['Sh_notify_contact_phone'] = $phoneNumbers['phone_number'];
224: $requestResult = $this->_getDpdHelper()->getApi($this->getStore(), $this->_code)
225: ->autoSendData($requestData);
226:
227: $this->_setDataToOrder($order, $requestResult);
228:
229:
230:
231: return array('barcode' => '##'. implode(',', $requestResult['Parcel_numbers']).'##');
232: }
233:
234:
235:
236: 237: 238: 239: 240:
241: protected function ($order) {
242: return '';
243: }
244:
245:
246: 247: 248: 249: 250:
251: public function isDataSent(Mage_Sales_Model_Order $order) {
252: $orderData = $this->getDataFromOrder($order);
253: if (isset($orderData['DPD_OrderID'])) {
254: return true;
255: }
256: return false;
257: }
258:
259: 260: 261: 262: 263:
264: public function getBarcode(Mage_Sales_Model_Order $order) {
265: if (!$this->isBarcodeFunctionAvailable()) {
266: return false;
267: }
268:
269: $orderData = $this->getDataFromOrder($order);
270: if (isset($orderData['PDF_URL']) && $orderData['PDF_URL']) {
271: return $orderData['PDF_URL'];
272: }
273: return false;
274: }
275:
276: 277: 278: 279: 280:
281: public function getBarcodePdf(Mage_Sales_Model_Order $order) {
282: $orderData = $this->getDataFromOrder($order);
283: if (isset($orderData['PDF_URL']) && $orderData['PDF_URL']) {
284: return file_get_contents(urldecode($orderData['PDF_URL']));
285: }
286: }
287:
288:
289:
290:
291: 292: 293: 294: 295:
296: public function getDataFromOrder(Mage_Sales_Model_Order $order) {
297: return $this->_getOfficeHelper()->getDataFromOrder($order, self::ORDER_COMMENT_START_PREFIX);
298: }
299:
300: 301: 302: 303: 304: 305:
306: public function _setDataToOrder(Mage_Sales_Model_Order $order, $data = array()) {
307: return $this->_getOfficeHelper()->setDataToOrder($order, $data, self::ORDER_COMMENT_START_PREFIX);
308: }
309:
310:
311:
312:
313:
314: 315: 316: 317: 318:
319: public function getOfficeList() {
320: $body = $this->_getDpdHelper()->getApi($this->getStore(), $this->_code)->getOfficeList();
321: if (!$body || !is_array($body) || !isset($body['data'])) {
322: return false;
323: }
324: $result = array();
325: foreach ($body['data'] as $remoteParcelTerminal) {
326: $result[] = array(
327: 'place_id' => $remoteParcelTerminal['Sh_pudo_id'],
328: 'name' => $remoteParcelTerminal['Pudo_name'],
329: 'city' => trim($remoteParcelTerminal['Sh_city']),
330: 'county' => '',
331: 'description' => $this->_getDescription($remoteParcelTerminal),
332: 'country' => $remoteParcelTerminal['Sh_country'],
333: 'zip' => $remoteParcelTerminal['Sh_postal'],
334: 'group_sort' => $this->getGroupSort($remoteParcelTerminal['Sh_city']),
335: );
336:
337: }
338: if (count($result) == 0) {
339: return false;
340: }
341: return $result;
342: }
343:
344:
345:
346: 347: 348: 349: 350: 351: 352:
353: public function getTerminalTitle(Eabi_Postoffice_Model_Office $office) {
354: if ($this->getConfigData('shortname')) {
355: return htmlspecialchars($office->getName());
356: }
357: return htmlspecialchars($office->getName() . ' (' . $office->getDescription().')');
358: }
359:
360:
361: 362: 363: 364: 365: 366: 367:
368: public function getAdminTerminalTitle(Eabi_Postoffice_Model_Office $office) {
369: if ($this->getConfigData('shortname')) {
370: return htmlspecialchars($office->getGroupName().' - '.$office->getName());
371: }
372: return htmlspecialchars($office->getGroupName().' - '.$office->getName() . ' ' . $office->getDescription());
373: }
374:
375:
376: 377: 378: 379: 380:
381: private function _getStreetFromDescription(Eabi_Postoffice_Model_Office $selectedOffice) {
382: $zip = $selectedOffice->getZipCode();
383: $encoding = 'UTF-8';
384: return trim(mb_substr($selectedOffice->getDescription(), 0, mb_strpos($selectedOffice->getDescription(), $zip, 0, $encoding), $encoding));
385: }
386:
387: 388: 389: 390: 391:
392: private function _getPhoneFromDescription(Eabi_Postoffice_Model_Office $selectedOffice) {
393: $zip = $selectedOffice->getZipCode();
394: $country = $selectedOffice->getCountry();
395: $matches = array();
396: $isMatched = preg_match('/(?s:[\+][0-9]+)/', $selectedOffice->getDescription(), $matches);
397: if ($isMatched) {
398: return $matches[0];
399: }
400: return '';
401: }
402:
403:
404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414:
415: public function getGroupSort($group_name) {
416: $group_name = trim(strtolower($group_name));
417: $sorts = array(
418:
419: 'tallinn' => 20,
420: 'tartu' => 19,
421: 'pärnu' => 18,
422:
423:
424: 'riga' => 20,
425: 'daugavpils' => 19,
426: 'liepaja' => 18,
427: 'jelgava' => 17,
428: 'jurmala' => 16,
429:
430:
431:
432: 'vilnius' => 20,
433: 'kaunas' => 19,
434: 'klaipeda' => 18,
435: 'siauliai' => 17,
436: 'alytus' => 16,
437:
438: );
439: if (isset($sorts[$group_name]) && $this->getConfigData('sort_offices')) {
440: return $sorts[$group_name];
441: }
442: if (strpos($group_name, '/') > 0 && $this->getConfigData('sort_offices')) {
443: return 0;
444: }
445: return 0;
446: }
447:
448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458:
459: public function isPickedUpByCourier(Mage_Sales_Model_Order $order) {
460: if (!$this->getConfigData('courier_enable')) {
461: return null;
462: }
463: $orderData = $this->getDataFromOrder($order);
464: if (isset($orderData['courier_call_id']) && $orderData['courier_call_id']) {
465: return true;
466: }
467: return false;
468: }
469:
470:
471: 472: 473: 474: 475: 476:
477: protected function _getNumberOfPackagesForOrder(Mage_Sales_Model_Order $order) {
478: $productWeights = array();
479: foreach ($order->getAllVisibleItems() as $orderItem) {
480:
481: for ($i = 0; $i < ($orderItem->getQtyOrdered() - $orderItem->getQtyRefunded()); $i++) {
482: $productWeights[] = $orderItem->getWeight();
483: }
484:
485: }
486: return $this->_getOfficeHelper()->getNumberOfPackagesFromItemWeights($productWeights, $this->getConfigData('max_package_weight'));
487: }
488:
489:
490: 491: 492: 493: 494:
495: public function getConfigDataForThis($field) {
496: if (empty($this->_code)) {
497: return false;
498: }
499: $path = 'carriers/'.$this->_code.'/'.$field;
500: return Mage::getStoreConfig($path, $this->getStore());
501: }
502:
503: 504: 505: 506: 507:
508: public function getConfigData($field) {
509: if (empty($this->_code)) {
510: return false;
511: }
512: return $this->_getConfigDataOverride($field, $this->_code);
513: }
514:
515: 516: 517: 518: 519: 520: 521:
522: protected function _getConfigDataOverride($field, $code) {
523: if ($field == 'senddata_event') {
524: $timeout = Mage::getStoreConfig('carriers/'.$code.'/http_request_timeout', $this->getStore());
525: if ($timeout > 10) {
526: return 'manual';
527: }
528: }
529: $path = 'carriers/'.$code.'/'.$field;
530: return Mage::getStoreConfig($path, $this->getStore());
531: }
532:
533:
534: 535: 536: 537: 538:
539: protected function _applyPriceFromCod(Mage_Shipping_Model_Rate_Request $request) {
540:
541: if ($this->_getQuote() && !$this->_getQuote()->isVirtual() && Mage::getStoreConfig('payment/dpdcodpayment/active') && $this->_getQuote()->getPayment() && $this->_getQuote()->getPayment()->getMethod() && $this->_getQuote()->getPayment()->getMethod() == Eabi_DpdEE_Model_Config::PAYMENT_METHOD_COD && strpos($this->_getQuote()->getShippingAddress()->getShippingMethod(), $this->getCarrierCode()) === 0) {
542:
543: $shippingMatrix = $this->_decodeShippingMatrix($this->getConfigDataForThis('handling_fee_country'));
544: if ($request->getDestCountryId() && isset($shippingMatrix[$request->getDestCountryId()])) {
545:
546: if (isset($shippingMatrix[$request->getDestCountryId()]['cod_fee']) && $shippingMatrix[$request->getDestCountryId()]['cod_fee'] !== '') {
547: $codFee = (float) str_replace(',', '.', $shippingMatrix[$request->getDestCountryId()]['cod_fee']);
548: if ($codFee >= 0) {
549: return $codFee;
550: }
551: }
552: }
553: }
554: return 0;
555: }
556:
557: 558: 559: 560: 561: 562: 563:
564: public function getCodFee($address) {
565: if ($address->getCountryId()) {
566:
567:
568: $shippingMatrix = $this->_decodeShippingMatrix($this->getConfigDataForThis('handling_fee_country'));
569: if ($address->getCountryId() && isset($shippingMatrix[$address->getCountryId()])) {
570:
571: if (isset($shippingMatrix[$address->getCountryId()]['cod_fee']) && $shippingMatrix[$address->getCountryId()]['cod_fee'] !== '') {
572: $codFee = (float) str_replace(',', '.', $shippingMatrix[$address->getCountryId()]['cod_fee']);
573: if ($codFee >= 0) {
574: return $codFee;
575: }
576: }
577: }
578:
579: }
580:
581: return false;
582: }
583:
584:
585: 586: 587: 588: 589: 590:
591: public function isCodEnabled($address) {
592: $shippingMatrix = $this->_decodeShippingMatrix($this->getConfigDataForThis('handling_fee_country'));
593: if ($address->getCountryId() && isset($shippingMatrix[$address->getCountryId()]) && isset($shippingMatrix[$address->getCountryId()]['cod_fee'])
594: && $shippingMatrix[$address->getCountryId()]['cod_fee'] !== '') {
595: $codFee = (double) str_replace(',', '.', $shippingMatrix[$address->getCountryId()]['cod_fee']);
596: if ($codFee >= 0) {
597: return true;
598: }
599: }
600: return false;
601: }
602:
603: 604: 605: 606:
607: protected function _getDpdHelper() {
608: return Mage::helper('eabi_dpdee');
609: }
610:
611:
612:
613:
614: }
615:
616: