;*******************************************************************************
;* Desenvolvido: 03/05/2016 por Bruno Carvalho                                 *
;*******************************************************************************
Autolib="\\Sistemas\\Cadastro\\Bibliot,\\Sistemas\\Cadastro\\Bib_Show"

Mensagem("Ajustes CEST conforme NCM// //"+
         "Rotina Estoque//"+
         "// Este processo deve ser executado sem que outros Usuarios//"+
         "// estejam utilizando o Sistema, Ok !!!")

While True
  If Verifica_Usuario()
     Then QuitLoop
     Else ConfGeral("Comunicar os usuarios !// //Deseja Continuar com Atualizacao ?")
          If OK<>"S"
             Then Exit
          EndIf
  EndIf
EndWhile

;*******************************************************************************
;* Rotina para processar registros                                             *
;*******************************************************************************
Proc Processa()

If Not IsTable("Aux_Cest")
   Then Mensagem("N„o h  registros a processar!")
        Return
EndIf

{Ask} {Aux_Cest}
Moveto [CEST] Check
Do_it!
ClearAll

If Not IsEmpty("Cest")
   Then {Ask} {Answer} TypeIn "Delete" Enter
        Moveto [CEST] Example "Cest"
        {Ask} {Cest}
        Moveto [CEST] Example "Cest"
        Do_it!
        ClearAll
EndIf

If IsEmpty("Answer")
   Then Mensagem("N„o h  registros a processar!")
        Return
EndIf

{Ask} {Answer}
Moveto [CEST] Example "Cest"
{Ask} {Cest} TypeIn "Insert" Enter
Moveto [CEST] Example "Cest"
Do_it!
ClearAll

NR = NRecords("Aux_Cest")

View "Iten_Com"
View "Itens"
Coedit "Aux_Cest"
Scan
  Barra(NR, RecNo())
  Aguarde("Aguarde, ajustando tabela CEST "+BARRA_STRING)
  CEST = [CEST]
  Release Vars NCM
  CONT_NCM = 0
  DynArray NCM[]
  Switch
    Case Len([NCM])=8 : CONT_NCM = CONT_NCM + 1
                        NCM[CONT_NCM] = Format("W8",[NCM])+CEST
    Case Len([NCM])>8 : AUX_NCM = [NCM]
                        PosI = Search(",",AUX_NCM)
                        While PosI > 0
                          ARRAY_NCM = SubStr(AUX_NCM,1,PosI-1)
                          AUX_NCM   = SubStr(AUX_NCM,PosI+1,Len(AUX_NCM))
                          If Len(ARRAY_NCM) = 8
                             Then CONT_NCM = CONT_NCM + 1
                                  NCM[CONT_NCM] = Format("W8",ARRAY_NCM)+CEST
                          EndIf
                          PosI = Search(",",AUX_NCM)
                          If PosI = 0
                             Then If Len(AUX_NCM) = 8
                                     Then CONT_NCM = CONT_NCM + 1
                                          NCM[CONT_NCM] = Format("W8",AUX_NCM)+CEST
                                          QuitLoop
                                  EndIf
                          EnDIf
                        EndWhile
    OtherWise         : Loop
  EndSwitch

  If DynArraySize(NCM) = 0
     Then Loop
  EndIf
  
  For i From 1 To DynArraySize(NCM)
    Release Vars ITEM
    CONT_ITEM = 0
    DynArray ITEM[]
    
    AUX_NCM  = Rtrim(SubStr(NCM[i],1,8))
    AUX_CEST = Rtrim(SubStr(NCM[i],9,Len(NCM[i])))
    
    Moveto "Itens"
    Moveto [NCM]
    Locate AUX_NCM
    If RetVal
       Then While True
              If [NCM] = AUX_NCM
                 Then CONT_ITEM = CONT_ITEM + 1
                      ITEM[CONT_ITEM] = [Codigo do Item]
              EndIf
              Skip
              If Eot() Then QuitLoop EndIf
              Locate Next AUX_NCM
              If Not RetVal Then QuitLoop EndIf
            EndWhile
    EndIf
    
    If DynArraySize(ITEM)=0
       Then QuitLoop
    EnDIf
    
    For ii From 1 To DynArraySize(ITEM)
      Moveto "Iten_Com"
      Moveto [Codigo do Item]
      Locate ITEM[ii]
      If RetVal
         Then If [CEST] = ""
                 Then [CEST] = AUX_CEST
                      UnLockRecord
              EndIf
         Else If [Codigo do Item] = ""
                 Then End 
                 Else End Down
              EndIf
              [Codigo do Item] = ITEM[ii]
              [CEST          ] = AUX_CEST
              UnLockRecord
      EndIf
    Endfor
  EndFor
  Moveto "Aux_Cest"
EndScan
Do_it!
ClearAll

EndProc ;Processa()

;*******************************************************************************
;Rotina Principal                                                              *
;*******************************************************************************
utoLib="\\Sistemas\\Cadastro\\Bibliot,\\Sistemas\\Cadastro\\Bib_Show"

SetDir "\\Sistemas\\Cadastro"

Aguarde("Atualizando Sistema")
Password("t01avivcl")
Password("FechaSys")

View "SysTable"
SYS_CODIGO_CLIENTE = [Codigo do Cliente   ]
ClearAll

;-------------------------------------------------------------------------------

;****************** Estoque *******************
Diretorio="\\Sistemas\\Estoque\\"
SetDir Diretorio
Aguarde("Atualizando Pasta: "+Diretorio)

Processa()

;-------------------------------------------------------------------------------

{Tools} {More} {Directory} {\\Sistemas\\Cadastro\\}
UnPassword("t01avivcl")
UnPassword("FechaSys")
LimpAguarde()
FastMsg("Fim da atualiza‡Æo !")

Release Vars Diretorio
Release Procs Correcao_Tabelas
