-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqsql.prw
1291 lines (1028 loc) · 32.3 KB
/
qsql.prw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "rwmake.ch"
#include "topconn.ch"
#include "totvs.ch"
#include "vkey.ch"
#include "colors.ch"
#INCLUDE 'FILEIO.CH'
#DEFINE CRLF Chr(13)+Chr(10)
/*/{Protheus.doc} QSQL
Função para abrir executar consultas SQL no banco de dados
@author Emerson D Batista
@since 11/12/2017
@version 1.0
/*/
User Function AfterLogin()
SetKey( VK_F11, { || u_qsql() } )
SetKey( K_SH_F11, { || u_qsql() } )
Return nil
User Function QSQL(cFraseAuto,_aPosicoes,_oJanela)
Local cRPO2
Local btnX // Nome generico para botao
Private cTemp := "01"
Private _cMyAlias := ""
Private _cQueryTxt := space(500)//"Ctrl + Down para mostrar os campos (sair ESC)"+ CRLF +"F5 para executar a query"+CRLF
Private cTitulo := "QSQL"
Private a_Cmps := {"D_E_L_E_T_"}
Private cResult := ""
Private _cRet := ""
Private c_Pth := ""
Private c_Dtmp := ""
Private c_Hst := ""
// SET AUTOPEN OFF
// SET DELETED OFF
// SET SOFTSEEK ON
// SET DATE BRITISH
// SET CENTURY ON
// SET EPOCH TO 1950
If Select("SX2")=0
RPCCLEARENV()
RPCSetType(3)
RPCSetEnv("01","01","","","","",{})
EndIf
If ! IsInCallStack("U_SqlFile")
If !(FWIsAdmin( __cUserID ) ) .AND. GetNewPar("MV_X_RSQL",.F.)
MsgStop(' O usuario ' + __cUserID + ' nao pertence ao grupo de administradores!')
Return
Else
IF GetNewPar("MV_X_QSQL1",.F.) .AND. MsgNoYes("Abrir SqlFile?")
SetKey( VK_F11, NIL )
While U_SqlFile()
EndDo
SetKey( VK_F11, { || u_qsql() } )
Return
Endif
Endif
Endif
_cQueryTxt += space(500) //" <% e %> definem parametros. Ex. E5_DATA >= <%data_inicial%> "
If !Empty(cFraseAuto)
_cQueryTxt := cFraseAuto
EndIf
cRPO := GetSrvProfString("SourcePath", "\undefined")
cRPO2:= cRPO
If RAT("/",cRPO)>0
nPos := RAT("/",cRPO)
cRPO := Space(30)+"Data Promoçao RPO: "+DTOC(STOD(Substr(cRPO2,nPos+1,8)))
cRPO := cRPO + " Hora: "+Substr(cRPO2,nPos+9,2)+":"+Substr(cRPO2,nPos+11,2)+":"+Substr(cRPO2,nPos+13,2)
EndIf
c_Hst := "\" + GetRmtInfo()[1]
If GetRemoteType() = 2
//l:\tmp\
c_Dtmp := "l:" + StrTran(GetTempPath(),'/','\')
c_Pth := "l:" + StrTran(GetTempPath(),'/','\') + 'cfgqsql.tmp'
Else
c_Dtmp := GetTempPath()
c_Pth := GetTempPath() + 'cfgqsql.tmp'
Endif
If File(c_Pth)
nHandle := FT_FUse(AllTrim(c_Pth))
If nHandle == -1
MessageBox('Não foi possível ler o arquivo ' + c_Pth , 'AVISO', 16)
Else
FT_FGoTop()
While ! FT_FEof()
//oTMultiget2:AppendText(Alltrim(FT_FReadLn()) + CRLF)
//_cQueryTxt += Alltrim(FT_FReadLn()) + CRLF
FT_FSkip()
EndDo
FT_FUSE()
EndIf
Else
//oTMultiget2:AppendText("Ctrl + Down para mostrar os campos (sair ESC)"+ CRLF +"F5 para executar a query"+ CRLF + CRLF +"SELECT * FROM"+ CRLF +"WHERE D_E_L_E_T_ = ' '"+ CRLF)
Endif
If _aPosicoes == nil
DEFINE MSDIALOG oDlg1 TITLE "DM Tools - QSQL "+SPACE(150)+cRPO From 001,001 To 040,150
DEFINE FONT oFont NAME "Courier New" SIZE 0, -12 BOLD
DEFINE FONT oFont2 NAME "Courier New" SIZE 0, -12
oDlg1:lEscClose := .F. //Nao permite sair ao se pressionar a tecla ESC.
oTMultiget2 := TMultiget():Create(oDlg1,{|u|if(Pcount()>0,_cQueryTxt:=u,_cQueryTxt)},001,001,520,150,oFont,,,,,.T.,,,,,,,,,,,.T.)
@ 010 ,525 SAY "____________________" OF oDlg1 pixel color CLR_HBLUE
@ 001 ,525 BUTTON "&Executa (F5)" Size 60,10 action fRun() Object btnOK
@ 010+10*1 ,525 BUTTON "E&xporta XML" Size 60,10 action fExport() Object btnExp
EndIf
If Empty(cFraseAuto)
SetKey( VK_F5 ,{|| fRun()})
SetKey( 75,{|| LstCmp()})
@ 010+09*02 ,525 BUTTON "Abrir" Size 55,10 action Open() Object btnAbrir
@ 010+09*03 ,525 BUTTON "Salvar" Size 55,10 action Save() Object btnAbrir
@ 010+09*04 ,525 BUTTON "Exec.Fórmula" Size 55,10 action Terminal() Object btnAbrir
@ 010+09*05 ,525 BUTTON "Editar Tabela" Size 55,10 action fDicio() Object btnDicio
@ 010+09*06 ,525 BUTTON "Historico" Size 55,10 action ShwHst(0) Object btnHist
@ 010+09*07 ,525 BUTTON "Limpa Hist" Size 55,10 action ShwHst(1) Object btnHist
@ 010+09*08 ,525 BUTTON "Muda RPO" Size 55,10 action u_MudaAmb() Object btnHist
@ 010+09*09 ,525 BUTTON "Copia RPO" Size 55,10 action u_Promove() Object btnHist
@ 010+09*10 ,525 BUTTON "Comando no Srv." Size 55,10 action fPowerShell() Object btnPS
@ 010+09*11 ,525 BUTTON "Copia Arquivos" Size 55,10 action fCopiar() Object btnCopiar
@ 010+09*12 ,525 BUTTON "SQL File" Size 55,10 action u_SqlFile() Object btnSqlF
@ 010+09*13 ,525 BUTTON "Help" Size 55,10 action fHelp() Object btnX
@ 010+09*14 ,525 BUTTON "Sair" Size 55,10 action oDlg1:End() Object btnCancela
Activate Dialog oDlg1 centered
SetKey( VK_F5, Nil )
SetKey( 75,Nil)
Else
If _aPosicoes == nil
fRun()
fExport()
Else
fRun(_aPosicoes,_oJanela)
EndIF
EndIf
Return
Static Function fDicio()
Static cArqDic := "SX3"
Static cFiltro := Space(200)
aPergs := {}
aRet := {}
Set(_SET_DELETED, .F.)
aAdd( aPergs ,{1,"Alias da tabela ",cArqDic ,"!!!",'.T.',,'.T.',40,.F.})
// aAdd( aPergs ,{1,"Filtro Registros ",cFiltDic,"@!",'.T.',,'.T.',100,.F.})
If ParamBox(aPergs ,"Confirme Dados",aRet)
cArqDic := aRet[1]
DbSelectArea(cArqDic)
cFiltro := StaticCall(APSDU, SduExp)
If ! Empty(cFiltro)
DbSetfilter({|| &(cFiltro)}, cFiltro)
Else
DbClearFilter()
Endif
fShowTrab(cArqDic)
/*
aStruct := DbStruct()
aCpoBrw := {}
For _i:=1 to Len(aStruct)
AADD(aCpoBrw,{aStruct[_i][1],,aStruct[_i][1]})
Next
DbGoTop()
oBrowseSQL := MsSelect():New(cArqDic,"","",aCpoBrw,.F.,"",{160,001,295,588})
//oBrowseSQL:blDblClick := {|| fSduEditCel(oBrowseSQL,oBrowseSQL:nColPos,,.f.)}
oBrowseSQL:oBrowse:Refresh()
*/
EndIf
Set(_SET_DELETED, .T.)
Return nil
Static Function fRun(_aPosicoes,_oJanela)
Local aCpoBrw := {}
Local cResult := ""
Local aStruct := {}
Local _i := 0
Local j := 0
Local a_Qry := {}
Local n_Pos := iif(type("oTMultiget2")=='O',oTMultiget2:nPos,1)
Local n_TPos := 0
Local n_PsBlB := 1
Local n_PsBlE := 1
Local a_QryTxt := {}
If type("oBrowseSQL")=='O'
oBrowseSQL:End()
EndIf
if type("oResult") =="O"
oResult:End()
endif
// If File(c_Pth)
// If FErase(c_Pth,,.T.) = -1
// MessageBox('Não foi possível excluir o arquivo ' + c_Pth , 'AVISO', 16)
// Endif
// Endif
//
// Sleep(100)
If _aPosicoes == nil
MemoWrite(c_Pth,_cQueryTxt)
cBkpSql := _cQueryTxt
_cQueryTxt := Ltrim(StrTran(_cQueryTxt,CRLF,CRLF+"§$@"))
a_Qry := StrToArray(_cQueryTxt,CRLF)
n_PsBlE := Len(a_Qry)
For _i := 1 to Len(a_Qry)
a_Qry[_i] := StrTran(a_Qry[_i],"§$@","")
if n_TPos < n_Pos
If AllTrim(a_Qry[_i]) = ''
n_PsBlB := _i
Endif
Endif
n_TPos += Len(a_Qry[_i] + CRLF)
if n_TPos > n_Pos
If AllTrim(a_Qry[_i]) = ''
n_PsBlE := _i
Exit
Endif
Endif
Next
_cQueryTxt := ""
For _i := n_PsBlB to n_PsBlE
If At('--',a_Qry[_i]) > 0
If At('--',Alltrim(a_Qry[_i])) > 1
_cQueryTxt += SubStr(AllTrim(a_Qry[_i]),1,At('--',Alltrim(a_Qry[_i])) - 1) + " "
Endif
Else
_cQueryTxt += a_Qry[_i] + " "
Endif
If SubStr(AllTrim(_cQueryTxt),Len(AllTrim(_cQueryTxt)),1) = ';'
aadd(a_QryTxt,_cQueryTxt)
_cQueryTxt := ""
Endif
Next
For _i:=1 to Len(a_QryTxt)
_cQueryTxt += a_QryTxt[_i]
Next
EndIf
_i:=1
_cOper:={"DROP","TRUNCATE","INSERT","UPDATE","DELETE"}
lSelect := .t.
For _i:=1 to Len(_cOper)
If AT(_cOper[_i],UPPER(_cQueryTxt))>0
APMsgAlert("Alteracao de dados NAO permitida!",cTitulo)
Return
Endif
If AT("UPDATE",UPPER(_cQueryTxt))>0 .or. AT("DELETE",UPPER(_cQueryTxt))>0
lselect := .f.
exit
Endif
Next
nStart := 0
nFinish := 0
lParse := .T.
_cQueryTxt := AllTrim(_cQueryTxt)
For j:=1 to Len(_cQueryTxt)
If Substr(_cQueryTxt,j,2)=="<%"
nStart ++
EndIf
If Substr(_cQueryTxt,j,2)=="%>"
nFinish ++
EndIf
Next
If nStart > 0 .AND. nStart <> nFinish
Alert("Tags de Parametros Incorretas")
lParse := .F.
EndIf
If lSelect
nStart := 0
nFinish := 0
aPergs := {}
aRet := {}
aParam := {}
For j:=1 to Len(_cQueryTxt)
If Substr(_cQueryTxt,j,2)=="<%"
nStart := j+3
EndIf
If Substr(_cQueryTxt,j,2)=="%>"
nFinish := j-1
EndIf
IF nStart > 0 .and. nFinish > 0
If aScan(aParam,{|x| x==Substr(_cQueryTxt,nStart-1,nFinish-nStart+2)}) = 0
AADD(aParam,Substr(_cQueryTxt,nStart-1,nFinish-nStart+2))
cComando := Space(100)
aAdd( aPergs ,{1,aParam[Len(aParam)],cComando,"@!",'.T.',,'.T.',100,.T.})
EndIf
nStart := 0
nFinish := 0
EndIf
Next
IF len(aPergs)>0
If ParamBox(aPergs ,"Parametros",aRet,/*aButtons*/,/*lCentered*/,/*nPosX*/,/*nPosy*/,/*oDlgWizard*/,/*cLoad*/,.F./*lCanSave*/,.F./*lUserSave*/ )
For j := 1 to Len(aRet)
_cQueryTxt := StrTran(_cQueryTxt,"<%"+aParam[j]+"%>","'"+ Iif(Empty(aRet[j]),' ',Alltrim(aRet[j])) +"'")
Next
EndIf
EndIf
_nRet = TCSQLEXEC(_cQueryTxt)
If _nRet<>0
_cRet = TCSQLERROR()
//APMsgAlert(AllTrim(_cRet),cTitulo)
oResult := TMultiget():Create(oDlg1,{|u|if(Pcount()>0,_cRet:=u,_cRet)},160,001,588,170,oFont2,,,,,.T.,,,,,,,,,,,.T.)
_cRet = TCSQLERROR()
_cQueryTxt := cBkpSql
Return
EndIf
GrvHst(_cQueryTxt)
IF Select("WORK1")
WORK1->(DbCloseArea())
EndIf
cTemp := Soma1(cTemp)
_cMyAlias := "XDBF"+cTemp
IF SELECT(_cMyAlias )
DbSelectArea(_cMyAlias)
DbCloseArea()
EndIf
MsAguarde({|| dbUseArea(.T., "TOPCONN", TCGenQRY(,,_cQueryTxt), "WORK1", .F., .T.) },"Consultando","Aguarde...")
// FWMsgRun(,{|| dbUseArea(.T., "TOPCONN", TCGenQRY(,,_cQueryTxt), "WORK1", .F., .T.) },"Aguarde","Executando")
If !Select("WORK1")
Alert("Erro ao executar "+_cQueryTxt)
Return .f.
EndIf
DBSELECTAREA("WORK1")
DBGOTOP()
_aStruct:=DbStruct()
aStruct := {}
For j:= 1 to Len(_aStruct)
If !AllTrim(_aStruct[j,1]) $ "R_E_C_N_O_ , R_E_C_D_E_L_ , D_E_L_E_T_"
AADD(aStruct,_aStruct[j])
EndIf
Next
aCpoBrw:={}
_i:=1
dbSelectArea("SX3")
dbSetOrder(2)
a_Cmps := {"D_E_L_E_T_"}
For _i:=1 to Len(aStruct)
if dbSeek(aStruct[_i][1])
cLabel := AllTrim(x3Titulo()) + " ("+ AllTrim(aStruct[_i][1]) +")"
else
cLabel := aStruct[_i][1]
endif
AADD(aCpoBrw,{aStruct[_i][1],,cLabel})
AADD(a_Cmps,aStruct[_i][1])
Next
aSort(a_Cmps)
_i:=1
dbSelectArea("WORK1")
For _i := 1 to Len(aStruct)
If aStruct[_i,2] != "C"
TCSetField("WORK1", aStruct[_i,1], aStruct[_i,2],aStruct[_i,3],aStruct[_i,4])
If aStruct[_i,2] = "N"
aStruct[_i,3]:=15
aStruct[_i,4]:=02
Endif
Endif
Next
_cQueryTxt := cBkpSql
//_cArq := CriaTrab(aStruct,.T.)
//dbUseArea(.T.,,_cArq,_cMyAlias,.T.)
//DbSelectArea(_cMyAlias)
oTempTable := FWTemporaryTable():New(_cMyAlias,aStruct)
oTempTable:Create()
DbSelectArea(_cMyAlias)
FWMsgRun(,{|| fAppend() },"Aguarde","Executando 2")
&(_cMyAlias)->(dbgotop())
WORK1->(dbclosearea())
If _aPosicoes == nil
_aPosicoes := {160,001,295,588}
EndIf
oBrowseSQL := MsSelect():New(_cMyAlias,"","",aCpoBrw,.F.,"",_aPosicoes)
oBrowseSQL:oBrowse:Refresh()
else
cResult := ""
If Len(a_QryTxt) > 0
//Tratativa para update e delete
cResult := ""
For _i = 1 to Len(a_QryTxt)
nSqlError := TcSqlExec(a_QryTxt[_i])
if nSqlError <> 0
cResult += "Qry "+ StrZero(_i,5) +" Erro: " + TcSqlError() + chr(13) + chr(10)
else
GrvHst(a_QryTxt[_i])
cResult += "Qry "+ StrZero(_i,5) +" executado com sucesso." + TcSqlError() + chr(13) + chr(10)
endif
Next
Else
//Tratativa para update e delete
nSqlError := TcSqlExec(_cQueryTxt)
if nSqlError <> 0
cResult := TcSqlError()
else
GrvHst(_cQueryTxt)
cResult := "Comando executado com sucesso."+chr(13)+chr(10)+TcSqlError()
endif
Endif
oResult := TMultiget():Create(oDlg1,{|u|if(Pcount()>0,cResult:=u,cResult)},160,001,588,170,oFont2,,,,,.T.,,,,,,,,,,,.T.)
_cQueryTxt := cBkpSql
endif
RETURN
Static Function fAppend()
Append from WORK1
Return nil
Static Function fExport()
Local oExcel
Local j := 0
Local k := 0
if Empty(_cMyAlias)
Alert("Tabela Vazia")
Return nil
EndIf
oExcel := FWMSEXCEL():New()
oExcel:AddworkSheet("Dados")
oExcel:AddTable ("Dados","Planilha")
DbSelectArea(_cMyAlias)
nFields := FCount()
for j:=1 to nFields
oExcel:AddColumn("Dados","Planilha",FieldName(j),1,1)
next
DbSelectArea(_cMyAlias)
DbGoTop()
Do While !EOF()
aLinha := {}
For k:=1 To FCount()
If ValType(FieldGet(k))=="C"
AADD(aLinha,NoAcento(FieldGet(k)))
Else
AADD(aLinha,FieldGet(k))
Endif
Next
oExcel:AddRow("Dados","Planilha",aLinha)
DbSkip()
EndDo
DbGoTop()
oExcel:Activate()
_cArquivo := AllTrim(c_Dtmp+'exporta.xml')+Space(1)
_cDestino := ""
aPergs := {}
aRet := {}
aAdd( aPergs ,{6,"Arquivo",_cArquivo,"",,"", 100 ,.T.,"Arquivos .XML |*.XML","C:\",GETF_LOCALHARD})
If ParamBox(aPergs ,"Planilha",aRet,/*aButtons*/,/*lCentered*/,/*nPosX*/,/*nPosy*/,/*oDlgWizard*/,/*cLoad*/,.F./*lCanSave*/,.F./*lUserSave*/ )
_cDestino := Substr(aRet[1],1,Len(aRet[1])-1)
_cArquivo := AllTrim(Substr(_cDestino,RAT("\",_cDestino)+1,40))
_cPasta := Alltrim(Substr(_cDestino,1,RAT("\",_cDestino)-1))
MsAguarde({|| oExcel:GetXMLFile(_cArquivo) },"Gerando Planilha","Aguarde...")
MsAguarde({|| CpyS2T( _cArquivo, _cPasta, .F. ) },"Copiando Planilha","Aguarde...")
MsgInfo("Planilha Exportada em "+_cPasta)
nRet := ShellExecute("open", _cArquivo, "", _cPasta, 1)
IF nRet <= 32
Alert("Nao foi possivel abrir " +_cPasta+_cArquivo+ "!")
EndIf
EndIf
Return .t.
Static Function Open()
aPergs := {}
aRet := {}
cArquivo := padr("",150)
aAdd( aPergs ,{6,"Arquivo",cArquivo,"",,"", 90 ,.T.,"Arquivos |*.*","C:\",GETF_LOCALHARD})
If ParamBox(aPergs ,"Abrir",aRet)
_cQueryTxt := MemoRead(aRet[1])
EndIf
Return nil
Static Function Save()
aPergs := {}
aRet := {}
cArquivo := padr("",150)
aAdd( aPergs ,{6,"Arquivo",cArquivo,"",,"", 90 ,.T.,"Arquivos |*.*","C:\",GETF_LOCALHARD})
If ParamBox(aPergs ,"Salvar",aRet)
If MemoWrite(aRet[1],_cQueryTxt)
MsgInfo("Salvo com Sucesso.")
Else
Alert("Falha ao Criar "+aRet[1])
EndIf
EndIf
Return nil
Static FUNCTION LstCmp()
Local n_Pos := oTMultiget2:nPos
If Len(a_Cmps) <> 0
DEFINE DIALOG oDlg TITLE "Campos" FROM 180,180 TO 380,380 PIXEL
aItems := a_Cmps
nList := 1
oList2 := TListBox():Create(oDlg,001,001,{|u|if(Pcount()>0,nList:=u,nList)},aItems,100,100,,,,,.T.)
ACTIVATE DIALOG oDlg CENTERED
_cQueryTxt := SubStr(_cQueryTxt,1,n_Pos) + a_Cmps[nList] + SubStr(_cQueryTxt,n_Pos)
oTMultiget2:nPos := Len(SubStr(_cQueryTxt,1,n_Pos) + a_Cmps[nList])
Endif
RETURN
Static Function Terminal()
Local oError := ErrorBlock({|e| MsgAlert("Mensagem de Erro: " +chr(10)+ e:Description, "ERRO")})
IF !MsgYesNo("Confirma Execblock?")
Return nil
EndIf
cExpr := _cQueryTxt
If !Empty(cExpr)
Begin Sequence
nReturn := &cExpr
Return .T.
End Sequence
EndIf
ErrorBlock(oError)
Return .F.
Static Function fPowerShell()
Local oError := ErrorBlock({|e| MsgAlert("Mensagem de Erro: " +chr(10)+ e:Description, "ERRO")})
IF !MsgYesNo("Confirma Execucao?")
Return nil
EndIf
cExpr := _cQueryTxt
If !Empty(cExpr)
Begin Sequence
lOk := .t.
FWMsgRun(,{|| lOk := WaitRunSrv( cExpr , .T. , "C:\" ) },"Executando PowerShell","Aguarde... ")
IF lOk
MsgInfo("Executado com sucesso!")
Else
Alert("Erro na Execução")
EndIf
Return .T.
End Sequence
EndIf
ErrorBlock(oError)
Return .F.
Static Function GrvHst(c_Qry)
Local nHandle
Local a_Aux := {}
Local c_Aux := ""
Local n_I := 0
If File(c_Hst)
nHandle := FT_FUse(AllTrim(c_Hst))
If nHandle == -1
MessageBox('Não foi possível ler o arquivo ' + c_Hst , 'AVISO', 16)
Else
FT_FGoTop()
While ! FT_FEof()
AADD(a_Aux, FT_FReadLn())
FT_FSkip()
EndDo
FT_FUSE()
EndIf
EndIf
AADD(a_Aux, DtoC(Date()) + "-" + Time() +": " + c_Qry)
For n_I := Iif(Len(a_Aux) < 1000, 1, Len(a_Aux) - 1000) to Len(a_Aux)
c_Aux += a_Aux[n_I] + CRLF
Next
MemoWrite(c_Hst,c_Aux)
Return
Static function ShwHst(nPar)
Local nHandle
Local c_Aux := ""
If nPar == 1 .AND. MsgYesNo("Limpa Historico?")
fErase(AllTrim(c_Pth))
Alert("Histórico Excluído! "+c_Hst)
Return Nil
EndIF
If File(c_Hst)
nHandle := FT_FUse(AllTrim(c_Hst))
If nHandle == -1
MessageBox('Não foi possível ler o arquivo ' + c_Hst , 'AVISO', 16)
Else
FT_FGoTop()
While ! FT_FEof()
c_Aux += FT_FReadLn() + CRLF
FT_FSkip()
EndDo
FT_FUSE()
oResult := TMultiget():Create(oDlg1,{|u|if(Pcount()>0,c_Aux:=u,c_Aux)},160,001,588,170,oFont2,,,,,.T.,,,,,,,,,,,.T.)
EndIf
EndIf
Return
/*/{Protheus.doc} MudaAmb
//TODO Rotina para alterar caminho do Ambiente
@author emebatista
@since 26/12/2018
@version 1.0
@return ${return}, ${return_description}
@type function
/*/
User Function MudaAmb()
Local cRpoOld := ""
Local cRpoNew := ""
Local i
Local nPar
Local k
aParam := {}
aadd(aParam,"")
For i:= 1 to 20
aadd(aParam,space(180))
Next
cPergArq := "MudaAmb" //"mudaamb"
cPergArq2 := "MudaAmb2" //"mudaamb"
aPerg := {}
For nPar := 1 to 20
cItem := StrZero(nPar,2)
&("MV_PAR"+cItem) := aParam[nPar] := PadR(ParamLoad(cPergArq,aPerg,nPar,aParam[nPar]),180)
Next
aadd(aPerg,{1,"Ambiente" ,aParam[01],"","","",".T.",90,.F.})
For nPar := 1 to 20
aadd(aPerg,{1,"bin "+StrZero(nPar,2),aParam[nPar+1],"","","",".T.",180,.F.}) //
Next
aResp := {}
If ParamBox(aPerg,"Escolha o Ambiente",aResp,,,,,,,cPergArq,.T.,.T.)
For k:= 1 to len(aResp)
If k>1 .and. !Empty(aResp[k])
cRpoOld := GetPvProfString(AllTrim(aResp[1]), 'SourcePath', 'NaoEncontrado', aResp[k])
//Alert(aResp[k]+">"+cRpoOld)
If cRpoOld == "NaoEncontrado"
Alert("Ambinete Nao Encontrado em "+aResp[k] + chr(10) + chr(13) + "Processo Cancelado.")
Return .f.
EndIf
EndIf
Next
cAmbiente := AllTrim(aResp[1])
Else
Alert("Processo Cancelado.")
Return .f.
EndIf
aResp2 := {}
aParam2 := {}
cRpoAux := Substr(cRpoOld,Rat("\",Substr(cRpoOld,1,Len(cRpoOld)-1))+1,200)
cRpoAux := AllTrim(StrTran(cRpoAux,"\",""))
cRpoOldAux := cRpoAux
cRpoNew := Soma1(cRpoAux)
cRpoNew := StrTran(cRpoOld,cRpoOldAux,cRpoNew)
aadd(aParam2,Pad(cRpoOld,100))
aadd(aParam2,Pad(cRpoNew,100))
aPerg2 := {}
aadd(aPerg2,{1,"Localizar " ,aParam2[01],"","","",".T.",90,.F.}) //
aadd(aPerg2,{1,"Substituir por" ,aParam2[02],"","","",".T.",90,.F.}) //
If ParamBox(aPerg2,"Alterar SourcePath",aResp2) //,,,,,,,cPergArq2,.T.,.T.)
cRpoOld := StrTran(AllTrim(aResp2[1]),"\","\\")
cRPONew := AllTrim(aResp2[2])
cLog := "Arquivos Processados: "+chr(10)+chr(13)
For k:=1 to Len(aResp)
If k>1 .AND. !Empty(AllTrim(aResp[k]))
cArqIni := AllTrim(aResp[k])
xComando := 'powershell -Command "(Get-Content '+cArqIni+") -replace '"+cRpoOld+"' ,'"+cRpoNew
xComando += "' | Set-Content "+cArqIni+'" -encoding ASCII '
qout(time()+" "+xComando)
FWMsgRun(,{|| lOk := WaitRunSrv( xComando , .T. , "C:\" ) },"Alterando SourcePath.","Aguarde "+AllTrim(aResp[k]))
/*
If !WritePProString(cAmbiente, 'SourcePath', cRPONew, cArqIni)
MsgInfo("Falha ao Atualizar "+cArqIni)
EndIf
If GetPvProfString(cAmbiente, "SourcePath", "NaoAchou", cArqIni) <> cRPONew
MsgInfo("Falhou em "+cArqIni)
EndIf
*/
cLog += cArqIni + chr(10)+chr(13)
EndIf
Next
MsgInfo("Processado com Sucesso!"+CHR(10)+CHR(13)+StrTran(cRpoOld,"\\","\")+">>"+cRPONew+chr(10)+chr(13)+cLog)
Else
Alert("Processo Cancelado!")
EndIf
Return nil
User Function Promove()
xComando := Pad("xcopy C:\Totvs\Protheus\apo\*.* C:\Totvs\ /Y",250)
aParam := {}
aadd(aParam,Pad("C:\Totvs\Protheus\apo\" ,250))
aadd(aParam,Pad("C:\Totvs\Protheus\apo2\",250))
cPergArq := "PromoveRPO"
aPerg := {}
MV_PAR01 := aParam[1] := PadR(ParamLoad(cPergArq,aPerg,1,aParam[1]),250)
aadd(aPerg,{1,"Copiar RPO Origem " ,aParam[01],"","","",".T.",90,.F.})
aadd(aPerg,{1,"Copiar RPO Destino " ,aParam[02],"","","",".T.",90,.F.})
aResp := {}
If ParamBox(aPerg,"Comando no Servidor",aResp,,,,,,,cPergArq,.T.,.T.)
aResp[1] := AllTrim(aResp[1])
aResp[2] := AllTrim(aResp[2])
If Right(aResp[1],1)<>"\"
aResp[1]+="\"
EndIF
If Right(aResp[2],1)<>"\"
aResp[2]+="\"
EndIF
xComando := "xcopy "+ aResp[1]+"*.* "+aResp[2]+" /S /Y"
lOk := .F.
WaitRunSrv( "MD "+aResp[2],.T., "C:\")
FWMsgRun(,{|| lOk := WaitRunSrv( xComando , .T. , "C:\" ) },"Copia de RPO.","Copiando "+AllTrim(aResp[1])+" para "+AllTrim(aResp[2]))
IF lOk
MsgInfo("Executado com sucesso!")
Else
Alert("Erro na Execução")
EndIf
EndIf
Return nil
Static Function fCopiar()
_cOrigem := Space(80)
_cDestino := Space(80)
aPergs := {}
aRet := {}
// 1 2 3 4 5 6 7 8 9 10
aAdd( aPergs ,{6,"Arq.Origem" ,_cOrigem ,"",,"", 80 ,.F.,"Arquivos |*.*","C:\", GETF_LOCALHARD + GETF_NETWORKDRIVE})
If ParamBox(aPergs ,"Escolha a Origem",aRet)
_cOrigem := aRet[1]
EndIf
aPergs := {}
aRet := {}
// 1 2 3 4 5 6 7 8 9 10
aAdd( aPergs ,{6,"Arq.Destino",_cDestino,"",,"", 80 ,.T.,"Pastas|*.*","\", GETF_RETDIRECTORY + GETF_LOCALHARD + GETF_NETWORKDRIVE })
If ParamBox(aPergs ,"Escolha o Destino",aRet)
_cDestino := aRet[1]
EndIf
lResult := .F.
If Substr(_cOrigem,2,1)==":" .AND. Substr(_cDestino,2,1)<> ":"
lResult := CpyT2S(_cOrigem,_cDestino)
EndIf
If Substr(_cOrigem,2,1)<>":" .AND. Substr(_cDestino,2,1)== ":"
lResult := CpyS2T(_cOrigem,_cDestino)
EndIf
If lResult
MsgInfo("Executado Com Sucesso!")
Else
Alert("Falha de Execução!")
EndIF
Return nil
// Montagem do Grid
Static Function fShowTrab(_cAlias)
Local oBrow
Local cPict
Local cAlias
Local nPx
Local nI
Local cAlign
Private aEstrut := {}
lShared := .T.
lRead := .F.
DbSelectArea(_cAlias)
nPx := 1
lNewStru := .F.
oBrow := MsBrGetDBase():New( 160, 001, 580, 120,,,, oDlg1,,,,,,,,,,,, .F., _cAlias, .T.,, .F.,,, )
//oBrow:Align := CONTROL_ALIGN_ALLCLIENT
oBrow:nAt := 1
aEstrut := DbStruct()
For nI:= 1 to Len(aEstrut)
If aEstrut[nI,2] <> "M"
cAlign := "LEFT"
cPict := ""
If aEstrut[nI,2] == "N"
cAlign := "RIGHT"
If aEstrut[nI,4] >0
cPict := Replicate("9",aEstrut[nI,3]-(aEstrut[nI,4]+1)) + "." + Replicate("9",aEstrut[nI,4])
Else
cPict := Replicate("9",aEstrut[nI,3])
EndIf
EndIf
oBrow:AddColumn( TCColumn():New( aEstrut[nI,1], &("{ || "+_cAlias+"->"+aEstrut[nI,1]+"}"),cPict,,,cAlign) )
Else
oBrow:AddColumn( TCColumn():New( OemToAnsi(aEstrut[nI,1]), { || "Memo" },,,,,.F.) )
EndIf
Next
oBrow:lColDrag := .T.
oBrow:lLineDrag := .T.
oBrow:lJustific := .T. //StaticCall(APSDU, SduExp)
oBrow:blDblClick := {|| fSduEditCel(oBrow,oBrow:nColPos,.f.)} //'Read Only!'
oBrow:Cargo := {|| fSduEditCel(oBrow,oBrow:nColPos,.f.)}
oBrow:nColPos := 1
//oBrow:bChange := {|| SduRefreshStatusBar() }
//oBrow:bGotFocus := {|| SduRefreshStatusBar() }
oBrow:bDelOk := {|| fSduDeleteRecno(),oBrow:Refresh(.f.)}
oBrow:bSuperDel := {|| fSduDeleteRecno(.F.),oBrow:Refresh(.f.)}
oBrow:bAdd := {|| iIf( ApMsgYesNo("Adicionar Registro","Confirma?"),dbAppend(),)} //
oBrow:SetBlkColor({|| If(Deleted(),CLR_WHITE,CLR_BLACK)})
oBrow:SetBlkBackColor({|| If(Deleted(),CLR_LIGHTGRAY,CLR_WHITE)})
oBrow:Refresh()
Return nil
// --------------------------------------------------------------------------------
// Executada a partir da função lEditCol(MsGetDados), está função permite a edição
// do campos no Grid
STATIC Function fSduEditCel(oBrowse,nCol,lReadOnly,aStruct)
Local oDlg
Local oRect
Local oGet
Local oBtn
Local cMacro := ''
Local nRow := oBrowse:nAt
Local oOwner := oBrowse:oWnd
Local nLastKey
Local cValType
Local nX
Local cPict := ''
Local aItems := {'.T.','.F.'}
Local cCbx := '.T.'
//Local cBarMsg := "Altera"
Local cField
Local aColumns := oBrowse:GetBrwOrder()
Local nField
Local lFkInUse := .f. //SDUFkInUse()
Local cInfo := ''
Default nCol := oBrowse:nColPos
Default lReadOnly := .F.
aStruct := DbStruct()
If !DbRLock(recno())