*--------------------------------------------------------------------------------------------------------------------------------------------------------
* (EN) AUTOGENERATED - ATTENTION!! - NOT INTENDED FOR EXECUTION!! USE ONLY FOR MERGING CHANGES AND STORING WITH SCM TOOLS!!
*--------------------------------------------------------------------------------------------------------------------------------------------------------
*< FOXBIN2PRG: Version="1.21" SourceFile="frm_import_note_facturi_clienti.scx" CPID="1252" /> (Solo para binarios VFP 9 / Only for VFP 9 binaries)
*
*
#INCLUDE "comun.h"

DEFINE CLASS dataenvironment AS dataenvironment 
 	*< CLASSDATA: Baseclass="dataenvironment" Timestamp="" Scale="" Uniqueid="" ClassIcon="1" />

	*<PropValue>
		DataSource = .NULL.
		Height = 200
		Left = 1
		Name = "Dataenvironment"
		Top = 220
		Width = 520
	*</PropValue>

ENDDEFINE

DEFINE CLASS form1 AS form 
 	*< CLASSDATA: Baseclass="form" Timestamp="" Scale="" Uniqueid="" />

	*-- OBJECTDATA items order determines ZOrder / El orden de los items OBJECTDATA determina el ZOrder 
	*< OBJECTDATA: ObjPath="cmdExport" UniqueID="" Timestamp="" />
	*< OBJECTDATA: ObjPath="edtInfo" UniqueID="" Timestamp="" />
	*< OBJECTDATA: ObjPath="txtData1" UniqueID="" Timestamp="" />
	*< OBJECTDATA: ObjPath="txtData2" UniqueID="" Timestamp="" />
	*< OBJECTDATA: ObjPath="Label1" UniqueID="" Timestamp="" />
	*< OBJECTDATA: ObjPath="Command1" UniqueID="" Timestamp="" />
	*< OBJECTDATA: ObjPath="chkModificaNote" UniqueID="" Timestamp="" />
	*< OBJECTDATA: ObjPath="cmdTemplate" UniqueID="" Timestamp="" />
	*< OBJECTDATA: ObjPath="chkAnulat" UniqueID="" Timestamp="" />
	*< OBJECTDATA: ObjPath="But_help1" UniqueID="" Timestamp="" />

	*<DefinedPropArrayMethod>
		*m: completeazaclientiroa
		*m: connectroa
		*m: creeazanote
		*m: disconnectroa
		*m: exportdate
		*m: getidpartcasa
		*m: modificanote
		*m: prelucreazafisierebreeze
		*m: scrienote
		*m: sql_temp_insert
		*m: sql_temp_insert_values
		*m: sterge_import
		*m: templatexlsx
		*m: trace
		*m: verificaimport
		*p: lanulat
		*p: _memberdata		&& XML Metadata for customizable properties
	*</DefinedPropArrayMethod>

	*<PropValue>
		BackColor = 255,255,255
		Caption = "Import note contabile in ROA"
		DoCreate = .T.
		Height = 538
		Icon = ..\grafice\note.ico
		lanulat = .F.
		Left = 0
		Name = "Form1"
		ShowWindow = 0
		Top = 0
		Width = 660
		WindowType = 1
		_memberdata = <VFPData>
			<memberdata name="verificaimport" display="VerificaImport"/>
			<memberdata name="modificanote" display="ModificaNote"/>
			<memberdata name="getidpartcasa" display="GetIdPartCasa"/>
		</VFPData>
	*</PropValue>

	ADD OBJECT 'But_help1' AS but_help WITH ;
		Anchor = 8, ;
		Left = 615, ;
		Name = "But_help1", ;
		Top = 3
		*< END OBJECT: ClassLib="..\clase\cmd_butoane.vcx" BaseClass="commandbutton" />

	ADD OBJECT 'chkAnulat' AS _checkbox WITH ;
		Alignment = 0, ;
		Caption = "Facturi anulate", ;
		ControlSource = "Thisform.lAnulat", ;
		FontSize = 10, ;
		Left = 276, ;
		Name = "chkAnulat", ;
		Top = 11, ;
		Value = .F.
		*< END OBJECT: ClassLib="..\clase\_baza.vcx" BaseClass="checkbox" />

	ADD OBJECT 'chkModificaNote' AS _checkbox WITH ;
		Alignment = 0, ;
		Caption = "Modifica note contabile", ;
		FontSize = 10, ;
		Left = 396, ;
		Name = "chkModificaNote", ;
		Top = 12, ;
		Value = 1
		*< END OBJECT: ClassLib="..\clase\_baza.vcx" BaseClass="checkbox" />

	ADD OBJECT 'cmdExport' AS commandbutton WITH ;
		Anchor = 0, ;
		AutoSize = .F., ;
		BackColor = 255,128,64, ;
		Caption = "\<Import", ;
		FontBold = .T., ;
		FontSize = 10, ;
		Height = 36, ;
		Left = 132, ;
		Name = "cmdExport", ;
		TabIndex = 3, ;
		Top = 41, ;
		Width = 117
		*< END OBJECT: BaseClass="commandbutton" />

	ADD OBJECT 'cmdTemplate' AS commandbutton WITH ;
		Anchor = 0, ;
		AutoSize = .F., ;
		BackColor = 74,165,255, ;
		Caption = "Sablon \<xlsx", ;
		FontBold = .T., ;
		FontSize = 10, ;
		Height = 36, ;
		Left = 11, ;
		Name = "cmdTemplate", ;
		TabIndex = 3, ;
		Top = 41, ;
		Width = 117
		*< END OBJECT: BaseClass="commandbutton" />

	ADD OBJECT 'Command1' AS commandbutton WITH ;
		Anchor = 0, ;
		AutoSize = .F., ;
		BackColor = 255,159,159, ;
		Caption = "\<Sterge import", ;
		FontBold = .T., ;
		FontSize = 10, ;
		ForeColor = 0,0,0, ;
		Height = 36, ;
		Left = 251, ;
		Name = "Command1", ;
		TabIndex = 4, ;
		TabStop = .F., ;
		ToolTipText = "Sterge note importate, maxim o luna. Pentru mai multe luni trebuie repetata operatia cu alta perioada", ;
		Top = 41, ;
		Width = 117
		*< END OBJECT: BaseClass="commandbutton" />

	ADD OBJECT 'edtInfo' AS editbox WITH ;
		Anchor = 15, ;
		FontSize = 10, ;
		Height = 444, ;
		Left = 10, ;
		Name = "edtInfo", ;
		TabIndex = 4, ;
		Top = 84, ;
		Width = 638
		*< END OBJECT: BaseClass="editbox" />

	ADD OBJECT 'Label1' AS label WITH ;
		AutoSize = .T., ;
		BackStyle = 0, ;
		Caption = "Perioada", ;
		FontSize = 10, ;
		Height = 18, ;
		Left = 9, ;
		Name = "Label1", ;
		TabIndex = 5, ;
		Top = 11, ;
		Width = 53
		*< END OBJECT: BaseClass="label" />

	ADD OBJECT 'txtData1' AS textbox WITH ;
		FontSize = 10, ;
		Format = "!K", ;
		Height = 23, ;
		Left = 66, ;
		Name = "txtData1", ;
		TabIndex = 1, ;
		Top = 7, ;
		Width = 100
		*< END OBJECT: BaseClass="textbox" />

	ADD OBJECT 'txtData2' AS textbox WITH ;
		FontSize = 10, ;
		Format = "!K", ;
		Height = 23, ;
		Left = 172, ;
		Name = "txtData2", ;
		TabIndex = 2, ;
		Top = 7, ;
		Width = 100
		*< END OBJECT: BaseClass="textbox" />
	
	PROCEDURE completeazaclientiroa
		* Completez id_part
		*loHash = GetHash([cselect=>select id_part from nom_parteneri where replace(UPPER(cod_fiscal),' ', '') = replace(UPPER(?pcCodFiscal), ' ', '') and sters = 0 and inactiv = 0??cCursor=>cClientiROA])
		Local lcAdresa, lcApart, lcBanca, lcBloc, lcCod_fiscal, lcCodpostal, lcCont_Banca, lcCorespDel
		Local lcDA_apare, lcDenumire, lcDenumire_adresa, lcEmail, lcEtaj, lcFax, lcIdPart, lcIdString
		Local lcId_Judet, lcId_categ_ent, lcId_loc, lcId_loc_inreg, lcId_tara, lcId_util, lcItem, lcJudet
		Local lcLocalitate, lcMesaj, lcMotiv_inactiv, lcNumar, lcNume, lcPrefix, lcPrenume, lcPrincipala
		Local lcReg_comert, lcScara, lcSql, lcSqlCod, lcSqlDenumire, lcSqlInsert, lcStrada, lcSufix, lcTara
		Local lcTelefon1, lcTelefon2, lcTip_persoana, lcWeb, lcinactiv, lnItem, lnItems, lnSucces, lcAdreseParteneri
		Private pcCodFiscal, pcDenumire, pnIdPart, pnNrAdrese, pnIdJudet
		pnIdJudet = 0
		pcCodFiscal = ''
		pcDenumire = ''
		pnIdPart = 0
		pnNrAdrese = 0
		
		lcSqlPart = [select id_part, cod_fiscal, denumire from nom_parteneri where sters = 0 and inactiv = 0]
		lnSucces = goExecutor.oExecute(m.lcSqlPart, 'cParteneri')
		
		If m.lnSucces < 0
			Thisform.trace('Eroare la selectia din clienti ROA ' + goExecutor.oPrelucrareEroare())
			RETURN m.lnSucces
		ENDIF
		
		lcAdreseParteneri = [select id_adresa, id_part, localitate, id_loc, judet, id_judet, tara, id_tara from vadrese_parteneri]
		lnSucces = goExecutor.oExecute(m.lcAdreseParteneri, 'cAdrese')
		
		If m.lnSucces < 0
			Thisform.trace('Eroare la selectia din adrese parteneri ROA ' + goExecutor.oPrelucrareEroare())
			RETURN m.lnSucces
		ENDIF
			
		*!*	lcSqlCod = [select MAX(id_part) as id_part from nom_parteneri where replace(UPPER(cod_fiscal),' ', '') = ?pcCodFiscal and sters = 0 and inactiv = 0]
		*!*	lcSqlDenumire = [select MAX(id_part) as id_part from nom_parteneri where denumire = ?pcDenumire and sters = 0 and inactiv = 0]
		lnSucces  = 1
		
		Select cClienti
		Scan
			pcCodFiscal = Alltrim(Upper(STRTRAN(cod_fiscal, ' ', '')))
			pcDenumire = Alltrim(Upper(denumire))
			pnIdPart = 0
			
			lcAdresa = Strtran(Alltrim(Upper(Nvl(adresa, ''))), Chr(13), ' ')
			* Preciziei (Strada), Nr. 6M, Complex rezidential TEN BLOCKS Bl. 8B, Sc. 1, Et. 6, Ap. 32, Cod Postal 062203, Bucuresti, Romania
			* Doinei (Strada), Nr. 94, Et. 2, Ap. 12 int 412, Cod Postal 077086, Fundeni, Romania
			* Lieberman Partners Srl Braniste Valeriu (Strada), Nr. 37-39, Et. 2, Ap. 8, Cod Postal 030715, Bucuresti, Romania
			
			WAIT WINDOW 'Import clienti... Caut clientul ' + TRANSFORM(RECNO()) + '/' + TRANSFORM(RECCOUNT()) NOWAIT 
						
			SELECT cParteneri
			If !Empty(m.pcCodFiscal)
				LOCATE FOR ALLTRIM(STRTRAN(cod_fiscal, ' ', '')) = m.pcCodFiscal
		*!*			lnSucces = goExecutor.oSelect2Value(m.lcSqlCod, @pnIdPart)
			ELSE
				LOCATE FOR ALLTRIM(UPPER(denumire)) = m.pcDenumire
		*!*			lnSucces = goExecutor.oSelect2Value(m.lcSqlDenumire, @pnIdPart)
			Endif
		*!*		If m.lnSucces = -1
		*!*			Thisform.trace('Eroare la selectia din clienti ROA ' + goExecutor.oPrelucrareEroare())
		*!*			Exit
		*!*		Endif
			IF FOUND()
				pnIdPart = cParteneri.id_part
			ENDIF 
		
			If !Empty(Nvl(m.pnIdPart, 0))
				Replace id_part With m.pnIdPart In cClienti
				lcMesaj = 'Client existent ' + Alltrim(cClienti.denumire) + ' CUI: ' + Alltrim(cClienti.cod_fiscal) + ' ID: ' + Alltrim(Transform(cClienti.id_part))
				Thisform.trace(m.lcMesaj)
			Else
				* Adaugare clienti
				Select cClienti
				lcDenumire = Nvl(Strtran(Alltrim(Upper(denumire)), ['], ['']), "")
				lcNume = Nvl(Strtran(Alltrim(Upper(nume)), ['], ['']), "")
				lcPrenume = Nvl(Strtran(Alltrim(Upper(prenume)), ['], ['']), "")
				If Empty(m.lcNume)
					lcNume = m.lcDenumire
					m.lcPrenume = ''
				Endif
				lcCod_fiscal = Upper(Alltrim(cod_fiscal))
				lcReg_comert = Nvl(Alltrim(Upper(reg_com)), "")
				lcId_loc_inreg = 'NULL'
				lcId_categ_ent = 'NULL'
				lcPrefix = ""
				lcSufix = ""
				lcTip_persoana = IIF(!EMPTY(m.lcCod_fiscal) and LEN(m.lcCod_fiscal) < 13, "1", "2")
				lcBanca = ""
				lcCont_Banca = ""
				lcinactiv = "0"
				lcMotiv_inactiv = ""
				lcIdString = "16"
				lcCorespDel = ""
				lcId_util = "-3"
				lcSqlInsert = [begin pack_def.adauga_partener('] + lcDenumire + [','] + lcNume + [','] + lcPrenume + [','] + lcCod_fiscal + [','] + ;
					lcReg_comert + [',] + lcId_loc_inreg + [,] + lcId_categ_ent + [,'] + lcPrefix + [','] + lcSufix + [',] + ;
					lcTip_persoana + [,'] + lcBanca + [','] + lcCont_Banca + [',] + lcinactiv + [,'] + lcMotiv_inactiv + [',] + ;
					lcId_util + [,'] + lcIdString + [','] + lcCorespDel + [',?@pnIdPart); end;]
		
				lnSucces = goExecutor.oExecute(m.lcSqlInsert)
				If !Empty(Nvl(m.pnIdPart, 0))
					Replace id_part With m.pnIdPart In cClienti
					Thisform.trace('Client nou ' + Alltrim(cClienti.denumire) + ' CUI: ' + Alltrim(cClienti.cod_fiscal) + ' ID: ' + Alltrim(Transform(cClienti.id_part)))
				Else
					lcMesaj = 'Eroare la adaugarea in clienti ROA ' + Alltrim(cClienti.denumire) + ' CUI: ' + Alltrim(cClienti.cod_fiscal) + Chr(13) + Chr(10) + goExecutor.oPrelucrareEroare()
					Thisform.trace(m.lcMesaj)
					AMessagebox(m.lcMesaj, 0 + 48, _Screen.Caption )
					Exit
				Endif && !Empty(Nvl(m.pnIdPart,0))
		
			Endif && !Empty(Nvl(m.pnIdPart, 0))
			
			***********************************
			* Adresa partener
			***********************************
			If !Empty(m.lcAdresa)
				CALCULATE CNT(id_adresa) FOR id_part = m.pnIdPart TO pnNrAdrese IN cAdrese 
			*!*		pnNrAdrese = 0
			*!*		lnSucces = goExecutor.oSelect2Value([SELECT COUNT(*) as nr FROM adrese_parteneri WHERE id_part = ?pnIdPart], @pnNrAdrese)
			*!*		If m.lnSucces < 0
			*!*			lcMesaj = goExecutor.cEroare
			*!*			Thisform.trace(m.lcMesaj)
			*!*			AMessagebox(m.lcMesaj, 0 + 48, _Screen.Caption )
			*!*			Exit
			*!*		Endif
		
				lcIdPart = Alltrim(Str(m.pnIdPart))
				lcDenumire_adresa = ""
				lcDA_apare = "0"
				lcStrada = ""
				lcNumar = ""
		
				lcBloc = ""
				lcScara = ""
				lcApart = ""
				lcEtaj = ""
				lcId_loc = "NULL"
				lcLocalitate = ""
				lcId_Judet = "NULL"
				lcCodpostal = "NULL"
				lcId_tara =  "NULL"
				lcTelefon1 = ""
				lcTelefon2 = ""
				lcFax = ""
				lcEmail = ""
				lcWeb = ""
				lcPrincipala = Iif(m.pnNrAdrese = 0, "1", "0")
				lcinactiv = "0"
				lcId_util = "-3"
				
				lnItems = Getwordcount(m.lcAdresa, ',')
				For lnItem = 1 To m.lnItems
					lcItem = Alltrim(Getwordnum(m.lcAdresa, m.lnItem, ','))
					Do Case
						Case m.lnItem = 1 && strada
							lcStrada = LEFT(Alltrim(m.lcItem), 150)
						Case 'NR.' $ m.lcItem && nr strada
							lcNumar = LEFT(Alltrim(Strtran(m.lcItem, 'NR.', '')), 10)
						Case 'BL.' $ m.lcItem
							lcBloc = LEFT(Alltrim(Strtran(m.lcItem, 'BL.', '')), 30)
						Case 'SC.' $ m.lcItem
							lcScara = LEFT(Alltrim(Strtran(m.lcItem, 'SC.', '')), 10)
						Case 'ET.' $ m.lcItem
							lcEtaj = LEFT(Alltrim(Strtran(m.lcItem, 'ET.', '')), 20)
						Case 'AP.' $ m.lcItem
							lcApart = LEFT(Alltrim(Strtran(m.lcItem, 'AP.', '')), 10)
						Case 'COD POSTAL' $ m.lcItem
							lcCodpostal = ALLTRIM(STR(INT(VAL(LEFT(Alltrim(Strtran(m.lcItem, 'COD POSTAL', '')), 20)))))
						Case 'LOC.' $ m.lcItem
							lcLocalitate = Alltrim(Strtran(m.lcItem, 'LOC.', ''))
						Case 'JUD.' $ m.lcItem
							lcJudet = Alltrim(Strtran(m.lcItem, 'JUD.', ''))
						Case m.lnItem = m.lnItems - 1 And Empty(m.lcLocalitate)
							lcLocalitate = m.lcItem
						Case m.lnItem = m.lnItems
							lcTara = m.lcItem
					Endcase
				Endfor
		
				lnIdJudetBucuresti = 10
				lcJudetBucuresti = "BUCURESTI"
				lnIdLocalitateBucuresti = 1759
				lnIdTaraRO = 1
				
				If m.lcLocalitate = 'BUCURESTI'
					m.lcLocalitate = 'BUCURESTI SECTORUL 1'
				Endif
				If Empty(m.lcLocalitate)
					lcLocalitate = 'BUCURESTI SECTORUL 1'
				Endif
				If Empty(m.lcJudet)
					lcJudet = m.lcJudetBucuresti
				ENDIF
				
				* caut adresa dupa localitate. daca nu o gasesc, o adaug
				SELECT cAdrese
				LOCATE FOR id_part = m.pnIdPart AND localitate = m.lcLocalitate
				IF !FOUND()
					pnIdJudet = 0
					lnSucces = goExecutor.oSelect2Value([select j.id_judet, j.id_tara from syn_nom_judete j where j.judet = '] + m.lcJudet + [' and j.inactiv = 0 and j.sters = 0], @pnIdJudet)
				
					If m.lnSucces > 0
						IF EMPTY(NVL(m.pnIdJudet,0))
							lnSucces = goExecutor.oSelect2Value([select j.id_judet, j.id_tara from syn_nom_judete j where j.judet like '%] + m.lcJudetBucuresti + [%' and j.inactiv = 0 and j.sters = 0], @pnIdJudet)
							IF m.lnSucces > 0
								lnIdJudet = m.pnIdJudet
							ELSE
								lnIdJudet = m.lnIdJudetBucuresti	 										
							ENDIF 
						ELSE 
							lnIdJudet = m.pnIdJudet	
						ENDIF
					ELSE 
						This.Trace(goExecutor.cEroare)
						lnIdJudet = m.lnIdJudetBucuresti
					ENDIF 
					
					lcSql = [select l.id_loc, l.id_judet, j.id_tara from syn_nom_localitati l left join syn_nom_judete j on l.id_judet = j.id_judet where l.id_judet = ] + ALLTRIM(STR(m.lnIdJudet)) + [ and l.localitate = '] + m.lcLocalitate + [' and l.inactiv = 0 and l.sters = 0 order by l.localitate]
					lnSucces = goExecutor.oExecute(m.lcSql, 'cLocalitateTemp')
					If m.lnSucces > 0
						IF RECCOUNT('cLocalitateTemp') > 0
							Select cLocalitateTemp
							Go Top
							lcId_loc = Alltrim(Str(id_loc))
							lcId_Judet = Alltrim(Str(id_judet))
							lcId_tara = Alltrim(Str(id_tara))
							Use In (Select('cLocalitateTemp'))					
						ELSE
							Use In (Select('cLocalitateTemp'))					
							lcSql = [select l.id_loc, l.id_judet, j.id_tara from syn_nom_localitati l left join syn_nom_judete j on l.id_judet = j.id_judet where l.id_judet = ] + ALLTRIM(STR(m.lnIdJudet)) + [ and l.inactiv = 0 and l.sters = 0 order by l.localitate]
							lnSucces = goExecutor.oExecute(m.lcSql, 'cLocalitateTemp')
							If m.lnSucces > 0
								Select cLocalitateTemp
								Go Top
								lcId_loc = Alltrim(Str(id_loc))
								lcId_Judet = Alltrim(Str(id_judet))
								lcId_tara = Alltrim(Str(id_tara))					
							ELSE 
								This.Trace(goExecutor.cEroare)						
							ENDIF 
							Use In (Select('cLocalitateTemp'))
						ENDIF 
					ELSE
						This.Trace(goExecutor.cEroare)
					ENDIF 	
		
					IF EMPTY(NVL(m.lcId_loc, ''))
						lcId_loc = Alltrim(Str(m.lnIdLocalitateBucuresti))
						lcId_Judet = Alltrim(Str(m.lnIdJudetBucuresti))
						lcId_tara = Alltrim(Str(m.lnIdTaraRO))				
					Endif && lnSucces
		
					If m.lcId_loc <> 'NULL'
						pnIdAdresa = 0
		*!*					* caut adresa dupa localitate. daca nu o gasesc, o adaug
		*!*					lcSql = [SELECT id_adresa FROM adrese_parteneri where sters = 0 and inactiv = 0 and id_loc = ] + m.lcId_loc + [ AND id_part = ] + m.lcIdPart
		*!*					lnSucces = goExecutor.oSelect2Value(m.lcSql, @pnIdAdresa)
		*!*					If m.lnSucces < 0
		*!*						lcMesaj = goExecutor.cEroare
		*!*						Thisform.trace(m.lcMesaj)
		*!*						AMessagebox(m.lcMesaj, 0 + 48, _Screen.Caption )
		*!*						Exit
		*!*					Endif
		
						If Empty(NVL(m.pnIdAdresa,0))
							lcSql = [begin pack_def.adauga_adresa_partener2(] + lcIdPart + [,'] + lcDenumire_adresa + [',] + lcDA_apare + [,] + ;
								['] + lcStrada + [','] + lcNumar + [','] + ;
								lcBloc + [','] + lcScara + [','] + lcApart + [','] + lcEtaj + [',] + lcId_loc + [,'] + lcLocalitate + [',] + lcId_Judet + [,] + lcCodpostal + [,] + lcId_tara + [,'] + ;
								lcTelefon1 + [','] + lcTelefon2 + [','] + lcFax + [','] + lcEmail + [','] + lcWeb + [',] + ;
								lcPrincipala + [,] + lcinactiv + [,] + lcId_util + [,?@pnIdAdresa); end;]
		
							lnSucces = goExecutor.oExecute(m.lcSql)
							If m.lnSucces < 0
								lcMesaj = goExecutor.cEroare
								Thisform.trace(m.lcMesaj)
								AMessagebox(m.lcMesaj, 0 + 48, _Screen.Caption )
								Exit
							Endif
						Endif && empty(m.pnIdAdresa)
		
					Endif && m.lcId_loc <> 'NULL'
				ENDIF && !found()	
		
			Endif && !empty(m.lcAdresa)
		
		
		Endscan && cClienti
		
		USE IN (SELECT('cParteneri'))
		lnSucces = 1
		Return m.lnSucces
		
	ENDPROC

	PROCEDURE connectroa
	ENDPROC

	PROCEDURE creeazanote
		* Creeaza actactan cu note contabile vanzari
		Local lnIdPartCasa, lnSucces, lnIdSet, llTVAIncasare
		Local lcContTVA, lcSql, ldDataCurenta, ldDataIreg, ldLastData, lnCota, lnIdBaza, lnIdTVA, lnProcTVA, llAnulat
		Local ldDataAct, llFurnizorTVAI, lnIdBaza1, lnIdBaza2, lnIdJtva, lnIdTVA1, lnIdTVA2, lnTaxCode
		*:Global pcCasa
		Private pnTVAIncasare, pnIdFdoc, pcPartCasa, pnIdPartCasa
		
		lnIdSet = IIF(This.lAnulat, 99996, 50100)
		llAnulat = This.lAnulat
		pnIdPartCasa = 0
		pcPartCasa = ''
		lnSucces = -1
		
		UPDATE iesiri SET casa = UPPER(NVL(casa,''))
		
		SELECT distinct casa, CAST(0 as I) as id_part ;
			FROM iesiri ;
			WHERE !EMPTY(NVL(casa,'')) ;
			INTO CURSOR cCase READWRITE 
		
		SELECT cCase
		LOCATE FOR INLIST(casa, 'CENTRALA', 'CASA LEI')
		IF !FOUND()
			INSERT INTO cCase (casa, id_part) VALUES ('CENTRALA', 0)
			INSERT INTO cCase (casa, id_part) VALUES ('CASA LEI', 0)
		ENDIF 
		SELECT cCase
		INDEX on casa TAG casa 
		 
		
		SELECT cCase
		SCAN
			pcCasa = ALLTRIM(casa)
			lcSql = [SELECT MAX(ID_PART) AS ID_PART FROM nom_parteneri WHERE STERS = 0 AND INACTIV = 0 AND denumire = ?pcCasa]
			lnSucces = goExecutor.oSelect2Value(m.lcSql, @pnIdPartCasa)
			IF m.lnSucces > 0
				REPLACE id_part WITH NVL(m.pnIdPartCasa,0) IN cCase
			ELSE
				Thisform.trace('Eroare la selectia partener casa/banca ROA ' + m.pcCasa + " " + goExecutor.oPrelucrareEroare())
				EXIT 
			ENDIF 
		ENDSCAN 
		
		IF m.lnSucces < 0
			RETURN m.lnSucces		
		ENDIF 
		
		pnIdPartCasa = 0
		pcPartCasa = ''
		SELECT cCase
		LOCATE FOR INLIST(casa, 'CENTRALA', 'CASA LEI') AND !EMPTY(NVL(id_part,0))
		IF FOUND()
			pnIdPartCasa = id_part
			pcPartCasa = ALLTRIM(casa)
			UPDATE iesiri SET casa = m.pcPartCasa WHERE NVL(incasat,'') = 'DA' AND EMPTY(NVL(casa,''))
		ENDIF 	
		
		
		
		pnIdFdoc = 0
		lcSql = [SELECT ID_FDOC FROM NOM_FDOC WHERE STERS = 0 AND INACTIV = 0 AND FEL_DOCUMENT = 'FACTURA']
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pnIdFdoc)
		If m.lnSucces < 0
			AMESSAGEBOX(goExecutor.cEroare, 0 + 48, _Screen.Caption)
			RETURN m.lnSucces
		ELSE 	
			pnTVAIncasare = 0
			lcSql = [SELECT tva_incasare FROM calendar WHERE an = ] + Alltrim(Str(m.gnAn)) + [ and luna = ] + Alltrim(Str(m.gnLuna))
			lnSucces = goExecutor.oSelect2Value(m.lcSql, @pnTVAIncasare)
			llTVAIncasare = (m.pnTVAIncasare = 1)
		ENDIF 
		
		If m.lnSucces < 0
			AMESSAGEBOX(goExecutor.cEroare, 0 + 48, _Screen.Caption)
			RETURN m.lnSucces
		Else
			lcContTVA = Iif(m.llTVAIncasare, '4428', '4427')
			Create Cursor cNote (Id N(1), scd c(4), scc c(4), proctva N(2), id_jtva1 N(4) Null, id_jtva2 N(4) Null)
			For lnCota = 1 To 8
				Do Case
					Case lnCota = 1
						lnProcTVA = 24
						lnIdBaza1 = 15 && fara TVA Incsare
						lnIdTVA1 = 16
						lnIdBaza2 = 21 && cu TVA Incasare
						lnIdTVA2 = 22				
						
					Case lnCota = 2
						lnProcTVA = 20
						lnIdBaza1 = 27 && fara TVA Incsare
						lnIdTVA1 = 28
						lnIdBaza2 = 29 && cu TVA Incasare
						lnIdTVA2 = 30								
					Case lnCota = 3
						lnProcTVA = 19
						lnIdBaza1 = 1 && fara TVA Incsare
						lnIdTVA1 = 2
						lnIdBaza2 = 31 && cu TVA Incasare
						lnIdTVA2 = 32								
					Case lnCota = 4
						lnProcTVA = 9
						lnIdBaza1 = 3 && fara TVA Incsare
						lnIdTVA1 = 4
						lnIdBaza2 = 23 && cu TVA Incasare
						lnIdTVA2 = 24								
					Case lnCota = 5
						lnProcTVA = 5
						lnIdBaza1 = 13 && fara TVA Incsare
						lnIdTVA1 = 14
						lnIdBaza2 = 25 && cu TVA Incasare
						lnIdTVA2 = 26								
					Case lnCota = 6
						lnProcTVA = 0
						lnIdBaza1 = 11 && fara TVA Incsare
						lnIdTVA1 = 0
						lnIdBaza2 = 11 && cu TVA Incasare
						lnIdTVA2 = 0	
					Case lnCota = 7
						lnProcTVA = 21
						lnIdBaza1 = 35 && fara TVA Incsare
						lnIdTVA1 = 36
						lnIdBaza2 = 37 && cu TVA Incasare
						lnIdTVA2 = 38				
						
					Case lnCota = 8
						lnProcTVA = 11
						lnIdBaza1 = 39 && fara TVA Incsare
						lnIdTVA1 = 40
						lnIdBaza2 = 41 && cu TVA Incasare
						lnIdTVA2 = 42								
													
				Endcase
		
				Insert Into cNote (Id, scd, scc, proctva, id_jtva1, id_jtva2) Values (1, '4111', '707', m.lnProcTVA, m.lnIdBaza1, m.lnIdBaza2)
				If lnProcTVA <> 0
					Insert Into cNote (Id, scd, scc, proctva, id_jtva1, id_jtva2) Values (2, '4111', m.lcContTVA, m.lnProcTVA, m.lnIdTVA1, m.lnIdTVA2)
				Endif
				Insert Into cNote (Id, scd, scc, proctva, id_jtva1, id_jtva2) Values (3, '5311', '4111', m.lnProcTVA, Null, Null)
			Endfor
			ldLastData = Gomonth(Date(m.gnAn, m.gnLuna, 1), 1) - 1
			ldDataCurenta = Date()
			ldDataIreg = Iif(Year(m.ldDataCurenta) <> m.gnAn Or Month(m.ldDataCurenta) <> m.gnLuna, m.ldLastData, m.ldDataCurenta)
		
			Select lnIdSet As id_set, m.gnAn As an, m.gnLuna As luna, ;
				CAST(IIF(n.id = 1, IIF(i.cont7xx = '667', i.cont7xx, n.scd), IIF(n.id = 2, n.scd, IIF(!EMPTY(i.cont5xx), i.cont5xx, n.scd))) as C(4)) as scd, ;
				CAST(Iif(n.id = 1, IIF(i.cont7xx = '667', i.acont7xx, i.acont4111), IIF(n.id = 2, i.acont4111, i.acont5xx)) as C(4)) as ascd, ;
				CAST(Iif(n.id = 1, IIF(i.cont7xx = '667', n.scd, i.cont7xx), Iif(n.id = 2, IIF(!EMPTY(i.cont442x), i.cont442x, m.lcContTVA), n.scc)) as C(4)) As scc, ;
				CAST(Iif(n.id = 1, IIF(i.cont7xx = '667', i.acont4111, i.acont7xx), Iif(n.id = 2, i.acont442x, i.acont4111)) as C(4)) As ascc, ;
				i.dataact, IIF(m.llAnulat, 0, 1 + Round(i.proctva / 100, 2)) As proc_tva, ;
				CAST(IIF(m.llAnulat, null, IIF(!EMPTY(i.tvaincasare), IIF(NVL(i.tvaincasare,'') = 'NU', n.id_jtva1, n.id_jtva2), IIF(!m.llTvaIncasare, n.id_jtva1, n.id_jtva2))) as N(4)) As id_jtva_coloana, ;
				i.nract, i.serie_act, i.datascad, i.dataact As dataireg, ;
				Cast(Iif(n.id = 1, IIF(i.cont7xx = '667', 0, i.id_part), IIF(n.id = 2, i.id_part, Thisform.GetIdPartCasa(i.casa))) As N(10)) As id_partd, ;
				Cast(Iif(n.id = 1, IIF(i.cont7xx = '667', '', i.denumire), IIF(n.id = 2, i.denumire, i.casa)) As C(100)) As partd, ;
				Cast(Iif(n.id = 1, IIF(INLIST(i.cont7xx, '667', '419'), i.id_part, 0), IIF(n.id = 2, 0, i.id_part)) As N(10)) As id_partc, ;
				Cast(Iif(n.id = 1, IIF(INLIST(i.cont7xx, '667', '419'), i.denumire, ''), IIF(n.id = 2, '', i.denumire)) As C(100)) As partc, ;
				Cast(0 As N(14)) As pereched, Cast(Iif(N.Id = 3, i.nract, 0) As N(14)) As perechec, ;
				Cast(Iif(N.Id = 1, IIF(i.cont7xx = '667',-i.valftva, i.valftva), Iif(N.Id = 2, valtva, valftva + valtva)) As N(20, 2)) As suma, ;
				Cast(m.gnIdSucursala As I NULL) As id_sucursala, Space(100) As sucursala, ;
				Cast(m.pnIdFdoc As N(5)) As id_fdoc, 'FACTURA' As fdoc, ;
				CAST(0 as I) as id_factd,  Cast(Iif(N.Id = 3, -1, 0) As I) As id_factc, CAST(0 as I) as id_fact, pnTVAIncasare as tva_incasare, CAST(0 as I) as cod ;
				From iesiri i Join cNote N On i.proctva = N.proctva AND n.id <= IIF(i.incasat = 'DA', 3, 2) ;
				left join cCase c on i.casa = c.casa ;
				Into Cursor actactanTemp
		
			Select id_set, an, luna, dataact, datascad, dataireg, serie_act, nract, proc_tva, id_jtva_coloana,scd, ascd, scc, ascc, ;
				id_partd, partd, id_partc, partc, pereched, perechec, id_sucursala, sucursala, id_fdoc, fdoc, id_factd, id_factc, id_fact, tva_incasare, cod, ;
				Sum(suma) As suma ;
				From actactanTemp ;
				Group By id_set, an, luna, dataact, datascad, dataireg, serie_act, nract, proc_tva, id_jtva_coloana, scd, ascd, scc, ascc, id_partd, partd, id_partc, partc, pereched, perechec, id_sucursala, sucursala, id_fdoc, fdoc, id_factd, id_factc, id_fact, tva_incasare, cod ;
				Order By id_set, an, luna, dataact, datascad, dataireg, serie_act, nract, proc_tva, id_jtva_coloana, scd, ascd, scc, ascc, id_partd, partd, id_partc, partc, pereched, perechec, id_sucursala, sucursala, id_fdoc, fdoc, id_factd, id_factc, id_fact, tva_incasare, cod ;
				Into Cursor actactan READWRITE 
			
			Use In (Select('actactantemp'))
			Use In (Select('cNote'))
			lnSucces = 1
		Endif
		
		Return m.lnSucces
		 
	ENDPROC

	PROCEDURE disconnectroa
	ENDPROC

	PROCEDURE exportdate
		* Import from xlsx (Sheet1) cod C(8), Data D, Cont C(20), proctva N(2), valftva N(16, 2), valtva N(16, 2)
		* denumire, cod fiscal, reg_com, adresa, serie, nr, factura, data act, data scadenta, cont 7xx, procent tva, valoare fara TVA, valoare TVA
		Local lcFile, lnSucces, lcFileBinary, ldData1, ldData2
		ldData1 = Thisform.txtData1.Value
		ldData2 = Thisform.txtData2.Value
		lnSucces = 1
		
		AMESSAGEBOX('Alegeti fisierul xlsx completat cu facturile pentru import. Atentie! Pagina cu facturi din fisierul xlsx trebuie sa se numeasca "Sheet1"!',0+64,_Screen.Caption)
		
		lcFile=Getfile('xlsx','Fisier xlsx','Alege', 0,'Fisier xlsx cu facturi')
		If Empty(m.lcFile)
			lnSucces = -1
			Return m.lnSucces
		Endif
		
		llAnulat = This.lAnulat
		
		* Import first sheet
		importfromxlsx(m.lcFile,2,'Sheet1',.T.,.T.,1,'iesiri')
		If Used('iesiri')
			If m.llAnulat
				Select Upper(denumire) As denumire, Space(100) As nume, Space(100)As prenume, ;
					LEFT(Upper(cod_fiscal), 20) As cod_fiscal, Left(reg_com, 50) As reg_com, Left(Upper(Strtran(adresa, Chr(13) + Chr(10), ' ')), 250) As adresa, Left(serie_act, 20) As serie_act,;
					IIF(Type('iesiri.nract') = 'N', Int(nract), Int(Val(nract))) As nract, ;
					IIF(Type('iesiri.dataact') = 'D', dataact, Ctod(dataact)) As dataact, ;
					IIF(Type('iesiri.dataact') = 'D', dataact, Ctod(dataact)) As datascad, ;
					0 As proctva, ;
					0.00 As valftva, ;
					0.00 As valtva, ;
					CAST(Iif(Type('iesiri.cont7xx') <> 'C', Alltrim(Transform(cont7xx)), cont7xx) As C(4)) As cont7xx, ;
					CAST(Iif(Type('iesiri.acont7xx') <> 'C', Alltrim(Transform(acont7xx)), acont7xx) As C(4)) As acont7xx, ;
					CAST(Iif(Type('iesiri.aCont4111') <> 'C', Alltrim(Transform(aCont4111)), aCont4111) As C(4)) As aCont4111, ;
					SPACE(4) As Cont442x, ;
					SPACE(4) As aCont442x, ;
					'NU' As incasat, ;
					SPACE(100) As casa, ;
					SPACE(4) As cont5xx, ;
					SPACE(4) As acont5xx, ;
					'NU'  As tvaincasare, ;
					'NU'  As taxareinversa ;
					From iesiri Into Cursor cIesiri Readwrite
		
			Else
				Select Upper(denumire) As denumire, Space(100) As nume, Space(100)As prenume, ;
					LEFT(Upper(cod_fiscal), 20) As cod_fiscal, Left(reg_com, 50) As reg_com, Left(Upper(Strtran(adresa, Chr(13) + Chr(10), ' ')), 250) As adresa, Left(serie_act, 20) As serie_act,;
					IIF(Type('iesiri.nract') = 'N', Int(nract), Int(Val(nract))) As nract, ;
					IIF(Type('iesiri.dataact') = 'D', dataact, Ctod(dataact)) As dataact, ;
					IIF(Type('iesiri.datascad') = 'D', datascad, Ctod(datascad)) As datascad, ;
					CAST(Iif(Type('iesiri.proctva') = 'N', Round(proctva,2), Round(Val(proctva),2)) As N(5,2)) As proctva, ;
					CAST(Iif(Type('iesiri.valftva') = 'N', Round(valftva,2), Round(Val(valftva),2)) As N(18,2)) As valftva, ;
					CAST(Iif(Type('iesiri.valtva') = 'N', Round(valtva,2), Round(Val(valtva),2)) As N(18,2)) As valtva, ;
					CAST(Iif(Type('iesiri.cont7xx') = 'N', Alltrim(Transform(cont7xx)), cont7xx) As C(4)) As cont7xx, ;
					CAST(Iif(Type('iesiri.acont7xx') = 'N', Alltrim(Transform(acont7xx)), acont7xx) As C(4)) As acont7xx, ;
					CAST(Iif(Type('iesiri.aCont4111') = 'N', Alltrim(Transform(aCont4111)), aCont4111) As C(4)) As aCont4111, ;
					CAST(Iif(Type('iesiri.Cont442x') = 'N', Alltrim(Transform(Cont442x)), Cont442x) As C(4)) As Cont442x, ;
					CAST(Iif(Type('iesiri.aCont442x') = 'N', Alltrim(Transform(aCont442x)), aCont442x) As C(4)) As aCont442x, ;
					CAST(Alltrim(Transform(incasat)) As C(2)) As incasat, ;
					CAST(Upper(Alltrim(Transform(casa))) As C(100)) As casa, ;
					CAST(Iif(Type('iesiri.cont5xx') = 'N', Alltrim(Transform(cont5xx)), cont5xx) As C(4)) As cont5xx, ;
					CAST(Iif(Type('iesiri.acont5xx') = 'N', Alltrim(Transform(acont5xx)), acont5xx) As C(4)) As acont5xx, ;
					CAST(Alltrim(Transform(tvaincasare)) As C(2)) As tvaincasare, ;
					CAST(Alltrim(Transform(taxareinversa)) As C(2)) As taxareinversa ;
					From iesiri Into Cursor cIesiri Readwrite
			Endif
			Use In (Select('iesiri'))
			Update cIesiri Set datascad = Gomonth(dataact,1) Where Empty(datascad)
			Delete From cIesiri Where !Between(dataact, m.ldData1, m.ldData2)
		
			Update cIesiri Set cod_fiscal = Nvl(cod_fiscal, '')
		
			*** presupun ca denumire este de forma [PRENUME NUME] si transform in [NUME PRENUME] - trebuie in functie de optiune
			Update cIesiri Set prenume = Alltrim(Getwordnum(denumire, 1, ' ')) Where Empty(cod_fiscal) Or Len(Alltrim(cod_fiscal)) >= 13
			Update cIesiri Set nume = Alltrim(Strtran(denumire, Alltrim(prenume), '',1,1,1)) Where Empty(cod_fiscal) Or Len(Alltrim(cod_fiscal)) >= 13
			Update cIesiri Set denumire = Alltrim(nume) + " " + Alltrim(prenume) Where Empty(cod_fiscal) Or Len(Alltrim(cod_fiscal)) >= 13
		
			Select Cast(Null As I) As id_part, denumire, nume, prenume, cod_fiscal, reg_com, Max(adresa) As adresa ;
				From cIesiri ;
				GROUP By denumire, nume, prenume, cod_fiscal, reg_com ;
				Into Cursor cClienti Readwrite
		
			lnSucces = This.CompleteazaClientiROA()
		
			If m.lnSucces = 1
				Select I.nract, I.serie_act, C.id_part, C.denumire, C.cod_fiscal, C.reg_com, ;
					I.dataact, I.datascad, I.proctva, ;
					I.cont7xx, I.acont7xx, I.aCont4111, I.Cont442x, I.aCont442x, ;
					I.incasat, I.casa, I.cont5xx, I.acont5xx, I.tvaincasare, I.taxareinversa, ;
					SUM(I.valftva) As valftva, Sum(I.valtva) As valtva ;
					FROM cIesiri I Left Join cClienti C On I.denumire = C.denumire And I.cod_fiscal = C.cod_fiscal ;
					GROUP By I.nract, I.serie_act, C.id_part, C.denumire, C.cod_fiscal, C.reg_com, ;
					I.dataact, I.datascad, I.proctva, I.cont7xx, I.acont7xx, I.aCont4111, I.Cont442x, I.aCont442x, ;
					I.incasat, I.casa, I.cont5xx, I.acont5xx, I.tvaincasare, I.taxareinversa ;
					INTO Cursor iesiri Readwrite
			Endif
		Else
			* Create Cursor cIesiri (denumire C(100), cod_fiscal C(20), reg_com C(50), adresa C(250), serie_act C(20), nract N(14), dataact D, datascad D, proctva N(2), valftva N(16, 2), valtva N(16, 2), Cont7xx C(20), acont7xx C(20), aCont4111 C(20), Cont442x C(20), aCont442x C(20))
			* Create Cursor cClienti (id_part I, denumire C(100), cod_fiscal C(20), reg_com C(50), adresa C(250))
			lnSucces = -1
		Endif
		
		Return m.lnSucces
		
	ENDPROC

	PROCEDURE getidpartcasa
		LPARAMETERS tcCasa
		LOCAL lcCasa, lnId
		lnId = 0
		lcCasa = ALLTRIM(m.tcCasa)
		IF USED('cCase')
			IF SEEK(m.lcCasa, 'cCase','casa')
				lnId = id_part
			ENDIF 
		ENDIF 
		RETURN m.lnId
	ENDPROC

	PROCEDURE Init
		LPARAMETERS tcTip
		
		This.lAnulat = (UPPER(ALLTRIM(TRANSFORM(m.tcTip))) = 'ANULAT')
		
		DODEFAULT()
		*!*	this.Icon = _screen.Icon 
		thisform.txtData1.Value = DATE(m.gnAn, m.gnLuna, 1)
		thisform.txtData2.Value = GOMONTH(DATE(m.gnAn, m.gnLuna, 1), 1) - 1
		
		
		
	ENDPROC

	PROCEDURE modificanote
		PRIVATE pnCod
		Local loForm As [frm_modific2024]
		Local llMaximize, llNotaNoua, lnIdSet, lnSucces
		lnSucces = 1
		update_jtva_coloane("JV", 'crsExplicatiiTVATemp', 1)
		SET STEP ON 
		lcSql = [SELECT * FROM vact WHERE 1=2]
		llSucces = goExecutor.oExecuta(m.lcSql, 'actactan_temp')
		If m.llSucces
			Select actactan_temp
			IF USED('actactan')
				Append From Dbf('actactan')
			ENDIF 	
			
			Select .F. As ales, IIF(EMPTY(NVL(a.id_jtva_coloana,0)), 0, 1) As Cu_tva, Cast(0 As N(2)) As tipnota, a.*, b.denumire As explicatie_tva_2 ;
				From actactan_temp a Left Join crsExplicatiiTVATemp b On a.id_jtva_coloana = b.id_jtva_coloana ;
				Into Cursor tact Readwrite
		
			Update tact Set explicatie_tva = explicatie_tva_2
			
			USE IN (SELECT('actactan_temp'))
		
			* Actualizez taxcode SAFT in functie de furnizori RTVAI
			IF m.gl406
				SELECT tact 
				SCAN FOR !EMPTY(id_jtva_coloana)
					lnIdJtva = id_jtva_coloana
					ldDataAct = dataact
					llFurnizorTVAI = .F.
					lnTaxCode = GetTaxCode(m.gnAn, m.gnLuna, m.ldDataAct, m.lnIdJtva, m.llFurnizorTVAI)
					SELECT tact 
					REPLACE taxcode WITH m.lnTaxCode 
				ENDSCAN 
		
				UPDATE tAct SET paymentcode = '42' WHERE LEFT(scd,3) = '512' OR LEFT(scc,3) = '512' && plata in contul bancar
				UPDATE tAct SET paymentcode = '10' WHERE scd = '5311' && plata numerar
			ENDIF && gl406		
		ENDIF && llSucces
		
		update_jtva_coloane("", "crsJtvaTemp", 1)
		
		IF m.llSucces
			* Creez cursoare trul, trul_obinv
			lcSql = [select * from vrul_tot where 1=2]
			lcCursor = [v_rul]
		
			llSucces = goExecutor.oExecuta(lcSql,lcCursor)
		
			If m.llSucces		
				Select *, ;
					valoare + valtva As valoarectva, ;
					valoarev + valtvav As valoarevctva ;
					From v_rul ;
					Into Cursor trul READWRITE 
					
				Use In (SELECT('v_rul'))
		
				lcSql = [select * from vrul_obinv_tot where 1=2]
				lcCursor = [v_rul_obinv]
		
				llSucces = goExecutor.oExecuta(lcSql,lcCursor)
			Endif
		
			If m.llSucces
				Select *, ;
					valoare + valtva As valoarectva, ;
					valoarev + valtvav As valoarevctva ;
					From v_rul_obinv ;
					Into Cursor trul_obinv READWRITE 			
		
				Use In (SELECT('v_rul_obinv'))
			Endif
		ENDIF 
		
		Use In (Select('crsExplicatiiTVATemp'))
		IF m.llSucces
			lnIdSet = IIF(this.lAnulat, 99996, 50100)
			Select tact
			Go Top
			llNotaNoua = .T.
			llMaximize = .T.
			loForm = Createobject([frm_modific2024], m.lnIdSet, m.llNotaNoua)
			loForm.lb_titlu_alb_b121.Caption = 'Initializare solduri'
			Select tact
			loForm.Show(1, m.llMaximize)
			llSucces = (buton = 1)
		ENDIF 
		
		If m.llSucces
			Use In (Select('actactan'))
		
			Select tact
			Replace id_jtva_coloana With Null, PROC_TVA With 0 For Cu_tva = 0
		
			Select * From tact Into Cursor actactan Readwrite
		
			lcSql = [SELECT seq_cod.nextval FROM dual]
			SELECT distinct nract FROM  actactan INTO CURSOR cNract
			SELECT cNract
			SCAN 
				WAIT WINDOW 'Generez cod pentru factura ' + TRANSFORM(RECNO()) + '/' + TRANSFORM(RECCOUNT()) NOWAIT 
				pnCod = 0
				lnSucces = goExecutor.oSelect2Value(m.lcSql, @pnCod)
				IF lnSucces > 0
					UPDATE actactan SET cod = m.pnCod WHERE nract = cNract.nract
				ENDIF 
			ENDSCAN 
			USE IN (SELECT('cNract'))
		
			SELECT actactan
			lnSucces  = OSCRIE_IN_FISIERE(0, .F., .F., .T.)
			llSucces = (m.lnSucces = 1)	
		Endif && buton = 1
		
		Use In (Select('tAct'))
		Use In (Select('actactan'))
		Use In (Select('trul'))
		Use In (Select('rul_temp'))
		Use In (Select('trul_obinv'))
		Use In (Select('rul_temp_obinv'))
		USE IN (SELECT('crsJtvaTemp'))
		
		
		RETURN m.llSucces
		
	ENDPROC

	PROCEDURE prelucreazafisierebreeze
	ENDPROC

	PROCEDURE scrienote
		Local lcMesaj, lcSql, lnHandle, lnSucces, lnSucces2
		Private gnAn, gnIdSucursala, gnIdUtil, gnLuna
		lnSucces = SQLSetprop(m.lnHandle, "Transactions", 2)
		
		if lnSucces > 0
			&& Creez note pentru fiecare luna/an
			Select Distinct an, luna From actactan Order By an, luna Into Cursor cAnLuna
			Select cAnLuna
			Scan
				*gnAn = an
				*gnLuna = luna
				*gnIdUtil = - 3
				*gnIdSucursala = Null
		
				if m.lnSucces > 0
					lcSql = [begin pack_contafin.init_scriere_act_rul_local(?gnIdUtil,?gnAn,?gnLuna, 1,0,?gnIdSucursala); end;]
					lnSucces = goExecutor.oExecute(m.lcSql)
				Endif
		
				if lnSucces > 0
					SELECT * FROM actactan WHERE an = m.gnAn AND luna = m.gnLuna INTO CURSOR cAct NOFILTER 
					lnSucces = This.sql_temp_insert('cAct', 'ACT_TEMP')
					USE IN (SELECT('cAct'))
				Endif
		
				if lnSucces > 0
					Private pcMesajRefacereNota
					pcMesajRefacereNota = ""
					lcSql = [begin pack_contafin.final_scriere_act_rul_local(?gnIdUtil, ?gnAn, ?gnLuna, NULL, 0, 1, ?@pcMesajRefacereNota); end;]
					lnSucces = goExecutor.oExecute(m.lcSql)
					If !Empty(Nvl(pcMesajRefacereNota, ''))
						This.trace(m.pcMesajRefacereNota)
						AMESSAGEBOX(m.pcMesajRefacereNota, 0 + 48, 'Atentie')
					Endif
					Release pcMesajRefacereNota
				ELSE
					lcMesaj = goExecutor.cEroare
					This.Trace(m.lcMesaj)
					AMESSAGEBOX(m.lcMesaj,0+48, _screen.Caption )
					EXIT 	
				ENDIF && lnSucces
			Endscan && cAnLuna
		
			Use In (Select('cAnLuna'))
		
			if lnSucces > 0
				lnSucces2 = goExecutor.oExecute('COMMIT')
				IF lnSucces2 <> 1
					lnSucces = 0
				ENDIF 
			Else
				lnSucces2 = goExecutor.oExecute('ROLLBACK')
			Endif
		
			If lnSucces2 <> 1
				This.trace(goExecutor.cEroare)
				AMESSAGEBOX(lcSql + Chr(13) + goExecutor.cEroare, 0 + 16, "Eroare")
			ELSE
				This.trace('Import realizat cu succes!')	
			Endif
		
			lnSucces3 = SQLSetprop(m.lnHandle, "Transactions", 1)
		
			If lnSucces3 <> 1
				lcMesaj = 'Programul nu a reusit sa treaca pe tranzactie automata. Iesiti din program si intrati din nou!'
				This.trace(m.lcMesaj)
				AMESSAGEBOX(m.lcMesaj, 0 + 48, 'Atentie!')
			Endif
		Endif && lnSucces
		
		Return m.lnSucces
		
	ENDPROC

	PROCEDURE sql_temp_insert
		Lparameters tcAlias, tcTableName
		
		Local lcAlias, lcCursor, lnSucces
		Local lcDate, lcFieldName, lcFieldType, lcFieldValue, lcFields, lcInsert, lcMesaj, lcNumber, lcSql
		Local lcString, lcTableName, lcValues, lnCount
		Local lnField
		
		Private poIreg, pcTableName
		Local lcVariable
		
		If Empty(tcAlias)
			lcAlias = 'ACTACTAN'
		Else
			lcAlias = Upper(Alltrim(tcAlias))
		Endif
		If Empty(tcTableName)
			pcTableName = 'ACT_TEMP'
		Else
			pcTableName = Upper(Alltrim(tcTableName))
		Endif
		
		lcCursor = 'crs_Coloane'
		lcSql = [SELECT COLUMN_NAME AS COLOANA from user_tab_columns WHERE table_name = ?pcTableName]
		lnSucces = goExecutor.oExecute(m.lcSql, m.lcCursor)
		If m.lnSucces <> 1
			lcMesaj = 'Eroare la selectare coloane din ' + m.lcTableName + Chr(13) + goExecutor.cEroare
			This.trace(m.lcMesaj)
			AMESSAGEBOX(m.lcMesaj, 0 + 16, 'Eroare')
		Endif
		
		If m.lnSucces = 1
			Select (lcAlias)
			lnCount = Fcount()
			lcFields = []
			lcValues = []
			lcInsert = []
			For lnField = 1 To lnCount
				Select (lcAlias)
				lcFieldName = Upper(Alltrim(Field(lnField)))
				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
				lcValues = lcValues + [,] + lcVariable
			Endfor
		
			lcFields = Substr(lcFields, 2)
			lcValues = Substr(lcValues, 2)
		
			&& INSERT INTO ACT_TEMP
			lcInsert = [INSERT INTO ] + m.pcTableName + [ (] + m.lcFields + [) VALUES (] + m.lcValues + [)]
		
			Select (lcAlias)
			Scan
				Scatter Name poIreg
				IF MOD(RECNO(), 100) = 0
					This.trace(' Inregistrare ' + TRANSFORM(RECNO()) + ' / ' + TRANSFORM(RECCOUNT()) + ' ...')
				ENDIF 
				&& FAC NULL ID-URILE CU VALOARE 0
				If TYPE('poReg.id_sucursala') = 'N' AND poIreg.id_sucursala = 0
					poIreg.id_sucursala = Null
				Endif
		
				lnSucces = goExecutor.oExecute(m.lcInsert)
				If lnSucces <> 1
					This.trace(goExecutor.cEroare)
					AMESSAGEBOX(goExecutor.cEroare, 0 + 16, 'Eroare')
					Exit
				Endif
		
			Endscan
		Endif
		
		Use In (Select('crs_coloane'))
		
		Return m.lnSucces
		
	ENDPROC

	PROCEDURE sql_temp_insert_values
		* daca se doreste insert into act_temp cu valori in loc de ?
		* trebuie decomentata sectiunea cu valori
		Lparameters tcAlias, tcTableName
		
		Local lcAlias, lcCursor, lnSucces
		Local lcDate, lcFieldName, lcFieldType, lcFieldValue, lcFields, lcInsert, lcMesaj, lcNumber, lcSql
		Local lcString, lcTableName, lcValues, lnCount
		Local lnField
		
		Private poIreg, pcTableName
		Local lcVariable
		
		If Empty(tcAlias)
			lcAlias = 'ACTACTAN'
		Else
			lcAlias = Upper(Alltrim(tcAlias))
		Endif
		If Empty(tcTableName)
			pcTableName = 'ACT_TEMP'
		Else
			pcTableName = Upper(Alltrim(tcTableName))
		Endif
		
		lcCursor = 'crs_Coloane'
		lcSql = [SELECT COLUMN_NAME AS COLOANA from user_tab_columns WHERE table_name = ?pcTableName]
		lnSucces = goExecutor.oExecute(m.lcSql, m.lcCursor)
		If m.lnSucces <> 1
			lcMesaj = 'Eroare la selectare coloane din ' + m.pcTableName + Chr(13) + goExecutor.cEroare
			This.trace(m.lcMesaj)
			AMESSAGEBOX(m.lcMesaj, 0 + 16, 'Eroare')
		Endif
		
		If m.lnSucces = 1
			Select (lcAlias)
			lnCount = Fcount()
			Scan
				lcFields = []
				lcValues = []
				lcInsert = []
				Scatter Name poIreg
		
				For lnField = 1 To lnCount
					Select (lcAlias)
					lcFieldName = Upper(Alltrim(Field(lnField)))
					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 = ''
		
					&& FAC NULL ID-URILE CU VALOARE 0
					If Inlist(lcFieldName, 'ID_SUCURSALA')
						If poIreg.id_sucursala = 0
							poIreg.id_sucursala = Null
						Endif
					Endif
					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
		
				lcFields = Substr(lcFields, 2)
				lcValues = Substr(lcValues, 2)
		
				&& INSERT INTO ACT_TEMP
				lcInsert = [INSERT INTO ] + m.pcTableName + [ (] + lcFields + [) VALUES (] + lcValues + [)]
				lnSucces = goExecutor.oExecute(m.lcInsert)
				If lnSucces <> 1
					This.Trace(goExecutor.cEroare)
					AMESSAGEBOX(goExecutor.cEroare, 0 + 16, 'Eroare')
					Exit
				Endif
		
			Endscan
		Endif
		
		Use In (SELECT('crs_coloane'))
		
		Return m.lnSucces
		
		
		
	ENDPROC

	PROCEDURE sterge_import
		Local lcCursor, lcMesaj, lcSql, llContinuare, lnDocumente, lnPercent, lnSucces
		PRIVATE pnCod
		llContinuare = .F.
		lcIdSet = IIF(this.lAnulat, '99996', '50100')
		lcSql = [select distinct cod from act where an=?gnAn and Luna=?gnLuna and id_set=] + m.lcIdSet + [ and sters=0]
		lcCursor = [cCoduri]
		lnSucces = goExecutor.oExecute(m.lcSql, m.lcCursor)
		If m.lnSucces > 0
			Select(m.lcCursor)
			lnDocumente = Reccount()
			If m.lnDocumente > 0
				llContinuare = (AMessagebox('Doriti sa stergeti ' + Alltrim(Str(m.lnDocumente)) + 'document(e), tip operatie ' + m.lcIdSet + ', din luna ' + Padl(m.gnLuna, 2, '0') + '/' + Alltrim(Str(m.gnAn)) + '?', 4 + 32, _Screen.Caption) = 6)
			Else
				AMessagebox('Nu sunt inregistrari in luna ' + Padl(m.gnLuna, 2, '0') + '/' + Alltrim(Str(m.gnAn)), 0 + 64, _Screen.Caption)
				llContinuare = .F.
			Endif
		Else
			AMessagebox(goExector.cEroare, 0 + 48, _Screen.Caption )
			This.Trace(goExecutor.cEroare)
			llContinuare = .F.
		Endif
		
		If !m.llContinuare
			Use In (Select(m.lcCursor))
			Return
		Endif
		
		lnSucces = SQLSetprop(m.gnhandle, "Transactions", 2)
		
		If lnSucces > 0
			lcSql = [begin pack_contafin.init_scriere_act_rul_local(?gnIdUtil,?gnAn,?gnLuna, 1,0,?gnIdSucursala); end;]
			lnSucces = goExecutor.oExecute(lcSql)
			If lnSucces > 0
				Select cCoduri
				Scan
					pnCod = cod
					If Mod(Recno(), 10) = 0
						lnPercent = Round(Recno() / Reccount() * 100, 0)
						This.Trace('Stergere ' + Alltrim(Str(m.lnPercent)) + '%')
					Endif
		
		
					lcSql = [begin pack_contafin.sterge_document(?pnCod); end;]
					lnSucces = goExecutor.oExecute(lcSql)
					If lnSucces < 0
						This.Trace(goExecutor.cEroare)
						Exit
					Endif
				Endscan
			Else
				This.Trace(goExecutor.cEroare)
			Endif
		Else
			This.Trace(goExecutor.cEroare)
		Endif
		
		If lnSucces < 0
			goExecutor.oExecute('ROLLBACK')
			lcMesaj = 'Stergerea nu s-a efectuat!'
		Else
			goExecutor.oExecute('COMMIT')
			lcMesaj = 'Stergerea s-a efectuat cu succes'
		Endif
		
		SQLSetprop(m.gnhandle, "Transactions", 1)
		
		This.Trace(m.lcMesaj)
		AMessagebox(m.lcMesaj, 0 + 48, _Screen.Caption)
		
	ENDPROC

	PROCEDURE templatexlsx
		* Import from xlsx (Sheet1) cod C(8), Data D, Cont C(20), proctva N(2), valftva N(16, 2), valtva N(16, 2)
		* denumire, cod fiscal, reg_com, adresa, serie, nr, factura, data act, data scadenta, cont 7xx, procent tva, valoare fara TVA, valoare TVA
		* template.xlsx
		
		Local lcFile, lcXLSXTemplate
		
		lcXLSXTemplate = ADDBS(m.gcAppPath) + "include\import_note_facturi_clienti.xlsx"
		IF !FILE(m.lcXLSXTemplate)
			AMESSAGEBOX('Nu exista fisierul ' + m.lcXLSXTemplate + CHR(13) + CHR(10) + 'Sunati la suportul tehnic',0+48)
			RETURN
		ENDIF 
		
		lcFile = PUTFILE('Salvati fisierul de import', 'import_note_facturi_clienti', 'xlsx')
		
		COPY FILE (m.lcXLSXTemplate) TO (m.lcFile)
		
		open_default_app(m.lcFile) 
		
		AMESSAGEBOX('Se va deschide fisierul xlsx cu instructiuni si cateva randuri completate pentru exemplificare.' + CHR(13) + CHR(10) + ;
			'Completati fisierul, salvati-l si apoi importati-l',0+64,_screen.Caption)
		
	ENDPROC

	PROCEDURE trace
		LPARAMETERS tcMessage
		
		This.edtInfo.Value = This.edtInfo.Value + TRANSFORM(NVL(tcMessage,'')) + CHR(13) + CHR(10)
		this.edtInfo.SelStart = LEN(this.edtInfo.Value)
		This.edtInfo.Refresh()
	ENDPROC

	PROCEDURE verificaimport
		Local lcCursor, lcSql, ldData1, ldData2, llContinuare, lnHandle, lnInregistrari, lnNrLuni1
		Local lnNrLuni2, lnSucces
		lnInregistrari = 0
		
		
		ldData1 = Thisform.txtData1.Value
		ldData2 = Thisform.txtData2.Value
		lnNrLuni1 = Year(m.ldData1) * 12 + Month(m.ldData1)
		lnNrLuni2 = Year(m.ldData2) * 12 + Month(m.ldData2)
		
		llContinuare = .F.
		lcIdSet = IIF(this.lAnulat, '99996', '50100')
		lcSql = [select COUNT(*) as nr from act where an*12+luna between ] + Alltrim(Str(m.lnNrLuni1)) + [ and ] + Alltrim(Str(m.lnNrLuni2)) + ;
			[ and dataact between TO_DATE('] + Dtos(m.ldData1) + [', 'YYYYMMDD') and TO_DATE('] + Dtos(m.ldData2) + [', 'YYYYMMDD')] + ;
			[ and id_set=] + m.lcIdSet + [ and sters=0]
		
		lcCursor = [cInregistrari]
		lnSucces = goExecutor.oExecute(m.lcSql, m.lcCursor)
		If m.lnSucces > 0
			Select(m.lcCursor)
			lnInregistrari = Nvl(nr, 0)
		ELSE
			AMESSAGEBOX(goExecutor.cEroare)
			This.Trace(goExecutor.cEroare)	
			lnInregistrari = 1 && ca sa nu mai continue importul
		Endif
		Use In (Select(m.lcCursor))
		
		Return (m.lnInregistrari > 0)
		
		
	ENDPROC

	PROCEDURE But_help1.Click
		LOCAL lcInfo
		TEXT TO lcInfo noshow
		1. Completati perioada de import
		
		2. Apasati pe butonul "Sablon xlsx".
		Se va deschide un fisier sablon tip "xlsx" cu date de exemplificare in care completati datele reale.
		Stergeti datele de exemplificare
		
		3. Apasati pe butonul "Importa in ROA"
		ENDTEXT 
		
		AMESSAGEBOX(m.lcInfo)
	ENDPROC

	PROCEDURE cmdExport.Click
		Local llContinuare
		
		llContinuare = .T.
		ldData1 = Thisform.txtData1.Value
		ldData2 = Thisform.txtData2.Value
		lnNrLuni1 = Year(m.ldData1) * 12 + Month(m.ldData1)
		lnNrLuni2 = Year(m.ldData2) * 12 + Month(m.ldData2)
		lnNrLuni = m.gnAn * 12 + m.gnLuna
		If m.lnNrLuni1 <> m.lnNrLuni2 Or m.lnNrLuni1 <> m.lnNrLuni
			llContinuare = .F.
			AMessagebox('Atentie! Perioada trebuie sa fie in luna curenta!', 0+48, _Screen.Caption)
		Endif
		
		If m.llContinuare And Thisform.VerificaImport()
			lcIdSet = IIF(thisform.lAnulat, '99996', '50100')
			llContinuare = (AMessagebox('Atentie! Exista deja documente importate (tip operatie ' + m.lcIdSet + ') in perioada selectata! Daca continuati, se vor duplica! Doriti sa continuati?', 4 + 32, _Screen.Caption) = 6)
		Endif
		
		If m.llContinuare
			Local lnSucces
			* import note din sursa externa (dbf, xls)
			lnSucces = Thisform.ExportDate()
		
			* creez act_temp
			If m.lnSucces > 0
				lnSucces = Thisform.Creeazanote()
		
				* import act_temp
				If used('actactan')
					If Thisform.chkModificaNote.Value = 1
						lnSucces = Thisform.ModificaNote()
					Else
						lnSucces = Thisform.Scrienote()
					ENDIF
					
					USE IN (SELECT('actactan'))			
				Endif
			Endif
		Endif
		
		Use In (Select('cIesiri'))
		Use In (Select('cClienti'))
		
		
		AMessagebox('Importul s-a incheiat!',0+64,_Screen.Caption)
		
	ENDPROC

	PROCEDURE cmdTemplate.Click
		Thisform.templatexlsx()
	ENDPROC

	PROCEDURE Command1.Click
		Thisform.sterge_import()
	ENDPROC

ENDDEFINE
