<?php

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

   var $nm_data;

   //----- 
   function Analyze_wastetransactions_total($sc_page)
   {
      $this->sc_page = $sc_page;
      $this->nm_data = new nm_data("el");
      if (isset($_SESSION['sc_session'][$this->sc_page]['Analyze_wastetransactions']['campos_busca']) && !empty($_SESSION['sc_session'][$this->sc_page]['Analyze_wastetransactions']['campos_busca']))
      { 
          $Busca_temp = $_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['campos_busca'];
          if ($_SESSION['scriptcase']['charset'] != "UTF-8")
          {
              $Busca_temp = NM_conv_charset($Busca_temp, $_SESSION['scriptcase']['charset'], "UTF-8");
          }
          $this->wtid = $Busca_temp['wtid']; 
          $tmp_pos = strpos($this->wtid, "##@@");
          if ($tmp_pos !== false)
          {
              $this->wtid = substr($this->wtid, 0, $tmp_pos);
          }
          $wtid_2 = $Busca_temp['wtid_input_2']; 
          $this->wtid_2 = $Busca_temp['wtid_input_2']; 
          $this->wtprofilid = $Busca_temp['wtprofilid']; 
          $tmp_pos = strpos($this->wtprofilid, "##@@");
          if ($tmp_pos !== false)
          {
              $this->wtprofilid = substr($this->wtprofilid, 0, $tmp_pos);
          }
          $wtprofilid_2 = $Busca_temp['wtprofilid_input_2']; 
          $this->wtprofilid_2 = $Busca_temp['wtprofilid_input_2']; 
          $this->wtrequestdate = $Busca_temp['wtrequestdate']; 
          $tmp_pos = strpos($this->wtrequestdate, "##@@");
          if ($tmp_pos !== false)
          {
              $this->wtrequestdate = substr($this->wtrequestdate, 0, $tmp_pos);
          }
          $wtrequestdate_2 = $Busca_temp['wtrequestdate_input_2']; 
          $this->wtrequestdate_2 = $Busca_temp['wtrequestdate_input_2']; 
          $this->wtproducerbranchid = $Busca_temp['wtproducerbranchid']; 
          $tmp_pos = strpos($this->wtproducerbranchid, "##@@");
          if ($tmp_pos !== false)
          {
              $this->wtproducerbranchid = substr($this->wtproducerbranchid, 0, $tmp_pos);
          }
          $wtproducerbranchid_2 = $Busca_temp['wtproducerbranchid_input_2']; 
          $this->wtproducerbranchid_2 = $Busca_temp['wtproducerbranchid_input_2']; 
      } 
   }

   //---- 
   function quebra_geral()
   {
      global $nada, $nm_lang , $wtproducerbranchid, $wtproducerperiferia, $wtproducerewc;
      if ($_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['contr_total_geral'] == "OK") 
      { 
          return; 
      } 
      $_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['tot_geral'] = array() ;  
      $nm_comando = "select count(*), sum(wtProducerQty) as sum_wtproducerqty, avg(wtProducerQty) as avg_wtproducerqty, max(wtProducerQty) as max_wtproducerqty, min(wtProducerQty) as min_wtproducerqty, sum(wtFDestQty) as sum_wtfdestqty, avg(wtFDestQty) as avg_wtfdestqty, max(wtFDestQty) as max_wtfdestqty, min(wtFDestQty) as min_wtfdestqty from " . $this->Ini->nm_tabela . " " . $_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['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]['Analyze_wastetransactions']['tot_geral'][0] = "" . $this->Ini->Nm_lang['lang_msgs_totl'] . ""; 
      $_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['tot_geral'][1] = $rt->fields[0] ; 
      $rt->fields[1] = str_replace(",", ".", $rt->fields[1]);
      $rt->fields[1] = (string)$rt->fields[1]; 
      $_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['tot_geral'][2] = $rt->fields[1]; 
      $rt->fields[2] = str_replace(",", ".", $rt->fields[2]);
      $rt->fields[2] = (string)$rt->fields[2]; 
      $_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['tot_geral'][3] = $rt->fields[2]; 
      $rt->fields[3] = str_replace(",", ".", $rt->fields[3]);
      $rt->fields[3] = (string)$rt->fields[3]; 
      $_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['tot_geral'][4] = $rt->fields[3]; 
      $rt->fields[4] = str_replace(",", ".", $rt->fields[4]);
      $rt->fields[4] = (string)$rt->fields[4]; 
      $_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['tot_geral'][5] = $rt->fields[4]; 
      $rt->fields[5] = str_replace(",", ".", $rt->fields[5]);
      $rt->fields[5] = (string)$rt->fields[5]; 
      $_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['tot_geral'][6] = $rt->fields[5]; 
      $rt->fields[6] = str_replace(",", ".", $rt->fields[6]);
      $rt->fields[6] = (string)$rt->fields[6]; 
      $_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['tot_geral'][7] = $rt->fields[6]; 
      $rt->fields[7] = str_replace(",", ".", $rt->fields[7]);
      $rt->fields[7] = (string)$rt->fields[7]; 
      $_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['tot_geral'][8] = $rt->fields[7]; 
      $rt->fields[8] = str_replace(",", ".", $rt->fields[8]);
      $rt->fields[8] = (string)$rt->fields[8]; 
      $_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['tot_geral'][9] = $rt->fields[8]; 
      $rt->Close(); 
      $_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['contr_total_geral'] = "OK";
   } 

   //-----  wtrequestdate
   function quebra_wtrequestdate_sc_free_group_by($wtrequestdate, $Where_qb) 
   {
      global $tot_wtrequestdate , $wtproducerbranchid, $wtproducerperiferia, $wtproducerewc, $Sc_groupby_wtrequestdate;  
      $tot_wtrequestdate = array() ;  
      $nm_comando = "select count(*), sum(wtProducerQty) as sum_wtproducerqty, avg(wtProducerQty) as avg_wtproducerqty, max(wtProducerQty) as max_wtproducerqty, min(wtProducerQty) as min_wtproducerqty, sum(wtFDestQty) as sum_wtfdestqty, avg(wtFDestQty) as avg_wtfdestqty, max(wtFDestQty) as max_wtfdestqty, min(wtFDestQty) as min_wtfdestqty from " . $this->Ini->nm_tabela . " " . $_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['where_pesq']; 
      if (empty($_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['where_pesq'])) 
      { 
         $nm_comando .= " where " . $Where_qb ; 
      } 
      else 
      { 
         $nm_comando .= " and " . $Where_qb ; 
      } 
      $_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 ; 
      }  
      $tot_wtrequestdate[0] = NM_encode_input($wtrequestdate) ; 
      $tot_wtrequestdate[1] = $rt->fields[0] ; 
      $rt->fields[1] = str_replace(",", ".", $rt->fields[1]);
      $tot_wtrequestdate[2] = (string)$rt->fields[1]; 
      $rt->fields[2] = str_replace(",", ".", $rt->fields[2]);
      $tot_wtrequestdate[3] = (string)$rt->fields[2]; 
      $rt->fields[3] = str_replace(",", ".", $rt->fields[3]);
      $tot_wtrequestdate[4] = (string)$rt->fields[3]; 
      $rt->fields[4] = str_replace(",", ".", $rt->fields[4]);
      $tot_wtrequestdate[5] = (string)$rt->fields[4]; 
      $rt->fields[5] = str_replace(",", ".", $rt->fields[5]);
      $tot_wtrequestdate[6] = (string)$rt->fields[5]; 
      $rt->fields[6] = str_replace(",", ".", $rt->fields[6]);
      $tot_wtrequestdate[7] = (string)$rt->fields[6]; 
      $rt->fields[7] = str_replace(",", ".", $rt->fields[7]);
      $tot_wtrequestdate[8] = (string)$rt->fields[7]; 
      $rt->fields[8] = str_replace(",", ".", $rt->fields[8]);
      $tot_wtrequestdate[9] = (string)$rt->fields[8]; 
      $rt->Close(); 
   } 

   //-----  wtproducerbranchid
   function quebra_wtproducerbranchid_sc_free_group_by($wtproducerbranchid, $Where_qb) 
   {
      global $tot_wtproducerbranchid , $wtproducerbranchid, $wtproducerperiferia, $wtproducerewc, $Sc_groupby_wtrequestdate;  
      $tot_wtproducerbranchid = array() ;  
      $nm_comando = "select count(*), sum(wtProducerQty) as sum_wtproducerqty, avg(wtProducerQty) as avg_wtproducerqty, max(wtProducerQty) as max_wtproducerqty, min(wtProducerQty) as min_wtproducerqty, sum(wtFDestQty) as sum_wtfdestqty, avg(wtFDestQty) as avg_wtfdestqty, max(wtFDestQty) as max_wtfdestqty, min(wtFDestQty) as min_wtfdestqty from " . $this->Ini->nm_tabela . " " . $_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['where_pesq']; 
      if (empty($_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['where_pesq'])) 
      { 
         $nm_comando .= " where " . $Where_qb ; 
      } 
      else 
      { 
         $nm_comando .= " and " . $Where_qb ; 
      } 
      $_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 ; 
      }  
      $tot_wtproducerbranchid[0] = NM_encode_input($wtproducerbranchid) ; 
      $tot_wtproducerbranchid[1] = $rt->fields[0] ; 
      $rt->fields[1] = str_replace(",", ".", $rt->fields[1]);
      $tot_wtproducerbranchid[2] = (string)$rt->fields[1]; 
      $rt->fields[2] = str_replace(",", ".", $rt->fields[2]);
      $tot_wtproducerbranchid[3] = (string)$rt->fields[2]; 
      $rt->fields[3] = str_replace(",", ".", $rt->fields[3]);
      $tot_wtproducerbranchid[4] = (string)$rt->fields[3]; 
      $rt->fields[4] = str_replace(",", ".", $rt->fields[4]);
      $tot_wtproducerbranchid[5] = (string)$rt->fields[4]; 
      $rt->fields[5] = str_replace(",", ".", $rt->fields[5]);
      $tot_wtproducerbranchid[6] = (string)$rt->fields[5]; 
      $rt->fields[6] = str_replace(",", ".", $rt->fields[6]);
      $tot_wtproducerbranchid[7] = (string)$rt->fields[6]; 
      $rt->fields[7] = str_replace(",", ".", $rt->fields[7]);
      $tot_wtproducerbranchid[8] = (string)$rt->fields[7]; 
      $rt->fields[8] = str_replace(",", ".", $rt->fields[8]);
      $tot_wtproducerbranchid[9] = (string)$rt->fields[8]; 
      $rt->Close(); 
   } 

   //-----  wtproducerewc
   function quebra_wtproducerewc_sc_free_group_by($wtproducerewc, $Where_qb) 
   {
      global $tot_wtproducerewc , $wtproducerbranchid, $wtproducerperiferia, $wtproducerewc, $Sc_groupby_wtrequestdate;  
      $tot_wtproducerewc = array() ;  
      $nm_comando = "select count(*), sum(wtProducerQty) as sum_wtproducerqty, avg(wtProducerQty) as avg_wtproducerqty, max(wtProducerQty) as max_wtproducerqty, min(wtProducerQty) as min_wtproducerqty, sum(wtFDestQty) as sum_wtfdestqty, avg(wtFDestQty) as avg_wtfdestqty, max(wtFDestQty) as max_wtfdestqty, min(wtFDestQty) as min_wtfdestqty from " . $this->Ini->nm_tabela . " " . $_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['where_pesq']; 
      if (empty($_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['where_pesq'])) 
      { 
         $nm_comando .= " where " . $Where_qb ; 
      } 
      else 
      { 
         $nm_comando .= " and " . $Where_qb ; 
      } 
      $_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 ; 
      }  
      $tot_wtproducerewc[0] = $wtproducerewc ; 
      $tot_wtproducerewc[1] = $rt->fields[0] ; 
      $rt->fields[1] = str_replace(",", ".", $rt->fields[1]);
      $tot_wtproducerewc[2] = (string)$rt->fields[1]; 
      $rt->fields[2] = str_replace(",", ".", $rt->fields[2]);
      $tot_wtproducerewc[3] = (string)$rt->fields[2]; 
      $rt->fields[3] = str_replace(",", ".", $rt->fields[3]);
      $tot_wtproducerewc[4] = (string)$rt->fields[3]; 
      $rt->fields[4] = str_replace(",", ".", $rt->fields[4]);
      $tot_wtproducerewc[5] = (string)$rt->fields[4]; 
      $rt->fields[5] = str_replace(",", ".", $rt->fields[5]);
      $tot_wtproducerewc[6] = (string)$rt->fields[5]; 
      $rt->fields[6] = str_replace(",", ".", $rt->fields[6]);
      $tot_wtproducerewc[7] = (string)$rt->fields[6]; 
      $rt->fields[7] = str_replace(",", ".", $rt->fields[7]);
      $tot_wtproducerewc[8] = (string)$rt->fields[7]; 
      $rt->fields[8] = str_replace(",", ".", $rt->fields[8]);
      $tot_wtproducerewc[9] = (string)$rt->fields[8]; 
      $rt->Close(); 
   } 

   //-----  wtproducerperiferia
   function quebra_wtproducerperiferia_sc_free_group_by($wtproducerperiferia, $Where_qb) 
   {
      global $tot_wtproducerperiferia , $wtproducerbranchid, $wtproducerperiferia, $wtproducerewc, $Sc_groupby_wtrequestdate;  
      $tot_wtproducerperiferia = array() ;  
      $nm_comando = "select count(*), sum(wtProducerQty) as sum_wtproducerqty, avg(wtProducerQty) as avg_wtproducerqty, max(wtProducerQty) as max_wtproducerqty, min(wtProducerQty) as min_wtproducerqty, sum(wtFDestQty) as sum_wtfdestqty, avg(wtFDestQty) as avg_wtfdestqty, max(wtFDestQty) as max_wtfdestqty, min(wtFDestQty) as min_wtfdestqty from " . $this->Ini->nm_tabela . " " . $_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['where_pesq']; 
      if (empty($_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['where_pesq'])) 
      { 
         $nm_comando .= " where " . $Where_qb ; 
      } 
      else 
      { 
         $nm_comando .= " and " . $Where_qb ; 
      } 
      $_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 ; 
      }  
      $tot_wtproducerperiferia[0] = NM_encode_input($wtproducerperiferia) ; 
      $tot_wtproducerperiferia[1] = $rt->fields[0] ; 
      $rt->fields[1] = str_replace(",", ".", $rt->fields[1]);
      $tot_wtproducerperiferia[2] = (string)$rt->fields[1]; 
      $rt->fields[2] = str_replace(",", ".", $rt->fields[2]);
      $tot_wtproducerperiferia[3] = (string)$rt->fields[2]; 
      $rt->fields[3] = str_replace(",", ".", $rt->fields[3]);
      $tot_wtproducerperiferia[4] = (string)$rt->fields[3]; 
      $rt->fields[4] = str_replace(",", ".", $rt->fields[4]);
      $tot_wtproducerperiferia[5] = (string)$rt->fields[4]; 
      $rt->fields[5] = str_replace(",", ".", $rt->fields[5]);
      $tot_wtproducerperiferia[6] = (string)$rt->fields[5]; 
      $rt->fields[6] = str_replace(",", ".", $rt->fields[6]);
      $tot_wtproducerperiferia[7] = (string)$rt->fields[6]; 
      $rt->fields[7] = str_replace(",", ".", $rt->fields[7]);
      $tot_wtproducerperiferia[8] = (string)$rt->fields[7]; 
      $rt->fields[8] = str_replace(",", ".", $rt->fields[8]);
      $tot_wtproducerperiferia[9] = (string)$rt->fields[8]; 
      $rt->Close(); 
   } 


   //----- 
   function resumo_sc_free_group_by($destino_resumo, &$array_total_wtrequestdate, &$array_total_wtproducerbranchid, &$array_total_wtproducerewc, &$array_total_wtproducerperiferia)
   {
      global $nada, $nm_lang, $wtproducerbranchid, $wtproducerperiferia, $wtproducerewc;
   if (isset($_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['campos_busca']) && !empty($_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['campos_busca']))
   { 
      $Busca_temp = $_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['campos_busca'];
      if ($_SESSION['scriptcase']['charset'] != "UTF-8")
      {
          $Busca_temp = NM_conv_charset($Busca_temp, $_SESSION['scriptcase']['charset'], "UTF-8");
      }
       $this->wtid = $Busca_temp['wtid']; 
       $tmp_pos = strpos($this->wtid, "##@@");
       if ($tmp_pos !== false)
       {
           $this->wtid = substr($this->wtid, 0, $tmp_pos);
       }
       $this->wtid_2 = $Busca_temp['wtid_input_2']; 
       $this->wtprofilid = $Busca_temp['wtprofilid']; 
       $tmp_pos = strpos($this->wtprofilid, "##@@");
       if ($tmp_pos !== false)
       {
           $this->wtprofilid = substr($this->wtprofilid, 0, $tmp_pos);
       }
       $this->wtprofilid_2 = $Busca_temp['wtprofilid_input_2']; 
       $this->wtrequestdate = $Busca_temp['wtrequestdate']; 
       $tmp_pos = strpos($this->wtrequestdate, "##@@");
       if ($tmp_pos !== false)
       {
           $this->wtrequestdate = substr($this->wtrequestdate, 0, $tmp_pos);
       }
       $this->wtrequestdate_2 = $Busca_temp['wtrequestdate_input_2']; 
       $this->wtproducerbranchid = $Busca_temp['wtproducerbranchid']; 
       $tmp_pos = strpos($this->wtproducerbranchid, "##@@");
       if ($tmp_pos !== false)
       {
           $this->wtproducerbranchid = substr($this->wtproducerbranchid, 0, $tmp_pos);
       }
       $this->wtproducerbranchid_2 = $Busca_temp['wtproducerbranchid_input_2']; 
   } 
   $this->sc_where_orig   = $_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['where_orig'];
   $this->sc_where_atual  = $_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['where_pesq'];
   $this->sc_where_filtro = $_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['where_pesq_filtro'];
   $temp = "";
   foreach ($_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['SC_Gb_Free_sql'] as $cmp_gb => $ord)
   {
       $temp .= (empty($temp)) ? $cmp_gb . " " . $ord : ", " . $cmp_gb . " " . $ord;
   }
   $nmgp_order_by = (!empty($temp)) ? " order by " . $temp : "";
   $free_group_by = "";
   $all_sql_free  = array();
   $all_sql_free[] = "wtRequestDate";
   $all_sql_free[] = "wtProducerBranchID";
   $all_sql_free[] = "wtProducerEwc";
   $all_sql_free[] = "wtProducerPeriferia";
   foreach ($_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['SC_Gb_Free_sql'] as $cmp_gb => $ord)
   {
       $free_group_by .= (empty($free_group_by)) ? $cmp_gb : ", " . $cmp_gb;
   }
   foreach ($all_sql_free as $cmp_gb)
   {
       if (!isset($_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['SC_Gb_Free_sql'][$cmp_gb]))
       {
           $free_group_by .= (empty($free_group_by)) ? $cmp_gb : ", " . $cmp_gb;
       }
   }
     $comando  = "select count(*), sum(wtProducerQty) as sum_wtproducerqty, avg(wtProducerQty) as avg_wtproducerqty, max(wtProducerQty) as max_wtproducerqty, min(wtProducerQty) as min_wtproducerqty, sum(wtFDestQty) as sum_wtfdestqty, avg(wtFDestQty) as avg_wtfdestqty, max(wtFDestQty) as max_wtfdestqty, min(wtFDestQty) as min_wtfdestqty, wtRequestDate, wtProducerBranchID, wtProducerEwc, wtProducerPeriferia from " . $this->Ini->nm_tabela . " " . $this->sc_where_atual . " group by " . $free_group_by . "  " . $nmgp_order_by;
      if ($destino_resumo != "gra") 
      {
          $comando = str_replace("avg(", "sum(", $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;
      }
      $array_db_total = $this->get_array($rt);
      $rt->Close();
      foreach ($array_db_total as $registro)
      {
         $registro[9] = substr($registro[9], 0, 10);
         $wtrequestdate      = $registro[9];
         $wtrequestdate_orig = $registro[9];
         $conteudo = $registro[9];
        $conteudo_x =  $conteudo;
        nm_conv_limpa_dado($conteudo_x, "YYYY-MM-DD");
        if (is_numeric($conteudo_x) && $conteudo_x > 0) 
        { 
            $this->nm_data->SetaData($conteudo, "YYYY-MM-DD");
            $conteudo = $this->nm_data->FormataSaida($this->nm_data->FormatRegion("DT", "ddmmaaaa"));
        } 
         $wtrequestdate = $conteudo;
         if (null === $wtrequestdate)
         {
             $wtrequestdate = '';
         }
         if (null === $wtrequestdate_orig)
         {
             $wtrequestdate_orig = '';
         }
         $val_grafico_wtrequestdate = $wtrequestdate;
         $wtproducerbranchid      = $registro[10];
         $wtproducerbranchid_orig = $registro[10];
         $conteudo = $registro[10];
         $this->Lookup->lookup_wtproducerbranchid($conteudo , $wtproducerbranchid_orig) ; 
         $wtproducerbranchid = $conteudo;
         if (null === $wtproducerbranchid)
         {
             $wtproducerbranchid = '';
         }
         if (null === $wtproducerbranchid_orig)
         {
             $wtproducerbranchid_orig = '';
         }
         $val_grafico_wtproducerbranchid = $wtproducerbranchid;
         $wtproducerewc      = $registro[11];
         $wtproducerewc_orig = $registro[11];
         $conteudo = $registro[11];
         $this->Lookup->lookup_wtproducerewc($conteudo , $wtproducerewc_orig) ; 
         $wtproducerewc = $conteudo;
         if (null === $wtproducerewc)
         {
             $wtproducerewc = '';
         }
         if (null === $wtproducerewc_orig)
         {
             $wtproducerewc_orig = '';
         }
         $val_grafico_wtproducerewc = $wtproducerewc;
         $wtproducerperiferia      = $registro[12];
         $wtproducerperiferia_orig = $registro[12];
         $conteudo = $registro[12];
         $this->Lookup->lookup_wtproducerperiferia($conteudo , $wtproducerperiferia_orig) ; 
         $wtproducerperiferia = $conteudo;
         if (null === $wtproducerperiferia)
         {
             $wtproducerperiferia = '';
         }
         if (null === $wtproducerperiferia_orig)
         {
             $wtproducerperiferia_orig = '';
         }
         $val_grafico_wtproducerperiferia = $wtproducerperiferia;
         $registro[1] = str_replace(",", ".", $registro[1]);
         $registro[1] = (strpos(strtolower($registro[1]), "e")) ? (float)$registro[1] : $registro[1]; 
         $registro[1] = (string)$registro[1];
         if ($registro[1] == "") 
         {
             $registro[1] = 0;
         }
         $registro[2] = str_replace(",", ".", $registro[2]);
         $registro[2] = (strpos(strtolower($registro[2]), "e")) ? (float)$registro[2] : $registro[2]; 
         $registro[2] = (string)$registro[2];
         if ($registro[2] == "") 
         {
             $registro[2] = 0;
         }
         $registro[3] = str_replace(",", ".", $registro[3]);
         $registro[3] = (strpos(strtolower($registro[3]), "e")) ? (float)$registro[3] : $registro[3]; 
         $registro[3] = (string)$registro[3];
         if ($registro[3] == "") 
         {
             $registro[3] = 0;
         }
         $registro[4] = str_replace(",", ".", $registro[4]);
         $registro[4] = (strpos(strtolower($registro[4]), "e")) ? (float)$registro[4] : $registro[4]; 
         $registro[4] = (string)$registro[4];
         if ($registro[4] == "") 
         {
             $registro[4] = 0;
         }
         $registro[5] = str_replace(",", ".", $registro[5]);
         $registro[5] = (strpos(strtolower($registro[5]), "e")) ? (float)$registro[5] : $registro[5]; 
         $registro[5] = (string)$registro[5];
         if ($registro[5] == "") 
         {
             $registro[5] = 0;
         }
         $registro[6] = str_replace(",", ".", $registro[6]);
         $registro[6] = (strpos(strtolower($registro[6]), "e")) ? (float)$registro[6] : $registro[6]; 
         $registro[6] = (string)$registro[6];
         if ($registro[6] == "") 
         {
             $registro[6] = 0;
         }
         $registro[7] = str_replace(",", ".", $registro[7]);
         $registro[7] = (strpos(strtolower($registro[7]), "e")) ? (float)$registro[7] : $registro[7]; 
         $registro[7] = (string)$registro[7];
         if ($registro[7] == "") 
         {
             $registro[7] = 0;
         }
         $registro[8] = str_replace(",", ".", $registro[8]);
         $registro[8] = (strpos(strtolower($registro[8]), "e")) ? (float)$registro[8] : $registro[8]; 
         $registro[8] = (string)$registro[8];
         if ($registro[8] == "") 
         {
             $registro[8] = 0;
         }
         $wtrequestdate_orig        = NM_encode_input($wtrequestdate_orig);
         $val_grafico_wtrequestdate = NM_encode_input($val_grafico_wtrequestdate);
         $wtproducerbranchid_orig        = NM_encode_input($wtproducerbranchid_orig);
         $val_grafico_wtproducerbranchid = NM_encode_input($val_grafico_wtproducerbranchid);
         $wtproducerperiferia_orig        = NM_encode_input($wtproducerperiferia_orig);
         $val_grafico_wtproducerperiferia = NM_encode_input($val_grafico_wtproducerperiferia);
         $contr_arr = "";
         foreach ($_SESSION['sc_session'][$this->Ini->sc_page]['Analyze_wastetransactions']['SC_Gb_Free_cmp'] as $cmp_gb => $resto)
         {
             $temp       = $cmp_gb . "_orig";
             $contr_arr .= "['" . $$temp . "']";
             $arr_name   = "array_total_" . $cmp_gb . $contr_arr;
            eval ('
             if (!isset($' . $arr_name . '))
             {
                 $' . $arr_name . '[0] = ' . $registro[0] . ';
                 $' . $arr_name . '[1] = ' . $registro[1] . ';
                 $' . $arr_name . '[3] = ' . $registro[2] . ';
                 $' . $arr_name . '[4] = ' . $registro[3] . ';
                 $' . $arr_name . '[5] = ' . $registro[4] . ';
                 $' . $arr_name . '[2] = ' . $registro[5] . ';
                 $' . $arr_name . '[6] = ' . $registro[6] . ';
                 $' . $arr_name . '[7] = ' . $registro[7] . ';
                 $' . $arr_name . '[8] = ' . $registro[8] . ';
                 $' . $arr_name . '[9] = $val_grafico_' . $cmp_gb . ';
                 $' . $arr_name . '[10] = "' . $$temp . '";
             }
             else
             {
                 $' . $arr_name . '[0] += ' . $registro[0] . ';
                 $' . $arr_name . '[1] += ' . $registro[1] . ';
                 $' . $arr_name . '[3] += ' . $registro[2] . ';
                 $' . $arr_name . '[4] = max($' . $arr_name . '[4], ' . $registro[3] . ');
                 $' . $arr_name . '[5] = min($' . $arr_name . '[5], ' . $registro[4] . ');
                 $' . $arr_name . '[2] += ' . $registro[5] . ';
                 $' . $arr_name . '[6] += ' . $registro[6] . ';
                 $' . $arr_name . '[7] = max($' . $arr_name . '[7], ' . $registro[7] . ');
                 $' . $arr_name . '[8] = min($' . $arr_name . '[8], ' . $registro[8] . ');
             }
            ');
         }
      }
   }
   //-----
   function get_array($rs)
   {
       if ('ado_mssql' != $this->Ini->nm_tpbanco)
       {
           return $rs->GetArray();
       }

       $array_db_total = array();
       while (!$rs->EOF)
       {
           $arr_row = array();
           foreach ($rs->fields as $k => $v)
           {
               $arr_row[$k] = $v . '';
           }
           $array_db_total[] = $arr_row;
           $rs->MoveNext();
       }
       return $array_db_total;
   }
   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";
       }
       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 = "";
      $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 ($cont2 >= $tam_campo)
              {
                  $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 = $trab_saida;
          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 = $trab_saida;
   } 
}

?>
