class ModelExtensionShippingPek extends Model {
function getQuote($address) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$this->config->get('shipping_pek_geo_zone_id') . "' AND country_id = '" . (int)$address['country_id'] . "' AND (zone_id = '" . (int)$address['zone_id'] . "' OR zone_id = '0')");
if (!$this->config->get('shipping_pek_geo_zone_id')) {
$status = true;
} elseif ($query->num_rows) {
$status = true;
} else{
$status = false;
}
$city = mb_strtolower($this->session->data['payment_address']['city']);
$citySearch = $this->searchCity($city);
if ($citySearch) {
$cityCode = array_keys($citySearch)[0];
$pekData = $this->getPekCost($cityCode);
$pekCost = $pekData['auto'][2];
$this->config->set('shipping_pek_cost', $pekCost);
}
else{
$status = false;
}
$method_data = array();
if ($status) {
$quote_data = array();
$products = $this->cart->getProducts();
$product_info = $products[0];
$length = number_format($product_info['length'], 0);
$width = number_format($product_info['width'], 0);
$height = number_format($product_info['height'], 0);
$weight = number_format($product_info['weight'], 0);
$quote_data['pek'] = array(
'code' => 'pek.pek',
'title' => "ПЭК",
'cost' => $this->config->get('shipping_pek_cost'),
'tax_class_id' => $this->config->get('shipping_pek_tax_class_id'),
'text' => $this->currency->format($this->tax->calculate($this->config->get('shipping_pek_cost'), $this->config->get('shipping_pek_tax_class_id'), $this->config->get('config_tax')), $this->session->data['currency'])
);
$method_data = array(
'code' => 'pek',
'title' => 'ПЭК',
'quote' => $quote_data,
'sort_order' => $this->config->get('shipping_pek_sort_order'),
'error' => false
);
}
return $method_data;
}
function getPekCost($town_id){
// Публичный АПИ pecom
$ch = curl_init();
$url = 'http://calc.pecom.ru/bitrix/components/pecom/calc/ajax.php';
$arr = [
'places' => [
[
0, // Ширина
0, // Длина
0, // Высота
0.1, // Объем
1, // Вес
1, // Признак негабаритности груза
1 // Признак ЖУ
]
],
'take' => [
'town' => -477, //Код города отправителя (Пермь)
// 'tent' => 0,
// 'gidro' => 0,
// 'manip' => 0,
// 'speed' => 0,
// 'moscow' => 0
],
'deliver' => [
'town' => $town_id, // Код города получателя
// 'tent' => 0,
// 'gidro' => 0,
// 'manip' => 0,
// 'speed' => 0,
// 'moscow' => 0
],
// 'plombir' => 0,
'strah' => 3000,
// 'ashan' => 0,
// 'night' => 0,
// 'pal' => 0,
// 'pallets' => 0
];
$params = http_build_query($arr);
// echo $params;
curl_setopt_array($ch, array(
CURLOPT_URL => $url.'?'.$params,
CURLOPT_RETURNTRANSFER => true
));
$out = curl_exec($ch);
// print_r($out);
$res = json_decode($out, true);
return $res;
}
public function getCitiesList(){
$url = 'https://pecom.ru/ru/calc/towns.php';
$ch=curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE);
$cities=curl_exec($ch);
if($cities!=''){
$cities= json_decode($cities,true);
}
return $cities;
}
public function searchCity($q_string,$limit=1){
$cities=$this->getCitiesList();
$out=array();
if(mb_strlen($q_string)>2){
$l=mb_strlen($q_string)/2;
foreach ($cities as $region){
if(is_array($region)){
foreach ($region as $key=>$city){
if((mb_stripos(mb_strtolower($city,'utf-8'),mb_strtolower($q_string,'utf-8'))!==FALSE) && (mb_strlen($city,'utf-8') == mb_strlen($q_string,'utf-8'))){
$c['label']=$city;
$c['value']= $city;
$c['id']=$key;
$out[$c['id']]=$c;
if(count($out)>=$limit){
//$out=array_reverse($out);
return $out;
}
}
}
}
}
$out=array_reverse($out);
return $out;
}
}
}