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


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


// Check HTTP-Parameters
getSecHttpVars("1",array("f_act", "customerId", "cscIdRoot", "cscIdActual",
                  "f_name", "f_comp", "f_comp2", "f_street", "f_hsno", "f_zipcode",
                  "f_city", "f_country", "f_remark", "f_person",
                  "orderClause", "tourNo", "statusMessage"));

// Select user-type for mode of security check
$userType = getFieldValueFromId("user","usr_id",$usr_id,"usr_type");

// Check authentication verifying emmployee an his/her costcenter- and customer-association
if ( !( ($userType == "1" && $cscIdRoot != "") || authCheck($hq_id,$usr_id,$emp_id,$cscIdRoot,$customerId,$cscIdActual) ) ) :
   die ("$PHP_SELF: Access denied!");
endif;

   // Generate search-resultset
   if ($f_act == "search") :

      // *********************************************************************
      // * Selection of the costcenters of the current customer for the list *
      // *********************************************************************
      $whereClause = "";
      if ($f_name != "") : $whereClause .= "csc.csc_name LIKE '" . $f_name . "%'"; endif;
      if ($whereClause != "" && $f_comp != "") : $whereClause .= " AND "; endif;
      if ($f_comp != "") : $whereClause .= "cscad.cscad_comp LIKE '" . $f_comp . "%'"; endif;
      if ($whereClause != "" && $f_comp2 != "") : $whereClause .= " AND "; endif;
      if ($f_comp2 != "") : $whereClause .= "cscad.cscad_comp2 LIKE '" . $f_comp2 . "%'"; endif;
      if ($whereClause != "" && $f_street != "") : $whereClause .= " AND "; endif;
      if ($f_street != "") : $whereClause .= "ad.ad_street LIKE '" . $f_street . "%'"; endif;
      if ($whereClause != "" && $f_hsno != "") : $whereClause .= " AND "; endif;
      if ($f_hsno != "") : $whereClause .= "cscad.cscad_hsno LIKE '" . $f_hsno . "%'"; endif;
      if ($whereClause != "" && $f_zipcode != "") : $whereClause .= " AND "; endif;
      if ($f_zipcode != "") : $whereClause .= "ad.ad_zipcode LIKE '" . $f_zipcode . "%'"; endif;
      if ($whereClause != "" && $f_city != "") : $whereClause .= " AND "; endif;
      if ($f_city != "") : $whereClause .= "ad.ad_city LIKE '" . $f_city . "%'"; endif;
      if ($whereClause != "" && $f_country != "") : $whereClause .= " AND "; endif;
      if ($f_country != "") : $whereClause .= "ad.ad_country LIKE '" . $f_country . "%'"; endif;
      if ($whereClause != "" && $f_remark != "") : $whereClause .= " AND "; endif;
      if ($f_remark != "") : $whereClause .= "cscad.cscad_remark LIKE '" . $f_remark . "%'"; endif;
      if ($whereClause != "" && $f_person != "") : $whereClause .= " AND "; endif;
      if ($f_person != "") : $whereClause .= "cscad.cscad_person LIKE '" . $f_person . "%'"; endif;


      // Check authentication
      if ($whereClause != "" && $f_cmp_authenticated == "1") : $whereClause .= " AND "; endif;
      if ($f_cmp_authenticated == "1") : $whereClause .= "cmp.cmp_authenticated LIKE '" . $f_cmp_authenticated . "%'"; endif;

      if ($whereClause != "") : $whereClause = " AND " . $whereClause; endif;

      if ($orderClause == "") : $orderClause = "cscad.cscad_comp, ad.ad_street"; endif;


      // ***********************************************************************
      // * Selection of the costcenter based on a search by companyname-prefix *
      // ***********************************************************************
      $sqlquery = "SELECT csc.csc_id, csc.csc_name," .
                        " cscad.ad_id, cscad.cscad_comp, cscad.cscad_comp2, cscad.cscad_hsno," .
                        " cscad.cscad_remark, cscad.cscad_person, ad.ad_street, ad.ad_zipcode, ad.ad_city, ad.ad_country"
                   . " FROM customer AS cs, costcenter AS csc, costcenteraddress AS cscad, address AS ad"
                   . " WHERE cs.hq_id = '$hq_id'           AND"
                   . "       cs.cs_id = '$customerId'      AND"
                   . "       cs.cs_id = csc.cs_id          AND"
                   . "       csc.csc_id =  cscad.csc_id    AND"
                   . "       cscad.adt_id = '4'            AND"
                   . "       cscad.ad_id = ad.ad_id "
                   .     $whereClause
                   . " ORDER BY " . $orderClause;

      $result = $db->query($sqlquery);
      if (DB::isError($result)) die ("$PHP_SELF: '$sqlquery'" . $result->getMessage());

      $numOfCostcenters = 0;
      $out = "";
      while ($row = $result->fetch_assoc()):
            $numOfCostcenters++;

            $v_ad_id        = $row["ad_id"];
            $v_ad_street    = $row["ad_street"];
            $v_ad_zipcode   = $row["ad_zipcode"];
            $v_ad_city      = $row["ad_city"];
            $v_ad_country   = $row["ad_country"];
            $v_csc_id       = $row["csc_id"];
            $v_csc_name     = $row["csc_name"];
            $v_cscad_comp   = $row["cscad_comp"];
            $v_cscad_comp2  = $row["cscad_comp2"];
            $v_cscad_hsno   = $row["cscad_hsno"];
            $v_cscad_remark = $row["cscad_remark"];
            $v_cscad_person = $row["cscad_person"];

            $out .= "<tr class=\"f10bp1\">";
            // $out .= "<td class=\"f10bp1_red\"> $v_csc_name </td> ";
            $out .= "<td>&nbsp;" . "<a href=\"javascript:finishPage('".$v_csc_id."','".$v_csc_name."','".$v_cscad_comp."','".$v_cscad_comp2."','".$v_ad_street."','".$v_cscad_hsno."','".$v_ad_zipcode."','".$v_ad_city."','".$v_ad_country."','".$v_cscad_remark."','".$v_cscad_person."');\">"
                               . $v_csc_name . "</a>" . "</td>";
            $out .= "<td> $v_cscad_comp </td> ";
            $out .= "<td> $v_ad_street </td> ";
            $out .= "<td> $v_cscad_hsno </td> ";
            $out .= "<td> $v_ad_zipcode </td> ";
            $out .= "<td> $v_ad_city </td> ";
            $out .= "<td> $v_cscad_remark &nbsp;</td> ";
            $out .= "<td> $v_cscad_person &nbsp;</td> ";
            // $out .= "<td>&nbsp;" . "<a href=\"javascript:finishPage('".$v_csc_name."','".$v_cscad_comp."','".$v_cscad_comp2."','".$v_ad_street."','".$v_cscad_hsno."','".$v_ad_zipcode."','".$v_ad_city."','".$v_ad_country."','".$v_cscad_remark."','".$v_cscad_person."');\">"
            //                       . "<img src=\"../images/arrow_right.jpg\" border=\"0\" height=\"10\" width=\"25\">"
            //                       . "</a>" . "</td>";
            $out .= "</tr>\n";
      endwhile;

      $result->free();
   endif;
?>

<html>

<head>
<title>votian: Kostenstellen-Auswahl</title>
<link rel="stylesheet" type="text/css" href="../css/phoenix.css.php">

<script type="text/javascript">
<!--
var statusMessage = "<?php echo $statusMessage ?>";
var tourNo = "<?php echo $tourNo ?>";

var v_comp = '';
var v_comp2 = '';
var v_street = '';
var v_hsno = '';
var v_zipcode = '';
var v_city = '';
var v_country = '';
var v_remark = '';
var v_person = '';

//opener.parent.frames[2].isCurrentlySubmitting = false;

function finishPage(csc_id,name,comp,comp2,street,hsno,zipcode,city,country,remark,person) {
  v_csc_id = csc_id;
  v_name = name;
  v_comp = comp;
  v_comp2 = comp2;
  v_street = street;
  v_hsno = hsno;
  v_zipcode = zipcode;
  v_city = city;
  v_country = country;
  v_remark = remark;
  v_person = person;
//  alert(v_csc_id + ' ' + v_name + ' ' + v_comp + ' ' + v_comp2 + ' ' + v_street + ' ' + v_hsno  + ' ' + v_zipcode + ' ' + v_city + ' ' + v_country + ' ' + v_remark + ' ' + v_person);
  opener.setJobList(tourNo, opener.jl_csc_id, v_csc_id);
  opener.setJobList(tourNo, opener.jl_tr_comp, v_comp);
  opener.setJobList(tourNo, opener.jl_tr_person, v_person);
  opener.setJobList(tourNo, opener.jl_ad_street, v_street);
  opener.setJobList(tourNo, opener.jl_tr_hsno, v_hsno);
  opener.setJobList(tourNo, opener.jl_ad_zipcode, v_zipcode);
  opener.setJobList(tourNo, opener.jl_ad_city, v_city);
  opener.setJobList(tourNo, opener.jl_tr_remark, v_remark);
//  opener.refreshFormFields();
  self.setTimeout("opener.refreshFormFields()", 100);
  //opener.setCurTourNo(tourNo);
  //opener.parent.frames[1].document.forms[1].elements[0].focus();
//  opener.checkSendTour();
  self.setTimeout("opener.checkSendTour()", 150);
//  self.close();
  self.setTimeout("self.close()", 200);
};

function displayStatusMessage() {
 if (statusMessage != "") {
    alert(statusMessage);
 }
};
-->
</script>
</head>

<body onLoad="displayStatusMessage();">
  <form action="csc_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 ?>">
  <input type="hidden" name="tourNo" value="<?php echo $tourNo ?>">

  <table border="0">
    <tr>
      <td>
        <table border="0">
          <tr>
            <td class="f12bp1_red">
              Kostenstellen<br><br>
            </td>
          </tr>
          <tr>
            <td>
              <a href="javascript:document.forms[0].f_act.value='search';document.forms[0].submit();">Suchen</a>
              <br><br>
              <table class="f8np1" border="1" cellpadding="0">
                <tr>
                  <td><input type="text" name="f_name" value="<?php echo $f_name ?>" size="15"></td>
                  <td><input type="text" name="f_comp" value="<?php echo $f_comp ?>" size="15"></td>
                  <td><input type="text" name="f_street" value="<?php echo $f_street ?>" size="25"></td>
                  <td><input type="text" name="f_hsno" value="<?php echo $f_hsno ?>" size="5"></td>
                  <td><input type="text" name="f_zipcode" value="<?php echo $f_zipcode ?>" size="5"></td>
                  <td><input type="text" name="f_city" value="<?php echo $f_city ?>" size="20"></td>
                  <td><input type="text" name="f_remark" value="<?php echo $f_remark ?>" size="20"></td>
                  <td><input type="text" name="f_person" value="<?php echo $f_person ?>" size="20"></td>
                </tr>
                <tr>
                  <td><a href="javascript:document.forms[0].orderClause.value='csc.csc_name';document.forms[0].f_act.value='search';document.forms[0].submit();">Kostenstelle</a></td>
                  <td><a href="javascript:document.forms[0].orderClause.value='cscad.cscad_comp';document.forms[0].f_act.value='search';document.forms[0].submit();">Firma</a></td>
                  <td><a href="javascript:document.forms[0].orderClause.value='ad.ad_street';document.forms[0].f_act.value='search';document.forms[0].submit();">Straße</a></td>
                  <td><a href="javascript:document.forms[0].orderClause.value='cscad.cscad_hsno';document.forms[0].f_act.value='search';document.forms[0].submit();">Hausnr.</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><a href="javascript:document.forms[0].orderClause.value='cscad.cscad_remark';document.forms[0].f_act.value='search';document.forms[0].submit();">Bemerkung</a></td>
                  <td><a href="javascript:document.forms[0].orderClause.value='cscad.cscad_person';document.forms[0].f_act.value='search';document.forms[0].submit();">Person</a></td>
                </tr>
                <?php echo $out ?>
              </table>
              <br><br>
              Anzahl Einträge: <?php echo $numOfCostcenters ?>
            </td>
          </tr>
        </table>
      </td>
  </table>

</body>

</html>