<?php

class grid_fiche_prof_sante_labo_stat_total
{
   var $Db;
   var $Erro;
   var $Ini;
   var $Lookup;

   var $nm_data;

   //----- 
   function __construct($sc_page)
   {
      $this->sc_page = $sc_page;
      $this->nm_data = new nm_data("fr");
      if (isset($_SESSION['sc_session'][$this->sc_page]['grid_fiche_prof_sante_labo_stat']['campos_busca']) && !empty($_SESSION['sc_session'][$this->sc_page]['grid_fiche_prof_sante_labo_stat']['campos_busca']))
      { 
          $Busca_temp = $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['campos_busca'];
          if ($_SESSION['scriptcase']['charset'] != "UTF-8")
          {
              $Busca_temp = NM_conv_charset($Busca_temp, $_SESSION['scriptcase']['charset'], "UTF-8");
          }
          $this->id_fiche_ps = (isset($Busca_temp['id_fiche_ps'])) ? $Busca_temp['id_fiche_ps'] : ""; 
          $tmp_pos = (is_string($this->id_fiche_ps)) ? strpos($this->id_fiche_ps, "##@@") : false;
          if ($tmp_pos !== false && !is_array($this->id_fiche_ps))
          {
              $this->id_fiche_ps = substr($this->id_fiche_ps, 0, $tmp_pos);
          }
          $this->date_declaration = (isset($Busca_temp['date_declaration'])) ? $Busca_temp['date_declaration'] : ""; 
          $tmp_pos = (is_string($this->date_declaration)) ? strpos($this->date_declaration, "##@@") : false;
          if ($tmp_pos !== false && !is_array($this->date_declaration))
          {
              $this->date_declaration = substr($this->date_declaration, 0, $tmp_pos);
          }
          $date_declaration_2 = (isset($Busca_temp['date_declaration_input_2'])) ? $Busca_temp['date_declaration_input_2'] : ""; 
          $this->date_declaration_2 = $date_declaration_2; 
          $this->wilaya = (isset($Busca_temp['wilaya'])) ? $Busca_temp['wilaya'] : ""; 
          $tmp_pos = (is_string($this->wilaya)) ? strpos($this->wilaya, "##@@") : false;
          if ($tmp_pos !== false && !is_array($this->wilaya))
          {
              $this->wilaya = substr($this->wilaya, 0, $tmp_pos);
          }
      } 
   }


   //----- 
   function Calc_resumo_sc_free_group_by($destino_resumo, $res_export=false)
   {
      global $nm_lang;
      $this->nm_data = new nm_data("fr");
      unset($_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['sql_tot_res']);
      if (isset($_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['campos_busca']) && !empty($_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['campos_busca']))
      { 
          $Busca_temp = $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['campos_busca'];
          if ($_SESSION['scriptcase']['charset'] != "UTF-8")
          {
              $Busca_temp = NM_conv_charset($Busca_temp, $_SESSION['scriptcase']['charset'], "UTF-8");
          }
          $this->id_fiche_ps = (isset($Busca_temp['id_fiche_ps'])) ? $Busca_temp['id_fiche_ps'] : ""; 
          $tmp_pos = (is_string($this->id_fiche_ps)) ? strpos($this->id_fiche_ps, "##@@") : false;
          if ($tmp_pos !== false && !is_array($this->id_fiche_ps))
          {
              $this->id_fiche_ps = substr($this->id_fiche_ps, 0, $tmp_pos);
          }
          $this->date_declaration = (isset($Busca_temp['date_declaration'])) ? $Busca_temp['date_declaration'] : ""; 
          $tmp_pos = (is_string($this->date_declaration)) ? strpos($this->date_declaration, "##@@") : false;
          if ($tmp_pos !== false && !is_array($this->date_declaration))
          {
              $this->date_declaration = substr($this->date_declaration, 0, $tmp_pos);
          }
          $this->date_declaration_2 = (isset($Busca_temp['date_declaration_input_2'])) ? $Busca_temp['date_declaration_input_2'] : ""; 
          $this->wilaya = (isset($Busca_temp['wilaya'])) ? $Busca_temp['wilaya'] : ""; 
          $tmp_pos = (is_string($this->wilaya)) ? strpos($this->wilaya, "##@@") : false;
          if ($tmp_pos !== false && !is_array($this->wilaya))
          {
              $this->wilaya = substr($this->wilaya, 0, $tmp_pos);
          }
      } 
      $this->sc_where_atual  = $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['where_pesq'];
      $ind_qb                = $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['SC_Ind_Groupby'];
      $cmp_sql_def   = array('wilaya' => "wilaya",'commune' => "commune",'date_declaration' => "date_declaration",'maladie_confirmee_labo' => "maladie_confirmee_labo",'date_confirmation_labo' => "date_confirmation_labo",'type_prelevement_nm_fk' => "type_prelevement_nm_fk",'nationalite_nm_fk' => "nationalite_nm_fk",'pays_origine' => "pays_origine");
      $cmps_quebra_atual = array();
      foreach ($_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['SC_Gb_Free_cmp'] as $cmp => $resto)
      {
          $cmps_quebra_atual[] = $cmp;
      }
      $ult_cmp_quebra_atual = $cmps_quebra_atual[(count($cmps_quebra_atual) - 1)];
      $arr_tots = "";
      $join     = "";
      $group    = "";
      $i_group  = 1;
      $cmps_gb  = "";
      $cmps_gb1 = "";
      $cmps_gb2 = "";
      $cmps_gbS = array();
      $ind_cmps = 1;
      $ind_alias = "1";
      $cmp_dim   = array();
      $all_group = array();
      foreach ($cmps_quebra_atual as $cmp_gb)
      {
          $Format_tst = $this->Ini->Get_Gb_date_format($ind_qb, $cmp_gb);
          if (!empty($Format_tst))
          {
              $Str_arg_sum = $this->Ini->Get_date_arg_sum($cmp_gb, $Format_tst, $cmp_sql_def[$cmp_gb], false, true);
              $Str_arg_sql = ($Str_arg_sum == " is null") ? $cmp_sql_def[$cmp_gb] : $this->Ini->Get_sql_date_groupby($cmp_sql_def[$cmp_gb], $Format_tst);
          }
          else
          {
              $Str_arg_sql = "";
              $Str_arg_sum = $cmp_sql_def[$cmp_gb] . " *sc# SC." . $cmp_sql_def[$cmp_gb];
          }
          $cmp_dim[$cmp_gb] = $ind_cmps;
          $temp = explode(" and ", $Str_arg_sum);
          foreach ($temp as $cada_parte)
          {
              $temp1 = explode("*sc#", $cada_parte);
              if (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_informix))
              {
                  $group .= (empty($group)) ? $i_group : "," . $i_group;
              }
              elseif (!in_array($Str_arg_sql . trim($temp1[0]), $all_group))
              {
                  $group .= (empty($group)) ? $Str_arg_sql . trim($temp1[0]) : "," . $Str_arg_sql . trim($temp1[0]);
                  $all_group[] = $Str_arg_sql . trim($temp1[0]);
              }
              $cmps_gb1 .= (empty($cmps_gb1)) ? $Str_arg_sql . trim($temp1[0]) : "," . $Str_arg_sql . trim($temp1[0]);
              $cmps_gb1 .= " as a_cmp_" .  $ind_alias;
              $cmps_gb2 .= (empty($cmps_gb2)) ? $Str_arg_sql . trim($temp1[0]) : "," . $Str_arg_sql . trim($temp1[0]);
              $cmps_gb2 .= " as b_cmp_" .  $ind_alias;
              $join     .= empty($join) ? "" : " and ";
              $join     .= " SC_sel1.a_cmp_" .  $ind_alias . " =  SC_sel2.b_cmp_" .  $ind_alias;
              $ind_cmps++;
              $ind_alias++;
              $i_group++;
          }
      }
      $ind_cmps  = 1;
      $ind_alias = "1";
      $cmp_dim   = array();
      foreach ($cmps_quebra_atual as $cmp_gb)
      {
          $arr_tots .= "[\$" . $cmp_gb . "_orig]";
          $Format_tst = $this->Ini->Get_Gb_date_format($ind_qb, $cmp_gb);
          if (!empty($Format_tst))
          {
              $Str_arg_sum = $this->Ini->Get_date_arg_sum($cmp_gb, $Format_tst, $cmp_sql_def[$cmp_gb], false, true);
              $Str_arg_sql = ($Str_arg_sum == " is null") ? $cmp_sql_def[$cmp_gb] : $this->Ini->Get_sql_date_groupby($cmp_sql_def[$cmp_gb], $Format_tst);
          }
          else
          {
              $Str_arg_sql = "";
              $Str_arg_sum = $cmp_sql_def[$cmp_gb] . " *sc# SC." . $cmp_sql_def[$cmp_gb];
          }
          $cmp_dim[$cmp_gb] = $ind_cmps;
          $temp = explode(" and ", $Str_arg_sum);
          foreach ($temp as $cada_parte)
          {
              $temp1 = explode("*sc#", $cada_parte);
              $cmps_gb  .= (empty($cmps_gb)) ? "a_cmp_" .  $ind_alias : "," . "a_cmp_" .  $ind_alias;
              $cmps_gbS['a_cmp_' . $ind_alias] = $Str_arg_sql . trim($temp1[0]);
              $ind_cmps++;
              $ind_alias++;
          }
          $this->Res_Totaliza_sc_free_group_by($ind_qb, $cmp_gb, $arr_tots, $group, $join, $cmps_gb, $cmps_gb1, $cmps_gb2, $cmps_gbS, $cmp_dim, $cmps_quebra_atual, $cmp_sql_def, $res_export);
      }
      $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['arr_total'] = array();
      foreach ($cmps_quebra_atual as $cmp_gb)
      {
          $Arr_tot_name = "array_total_" . $cmp_gb;
          $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['arr_total'][$cmp_gb] = $this->$Arr_tot_name;
      }
   }

   function Res_Totaliza_sc_free_group_by($ind_qb, $cmp_tot, $arr_tots, $group, $join, $cmps_quebras, $cmps_quebras1, $cmps_quebras2, $cmps_quebrasS, $Cmp_dim, $cmps_quebra_atual, $cmp_sql_def, $res_export)
   {
      $sc_having = ((isset($parms_sub_sel['having']))) ? "  having " . $parms_sub_sel['having'] : "";
      $Tem_estat_manual = false;
      $where_ok = $this->sc_where_atual;
      $cmp_sql_tp_num = array('id_fiche_ps' => 'N','age_malade' => 'N');
      if (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_access))
      { 
         $cmd_simp = "select count(*)#@#cmps_quebras#@# from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp " . $where_ok;
         $comando  = "select count(*)#@#cmps_quebras#@# from (";
         $comando .= "select " . $cmps_quebras1 . " from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp1 " . $where_ok . ") SC_sel1 INNER JOIN (";
         $comando .= "select " . $cmps_quebras2 . " from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp2 " . $where_ok . " group by " . $group . $sc_having . ") SC_sel2 ";
         $comando .= " ON " . $join;
      } 
      elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_sybase))
      { 
         $cmd_simp = "select count(*)#@#cmps_quebras#@# from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp " . $where_ok;
         $comando  = "select count(*)#@#cmps_quebras#@# from (";
         $comando .= "select " . $cmps_quebras1 . " from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp1 " . $where_ok . ") SC_sel1 INNER JOIN (";
         $comando .= "select " . $cmps_quebras2 . " from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp2 " . $where_ok . " group by " . $group . $sc_having . ") SC_sel2 ";
         $comando .= " ON " . $join;
      } 
      elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_mssql))
      { 
         $cmd_simp = "select count(*)#@#cmps_quebras#@# from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp " . $where_ok;
         $comando  = "select count(*)#@#cmps_quebras#@# from (";
         $comando .= "select " . $cmps_quebras1 . " from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp1 " . $where_ok . ") SC_sel1 INNER JOIN (";
         $comando .= "select " . $cmps_quebras2 . " from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp2 " . $where_ok . " group by " . $group . $sc_having . ") SC_sel2 ";
         $comando .= " ON " . $join;
      } 
      elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_oracle))
      { 
         $cmd_simp = "select count(*)#@#cmps_quebras#@# from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp " . $where_ok;
         $comando  = "select count(*)#@#cmps_quebras#@# from (";
         $comando .= "select " . $cmps_quebras1 . " from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp1 " . $where_ok . ") SC_sel1 INNER JOIN (";
         $comando .= "select " . $cmps_quebras2 . " from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp2 " . $where_ok . " group by " . $group . $sc_having . ") SC_sel2 ";
         $comando .= " ON " . $join;
      } 
      elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_ibase))
      { 
         $cmd_simp = "select count(*)#@#cmps_quebras#@# from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp " . $where_ok;
         $comando  = "select count(*)#@#cmps_quebras#@# from (";
         $comando .= "select " . $cmps_quebras1 . " from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp1 " . $where_ok . ") SC_sel1 INNER JOIN (";
         $comando .= "select " . $cmps_quebras2 . " from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp2 " . $where_ok . " group by " . $group . $sc_having . ") SC_sel2 ";
         $comando .= " ON " . $join;
      } 
      else 
      { 
         $cmd_simp = "select count(*)#@#cmps_quebras#@# from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp " . $where_ok;
         $comando  = "select count(*)#@#cmps_quebras#@# from (";
         $comando .= "select " . $cmps_quebras1 . " from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp1 " . $where_ok . ") SC_sel1 INNER JOIN (";
         $comando .= "select " . $cmps_quebras2 . " from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp2 " .  $where_ok . " group by " . $group . $sc_having . ") SC_sel2 ";
         $comando .= " ON " . $join;
      } 
      if (!isset($_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['sql_tot_res']))
      {
         $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['sql_tot_res'] = str_replace("#@#cmps_quebras#@#", "", $comando);
      }
      $comando  = str_replace("#@#cmps_quebras#@#", "," . $cmps_quebras, $comando);
      $comando .= " group by " . $cmps_quebras . " order by " .  $cmps_quebras;
      if (!isset($_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['Res_search_metric_use']) || empty($_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['Res_search_metric_use']))
      {
          $comando = $cmd_simp;
          $cmps_S  = "";
          foreach ($cmps_quebrasS as $alias => $sql)
          {
              $cmps_S .= empty($cmps_S) ? $sql : ", " . $sql;
          }
          $comando = str_replace("#@#cmps_quebras#@#", "," . $cmps_S, $comando);
          $order_group = "";
          foreach ($cmps_quebrasS as $alias => $cada_tst)
          {
              $cada_tst = trim($cada_tst);
              $pos = strpos(" " . $order_group, " " . $cada_tst);
              if ($pos === false)
              {
                  $order_group .= (!empty($order_group)) ? ", " . $cada_tst : $cada_tst;
              }
          }
          $comando .= " group by " . $order_group . " order by " .  $order_group;
      }
      $comando  = $this->Ajust_statistic($comando);
      $_SESSION['scriptcase']['sc_sql_ult_comando'] = $comando;
      $_SESSION['scriptcase']['sc_sql_ult_conexao'] = '';
      if (!$rt = $this->Db->Execute($comando))
      {
         $this->Erro->mensagem(__FILE__, __LINE__, "banco", $this->Ini->Nm_lang['lang_errm_dber'], $this->Db->ErrorMsg()); 
         exit;
      }
      $format_dimensions = array();
      $format_dimensions['wilaya']['reg'] = "S";
      $format_dimensions['wilaya']['msk'] = "";
      $format_dimensions['commune']['reg'] = "S";
      $format_dimensions['commune']['msk'] = "";
      $format_dimensions['date_declaration']['reg'] = "N";
      $format_dimensions['date_declaration']['msk'] = "F/Y";
      $format_dimensions['maladie_confirmee_labo']['reg'] = "S";
      $format_dimensions['maladie_confirmee_labo']['msk'] = "";
      $format_dimensions['date_confirmation_labo']['reg'] = "N";
      $format_dimensions['date_confirmation_labo']['msk'] = "F/Y";
      $format_dimensions['type_prelevement_nm_fk']['reg'] = "S";
      $format_dimensions['type_prelevement_nm_fk']['msk'] = "";
      $format_dimensions['nationalite_nm_fk']['reg'] = "S";
      $format_dimensions['nationalite_nm_fk']['msk'] = "";
      $format_dimensions['pays_origine']['reg'] = "S";
      $format_dimensions['pays_origine']['msk'] = "";
      while (!$rt->EOF)
      {
          $sql_where = "";
          foreach ($Cmp_dim as $Cada_dim => $Ind_sql)
          {
              $prep_look  = $Cada_dim . "_SC_look";
              $$prep_look = $rt->fields[$Ind_sql];
              $SC_prep = $this->Ini->Get_format_dimension($Ind_sql, 'sc_free_group_by', $Cada_dim, $rt, $format_dimensions[$Cada_dim]['reg'], $format_dimensions[$Cada_dim]['msk']);
              $SC_orig = $Cada_dim . "_orig";
              $SC_graf = "val_grafico_" . $Cada_dim;
              $$Cada_dim = $SC_prep['fmt'];
              $$SC_orig = $SC_prep['orig'];
              if ($Cada_dim == "wilaya") {
                  $this->Lookup->lookup_sc_free_group_by_wilaya($$Cada_dim,  $wilaya);
              }
              if ($Cada_dim == "commune") {
                  $this->Lookup->lookup_sc_free_group_by_commune($$Cada_dim,  $commune);
              }
              if ($Cada_dim == "maladie_confirmee_labo") {
                  $this->Lookup->lookup_sc_free_group_by_maladie_confirmee_labo($$Cada_dim,  $maladie_confirmee_labo);
              }
              if ($Cada_dim == "type_prelevement_nm_fk") {
                  $this->Lookup->lookup_sc_free_group_by_type_prelevement_nm_fk($$Cada_dim,  $type_prelevement_nm_fk);
              }
              if ($Cada_dim == "nationalite_nm_fk") {
                  $this->Lookup->lookup_sc_free_group_by_nationalite_nm_fk($$Cada_dim); 
              }
              if ($Cada_dim == "pays_origine") {
                  $this->Lookup->lookup_sc_free_group_by_pays_origine($$Cada_dim,  $pays_origine);
              }
              if (null === $$Cada_dim)
              {
                  $$Cada_dim = '';
              }
              if (null === $$SC_orig)
              {
                  $$SC_orig = '__SCNULL__';
              }
              $$SC_graf = $$Cada_dim;
              if ($Tem_estat_manual)
              {
                  $Format_tst = $this->Ini->Get_Gb_date_format($ind_qb, $Cada_dim);
                  if (!empty($Format_tst))
                  {
                      $val_sql  = $rt->fields[$Ind_sql];
                      if ($Format_tst == 'YYYYMMDDHHII')
                      {
                          $val_sql .= "-" . $rt->fields[$Ind_sql + 1] . "-" . $rt->fields[$Ind_sql + 2] . " " . $rt->fields[$Ind_sql + 3] . ":" . $rt->fields[$Ind_sql + 4];
                      }
                      if ($Format_tst == 'YYYYMMDDHH')
                      {
                          $val_sql .= "-" . $rt->fields[$Ind_sql + 1] . "-" . $rt->fields[$Ind_sql + 2] . " " . $rt->fields[$Ind_sql + 3];
                      }
                      if ($Format_tst == 'YYYYMMDD2')
                      {
                          $val_sql .= "-" . $rt->fields[$Ind_sql + 1] . "-" . $rt->fields[$Ind_sql + 2];
                      }
                      if ($Format_tst == 'YYYYMM')
                      {
                          $val_sql .= "-" . $rt->fields[$Ind_sql + 1];
                      }
                      if ($Format_tst == 'YYYYHH' || $Format_tst == 'YYYYDD' || $Format_tst == 'YYYYDAYNAME' || $Format_tst == 'YYYYWEEK' || $Format_tst == 'YYYYBIMONTHLY' || $Format_tst == 'YYYYQUARTER' || $Format_tst == 'YYYYFOURMONTHS' || $Format_tst == 'YYYYSEMIANNUAL')
                      {
                          $val_sql .= $rt->fields[$Ind_sql + 1];
                      }
                      if ($Format_tst == 'HHIISS')
                      {
                          $val_sql  = $rt->fields[$Ind_sql] . ":" . $rt->fields[$Ind_sql + 1] . ":" . $rt->fields[$Ind_sql + 2];
                      }
                      if ($Format_tst == 'HHII')
                      {
                          $val_sql  = $rt->fields[$Ind_sql] . ":" . $rt->fields[$Ind_sql + 1];
                      }
                      $Str_arg_sum = $this->Ini->Get_date_arg_sum($val_sql, $Format_tst, $cmp_sql_def[$Cada_dim], true);
                      $Str_arg_sql = ($Str_arg_sum == " is null") ? $cmp_sql_def[$Cada_dim] : $this->Ini->Get_sql_date_groupby($cmp_sql_def[$Cada_dim], $Format_tst);
                  }
                  elseif (isset($cmp_sql_tp_num[$Cada_dim]))
                  {
                      $Str_arg_sql = $cmp_sql_def[$Cada_dim];
                      $Str_arg_sum = " = " . $rt->fields[$Ind_sql];
                  }
                  else
                  {
                      $Str_arg_sql = $cmp_sql_def[$Cada_dim];
                      $Str_arg_sum = " = " . $this->Db->qstr($rt->fields[$Ind_sql]);
                  }
                  $sql_where .= (!empty($sql_where)) ? " and " : "";
                  $sql_where .= $Str_arg_sql . $Str_arg_sum;
              }
          }
          if ($Tem_estat_manual)
          {
              $where_ok = (empty($this->sc_where_atual)) ? " where " . $sql_where : $this->sc_where_atual . " and " . $sql_where;
              $vl_statistic = $this->Calc_statist_manual_sc_free_group_by($where_ok);
              foreach ($vl_statistic as $ind => $val)
              {
                  $rt->fields[$ind] = $val;
              }
          }
          $str_tot = "array_total_" . $cmp_tot;
          if (!isset($this->$str_tot))
          {
              $this->$str_tot = array();
          }
          $str_tot = "array_total_" . $cmp_tot . $arr_tots . "[0]";
          eval ('$this->' . $str_tot . ' = ' . $rt->fields[0] . ';');
          $str_tot = "array_total_" . $cmp_tot . $arr_tots . "[1]";
          eval('$this->' . $str_tot . ' = ' . $rt->fields[0] . ';');
          $str_grf = "val_grafico_" . $cmp_tot;
          $str_tot = "array_total_" . $cmp_tot . $arr_tots . "[2]";
          eval ('$this->' . $str_tot . ' = $' . $str_grf . ';');
          $str_tot = "array_total_" . $cmp_tot . $arr_tots . "[3]";
          $str_org = $cmp_tot . "_orig";
          eval ('$this->' . $str_tot . ' = $' . $str_org . ';');
          eval ('ksort($this->array_total_' . $cmp_tot . $arr_tots . ');');
          $rt->MoveNext();
      }
      $rt->Close();
   }

   //----- 
   function Calc_resumo_stat1($destino_resumo, $res_export=false)
   {
      global $nm_lang;
      $this->nm_data = new nm_data("fr");
      unset($_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['sql_tot_res']);
      if (isset($_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['campos_busca']) && !empty($_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['campos_busca']))
      { 
          $Busca_temp = $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['campos_busca'];
          if ($_SESSION['scriptcase']['charset'] != "UTF-8")
          {
              $Busca_temp = NM_conv_charset($Busca_temp, $_SESSION['scriptcase']['charset'], "UTF-8");
          }
          $this->id_fiche_ps = (isset($Busca_temp['id_fiche_ps'])) ? $Busca_temp['id_fiche_ps'] : ""; 
          $tmp_pos = (is_string($this->id_fiche_ps)) ? strpos($this->id_fiche_ps, "##@@") : false;
          if ($tmp_pos !== false && !is_array($this->id_fiche_ps))
          {
              $this->id_fiche_ps = substr($this->id_fiche_ps, 0, $tmp_pos);
          }
          $this->date_declaration = (isset($Busca_temp['date_declaration'])) ? $Busca_temp['date_declaration'] : ""; 
          $tmp_pos = (is_string($this->date_declaration)) ? strpos($this->date_declaration, "##@@") : false;
          if ($tmp_pos !== false && !is_array($this->date_declaration))
          {
              $this->date_declaration = substr($this->date_declaration, 0, $tmp_pos);
          }
          $this->date_declaration_2 = (isset($Busca_temp['date_declaration_input_2'])) ? $Busca_temp['date_declaration_input_2'] : ""; 
          $this->wilaya = (isset($Busca_temp['wilaya'])) ? $Busca_temp['wilaya'] : ""; 
          $tmp_pos = (is_string($this->wilaya)) ? strpos($this->wilaya, "##@@") : false;
          if ($tmp_pos !== false && !is_array($this->wilaya))
          {
              $this->wilaya = substr($this->wilaya, 0, $tmp_pos);
          }
      } 
      $this->sc_where_atual  = $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['where_pesq'];
      $ind_qb                = $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['SC_Ind_Groupby'];
      $cmp_sql_def   = array('maladie_confirmee_labo' => "maladie_confirmee_labo",'wilaya_malade' => "wilaya_malade",'date_resultats_labo' => "date_resultats_labo");
      $cmps_quebra_atual = array("maladie_confirmee_labo","wilaya_malade","date_resultats_labo");
      $ult_cmp_quebra_atual = $cmps_quebra_atual[(count($cmps_quebra_atual) - 1)];
      $arr_tots = "";
      $join     = "";
      $group    = "";
      $i_group  = 1;
      $cmps_gb  = "";
      $cmps_gb1 = "";
      $cmps_gb2 = "";
      $cmps_gbS = array();
      $ind_cmps = 1;
      $ind_alias = "1";
      $cmp_dim   = array();
      $all_group = array();
      foreach ($cmps_quebra_atual as $cmp_gb)
      {
          $Format_tst = $this->Ini->Get_Gb_date_format($ind_qb, $cmp_gb);
          if (!empty($Format_tst))
          {
              $Str_arg_sum = $this->Ini->Get_date_arg_sum($cmp_gb, $Format_tst, $cmp_sql_def[$cmp_gb], false, true);
              $Str_arg_sql = ($Str_arg_sum == " is null") ? $cmp_sql_def[$cmp_gb] : $this->Ini->Get_sql_date_groupby($cmp_sql_def[$cmp_gb], $Format_tst);
          }
          else
          {
              $Str_arg_sql = "";
              $Str_arg_sum = $cmp_sql_def[$cmp_gb] . " *sc# SC." . $cmp_sql_def[$cmp_gb];
          }
          $cmp_dim[$cmp_gb] = $ind_cmps;
          $temp = explode(" and ", $Str_arg_sum);
          foreach ($temp as $cada_parte)
          {
              $temp1 = explode("*sc#", $cada_parte);
              if (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_informix))
              {
                  $group .= (empty($group)) ? $i_group : "," . $i_group;
              }
              elseif (!in_array($Str_arg_sql . trim($temp1[0]), $all_group))
              {
                  $group .= (empty($group)) ? $Str_arg_sql . trim($temp1[0]) : "," . $Str_arg_sql . trim($temp1[0]);
                  $all_group[] = $Str_arg_sql . trim($temp1[0]);
              }
              $cmps_gb1 .= (empty($cmps_gb1)) ? $Str_arg_sql . trim($temp1[0]) : "," . $Str_arg_sql . trim($temp1[0]);
              $cmps_gb1 .= " as a_cmp_" .  $ind_alias;
              $cmps_gb2 .= (empty($cmps_gb2)) ? $Str_arg_sql . trim($temp1[0]) : "," . $Str_arg_sql . trim($temp1[0]);
              $cmps_gb2 .= " as b_cmp_" .  $ind_alias;
              $join     .= empty($join) ? "" : " and ";
              $join     .= " SC_sel1.a_cmp_" .  $ind_alias . " =  SC_sel2.b_cmp_" .  $ind_alias;
              $ind_cmps++;
              $ind_alias++;
              $i_group++;
          }
      }
      $ind_cmps  = 1;
      $ind_alias = "1";
      $cmp_dim   = array();
      foreach ($cmps_quebra_atual as $cmp_gb)
      {
          $arr_tots .= "[\$" . $cmp_gb . "_orig]";
          $Format_tst = $this->Ini->Get_Gb_date_format($ind_qb, $cmp_gb);
          if (!empty($Format_tst))
          {
              $Str_arg_sum = $this->Ini->Get_date_arg_sum($cmp_gb, $Format_tst, $cmp_sql_def[$cmp_gb], false, true);
              $Str_arg_sql = ($Str_arg_sum == " is null") ? $cmp_sql_def[$cmp_gb] : $this->Ini->Get_sql_date_groupby($cmp_sql_def[$cmp_gb], $Format_tst);
          }
          else
          {
              $Str_arg_sql = "";
              $Str_arg_sum = $cmp_sql_def[$cmp_gb] . " *sc# SC." . $cmp_sql_def[$cmp_gb];
          }
          $cmp_dim[$cmp_gb] = $ind_cmps;
          $temp = explode(" and ", $Str_arg_sum);
          foreach ($temp as $cada_parte)
          {
              $temp1 = explode("*sc#", $cada_parte);
              $cmps_gb  .= (empty($cmps_gb)) ? "a_cmp_" .  $ind_alias : "," . "a_cmp_" .  $ind_alias;
              $cmps_gbS['a_cmp_' . $ind_alias] = $Str_arg_sql . trim($temp1[0]);
              $ind_cmps++;
              $ind_alias++;
          }
          $this->Res_Totaliza_stat1($ind_qb, $cmp_gb, $arr_tots, $group, $join, $cmps_gb, $cmps_gb1, $cmps_gb2, $cmps_gbS, $cmp_dim, $cmps_quebra_atual, $cmp_sql_def, $res_export);
      }
      $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['arr_total'] = array();
      foreach ($cmps_quebra_atual as $cmp_gb)
      {
          $Arr_tot_name = "array_total_" . $cmp_gb;
          $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['arr_total'][$cmp_gb] = $this->$Arr_tot_name;
      }
   }

   function Res_Totaliza_stat1($ind_qb, $cmp_tot, $arr_tots, $group, $join, $cmps_quebras, $cmps_quebras1, $cmps_quebras2, $cmps_quebrasS, $Cmp_dim, $cmps_quebra_atual, $cmp_sql_def, $res_export)
   {
      $sc_having = ((isset($parms_sub_sel['having']))) ? "  having " . $parms_sub_sel['having'] : "";
      $Tem_estat_manual = false;
      $where_ok = $this->sc_where_atual;
      $cmp_sql_tp_num = array('id_fiche_ps' => 'N','age_malade' => 'N');
      if (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_access))
      { 
         $cmd_simp = "select count(*)#@#cmps_quebras#@# from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp " . $where_ok;
         $comando  = "select count(*)#@#cmps_quebras#@# from (";
         $comando .= "select " . $cmps_quebras1 . " from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp1 " . $where_ok . ") SC_sel1 INNER JOIN (";
         $comando .= "select " . $cmps_quebras2 . " from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp2 " . $where_ok . " group by " . $group . $sc_having . ") SC_sel2 ";
         $comando .= " ON " . $join;
      } 
      elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_sybase))
      { 
         $cmd_simp = "select count(*)#@#cmps_quebras#@# from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp " . $where_ok;
         $comando  = "select count(*)#@#cmps_quebras#@# from (";
         $comando .= "select " . $cmps_quebras1 . " from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp1 " . $where_ok . ") SC_sel1 INNER JOIN (";
         $comando .= "select " . $cmps_quebras2 . " from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp2 " . $where_ok . " group by " . $group . $sc_having . ") SC_sel2 ";
         $comando .= " ON " . $join;
      } 
      elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_mssql))
      { 
         $cmd_simp = "select count(*)#@#cmps_quebras#@# from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp " . $where_ok;
         $comando  = "select count(*)#@#cmps_quebras#@# from (";
         $comando .= "select " . $cmps_quebras1 . " from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp1 " . $where_ok . ") SC_sel1 INNER JOIN (";
         $comando .= "select " . $cmps_quebras2 . " from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp2 " . $where_ok . " group by " . $group . $sc_having . ") SC_sel2 ";
         $comando .= " ON " . $join;
      } 
      elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_oracle))
      { 
         $cmd_simp = "select count(*)#@#cmps_quebras#@# from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp " . $where_ok;
         $comando  = "select count(*)#@#cmps_quebras#@# from (";
         $comando .= "select " . $cmps_quebras1 . " from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp1 " . $where_ok . ") SC_sel1 INNER JOIN (";
         $comando .= "select " . $cmps_quebras2 . " from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp2 " . $where_ok . " group by " . $group . $sc_having . ") SC_sel2 ";
         $comando .= " ON " . $join;
      } 
      elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_ibase))
      { 
         $cmd_simp = "select count(*)#@#cmps_quebras#@# from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp " . $where_ok;
         $comando  = "select count(*)#@#cmps_quebras#@# from (";
         $comando .= "select " . $cmps_quebras1 . " from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp1 " . $where_ok . ") SC_sel1 INNER JOIN (";
         $comando .= "select " . $cmps_quebras2 . " from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp2 " . $where_ok . " group by " . $group . $sc_having . ") SC_sel2 ";
         $comando .= " ON " . $join;
      } 
      else 
      { 
         $cmd_simp = "select count(*)#@#cmps_quebras#@# from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp " . $where_ok;
         $comando  = "select count(*)#@#cmps_quebras#@# from (";
         $comando .= "select " . $cmps_quebras1 . " from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp1 " . $where_ok . ") SC_sel1 INNER JOIN (";
         $comando .= "select " . $cmps_quebras2 . " from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) sc_sel_esp2 " .  $where_ok . " group by " . $group . $sc_having . ") SC_sel2 ";
         $comando .= " ON " . $join;
      } 
      if (!isset($_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['sql_tot_res']))
      {
         $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['sql_tot_res'] = str_replace("#@#cmps_quebras#@#", "", $comando);
      }
      $comando  = str_replace("#@#cmps_quebras#@#", "," . $cmps_quebras, $comando);
      $comando .= " group by " . $cmps_quebras . " order by " .  $cmps_quebras;
      if (!isset($_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['Res_search_metric_use']) || empty($_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['Res_search_metric_use']))
      {
          $comando = $cmd_simp;
          $cmps_S  = "";
          foreach ($cmps_quebrasS as $alias => $sql)
          {
              $cmps_S .= empty($cmps_S) ? $sql : ", " . $sql;
          }
          $comando = str_replace("#@#cmps_quebras#@#", "," . $cmps_S, $comando);
          $order_group = "";
          foreach ($cmps_quebrasS as $alias => $cada_tst)
          {
              $cada_tst = trim($cada_tst);
              $pos = strpos(" " . $order_group, " " . $cada_tst);
              if ($pos === false)
              {
                  $order_group .= (!empty($order_group)) ? ", " . $cada_tst : $cada_tst;
              }
          }
          $comando .= " group by " . $order_group . " order by " .  $order_group;
      }
      $comando  = $this->Ajust_statistic($comando);
      $_SESSION['scriptcase']['sc_sql_ult_comando'] = $comando;
      $_SESSION['scriptcase']['sc_sql_ult_conexao'] = '';
      if (!$rt = $this->Db->Execute($comando))
      {
         $this->Erro->mensagem(__FILE__, __LINE__, "banco", $this->Ini->Nm_lang['lang_errm_dber'], $this->Db->ErrorMsg()); 
         exit;
      }
      $format_dimensions = array();
      $format_dimensions['maladie_confirmee_labo']['reg'] = "S";
      $format_dimensions['maladie_confirmee_labo']['msk'] = "";
      $format_dimensions['wilaya_malade']['reg'] = "S";
      $format_dimensions['wilaya_malade']['msk'] = "";
      $format_dimensions['date_resultats_labo']['reg'] = "S";
      $format_dimensions['date_resultats_labo']['msk'] = "";
      while (!$rt->EOF)
      {
          $sql_where = "";
          foreach ($Cmp_dim as $Cada_dim => $Ind_sql)
          {
              $prep_look  = $Cada_dim . "_SC_look";
              $$prep_look = $rt->fields[$Ind_sql];
              $SC_prep = $this->Ini->Get_format_dimension($Ind_sql, 'stat1', $Cada_dim, $rt, $format_dimensions[$Cada_dim]['reg'], $format_dimensions[$Cada_dim]['msk']);
              $SC_orig = $Cada_dim . "_orig";
              $SC_graf = "val_grafico_" . $Cada_dim;
              $$Cada_dim = $SC_prep['fmt'];
              $$SC_orig = $SC_prep['orig'];
              if ($Cada_dim == "maladie_confirmee_labo") {
                  $this->Lookup->lookup_stat1_maladie_confirmee_labo($$Cada_dim,  $maladie_confirmee_labo);
              }
              if ($Cada_dim == "wilaya_malade") {
                  $this->Lookup->lookup_stat1_wilaya_malade($$Cada_dim,  $wilaya_malade);
              }
              if (null === $$Cada_dim)
              {
                  $$Cada_dim = '';
              }
              if (null === $$SC_orig)
              {
                  $$SC_orig = '__SCNULL__';
              }
              $$SC_graf = $$Cada_dim;
              if ($Tem_estat_manual)
              {
                  $Format_tst = $this->Ini->Get_Gb_date_format($ind_qb, $Cada_dim);
                  if (!empty($Format_tst))
                  {
                      $val_sql  = $rt->fields[$Ind_sql];
                      if ($Format_tst == 'YYYYMMDDHHII')
                      {
                          $val_sql .= "-" . $rt->fields[$Ind_sql + 1] . "-" . $rt->fields[$Ind_sql + 2] . " " . $rt->fields[$Ind_sql + 3] . ":" . $rt->fields[$Ind_sql + 4];
                      }
                      if ($Format_tst == 'YYYYMMDDHH')
                      {
                          $val_sql .= "-" . $rt->fields[$Ind_sql + 1] . "-" . $rt->fields[$Ind_sql + 2] . " " . $rt->fields[$Ind_sql + 3];
                      }
                      if ($Format_tst == 'YYYYMMDD2')
                      {
                          $val_sql .= "-" . $rt->fields[$Ind_sql + 1] . "-" . $rt->fields[$Ind_sql + 2];
                      }
                      if ($Format_tst == 'YYYYMM')
                      {
                          $val_sql .= "-" . $rt->fields[$Ind_sql + 1];
                      }
                      if ($Format_tst == 'YYYYHH' || $Format_tst == 'YYYYDD' || $Format_tst == 'YYYYDAYNAME' || $Format_tst == 'YYYYWEEK' || $Format_tst == 'YYYYBIMONTHLY' || $Format_tst == 'YYYYQUARTER' || $Format_tst == 'YYYYFOURMONTHS' || $Format_tst == 'YYYYSEMIANNUAL')
                      {
                          $val_sql .= $rt->fields[$Ind_sql + 1];
                      }
                      if ($Format_tst == 'HHIISS')
                      {
                          $val_sql  = $rt->fields[$Ind_sql] . ":" . $rt->fields[$Ind_sql + 1] . ":" . $rt->fields[$Ind_sql + 2];
                      }
                      if ($Format_tst == 'HHII')
                      {
                          $val_sql  = $rt->fields[$Ind_sql] . ":" . $rt->fields[$Ind_sql + 1];
                      }
                      $Str_arg_sum = $this->Ini->Get_date_arg_sum($val_sql, $Format_tst, $cmp_sql_def[$Cada_dim], true);
                      $Str_arg_sql = ($Str_arg_sum == " is null") ? $cmp_sql_def[$Cada_dim] : $this->Ini->Get_sql_date_groupby($cmp_sql_def[$Cada_dim], $Format_tst);
                  }
                  elseif (isset($cmp_sql_tp_num[$Cada_dim]))
                  {
                      $Str_arg_sql = $cmp_sql_def[$Cada_dim];
                      $Str_arg_sum = " = " . $rt->fields[$Ind_sql];
                  }
                  else
                  {
                      $Str_arg_sql = $cmp_sql_def[$Cada_dim];
                      $Str_arg_sum = " = " . $this->Db->qstr($rt->fields[$Ind_sql]);
                  }
                  $sql_where .= (!empty($sql_where)) ? " and " : "";
                  $sql_where .= $Str_arg_sql . $Str_arg_sum;
              }
          }
          if ($Tem_estat_manual)
          {
              $where_ok = (empty($this->sc_where_atual)) ? " where " . $sql_where : $this->sc_where_atual . " and " . $sql_where;
              $vl_statistic = $this->Calc_statist_manual_stat1($where_ok);
              foreach ($vl_statistic as $ind => $val)
              {
                  $rt->fields[$ind] = $val;
              }
          }
          $str_tot = "array_total_" . $cmp_tot;
          if (!isset($this->$str_tot))
          {
              $this->$str_tot = array();
          }
          $str_tot = "array_total_" . $cmp_tot . $arr_tots . "[0]";
          eval ('$this->' . $str_tot . ' = ' . $rt->fields[0] . ';');
          $str_tot = "array_total_" . $cmp_tot . $arr_tots . "[1]";
          eval('$this->' . $str_tot . ' = ' . $rt->fields[0] . ';');
          $str_grf = "val_grafico_" . $cmp_tot;
          $str_tot = "array_total_" . $cmp_tot . $arr_tots . "[2]";
          eval ('$this->' . $str_tot . ' = $' . $str_grf . ';');
          $str_tot = "array_total_" . $cmp_tot . $arr_tots . "[3]";
          $str_org = $cmp_tot . "_orig";
          eval ('$this->' . $str_tot . ' = $' . $str_org . ';');
          eval ('ksort($this->array_total_' . $cmp_tot . $arr_tots . ');');
          $rt->MoveNext();
      }
      $rt->Close();
   }
   //---- 
   function quebra_geral_sc_free_group_by($res_limit=false, $res_export=false)
   {
      global $nada, $nm_lang ;
      if ($_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['contr_total_geral'] == "OK") 
      { 
          return; 
      } 
      $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['tot_geral'] = array() ;  
    if (!$res_export && $res_limit && isset($_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['sql_tot_res']))
    {
        $nm_comando = $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['sql_tot_res'];
    }
    else
    {
      if (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_mssql))
      { 
          $nm_comando = "select count(*) from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) nm_sel_esp " . $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['where_pesq']; 
      } 
      else 
      { 
          $nm_comando = "select count(*) from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) nm_sel_esp " . $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['where_pesq']; 
      } 
    }
      $_SESSION['scriptcase']['sc_sql_ult_comando'] = $nm_comando;
      $_SESSION['scriptcase']['sc_sql_ult_conexao'] = '';
      if (!$rt = $this->Db->Execute($nm_comando)) 
      { 
         $this->Erro->mensagem (__FILE__, __LINE__, "banco", $this->Ini->Nm_lang['lang_errm_dber'], $this->Db->ErrorMsg()); 
         exit ; 
      }
      $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['tot_geral'][0] = "" . $this->Ini->Nm_lang['lang_msgs_totl'] . ""; 
      $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['tot_geral'][1] = $rt->fields[0] ; 
      $rt->Close(); 
      $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['contr_total_geral'] = "OK";
   } 

   //---- 
   function quebra_geral_stat1($res_limit=false, $res_export=false)
   {
      global $nada, $nm_lang ;
      if ($_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['contr_total_geral'] == "OK") 
      { 
          return; 
      } 
      $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['tot_geral'] = array() ;  
    if (!$res_export && $res_limit && isset($_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['sql_tot_res']))
    {
        $nm_comando = $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['sql_tot_res'];
    }
    else
    {
      if (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_mssql))
      { 
          $nm_comando = "select count(*) from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) nm_sel_esp " . $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['where_pesq']; 
      } 
      else 
      { 
          $nm_comando = "select count(*) from (SELECT     id_fiche_ps,          date_declaration,     wilaya,     commune,     \"structure\",          maladie_suspectee_nm_fk,         date_naissance_malade,     age_malade,     sexe_nm_fk,         wilaya_malade,     commune_malade,             user_fk,     validator_fk,         evolution_nm_fk,     symptomes,        maladie_suspectee_by_ia,     date_validation,     user_labo_fk,     date_confirmation_labo,     maladie_confirmee_labo,     statut_maladie,         date_prelevements,     date_reception_prelevement,     date_resultats_labo,     type_prelevement_nm_fk,     resultats_labo,     labo_destination,     nin,     commentaires_labo,     commentaires_semep,     statut_fiche,     pays_origine,     nationalite_nm_fk FROM     \"public\".fiche_prof_sante  WHERE (labo_destination= '" . $_SESSION['usr_fac'] . "')  and  (((fiche_prof_sante.date_declaration)::date >= (('now'::text)::date - (concat(( SELECT sec_settings.set_value                    FROM sec_settings                   WHERE ((sec_settings.set_name)::text = 'stat_observation_period'::text)), ' days'))::interval))  AND (fiche_prof_sante.deleted IS DISTINCT FROM 'O'::bpchar)) ) nm_sel_esp " . $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['where_pesq']; 
      } 
    }
      $_SESSION['scriptcase']['sc_sql_ult_comando'] = $nm_comando;
      $_SESSION['scriptcase']['sc_sql_ult_conexao'] = '';
      if (!$rt = $this->Db->Execute($nm_comando)) 
      { 
         $this->Erro->mensagem (__FILE__, __LINE__, "banco", $this->Ini->Nm_lang['lang_errm_dber'], $this->Db->ErrorMsg()); 
         exit ; 
      }
      $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['tot_geral'][0] = "" . $this->Ini->Nm_lang['lang_msgs_totl'] . ""; 
      $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['tot_geral'][1] = $rt->fields[0] ; 
      $rt->Close(); 
      $_SESSION['sc_session'][$this->Ini->sc_page]['grid_fiche_prof_sante_labo_stat']['contr_total_geral'] = "OK";
   } 

   //-----  wilaya
   function quebra_wilaya_sc_free_group_by($wilaya, $Where_qb, $Cmp_Name) 
   {
      $Var_name_gb = "SC_tot_" . $Cmp_Name;
      global $$Var_name_gb;
      $tot_wilaya = array() ;  
      $tot_wilaya[0] = $wilaya ; 
   }
   //-----  commune
   function quebra_commune_sc_free_group_by($commune, $Where_qb, $Cmp_Name) 
   {
      $Var_name_gb = "SC_tot_" . $Cmp_Name;
      global $$Var_name_gb;
      $tot_commune = array() ;  
      $tot_commune[0] = $commune ; 
   }
   //-----  date_declaration
   function quebra_date_declaration_sc_free_group_by($date_declaration, $Where_qb, $Cmp_Name) 
   {
      $Var_name_gb = "SC_tot_" . $Cmp_Name;
      global $$Var_name_gb;
      $tot_date_declaration = array() ;  
      $tot_date_declaration[0] = $date_declaration ; 
   }
   //-----  maladie_confirmee_labo
   function quebra_maladie_confirmee_labo_sc_free_group_by($maladie_confirmee_labo, $Where_qb, $Cmp_Name) 
   {
      $Var_name_gb = "SC_tot_" . $Cmp_Name;
      global $$Var_name_gb;
      $tot_maladie_confirmee_labo = array() ;  
      $tot_maladie_confirmee_labo[0] = $maladie_confirmee_labo ; 
   }
   //-----  date_confirmation_labo
   function quebra_date_confirmation_labo_sc_free_group_by($date_confirmation_labo, $Where_qb, $Cmp_Name) 
   {
      $Var_name_gb = "SC_tot_" . $Cmp_Name;
      global $$Var_name_gb;
      $tot_date_confirmation_labo = array() ;  
      $tot_date_confirmation_labo[0] = $date_confirmation_labo ; 
   }
   //-----  type_prelevement_nm_fk
   function quebra_type_prelevement_nm_fk_sc_free_group_by($type_prelevement_nm_fk, $Where_qb, $Cmp_Name) 
   {
      $Var_name_gb = "SC_tot_" . $Cmp_Name;
      global $$Var_name_gb;
      $tot_type_prelevement_nm_fk = array() ;  
      $tot_type_prelevement_nm_fk[0] = $type_prelevement_nm_fk ; 
   }
   //-----  nationalite_nm_fk
   function quebra_nationalite_nm_fk_sc_free_group_by($nationalite_nm_fk, $Where_qb, $Cmp_Name) 
   {
      $Var_name_gb = "SC_tot_" . $Cmp_Name;
      global $$Var_name_gb;
      $tot_nationalite_nm_fk = array() ;  
      $tot_nationalite_nm_fk[0] = $nationalite_nm_fk ; 
   }
   //-----  pays_origine
   function quebra_pays_origine_sc_free_group_by($pays_origine, $Where_qb, $Cmp_Name) 
   {
      $Var_name_gb = "SC_tot_" . $Cmp_Name;
      global $$Var_name_gb;
      $tot_pays_origine = array() ;  
      $tot_pays_origine[0] = $pays_origine ; 
   }
   //-----  maladie_confirmee_labo
   function quebra_maladie_confirmee_labo_stat1($maladie_confirmee_labo, $arg_sum_maladie_confirmee_labo) 
   {
      global $tot_maladie_confirmee_labo;
      $tot_maladie_confirmee_labo = array() ;  
      $tot_maladie_confirmee_labo[0] = $maladie_confirmee_labo ; 
   }
   //-----  wilaya_malade
   function quebra_wilaya_malade_stat1($maladie_confirmee_labo, $wilaya_malade, $arg_sum_maladie_confirmee_labo, $arg_sum_wilaya_malade) 
   {
      global $tot_wilaya_malade;
      $tot_wilaya_malade = array() ;  
      $tot_wilaya_malade[0] = $wilaya_malade ; 
   }
   //-----  date_resultats_labo
   function quebra_date_resultats_labo_stat1($maladie_confirmee_labo, $wilaya_malade, $date_resultats_labo, $arg_sum_maladie_confirmee_labo, $arg_sum_wilaya_malade, $arg_sum_date_resultats_labo) 
   {
      global $tot_date_resultats_labo;
      $tot_date_resultats_labo = array() ;  
      $tot_date_resultats_labo[0] = $date_resultats_labo ; 
   }
   function Ajust_statistic($comando)
   {
      if ((isset($this->Ini->nm_bases_vfp) && in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_vfp)) || (isset($this->Ini->nm_bases_odbc) && in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_odbc)))
      {
          $comando = str_replace(array('count(distinct ','varp(','stdevp(','variance(','stddev('), array('sum(','sum(','sum(','sum(','sum('), $comando);
      }
      if ($this->Ini->nm_tp_variance == "P")
      {
          if (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_access))
          { 
              $comando = str_replace(array('count(distinct ','varp(','stdevp('), array('count(','var(','stdev('), $comando);
          }
          elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_sqlite) && $this->Ini->sqlite_version == "old")
          {
              $comando = str_replace(array('variance(','stddev('), array('sum(','sum('), $comando);
          }
          elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_ibase) && $this->Ini->Ibase_version == "old")
          {
              $comando = str_replace(array('variance(','stddev('), array('sum(','sum('), $comando);
          }
          elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_postgres))
          {
              $comando = str_replace(array('variance(','stddev('), array('var_pop(','stddev_pop('), $comando);
          }
          elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_ibase))
          {
                  $comando = str_replace(array('variance(','stddev('), array('var_pop(','stddev_pop('), $comando);
          }
          elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_informix))
          {
              $comando = str_replace(array('variance(','stddev('), array('sum(','sum('), $comando);
          }
          elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_sybase))
          {
              $comando = str_replace(array('variance(','stddev('), array('var_pop(','stddev_pop('), $comando);
          }
          elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_oracle))
          {
              $comando = str_replace(array('variance(','stddev('), array('var_pop(','stddev_pop('), $comando);
          }
      }
      if ($this->Ini->nm_tp_variance == "A")
      {
          if (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_access))
          { 
              $comando = str_replace(array('count(distinct ','varp(','stdevp('), array('count(','var(','stdev('), $comando);
          }
          elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_sqlite) && $this->Ini->sqlite_version == "old")
          {
              $comando = str_replace(array('variance(','stddev('), array('sum(','sum('), $comando);
          }
          elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_mysql))
          {
              $comando = str_replace(array('variance(','stddev('), array('var_samp(','stddev_samp('), $comando);
          }
          elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_postgres))
          {
              $comando = str_replace(array('variance(','stddev('), array('var_samp(','stddev_samp('), $comando);
          }
          elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_ibase) && $this->Ini->Ibase_version == "old")
          {
              $comando = str_replace(array('variance(','stddev('), array('sum(','sum('), $comando);
          }
          elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_ibase))
          {
                  $comando = str_replace(array('variance(','stddev('), array('var_samp(','stddev_samp('), $comando);
          }
          elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_oracle))
          {
              $comando = str_replace(array('variance(','stddev('), array('var_samp(','stddev_samp('), $comando);
          }
          elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_mssql))
          { 
              $comando = str_replace(array('varp(','stdevp('), array('var(','stdev('), $comando);
          }
          elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_informix))
          {
              $comando = str_replace('stddev(', 'stdev(', $comando);
          }
          elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_db2))
          {
              $comando = str_replace(array('variance(','stddev('), array('variance_samp(','stddev_samp('), $comando);
          }
          elseif (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_sybase))
          {
              $comando = str_replace(array('variance(','stddev('), array('var_samp(','stddev_samp('), $comando);
          }
      }
      return $comando;
   }

   function nm_conv_data_db($dt_in, $form_in, $form_out)
   {
       $dt_out = $dt_in;
       if (strtoupper($form_in) == "DB_FORMAT") {
           if ($dt_out == "null" || $dt_out == "")
           {
               $dt_out = "";
               return $dt_out;
           }
           $form_in = "AAAA-MM-DD";
       }
       if (strtoupper($form_out) == "DB_FORMAT") {
           if (empty($dt_out))
           {
               $dt_out = "null";
               return $dt_out;
           }
           $form_out = "AAAA-MM-DD";
       }
       if (strtoupper($form_out) == "SC_FORMAT_REGION") {
           $this->nm_data->SetaData($dt_in, strtoupper($form_in));
           $prep_out  = (strpos(strtolower($form_in), "dd") !== false) ? "dd" : "";
           $prep_out .= (strpos(strtolower($form_in), "mm") !== false) ? "mm" : "";
           $prep_out .= (strpos(strtolower($form_in), "aa") !== false) ? "aaaa" : "";
           $prep_out .= (strpos(strtolower($form_in), "yy") !== false) ? "aaaa" : "";
           return $this->nm_data->FormataSaida($this->nm_data->FormatRegion("DT", $prep_out));
       }
       else {
           nm_conv_form_data($dt_out, $form_in, $form_out);
           return $dt_out;
       }
   }
   function nm_gera_mask(&$nm_campo, $nm_mask)
   { 
      $trab_campo = $nm_campo;
      $trab_mask  = $nm_mask;
      $tam_campo  = strlen($nm_campo);
      $trab_saida = "";
      $str_highlight_ini = "";
      $str_highlight_fim = "";
      if(substr($nm_campo, 0, 23) == '<div class="highlight">' && substr($nm_campo, -6) == '</div>')
      {
           $str_highlight_ini = substr($nm_campo, 0, 23);
           $str_highlight_fim = substr($nm_campo, -6);

           $trab_campo = substr($nm_campo, 23, -6);
           $tam_campo  = strlen($trab_campo);
      }      $mask_num = false;
      for ($x=0; $x < strlen($trab_mask); $x++)
      {
          if (substr($trab_mask, $x, 1) == "#")
          {
              $mask_num = true;
              break;
          }
      }
      if ($mask_num )
      {
          $ver_duas = explode(";", $trab_mask);
          if (isset($ver_duas[1]) && !empty($ver_duas[1]))
          {
              $cont1 = count(explode("#", $ver_duas[0])) - 1;
              $cont2 = count(explode("#", $ver_duas[1])) - 1;
              if ($tam_campo >= $cont2)
              {
                  $trab_mask = $ver_duas[1];
              }
              else
              {
                  $trab_mask = $ver_duas[0];
              }
          }
          $tam_mask = strlen($trab_mask);
          $xdados = 0;
          for ($x=0; $x < $tam_mask; $x++)
          {
              if (substr($trab_mask, $x, 1) == "#" && $xdados < $tam_campo)
              {
                  $trab_saida .= substr($trab_campo, $xdados, 1);
                  $xdados++;
              }
              elseif ($xdados < $tam_campo)
              {
                  $trab_saida .= substr($trab_mask, $x, 1);
              }
          }
          if ($xdados < $tam_campo)
          {
              $trab_saida .= substr($trab_campo, $xdados);
          }
          $nm_campo = $str_highlight_ini . $trab_saida . $str_highlight_ini;
          return;
      }
      for ($ix = strlen($trab_mask); $ix > 0; $ix--)
      {
           $char_mask = substr($trab_mask, $ix - 1, 1);
           if ($char_mask != "x" && $char_mask != "z")
           {
               $trab_saida = $char_mask . $trab_saida;
           }
           else
           {
               if ($tam_campo != 0)
               {
                   $trab_saida = substr($trab_campo, $tam_campo - 1, 1) . $trab_saida;
                   $tam_campo--;
               }
               else
               {
                   $trab_saida = "0" . $trab_saida;
               }
           }
      }
      if ($tam_campo != 0)
      {
          $trab_saida = substr($trab_campo, 0, $tam_campo) . $trab_saida;
          $trab_mask  = str_repeat("z", $tam_campo) . $trab_mask;
      }
   
      $iz = 0; 
      for ($ix = 0; $ix < strlen($trab_mask); $ix++)
      {
           $char_mask = substr($trab_mask, $ix, 1);
           if ($char_mask != "x" && $char_mask != "z")
           {
               if ($char_mask == "." || $char_mask == ",")
               {
                   $trab_saida = substr($trab_saida, 0, $iz) . substr($trab_saida, $iz + 1);
               }
               else
               {
                   $iz++;
               }
           }
           elseif ($char_mask == "x" || substr($trab_saida, $iz, 1) != "0")
           {
               $ix = strlen($trab_mask) + 1;
           }
           else
           {
               $trab_saida = substr($trab_saida, 0, $iz) . substr($trab_saida, $iz + 1);
           }
      }
      $nm_campo = $str_highlight_ini . $trab_saida . $str_highlight_ini;
   } 
}

?>
