Mage_Shipping_Model_Mysql4_Carrier_Tablerate Class Reference

Inheritance diagram for Mage_Shipping_Model_Mysql4_Carrier_Tablerate:

Mage_Core_Model_Mysql4_Abstract Mage_Core_Model_Resource_Abstract

List of all members.

Public Member Functions

 getRate (Mage_Shipping_Model_Rate_Request $request)
 uploadAndImport (Varien_Object $object)

Protected Member Functions

 _construct ()
 _getCsvValues ($string, $separator=",")
 _isPositiveDecimalNumber ($n)


Detailed Description

Definition at line 34 of file Tablerate.php.


Member Function Documentation

_construct (  )  [protected]

Resource initialization

Reimplemented from Mage_Core_Model_Resource_Abstract.

Definition at line 36 of file Tablerate.php.

00037     {
00038         $this->_init('shipping/tablerate', 'pk');
00039     }

_getCsvValues ( string,
separator = "," 
) [protected]

Definition at line 266 of file Tablerate.php.

00267     {
00268         $elements = explode($separator, trim($string));
00269         for ($i = 0; $i < count($elements); $i++) {
00270             $nquotes = substr_count($elements[$i], '"');
00271             if ($nquotes %2 == 1) {
00272                 for ($j = $i+1; $j < count($elements); $j++) {
00273                     if (substr_count($elements[$j], '"') > 0) {
00274                         // Put the quoted string's pieces back together again
00275                         array_splice($elements, $i, $j-$i+1, implode($separator, array_slice($elements, $i, $j-$i+1)));
00276                         break;
00277                     }
00278                 }
00279             }
00280             if ($nquotes > 0) {
00281                 // Remove first and last quotes, then merge pairs of quotes
00282                 $qstr =& $elements[$i];
00283                 $qstr = substr_replace($qstr, '', strpos($qstr, '"'), 1);
00284                 $qstr = substr_replace($qstr, '', strrpos($qstr, '"'), 1);
00285                 $qstr = str_replace('""', '"', $qstr);
00286             }
00287             $elements[$i] = trim($elements[$i]);
00288         }
00289         return $elements;
00290     }

_isPositiveDecimalNumber ( n  )  [protected]

Definition at line 292 of file Tablerate.php.

00293     {
00294         return preg_match ("/^[0-9]+(\.[0-9]*)?$/", $n);
00295     }

getRate ( Mage_Shipping_Model_Rate_Request request  ) 

Definition at line 41 of file Tablerate.php.

00042     {
00043         $read = $this->_getReadAdapter();
00044         $write = $this->_getWriteAdapter();
00045 
00046         $select = $read->select()->from($this->getMainTable());
00047         /*
00048         //commented out code since we don't want to get state by using zip code
00049         if (!$request->getDestCountryId() && !$request->getDestRegionId()) {
00050 
00051             // assuming that request is coming from shopping cart
00052             // for shipping prices pre-estimation...
00053 
00054             // also probably it will be required to move this part to
00055             // Sales/Model/Quote/Address.php !
00056 
00057             $selectCountry = $read->select()->from(Mage::getSingleton('core/resource')->getTableName('usa/postcode'), array('country_id', 'region_id'));
00058             $selectCountry->where('postcode=?', $request->getDestPostcode());
00059             $selectCountry->limit(1);
00060             $countryRegion = $read->fetchRow($selectCountry);
00061             $region = $read->quote($countryRegion['region_id']);
00062             $country = $read->quote($countryRegion['country_id']);
00063         } else {
00064             $region = $read->quote($request->getDestRegionId());
00065             $country = $read->quote($request->getDestCountryId());
00066         }
00067         */
00068 //        $bind = array(
00069 //            'zip'       => $read->quote($request->getDestPostcode()),
00070 //            'region'    => $read->quote($request->getDestRegionId()),
00071 //            'country'   => $read->quote($request->getDestCountryId())
00072 //        );
00073         $select->where(
00074             $read->quoteInto(" (dest_country_id=? ", $request->getDestCountryId()).
00075                 $read->quoteInto(" AND dest_region_id=? ", $request->getDestRegionId()).
00076                 $read->quoteInto(" AND dest_zip=?) ", $request->getDestPostcode()).
00077 
00078             $read->quoteInto(" OR (dest_country_id=? ", $request->getDestCountryId()).
00079                 $read->quoteInto(" AND dest_region_id=? AND dest_zip='') ", $request->getDestRegionId()).
00080 
00081             $read->quoteInto(" OR (dest_country_id=? AND dest_region_id='0' AND dest_zip='') ", $request->getDestCountryId()).
00082 
00083             $read->quoteInto(" OR (dest_country_id=? AND dest_region_id='0' ", $request->getDestCountryId()).
00084                 $read->quoteInto("  AND dest_zip=?) ", $request->getDestPostcode()).
00085 
00086             " OR (dest_country_id='0' AND dest_region_id='0' AND dest_zip='')"
00087         );
00088 
00089 //        $select->where("(dest_zip=:zip)
00090 //                     OR (dest_region_id=:region AND dest_zip='')
00091 //                     OR (dest_country_id=:country AND dest_region_id='0' AND dest_zip='')
00092 //                     OR (dest_country_id='0' AND dest_region_id='0' AND dest_zip='')");
00093         if (is_array($request->getConditionName())) {
00094             $i = 0;
00095             foreach ($request->getConditionName() as $conditionName) {
00096                 if ($i == 0) {
00097                     $select->where('condition_name=?', $conditionName);
00098                 } else {
00099                     $select->orWhere('condition_name=?', $conditionName);
00100                 }
00101                 $select->where('condition_value<=?', $request->getData($conditionName));
00102                 $i++;
00103             }
00104         } else {
00105             $select->where('condition_name=?', $request->getConditionName());
00106             $select->where('condition_value<=?', $request->getData($request->getConditionName()));
00107         }
00108         $select->where('website_id=?', $request->getWebsiteId());
00109 
00110         $select->order('dest_country_id DESC');
00111         $select->order('dest_region_id DESC');
00112         $select->order('dest_zip DESC');
00113         $select->order('condition_value DESC');
00114         $select->limit(1);
00115 
00116         /*
00117         pdo has an issue. we cannot use bind
00118         */
00119         $row = $read->fetchRow($select);
00120         return $row;
00121     }

uploadAndImport ( Varien_Object object  ) 

Definition at line 124 of file Tablerate.php.

00125     {
00126         $csvFile = $_FILES["groups"]["tmp_name"]["tablerate"]["fields"]["import"]["value"];
00127 
00128         if (!empty($csvFile)) {
00129 
00130             $csv = trim(file_get_contents($csvFile));
00131 
00132             $table = Mage::getSingleton('core/resource')->getTableName('shipping/tablerate');
00133 
00134             $websiteId = $object->getScopeId();
00135             $websiteModel = Mage::app()->getWebsite($websiteId);
00136             /*
00137             getting condition name from post instead of the following commented logic
00138             */
00139 
00140             if (isset($_POST['groups']['tablerate']['fields']['condition_name']['inherit'])) {
00141                 $conditionName = (string)Mage::getConfig()->getNode('default/carriers/tablerate/condition_name');
00142             } else {
00143                 $conditionName = $_POST['groups']['tablerate']['fields']['condition_name']['value'];
00144             }
00145 
00146 //            $conditionName = $object->getValue();
00147 //            if ($conditionName{0} == '_') {
00148 //                $conditionName = Mage::helper('core/string')->substr($conditionName, 1, strpos($conditionName, '/')-1);
00149 //            } else {
00150 //                $conditionName = $websiteModel->getConfig('carriers/tablerate/condition_name');
00151 //            }
00152             $conditionFullName = Mage::getModel('shipping/carrier_tablerate')->getCode('condition_name_short', $conditionName);
00153             if (!empty($csv)) {
00154                 $exceptions = array();
00155                 $csvLines = explode("\n", $csv);
00156                 $csvLine = array_shift($csvLines);
00157                 $csvLine = $this->_getCsvValues($csvLine);
00158                 if (count($csvLine) < 5) {
00159                     $exceptions[0] = Mage::helper('shipping')->__('Invalid Table Rates File Format');
00160                 }
00161 
00162                 $countryCodes = array();
00163                 $regionCodes = array();
00164                 foreach ($csvLines as $k=>$csvLine) {
00165                     $csvLine = $this->_getCsvValues($csvLine);
00166                     if (count($csvLine) > 0 && count($csvLine) < 5) {
00167                         $exceptions[0] = Mage::helper('shipping')->__('Invalid Table Rates File Format');
00168                     } else {
00169                         $countryCodes[] = $csvLine[0];
00170                         $regionCodes[] = $csvLine[1];
00171                     }
00172                 }
00173 
00174                 if (empty($exceptions)) {
00175                     $data = array();
00176                     $countryCodesToIds = array();
00177                     $regionCodesToIds = array();
00178                     $countryCodesIso2 = array();
00179 
00180                     $countryCollection = Mage::getResourceModel('directory/country_collection')->addCountryCodeFilter($countryCodes)->load();
00181                     foreach ($countryCollection->getItems() as $country) {
00182                         $countryCodesToIds[$country->getData('iso3_code')] = $country->getData('country_id');
00183                         $countryCodesToIds[$country->getData('iso2_code')] = $country->getData('country_id');
00184                         $countryCodesIso2[] = $country->getData('iso2_code');
00185                     }
00186 
00187                     $regionCollection = Mage::getResourceModel('directory/region_collection')
00188                         ->addRegionCodeFilter($regionCodes)
00189                         ->addCountryFilter($countryCodesIso2)
00190                         ->load();
00191 
00192                     foreach ($regionCollection->getItems() as $region) {
00193                         $regionCodesToIds[$countryCodesToIds[$region->getData('country_id')]][$region->getData('code')] = $region->getData('region_id');
00194                     }
00195 
00196                     foreach ($csvLines as $k=>$csvLine) {
00197                         $csvLine = $this->_getCsvValues($csvLine);
00198 
00199                         if (empty($countryCodesToIds) || !array_key_exists($csvLine[0], $countryCodesToIds)) {
00200                             $countryId = '0';
00201                             if ($csvLine[0] != '*' && $csvLine[0] != '') {
00202                                 $exceptions[] = Mage::helper('shipping')->__('Invalid Country "%s" in the Row #%s', $csvLine[0], ($k+1));
00203                             }
00204                         } else {
00205                             $countryId = $countryCodesToIds[$csvLine[0]];
00206                         }
00207 
00208                         if (!isset($countryCodesToIds[$csvLine[0]])
00209                             || !isset($regionCodesToIds[$countryCodesToIds[$csvLine[0]]])
00210                             || !array_key_exists($csvLine[1], $regionCodesToIds[$countryCodesToIds[$csvLine[0]]])) {
00211                             $regionId = '0';
00212                             if ($csvLine[1] != '*' && $csvLine[1] != '') {
00213                                 $exceptions[] = Mage::helper('shipping')->__('Invalid Region/State "%s" in the Row #%s', $csvLine[1], ($k+1));
00214                             }
00215                         } else {
00216                             $regionId = $regionCodesToIds[$countryCodesToIds[$csvLine[0]]][$csvLine[1]];
00217                         }
00218 
00219                         if ($csvLine[2] == '*' || $csvLine[2] == '') {
00220                             $zip = '';
00221                         } else {
00222                             $zip = $csvLine[2];
00223                         }
00224 
00225                         if (!$this->_isPositiveDecimalNumber($csvLine[3]) || $csvLine[3] == '*' || $csvLine[3] == '') {
00226                             $exceptions[] = Mage::helper('shipping')->__('Invalid %s "%s" in the Row #%s', $conditionFullName, $csvLine[3], ($k+1));
00227                         } else {
00228                             $csvLine[3] = (float)$csvLine[3];
00229                         }
00230 
00231                         if (!$this->_isPositiveDecimalNumber($csvLine[4])) {
00232                             $exceptions[] = Mage::helper('shipping')->__('Invalid Shipping Price "%s" in the Row #%s', $csvLine[4], ($k+1));
00233                         } else {
00234                             $csvLine[4] = (float)$csvLine[4];
00235                         }
00236 
00237                         $data[] = array('website_id'=>$websiteId, 'dest_country_id'=>$countryId, 'dest_region_id'=>$regionId, 'dest_zip'=>$zip, 'condition_name'=>$conditionName, 'condition_value'=>$csvLine[3], 'price'=>$csvLine[4]);
00238                         $dataDetails[] = array('country'=>$csvLine[0], 'region'=>$csvLine[1]);
00239                     }
00240                 }
00241                 if (empty($exceptions)) {
00242                     $connection = $this->_getWriteAdapter();
00243 
00244                     $condition = array(
00245                         $connection->quoteInto('website_id = ?', $websiteId),
00246                         $connection->quoteInto('condition_name = ?', $conditionName),
00247                     );
00248                     $connection->delete($table, $condition);
00249 
00250                     foreach($data as $k=>$dataLine) {
00251                         try {
00252                             $connection->insert($table, $dataLine);
00253                         } catch (Exception $e) {
00254                             $exceptions[] = Mage::helper('shipping')->__('Duplicate Row #%s (Country "%s", Region/State "%s", Zip "%s" and Value "%s")', ($k+1), $dataDetails[$k]['country'], $dataDetails[$k]['region'], $dataLine['dest_zip'], $dataLine['condition_value']);
00255                         }
00256                     }
00257                 }
00258 
00259                 if (!empty($exceptions)) {
00260                     throw new Exception( "\n" . implode("\n", $exceptions) );
00261                 }
00262             }
00263         }
00264     }


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:24:51 2009 for Magento by  doxygen 1.5.8