<?php

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

   var $nm_data;

   //----- 
   function line_grid_total($sc_page)
   {
      $this->sc_page = $sc_page;
      $this->nm_data = new nm_data("en_us");
      if (isset($_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']) && !empty($_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']))
      { 
          $this->style = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['style']; 
          $tmp_pos = strpos($this->style, "##@@");
          if ($tmp_pos !== false)
          {
              $this->style = substr($this->style, 0, $tmp_pos);
          }
          $this->name = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['name']; 
          $tmp_pos = strpos($this->name, "##@@");
          if ($tmp_pos !== false)
          {
              $this->name = substr($this->name, 0, $tmp_pos);
          }
          $this->width = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['width']; 
          $tmp_pos = strpos($this->width, "##@@");
          if ($tmp_pos !== false)
          {
              $this->width = substr($this->width, 0, $tmp_pos);
          }
          $width_2 = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['width_input_2']; 
          $this->width_2 = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['width_input_2']; 
          $this->fiber = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['fiber']; 
          $tmp_pos = strpos($this->fiber, "##@@");
          if ($tmp_pos !== false)
          {
              $this->fiber = substr($this->fiber, 0, $tmp_pos);
          }
          $this->ozsquare = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['ozsquare']; 
          $tmp_pos = strpos($this->ozsquare, "##@@");
          if ($tmp_pos !== false)
          {
              $this->ozsquare = substr($this->ozsquare, 0, $tmp_pos);
          }
          $ozsquare_2 = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['ozsquare_input_2']; 
          $this->ozsquare_2 = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['ozsquare_input_2']; 
          $this->ozlin = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['ozlin']; 
          $tmp_pos = strpos($this->ozlin, "##@@");
          if ($tmp_pos !== false)
          {
              $this->ozlin = substr($this->ozlin, 0, $tmp_pos);
          }
          $ozlin_2 = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['ozlin_input_2']; 
          $this->ozlin_2 = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['ozlin_input_2']; 
          $this->sqlb = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['sqlb']; 
          $tmp_pos = strpos($this->sqlb, "##@@");
          if ($tmp_pos !== false)
          {
              $this->sqlb = substr($this->sqlb, 0, $tmp_pos);
          }
          $sqlb_2 = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['sqlb_input_2']; 
          $this->sqlb_2 = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['sqlb_input_2']; 
          $this->meterkilo = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['meterkilo']; 
          $tmp_pos = strpos($this->meterkilo, "##@@");
          if ($tmp_pos !== false)
          {
              $this->meterkilo = substr($this->meterkilo, 0, $tmp_pos);
          }
          $meterkilo_2 = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['meterkilo_input_2']; 
          $this->meterkilo_2 = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['meterkilo_input_2']; 
          $this->meter2kilo = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['meter2kilo']; 
          $tmp_pos = strpos($this->meter2kilo, "##@@");
          if ($tmp_pos !== false)
          {
              $this->meter2kilo = substr($this->meter2kilo, 0, $tmp_pos);
          }
          $meter2kilo_2 = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['meter2kilo_input_2']; 
          $this->meter2kilo_2 = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['meter2kilo_input_2']; 
          $this->grammeter = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['grammeter']; 
          $tmp_pos = strpos($this->grammeter, "##@@");
          if ($tmp_pos !== false)
          {
              $this->grammeter = substr($this->grammeter, 0, $tmp_pos);
          }
          $grammeter_2 = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['grammeter_input_2']; 
          $this->grammeter_2 = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['grammeter_input_2']; 
          $this->grammeter2 = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['grammeter2']; 
          $tmp_pos = strpos($this->grammeter2, "##@@");
          if ($tmp_pos !== false)
          {
              $this->grammeter2 = substr($this->grammeter2, 0, $tmp_pos);
          }
          $grammeter2_2 = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['grammeter2_input_2']; 
          $this->grammeter2_2 = $_SESSION['sc_session'][$this->sc_page]['line_grid']['campos_busca']['grammeter2_input_2']; 
      } 
   }

   //---- 
   function quebra_geral()
   {
      global $nada, $nm_lang ;
      if ($_SESSION['sc_session'][$this->Ini->sc_page]['line_grid']['contr_total_geral'] == "OK") 
      { 
          return; 
      } 
      $_SESSION['sc_session'][$this->Ini->sc_page]['line_grid']['tot_geral'] = array() ;  
      if (in_array(strtolower($this->Ini->nm_tpbanco), $this->Ini->nm_bases_mssql))
      { 
          $nm_comando = "select count(*) from " . $this->Ini->nm_tabela . " " . $_SESSION['sc_session'][$this->Ini->sc_page]['line_grid']['where_pesq']; 
      } 
      else 
      { 
          $nm_comando = "select count(*) from " . $this->Ini->nm_tabela . " " . $_SESSION['sc_session'][$this->Ini->sc_page]['line_grid']['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]['line_grid']['tot_geral'][0] = "" . $this->Ini->Nm_lang['lang_msgs_totl'] . ""; 
      $_SESSION['sc_session'][$this->Ini->sc_page]['line_grid']['tot_geral'][1] = $rt->fields[0] ; 
      $rt->Close(); 
      $_SESSION['sc_session'][$this->Ini->sc_page]['line_grid']['contr_total_geral'] = "OK";
   } 

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

 function Nm_date_format($Type, $Format)
 {
     $Form_base = str_replace("/", "", $Format);
     $Form_base = str_replace("-", "", $Form_base);
     $Form_base = str_replace(":", "", $Form_base);
     $Form_base = str_replace(";", "", $Form_base);
     $Form_base = str_replace(" ", "", $Form_base);
     $Form_base = str_replace("a", "Y", $Form_base);
     $Form_base = str_replace("y", "Y", $Form_base);
     $Form_base = str_replace("h", "H", $Form_base);
     $date_format_show = "";
     if ($Type == "DT" || $Type == "DH")
     {
         $Str_date = str_replace("a", "y", strtolower($_SESSION['scriptcase']['reg_conf']['date_format']));
         $Str_date = str_replace("y", "Y", $Str_date);
         $Str_date = str_replace("h", "H", $Str_date);
         $Lim   = strlen($Str_date);
         $Ult   = "";
         $Arr_D = array();
         for ($I = 0; $I < $Lim; $I++)
         {
              $Char = substr($Str_date, $I, 1);
              if ($Char != $Ult)
              {
                  $Arr_D[] = $Char;
              }
              $Ult = $Char;
         }
         $Prim = true;
         foreach ($Arr_D as $Cada_d)
         {
             if (strpos($Form_base, $Cada_d) !== false)
             {
                 $date_format_show .= (!$Prim) ? $_SESSION['scriptcase']['reg_conf']['date_sep'] : "";
                 $date_format_show .= $Cada_d;
                 $Prim = false;
             }
         }
     }
     if ($Type == "DH" || $Type == "HH")
     {
         if ($Type == "DH")
         {
             $date_format_show .= " ";
         }
         $Str_time = strtolower($_SESSION['scriptcase']['reg_conf']['time_format']);
         $Str_time = str_replace("h", "H", $Str_time);
         $Lim   = strlen($Str_time);
         $Ult   = "";
         $Arr_T = array();
         for ($I = 0; $I < $Lim; $I++)
         {
              $Char = substr($Str_time, $I, 1);
              if ($Char != $Ult)
              {
                  $Arr_T[] = $Char;
              }
              $Ult = $Char;
         }
         $Prim = true;
         foreach ($Arr_T as $Cada_t)
         {
             if (strpos($Form_base, $Cada_t) !== false)
             {
                 $date_format_show .= (!$Prim) ? $_SESSION['scriptcase']['reg_conf']['time_sep'] : "";
                 $date_format_show .= $Cada_t;
                 $Prim = false;
             }
         }
     }
     return $date_format_show;
 }

}

?>
