_time = $time; $this->__version = "";//str_replace(".","-",(string)Mage::getConfig()->getNode()->modules->TRIC_Pacsoft->version); } public function getDeveloperId() { return $this->__developer_id; } public function testConnection($user,$pass,$session,$type='xml') { if(!function_exists('curl_version')) { Mage::getSingleton('adminhtml/session')->addError("Must have cURL to communicate with Pacsoftonline"); Mage::getSingleton('adminhtml/session')->addError("cURL not available in PHP, contact you hostmaster."); return false; } $developer_id = $this->__developer_id; $version = $this->__version; $url = "https://www.pacsoftonline.com/ufoweb/order?session=$session&user=$user&pin=$pass&developerid=$developer_id"; $xml = ''; $ch = curl_init($url); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml')); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); if($response === false) { return "CURL ERROR: " . curl_errno($ch) . " -> " . curl_error($ch); } curl_close($ch); try{ $xml = simplexml_load_string($response); $messages = array(); foreach($xml->val as $val) { $messages[(string)$val->attributes()->n] = (string)$val; } if(isset($messages['status']) && $messages['status'] == 500) { return true; } } catch(Exception $e){ } return false; } public function createLabelAndReturn($orders,$type,$store_id) { if(!is_array($orders)){ $orders = array($orders); } $xml = $this->__generateLabelAndReturnXmlFromOrderIds($orders,$type,$store_id); $response = $this->__createLabelsFromXML($xml,$store_id); if($this->__handleResponse($response,false)) { foreach($orders as $order_id) { $order = Mage::getModel('sales/order')->load($order_id); $message = array(); $status = (($order->getStatus() != 'complete' || !Mage::helper('pacsoft')->getPacsoftConfigDataSettings('new_order_status_change_if_complete',$order->getStoreId())) && Mage::helper('pacsoft')->getPacsoftConfigDataSettings('new_order_status',$order->getStoreId())) ? Mage::helper('pacsoft')->getPacsoftConfigDataSettings('new_order_status',$order->getStoreId()) : $order->getStatus(); $order->addStatusToHistory($status,"Pacsoft label oprettet i pacsoftonline".implode(" | ",$message),false)->save(); } } return $this; } private function __generateLabelAndReturnXmlFromOrderIds($order_ids,$type='auto',$store_id) { $writer = new XMLWriter(); $writer->openMemory(); $writer->setIndent(4); $writer->startDocument('1.0','UTF-8'); $writer->startElement('pacsoftonline'); $writer->writeRaw("\n"); $writer->writeRaw($this->__getMetaNode($store_id)); foreach($order_ids as $order_id) { try { $order = Mage::getModel('sales/order')->load($order_id); // LABEL $orderValues = $this->__getOrderValues($order,true,$type); $serviceAndAddons = Mage::helper('pacsoft/rates')->getServiceAndAddons($order->getData('shipping_method'),true,$type,$orderValues['country'],$order->getStoreId(),$order->getIncrementId()); if(in_array("DPDDK",$serviceAndAddons['service'])) { $writer->writeRaw($this->__getDpdNode($order,true,$type)); } else { $writer->writeRaw($this->__getDroppointNode($order,true,$type)); $writer->writeRaw($this->__getReceiverNode($order,true,$type)); $writer->writeRaw($this->__getShipmentNode($order,true,$type)); } // RETURN $orderValues = $this->__getOrderValues($order,false,$type); $serviceAndAddons = Mage::helper('pacsoft/rates')->getServiceAndAddons($order->getData('shipping_method'),false,$type,$orderValues['country'],$order->getStoreId(),$order->getIncrementId()); if(in_array("DPDDK",$serviceAndAddons['service'])) { $writer->writeRaw($this->__getDpdNode($order,false,$type)); } else { $writer->writeRaw($this->__getDroppointNode($order,false,$type)); $writer->writeRaw($this->__getReceiverNode($order,false,$type)); $writer->writeRaw($this->__getShipmentNode($order,false,$type)); } } catch(Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); } } $writer->endElement(); $writer->endDocument(); return $writer->outputMemory(TRUE); } public function createLabel($orders,$type,$store_id) { if(is_array($orders)) { $order_ids = $orders; $xml = $this->__generateXmlFromOrderIds($order_ids,false,$type,$store_id); } else { $order_id = $orders; $order = Mage::getModel('sales/order')->load($order_id); $xml = $this->__generateXmlFromOrder($order,false,$type); } $response = $this->__createLabelsFromXML($xml,$store_id); if($this->__handleResponse($response,false)) { if(!is_array($orders)){ $orders = array($orders); } foreach($orders as $order_id) { $order = Mage::getModel('sales/order')->load($order_id); $message = array(); $status = (($order->getStatus() != 'complete' || !Mage::helper('pacsoft')->getPacsoftConfigDataSettings('new_order_status_change_if_complete',$order->getStoreId())) && Mage::helper('pacsoft')->getPacsoftConfigDataSettings('new_order_status',$order->getStoreId())) ? Mage::helper('pacsoft')->getPacsoftConfigDataSettings('new_order_status',$order->getStoreId()) : $order->getStatus(); $order->addStatusToHistory($status,"Pacsoft label oprettet i pacsoftonline".implode(" | ",$message),false)->save(); } } return $this; } public function createReturnLabel($orders,$store_id) { if(is_array($orders)) { $order_ids = $orders; $xml = $this->__generateXmlFromOrderIds($order_ids,true,$store_id); } else { $order_id = $orders; $order = Mage::getModel('sales/order')->load($order_id); $xml = $this->__generateXmlFromOrder($order,true); } $response = $this->__createLabelsFromXML($xml,$store_id); $this->__handleResponse($response,true); return $this; } private function __generateXmlFromOrder($order,$returnlabel = false,$type='auto') { $orderValues = $this->__getOrderValues($order,$returnlabel,$type); $serviceAndAddons = Mage::helper('pacsoft/rates')->getServiceAndAddons($order->getData('shipping_method'),$returnlabel,$type,$orderValues['country'],$order->getStoreId(),$order->getIncrementId()); $writer = new XMLWriter(); $writer->openMemory(); $writer->setIndent(4); $writer->startDocument('1.0','UTF-8'); $writer->startElement('pacsoftonline'); $writer->writeRaw("\n"); $writer->writeRaw($this->__getMetaNode($order->getStoreId())); if(in_array("DPDDK",$serviceAndAddons['service'])) { $writer->writeRaw($this->__getDpdNode($order,$returnlabel,$type)); } else { $writer->writeRaw($this->__getDroppointNode($order,$returnlabel,$type)); $writer->writeRaw($this->__getReceiverNode($order,$returnlabel,$type)); $writer->writeRaw($this->__getShipmentNode($order,$returnlabel,$type)); } $writer->endElement(); $writer->endDocument(); return $writer->outputMemory(TRUE); } private function __generateXmlFromOrderIds($order_ids,$returnlabel = false,$type='auto',$store_id) { $writer = new XMLWriter(); $writer->openMemory(); $writer->setIndent(4); $writer->startDocument('1.0','UTF-8'); $writer->startElement('pacsoftonline'); $writer->writeRaw("\n"); $writer->writeRaw($this->__getMetaNode($store_id)); foreach($order_ids as $order_id) { $order = Mage::getModel('sales/order')->load($order_id); $orderValues = $this->__getOrderValues($order,$returnlabel,$type); try { $serviceAndAddons = Mage::helper('pacsoft/rates')->getServiceAndAddons($order->getData('shipping_method'),$returnlabel,$type,$orderValues['country'],$order->getStoreId(),$order->getIncrementId()); if(in_array("DPDDK",$serviceAndAddons['service'])) { $writer->writeRaw($this->__getDpdNode($order,$returnlabel,$type)); } else { $writer->writeRaw($this->__getDroppointNode($order,$returnlabel,$type)); $writer->writeRaw($this->__getReceiverNode($order,$returnlabel,$type)); $writer->writeRaw($this->__getShipmentNode($order,$returnlabel,$type)); } } catch(Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); } } $writer->endElement(); $writer->endDocument(); return $writer->outputMemory(TRUE); } private function __getDpdNode($order,$returnlabel,$type) { $sender_quickid = Mage::helper('pacsoft')->getPacsoftConfigDataSettings('sender_quickid',$order->getStoreId()); if($sender_quickid == "") { Mage::helper('pacsoft')->addWarning("Afsender KvikId ikke fundet, indstil dette under System->Konfiguration->TRIC->Post Danmark PacSoft"); } $writer = new XMLWriter(); $writer->openMemory(); $writer->setIndent(4); $writer->startElement('shipment'); $writer->writeAttribute('orderno',$this->_time); $reference = $order->getIncrementId(); if(Mage::helper('pacsoft')->getPacsoftConfigDataSettings('reference_suffix_active',$order->getStoreId())){ $reference .= Mage::helper('pacsoft')->getPacsoftConfigDataSettings('reference_suffix',$order->getStoreId()); } $writer->startElement("val"); $writer->writeAttribute('n', 'from'); $writer->text($sender_quickid); $writer->endElement(); $writer->startElement("val"); $writer->writeAttribute('n', 'to'); $writer->text($reference); $writer->endElement(); $writer->startElement("val"); $writer->writeAttribute('n', 'reference'); $writer->text($reference); $writer->endElement(); $writer->startElement('service'); $writer->writeAttribute('srvid', 'DPDDK'); $writer->endElement(); $writer->writeRaw("\n"); $writer->writeRaw($this->__getContainerNode($order,$returnlabel,$type)); $writer->endElement(); $writer->writeRaw($this->__getReceiverNode($order,$returnlabel,$type)); return $writer->outputMemory(TRUE); } private function __getMetaNode($store_id) { $attributes = array(); $dataArray = array(); $dataArray['origin'] = Mage::helper('pacsoft')->getPacsoftConfigDataLogin('brugerid',$store_id); $dataArray['created'] = date('Y-m-d H:i',Mage::getModel('core/date')->timestamp(time())); /* IKKE MULIGT MED XML - Citat IT Postdanmark (Helle - 2013-02-19) $dataArray['printer'] = "|ZDesigner GK420d|"; $dataArray['favorite'] = "0"; $dataArray['partition'] = "0"; $dataArray['autoprint'] = "YES"; */ return $this->__getXmlNode('meta',$attributes,$dataArray); } private function __getDroppoint($order,$returnlabel,$type) { $orderValues = $this->__getOrderValues($order,$returnlabel,$type); $country = $orderValues['country']; $serviceAndAddons = Mage::helper('pacsoft/rates')->getServiceAndAddons($order->getData('shipping_method'),$returnlabel,$type,$country,$order->getStoreId(),$order->getIncrementId()); if(in_array("PUPOPT",$serviceAndAddons['addons']) && $orderValues['droppoint']) { return $orderValues['droppoint']; } return false; } private function __getDroppointNode($order,$returnlabel,$type) { $orderValues = $this->__getOrderValues($order,$returnlabel,$type); $droppoint = $this->__getDroppoint($order,$returnlabel,$type); if($droppoint && !$returnlabel) { $attributes = array(); $attributes['rcvid'] = $droppoint; $streets = explode("\n",$orderValues['street']); $dataArray = array(); $dataArray['name'] = $orderValues['name']; $dataArray['address1'] = isset($streets[0]) ? $streets[0] : ""; $dataArray['zipcode'] = $orderValues['postcode']; $dataArray['city'] = $orderValues['city']; $dataArray['country'] = $orderValues['country']; $dataArray['contact'] = $orderValues['billingname']; if(Mage::getStoreConfig('pacsoft/design/new_data_save_version')) { $dataArray['name'] = $orderValues['company']; $dataArray['contact'] = $orderValues['name']; } return $this->__getXmlNode('receiver',$attributes,$dataArray); } return ""; } private function __getReceiverNode($order,$returnlabel,$type) { $attributes = array(); $reference = $order->getIncrementId(); if(Mage::helper('pacsoft')->getPacsoftConfigDataSettings('reference_suffix_active',$order->getStoreId())){ $reference .= Mage::helper('pacsoft')->getPacsoftConfigDataSettings('reference_suffix',$order->getStoreId()); } $attributes['rcvid'] = $reference; $orderValues = $this->__getOrderValues($order,$returnlabel,$type); $droppoint = $this->__getDroppoint($order,$returnlabel,$type); if($droppoint || $returnlabel) { $streets = explode("\n",$orderValues['billingstreet']); $dataArray = array(); $dataArray['name'] = $orderValues['billingname']; $dataArray['address1'] = isset($streets[0]) ? $streets[0] : ""; $dataArray['address2'] = isset($streets[1]) ? $streets[1] : $orderValues['region']; $dataArray['zipcode'] = $orderValues['billingpostcode']; $dataArray['city'] = $orderValues['billingcity']; $dataArray['country'] = $orderValues['billingcountry']; $dataArray['contact'] = $orderValues['billingcompany']; $dataArray['phone'] = $orderValues['billingtelephone']; $dataArray['email'] = strtolower($orderValues['customeremail']); $dataArray['sms'] = $orderValues['billingtelephone']; if(Mage::getStoreConfig('pacsoft/design/new_data_save_version')) { $dataArray['contact'] = $orderValues['name']; $dataArray['phone'] = $orderValues['telephone']; $dataArray['email'] = strtolower($orderValues['customeremail']); $dataArray['sms'] = $orderValues['telephone']; } } else { $streets = explode("\n",$orderValues['street']); $dataArray = array(); $dataArray['name'] = $orderValues['name']; $dataArray['address1'] = isset($streets[0]) ? $streets[0] : ""; $dataArray['address2'] = isset($streets[1]) ? $streets[1] : $orderValues['region']; $dataArray['zipcode'] = $orderValues['postcode']; $dataArray['city'] = $orderValues['city']; $dataArray['country'] = $orderValues['country']; $dataArray['contact'] = $orderValues['company_contact']; $dataArray['phone'] = $orderValues['telephone']; $dataArray['email'] = strtolower($orderValues['customeremail']); $dataArray['sms'] = $orderValues['telephone']; if(Mage::getStoreConfig('pacsoft/design/new_data_save_version')) { $dataArray['name'] = ($orderValues['company']) ? $orderValues['company'] : $dataArray['name']; $dataArray['contact'] = ($orderValues['company']) ? $orderValues['name'] : $dataArray['contact']; } if(!$dataArray['contact']) { $dataArray['contact'] = $dataArray['name']; } } return $this->__getXmlNode('receiver',$attributes,$dataArray); } private function __getCustomsDeclaration($order) { $writer = new XMLWriter(); $writer->openMemory(); $writer->setIndent(4); $text = Mage::helper('pacsoft')->getPacsoftConfigDataSettings('service_misc',$order->getStoreId()) ? Mage::helper('pacsoft')->getPacsoftConfigDataSettings('service_misc',$order->getStoreId()) : "-"; $reference = $order->getIncrementId(); if(Mage::helper('pacsoft')->getPacsoftConfigDataSettings('reference_suffix_active',$order->getStoreId())){ $reference .= Mage::helper('pacsoft')->getPacsoftConfigDataSettings('reference_suffix',$order->getStoreId()); } $writer->startElement('customsdeclaration'); $writer->writeAttribute('documents',Mage::helper('pacsoft')->getPacsoftConfigDataSettings('service_customsdeclaration_type',$order->getStoreId())); $writer->startElement("val"); $writer->writeAttribute('n', 'reference'); $writer->text($reference); $writer->endElement(); $writer->startElement("val"); $writer->writeAttribute('n', 'senderorgno'); $writer->text("-"); $writer->endElement(); $writer->startElement("val"); $writer->writeAttribute('n', 'customsunit'); $writer->text($order->getOrderCurrencyCode()); $writer->endElement(); $writer->startElement("val"); $writer->writeAttribute('n', 'impexptype'); $writer->text(Mage::helper('pacsoft')->getPacsoftConfigDataSettings('service_impexptype',$order->getStoreId())); $writer->endElement(); $writer->startElement("line"); $writer->startElement("val"); $writer->writeAttribute('n', 'statno'); $writer->text(Mage::helper('pacsoft')->getPacsoftConfigDataSettings('service_statno',$order->getStoreId())); $writer->endElement(); $writer->startElement("val"); $writer->writeAttribute('n', 'contents'); $writer->text($text); $writer->endElement(); $writer->startElement("val"); $writer->writeAttribute('n', 'customsvalue'); $writer->text($order->getSubtotal()); $writer->endElement(); $writer->startElement("val"); $writer->writeAttribute('n', 'netweight'); $writer->text($this->__getOrderWeight($order)); $writer->endElement(); $writer->startElement("val"); $writer->writeAttribute('n', 'sourcecountry'); $writer->text(Mage::helper('pacsoft')->getPacsoftConfigDataSettings('service_source_country',$order->getStoreId())); $writer->endElement(); $writer->endElement(); $writer->writeRaw("\n"); $writer->endElement(); /* $writer->startElement('customsdeclaration'); $writer->writeAttribute('documents','CN23'); $writer->startElement("val"); $writer->writeAttribute('n', 'reference'); $writer->text($reference); $writer->endElement(); $writer->startElement("val"); $writer->writeAttribute('n', 'senderorgno'); $writer->text("-"); $writer->endElement(); $writer->startElement("line"); $writer->startElement("val"); $writer->writeAttribute('n', 'contents'); $writer->text($text); $writer->endElement(); $writer->startElement("val"); $writer->writeAttribute('n', 'customsvalue'); $writer->text($order->getSubtotal()); $writer->endElement(); $writer->endElement(); $writer->writeRaw("\n"); $writer->endElement(); */ return $writer->outputMemory(TRUE); } private function __getShipmentNode($order, $returnlabel = false, $type) { $sender_quickid = Mage::helper('pacsoft')->getPacsoftConfigDataSettings('sender_quickid',$order->getStoreId()); if($sender_quickid == "") { Mage::helper('pacsoft')->addWarning("Afsender KvikId ikke fundet, indstil dette under System->Konfiguration->TRIC->Post Danmark PacSoft"); } $writer = new XMLWriter(); $writer->openMemory(); $writer->setIndent(4); $writer->startElement('shipment'); $writer->writeAttribute('orderno',$this->_time); if((int)Mage::helper('pacsoft')->getPacsoftConfigDataSettings('linkprintkey_active',$order->getStoreId()) || (int)Mage::helper('pacsoft')->getPacsoftConfigDataSettings('enot_active',$order->getStoreId())) { if((int)Mage::helper('pacsoft')->getPacsoftConfigDataSettings('linkprintkey_active',$order->getStoreId())) { $writer->writeAttribute('linkprintkey',strtotime($this->_time)); } $writer->startElement('ufonline'); if((int)Mage::helper('pacsoft')->getPacsoftConfigDataSettings('linkprintkey_active',$order->getStoreId())) { if((int)Mage::helper('pacsoft')->getPacsoftConfigDataSettings('linkprintkey_active_only_return',$order->getStoreId()) && $returnlabel) { $writer->writeRaw($this->__getLinkPrintKeyNode($order,$returnlabel)); }else if(!(int)Mage::helper('pacsoft')->getPacsoftConfigDataSettings('linkprintkey_active_only_return',$order->getStoreId())) { $writer->writeRaw($this->__getLinkPrintKeyNode($order,$returnlabel)); } } if((int)Mage::helper('pacsoft')->getPacsoftConfigDataSettings('enot_active',$order->getStoreId())) { $writer->writeRaw($this->__getEnotNode($order)); } $writer->endElement(); } $reference = $order->getIncrementId(); if(Mage::helper('pacsoft')->getPacsoftConfigDataSettings('reference_suffix_active',$order->getStoreId())){ $reference .= Mage::helper('pacsoft')->getPacsoftConfigDataSettings('reference_suffix',$order->getStoreId()); } $droppoint = $this->__getDroppoint($order,$returnlabel,$type); if($droppoint && !$returnlabel) { $writer->startElement("val"); $writer->writeAttribute('n', 'agentto'); $writer->text($droppoint); $writer->endElement(); } $writer->startElement("val"); $writer->writeAttribute('n', 'from'); $writer->text($sender_quickid); $writer->endElement(); $writer->startElement("val"); $writer->writeAttribute('n', 'to'); $writer->text($reference); $writer->endElement(); $writer->startElement("val"); $writer->writeAttribute('n', 'reference'); $writer->text($reference); $writer->endElement(); $waybillname = $this->__getWaybillName($order, $returnlabel, $type); $waybillname = ($waybillname) ? $waybillname : ""; $writer->startElement("val"); $writer->writeAttribute('n', 'bulkid'); $writer->text($waybillname); $writer->endElement(); $writer->writeRaw("\n"); $writer->writeRaw($this->__getServiceNode($order, $returnlabel, $type)); $writer->writeRaw($this->__getContainerNode($order,$returnlabel,$type)); if(Mage::helper('pacsoft')->getPacsoftConfigDataSettings('service_customsdeclaration_type',$order->getStoreId())) { $writer->writeRaw($this->__getCustomsDeclaration($order)); } $writer->writeRaw($this->__getFlexNode($order, $returnlabel, $type)); $writer->endElement(); return $writer->outputMemory(TRUE); } private function __getWaybillName($order, $returnlabel, $type) { $orderValues = $this->__getOrderValues($order,$returnlabel,$type); $country = $orderValues['country']; $serviceAndAddons = Mage::helper('pacsoft/rates')->getServiceAndAddons($order->getData('shipping_method'),$returnlabel,$type,$country,$order->getStoreId(),$order->getIncrementId()); $waybillServices = array('PDKWAY' => "Post Danmark Waybill", 'PDKBREVI' => "Post Danmark Varebrev til udlandet", 'PDKBREVISL' => "Post Danmark Varebrev til udlandet Følgeseddel"); if(isset($waybillServices[$serviceAndAddons['service'][0]])) { return $this->__getWaybillNameForCountry($country,$order); } return false; } private function __getWaybillNameForCountry($country,$order) { $serializedCountryWaybillNames = Mage::helper('pacsoft')->getPacsoftConfigDataWaybill("waybill_depending_on_country",$order->getStoreId()); $countryWaybillNames = unserialize($serializedCountryWaybillNames); foreach($countryWaybillNames as $waybill) { if($waybill['country_code'] == $country) { return $waybill['waybill_name']; } } } private function __getEnotNode($order) { /* Link to print */ $attributes = array(); $attributes['optid'] = "enot"; $dataArray = array(); $dataArray['from'] = strtolower(Mage::helper('pacsoft')->getPacsoftConfigDataSettings('enot_email_from',$order->getStoreId())); $dataArray['cc'] = strtolower(Mage::helper('pacsoft')->getPacsoftConfigDataSettings('enot_email_cc',$order->getStoreId())); $dataArray['bcc'] = strtolower(Mage::helper('pacsoft')->getPacsoftConfigDataSettings('enot_email_bcc',$order->getStoreId())); $dataArray['to'] = strtolower($order->getCustomerEmail()); $dataArray['message'] = Mage::helper('pacsoft')->getPacsoftConfigDataSettings('enot_message',$order->getStoreId()); return $this->__getXmlNode('option',$attributes,$dataArray); } private function __getFlexNode($order,$returnlabel = false,$type) { $orderValues = $this->__getOrderValues($order,$returnlabel,$type); $country = $orderValues['country']; $serviceAndAddons = Mage::helper('pacsoft/rates')->getServiceAndAddons($order->getData('shipping_method'),$returnlabel,$type,$country,$order->getStoreId(),$order->getIncrementId()); if(in_array("DLVFLEX",$serviceAndAddons['addons'])) { $writer = new XMLWriter(); $writer->openMemory(); $writer->setIndent(4); $writer->startElement("val"); $writer->writeAttribute('n', 'freetext1'); $writer->text($order->getPacsoftFlexmessage()); $writer->endElement(); return $writer->outputMemory(TRUE); } return ""; } private function __getServiceNode($order,$returnlabel = false,$type) { $orderValues = $this->__getOrderValues($order,$returnlabel,$type); $country = $orderValues['country']; $serviceAndAddons = Mage::helper('pacsoft/rates')->getServiceAndAddons($order->getData('shipping_method'),$returnlabel,$type,$country,$order->getStoreId(),$order->getIncrementId()); $writer = new XMLWriter(); $writer->openMemory(); $writer->setIndent(4); $service = isset($serviceAndAddons['service'][0]) ? $serviceAndAddons['service'][0] : ""; $writer->startElement('service'); $writer->writeAttribute('srvid',$service); $writer->startElement("val"); $writer->writeAttribute('n', 'returnlabel'); $value = ($returnlabel && $service != Mage::helper('pacsoft/rates')->getReturnService()) ? "yes" : "no"; $writer->text($value); $writer->endElement(); $writer->startElement("val"); $writer->writeAttribute('n', 'nondelivery'); $writer->text(Mage::helper('pacsoft')->getPacsoftConfigDataSettings('service_nondelivery',$order->getStoreId())); $writer->endElement(); $writer->writeRaw("\n"); foreach($serviceAndAddons['addons'] as $addon) { if($addon == "PUPOPT") { $droppoint = $this->__getDroppoint($order,$returnlabel,$type); if(!$droppoint) { continue; } } if($addon == 'NOTSMS' && isset($orderValues['telephone']) && (!$orderValues['telephone'] || $orderValues['telephone'] == '-')) { continue; } $writer->writeRaw($this->__getAddonNode($order,$addon)); } $writer->endElement(); return $writer->outputMemory(TRUE); } private function __getAddonNode($order,$addon) { $attributes = array(); $attributes["adnid"] = $addon; $reference = $order->getIncrementId(); if(Mage::helper('pacsoft')->getPacsoftConfigDataSettings('reference_suffix_active',$order->getStoreId())){ $reference .= Mage::helper('pacsoft')->getPacsoftConfigDataSettings('reference_suffix',$order->getStoreId()); } $dataArray = array(); if($addon == "COD") { $dataArray['amount'] = $order->getGrandTotal(); $dataArray['reference'] = $reference; $dataArray['referencetype'] = Mage::helper('pacsoft')->getPacsoftConfigDataSettings('service_referencetype',$order->getStoreId()); $dataArray['account'] = Mage::helper('pacsoft')->getPacsoftConfigDataSettings('service_account',$order->getStoreId()); $dataArray['accounttype'] = Mage::helper('pacsoft')->getPacsoftConfigDataSettings('service_accounttype',$order->getStoreId()); } elseif($addon == "INSU") { $dataArray['amount'] = $order->getGrandTotal(); $dataArray['misc'] = Mage::helper('pacsoft')->getPacsoftConfigDataSettings('service_misc',$order->getStoreId()); } return $this->__getXmlNode('addon',$attributes,$dataArray); } private function __getContainerNode($order,$returnlabel,$type) { $orderValues = $this->__getOrderValues($order,$returnlabel,$type); $country = $orderValues['country']; $serviceAndAddons = Mage::helper('pacsoft/rates')->getServiceAndAddons($order->getData('shipping_method'),$returnlabel,$type,$country,$order->getStoreId(),$order->getIncrementId()); $attributes = array(); $attributes['type'] = "parcel"; $dataArray = array(); $dataArray['copies'] = 1;//(int)ceil(count($order->getAllItems())/18); Hvis der skal laves to forsendelser pr. forsendsels. $dataArray['marking'] = ""; $dataArray['weight'] = $this->__getOrderWeight($order); $dataArray['volume'] = ""; $dataArray['contents'] = ""; if(isset($serviceAndAddons['service']) && is_array($serviceAndAddons['service']) && in_array("PDKLASTMILE",$serviceAndAddons['service'])) { $dataArray['contents'] = Mage::helper('pacsoft')->getPacsoftConfigDataSettings('service_lastmile_content',$order->getStoreId()); } return $this->__getXmlNode('container',$attributes,$dataArray); } private function __getLinkPrintKeyNode($order,$returnlabel) { if(!$returnlabel) { /* Link to print */ $attributes = array(); $attributes['optid'] = "LNKPRTN"; // LNKPRTR virker åbenbart ikke længere $dataArray = array(); $dataArray['sendemail'] = ((int)Mage::helper('pacsoft')->getPacsoftConfigDataSettings('linkprintkey_active',$order->getStoreId())) ? "YES" : "NO"; $dataArray['from'] = strtolower(Mage::helper('pacsoft')->getPacsoftConfigDataSettings('linkprintkey_email',$order->getStoreId())); $dataArray['to'] = strtolower(Mage::helper('pacsoft')->getPacsoftConfigDataSettings('linkprintkey_email',$order->getStoreId())); $dataArray['errorto'] = ""; $dataArray['message'] = ""; } else { /* Link to print */ $attributes = array(); $attributes['optid'] = "LNKPRTN"; // LNKPRTR virker åbenbart ikke længere $dataArray = array(); $dataArray['sendemail'] = ((int)Mage::helper('pacsoft')->getPacsoftConfigDataSettings('linkprintkey_active',$order->getStoreId())) ? "YES" : "NO"; $dataArray['from'] = strtolower(Mage::helper('pacsoft')->getPacsoftConfigDataSettings('linkprintkey_email',$order->getStoreId())); $to = ((int)Mage::helper('pacsoft')->getPacsoftConfigDataSettings('linkprintkey_email_return_to_customer',$order->getStoreId())) ? $order->getCustomerEmail() : Mage::helper('pacsoft')->getPacsoftConfigDataSettings('linkprintkey_email',$order->getStoreId()); $dataArray['to'] = strtolower($to); $dataArray['errorto'] = ""; $dataArray['message'] = ""; } return $this->__getXmlNode('option',$attributes,$dataArray); } private function __getXmlNode($nodename,$attributes = array(), $values = array()) { $writer = new XMLWriter(); $writer->openMemory(); $writer->setIndent(4); $writer->startElement($nodename); foreach($attributes as $key => $value) { $writer->writeAttribute($key,$value); } foreach($values as $key => $value) { $writer->startElement("val"); $writer->writeAttribute('n', $key); $writer->text($value); $writer->endElement(); } $writer->endElement(); return $writer->outputMemory(TRUE); } private function __createLabelsFromXML($xml,$store_id) { if(!function_exists('curl_version')) { Mage::getSingleton('adminhtml/session')->addError("Must have cURL to communicate with Pacsoftonline"); Mage::getSingleton('adminhtml/session')->addError("cURL not available in PHP, contact you hostmaster."); return false; } $params = Mage::helper('pacsoft')->getUrlParams($store_id); $user = $params['user']; $pass = $params['pass']; $session = $params['session']; $type = $params['type']; $developer_id = $this->__developer_id; $version = $this->__version; $url = "https://www.pacsoftonline.com/ufoweb/order?session=$session&user=$user&pin=$pass&developerid=$developer_id&type=$type&version=$version"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml')); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); curl_close($ch); return $response; } private function __handleResponse($response,$returnType) { if($response == "") { return ""; } $xml = simplexml_load_string($response); $messages = array(); foreach($xml->val as $val) { $messages[(string)$val->attributes()->n] = (string)$val; } if($messages['status'] == 403) { Mage::helper('pacsoft')->addError("403 (Unsuccessful login.) Please check user ID, password and that both the Pacsoft Online account and the Unifaun ERPConnect functionality are active."); return false; } elseif($messages['status'] == 500) { Mage::helper('pacsoft')->addError($messages['message']); Mage::helper('pacsoft')->addError("500 (The order file was not interpreted correctly.) Please check the order file for syntax errors."); return false; } Mage::helper('pacsoft')->addSuccess("Label(s) er oprettet i Pacsoft Online"); return true; } public function getOrderValues($order,$returnlabel = false,$type='auto') { $orderValues = $this->__getOrderValues($order,$returnlabel,$type); $orderValues['is_droppoint'] = $this->__getDroppoint($order,$returnlabel,$type); foreach($orderValues as $key => $value) { if(is_null($value)) { $orderValues[$key] = ""; } } return $orderValues; } private function __getOrderValues($order,$returnlabel = false,$type='auto') { $shippingAddress = !$order->getIsVirtual() ? $order->getShippingAddress() : null; $billingAddress = $order->getBillingAddress(); $orderValues['orderid'] = $order->getRealOrderId(); $orderValues['status'] = $order->getStatus(); $orderValues['subtotal'] = $order->getData('subtotal'); $orderValues['qty'] = $this->getTotalQtyItemsOrdered($order); $orderValues['customername'] = $order->getCustomerName(); $orderValues['customeremail'] = strtolower($order->getCustomerEmail()); if(Mage::getStoreConfig('pacsoft/design/new_data_save_version')) { $orderValues['name'] = ($shippingAddress) ? $shippingAddress->getName() : $billingAddress->getName(); // $orderValues['name'] = ($shippingAddress && $shippingAddress->getData("company")) ? $shippingAddress->getData("company") : $orderValues['name']; $orderValues['company'] = $shippingAddress ? $shippingAddress->getData("company") : ''; $orderValues['company_contact'] = ($shippingAddress && $shippingAddress->getData("company")) ? $shippingAddress->getName() : ''; } else { $orderValues['name'] = ($shippingAddress) ? $shippingAddress->getName() : $billingAddress->getName(); $orderValues['name'] = ($shippingAddress && $shippingAddress->getData("company")) ? $shippingAddress->getData("company") : $orderValues['name']; $orderValues['company'] = $shippingAddress ? $shippingAddress->getData("company") : ''; $orderValues['company_contact'] = ($shippingAddress && $shippingAddress->getData("company")) ? $shippingAddress->getName() : ''; } $orderValues['street'] = $shippingAddress ? $shippingAddress->getData("street") : ''; $orderValues['postcode'] = $shippingAddress ? $shippingAddress->getData("postcode") : ''; $orderValues['city'] = $shippingAddress ? $shippingAddress->getData("city") : ''; $orderValues['regioncode'] = $shippingAddress ? $shippingAddress->getRegionCode() : ''; $orderValues['region'] = $shippingAddress ? $shippingAddress->getRegion() : ''; $orderValues['region'] = ($orderValues['region'] == "-") ? "" : $orderValues['region']; $orderValues['country'] = $shippingAddress ? $shippingAddress->getCountry() : ''; $orderValues['countryname'] = $shippingAddress ? $shippingAddress->getCountryModel()->getName() : ''; $orderValues['telephone'] = $shippingAddress ? $shippingAddress->getData("telephone") : ''; if(Mage::getStoreConfig('pacsoft/design/new_data_save_version')) { $droppoint = $shippingAddress ? explode(": ",$shippingAddress->getData("fax")) : array(); } else { $droppoint = $shippingAddress ? explode(": ",$shippingAddress->getData("telephone")) : array(); } $orderValues['droppoint'] = isset($droppoint[1]) ? $droppoint[1] : ''; //$orderValues['droppoint'] = $shippingAddress ? str_replace("Service Id: ","",$shippingAddress->getData("fax")) : ''; $orderValues['billingname'] = ($billingAddress->getData("company")) ? $billingAddress->getData("company") : $billingAddress->getName(); $orderValues['billingcompany'] = ($billingAddress->getData("company")) ? $billingAddress->getName() : $billingAddress->getData("company"); $orderValues['billingstreet'] = $billingAddress->getData("street"); $orderValues['billingpostcode'] = $billingAddress->getData("postcode"); $orderValues['billingcity'] = $billingAddress->getData("city"); $orderValues['billingregioncode'] = $billingAddress->getRegionCode(); $orderValues['billingregion'] = $billingAddress->getRegion(); $orderValues['billingcountry'] = $billingAddress->getCountry(); $orderValues['billingcountryname'] = $billingAddress->getCountryModel()->getName(); $orderValues['billingtelephone'] = $billingAddress->getData("telephone"); //$orderValues['telephone'] = ($orderValues['droppoint']) ? $orderValues['billingtelephone'] : $orderValues['telephone']; $orderValues['order_weight'] = $this->__getOrderWeight($order); $orderValues['flex_message'] = $order->getPacsoftFlexmessage(); return $orderValues; } private function __getOrderWeight($order) { if(!Mage::helper('pacsoft')->getPacsoftConfigDataSettings('calculate_weight',$order->getStoreId())){ return 0.5; // må ikke være 0, da det ikke er godtaget værdi på udlandspakker } $orderItems = $order->getItemsCollection(); $weight = 0; $afsender = $this->getAfsender($order->getStoreId()); foreach($orderItems as $item) { if (!$item->isDummy()) { $q = (int)$item->getQtyOrdered(); $w = $item->getWeight(); $weight += $w*$q; } } if(Mage::helper('pacsoft')->getPacsoftConfigDataSettings('weight_unit',$order->getStoreId()) == "gram"){ $weight = $weight/1000; } if($weight == 0) { $weight = 0.5;// må ikke være 0, da det ikke er godtaget værdi på udlandspakker } return $weight; } }