;** Itens_CB *******************************************************************
;*  Manutencao Cadastro de Codigo de Barras                                    *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 30/04/2009 por Anivel Augusto Rodrigues
;Ultima Alteracao: 25/11/2009 por Bruno Carvalho
;Motivo Alteracao: AlteraÆo da DigitaÆo dos Dig. A-EAN13 e A-EAN8
;Ultima Alteracao: 13/01/2010 por Bruno Carvalho
;Motivo Alteracao: Ajustes
;Ultima Alteracao: 03/01/2011 por Bruno Carvalho
;Motivo Alteracao: Ajustes Novo Layout de Teclas 
;Ultima Alteracao: 15/01/2013 por Kamila Gomes
;Motivo Alteracao: Ajustes SetaCorRegistro("Editando") 
;Ultima Alteracao: 03/01/2017 por Rafael Matos
;Motivo Alteracao: Ajustes showdialog MensagemRadioButtons() 
;Ultima Alteracao: 07/01/2017 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes cor editar

;*******************************************************************************
;* Rotina para Localizar Registro                                              *
;*******************************************************************************
Proc CHAVE()

While True

 Moveto [Tipo CdBarras]
 End
 Down

 While true
 SetaCorRegistro("Editar")

 If Field()="Tipo CdBarras"
     Then TECLA="F9=Inserir  F1=Tabelas"
     Else TECLA="F9=Inserir"
 EndIf

 AguardaTeclasPadrao()

 Switch
    Case RetVal=-33 : SemFuncoes()
    Case RetVal="F3": SemFicha()
    Case RetVal=-94 : MsgTeclasPadrao()
    Case Retval="F1" And Field()="Tipo CdBarras":
         ShowPopup " Tipos " CENTERED
          "~A~-EAN13     " : " EAN13                                      " : "EAN13",
          "~B~-EAN8      " : " EAN8                                       " : "EAN8",
          "~C~ancelar    " : " Cancelar a Operao "  : "CANCELAR"
         EndMenu
          To MenuVar

          echo off

         If MenuVar<>"CANCELAR" And MenuVar<>"Esc"
             Then [Tipo CdBarras]=MenuVar
         EndIf
    Case RetVal="F7" Or RetVal="UP" Or RetVal="PGUP" Or RetVal="HOME":
         While True
           PAGINA("Itens_CB","MR")
           TIPO  =[Tipo CdBarras]
           CODIGO=[Codigo_Barras]
           If EDITAR="S"
              Then CHAVE=0
                   Controla_EdicaoC()
              Else If EXCLUIR="S"
                      Then If QuerExcluir("")
                              Then Exclusao()
                              Else UnLockRecord
                           EndIf
                      Else QuitLoop
                   EndIf
           EndIf
           SetaPosForm()
         EndWhile
         QuitLoop
    Case Retval="F9":
         Selecao()
         QuitLoop
    Case Retval="ESC":
         Switch
           Case Field()="Tipo CdBarras" :
             CHAVE=9
             Del
             Return
           OtherWise: Left
         EndSwitch
    Case Retval="ENTER":
         If IsBlank([])
            Then Loop
         EndIf
         Switch
           Case Field()="Tipo CdBarras" :
             TIPO  =[]
             Moveto [Codigo_Barras]
           Case Field()="Codigo_Barras" :
             CODIGO=[]
             Switch
               Case [Tipo CdBarras]="EAN13": VSDGTPCOD=1
               Case [Tipo CdBarras]="EAN8" : VSDGTPCOD=2
             EndSwitch
             VSDGCODINI=CODIGO
             VSDGCODFIM=CODIGO
             If Not Checa_Codigo()
                Then Loop
             EndIf

             Del
             Locate TIPO, CODIGO
             If Retval
                Then CHAVE=0
                     Mensagem("CĒdigo j  existe !")
                Else CHAVE=1
                     End Down
                     [Tipo CdBarras]=TIPO
                     [Codigo_Barras]=CODIGO
             EndIf
             If IsLockRecord()
                Then QuitLoop
             EndIf
             Return
           OtherWise: Right
         EndSwitch
    EndSwitch
  EndWhile
EndWhile
EndProc

;*******************************************************************************
;* Rotina para Digitacao das Informacoes                                       *
;*******************************************************************************
Proc DIGITA()

Moveto [Codigo do Item]
While True
  TECLA=" Esc=Retornar  F1=Tabelas  "

  If CHAVE=0
     Then TECLA=TECLA+"Del=Excluir  "
  EndIf

  If Field()="Flag1"
     Then MSG="Pausa para confirmao"
     Else MSG=""
  EndIf

  Prompt TECLA
  SetaCorRegistro("Editando")
  Wait Field
     Message MSG
       Until "ESC","ENTER","DEL","FIELDVIEW","F35","F1"

  Switch
  Case Retval="F1":
       If Field()="Codigo do Item"
          Then ConsultaF1()
       EndIf
  Case Retval="ESC":
       Switch
         Case Field()="Codigo do Item":
           If QuerDesfazer("")
              Then If CHAVE=1
                      Then Del
                      Else Undo
                   EndIf
                   CHAVE=9
                   QuitLoop
           EndIf
         Case Field()="Flag1":
           []=""
           Moveto [Codigo do Item]
         OtherWise: Left
       EndSwitch
  Case Retval="ENTER":
       Switch
         Case Field()="Codigo do Item":
           If Not IsBlank([])
              Then Moveto [Flag1]
           EndIf
         Case Field()="Flag1":
           []=""
           QuitLoop
         OtherWise: Right
       EndSwitch
  Case Retval="FIELDVIEW" or Retval="F35":
       EDITFIELD()
  Case Retval="DEL":
       If CHAVE=0
          Then If QuerExcluir("")
                  Then DEL
                       QuitLoop
               EndIf
       EndIf
  EndSwitch
EndWhile
EndProc


;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc SD_Dialog(Disparo,Campo,Evento,Elemento)
Temp=True
MSG_CAMPO=""
TECLA=""
If Disparo="OPEN"  Then AoCriarShowDialog() EndIf
If Disparo="EVENT" And Evento["Type"]="KEY" Then
   Switch
     Case Evento["KeyCode"] =   9: Temp=False
     Case Evento["KeyCode"] = -59: Dialog_F1()
     Case Evento["KeyCode"] =  27:
          Temp=False
          Switch
            Case Campo="TG_TPCOD"          : Campo="NO"
            Case Campo="TG_CODINI"         : Campo="TG_TPCOD"
            Case Campo="TG_CODFIM"         : Campo="TG_CODINI"
            Case Campo="YES" Or Campo="NO" : Campo="TG_TPCOD"
          EndSwitch
     Case Evento["KeyCode"] = 13:
          Temp=False
          If Checa_TAG()
             Then Switch
                    ;** Selecao **
                    Case Campo="TG_TPCOD"      :
                         VSDGTPCOD=ControlValue("TG_TPCOD")
                         Campo="TG_CODINI"
                    Case Campo="TG_CODINI"     :
                         VSDGCODINI=ControlValue("TG_CODINI")
                         Campo="TG_CODFIM"
                    Case Campo="TG_CODFIM"     :
                         VSDGCODFIM=ControlValue("TG_CODFIM")
                         Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_TPCOD":
     MensagemRadioButtons()
  Case Campo="":
     MSG_CAMPO=""
EndSwitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

;*******************************************************************************
;* Rotina Filtrar Registros                                                    *
;*******************************************************************************
Proc Selecao()
LimpAguarde()

VSDGCODINI=""
VSDGCODFIM=""

While True

  ShowDialog "Intervalo de CĒdigo a Cadastrar" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC","TAB",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140
   @ 01,01 Height 13 Width 47

    Frame Single From 01,00 to 03,44
    Label @ 01,01 "Tipo de CĒdigo " For "TG_TPCOD"
    RadioButtons @ 02,01 Height 1 Width 40
      "~A~-EAN13",
      "~B~-EAN8"
        Tag "TG_TPCOD"
        To VSDGTPCOD

    Label  @ 05,00 "Faixa de CĒdigo" For "TG_CODINI"
    Label  @ 06,01 "de" For "TG_CODINI"
    Accept @ 06,06 Width 15 "A12"  Picture "#*#" Tag "TG_CODINI" To VSDGCODINI
    Label  @ 07,01 "at" For "TG_CODFIM"
    Accept @ 07,06 Width 15 "A12"  Picture "#*#" Tag "TG_CODFIM" To VSDGCODFIM

    Pushbutton @ 09,10 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 09,24 width 11 "~C~ancela" CANCEL                    Tag "NO"  To BottonValue

  EndDialog
  If RetVal
     Then If Not Checa_Codigo()
             Then Loop
             Else GeraNum()
          EndIf
  EndIf

  QuitLoop
EndWhile

Echo Normal
Echo Off

EndProc


;*******************************************************************************
;* Rotina Checagem de Codigos                                                  *
;*******************************************************************************
Proc Checa_Codigo()

If IsBlank(VSDGCODINI) Or
   IsBlank(VSDGCODFIM)
   Then Mensagem("Faxia de CĒdigo Inv lida, em branco !")
        Return False
EndIf

If Not E_Numerico(VSDGCODINI) Or
   Not E_Numerico(VSDGCODFIM)
   Then Mensagem("Faxia de CĒdigo Inv lida, sĒ digitos !")
        Return False
EndIf
If SubStr(VSDGCODINI,1,Len(VSDGCODINI)-1)>
   SubStr(VSDGCODFIM,1,Len(VSDGCODFIM)-1)
   Then Mensagem("Faxia de CĒdigo Inv lida, Inicial>Final !")
        Return False
EndIf

CDBAROk=True
Switch
  Case VSDGTPCOD=1: ;;"A-EAN13"
    Switch
      Case Len(VSDGCODINI)>12 Or
           Len(VSDGCODFIM)>12:
        CDBAROK=False
        Mensagem("Informado CĒdigo de Barras EAN13 >12 digitos !")
      Case Len(VSDGCODINI)<12 Or
           Len(VSDGCODFIM)<12:
        CDBAROK=False
        Mensagem("Informado CĒdigo de Barras EAN13 <12 digitos !")
;      Case Mod11_CalcEAN13("CHEC",SubStr(VSDGCODINI,1,12))="" Or
;           Mod11_CalcEAN13("CHEC",SubStr(VSDGCODFIM,1,12))="":
;        CDBAROK=False
;        Mensagem("Informado CĒdigo de Barras EAN13 Inv lido !")
;      Case Mod11_CalcEAN13("CHEC",SubStr(VSDGCODINI,1,12))<>numVal(SubStr(VSDGCODINI,13,1)) Or
;           Mod11_CalcEAN13("CHEC",SubStr(VSDGCODFIM,1,12))<>numVal(SubStr(VSDGCODFIM,13,1)):
;        CDBAROK=False
;        Mensagem("Informado digito verificador inv lido !")
    EndSwitch

  Case VSDGTPCOD=2: ;;"B-EAN8"
    Switch
      Case Len(VSDGCODINI)>7 Or
           Len(VSDGCODFIM)>7:
        CDBAROK=False
        Mensagem("Informado CĒdigo de Barras EAN8 >7 digitos !")
      Case Len(VSDGCODINI)<7 Or
           Len(VSDGCODFIM)<7:
        CDBAROK=False
        Mensagem("Informado CĒdigo de Barras EAN8 <7 digitos !")
;      Case Mod11_CalcEAN8("CHEC",SubStr(VSDGCODINI,1,7))="" Or
;           Mod11_CalcEAN8("CHEC",SubStr(VSDGCODFIM,1,7))="":
;        CDBAROK=False
;        Mensagem("Informado CĒdigo de Barras EAN8 Inv lido !")
;      Case Mod11_CalcEAN8("CHEC",SubStr(VSDGCODINI,1,7))<>numVal(SubStr(VSDGCODINI,8,1)) Or
;           Mod11_CalcEAN8("CHEC",SubStr(VSDGCODFIM,1,7))<>numVal(SubStr(VSDGCODFIM,8,1)):
;        CDBAROK=False
;        Mensagem("Informado digito verificador inv lido !")
    EndSwitch

EndSwitch
If CDBAROK
   Then Return True
   Else Return False
EndIf

EndProc

;*******************************************************************************
;* Rotina Geracao de Codigos                                                   *
;*******************************************************************************
Proc GeraNum()
Private CODBAR, TIPBAR

Aguarde("Gerando novos numeros")

Switch
  Case VSDGTPCOD=1: TIPBAR="EAN13"  ;;"A-EAN13"
  Case VSDGTPCOD=2: TIPBAR="EAN8"   ;;"B-EAN8"
EndSwitch

CODBAR=VSDGCODINI

Echo Normal
Moveto [Tipo CdBarras]
While True
  Switch
    Case TIPBAR="EAN13": CODBAR=NumVal(SubStr(CODBAR,1,12))
                         CODBAR=Mod11_CalcEAN13("CALC",CODBAR)
    Case TIPBAR="EAN8" : CODBAR=NumVal(SubStr(CODBAR,1,7))
                         CODBAR=Mod11_CalcEAN8("CALC",CODBAR)
  EndSwitch

  Locate TIPBAR,CODBAR
  If Not RetVal
     Then End
          Down
          [Tipo CdBarras]=TIPBAR
          [Codigo_Barras]=CODBAR
          unLockRecord
  EndIf

  
  Switch
    Case TIPBAR="EAN13": 
         If SubStr(CODBAR,1,12)= SubStr(VSDGCODFIM,1,12)
         Then QuitLoop EndIf
    Case TIPBAR="EAN8" : 
         If SubStr(CODBAR,1,7)= SubStr(VSDGCODFIM,1,7)
         Then QuitLoop EndIf
  EndSwitch
  
;  If CODBAR=VSDGCODFIM
;     Then QuitLoop
;  EndIf

  Switch
    Case TIPBAR="EAN13": CODBAR=NumVal(SubStr(CODBAR,1,12))+1
    Case TIPBAR="EAN8" : CODBAR=NumVal(SubStr(CODBAR,1,7))+1
  EndSwitch

EndWhile
Echo Off

LimpAguarde()

GERACAOOK=True
EndProc

;*******************************************************************************
;* Rotina para Excluir Registro                                                *
;*******************************************************************************
Proc Exclusao()
Private ItemOk

ItemOk=True
If Not IsBlank([Codigo do Item])
   Then Moveto "Itens"
        Moveto [Codigo do Item]
        Locate [Itens_CB->Codigo do Item]
        If RetVal
           Then If IsLockRecord()
                   Then ItemOk=False
                   Else [Tipo Codigo_Barras]=""
                        [Codigo de Barras  ]=""
                        UnLockRecord
                EndIf
        EndIf
EndIf

Moveto "Itens_CB"
If ItemOK
   Then Del
EndIf

EndProc

;*******************************************************************************
;* Rotina para controlar Edicao do registro                                    *
;*******************************************************************************
Proc Controla_EdicaoC()
If RecordStatus("Locked") Then UnLockRecord EndIf
Return



;While True
;  DIGITA()
;  If RecordStatus("Locked") Then UnLockRecord EndIf
;  If CHAVE=9
;     Then QuitLoop
;  EndIf
;  Switch
;    Case CHAVE=9: QuitLoop
;    OtherWise:
;      If Atualiza("GRAVAR")
;         Then QuitLoop
;      EndIf
;  EndSwitch
;EndWhile
EndProc

;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************
AutoLib="\\Sistemas\\Cadastro\\Bibliot,\\Sistemas\\Cadastro\\Bib_Show,\\Sistemas\\Estoque\\Bib_Estq"

VSDGCODINI = ""
VSDGCODFIM = ""

CoEdit "Itens_CB"
PickForm "F"
While True
  CHAVE()
  If CHAVE=9
     Then QuitLoop
  EndIf
  Controla_EdicaoC()
EndWhile
Do_It!
ClearAll
Release Vars All
Release Procs All
