;** Rm_0400  *******************************************************************
;*  Emissao de Ordem de Pagamento                                              *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 30/07/2012 por Anivel Augusto Rodrigues
;Ultima Alteracao: 30/07/2012 por Anivel Augusto Rodrigues
;Motivo Alteracao: Substituindo scriptïs Rm_0120, Rm_0130, Rm_0220
;Ultima Alteracao: 27/08/2012 por Bruno Carvalho
;Motivo Alteracao: Ajustes no Modelo 2, impressÆo atrav‚s de script "IMPRIME()"
;Ultima Alteracao: 02/10/2012 por Nikolas 
;Motivo Alteracao: Ajustes no Modelo 2 layout
;Ultima Alteracao: 18/10/2012 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes no Modelo 2 layout
;Ultima Alteracao: 02/03/2016 por Kamila Gomes
;Motivo Alteracao: Adicionado modelo 3
;Ultima Alteracao: 08/09/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   
;Ultima Alteracao: 05/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes TG_NCONTROLE                   
;Ultima Alteracao: 28/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog     
;Ultima Alteracao: 28/12/2016 por Rafael Matos
;Motivo Alteracao: Ajustes showdialog TG_DESTINAT               
;Ultima Alteracao: 29/05/2018 por Anivel Augusto Rodrigues
;Motivo Alteracao: Adicionado ValidarModelosRelatorios()

;*******************************************************************************
;* Rotina p/ Selecionar e Ordenar Registros                                    *
;*******************************************************************************
Proc Seleciona()
ClearAll

If IsTable("\\Sistemas\\Ctapag\\"+Dir_Trab+"Titulos")
  Then {Ask} TypeIn "\\Sistemas\\Ctapag\\"+Dir_Trab+"Titulos" Enter Check
       Moveto [Codigo da Empresa ] TypeIn "~VSDGEMPRESA.."
       If IsBlank(VSDGNCONTROLE)
          Then Switch
                 Case VSDGOPRELCODALF="1" :  Moveto [Data_Inclusao     ] TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM"
                 Case VSDGOPRELCODALF="2" :  Moveto [Data de Vencimento] TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM"
               EndSwitch  
               Moveto [Codigo da Conta     ] TypeIn "~VSDGCONTA.."
               Moveto [Codigo_Usuario      ] TypeIn "~VSDGCHAPA.."
               Moveto [Numero do Lancamento] TypeIn ">=~VSDGLANINI, <=~VSDGLANFIM"
               Moveto [Codigo do Emitente  ] TypeIn "~VSDGDESTINAT.."
          Else Moveto [Numero_Controle_Dia ] TypeIn "~VSDGNCONTROLE.."
       EndIf
       Do_It!
       ClearAll
EndIf

EndProc  ; (Seleciona)

;*******************************************************************************
;* Rotina para ImpressÆo                                                       *
;*******************************************************************************
Proc IMPRIME()      
Private Contador

Contador = 0

View Tabela
Scan
  Print File ARQIMP "\n"
  Print File ARQIMP Spaces(3)+"** ORDEM DE PAGAMENTO **"+"\n"
  
  Print File ARQIMP Spaces(3)+"FORNECEDOR.: "+Format("W4,AL",[Codigo do Emitente])+" - "+Format("W30,AL",[Razao Social])+"\n"
  Print File ARQIMP Spaces(3)+"DOCTO......: "+Format("W15,AL",[Documento Referente])+Spaces(4)+"ORDEM..: "+Format("W10,AL",[Numero do Documento])+"\n"
  Print File ARQIMP Spaces(3)+"VENCIMENTO.: "+Format("D13",[Data de Vencimento])+"\n"
  Print File ARQIMP Spaces(35)+"EMISSAO.: "+Format("D13",[Data de Emissao])+"\n"
  Print File ARQIMP Spaces(3)+"PLANO CONTA: "+Format("W13,AL",[Codigo Plano_Conta])+" - "+Format("W45,AL",Ltrim([Descricao Plano_Conta]))+"\n"
  Print File ARQIMP Spaces(3)+"CT. CUSTO..: "+Format("W7,AL",[Codigo CCusto])+Spaces(6)+" - "+Format("W45,AL",Ltrim([Descricao CCusto]))+"\n"
  Print File ARQIMP Spaces(3)+"VALOR......: "+Format("W14.2,EIC",[Valor do Documento])+"\n\n"
                    
  Print File ARQIMP Spaces(3)+"DESCONTO...: "+Fill("_",18)+"\n"
  Print File ARQIMP Spaces(3)+"JUROS/MULTA: "+Fill("_",18)+"\n"
  Print File ARQIMP Spaces(3)+"VALOR TOTAL: "+Fill("_",18)+"\n\n"
  
  Print File ARQIMP Spaces(3)+"OBS: "+Format("W69,AL",[Observacoes])+"\n\n" 
  
  Print File ARQIMP Fill("-",38)+Spaces(2)+Fill("-",38)+"\n"
  Print File ARQIMP Format("W38,AC","ASSINATURA")+Spaces(02)+Format("W38,AC","ASSINATURA")+"\n"

  Contador = Contador + 1
 
  If Contador = 3
     Then Contador = 0 
          Print File ARQIMP "\f"
     Else Print File ARQIMP "\n\n\n\n"
  EndIf
     
EndScan

EndProc ;(IMPRIME)

;*******************************************************************************
;* Rotina para ImpressÆo                                                       *
;*******************************************************************************
Proc IMPRIME2()
Private Contador

Contador = 0

Sort Tabela On "Numero do Documento"

View Tabela
Scan

   ENDERECO=""
   BAIRRO  =""
   CIDADE  =""
   TELEFONE=""
   RG      =""

  GetRecord "\\Sistemas\\Cadastro\\Destinat" [Codigo do Emitente] To CLIE_DADOS 
  If RetVal
     Then ENDERECO = CLIE_DADOS["Endereco" ]                
          BAIRRO   = CLIE_DADOS["Bairro"   ]
          CIDADE   = CLIE_DADOS["Cidade"   ]
          TELEFONE = CLIE_DADOS["Telefone" ]
          RG       = CLIE_DADOS["RG"       ]  
  EndIf


  Print File ARQIMP "\n\n"
  Print File ARQIMP CI_FEXP
  Print File ARQIMP CI_FNEG
  Print File ARQIMP CI_15CPP
  Print File ARQIMP Fill("=",136)+"\n"
  Print File ARQIMP CI_10CPP
  Print File ARQIMP CI_IEXP
  Print File ARQIMP CI_INEG
  
  Print File ARQIMP "         ORDEM DE PAGAMENTO"
  Print File ARQIMP CI_15CPP
  Print File ARQIMP CI_FEXP
  Print File ARQIMP CI_FNEG+"\n"
  
  Print File ARQIMP "EMISSAO: "+Format("D13",Today())+"-"+Time()+"\n"
  Print File ARQIMP Fill("-",136)+"\n"
  Print File ARQIMP "NOME..............: "+Format("W20,AL",[Razao Social])
  
  If IsAssigned(RG)
     Then Print File ARQIMP Spaces(02)+"RG: "+Format("W20,AL",RG)+"\n"
     Else Print File ARQIMP "\n" 
  EndIf
  
  Print File ARQIMP "ENDERE€O..........: " + Format("W40,AL",ENDERECO)+"\n"
  Print File ARQIMP "BAIRRO............: " + Format("W20,AL",BAIRRO)+Spaces(05)+"FONE: " + Format("W20,AL",TELEFONE)+"\n"
  Print File ARQIMP "CIDADE............: " + Format("W20,AL",CIDADE)+"\n"
  Print File ARQIMP "CENTRO DE CUSTO...: " + [Codigo CCusto]+Spaces(1)+Format("W30,AL",[Descricao CCusto])+"\n\n"
  
  ESCEXT([Valor do Documento])
  EXTENSO=" ("+SubStr(EXTENSO,2,97)+")"  
  Print File ARQIMP "VALOR DO DOCUMENTO: " + Format("W10.2,EIC",[Valor do Documento])+ Format("W100",SubStr(EXTENSO,1,100))+ "\n\n"
  
  Print File ARQIMP "Referente.........: "+Format("W15,AL",[Documento Referente])+"\n"
  
  Print File ARQIMP Format("W69,AL",[Observacoes]) 

  Print File ARQIMP "\n\n"
  
  Print File ARQIMP Spaces(01)+"                                            -------------------------------------- Valores --------------------------------------\n"
  Print File ARQIMP Spaces(01)+"          N§ Lancamento         Documento           Original            Juros/Multa               Desconto                  Total\n"
  Print File ARQIMP Spaces(01)+"       ---------------- ----------------- ------------------ ---------------------- ---------------------- ----------------------\n"
 
 Print File ARQIMP CI_15CPP
 Print File ARQIMP CI_FEXP
 Print File ARQIMP CI_FNEG
 
 Print File ARQIMP Spaces(18)+Format("W06,AL",[Numero do Lancamento])
 Print File ARQIMP Spaces(03)+Format("W04,AL",[Tipo do Documento])
 Print File ARQIMP Spaces(01)+Format("W10,AL",[Numero do Documento])
 Print File ARQIMP Spaces(06)+Format("W13.2,EIC",([Valor do Documento]+[Valor do Desconto])-[Valor de Juros])
 Print File ARQIMP Spaces(11)+Format("W12.2,EIC",[Valor de Juros])
 Print File ARQIMP Spaces(11)+Format("W12.2,EIC",[Valor do Desconto])
 Print File ARQIMP Spaces(11)+Format("W12.2,EIC",[Valor do Documento])+"\n"
 
 Print File ARQIMP CI_15CPP
 Print File ARQIMP CI_FEXP
 Print File ARQIMP CI_FNEG
 
 Print File ARQIMP Fill("-",136)+"\n\n"
  
 Print File ARQIMP Format("W136,AC","   /   /     -                                                         ")+"\n"
 Print File ARQIMP Format("W136,AC","-----------------------------------------------------------------------")+"\n"
 Print File ARQIMP Format("W136,AC","Data e Assinatura")+"\n";Spaces(17)++"\n"
  
 Print File ARQIMP Fill("=",136)
 Contador = Contador + 1
 
 If Contador = 2
     Then Contador = 0 
          Print File ARQIMP "\f"
     Else Print File ARQIMP "\n\n\n\n"
  EndIf
     
EndScan

EndProc ;IMPRIME()
 
;************************************
;* ROTINA PARA Ler/Gravacao Opcao   *
;************************************
Proc ModeloRelatoriosCarregaLista()

VSDGCODMODELO     = ""
VSDGMODELO        = ""

;ModeloRelatoriosAdd("XXX XX maximo 70 XX")

ModeloRelatoriosAdd("Criar") ; linha obrigatoria inicial
ModeloRelatoriosAdd("01  Modelo I")
ModeloRelatoriosAdd("02  Modelo II")
ModeloRelatoriosAdd("03  Modelo III")
ModeloRelatoriosAdd("Fim")
                           
EndProc ;ModeloRelatoriosCarregaLista

;*******************************************************************************
;* 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()
     Case Evento["KeyCode"] =  27:
          Temp=False
          Switch
            Case Campo="TG_EMPRESA"       : Campo="NO"
            Case Campo="TG_NCONTROLE"     : Campo="TG_EMPRESA"
            Case Campo="TG_LANINI"        : Campo="TG_NCONTROLE"
            Case Campo="TG_LANFIM"        : Campo="TG_LANINI"
            Case Campo="TG_CHAPA"         : Campo="TG_LANFIM"
            Case Campo="TG_CONTA"         : Campo="TG_CHAPA"
            Case Campo="TG_DESTINAT"      : Campo="TG_CONTA"
            Case Campo="TG_OPRELCODALF"   : Campo="TG_DESTINAT" 
            Case Campo="TG_DTINI"         : Campo="TG_OPRELCODALF"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="TG_CODMODELO"     : Campo="TG_DTFIM"
            Case Campo="YES" Or Campo="NO": Campo="TG_EMPRESA"
          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_EMPRESA"      : Campo="TG_NCONTROLE"
                    Case Campo="TG_NCONTROLE" :
                         If IsBlank(VSDGNCONTROLE)
                            Then Campo="TG_LANINI"
                            Else VSDGCHAPA      =""  VSDGNOMCHAPA      =""
                                 VSDGCONTA      =""  VSDGNOMCONTA      =""
                                 VSDGOPRELCODALF=""  VSDGNOMOPRELCODALF=""
                                 VSDGDTINI      =BlankDate()
                                 VSDGDTFIM      =BlankDate()
                                 VSDGDESTINAT   =""  VSDGNOMDEST    =""
                                 VSDGLANINI     =""  VSDGLANFIM     ="" 
                                 Campo="TG_CODMODELO"
                         EndIf
                    Case Campo="TG_LANINI"       : Campo="TG_LANFIM"
                         VSDGLANINI=ControlValue("TG_LANINI")
                         If IsBlank(VSDGLANINI)
                            Then VSDGLANINI="000001"
                            Else VSDGLANINI=ALINHM(VSDGLANINI,6)
                         EndIf
                    Case Campo="TG_LANFIM"       : Campo="TG_CHAPA"
                         VSDGLANFIM=ControlValue("TG_LANFIM")
                         If IsBlank(VSDGLANFIM)
                            Then VSDGLANFIM="999999"
                            Else VSDGLANFIM=ALINHM(VSDGLANFIM,6)
                         EndIf
                    Case Campo="TG_CHAPA"        : Campo="TG_CONTA"
                    Case Campo="TG_CONTA"        : Campo="TG_DESTINAT" 
                    Case Campo="TG_DESTINAT"     : Campo="TG_OPRELCODALF"
                    Case Campo="TG_OPRELCODALF"  : Campo="TG_DTINI" 
                         VSDGOPRELCODALF=ControlValue("TG_OPRELCODALF")
                         If IsBlank(VSDGOPRELCODALF)
                            Then VSDGOPRELCODALF="1"
                         EndIf
                         Switch
                           Case VSDGOPRELCODALF="1" : VSDGNOMOPRELCODALF="INCLUS¶O"
                           Case VSDGOPRELCODALF="2" : VSDGNOMOPRELCODALF="VENCIMENTO"
                         EndSwitch       
                    Case Campo="TG_DTINI"     : Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"     : Campo="TG_CODMODELO"
                    Case Campo="TG_CODMODELO"    : Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf

Switch
  Case Campo="TG_LANINI"      : MSG_CAMPO="Informe o Numero do Lancamento Inicial"
  Case Campo="TG_LANFIM"      : MSG_CAMPO="Informe o Numero do Lancamento Final"
  Case Campo="TG_OPRELCODALF" : MSG_CAMPO="1=InclusÆo  2=Vencimento"
EndSwitch  

Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

;*******************************************************************************
;* ROTINA PRINCIPAL                                                            *
;*******************************************************************************
AutoLib="\\Sistemas\\Cadastro\\Bib_Show,\\Sistemas\\Cadastro\\Bibliot"
LimpAguarde()

ARQIMP=PrivDir()+"RM_0400.Txt"
If IsFile(ARQIMP)
   Then Deleta_Arquivo(ARQIMP)
EndIf

VSDGEMPRESA  =""
VSDGNCONTROLE=""
VSDGCHAPA    =""
VSDGOPRELCODALF=""
VSDGCONTA    =""
VSDGDESTINAT=""
VSDGDTINI    =BlankDate()
VSDGDTFIM    =BlankDate()
VSDGLANINI   =""  
VSDGLANFIM   ="" 

ModeloRelatoriosCarregaLista()

While True
  ShowDialog "Emiss„o de Ordem de Pagamento" 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 18 Width 72
   
    Label  @ 00,00 "Empresa"  For "TG_EMPRESA"
    Accept @ 01,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_EMPRESA" To VSDGEMPRESA
    Accept @ 01,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMEMP" To VSDGNOMEMP

    Label  @ 02,00 "Controle Dia" For "TG_NCONTROLE"
    Accept @ 03,01 Width 9  "A6"  Picture "#*#" Tag "TG_NCONTROLE"    To VSDGNCONTROLE
    Accept @ 03,09 Width 25 "A30" Picture "!*!" Tag "TG_NOMNCONTROLE" To VSDGNOMNCONTROLE

    Label  @ 04,00 "Lan‡amento de" For "TG_LANINI"
    Accept @ 05,01 Width 14 "A6"  Picture "#*#" Tag "TG_LANINI" To VSDGLANINI
    Label  @ 05,15 "at‚" For "TG_LANFIM"
    Accept @ 05,20 Width 14 "A6"  Picture "#*#" Tag "TG_LANFIM" To VSDGLANFIM

    Label  @ 06,00 "Usu rio" For "TG_CHAPA"
    Accept @ 07,01 Width 07 "A4"  Picture "#*#" Tag "TG_CHAPA" To VSDGCHAPA
    Accept @ 07,09 Width 25 "A30" Picture "!*!" Tag "TG_CHAPA" To VSDGNOMCHAPA

    Label  @ 08,00 "Conta"  For "TG_CONTA"
    Accept @ 09,01 Width 07 "A4"  Picture "#*#" Tag "TG_CONTA"    To VSDGCONTA
    Accept @ 09,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMCONTA" To VSDGNOMCONTA

    Label  @ 00,35 "Pessoa"  For "TG_DESTINAT"
    Accept @ 01,36 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_DESTINAT" To VSDGDESTINAT
    Accept @ 01,44 Width 25 "A20" Picture "!*!" Tag "TG_NOMDEST"  To VSDGNOMDEST

    Label  @ 02,35 "Op‡äes de Datas" For "TG_OPRELCODALF"
    Accept @ 03,39 Width 04 "A1"  Picture "1,2" Tag "TG_OPRELCODALF" To VSDGOPRELCODALF
    Accept @ 03,44 Width 25 "A20" Picture "!*!" Tag "TG_NOMOPRELCODALF" To VSDGNOMOPRELCODALF

    Label  @ 04,35 "Per¡odo Referente de" For "TG_DTINI"
    Accept @ 05,38 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
    Label  @ 05,51 "at‚" For "TG_DTFIM"
    Accept @ 05,56 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM

    Label  @ 11,00  "Modelo de ~R~elat¢rio"  For "TG_CODMODELO"
    Accept @ 12,01  Width 06 "A3"  Picture "!*!" Tag "TG_CODMODELO" To VSDGCODMODELO
    Accept @ 12,06  Width 63 "A60"  Picture "!*!" Tag "TG_MODELO" To VSDGMODELO        

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

  EndDialog
  If Not RetVal Then QuitLoop EndIf
  if not ValidarModelosRelatorios() then loop endif         

  If IsBlank(VSDGNCONTROLE) And IsBlank(VSDGDTFIM)
     Then Mensagem("Data Final Invalida !")
          Loop
  EndIf

  DISPOSITIVO()
  If OK="" Then Loop EndIf

  AGUARDE("Aguarde, Gerando o Relat¢rio")
  Tabela=Cria_Temp(PrivDir())

  Monta_Dir_Trab(VSDGDTINI,VSDGDTFIM)
  While Novo_Dir_Trab()
    If IsTable("Answer")     Then Delete "Answer" EndIf
    Seleciona()
    If Not IsTable("Answer") Then Loop EndIf
    If IsTable(Tabela)
       Then Add "Answer" Tabela
       Else Rename "Answer" Tabela
    EndIf
  EndWhile

  If IsEmpty(Tabela)
     Then MENSAGEM("N„o existem registros a processar")
          Delete Tabela
          Loop
  EndIf
  
  Switch
    Case VSDGCODMODELO="01": CopyReport "\\Base\\Sistemas\\CtaPag\\Rm_1000" "1"  Tabela "R"
                             IMPRIREL(Tabela,"R")
    Case VSDGCODMODELO="02": IMPRIME()
                             ImpriArq(ARQIMP)
                             ;CopyReport "\\Base\\Sistemas\\CtaPag\\Rm_1000" "R"  Tabela "R"
    Case VSDGCODMODELO="03": IMPRIME2()
                             ImpriArq(ARQIMP)
  EndSwitch
  Delete Tabela

EndWhile
ClearAll
Release Vars All
Release Procs All
