;** RM_3043 ********************************************************************
;* Resumo de Vendas - Quantidade Solicitada x Quantidade Faturada              *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 24/10/2019 por Bruno Carvalho
;Ultima Alteracao:   /  /     por             
;Motivo Alteracao: 

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

If IsTable("\\Sistemas\\Vendas\\FaturaC") And IsTable("\\Sistemas\\Vendas\\Faturai")
   Then If Not IsBlank(VSDGMARCA)     Or
           Not IsBlank(VSDGFAMI)      Or
           Not IsBlank(VSDGGRP)       Or
           Not IsBlank(VSDGITEM)      Or
           Not IsBlank(VSDGREFERENCIA) 
           Then {Ask} {\\Sistemas\\Estoque\\Itens}
                Moveto [Codigo do Item   ] TypeIn "_cite, ~VSDGITEM.."
                Moveto [Referencia       ] TypeIn "~VSDGREFERENCIA.."
                Moveto [Codigo da Familia] TypeIn "~VSDGFAMI.."
                Moveto [Codigo do Grupo  ] TypeIn "~VSDGGRP.."
                Moveto [Codigo da Marca  ] TypeIn "~VSDGMARCA.."    
        EndIf
        {Ask} TypeIn "\\Sistemas\\Vendas\\FaturaC" Enter
        Moveto [Numero da OF           ]       Example "of"
        Moveto [Codigo da Empresa      ] Check Example "cemp" TypeIn ", ~VSDGEMPRESA2.."
        Moveto [Nome Fantasia Empresa  ] Check 
        Moveto [Codigo                 ] Check TypeIn "~VSDGDESTINAT.."
        Moveto [Razao Social           ] Check
        Moveto [Data de Emissao        ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
        Moveto [Descricao da Destinacao] TypeIn "..VEND.."
        Moveto [Tipo Movimento Estoque ] TypeIn "S"
        Moveto [Sistema Gerador        ] TypeIn "Not C"

        {Ask} TypeIn "\\Sistemas\\Vendas\\Faturai" Enter
        Moveto [Numero da OF           ]       Example "of"
        Moveto [Codigo do Item         ] Check
        If Not IsBlank(VSDGMARCA)     Or
           Not IsBlank(VSDGFAMI)      Or
           Not IsBlank(VSDGGRP)       Or
           Not IsBlank(VSDGITEM)      Or
           Not IsBlank(VSDGREFERENCIA)   
           Then Moveto [Codigo do Item ] Example "cite"
        EndIf
        Moveto [Descricao do Item      ] Check
        Moveto [Quantidade Solicitada  ] TypeIn "Calc Sum as Quantidade Solicitada"
        Moveto [Quantidade             ] TypeIn "Calc Sum as Quantidade"
        Do_It!
        ClearAll
        
        {Ask} {Answer} Check
        Moveto [Quantidade Solicitada  ] TypeIn ">0" 
        Do_It!
        ClearAll
        
        {Ask} {Answer} Check
        Moveto [Quantidade           ] TypeIn "_qtd"
        Moveto [Quantidade Solicitada] TypeIn "> _qtd" 
        Do_It!
        ClearAll
        
        Add "Answer" Tabela
EndIf

EndProc   ; (Seleciona)

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

VSDGCODMODELO     = ""
VSDGMODELO        = ""

ModeloRelatoriosAdd("Criar") ; linha obrigatoria inicial
ModeloRelatoriosAdd("01  Por Cliente/Item")
ModeloRelatoriosAdd("02  Por Item")
ModeloRelatoriosAdd("Fim")
                           
EndProc ;ModeloRelatoriosCarregaLista

;*******************************************************************************
;* 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_EMPRESATE"       : Campo="NO"
            Case Campo="TG_DESTINAT"      : Campo="TG_EMPRESATE"
            Case Campo="TG_ITEM"          : Campo="TG_DESTINAT"
            Case Campo="TG_REFERENCIA"    : Campo="TG_ITEM"
            Case Campo="TG_MARCA"         : Campo="TG_REFERENCIA"
            Case Campo="TG_FAMI"          : Campo="TG_MARCA"
            Case Campo="TG_GRP"           : Campo="TG_FAMI"
            Case Campo="TG_DTINI"         : Campo="TG_GRP"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="TG_CODMODELO"     : Campo="TG_DTFIM"  
            Case Campo="YES" Or Campo="NO": Campo="TG_EMPRESATE"
          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_EMPRESATE"   : Campo="TG_DESTINAT"
                    Case Campo="TG_DESTINAT"    : Campo="TG_ITEM"
                    Case Campo="TG_ITEM"        : Campo="TG_REFERENCIA"
                    Case Campo="TG_REFERENCIA"  : Campo="TG_MARCA"
                    Case Campo="TG_MARCA"       : Campo="TG_FAMI"
                    Case Campo="TG_FAMI"        : Campo="TG_GRP"
                    Case Campo="TG_GRP"         : Campo="TG_DTINI"
                    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_DTFIM":
     MSG_CAMPO="Informe a Data Final"
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGEMPRESA =""
VSDGDESTINAT=""
VSDGMARCA   =""
VSDGFAMI    =""
VSDGGRUP    =""
VSDGDTINI   =BlankDate()
VSDGDTFIM   =BlankDate()
VSDGOPRELCODALF=""

Array Array_MesAno[13]
Array Array_Tit_MesAno[13]

ModeloRelatoriosCarregaLista()

While True

  ShowDialog "Quantidade Solicitada x Quantidade Faturada" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC","TAB",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 05,12   Height 20 Width 72

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

    Label  @ 02,00 "Pessoa"  For "TG_DESTINAT"
    Accept @ 03,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_DESTINAT" To VSDGDESTINAT
    Accept @ 03,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMCLI" To VSDGNOMDEST    
    
    Label  @ 04,00 "Item"  For "TG_ITEM"
    Accept @ 05,01 Width 33 "A10" Picture "#*#" Tag "TG_ITEM"     To VSDGITEM   
    Accept @ 06,01 Width 33 "A35" Picture "!*!" Tag "TG_NOMITEM" To VSDGNOMITEM

    Label  @ 07,00 "Referˆncia"  For "TG_REFERENCIA"
    Accept @ 08,01 Width 33 "A20" Picture "!*!" Tag "TG_REFERENCIA"     To VSDGREFERENCIA

    Label  @ 09,00 "Marca"  For "TG_MARCA"
    Accept @ 10,02 Width 06 "A3"  Picture "#*#" Tag "TG_MARCA"    To VSDGMARCA
    Accept @ 10,09 Width 25 "A30" Picture "!*!" Tag "TG_NOMMARCA" To VSDGNOMMARCA

    Label  @ 11,00 "Familia"  For "TG_FAMI"
    Accept @ 12,02 Width 06 "A3"  Picture "#*#" Tag "TG_FAMI"    To VSDGFAMI
    Accept @ 12,09 Width 25 "A30" Picture "!*!" Tag "TG_NOMFAMI" To VSDGNOMFAMI

    Label  @ 00,35 "Grupo"  For "TG_GRP"
    Accept @ 01,37 Width 06 "A3"  Picture "#*#" Tag "TG_GRP"    To VSDGGRP
    Accept @ 01,44 Width 25 "A30" Picture "!*!" Tag "TG_NOMGRP" To VSDGNOMGRP

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

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

    Pushbutton @ 16,22 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 16,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(VSDGDTFIM)
     Then Mensagem("Data Final Invalida !")
          Loop
  EndIf

  If VSDGEMPRESA="0000"
     Then VSDGEMPRESA2=""
     Else VSDGEMPRESA2=VSDGEMPRESA
  EndIf
  
  DISPOSITIVO()
  If OK=""  Then Loop EndIf

  AGUARDE("Aguarde, gerando o relat¢rio")
  
  Tabela=Cria_Temp(PrivDir())
  Empty "\\Base\\Sistemas\\Vendas\\RM_3043"
  Copy "\\Base\\Sistemas\\Vendas\\RM_3043" Tabela
      
  Seleciona()
  If IsEmpty(Tabela)
     Then MENSAGEM("N„o existem registros a processar")
          Delete Tabela
          Loop
  EndIf
    
  TITULO_RELATORIO = "Quantidade Solicitada x Quantidade Faturada"+Format("D13",VSDGDTINI)+" at‚ "+Format("D13",VSDGDTFIM+30)
  Switch
    Case VSDGCODMODELO="01" : IMPRIREL(Tabela,"R")
    Case VSDGCODMODELO="02" : IMPRIREL(Tabela,"1")
  EndSwitch
  Delete Tabela
  
EndWhile
ClearAll
Release Vars All
Release Procs All
