;** AC_CFACG *******************************************************************
;*  Acerto Programcao Faccao/Producao                                          *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 24/06/2019 por Bruno Carvalho
;Ultima Alteracao: 24/12/2019 por Bruno Carvalho
;Motivo Alteracao: Adicionado Bib_Proc
;*******************************************************************************
;* Rotina para Controlar Chave                                                 *
;*******************************************************************************
Proc CHAVE()
INICIO=True
campocorrente=""

While True
  Moveto "CFACC"
  Moveto [Numero_Corte]
  NPROG=[Numero_Corte]
  
  If ([Data_Programacao] >= VSDGDTINI And [Data_Programacao] <= VSDGDTFIM) And
     [Situacao_Programacao]<>"FINALIZADA"
     Then CHAVE=0    
     Else CHAVE=9 
          Return
  EndIf
  
  While True
    TECLA=""
  
     If OPCAO_FORM="F"
        Then PickForm "2"
     EndIf  
     If IsLockRecord()
        Then If OPCAO_FORM="F"
                Then PickForm "F"
             EndIf
             QuitLoop
    EndIf
    Return
  EndWhile
EndWhile
EndProc   ; (Chave)

;*******************************************************************************
;* Rotina para Digitacao Dados Gerais                                          *
;*******************************************************************************
Proc DIGITA()
MSGCAMPO=""
gerou_cfacri=False  
If IsBlank(campocorrente)
   Then If [Situacao_Programacao]="FINALIZADA" 
           Then ;If Not Checa_Supervisor()
                ;   Then CHAVE=9
                ;        Return
                ;EndIf
                
                CHAVE=9
                Return
        EndIf

        If Not IsBlank([Codigo da Empresa])
           Then If Not Carrega_Parametros()
                   Then If CHAVE=1
                           Then Del
                        EndIf
                        CHAVE=9
                        Return
                EndIf
        EndIf
   Else Moveto Field campocorrente
EndIf

While True
  FINALIZADO=True
  OPCAO=""
  QuitLoop
EndWhile
EndProc

;*******************************************************************************
;* Rotina Gera Tabela Cfacrig                                                  *
;*******************************************************************************
Proc Gera_Cfacri()
Private codite

Aguarde("Convertendo programa‡„o modo padr„o")
Moveto "Cfacri"
Moveto [Numero_Corte]
Locate [Cfacc->Numero_Corte]
If RetVal
   Then While [Numero_Corte]=[Cfacc->Numero_Corte]
          Del
        EndWhile
EndIf

refant="****"

Seq=0
Moveto "Cfacrig"
NR = NIMAGERECORDS()
Scan
  Barra(NR, RecNo())
  Aguarde("Aguarde, Convertendo programa‡„o modo padr„o "+BARRA_STRING)

  CopyToArray RegRq
   
	If RegRq["Referencia"]<>refant
	   Then Array_Monta_Grade("")
	        refant=[Referencia]
	EndIf        

  For ic From 1 to 12
    Qtd=0
    TM=RTrim(SubStr(Array_TM[ic],4,2))
    Switch
      Case ic=1:  Qtd=RegRq["Qtd1"]  
      Case ic=2:  Qtd=RegRq["Qtd2"]  
      Case ic=3:  Qtd=RegRq["Qtd3"]  
      Case ic=4:  Qtd=RegRq["Qtd4"]  
      Case ic=5:  Qtd=RegRq["Qtd5"]  
      Case ic=6:  Qtd=RegRq["Qtd6"]  
      Case ic=7:  Qtd=RegRq["Qtd7"]  
      Case ic=8:  Qtd=RegRq["Qtd8"]  
      Case ic=9:  Qtd=RegRq["Qtd9"]  
      Case ic=10: Qtd=RegRq["Qtd10"] 
      Case ic=11: Qtd=RegRq["Qtd11"] 
      Case ic=12: Qtd=RegRq["Qtd12"] 
    EndSwitch

    If Qtd>0
       Then If Len(TM)=1
               Then codite=SubStr(RegRq["Codigo do Item"],1,9)+TM
               Else codite=SubStr(RegRq["Codigo do Item"],1,8)+TM
            EndIf

            Moveto "Cfacri"
            Moveto [Numero_Corte]
            Locate RegRq["Numero_Corte"],codite
            If Not RetVal
               Then End Down
                    Seq=Seq+1
                    [Numero_Corte             ]=RegRq["Numero_Corte"]

                    [Codigo do Item           ]=codite
            EndIf
            [Quantidade Programada    ]=[Quantidade Programada    ]+Qtd

            Moveto "Cfacri"
            UnlockRecord
    EndIf
  EndFor

  Moveto "Cfacrig"
EndScan
CHAVEF=8
LimpAguarde()
EndProc

;*******************************************************************************
;* Rotina para Carregar Parametros                                             *
;*******************************************************************************
Proc Carrega_Parametros()
Private Result

If IsEmpty("P_Fac")
   Then Mensagem("N„o esta Configurado os Parametros para Digita‡„o !")
        Result=False
   Else UnLockRecord
        KeyPress "F7"
        While True
          If Not IsLockRecord()
             Then QuitLoop
          EndIf
        EndWhile
        
        Moveto "P_Fac"
        Moveto [Codigo_Empresa]
        Locate [Cfacc->Codigo da Empresa]
        If Not RetVal
           Then Mensagem("Parametros n„o Cadastrado para esta Empresa !")
                Result=False
           Else     
                PARAM_EMPRESA         = [Codigo_Empresa           ]
                PARAM_COR_TECIDO      = [Cor_Tecido               ]
                PARAM_RELATORIO       = [Script_Impressao_ProgCort]

                Result=True
        EndIf
EndIf

Moveto "Cfacc"
KeyPress "F7"

Return Result

EndProc

;*******************************************************************************
;* Rotina p/ abrir tabelas                                                     *
;*******************************************************************************
Proc AbreTabelas()

View NoWindow "P_FAC"
View NoWindow "CFACP"
View NoWindow "CFACRI"
View NoWindow "CFACRIG"
CoEdit "CFACC"

EndProc ;AbreTabelas()

;*******************************************************************************
;* Rotina para controlar Edicao do registro                                    *
;*******************************************************************************
Proc Controla_EdicaoC()
FINALIZADO=False
 
While Not FINALIZADO
  Tecla_Funcao=BlankNum()
  DIGITA()
  INICIO=False

  If CHAVE=9
     Then QuitLoop
  EndIf   

  If Finalizado
     Then If Not gerou_CFACRI            
             Then Gera_Cfacri()  
          EndIf
  EndIf
  Moveto "CFACC"
EndWhile
If RecordStatus("Locked") Then UnLockRecord EndIf
If OPCAO_FORM="F"
   Then PickForm "F"
EndIf
EndProc

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc SD_Dialog(Disparo,Campo,Evento,Elemento)
Temp = True
TECLA    =""
MSG_CAMPO=""
If Disparo="OPEN"  Then AoCriarShowDialog() EndIf
If Disparo="EVENT" And Evento["Type"]="KEY" Then
   Switch
     Case Evento["KeyCode"] = -59: Dialog_F1()
          If Campo="TG_OPDEST"
             Then Seleciona_Opcoes("Op‡”es de Desinat rio","7,9")
          EndIf
     Case Evento["KeyCode"] =  27:
          Temp=False
          Switch
            Case Campo="TG_DTINI"         : Campo="NO"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="YES" Or Campo="NO": Campo="TG_DTINI"
          EndSwitch
     Case (Evento["KeyCode"] <= -104 And Evento["KeyCode"] >= -113) Or 
           Evento["KeyCode"]  = -139 Or  Evento["KeyCode"]  = -140:
           SYS_TECLA_ACIONADA=Evento["KeyCode"]
           Seta_Tecla_Acionada()
           CancelDialog
     Case Evento["KeyCode"] = 13:
          Temp=False
          If Checa_TAG()
             Then Switch
                    Case Campo="TG_DTINI"      : Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"      : Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
MSG_CAMPO=""
TECLA=""
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************
AutoLib="\\Sistemas\\Cadastro\\Bibliot,\\Sistemas\\Cadastro\\Bib_Show,\\Sistemas\\Cadastro\\Bib_Proc"
SYS_Valor()

If Not IsAssigned(SYS_EMPRESA_DEFAULT) Then SYS_EMPRESA_DEFAULT=""   EndIf
If Not IsAssigned(SYS_CHAPA_USUARIO)   Then SYS_CHAPA_USUARIO="9999" EndIf
If Not IsAssigned(SYS_CODIGO_CLIENTE)  Then SYS_CODIGO_CLIENTE=""    EndIf
If Not IsAssigned(SYS_CLIENTE_CONFEC)  Then SYS_CLIENTE_CONFEC=""    EndIf                       
If Not IsAssigned(PRODUCAO2)           Then PRODUCAO2=False          EndIf
                                  
VCITENS   =BlankNum()
VSQTDE    =BlankNum()
VSVRMERC  =BlankNum()
VSDGDTINI =BlankDate()
VSDGDTFIM =BlankDate()

Array Array_TM[12]
grdant="***"

While True
  ShowDialog "Acerto de Programa‡Æo do Corte" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 05,12   Height 09 Width 56

    Label  @ 01,04 "Per¡odo Referente" For "TG_DTINI"
    Label  @ 02,04 "de" For "TG_DTINI"
    Accept @ 02,08 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
    Label  @ 02,21 "at‚" For "TG_DTFIM"
    Accept @ 02,26 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM

    Pushbutton @ 05,14 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 05,28 width 11 "~C~ancela" CANCEL                    Tag "NO"  To BottonValue

  EndDialog
  If Not RetVal Then QuitLoop EndIf

  AbreTabelas() 
  PickForm "2"
  ;---- Ocultar Campos Linkados no Form --------------------
  Moveto "CFACRIG"   SetHighLight Current Record 51 Field 51
  Moveto "CFACRI"    SetHighLight Current Record 51 Field 51
  Moveto "CFACC"
  
  PickForm "F"
  OPCAO_FORM="F"
  Scan
    NR=NRecords("CFACC")
    Barra(NR, RecNo())
    Aguarde("Aguarde, Corrigindo "+Completa(StrVal(Recno())+"/"+StrVal(NR),9," ")+BARRA_STRING)
    CHAVE()
    If CHAVE=0 Then Controla_EdicaoC() EndIf    
  EndScan
  Do_It!
  ClearAll
  MENSAGEM("Opera‡Æo Finalizada !")
EndWhile
Release Vars All
Release Procs All 
