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


include_once ("../include/mcglobal.inc.php");
include_once ("../include/auth.inc.php");
include_once ("../geo/geocode.inc.php");


// Check HTTP-Parameters
getSecHttpVars("1",array("f_act", "customerId", "cscIdRoot", "cscIdActual",
                  "f_srvp_plz", "f_ad_street", "f_srvpxa_hsno_from", "f_srvpxa_hsno_to", 
                  "f_ad_zipcode", "f_ad_city", "f_ad_country", "f_srvpxa_mode", "f_del_assoc", 
                  "orderClause", "statusMessage", "tourno", "deactivateMenu"));

$pageTitel = "PLZ-ADRESSEN";
include_once ("../admin/menu.php");
include_once ("../include/html.inc.php");

// Check for authentication access and granted rights
$usrAccessArray["hq"] = "1";
authCheckForAccess($hq_id, $usr_id, $emp_id, "1", $customerId, $cscIdRoot, $cscIdActual);
authCheckEmployeeRights($emp_id, "2", "1");

$numOfRows = 0;
mcTrim($f_srvp_plz); mcTrim($f_ad_street); mcTrim($f_ad_zipcode); mcTrim($f_ad_city); mcTrim($f_ad_country);
mcTrim($f_srvpxa_hsno_from); mcTrim($f_srvpxa_hsno_to);

if ($f_del_assoc == "") : $f_del_assoc = array(); endif;
$out = "";

// NEW association between zipcode and address
if ($f_act == "newZipcodeAddress") :

   // Get the id of the invoice zipcode
   $f_srvp_id_inv = getFieldValueFromId("serviceplz","srvp_plz",$f_srvp_plz,"srvp_id");

   // Check existence zipcode 
   $f_srvp_id = getFieldValueFromId("serviceplz","srvp_plz",$f_ad_zipcode,"srvp_id");
   
   // Try to get name of the city if does not exist
   if ($f_ad_city == "") :
      $sqlStmt = "SELECT st_city FROM phoenix_special.street WHERE st_zipcode = '$f_ad_zipcode'";
      $f_ad_city = $db->getOne($sqlStmt);     
   endif;

   // Check ranges of hsno´s
   // ... To be done to be beautiful... !!!

   if ($f_srvp_id_inv != "" && $f_srvp_id != "" && $f_ad_street != "") :
   
      // Get ad_id of the new address
      // insertStmt("address", array("ad_street", $f_ad_street, "ad_zipcode", $f_ad_zipcode, "ad_city", $f_ad_city, "ad_country", $f_ad_country));
      // $ad_id_new = getLastInsertID();

      // Fill "f_ad_city" with signs because of function call "insertAddress"
      if ($f_ad_city == "") : $f_ad_city = "???"; endif;
      if ($f_ad_country == "") : $f_ad_country = "DE"; endif;

      $tmpArray = insertAddress($f_ad_street, $f_ad_zipcode, $f_ad_city, "", $f_ad_country);
      $ad_id_new = $tmpArray[0];         

      // Insert relation
      if ($ad_id_new != "" && $ad_id_new != "0") :
         insertStmt("serviceplzaddress", array("hq_id", $hq_id, "srvp_id", $f_srvp_id_inv, "ad_id", $ad_id_new, "srvpxa_hsno_from", $f_srvpxa_hsno_from, "srvpxa_hsno_to", $f_srvpxa_hsno_to, "srvpxa_mode", $f_srvpxa_mode));
      else :
         $statusMessage = "Es wurde kein Eintrag angelegt!";
      endif;
      
      $f_act = "search";
   else :      
      if ($f_srvp_id_inv == "") :
         $statusMessage = "Die angegebene Rechnungs-PLZ existiert nicht!";
      elseif ($f_srvp_id == "") :
         $statusMessage = "Die angegebene PLZ ist nicht gültig!";
      elseif ($f_ad_street == "") :
         $statusMessage = "Es ist keine Straße angegeben!";
      else :
         $statusMessage = "Der Datensatz konnte nicht eingestellt werden! Bitte alle Felder vollständig ausfüllen!";
      endif;
   endif;
endif;

// REMOVE association between zipcode and address
if ($f_act == "removeZipcodeAddress") :

   TA("B");       

   // Entries to be deleted?
   $fieldsLength = count($f_del_assoc);

   if ($fieldsLength > 0 && $f_del_assoc[0] != "") :
   
      // Remove all associations according to the zipcode nested in array $f_del_zipcode_id
      for ($i = 0; $i < $fieldsLength; $i++) :

          deleteStmt("serviceplzaddress","srvpxa_id = '" . $f_del_assoc[$i] . "' AND hq_id = '" . $hq_id . "'");
      endfor;
      
      $f_act = "search";
   else :
      $statusMessage = "Bitte markieren Sie zu löschende Beziehungen durch anklicken der jeweiligen Checkbox hinter dem Eintrag!";
   endif;

   TA("C");
   TA("E");
endif;


// Generate search-resultset
// if ($f_act == "search" && $searchValues != "") :
if ($f_act == "search") :
        
   if (strlen($f_srvp_plz) > 0 || strlen($f_ad_street) > 0 || strlen($f_ad_zipcode) > 0 || strlen($f_ad_city) > 0 ||
       strlen($f_srvpxa_hsno_from) > 0 || strlen($f_srvpxa_hsno_to) > 0) :

      // Get the id of the area
      $f_srvp_id_inv = getFieldValueFromId("serviceplz","srvp_plz",$f_srvp_plz,"srvp_id");


      // *************************************************
      // * Selection of the mappings "address" => "area" *
      // *************************************************
      $whereClause = "";
      if ($f_ad_street != "") : $whereClause .= "ad.ad_street LIKE '" . $f_ad_street . "%'"; endif;      
      if ($whereClause != "" && $f_ad_zipcode != "") : $whereClause .= " AND "; endif;
      if ($f_ad_zipcode != "") : $whereClause .= "ad.ad_zipcode LIKE '" . $f_ad_zipcode . "%'"; endif;
      if ($whereClause != "" && $f_ad_city != "") : $whereClause .= " AND "; endif;      
      if ($f_ad_city != "") : $whereClause .= "ad.ad_city LIKE '" . $f_ad_city . "%'"; endif;      
      if ($whereClause != "" && $f_ad_country != "") : $whereClause .= " AND "; endif;      
      if ($f_ad_country != "") : $whereClause .= "ad.ad_country LIKE '" . $f_ad_country . "%'"; endif;

      // Only prefix !
      if ($whereClause != "" && $f_srvp_id_inv != "") : $whereClause .= " AND "; endif;
      if ($f_srvp_id_inv != "") : $whereClause .= "srvp.srvp_id = '" . $f_srvp_id_inv . "'"; endif;
      if ($whereClause != "" && $f_srvpxa_hsno_from != "") : $whereClause .= " AND "; endif;      
      if ($f_srvpxa_hsno_from != "") : $whereClause .= "srvpxa.srvpxa_hsno_from = '" . $f_srvpxa_hsno_from . "'"; endif;
      if ($whereClause != "" && $f_srvpxa_hsno_to != "") : $whereClause .= " AND "; endif;
      if ($f_srvpxa_hsno_to != "") : $whereClause .= "srvpxa.srvpxa_hsno_to = '" . $f_srvpxa_hsno_to . "'"; endif;
      
      if ($whereClause != "") : $whereClause .= " AND "; endif;

      if ($orderClause == "") : $orderClause = "ad.ad_street, srvpxa.srvpxa_hsno_to, ad.ad_zipcode"; endif;
      
      
      // *********************************************************************
      // * Selection of the associations between invoice zipcode and address *
      // *********************************************************************      
      $sqlquery = "SELECT ad.ad_id, ad.ad_street, ad.ad_zipcode, ad.ad_city, ad.ad_country," 
                   . "    srvpxa.srvpxa_id, srvpxa.srvpxa_hsno_from, srvpxa.srvpxa_hsno_to, srvpxa.srvpxa_mode,"
                   . "    srvp.srvp_id, srvp.srvp_plz"
                   . " FROM address AS ad, serviceplz AS srvp, serviceplzaddress AS srvpxa"
                   . " WHERE "    . $whereClause
                   . "    srvpxa.hq_id = '" . $hq_id . "'  AND"
                   . "    ad.ad_id = srvpxa.ad_id    AND"
                   . "    srvp.srvp_id = srvpxa.srvp_id"
                   . " ORDER BY " . $orderClause;
      // echo $sqlquery;
      $result = $db->dbQ($sqlquery);

      while ($row = $result->fetch_assoc()):            
            $numOfRows++;

            $out .= "<tr class=\"f10bp1\">";
            $out .= "<td> ". $row["srvp_plz"] . " &nbsp;</td> ";
            $out .= "<td> ". $row["ad_street"] . " &nbsp;</td>";            
            $out .= "<td> ". $row["srvpxa_hsno_from"] . " &nbsp;</td> ";
            $out .= "<td> ". $row["srvpxa_hsno_to"] . " &nbsp;</td> ";
            if ($row["srvpxa_mode"] == "1") :
               $out .= "<td> Nur ungerade &nbsp;</td> ";
            elseif ($row["srvpxa_mode"] == "2") :
               $out .= "<td> Nur gerade &nbsp;</td> ";
            else :
               $out .= "<td> &nbsp;</td> ";
            endif;
            $out .= "<td> ". $row["ad_zipcode"] . " &nbsp;</td> ";
            $out .= "<td> ". $row["ad_city"] . " &nbsp;</td> ";

            $out .= "<td align=\"center\"><input type=\"checkbox\" name=\"f_del_assoc[]\" value=\"" . $row["srvpxa_id"] . "\"></td>";
            $out .= "</tr>\n";
      endwhile;
                        
      $result->free();     
   else :
      $statusMessage = "Eingabe mind. 1 Zeichen in einem Feld!";
   endif;
endif;   
?>

<html>

<head>
<title><?php echo $pageTitel ?></title>

<style type="text/css">
  <?php include_once ("../css/phoenix.css.php"); ?>
  <?php include_once ("../css/navigation.css.php"); ?>
  <?php include_once ("../css/mc.css.php"); ?>
  
  table, th, td {
    border: 1px solid var(--primary-color);;
    border-collapse: collapse;
    padding: 8px;
  }
</style>

<?php include_once ("../include/js_framework.inc.php"); ?>

<script type="text/javascript">
<!--
// NAVIGATION
<?php echo $jsMenuOut; ?>

function finishPage(mode) {
  if (mode == 'removeZipcodeAddress') {
     if (confirm('Möchten Sie die markierten Einträge wirklich löschen?')) {
     	document.forms[0].f_act.value = mode;
        document.forms[0].submit();
     }
  }
  if (mode == 'newZipcodeAddress') {
     if (confirm('Möchten Sie die neue Beziehung wirklich einstellen?')) {
     	document.forms[0].f_act.value = mode;
        document.forms[0].submit();
     }
  }  
};

function clearFields() {
  document.forms[0].f_srvp_plz.value = '';
  document.forms[0].f_ad_street.value = '';
  document.forms[0].f_srvpxa_hsno_from.value = '';
  document.forms[0].f_srvpxa_hsno_to.value = '';
  document.forms[0].f_ad_zipcode.value = '';
  document.forms[0].f_ad_city.value = '';
};
-->
</script>
</head>

<body onLoad="<?php echo $phpCurrentNavigationOnLoad ?>displayStatusMessage();">

<?php echo $phpMenuOut ?>
<?php echo $phpReducedMenuOut ?>
<?php echo $phpPageTitelOut ?>

<div class="mc_page-header">
  <?php echo getLngt("Zuordnung Adressen zu PLZn für die Preisberechnung") ?>
</div>

<div class="maincontent mc_elem" name="maincontent" id="maincontent">

<form action="srvpa_list.php" method="post">
  <input type="hidden" name="f_act" value="">  
  <input type="hidden" name="customerId" value="<?php echo $customerId ?>">
  <input type="hidden" name="cscIdRoot" value="<?php echo $cscIdRoot ?>">
  <input type="hidden" name="cscIdActual" value="<?php echo $cscIdActual ?>">  
  <input type="hidden" name="orderClause" value="<?php echo $orderClause ?>">
  <?php echo $phpCurrentNavigationInputHidden ?>
  <input type="hidden" name="deactivateMenu" value="<?php echo ec($deactivateMenu) ?>">

  <?php echo htmlDivLineSpacer("20px"); ?>

  <?php echo defineButtonType08(getLngt("Suchen"), "action_search", "javascript:document.forms[0].f_act.value='search';document.forms[0].submit();", "180", "left", "3"); ?>
  <?php echo defineButtonType08(getLngt("Felder zurücksetzen"), "action_clear", "javascript:clearFields();", "180", "left", "3"); ?>
  <?php echo defineButtonType08(getLngt("Neu"), "action_clear", "javascript:finishPage('newZipcodeAddress');", "180"); ?>
  <?php echo htmlDivLineSpacer("10px"); ?>

  <div>
<?php if (false) : ?>
    <table border="0">
      <tr>      
        <td>
          <table border="0">
            <tr>
              <td>
<?php endif; ?>
                <table class="f8np1" cellpadding="0">
                  <tr>
                    <td><input type="text" name="f_srvp_plz" value="<?php echo $f_srvp_plz ?>" size="5"></td>
                    <td><input type="text" name="f_ad_street" value="<?php echo $f_ad_street ?>" size="50"></td>
                    <td><input type="text" name="f_srvpxa_hsno_from" value="<?php echo $f_srvpxa_hsno_from ?>" size="7"></td>
                    <td><input type="text" name="f_srvpxa_hsno_to" value="<?php echo $f_srvpxa_hsno_to ?>" size="7"></td>
                    <td>
                        <input type="radio" name="f_srvpxa_mode" value="0" <?php if ($f_srvpxa_mode == "0" || $f_srvpxa_mode == "") : echo "checked"; endif;  ?>> Alle<br>
                        <input type="radio" name="f_srvpxa_mode" value="1" <?php if ($f_srvpxa_mode == "1") : echo "checked"; endif;  ?>> Ungerade<br>
                        <input type="radio" name="f_srvpxa_mode" value="2" <?php if ($f_srvpxa_mode == "2") : echo "checked"; endif;  ?>> Gerade
                    </td>                                                      
                    <td><input type="text" name="f_ad_zipcode" value="<?php echo $f_ad_zipcode ?>" size="5"></td>
                    <td><input type="text" name="f_ad_city" value="<?php echo $f_ad_city ?>" size="30"></td>
                    <td><?php echo defineButtonType10("Löschen", "action_remove", "finishPage('removeZipcodeAddress');", "80", "left"); ?></td>
                  </tr>
                  <tr>
                    <td><a href="javascript:document.forms[0].orderClause.value='srvp.srvp_plz';document.forms[0].f_act.value='search';document.forms[0].submit();">PLZ</a></td>
                    <td><a href="javascript:document.forms[0].orderClause.value='ad_street, srvpxa.srvpxa_hsno_from';document.forms[0].f_act.value='search';document.forms[0].submit();">Straße</a></td>
                    <td><a href="javascript:document.forms[0].orderClause.value='srvpxa.srvpxa_hsno_from';document.forms[0].f_act.value='search';document.forms[0].submit();">Hausnr. von</a></td>
                    <td><a href="javascript:document.forms[0].orderClause.value='srvpxa.srvpxa_hsno_to';document.forms[0].f_act.value='search';document.forms[0].submit();">Hausnr. bis</a></td>
                    <td><a href="javascript:document.forms[0].orderClause.value='srvpxa.srvpxa_mode';document.forms[0].f_act.value='search';document.forms[0].submit();">Modus</a></td>
                    <td><a href="javascript:document.forms[0].orderClause.value='ad.ad_zipcode';document.forms[0].f_act.value='search';document.forms[0].submit();">PLZ</a></td>
                    <td><a href="javascript:document.forms[0].orderClause.value='ad.ad_city';document.forms[0].f_act.value='search';document.forms[0].submit();">Ort</a></td>
                    <td>&nbsp;</td>
                  </tr>                                
                  <?php echo $out ?>
                </table>
<?php if (false) : ?>
              </td>
            </tr>        
          </table>
        </td>      
    </table>
<?php endif; ?>
  </div>
  <?php echo htmlDivLineSpacer("10px"); ?>
  
  <div>
    Anzahl Einträge: <?php echo $numOfRows ?><?php if ($numOfRows == "0" && $f_act == "search" && $statusMessage == "") : echo " (Keine Einträge gefunden.)"; endif; ?>
  </div>
  <?php echo htmlDivLineSpacer("10px"); ?>

</form>
</div>
</body>

</html>