<?php
include($_SERVER['DOCUMENT_ROOT'] .'/script/HeaderInc.php');

if ((true === isset($_SESSION['SessionAuth']['SvtAdmin']['reg']) && 1 == $_SESSION['SessionAuth']['SvtAdmin']['reg']) ||
    (true === isset($_SESSION['SessionAuth']['SvtOperatorAdv']['reg']) && 1 == $_SESSION['SessionAuth']['SvtOperatorAdv']['reg']) ||
    (true === isset($_SESSION['SessionAuth']['SvtOperator']['reg']) && 1 == $_SESSION['SessionAuth']['SvtOperator']['reg'])
   ){
  $smarty->assign('VarMenuMain', $VarMenu);
  
	$ObjDemandsClassifMod = new DemandsClassifMod($MS_SP);
	$ArrDemandsClassif = $ObjDemandsClassifMod->GetFromDb('SqlAll', 'GetAll');
  $smarty->assign('ArrDemandsClassif', $ArrDemandsClassif);
  //SiteLibs::GetVarDump($ArrDemandsClassif);
  
	$ObjDemandsMod = new DemandsMod($MS_SP);
	$ObjDbMod = new DbMod($MS_SP);
  
  
  $VarSql = "SELECT b.demandsclassif_id, year(a.demands_date) AS year_demands_date
FROM ". $ObjDbMod->VarTableDemands ." AS a, ". $ObjDbMod->VarTableDemandsClassif ." AS b
GROUP BY b.demandsclassif_id, year(a.demands_date)
ORDER BY YEAR(a.demands_date)";
  
  $VarSqlTempl = "SELECT count(*) AS cnt
     , year(demands_date) AS year_demands_date
     , b.demandsclassif_id
FROM
  ". $ObjDbMod->VarTableDemands ." AS a, $ObjDbMod->VarTableDemandsClassif AS b
WHERE
  a.demandsclassif_id = b.demandsclassif_id
  AND a.demands_date IS NOT NULL
GROUP BY
  year_demands_date, b.demandsclassif_id
ORDER BY
  year_demands_date, demandsclassif_id";

	$ArrDate = $ObjDemandsMod->GetFromDb('SqlQuery', 'GetAll', $VarSql);
  $ArrDateTempl = $ObjDemandsMod->GetFromDb('SqlQuery', 'GetAll', $VarSqlTempl);
  //$VarYearPrev = '';
  foreach ($ArrDate as $key => $value) {
    $ArrDate[$key]['cnt'] = 0;
    foreach ($ArrDateTempl as $keyTempl => $valueTempl) {
      if($valueTempl['year_demands_date'] == $value['year_demands_date'] && $valueTempl['demandsclassif_id'] == $value['demandsclassif_id']){
        $ArrDate[$key]['cnt'] = $valueTempl['cnt'];
      }
    }
  }
  //SiteLibs::GetVarDump($ArrDate);
  $smarty->assign('ArrDate', $ArrDate);
  $VarPagesTxt .= $smarty->fetch('Demands/DemandsReportForOperatorsCharts.tpl');

  if(true === is_file($MS_SP->SP_CurrFileNameText)){
    $VarPagesTxt .= $smarty->fetch($MS_SP->SP_CurrFileNameText);
  }
}
else{
  $smarty->assign('VarError', $MS_SP->Sp_LangMessage['error']['ErrAuthAccessDenied']);
  $VarPagesTxt .= $smarty->fetch('AccessDenied.tpl');
}

$smarty->assign('VarPagesTxt', @$VarPagesTxt);
if (true === isset($MS_SP->SP_CurrPage['template_file']) && '' != $MS_SP->SP_CurrPage['template_file']){
	$smarty->display($MS_SP->SP_CurrPage['template_file']);
}
else {
	$smarty->display('Page.tpl');
}
include($_SERVER['DOCUMENT_ROOT'] .'/script/FooterInc.php');
?>