<?php
/*=======================================================================
 *
 * dashb_cs.php
 *
 =======================================================================*/


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


// Check HTTP-Parameters
getSecHttpVars("1",array("f_act", "objType", "objId", "xxxx"));

// Id of the actual employee and the costcenter (individual root-node) logged in
if ($cscIdRoot == ""): $cscIdRoot = getFieldValueFromId("employee","emp_id",$emp_id,"csc_id"); endif;
if ($customerId == ""): $customerId = getFieldValueFromId("costcenter","csc_id",$cscIdRoot,"cs_id"); endif;
if ($cscIdActual == ""): $cscIdActual = $cscIdRoot; endif;


getLanguage(__FILE__);

$pageTitel = getLngt("DASHBOARD");
include_once ("../admin/menu.php");
include_once ("../include/html.inc.php");

getCurrentScript(__FILE__);


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

// Get2 the emp_id of the root admin, e.g. setting new passwords of employees
$empIdRootAdmin = getEmpIdOfRootAdmin("hq");


// Init
if ($objType == "") : $objType = "cs"; endif;
if ($objId == "") : $objId = ""; endif;
if ($objId != "" && is_numeric($objId)) :
   $objId = getFieldValueFromId("customer","cmp_id",$objId,"cs_id");
endif;

$currentTime = getDateTime("0");


// DIRECT SEARCH
// border-style:solid; border-width:1px; border-color:#0080FF;
// background-image: url('../images/loupe.png'); background-repeat:no-repeat; background-position: 2px 2px; background-size: auto; padding-left: 40px;
$ctSearchCss = "
 .ct_search {width:100%; padding:0px; margin:0px; border-spacing:0px; border-collapse:collapsed; background-color:#97bcFF;}
 .ct_search_hl {padding:0px; margin:0px; background-color:#4e45ec; color:white;}
 .ct_search_hl_td {padding:5px; margin:0px; font-size:10pt; font-family:Helvetica,Arial; font-style:normal; font-weight:normal;}
 .ct_search_bd {padding:0px; margin:0px; background-color:#97bcFF; color:white; vertical-align:top;}
 .ct_search_bd_td {height:1px; padding:5px; margin:0px;}
 input[type=text] {width:200px; height:25px; background-image: url('../images/loupe.png'); background-repeat:no-repeat; background-position: 4px 4px; background-size: 12px 12px; padding-left: 25px;}
 .buttonIcon {width:25px; height:25px; vertical-align:top;}
";
$ctSearchHtml = " 
 <table class=\"ct_search\" id=\"ct_search\">
   <tr class=\"ct_search_hl\" id=\"ct_search_hl\">
     <td class=\"ct_search_hl_td\" colspan=\"4\"><input type=\"text\" id=\"metaSearch\" name=\"metaSearch\" value=\"\">&nbsp;&nbsp;&nbsp;<button type=\"button\" class=\"buttonIcon\" onClick=\"metasearchStart('cs');\">&nbsp;</button></td>
   </tr>
   <tr class=\"ct_search_bd\" id=\"ct_search_bd\">
     <td class=\"ct_search_bd_td\" id=\"metaSearchResult_01\"></td>
     <td class=\"ct_search_bd_td\" id=\"metaSearchResult_02\"></td>
     <td class=\"ct_search_bd_td\" id=\"metaSearchResult_03\"></td>
     <td class=\"ct_search_bd_td\" id=\"metaSearchResult_04\"></td>
   </tr>
 </table>
";   


// GRID
$gridParArr = array();
// border-style:solid; border-width:1px; border-color:#0080FF;
$gridParArr["output_css"] = " 
 .grid_ct {float:left; padding:0px; margin:0px; min-width:250px; 
           font-size:10pt; font-family:Helvetica,Arial; font-style:normal; font-weight:normal;}
 .grid_ct_hl {min-height:22px; padding:5px; background-color:#4e45ec; color:white;}
 .grid_ct_bd {min-height:250px; padding:5px; background-color:#97bcFF; color:white;}
";
// $gridParArr = array();
$grid = new Grid("400px", array(2,5), $gridParArr);
$gridJsOut = "";
$grid->resetContainerPageCounter();

// Include ouput function
include_once("../include/getDashboardData.php");

/*
$passphrase = getPwd("34a78d91s83g");
echo "passphrase = " . $passphrase . "</br>";
// STB909307 bito :: Storno MIT Kosten => In Statistik mitgezählt?
// STB970299 STB  :: Hat (Test-)Berichte 2017
*/

// Wrapper for function "getDashData()"
function gdd ($tag, $headline, $structureType = "", $brArr = array(), $linkArr = array(), $styleArr = array(), $id = "", $type = "", $dateformat = "") {
	global $objType, $objId;
	$retVal = "";
	if ($tag != "") :
	   if ($type == "") : $type = $objType; endif;
	   if ($id == "") : $id = $objId; endif;
	   if ($structureType == "") : $structureType = "table"; endif;
     $retVal = getDashData($type, $tag, $id, $brArr, $styleArr, $dateformat, $structureType, $headline, $linkArr);
  endif;
  return $retVal;
}


if ($userTypeName == "hq") :

   // Get data
   $outCt = "";
   // $outCt = "<div style=\"display:inline-block; width:100%;\">";
   if ($objType != "" && $objId != "") :

      // "FIRMENDATEN"
      $outCt .= gdd("id", getLngt("DEBITOR"), "line", array(), array("cs", "cmp_id"));
      $outCt .= gdd("company", getLngt("FIRMIERUNG"), "table", array(0,1), array("cs", "cmp_id"));
      $outCt .= gdd("address", getLngt("ADRESSE"), "table", array(0), array("cs", "cmp_id"));
      $outCt .= gdd(array("usr_email", "usr_phone", "usr_phone2", "usr_fax"), array(getLngt("MAIL"),getLngt("FON"), getLngt("MOB"), getLngt("FAX")), "line", array(), array("cs", "cmp_id"));
      $outCt .= gdd("bank", getLngt("BANKVERBINDUNG"), "table", array(0,1,2,3,4), array("cs", "cmp_id"));
      $gridMatrix = $grid->getNextContainerPageCounter();
      // $gridJsOut .= $grid->gridSetCss($gridMatrix[0], $gridMatrix[1], array("width", "300px"), "");
      // $gridJsOut .= $grid->gridSetCss($gridMatrix[0], $gridMatrix[1], array("background-color", "#0080FF"), "hl");
      $gridJsOut .= $grid->gridSetHtml($gridMatrix[0], $gridMatrix[1], getLngt("FIRMENDATEN"), "hl");
      $gridJsOut .= $grid->gridSetCss($gridMatrix[0], $gridMatrix[1], array("color", "black", "font-size", "10pt", "font-weight", "normal"), "bd");
      $gridJsOut .= $grid->gridSetHtml($gridMatrix[0], $gridMatrix[1], strWrapJs($outCt), "bd");
/*
function gdd ($tag, $headline, $structureType = "", $brArr = array(), $styleArr = array(), $id = "", $type = "", $dateformat = "") {
....
$retVal = getDashData($type, $tag, $id, $brArr, $styleArr, $dateformat, $structureType, $headline);
*/
      // "VORGÄNGE"
      $outCt = "";
      $outCt .= gdd("reports_num_since_last_login:" . date("z") . ",0," . $emp_id, getLngt("ANZAHL BERICHTE SEIT LETZTEM LOGIN"), "line");
      $outCt .= gdd("reports_data_since_last_login:" . date("z") . ",0," . $emp_id, getLngt("BERICHTE SEIT LETZTEM LOGIN"), "table", array(1), array("rp", "rp_id"));
      $outCt .= gdd("reports_num_since_last_login:" . date("z") . ",0", getLngt("ANZAHL BERICHTE") . " " . getDateTime("year"), "line");
      $outCt .= gdd("reports_data_since_last_login:" . date("z") . ",0", getLngt("BERICHTE") . " " . getDateTime("year"), "table", array(1), array("rp", "rp_id"));
      $outCt .= gdd("appointments_num_since_last_login:" . date("z") . ",0," . $emp_id, getLngt("ANZAHL TERMINE SEIT LETZTEM LOGIN"), "line");
      $outCt .= gdd("appointments_data_since_last_login:" . date("z") . ",0," . $emp_id, getLngt("TERMINE SEIT LETZTEM LOGIN"), "table", array(0), array("ap", "ap_id"));
      $gridMatrix = $grid->getNextContainerPageCounter();
      // $gridJsOut .= $grid->gridSetCss($gridMatrix[0], $gridMatrix[1], array("width", "300px"), "");
      $gridJsOut .= $grid->gridSetCss($gridMatrix[0], $gridMatrix[1], array("background-color", "#4e45ec"), "hl");
      $gridJsOut .= $grid->gridSetHtml($gridMatrix[0], $gridMatrix[1], getLngt("VORGÄNGE"), "hl");
      $gridJsOut .= $grid->gridSetCss($gridMatrix[0], $gridMatrix[1], array("color", "black", "font-size", "10pt", "font-weight", "normal"), "bd");
      $gridJsOut .= $grid->gridSetHtml($gridMatrix[0], $gridMatrix[1], strWrapJs($outCt), "bd");

      // "STATISTIK"
      $outCt = "";
      $outCt .= gdd(array("stat_year_curr", "stat_year_last", "stat_year_1_before_last"), array(getDateTime("year"),getDateTime("lastyear"), date("Y",mktime(0, 0, 0, date("m"),date("d"),date("Y")-2))), "line", array());
      $outCt .= gdd("user_since", getLngt("KUNDE SEIT"), "line");
      $outCt .= gdd("last_order", getLngt("LETZTER AUFTRAG"), "line", array(), array("jb", "cs_last_job"));
      $gridMatrix = $grid->getNextContainerPageCounter();
      $gridJsOut .= $grid->gridSetCss($gridMatrix[0], $gridMatrix[1], array("background-color", "#4e45ec"), "hl");
      $gridJsOut .= $grid->gridSetHtml($gridMatrix[0], $gridMatrix[1], getLngt("STATISTIK"), "hl");
      $gridJsOut .= $grid->gridSetCss($gridMatrix[0], $gridMatrix[1], array("color", "black", "font-size", "10pt", "font-weight", "normal"), "bd");
      $gridJsOut .= $grid->gridSetHtml($gridMatrix[0], $gridMatrix[1], strWrapJs($outCt), "bd");
// gdd ($tag, $headline, $structureType = "", $brArr = array(), $styleArr = array(), $id = "", $type = "", $dateformat = "")

      // "FAHRER"
      $outCt = "";
      $outCt .= gdd("cr_fav", getLngt("BEVORZUGTE FAHRER"), "table", array(1,3,5,7), array("cr", "cmp_id"));
      $outCt .= gdd("cr_block", getLngt("GESPERRTE FAHRER"), "table", array(1,3,5,7), array("cr", "cmp_id"));
      $gridMatrix = $grid->getNextContainerPageCounter();
      $gridJsOut .= $grid->gridSetCss($gridMatrix[0], $gridMatrix[1], array("background-color", "#4e45ec"), "hl");
      $gridJsOut .= $grid->gridSetHtml($gridMatrix[0], $gridMatrix[1], getLngt("FAHRER"), "hl");
      $gridJsOut .= $grid->gridSetCss($gridMatrix[0], $gridMatrix[1], array("color", "black", "font-size", "10pt", "font-weight", "normal"), "bd");
      $gridJsOut .= $grid->gridSetHtml($gridMatrix[0], $gridMatrix[1], strWrapJs($outCt), "bd");

      // "KOSTENSTELLENADRESSEN"
      /*
      $outCt = "";
      $outCt .= gdd("costcenteraddresses", getLngt("KOSTENSTELLEN"), "table", array(1,2,4), array("csc", "csc_id"));
      // $outCt .= getDashData($objType, "usr.usr_account", $objId);
      $gridMatrix = $grid->getNextContainerPageCounter();
      $gridJsOut .= $grid->gridSetCss($gridMatrix[0], $gridMatrix[1], array("background-color", "#4e45ec"), "hl");
      $gridJsOut .= $grid->gridSetHtml($gridMatrix[0], $gridMatrix[1], getLngt("KOSTENSTELLEN"), "hl");
      $gridJsOut .= $grid->gridSetCss($gridMatrix[0], $gridMatrix[1], array("color", "black", "font-size", "10pt", "font-weight", "normal"), "bd");
      $gridJsOut .= $grid->gridSetHtml($gridMatrix[0], $gridMatrix[1], strWrapJs($outCt), "bd");
      */

      // "KOSTENSTELLEN"
      $outCt = "";
      $outCt .= gdd("costcenters", getLngt("KOSTENSTELLEN"), "table", array(0), array("csc", "csc_id"));
      $gridMatrix = $grid->getNextContainerPageCounter();
      $gridJsOut .= $grid->gridSetCss($gridMatrix[0], $gridMatrix[1], array("background-color", "#4e45ec"), "hl");
      $gridJsOut .= $grid->gridSetHtml($gridMatrix[0], $gridMatrix[1], getLngt("KOSTENSTELLEN"), "hl");
      $gridJsOut .= $grid->gridSetCss($gridMatrix[0], $gridMatrix[1], array("color", "black", "font-size", "10pt", "font-weight", "normal"), "bd");
      $gridJsOut .= $grid->gridSetHtml($gridMatrix[0], $gridMatrix[1], strWrapJs($outCt), "bd");

      // "CHART: ANZAHL AUFTRÄGE"
      $outCt =  base64_encode(gdd("stat_chart_jobs:2", "", "table", array(), array()));
      $gridMatrix = $grid->getNextContainerPageCounter();
      $gridJsOut .= $grid->gridSetCss($gridMatrix[0], $gridMatrix[1], array("background-color", "#4e45ec"), "hl");
      $gridJsOut .= $grid->gridSetHtml($gridMatrix[0], $gridMatrix[1], getLngt("ANZAHL AUFTRÄGE"), "hl");
      $gridJsOut .= $grid->gridSetCss($gridMatrix[0], $gridMatrix[1], array("color", "black", "font-size", "10pt", "font-weight", "normal"), "bd");
      $gridJsOut .= $grid->gridSetContent($gridMatrix[0], $gridMatrix[1], "atob(\"" . $outCt . "\")", "bd", "html", "");  // Usage of "gridSetContent" instead of "gridSetHtml" (!!!!)

      // "CHART: UMSÄTZE"
      $outCt =  base64_encode(gdd("stat_chart_sales:2", "", "table", array(), array()));
      $gridMatrix = $grid->getNextContainerPageCounter();
      $gridJsOut .= $grid->gridSetCss($gridMatrix[0], $gridMatrix[1], array("background-color", "#4e45ec"), "hl");
      $gridJsOut .= $grid->gridSetHtml($gridMatrix[0], $gridMatrix[1], getLngt("UMSÄTZE"), "hl");
      $gridJsOut .= $grid->gridSetCss($gridMatrix[0], $gridMatrix[1], array("color", "black", "font-size", "10pt", "font-weight", "normal"), "bd");
      $gridJsOut .= $grid->gridSetContent($gridMatrix[0], $gridMatrix[1], "atob(\"" . $outCt . "\")", "bd", "html", "");  // Usage of "gridSetContent" instead of "gridSetHtml" (!!!!)

   endif;
   // $outCt = "</div>";

   
// Current container page counter
/*
$tmpArr = $grid->getCurrentContainerPageCounter("0", "1");
echo "getCurrentContainerPageCounter.grid: " . $tmpArr[0] . " " . $tmpArr[1] . "<br>";
$tmpVal = $grid->getCurrentContainerPageCounter("1", "1");
echo "getCurrentContainerPageCounter.counter: " . $tmpVal . "<br><br>";
// $grid->addGridContainer($tmpArr[0], $tmpArr[1]);
*/

endif;


// Hide remaining containers without content
$gridJsOut .= $grid->gridHideRemaining();
?>


<html>
<head>
<title><?php echo $pageTitel ?></title>
<script>
/*
window.onerror=fnErrorTrap;
function fnErrorTrap(sMsg,sUrl,sLine){
    var tmp_jb_id = "";
    if(typeof(jb_id)!='undefined')    
      tmp_jb_id = jb_id;
    var out = '';
    iframe = document.createElement("IFRAME");
    iframe.setAttribute("src", "../include/ajaxJsError.php?url="+escape(sUrl) + "&line=" + escape(sLine) + "&message=" + escape(sMsg) + "&user_agent=" + escape(navigator.userAgent) + "&jb_id=" + tmp_jb_id + "&out=" + escape(out));
    iframe.width = 1;
    iframe.height = 1;
    iframe.style.display = 'none';
    setTimeout(function(){
      document.body.appendChild(iframe);
    },10);
  return false;
}
*/
</script>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="description" content="votian"> <meta name="keywords" content="votian">
<link rel="stylesheet" type="text/css" href="../css/phoenix.css">

<style type="text/css">
  <?php include_once ("../css/navigation.css.php"); ?>
</style>

<style type="text/css">
  <?php echo $ctSearchCss ?>
  <?php echo $grid->getGridItemParameter("output_css") ?>
</style>

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

<link rel="stylesheet" type="text/css" href="../css/Chart.min.css">
<script src="../js/moment.js"></script>
<script src="../js/Chart.js"></script>

<style>
    canvas{
        -moz-user-select: none;
        -webkit-user-select: none;
        -ms-user-select: none;
    }

    .chartjs-tooltip {
        opacity: 1;
        position: absolute;
        background: rgba(0, 0, 0, .7);
        color: white;
        border-radius: 3px;
        -webkit-transition: all .1s ease;
        transition: all .1s ease;
        pointer-events: none;
        -webkit-transform: translate(-50%, 0);
        transform: translate(-50%, 0);
        padding: 4px;
    }

    .chartjs-tooltip-key {
        display: inline-block;
        width: 10px;
        height: 10px;
    }
</style>

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

<script language="JavaScript">
<!--
function metasearchStart (objType = '') {
	metasearch(objType);
  <?php 
    if ($userTypeName == "hq") :
       // echo "$('#metaSearch').focus();";
    endif;
  ?>
}

function storeCsGlobalInfo (elem) {
  var content = $('#' + elem).val();
  ajaxRequestGet('../include/ajaxReqLib.php', 'mode=500&wrap_html=1&value_01=hq2cs&value_02=<?php echo ec($hq_id) ?>&value_03=' + elem + '&value_04=' + encodeURI(escape(content)) + '&value_05=');
}

function openPopup (url,title,config) {
  var widthPopupWin = screen.width - 80;
  var heightPopupWin = screen.height - 180;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2);
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2);
  var popup;
  popup = window.open(url,title, "dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
}

function openLinkByObject (objType, objValue) {
	// alert(objType + ' ' + objValue);
  // redirectLocation('../admin/dashb.php?objType=' + objType + '&objId=' + objValue);
  $('#objType').val(objType);
  $('#objId').val(objValue);
  document.forms[0].submit();
}

function openLinkByContentObject (objType, objValue) {
	// alert(objType + ' ' + objValue);
	if (objType == 'jb') {
	   openPopup('../admin/jb_detail.php?job_id=' + objValue,'','');
	} 
	if (objType == 'cs') {
		 openPopup('../admin/customer_special.php?companyId=' + objValue,'','');
	}
	if (objType == 'csc') {
		 if (objValue != '') {
		    // Get cs_id from csc_id (objValue)
        ajaxRequestGet('../include/ajaxReqLib.php', 'mode=0&db_table=costcenter&db_id_field=csc_id&search_value=' + objValue + '&db_return_field=cs_id');
		 	  if (retValue != '') {
		 	  	 csId = retValue;  // cs_id
           // Open costcenter page
           if (csId != '') {
		          openPopup('../customer/costcenter.php?customerId=' + csId + '&cscIdActual=' + objValue,'','');
		       }
		    }
		 }
	}
	if (objType == 'cr') {
		 openPopup('../admin/courier_special.php?companyId=' + objValue,'','');
	}
	if (objType == 'crvh') {
		 openPopup('../admin/courier_vehicle.php?courierId=' + objValue,'','');
	}
	if (objType == 'rp') {
		 if (objValue != '') {
		    // Get rp_createtime from rp_id (objValue)
        ajaxRequestGet('../include/ajaxReqLib.php', 'mode=0&db_table=phoenix_group.report_process&db_id_field=rp_id&search_value=' + objValue + '&db_return_field=rp_createtime');
		 	  if (retValue != '') {
		 	  	 rpCreatetime = retValue;  // rp_createtime
		       // Get rp_objid (customer) from rp_id (objValue)
           ajaxRequestGet('../include/ajaxReqLib.php', 'mode=0&db_table=phoenix_group.report_process&db_id_field=rp_id&search_value=' + objValue + '&db_return_field=rp_objid');
           if (retValue != '') {
           	  rpObjid = retValue;  // cs_id
           	  // Get cs_eid
           	  ajaxRequestGet('../include/ajaxReqLib.php', 'mode=0&db_table=customer&db_id_field=cs_id&search_value=' + rpObjid + '&db_return_field=cs_eid');
           	  if (retValue != '') {
           	  	 csEid = retValue;  // cs_eid
           	  }
           }
           // Open report page
           if (rpCreatetime != '' && rpObjid != "" && csEid != '') {
		          openPopup('../groupware/report.php?rpObjType=cs&g_cs_eid=' + csEid + '&day_from=' + rpCreatetime.substring(8,10) + '&month_from=' + rpCreatetime.substring(5,7) + '&year_from=' + rpCreatetime.substring(0,4) + '&day_to=' + rpCreatetime.substring(8,10) + '&month_to=' + rpCreatetime.substring(5,7) + '&year_to=' + rpCreatetime.substring(0,4),'','');
		       }
		    }
		 }
	}
}

function execBodyOnLoad() {
  <?php echo $gridJsOut ?>
  // checkStartTickerOut();
  <?php 
    if ($userTypeName == "hq") :
       echo "$('#maincontent').css('margin-left','0px');";
       echo "$('#metaSearch').focus();";
    endif;
  ?>
}

$(document).keypress(function(e) {
  if (e.keyCode === 13 && e.target.nodeName == 'INPUT') {
     e.preventDefault(); //prevent default if it is INPUT
     var focused = $(':focus');
     if ($(focused).attr('id') == "metaSearch") {
     	  metasearchStart('<?php echo $objType ?>');
     };
  }
});

// -->
</script>

<noscript>
  <center>
    <b><br>JavaScript ist nicht verf&uuml;gbar. Bitte aktivieren Sie JavaScript<br><br>

        in Ihrem Browser, damit diese Seite ordnungsgem&auml;&szlig; funktioniert!</b><br><br>

  </center>
</noscript>
</head>

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

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

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

  <form id="dashboard" name="dashboard" action="../admin/dashb_cs.php" method="post">

  <input type="hidden" name="f_act" value="">
  <input type="hidden" id="objType" name="objType" value="<?php echo ec($objType) ?>">
  <input type="hidden" id="objId" name="objId" value="<?php echo ec($objId) ?>">

  <div>  	
  	<?php echo $ctSearchHtml ?>
  	<?php echo $grid->getGridItemParameter("output_html") ?>
  </div>

  </form>

</div>

</body>

</html>

