/** * remove the shipment tracking code form DPD system * after this action pdf content will be set to null * but the shipment will still exist in magento admin interface * * @param $shipment * @param $saveShipmentResponse * * @return mixed */ public function deleteWsShipment($shipment, $saveShipmentResponse) { $apiParams = $this->getShipmentParams($shipment->getStore()); $apiParams['method'] = Zitec_Dpd_Api_Configs::METHOD_SHIPMENT_DELETE; $dpdApi = new Zitec_Dpd_Api($apiParams); $deleteShipment = $dpdApi->getApiMethodObject(); $deleteShipment->addShipmentReference($saveShipmentResponse->getDpdShipmentId(), $saveShipmentResponse->getDpdShipmentReferenceNumber()); $deleteShipment->execute(); $wsResult = $deleteShipment->getDeleteShipmentResponse(); return $wsResult; }
/** * * @param array|int|null $shipmentIds * * @return boolean */ public function createManifestForShipments($shipmentIds) { $this->_clearNotifications(); if ($shipmentIds && is_numeric($shipmentIds)) { $shipmentIds = array($shipmentIds); } if (!$shipmentIds || !is_array($shipmentIds)) { $message = $this->_getHelper()->__("Please select the shipments to include in the closed manifest."); $this->_addNotification($message); return false; } $shipments = Mage::getResourceModel('sales/order_shipment_collection'); /* @var $shipments Mage_Sales_Model_Resource_Order_Shipment_Collection */ $shipments->addFieldToFilter('entity_id', array("in" => $shipmentIds)); $ships = Mage::getResourceModel('zitec_dpd/dpd_ship_collection'); /* @var $ships Zitec_Dpd_Model_Mysql4_Dpd_Ship_Collection */ $ships->filterByShipmentIds($shipmentIds); $shipmentsForManifest = array(); $shipsForManifest = array(); $manifestParams = null; foreach ($shipments as $shipment) { /* @var $shipment Mage_Sales_Model_Order_Shipment */ if (!$this->_getHelper()->isShippingMethodDpd($shipment->getOrder()->getShippingMethod())) { $message = "Could not include shipment %s in the manifest because it is not a DPD shipment."; $this->_addNotification($this->_getHelper()->__($message, $shipment->getIncrementId()), true); continue; } $ship = $ships->findByShipmentId($shipment->getId()); /* @var $ship Zitec_Dpd_Model_Dpd_Ship */ if (!$ship) { $message = "Could not include shipment %s in the manifest because it was not communicated to DPD."; $this->_addNotification($this->_getHelper()->__($message, $shipment->getIncrementId()), true); continue; } if ($this->_getHelper()->isCancelledWithDpd($shipment)) { $message = "Could not include shipment %s in the manifest because it was cancelled with DPD."; $this->_addNotification($this->_getHelper()->__($message, $shipment->getIncrementId()), true); continue; } if ($ship->getManifestId()) { $message = "Could not include shipment %s in the manifest because it is already in a manifest."; $this->_addNotification($this->_getHelper()->__($message, $shipment->getIncrementId()), true); continue; } $storeManifestParams = $this->_getWsHelper()->getManifestParams($shipment->getOrder()->getStore()); if (isset($manifestParams) && $storeManifestParams != $manifestParams) { $this->_clearNotifications(); $message = "The shipments you selected come from different stores which have different DPD connection parameters. Please select shipments which belong " . "to stores that use the same DPD connection parameters."; $this->_addNotification($this->_getHelper()->__($message)); return false; } else { $manifestParams = $storeManifestParams; } $this->_addNotification($this->_getHelper()->__('Shipment %s was added to the manifest.', $shipment->getIncrementId())); $shipmentsForManifest[] = $shipment; $shipsForManifest[] = $ship; } if (count($shipmentsForManifest) == 0) { $this->_clearNotificationErrorStyles(); $this->_addNotification("None of the shipments selected could be included in a manifest."); return false; } $manifestParams['method'] = Zitec_Dpd_Api_Configs::METHOD_MANIFEST_CLOSE; $dpdApi = new Zitec_Dpd_Api($manifestParams); $closeManifest = $dpdApi->getApiMethodObject(); foreach ($shipmentsForManifest as $index => $shipment) { /* @var $shipment Mage_Sales_Model_Order_Shipment */ $ship = $shipsForManifest[$index]; /* @var $ship Zitec_Dpd_Model_Dpd_Ship */ $shipResponse = unserialize($ship->getSaveShipmentResponse()); /* @var $response Zitec_Dpd_Api_Shipment_Save_Response */ $closeManifest->addShipment($shipResponse->getDpdShipmentId(), $shipResponse->getDpdShipmentReferenceNumber()); if ($index == 0) { $closeManifest->setManifestReferenceNumber($shipResponse->getDpdShipmentReferenceNumber()); } } try { $closeManifest->execute(); } catch (Exception $e) { $this->_clearNotifications(); $this->_addNotification($this->_getHelper()->__("An error occurred whilst requesting the manifest from DPD: %s", $e->getMessage())); return false; } $response = $closeManifest->getCloseManifestResponse(); if ($response->hasError()) { $this->_clearNotifications(); $this->_addNotification($this->_getHelper()->__('An error occurred whilst communicating the manifest details to DPD. DPD says: "%s"', $response->getErrorText())); return false; } $this->setManifestRef($response->getManifestReferenceNumber()); $this->setManifestDpdId($response->getManifestId()); $this->setManifestDpdName($response->getManifestName()); $this->setPdf(base64_encode($response->getPdfFile())); $this->setShipsForManifest($shipsForManifest); try { $this->save(); } catch (Exception $e) { $this->_clearNotificationErrorStyles(); $message = "The manifest was communicated successfully to DPD, but an error occurred whilst saving the manifest details in Magento. <br />" . "Please make a capture of this screen so that you have a record of the shipments included in the manifest. <br />" . "For you reference when communicating with DPD, the manifest details are: <br /> " . "Manifest Reference: %s <br />" . "Manifest Name: %s <br />" . "Manifest internal DPD ID: %s <br />" . "The error message returned was: %s"; $this->_addNotification($this->_getHelper()->__($message, $this->getManifestRef(), $this->getManifestDpdId(), $this->getManifestDpdName(), $e->getMessage())); $this->_getHelper()->log(sprintf($message, $this->getManifestRef(), $this->getManifestDpdId(), $this->getManifestDpdName(), $e->getMessage())); return false; } return true; }
/** * * Create a pickup request in the future * sender address have to be configures * shipment should be already generated * the manifest can be closed or not */ public function createPickupAction() { $shipmentIds = $this->getRequest()->getParam("shipment_ids"); if (!$shipmentIds) { $this->_createPickupRedirect($this->__('Please select the shipments for which you wish to arrange a pickup.')); return; } list($day, $month, $year) = explode("/", $this->getRequest()->getParam("zitec_dpd_pickup_date")); if (!checkdate($month, $day, $year)) { $this->_createPickupRedirect($this->__('Please enter a pickup date in the format DD/MM/YYYY.')); return; } $year = isset($year) && strlen($year) == 2 ? "20{$year}" : $year; $month = isset($month) && strlen($month) < 2 ? str_pad($month, 2, "0") : $month; $day = isset($day) && strlen($day) < 2 ? str_pad($day, 2, "0") : $day; $pickupDate = "{$year}{$month}{$day}"; $pickupFromParts = $this->getRequest()->getParam("zitec_dpd_pickup_from"); if (!is_array($pickupFromParts) || count($pickupFromParts) != 3) { $this->_createPickupRedirect($this->__('Please select a from and to time for the pickup.')); return; } $pickupFrom = implode("", $pickupFromParts); $pickupToParts = $this->getRequest()->getParam("zitec_dpd_pickup_to"); if (!is_array($pickupToParts) || count($pickupToParts) != 3) { $this->_getHelper()->addError($this->__('Please select a from and to time for the pickup.')); $this->_redirect("adminhtml/sales_shipment/index"); return; } $pickupTo = implode("", $pickupToParts); $instruction = $this->getRequest()->getParam("zitec_dpd_pickup_instruction"); $pickupAddress = $this->_getWsHelper()->getPickupAddress(); if (!is_array($pickupAddress)) { $this->_getHelper()->addError($this->__('You cannot create a pickup because you have not fully specified your pickup address. <br />Please set your pickup address in System->Configuration->Sales->Shipping Settings->DPD GeoPost Pickup Address.')); $this->_redirect("adminhtml/sales_shipment/index"); return; } $apiParams = $this->_getWsHelper()->getPickupParams(); $apiParams['method'] = Zitec_Dpd_Api_Configs::METHOD_PICKUP_CREATE; $dpdApi = new Zitec_Dpd_Api($apiParams); $createPickup = $dpdApi->getApiMethodObject(); $createPickup->setPickupTime($pickupDate, $pickupFrom, $pickupTo); $createPickup->setSpecialInstruction($instruction); $createPickup->setPickupAddress($pickupAddress); $shipments = Mage::getResourceModel('sales/order_shipment_collection'); /* @var $shipments Mage_Sales_Model_Resource_Order_Shipment_Collection */ $shipments->addFieldToFilter('entity_id', array("in" => $shipmentIds)); $ships = Mage::getResourceModel('zitec_dpd/dpd_ship_collection'); /* @var $ships Zitec_Dpd_Model_Mysql4_Dpd_Ship_Collection */ $ships->filterByShipmentIds($shipmentIds); $includedShipments = array(); foreach ($shipments as $shipment) { /* @var $shipment Mage_Sales_Model_Order_Shipment */ $ship = $ships->findByShipmentId($shipment->getId()); /* @var $ship Zitec_Dpd_Model_Dpd_Ship */ if (!$ship || !$this->_getHelper()->isShippingMethodDpd($shipment) || $this->_getHelper()->isCancelledWithDpd($shipment)) { continue; } $includedShipments[] = $shipment; $call = @unserialize($ship->getSaveShipmentCall()); /* @var $call Zitec_Dpd_Api_Shipment_Save */ if (!$call) { $message = $this->__("Unable to load shipment information for this shipment %s.", $shipment); $this->_createPickupRedirect($message); return; } $createPickup->addPieces($call->getShipmentServiceCode(), $call->getParcelCount(), $call->getTotalWeight(), $call->getReceiverCountryCode()); } if (!$includedShipments) { $message = $this->__("Your list did not contain any DPD shipments for which to arrange a pickup.", $shipment); $this->_createPickupRedirect($message); return; } try { $createPickup->execute(); } catch (Exception $e) { $message = $this->__('A problem occurred whilst communicating your shipment to DPD. <br />"%s"', $e->getMessage()); $this->_getHelper()->log($message); $this->_createPickupRedirect($message); return; } $response = $createPickup->getCreatePickupResponse(); if ($response->hasError()) { $message = $this->__('DPD reported an error whilst attempting to arrange your pickup. <br />DPD says, "%s"', $response->getErrorText()); $this->_getHelper()->log($message); $this->_createPickupRedirect($message); return; } $pickup = Mage::getModel('zitec_dpd/dpd_pickup'); /* @var $pickup Zitec_Dpd_Model_Dpd_Pickup */ $pickup->setReference($response->getReferenceNumber())->setDpdId($response->getDpdId())->setPickupDate("{$year}-{$month}-{$day}")->setPickupTimeFrom("{$year}-{$month}-{$day} " . implode(":", $pickupFromParts))->setPickupTimeTo("{$year}-{$month}-{$day} " . implode(":", $pickupToParts))->setCallData(serialize($createPickup))->setResponseData(serialize($response))->save(); foreach ($includedShipments as $includedShipment) { $includedShipment->setData('zitec_dpd_pickup_time', "{$year}-{$month}-{$day} " . implode(":", $pickupFromParts)); $includedShipment->setData('zitec_dpd_pickup_id', $pickup->getEntityId()); $includedShipment->save(); } $this->_getHelper()->addNotice("Your pickup was created successfully"); $this->_redirect("adminhtml/sales_shipment/index"); }
/** * @param Zitec_PackedShipment_Model_PackedShipment $packedShipment * * @return bool * @throws Mage_Core_Exception */ protected function _createShipment(Zitec_PackedShipment_Model_PackedShipment $packedShipment) { $dpdApi = new Zitec_Dpd_Api($this->_getShipmentParams()); $dpdShipment = $dpdApi->getApiMethodObject(); $serviceCode = $this->_getHelper()->getDPDServiceCode($this->_getOrder()->getShippingMethod()); if (!$serviceCode) { Mage::throwException(sprintf($this->__("An error occurred communicating the shipment to DPD. The shipping method '%s' is invalid"), $this->_getOrder()->getShippingMethod())); } $dpdShipment->setReceiverAddress($this->_getShippingAddress())->setShipmentReferenceNumber($this->_getShipment()->getIncrementId())->setShipmentServiceCode($serviceCode); foreach ($packedShipment->getPackages() as $packageIdx => $package) { $dpdShipment->addParcel($packageIdx + 1, $package->getPackageWeight(), $package->getRef()); } if ($this->_getHelper()->isOrderCashOnDelivery($this->_getOrder())) { $paymentType = $this->_getHelper()->getCodPaymentType($this->_getOrder()); $dpdShipment->setCashOnDelivery(round($this->_getOrder()->getBaseGrandTotal(), 2), $this->_getOrder()->getBaseCurrencyCode(), $paymentType); } $order = $this->_getOrder(); $insurance = Mage::helper('zitec_dpd')->extractInsuranceValuesByOrder($order); $dpdShipment->setAdditionalHighInsurance($insurance['goodsValue'], $insurance['currency'], $insurance['content']); try { $response = $dpdShipment->execute(); } catch (Zitec_Dpd_Api_Shipment_Save_Exception_ReceiverAddressTooLong $e) { $message = "The shipment could not be communicated to DPD because the shipping street the maximum permitted length of %s characters. <br />Please edit the shipping address to reduce the length of the street in the shipping address."; Mage::throwException(sprintf($this->__($message), $e->getMaxLength())); } catch (Exception $e) { Mage::throwException(sprintf($this->__("An error occurred communicating the shipment to DPD at %s:<br /> '%s'"), $dpdShipment->getUrl(), $e->getMessage())); } if ($response->hasError()) { $message = sprintf($this->__('DPD could not process the new shipment. The following error was returned: <br /> "%s: %s"'), $response->getErrorCode(), $response->getErrorText()); Mage::throwException($message); } $this->_response = $response; $this->_call = $dpdShipment; $this->_saveShipmentResponse($response, $dpdShipment); return true; }
/** * * @param Mage_Sales_Model_Order $order * @param string $city * @param string $postcode * @param array $weightsPackages * @param string $errorStr * * @return double */ public function getShippingCost(Mage_Sales_Model_Order $order, $city, $postcode, $weightsPackages, &$errorStr) { $shippingAddress = $order->getShippingAddress(); $city = $city ? $city : $shippingAddress->getCity(); $postcode = $postcode ? $postcode : $shippingAddress->getPostcode(); $serviceCode = $this->_getHelper()->getDPDServiceCode($order->getShippingMethod()); $street = is_array($shippingAddress->getStreetFull()) ? implode("\n", $shippingAddress->getStreetFull()) : $shippingAddress->getStreetFull(); $apiParams = $this->_getWsHelper()->getShipmentParams($order->getStore()); $apiParams['method'] = Zitec_Dpd_Api_Configs::METHOD_SHIPMENT_CALCULATE_PRICE; try { $dpdApi = new Zitec_Dpd_Api($apiParams); $calculatePrice = $dpdApi->getApiMethodObject(); $calculatePrice->setReceiverAddress($street, $city, $postcode, $shippingAddress->getCountryId())->setShipmentServiceCode($serviceCode); foreach ($weightsPackages as $parcelWeight) { $calculatePrice->addParcel($parcelWeight); } $insurance = Mage::helper('zitec_dpd')->extractInsuranceValuesByOrder($order); $calculatePrice = $calculatePrice->setAdditionalHighInsurance($insurance['goodsValue'], $insurance['currency'], $insurance['content']); $calculatePrice->execute(); } catch (Exception $e) { $errorStr = $this->_getHelper()->__("Error obtaining shipping price: %s", $e->getMessage()); $this->_getHelper()->log("An error occurred whilst calculating the DPD price for the shipment {$e->getMessage()}"); return 0; } $response = $calculatePrice->getCalculatePriceResponse(); if ($response->hasError()) { $errorStr = $this->_getHelper()->__("DPD error: %s", $response->getErrorText()); $this->_getHelper()->log("DPD returned the following error whilst attempting to calculate the price of a shipment: {$response->getErrorText()}"); return 0; } if ($order->getBaseCurrencyCode() == $response->getCurrency()) { return $response->getAmount(); } else { if ($order->getBaseCurrencyCode() == $response->getCurrencyLocal()) { return $response->getAmountLocal(); } else { $errorStr = $this->_getHelper()->__("Shipping price not available in order currency"); $this->_getHelper()->log("An error occurred whilst calculating the price of a shipment. The base currency of the shipment ({$order->getBaseCurrencyCode()}) does not correspond to the currency ({$response->getCurrency()}) or the local currency ({$response->getCurrencyLocal()}) used by DPD."); return 0; } } }