コード例 #1
0
ファイル: Eparcel.php プロジェクト: Zookal/fontis_australia
 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;
 }