Example #1
0
 public function getRates()
 {
     // Weight in kg
     // $this->weight
     // shipping destination postal code
     // $this->destZip
     // shipping destination country
     // $this->destCountry
     $result = new ShippingRateSet();
     $r1->setServiceName('Fixed Cost Delivery');
     $r1->setCost(100, 'USD');
     $r1->setClassName(get_class($this));
     $r1->setProviderName($this->getProviderName());
     $result->add($r1);
     $r1->setServiceName('Calculated Cost Delivery');
     if ($this->weight < 10) {
         $cost = $this->weight * 2;
     } else {
         if ($this->weight < 20) {
             $cost = $this->weight * 1.5;
         } else {
             $cost = ceil($this->weight / 10) * 10 * 1.5;
         }
     }
     $r2->setCost($cost, 'USD');
     $r2->setClassName(get_class($this));
     $r2->setProviderName($this->getProviderName());
     $result->add($r2);
     return $result;
 }
Example #2
0
 public static function getRealTimeRates(ShippingRateCalculator $handler, Shipment $shipment)
 {
     $rates = new ShippingRateSet();
     $handler->setWeight($shipment->getChargeableWeight());
     $order = $shipment->order->get();
     if ($order->isMultiAddress->get()) {
         $address = $shipment->shippingAddress->get();
     } else {
         $address = $order->shippingAddress->get();
     }
     if (!$address) {
         return $rates;
     }
     $handler->setDestCountry($address->countryID->get());
     $handler->setDestState($address->state->get() ? $address->state->get()->code->get() : $address->stateName->get());
     $handler->setDestZip($address->postalCode->get());
     $config = $shipment->getApplication()->getConfig();
     $handler->setSourceCountry($config->get('STORE_COUNTRY'));
     $handler->setSourceZip($config->get('STORE_ZIP'));
     $handler->setSourceState($config->get('STORE_STATE'));
     foreach ($handler->getAllRates() as $k => $rate) {
         $newRate = new ShipmentDeliveryRate();
         $newRate->setApplication($shipment->getApplication());
         $newRate->setCost($rate->getCostAmount(), $rate->getCostCurrency());
         $newRate->setServiceName($rate->getServiceName());
         $newRate->setClassName($rate->getClassName());
         $newRate->setProviderName($rate->getProviderName());
         $newRate->setServiceId($rate->getClassName() . '_' . $k);
         $rates->add($newRate);
     }
     return $rates;
 }
Example #3
0
 private function getRatesByType(Fedex $fedex, $type)
 {
     $fedex->setCarrierCode($type);
     $enabledServices = $this->getConfigValue('enabledServices', null);
     $price = $fedex->getPrice();
     if (isset($price['FDXRATEAVAILABLESERVICESREPLY'][0]['ENTRY'])) {
         $rates = $price['FDXRATEAVAILABLESERVICESREPLY'][0]['ENTRY'];
         $result = new ShippingRateSet();
         foreach ($rates as $price) {
             $r = new ShippingRateResult();
             $code = $price['SERVICE'][0]['VALUE'];
             if (!is_array($enabledServices) || isset($enabledServices[$code])) {
                 $name = self::$names[$code];
                 if (isset($price['DELIVERYDATE'][0]['VALUE'])) {
                     $date = $price['DELIVERYDATE'][0]['VALUE'];
                     $name = $name . ' (' . $date . ')';
                 }
                 $r->setServiceName($name);
                 $cost = $price['ESTIMATEDCHARGES'][0]['DISCOUNTEDCHARGES'][0]['NETCHARGE'][0]['VALUE'];
                 $currency = isset($price['ESTIMATEDCHARGES'][0]['CURRENCYCODE'][0]['VALUE']) ? $price['ESTIMATEDCHARGES'][0]['CURRENCYCODE'][0]['VALUE'] : 'USD';
                 $r->setCost($cost, $currency);
                 $r->setClassName(get_class($this));
                 $r->setProviderName($this->getProviderName());
                 $result->add($r);
             }
         }
     } else {
         $price = $price['FDXRATEAVAILABLESERVICESREPLY'][0];
         $msg = isset($price['ERROR']) ? $price['ERROR'] : (isset($price['SOFTERROR']) ? $price['SOFTERROR'][0]['MESSAGE'] : '');
         $result = new ShippingRateError($msg);
     }
     $result->setRawResponse($price);
     return $result;
 }
Example #4
0
 public function getRates()
 {
     $rates = $this->getAllRates();
     if ($rates instanceof ShippingRateSet) {
         $result = new ShippingRateSet();
         $result->setRawResponse($rates->getRawResponse());
         foreach ($rates as $rate) {
             if (substr($rate->getServiceName(), 0, strlen($this->service)) == $this->service) {
                 $result->add($rate);
             }
         }
     } else {
         $result = $rates;
     }
     return $result;
 }
Example #5
0
 public function getRates()
 {
     include_once dirname(__FILE__) . '/../library/usps/usps.php';
     // Priority mail only supports flat-rate or unspecified containers
     if ('Priority' == $this->service && strpos(strtolower($this->container), 'flat') === false) {
         $this->container = '';
     }
     $usps = new USPSHandler();
     $usps->setServer($this->getConfigValue('server', 'http://production.shippingapis.com/ShippingAPI.dll'));
     $usps->setUserName($this->getConfigValue('userId'));
     $usps->setOrigZip($this->sourceZip);
     $usps->setDestZip($this->destZip);
     $country = isset($this->countries[$this->destCountry]) ? $this->countries[$this->destCountry] : 'USA';
     $usps->setCountry($country);
     // get weight in pounds/ounces
     $weight = $this->weight * 1000;
     $pounds = floor($weight / 453.59237);
     $ounces = ceil($weight % 453.59237 / 28.3495231);
     $usps->setWeight($pounds, $ounces);
     $usps->setMachinable($this->getConfigValue('isMachinable') ? 'TRUE' : 'FALSE');
     $usps->setService($this->service);
     $usps->setSize($this->getConfigValue('size', 'Regular'));
     if (!empty($this->container)) {
         $usps->setContainer($this->container);
     }
     $price = $usps->getPrice();
     // success
     if (isset($price->list)) {
         $result = new ShippingRateSet();
         foreach ($price->list as $rate) {
             $r = new ShippingRateResult();
             $type = $rate->svcdescription;
             $type = str_replace(array('&lt', ';sup', '&gt;', '&amp;', 'amp;', 'reg;', ';/sup', 'trade;'), '', $type);
             $type = str_replace('**', '', $type);
             $r->setServiceName(isset($rate->mailservice) ? $rate->mailservice : $type . ' (' . $rate->svccommitments . ')');
             $r->setCost($rate->rate, 'USD');
             $r->setClassName(get_class($this));
             $r->setProviderName($this->getProviderName());
             $result->add($r);
         }
     } else {
         $errorMsg = isset($price->error) ? $price->error->description : '';
         $result = new ShippingRateError($errorMsg);
     }
     $result->setRawResponse($price);
     return $result;
 }
Example #6
0
 public function getRates()
 {
     $result = new ShippingRateSet();
     foreach ($this->getConfigValue('enabledServices', array()) as $key => $value) {
         $res = $this->getRate($key);
         if ($res) {
             $r = new ShippingRateResult();
             $r->setServiceName(self::$names[$key]);
             $r->setCost($res['MONETARYVALUE'], 'USD', $res['CURRENCYCODE']);
             $r->setClassName(get_class($this));
             $r->setProviderName($this->getProviderName());
             $result->add($r);
         }
     }
     $result->setRawResponse(null);
     return $result;
 }
Example #7
0
 public static function getRealTimeRates(ShippingRateCalculator $handler, Shipment $shipment)
 {
     $rates = new ShippingRateSet();
     $handler->setWeight($shipment->getChargeableWeight());
     $order = $shipment->order->get();
     // TODO: fix issue when address has zip and country data, but are missing city, user and record id!
     //             (now workround - get address id, if $address has no id, load address by id)
     if ($order->isMultiAddress->get()) {
         $address = $shipment->shippingAddress->get();
         $arr = $shipment->toArray();
     } else {
         $address = $order->shippingAddress->get();
         $arr = $order->toArray();
     }
     if (!$address->getID() && array_key_exists('shippingAddressID', $arr)) {
         $address = ActiveRecordModel::getInstanceByID('UserAddress', $arr['shippingAddressID'], true);
     }
     if (!$address) {
         return $rates;
     }
     $handler->setDestCountry($address->countryID->get());
     $handler->setDestState($address->state->get() ? $address->state->get()->code->get() : $address->stateName->get());
     $handler->setDestZip($address->postalCode->get());
     $handler->setDestCity($address->city->get());
     $config = $shipment->getApplication()->getConfig();
     $handler->setSourceCountry($config->get('STORE_COUNTRY'));
     $handler->setSourceZip($config->get('STORE_ZIP'));
     $handler->setSourceState($config->get('STORE_STATE'));
     foreach ($handler->getAllRates() as $k => $rate) {
         $newRate = new ShipmentDeliveryRate();
         $newRate->setApplication($shipment->getApplication());
         $newRate->setCost($rate->getCostAmount(), $rate->getCostCurrency());
         $newRate->setServiceName($rate->getServiceName());
         $newRate->setClassName($rate->getClassName());
         $newRate->setProviderName($rate->getProviderName());
         $newRate->setServiceId($rate->getClassName() . '_' . $k);
         $rates->add($newRate);
     }
     return $rates;
 }
Example #8
0
 /**
  *  Returns both real time and calculated shipping rates for the particular shipment
  *
  *	@return ShippingRateSet
  */
 public function getShippingRates(Shipment $shipment)
 {
     $defined = $this->getDefinedShippingRates($shipment);
     if (!$this->isRealTimeDisabled->get()) {
         $defined->merge($this->getRealTimeRates($shipment));
     }
     // calculate surcharge
     $surcharge = 0;
     foreach ($shipment->getItems() as $item) {
         $surcharge += $item->getProduct()->getParent()->shippingSurchargeAmount->get() * $item->getCount();
     }
     $currency = self::getApplication()->getDefaultCurrency();
     // apply to rates
     foreach ($defined as $rate) {
         $rate->setAmountByCurrency($currency, $rate->getAmountByCurrency($currency) + $surcharge);
     }
     // apply taxes
     foreach ($defined as $rate) {
         $zone = $shipment->getShippingTaxZone();
         $amount = !$zone->isDefault() ? $shipment->applyTaxesToShippingAmount($rate->getCostAmount()) : $rate->getCostAmount();
         $rate->setAmountWithTax($amount);
         $rate->setAmountWithoutTax($shipment->reduceTaxesFromShippingAmount($amount));
     }
     // look for "override" rates
     foreach ($defined as $rate) {
         if ($service = $rate->getService()) {
             if ($service->isFinal->get()) {
                 $rates = new ShippingRateSet();
                 $rates->add($rate);
                 return $rates;
             }
         }
     }
     return $defined;
 }