<?php
/*=======================================================================
 *
 * service_radius.php
 *
 * Autor: Marc Vollmann
 *
 =======================================================================*/

include_once("../include/global.inc.php");
include_once("../include/auth.inc.php");


// Get the rights of the employee logged in and check the accessibility
if (!(authCheckEmployeeRights($emp_id, "0") || authCheckEmployeeRights($emp_id, "1") || authCheckEmployeeRights($emp_id, "14"))) :
   die ("$PHP_SELF: Access denied!");
endif;

$httpVars = array("f_act", "csId", "deactivateMenu");

getSecHttpVars("1", $httpVars);                         

getLanguage(__FILE__);

$pageTitel = getLngt("RADIUS");
// include_once ("../geo/geocode.inc.php");
include_once("../admin/menu.php");
include_once("../locating/xServer.inc.php");
include_once("../include/inc_geo.inc.php");
// include_once ("../include/html.inc.php");

getCurrentScript(__FILE__);


// Execution-Time for script
set_time_limit(0);

// Activate buffering
ob_start();

$long = "";
$lat = "";


if ($f_act == "") :    // $f_act == "xxx" && $cs_id != ""
   
   // Get address of the current customer
   // ...

   // Addresses
   $theAdresses = array(array("22523","Hamburg","Holsteiner Chaussee","49"),
                        array("24143","Kiel","Theodor-Heuss-Ring","136"),
                        array("28201","Bremen","Neuenlander Str.","113A"),
                        array("28307","Bremen","Werner Steenken Str.","5"),
                        array("27572","Bremerhaven","Seeborg","6"),
                        array("26131","Oldenburg","Edewechter Landstr.","149"),
                        array("26389","Wilhelmshaven","Heuweg","1"),
                        array("30916","Isernhagen","Ernst-Grote-Straße","2"),
                        array("30449","Hannover","Bornumerstr.","19"),
                        array("30823","Garbsen","Langenhagener Straße","1"),
                        array("38122","Braunschweig","Fabrikstr.","2"),
                        array("38444","Wolfsburg","Brandgehaege","7"),
                        array("39128","Magdeburg","Silberbergweg","2"),
                        array("80939","München-Fröttmaning","Muthmannstr.","4"),
                        array("81249","München","Hans-Steinkohl-Straße","30"),
                        array("64293","Darmstadt","Otto-Röhm-Str.","76"),
                        array("65203","Wiesbaden","Friedrich Bergius Str.","21"),
                        array("60386","Frankfurt","Hanauer Landstr.","477"),
                        array("60437","Frankfurt","Züricher Str.","11"),
                        array("63450","Hanau","Kinzigheimer Weg","132"),
                        array("55128","Mainz","Am Schleifweg","5"),
                        array("55252","Mainz","Kurt Hebach Straße","1"),
                        array("49084","Osnabrück","Hannoversche Str.","111"),
                        array("48157","Münster","An der alten Ziegelei","33"),
                        array("45711","Datteln","Walter-Sauer-Str.","2"),
                        array("44625","Herne","Bochumer Str.","227"),
                        array("44145","Dortmund","Borsigstr.","20-22"),
                        array("42285","Wuppertal","Oberbergische Str.","201"),
                        array("45881","Gelsenkirchen","Caubstraße",""),
                        array("45356","Essen","Econova Allee","2"),
                        array("47167","Duisburg","Theodor Heuss Str.","79-89"),
                        array("47803","Krefeld","Mevissenstr.","45"),
                        array("47443","Moers","Franz-Haniel-Str.","71"),
                        array("41063","Mönchengladbach","Kuenkelstr.","88"),
                        array("41199","Mönchengladbach","Reststrauch","305"),
                        array("33104","Paderborn","Heisenbergstr.","2"),
                        array("33334","Gütersloh","Hans Boeckler Str.","57"),
                        array("33605","Bielefeld","Detmolder Str.","246"),
                        array("52382","Niederzier","Industriestr.","19"),
                        array("56070","Koblenz","August Thyssen Str.","10"));

// $theAdresses = array(array("22523","Hamburg","Holsteiner Chaussee","49"));

   
   $theAdressesLen = count($theAdresses);
   echo "Anzahl Adressen: " . $theAdressesLen . "<br><br>";
   for ($i = 0; $i < $theAdressesLen; $i++) :
       $address = $theAdresses[$i];
       $zipcode = $address[0];
       $city = $address[1];
       $street = $address[2];
       $hsno = $address[3];
$hq_id = "0";
$cs_id = $i;
       // First remove existing entries regarding the customer in $cs_id
       deleteStmt("serviceradius","cs_id = " . $cs_id);

       echo $i . ".: " . $zipcode . " " . $city . " " . $street . " " . $hsno . ": &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
   
       // $coords[0][0] : 100 = gefunden
       // $coords[0][1] : Länge
       // $coords[0][2] : Breite
       $coords = XSfindAddress("D", $zipcode, $city, $street, $hsno);
       if ($coords[0][0] == 100) :
          // Found
          $long = $coords[0][1];
          $lat  = $coords[0][2];
          echo "lat: " . $lat . "&nbsp;&nbsp;&nbsp;" . "long: " . $long . ": &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

          $distance = "10";
          $distance2 = "";
          $radiusAreaNo = "1";
          // Get all areas 
          $tmpArray = getDistanceData($distance, $long, $lat, $distance2);
          $tmpArrayLen = count($tmpArray);
          for ($j = 0; $j < $tmpArrayLen; $j++) :
              // Insert into db
              $tmpIncArray = array();
              if (($j + 1) < $tmpArrayLen) :
                 $tmpIncArray = $tmpArray[($j + 1)];
              endif;
              $srvr_id_new = insertServiceRadius($tmpArray[$j], $hq_id, $cs_id, $radiusAreaNo, $tmpIncArray);
          endfor;
          echo "[0-10] ";
          ob_flush(); flush();

          $distance = "20";
          $distance2 = "10";
          $radiusAreaNo = "2";
          // Get all areas 
          $tmpArray = getDistanceData($distance, $long, $lat, $distance2);
          $tmpArrayLen = count($tmpArray);
          for ($j = 0; $j < $tmpArrayLen; $j++) :
              // Insert into db
              $tmpIncArray = array();
              if (($j + 1) < $tmpArrayLen) :
                 $tmpIncArray = $tmpArray[($j + 1)];
              endif;
              $srvr_id_new = insertServiceRadius($tmpArray[$j], $hq_id, $cs_id, $radiusAreaNo, $tmpIncArray);
          endfor;
          echo "[10-20] ";
          ob_flush(); flush();

          $distance = "30";
          $distance2 = "20";
          $radiusAreaNo = "3";
          // Get all areas 
          $tmpArray = getDistanceData($distance, $long, $lat, $distance2);
          $tmpArrayLen = count($tmpArray);
          for ($j = 0; $j < $tmpArrayLen; $j++) :
              // Insert into db
              $tmpIncArray = array();
              if (($j + 1) < $tmpArrayLen) :
                 $tmpIncArray = $tmpArray[($j + 1)];
              endif;
              $srvr_id_new = insertServiceRadius($tmpArray[$j], $hq_id, $cs_id, $radiusAreaNo, $tmpIncArray);
          endfor;
          echo "[20-30] ";
          ob_flush(); flush();
       else :
         	echo "ADDRESS NOT FOUND, SIR!!!";
       endif;
       echo "<br>";
       ob_flush(); flush();
   endfor;
endif;

// Deactivate buffering and flush data
ob_end_flush();


/*
22523	Hamburg	Holsteiner Chaussee 49
24143	Kiel	Theodor-Heuss-Ring/Ostring
28201	Bremen	Neuenlander Str. 113A
28307	Bremen	Werner Steenken Str. 5
27572	Bremerhaven	Seeborg 6
26131	Oldenburg	Edewechter Landstr. 149
26389	Wilhelmshaven	Heuweg
30916	Hannover	Ernst Grote Str. 2
30449	Hannover	Bornumerstr. 19
30823	Hannover	Langenhagenerstr. 1
38122	Braunschweig	Fabrikstr. 2
38444	Wolfsburg	Brandgehaege 7
39128	Magdeburg	Silberbergweg 2
80939	München-Fröttmaning	Muthmannstr. 4
81249	München-Freiham	Hans-Steinkohl-Str. 30
64293	Darmstadt	Otto-Röhm-Str. 76
65203	Wiesbaden	Friedrich Bergius Str. 21
60386	Frankfurt	Hanauer Landstr. 477
60437	Frankfurt	Züricher Str. 11
63450	Hanau	Kinzigheimer Weg 132
55128	Mainz	Am Schleifweg 5
55252	Mainz	Kurt Hebach Straße 1
49084	Osnabrück	Hannoversche Str. 111
48157	Münster	An der alten Ziegelei 33
45711	Datteln	Walter Sauer Str. 2
44625	Herne	Bochumer Str. 227
44145	Dortmund	Borsigstr. 20-22
42285	Wuppertal	Oberbergische Str. 201
45881	Gelsenkirchen	Caubstraße
45356	Essen	Econova Allee 2
47167	Duisburg	Theodor Heuss Str. 79-89
47803	Krefeld	Mevissenstr. 45
47443	Moers	Franz-Haniel-Str. 71
41063	Mönchengladbach	Kuenkelstr. 88
41199	Mönchengladbach	Reststrauch 305
33104	Paderborn	Heisenbergstr. 2
33334	Gütersloh	Hans Boeckler Str. 57
33605	Bielefeld	Detmolder Str. 246
52382	Niederzier b. Düren	Industriestr. Rurbenden 19
56070	Koblenz	August Thyssen Str. 10


CREATE TABLE serviceradius (
  srvr_id int(10) NOT NULL auto_increment,
  hq_id int(10) NOT NULL default '0',
  cs_id int(10) NOT NULL default '0',
  srvr_zipcode varchar(10) NOT NULL,
  srvr_city_district varchar(50) NOT NULL,
  srvr_radiusarea_no tinyint(3) default '0',
  PRIMARY KEY  (srvr_id)
) ENGINE=InnoDB;


select cs_id, srvr_radiusarea_no, count(*)
  from serviceradius
  group by cs_id, srvr_radiusarea_no
  order by cs_id, srvr_radiusarea_no
  limit 0,1000
  
select cs_id, srvr_zipcode, srvr_city_district, count(*) 
  from serviceradius 
  group by cs_id, srvr_zipcode, srvr_city_district 
  order by cs_id, srvr_zipcode, srvr_city_district
  limit 0,1000
*/


?>
