public function getPrice(Package $package) { $weight = $package->getWeight(); $volumetricWeight = $this->getVolumetricWeightCalculator()->calculate($package->getDimensions()); $weight = $this->getWeightConverter()->convert($weight->getValue(), $weight->getUnit(), $this->get('mass_unit')); $volumetricWeight = $this->getWeightConverter()->convert($volumetricWeight->getValue(), $volumetricWeight->getUnit(), $this->get('mass_unit')); $math = $this->getMath(); if ($math->greaterThan($volumetricWeight, $weight)) { $weight = $volumetricWeight; } $importCountry = $this->detectImportCountry($package->getRecipientAddress()); $zone = $this->get('zones')[$importCountry->getZone()]; $currentWeight = null; $price = null; foreach ($zone['weight_prices'] as $w => $p) { if ($math->lessOrEqualThan($weight, $w) && $math->greaterThan($weight, $currentWeight)) { $currentWeight = $w; $price = $p; } } if (is_null($price)) { throw new ViolationException('Can not calculate shipping for this weight.'); } return $price; }
/** * @param Package $package * @return ZoneCalculator */ public function getZoneCalculator($package) { $country = $this->getImportCountry($package->getRecipientAddress()->getCountryCode()); $calculators = $this->get('zone_calculators'); if (!array_key_exists($country->getZone(), $calculators)) { throw new InvalidConfigurationException('Price group does not exist.'); } return $calculators[$country->getZone()]; }
<?php use EsteIt\ShippingCalculator\Calculator\BaseCalculator; use EsteIt\ShippingCalculator\Handler\IParcelHandler; use EsteIt\ShippingCalculator\Weight; use EsteIt\ShippingCalculator\Dimensions; use EsteIt\ShippingCalculator\Address; use EsteIt\ShippingCalculator\Package; include_once __DIR__ . '/../vendor/autoload.php'; $config = (include __DIR__ . '/../src/Resources/IParcel/tariff_2015_01_12_usa.php'); $calculator = new BaseCalculator(['handler' => IParcelHandler::create($config)]); $weight = new Weight(); $weight->setValue(10); $weight->setUnit('lb'); $dimensions = new Dimensions(); $dimensions->setLength(10); $dimensions->setWidth(10); $dimensions->setHeight(10); $dimensions->setUnit('in'); $senderAddress = new Address(); $senderAddress->setCountryCode('USA'); $recipientAddress = new Address(); $recipientAddress->setCountryCode('SGP'); $package = new Package(); $package->setWeight($weight); $package->setDimensions($dimensions); $package->setSenderAddress($senderAddress); $package->setRecipientAddress($recipientAddress); $result = $calculator->calculate($package); var_dump($result->getData());