<?php
/*=======================================================================
 *
 * statistic_special_01.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");


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


// Init global parameter for script
$aligns = "";
$f_statusMode = "1";  // Finished jobs
$f_dateMode = "";  // "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
if ($f_hq_id == "") : 
   // $f_hq_id = getColVectorFromDB2ArrayByClause("mandatorheadquarters AS mdhq, headquarters AS hq", "hq.hq_id", "mdhq.md_id = '" . $md_id . "' AND mdhq.hq_id = hq.hq_id", "", "hq.hq_mnemonic", "");
   $f_hq_id = getColVectorFromDB2ArrayByClause("headquarters", "hq_id", "", "", "", "");
endif;
// $f_hq_id = array("1111");  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! TEST !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// print_r($f_hq_id); echo "\n\n <br><br>";
$csEid = "";
$crEid = "";
$orderByClause = "";

// Output matrix
$matrix = array();  // array("","Monat Vorjahr","Vormonat","Aktueller Monat IST","Aktueller Monat SOLL")
$output = "";

// **** Business volume current month of current year ****
$globalParArray = statisticSetGlobalParameterArray();
$f_category = "1";
$f_statistic = "30";
$idArrayLen = getCategoryDBFields($f_statistic);
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;
$volCurrentMonthCurrentYear = getStatistic($globalParArray, $f_statistic, $fromDateRange . " 00:00:00", $toDateRange . " 23:59:59", $csEid, $crEid, $orderByClause);
$matrix[0][3] = "Aktueller Monat IST: [" . $fromDateRange . " bis " . $toDateRange . "]";
$matrix[1][3] = number_format(round($volCurrentMonthCurrentYear[0][0]["business_volume"],2), 2, ",", ".");


// **** Business volume last month of current year ****
$globalParArray = statisticSetGlobalParameterArray();
$f_category = "1";
$f_statistic = "30";
$idArrayLen = getCategoryDBFields($f_statistic);
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;
$volLastMonthCurrentYear = getStatistic($globalParArray, $f_statistic, $fromDateRange . " 00:00:00", $toDateRange . " 23:59:59", $csEid, $crEid, $orderByClause);
$matrix[0][2] = "Vormonat: [" . $fromDateRange . " bis " . $toDateRange . "]";
$matrix[1][2] = number_format(round($volLastMonthCurrentYear[0][0]["business_volume"],2), 2, ",", ".");


// **** Business volume current month of last year ****
$globalParArray = statisticSetGlobalParameterArray();
$f_category = "1";
$f_statistic = "30";
$idArrayLen = getCategoryDBFields($f_statistic);
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;
$volCurrentMonthLastYear = getStatistic($globalParArray, $f_statistic, $fromDateRange . " 00:00:00", $toDateRange . " 23:59:59", $csEid, $crEid, $orderByClause);
$matrix[0][1] = "Monat Vorjahr: [" . $fromDateRange . " bis " . $toDateRange . "]";
$matrix[1][1] = number_format(round($volCurrentMonthLastYear[0][0]["business_volume"],2), 2, ",", ".");



/*
   // Current year
   if ($mode == "date_firstday_firstmonth_currentyear") : $retVal = date("Y-m-d",mktime(0,0,0,1,1,date("Y"))); endif;    // 2015-01-01, ...
   if ($mode == "date_yesterday_currentmonth_currentyear") : $retVal = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-1,date("Y"))); endif;    // 2015-05-25, ...
   if ($mode == "date_firstday_currentmonth_currentyear") : $retVal = date("Y-m-d",mktime(0,0,0,date("m"),1,date("Y"))); endif;    // 2015-05-01, ...      
   if ($mode == "date_lastday_currentmonth_currentyear") : $retVal = date("Y-m-d",mktime(0,0,0,date("m")+1,0,date("Y"))); endif;    // 2015-05-31, ...
   if ($mode == "date_yesterday_lastmonth_currentyear") : $retVal = date("Y-m-d",mktime(0,0,0,date("m")-1,date("d")-1,date("Y"))); endif;    // 2015-04-25, ...
   if ($mode == "date_firstday_lastmonth_currentyear") : $retVal = date("Y-m-d",mktime(0,0,0,date("m")-1,1,date("Y"))); endif;    // 2015-04-01, ...
   if ($mode == "date_lastday_lastmonth_currentyear") : $retVal = date("Y-m-d",mktime(0,0,0,date("m"),0,date("Y"))); endif;    // 2015-04-31, ...
   if ($mode == "date_yesterday_nexttolastmonth_currentyear") : $retVal = date("Y-m-d",mktime(0,0,0,date("m")-2,date("d")-1,date("Y"))); endif;    // 2015-03-25, ...      
   if ($mode == "date_firstday_nexttolastmonth_currentyear") : $retVal = date("Y-m-d",mktime(0,0,0,date("m")-2,1,date("Y"))); endif;    // 2015-03-01, ...
   if ($mode == "date_lastday_nexttolastmonth_currentyear") : $retVal = date("Y-m-d",mktime(0,0,0,date("m")-1,0,date("Y"))); endif;    // 2015-03-31, ...

   // Last year
   if ($mode == "date_firstday_firstmonth_lastyear") : $retVal = date("Y-m-d",mktime(0,0,0,1,1,date("Y")-1)); endif;    // 2014-01-01, ...
   if ($mode == "date_yesterday_currentmonth_lastyear") : $retVal = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-1,date("Y")-1)); endif;    // 2014-05-25, ...
   if ($mode == "date_firstday_currentmonth_lastyear") : $retVal = date("Y-m-d",mktime(0,0,0,date("m"),1,date("Y")-1)); endif;    // 2014-04-01, ...
   if ($mode == "date_yesterday_lastmonth_lastyear") : $retVal = date("Y-m-d",mktime(0,0,0,date("m")-1,date("d")-1,date("Y")-1)); endif;    // 2014-04-25, ...
   if ($mode == "date_firstday_lastmonth_lastyear") : $retVal = date("Y-m-d",mktime(0,0,0,date("m")-1,1,date("Y")-1)); endif;    // 2015-05-01, ...
   if ($mode == "date_lastday_lastmonth_lastyear") : $retVal = date("Y-m-d",mktime(0,0,0,date("m"),0,date("Y")-1)); endif;    // 2015-05-31, ...
   if ($mode == "date_yesterday_nexttolastmonth_lastyear") : $retVal = date("Y-m-d",mktime(0,0,0,date("m")-2,date("d")-1,date("Y")-1)); endif;    // 2014-04-25, ...
   if ($mode == "date_firstday_nexttolastmonth_lastyear") : $retVal = date("Y-m-d",mktime(0,0,0,date("m")-2,1,date("Y")-1)); endif;    // 2015-05-01, ...
   if ($mode == "date_lastday_nexttolastmonth_lastyear") : $retVal = date("Y-m-d",mktime(0,0,0,date("m")-1,0,date("Y")-1)); endif;    // 2015-05-31, ...
*/

$output .= "<table border=\"1\">";
$output .= "<tr><td> </td><td colspan=\"4\"><b>" . "Umsatz" . "</b></td><td> </td><td colspan=\"4\"><b>" . "Anzahl Aufträge" . "</b></td><tr>";
for ($i = 0; $i < 2; $i++) :
    $output .= "<tr>";
    for ($j = 0; $j < 9; $j++) :
         $output .= "<td align=\"right\">" . $matrix[$i][$j] . "</td>";
    endfor;
    $output .= "</tr>";
endfor;
$output .= "</table>";

echo $output;
?>
