*!* 11.11.2009
*!* marius.mutu
*!* alocare, dezalocare numere inventar

*!* 04.07.2018
*!* marius.mutu
*!* op_transformare_mf - transformare imobilizare curs in imobilizare corporala/necorporala

*!*	03.06.2021
*!*	marius.mutu
*!*	+ op_schimbaredns_reevaluare - adaugare operatie schimbare dns si reevaluare

*!*	27.08.2024
*!*	marius.mutu
*!*	op_adaugare_la_mf - transformare imobilizare in curs in majorare imobilizare corporala/necorporala

************************************
***	Cod operatie
************************************
***		 1 - introducere
***		 2 - preluare
***		 3 - transformare in MF
***		 4 - iesire din gestiune
***		 5 - intrare in gestiune
***		 6 - majorare
***		 7 - reevaluare
***		 8 - schimbarea DNS
***		 9 - recalculare cota
***		10 - conservare
***		11 - scoatere din conservare
***		12 - modificare cu istoric
***		13 - inchidere
***		14 - schimbare DNS + reevaluare
************************************
* Intoarce un obiect cu lSucces = .T. daca succes, .F. daca eroare sau renunt

Procedure op_majorare
	Parameters tnTip, tnValoareMajorare
	
	Private pnvaloaremaj, pddatareeval, pnNrDoc, pdDataDoc, pnBunCapital, pnBazaTva, pnIdCotaTva, pnTva, pnProrataTva
	Store '' To ofrmmajorare
	Local llBunCapital
	gnButon = 2
	pnValoareMaj = IIF(EMPTY(m.tnValoareMajorare), 0, m.tnValoareMajorare)
	pdDataReeval = {}
	pcExplicatie = []
	pnFiscala = 0
	pnNrDoc = 0
	pdDataDoc = {}
	pnBunCapital = 0
	pnBazaTva = 0
	pnIdCotaTva = 0
	pnTva = 0
	pnProrataTva = 0

	lcSql = [select * from imob_vbunuricapital where id_tip_operatie in (1,2) and id_mf = ?poDetaliiMf.id_mf]
	lnSucces = goExecutor.oExecute(lcSql, [crsNrbuncapital])
	If lnSucces < 0
		aMessagebox(goExecutor.cEroare, 0 + 16, _screen.Caption)
		Return
	Endif
	Select crsNrbuncapital
	If Reccount() > 0
		llBunCapital = .T.
	Endif
	Use In crsNrbuncapital

	= update_cote_TVA()

	ofrmmajorare = Createobject('frm_op_majorare', tnTip, llBunCapital)
	ofrmmajorare.Show(1)

	Release ofrmmajorare

	m.llSucces = .F.
	If gnButon = 1
		lcSql = [begin pack_imob.inreg_majorare(?gcS,?poDetaliiMf.id_sucursala,?poDetaliiMf.id_mf,] + ;
			[?pnvaloaremaj,?pddatareeval,?pcExplicatie,?gnIdUtil,?pnFiscala,?pdDatadoc,?pnNrDoc,?pnBunCapital ,?pnBazaTva,?pnIdCotaTva,?pnTva,?pnProrataTva ); end;]
		llSucces = goExecutor.oExecuta(lcSql)
		If !m.llSucces
			aMessagebox('Eroare la inregistrarea majorarii! ' + CRLF + goExecutor.cEroare, 0 + 16, _screen.Caption)
		ENDIF
	ENDIF

	loReturn = CREATEOBJECT("empty")
	ADDPROPERTY(loReturn, "lSucces", m.llSucces)
	ADDPROPERTY(loReturn, "valoaremaj", m.pnvaloaremaj)
	ADDPROPERTY(loReturn, "datareeval", m.pddatareeval)
	ADDPROPERTY(loReturn, "Explicatie", m.pcExplicatie)
	ADDPROPERTY(loReturn, "Fiscala", m.pnFiscala)
	ADDPROPERTY(loReturn, "Datadoc", m.pdDatadoc)
	ADDPROPERTY(loReturn, "NrDoc", m.pnNrDoc)
	ADDPROPERTY(loReturn, "BunCapital", m.pnBunCapital )								
	ADDPROPERTY(loReturn, "BazaTva", m.pnBazaTva)	
	ADDPROPERTY(loReturn, "IdCotaTva", m.pnIdCotaTva)	
	ADDPROPERTY(loReturn, "Tva", m.pnTva)	
	ADDPROPERTY(loReturn, "ProrataTva", m.pnProrataTva)				

	RETURN loReturn
Endproc && op_majorare
*************************************************************************************************************
Procedure op_reevaluare
	Parameters tnTip
	Private pnvaloarenoua, pddatareeval, pnamortnoua, pnNrDoc, pdDataDoc, pnBunCapital, pnValreev
	Local llBunCapital
	Store '' To ofrmreeval
	pnvaloarenoua = poDetaliiMf.valoare
	pnamortnoua = poDetaliiMf.amort_prec + poDetaliiMf.rata
	pddatareeval = {}
	pcExplicatie = []
	pnFiscala = 0
	pnNrDoc = 0
	pdDataDoc = {}
	pnBunCapital = 0
	pnValreev = 0

	lcSql = [select * from imob_vbunuricapital where id_tip_operatie in (1,2) and id_mf = ?poDetaliiMf.id_mf]
	lnSucces = goExecutor.oExecute(lcSql, [crsNrbuncapital])
	If lnSucces < 0
		aMessagebox(goExecutor.cEroare, 0 + 16, "ROA IMOB")
		Return
	Endif
	Select crsNrbuncapital
	If Reccount() > 0
		llBunCapital = .T.
	Endif
	Use In crsNrbuncapital


	ofrmreeval = Createobject('frm_op_reevaluare', tnTip, llBunCapital )
	ofrmreeval.Show(1)

	*!*	WAIT WINDOW STR(pnFiscala)
	Release ofrmreeval
	If pnButon = 1
		lcSql = [begin pack_imob.inreg_reevaluare(?gcS,?poDetaliiMf.id_sucursala,?poDetaliiMf.id_mf,] + ;
			[?pnvaloarenoua,?pnamortnoua,?pddatareeval,?pcExplicatie,?gnIdUtil,?pnFiscala,?pdDataDoc,?pnNrDoc,?pnBunCapital,?pnValreev ); end;]
		lnSucces = goExecutor.oExecute(lcSql)
		If lnSucces < 0
			aMessagebox('Eroare la inregistrarea reevaluarii! ' + CRLF + goExecutor.cEroare, 0 + 16, "ROA IMOB")
			Return
		Endif
	Endif

Endproc && op_reevaluare

*************************************************************************************************************
Procedure op_schimbaredns_reevaluare
	Parameters tnTip
	Local ofrmreeval, lcSql, lnSucces, llBunCapital
	Private pnvaloarenoua, pddatareeval, pnAmortizareNoua, pnNrDoc, pdDataDoc, pnBunCapital, pnValreev
	PRIVATE pcExplicatie, pnDNSNou, pnDurataRamasaNoua, pnFiscala, pnUzuraNoua, pcCodNou
	private pnValoareRamasaNoua, pnValoareRezidualaNoua, pnValoareRamasa, pnAmortizare
	Store '' To ofrmreeval
	pnValoareNoua = poDetaliiMf.valoare
	pnAmortizare = poDetaliiMf.amort_prec + poDetaliiMf.rata
	pnAmortizareNoua = m.pnAmortizare
	pnValoareRamasa = poDetaliiMf.valoare - poDetaliiMf.amort_prec - poDetaliiMf.rata - poDetaliiMf.valoare_reziduala
	pnValoareRamasaNoua = m.pnValoareRamasa
	pnValoareRezidualaNoua = poDetaliiMf.valoare_reziduala
	pnDNSNou = poDetaliiMf.dns_luni
	pnUzuraNoua = poDetaliiMf.uzura
	pnDurataRamasaNoua = poDetaliiMf.durata_ramasa
	pcCodNou = poDetaliiMf.cod_mf

	pcExplicatie = []
	pnFiscala = 0
	pnNrDoc = 0
	pnBunCapital = 0
	pnValReev = 0

	pdDatadoc = get_ultima_zi()
	pddatareeval = m.pdDataDoc
	
	lcSql = [select * from imob_vbunuricapital where id_tip_operatie in (1,2) and id_mf = ?poDetaliiMf.id_mf]
	lnSucces = goExecutor.oExecute(lcSql, [crsNrbuncapital])
	If lnSucces < 0
		aMessagebox(goExecutor.cEroare, 0 + 16, "ROA IMOB")
		Return
	Endif
	Select crsNrbuncapital
	If Reccount() > 0
		llBunCapital = .T.
	Endif
	Use In crsNrbuncapital


	ofrmreeval = Createobject('frm_op_schimbdns_reevaluare', m.tnTip, m.llBunCapital)
	ofrmreeval.Show(1)

	*!*	WAIT WINDOW STR(pnFiscala)
	Release ofrmreeval
	If pnButon = 1
		lcSql = [begin pack_imob.inreg_schimbdns_reevaluare(?gcS,?poDetaliiMf.id_sucursala,?poDetaliiMf.id_mf,] + ;
			[?pcCodNou,?pnDnsNou,?pnValoareNoua,?pnAmortizareNoua,?pnUzuraNoua,?pnValoareRezidualaNoua,] + ;
			[?pdDataReeval,?pcExplicatie,?gnIdUtil,?pnFiscala,?pdDataDoc,?pnNrDoc,?pnBunCapital,?pnValreev ); end;]

		lnSucces = goExecutor.oExecute(lcSql)
		If lnSucces < 0
			aMessagebox('Eroare la inregistrarea schimbare DNS si reevaluare! ' + CRLF + goExecutor.cEroare, 0 + 16, _Screen.Caption)
			Return
		Endif
	Endif

Endproc && op_schimbaredns_reevaluare

*************************************************************************************************************
Procedure op_conservare
	Parameters tnTip, tlConservat
	Private pddataconservare, pccauza, pnNrDoc, pdDataDoc, pnBunCapital, pnBazaTva, pnId_cota_tva, pnTva, pnProrataTva
	Local llBunCapital
	Store '' To ofrmgest
	pddataconservare = {}
	pccauza = []
	pnFiscala = 0
	pnNrDoc = 0
	pdDataDoc = {}

	pnBunCapital = 0
	pnBazaTva = 0
	pnId_cota_tva = 0
	pnTva = 0
	pnProrataTva  = 0

	lcSql = [select * from imob_vbunuricapital where id_tip_operatie in (1,2) and id_mf = ?poDetaliiMf.id_mf]
	lnSucces = goExecutor.oExecute(lcSql, [crsNrbuncapital])
	If lnSucces < 0
		aMessagebox(goExecutor.cEroare, 0 + 16, "ROA IMOB")
		Return
	Endif
	Select crsNrbuncapital
	If Reccount() > 0
		llBunCapital = .T.
	Endif
	Use In crsNrbuncapital


	= update_cote_TVA()

	ofrmgest = Createobject('frm_op_conservare', tlConservat, tnTip, llBunCapital)
	ofrmgest.Show(1)

	Release ofrmgest
	If pnButon = 1
		lcSql = [begin pack_imob.inreg_conservare(?gcS,?poDetaliiMf.id_sucursala,?poDetaliiMf.id_mf,] + Iif(!tlConservat, '1', '0') + [,] + ;
			[?pccauza,?pddataconservare,?gnIdUtil,?pnFiscala,?pdDataDoc,?pnNrDoc,?pnBunCapital,?pnBazaTva,?pnId_cota_tva,?pnTva,?pnProrataTva); end;]
		lnSucces = goExecutor.oExecute(lcSql)
		If lnSucces < 0
			aMessagebox('Eroare la inregistrarea ' + Iif(!tlConservat, 'intrarii in conservare', 'iesirii din conservare') + ;
				  '!' + CRLF + goExecutor.cEroare, 0 + 16, "ROA IMOB")
			Return
		Endif
	Endif


Endproc && op_conservare
*************************************************************************************************************
Procedure op_schimbaredns
	Parameters tnTip
	Private pddataschimbare, pndns, pccodnou, pnNrDoc, pdDataDoc
	Store '' To ofrmschdns
	pddataschimbare = {}
	pndns = 0
	pccodnou = []
	pnUzura = 0
	pcExplicatie = []
	pnFiscala = 0
	pnNrDoc = 0
	pdDataDoc = {}

	ofrmschdns = Createobject('frm_op_schimb_dns')
	ofrmschdns.Show(1)


	Release ofrmschdns
	If pnButon = 1
		lcSql = [begin pack_imob.inreg_schimbdns(?gcS,?poDetaliiMf.id_sucursala,?poDetaliiMf.id_mf,?pccodnou,] + ;
			[?pndns,?pddataschimbare,?pcExplicatie,?gnIdUtil,?pnFiscala,?pdDataDoc,?pnNrDoc); end;]
		lnSucces = goExecutor.oExecute(lcSql)
		If lnSucces < 0
			aMessagebox('Eroare la inregistrarea schimbarii DNS-ului !' + CRLF + goExecutor.cEroare, 0 + 16, "ROA IMOB")
			Return
		Endif
	Endif

Endproc	&& op_schimbaredns
*************************************************************************************************************
Procedure op_gest
	Parameters tnTip, tlCasat
	Private pddataschimbare, pccauza, pnNrDoc, pdDataDoc, pnProrataTva, pnIdTipIesire
	Local llBunCapital
	Store '' To ofrmgest
	pddataschimbare = {}
	pccauza = []
	pnFiscala = 0
	pnNrDoc = 0
	pdDataDoc = {}

	pnBunCapital = 0
	pnBazaTva = 0
	pnId_cota_tva = 0
	pnTva = 0
	pnProrataTva = 0
	pnIdTipIesire = 1 && vanzare

	lcSql = [select * from imob_vbunuricapital where id_tip_operatie in (1,2) and id_mf = ?poDetaliiMf.id_mf]
	lnSucces = goExecutor.oExecute(lcSql, [crsNrbuncapital])
	If lnSucces < 0
		aMessagebox(goExecutor.cEroare, 0 + 16, "ROA IMOB")
		Return
	Endif
	Select crsNrbuncapital
	If Reccount() > 0
		llBunCapital = .T.
	Endif
	Use In crsNrbuncapital

	= update_cote_TVA()

	lcSql = [select id, tip from imob_tipuri_iesire order by ordine]
	llSucces = goExecutor.oExecuta(m.lcSql, [crsTipuriIesire])
	If !m.llSucces
		Create Cursor crsTipuriIesire(Id I, tip C(100))
		Insert Into crsTipuriIesire(Id, tip) Values (1, 'VANZARE')
	Endif
	Go Top In crsTipuriIesire

	ofrmgest = Createobject('frm_op_gest', tlCasat, tnTip, llBunCapital )
	ofrmgest.Show(1)

	Release ofrmgest
	Use In (Select('crsTipIesire'))

	If tlCasat && la intrarea in gestiune nu mai am "tip iesire din gestiune"
		pnIdTipIesire = Null
	Endif
	If pnButon = 1
		*!* am pus valori pentru ca dadea la plevnei C00005
		*!* MARIUS.MUTU
		*!* 21.11.2006
		*!*	lcsql=[ begin pack_imob.inreg_gest(?gcs,?poDetaliiMf.id_mf, ] + Iif(!tlCasat, '1', '0') + [, ?pccauza, ?pddataschimbare, ?gnIdUtil, ?pnFiscala); end;]
		lcSql = [ begin pack_imob.inreg_gest('] + gcs + [',] + Iif(Nvl(poDetaliiMf.id_sucursala, 0) = 0, 'NULL', Alltrim(Str(poDetaliiMf.id_sucursala))) + [, ] + Alltrim(Str(poDetaliiMf.id_mf)) + [, ] + Iif(!tlCasat, '1', '0') + [, '] + pccauza + ;
			[' , TO_DATE('] + Dtos(pddataschimbare) + [', 'YYYYMMDD') , ] + ;
			Alltrim(Str(gnIdUtil)) + [, ] + Alltrim(Str(pnFiscala)) + [, TO_DATE('] + Dtos(pdDataDoc) + [', 'YYYYMMDD') , ] + Alltrim(Str(pnNrDoc)) + [,] + ;
			Alltrim(Str(pnBunCapital )) + [,] + Alltrim(Transform(pnBazaTva)) + [,] + Alltrim(Transform(pnId_cota_tva)) + [,] + Alltrim(Transform(pnTva )) + [,] + Alltrim(Transform(pnProrataTva )) + [,] + Iif(Isnull(m.pnIdTipIesire), 'NULL', Alltrim(Transform(m.pnIdTipIesire))) + [); end;]
		*STRTOFILE(lcsql,'c:\inreg_gest.txt')

		lnSucces = goExecutor.oExecute(lcSql)
		If lnSucces < 0
			aMessagebox('Eroare la inregistrarea ' + Iif(!tlCasat, 'iesirii din gestiune', 'intrarii in gestiune') + ;
				  '!' + CRLF + goExecutor.cEroare, 0 + 16, "ROA IMOB")
			Return
		Endif
	Endif

Endproc	&&	op_iesiregest
*************************************************************************************************************
Procedure op_modificare
	Parameters tnTip
	Store '' To ofrmmodificare

	= cctipuri_amort()
	= update_cote_TVA()	&&	modificare v 2.0.18
	ofrmmodificare = Createobject('frm_introducere', tnTip, 2)
	ofrmmodificare.Show(1)

	If Used('v_tipuri_amort')
		Use In v_tipuri_amort
	Endif
	*!*	modificare	v 2.0.18
	If Used('cote_TVA')
		Use In cote_TVA
	Endif
	*!*	modificare	v 2.0.18	^
	Release ofrmmodificare

Endproc	&& op_modificare
************************************************************************************************************
Procedure op_transformare_mf
	Parameters tnTip
	******************************
	** tnTip:
	** 1) imob.corporale
	** 2) imob.necorporale
	Local lnTip
	Private ofrmintro
	= cctipuri_amort()
	= update_cote_TVA()

	Local lnRezultat, lnIdTipDocNrInv
	Private poGeneratorNumere
	lnIdTipDocNrInv = 18
	poGeneratorNumere = Createobject('oGeneratorNumere')
	lnRezultat = poGeneratorNumere.creeaza_cursor_serii(m.lnIdTipDocNrInv)

	lnTip = Iif(!Empty(m.tnTip), m.tnTip, 1)

	gnButon = 2
	ofrmintro = Createobject('frm_introducere', m.lnTip, 4)
	ofrmintro.clb_valoare.Enabled = .F.
	ofrmintro.Show(1)
	Release ofrmintro

	If gnButon = 2
		poGeneratorNumere.dezaloca_numere()
	Endif

	Use In (Select('v_tipuri_amort'))
	Use In (Select('cote_TVA'))
	Return (gnButon = 1)

Endproc && op_transformare_mf

*************************************************************************************************************
*** adaugare valoare imobilizare in curs la o imobilizare corporala/necorporala deja existenta, prin operatia majorare valoare
*************************************
Procedure op_adaugare_la_mf
	Parameters tnTip
	******************************
	** tnTip:
	** 1) imob.corporale
	** 2) imob.necorporale
		
	Private pdDataI, poDetaliiMf, poMF, pnIdMf
	Local lcSql, llSucces, lnFiscala, lnValoareMajorare, ldDataReevaluare
	Local llBlank, llReturn, llSucces2, lnIdMf, loImobBlank, loRec

	ldDataReevaluare = {}
	
	**************************************
	* Aleg imobilizarea pe care doresc sa o majorez
	**************************************

	CALCULATE SUM(valoareretinuta) TO lnValoareMajorare IN crsLista3
	
	poDetaliiMf = caut_imobilizare(m.tnTip)
	pnIdMf = poDetaliiMf.id_mf


	llSucces = .T.
	llSucces2 = .T.
	llSucces = goConn.BeginManualTransaction()

	loMajorare = op_majorare(m.tnTip, m.lnValoareMajorare)
	llSucces = loMajorare.lSucces

	IF m.llSucces
		=cctipuri_amort()

		* ADAUG OPERATIE TRANSFORMARE MIJLOC FIX PE FIECARE IMOBILIZARE IN CURS
		TEXT TO lcSql NOSHOW 
begin
  pack_imob.inreg_transformare_in_mf(v_schema => ?gcS,
       v_id_mf => ?poMF.id_mf,
       v_id_mf_nou => ?pnIdMf,
       v_datamodificare => ?poMF.datamodificare,
       v_nract => ?poMF.nract,
       v_dataact => ?poMF.dataact,
       v_denumire => ?poMF.denumire,
       v_explicatia => ?poMF.explicatia,
       v_valoare => ?poMF.valoare,
       v_data_pif => ?poMF.data_pif,
       v_nr_pif => ?poMF.nr_pif,
       v_nr_inventar => ?poMF.nr_inventar,
       v_cod => ?poMF.cod,
       v_dns => ?poMF.dns,
       v_uzura => ?poMF.uzura,
       v_dns_ramas => ?poMF.dns_ramas,
       v_valoare_ramasa => ?poMF.valoare_ramasa,
       v_valoare_retinuta => ?poMF.valoare_retinuta,
       v_id_gestiune => ?poMF.id_gestiune,
       v_id_responsabil => ?poMF.id_responsabil,
       v_id_sectie => ?poMF.id_sectie,
       v_id_lucrare => ?poMF.id_lucrare,
       v_id_sucursala => ?poMF.id_sucursala,
       v_id_tip_amortizare => ?poMF.id_tip_amortizare,
       v_procent => ?poMF.procent,
       v_coeficient => ?poMF.coeficient,
       v_id_tip_imobilizare => ?poMF.id_tip_imobilizare,
       v_cont => ?poMF.cont,
       v_acont => ?poMF.acont,
       v_id_util => ?gnIdUtil,
       v_amort_max_deductibil => ?poMF.amort_max_deductibil,
       v_fiscala => ?poMF.fiscala,
       v_datadoc => ?poMF.datadoc,
       v_nrdoc => ?poMF.nrdoc,
       v_id_tip_operatie => ?poMF.id_tip_operatie);
end;		
		ENDTEXT 

			
		Select crslista3
		SCAN
			SCATTER NAME poMF MEMO 
			
			poMF.valoare_ramasa = poMF.valoare_ramasa - poMF.valoareretinuta
			ADDPROPERTY(poMF, "dataact", poMf.data_achizitie)
			ADDPROPERTY(poMF, "valoare_retinuta", poMF.valoareretinuta)
			ADDPROPERTY(poMF, "datamodificare", loMajorare.datareeval)
			ADDPROPERTY(poMF, "datadoc", loMajorare.datadoc) 						
			ADDPROPERTY(poMF, "nrdoc", loMajorare.nrdoc)			
			ADDPROPERTY(poMF, "data_pif", poMf.data_achizitie)
			ADDPROPERTY(poMF, "nr_pif", poMf.nr_inventar)		
			ADDPROPERTY(poMF, "id_tip_amortizare", 1) && liniar
			ADDPROPERTY(poMF, "id_tip_imobilizare", 3) && in curs
			ADDPROPERTY(poMF, "fiscala", 0) 
			ADDPROPERTY(poMF, "cod", NULL) 
			ADDPROPERTY(poMF, "dns", 0) 
			ADDPROPERTY(poMF, "uzura", 0) 
			ADDPROPERTY(poMF, "dns_ramas", 0) 	
			ADDPROPERTY(poMF, "procent", 0) 
			ADDPROPERTY(poMF, "coeficient", 0) 
			ADDPROPERTY(poMF, "amort_max_deductibil", 0) 
			ADDPROPERTY(poMF, "id_tip_operatie", 3) && transformare in imobilizare
			WITH poMF
				.explicatia = ALLTRIM(.explicatia)
			ENDWITH 
      					
			llSucces = goExecutor.oExecuta(lcSql)
			If !m.llSucces
				Exit
			Endif

			Select crslista3
		Endscan

		Use In (SELECT('v_tipuri_amort'))
	ENDIF && llSucces

	USE IN (SELECT('cImobBlank'))

	llSucces2 = goConn.EndManualTransaction(Iif(m.llSucces, 'COMMIT', 'ROLLBACK'))

	llReturn = m.llSucces And m.llSucces2
	
	RETURN m.llReturn

Endproc && op_transformare_mf
*!*	*************************************************************************************************************
 
 Define class oImobFrm as form
	width=650
	height=480
	showWindow=2
	Autocenter=.T.
	caption="Alegeti imobilizarea"
	name="form1"

	Add object grid1 as grid with ;
		Anchor=15,;
		left=5,;
		top=100,;
		width=600,;
		height=300,;
		gridlines=0,;
		deletemark=.t.,;
		name="grid1"

	procedure grid1.init
		with this
			.recordsource="crsListaTemp"
			.recordsourcetype=1
		endwith
	endproc	
ENDDEFINE  