getCode('condition_name') as $k=>$v) { $this->_conditionNames[] = $k; } } public function getTrackingInfo($parcelNo) { $obj = new Varien_Object(); $obj->setData('tracking',$parcelNo); //$obj->setData('url',"http://www.pakkeboksen.dk/track-trace.html?searchId=$parcelNo"); $url = Mage::getModel('core/url')->getBaseUrl()."pacsoft/track/package/number/$parcelNo"; $obj->setData('url',$url); return $obj; } private function __getSessionId($user,$pass) { try { $client = new SoapClient("https://www.unifaunonline.com/ws-extapi2/Authentication2?wsdl", array('encoding' => 'UTF-8','soap_version' => SOAP_1_1)); $login = $client->Login1(array( 'userId' => $user, 'pass' => $pass, 'developerId' => Mage::getModel('pacsoft/pacsoft')->getDeveloperId(), )); return $login->return; } catch (Exception $e) { Mage::log($e->getMessage(),null,"pacsoft.log"); return false; } } private function __getParcels($sessionid,$fetchId,$store_id) { try { $client = new SoapClient("https://www.unifaunonline.com/ws-extapi2/History3?wsdl", array('encoding' => 'UTF-8','soap_version' => SOAP_1_1)); $result = $client->FetchNewShipments1( array('session' => $sessionid, 'fetchId' => $fetchId, ) ); return $result; } catch (Exception $e) { Mage::log($e->getMessage(),null,"pacsoft.log"); return false; } } private function __handleResult($result,$sessionid,$fetchId,$store_id) { if($result && $result->return) { if($result->return->shipments && !is_array($result->return->shipments)) { $result->return->shipments = array($result->return->shipments); } foreach($result->return->shipments as $parcel) { $reference = $parcel->reference; if(Mage::helper('pacsoft')->getPacsoftConfigDataSettings('reference_suffix_active')){ $suffix = Mage::helper('pacsoft')->getPacsoftConfigDataSettings('reference_suffix'); if($suffix == substr($reference,(strlen($suffix)*-1))){ $reference = substr($reference,0,(strlen($reference)-strlen($suffix))); } } $incrementId = $reference; $parcelNo = $parcel->parcelNos; if($incrementId && $parcelNo) { $order = Mage::getModel('sales/order')->loadByIncrementId($incrementId); if($order->getId()) { $create_shipment = ((int)Mage::helper('pacsoft')->getPacsoftConfigDataTracking('create_shipment',$order->getStoreId())) ? true : false; $send_shipment_mail = ((int)Mage::helper('pacsoft')->getPacsoftConfigDataTracking('send_shipment_mail',$order->getStoreId())) ? true : false; $shipment_collection = Mage::getResourceModel('sales/order_shipment_collection'); $shipment_collection->addAttributeToFilter('order_id', $order->getId()); if($shipment_collection->getSize() > 0) { foreach($shipment_collection as $sc) { $addTrack = true; $shipment = Mage::getModel('sales/order_shipment'); $shipment->load($sc->getId()); foreach($shipment->getTracksCollection() as $track) { if(is_array($parcelNo)) { foreach($parcelNo as $key => $pNo) { if($track->getTrackNumber() == $pNo || $track->getNumber() == $pNo) { unset($parcelNo[$key]); } } if(count($parcelNo) == 0) { $addTrack = false; } } else { if($track->getTrackNumber() == $parcelNo || $track->getNumber() == $parcelNo) { $addTrack = false; break; } } } if($addTrack) { if(is_array($parcelNo)) { foreach($parcelNo as $pNo) { Mage::getModel('sales/order_shipment_api')->addTrack($shipment->getIncrementId(), 'postdk', "Post Danmark", $pNo); } } else { Mage::getModel('sales/order_shipment_api')->addTrack($shipment->getIncrementId(), 'postdk', "Post Danmark", $parcelNo); } if($send_shipment_mail) { $shipment = Mage::getModel('sales/order_shipment')->loadByIncrementId($shipment->getIncrementId()); $shipment->sendEmail($send_shipment_mail, ''); } } } } elseif($create_shipment && $order->canShip()) { $shipmentIncrementId = Mage::getModel('sales/order_shipment_api')->create($order->getIncrementId()); $shipment = Mage::getModel('sales/order_shipment')->loadByIncrementId($shipmentIncrementId); $shipment->setEmailSent($send_shipment_mail); $transactionSave = Mage::getModel('core/resource_transaction') ->addObject($shipment) ->addObject($shipment->getOrder()) ->save(); if(is_array($parcelNo)) { foreach($parcelNo as $pNo) { Mage::getModel('sales/order_shipment_api')->addTrack($shipment->getIncrementId(), 'postdk', "Post Danmark", $pNo); } } else { Mage::getModel('sales/order_shipment_api')->addTrack($shipment->getIncrementId(), 'postdk', "Post Danmark", $parcelNo); } if($send_shipment_mail) { $shipment = Mage::getModel('sales/order_shipment')->loadByIncrementId($shipment->getIncrementId()); $shipment->sendEmail($send_shipment_mail, ''); } } } } } if(!$result->return->done) { usleep($result->return->minDelay*1000); $fetchId++; $coreConfig = new Mage_Core_Model_Config(); $coreConfig->saveConfig('pacsoft/general/fetchid', $fetchId, 'default', $store_id); $result = $this->__getParcels($sessionid,$fetchId,$store_id); $this->__handleResult($result,$sessionid,$fetchId,$store_id); } } } public function importTrackingCode() { $import = ((int)Mage::helper('pacsoft')->getPacsoftConfigDataTracking('import')) ? true : false; if($import) { $stores = Mage::app()->getStores(); $store_users = array(); foreach($stores as $store) { $user = Mage::helper('pacsoft')->getPacsoftConfigDataLogin('brugerid',$store->getId()); if($user && !isset($store_users[$user])) { $store_users[$user] = $store->getId(); } } foreach($store_users as $user => $store_id) { $params = Mage::helper('pacsoft')->getUrlParams($store_id); $user = $params['user']; $pass = $params['pass']; $fetchId = (Mage::getStoreConfig('pacsoft/general/fetchid',$store->getId())) ? Mage::getStoreConfig('pacsoft/general/fetchid',$store->getId()) : 0; if($import && $user && $pass) { try{ $sessionid = $this->__getSessionId($user,$pass); if($sessionid) { $result = $this->__getParcels($sessionid,$fetchId,$store_id); $this->__handleResult($result,$sessionid,$fetchId,$store_id); } } catch (Exception $e) { Mage::log($e->getMessage(),null,'pacsoft.log'); } } } } } public function collectRates(Mage_Shipping_Model_Rate_Request $request) { if (!$this->getConfigFlag('active')) { return false; } if (!$this->getConfigFlag('include_virtual_price') && $request->getAllItems()) { foreach ($request->getAllItems() as $item) { if ($item->getParentItem()) { continue; } if ($item->getHasChildren() && $item->isShipSeparately()) { foreach ($item->getChildren() as $child) { if ($child->getProduct()->isVirtual() || $item->getProductType() == 'downloadable') { $request->setPackageValue($request->getPackageValue() - $child->getBaseRowTotal()); } } } elseif ($item->getProduct()->isVirtual() || $item->getProductType() == 'downloadable') { $request->setPackageValue($request->getPackageValue() - $item->getBaseRowTotal()); } } } $freeQty = 0; if ($request->getAllItems()) { foreach ($request->getAllItems() as $item) { if ($item->getProduct()->isVirtual() || $item->getParentItem()) { continue; } if ($item->getHasChildren() && $item->isShipSeparately()) { foreach ($item->getChildren() as $child) { if ($child->getFreeShipping() && !$child->getProduct()->isVirtual()) { $freeQty += $item->getQty() * ($child->getQty() - (is_numeric($child->getFreeShipping()) ? $child->getFreeShipping() : 0)); } } } elseif ($item->getFreeShipping()) { $freeQty += ($item->getQty() - (is_numeric($item->getFreeShipping()) ? $item->getFreeShipping() : 0)); } } } if (!$request->getMRConditionName()) { $request->setMRConditionName($this->getConfigData('condition_name') ? $this->getConfigData('condition_name') : $this->_default_condition_name); } $oldWeight = $request->getPackageWeight(); $oldQty = $request->getPackageQty(); $allow_free_shipping_promotions = $this->getConfigData('allow_free_shipping_promotions'); if ($allow_free_shipping_promotions && !$this->getConfigData('include_free_ship_items')) { $request->setPackageWeight($request->getFreeMethodWeight()); $request->setPackageQty($oldQty - $freeQty); } $result = Mage::getModel('shipping/rate_result'); $ratearray = $this->getRate($request); $freeShipping=false; $free_shipping_threshold = $this->getConfigData('free_shipping_threshold'); if (is_numeric($free_shipping_threshold) && $free_shipping_threshold>0 && $request->getPackageValue()>$free_shipping_threshold) { $freeShipping=true; } if ($allow_free_shipping_promotions && ($request->getFreeShipping() === true || $request->getPackageQty() == $this->getFreeBoxes())) { $freeShipping=true; } if(Mage::getStoreConfig('pacsoft/settings/exclude_greenland_zipcodes_from_dk')) { if($request->getDestCountryId() == "DK" && ($request->getDestPostcode() == 2412 || ($request->getDestPostcode() >= 3900 && $request->getDestPostcode() < 4000))) { return $result; // VI ØNSKER IKKE SENDE TIL GRØNLAND } } foreach ($ratearray as $rate) { if (!empty($rate) && $rate['price'] >= 0) { $method = Mage::getModel('shipping/rate_result_method'); $method->setCarrier('postdk'); $method->setCarrierTitle($this->getConfigData('title')); $method->setMethod('postdk_'.$rate['pk']); $method->setMethodTitle(Mage::helper('pacsoft')->__($rate['title'])); if($freeShipping) { $shippingPrice = $this->getFinalPriceWithHandlingFee('0.00'); } else { $shippingPrice = $this->getFinalPriceWithHandlingFee($rate['price']); } $method->setCost($rate['cost']); $method->setPrice($shippingPrice); $result->append($method); } } Mage::dispatchEvent('pacsoft_rates_result_handle_specific_freeshipping', array( 'quote' => Mage::getSingleton('checkout/cart')->getQuote(), 'result' => $result, )); return $result; } public function getRate(Mage_Shipping_Model_Rate_Request $request) { return Mage::getResourceModel('pacsoft/rate')->getNewRate($request,$this->getConfigFlag('zip_range')); } public function getAllowedMethods() { $method_code = $this->_code; $ratesCollection = Mage::getModel('pacsoft/rate')->getCollection(); $ratesCollection->addFieldToFilter('active',1); $website_names = array(); $websites = Mage::app()->getWebsites(); foreach($websites as $website) { $website_names[$website->getId()] = $website->getName(); } $allowed_rates = array(); foreach($ratesCollection as $rate) { if(!isset($website_names[$rate->getData('website_id')])) { continue; } $title = $rate->getData('title') . " | ".trim($rate->getData('dest_country_id'),",")." - ".$rate->getData('condition_from_value')." - ".$rate->getData('condition_to_value'); if(count($website_names) > 1) { $title .= " | ".$website_names[$rate->getData('website_id')]; } $allowed_rates[$method_code."_".$rate->getData('pk')] = $title; } return $allowed_rates; } public function getCode($type, $code='') { $codes = array( 'condition_name'=>array( 'package_value' => Mage::helper('pacsoft')->__('Price vs. Destination'), 'package_weight' => Mage::helper('pacsoft')->__('Weight vs. Destination'), 'package_qty' => Mage::helper('pacsoft')->__('# of Items vs. Destination'), ), 'condition_name_short'=>array( 'package_value' => Mage::helper('pacsoft')->__('Order Subtotal'), 'package_weight' => Mage::helper('pacsoft')->__('Weight'), 'package_qty' => Mage::helper('pacsoft')->__('# of Items'), ), ); if (!isset($codes[$type])) { throw Mage::exception('Mage_Shipping', Mage::helper('pacsoft')->__('Invalid PostDK Rate code type: %s', $type)); } if (''===$code) { return $codes[$type]; } if (!isset($codes[$type][$code])) { throw Mage::exception('Mage_Shipping', Mage::helper('pacsoft')->__('Invalid PostDK Rate code for type %s: %s', $type, $code)); } return $codes[$type][$code]; } public function getExtraJsSalesRuleScript() { $field = self::FREE_SHIPPING_RATES; $script = "if('$field' == $('rule_simple_action').value) { $('rule_discount_qty').up().up().hide(); $('rule_discount_step').up().up().hide(); $('rule_apply_to_shipping').up().up().hide(); $('rule_simple_free_shipping').up().up().hide(); }"; return $script; } }