* test_init_env_auto_roagest.prg
* Varianta ROAGEST a test_init_env_auto.prg (acel fisier e specializat pe ROACONT -
* gcAppPath/clase/proceduri hardcodate D:\ROA\ROACONT\ - nu poate initializa mediul ROAGEST).
* Initializare completa de mediu FARA interactiune, pentru vfp9.exe -A -T.
*
* Utilizare:
*   DO test_init_env_auto_roagest WITH 'CENTRAL', 'MARIUSM_AUTO', 'parola'
*   IF gnHandle <= 0 ... eroare conectare
*
* Dupa apel sunt disponibile: goConn/goExecutor/goApp/goLog/goApi, goFirma/goCalendar,
* gnAn/gnLuna/gcS/gnIdFirma etc., SET PATH/CLASSLIB/PROCEDURE ca in roagest.prg,
* pachetele Oracle de sesiune setate (pack_sesiune/pack_contafin).
* ATENTIE: mentine lista de SET CLASSLIB/PROCEDURE sincronizata cu Programe\roagest.prg.

LPARAMETERS tcHost, tcSchema, tcPassword, tnAn, tnLuna

tcHost = IIF(EMPTY(tcHost), 'CENTRAL', tcHost)
tcSchema = IIF(EMPTY(tcSchema), 'MARIUSM_AUTO', tcSchema)
tcPassword = IIF(EMPTY(tcPassword), 'ROMFASTSOFT', tcPassword)

SET DELETED ON
SET ANSI ON
SET EXACT ON
SET SAFETY OFF
CLOSE DATABASES
SET CENTURY ON
SET DATE DMY
SET NULLDISPLAY TO ''
SET DECIMALS TO 4
SET POINT TO '.'

PUBLIC CT_SUCCES, CT_INSUCCES, crlf, gnAn, gnLuna, gnIdUtil, glEMama, gnIdFirma, gcCondSucursala, ;
	gnIdSucursala, gcS, gcFirma, gcUserName, gcUserNameApp, gcNumeProgram, gcAppName, glLunaInchisa, ;
	buton, gnButon, pnButon, gl406, gcAcces, gnIdProgram, gnId_Prg_Owner
CT_SUCCES = 1
CT_INSUCCES = -1
crlf = CHR(13) + CHR(10)
gnAn = IIF(EMPTY(tnAn), YEAR(DATE()), tnAn)
gnLuna = IIF(EMPTY(tnLuna), MONTH(DATE()), tnLuna)
gnIdUtil = -3
gnIdProgram = 0
gnId_Prg_Owner = 0
gnIdFirma = 110
gcCondSucursala = []
gnIdSucursala = m.gnIdFirma
gcS = UPPER(ALLTRIM(tcSchema))
gcFirma = m.gcS
gcUserName = m.gcS
gcUserNameApp = [MARIUS M]
gcNumeProgram = [ROAGEST]
gcAppName = [ROAGEST]
glLunaInchisa = .F.
glEMama = .F.
gl406 = .F.
* PUBLIC, nu Declare: nrcrt() (oproceduri_comune.prg) citeste NROR din ControlSource-uri
* de grid mult dupa ce acest program s-a terminat (Declare ar muri la RETURN)
PUBLIC nror(65000)
buton = 1
gnButon = 1
pnButon = 1

LOCAL lcPath
PUBLIC gcAppPath, DIRGEN, gcDirMare, gcBasePath, gcTempPath, gcGeneralIniFile, gcSettingsFile, ;
	gcSecurityPath, gcSecurityFile
gcAppPath = [D:\ROA\ROAGEST\]
Set Default To (gcAppPath)
lcPath = gcAppPath + 'Date;' + ;
	gcAppPath + 'Include;' + ;
	gcAppPath + 'FERESTRE;' + ;
	gcAppPath + 'GRAFICE;' + ;
	gcAppPath + 'CLASE;' + ;
	gcAppPath + 'MENIURI;' + ;
	gcAppPath + 'PROGRAME;' + ;
	gcAppPath + 'RAPOARTE;' + ;
	gcAppPath + 'COMUN;' + ;
	gcAppPath + 'COMUN\CLASE;' + ;
	gcAppPath + 'COMUN\FERESTRE;' + ;
	gcAppPath + 'COMUN\PROGRAME;' + ;
	gcAppPath + 'COMUN\GRAFICE;' + ;
	gcAppPath + 'COMUN\RAPOARTE;' + ;
	gcAppPath + 'COMUN\MENIURI;' + ;
	gcAppPath + 'COMUN\UTILE\GRIDEXTRAS;' + ;
	gcAppPath + 'COMUN\UTILE\CTL32;' + ;
	gcAppPath + 'COMUN\UTILE\HPDF;' + ;
	gcAppPath + 'COMUN\UTILE\HPDF\REPORTOUTPUT;' + ;
	gcAppPath + 'COMUN\UTILE\WEB;' + ;
	gcAppPath + 'COMUN\UTILE\EXCEL;' + ;
	gcAppPath + 'COMUN\UTILE\NFJSON;' + ;
	Addbs(Substr(gcAppPath,1,Rat([\],gcAppPath,2)))+[COMUNROA\]
SET PATH TO &lcPath ADDITIVE

Local lcMainClassLib
lcMainClassLib = gcAppPath + "comun\clase\appwiz"
SET CLASSLIB TO (lcMainClassLib) ADDITIVE
SET CLASSLIB TO GESTIUNI ADDITIVE
SET CLASSLIB TO CAUT ADDITIVE
SET CLASSLIB TO BAZA ADDITIVE
SET CLASSLIB TO comun ADDITIVE
SET CLASSLIB TO accessibility.vcx ADDITIVE
SET CLASSLIB TO messagebox ADDITIVE
SET CLASSLIB TO registry ADDITIVE
SET CLASSLIB TO cauta_alfa_forms.vcx ADDITIVE
SET CLASSLIB TO DECABAZA ADDITIVE
SET CLASSLIB TO onomenclatoare ADDITIVE
SET CLASSLIB TO stocuri.vcx ADDITIVE
SET CLASSLIB TO rulaje.vcx ADDITIVE
SET CLASSLIB TO ointroduceri ADDITIVE
SET CLASSLIB TO ointroduceri_web ADDITIVE
SET CLASSLIB TO ointroduceri_depozit ADDITIVE
SET CLASSLIB TO overificari ADDITIVE
SET CLASSLIB TO ferestre_oracle ADDITIVE
SET CLASSLIB TO ferestre_cere_date.vcx ADDITIVE
SET CLASSLIB TO configurare.vcx ADDITIVE
SET CLASSLIB TO serii_numere.vcx ADDITIVE
SET CLASSLIB TO omodificari.vcx ADDITIVE
SET CLASSLIB TO ocompensari.vcx ADDITIVE
SET CLASSLIB TO caut_ora ADDITIVE
SET CLASSLIB TO onote_contabile ADDITIVE
SET CLASSLIB TO otoolbar ADDITIVE
SET CLASSLIB TO bon_fisc ADDITIVE
SET CLASSLIB TO onom_articole ADDITIVE
SET CLASSLIB TO onom_retete ADDITIVE
Set Classlib To oinventar ADDITIVE
SET CLASSLIB TO orapoarte ADDITIVE
SET CLASSLIB TO orapoarte_gestiuni ADDITIVE
SET CLASSLIB TO orapoarte_parametri ADDITIVE
SET CLASSLIB TO ONOM_CURS ADDITIVE
SET CLASSLIB TO ctl32_statusbar_fals.vcx additive
SET CLASSLIB TO ctl32_common.vcx additive
SET CLASSLIB TO ctl32_structs.vcx additive
SET CLASSLIB TO ctl32_progressbar.vcx additive
SET CLASSLIB TO ctl32_balloontip.vcx additive
SET CLASSLIB TO ocriterii.vcx ADDITIVE
SET CLASSLIB TO oavize.vcx ADDITIVE
SET CLASSLIB TO onomenclatoare2 ADDITIVE
Set Classlib To oschimbare_pret.vcx additive
SET CLASSLIB TO wwdialogs.vcx additive
SET CLASSLIB TO oimportxml.vcx additive
SET CLASSLIB TO excelxml.vcx ADDITIVE
SET CLASSLIB TO orapoarte_obinv.vcx ADDITIVE
SET CLASSLIB TO locale ADDITIVE

SET PROCEDURE TO PROCEDURI ADDITIVE
SET PROCEDURE TO acces_meniu ADDITIVE
SET PROCEDURE TO cauta_alfa ADDITIVE
SET PROCEDURE TO pmenu ADDITIVE
SET PROCEDURE TO proceduri_comune ADDITIVE
SET PROCEDURE TO quitapp ADDITIVE
SET PROCEDURE TO init_program ADDITIVE
SET PROCEDURE TO oproceduri_listari.prg ADDITIVE
SET PROCEDURE TO oproceduri_configurare.prg ADDITIVE
SET PROCEDURE TO oproceduri_articole.prg ADDITIVE
SET PROCEDURE TO oproceduri_retete.prg ADDITIVE
SET PROCEDURE TO orapoarte.prg ADDITIVE
SET PROCEDURE TO mesaje ADDITIVE
SET PROCEDURE TO oserii_numere.prg ADDITIVE
SET PROCEDURE TO oexport.prg ADDITIVE
SET PROCEDURE TO wwConfig.prg ADDITIVE
SET PROCEDURE TO proceduri_rapoarte.prg additive
SET PROCEDURE TO OPROCEDURI_CURS.PRG ADDITIVE
Set procedure to orapoarte_dinamice.prg additive
SET PROCEDURE TO validare.prg additive
SET PROCEDURE TO GENCURSOR.PRG ADDITIVE
SET PROCEDURE TO OPROCEDURI_COMUNE.PRG ADDITIVE
SET PROCEDURE TO OINIT_OPTIUNI.PRG ADDITIVE
SET PROCEDURE TO updateserver.prg ADDITIVE
SET PROCEDURE TO oinainte_de.prg ADDITIVE
SET PROCEDURE TO ooperatii_comune.prg ADDITIVE
SET PROCEDURE TO Ocompensari.PRG ADDITIVE
SET PROCEDURE TO OPROCEDURI_aMS.PRG ADDITIVE
SET PROCEDURE TO oproceduri_rulaje additive
SET PROCEDURE TO oproceduri_stocuri additive
SET PROCEDURE TO OINTRODUCERI ADDITIVE
SET PROCEDURE TO oHeader.prg ADDITIVE
SET PROCEDURE TO proceduri_excel.PRG ADDITIVE
SET PROCEDURE TO onomenclatoare.PRG ADDITIVE
SET PROCEDURE TO osecurity ADDITIVE
SET PROCEDURE TO ocautare ADDITIVE
SET PROCEDURE TO orefaceri ADDITIVE
SET PROCEDURE TO ofacturare_comun.PRG ADDITIVE
SET PROCEDURE TO ofacturare_stoc.PRG ADDITIVE
SET PROCEDURE TO oproceduri_casademarcat ADDITIVE
SET PROCEDURE TO oproceduri_casa_marcat_e500 ADDITIVE
SET PROCEDURE TO oproceduri_casa_marcat_mp500 ADDITIVE
SET PROCEDURE TO oproceduri_util.prg ADDITIVE
SET PROCEDURE TO wwxmlhttp.prg ADDITIVE
SET PROCEDURE TO ini.prg ADDITIVE
SET PROCEDURE TO odocumente.prg ADDITIVE
SET PROCEDURE TO regex.prg ADDITIVE
SET PROCEDURE TO wwutils.prg ADDITIVE
SET PROCEDURE TO inchidere_k ADDITIVE
Set procedure to oschimbare_pret.prg additive
SET PROCEDURE TO iniacces.prg ADDITIVE
SET PROCEDURE TO oupdate.prg additive
SET PROCEDURE TO procese.prg additive
SET PROCEDURE TO version.prg additive
SET PROCEDURE TO xmlaccess.prg additive
SET PROCEDURE TO xmlparser.prg additive
SET PROCEDURE TO filebringer.prg additive
SET PROCEDURE TO wwcodeupdate.prg additive
SET PROCEDURE TO wwhttp.prg ADDITIVE
Set Procedure To wwApi.prg Additive
SET PROCEDURE TO xdate.prg Additive
SET PROCEDURE TO ofacturare.prg ADDITIVE
SET PROCEDURE TO oproceduri_obinv.prg ADDITIVE
SET PROCEDURE TO oproceduri_facturare.prg ADDITIVE
SET PROCEDURE TO email.prg ADDITIVE

Declare Integer GetPrivateProfileString In WIN32API ;
	string, String, String, String @, Integer, String
Declare Integer WritePrivateProfileString In Win32Api ;
	string, String, String, String
Declare Integer CopyFile In WIN32API ;
	STRING lpExistingFileName, STRING lpNewFileName, INTEGER bFailIfExists
Declare Integer PathFileExists In shlwapi STRING pszPath

If Type('goApi') = 'U'
	Public goApi
	goApi = Newobject("wwAPI", "wwAPI.prg")
ENDIF

PUBLIC gnHandle, goExecutor, goConn, goFirma, goCalendar, poLog, goLog, goApp
DIRGEN = [D:\ROA\]
gcDirMare = DIRGEN
gcBasePath = dirgen
gcSecurityPath = DIRGEN + 'Security\'
gcSecurityFile = gcSecurityPath + 'ROA_SECURITY.TXT'
gcGeneralIniFile = DIRGEN + "settings.ini"
gcSettingsFile = m.gcGeneralIniFile
gcTempPath = Addbs(shortpath(goApi.GetTempPath())) + gcS + [\]
If !Directory(gcTempPath)
	Md (gcTempPath)
Endif
gcAcces = [1;2;3;4;5;6;7;8;]

poLog = Newobject("Log_Mesaje","Log_Mesaje.prg")
goLog = poLog
goApp = Createobject("wzApplication")
gnHandle = 0
goExecutor = createobject("oExecutor")
goConn = createobject("oConn")
goConn.Connect(m.tcHost, m.tcSchema, m.tcPassword)
IF m.gnHandle <= 0
	RETURN .F.
ENDIF

LOCAL llSucces, lcSql
goFirma = null
llSucces = goExecutor.oExecuta("select * from syn_v_nom_firme where schema = ?gcS", "cFirma")
IF m.llSucces
	SELECT cFirma
	gnIdFirma = id_firma
	gnIdSucursala = null
	gcFirma = ALLTRIM(firma)
	SCATTER NAME goFirma
	IF TYPE('goFirma.codfiscalfro') = 'U'
		ADDPROPERTY(goFirma, 'codfiscalfro', ALLTRIM(STRTRAN(ALLTRIM(NVL(goFirma.cod_fiscal, '')), 'RO', '')))
	ENDIF
ELSE
	goFirma = CREATEOBJECT('custom')
	goFirma.addproperty('cod_fiscal', '')
	goFirma.addproperty('schema', m.gcS)
	goFirma.addproperty('firma', m.gcS)
	goFirma.addproperty('codfiscalfro', '')
ENDIF
USE IN (SELECT('cFirma'))

goCalendar = null
llSucces = goExecutor.oExecuta('SELECT anul, luna, tva_incasare FROM calendar WHERE an*12+luna = (select MAX(an*12+luna) FROM calendar)', 'cCalendar')
IF m.llSucces
	SELECT cCalendar
	GO TOP
	IF EMPTY(m.tnAn)
		gnAn = anul
		gnLuna = luna
	ENDIF
	SCATTER NAME goCalendar
ENDIF
USE IN (SELECT('cCalendar'))

CREATE CURSOR dual (dummy c(10))
INSERT INTO dual (dummy) VALUES ("")

actualizeaza_optiuni()
Do optiuni_firma IN oinit_optiuni.prg

lcSql = [begin pack_sesiune.set_Id_Util(?gnIdUtil); end;]
llSucces = goExecutor.oExecuta(lcSql)
lcSql = [begin pack_sesiune.setluna(?gnLuna); pack_sesiune.setan(?gnAn); end;]
llSucces = goExecutor.oExecuta(lcSql)
lcSql = [begin pack_sesiune.setlunabal(?gnLuna); pack_sesiune.setanbal(?gnAn); end;]
llSucces = goExecutor.oExecuta(lcSql)
lcSql = [begin pack_contafin.SET_DATA_RON(?gcS); end;]
llSucces = goExecutor.oExecuta(lcSql)
lcSql = [begin pack_sesiune.set_data_ron(?gcS); end;]
llSucces = goExecutor.oExecuta(lcSql)
lcSql = [begin pack_contafin.set_id_sucursala(?gnIdSucursala); end;]
llSucces = goExecutor.oExecuta(lcSql)
lcSql = [begin pack_contafin.set_id_firma(?gnIdFirma); end;]
llSucces = goExecutor.oExecuta(lcSql)

RETURN .T.
