*!* EXECUTE_SCRIPT
Local lcDir, lcSchema
lcSchema = ""
lcDir = ""

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

Cd (lcDir)
Create Cursor crsScripturi (script c(100), Data D, tip c(20), seq N(5), dataseq c(20), ales L, aplicat N(1), rec N(5), filename c(100), aplicat2 N(1))

Wait Window 'xdir' Nowait
*!*	oFolder=CREATEOBJECT('files_list') && xdir.prg
*!*	oFolder.prompt_completed=.F.  &&Preview/Print without message
*!*	oFolder.FileExtensions = 'sql'
*!*	oFolder.IgnoreEmptyFolders= .T.
*!*	oFolder.xScan(lcDir)

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

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

lcPrefixScript = ReadINI(goApp.cIniFile, "script", Alltrim(lcSchema)) && CO_, RF_, SYS_, JCS_
lcPrefixScript = Upper(Nvl(lcPrefixScript,''))
If Empty(lcPrefixScript)
	lcPrefixScript = ReadINI(goApp.cIniFile, "script", 'SCHEMAROA') && FF_
Endif
lcPrefixScript = Upper(Nvl(lcPrefixScript,''))
If !Empty(lcPrefixScript)
	Select dirlist
	Set Filter To Like(lcPrefixScript + '*', Upper(filename))
Else
	Do Case
		Case Upper(Alltrim(lcSchema)) = "CONTAFIN_ORACLE"
			Select dirlist
			Set Filter To Like('CO_*', Upper(filename))
		Case Upper(Alltrim(lcSchema)) = "SYS"
			Select dirlist
			Set Filter To Like('SYS_*', Upper(filename))
		Otherwise
			Select dirlist
			Set Filter To !(Like('CO_*', Upper(filename)) Or Like('SYS_*', Upper(filename)))

	Endcase
Endif

*!*	IF UPPER(ALLTRIM(lcSchema)) = "CONTAFIN_ORACLE"
*!*		lnFiles = ADIR(laScripts, 'co_*.sql')
*!*	ELSE
*!*		lnFiles = ADIR(laScripts, 'ff_*.sql')
*!*	ENDIF

Wait Window 'insert into crsScripturi' Nowait
Select dirlist
Scan

*!*	FOR i = 1 TO lnFiles
*!*		lcFileName = laScripts(i,1)
	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)



*!* ff_2006_04_04_01_COMUN_CONTABILITATE.sql
*!* cf_2006_02_22_1.sql
	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)


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

*!*	ENDFOR
Endscan
Use In dirlist



Select crsScripturi
*INDEX ON DTOS(DATA) + PADL(seq,5,'0') TAG dataseq
*SET ORDER TO dataseq DESCENDING
Index On script Tag scriptName
Set Order To scriptName Descending

*!*	WAIT WINDOW 'select from versiune' NOWAIT
*!*	*lcSql = [select script_final as script, data_script as data, tip_script as tip, seq_script as seq, 0 as aplicat, 00000 as rec from versiune order by data_script desc, seq_script desc]
*!*	lcSql=[select distinct (case when UPPER(TRIM(script_final)) not like '%.SQL' ]+;
*!*		[then UPPER(TRIM(script_final))||'.SQL' else UPPER(TRIM(script_final)) end) ]+;
*!*		[as script, data_script as data, tip_script as tip, seq_script as seq, 0 as aplicat, 00000 as rec ] +;
*!*		[from versiune order by 1 desc]

*!*	lnSucces = goExecutor.oExecute(lcSql, 'crsScripturiVersiune1')
*!*	IF lnSucces < 0
*!*		MESSAGEBOX(goExecutor.cEroare,0+16, 'Eroare')
*!*	ENDIF


*!*	IF lnSucces > 0
*!*		WAIT WINDOW 'update aplicat' NOWAIT
*!*		UPDATE crsScripturiVersiune1 SET script = SUBSTR(script,8) WHERE UPPER(LEFT(script,7)) = 'SCRIPT_'
*!*		SELECT * FROM crsScripturiVersiune1 WITH (BUFFERING = .T.) INTO CURSOR crsScripturiVersiune ORDER BY script DESC READWRITE


*!*		UPDATE crsScripturi SET aplicat = 1 ;
*!*			WHERE UPPER(ALLTRIM(script)) IN ;
*!*			(SELECT UPPER(ALLTRIM(script)) FROM crsScripturiVersiune)
*!*		UPDATE crsScripturi SET ales = .T. ;
*!*			WHERE aplicat = 0

*!*		*!* DESELECTEZ SCRIPTURILE NEAPLICATE CARE MAI AU CEL PUTIN UN SCRIPT DE ACELASI TIP APLICAT CU O DATA MAI MARE
*!*		WAIT WINDOW 'update aplicat roz' NOWAIT
*!*		SELECT crsScripturi
*!*		SCAN FOR ales
*!*			lcScript = ALLTRIM(UPPER(script))
*!*			lcTip = UPPER(ALLTRIM(GETWORDNUM(JUSTSTEM(script),6,'_')))
*!*			SELECT COUNT(*) AS NR ;
*!*				FROM crsScripturi ;
*!*				WHERE aplicat = 1 AND ALLTRIM(UPPER(script)) > lcScript AND UPPER(ALLTRIM(GETWORDNUM(JUSTSTEM(script),6,'_'))) = lcTip ;
*!*				INTO CURSOR crsScripturiAplicate
*!*			IF _TALLY > 0 AND crsScripturiAplicate.NR > 0
*!*				REPLACE aplicat2 WITH 1, ales WITH .F. IN crsScripturi
*!*			ENDIF
*!*			USE IN crsScripturiAplicate
*!*			SELECT crsScripturi
*!*		ENDSCAN

*!*		SELECT crsScripturiVersiune
*!*		SCAN
*!*			lcScriptName = PADR(UPPER(ALLTRIM(script)),100," ")
*!*			IF SEEK(lcScriptName, 'crsScripturi', 'scriptName')
*!*				REPLACE rec WITH RECNO('crsScripturi') IN crsScripturiVersiune
*!*				REPLACE rec WITH RECNO('crsScripturiVersiune') IN crsScripturi
*!*			ENDIF
*!*		ENDSCAN

*!*		SELECT crsScripturi
*!*		GO TOP
*!*		SELECT crsScripturiVersiune
*!*		GO TOP
Select crsScripturi
Go Top
loFrmExecute = Createobject("frm_exec_script")
loFrmExecute.cDirScripturi = lcDir
*!*	loFrmExecute.lbScripturiDirector.CAPTION = 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.grdScripturiVersiune.SETALL("DynamicBackColor","iif(aplicat = 1, RGB(255,255,225), RGB(255,255,255))", "Column")
loFrmExecute.Show(1)
*!*		DO FORM frm_exec_script NAME loFrmExecute LINKED
*!*	ENDIF
If Used('dirlist')
	Use In dirlist
Endif
If Used('crsScripturi')
	Use In crsScripturi
Endif

*!*	USE IN crsScripturiVersiune

*!*	PRIVATE poScripturi
*!*	lcCursor = "crsScripturi"
*!*	lcSelect = ""
*!*	poScripturi = null
*!*	gencursor('poScripturi',lccursor,tcselect,tcfiltru,tcschema,tcorder,.F.,'', llModParam ,lcFiltOriginal)
