Local lcDir, lcSchema , lcsql , lcsql1
lcSchema = ""
lcDir = ""
lcSql = ""
lcActualizare = ""
lcDir = ReadINI(goApp.cIniFile, "folder", "script_folder")
lcSchema = goApp.cUserName
If Empty(lcDir) Or !Directory(lcDir)
	lcDir = Getdir("d:\contafin_oracle\comun\scripturi\","Alegeti directorul cu scripturile","Director scripturi")
	If Empty(lcDir)
		Return
	Endif
	WriteINI(goApp.cIniFile, "folder", "script_folder", lcDir)
Endif
lcDir = ADDBS(m.lcDir)
* 09.07.2020
* Nu mai criptez scripturile, deci o sa iau scripturile din SCRIPTURI_CLAR, in loc de directorul SCRIPTURI
lcDir = STRTRAN(m.lcDir, '\SCRIPTURI\', '\SCRIPTURI_CLAR\',1,1,1)

Cd (lcDir)

*** cursor crsScripturiMasa cu scripturi pe care doresc sa le execut
CREATE CURSOR crsScripturiMasa (script c(250), Data D, tip c(100), seq N(5), dataseq c(20), ;
	ales L, aplicat N(1), rec N(5), filename c(250), aplicat2 N(1), server c(100), schema c(100))

*** cursor crsScripturi cu scripturile fizice - pentru generarea xml cu scripturi de aplicat pe scheme
Create Cursor crsScripturi (script c(250), Data D, tip c(100), seq N(5), dataseq c(20), ;
	ales L, aplicat N(1), rec N(5), filename c(250), aplicat2 N(1), server c(100), schema c(100))

Wait Window 'xdir' Nowait


loList = Newobject("fileslist","xrecurse.prg")
loList.FileExtensions = 'sql'
loList.Recurse(lcDir)

If Reccount('dirlist')=0
	Use
	Return
Endif


Wait Window 'insert into crsScripturi' Nowait
Select dirlist
Scan
	lcFileName = Addbs(Alltrim(DIRNAME)) + Alltrim(filename)
	lcScript = Upper(Alltrim(filename))
	lcScript = Strtran(lcScript, 'SCRIPT_', '')

	lnPos1 = At("_", lcScript, 1)
	lnPos2 = At("_", lcScript, 2)
	lnPos3 = At("_", lcScript, 3)
	lnPos4 = At("_", lcScript, 4)
	lnPos5 = At("_", lcScript, 5)
	lnPos6 = At("_", lcScript, 5)

	lnPosp = At(".", lcScript, 1)

	lnOccurs = Occurs('_', lcScript)

	If lnOccurs = 4
		lcAn = Substr(lcScript,lnPos1 + 1, 4)
		lcLuna = Substr(lcScript,lnPos2 + 1, 2)
		lcZi = Substr(lcScript,lnPos3 + 1, lnPos4 - lnPos3 - 1)
		lcSeq = Substr(lcScript,lnPos4 + 1, lnPosp - lnPos4 - 1)


		lcTip = ""
	Else
		lcAn = Substr(lcScript,lnPos1 + 1, 4)
		lcLuna = Substr(lcScript,lnPos2 + 1, 2)
		lcZi = Substr(lcScript,lnPos3 + 1, lnPos4 - lnPos3 - 1)
		lcSeq = Substr(lcScript,lnPos4 + 1, lnPos5 - lnPos4 - 1)
		lcTip = Substr(lcScript,lnPos5 + 1, lnPosp - lnPos5 - 1)

	Endif

	lcScript = Lower(lcScript)
	lnSeq = Round(Val(lcSeq),0)
	lcDataSeq = Padl(lcAn,4,'0') + '_' + Padl(lcLuna,2,'0') + '_' + Padl(lcZi,2,'0') + '_' + Padl(lcSeq,2,'0')

	lnAn = Val(lcAn)
	lnLuna = Val(lcLuna)
	lnZi = Val(lcZi)


	If lnAn < 2000 Or lnAn > 2099
		lnAn = Year(Date())
	Endif
	If !Between(lnLuna,1,12)
		lnLuna = Month(Date())
	Endif
	If !Between(lnZi, 1, 31)
		lnZi = 1
	Endif

	ldData = Date(lnAn, lnLuna, lnZi)
	*** nu mai ma intereseaza scripturile mai vechi de 2011
	if year(m.ldData) < 2011
		loop
	endif 

	Insert Into crsScripturi (script, Data, seq, dataseq, tip, filename, server, schema ) ;
		Values (Upper(lcScript), ldData, lnSeq, lcDataSeq, lcTip, lcFileName, server, schema)
ENDSCAN


lcSql = [select 0 as ales, id, customer_id, customer_name, dsn, server, schema, parola ] + ;
	[from UPD_VSCHEME order by customer_name, server, schema]

use in (select('crsScheme'))
lnSucces = goExecutor.oExecute(lcSql, 'crsScheme')
If lnSucces < 0
	Messagebox(goExecutor.cEroare,0+16, 'Eroare')
Endif


Use In (SELECT("dirlist"))


Select crsScripturi

Index On LEFT(script,100) Tag scriptName
Set Order To scriptName Descending

Select crsScripturi
Go Top


SELECT crsScheme
GO top
loFrmExecute = Createobject("frm_actualizare_roa")
*!*	loFrmExecute.cDirScripturi = lcDir
*!*	loFrmExecute.grdScripturi.SetAll("DynamicBackColor","iif(aplicat = 0, IIF(aplicat2=1,RGB(255,200,200),RGB(255,255,225)), RGB(255,255,255))", "Column")
loFrmExecute.Show(1)

use in (select('crsScheme'))
Use In (select('dirlist'))
Use In (select('crsScripturi'))

