Пример #1
0
 public static function getCoordinates($checkCache = true)
 {
     $response = new ApiResponseModel();
     $data = array();
     if ($checkCache) {
         $data = ApiCache::get(Yii::app()->params['cache']['keys']['coordinates']);
     }
     if (!$data) {
         $availableBanks = BankCourses::model()->findAvailableBanks()->findAll();
         if (count($availableBanks) > 0) {
             $ids = array();
             foreach ($availableBanks as $bankInfo) {
                 $ids[] = $bankInfo['bank_id'];
             }
             $answer = new CoordinatesApiModel();
             $coordinates = BankBranches::model()->getDepartmentCoordinates($ids)->findAll();
             foreach ($coordinates as $coordinate) {
                 $answer->add($coordinate);
             }
             $data = $answer->getResult();
             $response->setData($data);
             $response->setHash(ApiCache::set(Yii::app()->params['cache']['keys']['coordinates'], $data, Yii::app()->params['cache']['time']));
         }
     } else {
         $response->setData($data);
         $response->setHash(ApiCache::getCheck(Yii::app()->params['cache']['keys']['coordinates']));
     }
     return $response->getApiResponse();
 }
 public function actionView()
 {
     $bankId = Yii::app()->request->getQuery('id');
     $bank = Bank::model()->findByPk($bankId);
     $departments = BankBranches::model()->getBankDepartment($bankId)->findAll();
     $rates = BankCourses::model()->getBankRates($bankId)->findAll();
     $this->render('view', array('bank' => $bank, 'departments' => $departments, 'rates' => $rates));
 }
Пример #3
0
 public function save()
 {
     foreach ($this->banks as $key => $bankInfo) {
         $bank = False;
         $bankResource = BankResources::model()->getBankResource($this->sourceType, $key)->find();
         if ($bankResource) {
             $bank = Bank::model()->findByPk($bankResource->bank_id);
         }
         if (!$bank) {
             $bank = new Bank();
         }
         foreach ($bankInfo as $sum => $info) {
             if (!$bank->id) {
                 //                    $bank->phone = $info['info']['tel'];
                 $bank->address = $info['info']['address'];
                 $bank->name = $info['name'];
                 $bank->save();
                 $bankResource = new BankResources();
                 $bankResource->bank_id = $bank->id;
                 $bankResource->source_id = $this->sourceType;
                 $bankResource->source_alias = $key;
                 $bankResource->save();
                 $newBranch = new BankBranches();
                 $newBranch->address = $info['info']['address'];
                 $yandexGeoDecode = @file_get_contents('http://geocode-maps.yandex.ru/1.x/?format=json&results=1&geocode=город Москва, ' . $newBranch->address);
                 if ($yandexGeoDecode) {
                     $answer = json_decode($yandexGeoDecode, True);
                     if (!isset($answer['response']) && count($answer['response']['GeoObjectCollection']['featureMember']) == 0) {
                         continue;
                     }
                     $pos = $answer['response']['GeoObjectCollection']['featureMember'][0]['GeoObject']['Point']['pos'];
                     $pos = explode(' ', $pos);
                     $newBranch->latitude = $pos[1];
                     $newBranch->longtitude = $pos[0];
                     $newBranch->preparePhone($info['info']['tel']);
                     $newBranch->save();
                     $newBranch->bank_id = $bank->id;
                     $newBranch->save();
                 }
             }
             if ($bank->id) {
                 $cur = new BankCourses();
                 $cur->bank_id = $bank->id;
                 $cur->buy = $info['pok'];
                 $cur->sale = $info['prod'];
                 $cur->sum = $sum;
                 $cur->currency = $this->siteCurrencyType;
                 $cur->save();
             }
         }
     }
 }
 public function run($args)
 {
     $mem_start = memory_get_usage();
     $url = 'http://www.banki.ru/api/';
     $data = array('id' => '1', 'method' => 'bankInfo/getBankList', 'jsonrpc' => '2.0', 'params' => array('region_id' => array(4), 'show_on_banki' => array(0, 1, 2)));
     $options = array('http' => array('header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => json_encode($data)));
     $context = stream_context_create($options);
     $result = file_get_contents($url, false, $context);
     $banks = json_decode($result, True);
     if (isset($banks['error'])) {
         print $banks['error'];
         die;
     }
     $banksData = array();
     $bankNewId = array();
     foreach ($banks['result']['data'] as $bankInfo) {
         switch ($bankInfo['region']) {
             case 'Москва':
                 $source_bank_id = $bankInfo['bank_id'];
                 $bank = Bank::model()->getBankResource(Yii::app()->params['sourceId']['bankiRu'], $source_bank_id)->find();
                 if (!$bank) {
                     $bank = new Bank();
                     $bank->name = $bankInfo['bank_name'];
                     $bank->source_id = Yii::app()->params['sourceId']['bankiRu'];
                     $bank->source_alias = $source_bank_id;
                     $bank->save();
                 }
                 $bankNewId[$source_bank_id] = $bank->id;
                 $banksData[$bank->id] = $bank;
                 break;
             default:
                 break;
         }
     }
     $data = array('id' => '2', 'method' => 'bankGeo/getObjectsByFilter', 'jsonrpc' => '2.0', 'params' => array('bank_id' => array_keys($bankNewId), 'limit' => 100000000, 'region_id' => array(4), 'type' => array('office', 'branch', 'cash'), 'with_empty_coordinates' => true));
     $options = array('http' => array('header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => json_encode($data)));
     $context = stream_context_create($options);
     $result = file_get_contents($url, false, $context);
     $branchData = json_decode($result, True);
     if (isset($branchData['error'])) {
         print_r($branchData['error']);
         die;
     }
     foreach ($branchData['result']['data'] as $branch) {
         $newBranch = BankBranches::model()->getBranchesResource(Yii::app()->params['sourceId']['bankiRu'], $branch['id'])->find();
         if (!$newBranch) {
             $newBranch = new BankBranches();
             $newBranch->address = $branch['address'];
             $data = array('id' => '3', 'method' => 'bank/getBankObjectsData', 'jsonrpc' => '2.0', 'params' => array('id_list' => array($branch['id'])));
             $options = array('http' => array('header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => json_encode($data)));
             $context = stream_context_create($options);
             $result = json_decode(file_get_contents($url, false, $context), true);
             $newBranch->latitude = $branch['latitude'];
             $newBranch->longtitude = $branch['longitude'];
             $newBranch->bank_id = $bankNewId[$branch['bank_id']];
             if (!isset($result['error'])) {
                 $ph = $newBranch->preparePhone($result['result']['data'][0]['phone']);
                 if ($result['result']['data'][0]['is_main_office'] == 1) {
                     if (isset($banksData[$bankNewId[$branch['bank_id']]])) {
                         $b = $banksData[$bankNewId[$branch['bank_id']]];
                         $b->phone = $ph;
                         $b->save();
                     }
                 }
             }
             $newBranch->save();
         }
     }
     echo memory_get_usage() - $mem_start;
 }