;** Ft_Comis *******************************************************************
;*  Manutencao do Rateio de Comissao por Item ou Nota                          *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 11/03/2004 por Karen Lauar Nery
;Ultima Alteracao: 10/11/2005 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes  Rateio Total

;*******************************************************************************
;* Rotina p/ iniciar variaveis p/ showdialog                                   *
;*******************************************************************************
Proc Inicia_Vars()
CHAVECO         =1
VSD_I_Represent =""
VSD_I_NRepresent=""
VSD_I_Comissao  =""
EndProc

;*******************************************************************************
;* Rotina p/ organizar array                                                   *
;*******************************************************************************
Proc Organiza_Array()
Private i,c, Array_Temp

If IsAssigned(Array_Temp) Then Release Vars Array_Temp EndIf
DynArray Array_Temp[]
c=0
For i From 1 To DynArraySize(Array_VCOMISSOES)
  If SubStr(Array_VCOMISSOES[i],1,4)<>""
     Then c=c+1
          Array_Temp[c]=Array_VCOMISSOES[i]
  EndIf
EndFor

Release Vars Array_VComissoes
DynArray Array_VCOMISSOES[]
For i From 1 To DynArraySize(Array_Temp)
  Array_VCOMISSOES[i]=Array_Temp[i]
EndFor

Release Vars Array_Temp
EndProc

;*******************************************************************************
;* Rotina de Controle p/ ShowDialog                                            *
;*******************************************************************************
Proc Sd_DialogC(Disparo,Campo,Evento,Elemento)
Temp=True
If Disparo="EVENT" And DynArraySize(Evento)>0 And Evento["Type"]="KEY" Then
   Switch
      Case Evento["KeyCode"] = 9:
           Temp=False
      Case Evento["KeyCode"] = -59:
          If Campo="I_Represent"
             Then Dynarray WinArray5[]
                  Window Handle Dialog to JANELA
                  Window GetAttributes JANELA To WinArray5
                  WinArray5["FLOATING"]=False
                  Window SetAttributes JANELA From WinArray5

                  If Campo="I_Represent"       Then Moveto [Codigo do Representante] EndIf
                  echo off
                  ConsultaF1_SemRetorno()

                  Window Select JANELA
                  WinArray5["FLOATING"]=True
                  Window SetAttributes JANELA From WinArray5

                  echo Normal
                  echo off

                  If Campo="I_Represent" And Resultado_F1
                     Then VSD_I_Represent     =Registro_F1["Codigo do Representante"]
                          VSD_I_NRepresent    =Registro_F1["Nome Fantasia Represent"]
                  EndIf
         EndIf
         Temp=False
       Case Evento["KeyCode"] =-19                          : Campo="TG_VCOMISSOES"
       Case Evento["KeyCode"] =-147  And Campo="TG_VCOMISSOES":
						If DynArraySize(Array_VCOMISSOES)>0
							 Then Confirma()
										If OK="S"
											 Then Array_VCOMISSOES[VSDGVCOMISSOES]=""
														Organiza_Array()
										EndIf
            EndIf
       Case Evento["KeyCode"] =-147 And Campo="I_Comissao":
            If CHAVECO=0
               Then Confirma()
                    If OK="S"
                       Then Array_VCOMISSOES[INDICECO]=""
                            Organiza_Array()
                            Inicia_Vars()
                            Campo="I_Represent"
                    EndIf
            EndIf
      Case Evento["KeyCode"] = 27:
           Switch
             Case Campo="I_Represent"  : CancelDialog
             Case Campo="I_Comissao"   : Campo="I_Represent"
                  Inicia_Vars()
             Case Campo="TG_VCOMISSOES": Campo="I_Represent"
                  Inicia_Vars()
           EndSwitch
      Case Evento["KeyCode"] = 13:
           Switch
             Case Campo="I_Represent"  :
                  VSD_I_Represent=ControlValue("I_Represent")
                  If Not IsBlank(VSD_I_Represent)
                     Then VSD_I_Represent=AlinhM(VSD_I_Represent,4)
                          INDICECO=0
                          CHAVECO =1
                          For FCI From 1 To DynArraySize(Array_VCOMISSOES)
                            If SubStr(Array_VCOMISSOES[FCI],1,4)=VSD_I_Represent
                               Then CHAVECO =0
                                    INDICECO=FCI
                                    VSD_I_Represent =SubStr(Array_VCOMISSOES[FCI],1,4)
                                    VSD_I_NRepresent=RTrim(SubStr(Array_VCOMISSOES[FCI],06,20))
                                    VSD_I_Comissao  =NumVal(SubStr(Array_VCOMISSOES[FCI],28,08))
                                    QuitLoop
                            EndIf
                          EndFor

                          If CHAVECO=1
                             Then Moveto [Codigo do Representante]
                                  Ativa_Lookup()
                                  Moveto [Codigo do Representante]
                                  Locate VSD_I_Represent
                                  If RetVal
                                     Then VSD_I_NRepresent=[Nome Fantasia Represent  ]
                                          VSD_I_Comissao  =[Comissao do Representante]
                                          KeyPress "ESC"
                                          Campo="I_Comissao"
                                     Else Mensagem("Representante n„o Cadastrado !")
                                          VSD_I_Represent=""
                                          KeyPress "ESC"
                                  EndIf
                                  Echo Off
                             Else Campo="I_Comissao"
                          EndIf
                     Else Campo="TG_VCOMISSOES"
                   EndIf
             Case Campo="I_Comissao"   :
                  VSD_I_Comissao=ControlValue("I_Comissao")
                  If IsBlank(VSD_I_Comissao)
                     Then VSD_I_Comissao=0
                  EndIf

                  If INDICECO>0
                     Then FCI=INDICECO
                     Else FCI=DynArraySize(Array_VCOMISSOES)+1
                  EndIf
                  Array_VCOMISSOES[FCI]=VSD_I_Represent+" "+
                                        Completa(VSD_I_NRepresent,22," ")+
                                        Format("W08.2",VSD_I_Comissao)

                  Inicia_Vars()
                  Campo="I_Represent"
             Case Campo="TG_VCOMISSOES" :
									If DynArraySize(Array_VCOMISSOES)>0
										 Then INDICECO=NumVal(VSDGVCOMISSOES)
													VSD_I_Represent =SubStr(Array_VCOMISSOES[VSDGVCOMISSOES],1,4)
													VSD_I_NRepresent=RTrim(SubStr(Array_VCOMISSOES[VSDGVCOMISSOES],6,20))
													VSD_I_Comissao	=NumVal(SubStr(Array_VCOMISSOES[VSDGVCOMISSOES],28,08))

													Campo="I_Comissao"
									EndIf
           EndSwitch
   EndSwitch
EndIf

SelectControl Campo
TECLA=" Esc=Retornar  "

If Campo="I_Represent"
   Then TECLA=TECLA+"F1=Tabelas  F8=Rela‡„o  "
EndIf

If CHAVECO=0
   Then TECLA=TECLA+"Ctrl+Del=Excluir  "
EndIf

If Campo="TG_VCOMISSOES"
   Then TECLA=TECLA+"Enter=Editar  Ctrl+Del=Excluir  "+"=Selecionar  "
EndIf

@ 24,0 ?? TECLA+Spaces(80-Len(TECLA))
PaintCanvas Attribute 31 24,0,24,79
MSGCAMPO=""
Message(MSGCAMPO)
RefreshDialog
Return TEMP
EndProc     ;  (Geral_Dialog)

;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************
Proc Rotina_Inicial()
If PARAM_RATEIO_COMISSAO="I"
   Then If VSD_I_Tipo_Item<>"C"
           Then Return
        EndIf
EndIf

If APLIC_ORIGEM="NOTA"
   Then Moveto "NotaC"
   Else Moveto "FaturaC"
EndIf
UnLockRecord
FormKey

If IsAssigned(Array_VCOMISSOES) Then Release Vars Array_VComissoes EndIf
DynArray Array_VCOMISSOES[]
FCI=0

If APLIC_ORIGEM="NOTA"
   Then Moveto "NotaIC"
        Moveto [Codigo da Empresa]
        Locate EMPRESA,NUMDOC,SERIE,SEQIT
        If RetVal
           Then While [Codigo da Empresa  ]=EMPRESA And
                      [Numero do Documento]=NUMDOC  And
                      [Serie do Documento ]=SERIE   And
                      [Item do Documento  ]=SEQIT
                  FCI=FCI+1
                  Array_VCOMISSOES[FCI]=[Codigo do Representante]+" "+
                                        Completa([Nome Fantasia Represent],22," ")+
                                        Format("W08.2",[Comissao do Representante])
                  Skip
                  If EOT() Then QuitLoop EndIf
                EndWhile
        EndIf
        Moveto "NotaC"
   Else Moveto "FaturaIC"
        Moveto [Numero da OF]
        Locate ORDFAT,SEQIT
        If RetVal
           Then While [Numero da OF]=ORDFAT And [Item da OF]=SEQIT
                  FCI=FCI+1
                  Array_VCOMISSOES[FCI]=[Codigo do Representante]+" "+
                                        Completa([Nome Fantasia Represent],22," ")+
                                        Format("W08.2",[Comissao do Representante])
                  Skip
                  If EOT() Then QuitLoop EndIf
                EndWhile
        EndIf
        Moveto "FaturaC"
EndIf
LockRecord
FormKey


If PARAM_RATEIO_COMISSAO="I"
   Then TITULO1="Rateio Comissao Item ["+StrVal(SEQIT)+"]"
        TITULO2="[ "+VSD_I_Descricao_Item+" ]"
   Else TITULO1="Rateio Comissao Nota"
        TITULO2=""
EndIf

;Echo Normal
;Echo Off

Inicia_Vars()
Showdialog TITULO1 Proc "Sd_DialogC"
   Trigger "OPEN","ARRIVE"
    Key "F1","TAB","ReverseTab","ENTER","ESC","ClearArea",-66,-147
     @ 07,12 height 12 width 51

   @ 0,1 ?? TITULO2

   Label  @ 1,00 "Representante" for "I_Represent"
   Accept @ 2,01  Width 8  "A4"  picture "#*#" Tag "I_Represent"  to VSD_I_Represent
   Accept @ 2,10  Width 26 "A20" picture "!*!" Tag "I_NRepresent" to VSD_I_NRepresent

   Label  @ 1,37 "Comiss„o" for "I_COMISSAO"
   Accept @ 2,37  Width 9  "N" picture "#*#.[##]" Tag "I_Comissao" to VSD_I_Comissao

   Frame Single From 03,00 to 09,47
   Label @ 03,01 "~R~elac„o"  For "TG_VCOMISSOES"
   PickDynArray @4,01 Height 5 width 45
    Columns 1
     Array_VCOMISSOES
      Tag "TG_VCOMISSOES"
       To VSDGVCOMISSOES

EndDialog
UnLockRecord
FormKey

If APLIC_ORIGEM="NOTA"
	 Then Moveto "NotaIC"
				Moveto [Codigo da Empresa]
				Locate EMPRESA,NUMDOC,SERIE,SEQIT
				If RetVal
           Then While [Codigo da Empresa  ]=EMPRESA And
                      [Numero do Documento]=NUMDOC  And
                      [Serie do Documento ]=SERIE   And
                      [Item do Documento  ]=SEQIT
                  Del
                EndWhile
				EndIf
   Else Moveto "FaturaIC"
        Moveto [Numero da OF]
        Locate ORDFAT,SEQIT
        If RetVal
           Then While [Numero da OF]=ORDFAT And [Item da OF]=SEQIT
                  Del
                EndWhile
        EndIf
EndIf

For FCI From 1 To DynArraySize(Array_VCOMISSOES)
  If SubStr(Array_VCOMISSOES[FCI],1,4)=""
     Then QuitLoop
     Else End Down
          If APLIC_ORIGEM="NOTA"
             Then [Codigo da Empresa  ]=EMPRESA
                  [Numero do Documento]=NUMDOC
                  [Serie do Documento ]=SERIE
                  [Item do Documento  ]=SEQIT
             Else [Numero da OF       ]=ORDFAT
                  [Item da OF         ]=NumVal(SEQIT)
          EndIf
          [Codigo do Representante    ]=SubStr(Array_VCOMISSOES[FCI],1,4)
          [Comissao do Representante  ]=NumVal(SubStr(Array_VCOMISSOES[FCI],28,08))
          UnLockRecord
  EndIf
EndFor

If APLIC_ORIGEM="NOTA"
   Then Moveto "NotaC"
   Else Moveto "FaturaC"
EndIf
UnLockRecord
FormKey

If APLIC_ORIGEM="NOTA"
   Then Moveto "NotaC"
				Moveto [Codigo da Empresa]
        Locate EMPRESA,NUMDOC,SERIE
   Else Moveto "FaturaC"
        Moveto [Numero da OF]
        Locate ORDFAT
EndIf
LockRecord

EndProc

;*** Inicio ****
Rotina_Inicial()

Release Vars Array_VCOMISSOES, FCI, CHAVECO, INDICECO, TITULO1, TITULO2
Release Procs Sd_DialogC, Inicia_Vars, Organiza_Array, Rotina_Inicial


