opt = 1
diga (2, 40, 38, "7/1", "TABELA DE HORARIOS")
abra ("1", r_siga, "fp_ptth", "fp_ptth", "fp_ptth", "STR(TABELA,2)+ANOMES+STRZERO(DIA,2)")
while .T.
   cod = 0
   scroll (5, 1, 20, 78, 0)
   prim_vez = .T.
   dbGoBottom()
   m_tabela = tabela
   m_ano_mes = left (anomes, 2) + "/" + right (anomes, 2)
   m_toleranc = tolerancia
   fg_aut = simnao (24, 0, "Digitacao Automatica ? ", " Sim ", " Nao ", 1, 76)
   Set( 33, "on" )
   DevPos( 5, 1 ) ; DevOut( "Tabela: " ) ; SetPos( Row(), Col()+1 ) ; AAdd( GetList, _GET_( m_tabela, "m_tabela", "99",, ):display() )
   DevPos( 5, 15 ) ; DevOut( "Ano/Mes: " ) ; SetPos( Row(), Col()+1 ) ; AAdd( GetList, _GET_( m_ano_mes, "m_ano_mes", "99/99",, ):display() )
   DevPos( 5, 33 ) ; DevOut( "Tol: " ) ; SetPos( Row(), Col()+1 ) ; AAdd( GetList, _GET_( m_toleranc, "m_toleranc", "99",, ):display() )
   ReadModal(GetList) ; GetList := {}
   Set( 33, "off" )
   v_h1 = array(31)
   v_h2 = array(31)
   v_h3 = array(31)
   v_h4 = array(31)
   afill (v_h1, "00.00")
   afill (v_h2, "00.00")
   afill (v_h3, "00.00")
   afill (v_h4, "00.00")

   m_anomes = left (m_ano_mes, 2) + right (m_ano_mes, 2)
   dbSeek( (str (m_tabela, 2) + m_anomes) )
   if found ()
      mensagem ("A tabela " + str (m_tabela, 2) + " ja' esta' cadastrada no ano " + m_ano_mes)
      if ! simnao (24, 0, "Tenta Outra Digitacao ? ", " Sim ", " Nao ", 2, 76)
         exit
      endif
      loop
   endif

   do telahoras
   while simnao (24, 0, "Altera Dados Exibidos ? ", " Sim ", " Nao ", 2, 76)
      do telahoras
      ReadModal(GetList) ; GetList := {}
      prim_vez = .F.
   enddo

   if ! prim_vez .AND. (opt = 1 .OR. opt = 2) .OR. opt <> 1 .AND. opt <> 2
      if simnao (24, 0, "CONFIRMA OPERACAO ? ", " Sim ", " Nao ", 1, 76)
         do trocatab
      endif
   endif
   exit
enddo
DbCloseAll()
return

procedure telahoras
i = 1
a = 1
if fg_aut
   mdt = 0
   mdf = 0
   for i := 1 to 8
      DevPos( i + 5, 1 ) ; DevOut( "Dia " + str (i, 2) )
      SetPos( i + 5, 8 ) ; AAdd( GetList, _GET_( v_h1 [i], "v_h1 [i]", "99.99",, ):display() )
      SetPos( i + 5, 14 ) ; AAdd( GetList, _GET_( v_h2 [i], "v_h2 [i]", "99.99",, ):display() )
      SetPos( i + 5, 21 ) ; AAdd( GetList, _GET_( v_h3 [i], "v_h3 [i]", "99.99",, ):display() )
      SetPos( i + 5, 27 ) ; AAdd( GetList, _GET_( v_h4 [i], "v_h4 [i]", "99.99",, ):display() )
      ReadModal(GetList) ; GetList := {}
      if v_h1 [i] <> "00.00"
         mdt = mdt + 1
      else
         mdt = 0
      endif
   next
   for i := 9 to 31
      if mdt = 5
         for a := 1 to 1
            v_h1 [i] = "00.00"
            v_h2 [i] = "00.00"
            v_h3 [i] = "00.00"
            v_h4 [i] = "00.00"
            i = i + a
         next
         mdt = 0
      else
         if v_h1 [1] = "00.00" .OR. v_h1 [2] = "00.00"
            v_h1 [i] = v_h1 [4]
            v_h2 [i] = v_h2 [4]
            v_h3 [i] = v_h3 [4]
            v_h4 [i] = v_h4 [4]
         else
            v_h1 [i] = v_h1 [1]
            v_h2 [i] = v_h2 [1]
            v_h3 [i] = v_h3 [1]
            v_h4 [i] = v_h4 [1]
         endif
         mdt = mdt + 1
      endif
   next
endif

for i := 1 to 15
   DevPos( i + 5, 1 ) ; DevOut( "Dia " + str (i, 2) )
   SetPos( i + 5, 8 ) ; AAdd( GetList, _GET_( v_h1 [i], "v_h1 [i]", "99.99",, ):display() )
   SetPos( i + 5, 14 ) ; AAdd( GetList, _GET_( v_h2 [i], "v_h2 [i]", "99.99",, ):display() )
   SetPos( i + 5, 21 ) ; AAdd( GetList, _GET_( v_h3 [i], "v_h3 [i]", "99.99",, ):display() )
   SetPos( i + 5, 27 ) ; AAdd( GetList, _GET_( v_h4 [i], "v_h4 [i]", "99.99",, ):display() )
next
for i := 16 to 31
   DevPos( i - 11, 45 ) ; DevOut( "Dia " + str (i, 2) )
   SetPos( i - 11, 52 ) ; AAdd( GetList, _GET_( v_h1 [i], "v_h1 [i]", "99.99",, ):display() )
   SetPos( i - 11, 58 ) ; AAdd( GetList, _GET_( v_h2 [i], "v_h2 [i]", "99.99",, ):display() )
   SetPos( i - 11, 65 ) ; AAdd( GetList, _GET_( v_h3 [i], "v_h3 [i]", "99.99",, ):display() )
   SetPos( i - 11, 71 ) ; AAdd( GetList, _GET_( v_h4 [i], "v_h4 [i]", "99.99",, ):display() )
next
return

procedure trocatab
for i := 1 to 31
   if day (ctod (strzero (i, 2) + "." + right (m_anomes, 2) + "." + left (m_anomes, 2))) > 0
      dbSeek( (str (m_tabela, 2) + m_anomes + strzero (i, 2)) )
      if found ()
         reglock ()
         dbRecall()
      else
         appendblan ()
      endif
      _FIELD->tabela := m_tabela; _FIELD->anomes := m_anomes; _FIELD->tolerancia := m_toleranc
      _FIELD->dia := i; _FIELD->h1 := v_h1 [i]; _FIELD->h2 := v_h2 [i]; _FIELD->h3 := v_h3 [i]; _FIELD->h4 := v_h4 [i]
      dbCommitAll()
      dbUnlockAll()
   endif
next
return

procedure deletatab
for i := 1 to 31
   if day (ctod (strzero (i, 2) + "." + right (m_anomes, 2) + "." + left (m_anomes, 2))) > 0
      dbSeek( (str (m_tabela, 2) + m_anomes + strzero (i, 2)) )
      if found ()
         reglock ()
         dbDelete()
         dbCommitAll()
         dbUnlockAll()
      endif
   endif
next
return

procedure recalltab
for i := 1 to 31
   if day (ctod (strzero (i, 2) + "." + right (m_anomes, 2) + "." + left (m_anomes, 2))) > 0
      dbSeek( (str (m_tabela, 2) + m_anomes + strzero (i, 2)) )
      if found ()
         reglock ()
         dbRecall()
         dbCommitAll()
         dbUnlockAll()
      endif
   endif
next
return
