* test_rand_nou_din_cache.prg - de ce apare un rand NOU in ISTORIC_CODURI_FISCALE cand
* verificarea in masa a fost servita integral din cache.
*
* Ruleaza VerificaListaCIF pe coduri proaspete (ANAF blocat), apoi identifica randurile
* adaugate peste watermark si le compara, camp cu camp, cu randul care exista deja pentru
* acelasi cod fiscal - ca sa se vada CE anume a considerat pachetul ca fiind "diferit"
* (pack_istoric_cf.SAVE_ISTORIC_COD_FISCAL compara ultima inregistrare cu parametrii primiti).
*
* Rulare: vfp9.exe -A -T "...\test_rand_nou_din_cache.prg" [N]

LPARAMETERS tcN

SET SAFETY OFF
SET TALK OFF
SET DELETED ON
SET EXACT ON
SET CENTURY ON
SET DATE DMY
SET NULLDISPLAY TO ''
CLOSE DATABASES

PUBLIC gcTestLog, gnApeluriANAF, gnErorriSQL, gcUltimaEroareSQL
gcTestLog = "D:\ROA\ROACONT\COMUN\utile\Teste\cache_anaf\test_rand_nou_din_cache_log.txt"
STRTOFILE("START " + TTOC(DATETIME()) + CHR(13) + CHR(10), gcTestLog)
gnApeluriANAF = 0
gnErorriSQL = 0
gcUltimaEroareSQL = ''

ON ERROR DO ErrRand WITH ERROR(), MESSAGE(), PROGRAM(), LINENO()
ON SHUTDOWN QUIT

DO D:\ROA\ROACONT\COMUN\utile\Teste\test_init_env_auto.prg WITH 'CENTRAL', 'MARIUSM_AUTO', 'ROMFASTSOFT'
IF gnHandle <= 0
	DO WrLog4 WITH 'EROARE: conectare Oracle esuata - GATA'
	QUIT
ENDIF
DO WrLog4 WITH 'mediu OK, schema=' + gcS

LOCAL loExecNou
loExecNou = CREATEOBJECT('oexec_fara_dialog2')
loExecNou.nHandle = gnHandle
goExecutor = loExecNou

LOCAL lnN, lcSql, lnZile, loVer
lnN = IIF(TYPE('tcN') = 'C' AND !EMPTY(tcN), INT(VAL(tcN)), 200)
loVer = CREATEOBJECT('VerificareANAFRand')
lnZile = loVer.ZileCache()

PUBLIC gnWm
gnWm = 0
IF goExecutor.oExecuta('SELECT NVL(MAX(ID),0) AS IDMAX FROM ISTORIC_CODURI_FISCALE', 'crsW')
	gnWm = crsW.IDMAX
	USE IN (SELECT('crsW'))
ENDIF
DO WrLog4 WITH 'watermark = ' + TRANSFORM(gnWm)
IF gnWm <= 0
	DO WrLog4 WITH 'EROARE: watermark invalid - GATA'
	QUIT
ENDIF

lcSql = 'SELECT COD_FISCAL_N FROM (SELECT DISTINCT COD_FISCAL_N FROM ISTORIC_CODURI_FISCALE'
lcSql = lcSql + ' WHERE COD_FISCAL_N IS NOT NULL AND DATAORAV_ANAF IS NOT NULL'
lcSql = lcSql + ' AND SYSDATE - DATAORAV_ANAF <= ' + TRANSFORM(lnZile)
lcSql = lcSql + ' ORDER BY COD_FISCAL_N) WHERE ROWNUM <= ' + TRANSFORM(lnN)
=goExecutor.oExecuta(lcSql, 'crsCodR')

IF USED('cLotR')
	USE IN cLotR
ENDIF
CREATE CURSOR cLotR (cod_fiscal C(30) NULL, data D NULL)
SELECT crsCodR
SCAN
	INSERT INTO cLotR (cod_fiscal, data) VALUES (ALLTRIM(TRANSFORM(crsCodR.COD_FISCAL_N)), DATE())
	SELECT crsCodR
ENDSCAN
USE IN (SELECT('crsCodR'))
DO WrLog4 WITH 'lot = ' + TRANSFORM(RECCOUNT('cLotR')) + ' coduri proaspete'

=loVer.VerificaListaCIF('cLotR', DATE(), 'crsRezR', 'MASA')
DO WrLog4 WITH 'randuri intoarse: ' + TRANSFORM(IIF(USED('crsRezR'), RECCOUNT('crsRezR'), 0))
DO WrLog4 WITH 'apeluri ANAF (blocate): ' + TRANSFORM(gnApeluriANAF)

* --- ce s-a adaugat ---
lcSql = 'SELECT ID, COD_FISCAL, COD_FISCAL_N, FIRMA, ADRESA, JUDET, REGCOM, CODPOSTAL,'
lcSql = lcSql + ' TELEFON, FAX, DATATVAMFIN, PLATITORTVAMFIN, STARE, PLATITORTVAVIES,'
lcSql = lcSql + ' INACTIV, TVAINCASARE, SPLITTVA, SCHEMA_SURSA'
lcSql = lcSql + ' FROM ISTORIC_CODURI_FISCALE WHERE ID > ' + TRANSFORM(gnWm) + ' ORDER BY ID'
IF !goExecutor.oExecuta(lcSql, 'crsNoi')
	DO WrLog4 WITH 'EROARE la citirea randurilor noi'
ELSE
	DO WrLog4 WITH ''
	DO WrLog4 WITH '=== randuri NOI adaugate: ' + TRANSFORM(RECCOUNT('crsNoi')) + ' ==='
	SELECT crsNoi
	SCAN
		DO WrLog4 WITH ''
		DO WrLog4 WITH 'ID nou ' + TRANSFORM(crsNoi.ID) + '  cod_fiscal=[' + TRANSFORM(crsNoi.COD_FISCAL) + ']' + ;
			'  cod_fiscal_n=' + TRANSFORM(crsNoi.COD_FISCAL_N)
		DO ComparaCuAnterior WITH crsNoi.COD_FISCAL_N, crsNoi.ID
		DO AratatCeSATrimis WITH crsNoi.COD_FISCAL_N
		SELECT crsNoi
	ENDSCAN
	USE IN (SELECT('crsNoi'))
ENDIF

DO WrLog4 WITH ''
=goExecutor.oExecuta('DELETE FROM ISTORIC_CODURI_FISCALE WHERE ID > ' + TRANSFORM(gnWm))
=goExecutor.oExecuta('COMMIT')
DO WrLog4 WITH 'curatenie: sterse randurile cu ID > ' + TRANSFORM(gnWm)
DO WrLog4 WITH 'GATA ' + TTOC(DATETIME())
QUIT


*** ce a trimis SalveazaIstoricDinCursor pentru codul asta (randul din cursorul de rezultat)
PROCEDURE AratatCeSATrimis
	LPARAMETERS tnCodN
	LOCAL lcSel
	lcSel = SELECT()
	IF !USED('crsRezR')
		RETURN
	ENDIF
	SELECT crsRezR
	LOCATE FOR cui = tnCodN
	IF !FOUND()
		DO WrLog4 WITH '  (codul nu e in cursorul de rezultat)'
		SELECT (lcSel)
		RETURN
	ENDIF
	DO WrLog4 WITH '  trimis de VFP: sursa=[' + ALLTRIM(NVL(crsRezR.sursa, '')) + ']' + ;
		' scptva=' + TRANSFORM(crsRezR.scpTVA) + ' tvainc=' + TRANSFORM(crsRezR.tvaincasare) + ;
		' split=' + TRANSFORM(crsRezR.splittva) + ' inactiv=' + TRANSFORM(crsRezR.inactiv)
	DO WrLog4 WITH '     iScp=' + DTOC(crsRezR.data_inceput_ScpTVA) + ' sScp=' + DTOC(crsRezR.data_sfarsit_ScpTVA) + ;
		' aScp=' + DTOC(crsRezR.data_anul_imp_ScpTVA) + ' iInc=' + DTOC(crsRezR.dataInceputTvaInc) + ;
		' sInc=' + DTOC(crsRezR.dataSfarsitTvaInc)
	DO WrLog4 WITH '     inactivare=' + DTOC(crsRezR.dataInactivare) + ' reactivare=' + DTOC(crsRezR.dataReactivare) + ;
		' iSplit=' + DTOC(crsRezR.dataInceputSplitTVA) + ' aSplit=' + DTOC(crsRezR.dataAnulareSplitTVA)
	DO WrLog4 WITH '     lungimi trimise: firma=' + TRANSFORM(LEN(NVL(crsRezR.firma, ''))) + ;
		' adresa=' + TRANSFORM(LEN(NVL(crsRezR.adresa, ''))) + ;
		' judet=' + TRANSFORM(LEN(NVL(crsRezR.judet, ''))) + ;
		' regcom=' + TRANSFORM(LEN(NVL(crsRezR.nrRegCom, '')))
	DO WrLog4 WITH '     firma=[' + NVL(crsRezR.firma, '') + ']'
	DO WrLog4 WITH '     adresa=[' + NVL(crsRezR.adresa, '') + ']'
	DO AratatLungimiOracle WITH tnCodN
	SELECT (lcSel)
ENDPROC

*** lungimile reale, in Oracle, ale campurilor text din randurile codului
PROCEDURE AratatLungimiOracle
	LPARAMETERS tnCodN
	LOCAL lcSql
	lcSql = 'SELECT ID, LENGTH(FIRMA) AS LF, LENGTH(ADRESA) AS LA, LENGTH(JUDET) AS LJ,'
	lcSql = lcSql + ' LENGTH(REGCOM) AS LR, LENGTH(CODPOSTAL) AS LC, LENGTH(TELEFON) AS LT,'
	lcSql = lcSql + ' LENGTH(FAX) AS LX, LENGTH(DATATVAMFIN) AS LD, LENGTH(STARE) AS LS'
	lcSql = lcSql + ' FROM ISTORIC_CODURI_FISCALE WHERE COD_FISCAL_N = ' + TRANSFORM(tnCodN) + ' ORDER BY ID'
	IF goExecutor.oExecuta(lcSql, 'crsLen')
		DO WrLog4 WITH '  lungimi in Oracle (NULL = gol):'
		SELECT crsLen
		SCAN
			DO WrLog4 WITH '    id=' + TRANSFORM(crsLen.ID) + ' firma=' + TRANSFORM(NVL(crsLen.LF, -1)) + ;
				' adresa=' + TRANSFORM(NVL(crsLen.LA, -1)) + ' judet=' + TRANSFORM(NVL(crsLen.LJ, -1)) + ;
				' regcom=' + TRANSFORM(NVL(crsLen.LR, -1)) + ' cp=' + TRANSFORM(NVL(crsLen.LC, -1)) + ;
				' tel=' + TRANSFORM(NVL(crsLen.LT, -1)) + ' fax=' + TRANSFORM(NVL(crsLen.LX, -1)) + ;
				' dtvamfin=' + TRANSFORM(NVL(crsLen.LD, -1)) + ' stare=' + TRANSFORM(NVL(crsLen.LS, -1))
			SELECT crsLen
		ENDSCAN
		USE IN (SELECT('crsLen'))
	ENDIF
ENDPROC

PROCEDURE ComparaCuAnterior
	LPARAMETERS tnCodN, tnIdNou
	LOCAL lcSql, lcLinie
	lcSql = 'SELECT ID, COD_FISCAL, FIRMA, ADRESA, JUDET, REGCOM, CODPOSTAL, TELEFON, FAX,'
	lcSql = lcSql + ' DATA_INCEPUT_SCPTVA, DATA_SFARSIT_SCPTVA, DATA_ANUL_IMP_SCPTVA,'
	lcSql = lcSql + ' DATA_INCEPUT_TVAINC, DATA_SFARSIT_TVAINC, DATA_INACTIVARE, DATA_REACTIVARE,'
	lcSql = lcSql + ' DATA_INCEPUT_SPLITTVA, DATA_ANULARE_SPLITTVA,'
	lcSql = lcSql + ' DATATVAMFIN, PLATITORTVAMFIN, STARE, PLATITORTVAVIES, INACTIV, TVAINCASARE, SPLITTVA,'
	lcSql = lcSql + ' TO_CHAR(DATAORAV,' + CHR(39) + 'DD.MM.YYYY HH24:MI' + CHR(39) + ') AS DORAV, SCHEMA_SURSA'
	lcSql = lcSql + ' FROM ISTORIC_CODURI_FISCALE WHERE COD_FISCAL_N = ' + TRANSFORM(tnCodN)
	lcSql = lcSql + ' ORDER BY ID'
	IF !goExecutor.oExecuta(lcSql, 'crsIst')
		DO WrLog4 WITH '  eroare la citirea istoricului codului'
		RETURN
	ENDIF
	DO WrLog4 WITH '  istoricul codului (' + TRANSFORM(RECCOUNT('crsIst')) + ' randuri):'
	SELECT crsIst
	SCAN
		lcLinie = '   ' + IIF(crsIst.ID = tnIdNou, '>>', '  ') + ' id=' + TRANSFORM(crsIst.ID) + ;
			' cf=[' + ALLTRIM(NVL(crsIst.COD_FISCAL, '')) + ']' + ;
			' firma=[' + ALLTRIM(LEFT(NVL(crsIst.FIRMA, ''), 30)) + ']' + ;
			' adresa=[' + ALLTRIM(LEFT(NVL(crsIst.ADRESA, ''), 25)) + ']' + ;
			' judet=[' + ALLTRIM(NVL(crsIst.JUDET, '')) + ']' + ;
			' regcom=[' + ALLTRIM(NVL(crsIst.REGCOM, '')) + ']'
		DO WrLog4 WITH lcLinie
		lcLinie = '        cp=[' + ALLTRIM(NVL(crsIst.CODPOSTAL, '')) + ']' + ;
			' tel=[' + ALLTRIM(NVL(crsIst.TELEFON, '')) + ']' + ;
			' fax=[' + ALLTRIM(NVL(crsIst.FAX, '')) + ']' + ;
			' dtvamfin=[' + ALLTRIM(NVL(crsIst.DATATVAMFIN, '')) + ']' + ;
			' ptvamfin=' + TRANSFORM(NVL(crsIst.PLATITORTVAMFIN, -1)) + ;
			' stare=[' + ALLTRIM(NVL(crsIst.STARE, '')) + ']' + ;
			' vies=' + TRANSFORM(NVL(crsIst.PLATITORTVAVIES, -1)) + ;
			' inactiv=' + TRANSFORM(NVL(crsIst.INACTIV, -1)) + ;
			' tvainc=' + TRANSFORM(NVL(crsIst.TVAINCASARE, -1)) + ;
			' split=' + TRANSFORM(NVL(crsIst.SPLITTVA, -1)) + ;
			' dataorav=' + ALLTRIM(NVL(crsIst.DORAV, '')) + ;
			' schema=' + ALLTRIM(NVL(crsIst.SCHEMA_SURSA, ''))
		DO WrLog4 WITH lcLinie
		lcLinie = '        iScp=' + DTOC(NVL(crsIst.DATA_INCEPUT_SCPTVA, {})) + ;
			' sScp=' + DTOC(NVL(crsIst.DATA_SFARSIT_SCPTVA, {})) + ;
			' aScp=' + DTOC(NVL(crsIst.DATA_ANUL_IMP_SCPTVA, {})) + ;
			' iInc=' + DTOC(NVL(crsIst.DATA_INCEPUT_TVAINC, {})) + ;
			' sInc=' + DTOC(NVL(crsIst.DATA_SFARSIT_TVAINC, {})) + ;
			' inact=' + DTOC(NVL(crsIst.DATA_INACTIVARE, {})) + ;
			' react=' + DTOC(NVL(crsIst.DATA_REACTIVARE, {})) + ;
			' iSpl=' + DTOC(NVL(crsIst.DATA_INCEPUT_SPLITTVA, {})) + ;
			' aSpl=' + DTOC(NVL(crsIst.DATA_ANULARE_SPLITTVA, {}))
		DO WrLog4 WITH lcLinie
		SELECT crsIst
	ENDSCAN
	USE IN (SELECT('crsIst'))
ENDPROC

PROCEDURE WrLog4
	LPARAMETERS tcText
	STRTOFILE(TRANSFORM(tcText) + CHR(13) + CHR(10), gcTestLog, .T.)
ENDPROC

PROCEDURE ErrRand
	LPARAMETERS tnError, tcMessage, tcProgram, tnLine
	DO WrLog4 WITH 'EROARE ' + TRANSFORM(tnError) + ' [' + TRANSFORM(tcMessage) + '] in ' + ;
		TRANSFORM(tcProgram) + ' linia ' + TRANSFORM(tnLine)
	RETURN
ENDPROC


DEFINE CLASS oexec_fara_dialog2 AS oexecutor

	FUNCTION oExecuta
		LPARAMETERS tcSql, tcCursor, tlProgress, tcTitluProgress, tnHandle, tlShowError, tlQuitOnError, tlReconnect
		LOCAL lcSql, lcCursor, lnHandle, lnRez
		lcSql = IIF(EMPTY(tcSql), This.cSql, tcSql)
		lcCursor = IIF(EMPTY(tcCursor), This.cCursor, tcCursor)
		lnHandle = IIF(EMPTY(tnHandle), This.nHandle, tnHandle)
		lnRez = This.oExecute(lcSql, lcCursor, tlProgress, tcTitluProgress, lnHandle)
		IF lnRez < 1
			gnErorriSQL = gnErorriSQL + 1
			gcUltimaEroareSQL = TRANSFORM(This.cEroare)
		ENDIF
		RETURN (lnRez >= 1)
	ENDFUNC

ENDDEFINE


DEFINE CLASS VerificareANAFRand AS VerificareANAF

	PROCEDURE OrdineCache
		LPARAMETERS tcTraseu
		RETURN 'CACHE,ANAF'
	ENDPROC

	PROCEDURE ANAF_SincronWebService_PlatitorTva
		LPARAMETERS tcCursorListaCIF, tdData, tcAlias
		gnApeluriANAF = gnApeluriANAF + 1
		RETURN IIF(EMPTY(tcAlias), 'crsANAF', tcAlias)
	ENDPROC

ENDDEFINE
