Example #1
0
 public function getResellersByZip($zip)
 {
     $region = $department = null;
     $resellersCity = $resellersRegion = $resellersDepartment = [];
     $city = Model::City()->where(['zip', '=', (string) $zip])->first(true);
     if ($city instanceof ZeliftCityModel) {
         $department = $city->department();
         $region = $city->region();
         $zones = Model::Customzonereseller()->where(['type', '=', 'city'])->where(['type_id', '=', $city->id])->exec();
         dd($zones);
         $resellersCity = $this->getResellersByZones($zones);
     } else {
         $dpt = substr($zip, 0, 2);
         $department = Model::Department()->where(['code', '=', (string) $dpt])->first(true);
         if ($department) {
             $region = $department->region();
         }
     }
     if ($region instanceof ZeliftRegionModel) {
         $zones = Model::Customzonereseller()->where(['type', '=', 'region'])->where(['type_id', '=', $region->id])->exec();
         $resellersRegion = $this->getResellersByZones($zones);
     }
     if ($department instanceof ZeliftDepartmentModel) {
         $zones = Model::Customzonereseller()->where(['type', '=', 'department'])->where(['type_id', '=', $department->id])->exec();
         $resellersDepartment = $this->getResellersByZones($zones);
     }
     return $this->unTuple($resellersCity, $resellersRegion, $resellersDepartment);
 }
Example #2
0
 public function siren($siren)
 {
     require_once APPLICATION_PATH . DS . '..' . '/public/vendeur/lib/simple_html_dom.php';
     set_time_limit(0);
     $key = 'siren.' . $siren;
     $cached = redis()->get($key);
     if (empty($cached)) {
         $html = dwn("http://www.societe.com/cgi-bin/fiche?rncs={$siren}&mode=prt");
         redis()->set($key, $html);
     } else {
         $html = $cached;
     }
     $html = utf8_encode($html);
     $infos = [];
     $html = str_get_html($html);
     $table = $html->find('table[id=rensjur]', 0);
     if (is_object($table)) {
         $trs = $table->find('tr');
         foreach ($trs as $tr) {
             $tds = $tr->find('td');
             $first = true;
             foreach ($tds as $td) {
                 if ($first) {
                     $attribute = Inflector::urlize($td->innertext, '_');
                 } else {
                     $value = $td->innertext;
                 }
                 $first = false;
             }
             $attribute = str_replace(['denomination', 'description_de_l_activite_de_l_entreprise', 'siret_siege', 'tranche_d_effectif'], ['raison_sociale', 'activite', 'siret', 'effectif'], $attribute);
             if ($attribute == 'activite') {
                 continue;
             }
             $infos[$attribute] = $value;
         }
     }
     $table = $html->find('table[id=rensjurcomplete]', 0);
     if (is_object($table)) {
         $trs = $table->find('tr');
         foreach ($trs as $tr) {
             $tds = $tr->find('td');
             $first = true;
             foreach ($tds as $td) {
                 if ($first) {
                     $attribute = Inflector::urlize($td->innertext, '_');
                 } else {
                     $value = $td->innertext;
                 }
                 $first = false;
             }
             $attribute = str_replace(['denomination', 'description_de_l_activite_du_siege', 'description_de_l_activite_de_l_entreprise', 'siret_siege', 'tranche_d_effectif', 'code_ape_naf_de_l_entreprise', 'code_ape_naf_du_siege', 'n_dossier'], ['raison_sociale', 'activite_siege', 'activite_entreprise', 'siret', 'effectif', 'naf_entrprise', 'naf_siege', 'numero_dossier'], $attribute);
             $infos[$attribute] = $value;
         }
         if (isset($infos['adresse_rcs'])) {
             $infos['adresse'] = $infos['adresse_rcs'];
         }
         if (isset($infos['adresse_insee'])) {
             $infos['adresse'] = $infos['adresse_insee'];
         }
         if (isset($infos['complement_nom_adressage'])) {
             $infos['nom_commercial'] = $infos['complement_nom_adressage'];
             unset($infos['complement_nom_adressage']);
         }
         $infos['code_postal'] = $infos['code_postal'];
         $infos['department_code'] = substr($infos['code_postal'], 0, 2);
         $department = Model::Department()->where(['code', '=', (string) $infos['department_code']])->first(true);
         if ($department) {
             $infos['department_id'] = $department->id;
             $infos['department'] = $department->name;
             $region = $department->region(true);
             if ($region) {
                 $infos['region_id'] = $region->id;
                 $infos['region'] = $region->name;
             }
         }
         if (isset($infos['adresse'])) {
             $coords = getCoords($infos['adresse'] . ' ' . $infos['code_postal'] . ' ' . $infos['ville']);
             $lat = isAke($coords, 'lat', false);
             $lng = isAke($coords, 'lng', false);
             if (false !== $lat) {
                 $infos['lat'] = $lat;
             }
             if (false !== $lng) {
                 $infos['lng'] = $lng;
             }
             $infos['coords'] = $coords;
         }
     }
     unset($infos['adresse_rcs'], $infos['nom_adressage'], $infos['adresse_insee']);
     return $infos;
 }
Example #3
0
 public function searchStreetBySellzone($sellzone_id, $q, $max = 299)
 {
     $collection = [];
     $sz = Model::Sellzone()->refresh()->find((int) $sellzone_id);
     if ($sz) {
         $url = 'http://search.mappy.net/search/1.0/find?extend_bbox=0&bbox=' . $sz->bbox . '&q=' . urlencode($q . ' ' . $sz->department) . '&favorite_country=250&language=FRE&loc_format=geojson&max_results=' . $max;
         $key = 'searchStreetBySellzones.' . $sellzone_id . '.' . sha1($q);
         $json = redis()->get($key);
         if (!$json) {
             $json = dwn($url);
             redis()->set($key, $json);
         }
         $tab = json_decode($json, true);
         if (isset($tab['addresses'])) {
             if (isset($tab['addresses']['features'])) {
                 if (count($tab['addresses']['features'])) {
                     foreach ($tab['addresses']['features'] as $infos) {
                         if (isset($infos['properties'])) {
                             if (isset($infos['properties']['address_components'])) {
                                 $address_components = $infos['properties']['address_components'];
                                 if (isset($address_components['postcode'])) {
                                     $address_components['zip'] = $address_components['postcode'];
                                 }
                                 if (isset($address_components['town'])) {
                                     $address_components['city'] = $address_components['town'];
                                 }
                                 if (isset($address_components['admin_1'])) {
                                     $address_components['region'] = $address_components['admin_1'];
                                 }
                                 if (isset($address_components['region'])) {
                                     if (isset($address_components['region']['label'])) {
                                         $address_components['region_name'] = $address_components['region']['label'];
                                     }
                                     if (isset($address_components['region']['code'])) {
                                         $address_components['region_id'] = $address_components['region']['code'];
                                     }
                                 }
                                 unset($address_components['region']);
                                 if (isset($address_components['way'])) {
                                     $address_components['address'] = $address_components['way'];
                                 }
                                 if (isset($address_components['way_number'])) {
                                     $address_components['address_number'] = $address_components['way_number'];
                                 }
                                 if (isset($address_components['country'])) {
                                     if (isset($address_components['country']['code'])) {
                                         $address_components['country_id'] = $address_components['country']['code'];
                                     }
                                     if (isset($address_components['country']['label'])) {
                                         $address_components['country_name'] = $address_components['country']['label'];
                                     }
                                 }
                                 unset($address_components['country']);
                                 if (isset($address_components['city']) && isset($address_components['zip'])) {
                                     if (!is_array($address_components['zip'])) {
                                         $dpt = (int) substr($address_components['zip'], 0, 2);
                                         $cityModel = Model::City()->where(['name', '=i', (string) $address_components['city']['label']])->where(['zip', '=', (string) $address_components['zip']])->first(true);
                                         if ($cityModel) {
                                             $address_components['city_name'] = $address_components['city']['label'];
                                             $address_components['city_id'] = $cityModel->id;
                                             unset($address_components['city']);
                                             $dptModel = $cityModel->department(true);
                                             if ($dptModel) {
                                                 $address_components['department_code'] = $dpt;
                                                 $address_components['department_id'] = $dptModel->id;
                                                 $address_components['department_name'] = $dptModel->name;
                                             } else {
                                                 $address_components['department'] = $dpt;
                                             }
                                         } else {
                                             $dptModel = Model::Department()->where(['code', '=', (string) $dpt])->first(true);
                                             if ($dptModel) {
                                                 $address_components['department_code'] = $dpt;
                                                 $address_components['department_id'] = $dptModel->id;
                                                 $address_components['department_name'] = $dptModel->name;
                                             } else {
                                                 $address_components['department'] = $dpt;
                                             }
                                         }
                                     }
                                 }
                                 unset($address_components['postcode']);
                                 unset($address_components['admin_1']);
                                 unset($address_components['town']);
                                 unset($address_components['way']);
                                 unset($address_components['way_number']);
                             }
                             if (isset($infos['properties']['formatted_address'])) {
                                 if (isset($infos['properties']['formatted_address']['label'])) {
                                     $address_components['address_label'] = $infos['properties']['formatted_address']['label'];
                                 }
                             }
                             if (isset($infos['properties']['viewport'])) {
                                 $viewport = $infos['properties']['viewport'];
                                 list($lng1, $lat1, $lng2, $lat2) = $viewport;
                                 $lng1 = (double) $lng1;
                                 $lat1 = (double) $lat1;
                                 $lng2 = (double) $lng2;
                                 $lat2 = (double) $lat2;
                             }
                         }
                         if (isset($infos['geometry'])) {
                             if (isset($infos['geometry']['geometries'])) {
                                 if (count($infos['geometry']['geometries'])) {
                                     $coords = current($infos['geometry']['geometries']);
                                     $coords = isAke($coords, 'coordinates', []);
                                     list($lng, $lat) = $coords;
                                     $lng = (double) $lng;
                                     $lat = (double) $lat;
                                 }
                             }
                         }
                         $lat = str_replace(',', '.', $lat);
                         $lng = str_replace(',', '.', $lng);
                         $lat1 = str_replace(',', '.', $lat1);
                         $lng1 = str_replace(',', '.', $lng1);
                         $lng2 = str_replace(',', '.', $lng2);
                         $lat1 = str_replace(',', '.', $lat1);
                         $lat2 = str_replace(',', '.', $lat2);
                         $return = array_merge(['lat' => $lat, 'lng' => $lng, 'lat1' => $lat1, 'lng1' => $lng1, 'lat2' => $lat2, 'lng2' => $lng2], $address_components);
                         ksort($return);
                         $collection[] = $return;
                     }
                 }
             }
         }
     }
     return $collection;
 }