public function execute() { $this->tm = $tm = new shopTaxModel(); $taxes = $tm->getAll('id'); $tax_id = waRequest::request('id'); if (!$tax_id) { $tax_id = $taxes ? key($taxes) : 'new'; } if (!empty($taxes[$tax_id])) { $tax = $taxes[$tax_id]; } else { if ($tax_id == 'new') { $tax = $tm->getEmptyRow(); $tax_id = null; } else { throw new waException('Tax record not found.', 404); } } $this->trm = $trm = new shopTaxRegionsModel(); $this->tzcm = $tzcm = new shopTaxZipCodesModel(); $countries = $this->getCountryList(); $tax = $this->processPostData($tax); if ($tax['id'] && !$tax_id) { $tax_id = $tax['id']; } if ($tax_id) { $taxes[$tax_id] = $tax; } uasort($taxes, wa_lambda('$a,$b', 'return strcmp($a["name"], $b["name"]);')); $this->view->assign('tax_countries', $this->getTaxCountries($tax, $countries)); $this->view->assign('tax_zip_codes', $this->getTaxZipCodes($tax)); $this->view->assign('countries', $countries); $this->view->assign('taxes', $taxes); $this->view->assign('tax', $tax); $checkout_settings = $this->getConfig()->getCheckoutSettings(); $this->view->assign('billing_address_required', isset($checkout_settings['contactinfo']['fields']['address.billing'])); }
/** * Creates new or modifies existing tax. * * Examples: * // Pass id to modify existing tax: shopTaxes::save(array( 'id' => 100500, ... see below ... )); // Pass no id to create new tax: shopTaxes::save(array( 'name' => '...', 'included' => true, // true for taxes included in price, false for added 'address_type' => 'shipping', // shipping|billing // rates by zip code: zip code => % 'zip_codes' => array( '1234*' => 1.11, '123*' => 2.22, ), // rates by country and/or country regions 'countries' => array( // Country in simple mode: global rate only, no by-region setup 'rus' => array( 'global_rate' => 10.1, // % ), // Country in advanced mode: rate by region 'usa' => array( 'regions' => array( 'AK' => 2.34, // % 'AL' => 3.45, ), ), // Country in super-advanced mode: global rate and by-region modifiers 'can' => array( 'global_rate' => 5, 'regions' => array( 'BC' => array( 'name' => 'ASDF', 'tax_value' => 1.23, // % 'tax_value_modifier' => '+', ), 'MB' => array( 'name' => 'QWER', 'tax_value' => 1.05, 'tax_value_modifier' => '*', ), 'NB' => array( 'name' => 'ZXCV', 'tax_value' => 6.15, 'tax_value_modifier' => '', // replaces global rate ), ), ), // Use special codes instead of country-iso3 for groups: // '%AL' = All countries // '%EU' = All european countries // '%RW' = Rest of the world ), )); * * @param array $tax_data * @return array DB row from shop_tax, including id (useful for new record) */ public static function save($tax_data) { if (!is_array($tax_data)) { throw new waException('$tax_data must be an array.'); } $tm = new shopTaxModel(); // // shop_tax // $tax = $tm->getEmptyRow(); unset($tax['id']); $tax = array_intersect_key($tax_data, $tax) + $tax; // Default values (instead of validation) if (!strlen(ifset($tax['name'], ''))) { $tax['name'] = _w('<no name>'); } $tax['included'] = $tax['included'] ? 1 : 0; $tax['address_type'] = $tax['address_type'] === 'shipping' ? 'shipping' : 'billing'; // Save into shop_tax if (empty($tax_data['id'])) { $tax['id'] = $tm->insert($tax); } else { $tm->updateById($tax_data['id'], $tax); $tax['id'] = $tax_data['id']; } // // shop_tax_zip_codes // $tzcm = new shopTaxZipCodesModel(); $tzcm->deleteByField('tax_id', $tax['id']); if (!empty($tax_data['zip_codes']) && is_array($tax_data['zip_codes'])) { $rows = array(); foreach ($tax_data['zip_codes'] as $code => $rate) { if (!$code) { continue; } $code = str_replace('*', '%', $code); $rate = (double) str_replace(',', '.', ifempty($rate, '0')); $rows[$code] = array('tax_id' => $tax['id'], 'zip_expr' => $code, 'tax_value' => $rate, 'sort' => count($rows)); } if ($rows) { $tzcm->multipleInsert(array_values($rows)); } } // // shop_tax_regions // $trm = new shopTaxRegionsModel(); $trm->deleteByField('tax_id', $tax['id']); if (!empty($tax_data['countries']) && is_array($tax_data['countries'])) { $region_rates = array(); foreach ($tax_data['countries'] as $country_iso3 => $country_data) { $country_global_rate = (double) str_replace(',', '.', ifempty($country_data['global_rate'], '0')); $no_region_added = true; $params_added = false; if (!empty($country_data['regions']) && is_array($country_data['regions'])) { foreach ($country_data['regions'] as $region_code => $region_data) { if (is_array($region_data)) { $tax_value_modifier = ifempty($region_data['tax_value_modifier']); $tax_value = (double) str_replace(',', '.', ifempty($region_data['tax_value'], '0')); $tax_name = ifempty($region_data['name']); } else { $tax_name = null; $tax_value_modifier = ''; $tax_value = (double) str_replace(',', '.', $region_data); } $params = null; if ($tax_value_modifier) { $params = serialize(array('tax_value_modifier' => $tax_value_modifier, 'tax_value' => $tax_value)); switch ($tax_value_modifier) { case '*': $tax_value *= $country_global_rate; break; case '+': if ($country_global_rate) { $tax_value += $country_global_rate; } else { $params = null; } break; } if ($tax_value == $country_global_rate) { continue; } } $region_rates[] = array('tax_id' => $tax['id'], 'tax_name' => $tax_name, 'country_iso3' => $country_iso3, 'region_code' => $region_code, 'tax_value' => $tax_value, 'params' => $params); if ($params) { $params_added = true; } $no_region_added = false; } } if ($no_region_added || $params_added || $country_global_rate > 0) { $region_rates[] = array('tax_id' => $tax['id'], 'tax_name' => null, 'country_iso3' => $country_iso3, 'region_code' => null, 'tax_value' => $country_global_rate, 'params' => null); } } if ($region_rates) { $trm->multipleInsert($region_rates); } } return $tax; }