#Include 'Protheus.ch'
User Function GFEA1155()
Local aOldArea := GetArea()
Local cNumeroNF := ""
Local lRet := .F.
Local cAliasQry, cAliasAux
Local cPB5CDTPDE, cPB5EMISDE, cPB5SERDCE, cPB5NRDCE
If GU3->( dbSeek(xFilial("GU3") + GXG->GXG_CDDEST) )
If GU3->GU3_EMFIL == "1" // Se não for filial é Nota Fiscal de compra e o emissor é um fornecedor
If Len(AllTrim(GXH->GXH_NRDC)) == 8
cNumeroNF := SubStr(GXH->GXH_NRDC,2,7)
Else
cNumeroNF := GXH->GXH_NRDC
EndIf
Store '' to cPB5CDTPDE, cPB5EMISDE, cPB5SERDCE, cPB5NRDCE
cAliasQry := GetNextAlias()
cQuery := "SELECT PB5.PB5_FILIAL, PB5.PB5_CDTPDE, PB5.PB5_EMISDE, PB5.PB5_SERDCE, PB5.PB5_NRDCE "
cQuery += " FROM "+RetSQLName("PB5")+" PB5 "
cQuery += " WHERE PB5.PB5_CDTPDS = '" + GXH->GXH_TPDC + "'"
cQuery += " AND PB5.PB5_EMISDS = '" + GXH->GXH_EMISDC + "'"
cQuery += " AND PB5.PB5_SERDCS = '" + GXH->GXH_SERDC + "'"
cQuery += " AND PB5.PB5_NRDCS = '" + AllTrim(cNumeroNF) + "'"
cQuery += " AND PB5.D_E_L_E_T_ <> '*'"
cQuery := ChangeQuery(cQuery)
dbUseArea( .T., "TOPCONN", TCGENQRY(,,cQuery),cAliasQry, .F., .T.)
dbSelectArea(cAliasQry)
(cAliasQry)->(dbGoTop())
If !(cAliasQry)->(Eof())
cPB5CDTPDE := (cAliasQry)->PB5_CDTPDE
cPB5EMISDE := (cAliasQry)->PB5_EMISDE
cPB5SERDCE := (cAliasQry)->PB5_SERDCE
cPB5NRDCE := (cAliasQry)->PB5_NRDCE
Else
cAliasAux := GetNextAlias()
cQuery := "SELECT PB5.PB5_FILIAL, PB5.PB5_CDTPDE, PB5.PB5_EMISDE, PB5.PB5_SERDCE, PB5.PB5_NRDCE "
cQuery += " FROM "+RetSQLName("PB5")+" PB5 "
cQuery += " WHERE PB5.PB5_EMISDS = '" + GXG->GXG_CDDEST + "'"
cQuery += " AND PB5.PB5_SERDCS = '" + GXH->GXH_SERDC + "'"
cQuery += " AND PB5.PB5_NRDCS = '" + AllTrim(cNumeroNF) + "'"
cQuery += " AND PB5.D_E_L_E_T_ <> '*'"
cQuery := ChangeQuery(cQuery)
dbUseArea( .T., "TOPCONN", TCGENQRY(,,cQuery),cAliasAux, .F., .T.)
dbSelectArea(cAliasAux)
(cAliasAux)->(dbGoTop())
If (cAliasAux)->(Eof())
cPB5CDTPDE := (cAliasAux)->PB5_CDTPDE
cPB5EMISDE := (cAliasAux)->PB5_EMISDE
cPB5SERDCE := (cAliasAux)->PB5_SERDCE
cPB5NRDCE := (cAliasAux)->PB5_NRDCE
EndIf
(cAliasAux)->(dbclosearea())
EndIf
(cAliasQry)->(dbclosearea())
If !Empty(cPB5CDTPDE)
dbSelectArea("GXH")
RecLock("GXH", .F.)
GXH->GXH_TPDC := cPB5CDTPDE
GXH->GXH_EMISDC := cPB5EMISDE
GXH->GXH_SERDC := cPB5SERDCE
GXH->GXH_NRDC := cPB5NRDCE
GXH->(MsUnlock())
dbSelectArea("GW1")
dbSetOrder(1)
If dbSeek(GXH->GXH_FILDC+GXH->GXH_TPDC+GXH->GXH_EMISDC+GXH->GXH_SERDC+GXH->GXH_NRDC)
lRet := .t.
EndIf
EndIf
EndIf
EndIf
RestArea(aOldArea)
Return lRet