/** * @param string $barcode * @param TIG_MyParcel2014_Model_Shipment $myParcelShipment * * @return bool * @throws TIG_MyParcel2014_Exception */ public function sendBarcodeEmail($barcode = '', $myParcelShipment) { if (empty($barcode)) { return false; } if (!$myParcelShipment instanceof TIG_MyParcel2014_Model_Shipment) { return false; } $order = $myParcelShipment->getOrder(); $storeId = $order->getStoreId(); $templateId = $this->getConfig('tracktrace_template', 'general', $storeId); //if no template is set, return false: tracktrace should be send by MyParcel if ($templateId === null || $templateId == 'tig_myparcel_general_tracktrace_template') { return false; } try { // Retrieve specified view block from appropriate design package (depends on emulated store) $paymentBlock = Mage::helper('payment')->getInfoBlock($order->getPayment())->setIsSecureMode(true); $paymentBlock->getMethod()->setStore($storeId); $paymentBlockHtml = $paymentBlock->toHtml(); } catch (Exception $exception) { $paymentBlockHtml = ''; } $shippingAddress = $myParcelShipment->getShippingAddress(); $barcodeUrl = $this->getBarcodeUrl($barcode, $shippingAddress); // Set pakjegemak foreach ($order->getAddressesCollection() as $address) { if ($address->getAddressType() == 'pakje_gemak' && !$address->isDeleted()) { $myParcelShipment->setShippingAddress($address); $order->setShippingAddress($address); } } $templateVariables = array('tracktrace_url' => $barcodeUrl, 'order' => $order, 'shipment' => $myParcelShipment->getShipment(), 'billing' => $order->getBillingAddress(), 'payment_html' => $paymentBlockHtml); try { $mailer = Mage::getModel('core/email_template_mailer'); $emailInfo = Mage::getModel('core/email_info'); $emailInfo->addTo($order->getCustomerEmail(), $shippingAddress->getName()); $mailer->addEmailInfo($emailInfo); // Set all required params and send emails. $mailer->setSender(Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $storeId)); $mailer->setStoreId($storeId); $mailer->setTemplateId($templateId); $mailer->setTemplateParams($templateVariables); $mailer->send(); } catch (Exception $e) { $this->logException($e); return false; } return true; }
/** * Gets the shipping address and product code data for this shipment. * * @param TIG_MyParcel2014_Model_Shipment $myParcelShipment * * @return array * * @throws TIG_MyParcel2014_Exception */ protected function _getConsignmentData(TIG_MyParcel2014_Model_Shipment $myParcelShipment) { /** @var TIG_MyParcel2014_Helper_Data $helper */ $helper = Mage::helper('tig_myparcel'); $order = $myParcelShipment->getOrder(); $storeId = $order->getStore()->getId(); if ($storeId != $this->getStoreId()) { $this->apiUsername = $helper->getConfig('username', 'api', $storeId); $this->apiKey = $helper->getConfig('key', 'api', $storeId, true); } $shippingAddress = $myParcelShipment->getShippingAddress(); $streetData = $helper->getStreetData($shippingAddress); $email = $myParcelShipment->getOrder()->getCustomerEmail(); $data = array('recipient' => array('cc' => $shippingAddress->getCountry(), 'person' => trim($shippingAddress->getName()), 'company' => (string) trim($shippingAddress->getCompany()), 'postal_code' => trim($shippingAddress->getPostcode()), 'street' => trim($streetData['streetname']), 'number' => trim($streetData['housenumber']), 'number_suffix' => trim($streetData['housenumberExtension']), 'city' => trim($shippingAddress->getCity()), 'email' => $email), 'options' => $this->_getOptionsData($myParcelShipment)); if ($myParcelShipment->getShippingAddress()->getCountry() != 'NL') { $phone = $order->getBillingAddress()->getTelephone(); if ($phone) { $data['recipient']['phone'] = $phone; } $data['recipient']['street'] = trim(str_replace(' ', ' ', implode(' ', $streetData))); unset($data['recipient']['number']); unset($data['recipient']['number_suffix']); } // add customs data for EUR3 and World shipments if ($helper->countryNeedsCustoms($shippingAddress->getCountry())) { $customsContentType = null; if ($myParcelShipment->getCustomsContentType()) { $customsContentType = explode(',', $myParcelShipment->getCustomsContentType()); } if ($data['options']['package_type'] == 2) { throw new TIG_MyParcel2014_Exception($helper->__('International shipments can not be sent by') . ' ' . strtolower($helper->__('Letter box')), 'MYPA-0027'); } $data['customs_declaration'] = array(); $data['customs_declaration']['items'] = array(); $data['customs_declaration']['invoice'] = $order->getIncrementId(); $customType = (int) $helper->getConfig('customs_type', 'shipment', $storeId); $data['customs_declaration']['contents'] = $customType == 0 ? 1 : $customType; $totalWeight = 0; $items = $myParcelShipment->getOrder()->getAllItems(); $i = 0; foreach ($items as $item) { if ($item->getProductType() == 'simple') { $parentId = $item->getParentItemId(); $weight = floatval($item->getWeight()); $price = floatval($item->getPrice()); $qty = intval($item->getQtyOrdered()); if (!empty($parentId)) { $parent = Mage::getModel('sales/order_item')->load($parentId); if (empty($weight)) { $weight = $parent->getWeight(); } if (empty($price)) { $price = $parent->getPrice(); } } $weight *= $qty; $weight = max(array(1, $weight)); $totalWeight += $weight; $price *= $qty; if (empty($customsContentType)) { $customsContentTypeItem = $helper->getHsCode($item, $storeId); } else { $customsContentTypeItem = key_exists($i, $customsContentType) ? $customsContentType[$i] : $customsContentType[0]; } if (!$customsContentTypeItem) { throw new TIG_MyParcel2014_Exception($helper->__('No Customs Content HS Code found. Go to the MyParcel plugin settings to set this code.'), 'MYPA-0026'); } $data['customs_declaration']['items'][] = array('description' => $item->getName(), 'amount' => $qty, 'weight' => (int) $weight * 1000, 'item_value' => array('amount' => $price * 100, 'currency' => 'EUR'), 'classification' => $customsContentTypeItem, 'country' => Mage::getStoreConfig('general/country/default', $storeId)); if (++$i >= 5) { break; // max 5 entries } } } $data['customs_declaration']['weight'] = (int) $totalWeight; $data['physical_properties']['weight'] = (int) $totalWeight; } /** * If the customer has chosen to pick up their order at a PakjeGemak location, add the PakjeGemak address. */ $pgAddress = $helper->getPgAddress($myParcelShipment); $shippingMethod = $order->getShippingMethod(); if ($pgAddress && $helper->shippingMethodIsPakjegemak($shippingMethod)) { $pgStreetData = $helper->getStreetData($pgAddress); $data['options']['signature'] = 1; $data['pickup'] = array('postal_code' => trim($pgAddress->getPostcode()), 'street' => trim($pgStreetData['streetname']), 'city' => trim($pgAddress->getCity()), 'number' => trim($pgStreetData['housenumber']), 'location_name' => trim($pgAddress->getCompany())); } $data['carrier'] = 1; return $data; }