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

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


function checkJobForSurvey ($jbId, $category = "15") {
  global $db, $PHP_SELF;

  $retBool = false;
  $debug = true;
  if ($jbId != "" && is_numeric($jbId)) :
     if ($debug) : echo "<h2>jbId : " . $jbId . "</h2><br>"; endif;

     $jbStatus = getFieldValueFromId("job", "jb_id", $jbId, "jb_status");
     if ($jbStatus == "1") :

        // Get object IDs fore categogy "survey"
        $sqlquery = "SELECT DISTINCT mtfv.mtfv_id"
                     . " FROM metafieldvalue AS mtfv, metafieldcategorykey AS mtfck, metafieldkey AS mtfk"
                     . " WHERE mtfv.mtfck_id = mtfck.mtfck_id      AND"
                     . "       mtfck.mtfc_id = '" . $category . "' AND"
                     . "       mtfck.mtfk_id = mtfk.mtfk_id";
      
        $result = $db->query($sqlquery);
        if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
      
        $objIdArray = array();
        while ($row = $result->fetch_assoc()):
              $objIdArray[] = $row["mtfv_id"];
        endwhile;
        $result->free();
      
        // Iterate survey trigger configurations
        // If at least one trigger does match then return "true"
        $objIdArrayLen = count($objIdArray);
        if ($objIdArrayLen > 0) :
   
           $objValues = array();
   
           for ($i = 0; $i < $objIdArrayLen; $i++) :
   
               if ($debug) : echo "<h3>[----" . $i . "----]</h3><br>"; endif;
               $currObjId = $objIdArray[$i];
   
               $sqlquery = "SELECT mtfv.mtfck_id, mtfv.mtfv_id, mtfv.mtfv_value, mtfk.mtfk_type, mtfk.mtfk_id"
                            . " FROM metafieldvalue AS mtfv, metafieldcategorykey AS mtfck, metafieldkey AS mtfk"
                            . " WHERE mtfv.mtfv_id = " . $currObjId . " AND"
                            . "       mtfck.mtfc_id = '" . $category . "' AND"
                            . "       mtfk.mtfk_id = mtfck.mtfk_id AND "
                            . "       mtfv.mtfck_id = mtfck.mtfck_id"
                            . " ORDER BY mtfv.mtfv_id, mtfck.mtfck_sort";
           
               $result = $db->query($sqlquery);
               if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
   
               while ($row = $result->fetch_assoc()):
                     if ($debug) : echo $row["mtfk_type"] . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; endif;
                     $objValues[$currObjId][$row["mtfk_type"]] = $row["mtfv_value"];
                     if ($debug) : echo $objValues[$currObjId][$row["mtfk_type"]] . "<br>"; endif;
               endwhile;
               $result->free();
   
               $doContinue = true;
               if ($doContinue) :
   
                  // [1.] Check date interval
                  if ($doContinue) :
                     if ($debug) : echo "<br>[1.] Check date interval<br>"; endif;
                     $dateBegin = $objValues[$currObjId]["date_begin"];
                     $dateEnd   = $objValues[$currObjId]["date_end"];
                     if ($debug) : echo "dateBeginPRE : " . $dateBegin . "<br>"; endif;
                     if ($debug) : echo "dateEndPRE : " . $dateEnd . "<br>"; endif;
                     if ($dateBegin != "" || $dateEnd != "") :
                        $dateBegin = substr($dateBegin,6,4) . "-" . substr($dateBegin,3,2) . "-" . substr($dateBegin,0,2);
                        $dateEnd = substr($dateEnd,6,4) . "-" . substr($dateEnd,3,2) . "-" . substr($dateEnd,0,2);
                        if ($debug) : echo "dateBegin : " . $dateBegin . "<br>"; endif;
                        if ($debug) : echo "dateEnd : " . $dateEnd . "<br>"; endif;
                        // $currentDay = getDateTime("3");   // "Y-m-d" (today)
                        $jobDay = getFieldValueFromId("job", "jb_id", $jbId, "jb_ordertime");  // "Y-m-d" (Ordertime of the job)
                        $jobDay = substr($jobDay,0,10);
                        if ($debug) : echo "jobDay : " . $jobDay . "<br>"; endif;
                        if (($dateBegin != "" && $jobDay < $dateBegin) || ($dateEnd != "" && $jobDay > $dateEnd)) :
                           $doContinue = false;  // Date does not match date interval
                           if ($debug) : echo "<br>DATE DOES NOT MATCH INTERVAL! => CHECK NEXT TRIGGER!<br>"; endif;
                        endif;
                     endif;
                  endif;
   
                  // [2.] Check HQ trigger being empy OR hq_id of the job being in current trigger headquarters list
                  if ($doContinue) :
                     if ($debug) : echo "<br>[2.] Check HQ trigger being empy OR hq_id of the job being in current trigger headquarters list<br>"; endif;
                     if ($objValues[$currObjId]["checkboxes_hq"] != "") :
                     
                        $jbHqId = getFieldValueFromId("job", "jb_id", $jbId, "hq_id");  // Get hq_id of the job
                        if ($debug) : echo "jbHqId : " . $jbHqId . "<br>"; endif;
                        $tmpHqIdArray = spliti(",", $objValues[$currObjId]["checkboxes_hq"]);
                        $j = array_search($jbHqId, $tmpHqIdArray);
                        if ($j === FALSE) :
                           $doContinue = false;  // Not found, trigger does not match
                           if ($debug) : echo "<br>NOT FOUND! =>  => CHECK NEXT TRIGGER!<br>"; endif;
                        endif;
                     endif;
                  endif;
                  
                  // [3.] Check single customer
                  if ($doContinue) :
                     if ($debug) : echo "<br>[3.] Check single customer<br>"; endif;
                     $csEid = $objValues[$currObjId]["button:cs:cs_eid"];
                     if ($csEid != "") :
                        if ($debug) : echo "csEid : " . $csEid . "<br>"; endif;
                        $jbCscIdPayer = getFieldValueFromId("job", "jb_id", $jbId, "csc_id_payer");
                        $jbCsIdPayer = getFieldValueFromId("costcenter", "csc_id", $jbCscIdPayer, "cs_id");
                        $jbCsEidPayer = getFieldValueFromId("customer", "cs_id", $jbCsIdPayer, "cs_eid");
                        if ($debug) : echo "jbCsEidPayer : " . $jbCsEidPayer . "<br>"; endif;
                        if ($jbCsEidPayer != $csEid) :
                           $doContinue = false;  // Not found, trigger does not match
                           if ($debug) : echo "<br>SINGLE CUSTOMER DOES NOT MATCH! => CHECK NEXT TRIGGER!<br>"; endif;
                        endif;
                     endif;
                  endif;
   
                  // [4.] Check single courier
                  if ($doContinue) :
                     if ($debug) : echo "<br>[4.] Check single courier<br>"; endif;
                     $crEid = $objValues[$currObjId]["button:cr:cr_eid"];
                     if ($crEid != "") :
                        if ($debug) : echo "crEid : " . $crEid . "<br>"; endif;
                        $jbCrId = getFieldValueFromId("job", "jb_id", $jbId, "cr_id");
                        $jbCrEid = getFieldValueFromId("courier", "cr_id", $jbCrId, "cr_eid");
                        if ($debug) : echo "jbCrEid : " . $jbCrEid . "<br>"; endif;
                        if ($jbCrEid != $crEid) :
                           $doContinue = false;  // Not found, trigger does not match
                           if ($debug) : echo "<br>SINGLE COURIER DOES NOT MATCH! => CHECK NEXT TRIGGER!<br>"; endif;
                        endif;
                     endif;
                  endif;
   
                  // [5.] Check single couriervehicle
                  if ($doContinue) :
                     if ($debug) : echo "<br>[5.] Check single couriervehicle<br>"; endif;
                     $crvhSid = $objValues[$currObjId]["select:crvh:crvh_sid"];
                     if ($crvhSid != "") :
                        if ($debug) : echo "crvhSid : " . $crvhSid . "<br>"; endif;
                        $jbCrSid = getFieldValueFromId("job", "jb_id", $jbId, "cr_sid");
                        if ($debug) : echo "jbCrSid : " . $jbCrSid . "<br>"; endif;
                        if ($jbCrSid != $crvhSid) :
                           $doContinue = false;  // Not found, trigger does not match
                           if ($debug) : echo "<br>SINGLE COURIERVEHICLE DOES NOT MATCH! => CHECK NEXT TRIGGER!<br>"; endif;
                        endif;
                     endif;
                  endif;
   
                  // [6.] Check couriervehicle class
                  if ($doContinue) :
                     if ($debug) : echo "<br>[6.] Check couriervehicle class<br>"; endif;
                     $vhtId = $objValues[$currObjId]["select:vht"];
                     if ($vhtId != "") :
                        if ($debug) : echo "vhtId : " . $vhtId . "<br>"; endif;
                        $jbVhtIdReal = getFieldValueFromId("job", "jb_id", $jbId, "vht_id_real");
                        if ($debug) : echo "jbVhtIdReal : " . $jbVhtIdReal . "<br>"; endif;
                        if ($jbVhtIdReal != $vhtId) :
                           $doContinue = false;  // Not found, trigger does not match
                           if ($debug) : echo "<br>VEHICLE CLASS DOES NOT MATCH! => CHECK NEXT TRIGGER!<br>"; endif;
                        endif;
                     endif;
                  endif;
   
                  // [7.] Check customer group
                  if ($doContinue) :
                     if ($debug) : echo "<br>[7.] Check customer group<br>"; endif;
                     $csGrpID = $objValues[$currObjId]["select:grp"];
                     if ($csGrpID != "") :
                        if ($debug) : echo "csGrpID : " . $csGrpID . "<br>"; endif;
                        // Get customer ID of the current job
                        $jbCscIdPayer = getFieldValueFromId("job", "jb_id", $jbId, "csc_id_payer");
                        $jbCsIdPayer = getFieldValueFromId("costcenter", "csc_id", $jbCscIdPayer, "cs_id");
                        // Get members of the (customer) group
                        $csGroupMemberIds = getGroupMemberIDs(array($csGrpID), "cs");
                        // Check customer of the job being member of the specified (customer) group
                        $j = array_search($jbCsIdPayer, $csGroupMemberIds);
                        if ($j === FALSE) :
                           $doContinue = false;  // Not found, trigger does not match
                           if ($debug) : echo "<br>CUSTOMER NOT IN SPECIFIED GROUP! => CHECK NEXT TRIGGER!!<br>"; endif;
                        endif;
                     endif;
                  endif;
   
                  // [8.] Check branch
                  if ($doContinue) :
                     $brId = $objValues[$currObjId]["select:br"];
                     if ($brId != "") :
                        if ($debug) : echo "brId : " . $brId . "<br>"; endif;
                        $jbCscIdPayer = getFieldValueFromId("job", "jb_id", $jbId, "csc_id_payer");
                        $jbCsIdPayer = getFieldValueFromId("costcenter", "csc_id", $jbCscIdPayer, "cs_id");
                        $jbCmpIdPayer = getFieldValueFromId("customer", "cs_id", $jbCsIdPayer, "cmp_id");
                        $jbBrIdPayer = getFieldValueFromId("company", "cmp_id", $jbCsIdPayer, "br_id");
                        if ($debug) : echo "$jbBrIdPayer : " . $jbBrIdPayer . "<br>"; endif;
                        if ($jbBrIdPayer != $brId) :
                           $doContinue = false;  // Not found, trigger does not match
                           if ($debug) : echo "<br>BRANCH DOES NOT MATCH! => CHECK NEXT TRIGGER!<br>"; endif;
                        endif;
                     endif;
                  endif;
   
                  // If $doContinue == "true" at this place then all checks are matched for the 
                  // current trigger (object) and therefore condition is green to display survey in PDA !!!!
                  if ($doContinue) :
                     if ($debug) : echo "<br>TRIGGER FOUND!!!!!!!!!!!!!!!!!!!!!!!!!! => BREAK!<br>"; endif;
                     // Insert into GDC
                     if (!$debug) :
                        insertStmt("genericdatacontainer", array("gdc_obj_type", "jb", "gdc_obj_id", $jbId, "gdc_gen_fieldname", "jb_pda_survey", "gdc_content", "1", "gdc_context", ""));
                     endif;
                     $retBool = true;
                     break;
                  endif;
                  if ($debug) : echo "--------------------------------------------------------------------------------<br><br>"; endif;
               endif;
           endfor;
        endif;
     endif;
  endif;

  return $retBool;
}

/*
mtfck_id  mtfv_id 	mtfv_value
1416 	    200510 	  01.02.2016    date                    Datum Beginn
1417 	    200510 	  28.02.2016    date                    Datum Ende
1418 	    200510 	  102,109,110   checkboxes_hq           Auswahl Niederlassung
1413 	    200510 	  HTHH41323     button:cs:cs_eid        Auswahl Kunde
1414 	    200510 	  HTHH81104     button:cr:cr_eid        Auswahl Transporteur
1415 	    200510 	  HH010         select:crvh:crvh_sid    Auswahl Fahrzeug
1419 	    200510 	  110           select:vht              Auswahl Fahrzeugklasse
1420 	    200510 	  78            select:grp              Auswahl Gruppe
1421 	    200510 	  69            select:br               Auswahl Branche

jb.jb_id,jb.csc_id,jb.vht_id,jb.vht_id_real,jb.csc_id_payer,jb.jb_payment,jb.jb_outlay,jb.jb_weight,jb.jb_ordertime,
jb.jb_reserv,jb.jb_permanent,jb.jb_permanent2,jb.jp_permenddat,jb.jb_specials,jb.cr_id,jb.cr_sid,jb.cr_id_order,
jb.cr_id_permanent,jb.jb_cr_filter,jb.jb_cr_filter_opt,jb.jb_waitstorno,jb.jb_waittime,jb.jb_taketime,jb.jb_tourname,
jb.jb_status,jb.jb_type,jb.jb_incomplete,jb.jb_storno,jb.jb_finishtime,jb.jb_warningtime,jb.jb_createtime,jb.emp_id,
jb.jb_fixprice,jb.jb_serviceprice,jb.jb_totalprice,jb.jb_cr_price,jb.jb_cr_subprice,jb.jb_id_parent,jb.jb_booktime,
jb.jb_export_time,jb.jb_postage,jb.jb_modify,jb.jb_longhaul,jb.jb_dispoinfo,jb.jb_offer,jb.jb_subtotalprice,
jb.jb_markup,jb.csc_id_related,jb.jb_timeunits,jb.jb_service"
*/

$jbId = "1906926";
$retBool = checkJobForSurvey($jbId);
if ($retBool) : 
   echo "Auftrag " . $jbId . " => Activated!<br><br>";
else :
   echo "Auftrag " . $jbId . " => Deactivated!<br><br>";
endif;


?>