Estou com um problema em uma tela de MVC onde ele nao esta executando a gravação de todas as linhas dos Grids.
1ª Grid
2ª Grid
3ª Grid
Sucesso
Banco de Dados
Como pode ver ja estou trazendo a tela, e ao clicar em confirmar ele ate traz a mensagem de sucesso, no entanto não grava todas as linhas no banco.
Segue fonte:
//Bibliotecas
#Include 'Protheus.ch'
#Include 'FWMVCDef.ch'
#Include "TBICONN.CH"
#Include "TopConn.ch"
Static cCampoCab := "ZG_COD;ZG_DESCRIC;ZG_PERIODO;ZG_DATAINI;ZG_DATAFIM;"
Static cCampoGrid := "ZG_TIPO;ZG_CFOP;ZG_DESCFOP;ZG_VALCONT;ZG_BASEICM;ZG_VALICM;"
//----------------------------------------------------------------------
/*/{PROTHEUS.DOC} PFISF01
FUNÇÃO PFISF01 - Tela resumo de operações por CFOP
@OWNER Pan Cristal
@VERSION PROTHEUS 12
@SINCE 27/06/2023
@Tratamento para calculo do PRODEPE
/*/
//----------------------------------------------------------------------
User Function PFISF01()
Local aArea := GetArea()
Local oBrowse
oBrowse := FWMBrowse():New()
oBrowse:SetAlias("SZG")
oBrowse:SetDescription("Operações por CFOP")
oBrowse:Activate()
RestArea(aArea)
Return
/*---------------------------------------------------------------------*
| Func: MenuDef |
| Desc: Criação do menu MVC |
| Obs.: / |
*---------------------------------------------------------------------*/
Static Function MenuDef()
Local aRot := {}
ADD OPTION aRot TITLE 'Incluir' ACTION 'VIEWDEF.PFISF01' OPERATION MODEL_OPERATION_INSERT ACCESS 0 //OPERATION 3
ADD OPTION aRot TITLE 'Visualizar' ACTION 'VIEWDEF.PFISF01' OPERATION MODEL_OPERATION_VIEW ACCESS 0 //OPERATION 1
ADD OPTION aRot TITLE 'Excluir' ACTION 'VIEWDEF.PFISF01' OPERATION MODEL_OPERATION_DELETE ACCESS 0 //OPERATION 5
Return aRot
/*---------------------------------------------------------------------*
| Func: ModelDef |
| Desc: Criação do modelo de dados MVC |
| Obs.: / |
*---------------------------------------------------------------------*/
Static Function ModelDef()
Local oModel as object
Local oStrField as object
Local oStrExclu as object
Local oStrComum as object
Local oStrOutro as object
oStrField := FWFormStruct(1, 'SZG', {|cCampo| Alltrim(cCampo) $ cCampoCab})
oStrExclu := FWFormStruct(1, 'SZG', {|cCampo| Alltrim(cCampo) $ cCampoGrid})
oStrComum := FWFormStruct(1, 'SZG', {|cCampo| Alltrim(cCampo) $ cCampoGrid})
oStrOutro := FWFormStruct(1, 'SZG', {|cCampo| Alltrim(cCampo) $ cCampoGrid})
oModel := MPFormModel():New('PFISF01M',/*bPre*/,/*bPost*/,/*bCommit*/,/*bCancel*/)
oModel:AddFields('CABEC',/*cOwner*/,oStrField/*bPre*/,/*bPos*/,/*bLoad*/)
oModel:AddGrid('EXCLU','CABEC',oStrExclu,/*bLinePre*/,/*bLinePost*/,/*bPre - Grid Inteiro*/,/*bPos - Grid Inteiro*/,/*bLoad - Carga do modelo manualmente*/)
oModel:AddGrid('COMUM','CABEC',oStrComum,/*bLinePre*/,/*bLinePost*/,/*bPre - Grid Inteiro*/,/*bPos - Grid Inteiro*/,/*bLoad - Carga do modelo manualmente*/)
oModel:AddGrid('OUTRO','CABEC',oStrOutro,/*bLinePre*/,/*bLinePost*/,/*bPre - Grid Inteiro*/,/*bPos - Grid Inteiro*/,/*bLoad - Carga do modelo manualmente*/)
oModel:SetPrimaryKey({})
oModel:SetRelation("EXCLU", {{"ZG_FILIAL", "xFilial('SZG')"},;
{"ZG_COD", "ZG_COD"},;
{"ZG_PERIODO", "ZG_PERIODO"}}, SZG->(IndexKey(1)))
oModel:SetRelation("COMUM", {{"ZG_FILIAL", "xFilial('SZG')"},;
{"ZG_COD", "ZG_COD"},;
{"ZG_PERIODO", "ZG_PERIODO"}}, SZG->(IndexKey(1)))
oModel:SetRelation("OUTRO", {{"ZG_FILIAL", "xFilial('SZG')"},;
{"ZG_COD", "ZG_COD"},;
{"ZG_PERIODO", "ZG_PERIODO"}}, SZG->(IndexKey(1)))
oModel:SetDescription("Movimentos por CFOP")
oStrField:SetProperty("ZG_COD", MODEL_FIELD_INIT, FWBuildFeature(STRUCT_FEATURE_INIPAD, "GetSXENum('SZG','ZG_COD')"))
oStrField:SetProperty("ZG_DATAFIM", MODEL_FIELD_VALID, FwBuildFeature(STRUCT_FEATURE_VALID, 'IIF(SubStr(DToC(FWFldGet("ZG_DATAINI")),4) == SubStr(DToC(FWFldGet("ZG_DATAFIM")),4),.T.,.F.)'))
oModel:AddCalc( 'CALCULOEXCLU', 'CABEC', 'EXCLU', 'ZG_VALCONT' , 'VALCONT' , 'SUM' , { || .T. },,'Total VC' )
oModel:AddCalc( 'CALCULOEXCLU', 'CABEC', 'EXCLU', 'ZG_BASEICM' , 'BASEICM' , 'SUM' , { || .T. },,'Total BC' )
oModel:AddCalc( 'CALCULOEXCLU', 'CABEC', 'EXCLU', 'ZG_VALICM' , 'VALICM' , 'SUM' , { || .T. },,'Total ICMS' )
oModel:AddCalc( 'CALCULOCOMUM', 'CABEC', 'COMUM', 'ZG_VALCONT' , 'VALCONT' , 'SUM' , { || .T. },,'Total VC' )
oModel:AddCalc( 'CALCULOCOMUM', 'CABEC', 'COMUM', 'ZG_BASEICM' , 'BASEICM' , 'SUM' , { || .T. },,'Total BC' )
oModel:AddCalc( 'CALCULOCOMUM', 'CABEC', 'COMUM', 'ZG_VALICM' , 'VALICM' , 'SUM' , { || .T. },,'Total ICMS' )
oModel:AddCalc( 'CALCULOOUTRO', 'CABEC', 'OUTRO', 'ZG_VALCONT' , 'VALCONT' , 'SUM' , { || .T. },,'Total VC' )
oModel:AddCalc( 'CALCULOOUTRO', 'CABEC', 'OUTRO', 'ZG_BASEICM' , 'BASEICM' , 'SUM' , { || .T. },,'Total BC' )
oModel:AddCalc( 'CALCULOOUTRO', 'CABEC', 'OUTRO', 'ZG_VALICM' , 'VALICM' , 'SUM' , { || .T. },,'Total ICMS' )
Return oModel
/*---------------------------------------------------------------------*
| Func: ViewDef |
| Desc: Criação da visão MVC |
| Obs.: / |
*---------------------------------------------------------------------*/
Static Function ViewDef()
Local oView as object
Local oModel as object
Local oStrCab as object
Local oStrExclu as object
Local oStrComum as object
Local oStrOutro as object
Local oStrCalcExclu as object
Local oStrCalcComum as object
Local oStrCalcOutro as object
oModel := FWLoadModel("PFISF01")
oStrCab := FWFormStruct(2, 'SZG', {|cCampo| Alltrim(cCampo) $ cCampoCab})
oStrExclu := FWFormStruct(2, 'SZG', {|cCampo| Alltrim(cCampo) $ cCampoGrid})
oStrComum := FWFormStruct(2, 'SZG', {|cCampo| Alltrim(cCampo) $ cCampoGrid})
oStrOutro := FWFormStruct(2, 'SZG', {|cCampo| Alltrim(cCampo) $ cCampoGrid})
oStrCalcExclu := FWCalcStruct(oModel:GetModel('CALCULOEXCLU'))
oStrCalcComum := FWCalcStruct(oModel:GetModel('CALCULOCOMUM'))
oStrCalcOutro := FWCalcStruct(oModel:GetModel('CALCULOOUTRO'))
oView := FwFormView():New()
oView:SetModel(oModel)
oView:SetProgressBar(.T.)
oView:AddField("VIEW_MASTER", oStrCab,"CABEC")
oView:AddGrid("VIEW_EXCLU" , oStrExclu,"EXCLU")
oView:AddGrid("VIEW_COMUM" , oStrComum,"COMUM")
oView:AddGrid("VIEW_OUTRO" , oStrOutro,"OUTRO")
oView:AddField('VIEW_CALCEXCLU' , oStrCalcExclu,'CALCULOEXCLU')
oView:AddField('VIEW_CALCCOMUM' , oStrCalcComum,'CALCULOCOMUM')
oView:AddField('VIEW_CALCOUTRO' , oStrCalcOutro,'CALCULOOUTRO')
oView:CreateHorizontalBox("BOX_CABEC", 10 )
oView:CreateHorizontalBox("BOX_MEIO", 90 )
oView:CreateFolder('ABAS','BOX_MEIO')
oView:AddSheet('ABAS', 'ABA_1', 'Exclusivo/Compra Revenda')
oView:AddSheet('ABAS', 'ABA_2', 'Comuns/Rateio')
oView:AddSheet('ABAS', 'ABA_3', 'Outros')
oView:CreateHorizontalBox("BOX_EXCLU", 90, /*cIdOwner*/, /*lUsePixel*/, 'ABAS', 'ABA_1')
oView:CreateHorizontalBox('CALCEXCLU', 10, /*cIdOwner*/, /*lUsePixel*/, 'ABAS', 'ABA_1')
oView:CreateHorizontalBox("BOX_COMUM", 90, /*cIdOwner*/, /*lUsePixel*/, 'ABAS', 'ABA_2')
oView:CreateHorizontalBox('CALCCOMUM', 10, /*cIdOwner*/, /*lUsePixel*/, 'ABAS', 'ABA_2')
oView:CreateHorizontalBox("BOX_OUTRO", 90, /*cIdOwner*/, /*lUsePixel*/, 'ABAS', 'ABA_3')
oView:CreateHorizontalBox('CALCOUTRO', 10, /*cIdOwner*/, /*lUsePixel*/, 'ABAS', 'ABA_3')
oView:setOwnerView("VIEW_MASTER", "BOX_CABEC")
oView:setOwnerView("VIEW_EXCLU", "BOX_EXCLU")
oView:SetOwnerView('VIEW_CALCEXCLU' ,'CALCEXCLU')
oView:setOwnerView("VIEW_COMUM", "BOX_COMUM")
oView:SetOwnerView('VIEW_CALCCOMUM' ,'CALCCOMUM')
oView:setOwnerView("VIEW_OUTRO", "BOX_OUTRO")
oView:SetOwnerView('VIEW_CALCOUTRO' ,'CALCOUTRO')
oView:setDescription( "" )
oView:AddOtherObject("VIEW_OTHER", {|oPanel| CABEXT(oPanel)})
oView:SetOwnerView('VIEW_OTHER', 'BOX_CABEC')
Return oView
/*---------------------------------------------------------------------*
| Func: CABEXT |
| Desc: Campo na View Cabeçalho |
| Obs.: / |
*---------------------------------------------------------------------*/
Static Function CABEXT(oPanel)
Local oBtn as object
Local oModel := FWModelActive()
Local cMsg := 'Os campos "Data Inicio" e "Data Fim", devem está preenchidos.'
Local cFont := "Arial"
Local oFontBtn := TFont():New(cFont,,-14,,.F.)
If oModel:GetOperation() == 3
oBtn := TButton():New( 009, 870,"Carregar Dados",oPanel,;
{||IIF(!Empty(FWFldGet("ZG_DATAINI")) .AND. !Empty(FWFldGet("ZG_DATAFIM")), DadosGrid(), FWAlertWarning(cMsg))},;
60,15,,oFontBtn,.F.,.T.,.F.,,.F.,,,.F. )
oBtn:setCSS( " QPushButton { background-color: rgb(244, 164, 96); border-style: outset; border-width: 1px; "+;
" border-color: black; border-radius: 10px; font-weight: bold; }")
oBtn:Refresh()
EndIf
Return
/*---------------------------------------------------------------------*
| Func: DadosGrid |
| Desc: Realiza a carga de dados nos grids da tela. |
| Obs.: / |
*---------------------------------------------------------------------*/
Static Function DadosGrid()
Local oModel := FWModelActive()
Local oView := FWViewActive()
Local oModelCabec := oModel:GetModel("CABEC")
Local oModelExclu := oModel:GetModel("EXCLU")
Local oModelComum := oModel:GetModel("COMUM")
Local oModelOutro := oModel:GetModel("OUTRO")
Local __cAlias := "TEMP"+FWTimeStamp(1)
Local cQry := ""
If !Empty(Posicione("SZG",2,FWxFilial("SZG")+Pad(SubStr(DToC(FWFldGet("ZG_DATAFIM")),4),TamSX3("ZG_PERIODO")[1]),"ZG_PERIODO"))
FWAlertWarning("Já existe o mesmo periodo cadastrado.")
oModelExclu:ClearData(.T.)
oModelComum:ClearData(.T.)
oModelOutro:ClearData(.T.)
oModel:GetModel('CALCULOEXCLU'):LoadValue("VALCONT", 0)
oModel:GetModel('CALCULOEXCLU'):LoadValue("BASEICM", 0)
oModel:GetModel('CALCULOEXCLU'):LoadValue("VALICM" , 0)
oModel:GetModel('CALCULOCOMUM'):LoadValue("VALCONT", 0)
oModel:GetModel('CALCULOCOMUM'):LoadValue("BASEICM", 0)
oModel:GetModel('CALCULOCOMUM'):LoadValue("VALICM" , 0)
oModel:GetModel('CALCULOOUTRO'):LoadValue("VALCONT", 0)
oModel:GetModel('CALCULOOUTRO'):LoadValue("BASEICM", 0)
oModel:GetModel('CALCULOOUTRO'):LoadValue("VALICM" , 0)
oView:Refresh()
Return
Else
oModelCabec:LoadValue("ZG_PERIODO", SubStr(DToC(FWFldGet("ZG_DATAFIM")),4))
EndIf
oModelExclu:ClearData(.T.)
oModelComum:ClearData(.T.)
oModelOutro:ClearData(.T.)
oModel:GetModel('CALCULOEXCLU'):LoadValue("VALCONT", 0)
oModel:GetModel('CALCULOEXCLU'):LoadValue("BASEICM", 0)
oModel:GetModel('CALCULOEXCLU'):LoadValue("VALICM" , 0)
oModel:GetModel('CALCULOCOMUM'):LoadValue("VALCONT", 0)
oModel:GetModel('CALCULOCOMUM'):LoadValue("BASEICM", 0)
oModel:GetModel('CALCULOCOMUM'):LoadValue("VALICM" , 0)
oModel:GetModel('CALCULOOUTRO'):LoadValue("VALCONT", 0)
oModel:GetModel('CALCULOOUTRO'):LoadValue("BASEICM", 0)
oModel:GetModel('CALCULOOUTRO'):LoadValue("VALICM" , 0)
cQry := " SELECT FT_FILIAL, FT_CFOP, SUM(FT_VALCONT) AS FT_VALCONT, SUM(FT_BASEICM) AS FT_BASEICM, SUM(FT_VALICM) AS FT_VALICM "
cQry += " FROM "+ RetSqlName("SFT") + " SFT "
cQry += " WHERE SFT.D_E_L_E_T_ <> '*' "
cQry += " AND SFT.FT_FILIAL = '"+FwxFilial('SFT')+"' "
cQry += " AND SFT.FT_EMISSAO BETWEEN "+ DToS(FWFldGet("ZG_DATAINI")) +" AND "+ DToS(FWFldGet("ZG_DATAFIM"))
cQry += " GROUP BY FT_FILIAL, FT_CFOP "
cQry := ChangeQuery(cQry)
IF Select(__cAlias) <> 0
(__cAlias)->(DbCloseArea())
EndIf
dbUseArea(.T.,"TOPCONN",TcGenQry(,,cQry),__cAlias,.T.,.T.)
While!(__cAlias)->(EOF())
oModelExclu:AddLine()
oModelExclu:SetValue("ZG_TIPO" , "E")
oModelExclu:SetValue("ZG_CFOP" , (__cAlias)->FT_CFOP)
oModelExclu:SetValue("ZG_DESCFOP" , FWGetSX5('13',(__cAlias)->FT_CFOP,'pt-br')[1][4])
oModelExclu:SetValue("ZG_VALCONT" , (__cAlias)->FT_VALCONT)
oModelExclu:SetValue("ZG_BASEICM" , (__cAlias)->FT_BASEICM)
oModelExclu:SetValue("ZG_VALICM" , (__cAlias)->FT_VALICM)
oView:Refresh("VIEW_EXCLU")
oView:Refresh("VIEW_CALCEXCLU")
(__cAlias)->(DBSkip())
EndDo
If Select(__cAlias) > 0
(__cAlias)->(dbCloseArea())
EndIf
cQry := " SELECT FT_FILIAL, FT_CFOP, SUM(FT_VALCONT) AS FT_VALCONT, SUM(FT_BASEICM) AS FT_BASEICM, SUM(FT_VALICM) AS FT_VALICM "
cQry += " FROM "+ RetSqlName("SFT") + " SFT "
cQry += " WHERE SFT.D_E_L_E_T_ <> '*' "
cQry += " AND SFT.FT_FILIAL = '"+FwxFilial('SFT')+"' "
cQry += " AND SFT.FT_EMISSAO BETWEEN "+ DToS(FWFldGet("ZG_DATAINI")) +" AND "+ DToS(FWFldGet("ZG_DATAFIM"))
cQry += " GROUP BY FT_FILIAL, FT_CFOP "
cQry := ChangeQuery(cQry)
IF Select(__cAlias) <> 0
(__cAlias)->(DbCloseArea())
EndIf
dbUseArea(.T.,"TOPCONN",TcGenQry(,,cQry),__cAlias,.T.,.T.)
While!(__cAlias)->(EOF())
oModelComum:AddLine()
oModelComum:SetValue("ZG_TIPO" , "C")
oModelComum:SetValue("ZG_CFOP" , (__cAlias)->FT_CFOP)
oModelComum:SetValue("ZG_DESCFOP" , FWGetSX5('13',(__cAlias)->FT_CFOP,'pt-br')[1][4])
oModelComum:SetValue("ZG_VALCONT" , (__cAlias)->FT_VALCONT)
oModelComum:SetValue("ZG_BASEICM" , (__cAlias)->FT_BASEICM)
oModelComum:SetValue("ZG_VALICM" , (__cAlias)->FT_VALICM)
oView:Refresh("VIEW_COMUM")
oView:Refresh("VIEW_CALCCOMUM")
(__cAlias)->(DBSkip())
EndDo
If Select(__cAlias) > 0
(__cAlias)->(dbCloseArea())
EndIf
cQry := " SELECT FT_FILIAL, FT_CFOP, SUM(FT_VALCONT) AS FT_VALCONT, SUM(FT_BASEICM) AS FT_BASEICM, SUM(FT_VALICM) AS FT_VALICM "
cQry += " FROM "+ RetSqlName("SFT") + " SFT "
cQry += " WHERE SFT.D_E_L_E_T_ <> '*' "
cQry += " AND SFT.FT_FILIAL = '"+FwxFilial('SFT')+"' "
cQry += " AND SFT.FT_EMISSAO BETWEEN "+ DToS(FWFldGet("ZG_DATAINI")) +" AND "+ DToS(FWFldGet("ZG_DATAFIM"))
cQry += " GROUP BY FT_FILIAL, FT_CFOP "
cQry := ChangeQuery(cQry)
IF Select(__cAlias) <> 0
(__cAlias)->(DbCloseArea())
EndIf
dbUseArea(.T.,"TOPCONN",TcGenQry(,,cQry),__cAlias,.T.,.T.)
While!(__cAlias)->(EOF())
oModelOutro:AddLine()
oModelOutro:SetValue("ZG_TIPO" , "O")
oModelOutro:SetValue("ZG_CFOP" , (__cAlias)->FT_CFOP)
oModelOutro:SetValue("ZG_DESCFOP" , FWGetSX5('13',(__cAlias)->FT_CFOP,'pt-br')[1][4])
oModelOutro:SetValue("ZG_VALCONT" , (__cAlias)->FT_VALCONT)
oModelOutro:SetValue("ZG_BASEICM" , (__cAlias)->FT_BASEICM)
oModelOutro:SetValue("ZG_VALICM" , (__cAlias)->FT_VALICM)
oView:Refresh("VIEW_OUTRO")
oView:Refresh("VIEW_CALCOUTRO")
(__cAlias)->(DBSkip())
EndDo
If Select(__cAlias) > 0
(__cAlias)->(dbCloseArea())
EndIf
oModelExclu:GoLine(1)
oModelComum:GoLine(1)
oModelOutro:GoLine(1)
Return