00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Shipping_Model_Mysql4_Carrier_Tablerate extends Mage_Core_Model_Mysql4_Abstract
00035 {
00036 protected function _construct()
00037 {
00038 $this->_init('shipping/tablerate', 'pk');
00039 }
00040
00041 public function getRate(Mage_Shipping_Model_Rate_Request $request)
00042 {
00043 $read = $this->_getReadAdapter();
00044 $write = $this->_getWriteAdapter();
00045
00046 $select = $read->select()->from($this->getMainTable());
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
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
00090
00091
00092
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
00118
00119 $row = $read->fetchRow($select);
00120 return $row;
00121 }
00122
00123
00124 public function uploadAndImport(Varien_Object $object)
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
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
00147
00148
00149
00150
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 }
00265
00266 protected function _getCsvValues($string, $separator=",")
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
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
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 }
00291
00292 protected function _isPositiveDecimalNumber($n)
00293 {
00294 return preg_match ("/^[0-9]+(\.[0-9]*)?$/", $n);
00295 }
00296
00297 }