;** Rm_0062 ********************************************************************
;*  Emissao da Apuracao da Comissao usando % da Nota Fiscal                    *
;*******************************************************************************
;Autor: Visao/Vise
;Desenvolvido    : 24/12/2020 por Bruno Carvalho
;Ultima Alteracao: 14/04/2021 por Anivel Augusto Rodrigues                           
;Motivo Alteracao: Ajustes modelo 01D #0353

;*******************************************************************************
;* 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_NCARREG"       : Campo="TG_DESTINAT"
            Case Campo="TG_GERENCIA"      : Campo="TG_NCARREG"
            Case Campo="TG_REPRESEN"      : Campo="TG_GERENCIA"
            Case Campo="TG_TAXAD"         : Campo="TG_REPRESEN"
            Case Campo="TG_DTINI"         : Campo="TG_TAXAD"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="TG_OPCAO"         : Campo="TG_DTFIM"
            Case Campo="TG_DIVIDEPOR2"    : Campo="TG_OPCAO"
            Case Campo="TG_COMISSAO"      : 
                  If SYS_CODIGO_CLIENTE="0171"
                     Then Campo="TG_DIVIDEPOR2"
                     Else Campo="TG_OPCAO"
                  EndIf    
            Case Campo="TG_CODMODELO"     : Campo="TG_COMISSAO"
            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_NCARREG"  
                    Case Campo="TG_NCARREG"     : Campo="TG_GERENCIA"
                    Case Campo="TG_GERENCIA"    : Campo="TG_REPRESEN"
                    Case Campo="TG_REPRESEN"    : Campo="TG_TAXAD" 
                    Case Campo="TG_TAXAD"       : Campo="TG_DTINI"
                    Case Campo="TG_DTINI"       : Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"       : Campo="TG_OPCAO"
                    Case Campo="TG_OPCAO"       : 
                         VSDGOPCAO = ControlValue("TG_OPCAO")
                         If IsBlank(VSDGOPCAO) Then VSDGOPCAO = "1" EndIf
                         Switch
                           Case VSDGOPCAO = "1" : VSDGNOMOPCAO = "SEM RATEIO"
                           Case VSDGOPCAO = "2" : VSDGNOMOPCAO = "COM RATEIO"
                           Case VSDGOPCAO = "3" : VSDGNOMOPCAO = "APENAS RATEIO"
                         EndSwitch   
                         If SYS_CODIGO_CLIENTE="0171"
                             Then Campo="TG_DIVIDEPOR2"
                             Else Campo="TG_COMISSAO"
                         EndIf 
                    Case Campo="TG_DIVIDEPOR2"          : Campo="TG_COMISSAO"     
                    Case Campo="TG_COMISSAO"            : Campo="TG_CODMODELO" 
                    Case Campo="TG_CODMODELO"           : Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_DIVIDEPOR2":
      MSG_CAMPO="Op‡„o para dividir o valor do relat¢rio por 2"
  Case Campo="TG_OPCAO":
     MSG_CAMPO="1=SEM RATEIO  2=COM RATEIO  3=APENAS RATEIO"
  Case Campo="TG_DTFIM":
     MSG_CAMPO="Informe a Data Final"
  Case Campo="TG_COMISSAO":
       MSG_CAMPO="Defina a op‡„o"
       TECLA="Barra de Espa‡o=Marca/Desmarca  "
  Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGCODMODELO     = ""
VSDGMODELO        = ""

MSG_CAMPO="1=  2=Representante/Cidade"

ModeloRelatoriosAdd("Criar")
ModeloRelatoriosAdd("01  Representante")
ModeloRelatoriosAdd("01B Representante II")
ModeloRelatoriosAdd("01C Representante +Descontos")
ModeloRelatoriosAdd("01D Representante +Pedidos")
ModeloRelatoriosAdd("02  Representante/Cidade")
ModeloRelatoriosAdd("03  Representante+Pedidos Aberto")
ModeloRelatoriosAdd("04  Representante+Pedidos Aberto/Cidade")
ModeloRelatoriosAdd("Separator")
ModeloRelatoriosAdd("98A Representante")
ModeloRelatoriosAdd("98B Representante/Cidade")
ModeloRelatoriosAdd("Separator")
ModeloRelatoriosAdd("97  Carregamento/Representante")
ModeloRelatoriosAdd("Separator")
ModeloRelatoriosAdd("1I  Representante - Condi‡Æo de Pagamento")
ModeloRelatoriosAdd("Fim")
                           
EndProc ;ModeloRelatoriosCarregaLista()

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

sys_Valor()

If Not IsAssigned(ROTINA_SCRIPT) Then ROTINA_SCRIPT = "" EndIf  

VSDGEMPRESA          = ""
VSDGREPRESEN         = ""
VSDGGERENCIA         = ""
VSDGDESCGERENCIA     = ""
VSDGOPCAO            = ""
VSDGNOMOPCAO         = ""
VSDGDESTINAT         = ""
VSDGNOMDEST          = ""
VSDGNCARREG          = ""
VSDGNOMNCARREG       = ""
VSDGMARCA            = ""
VSDGNOMMARCA         = ""
VSDGTAXAD            = BlankNum()
VSDGDTINI            = BlankDate()
VSDGDTFIM            = BlankDate()

ModeloRelatoriosCarregaLista()

TM=80
Array DIVIDEPOR2[1]
DIVIDEPOR2[1]= False
If ROTINA_SCRIPT="RM_0062A"
   Then If SYS_CODIGO_CLIENTE="0171"
           Then DIVIDEPOR2[1]= True
                TM=38
        EndIf   
EndIf

Array TG_COMISSAO[1]
TG_COMISSAO[1]="Gera comiss„o Contas a Pagar"

Array CheckTG_COMISSAO[ArraySize(TG_COMISSAO)]
CheckTG_COMISSAO[1]=False

While True
  ShowDialog "Apura‡„o de Comiss„o p/ Repres.- % Nota Fiscal" 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_NOMDEST"  To VSDGNOMDEST
                                                                                                       
    Label  @ 04,00 "Carregamento" For "TG_NCARREG"
    Accept @ 05,01 Width 09  "A6"  Picture "[&,#][#*#]" Tag "TG_NCARREG"    To VSDGNCARREG
    Accept @ 05,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMNCARREG" To VSDGNOMNCARREG    
             
    Label  @ 06,00 "Gerˆncia " For "TG_GERENCIA"
    Accept @ 07,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_GERENCIA" To VSDGGERENCIA
    Accept @ 07,09 Width 25 "A10"  Picture "!*!" Tag "TG_DESCGERENCIA" To VSDGDESCGERENCIA

    Label  @ 08,00 "Representante"  For "TG_REPRESEN"
    Accept @ 09,01 Width 07 "A4"  Picture "#*#" Tag "TG_REPRESEN"   To VSDGREPRESEN
    Accept @ 09,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMREPR"    To VSDGNOMREPR
        
    Label  @ 10,08 "% Devoluc„o"  For "TG_TAXAD"
    Accept @ 11,09 Width 12 "N" Tag "TG_TAXAD" To VSDGTAXAD

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

    Label  @ 02,35 "Rateio de Comiss„o" For "TG_OPCAO"
    Accept @ 03,39 Width 04 "A1"  Picture "1,2,3" Tag "TG_OPCAO" To VSDGOPCAO
    Accept @ 03,44 Width 25 "A30" Picture "!*!" Tag "TG_NOMOPCAO" To VSDGNOMOPCAO
    
    CheckBoxes @06,TM Height 1 Width 26 Tag "TG_DIVIDEPOR2"
    "Dividir Valor por 2" to DIVIDEPOR2[1]
    
    CheckBoxes @ 08,35 Height 1 Width 35 Tag "TG_COMISSAO"
    TG_COMISSAO[1] to CheckTG_COMISSAO[1]        
    
    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 Valida_ShowDialog("") Then Loop EndIf
  if not ValidarModelosRelatorios() then loop endif         

  If VSDGDTFIM=BlankDate()
     Then Mensagem("Data Final Invalida !")
          Loop
  EndIf
  
  Switch
    Case VSDGOPCAO = "1" : INICIO_RATEIO = 1
                           FIM_RATEIO    = 1 
    Case VSDGOPCAO = "2" : INICIO_RATEIO = 1
                           FIM_RATEIO    = 2
    Case VSDGOPCAO = "3" : INICIO_RATEIO = 2
                           FIM_RATEIO    = 2
  EndSwitch   
    
  If VSDGEMPRESA="0000"
     Then VSDGEMPRESA2=""
     Else VSDGEMPRESA2=VSDGEMPRESA
  EndIf
  
  DISPOSITIVO()
  If OK="" Then Loop EndIf
  
  Switch
    Case VSDGCODMODELO="01"  Or
         VSDGCODMODELO="01B" Or
         VSDGCODMODELO="01C" Or
         VSDGCODMODELO="01D" Or
         VSDGCODMODELO="02"  Or
         VSDGCODMODELO="03"  Or
         VSDGCODMODELO="04"     : If Ok="A"
                                     Then IMPRIME_ARQUIVO=True
                                     Else IMPRIME_ARQUIVO=False
                                  EndIf   
                                  Play "RM_0062A" ;rm_0060
    Case VSDGCODMODELO="98A" Or 
         VSDGCODMODELO="98B"    : Play "RM_0062B" ;Rm_0060A
    Case VSDGCODMODELO="97"     : Play "RM_0062C" ;Rm_0061
    Case VSDGCODMODELO="1I"     : Play "RM_0062D" ;Rm_0060B    
  EndSwitch
EndWhile
ClearAll
Release Vars All
Release Procs All
