
if sys_codigo_cliente="0431" and substr(Directory(),1,1)="X"
   then return
endif

;** FT_CST *********************************************************************
;*  Para aplica‡äes quando estado for                                          *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 28/03/2014 por Bruno Carvalho
;Ultima Alteracao: 24/04/2014 por Bruno Carvalho
;Motivo Alteracao: Altera‡Æo do caracter "|*" - "*|" por "[*" - "*]"  
;Ultima Alteracao: 08/10/2014 por Bruno Carvalho
;Motivo Alteracao: Ajustes conforme PARAM_AGRUPA_ITEM
;Ultima Alteracao: 30/04/2019 por Anivel Augusto Rodrigues                           
;Motivo Alteracao: Ajustes mensagem automatica e truncamento             
;Ultima Alteracao: 08/10/2020 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes release vars e procs   
;Ultima Alteracao: 15/10/2020 por Bruno Carvalho
;Motivo Alteracao: Erro de Sintaxe -> "Release Proc" para "Release Procs"  

;*******************************************************************************
;* Rotina para Add Obs quando estado for selecionado                           *
;*******************************************************************************
Proc Valores()

CST      = [Tipo Tributacao ]
ALQ_ICMS = [Aliquota de ICMS]
PREC_TOT = [Preco Total     ]
BC_ICMS  = [ICMS_BC         ]
VR_ICMS  = [Valor do ICMS   ]
VR_IPI   = [Valor do IPI    ]

ACHOU = False
For wac From 1 To DynArraySize(It_cst_icms)
  If It_cst_icms[wac]=CST And It_aliq_icms[wac]=ALQ_ICMS
     Then ACHOU=True
          It_prtotal[wac] = It_prtotal[wac] + PREC_TOT
          It_bc_icms[wac] = It_bc_icms[wac] + BC_ICMS
          It_vr_icms[wac] = It_vr_icms[wac] + VR_ICMS            
          It_vr_ipi[wac]  =  It_vr_ipi[wac] + VR_IPI
          QuitLoop
  EndIf        
EndFor

If Not ACHOU
   Then wac = DynArraySize(It_cst_icms)+1
         It_cst_icms[wac] = CST
        It_aliq_icms[wac] = ALQ_ICMS
          It_prtotal[wac] = PREC_TOT
          It_bc_icms[wac] = BC_ICMS
          It_vr_icms[wac] = VR_ICMS            
           It_vr_ipi[wac] = VR_IPI
EndIf   

EndProc ;Valores()

;*******************************************************************************
;* Rotina para Add Obs quando estado for selecionado                           *
;*******************************************************************************
Proc Obs_Imposto_UF()
Private ContObs, TabAnt, FieldAnt,wac,It_cst_icms,It_aliq_icms,It_prtotal,It_bc_icms,
        It_vr_icms,It_vr_ipi, CST, ALQ_ICMS, PREC_TOT, BC_ICMS, VR_ICMS, VR_IPI, ACHOU

TabAnt = Table()
FieldAnt = Field()

If PARAM_AGRUPA_ITEM 
   Then TabelaRefI="FaturaIG"
   Else TabelaRefI="FaturaI"
EndIf

Release Vars It_cst_icms,It_aliq_icms,It_prtotal,It_bc_icms,It_vr_icms,It_vr_ipi,
             CST, ALQ_ICMS, PREC_TOT, BC_ICMS, VR_ICMS, VR_IPI, ACHOU
             
DynArray It_cst_icms[]
DynArray It_aliq_icms[]
DynArray It_prtotal[]
DynArray It_bc_icms[] 
DynArray It_vr_icms[]                
DynArray It_vr_ipi[]

ContObs = 0

If TabelaRefI="FaturaI"
   Then Moveto TabelaRefI 
        Scan
          Valores()
        EndScan
   Else Moveto "FaturaC"
        UnLockRecord
        KeyPress "F7"  

        Moveto TabelaRefI
        Moveto [Numero da OF]
        Locate [FaturaC->Numero da Of]
        If RetVal
           Then While [Numero da OF] = [FaturaC->Numero da Of]
                  Valores()
                  Skip
                  If Eot() Then QuitLoop EndIf
                EndWhile
        EndIf        
        
        Moveto "FaturaC"
        KeyPress "F7"    
EndIf

;" [** CST....: XXX  Alq.ICMS: XXX.XX  Vr.Total: XXX.XXX.XX  BC.ICMS: XXX.XXX.XX"
;"     Vr.ICMS: XX.XXX.XX              Vr.IPI..: XXX.XXX.XX             *] " 

MSG_CST_AUTOMATICA = ""
For wac From 1 To DynArraySize(It_cst_icms)
  If wac=1 
     Then MSG_CST_AUTOMATICA="[**"
     Else MSG_CST_AUTOMATICA=MSG_CST_AUTOMATICA+"\n"+"   "  
  EndIf  
  MSG_CST_AUTOMATICA = MSG_CST_AUTOMATICA + " CST....: "+Format("W3",It_cst_icms[wac])+
                                            "  Alq.ICMS: "+Format("W6.2",It_aliq_icms[wac] )+"  Vr.Total: "+Format("W10.2",It_prtotal[wac])+
                                            "  BC.ICMS: "+Format("W10.2",It_bc_icms[wac])+"\n"+"    Vr.ICMS: "+Format("W9.2",It_vr_icms[wac])+
                                            "              Vr.IPI..: "+Format("W10.2",It_vr_ipi[wac])
EndFor
If MSG_CST_AUTOMATICA<>"" Then MSG_CST_AUTOMATICA = MSG_CST_AUTOMATICA + Spaces(12)+" **] "+"\n" EndIf

Moveto "FaturaC"

If MSG_CST_AUTOMATICA<>""
  Then [Observacao na Nota Fiscal]=[Observacao na Nota Fiscal]+MSG_CST_AUTOMATICA
EndIf

Moveto TabAnt
Moveto Field FieldAnt

Release Vars It_cst_icms,It_aliq_icms,It_prtotal,It_bc_icms,It_vr_icms,It_vr_ipi,
             CST, ALQ_ICMS, PREC_TOT, BC_ICMS, VR_ICMS, VR_IPI, ACHOU

Return MSG_CST_AUTOMATICA 

EndProc ;Obs_Imposto_UF() 

;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************

Obs_Imposto_UF()

Release Procs Valores, Obs_Imposto_UF
