Você está na página 1de 6

#include "protheus.

ch"
//Principal
User Function funcM2()
Local cAlias1 := "SC1"
Private cCadastro := "Modelo 2 SC1"
Private aRotina := {}
AADD( aRotina, {"Pesquisar", "AxPesqui", 0,
AADD( aRotina, {"Visualizar", 'U_Mod2Mont',
AADD( aRotina, {"Incluir", 'U_Mod2Incl', 0,
AADD( aRotina, {"Alterar", 'U_Mod2Mont', 0,
AADD( aRotina, {"Excluir", 'U_Mod2Mont', 0,

1})
0, 2})
3})
4})
5})

dbSelectArea( cAlias1 )
dbSetOrder(1)
MBrowse(6,1,22,75, cAlias1)
Return
//Rotina para
User Function
Local
Local
Local
Local
Local
Local

incluir dados:
Mod2Incl( cAlias1, nReg, nOpc )
oDlg
oGet
oTPanel1
oTPanel2
nOpcA
nOpcB

Local aPosObj,aSize
Private aHeader :=
Private aCOLS
Private aREG

{}
:=
:=

{}
{}

Private aCamp := {"C1_NUM","C1_SOLICIT","C1_EMISSAO","C1_UNIDREQ","C1_CO


DCOMP","C1_FILENT"}
RegToMemory(cAlias1, If(nOpc==3,.T.,.F.))
dbSelectArea( cAlias1 )
dbSetOrder(1)
Mod2aHeader( cAlias1 )
Mod2aCOLS( cAlias1, nReg, 3 )
PosObjetos(@aSize,@aPosObj)
DEFINE MSDIALOG oDlg Title cCadastro FROM aSize[7],0 TO aSize[6],aSize[5
] OF oMainWnd PIXEL
Enchoice(cAlias1,nReg,nOpc,,,,aCamp,aPosObj[1],,3,,,,oDlg)
oGet := MSGetDados():New(aPosObj[2,1],aPosObj[2,2],aPosObj[2,3],aPosObj[
2,4],nOpc,"u_Mod2LineOK()","U_Mod2TudoOK()","+C1_ITEM",.T.,,,,200,,,,"u_DelItt()
",oDlg)
ACTIVATE MSDIALOG oDlg CENTER ON INIT ;
EnchoiceBar(oDlg, {||nOpcA := if( U_Mod2TudoOK(),1,0),;
if (nOpcA==1,oDlg:End(),)}, {||nOpcB := -1, oDlg:End() })

If (nOpcA == 1 )
Mod2GrvI()
Endif
If (nOpcB == -1)
Rollbacksxe()
Endif
Return
//Rotina de Visualizao, Alterao e Excluso
User Function Mod2Mont( cAlias1, nReg, nOpc )
Local
Local
Local
Local

oDlg
oGet
aPosObj
aSize

Local
Local
Local
Local
Local

cSolicitante
dDataEmissao
cUnidReq
cCodCompr
cFilialEntrega

Private
Private
Private
Private

CA110NUM
aHeader :=
aCOLS
aREG

:=
:=
:=
:=
:=

TamSx3("C1_SOLICIT")[1]
Ctod(Space(8))
TamSx3("C1_UNIDREQ")[1]
TamSx3("C1_CODCOMP")[1]
TamSx3("C1_FILENT")[1]
:= TamSx3("C1_NUM")[1]

{}
:=
:=

{}
{}

Private aCamp := {"C1_NUM","C1_SOLICIT","C1_EMISSAO","C1_UNIDREQ","C1_CO


DCOMP","C1_FILENT"}
RegToMemory(cAlias1, If(nOpc==3,.T.,.F.))
dbSelectArea( cAlias1 )
dbGoTo( nReg )
CA110NUM
:= SC1->C1_NUM
cSolicitante
:= SC1->C1_SOLICIT
dDataEmissao
:= SC1->C1_EMISSAO
cUnidReq
:= SC1->C1_UNIDREQ
cCodCompr
:= SC1->C1_CODCOMP
cFilialEntrega := SC1->C1_FILENT
Mod2aHeader( cAlias1 )
Mod2aCOLS( cAlias1, nReg, nOpc )
PosObjetos(@aSize,@aPosObj)
DEFINE MSDIALOG oDlg Title cCadastro FROM aSize[7],0 TO aSize[6],aSize[5
] OF oMainWnd PIXEL
Enchoice(cAlias1,nReg,nOpc,,,,aCamp,aPosObj[1],,3,,,,oDlg)
If nOpc == 4
oGet := MSGetDados():New(aPosObj[2,1],aPosObj[2,2],aPosO
bj[2,3],aPosObj[2,4],nOpc,"u_Mod2LineOK()","U_Mod2TudoOK()","+C1_ITEM",.T.)
Else
oGet := MSGetDados():New(aPosObj[2,1],aPosObj[2,2],aPosO
bj[2,3],aPosObj[2,4],nOpc)

Endif
ACTIVATE MSDIALOG oDlg CENTER ON INIT ;
EnchoiceBar( oDlg, {|| ( IIF( nOpc == 4 .And. U_Mod2TudoOK(), Mod2GrvA()
, IIF( nOpc == 5, Mod2GrvE(), oDlg:End() ) ), ;
oDlg:End() ) }, {|| oDlg:End()} )
Return
//Montagem do array aHeader
Static Function Mod2aHeader( cAlias1 )
Local aArea := GetArea()
dbSelectArea("SX3")
dbSetOrder(1)
dbSeek( cAlias1 )
While !EOF() .And. X3_ARQUIVO == cAlias1
//Validar campos
If X3Uso(X3_USADO) .And. cNivel >= X3_NIVEL .And. Alltrim(x3_cam
po)<>"C1_NUM" .And. Alltrim(x3_campo)<>"C1_SOLICIT" .And. Alltrim(x3_campo)<>"C1
_EMISSAO" .And. Alltrim(x3_campo)<>"C1_UNIDREQ" .And. Alltrim(x3_campo)<>"C1_COD
COMP" .And. Alltrim(x3_campo)<>"C1_FILENT"
AADD( aHeader, { Trim( X3Titulo() ), ;
X3_CAMPO,;
X3_PICTURE,;
X3_TAMANHO,;
X3_DECIMAL,;
"U_Md2Valid()",; //Mtodo de validao
X3_USADO,;
X3_TIPO,;
X3_ARQUIVO,;
X3_CONTEXT })
EndIf
dbSkip()
End
RestArea(aArea)
Return
User Function Md2Valid()
Local lRet := .T.
Local cVar := ReadVar()
If cVar = "M->C1_NUM"
lRet := ExistChav("SC1", &cVar) .And. NAOVAZIO()
Elseif cVar = "M->C1_UNIDREQ"
lRet := ExistCpo("SY3", &cVar)
Elseif cVar = "M->C1_PRODUTO"
lRet := ExistCpo("SB1", &cVar)
Endif
Return lRet
//Montagem do array aCOLS
Static Function Mod2aCOLS( cAlias1, nReg, nOpc )
Local aArea := GetArea()
Local cChave := SC1->C1_NUM
Local nI := 0
If nOpc <> 3

dbSelectArea( cAlias1 )
dbSetOrder(1)
dbSeek( xFilial( cAlias1 ) + cChave )
While !EOF() .And. ;
SC1->( C1_FILIAL + C1_NUM ) == xFilial( cAlias1 ) + cChave
AADD( aREG, SC1->( RecNo() ) )
AADD( aCOLS, Array( Len( aHeader ) + 1 ) )
For nI := 1 To Len( aHeader )
If aHeader[nI, 10] == "V"
aCOLS[Len(aCOLS) , nI] := CriaVar(aHeade
r[nI , 2 ], .T.)
Else
aCOLS[Len(aCOLS) , nI] := FieldGet(Field
Pos(aHeader[nI , 2]))
Endif
Next nI
aCOLS[Len(aCOLS), Len(aHeader)+1] := .F.
dbSkip()
End
Else
aCOLS:={Array(Len( aHeader )+1)}
aCOLS[1,Len( aHeader )+1]:=.F.
For nX:=1 to Len( aHeader )
If Trim(aHeader[nX, 2]) == "C1_ITEM"
aCOLS[1,nX]:= StrZero(1, TamSx3("C1_ITEM")[1])
Else
aCOLS[1,nX]:=CriaVar(aHeader[nX,2], .T.)
Endif
Next
Endif
RestArea( aArea)
Return
//Efetivao da Incluso
Static Function Mod2GrvI()
Local aArea := GetArea()
Local nI := 0
Local nX := 0
dbSelectArea("SC1")
dbSetOrder(1)
For nI := 1 To Len( aCOLS )
If !aCOLS[nI , Len(aHeader)+1]
RecLock("SC1", .T.)
SC1->C1_FILIAL := xFilial("SC1")
SC1->C1_NUM := M->C1_NUM
SC1->C1_EMISSAO := M->C1_EMISSAO
SC1->C1_SOLICIT := M->C1_SOLICIT
SC1->C1_UNIDREQ := M->C1_UNIDREQ
SC1->C1_CODCOMP := M->C1_CODCOMP
SC1->C1_FILENT := M->C1_FILENT
For nX := 1 To Len( aHeader )
FieldPut( FieldPos( aHeader[nX, 2] ), aCOLS[nI ,
nX] )
Next nX
MsUnLock()
Endif

Next nI
RestArea( aArea )
Return
//Efetivao da Alterao
Static Function Mod2GrvA()
Local aArea := GetArea()
Local nI := 0
Local nX := 0
dbSelectArea("SC1")
For nI := 1 To Len( aREG )
If nI <= Len( aREG )
dbGoTo( aREG[nI] )
RecLock("SC1", .F.)
If aCOLS[nI , Len(aHeader)+1]
dbDelete()
Endif
Else
RecLock("SC1", .T.)
Endif
If !aCOLS[nI , Len(aHeader)+1]
SC1->C1_FILIAL := xFilial("SC1")
SC1->C1_NUM
:= M->C1_NUM
SC1->C1_EMISSAO := M->C1_EMISSAO
SC1->C1_SOLICIT := M->C1_SOLICIT
SC1->C1_UNIDREQ := M->C1_UNIDREQ
SC1->C1_CODCOMP := M->C1_CODCOMP
SC1->C1_FILENT := M->C1_FILENT
For nX := 1 To Len( aHeader )
FieldPut( FieldPos( aHeader[nX , 2] ), aCOLS[nI
, nX] )
Next nX
Endif
MsUnLock()
Next nI
RestArea( aArea )
Return
//Efetivao da excluso
Static Function Mod2GrvE()
Local nI := 0
dbSelectArea("SC1")
For nI := 1 To Len( aCOLS )
dbGoTo( aREG[nI] )
RecLock("SC1", .F.)
dbDelete()
MsUnLock()
Next nI
Return
//Func aux: Validao do cdigo do produto na mudana de linha
User Function Mod2LineOK()
Local lRet := .T.
Local cMensagem := "No ser permitido linhas com o produto vazio."
If !aCOLS[n , Len(aHeader)+1]
If Empty(aCOLS[n , GdFieldPos("C1_PRODUTO")])

MsgAlert(cMensagem, cCadastro)
lRet := .F.
Endif
Endif
Return ( lRet )
//Func Aux: Validao do cdigo do produto para todas as linhas
User Function Mod2TudoOK()
Local lRet := .T.
Local nI := 0
Local cMensagem := "No ser permitido linhas com o produto vazio."
For nI := 1 To Len( aCOLS )
If !aCOLS[nI , Len(aHeader)+1]
If Empty(aCOLS[nI , GdFieldPos("C1_PRODUTO")])
MsgAlert(cMensagem, cCadastro)
lRet := .F.
Exit
Endif
Endif
Next nI
Return( lRet )
//Validar excluso de uma linha do aCols
User Function DelItt()
Return .T.
Static Function PosObjetos(aSize,aPosObj)
Local aInfo
Local aObjects := {}
aSize := MsAdvSize()
aadd(aObjects,{100,060,.t.,.f.})
aadd(aObjects,{100,100,.t.,.t.})
aInfo := {aSize[1],aSize[2],aSize[3],aSize[4],3,3}
aPosObj := MsObjSize(aInfo,aObjects)
return