*!* 21.09.2010
*!* marius.mutu
*!*	#2722 Fruvimed
*!* inchidere_k am folosit lnCoeficientTva si GetProcTvaStandard() in loc de 19/119

*!* 11.10.2017
*!* marius.mutu
*!* inchidere_k_custodie - tratare optiuni tip nou pentru analitice 371,378,4428

* inchidere cu K
Procedure inchidere_k

Private pnK, pcAcont371, pcAcont378, pcAcont4111, pcAcont4428
Store 0 To pnK
Store '' To pcAcont371, pcAcont378, pcAcont4111, pcAcont4428

Local lnK, lcCursor, llStergeNotaPrecedenta, lnId_Set, lnCod, llVerificAnalitic, llCompletareParteneri, lnRaspuns
Store 0 To lnK, lnCod, lnRaspuns
Store .T. To llVerificAnalitic, llCompletareParteneri

Local lnAnalitice371, lnAnalitice378, lnAnalitice4111, lnAnalitice4428
Store 0 To lnAnalitice371, lnAnalitice378, lnAnalitice4111, lnAnalitice4428

llStergeNotaPrecedenta = .T.
lnId_Set = 90017
lnCod = 0

*!* 21.09.2010
lnCoeficientTva = GetProcTvaStandard() / (100 + GetProcTvaStandard()) && 24/124
*!* 21.09.2010 ^

***---------------------------------------------------
*** 371
lcSql=[select count(*) as cnt from balana where an = ?gnAn and luna = ?gnLuna and cont = '371' ]
lcCursor = 'crs_acont371'
lnSucces = goExecutor.oExecute(lcSql,lcCursor)
If lnSucces < 0
	AMESSAGEBOX(goExecutor.cEroare,0+16,"Eroare")
Endif

Select crs_acont371
lnAnalitice371 = Cnt

If  lnAnalitice371 > 0
	loCauta = caut_acont('371')
	pcAcont371 = loCauta.acont
Endif

If Used('crs_acont371')
	Use In crs_acont371
Endif

*** 378
lcSql=[select count(*) as cnt from balana where an = ?gnAn and luna = ?gnLuna and cont = '378' ]
lcCursor = 'crs_acont378'
lnSucces = goExecutor.oExecute(lcSql,lcCursor)
If lnSucces < 0
	AMESSAGEBOX(goExecutor.cEroare,0+16,"Eroare")
Endif

Select crs_acont378
lnAnalitice378 = Cnt

If  lnAnalitice378 > 0
	loCauta = caut_acont('378')
	pcAcont378 = loCauta.acont
Endif

If Used('crs_acont378')
	Use In crs_acont378
Endif

*** 4111
lcSql=[select count(*) as cnt from balana where an = ?gnAn and luna = ?gnLuna and cont = '4111' ]
lcCursor = 'crs_acont4111'
lnSucces = goExecutor.oExecute(lcSql,lcCursor)
If lnSucces < 0
	AMESSAGEBOX(goExecutor.cEroare,0+16,"Eroare")
Endif

Select crs_acont4111
lnAnalitice4111 = Cnt

If  lnAnalitice4111 > 0
	loCauta = caut_acont('4111')
	pcAcont4111 = loCauta.acont
Endif

If Used('crs_acont4111')
	Use In crs_acont4111
Endif

*** 4428
lcSql=[select count(*) as cnt from balana where an = ?gnAn and luna = ?gnLuna and cont = '4428' ]
lcCursor = 'crs_acont4428'
lnSucces = goExecutor.oExecute(lcSql,lcCursor)
If lnSucces < 0
	AMESSAGEBOX(goExecutor.cEroare,0+16,"Eroare")
Endif

Select crs_acont4428
lnAnalitice4428 = Cnt

If  lnAnalitice4428 > 0
	loCauta = caut_acont('4428')
	pcAcont4428 = loCauta.acont
Endif

If Used('crs_acont4428')
	Use In crs_acont4428
Endif
***---------------------------------------------------

If llStergeNotaPrecedenta
	lnSucces = sterge_nota(lnCod, lnId_Set)
	If lnSucces < 0
		Return
	Endif
Endif

* lcSel = [{call pack_inchideri.K_transferuri(?gnAn, ?gnLuna)}]
lcSel = [{call pack_inchideri.K_transferuri(]+Alltrim(Str(gnAn))+[,]+Alltrim(Str(gnLuna))+ [)}]
lcCursor = 'crsTransferuri'
lnSucces = goExecutor.oExecute(lcSel, lcCursor)
If lnSucces < 0
	AMESSAGEBOX(goExecutor.cEroare,0+16,"Eroare")
	Return
Endif

Select Count(*) As Cnt From crsTransferuri Into Cursor cNrTransf
Select cNrTransf
lnTransferuri = Cnt
If Used('cNrTransf')
	Use In cNrTransf
Endif
If lnTransferuri > 0
	lnRaspuns = AMESSAGEBOX('Doriti sa faceti nota de reglare transferuri?',4, 'Reglare transferuri')

	If lnRaspuns = 6
***---------------------------------------
* lcSel = [{call pack_inchideri.K_general(?gnAn, ?gnLuna, ?pcAcont371, ?pcAcont378, ?pcAcont4428)}]
		lcSel = [{call pack_inchideri.K_general(]+Alltrim(Str(gnAn))+[,]+Alltrim(Str(gnLuna))+[,]+;
			IIF(!Empty(pcAcont371),['] + Alltrim(pcAcont371) + ['],'NULL')+[,]+ ;
			IIF(!Empty(pcAcont378),['] + Alltrim(pcAcont378) + ['],'NULL')+[,]+;
			IIF(!Empty(pcAcont4428),['] + Alltrim(pcAcont4428) + ['],'NULL')+[)}]
		lcCursor = 'crsK'
		lnSucces = goExecutor.oExecute(lcSel, lcCursor)
		If lnSucces < 0
			AMESSAGEBOX(goExecutor.cEroare,0+16,"Eroare")
			Return
		Endif

		Select crsK
		pnK = Round(Nvl(k, 0),gnPC)

***---------------------------------------

		Create Cursor crsCorecturi (id_partd N(10), partd C(70), id_partc N(10), partc C(70), scd C(4), scc C(4), suma N(20,4))

		Select crsTransferuri
		Scan
			lnId_partd = id_partd
			lcPartd = partd
			lnId_partc = id_partc
			lcPartc = partc
			lnsuma = suma
			
			*!* 21.09.2010
	
			Insert Into crsCorecturi (id_partd, partd, id_partc, partc, scd, scc, suma) ;
				VALUES (lnId_partc, lcPartc, lnId_partd, lcPartd, '4428', '4428', Round(lnsuma * m.lnCoeficientTva,gnPC))
			lnTva = ROUND(lnsuma * m.lnCoeficientTva, gnPc)
			*!* 21.09.2010 ^
*!*				Insert Into crsCorecturi (id_partd, partd, id_partc, partc, scd, scc, suma) ;
*!*					VALUES (lnId_partc, lcPartc, lnId_partd, lcPartd, '378', '378', ROUND((lnsuma-lnTva)*pnK/100,gnPC))
			Insert Into crsCorecturi (id_partd, partd, id_partc, partc, scd, scc, suma) ;
				VALUES (lnId_partc, lcPartc, lnId_partd, lcPartd, '378', '378', Round((lnsuma-lnTva)*pnK/(100+pnK),gnPC))
			Select crsTransferuri
		Endscan

		If Used('crsTransferuri')
			Use In crsTransferuri
		Endif

***---------------------------------------

		llVerificAnalitic = .T.
		llCompletareParteneri = .T.
		lcCursor = 'crsCorecturi'

		If lnSucces > 0
			lnSucces = scrie_nota_import(lnId_Set, llVerificAnalitic, llCompletareParteneri, lcCursor)
		Endif
	Endif
Endif

***---------------------------------------
***---------------------------------- calcul K magazine

***------------

* lcSel = [{call pack_inchideri.K_parteneri(?gnAn, ?gnLuna, ?pcAcont371, ?pcAcont378, ?pcAcont4111, ?pcAcont4428)}]
lcSel = [{call pack_inchideri.K_parteneri(]+Alltrim(Str(gnAn))+[,]+Alltrim(Str(gnLuna))+[,]+ ;
	IIF(!Empty(pcAcont371),['] + Alltrim(pcAcont371) + ['],'NULL')+[,]+ ;
	IIF(!Empty(pcAcont378),['] + Alltrim(pcAcont378) + ['],'NULL')+[,]+;
	IIF(!Empty(pcAcont4111),['] + Alltrim(pcAcont4111) + ['],'NULL')+[,]+ ;
	IIF(!Empty(pcAcont4428),['] + Alltrim(pcAcont4428) + ['],'NULL')+[)}]
lcCursor = 'crsK_mag'
lnSucces = goExecutor.oExecute(lcSel,lcCursor)
If lnSucces < 0
	AMESSAGEBOX(goExecutor.cEroare,0+16,"Eroare")
	Return
Endif
***---------------------------------- calcul K magazine

Create Cursor crsInchKmag (id_partd N(10), partd C(70), id_partc N(10), partc C(70), scd C(4), scc C(4), ascc C(4), suma N(20,4))

Select crsK_mag
Scan For rc4111 + rd4111 <> 0
	lnId_part = id_part
	lcPartener = Alltrim(denumire)
	lnK = Round(k/100,gnPC)
	ln4111 = Round(rc4111+rd4111 ,gnPC)

	lnTva = Round(ln4111 * m.lnCoeficientTva,gnPC)
	*lnAD = Round((ln4111-lnTva)*lnK,gnPC)
	*!* 09.09.2008 
	*!* MARIUS MUTU - TREBUIE SESIZARE DE LA FRUVIMED
*!*		lnAD = Round((ln4111-lnTva)*k/(100+k),gnPC)
	lnAD = Round((ln4111-lnTva)*k/100,gnPC)
	*!* 09.09.2008 ^
	Insert Into crsInchKmag (id_partd, partd, id_partc, partc, scd, scc, suma) ;
		VALUES (lnId_part, lcPartener,lnId_part, lcPartener,'4428', '371', lnTva)
	Insert Into crsInchKmag (id_partd, partd, id_partc, partc, scd, scc, suma) ;
		VALUES (lnId_part, lcPartener,lnId_part, lcPartener,'378', '371', lnAD)
	Insert Into crsInchKmag (id_partd, partd, id_partc, partc, scd, scc, suma) ;
		VALUES (0, '',lnId_part, lcPartener,'607', '371', ln4111-lnTva-lnAD)

	Select crsK_mag
Endscan

***-------------------------------
llStergeNotaPrecedenta = .T.
lnId_Set = 90018
lnCod = 0
llVerificAnalitic = .T.
llCompletareParteneri = .T.
lcCursor = 'crsInchKmag'

If llStergeNotaPrecedenta
	lnSucces = sterge_nota(lnCod, lnId_Set)
Endif

If lnSucces > 0
	Select crsInchKmag
	Replace All ascc With pcAcont371
	lnSucces = scrie_nota_import(lnId_Set, llVerificAnalitic, llCompletareParteneri, lcCursor)
Endif

If lnSucces > 0
	Private pcTitlu, pcDataOra
	pcTitlu = "Inchidere folosind metoda cu k"
	pcDataOra = get_ora(2)

	Select km.*, 1 As tip, ;
		0000000000 As id_partd, Space(70) As partd, ;
		0000000000 As id_partc, Space(70) As partc, ;
		SPACE(4) As scd, ;
		SPACE(4) As scc, ;
		SPACE(4) As ascc, ;
		0000000000000000000000.00 As suma ;
		From crsK_mag km Into Cursor crsList Readwrite


	If Used('crsCorecturi')
		Select C.*, 2 As tip From crsCorecturi C Into Cursor c2
		Select crsList
		Append From Dbf('c2')
		If Used('c2')
			Use In c2
		Endif

		Use In crsCorecturi
	Endif

	Select C.*, 3 As tip From crsInchKmag C Into Cursor c3

	If Used('crsInchKmag')
		Use In crsInchKmag
	Endif

	Select crsList
	Append From Dbf('c3')
	If Used('c3')
		Use In c3
	Endif

	Select crsList
	Report Form rap_inchidere_k.frx To Printer Prompt Preview

* Select * From crsList Into Table C:\List.Dbf

	If Used('crsList')
		Use In crsList
	Endif
Endif

If Used('crsK_mag')
	Use In crsK_mag
Endif

Endproc && inchidere_k
************************************************************************************************************************
*!*	modificare	v 2.2.35
Procedure inchidere_k_custodie
	Local lcCursor, lcCursorFinal, llStergeNotaPrecedenta, lnId_Set, lnCod, llVerificAnalitic, llCompletareParteneri
	Store .T. To llVerificAnalitic, llCompletareParteneri, llStergeNotaPrecedenta
	Store 0 To lnCod
	lnId_Set = 90019
	lcCursorFinal = [crsK]
	lcCursor = [crsK_custodie]	
	If llStergeNotaPrecedenta
		lnSucces = sterge_nota(lnCod, lnId_Set)
	Endif
SET STEP ON 
	*************************
	*** Analitice 371,378,4428 versiune noua
	*** FactAnaliticCustK_5 / FactAnaliticCustK_9 / FactAnaliticCustK_19
	*************************
	lcSql = [SELECT procent FROM cote_tva WHERE an = ?gnAn AND luna = ?gnLuna and procent <> 0]
	llSucces = goExecutor.oExecuta(m.lcSql, "cCoteTVATemp")
	IF m.llSucces
		SELECT cCoteTVATemp
		SCAN
			lcProcent = ALLTRIM(STR(INT(procent)))
			lcVariabilaName = 'gcFactAnaliticCustK_' + m.lcProcent && gcFactAnaliticCustK_5 / gcFactAnaliticCustK_9 / gcFactAnaliticCustK_19
			
			If TYPE(m.lcVariabilaName) <> "U"
				lcAnaliticK = &lcVariabilaName
				IF !EMPTY(m.lcAnaliticK)
						lcSel = [{call pack_inchideri.K_custodie(]+Alltrim(Str(gnAn))+[,]+Alltrim(Str(gnLuna))+[,] + m.lcProcent + [)}]
						llSucces = goExecutor.oExecuta(lcSel,lcCursor)
						If m.llSucces
							IF !USED(m.lcCursorFinal)
								SELECT * FROM (lcCursor) WHERE .F. INTO CURSOR (m.lcCursorFinal) READWRITE 
							ENDIF && !USED(m.lcCursorFinal)
							
							Insert Into (m.lcCursorFinal) Select * From (m.lcCursor)
							USE IN (SELECT(m.lcCursor))
						ENDIF && m.llSucces	
				ENDIF && !EMPTY(m.lcAnaliticK)
			ENDIF 
		ENDSCAN 
	ENDIF && m.llSucces 

	*************************
	*** Daca nu am gasit analiticele versiune noua, caut si analitice versiune veche
	IF !USED(m.lcCursorFinal)
		lcSel = [{call pack_inchideri.K_custodie(]+Alltrim(Str(gnAn))+[,]+Alltrim(Str(gnLuna))+[,1)}]
		llSucces = goExecutor.oExecuta(lcSel,lcCursor)
		If llSucces
			copiaza_structura_cursor(lcCursor,lcCursorFinal)
			If !Empty(NVL(IIF(TYPE("gcFactAnaliticCustK2")=="U",NULL,gcFactAnaliticCustK2),""))
				Insert Into (lcCursorFinal) Select * From (lcCursor)
				If Used(lcCursor)
					Use In (lcCursor)
				Endif
				lcSel = [{call pack_inchideri.K_custodie(]+Alltrim(Str(gnAn))+[,]+Alltrim(Str(gnLuna))+[,2)}]
				llSucces = goExecutor.oExecuta(lcSel,lcCursor)
			Endif
			If llSucces
				Insert Into (lcCursorFinal) Select * From (lcCursor)
				lnSucces = scrie_nota_import(lnId_Set, llVerificAnalitic, llCompletareParteneri, lcCursorFinal)
			Endif

		Endif
		If Used(lcCursor)
			Use In (lcCursor)
		ENDIF
	ELSE
		lnSucces = scrie_nota_import(lnId_Set, llVerificAnalitic, llCompletareParteneri, lcCursorFinal)	
	ENDIF && !USED(m.lcCursorFinal)	

	Use In (SELECT(lcCursorFinal))
Endproc && inchidere_k_custodie
*!*	modificare	v 2.1.8	^
************************************************************************************************************************
