*!* 21.12.2011
*!* marius.mutu
*!*	tlNuSuprascrieCod default .F. se foloseste codul generat din pack_contafin, .T. se foloseste codul din act_temp
*!* folosit in inventare de corectie. se foloseste acelasi cod, id_fact ca la inventarul original

*!* 11.01.2012
*!* marius.mutu
*!* tlLoadBackup - apelare oscrie_in_fisiere cu restaurarea actactan, rul_temp, rul_temp_obinv din backupset

*!* 24.03.2026
*!* marius.mutu
*!* stergere_import_note_contabile - se stergea doar prima nota cu succes, apoi iesea din scan

Parameters tnScrie_Sterge, tlModificare, tlRul, tlNuSuprascrieCod, tlLoadBackup
*!*	tnScrie_Sterge: 0 = scriere, 2 = stergere, 1 = refacere (nu se foloseste de oscrie_in_fisiere)
*!*	tlModificare: .F. = nota noua, .T. modificare nota (scriere + stergere)
*!*	tlNuSuprascrieCod default .F. se foloseste codul generat din pack_contafin, .T. se foloseste codul din act_temp
*!* tlLoadBackup: default .F. - nu restaurez actactan, rul_temp, rul_temp_obinv din backupset, .T. restaurez actactan, rul_temp, rul_temp_obinv din backupset

Private pcMesajRefacereNota, pdData
Local lnSucces, lnCod, lcSql, llManualTransactions, lnSucces2

*!* 11.01.2012
Local loBackupXML
pcMesajRefacereNota = ""
loBackupXML = Createobject("backupxml", "oscrie_in_fisiere") && oproceduri_comune.prg
If m.tlLoadBackup
	If loBackupXML.ExistsBackupSet()
		loBackupXML.RestoreBackupSet()
		If Used('actactan')
			Select ACTACTAN
			Browse
		Endif
		If Used('rul_temp')
			Select RUL_TEMP
			Browse
		Endif
		If Used('rul_temp_obinv')
			Select RUL_TEMP_OBINV
			Browse
		Endif
		If AMESSAGEBOX('Doriti sa continuati scrierea notelor din backup?', 4 + 32, _Screen.Caption) <> 6
			lnSucces = -1
			Return m.lnSucces
		Endif
	Else
		AMESSAGEBOX('Nu Exista backupset-ul "oscrie_in_fisiere"!', 0 + 48, _Screen.Caption)
		lnSucces = -1
		Return m.lnSucces
	Endif
Else
	If loBackupXML.ExistsBackupSet()
		loBackupXML.DeleteBackupSet()
	Endif

	If Used('actactan')
		loBackupXML.SaveBackupSet("actactan")
	Endif
	If Used('rul_temp')
		loBackupXML.SaveBackupSet("rul_temp")
	Endif
	If Used('rul_temp_obinv')
		loBackupXML.SaveBackupSet("rul_temp_obinv")
	Endif
Endif
*!* 11.01.2012 ^

lnSucces = -1
If Reccount('actactan') = 0
	AMESSAGEBOX('Nu s-a inregistrat nota contabila.')
	Return lnSucces
Endif

If Empty(tnScrie_Sterge) Or Isnull(tnScrie_Sterge)
	tnScrie_Sterge = 0
Endif

If !Used('ACTACTAN')
	AMESSAGEBOX('Nu exista fisierul <actactan>.')
	Return lnSucces
Endif

If tnScrie_Sterge = 2
	Select ACTACTAN
	Locate
	lnCod = cod
Else
	lnCod = Null
Endif

*!*	modificare 18.09.2007
If tnScrie_Sterge = 2 And !tlModificare And Used('rul_temp') And Reccount('rul_temp') > 0
	Private pcMesaj
	Store '' To pcMesaj
	lcSql = [begin ] + gcS + [.verifica_stoc(] + Alltrim(Str(lnCod)) + [,?@pcMesaj); end;]
	lnSucces = goExecutor.oExecute(lcSql)
	If lnSucces < 0
		AMESSAGEBOX(goExecutor.oPrelucrareEroare(), 16, "Eroare")
		Return lnSucces
	Endif
	If !Isnull(pcMesaj)
		AMESSAGEBOX(pcMesaj, 48, "Atentie")
		Release pcMesaj
		Return - 5
	Endif
	Release pcMesaj
Else
	lnSucces = 1
Endif

llManualTransactions = Iif(SQLGetprop(gnhandle, "Transactions") = 2, .T., .F.)

If lnSucces > 0
	If !tlModificare And !llManualTransactions
		lnSucces = SQLSetprop(gnhandle, "Transactions", 2)
	Endif
Endif

If lnSucces > 0
	pdData = {}
	lcSql = [begin pack_contafin.init_scriere_act_rul_local(?gnIdUtil,?gnAn,?gnLuna,] + ;
		Iif(m.tlNuSuprascrieCod, "0", "1") + [,] + Alltrim(Str(tnScrie_Sterge)) + [,?gnIdSucursala); end;]
	lnSucces = Iif(goExecutor.oExecuta(lcSql), 1, -1)
Endif

If lnSucces > 0
	lnSucces = sql_temp_insert('actactan', 'ACT_TEMP')

	If lnSucces > 0 And tlRul
		If Used('rul_temp') And Reccount('rul_temp') > 0
			lnSucces = sql_temp_insert('rul_temp', 'RUL_TEMP')
		Endif
		If Used('RUL_TEMP_OBINV') And Reccount('RUL_TEMP_OBINV') > 0
			lnSucces = sql_temp_insert('rul_temp_obinv', 'RUL_TEMP_OBINV')
		Endif
	Endif
Endif

If lnSucces > 0
	pcMesajRefacereNota = ""
	lcSql = [begin pack_contafin.final_scriere_act_rul_local(?gnIdUtil,?gnAn,?gnLuna,] + Iif(tnScrie_Sterge == 2, Alltrim(Str(lnCod)), [NULL]) + [,] + ;
		Alltrim(Str(tnScrie_Sterge)) + [,] + Iif(tlModificare, [0], [1]) + [,?@pcMesajRefacereNota); end;]
	lnSucces = Iif(goExecutor.oExecuta(lcSql), 1, -1)
	If !Empty(Nvl(pcMesajRefacereNota, ''))
		AMESSAGEBOX(pcMesajRefacereNota, 0 + 48, 'Atentie')
	Endif
Endif

If !tlModificare And !llManualTransactions
	If lnSucces < 0
		lcSql = "ROLLBACK"
	Else
		lcSql = "COMMIT"
	Endif
	lnSucces2 = goExecutor.oExecute(lcSql)

	If lnSucces2 < 0
		AMESSAGEBOX(lcSql + Chr(13) + goExecutor.cEroare, 0 + 16, "Eroare")
	Endif

	lnSucces2 = SQLSetprop(gnhandle, "Transactions", 1)
	If lnSucces2 < 0
		AMESSAGEBOX('Programul nu a reusit sa treaca pe tranzactie automata. Iesiti din program si intrati din nou!', 0 + 48, 'Atentie!')
	Endif
Endif

Return lnSucces



&& ------------------------------------------------------------------------------------
Procedure sql_temp_insert
	Lparameters tcAlias, tcTableName

	Local lcAlias, lcCursor, lnSucces
	Private poIreg
	Local lcVariable

	If Empty(tcAlias)
		lcAlias = 'ACTACTAN'
	Else
		lcAlias = Upper(Alltrim(tcAlias))
	Endif
	If Empty(tcTableName)
		lcTableName = 'ACT_TEMP'
	Else
		lcTableName = Upper(Alltrim(tcTableName))
	Endif

	lcCursor = 'crs_Coloane'
	lcSql = [SELECT COLUMN_NAME AS COLOANA from user_tab_columns WHERE table_name = ?lcTableName]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)
	If lnSucces < 0
		AMESSAGEBOX('Eroare la selectare coloane din ' + lcTableName + Chr(13) + goExecutor.cEroare, 0 + 16, 'Eroare')
	Endif

	If lnSucces > 0
		***
		Select (lcAlias)
		lnCount = Fcount()
		Scan
			lcFields = []
			lcValues = []
			lcInsert = []
			Scatter Name poIreg

			For i = 1 To lnCount
				Select (lcAlias)
				lcFieldName = Upper(Alltrim(Field(i)))
				lcFieldType = Type(lcFieldName)
				lcFieldValue = Evaluate(lcFieldName)
				If lcFieldType = 'C'
					lcVariable = '?ALLTRIM(poIreg.' + lcFieldName + ')'
				Else
					lcVariable = '?poIreg.' + lcFieldName
				Endif

				Select CRS_COLOANE
				Locate For Upper(Alltrim(COLOANA)) == lcFieldName
				If !Found()
					Loop
				Endif
				lcFields = lcFields + [,] + lcFieldName
				lcNumber = '0'
				lcDate = ''
				lcString = ''

				lcValues = lcValues + [,] + lcVariable

				*!*				DO CASE
				*!*					CASE lcFieldType = 'N'
				*!*						IF EMPTY(lcFieldValue) OR ISNULL(lcFieldValue)
				*!*							lcNumber = '0'
				*!*						ELSE
				*!*							lcNumber = ALLTRIM(STR(lcFieldValue,24,4))
				*!*						ENDIF

				*!*						lcValues = lcValues + [,] + lcNumber
				*!*					CASE lcFieldType = 'C'
				*!*						IF EMPTY(lcFieldValue) OR ISNULL(lcFieldValue)
				*!*							lcString = ''
				*!*						ELSE
				*!*							lcString = ALLTRIM(lcFieldValue)
				*!*						ENDIF

				*!*						lcValues = lcValues + [,] + ['] + lcString + [']
				*!*					CASE INLIST(lcFieldType,'D','T')
				*!*						IF EMPTY(lcFieldValue) OR ISNULL(lcFieldValue)
				*!*							lcDate = ''
				*!*						ELSE
				*!*							lcDate = DTOS(lcFieldValue)
				*!*						ENDIF
				*!*						lcValues = lcValues + [,] + [TO_DATE('] + lcDate + [','YYYY-MM-DD')]
				*!*				ENDCASE

			Endfor
			* FAC NULL ID-URILE CU VALOARE 0
			poIreg.id_sucursala = IIF(!EMPTY(NVL(poIreg.id_sucursala,0)), poIreg.id_sucursala, NULL)
			IF TYPE('poIreg.taxcode') <> 'U'
				poIreg.taxcode = IIF(!EMPTY(NVL(poIreg.taxcode,0)), poIreg.taxcode, NULL)
			ENDIF 		
			IF TYPE('poIreg.paymentcode') <> 'U'
				poIreg.paymentcode = IIF(!EMPTY(NVL(poIreg.paymentcode,'')), poIreg.paymentcode, NULL)
			ENDIF 
			IF TYPE('poIreg.id_jtva_coloana') <> 'U'
				poIreg.id_jtva_coloana = IIF(!EMPTY(NVL(poIreg.id_jtva_coloana,0)), poIreg.id_jtva_coloana, NULL)
			ENDIF 		

			lcFields = Substr(lcFields, 2)
			lcValues = Substr(lcValues, 2)

			&& INSERT INTO ACT_TEMP
			lcInsert = [INSERT INTO ] + lcTableName + [ (] + lcFields + [) VALUES (] + lcValues + [)]
			lnSucces = goExecutor.oExecute(lcInsert)
			If lnSucces < 0

				AMESSAGEBOX(goExecutor.cEroare, 0 + 16, 'Eroare')
				Exit
			Endif

		Endscan
	Endif
	If Used('crs_coloane')
		Use In CRS_COLOANE
	Endif

	Return lnSucces

Endproc && sql_temp_insert


******************
* Sterge importuri dupa act.explicatia5 sau un cod, cate un calup de note (cod)
******************
PROCEDURE stergere_import_note_contabile
	LPARAMETERS tcExplicatia, tnCod, tcColoana
	* tcExplicatia = valoarea din coloana act.explicatia5
	* tcColoana (optional) default = 'explicatia5'
	* tnCod (optional) daca doresc sa sterg un singur cod 

	PRIVATE pcExplicatia
	Local lcColoana, lcCursor, lcSql, llSucces, lnSucces, lcSelect
	LOCAL lnPercent, lcTask
	lcselect = SELECT()
	lcColoana = IIF(EMPTY(m.tcColoana), 'explicatia5', m.tcColoana)
	pcExplicatia = ALLTRIM(NVL(m.tcExplicatia, ''))
	pnCod = NVL(m.tnCod, 0)
	IF EMPTY(m.pcExplicatia) AND EMPTY(m.pnCod)
		llSucces = .F.
		SELECT (m.lcSelect)
		RETURN m.llSucces
	ENDIF 
	lcTask = 'Stergere...'

	lcSql = [select distinct cod from act where an=?gnAn and Luna=?gnLuna and sters=0 ] + ;
		IIF(!EMPTY(m.pcExplicatia), [ and ] + m.lcColoana + [ = ?pcExplicatia], '') + ;
		IIF(!EMPTY(m.pnCod), [ and cod = ?pnCod], '')
	lcCursor = [cCoduri]
	llSucces = goExecutor.oExecuta(m.lcSql, m.lcCursor)

	IF !m.llSucces
		SELECT (m.lcSelect)
		RETURN m.llSucces
	ENDIF	

	lnSucces = SQLSetprop(m.gnhandle, "Transactions", 2)
	lcSql = [begin pack_contafin.init_scriere_act_rul_local(?gnIdUtil,?gnAn,?gnLuna, 1,0,?gnIdSucursala); end;]
	llSucces = goExecutor.oExecuta(lcSql)
	If m.llSucces
		Select cCoduri
		Scan
			pnCod = cod
			If Mod(Recno(), 10) = 0
				lnPercent = Round(Recno() / Reccount() * 100, 0)
				WAIT WINDOW m.lcTask + Alltrim(Str(m.lnPercent)) + '%' NOWAIT 
			Endif
			lcSql = [begin pack_contafin.sterge_document(?pnCod); end;]
			llSucces = goExecutor.oExecuta(lcSql)
			If !m.llSucces
				Exit
			Endif
		ENDSCAN		
	Endif

	If m.llSucces
		llSucces2 = goExecutor.oExecuta('COMMIT')
	Else
		llSucces2 = goExecutor.oExecuta('ROLLBACK')
	Endif

	SQLSetprop(m.gnhandle, "Transactions", 1)

	SELECT (m.lcSelect)
		
	RETURN m.llSucces
ENDPROC && stergere_import_note_contabile
 