;** Rm_0025C  *******************************************************************
;*  Relatorio de Notas Fiscais Transportadora/Itens                            *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Ultima Alteracao: 24/12/2010 por Bruno Carvalho
;Motivo Alteracao: Ajustes Frete por Conta

;*******************************************************************************
;* Rotina p/ Selecionar e Order Registros                                      *
;*******************************************************************************
Proc Seleciona()
ClearAll
debug
If IsTable("\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC")
   Then {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC" Enter 
        Moveto [Codigo da Empresa       ] Check TypeIn "~VSDGEMPRESA.."   
        Moveto [Nome Fantasia Empresa   ] Check
        Moveto [Tipo do Documento       ] Check 
        Moveto [Numero do Documento     ] Check 
        Moveto [Codigo do Documento     ] Check               
        Moveto [Razao Social            ] Check
        Moveto [Frete por Conta         ] Check TypeIn "~VSDGFRETEPC.."
        Moveto [Valor Nota Fiscal       ] Check
        Moveto [Valor do Frete          ] Check
        Moveto [Data de Emissao         ] Check TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM" 
        Moveto [Codigo da Transportadora] Check TypeIn "~VSDGTRANSP.."
        Moveto [Nome Fantasia Transport ] Check
        Moveto [Situacao                ] TypeIn "Not C"
EndIf
Do_It!
ClearAll
EndProc ; (Seleciona)

;*******************************************************************************
;* 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_EMPRESA"       : Campo="NO"
            Case Campo="TG_TRANSP"        : Campo="TG_EMPRESA"
            Case Campo="TG_DTINI"         : Campo="TG_TRANSP"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="TG_FRETE_PC"      : Campo="TG_DTFIM"            
            Case Campo="YES" Or Campo="NO": Campo="TG_FRETE_PC"
          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_TRANSP"
                    Case Campo="TG_TRANSP"       :Campo="TG_DTINI"
                    Case Campo="TG_DTINI"        :Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"        :Campo="TG_FRETE_PC"
                    Case Campo="TG_FRETE_PC"     :
                         VSDGFRETEPC=ControlValue("TG_FRETE_PC")
                         Switch                           
                            Case VSDGFRETEPC="1": VSDGNOMFRETEPC="EMITENTE/CIF"
                            Case VSDGFRETEPC="2": VSDGNOMFRETEPC="DESTINATARIO/FOB"
                            OtherWise           : VSDGNOMFRETEPC="TODOS"
                         EndSwitch
                         Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf

Switch
  Case Campo="TG_DTFIM":
       MSG_CAMPO="Informe a Data Final"
  Case Campo="TG_FRETE_PC":
       MSG_CAMPO="Op‡”es: Em Branco=Todas  1=EMITENTE/CIF  2=DESTINATARIO/FOB"        
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGEMPRESA      =""
VSDGDESTIN       =""
VSDGTRANSP       =""
VSDGDTINI        =BlankDate()
VSDGDTFIM        =BlankDate()
VSDGFRETEPC      =""
VSDGNOMFRETEPC   =""
While True

  ShowDialog "Rela‡„o de Notas Fiscais - Transportadora" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC","TAB",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 26,26   Height 14 Width 52

    Label  @ 00,00 "Empresa"  For "TG_EMPRESA"
    Accept @ 01,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_EMPRESA"    To VSDGEMPRESA
    Accept @ 01,09 Width 23 "A20" Picture "!*!" Tag "TG_NOMEMPRESA" To VSDGNOMEMP   

    Label  @ 02,00 "Transportadora"  For "TG_TRANSP"
    Accept @ 03,01 Width 06 "A4"  Picture "[&,#][#*#]" Tag "TG_TRANSP"    To VSDGTRANSP
    Accept @ 03,08 Width 23 "A20" Picture "!*!" Tag "TG_NOMTRANSP" To VSDGNOMTRANSP
     
    Label  @ 04,00 "Perˇodo Referente" For "TG_DTINI"
    Label  @ 05,00 "de" For "TG_DTINI"
    Accept @ 05,04 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
    Label  @ 05,17 "at‚" For "TG_DTFIM"
    Accept @ 05,22 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM
     
    Label  @ 06,00 "Frete por Conta" For "TG_FRETE_PC"
    Accept @ 07,01 Width 04 "A1"  Picture "1,2" Tag "TG_FRETE_PC" To VSDGFRETEPC
    Accept @ 07,06 Width 28 "A25"  Picture "!*!" Tag "TG_NOMFRETE_PC" To VSDGNOMFRETEPC    

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

  EndDialog
  If Not RetVal Then QuitLoop EndIf

  If 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
    
  Coedit "Rm_0025C","\\Sistemas\\Ctapag\\Titulos","Answer"        
  
  Scan     
    EMPRESA =[Codigo da Empresa  ]
    TPDOCTO =[Tipo do Documento  ]
    NUMDOCTO=[Numero do Documento]

    Moveto "\\Sistemas\\Ctapag\\Titulos"
    Home

    TitOk=False
    Moveto [Codigo da Empresa]
    Locate IndexOrder by "IndTit1" EMPRESA,TPDOCTO,NUMDOCTO
    If RetVal
       Then TitOk=True
    EndIf
     
    Moveto "Rm_0025C"
    [Codigo da Empresa       ]=[Answer->Codigo da Empresa     ]
    [Nome Fantasia Empresa   ]=[Answer->Nome Fantasia Empresa ]
    [Numero do Documento     ]=[Answer->Numero do Documento   ]
    [Codigo do Documento     ]=[Answer->Codigo do Documento   ]
    [Nome Fantasia EmitDest  ]=[Answer->Razao Social          ]
    Switch
       Case [Answer->Frete por Conta ]="0": [Descricao Frete]="S/Fre"
       Case [Answer->Frete por Conta ]="1": [Descricao Frete]="Emit"  
       Case [Answer->Frete por Conta ]="2": [Descricao Frete]="Dest"                
       OtherWise                          : [Descricao Frete]="Terc"        
    EndSwitch             
    [Valor Nota Fiscal       ]=[Answer->Valor Nota Fiscal       ]
    [Valor do Frete          ]=[Answer->Valor do Frete          ]
    [Data de Emissao         ]=[Answer->Data de Emissao         ]
    [Codigo da Transportadora]=[Answer->Codigo da Transportadora]  
    [Nome Fantasia Transport ]=[Answer->Nome Fantasia Transport ]
    If TitOk
       Then
            [Numero do Lancamento    ]=[\Sistemas\Ctapag\Titulos->Numero do Lancamento ]
            [Valor Do Documento      ]=[\Sistemas\Ctapag\Titulos->Valor Do Documento   ]
            [Saldo do Documento      ]=[\Sistemas\Ctapag\Titulos->Saldo do Documento   ]
    EndIf  
    [Diferenca               ]=[Valor do Frete]-[Valor Do Documento]
    End Down          
     
    Moveto "Answer"
  EndScan
  Do_It!
  ClearAll        
        
  If IsTable("\\Sistemas\\Vendas\\Rm_0025C")
     Then {Ask} TypeIn "\\Sistemas\\Vendas\\Rm_0025C" Enter Check    
          Do_It!
          ClearAll
  EndIf
  Empty "RM_0025C"
  
  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
         
  CopyReport "\\Base\\Sistemas\\Vendas\\Rm_0025C" "R" Tabela "R"
  IMPRIREL(Tabela,"R")  
  Delete Tabela

EndWhile
ClearAll
Release Vars All
Release Procs All
