Páginas filhas
  • Smart View - Objeto de negócio Localizado

Versões comparadas

Chave

  • Esta linha foi adicionada.
  • Esta linha foi removida.
  • A formatação mudou.

...

Bloco de código
titleFonte padrão
collapsetrue
#include "msobject.ch"
#include "protheus.ch"
#include "apcfg021.ch"
#include "totvs.framework.treports.integratedprovider.th"
   
namespace custom.materiais

//-------------------------------------------------------------------
/*{Protheus.doc} MATR190TReportsBusinessObject
Classe para criação do Objeto de Negócio de Prod  x Forn para o TReports

@author Vanessa Ruama
@since 02/03/2023
@version 1.0
*/
//-------------------------------------------------------------------   
@totvsFrameworkTReportsIntegratedProvider(active=.T., tables="SA5", country="ALL")
class MATR190TReportsBusinessObject from totvs.framework.treports.integratedprovider.IntegratedProvider
    public method new() as object
    public method getDisplayName() as character
    public method getDescription() as character
    public method getData() as object
    public method getSchema() as object

    protected data aFields as array
    protected data aStruct as array
    protected data cQuery as character
    protected data cWhere as character
    protected data jItems as json

endclass

//-------------------------------------------------------------------
/*{Protheus.doc} new
Método de instância da classe

@return object: self

@author Vanessa Ruama
@since 02/03/2023
@version 1.0
*/
//-------------------------------------------------------------------    
method new() class MATR190TReportsBusinessObject
_Super:new()
self:appendArea("Compras")

self:aFields := {"A5_LOJA", "A5_NOMEFOR", "A5_PRODUTO"}

return self

//-------------------------------------------------------------------
/*{Protheus.doc} getDisplayName
Retorna o nome de exibição do objeto de negócio

@return string

@author Vanessa Ruama
@since 02/03/2023
@version 1.0
*/
//-------------------------------------------------------------------    
method getDisplayName() as character class MATR190TReportsBusinessObject
return "Produtos x Fornecedores - Localizado"

//-------------------------------------------------------------------
/*{Protheus.doc} getDescription
Retorna a descrição do objeto de negócio

@return string

@author Vanessa Ruama
@since 02/03/2023
@version 1.0
*/
//-------------------------------------------------------------------   
method getDescription() as character class MATR190TReportsBusinessObject
return "Produtos x Fornecedores"

//-------------------------------------------------------------------
/*{Protheus.doc} getData
Retorna os dados do objeto de negócio

@param nPage, numérico, indica a página atual do relatório
@param oFilter, objeto, contém o filtro do TReports

@return object: self:oData

@author Vanessa Ruama
@since 02/03/2023
@version 1.0
*/
//-------------------------------------------------------------------    
method getData(nPage as numeric, oFilter as object) as object class MATR190TReportsBusinessObject
local cAlias as character
local nSkip as numeric
local nCount as numeric
local nX as numeric
local aPDFields as array
local cFieldsQry as character
 
nCount := 0 
cFieldsQry := ArrTokStr(self:aFields, ",")

self:cQuery := "SELECT "
self:cQuery += cFieldsQry
self:cQuery += " FROM " + RetSQLName("SA5")

if !empty(self:cWhere)
    self:cQuery += self:cWhere //Where que foi populado no objeto localizado
endif

//Os filtros serão setados na interface do novo TReports
if oFilter:hasFilter()
    self:cQuery += " AND " + oFilter:getSQLExpression()
endif
 
cAlias := MPSysOpenQuery(self:cQuery)
 
if nPage == 1
    (cAlias)->(dbGoTop())
else
    //Encontra a quantidade de itens que iráirá pular de acordo com a páginapágina atual
    nSkip := ((nPage - 1) * self:getPageSize())      
 
    (cAlias)->(dbSkip(nSkip))
endif   
 
//Verifica se precisa fazer o tratamento para LGPD
aPDFields := FwProtectedDataUtil():UsrAccessPDField(__cUserID, self:aFields)
lObfuscated := len( aPDFields ) != Len(self:aFields)

while !(cAlias)->(Eof())
    self:jItems := JsonObject():new()

    for nX := 1 To Len(self:aStruct)
        if lObfuscated .and. aScan(aPDFields, self:aStruct[nX][5]) == 0
            self:jItems[self:aStruct[nX][1]] := FwProtectedDataUtil():ValueAsteriskToAnonymize((cAlias)->&(self:aStruct[nX][5]))
        else
            self:jItems[self:aStruct[nX][1]] := (cAlias)->&(self:aStruct[nX][5])
        endif
    next nX

    self:processData()
    self:oData:appendData(self:jItems)

    (cAlias)->(DBSkip())
    nCount++
 
    //Sai do loop quando chegar no tamanho de itens da página
    if nCount == self:getPageSize()
      exit
    endif
enddo  
 
//Se não for o último registro indica que terá próxima página
self:setHasNext(!(cAlias)->(Eof()))  
 
(cAlias)->(DBCloseArea())
   
return self:oData

//-------------------------------------------------------------------
/*{Protheus.doc} getSchema
Retorna a estrutura dos campos

@return object: self:oSchema

@author Vanessa Ruama
@since 02/03/2023
@version 1.0
*/
//-------------------------------------------------------------------    
method getSchema() as object class MATR190TReportsBusinessObject
Local nX as numeric

self:aStruct := getStruct(self:aFields)

for nX := 1 To Len(self:aStruct)
    self:addProperty(self:aStruct[nX][1], self:aStruct[nX][2], self:aStruct[nX][3], self:aStruct[nX][4], self:aStruct[nX][5])
Next nX
 
return self:oSchema

//-------------------------------------------------------------------
/*{Protheus.doc} getStruct
Prepara a estrutura dos campos

@param aCpos array: Array com os campos do relatório

@return array: Array com a estrutura dos campos

@author Vanessa Ruama
@since 02/03/2023
@version 1.0
*/
//-------------------------------------------------------------------  
function getStruct(aCpos)
Local aDeParaCpo as array
Local aCpoTmp    as array
Local cCampo     as character
Local cCpoQry    as character
Local cTipR      as character
Local nPos       as numeric
Local nC         as numeric

aDeParaCpo := {{"C", "string"}, {"D", "date"}, {"N", "number"}, {"L", "boolean"}}
aCpoTmp    := {}

for nC := 1 to Len(aCpos)
    cCpoQry := aCpos[nC]
    nPos    := AT(".", aCpos[nC]) + 1
    
    if nPos > 0
        cCampo := Substr(cCpoQry, nPos)
    else
        cCampo := cCpoQry
    endif
    
    cTipo := GetSx3Cache(cCampo, "X3_TIPO")
    
    if (nPos := aScan(aDeParaCpo, {|c| c[01] = cTipo})) > 0
        cTipR := aDeParaCpo[nPos, 02]
    else
        cTipR := "string"
    endif

    AAdd(aCpoTmp, {strTran(cCampo, "_", ""), FWSX3Util():GetDescription(cCampo), cTipR, FWSX3Util():GetDescription(cCampo), cCampo})
next nC

return (aCpoTmp)

...