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

include_once ("../include/mcglobal.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, "7"))) : die ("$PHP_SELF: Access denied!"); endif;

// 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") || authCheck($hq_id,$usr_id,$emp_id,$cscIdRoot,$customerId,$cscIdActual) ) ) :
   die ("$PHP_SELF: Access denied!");
endif;

// Check HTTP-Parameters
getSecHttpVars("1",array("f_act", "generic", "f_mode", "companyId", "orderClause", "statusMessage", "f_cmp_authenticated", "f_searchmode", "f_cmp_match",
                         "f_jb_id", "f_days", "f_cmp_comp", "f_cmp_comp2", "f_cs_eid", "f_usr_name", "f_usr_phone", "f_usr_email", "f_csc_name", "f_csc_is_extern",
                         "f_street", "f_hsno", "f_zipcode", "f_city", "f_country", "f_remark", "f_person", "tourno"));

$numOfRows = 0;
$htmlClass01 = "class=\"smaller\"";   // input,select

if ($f_searchmode == "") : $f_searchmode = "1"; endif;
if ($f_csc_is_extern == "") : $f_csc_is_extern = "0"; endif;


$f_jb_id = trim($f_jb_id);
if (!is_numeric($f_jb_id)) : $f_jb_id = ""; endif;

$f_days = trim($f_days);
if ($f_days == "" || !is_numeric($f_days)) : $f_days = "0"; endif;

$f_cmp_comp = trim($f_cmp_comp);
$f_cmp_comp2 = trim($f_cmp_comp2);
$f_cmp_match = trim($f_cmp_match);
$f_cs_eid = trim($f_cs_eid);
$f_csc_name = trim($f_csc_name);
$f_usr_name = trim($f_usr_name);
$f_usr_phone = trim($f_usr_phone);
$f_usr_email = trim($f_usr_email);
$f_street = trim($f_street);
$f_hsno = trim($f_hsno);
$f_zipcode = trim($f_zipcode);
$f_city = trim($f_city);
$f_country = trim($f_country);
$f_remark = trim($f_remark);
$f_person = trim($f_person);

// if called e.g. by the first time with empty search-fields
$searchValues = $f_jb_id . $f_cmp_comp . $f_cmp_comp2 . $f_cmp_match . $f_cs_eid . $f_csc_name . $f_street . $f_hsno . $f_zipcode . $f_city . $f_country . $f_remark . $f_person;
// $searchValues .= $f_usr_name . $f_usr_phone . $f_usr_email;

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

      if (strlen($f_jb_id) > 0 || strlen($f_cmp_comp) > 2 || strlen($f_cmp_comp2) > 2 || strlen($f_cmp_match) > 2 || strlen($f_cs_eid) > 2 || strlen($f_csc_name) > 0 ||
          strlen($f_street) > 2 || strlen($f_hsno) > 0 || strlen($f_zipcode) > 2 || strlen($f_city) > 2 ||
          strlen($f_remark) > 2 || strlen($f_person) > 2) :

      // *******************************************
      // * Selection of the customers for the list *
      // *******************************************
      if ($f_searchmode == "1") : $prefix = "%"; else : $prefix = ""; endif;
      $whereClause = "";
      if ($f_jb_id != "") : $whereClause .= "jb.jb_id = '" . $f_jb_id . "'"; endif;
      if ($whereClause != "" && $f_cmp_comp != "") : $whereClause .= " AND "; endif;
      if ($f_cmp_comp != "") : $whereClause .= "tr.tr_comp LIKE '" . $prefix . $f_cmp_comp . "%'"; endif;
      if ($whereClause != "" && $f_cmp_comp2 != "") : $whereClause .= " AND "; endif;
      if ($f_cmp_comp2 != "") : $whereClause .= "tr.tr_comp2 LIKE '" . $prefix . $f_cmp_comp2 . "%'"; endif;
      if ($whereClause != "" && $f_cmp_match != "") : $whereClause .= " AND "; endif;
      if ($f_cmp_match != "") : $whereClause .= "cmp.cmp_match LIKE '" . $prefix . $f_cmp_match . "%'"; endif;
      if ($whereClause != "" && $f_cs_eid != "") : $whereClause .= " AND "; endif;
      if ($f_cs_eid != "") : $whereClause .= "cs.cs_eid LIKE '" . $prefix . $f_cs_eid . "%'"; endif;
      if ($whereClause != "" && $f_csc_name != "") : $whereClause .= " AND "; endif;
      if ($f_csc_name != "") : $whereClause .= "csc.csc_name LIKE '" . $prefix . $f_csc_name . "%'"; endif;
      // if ($whereClause != "" && $f_usr_name != "") : $whereClause .= " AND "; endif;
      // if ($f_usr_name != "") : $whereClause .= "usr.usr_name LIKE '" . $prefix . $f_usr_name . "%'"; endif;
      // if ($whereClause != "" && $f_usr_phone != "") : $whereClause .= " AND "; endif;
      // if ($f_usr_phone != "") : $whereClause .= "usr.usr_phone LIKE '" . $prefix . $f_usr_phone . "%'"; endif;
      // if ($whereClause != "" && $f_usr_email != "") : $whereClause .= " AND "; endif;
      // if ($f_usr_email != "") : $whereClause .= "usr.usr_email LIKE '" . $prefix . $f_usr_email . "%'"; endif;
      if ($whereClause != "" && $f_cmp_iln != "") : $whereClause .= " AND "; endif;
      if ($f_cmp_iln != "") : $whereClause .= "cmp.cmp_iln LIKE '" . $prefix . $f_cmp_iln . "%'"; endif;
      if ($whereClause != "" && $f_cmp_tax_idno != "") : $whereClause .= " AND "; endif;
      if ($f_cmp_tax_idno != "") : $whereClause .= "cmp.cmp_tax_idno LIKE '" . $prefix . $f_cmp_tax_idno . "%'"; endif;
      if ($whereClause != "" && $f_cmp_bank != "") : $whereClause .= " AND "; endif;
      if ($f_cmp_bank != "") : $whereClause .= "cmp.cmp_bank LIKE '" . $prefix . $f_cmp_bank . "%'"; endif;
      if ($whereClause != "" && $f_cmp_bankno != "") : $whereClause .= " AND "; endif;
      if ($f_cmp_bankno != "") : $whereClause .= "cmp.cmp_bankno LIKE '" . $prefix . $f_cmp_bankno . "%'"; endif;
      if ($whereClause != "" && $f_cmp_bankacc != "") : $whereClause .= " AND "; endif;
      if ($f_cmp_bankacc != "") : $whereClause .= "cmp.cmp_bankacc LIKE '" . $prefix . $f_cmp_bankacc . "%'"; 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 .= "tr.tr_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 .= "tr.tr_remark LIKE '" . $f_remark . "%'"; endif;
      if ($whereClause != "" && $f_person != "") : $whereClause .= " AND "; endif;
      if ($f_person != "") : $whereClause .= "tr.tr_person LIKE '" . $f_person . "%'"; endif;


      // Past days to search in...
      // if ($f_days > 0) :

         // Set the current timestamp to store the data
         $fromDateRange = date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d") - $f_days ,date("Y")));

         if ($whereClause != "") : $whereClause .= " AND "; endif;
         $whereClause .= "jb.jb_ordertime >= '$fromDateRange' ";
      // 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 "; endif;

      if ($orderClause == "") : $orderClause = "jb.jb_ordertime"; endif;


      $sqlquery = "SELECT jb.jb_id, jb.jb_ordertime,"
                   . "    tr.tr_comp, tr.tr_comp2, tr.tr_hsno, tr.tr_remark, tr.tr_person,"
                   . "    csc.csc_id, csc.csc_name, cs.cs_id, cs.cs_eid,"
                   . "    cmp.cmp_id, cmp.cmp_comp, cmp.cmp_comp2, cmp.cmp_authenticated, cmp.cmp_match,"
                   . "    ad.ad_street, ad.ad_zipcode, ad.ad_city, ad.ad_country"
                   . " FROM job AS jb, tour AS tr, address AS ad,"
                   . "      customer AS cs, costcenter AS csc, company AS cmp"
                   . " WHERE " . $whereClause
                   . "       (ISNULL(jb.jb_export_time) OR (jb.jb_export_time = '0000-00-00 00:00:00')) AND"
                   . "       jb.jb_id = tr.jb_id           AND"
                   . "       jb.hq_id = '$hq_id'           AND"
                   . "       tr.tr_sort = '1'              AND"
                   . "       tr.ad_id = ad.ad_id           AND"
                   . "       jb.csc_id_payer = csc.csc_id  AND"
                   . "       csc.cs_id = cs.cs_id          AND"
                   . "       cs.hq_id = '$hq_id'           AND"
                   . "       cmp.cmp_id = cs.cmp_id"
                   . " ORDER BY " . $orderClause;

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

      // Table with header
      while ($row = $result->fetch_assoc()):

            $numOfRows++;

            $tableOfRows .= "<tr>";

            $tableOfRows .= "<td>&nbsp;<a href=\"javascript:finishPage('" . ec($row["jb_id"]) . "');\">" . $row["jb_id"] . "</a></td>";

            $tableOfRows .= "<td>&nbsp;" . substr($row["tr_comp"], 0, 30) . "</td>";
            // $tableOfRows .= "<td>&nbsp;<a href=\"javascript:finishPage('" . $row["cs_id"] . "','" . $row["csc_id"] . "');\">" . substr($row["tr_comp"], 0, 15) . "</a></td>";

            $tableOfRows .= "<td>&nbsp;" . substr($row["tr_comp2"], 0, 15) . "</td>";
            $tableOfRows .= "<td>&nbsp;" . substr($row["cs_eid"], 0, 11) . "</td>";

            $tableOfRows .= "<td>&nbsp;" . substr($row["csc_name"], 0, 30) . "</td>";
            // $tableOfRows .= "<td>&nbsp;<a href=\"javascript:finishPage('" . $row["cs_id"] . "','" . $row["csc_id"] . "','" . $row["csc_name"] . "','" . $row["tr_comp"] . "','" . $row["tr_comp2"] . "','" . $row["ad_street"] . "','" . $row["tr_hsno"] . "','" . $row["ad_zipcode"] . "','" . $row["ad_city"] . "','" . $row["ad_country"] . "','" . $row["tr_remark"] . "','" . $row["tr_person"] . "','" . $tourno . "');\">" . substr($row["csc_name"], 0, 20) . "</a></td>";

            $tableOfRows .= "<td>&nbsp;" . substr($row["cmp_match"], 0, 30) . "</td>";

            // $tableOfRows .= "<td>&nbsp;" . substr($row["usr_name"], 0, 30) . "</td>";
            // $tableOfRows .= "<td>&nbsp;" . substr($row["usr_phone"], 0, 20) . "</td>";
            // $tableOfRows .= "<td>&nbsp;" . substr($row["usr_email"], 0, 30) . "</td>";

            $tableOfRows .= "<td>&nbsp;" . substr($row["ad_street"], 0, 20) . "</td>";
            $tableOfRows .= "<td>&nbsp;" . substr($row["tr_hsno"], 0, 5) . "</td>";
            $tableOfRows .= "<td>&nbsp;" . substr($row["ad_zipcode"], 0, 5) . "</td>";
            $tableOfRows .= "<td>&nbsp;" . substr($row["ad_city"], 0, 20) . "</td>";
            $tableOfRows .= "<td>&nbsp;" . substr($row["tr_remark"], 0, 20) . "</td>";
            $tableOfRows .= "<td>&nbsp;" . substr($row["tr_person"], 0, 20) . "</td>";

            $authImgName = "circle_red.jpg";
            if ($row["cmp_authenticated"] == "1") : $authImgName = "circle_green.jpg"; endif;
            $tableOfRows .= "<td align=\"center\"><img src=\"../images/" . $authImgName . "\" border=\"0\" height=\"10\" width=\"25\"></td>";

            $tableOfRows .= "</tr>";
      endwhile;
      $result->free();


      else :
        $statusMessage = "Bei Eingabe weniger als 3 Zeichen in mindestens einem Feld erfolgt keine Suche! Mind. ein Zeichen bei Kostenstelle reicht aus!";
      endif;
   endif;
?>

<html>

<head>
<title>votian: Suchliste Jobs</title>
<link rel="stylesheet" type="text/css" href="../css/phoenix.css.php">

<script src="../include/checkFormTags.js" type="text/javascript"></script>

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

function displayStatusMessage() {
 if (statusMessage != "") {
    alert(statusMessage);
 }
};

function finishPage(jb_id) {
  opener.parent.document.location.href = "../jobs/job_edit.php?jb_id=" + jb_id;
  self.setTimeout("self.close()", 100);
//  self.close();
};

function clearFields() {
  document.forms[0].f_jb_id.value = '';
  document.forms[0].f_cmp_comp.value = '';
  document.forms[0].f_cmp_comp2.value = '';
  document.forms[0].f_cmp_match.value = '';
  document.forms[0].f_cs_eid.value = '';
  document.forms[0].f_csc_name.value = '';
  document.forms[0].f_street.value = '';
  document.forms[0].f_hsno.value = '';
  document.forms[0].f_zipcode.value = '';
  document.forms[0].f_city.value = '';
  document.forms[0].f_remark.value = '';
  document.forms[0].f_person.value = '';
};
-->
</script>
</head>

<body onLoad="displayStatusMessage();">
  <form action="../jobs/jb_search_list.php" method="post">
  <input type="hidden" name="f_act" value="">
  <input type="hidden" name="orderClause" value="<?php echo $orderClause ?>">
  <input type="hidden" name="tourno" value="<?php echo $tourno ?>">
  <input type="hidden" name="generic" value="<?php echo $generic ?>">

  <table border="0">
    <tr>
      <td>
        <table border="0">
          <tr>
            <td class="f12bp1_red">
              Aufträge<br><br>
            </td>
          </tr>
          <tr>
            <td>
              <a href="javascript:document.forms[0].f_act.value='search';document.forms[0].submit();">&nbsp;Suchen&nbsp;</a>
              &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;
              <a href="javascript:clearFields();">Felder zurücksetzen</a>
              &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;
              Option:&nbsp;&nbsp;
              <input type="radio" name="f_searchmode" value="0" <?php if ($f_searchmode == "0") : echo "checked"; endif; ?>> Präfix
              <input type="radio" name="f_searchmode" value="1" <?php if ($f_searchmode == "1") : echo "checked"; endif; ?>> Teilwort
              &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;
              Suche in zurückliegenden Tagen (0 = heute): <input type="text" <?php echo $htmlClass01 ?> name="f_days" value="<?php echo $f_days ?>" size="3" onchange="javascript:checkNumRanges(document.forms[0].f_days,0,100,'Bitte geben Sie an, wieviele Tage in der Vergangenheit gesucht werden soll!');">
              <br><br>
              <table class="f8np1" border="1" cellpadding="0">
                <tr>
                  <td><input type="text" <?php echo $htmlClass01 ?> name="f_jb_id" value="<?php echo $f_jb_id ?>" size="5"></td>
                  <td><input type="text" <?php echo $htmlClass01 ?> name="f_cmp_comp" value="<?php echo $f_cmp_comp ?>" size="30"></td>
                  <td><input type="text" <?php echo $htmlClass01 ?> name="f_cmp_comp2" value="<?php echo $f_cmp_comp2 ?>" size="15"></td>
                  <td><input type="text" <?php echo $htmlClass01 ?> name="f_cs_eid" value="<?php echo $f_cs_eid ?>" size="11"></td>
                  <td><input type="text" <?php echo $htmlClass01 ?> name="f_csc_name" value="<?php echo $f_csc_name ?>" size="25"></td>
                  <td><input type="text" <?php echo $htmlClass01 ?> name="f_cmp_match" value="<?php echo $f_cmp_match ?>" size="30"></td>

            <!--  <td><input type="text" <?php echo $htmlClass01 ?> name="f_usr_name" value="<?php echo $f_usr_name ?>" size="15"></td> -->
            <!--  <td><input type="text" <?php echo $htmlClass01 ?> name="f_usr_phone" value="<?php echo $f_usr_phone ?>" size="20"></td> -->
            <!--  <td><input type="text" <?php echo $htmlClass01 ?> name="f_usr_email" value="<?php echo $f_usr_email ?>" size="30"></td> -->

                  <td><input type="text" <?php echo $htmlClass01 ?> name="f_street" value="<?php echo $f_street ?>" size="25"></td>
                  <td><input type="text" <?php echo $htmlClass01 ?> name="f_hsno" value="<?php echo $f_hsno ?>" size="5"></td>
                  <td><input type="text" <?php echo $htmlClass01 ?> name="f_zipcode" value="<?php echo $f_zipcode ?>" size="5"></td>
                  <td><input type="text" <?php echo $htmlClass01 ?> name="f_city" value="<?php echo $f_city ?>" size="20"></td>
                  <td><input type="text" <?php echo $htmlClass01 ?> name="f_remark" value="<?php echo $f_remark ?>" size="20"></td>
                  <td><input type="text" <?php echo $htmlClass01 ?> name="f_person" value="<?php echo $f_person ?>" size="20"></td>

                  <td><a href="javascript:document.forms[0].f_act.value='search';document.forms[0].submit();">&nbsp;Suchen&nbsp;</a></td>
                </tr>
                <tr>
                  <td><a href="javascript:document.forms[0].orderClause.value='jb.jb_id';document.forms[0].f_act.value='search';document.forms[0].submit();">&nbsp;Auftrag&nbsp;</a></td>
                  <td><a href="javascript:document.forms[0].orderClause.value='tr.tr_comp';document.forms[0].f_act.value='search';document.forms[0].submit();">&nbsp;Firma&nbsp;</a></td>
                  <td><a href="javascript:document.forms[0].orderClause.value='tr.tr_comp2';document.forms[0].f_act.value='search';document.forms[0].submit();">&nbsp;Firma2&nbsp;</a></td>
                  <td><a href="javascript:document.forms[0].orderClause.value='cs.cs_eid';document.forms[0].f_act.value='search';document.forms[0].submit();">&nbsp;ExtID&nbsp;</a></td>

            <!--  <td><a href="javascript:document.forms[0].orderClause.value='csc2.csc_name';document.forms[0].f_act.value='search';document.forms[0].submit();">&nbsp;Kostenstelle&nbsp;</a></td> -->
            <!--  <td><a href="javascript:document.forms[0].orderClause.value='usr.usr_name';document.forms[0].f_act.value='search';document.forms[0].submit();">&nbsp;Ansprechpartner&nbsp;</a></td> -->
            <!--  <td><a href="javascript:document.forms[0].orderClause.value='usr.usr_phone';document.forms[0].f_act.value='search';document.forms[0].submit();">&nbsp;Telefon&nbsp;</a></td> -->
            <!--  <td><a href="javascript:document.forms[0].orderClause.value='usr.usr_email';document.forms[0].f_act.value='search';document.forms[0].submit();">&nbsp;E-Mail&nbsp;</a></td> -->

                  <td><a href="javascript:document.forms[0].orderClause.value='csc.csc_name';document.forms[0].f_act.value='search';document.forms[0].submit();">&nbsp;Kostenstelle&nbsp;</a></td>
                  <td><a href="javascript:document.forms[0].orderClause.value='cmp.cmp_match';document.forms[0].f_act.value='search';document.forms[0].submit();">&nbsp;Freitext&nbsp;</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();">&nbsp;Straße (1. Etappe)</a></td>
                  <td><a href="javascript:document.forms[0].orderClause.value='tr.tr_hsno';document.forms[0].f_act.value='search';document.forms[0].submit();">&nbsp;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();">&nbsp;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();">&nbsp;Ort (1. Etappe)</a></td>
                  <td><a href="javascript:document.forms[0].orderClause.value='tr.tr_remark';document.forms[0].f_act.value='search';document.forms[0].submit();">&nbsp;Bemerkung</a></td>
                  <td><a href="javascript:document.forms[0].orderClause.value='tr.tr_person';document.forms[0].f_act.value='search';document.forms[0].submit();">&nbsp;Person</a></td>

                  <td>&nbsp;Freigabe&nbsp;</td>
                </tr>
                <?php echo $tableOfRows ?>
              </table>
              <br><br>
              Anzahl Einträge: <?php echo $numOfRows ?><?php if ($numOfRows == "0" && $f_act == "search" && $statusMessage == "") : echo " (Keine Einträge gefunden.)"; endif; ?>
            </td>
          </tr>
        </table>
      </td>
  </table>

</body>

</html>