Index: branches/5.1.x/units/taxes/taxes_tag_processor.php =================================================================== diff -u -N -r13147 -r13465 --- branches/5.1.x/units/taxes/taxes_tag_processor.php (.../taxes_tag_processor.php) (revision 13147) +++ branches/5.1.x/units/taxes/taxes_tag_processor.php (.../taxes_tag_processor.php) (revision 13465) @@ -1,6 +1,6 @@ Application->recallObject('tax'); - $zone->SetDBField('TaxZoneId', $zone->ID); - $destination =& $this->Application->recallObject('taxdst'); + $object =& $this->getObject($params); + /* @var $object kDBItem */ - if(!$this->Application->GetVar('loaded')) - { - if ($zone->GetID() == 0) - { - $this->Application->DeleteVar('taxdst'); - } - else - { - $sql = 'SELECT * FROM '.$destination->TableName.' WHERE TaxZoneId='.$zone->GetID(); - $res = $this->Conn->Query($sql); - if (is_array($res)) foreach ($res as $dest_record) - { - $temp[$dest_record['TaxZoneDestId']]['TaxZoneDestId'] = $dest_record['TaxZoneDestId']; - $temp[$dest_record['TaxZoneDestId']]['StdDestId'] = $dest_record['StdDestId']; - $temp[$dest_record['TaxZoneDestId']]['DestValue'] = $dest_record['DestValue']; - } - $this->Application->SetVar('taxdst', $temp); - } - } - $destination =& $this->Application->recallObject('taxdst'); + $destination_table = $this->getDestinationsTable($params); + $selected_country_id = (int)$this->Application->GetVar('CountrySelector'); - $hidden_clause = ''; + $name_field = 'l' . $this->Application->GetVar('m_lang') . '_Name'; + $id_field = $this->Application->getUnitOption('country-state', 'IDField'); + $table_name = $this->Application->getUnitOption('country-state', 'TableName'); - switch ( $zone->GetDBField('Type') ) - { - case 1: + switch ($params['show']) { + case 'current': + // selected countries in current zone + $sql = 'SELECT cs.' . $name_field . ', cs.' . $id_field . ' + FROM ' . $table_name . ' cs + LEFT JOIN ' . $destination_table . ' zd ON zd.StdDestId = cs.' . $id_field . ' + WHERE cs.Type = ' . DESTINATION_TYPE_COUNTRY . ' AND zd.TaxZoneId = ' . $object->GetID() . ' + ORDER BY cs.' . $name_field; + break; - $sql = 'SELECT * FROM '.TABLE_PREFIX.'StdDestinations WHERE DestType=1'; - $res = $this->Conn->Query($sql, 'DestId'); - - $dropdown = ''."\n"; - - $form_params = Array(); - $form_params['dropdown'] = $dropdown; - $form_params['block'] = $param['block']; - $form_params['res'] = $res; - $ret = $this->ShowDestionationForm($form_params); + case 'available': + // available countries in current zone + $sql = 'SELECT cs.' . $name_field . ', cs.' . $id_field . ' + FROM ' . $table_name . ' cs + LEFT JOIN ' . $destination_table . ' zd ON zd.StdDestId = cs.' . $id_field . ' + WHERE cs.Type = ' . DESTINATION_TYPE_COUNTRY . ' AND zd.TaxZoneId IS NULL + ORDER BY cs.' . $name_field; break; - case 2: - $country_sql = 'SELECT d1.* FROM '.TABLE_PREFIX.'StdDestinations d1, - '.TABLE_PREFIX.'StdDestinations d2 - WHERE d1.DestType=1 AND d1.DestId=d2.DestParentId - GROUP BY d1.DestId'; - if( !($current_country = $this->Application->GetVar('StatesCountry')) ) - { - $current_country_sql = 'SELECT sd.DestParentId FROM '.TABLE_PREFIX.'StdDestinations sd - LEFT JOIN '.$destination->TableName.' zd - ON zd.StdDestId = sd.DestId - WHERE sd.DestType=2 AND zd.TaxZoneId='.$zone->GetDBField('TaxZoneId'); - if($zone->GetDBField('TaxZoneId')) - { - $current_country = $this->Conn->GetOne($current_country_sql); - } + case 'all': + // always preselect 1st country, when user haven't selected any + if (!$selected_country_id) { + $sql = 'SELECT StdDestId + FROM ' . $destination_table . ' + WHERE TaxZoneId = ' . $object->GetID(); + $selected_country_id = $this->Conn->GetOne($sql); - if(!$current_country) - { - $current_country_sql = 'SELECT DestId FROM '.TABLE_PREFIX.'StdDestinations WHERE DestType=1'; - $current_country = $this->Conn->GetOne($current_country_sql); + if ($selected_country_id) { + $this->Application->SetVar('CountrySelector', $selected_country_id); } } - $states_sql = 'SELECT * FROM '.TABLE_PREFIX.'StdDestinations WHERE DestType=2 AND DestParentId='.$current_country; - $countries = $this->Conn->Query($country_sql, 'DestId'); - $states = $this->Conn->Query($states_sql, 'DestId'); - if($countries) - { - $countries_dropdown = ''."\n"; - } - - if($states) - { - $states_dropdown = ''."\n"; - } - - $form_params = Array(); - $table = ''; - $table .= '
'.$this->Application->Phrase('la_Country').': '.$countries_dropdown.'
'.$this->Application->Phrase('la_State').': '.$states_dropdown.'
'; - $form_params['dropdown'] = $table; - $form_params['block'] = $param['block']; - $form_params['res'] = $states; - $ret = $this->ShowDestionationForm($form_params); - + // all countries + $sql = 'SELECT ' . $name_field . ', ' . $id_field . ' + FROM ' . $table_name . ' + WHERE Type = ' . DESTINATION_TYPE_COUNTRY . ' + ORDER BY ' . $name_field; break; - case 3: - if( !($current_country = $this->Application->GetVar('StatesCountry')) ) - { - $current_country_sql = 'SELECT sd.DestParentId FROM '.TABLE_PREFIX.'StdDestinations sd - LEFT JOIN '.$destination->TableName.' zd - ON zd.StdDestId = sd.DestId - WHERE sd.DestType=2 AND zd.TaxZoneId='.$zone->GetDBField('TaxZoneId'); - if($zone->GetDBField('TaxZoneId')) - { - $current_country = $this->Conn->GetOne($current_country_sql); - } + case 'has_states': + $cs_helper =& $this->Application->recallObject('CountryStatesHelper'); + /* @var $cs_helper kCountryStatesHelper */ - if(!$current_country) - { - $current_country_sql = 'SELECT StdDestId FROM '.$destination->TableName.' WHERE TaxZoneId='.$zone->GetID(); - $current_country = $this->Conn->GetOne($current_country_sql); - } + $has_states = $cs_helper->getCountriesWithStates(); - if(!$current_country) - { - $current_country_sql = 'SELECT DestId FROM '.TABLE_PREFIX.'StdDestinations WHERE DestType=1'; - $current_country = $this->Conn->GetOne($current_country_sql); - } - - + if ($selected_country_id && !array_key_exists($selected_country_id, $has_states)) { + list ($selected_country_id, ) = each($has_states); + $this->Application->SetVar('CountrySelector', $selected_country_id); } - $country_sql = 'SELECT d1.* FROM '.TABLE_PREFIX.'StdDestinations d1 - WHERE d1.DestType=1 - GROUP BY d1.DestId'; - $countries = $this->Conn->Query($country_sql, 'DestId'); + // preselect country from 1st found state + if (!$selected_country_id) { + $sql = 'SELECT cs.StateCountryId + FROM ' . $table_name . ' cs + LEFT JOIN ' . $destination_table . ' zd ON zd.StdDestId = cs.' . $id_field . ' + WHERE (cs.Type = ' . DESTINATION_TYPE_STATE . ') AND (zd.TaxZoneId = ' . $object->GetID() . ')'; + $selected_country_id = $this->Conn->GetOne($sql); - if($countries) - { - $countries_dropdown = ''."\n"; - } - - $sql = 'SELECT DestValue FROM '.$this->Application->getUnitOption('taxdst', 'TableName').' WHERE NOT(DestValue IS NULL) AND DestValue<>"" AND StdDestId='.$current_country; - $res = array_unique( $this->Conn->GetCol($sql) ); - $dropdown = ''; - if($res) - { - $dropdown .= ' or '."\n"; } - $table = ''; - $table .= '
'.$this->Application->Phrase('la_Country').': '.$countries_dropdown.'
'.$this->Application->Phrase('la_fld_ZIP').': '.$dropdown.'
'; - - $form_params = Array(); - $form_params['dropdown'] = $table; - $form_params['block'] = $param['block']; - $form_params['res'] = $res; - $ret = $this->ShowDestionationForm($form_params); - + // gets only countries with states + $sql = 'SELECT ' . $name_field . ', ' . $id_field . ' + FROM ' . $table_name . ' + WHERE Type = ' . DESTINATION_TYPE_COUNTRY . ' AND ' . $id_field . ' IN (' . implode(',', array_keys($has_states)) . ') + ORDER BY ' . $name_field; break; + default: + trigger_error('Unknown "show" parameter value "' . $params['show'] . '" used.', E_USER_ERROR); + break; } - $ret .= $hidden_clause; - return $ret; - } - */ - /* - function ShowDestionationForm($param) - { - $add_button = ''; + $ret = ''; + $countries = $this->Conn->GetCol($sql, $id_field); - $main_processor =& $this->Application->RecallObject('m_TagProcessor'); - $oddevenparam['odd'] = 'table-color1'; - $oddevenparam['even'] = 'table-color2'; - $ret = ''.$param['dropdown'].''.$add_button.''; + $block_params = $this->prepareTagParams($params); + $block_params['name'] = $params['block']; - $dest_list = $this->Application->GetVar('taxdst'); - if (is_array($dest_list)) - { + foreach ($countries as $country_id => $country_name) { + $block_params['id'] = $country_id; + $block_params['destination_title'] = $country_name; + $block_params['selected'] = $selected_country_id == $country_id ? ' selected="selected"' : ''; - - if (sizeof($dest_list)>0){ - $ret .= ' '; - $ret .= ''; - if($destination['StdDestId'] && !$destination['DestValue']) - { - $params['destination_title'] = $param['res'][$destination['StdDestId']]['DestName']; - $hidden .= ''; - } - else - { - $params['destination_title'] = $destination['DestValue']; - $hidden .= ''; - } - - $params['name'] = $param['block']; - $ret .= $main_processor->ParseBlock($params); - } - $ret .= ''; - - $ret .= ''; - $ret .= $hidden; - $ret .= " "; - }else{ - - } + $ret .= $this->Application->ParseBlock($block_params); } -// ][TaxZoneDestId]" name="taxdst[][TaxZoneDestId]" value=""> return $ret; } - */ - function ShowCountries($param){ - $param = $this->prepareTagParams($param); - $param['name'] = $param['block']; + function ShowStates($params) + { + $object =& $this->getObject($params); + /* @var $object kDBItem */ - $destination = &$this->Application->recallObject('taxdst'); - $zone = &$this->Application->recallObject('tax'); + $destination_table = $this->getDestinationsTable($params); - switch ($param['show']){ + $name_field = 'l' . $this->Application->GetVar('m_lang') . '_Name'; + $id_field = $this->Application->getUnitOption('country-state', 'IDField'); + $table_name = $this->Application->getUnitOption('country-state', 'TableName'); + + $country_id = $this->Application->GetVar('CountrySelector'); + + switch ($params['show']) { case 'current': - $sql = 'SELECT sd.* - FROM '.TABLE_PREFIX.'StdDestinations sd - LEFT JOIN '.$destination->TableName.' zd ON zd.StdDestId = sd.DestId - LEFT JOIN '.TABLE_PREFIX.'Phrase ON '.TABLE_PREFIX.'Phrase.Phrase = sd.DestName - WHERE sd.DestType = 1 AND zd.TaxZoneId = '.$zone->GetDBField('TaxZoneId').' - ORDER BY l' . $this->Application->GetVar('lang.current_id') . '_Translation'; + // selected states for current country and zone + $sql = 'SELECT cs.' . $name_field . ', cs.' . $id_field . ' + FROM ' . $table_name . ' cs + LEFT JOIN ' . $destination_table . ' zd ON zd.StdDestId = cs.' . $id_field . ' + WHERE + cs.Type = ' . DESTINATION_TYPE_STATE . ' AND + cs.StateCountryId = ' . $country_id . ' AND + zd.TaxZoneId = ' . $object->GetID() . ' + ORDER BY cs.' . $name_field; break; case 'available': - $sql = 'SELECT sd.* - FROM '.TABLE_PREFIX.'StdDestinations sd - LEFT JOIN '.$destination->TableName.' zd ON zd.StdDestId = sd.DestId - LEFT JOIN '.TABLE_PREFIX.'Phrase ON '.TABLE_PREFIX.'Phrase.Phrase = sd.DestName - WHERE sd.DestType = 1 AND zd.TaxZoneId IS NULL - ORDER BY l' . $this->Application->GetVar('lang.current_id') . '_Translation'; + // available states for current country and zone + $sql = 'SELECT cs.' . $name_field . ', cs.' . $id_field . ' + FROM ' . $table_name . ' cs + LEFT JOIN ' . $destination_table . ' zd ON zd.StdDestId = cs.' . $id_field . ' + WHERE + cs.Type = ' . DESTINATION_TYPE_STATE . ' + AND zd.TaxZoneId IS NULL + AND cs.StateCountryId = ' . $country_id . ' + ORDER BY cs.' . $name_field; break; case 'all': - $selected_country = $this->Application->GetVar('CountrySelector'); - if (!$selected_country){ - // get 1st available country ID - $selected_country = $this->Conn->GetOne('SELECT StdDestId FROM '.$destination->TableName.' - WHERE TaxZoneId='.$zone->GetDBField('TaxZoneId')); - if ($selected_country){ - $this->Application->SetVar('CountrySelector', $selected_country); - } - } - - $sql = 'SELECT sd.* - FROM '.TABLE_PREFIX.'StdDestinations sd - LEFT JOIN '.TABLE_PREFIX.'Phrase ON '.TABLE_PREFIX.'Phrase.Phrase = sd.DestName - WHERE sd.DestType = 1 - ORDER BY l' . $this->Application->GetVar('lang.current_id') . '_Translation'; + // all possible states for selected country + $sql = 'SELECT ' . $name_field . ', ' . $id_field . ' + FROM ' . $table_name . ' + WHERE Type = ' . DESTINATION_TYPE_STATE . ' AND StateCountryId = ' . $country_id . ' + ORDER BY ' . $name_field; break; - case 'has_states': - $has_states = $this->Conn->GetCol('SELECT DISTINCT DestParentId FROM '.TABLE_PREFIX.'StdDestinations sd - WHERE sd.DestType=2'); - $selected_country = $this->Application->GetVar('CountrySelector'); - - if ($selected_country && !in_array($selected_country, $has_states)){ - $selected_country = $has_states[0]; - $this->Application->SetVar('CountrySelector', $selected_country); - } - - if (!$selected_country){ - // get 1st available country ID - $selected_country = $this->Conn->GetOne('SELECT DestParentId FROM '.TABLE_PREFIX.'StdDestinations sd - LEFT JOIN '.$destination->TableName.' zd - ON zd.StdDestId = sd.DestId - WHERE sd.DestType=2 - AND zd.TaxZoneId='.$zone->GetDBField('TaxZoneId')); - if ($selected_country){ - $this->Application->SetVar('CountrySelector', $selected_country); - } - else { - $selected_country = $has_states[0]; - $this->Application->SetVar('CountrySelector', $selected_country); - } - } - - $sql = 'SELECT sd.* - FROM '.TABLE_PREFIX.'StdDestinations sd - LEFT JOIN '.TABLE_PREFIX.'Phrase ON '.TABLE_PREFIX.'Phrase.Phrase = sd.DestName - WHERE sd.DestType = 1 AND DestId IN ('.implode(',', $has_states).') - ORDER BY l' . $this->Application->GetVar('lang.current_id') . '_Translation'; + default: + trigger_error('Unknown "show" parameter value "' . $params['show'] . '" used.', E_USER_ERROR); break; } - $countries = $this->Conn->Query($sql); - $o = ''; + $ret = ''; + $states = $this->Conn->GetCol($sql, $id_field); + $block_params = $this->prepareTagParams($params); + $block_params['name'] = $params['block']; - foreach($countries as $key => $country) { - $param['id'] = $country['DestId']; - $param['destination_title'] = $this->Application->Phrase($country['DestName']); - if (isset($selected_country) && $selected_country == $param['id']){ - $param['selected'] = ' selected="selected"'; - } - else { - $param['selected']=''; - } - $o .= $this->Application->ParseBlock($param); + foreach($states as $state_id => $state_name) { + $block_params['id'] = $state_id; + $block_params['destination_title'] = $state_name; + + $ret .= $this->Application->ParseBlock($block_params); } - return $o; + return $ret; } - function ShowStates($param) + function ShowZips($params) { - $param = $this->prepareTagParams($param); - $param['name'] = $param['block']; + $object =& $this->getObject($params); + /* @var $object kDBItem */ - $destination = &$this->Application->recallObject('taxdst'); - $zone = &$this->Application->recallObject('tax'); + $destination_table = $this->getDestinationsTable($params); - switch ($param['show']){ - case 'current': - $sql = 'SELECT * - FROM '.TABLE_PREFIX.'StdDestinations sd - LEFT JOIN '.$destination->TableName.' zd ON zd.StdDestId = sd.DestId - LEFT JOIN '.TABLE_PREFIX.'Phrase ON '.TABLE_PREFIX.'Phrase.Phrase = sd.DestName + $country_id = (int)$this->Application->GetVar('CountrySelector'); + + $current_sql = 'SELECT DestValue + FROM ' . $destination_table . ' WHERE - sd.DestType=2 - AND sd.DestParentId='.$this->Application->GetVar('CountrySelector').' - AND zd.TaxZoneId='.$zone->GetDBField('TaxZoneId').' - ORDER BY l' . $this->Application->GetVar('lang.current_id') . '_Translation'; + COALESCE(DestValue, "") <> "" AND + TaxZoneId = ' . $object->GetID() . ' + ORDER BY DestValue'; + + switch ($params['show']) { + case 'current': + $sql = $current_sql; break; case 'available': - $sql = 'SELECT * - FROM '.TABLE_PREFIX.'StdDestinations sd - LEFT JOIN '.$destination->TableName.' zd ON zd.StdDestId = sd.DestId - LEFT JOIN '.TABLE_PREFIX.'Phrase ON '.TABLE_PREFIX.'Phrase.Phrase = sd.DestName + $selected_zips = $this->Conn->GetCol($current_sql); + $selected_zips = array_map(Array (&$this->Conn, 'qstr'), $selected_zips); + + $sql = 'SELECT DISTINCT DestValue + FROM ' . $this->Application->getUnitOption('taxdst', 'TableName') . ' WHERE - sd.DestType=2 - AND zd.TaxZoneId IS NULL - AND sd.DestParentId='.$this->Application->GetVar('CountrySelector').' - ORDER BY l' . $this->Application->GetVar('lang.current_id') . '_Translation'; + COALESCE(DestValue, "") <> "" AND + TaxZoneId <> ' . $object->GetID() . ' AND + ' . ($selected_zips ? 'DestValue NOT IN (' . implode(',', $selected_zips) . ') AND' : '') . ' + StdDestId = ' . $country_id . ' + ORDER BY DestValue'; break; - case 'all': - $sql = 'SELECT sd.* - FROM '.TABLE_PREFIX.'StdDestinations sd - LEFT JOIN '.TABLE_PREFIX.'Phrase ON '.TABLE_PREFIX.'Phrase.Phrase = sd.DestName - WHERE sd.DestType = 2 AND sd.DestParentId='.$this->Application->GetVar('CountrySelector').' - ORDER BY l' . $this->Application->GetVar('lang.current_id') . '_Translation'; + default: + trigger_error('Unknown "show" parameter value "' . $params['show'] . '" used.', E_USER_ERROR); break; } - $states = $this->Conn->Query($sql); - $o = ''; - foreach($states as $key => $state) { - $param['id'] = $state['DestId']; - $param['destination_title'] = $this->Application->Phrase($state['DestName']); - $o .= $this->Application->ParseBlock($param); - } - return $o; + $zips = $this->Conn->GetCol($sql); - } + $ret = ''; + $block_params = $this->prepareTagParams($params); + $block_params['name'] = $params['block']; - function ShowZips($param){ + foreach($zips as $zip) { + $block_params['id'] = '0|' . $zip; + $block_params['destination_title'] = $zip; - $param = $this->prepareTagParams($param); - $param['name'] = $param['block']; + $ret .= $this->Application->ParseBlock($block_params); + } - $destination = &$this->Application->recallObject('taxdst'); - $zone = &$this->Application->recallObject('tax'); + return $ret; + } - $country_selector = $this->Application->GetVar('CountrySelector'); - if (!$country_selector){ - $country_selector=0; - } + /** + * Returns table for shipping zone destinations + * + * @param Array $params + * @return string + */ + function getDestinationsTable($params) + { + static $table_name = ''; - switch ($param['show']){ - case 'current': - $sql = 'SELECT * FROM '.$destination->TableName.' - WHERE NOT(DestValue IS NULL) - AND DestValue<>"" - AND TaxZoneID='.$zone->GetDBField('TaxZoneId').' - ORDER BY DestValue - '; - break; - case 'available': - $selected_zips = $this->Conn->GetCol('SELECT DestValue FROM '.$destination->TableName.' - WHERE NOT(DestValue IS NULL) - AND DestValue<>"" - AND TaxZoneID='.$zone->GetDBField('TaxZoneId').' - ORDER BY DestValue - '); + if (!$table_name) { + $object =& $this->getObject($params); + /* @var $object kDBItem */ - $sql = 'SELECT DISTINCT(DestValue) FROM '.$this->Application->getUnitOption('taxdst', 'TableName').' - WHERE NOT(DestValue IS NULL) - AND TaxZoneID!='.$zone->GetDBField('TaxZoneId').' - AND DestValue NOT IN ("'.implode('", "', $selected_zips).'") - AND DestValue<>"" AND StdDestId='.$country_selector.' - ORDER BY DestValue - '; + $table_name = $this->Application->getUnitOption('taxdst', 'TableName'); - break; - case 'all': - $sql = 'SELECT sd.* FROM '.TABLE_PREFIX.'StdDestinations sd - WHERE sd.DestType=3 AND sd.DestParentId='.$country_selector.' - ORDER BY DestValue - '; - break; + if ($object->IsTempTable()) { + $table_name = $this->Application->GetTempName($table_name, 'prefix:' . $this->Prefix); + } } - $zips = $this->Conn->Query($sql); - $o = ''; - foreach($zips as $key => $zip) { - $param['id'] = $zip['DestId'].'|'.$zip['DestValue']; - $param['destination_title'] = $zip['DestValue']; - $o .= $this->Application->ParseBlock($param); - } - return $o; - + return $table_name; } - } \ No newline at end of file