'.$this->renderZones($tax_rules, (int)$cookie->id_lang);
echo '
';
}
public function renderZones($tax_rules, $id_lang)
{
$html = '';
$zones = Zone::getZones(true);
foreach ($zones AS $key => $zone)
{
$html .= '
'.$zone['name'].'
'.$this->renderCountries($tax_rules, $zone['id_zone'], $id_lang).'
';
}
return $html;
}
public function renderCountries($tax_rules, $id_zone, $id_lang)
{
$html = '
';
$html .= '
'.$this->l('Country / State / County').'
'.$this->l('Tax to apply').'
';
$countries = Country::getCountriesByZoneId((int)$id_zone, (int)$id_lang);
$countCountries = sizeof($countries);
$i = 1;
foreach ($countries AS $country)
{
$id_tax = 0;
if (array_key_exists($country['id_country'], $tax_rules) AND array_key_exists(0, $tax_rules[$country['id_country']]))
$id_tax = (int)$tax_rules[$country['id_country']][0][0]['id_tax'];
$html .= '
'.($country['contains_states'] ? ' ' : '').'
'.Tools::htmlentitiesUTF8($country['name']).'
'.$this->renderTaxesSelect($id_lang, $id_tax, array('class' => 'tax_'.$id_zone, 'id' => 'tax_'.$country['id_country'].'_0', 'name' => 'tax_'.$country['id_country'].'_0' )).'
';
if ($country['contains_states']) {
$html .= $this->renderStates($tax_rules, (int)$id_zone, (int)$country['id_country'], (int)$id_lang);
}
$i++;
}
$html .= '
';
return $html;
}
public function renderStates($tax_rules, $id_zone, $id_country, $id_lang)
{
$states = State::getStatesByIdCountry((int)$id_country);
$countStates = sizeof($states);
$i = 1;
$html = '';
foreach ($states AS $state)
{
$id_tax = 0;
$selected = PS_PRODUCT_TAX;
if (array_key_exists($id_country, $tax_rules)
AND array_key_exists($state['id_state'], $tax_rules[$id_country])
AND array_key_exists(0, $tax_rules[$id_country][$state['id_state']]))
{
$id_tax = (int)$tax_rules[$id_country][$state['id_state']][0]['id_tax'];
$selected = (int)$tax_rules[$id_country][$state['id_state']][0]['state_behavior'];
}
$disable = (PS_PRODUCT_TAX == $selected ? 'disabled' : '');
$html .= '
'.(State::hasCounties($state['id_state']) ? ' ' : '').'
'.Tools::htmlentitiesUTF8($state['name']).'
'.$this->renderTaxesSelect($id_lang, $id_tax, array('class' => 'tax_'.$id_zone,
'id' => 'tax_'.$id_country.'_'.$state['id_state'],
'name' => 'tax_'.$id_country.'_'.$state['id_state'],
'disabled' => $disable )).' -
'.$this->l('Apply country tax only').'
'.$this->l('Apply state tax only').'
'.$this->l('Apply both taxes').'
';
if (State::hasCounties($state['id_state']))
$html .= $this->renderCounties($tax_rules, $id_zone, $id_country, $state['id_state'], $id_lang);
$i++;
}
return $html;
}
public function renderCounties($tax_rules, $id_zone, $id_country, $id_state, $id_lang)
{
$counties = County::getCounties((int)$id_state);
$countCounties = sizeof($counties);
$i = 1;
$html = '';
foreach ($counties AS $county)
{
$id_tax = 0;
$selected = County::USE_STATE_TAX;
if (array_key_exists($id_country, $tax_rules)
AND array_key_exists($id_state, $tax_rules[$id_country])
AND array_key_exists($county['id_county'], $tax_rules[$id_country][$id_state]))
{
$id_tax = (int)$tax_rules[$id_country][$id_state][$county['id_county']]['id_tax'];
$selected = (int)$tax_rules[$id_country][$id_state][$county['id_county']]['county_behavior'];
}
$disable = (County::USE_STATE_TAX == $selected ? 'disabled' : '');
$html .= '
'.Tools::htmlentitiesUTF8($county['name']).'
'.$this->renderTaxesSelect($id_lang, $id_tax, array('class' => 'tax_'.$id_zone,
'id' => 'tax_'.$id_country.'_'.$id_state.'_'.$county['id_county'],
'name' => 'tax_'.$id_country.'_'.$id_state.'_'.$county['id_county'],
'disabled' => $disable )).' -
'.$this->l('Apply state tax only').'
'.$this->l('Apply county tax only').'
'.$this->l('Apply both taxes').'
';
}
return $html;
}
public function renderTaxesSelect($id_lang, $default_value, $html_options)
{
$opt = '';
foreach( array('id', 'class', 'name', 'disabled') AS $prop)
if (array_key_exists($prop, $html_options) && !empty($html_options[$prop]))
$opt .= $prop.'="'.$html_options[$prop].'"';
$html = '
'.$this->l('No Tax').' ';
$taxes = Tax::getTaxes((int)$id_lang, true);
foreach ($taxes AS $tax)
{
$selected = ($default_value == $tax['id_tax']) ? 'selected="selected"' : '';
$html .= ''.Tools::htmlentitiesUTF8($tax['name']).' ';
}
$html .= ' ';
return $html;
}
protected function afterAdd($object)
{
$this->afterUpdate($object);
}
protected function afterUpdate($object)
{
global $cookie;
TaxRule::deleteByGroupId($object->id);
foreach(Country::getCountries($cookie->id_lang, true) AS $country)
{
$id_tax = (int)Tools::getValue('tax_'.$country['id_country'].'_0');
// default country rule
if (!empty($id_tax))
{
$tr = new TaxRule();
$tr->id_tax_rules_group = $object->id;
$tr->id_country = (int)$country['id_country'];
$tr->id_state = 0;
$tr->id_county = 0;
$tr->id_tax = $id_tax;
$tr->state_behavior = 0;
$tr->county_behavior = 0;
$tr->save();
}
// state specific rule
if (!empty($country['contains_states']))
{
foreach ($country['states'] AS $state)
{
$state_behavior = (int)Tools::getValue('behavior_state_'.$state['id_state']);
if ($state_behavior != PS_PRODUCT_TAX)
{
$tr = new TaxRule();
$tr->id_tax_rules_group = $object->id;
$tr->id_country = (int)$country['id_country'];
$tr->id_state = (int)$state['id_state'];
$tr->id_county = 0;
$tr->id_tax = (int)Tools::getValue('tax_'.$country['id_country'].'_'.$state['id_state']);
$tr->state_behavior = $state_behavior;
$tr->county_behavior = 0;
$tr->save();
}
// county specific rule
if (State::hasCounties($state['id_state']))
{
$counties = County::getCounties($state['id_state']);
foreach ($counties AS $county)
{
$county_behavior = (int)Tools::getValue('behavior_county_'.$county['id_county']);
if ($county_behavior != County::USE_STATE_TAX)
{
$tr = new TaxRule();
$tr->id_tax_rules_group = $object->id;
$tr->id_country = (int)$country['id_country'];
$tr->id_state = (int)$state['id_state'];
$tr->id_county = (int)$county['id_county'];
$tr->id_tax = (int)Tools::getValue('tax_'.$country['id_country'].'_'.$state['id_state'].'_'.$county['id_county']);
$tr->state_behavior = 0;
$tr->county_behavior = $county_behavior;
$tr->save();
}
}
}
}
}
}
}
public function postProcess()
{
global $currentIndex, $cookie;
if (!isset($this->table))
return false;
// set token
$token = Tools::getValue('token') ? Tools::getValue('token') : $this->token;
if (Tools::getValue('submitAdd'.$this->table))
{
$id_product = Tools::getValue('id_product');
/* Checking fields validity */
$this->validateRules();
if (!sizeof($this->_errors))
{
$id = (int)(Tools::getValue($this->identifier));
/* Object update */
if (isset($id) AND !empty($id))
{
if ($this->tabAccess['edit'] === '1')
{
$object = new $this->className($id);
if (Validate::isLoadedObject($object))
{
/* Specific to objects which must not be deleted */
if ($this->deleted AND $this->beforeDelete($object))
{
// Create new one with old objet values
$objectNew = new $this->className($object->id);
$objectNew->id = NULL;
$objectNew->date_add = '';
$objectNew->date_upd = '';
// Update old object to deleted
$object->deleted = 1;
$object->update();
// Update new object with post values
$this->copyFromPost($objectNew, $this->table);
$result = $objectNew->add();
if (Validate::isLoadedObject($objectNew))
$this->afterDelete($objectNew, $object->id);
}
else
{
$this->copyFromPost($object, $this->table);
$result = $object->update();
$this->afterUpdate($object);
}
if (!$result)
$this->_errors[] = Tools::displayError('An error occurred while updating object.').'
'.$this->table.' ('.Db::getInstance()->getMsgError().')';
elseif ($this->postImage($object->id) AND !sizeof($this->_errors))
{
$parent_id = (int)(Tools::getValue('id_parent', 1));
// Save and stay on same form
if (Tools::isSubmit('submitAdd'.$this->table.'AndStay'))
Tools::redirectAdmin($currentIndex.'&'.$this->identifier.'='.$object->id.'&conf=4&update'.$this->table.'&token='.$token);
// Default behavior (save and back)
$id_product = (int)Tools::getValue('id_product');
if ($id_product)
Tools::redirectAdmin('?tab=AdminCatalog&id_product='.$id_product.'&updateproduct&token='.Tools::getAdminToken('AdminCatalog'.(int)(Tab::getIdFromClassName('AdminCatalog')).(int)($cookie->id_employee)));
Tools::redirectAdmin($currentIndex.($parent_id ? '&'.$this->identifier.'='.$object->id : '').'&conf=3&token='.$token);
}
}
else
$this->_errors[] = Tools::displayError('An error occurred while updating object.').'
'.$this->table.' '.Tools::displayError('(cannot load object)');
}
else
$this->_errors[] = Tools::displayError('You do not have permission to edit here.');
}
/* Object creation */
else
{
if ($this->tabAccess['add'] === '1')
{
$object = new $this->className();
$this->copyFromPost($object, $this->table);
if (!$object->add())
$this->_errors[] = Tools::displayError('An error occurred while creating object.').'
'.$this->table.' ('.mysql_error().') ';
elseif (($_POST[$this->identifier] = $object->id /* voluntary */) AND $this->postImage($object->id) AND !sizeof($this->_errors) AND $this->_redirect)
{
$parent_id = (int)(Tools::getValue('id_parent', 1));
$this->afterAdd($object);
// Save and stay on same form
if (Tools::isSubmit('submitAdd'.$this->table.'AndStay'))
Tools::redirectAdmin($currentIndex.'&'.$this->identifier.'='.$object->id.'&conf=3&update'.$this->table.'&token='.$token);
$id_product = (int)Tools::getValue('id_product');
if ($id_product)
Tools::redirectAdmin('?tab=AdminCatalog&id_product='.$id_product.'&updateproduct&token='.Tools::getAdminToken('AdminCatalog'.(int)(Tab::getIdFromClassName('AdminCatalog')).(int)($cookie->id_employee)));
Tools::redirectAdmin($currentIndex.($parent_id ? '&'.$this->identifier.'='.$object->id : '').'&conf=3&token='.$token);
// Default behavior (save and back)
Tools::redirectAdmin($currentIndex.($parent_id ? '&'.$this->identifier.'='.$object->id : '').'&conf=3&token='.$token);
}
}
else
$this->_errors[] = Tools::displayError('You do not have permission to add here.');
}
}
$this->_errors = array_unique($this->_errors);
}
parent::postProcess();
}
}