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


include_once ("../include/mcglobal.inc.php");
include_once ("../include/inc_category_dbfields.inc.php");
include_once ("../include/inc_dbfields2array.inc.php");
include_once ("../include/inc_group.inc.php");
include_once ("../statistic/statistic_sql.inc.php");
include_once ("../include/email/htmlMimeMail.php");


$autoMode = true;
$hasAccess = false;
if (!isset($argv[1]) || $argv[1] == "") :
   include_once ("../include/auth.inc.php");
   $usrAccessArray["hq"] = "1";
   authCheckForAccess($hq_id, $usr_id, $emp_id, "1", $customerId, $cscIdRoot, $cscIdActual);
   authCheckEmployeeRights($emp_id, "8", "1");
   $autoMode = false;
   $hasAccess = true;
else :
   if ($argv[1] == "acapella7890") :
       $hasAccess = true;
   endif;
endif;


function statisticSetGlobalParameterArray () {
   global $aligns, $f_statusMode, $f_dateMode, $f_showDateAndTime, $f_crvh_sid, $f_direction_sort;
   global $f_filter, $f_filter2, $f_filter3, $f_filter4, $f_filter5, $f_filter6, $f_filter7, $f_filter8, $f_filter9, $f_filter10;
   global $f_price_formular, $f_filter_a, $f_filter_interval, $g_crvh_filter, $f_mode_statistic, $f_filter_calc, $f_service, $f_servicetype, $f_jb_service, $f_split_jb_services, $f_jb_specifics;
   global $f_cs_meta, $f_cr_meta, $f_cr_parent, $f_cr_pda_actions, $f_show_invoice_address, $f_show_delivery_address, $summationField, $summationField2, $uniqueSearchFields;
   global $hour_from, $minute_from, $hour_to, $minute_to, $fromTimeRange, $toTimeRange, $f_cmp_new_date, $f_cs_become_cs_date, $f_net_gross;
   global $f_group, $f_groupLen, $f_staticGroup, $f_staticGroupLen;

   $globalParArray = array($aligns, $f_statusMode, $f_dateMode, $f_showDateAndTime, $f_crvh_sid, $f_direction_sort, 
                           $f_filter, $f_filter2, $f_filter3, $f_filter4, $f_filter5, $f_filter6, $f_filter7, $f_filter8, $f_filter9, $f_filter10,
                           $f_price_formular, $f_filter_a, $f_filter_interval, $g_crvh_filter, $f_mode_statistic, $f_filter_calc, $f_service, $f_servicetype, $f_jb_service, $f_split_jb_services, $f_jb_specifics,
                           $f_cs_meta, $f_cr_meta, $f_cr_parent, $f_cr_pda_actions, $f_show_invoice_address, $f_show_delivery_address, $summationField, $summationField2, $uniqueSearchFields,
                           $hour_from, $minute_from, $hour_to, $minute_to, $fromTimeRange, $toTimeRange, $f_cmp_new_date, $f_cs_become_cs_date, $f_net_gross,
                           $f_group, $f_groupLen, $f_staticGroup, $f_staticGroupLen);
  return $globalParArray;
}


if ($hasAccess) :

   // Get current requested headquarters from call
   $currHqId = trim($argv[2]);
    
   // Init system parameter
   $path = "../temp/download/";
   $f_secretFileName = "NL_" . str_replace("-","",getDateTime("date_yesterday_currentmonth_currentyear")) . ".csv";

   // Init global parameter for script
   $aligns = "";
   $f_statusMode = "1";  // Finished jobs
   $f_dateMode = "1";  // "creating date", "booking date", "finish date" [default], "export date", "order_date"
   $f_showDateAndTime = "0";  // Clock time restriction (0 = none)
   $f_crvh_sid = "";  // Filter for special vehicle SID
   $f_direction_sort = "0";  // ORDER BY ASC or DESC"
   $f_filter = "0";  // Filter (Split for vehicletype, start zipcode, etc.)
   $f_filter2 = "0";  // Filter (Split for "Aufträge und Gutschriften") (0 = none)
   $f_filter3 = "0";  // Filter (Split for "Fahrzeugkategorie" ["jb_type"]) (0 = none)
   $f_filter4 = "0";  // Filter (Split for headquarters) (0 = none)
   $f_filter5 = "0";  // Filter (Split for branches) (0 = none)
   $f_filter6 = "0";  // Filter (Split for carriers [ONLY customer statistics]) (0 = none)
   $f_filter7 = "0";  // Filter (Split for vehicles) (0 = none)
   $f_filter8 = "0";  // Filter (Split for employees) (0 = none)
   $f_filter9 = "0";  // Filter (Split for job types ["jb_type"]) (0 = none)
   $f_filter10 = "0";  // Filter (Split for costcenter ["csc_id_payer" / "csc_id_related"]) (0 = none)
   $f_price_formular = "0";  // Computation of the price (0 = jb_totalprice)
   $f_filter_a = "0";  // Subfilter to $f_filter (Kind of vehicletype [Requested or real]) (0 = none)
   $f_filter_interval = "0";  // Filter (Split for time intervals) (0 = none)
   $g_crvh_filter = "";  // Vehicle filter  ("" = default)
   $f_mode_statistic = "0";  // Normal statistic (0 = default, 1 = calculator)
   $f_filter_calc = "0";  // (0 = none)
   $f_service = "0";  // Service for "service statistic" (0 = none)
   $f_servicetype = "0";  // Servicetype for "service statistic" (0 = none)
   $f_jb_service = array();  // Job service array as filter (empty array = default)
   $f_split_jb_services = "0";  // Filter (Split for job services) (0 = none)
   $f_jb_specifics = array();  // Job specifics used as filter (empty array = default)
   $f_cs_meta = "0";  // Filter (Split for sub customers ["meta-customer"]) (0 = none)
   $f_cr_meta = "0";  // Filter (Split for driver) (0 = none)
   $f_cr_parent = "";  // Every courier or driver OR cumulation by parent ("" = default)
   $f_cr_pda_actions = "0";  // Filter for courier action statistics (absolute/relative per job) (0 = none)
   $f_show_invoice_address = "0";
   $f_show_delivery_address = "0";
   $summationField = "stat.count_jobs";
   $summationField2 = "stat.business_volume";
   $uniqueSearchFields = array();
   $hour_from = "";
   $minute_from = "";
   $hour_to = "";
   $minute_to = "";
   $fromTimeRange = "";
   $toTimeRange = "";
   $f_cmp_new_date = "";  // Filter (Only new data set since date)
   $f_cs_become_cs_date = "";  // Filter (Only new customers since date)
   $f_net_gross = "0";
   $f_group = array();
   $f_groupLen = 0;
   $f_staticGroup = array();
   $f_staticGroupLen = 0;

   // Init "Local" parameters
   $hqName = "GESAMT";
   $hqMnemonic = "GESAMT";
   if ($currHqId != "" && is_numeric($currHqId)) : 
      $hqMnemonic = getFieldValueFromId("headquarters","hq_id",$currHqId,"hq_mnemonic");
      $hqName = getFieldValueFromId("headquarters","hq_id",$currHqId,"hq_name");
      if ($hqMnemonic == "" && $hqName == "") :
         $currHqId = "";
      endif;
   endif;
   if ($currHqId == "") :
      $f_hq_id = getColVectorFromDB2ArrayByClause("headquarters", "hq_id", "", "", "", "");
   else :
      $f_hq_id = array($currHqId);
   endif;

   $csEid = "";
   $crEid = "";
   $orderByClause = "";

   // Decimal for ROUND()
   $roundDec = 0;

   // Output matrix
   $matrix = array();  // array("","Monat Vorjahr","Vormonat","Aktueller Monat IST","Aktueller Monat SOLL")
   $output = "";
   
   // **** Current month of last year ****
   $globalParArray = statisticSetGlobalParameterArray();
   $f_category = "1";
   $f_statistic = "30";
   $idArrayLen = getCategoryDBFields($f_statistic);
   // Single day yesterday
   $fromDateRange = getDateTime("date_yesterday_currentmonth_lastyear");
   $toDateRange = getDateTime("date_yesterday_currentmonth_lastyear");
   $yesterdayCurrentMonthLastYear = getStatistic($globalParArray, $f_statistic, $fromDateRange . " 00:00:00", $toDateRange . " 23:59:59", $csEid, $crEid, $orderByClause);
   // Cumulated month
   if (date("d") == "01") :
     $fromDateRange = getDateTime("date_firstday_lastmonth_lastyear");
     $toDateRange = getDateTime("date_yesterday_lastmonth_lastyear");
   else :
     $fromDateRange = getDateTime("date_firstday_currentmonth_lastyear");
     $toDateRange = getDateTime("date_yesterday_currentmonth_lastyear");
   endif;
   $currentMonthLastYear = getStatistic($globalParArray, $f_statistic, $fromDateRange . " 00:00:00", $toDateRange . " 23:59:59", $csEid, $crEid, $orderByClause);
   // Output
   $matrix[0][1] = "Monat Vorjahr"; // ": [" . $fromDateRange . " bis " . $toDateRange . "]";
   $matrix[1][1] = number_format(round($yesterdayCurrentMonthLastYear[0][0]["business_volume"],$roundDec), $roundDec, ",", ".");
   $matrix[2][1] = number_format(round($currentMonthLastYear[0][0]["business_volume"],$roundDec), $roundDec, ",", ".");
   $matrix[0][5] = "Monat Vorjahr";
   $matrix[1][5] = number_format($yesterdayCurrentMonthLastYear[0][0]["count_jobs"], $roundDec, ",", ".");
   $matrix[2][5] = number_format($currentMonthLastYear[0][0]["count_jobs"], $roundDec, ",", ".");
   $matrix[0][9] = "Monat Vorjahr";
   $matrix[1][9] = number_format(round(($yesterdayCurrentMonthLastYear[0][0]["business_volume"] / $yesterdayCurrentMonthLastYear[0][0]["count_jobs"]),$roundDec), $roundDec, ",", ".");
   $matrix[2][9] = number_format(round(($currentMonthLastYear[0][0]["business_volume"] / $currentMonthLastYear[0][0]["count_jobs"]),$roundDec), $roundDec, ",", ".");
   
   
   // **** Last month of current year ****
   $globalParArray = statisticSetGlobalParameterArray();
   $f_category = "1";
   $f_statistic = "30";
   $idArrayLen = getCategoryDBFields($f_statistic);
   // Single day yesterday
   $fromDateRange = getDateTime("date_yesterday_lastmonth_currentyear");
   $toDateRange = getDateTime("date_yesterday_lastmonth_currentyear");
   $yesterdayLastMonthCurrentYear = getStatistic($globalParArray, $f_statistic, $fromDateRange . " 00:00:00", $toDateRange . " 23:59:59", $csEid, $crEid, $orderByClause);
   // Cumulated month
   if (date("d") == "01") :
     $fromDateRange = getDateTime("date_firstday_nexttolastmonth_currentyear");
     $toDateRange = getDateTime("date_yesterday_nexttolastmonth_currentyear");
   else :
     $fromDateRange = getDateTime("date_firstday_lastmonth_currentyear");
     $toDateRange = getDateTime("date_yesterday_lastmonth_currentyear");
   endif;
   $lastMonthCurrentYear = getStatistic($globalParArray, $f_statistic, $fromDateRange . " 00:00:00", $toDateRange . " 23:59:59", $csEid, $crEid, $orderByClause);
   // Output
   $matrix[0][2] = "Vormonat"; // ": [" . $fromDateRange . " bis " . $toDateRange . "]";
   $matrix[1][2] = number_format(round($yesterdayLastMonthCurrentYear[0][0]["business_volume"],$roundDec), $roundDec, ",", ".");
   $matrix[2][2] = number_format(round($lastMonthCurrentYear[0][0]["business_volume"],$roundDec), $roundDec, ",", ".");
   $matrix[0][6] = "Vormonat";
   $matrix[1][6] = number_format($yesterdayLastMonthCurrentYear[0][0]["count_jobs"], $roundDec, ",", ".");
   $matrix[2][6] = number_format($lastMonthCurrentYear[0][0]["count_jobs"], $roundDec, ",", ".");
   $matrix[0][10] = "Vormonat";
   $matrix[1][10] = number_format(round(($yesterdayLastMonthCurrentYear[0][0]["business_volume"] / $yesterdayLastMonthCurrentYear[0][0]["count_jobs"]),$roundDec), $roundDec, ",", ".");
   $matrix[2][10] = number_format(round(($lastMonthCurrentYear[0][0]["business_volume"] / $lastMonthCurrentYear[0][0]["count_jobs"]),$roundDec), $roundDec, ",", ".");
   
   
   // **** Current month of current year ****
   $globalParArray = statisticSetGlobalParameterArray();
   $f_category = "1";
   $f_statistic = "30";
   $idArrayLen = getCategoryDBFields($f_statistic);
   // Single day yesterday
   $fromDateRange = getDateTime("date_yesterday_currentmonth_currentyear");
   $toDateRange = getDateTime("date_yesterday_currentmonth_currentyear");
   $yesterdayCurrentMonthCurrentYear = getStatistic($globalParArray, $f_statistic, $fromDateRange . " 00:00:00", $toDateRange . " 23:59:59", $csEid, $crEid, $orderByClause);
   // Cumulated month
   if (date("d") == "01") :
     $fromDateRange = getDateTime("date_firstday_lastmonth_currentyear");
     $toDateRange = getDateTime("date_yesterday_lastmonth_currentyear");
   else :
     $fromDateRange = getDateTime("date_firstday_currentmonth_currentyear");
     $toDateRange = getDateTime("date_yesterday_currentmonth_currentyear");
   endif;
   $currentMonthCurrentYear = getStatistic($globalParArray, $f_statistic, $fromDateRange . " 00:00:00", $toDateRange . " 23:59:59", $csEid, $crEid, $orderByClause);
   // Output
   $matrix[0][3] = "Aktueller Monat IST"; // ": [" . $fromDateRange . " bis " . $toDateRange . "]";
   $matrix[1][3] = number_format(round($yesterdayCurrentMonthCurrentYear[0][0]["business_volume"],$roundDec), $roundDec, ",", ".");
   $matrix[2][3] = number_format(round($currentMonthCurrentYear[0][0]["business_volume"],$roundDec), $roundDec, ",", ".");
   $matrix[0][7] = "Aktueller Monat IST";
   $matrix[1][7] = number_format($yesterdayCurrentMonthCurrentYear[0][0]["count_jobs"], $roundDec, ",", ".");
   $matrix[2][7] = number_format($currentMonthCurrentYear[0][0]["count_jobs"], $roundDec, ",", ".");
   $matrix[0][11] = "Aktueller Monat IST";
   $matrix[1][11] = number_format(round(($yesterdayCurrentMonthCurrentYear[0][0]["business_volume"] / $yesterdayCurrentMonthCurrentYear[0][0]["count_jobs"]),$roundDec), $roundDec, ",", ".");
   $matrix[2][11] = number_format(round(($currentMonthCurrentYear[0][0]["business_volume"] / $currentMonthCurrentYear[0][0]["count_jobs"]),$roundDec), $roundDec, ",", ".");
   
   
   // **** Planned values ****
   $plannedYesterdayVolume = 0;
   $plannedYesterdayCount = 0;
   $plannedMonthVolume = 0;
   $plannedMonthCount = 0;
   // Output
   $matrix[0][4] = "Plan";
   $matrix[1][4] = $plannedYesterdayVolume;
   $matrix[2][4] = $plannedMonthVolume;
   $matrix[0][8] = "Plan";
   $matrix[1][8] = $plannedYesterdayCount;
   $matrix[2][8] = $plannedMonthCount;
   $matrix[0][12] = "Plan";
   $matrix[1][12] = number_format(round(($plannedYesterdayVolume / $plannedYesterdayCount),$roundDec), $roundDec, ",", ".");
   $matrix[2][12] = number_format(round(($plannedMonthVolume / $plannedMonthCount),$roundDec), $roundDec, ",", ".");
   
   
   // **** Computed values ****
   // Abweichung aktueller Tag [€]
   $yesterdayVolumeIstMinusVorjahr = ($yesterdayCurrentMonthCurrentYear[0][0]["business_volume"] - $yesterdayCurrentMonthLastYear[0][0]["business_volume"]);  // IST minus Vorjahr
   $yesterdayVolumeIstMinusVormonat = ($yesterdayCurrentMonthCurrentYear[0][0]["business_volume"] - $yesterdayLastMonthCurrentYear[0][0]["business_volume"]);  // IST minus Vormonat
   $yesterdayVolumeIstMinusPlan = ($yesterdayCurrentMonthCurrentYear[0][0]["business_volume"] - $plannedYesterdayVolume);  // IST minus Plan
   
   $yesterdayCountIstMinusVorjahr = ($yesterdayCurrentMonthCurrentYear[0][0]["count_jobs"] - $yesterdayCurrentMonthLastYear[0][0]["count_jobs"]);  // IST minus Vorjahr
   $yesterdayCountIstMinusVormonat = ($yesterdayCurrentMonthCurrentYear[0][0]["count_jobs"] - $yesterdayLastMonthCurrentYear[0][0]["count_jobs"]);  // IST minus Vormonat
   $yesterdayCountIstMinusPlan = ($yesterdayCurrentMonthCurrentYear[0][0]["count_jobs"] - $plannedYesterdayCount);  // IST minus Plan
   
   $yesterdayAverageIstMinusVorjahr = ($yesterdayCurrentMonthCurrentYear[0][0]["business_volume"] / $yesterdayCurrentMonthCurrentYear[0][0]["count_jobs"]) - ($yesterdayCurrentMonthLastYear[0][0]["business_volume"] / $yesterdayCurrentMonthLastYear[0][0]["count_jobs"]);  // IST minus Vorjahr
   $yesterdayAverageIstMinusVormonat = ($yesterdayCurrentMonthCurrentYear[0][0]["business_volume"] / $yesterdayCurrentMonthCurrentYear[0][0]["count_jobs"]) - ($yesterdayLastMonthCurrentYear[0][0]["business_volume"] / $yesterdayLastMonthCurrentYear[0][0]["count_jobs"]);  // IST minus Vormonat
   $yesterdayAverageIstMinusPlan = ($yesterdayCurrentMonthCurrentYear[0][0]["business_volume"] / $yesterdayCurrentMonthCurrentYear[0][0]["count_jobs"]) - ($plannedYesterdayVolume / $plannedYesterdayCount);  // IST minus Plan
   
   $matrix[3][1] = number_format(round($yesterdayVolumeIstMinusVorjahr,$roundDec), $roundDec, ",", ".");  // IST minus Vorjahr
   $matrix[3][2] = number_format(round($yesterdayVolumeIstMinusVormonat,$roundDec), $roundDec, ",", ".");  // IST minus Vormonat
   $matrix[3][3] = " ";
   $matrix[3][4] = number_format(round($yesterdayVolumeIstMinusPlan,$roundDec), $roundDec, ",", ".");  // IST minus Plan
   
   $matrix[3][5] = number_format(round($yesterdayCountIstMinusVorjahr,$roundDec), $roundDec, ",", ".");  // IST minus Vorjahr
   $matrix[3][6] = number_format(round($yesterdayCountIstMinusVormonat,$roundDec), $roundDec, ",", ".");  // IST minus Vormonat
   $matrix[3][7] = " ";
   $matrix[3][8] = number_format(round($yesterdayCountIstMinusPlan,$roundDec), $roundDec, ",", ".");  // IST minus Plan
   
   $matrix[3][9] = number_format(round($yesterdayAverageIstMinusVorjahr,$roundDec), $roundDec, ",", ".");  // IST minus Vorjahr
   $matrix[3][10] = number_format(round($yesterdayAverageIstMinusVormonat,$roundDec), $roundDec, ",", ".");  // IST minus Vormonat
   $matrix[3][11] = " ";
   $matrix[3][12] = number_format(round($yesterdayAverageIstMinusPlan,$roundDec), $roundDec, ",", ".");  // IST minus Plan
   
   // Abweichung Monat kumuliert [€]
   $currentMonthVolumeIstMinusVorjahr = ($currentMonthCurrentYear[0][0]["business_volume"] - $currentMonthLastYear[0][0]["business_volume"]);  // IST minus Vorjahr
   $currentMonthVolumeIstMinusVormonat = ($currentMonthCurrentYear[0][0]["business_volume"] - $lastMonthCurrentYear[0][0]["business_volume"]);  // IST minus Vormonat
   $currentMonthVolumeIstMinusPlan = ($currentMonthCurrentYear[0][0]["business_volume"] - $plannedYesterdayVolume);  // IST minus Plan
   
   $currentMonthCountIstMinusVorjahr = ($currentMonthCurrentYear[0][0]["count_jobs"] - $currentMonthLastYear[0][0]["count_jobs"]);  // IST minus Vorjahr
   $currentMonthCountIstMinusVormonat = ($currentMonthCurrentYear[0][0]["count_jobs"] - $lastMonthCurrentYear[0][0]["count_jobs"]);  // IST minus Vormonat
   $currentMonthCountIstMinusPlan = ($currentMonthCurrentYear[0][0]["count_jobs"] - $plannedYesterdayCount);  // IST minus Plan
   
   $currentMonthAverageIstMinusVorjahr = ($currentMonthCurrentYear[0][0]["business_volume"] / $currentMonthCurrentYear[0][0]["count_jobs"]) - ($currentMonthLastYear[0][0]["business_volume"] / $currentMonthLastYear[0][0]["count_jobs"]);  // IST minus Vorjahr
   $currentMonthAverageIstMinusVormonat = ($currentMonthCurrentYear[0][0]["business_volume"] / $currentMonthCurrentYear[0][0]["count_jobs"]) - ($lastMonthCurrentYear[0][0]["business_volume"] / $lastMonthCurrentYear[0][0]["count_jobs"]);  // IST minus Vormonat
   $currentMonthAverageIstMinusPlan = ($currentMonthCurrentYear[0][0]["business_volume"] / $currentMonthCurrentYear[0][0]["count_jobs"]) - ($plannedYesterdayVolume / $plannedYesterdayCount);  // IST minus Plan
   
   $matrix[4][1] = number_format(round($currentMonthVolumeIstMinusVorjahr,$roundDec), $roundDec, ",", ".");  // IST minus Vorjahr
   $matrix[4][2] = number_format(round($currentMonthVolumeIstMinusVormonat,$roundDec), $roundDec, ",", ".");  // IST minus Vormonat
   $matrix[4][3] = " ";
   $matrix[4][4] = number_format(round($currentMonthVolumeIstMinusPlan,$roundDec), $roundDec, ",", ".");  // IST minus Plan
   
   $matrix[4][5] = number_format(round($currentMonthCountIstMinusVorjahr,$roundDec), $roundDec, ",", ".");  // IST minus Vorjahr
   $matrix[4][6] = number_format(round($currentMonthCountIstMinusVormonat,$roundDec), $roundDec, ",", ".");  // IST minus Vormonat
   $matrix[4][7] = " ";
   $matrix[4][8] = number_format(round($currentMonthCountIstMinusPlan,$roundDec), $roundDec, ",", ".");  // IST minus Plan
   
   $matrix[4][9] = number_format(round($currentMonthAverageIstMinusVorjahr,$roundDec), $roundDec, ",", ".");  // IST minus Vorjahr
   $matrix[4][10] = number_format(round($currentMonthAverageIstMinusVormonat,$roundDec), $roundDec, ",", ".");  // IST minus Vormonat
   $matrix[4][11] = " ";
   $matrix[4][12] = number_format(round($currentMonthAverageIstMinusPlan,$roundDec), $roundDec, ",", ".");  // IST minus Plan
   
   // Abweichung aktueller Tag [%]
   $yesterdayPercentVolumeIstMinusVorjahr = (($yesterdayVolumeIstMinusVorjahr / $yesterdayCurrentMonthLastYear[0][0]["business_volume"]) * 100);  // (Abweichung Vorjahr in € durch Vorjahr in €) * 100
   $yesterdayPercentVolumeIstMinusVormonat = (($yesterdayVolumeIstMinusVormonat / $yesterdayLastMonthCurrentYear[0][0]["business_volume"]) * 100);  // (Abweichung Vormonat in € durch Vormonat in €) * 100
   $yesterdayPercentVolumeIstMinusPlan = (($yesterdayVolumeIstMinusPlan / $plannedYesterdayVolume) * 100);  // (Abweichung Plan in € durch Plan in €) * 100
   
   $yesterdayPercentCountIstMinusVorjahr = (($yesterdayCountIstMinusVorjahr / $yesterdayCurrentMonthLastYear[0][0]["count_jobs"]) * 100);  // (Abweichung Vorjahr durch Vorjahr) * 100
   $yesterdayPercentCountIstMinusVormonat = (($yesterdayCountIstMinusVormonat / $yesterdayLastMonthCurrentYear[0][0]["count_jobs"]) * 100);  // (Abweichung Vormonat durch Vormonat) * 100
   $yesterdayPercentCountIstMinusPlan = (($yesterdayCountIstMinusPlan / $plannedYesterdayCount) * 100);  // (Abweichung Plan durch Plan) * 100
   
   $yesterdayPercentAverageIstMinusVorjahr = (($yesterdayAverageIstMinusVorjahr / ($yesterdayCurrentMonthLastYear[0][0]["business_volume"] / $yesterdayCurrentMonthLastYear[0][0]["count_jobs"])) * 100);  // (Abweichung Vorjahr durch Vorjahr) * 100
   $yesterdayPercentAverageIstMinusVormonat = (($yesterdayAverageIstMinusVormonat / ($yesterdayLastMonthCurrentYear[0][0]["business_volume"] / $yesterdayLastMonthCurrentYear[0][0]["count_jobs"])) * 100);  // (Abweichung Vormonat durch Vormonat) * 100
   $yesterdayPercentAverageIstMinusPlan = (($yesterdayAverageIstMinusPlan / ($plannedYesterdayVolume / $plannedYesterdayCount)) * 100);  // (Abweichung Plan durch Plan) * 100
   
   $matrix[5][1] = number_format(round($yesterdayPercentVolumeIstMinusVorjahr,2), 2, ",", ".");
   $matrix[5][2] = number_format(round($yesterdayPercentVolumeIstMinusVormonat,2), 2, ",", ".");
   $matrix[5][3] = " ";
   $matrix[5][4] = number_format(round($yesterdayPercentVolumeIstMinusPlan,2), 2, ",", ".");
   
   $matrix[5][5] = number_format(round($yesterdayPercentCountIstMinusVorjahr,2), 2, ",", ".");
   $matrix[5][6] = number_format(round($yesterdayPercentCountIstMinusVormonat,2), 2, ",", ".");
   $matrix[5][7] = " ";
   $matrix[5][8] = number_format(round($yesterdayPercentCountIstMinusPlan,2), 2, ",", ".");
   
   $matrix[5][9] = number_format(round($yesterdayPercentAverageIstMinusVorjahr,2), 2, ",", ".");
   $matrix[5][10] = number_format(round($yesterdayPercentAverageIstMinusVormonat,2), 2, ",", ".");
   $matrix[5][11] = " ";
   $matrix[5][12] = number_format(round($yesterdayPercentAverageIstMinusPlan,2), 2, ",", ".");
   
   // Abweichung Monat kumuliert [%]
   $currentMonthPercentVolumeIstMinusVorjahr = (($currentMonthVolumeIstMinusVorjahr / $currentMonthLastYear[0][0]["business_volume"]) * 100);  // (Abweichung Vorjahr in € durch Vorjahr in €) * 100
   $currentMonthPercentVolumeIstMinusVormonat = (($currentMonthVolumeIstMinusVormonat / $lastMonthCurrentYear[0][0]["business_volume"]) * 100);  // (Abweichung Vormonat in € durch Vormonat in €) * 100
   $currentMonthPercentVolumeIstMinusPlan = (($currentMonthVolumeIstMinusPlan / $plannedMonthVolume) * 100);  // (Abweichung Plan in € durch Plan in €) * 100
   
   $currentMonthPercentCountIstMinusVorjahr = (($currentMonthCountIstMinusVorjahr / $currentMonthLastYear[0][0]["count_jobs"]) * 100);  // (Abweichung Vorjahr durch Vorjahr) * 100
   $currentMonthPercentCountIstMinusVormonat = (($currentMonthCountIstMinusVormonat / $lastMonthCurrentYear[0][0]["count_jobs"]) * 100);  // (Abweichung Vormonat durch Vormonat) * 100
   $currentMonthPercentCountIstMinusPlan = (($currentMonthCountIstMinusPlan / $plannedMonthCount) * 100);  // (Abweichung Plan durch Plan) * 100
   
   $currentMonthPercentAverageIstMinusVorjahr = (($currentMonthAverageIstMinusVorjahr / ($currentMonthLastYear[0][0]["business_volume"] / $currentMonthLastYear[0][0]["count_jobs"])) * 100);  // (Abweichung Vorjahr durch Vorjahr) * 100
   $currentMonthPercentAverageIstMinusVormonat = (($currentMonthAverageIstMinusVormonat / ($lastMonthCurrentYear[0][0]["business_volume"] / $lastMonthCurrentYear[0][0]["count_jobs"])) * 100);  // (Abweichung Vormonat durch Vormonat) * 100
   $currentMonthPercentAverageIstMinusPlan = (($currentMonthAverageIstMinusPlan / ($plannedMonthVolume / $plannedMonthCount)) * 100);  // (Abweichung Plan durch Plan) * 100
   
   $matrix[6][1] = number_format(round($currentMonthPercentVolumeIstMinusVorjahr,2), 2, ",", ".");
   $matrix[6][2] = number_format(round($currentMonthPercentVolumeIstMinusVormonat,2), 2, ",", ".");
   $matrix[6][3] = " ";
   $matrix[6][4] = number_format(round($currentMonthPercentVolumeIstMinusPlan,2), 2, ",", ".");  // IST minus Plan
   
   $matrix[6][5] = number_format(round($currentMonthPercentCountIstMinusVorjahr,2), 2, ",", ".");  // IST minus Vorjahr
   $matrix[6][6] = number_format(round($currentMonthPercentCountIstMinusVormonat,2), 2, ",", ".");  // IST minus Vormonat
   $matrix[6][7] = " ";
   $matrix[6][8] = number_format(round($currentMonthPercentCountIstMinusPlan,2), 2, ",", ".");  // IST minus Plan
   
   $matrix[6][9] = number_format(round($currentMonthPercentAverageIstMinusVorjahr,2), 2, ",", ".");  // IST minus Vorjahr
   $matrix[6][10] = number_format(round($currentMonthPercentAverageIstMinusVormonat,2), 2, ",", ".");  // IST minus Vormonat
   $matrix[6][11] = " ";
   $matrix[6][12] = number_format(round($currentMonthPercentAverageIstMinusPlan,2), 2, ",", ".");  // IST minus Plan


   // **** Names of rows ****
   $matrix[0][0] = " ";
   $matrix[1][0] = "Gestriger Tag";
   $matrix[2][0] = "Monat kumuliert";
   $matrix[3][0] = "Abweichung gestriger Tag [€]";
   $matrix[4][0] = "Abweichung Monat kumuliert [€]";
   $matrix[5][0] = "Abweichung gestriger Tag [%]";
   $matrix[6][0] = "Abweichung Monat kumuliert [%]";


   // Define potential hidden cols
   $hiddenCols = array("4","8","12");// OHNE Spalte "Plan"


   // Define HTML output
   $output .= "<table style=\"border:1px solid #7777FF; border-collapse:collapse; background-color:#CCCCFF;\" border=\"1\">";
   // $output .= "<tr><td> </td><td colspan=\"4\" align=\"center\"><b>" . "Umsatz " . $hqName . "</b></td><td colspan=\"4\" align=\"center\"><b>" . "Anzahl Aufträge" . "</b></td><td colspan=\"4\" align=\"center\"><b>" . "Durchschnittlicher Auftragswert" . "</b></td><tr>";
   $output .= "<tr><td class=\"td_border td_cell\"> </td><td colspan=\"3\" class=\"td_border td_cell\" align=\"center\"><b>" . "Umsatz " . $hqName . "</b></td><td colspan=\"3\" class=\"td_border td_cell\" align=\"center\"><b>" . "Anzahl Aufträge" . "</b></td><td colspan=\"3\" class=\"td_border td_cell\" align=\"center\"><b>" . "Durchschnittlicher Auftragswert" . "</b></td><tr>";
   for ($i = 0; $i < 7; $i++) :
       $output .= "<tr>";
       for ($j = 0; $j < 13; $j++) :
           $k = array_search($j, $hiddenCols);
           if ($k === FALSE) :
	            $output .= "<td class=\"td_border td_cell\" align=\"right\">" . $matrix[$i][$j] . "</td>";
	         endif;
       endfor;
       $output .= "</tr>";
   endfor;
   $output .= "</table>";


   // **** Generate CSV output (lines) and write file ****
   $lineArray = convertMatrixToCsv ($matrix, ";Umsatz " . $hqName . ";;;Anzahl Aufträge;;;Durchschnittlicher Auftragswert", ";", $hiddenCols);  // // OHNE Spalte "Plan"
   $lineArrayLen = count($lineArray);
   for ($i = 0; $i < $lineArrayLen; $i++) :
        writeToFile($path . $f_secretFileName, $lineArray[$i]);
   endfor;


   // **** Send CSV file per mail ****
   $mailObj = new htmlMimeMail();
   $mailCssFontType = "Calibri, Arial, Verdana, Helvetica, sans-serif";

   $mailtext  = "<html><head><meta text/html>";
   $mailtext .= "<style type=\"text/css\">";
   $mailtext .= ".f8np1 { font-family: " . $mailCssFontType . "; font-size: 8pt; font-weight: normal; padding: 1px}";
   $mailtext .= ".f8bp1 { font-family: " . $mailCssFontType . "; font-size: 8pt; font-weight: bold; padding: 1px}";
   $mailtext .= ".f10np1 { font-family: " . $mailCssFontType . "; font-size: 10pt; font-weight: normal; padding: 1px}";
   $mailtext .= ".f10bp1 { font-family: " . $mailCssFontType . "; font-size: 10pt; font-weight: bold; padding: 1px}";
   $mailtext .= ".f8np1_red { font-family: " . $mailCssFontType . "; font-size: 8pt; font-weight: normal; padding: 1px; color: #FF0000;}";

   $mailtext .= " table { border-width: 2px; border-style: solid; border-color: white; }";
   $mailtext .= " .td_border { border-width: 2px; border-style: solid; border-color: white; }";
   $mailtext .= " .td_cell { padding: 5px; }";

   $mailtext .= "</style>";
   $mailtext .= "</head><body><h4>";
   $mailtext .= $output;
   $mailtext .= "</h4></body></html>";

   $mailSubject = "UMSATZ- UND AUFTRAGSDATENÜBERSICHT " . $hqName . " für den " . getDateTime("date_yesterday_currentmonth_currentyear");
   $mailObj->setSubject($mailSubject);

   $mailObj->setHtml($mailtext, null, "./");

   if ($currHqId != "") :
      $mailSenderAddress = getParameterValue("0", "MAIL_SENDER_ADDRESS", $currHqId);
   endif;
   if ($mailSenderAddress == "" || !checkEmailFormat($mailSenderAddress)) :
      $mailSenderAddress = "hh.transport@hansetrans.de";
   endif;            
   $mailObj->setFrom($mailSenderAddress);

   $mailCcAddress = getParameterValue("0", "MAIL_CC_ADDRESS", "0");
   if ($mailCcAddress != "" && checkEmailFormat($mailCcAddress)) :
      $mailObj->setCc($mailCcAddress);
   endif;
   
   $mailBccAddress = getParameterValue("0", "MAIL_BCC_ADDRESS", "0");
   if ($mailBccAddress != "" && checkEmailFormat($mailBccAddress)) :
      $mailObj->setBcc($mailBccAddress);
   endif;            
   
   if (file_exists($path . $f_secretFileName)) :
      $attachment = $mailObj->getFile($path . $f_secretFileName);
      $mailObj->addAttachment($attachment, $f_secretFileName, 'text/csv');
   endif;

   $mailResult = $mailObj->send(array("mail-cron@assecutor.de","ca@hansetrans.de","sascha.engel@hansetrans.de"), 'smtp');
// $mailResult = $mailObj->send(array("mv@assecutor.de"), 'smtp');

   if ($mailResult) :
      // $mailsendStatus = getLngt("Die Nachricht wurde versandt!");
      // Write logdata into log database
      // writeToLogDB("22",$currentHqId,$job_id,$currentSessionUsrId,"","","",$f_email . "|STATION");
   else :
      // $mailsendStatus = getLngt("Die Nachricht konnte nicht gesendet werden!");
      // Write logdata into log database
      // writeToLogDB("23",$currentHqId,$job_id,$currentSessionUsrId,"","","",$f_email . "|STATION");
   endif;

   $mailObj = NULL;
   if (file_exists($path . $f_secretFileName)) :
      unlink($path . $f_secretFileName);
   endif;


   // OUTPUT
   if (!$autoMode) :
      echo $mailtext;
   endif;
endif;
?>
