<?php

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

   var $nm_data;

   //----- 
   function IndexWasteTransactions_total($sc_page)
   {
      $this->sc_page = $sc_page;
      $this->nm_data = new nm_data("el");
      if (isset($_SESSION['sc_session'][$this->sc_page]['IndexWasteTransactions']['campos_busca']) && !empty($_SESSION['sc_session'][$this->sc_page]['IndexWasteTransactions']['campos_busca']))
      { 
          $Busca_temp = $_SESSION['sc_session'][$this->Ini->sc_page]['IndexWasteTransactions']['campos_busca'];
          if ($_SESSION['scriptcase']['charset'] != "UTF-8")
          {
              $Busca_temp = NM_conv_charset($Busca_temp, $_SESSION['scriptcase']['charset'], "UTF-8");
          }
          $this->wastetransactions_intnumber = $Busca_temp['wastetransactions_intnumber']; 
          $tmp_pos = strpos($this->wastetransactions_intnumber, "##@@");
          if ($tmp_pos !== false)
          {
              $this->wastetransactions_intnumber = substr($this->wastetransactions_intnumber, 0, $tmp_pos);
          }
          $wastetransactions_intnumber_2 = $Busca_temp['wastetransactions_intnumber_input_2']; 
          $this->wastetransactions_intnumber_2 = $Busca_temp['wastetransactions_intnumber_input_2']; 
          $this->producerbranch_prbranchname = $Busca_temp['producerbranch_prbranchname']; 
          $tmp_pos = strpos($this->producerbranch_prbranchname, "##@@");
          if ($tmp_pos !== false)
          {
              $this->producerbranch_prbranchname = substr($this->producerbranch_prbranchname, 0, $tmp_pos);
          }
          $this->wastetransactions_wtrequestdate = $Busca_temp['wastetransactions_wtrequestdate']; 
          $tmp_pos = strpos($this->wastetransactions_wtrequestdate, "##@@");
          if ($tmp_pos !== false)
          {
              $this->wastetransactions_wtrequestdate = substr($this->wastetransactions_wtrequestdate, 0, $tmp_pos);
          }
          $wastetransactions_wtrequestdate_2 = $Busca_temp['wastetransactions_wtrequestdate_input_2']; 
          $this->wastetransactions_wtrequestdate_2 = $Busca_temp['wastetransactions_wtrequestdate_input_2']; 
          $this->wastetransactions_wtproducerewc = $Busca_temp['wastetransactions_wtproducerewc']; 
          $tmp_pos = strpos($this->wastetransactions_wtproducerewc, "##@@");
          if ($tmp_pos !== false)
          {
              $this->wastetransactions_wtproducerewc = substr($this->wastetransactions_wtproducerewc, 0, $tmp_pos);
          }
          $this->transporter_trans_title = $Busca_temp['transporter_trans_title']; 
          $tmp_pos = strpos($this->transporter_trans_title, "##@@");
          if ($tmp_pos !== false)
          {
              $this->transporter_trans_title = substr($this->transporter_trans_title, 0, $tmp_pos);
          }
          $this->wastetransactions_wttransportdate = $Busca_temp['wastetransactions_wttransportdate']; 
          $tmp_pos = strpos($this->wastetransactions_wttransportdate, "##@@");
          if ($tmp_pos !== false)
          {
              $this->wastetransactions_wttransportdate = substr($this->wastetransactions_wttransportdate, 0, $tmp_pos);
          }
          $wastetransactions_wttransportdate_2 = $Busca_temp['wastetransactions_wttransportdate_input_2']; 
          $this->wastetransactions_wttransportdate_2 = $Busca_temp['wastetransactions_wttransportdate_input_2']; 
          $this->fdestbranch_prbranchname = $Busca_temp['fdestbranch_prbranchname']; 
          $tmp_pos = strpos($this->fdestbranch_prbranchname, "##@@");
          if ($tmp_pos !== false)
          {
              $this->fdestbranch_prbranchname = substr($this->fdestbranch_prbranchname, 0, $tmp_pos);
          }
          $this->wastetransactions_wtfdestdate = $Busca_temp['wastetransactions_wtfdestdate']; 
          $tmp_pos = strpos($this->wastetransactions_wtfdestdate, "##@@");
          if ($tmp_pos !== false)
          {
              $this->wastetransactions_wtfdestdate = substr($this->wastetransactions_wtfdestdate, 0, $tmp_pos);
          }
          $wastetransactions_wtfdestdate_2 = $Busca_temp['wastetransactions_wtfdestdate_input_2']; 
          $this->wastetransactions_wtfdestdate_2 = $Busca_temp['wastetransactions_wtfdestdate_input_2']; 
      } 
   }

   //---- 
   function quebra_geral()
   {
      global $nada, $nm_lang , $wastetransactions_wtproducerewc;
      if ($_SESSION['sc_session'][$this->Ini->sc_page]['IndexWasteTransactions']['contr_total_geral'] == "OK") 
      { 
          return; 
      } 
      $_SESSION['sc_session'][$this->Ini->sc_page]['IndexWasteTransactions']['tot_geral'] = array() ;  
      $nm_comando = "select count(*), sum(wastetransactions.wtProducerQty) as sum_wastetransactions_wtproducerqty, avg(wastetransactions.wtProducerQty) as avg_wastetransactions_wtproducerqty, max(wastetransactions.wtProducerQty) as max_wastetransactions_wtproducerqty, min(wastetransactions.wtProducerQty) as min_wastetransactions_wtproducerqty from " . $this->Ini->nm_tabela . " " . $_SESSION['sc_session'][$this->Ini->sc_page]['IndexWasteTransactions']['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]['IndexWasteTransactions']['tot_geral'][0] = "" . $this->Ini->Nm_lang['lang_msgs_totl'] . ""; 
      $_SESSION['sc_session'][$this->Ini->sc_page]['IndexWasteTransactions']['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]['IndexWasteTransactions']['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]['IndexWasteTransactions']['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]['IndexWasteTransactions']['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]['IndexWasteTransactions']['tot_geral'][5] = $rt->fields[4]; 
      $rt->Close(); 
      $_SESSION['sc_session'][$this->Ini->sc_page]['IndexWasteTransactions']['contr_total_geral'] = "OK";
   } 

   //-----  producerbranch_prbranchname
   function quebra_producerbranch_prbranchname_sc_free_group_by($producerbranch_prbranchname, $Where_qb) 
   {
      global $tot_producerbranch_prbranchname , $wastetransactions_wtproducerewc;  
      $tot_producerbranch_prbranchname = array() ;  
      $nm_comando = "select count(*), sum(wastetransactions.wtProducerQty) as sum_wastetransactions_wtproducerqty, avg(wastetransactions.wtProducerQty) as avg_wastetransactions_wtproducerqty, max(wastetransactions.wtProducerQty) as max_wastetransactions_wtproducerqty, min(wastetransactions.wtProducerQty) as min_wastetransactions_wtproducerqty from " . $this->Ini->nm_tabela . " " . $_SESSION['sc_session'][$this->Ini->sc_page]['IndexWasteTransactions']['where_pesq']; 
      if (empty($_SESSION['sc_session'][$this->Ini->sc_page]['IndexWasteTransactions']['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_producerbranch_prbranchname[0] = $producerbranch_prbranchname ; 
      $tot_producerbranch_prbranchname[1] = $rt->fields[0] ; 
      $rt->fields[1] = str_replace(",", ".", $rt->fields[1]);
      $tot_producerbranch_prbranchname[2] = (string)$rt->fields[1]; 
      $rt->fields[2] = str_replace(",", ".", $rt->fields[2]);
      $tot_producerbranch_prbranchname[3] = (string)$rt->fields[2]; 
      $rt->fields[3] = str_replace(",", ".", $rt->fields[3]);
      $tot_producerbranch_prbranchname[4] = (string)$rt->fields[3]; 
      $rt->fields[4] = str_replace(",", ".", $rt->fields[4]);
      $tot_producerbranch_prbranchname[5] = (string)$rt->fields[4]; 
      $rt->Close(); 
   } 

   //-----  wastetransactions_wtrequestdate
   function quebra_wastetransactions_wtrequestdate_sc_free_group_by($wastetransactions_wtrequestdate, $Where_qb) 
   {
      global $tot_wastetransactions_wtrequestdate , $wastetransactions_wtproducerewc, $Sc_groupby_wastetransactions_wtrequestdate;  
      $tot_wastetransactions_wtrequestdate = array() ;  
      $nm_comando = "select count(*), sum(wastetransactions.wtProducerQty) as sum_wastetransactions_wtproducerqty, avg(wastetransactions.wtProducerQty) as avg_wastetransactions_wtproducerqty, max(wastetransactions.wtProducerQty) as max_wastetransactions_wtproducerqty, min(wastetransactions.wtProducerQty) as min_wastetransactions_wtproducerqty from " . $this->Ini->nm_tabela . " " . $_SESSION['sc_session'][$this->Ini->sc_page]['IndexWasteTransactions']['where_pesq']; 
      if (empty($_SESSION['sc_session'][$this->Ini->sc_page]['IndexWasteTransactions']['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_wastetransactions_wtrequestdate[0] = NM_encode_input($wastetransactions_wtrequestdate) ; 
      $tot_wastetransactions_wtrequestdate[1] = $rt->fields[0] ; 
      $rt->fields[1] = str_replace(",", ".", $rt->fields[1]);
      $tot_wastetransactions_wtrequestdate[2] = (string)$rt->fields[1]; 
      $rt->fields[2] = str_replace(",", ".", $rt->fields[2]);
      $tot_wastetransactions_wtrequestdate[3] = (string)$rt->fields[2]; 
      $rt->fields[3] = str_replace(",", ".", $rt->fields[3]);
      $tot_wastetransactions_wtrequestdate[4] = (string)$rt->fields[3]; 
      $rt->fields[4] = str_replace(",", ".", $rt->fields[4]);
      $tot_wastetransactions_wtrequestdate[5] = (string)$rt->fields[4]; 
      $rt->Close(); 
   } 

   //-----  wastetransactions_wtproducerewc
   function quebra_wastetransactions_wtproducerewc_sc_free_group_by($wastetransactions_wtproducerewc, $Where_qb) 
   {
      global $tot_wastetransactions_wtproducerewc , $wastetransactions_wtproducerewc, $Sc_groupby_wastetransactions_wtrequestdate;  
      $tot_wastetransactions_wtproducerewc = array() ;  
      $nm_comando = "select count(*), sum(wastetransactions.wtProducerQty) as sum_wastetransactions_wtproducerqty, avg(wastetransactions.wtProducerQty) as avg_wastetransactions_wtproducerqty, max(wastetransactions.wtProducerQty) as max_wastetransactions_wtproducerqty, min(wastetransactions.wtProducerQty) as min_wastetransactions_wtproducerqty from " . $this->Ini->nm_tabela . " " . $_SESSION['sc_session'][$this->Ini->sc_page]['IndexWasteTransactions']['where_pesq']; 
      if (empty($_SESSION['sc_session'][$this->Ini->sc_page]['IndexWasteTransactions']['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_wastetransactions_wtproducerewc[0] = $wastetransactions_wtproducerewc ; 
      $tot_wastetransactions_wtproducerewc[1] = $rt->fields[0] ; 
      $rt->fields[1] = str_replace(",", ".", $rt->fields[1]);
      $tot_wastetransactions_wtproducerewc[2] = (string)$rt->fields[1]; 
      $rt->fields[2] = str_replace(",", ".", $rt->fields[2]);
      $tot_wastetransactions_wtproducerewc[3] = (string)$rt->fields[2]; 
      $rt->fields[3] = str_replace(",", ".", $rt->fields[3]);
      $tot_wastetransactions_wtproducerewc[4] = (string)$rt->fields[3]; 
      $rt->fields[4] = str_replace(",", ".", $rt->fields[4]);
      $tot_wastetransactions_wtproducerewc[5] = (string)$rt->fields[4]; 
      $rt->Close(); 
   } 

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

?>
