*!* 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))
IF UPPER(ALLTRIM(lcSchema)) = "CONTAFIN_ORACLE"
	lnFiles = ADIR(laScripts, 'co_*.sql')
ELSE
	lnFiles = ADIR(laScripts, 'ff_*.sql')
ENDIF

FOR i = 1 TO lnFiles
	lcFileName = laScripts(i,1)
	lcScript = UPPER(laScripts(i,1))
	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, ADDBS(lcDir) + lcFileName)
ENDFOR

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

*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

	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

	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

	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

USE IN crsScripturi
USE IN crsScripturiVersiune

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