Exemplo n.º 1
0
 public function getRate(Mage_Shipping_Model_Rate_Request $request)
 {
     $read = $this->_getReadAdapter();
     $write = $this->_getWriteAdapter();
     $select = $read->select()->from($this->getMainTable());
     /*
     //commented out code since we don't want to get state by using zip code
     if (!$request->getDestCountryId() && !$request->getDestRegionId()) {
     
         // assuming that request is coming from shopping cart
         // for shipping prices pre-estimation...
     
         // also probably it will be required to move this part to
         // Sales/Model/Quote/Address.php !
     
         $selectCountry = $read->select()->from(Mage::getSingleton('core/resource')->getTableName('usa/postcode'), array('country_id', 'region_id'));
         $selectCountry->where('postcode=?', $request->getDestPostcode());
         $selectCountry->limit(1);
         $countryRegion = $read->fetchRow($selectCountry);
         $region = $read->quote($countryRegion['region_id']);
         $country = $read->quote($countryRegion['country_id']);
     } else {
         $region = $read->quote($request->getDestRegionId());
         $country = $read->quote($request->getDestCountryId());
     }
     */
     $region = $read->quote($request->getDestRegionId());
     $country = $read->quote($request->getDestCountryId());
     $zip = $read->quote($request->getDestPostcode());
     $select->where("(dest_zip={$zip})\n                     OR (dest_region_id={$region} AND dest_zip='')\n                     OR (dest_country_id={$country} AND dest_region_id='0' AND dest_zip='')\n                     OR (dest_country_id='0' AND dest_region_id='0' AND dest_zip='')");
     if (is_array($request->getConditionName())) {
         $i = 0;
         foreach ($request->getConditionName() as $conditionName) {
             if ($i == 0) {
                 $select->where('condition_name=?', $conditionName);
             } else {
                 $select->orWhere('condition_name=?', $conditionName);
             }
             $select->where('condition_value<=?', $request->getData($conditionName));
             $i++;
         }
     } else {
         $select->where('condition_name=?', $request->getConditionName());
         $select->where('condition_value<=?', $request->getData($request->getConditionName()));
     }
     $select->where('website_id=?', $request->getWebsiteId());
     $select->order('condition_value DESC')->limit(1);
     $row = $read->fetchRow($select);
     return $row;
 }
Exemplo n.º 2
0
 public function getNewRate(Mage_Shipping_Model_Rate_Request $request, $zipRangeSet = 0)
 {
     $newdata = array();
     $collection = Mage::getResourceModel('matrixrate_shipping/carrier_matrixrate_collection');
     $collection->setConditionFilter($request->getConditionName())->setWebsiteFilter($request->getWebsiteId());
     $collection->getSelect()->reset(Zend_Db_Select::COLUMNS)->columns(array('website_id', 'zone', 'condition_name', 'condition_from_value', 'condition_to_value', 'shipping_charge'));
     $collection->getSelect()->join(array('zones' => 'shipping_zones'), "zones.zone=s.zone and zones.delivery_type='standard' and zones.country_code='" . $request->getDestCountryId() . "' AND (condition_from_value<='" . $request->getData($request->getConditionName()) . "') AND (condition_to_value>='" . $request->getData($request->getConditionName()) . "')", array('delivery_type', 'shipping_provider'));
     //print $collection->getSelect();die;
     if ($collection->count()) {
         foreach ($collection->getData() as $data) {
             $newdata[] = $data;
         }
     }
     return $newdata;
 }
 /**
  * Fetch rate from the table for selected shipping address.
  *
  * @param Mage_Shipping_Model_Rate_Request $request
  * @return array
  */
 public function getRate(Mage_Shipping_Model_Rate_Request $request)
 {
     $adapter = $this->_getReadAdapter();
     $bind = array(':website_id' => (int) $request->getWebsiteId(), ':country_id' => $request->getDestCountryId(), ':region_id' => (int) $request->getDestRegionId(), ':postcode' => $request->getDestPostcode());
     $select = $adapter->select()->from($this->getMainTable())->where('website_id = :website_id')->order(array('dest_country_id DESC', 'dest_region_id DESC', 'dest_zip DESC', 'condition_value DESC'))->limit(1);
     // Render destination condition
     $orWhere = '(' . implode(') OR (', array("dest_country_id = :country_id AND dest_region_id = :region_id AND dest_zip = :postcode", "dest_country_id = :country_id AND dest_region_id = :region_id AND dest_zip = ''", "dest_country_id = :country_id AND dest_region_id = :region_id AND dest_zip = '*'", "dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = '*'", "dest_country_id = '0' AND dest_region_id = :region_id AND dest_zip = '*'", "dest_country_id = '0' AND dest_region_id = 0 AND dest_zip = '*'", "dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = ''", "dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = :postcode", "dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = '*'")) . ')';
     $select->where($orWhere);
     // Render condition by condition name
     if (is_array($request->getConditionName())) {
         $orWhere = array();
         $i = 0;
         foreach ($request->getConditionName() as $conditionName) {
             $bindNameKey = sprintf(':condition_name_%d', $i);
             $bindValueKey = sprintf(':condition_value_%d', $i);
             $orWhere[] = "(condition_name = {$bindNameKey} AND condition_value <= {$bindValueKey})";
             $bind[$bindNameKey] = $conditionName;
             $bind[$bindValueKey] = $request->getData($conditionName);
             $i++;
         }
         if ($orWhere) {
             $select->where(implode(' OR ', $orWhere));
         }
     } else {
         $bind[':condition_name'] = $request->getConditionName();
         $bind[':condition_value'] = $request->getData($request->getConditionName());
         $select->where('condition_name = :condition_name');
         $select->where('condition_value <= :condition_value');
     }
     $result = $adapter->fetchRow($select, $bind);
     // Normalize destination zip code
     if ($result && $result['dest_zip'] == '*') {
         $result['dest_zip'] = '';
     }
     return $result;
 }
Exemplo n.º 4
0
 /**
  * Return table rate array or false by rate request
  *
  * @param Mage_Shipping_Model_Rate_Request $request
  * @return array|false
  */
 public function getRate(Mage_Shipping_Model_Rate_Request $request)
 {
     $adapter = $this->_getReadAdapter();
     $bind = array(':website_id' => (int) $request->getWebsiteId(), ':country_id' => $request->getDestCountryId(), ':region_id' => $request->getDestRegionId());
     $select = $adapter->select()->from($this->getMainTable())->where('website_id=:website_id')->order(array('dest_country_id DESC', 'dest_region_id DESC', 'dest_zip DESC'))->limit(1);
     // render destination condition
     $orWhere = '(' . implode(') OR (', array("dest_country_id = :country_id AND dest_region_id = :region_id AND dest_zip like '{$request->getDestPostcode()}%'", "dest_country_id = :country_id AND dest_region_id = :region_id AND dest_zip = ''", "dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = ''", "dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip like '{$request->getDestPostcode()}%'", "dest_country_id = '0' AND dest_region_id = 0 AND dest_zip = ''")) . ')';
     $select->where($orWhere);
     // render condition by condition name
     if (is_array($request->getConditionName())) {
         $orWhere = array();
         $i = 0;
         foreach ($request->getConditionName() as $conditionName) {
             $bindNameKey = sprintf(':condition_name_%d', $i);
             $bindValueKey = sprintf(':condition_value_%d', $i);
             $orWhere[] = "(condition_name = {$bindNameKey} AND condition_value <= {$bindValueKey})";
             $bind[$bindNameKey] = $conditionName;
             $bind[$bindValueKey] = $request->getData($conditionName);
             $i++;
         }
         if ($orWhere) {
             $select->where(implode(' OR ', $orWhere));
         }
     } else {
         $bind[':condition_name'] = $request->getConditionName();
         $bind[':condition_value'] = $request->getData($request->getConditionName());
         $select->where('condition_name = :condition_name');
         $select->where('condition_value <= :condition_value');
     }
     //if( $_SERVER['REMOTE_ADDR'] == '193.108.122.187') { mage::D($bind ); mage::d($select->__toString()); }
     return $adapter->fetchRow($select, $bind);
 }
Exemplo n.º 5
0
 public function getRate(Mage_Shipping_Model_Rate_Request $request)
 {
     $read = $this->_getReadAdapter();
     $postcode = $request->getDestPostcode();
     $table = $this->getMainTable();
     $storeId = $request->getStoreId();
     $insuranceStep = (double) Mage::getStoreConfig('carriers/eparcel/insurance_step', $storeId);
     $insuranceCostPerStep = (double) Mage::getStoreConfig('carriers/eparcel/insurance_cost_per_step', $storeId);
     $signatureRequired = Mage::getStoreConfigFlag('carriers/eparcel/signature_required', $storeId);
     if ($signatureRequired) {
         $signatureCost = (double) Mage::getStoreConfig('carriers/eparcel/signature_cost', $storeId);
     } else {
         $signatureCost = 0;
     }
     for ($j = 0; $j < 5; $j++) {
         $select = $read->select()->from($table);
         // Support for Multi Warehouse Extension.
         if ($request->getWarehouseId() > 0) {
             $select->where('stock_id = ?', $request->getWarehouseId());
         }
         switch ($j) {
             case 0:
                 $select->where($read->quoteInto(" (dest_country_id=? ", $request->getDestCountryId()) . $read->quoteInto(" AND dest_region_id=? ", $request->getDestRegionId()) . $read->quoteInto(" AND dest_zip=?) ", $postcode));
                 break;
             case 1:
                 $select->where($read->quoteInto("  (dest_country_id=? ", $request->getDestCountryId()) . $read->quoteInto(" AND dest_region_id=? AND dest_zip='0000') ", $request->getDestRegionId()));
                 break;
             case 2:
                 $select->where($read->quoteInto("  (dest_country_id=? AND dest_region_id='0' AND dest_zip='0000') ", $request->getDestCountryId()));
                 break;
             case 3:
                 $select->where($read->quoteInto("  (dest_country_id=? AND dest_region_id='0' ", $request->getDestCountryId()) . $read->quoteInto("  AND dest_zip=?) ", $postcode));
                 break;
             case 4:
                 $select->where("  (dest_country_id='0' AND dest_region_id='0' AND dest_zip='0000')");
                 break;
         }
         if (is_array($request->getConditionName())) {
             $i = 0;
             foreach ($request->getConditionName() as $conditionName) {
                 if ($i == 0) {
                     $select->where('condition_name=?', $conditionName);
                 } else {
                     $select->orWhere('condition_name=?', $conditionName);
                 }
                 $select->where('condition_from_value<=?', $request->getData($conditionName));
                 $select->where('condition_to_value>=?', $request->getData($conditionName));
                 $i++;
             }
         } else {
             $select->where('condition_name=?', $request->getConditionName());
             $select->where('condition_from_value<=?', $request->getData($request->getConditionName()));
             $select->where('condition_to_value>=?', $request->getData($request->getConditionName()));
         }
         $select->where('website_id=?', $request->getWebsiteId());
         $select->order('dest_country_id DESC');
         $select->order('dest_region_id DESC');
         $select->order('dest_zip DESC');
         $select->order('condition_from_value DESC');
         // pdo has an issue. we cannot use bind
         $newdata = array();
         $row = $read->fetchAll($select);
         if (!empty($row) && $j < 5) {
             // have found a result or found nothing and at end of list!
             foreach ($row as $data) {
                 try {
                     $price = (double) $data['price'];
                     // add per-Kg cost
                     $conditionValue = (double) $request->getData($request->getConditionName());
                     $price += (double) $data['price_per_kg'] * $conditionValue;
                     // add signature cost
                     $price += $signatureCost;
                     // add version without insurance
                     $data['price'] = (string) $price;
                     $newdata[] = $data;
                     if (Mage::getStoreConfig('carriers/eparcel/insurance_enable', $storeId)) {
                         // add version with insurance
                         // work out how many insurance 'steps' we have
                         $steps = ceil($request->getPackageValue() / $insuranceStep);
                         // add on number of 'steps' multiplied by the
                         // insurance cost per step
                         $insuranceCost = $insuranceCostPerStep * $steps;
                         $price += $insuranceCost;
                         $data['price'] = (string) $price;
                         $data['delivery_type'] .= " with TransitCover";
                         $newdata[] = $data;
                     }
                 } catch (Exception $e) {
                     Mage::log($e->getMessage());
                 }
             }
             break;
         }
     }
     return $newdata;
 }
Exemplo n.º 6
0
 /**
  * Generation Shipment Details Node according to origin region
  *
  * @param SimpleXMLElement $xml
  * @param Mage_Shipping_Model_Rate_Request $rawRequest
  * @param string $originRegion
  * @return void
  */
 protected function _shipmentDetails($xml, $rawRequest, $originRegion = '')
 {
     $nodeShipmentDetails = $xml->addChild('ShipmentDetails', '', '');
     $nodeShipmentDetails->addChild('NumberOfPieces', count($rawRequest->getPackages()));
     if ($originRegion) {
         $nodeShipmentDetails->addChild('CurrencyCode', Mage::app()->getWebsite($this->_request->getWebsiteId())->getBaseCurrencyCode());
     }
     $nodePieces = $nodeShipmentDetails->addChild('Pieces', '', '');
     /*
      * Package type
      * EE (DHL Express Envelope), OD (Other DHL Packaging), CP (Custom Packaging)
      * DC (Document), DM (Domestic), ED (Express Document), FR (Freight)
      * BD (Jumbo Document), BP (Jumbo Parcel), JD (Jumbo Junior Document)
      * JP (Jumbo Junior Parcel), PA (Parcel), DF (DHL Flyer)
      */
     $i = 0;
     foreach ($rawRequest->getPackages() as $package) {
         $nodePiece = $nodePieces->addChild('Piece', '', '');
         $packageType = 'EE';
         if ($package['params']['container'] == self::DHL_CONTENT_TYPE_NON_DOC) {
             $packageType = 'CP';
         }
         $nodePiece->addChild('PieceID', ++$i);
         $nodePiece->addChild('PackageType', $packageType);
         $nodePiece->addChild('Weight', round($package['params']['weight'], 1));
         $params = $package['params'];
         if ($params['width'] && $params['length'] && $params['height']) {
             if (!$originRegion) {
                 $nodePiece->addChild('Width', round($params['width']));
                 $nodePiece->addChild('Height', round($params['height']));
                 $nodePiece->addChild('Depth', round($params['length']));
             } else {
                 $nodePiece->addChild('Depth', round($params['length']));
                 $nodePiece->addChild('Width', round($params['width']));
                 $nodePiece->addChild('Height', round($params['height']));
             }
         }
         $content = array();
         foreach ($package['items'] as $item) {
             $content[] = $item['name'];
         }
         $nodePiece->addChild('PieceContents', substr(implode(',', $content), 0, 34));
     }
     if (!$originRegion) {
         $nodeShipmentDetails->addChild('Weight', round($rawRequest->getPackageWeight(), 1));
         $nodeShipmentDetails->addChild('WeightUnit', substr($this->_getWeightUnit(), 0, 1));
         $nodeShipmentDetails->addChild('GlobalProductCode', $rawRequest->getShippingMethod());
         $nodeShipmentDetails->addChild('LocalProductCode', $rawRequest->getShippingMethod());
         $nodeShipmentDetails->addChild('Date', Mage::getModel('core/date')->date(self::REQUEST_DATE_FORMAT));
         $nodeShipmentDetails->addChild('Contents', 'DHL Parcel');
         /*
          * The DoorTo Element defines the type of delivery service that applies to the shipment.
          * The valid values are DD (Door to Door), DA (Door to Airport) , AA and DC (Door to
          * Door non-compliant)
          */
         $nodeShipmentDetails->addChild('DoorTo', 'DD');
         $nodeShipmentDetails->addChild('DimensionUnit', substr($this->_getDimensionUnit(), 0, 1));
         if ($package['params']['container'] == self::DHL_CONTENT_TYPE_NON_DOC) {
             $packageType = 'CP';
         }
         $nodeShipmentDetails->addChild('PackageType', $packageType);
         if ($this->getConfigData('content_type') == self::DHL_CONTENT_TYPE_NON_DOC) {
             $nodeShipmentDetails->addChild('IsDutiable', 'Y');
         }
         $nodeShipmentDetails->addChild('CurrencyCode', Mage::app()->getWebsite($this->_request->getWebsiteId())->getBaseCurrencyCode());
     } else {
         if ($package['params']['container'] == self::DHL_CONTENT_TYPE_NON_DOC) {
             $packageType = 'CP';
         }
         $nodeShipmentDetails->addChild('PackageType', $packageType);
         $nodeShipmentDetails->addChild('Weight', $rawRequest->getPackageWeight());
         $nodeShipmentDetails->addChild('DimensionUnit', substr($this->_getDimensionUnit(), 0, 1));
         $nodeShipmentDetails->addChild('WeightUnit', substr($this->_getWeightUnit(), 0, 1));
         $nodeShipmentDetails->addChild('GlobalProductCode', $rawRequest->getShippingMethod());
         $nodeShipmentDetails->addChild('LocalProductCode', $rawRequest->getShippingMethod());
         /*
          * The DoorTo Element defines the type of delivery service that applies to the shipment.
          * The valid values are DD (Door to Door), DA (Door to Airport) , AA and DC (Door to
          * Door non-compliant)
          */
         $nodeShipmentDetails->addChild('DoorTo', 'DD');
         $nodeShipmentDetails->addChild('Date', Mage::getModel('core/date')->date(self::REQUEST_DATE_FORMAT));
         $nodeShipmentDetails->addChild('Contents', 'DHL Parcel');
     }
 }
Exemplo n.º 7
0
 public function getNewRate(Mage_Shipping_Model_Rate_Request $request, $zipRangeSet = 0)
 {
     $read = $this->_getReadAdapter();
     $write = $this->_getWriteAdapter();
     $postcode = $request->getDestPostcode();
     $table = Mage::getSingleton('core/resource')->getTableName('matrixrate_shipping/matrixrate');
     if ($zipRangeSet && is_numeric($postcode)) {
         #  Want to search for postcodes within a range
         $zipSearchString = ' AND ' . $postcode . ' BETWEEN dest_zip AND dest_zip_to )';
     } else {
         $zipSearchString = $read->quoteInto(" AND ? LIKE dest_zip )", $postcode);
     }
     for ($j = 0; $j < 10; $j++) {
         $select = $read->select()->from($table);
         switch ($j) {
             case 0:
                 $select->where($read->quoteInto(" (dest_country_id=? ", $request->getDestCountryId()) . $read->quoteInto(" AND dest_region_id=? ", $request->getDestRegionId()) . $read->quoteInto(" AND STRCMP(LOWER(dest_city),LOWER(?)) = 0  ", $request->getDestCity()) . $zipSearchString);
                 break;
             case 1:
                 $select->where($read->quoteInto(" (dest_country_id=? ", $request->getDestCountryId()) . $read->quoteInto(" AND dest_region_id=?  AND dest_city=''", $request->getDestRegionId()) . $zipSearchString);
                 break;
             case 2:
                 $select->where($read->quoteInto(" (dest_country_id=? ", $request->getDestCountryId()) . $read->quoteInto(" AND dest_region_id=? ", $request->getDestRegionId()) . $read->quoteInto(" AND STRCMP(LOWER(dest_city),LOWER(?)) = 0  AND dest_zip='')", $request->getDestCity()));
                 break;
             case 3:
                 $select->where($read->quoteInto("  (dest_country_id=? ", $request->getDestCountryId()) . $read->quoteInto(" AND STRCMP(LOWER(dest_city),LOWER(?)) = 0  AND dest_region_id='0'", $request->getDestCity()) . $zipSearchString);
                 break;
             case 4:
                 $select->where($read->quoteInto("  (dest_country_id=? ", $request->getDestCountryId()) . $read->quoteInto(" AND STRCMP(LOWER(dest_city),LOWER(?)) = 0  AND dest_region_id='0' AND dest_zip='') ", $request->getDestCity()));
                 break;
             case 5:
                 $select->where($read->quoteInto("  (dest_country_id=? AND dest_region_id='0' AND dest_city='' ", $request->getDestCountryId()) . $zipSearchString);
                 break;
             case 6:
                 $select->where($read->quoteInto("  (dest_country_id=? ", $request->getDestCountryId()) . $read->quoteInto(" AND dest_region_id=? AND dest_city='' AND dest_zip='') ", $request->getDestRegionId()));
                 break;
             case 7:
                 $select->where($read->quoteInto("  (dest_country_id=? AND dest_region_id='0' AND dest_city='' AND dest_zip='') ", $request->getDestCountryId()));
                 break;
             case 8:
                 $select->where("  (dest_country_id='0' AND dest_region_id='0'" . $zipSearchString);
                 break;
             case 9:
                 $select->where("  (dest_country_id='0' AND dest_region_id='0' AND dest_zip='')");
                 break;
         }
         if (is_array($request->getMRConditionName())) {
             $i = 0;
             foreach ($request->getMRConditionName() as $conditionName) {
                 if ($i == 0) {
                     $select->where('condition_name=?', $conditionName);
                 } else {
                     $select->orWhere('condition_name=?', $conditionName);
                 }
                 $select->where('condition_from_value<=?', $request->getData($conditionName));
                 $i++;
             }
         } else {
             $select->where('condition_name=?', $request->getMRConditionName());
             $select->where('condition_from_value<=?', $request->getData($request->getMRConditionName()));
             $select->where('condition_to_value>=?', $request->getData($request->getMRConditionName()));
         }
         $select->where('website_id=?', $request->getWebsiteId());
         if ($filter = $request->getData('filter')) {
             $select->where('filter IN (?)', $filter);
         }
         $select->order('dest_country_id DESC');
         $select->order('dest_region_id DESC');
         $select->order('dest_zip DESC');
         $select->order('condition_from_value DESC');
         /*
         pdo has an issue. we cannot use bind
         */
         $newdata = array();
         $row = $read->fetchAll($select);
         if (!empty($row)) {
             // have found a result or found nothing and at end of list!
             foreach ($row as $data) {
                 $newdata[] = $data;
             }
             break;
         }
     }
     return $newdata;
 }
 /**
  * Collect rate to get shipping method
  *
  * @param Mage_Shipping_Model_Rate_Request $request
  * @return Mage_Shipping_Model_Rate_Request $request
  */
 public function collectRates(Mage_Shipping_Model_Rate_Request $request)
 {
     $result = "";
     $ship_price = 0;
     if (!$this->getConfigFlag('active')) {
         return false;
     }
     $website_id = (int) $request->getWebsiteId();
     // Default condition Name: Weight vs. Destination
     $weight = $request->getPackageWeight();
     // Check Weight Limit
     if ($this->getConfigFlag('active_weight_limit') && $weight >= $this->getConfigData('weight_limit')) {
         return false;
     }
     // Condition Name: Price Vs. Destination
     if ($this->getConfigData('condition_name') != $this->_default_condition_name) {
         // The weight is now the price
         $weight = Mage::helper('mmshippingplus')->getOrderAmount();
     }
     // Get country, region and postcode data
     $country = Mage::helper('mmshippingplus')->getCustomerCountryCode();
     $region = Mage::helper('mmshippingplus')->getCustomerRegionCode();
     $postcode = Mage::helper('mmshippingplus')->getCustomerPostcode();
     // Free shipping by qty
     $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->getConditionName()) {
         $request->setConditionName($this->getConfigData('condition_name') ? $this->getConfigData('condition_name') : $this->_default_condition_name);
     }
     // Check tablerate with condition
     $tablerate = Mage::getModel('mmshippingplus/shippingplus')->getCollection()->setOrder('weight', 'DESC')->addFieldToFilter('website_id', array('in' => $website_id))->addFieldToFilter('dest_country', array('in' => array('*', $country)))->addFieldToFilter('dest_zip', array('in' => array('*', $postcode)))->addFieldToFilter('dest_region', array('in' => array('*', $region)))->addFieldToFilter('weight', array('lteq' => $weight))->addFieldToFilter('type', array('eq' => $this->getConfigData('condition_name')));
     // Tablerate price
     $ship_price = $tablerate->getFirstItem()->getPrice();
     // Price with shipping weight range
     if ($this->getConfigFlag('active_ship_kg')) {
         if ($this->getConfigData('ship_kg_country')) {
             $kg_country = explode(',', $this->getConfigData('ship_kg_country'));
         }
         $country = Mage::helper('mmshippingplus')->getCustomerCountryCode(2);
         if (in_array($country, $kg_country)) {
             if ($weight >= $this->getConfigData('ship_from_kg') && $weight <= $this->getConfigData('ship_to_kg')) {
                 $ship_price = $this->getConfigData('ship_kg_price');
             }
         }
     }
     // Price with shipping price range
     if ($this->getConfigFlag('active_ship_price')) {
         if ($this->getConfigData('ship_price_country')) {
             $price_country = explode(',', $this->getConfigData('ship_price_country'));
         }
         $country = Mage::helper('mmshippingplus')->getCustomerCountryCode(2);
         if (in_array($country, $price_country)) {
             $amount = Mage::helper('mmshippingplus')->getOrderAmount();
             if ($amount >= $this->getConfigData('ship_from_price') && $amount <= $this->getConfigData('ship_to_price')) {
                 $ship_price = $this->getConfigData('ship_price_price');
             }
         }
     }
     if (!is_null($ship_price) && $ship_price != 0) {
         // Free shipping by tablerate
         $ship_price = $ship_price == $this->_free_ship_tablerate ? 0 : $ship_price;
         // Check if price has charge
         $charge = $tablerate->getFirstItem()->getCharge();
         if ($charge > 0) {
             $amount = Mage::helper('mmshippingplus')->getOrderAmount(MMind_Shippingplus_Model_Config_Source_Rangeprice::TYPE_SUBTOTAL);
             // Charge type
             if ($this->getConfigData('charge_type') == MMind_Shippingplus_Model_Config_Source_Charge::TYPE_CHARGE_FIX) {
                 // Fix price
                 $ship_price += $charge;
             } else {
                 // Percentage price
                 $ship_price += $amount * $charge / 100;
             }
         }
         // Package weight and qty free shipping
         $oldWeight = $request->getPackageWeight();
         $oldQty = $request->getPackageQty();
         $request->setPackageWeight($request->getFreeMethodWeight());
         $request->setPackageQty($oldQty - $freeQty);
         $result = Mage::getModel('shipping/rate_result');
         $request->setPackageWeight($oldWeight);
         $request->setPackageQty($oldQty);
         $method = Mage::getModel('shipping/rate_result_method');
         $method->setCarrier($this->_code);
         $method->setCarrierTitle($this->getConfigData('title'));
         $method->setMethod($this->_code);
         $method->setMethodTitle($this->getConfigData('method_name'));
         if ($request->getFreeShipping() === true || $request->getPackageQty() == $freeQty) {
             $ship_price = 0;
         }
         $method->setPrice($ship_price);
         $method->setCost(0);
         $result->append($method);
         return $result;
     } else {
         // View method also with zero price
         if ($this->getConfigData('active_shipping_zeroprice')) {
             // Package weight and qty free shipping
             $oldWeight = $request->getPackageWeight();
             $oldQty = $request->getPackageQty();
             $request->setPackageWeight($request->getFreeMethodWeight());
             $request->setPackageQty($oldQty - $freeQty);
             $result = Mage::getModel('shipping/rate_result');
             $request->setPackageWeight($oldWeight);
             $request->setPackageQty($oldQty);
             $method = Mage::getModel('shipping/rate_result_method');
             $method->setCarrier($this->_code);
             $method->setCarrierTitle($this->getConfigData('title'));
             $method->setMethod($this->_code);
             $method->setMethodTitle($this->getConfigData('method_name'));
             $ship_price = 0;
             $method->setPrice($ship_price);
             $method->setCost(0);
             $result->append($method);
             return $result;
         }
     }
 }
Exemplo n.º 9
0
 /**
  * Return table rate array or false by rate request
  *
  * @param Mage_Shipping_Model_Rate_Request $request
  *
  * @return array|false
  */
 public function getRate(Mage_Shipping_Model_Rate_Request $request)
 {
     $adapter = $this->_getReadAdapter();
     $bind = array(':website_id' => (int) $request->getWebsiteId(), ':country_id' => $request->getDestCountryId(), ':region_id' => $request->getDestRegionId(), ':postcode' => $request->getDestPostcode(), ':weight' => (double) $request->getPackageWeight(), ':price' => (double) $request->getData('zitec_table_price'));
     $select = $adapter->select()->from($this->getMainTable())->where('website_id=:website_id')->order(array('dest_country_id DESC', 'dest_region_id DESC', 'dest_zip DESC', 'method DESC', 'price_vs_dest DESC', 'weight DESC'));
     // render destination condition
     $orWhere = '(' . implode(') OR (', array("dest_country_id = :country_id AND dest_region_id = :region_id AND dest_zip = :postcode", "dest_country_id = :country_id AND dest_region_id = :region_id AND dest_zip = ''", "dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = ''", "dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = :postcode", "dest_country_id = '0' AND dest_region_id = 0 AND dest_zip = ''")) . ')';
     $select->where($orWhere);
     $select->where('((weight <= :weight and price_vs_dest = 0) or (weight <= :price and price_vs_dest = 1))');
     $rates = $adapter->fetchAll($select, $bind);
     if (empty($rates)) {
         $rates = Mage::getModel('zitec_dpd/config_source_service')->getDefaultShipingRates();
     }
     return $rates;
 }
Exemplo n.º 10
0
 public function collectRates(Mage_Shipping_Model_Rate_Request $request)
 {
     //pega os dados de entrega
     /*
                     [dest_country_id] => US
                     [dest_region_id] => 3
                     [dest_region_code] => AS
                     [dest_street] => teste
                     [dest_city] => São lourenço
                     [dest_postcode] => 43243
                     [package_value] => 2
                     [package_value_with_discount] => 2
                     [package_weight] => 180
                     [package_qty] => 2
                     [package_physical_value] => 2
                     [free_method_weight] => 180
                     [store_id] => 1
                     [website_id] => 1
                     [free_shipping] => 0
                     [base_subtotal_incl_tax] => 2
                     [country_id] => US
                     [region_id] => 12
                     [postcode] => 90034
     */
     $cep = str_replace('-', '', $request->getDestPostcode());
     $this->_condicoes[] = array('pais', 'finset', $request->getDestCountryId());
     $this->_condicoes[] = array('estado', 'finset', $request->getDestRegionCode());
     $this->_condicoes[] = array('cep_de', 'lteq', $cep);
     $this->_condicoes[] = array('cep_ate', 'gteq', $cep);
     $this->_condicoes[] = array('website', 'finset', $request->getWebsiteId());
     $this->_condicoes[] = array('cidade', 'like', $request->getDestCity());
     $produtoEnviavel = false;
     foreach ($request->getAllItems() as $item) {
         /*
             [item_id] => 10
             [quote_id] => 9
             [product_id] => 2
             [store_id] => 1
             [is_virtual] => 0
             [sku] => 324
             [name] => Teste 2
             [free_shipping] =>
             [weight] => 90.0000
             [qty] => 1
             [price] => 1
             [base_price] => 1
             [base_row_total] => 1
             [row_weight] => 90
             [product_type] => simple
         */
         $pesoTotal = 0;
         //Verifica o tipo de produto
         if ($item->getProductType() != Mage_Catalog_Model_Product_Type::TYPE_VIRTUAL && $item->getProductType() != 'downloadable') {
             $produtoEnviavel = true;
             //Se calculo for por produto, adiciona o peso de cada produto a condição
             if ($this->getConfigData('fretePorProduto')) {
                 $this->_condicoes[] = array('peso_de', 'lteq', $item->getWeight());
                 $this->_condicoes[] = array('peso_ate', 'gteq', $item->getWeight());
             }
             //pega o peso total dos produtos
             $pesoTotal += $item->getWeight() * $item->getQty();
             $quantidadeTotal += $item->getQty();
         }
     }
     //caso o frete não seja por produto, adiciona a condição do peso total dos produtos
     if (!$this->getConfigData('fretePorProduto')) {
         $this->_condicoes[] = array('peso_de', 'lteq', $pesoTotal);
         $this->_condicoes[] = array('peso_ate', 'gteq', $pesoTotal);
     }
     if ($produtoEnviavel) {
         $result = Mage::getModel('shipping/rate_result');
         //verifica quais regras se encaixam nas condições adicionadas acima
         $regras = $this->getRegras();
         foreach ($regras as $regra) {
             $method = Mage::getModel('shipping/rate_result_method');
             $method->setCarrier($this->_code);
             $method->setCarrierTitle($this->getConfigData('title'));
             $method->setMethod($this->_code);
             $method->setMethodTitle($regra->getTitulo());
             if ($this->getConfigData('fretePorProduto')) {
                 $method->setPrice($regra->getValor() * $quantidadeTotal);
                 $method->setCost($regra->getCusto() * $quantidadeTotal);
             } else {
                 $method->setPrice($regra->getValor());
                 $method->setCost($regra->getCusto());
             }
             $result->append($method);
         }
         return $result;
     } else {
         //se não houver nenhum produto que possa ser enviado, finaliza o modelo
         return true;
     }
 }