<?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, $f_priceMode;

   $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, $f_priceMode);
  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 = "2";  // "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;
   $f_priceMode = "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();  // HEADLINE
   $summatedValues = array();
   $output = "";
   
   // **** HEADLINE ****
   $rowCounter = 0;
   // "Umsatz"
   $matrix[0][0] = "";
   $matrix[0][1] = "Vorjahr";
   $matrix[0][2] = "Vormonat";
   $matrix[0][3] = "IST";
   $matrix[0][4] = "Nicht&nbsp;exp.";
   $matrix[0][5] = "BSG";
   // "Umsatz kumuliert"
   $matrix[0][10] = "Vorjahr";
   $matrix[0][11] = "Vormonat";
   $matrix[0][12] = "IST";
   $matrix[0][13] = "Nicht&nbsp;exp.";
   $matrix[0][14] = "BSG";
   // "Anzahl Aufträge"
   $matrix[0][20] = "Vorjahr";
   $matrix[0][21] = "Vormonat";
   $matrix[0][22] = "IST";
   $matrix[0][23] = "Nicht&nbsp;exp.";
   // "Durchschnittlicher Auftragswert"
   $matrix[0][30] = "Vorjahr";
   $matrix[0][31] = "Vormonat";
   $matrix[0][32] = "IST";


   // **** Days of the month of yesterday ****
   $yesterday = getDateTime("date_yesterday_currentmonth_currentyear");
   $dayOfYesterday = substr($yesterday,8,2);
   $monthOfYesterday = substr($yesterday,5,2);
   $yearOfYesterday = substr($yesterday,0,4);

   $globalParArray = statisticSetGlobalParameterArray();
   $f_category = "1";
   $f_statistic = "32";
   $idArrayLen = getCategoryDBFields($f_statistic);


   // **** STATISTIC for count and volume ****
   // Yesterday current month current year
   $fromDateRange = $yearOfYesterday . "-" . $monthOfYesterday . "-01 00:00:00";
   $toDateRange = $yearOfYesterday . "-" . $monthOfYesterday . "-" .  $dayOfYesterday . " 23:59:59";
   $yesterdayCurrentMonthCurrentYear = getStatistic($globalParArray, $f_statistic, $fromDateRange, $toDateRange, $csEid, $crEid, $orderByClause);
   
   // Yesterday last month current year
   $lastMonth = intval($monthOfYesterday) - 1;
   if ($lastMonth == 0) :
      $lastMonth = 12;
      $lastYear = intval($yearOfYesterday) - 1;
   endif;
   $fromDateRange = $yearOfYesterday . "-" . pad($lastMonth,2) . "-01 00:00:00";
   $toDateRange = $yearOfYesterday . "-" . pad($lastMonth,2) . "-" .  $dayOfYesterday . " 23:59:59";
   $yesterdayLastMonthCurrentYear = getStatistic($globalParArray, $f_statistic, $fromDateRange, $toDateRange, $csEid, $crEid, $orderByClause);

   // Yesterday current month last year
   $lastYear = intval($yearOfYesterday) - 1;
   $fromDateRange = $lastYear . "-" . $monthOfYesterday . "-01 00:00:00";
   $toDateRange = $lastYear . "-" . $monthOfYesterday . "-" .  $dayOfYesterday . " 23:59:59";
   $yesterdayCurrentMonthLastYear = getStatistic($globalParArray, $f_statistic, $fromDateRange, $toDateRange, $csEid, $crEid, $orderByClause);


   // **** STATISTIC for carrier volume ****
   $f_priceMode = "1";
   $globalParArray = statisticSetGlobalParameterArray();

   // Yesterday current month current year
   $fromDateRange = $yearOfYesterday . "-" . $monthOfYesterday . "-01 00:00:00";
   $toDateRange = $yearOfYesterday . "-" . $monthOfYesterday . "-" .  $dayOfYesterday . " 23:59:59";
   $CARRIERVOLUMEYesterdayCurrentMonthCurrentYear = getStatistic($globalParArray, $f_statistic, $fromDateRange, $toDateRange, $csEid, $crEid, $orderByClause);


   // **** STATISTIC for BSG ****
   $f_priceMode = "2";
   $globalParArray = statisticSetGlobalParameterArray();

   // Yesterday current month current year
   $fromDateRange = $yearOfYesterday . "-" . $monthOfYesterday . "-01 00:00:00";
   $toDateRange = $yearOfYesterday . "-" . $monthOfYesterday . "-" .  $dayOfYesterday . " 23:59:59";
   $BSGYesterdayCurrentMonthCurrentYear = getStatistic($globalParArray, $f_statistic, $fromDateRange, $toDateRange, $csEid, $crEid, $orderByClause);


   /*
   // Yesterday last month current year
   $lastMonth = intval($monthOfYesterday) - 1;
   if ($lastMonth == 0) :
      $lastMonth = 12;
      $lastYear = intval($yearOfYesterday) - 1;
   endif;
   $fromDateRange = $yearOfYesterday . "-" . pad($lastMonth,2) . "-01 00:00:00";
   $toDateRange = $yearOfYesterday . "-" . pad($lastMonth,2) . "-" .  $dayOfYesterday . " 23:59:59";
   $BSGYesterdayLastMonthCurrentYear = getStatistic($globalParArray, $f_statistic, $fromDateRange, $toDateRange, $csEid, $crEid, $orderByClause);

   // Yesterday current month last year
   $lastYear = intval($yearOfYesterday) - 1;
   $fromDateRange = $lastYear . "-" . $monthOfYesterday . "-01 00:00:00";
   $toDateRange = $lastYear . "-" . $monthOfYesterday . "-" .  $dayOfYesterday . " 23:59:59";
   $BSGYesterdayCurrentMonthLastYear = getStatistic($globalParArray, $f_statistic, $fromDateRange, $toDateRange, $csEid, $crEid, $orderByClause);
   */


   // **** STATISTIC for finished but NOT exported jobs ****
   // RESULT = $yesterdayCurrentMonthCurrentYear MINUS this result (!!!!)
   $f_priceMode = "0";
   $f_statusMode = "2";  // Finished AND exported jobs
   $globalParArray = statisticSetGlobalParameterArray();

   // Yesterday current month current year
   $fromDateRange = $yearOfYesterday . "-" . $monthOfYesterday . "-01 00:00:00";
   $toDateRange = $yearOfYesterday . "-" . $monthOfYesterday . "-" .  $dayOfYesterday . " 23:59:59";
   $exportedJobsYesterdayCurrentMonthCurrentYear = getStatistic($globalParArray, $f_statistic, $fromDateRange, $toDateRange, $csEid, $crEid, $orderByClause);


   // Reset price mode
   $f_priceMode = "0";
   $f_statusMode = "1";  // Finished jobs
   $globalParArray = statisticSetGlobalParameterArray();


   $arrPos1 = 0;
   $arrPos2 = 0;
   $arrPos3 = 0;
   $arrPos4 = 0;
   $arrPos5 = 0;
   $iterDay = intval($dayOfYesterday);
   for ($i = 0; $i < $iterDay; $i++) :
       $rowCounter++;

       // Yesterday current month current year
       $VOLUME_yesterdayCurrentMonthCurrentYear = 0;
       $COUNT_yesterdayCurrentMonthCurrentYear = 0;
       $NEXP_VOLUME_yesterdayCurrentMonthCurrentYear = 0;
       $NEXP_COUNT_yesterdayCurrentMonthCurrentYear = 0;
       if (intval(substr($yesterdayCurrentMonthCurrentYear[0][$arrPos3]["jb_finishtime"],-2)) == $rowCounter) :
          $VOLUME_yesterdayCurrentMonthCurrentYear = $yesterdayCurrentMonthCurrentYear[0][$arrPos3]["business_volume"];
          $COUNT_yesterdayCurrentMonthCurrentYear = $yesterdayCurrentMonthCurrentYear[0][$arrPos3]["count_jobs"];
          if ($summatedValues[12] == "") : $summatedValues[12] = 0; endif;
          $summatedValues[12] += $VOLUME_yesterdayCurrentMonthCurrentYear;
          $summatedValues[22] += $COUNT_yesterdayCurrentMonthCurrentYear;

          $tmpArrLen = count($exportedJobsYesterdayCurrentMonthCurrentYear[0]);  // # rows in $exportedJobsYesterdayCurrentMonthCurrentYear
          for ($j = 0; $j < $tmpArrLen; $j++) :
              if (intval(substr($exportedJobsYesterdayCurrentMonthCurrentYear[0][$j]["jb_finishtime"],-2)) == $rowCounter) :
                 $EXP_VOLUME_yesterdayCurrentMonthCurrentYear = $exportedJobsYesterdayCurrentMonthCurrentYear[0][$j]["business_volume"];
                 $EXP_COUNT_yesterdayCurrentMonthCurrentYear = $exportedJobsYesterdayCurrentMonthCurrentYear[0][$j]["count_jobs"];
                 $NEXP_VOLUME_yesterdayCurrentMonthCurrentYear = ($VOLUME_yesterdayCurrentMonthCurrentYear - $EXP_VOLUME_yesterdayCurrentMonthCurrentYear);
                 $NEXP_COUNT_yesterdayCurrentMonthCurrentYear = ($COUNT_yesterdayCurrentMonthCurrentYear - $EXP_COUNT_yesterdayCurrentMonthCurrentYear);
                 if ($summatedValues[13] == "") : $summatedValues[13] = 0; endif;
                 $summatedValues[13] += $NEXP_VOLUME_yesterdayCurrentMonthCurrentYear;
                 $summatedValues[23] += $NEXP_COUNT_yesterdayCurrentMonthCurrentYear;
              endif;
          endfor;
          
          $arrPos3++;
       endif;

       // Yesterday last month current year
       $VOLUME_yesterdayLastMonthCurrentYear = 0;
       $COUNT_yesterdayLastMonthCurrentYear = 0;
       if (intval(substr($yesterdayLastMonthCurrentYear[0][$arrPos2]["jb_finishtime"],-2)) == $rowCounter) :
          $VOLUME_yesterdayLastMonthCurrentYear = $yesterdayLastMonthCurrentYear[0][$arrPos2]["business_volume"];
          $COUNT_yesterdayLastMonthCurrentYear = $yesterdayLastMonthCurrentYear[0][$arrPos2]["count_jobs"];
          if ($summatedValues[11] == "") : $summatedValues[11] = 0; endif;
          $summatedValues[11] += $VOLUME_yesterdayLastMonthCurrentYear;
          $summatedValues[21] += $COUNT_yesterdayLastMonthCurrentYear;
          $arrPos2++;
       endif;

       // Yesterday current month last year
       $VOLUME_yesterdayCurrentMonthLastYear = 0;
       $COUNT_yesterdayCurrentMonthLastYear = 0;
       if (intval(substr($yesterdayCurrentMonthLastYear[0][$arrPos1]["jb_finishtime"],-2)) == $rowCounter) :
          $VOLUME_yesterdayCurrentMonthLastYear = $yesterdayCurrentMonthLastYear[0][$arrPos1]["business_volume"];
          $COUNT_yesterdayCurrentMonthLastYear = $yesterdayCurrentMonthLastYear[0][$arrPos1]["count_jobs"];
          if ($summatedValues[10] == "") : $summatedValues[10] = 0; endif;
          $summatedValues[10] += $VOLUME_yesterdayCurrentMonthLastYear;
          $summatedValues[20] += $COUNT_yesterdayCurrentMonthLastYear;
          $arrPos1++;
       endif;

       // CARRIER VOLUME: Yesterday current month current year
       $CARRIER_VOLUME_yesterdayCurrentMonthCurrentYear = 0;
       if (intval(substr($CARRIERVOLUMEYesterdayCurrentMonthCurrentYear[0][$arrPos5]["jb_finishtime"],-2)) == $rowCounter) :
          $CARRIER_VOLUME_yesterdayCurrentMonthCurrentYear = $CARRIERVOLUMEYesterdayCurrentMonthCurrentYear[0][$arrPos5]["business_volume"];  // Fuhrlohn
          if ($summatedValues[14] == "") : $summatedValues[14] = 0; endif;
          $summatedValues[14] += $CARRIER_VOLUME_yesterdayCurrentMonthCurrentYear;
          $arrPos5++;
       endif;

       // BSG: Yesterday current month current year
       $BSG_yesterdayCurrentMonthCurrentYear = 0;
       if (intval(substr($BSGYesterdayCurrentMonthCurrentYear[0][$arrPos4]["jb_finishtime"],-2)) == $rowCounter) :
          $BSG_yesterdayCurrentMonthCurrentYear = $BSGYesterdayCurrentMonthCurrentYear[0][$arrPos4]["business_volume"];  // BSG-Quote
          $arrPos4++;
       endif;

       // BSG of SUMMATED volumes
       $summatedBSG = 0;
       if ($summatedValues[12] > 0) :
          $summatedBSG = (($summatedValues[12] - $summatedValues[14]) / $summatedValues[12]) * 100;
       endif;

       if ($i == ($iterDay - 1)) :
          $diffYesterdayLastMonthCurrentYearVolume = $VOLUME_yesterdayCurrentMonthCurrentYear - $VOLUME_yesterdayLastMonthCurrentYear;
          $diffYesterdayCurrentMonthLastYearVolume = $VOLUME_yesterdayCurrentMonthCurrentYear - $VOLUME_yesterdayCurrentMonthLastYear;
          
          $diffYesterdayLastMonthCurrentYearCount = $COUNT_yesterdayCurrentMonthCurrentYear - $COUNT_yesterdayLastMonthCurrentYear;
          $diffYesterdayCurrentMonthLastYearCount = $COUNT_yesterdayCurrentMonthCurrentYear - $COUNT_yesterdayCurrentMonthLastYear;

          $diffYesterdayLastMonthCurrentYearAverage = ($VOLUME_yesterdayCurrentMonthCurrentYear / $COUNT_yesterdayCurrentMonthCurrentYear) - ($VOLUME_yesterdayLastMonthCurrentYear / $COUNT_yesterdayLastMonthCurrentYear);
          $diffYesterdayCurrentMonthLastYearAverage = ($VOLUME_yesterdayCurrentMonthCurrentYear / $COUNT_yesterdayCurrentMonthCurrentYear) - ($VOLUME_yesterdayCurrentMonthLastYear / $COUNT_yesterdayCurrentMonthLastYear);
       endif;

       // **** BODY ****
       $matrix[$rowCounter][0] = "Tag " . pad(($i + 1),2);
       // "Umsatz"
       $matrix[$rowCounter][1] = number_format(round($VOLUME_yesterdayCurrentMonthLastYear,$roundDec), $roundDec, ",", ".");
       $matrix[$rowCounter][2] = number_format(round($VOLUME_yesterdayLastMonthCurrentYear,$roundDec), $roundDec, ",", ".");
       $matrix[$rowCounter][3] = number_format(round($VOLUME_yesterdayCurrentMonthCurrentYear,$roundDec), $roundDec, ",", ".");
       $matrix[$rowCounter][4] = number_format(round($NEXP_VOLUME_yesterdayCurrentMonthCurrentYear,$roundDec), $roundDec, ",", ".");
       $matrix[$rowCounter][5] = number_format(round($BSG_yesterdayCurrentMonthCurrentYear,2), 2, ",", ".");

       // "Umsatz kumuliert"
       $matrix[$rowCounter][10] = number_format(round($summatedValues[10],$roundDec), $roundDec, ",", ".");
       $matrix[$rowCounter][11] = number_format(round($summatedValues[11],$roundDec), $roundDec, ",", ".");
       $matrix[$rowCounter][12] = number_format(round($summatedValues[12],$roundDec), $roundDec, ",", ".");
       $matrix[$rowCounter][13] = number_format(round($summatedValues[13],$roundDec), $roundDec, ",", ".");
       $matrix[$rowCounter][14] = number_format(round($summatedBSG,2), 2, ",", ".");
       // "Anzahl Aufträge"
       $matrix[$rowCounter][20] = number_format(round($COUNT_yesterdayCurrentMonthLastYear,$roundDec), $roundDec, ",", ".");
       $matrix[$rowCounter][21] = number_format(round($COUNT_yesterdayLastMonthCurrentYear,$roundDec), $roundDec, ",", ".");
       $matrix[$rowCounter][22] = number_format(round($COUNT_yesterdayCurrentMonthCurrentYear,$roundDec), $roundDec, ",", ".");
       $matrix[$rowCounter][23] = number_format(round($NEXP_COUNT_yesterdayCurrentMonthCurrentYear,$roundDec), $roundDec, ",", ".");
       // "Durchschnittlicher Auftragswert"
       $matrix[$rowCounter][30] = number_format(round(($VOLUME_yesterdayCurrentMonthLastYear / $COUNT_yesterdayCurrentMonthLastYear),$roundDec), $roundDec, ",", ".");
       $matrix[$rowCounter][31] = number_format(round(($VOLUME_yesterdayLastMonthCurrentYear / $COUNT_yesterdayLastMonthCurrentYear),$roundDec), $roundDec, ",", ".");
       $matrix[$rowCounter][32] = number_format(round(($VOLUME_yesterdayCurrentMonthCurrentYear / $COUNT_yesterdayCurrentMonthCurrentYear),$roundDec), $roundDec, ",", ".");
   endfor;
   

   // Cumulated values
   $rowCounter++;
   $matrix[$rowCounter][0] = "Kumuliert";
   // "Umsatz"
   $matrix[$rowCounter][1] = number_format(round($summatedValues[10],$roundDec), $roundDec, ",", ".");
   $matrix[$rowCounter][2] = number_format(round($summatedValues[11],$roundDec), $roundDec, ",", ".");
   $matrix[$rowCounter][3] = number_format(round($summatedValues[12],$roundDec), $roundDec, ",", ".");
   $matrix[$rowCounter][4] = number_format(round($summatedValues[13],$roundDec), $roundDec, ",", ".");
   $matrix[$rowCounter][5] = "";
   // "Umsatz kumuliert"
   $matrix[$rowCounter][10] = "";
   $matrix[$rowCounter][11] = "";
   $matrix[$rowCounter][12] = "";
   $matrix[$rowCounter][13] = "";
   $matrix[$rowCounter][14] = "";
   // "Anzahl Aufträge"
   $matrix[$rowCounter][20] = number_format(round($summatedValues[20],$roundDec), $roundDec, ",", ".");
   $matrix[$rowCounter][21] = number_format(round($summatedValues[21],$roundDec), $roundDec, ",", ".");
   $matrix[$rowCounter][22] = number_format(round($summatedValues[22],$roundDec), $roundDec, ",", ".");
   $matrix[$rowCounter][23] = number_format(round($summatedValues[23],$roundDec), $roundDec, ",", ".");
   // "Durchschnittlicher Auftragswert"
   $matrix[$rowCounter][30] = number_format(round(($summatedValues[10] / $summatedValues[20]),$roundDec), $roundDec, ",", ".");
   $matrix[$rowCounter][31] = number_format(round(($summatedValues[11] / $summatedValues[21]),$roundDec), $roundDec, ",", ".");
   $matrix[$rowCounter][32] = number_format(round(($summatedValues[12] / $summatedValues[22]),$roundDec), $roundDec, ",", ".");
   $matrix[$rowCounter][33] = number_format(round(($summatedValues[13] / $summatedValues[23]),$roundDec), $roundDec, ",", ".");


   // Difference current day
   $rowCounter++;
   $matrix[$rowCounter][0] = "Abw. akt. Tag";
   // "Umsatz"
   $matrix[$rowCounter][1] = number_format(round($diffYesterdayCurrentMonthLastYearVolume,$roundDec), $roundDec, ",", ".");
   $matrix[$rowCounter][2] = number_format(round($diffYesterdayLastMonthCurrentYearVolume,$roundDec), $roundDec, ",", ".");
   $matrix[$rowCounter][3] = "";
   $matrix[$rowCounter][4] = "";
   $matrix[$rowCounter][5] = "";
   // "Umsatz kumuliert"
   $matrix[$rowCounter][10] = "";
   $matrix[$rowCounter][11] = "";
   $matrix[$rowCounter][12] = "";
   $matrix[$rowCounter][13] = "";
   $matrix[$rowCounter][14] = "";
   // "Anzahl Aufträge"
   $matrix[$rowCounter][20] = number_format(round($diffYesterdayCurrentMonthLastYearCount,$roundDec), $roundDec, ",", ".");
   $matrix[$rowCounter][21] = number_format(round($diffYesterdayLastMonthCurrentYearCount,$roundDec), $roundDec, ",", ".");
   $matrix[$rowCounter][22] = "";
   $matrix[$rowCounter][23] = "";
   // "Durchschnittlicher Auftragswert"
   $matrix[$rowCounter][30] = number_format(round($diffYesterdayCurrentMonthLastYearAverage,$roundDec), $roundDec, ",", ".");
   $matrix[$rowCounter][31] = number_format(round($diffYesterdayLastMonthCurrentYearAverage,$roundDec), $roundDec, ",", ".");
   $matrix[$rowCounter][32] = "";


   // Difference cumulated month
   $rowCounter++;
   $matrix[$rowCounter][0] = "Abw. kumuliert";
   // "Umsatz"
   $matrix[$rowCounter][1] = number_format(round(($summatedValues[12] - $summatedValues[10]),$roundDec), $roundDec, ",", ".");
   $matrix[$rowCounter][2] = number_format(round(($summatedValues[12] - $summatedValues[11]),$roundDec), $roundDec, ",", ".");
   $matrix[$rowCounter][3] = "";
   $matrix[$rowCounter][4] = "";
   $matrix[$rowCounter][5] = "";
   // "Umsatz kumuliert"
   $matrix[$rowCounter][10] = "";
   $matrix[$rowCounter][11] = "";
   $matrix[$rowCounter][12] = "";
   $matrix[$rowCounter][13] = "";
   $matrix[$rowCounter][14] = "";
   // "Anzahl Aufträge"
   $matrix[$rowCounter][20] = number_format(round(($summatedValues[22] - $summatedValues[20]),$roundDec), $roundDec, ",", ".");
   $matrix[$rowCounter][21] = number_format(round(($summatedValues[22] - $summatedValues[21]),$roundDec), $roundDec, ",", ".");
   $matrix[$rowCounter][22] = "";
   $matrix[$rowCounter][23] = "";
   // "Durchschnittlicher Auftragswert"
   $matrix[$rowCounter][30] = number_format(round((($summatedValues[12] / $summatedValues[22]) - ($summatedValues[10] / $summatedValues[20])),$roundDec), $roundDec, ",", ".");
   $matrix[$rowCounter][31] = number_format(round((($summatedValues[12] / $summatedValues[22]) - ($summatedValues[11] / $summatedValues[21])),$roundDec), $roundDec, ",", ".");
   $matrix[$rowCounter][32] = "";


   // Define HTML output
   $output .= "<table style=\"border:1px solid #77FF77; border-collapse:collapse; background-color:#CCFFCC;\" border=\"1\">\n";
   $output .= "<tr><td> </td><td colspan=\"3\" align=\"center\"><b>" . "Umsatz " . $hqName . "</b></td>"
            .     "<td colspan=\"3\" align=\"center\"><b>" . "Umsatz kumuliert</b></td>"
            .     "<td colspan=\"4\" align=\"center\"><b>" . "Anzahl Aufträge" . "</b></td>"
            .     "<td colspan=\"3\" align=\"center\"><b>" . "Durchschnittlicher Auftragswert" . "</b></td><tr>\n";
   // $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 <= $rowCounter; $i++) :
       $output .= "<tr>\n";

       $output .= "  <td class=\"td_border td_cell\" align=\"left\">" . $matrix[$i][0] . "</td>\n";
       $output .= "  <td class=\"td_border td_cell\" align=\"right\">" . $matrix[$i][1] . "</td>\n";
       $output .= "  <td class=\"td_border td_cell\" align=\"right\">" . $matrix[$i][2] . "</td>\n";
       $output .= "  <td class=\"td_border td_cell\" align=\"right\">" . $matrix[$i][3] . "</td>\n";
       // $output .= "  <td class=\"td_border td_cell\" align=\"right\">" . $matrix[$i][4] . "</td>\n";
       // $output .= "  <td class=\"td_border td_cell\" align=\"right\">" . $matrix[$i][5] . "</td>\n";
       
       $output .= "  <td class=\"td_border td_cell\" align=\"right\">" . $matrix[$i][10] . "</td>\n";
       $output .= "  <td class=\"td_border td_cell\" align=\"right\">" . $matrix[$i][11] . "</td>\n";
       $output .= "  <td class=\"td_border td_cell\" align=\"right\">" . $matrix[$i][12] . "</td>\n";
       // $output .= "  <td class=\"td_border td_cell\" align=\"right\">" . $matrix[$i][13] . "</td>\n";
       // $output .= "  <td class=\"td_border td_cell\" align=\"right\">" . $matrix[$i][14] . "</td>\n";
       
       $output .= "  <td class=\"td_border td_cell\" align=\"right\">" . $matrix[$i][20] . "</td>\n";
       $output .= "  <td class=\"td_border td_cell\" align=\"right\">" . $matrix[$i][21] . "</td>\n";
       $output .= "  <td class=\"td_border td_cell\" align=\"right\">" . $matrix[$i][22] . "</td>\n";
       $output .= "  <td class=\"td_border td_cell\" align=\"right\">" . $matrix[$i][23] . "</td>\n";

       $output .= "  <td class=\"td_border td_cell\" align=\"right\">" . $matrix[$i][30] . "</td>\n";
       $output .= "  <td class=\"td_border td_cell\" align=\"right\">" . $matrix[$i][31] . "</td>\n";
       $output .= "  <td class=\"td_border td_cell\" align=\"right\">" . $matrix[$i][32] . "</td>\n";

       $output .= "</tr>\n";
   endfor;
   $output .= "</table>\n";


   // **** 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;
   

HTB  lutz.richter@hansetrans.de
HTDD anett.wachter@hansetrans.de
HTE  marcell.jansen@hansetrans.de, dirk.franke@hansetrans.de
HTF  michael.suchy@hansetrans.de, axel.vetter@hansetrans.de
HTH  gritzko@hansetrans.de, dirk.franke@hansetrans.de
HTHB nicolai.parwanow@hansetrans.de, tobias.thormann@hansetrans.de
HTHH arwed.wagner@hansetrans.de, thomas.jacobson@hansetrans.de
HTK  andreas.repenning@hansetrans.de, dirk.franke@hansetrans.de
HTL  rene.mueller@hansetrans.de
HTM  ralph.jaegermeyr@hansetrans.de, stefan.leismann@hansetrans.de
HTN  lutz.lauenstein@hansetrans.de, axel.vetter@hansetrans.de
HTS  axel.vetter@hansetrans.de

Gesamtauswertung  gfl.gt@hansetrans.de
*/

   if ($currHqId == "101") :
      $mailResult = $mailObj->send(array("nicolai.parwanow@hansetrans.de","tobias.thormann@hansetrans.de","mail-cron@assecutor.de"), 'smtp');
   elseif ($currHqId == "102") :
      $mailResult = $mailObj->send(array("arwed.wagner@hansetrans.de","thomas.jacobson@hansetrans.de","ingo.vonderheiden@hansetrans.de","mail-cron@assecutor.de"), 'smtp');
   elseif ($currHqId == "103") :
      $mailResult = $mailObj->send(array("lutz.richter@hansetrans.de","mail-cron@assecutor.de"), 'smtp');
   elseif ($currHqId == "104") :
      $mailResult = $mailObj->send(array("gritzko@hansetrans.de","dirk.franke@hansetrans.de","mail-cron@assecutor.de"), 'smtp');
   elseif ($currHqId == "105") :
      $mailResult = $mailObj->send(array("michael.suchy@hansetrans.de","mail-cron@assecutor.de"), 'smtp');
   elseif ($currHqId == "106") :
      $mailResult = $mailObj->send(array("anett.wachter@hansetrans.de","mail-cron@assecutor.de"), 'smtp');
   elseif ($currHqId == "107") :
      $mailResult = $mailObj->send(array("marcell.jansen@hansetrans.de","dirk.franke@hansetrans.de","mail-cron@assecutor.de"), 'smtp');
   elseif ($currHqId == "108") :
      $mailResult = $mailObj->send(array("rene.mueller@hansetrans.de","mail-cron@assecutor.de"), 'smtp');
   elseif ($currHqId == "109") :
      $mailResult = $mailObj->send(array("ralph.jaegermeyr@hansetrans.de","mail-cron@assecutor.de"), 'smtp');
   elseif ($currHqId == "110") :
      $mailResult = $mailObj->send(array("gerhart.hintermeier@hansetrans.de","mail-cron@assecutor.de"), 'smtp');
   elseif ($currHqId == "111") :
      $mailResult = $mailObj->send(array("michael.rettkowski@hansetrans.de","mail-cron@assecutor.de"), 'smtp');
   elseif ($currHqId == "112") :
      $mailResult = $mailObj->send(array("andreas.repenning@hansetrans.de","dirk.franke@hansetrans.de","mail-cron@assecutor.de"), 'smtp');
   elseif ($currHqId == "203") :
      $mailResult = $mailObj->send(array("ive.kuehne@hansetrans,de","mareen.vehlgut@hansetrans.de","mail-cron@assecutor.de"), 'smtp');
   else :
      $mailResult = $mailObj->send(array("rainer.westphal@hansetrans.de","jaenecke@hansetrans.de","sascha.engel@hansetrans.de","gf@assecutor.de"), 'smtp');
   endif;


   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;
?>
