public function calculate()
 {
     $params = array();
     $params['weight'] = max(0.1, $this->getTotalWeight());
     if ($params['weight'] > 31.5) {
         /* hardcoded */
         return 'Вес отправления превышает максимально допустимый (31,5 кг).';
     } elseif (empty($params['weight'])) {
         return 'Вес отправления не задан.';
     }
     $incomplete = false;
     switch ($country_iso3 = $this->getAddress('country')) {
         case 'rus':
             $address = array_merge(array('country' => 'rus'), $this->getSettings());
             $params['from'] = $this->findTo($address);
             $params['to'] = $this->findTo($this->getAddress());
             if (empty($params['to'])) {
                 $incomplete = empty($address['city']) && empty($address['region']);
             }
             break;
         default:
             /* International shipping*/
             $country_model = new waCountryModel();
             if ($country = $country_model->get($country_iso3)) {
                 $params['to'] = mb_strtoupper($country['iso2letter']);
             } else {
                 $params['to'] = false;
             }
             $params['type'] = 'att';
             $incomplete = empty($params['to']);
             break;
     }
     $services = array();
     if (!empty($params['to'])) {
         if (!empty($params['from']) || !empty($params['type'])) {
             if ($result = $this->request('ems.calculate', $params)) {
                 $est_delivery = '';
                 $time = array('min' => sprintf('+%d day', ifset($result['term']['min'], 7)), 'max' => sprintf('+%d day', ifset($result['term']['max'], 14)));
                 $est_delivery .= waDateTime::format('humandate', strtotime($time['min']));
                 $est_delivery .= ' — ';
                 $est_delivery .= waDateTime::format('humandate', strtotime($time['max']));
                 $rate = doubleval(ifset($result['price'], 0));
                 if (doubleval($this->surcharge) > 0) {
                     $rate += $this->getTotalPrice() * doubleval($this->surcharge) / 100.0;
                 }
                 $services['main'] = array('rate' => $rate, 'currency' => 'RUB', 'est_delivery' => $est_delivery);
             } else {
                 $services = 'Ошибка расчета стоимости доставки в указанные город и регион.';
             }
         } else {
             $services = 'Адрес отправителя не указан в настройках способа доставки «EMS Почта России».';
         }
     } elseif ($incomplete) {
         $services = array();
     } else {
         $services = 'Ошибка расчета стоимости доставки в указанные город и регион.';
     }
     return $services;
 }
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */
function smarty_modifier_wa_format_country($code, $locale = null)
{
    if ($locale === null) {
        /**
         * @var waSmarty3View
         */
        $smarty = waConfig::get('current_smarty');
    }
    if ($locale === null) {
        $locale = $smarty->getVars('locale');
    }
    if ($locale === null) {
        $locale = waSystem::getInstance()->getUser()->getLocale();
    }
    $country_model = new waCountryModel();
    $country = $country_model->get($code, $locale);
    return isset($country['name']) ? $country['name'] : $code;
}
 public function execute()
 {
     $country_iso3 = waRequest::request('country');
     if (!$country_iso3) {
         throw new waException('Country not specified.', 404);
     }
     $regions = array();
     switch ($country_iso3) {
         case '%AL':
             $country = array('name' => _w('All countries'));
             break;
         case '%EU':
             $country = array('name' => _w('All European countries'));
             break;
         case '%RW':
             $country = array('name' => _w('Rest of world'));
             break;
         default:
             // Country
             $cm = new waCountryModel();
             $country = $cm->get($country_iso3);
             if (!$country) {
                 throw new waException('Country not found.', 404);
             }
             // Country regions
             $rm = new waRegionModel();
             foreach ($rm->getByCountry($country_iso3) as $r) {
                 $r['css_class'] = 'highlighted just-added hidden';
                 $r['tax_name'] = '';
                 $r['tax_value'] = '';
                 $r['params'] = array('tax_value_modifier' => '+');
                 $regions[] = $r;
             }
             break;
     }
     $this->view->assign('c', array('css_class' => 'highlighted just-added one_rate', 'name' => $country['name'], 'iso3' => $country_iso3, 'regions' => $regions, 'global_rate' => ''));
 }
 /**
  * @param $iso3code
  * @return mixed
  * @throws waException
  */
 protected function getCountryISO2Code($iso3code)
 {
     $country_model = new waCountryModel();
     $country = $country_model->get($iso3code);
     if (!$country) {
         throw new waException($this->_w("Unknown country: ") . $iso3code);
     }
     return strtoupper($country['iso2letter']);
 }
 /**
  * @param string $code iso3 code
  * @throws waException
  * @return string
  */
 protected function getCountryName($code)
 {
     $country_model = new waCountryModel();
     $country = $country_model->get($code);
     if (!$country) {
         throw new waException($this->plugin->_w("Unknown country: ") . $code);
     }
     $iso2letter = strtoupper($country['iso2letter']);
     $country_list = $this->getCountryList();
     if (!isset($country_list[$iso2letter])) {
         throw new waException($this->plugin->_w("Unknown country: ") . $code);
     }
     return $country_list[$iso2letter];
 }
 private function overview()
 {
     $this->setLayout(new shopWelcomeLayout());
     #countries
     $cm = new waCountryModel();
     $cm->preload();
     $countries = array();
     if (!empty($this->countries)) {
         foreach ($this->countries as $iso3) {
             $countries[$iso3] = $cm->get($iso3);
         }
     }
     $locale = waLocale::getInfo(wa()->getUser()->getLocale());
     if (!isset($locale['iso3']) || !isset($countries[$locale['iso3']])) {
         if (isset($countries['usa'])) {
             $country_iso3 = 'usa';
         } else {
             reset($countries);
             $country_iso3 = key($countries);
         }
     } else {
         $country_iso3 = $locale['iso3'];
     }
     $this->view->assign('countries', $countries);
     $this->view->assign('country_iso', $country_iso3);
     $this->view->assign('translate', $this->translate);
     #product types
     $types = array();
     if (!empty($this->types)) {
         foreach ($this->types as $id => $type) {
             if ($id != 'default') {
                 $name = ifempty($type['name'], $id);
                 $types[$id] = array('name' => ifempty($this->translate[$name], $name), 'icon' => ifempty($type['icon'], ''), 'description' => '');
             }
         }
     }
     $this->view->assign('types', $types);
 }