
*!* marius.mutu
*!* 12.07.2016
*!* initializeaza_cai: citire cale bon din settings.ini [ecr]calebon, caleroaprint

*!* 07.02.2024
*!* bon2ecr : tratare discount, pret negativ/cantitate negativa =  reducere valorica la casa de marcat

*#INCLUDE COMUN.H
#IFNDEF COMUN_H
	#DEFINE COMUN_H

	#DEFINE LF				CHR(10)
	#DEFINE CR				CHR(10)
	#DEFINE CRLF			CHR(13) + CHR(10)
	#DEFINE CTAB			CHR(9)

	#DEFINE CT_INSUCCES	 	-1
	#DEFINE CT_SUCCES		1
#ENDIF

* #INCLUDE MVC.H
#IFNDEF ROARETAIL_H
	#DEFINE ROARETAIL_H

	#DEFINE RR_TIPCURSOR		1
	#DEFINE RR_TIPARRAY		2
	#DEFINE RR_TIPCOLLECTION	3
#ENDIF

Define Class oControllerEcr As Custom
	cNrDispozitiv	=	[]
	nNumarBon		=	0
	nNumarTichete1	=	0
	nPretTichet1	=	0
	nNumarTichete2	=	0
	nPretTichet2	=	0
	nValoareTichete	=	0
	nValoareCard	=	0
	nValoareReducere=	0
	nProcentReducere= 	0
	nValoareNumerar	=	0
	nValoareTotal	=	0
	cCursor			=	Null
	cNumeFirma		=	Null
	cFirma			=	Null
	cUtilizator		=	Null
	cNumeProgram	=	Null
	cDescriere		=	Null
	cCaleProgramIni	=	[C:\ROAPRINT\ROAPRINT.EXE]
	cCaleFisierIni	=	[C:\ROA.BON]
	cCaleProgram	=	Null
	cCaleFisier		=	Null
	tDataOra		=	{}
	
	cSchemaTip		=	[0;TIP N(2);DENUMIRE C(50);CANTITATE N(10,4);PRET N(10,4);UM C(10);COTATVA N(5,2);DEPARTAMENT N(2);]
**********************************************************************************************************************************
	Procedure Init
		This.cNumeProgram 	= gcNumeProgram
	Endproc
**********************************************************************************************************************************
	Procedure Reset
	With This
		.cNrDispozitiv			=	[]
		.nNumarBon		=	0
		.nNumarTichete1	=	0
		.nPretTichet1	=	0
		.nNumarTichete2	=	0
		.nPretTichet2	=	0
		.nValoareTichete	=	0
		.nValoareCard		=	0
		.nValoareReducere	=	0
		.nProcentReducere	=	0
		.nValoareNumerar	=	0
		.nValoareTotal	=	0
		.cCursor		=	Null
		.cNumeFirma		=	Null
		.tDataOra		=	{}
		.cCaleProgram	=	.cCaleProgramIni
		.cCaleFisier	=	.cCaleFisierIni
		.cFirma			=	Null
		.cUtilizator	=	Null
		.cDescriere		=	Null
	Endwith
	Endproc
**********************************************************************************************************************************
	Procedure initializeaza_util_firma
		Lparameters tcUtilizator,tcNumeFirma,tcFirma,tcDescriere
		This.cUtilizator = tcUtilizator
		This.cNumeFirma = tcNumeFirma
		This.cFirma = tcFirma
		This.cDescriere = tcDescriere
	Endproc
**********************************************************************************************************************************
	Procedure initializeaza_date
	Lparameters toHash
	Local ltDataOra,lnNumarTichete1,lnNumarTichete2
	With This
		lnNumarTichete1 = toHash.getValue("nNumarTichete1")
		lnNumarTichete2 = toHash.getValue("nNumarTichete2")
		ltDataOra = toHash.getValue("tDataOra")

		.cNrDispozitiv = toHash.getValue("cTip")
		.cCursor = toHash.getValue("cCursor")
		.nNumarBon = toHash.getValue("nNumarBon")
		.nValoareNumerar = toHash.getValue("nValoareNumerar")
		.nValoareCard = toHash.getValue("nValoareCard")
		.nProcentReducere = toHash.getValue("nProcentReducere")
		.nValoareReducere = toHash.getValue("nValoareReducere")
		.nValoareTotal  = toHash.getValue("nValoareTotal")
		IF EMPTY(NVL(.cNumeFirma,[]))
			.cNumeFirma = gcS
		ENDIF
		.tDataOra = Iif(Empty(Nvl(ltDataOra,{})),Datetime(),ltDataOra)
		IF EMPTY(NVL(.cUtilizator,[]))
			.cUtilizator	= gcUsernameApp
		ENDIF
		IF EMPTY(NVL(.cFirma,[]))
			.cFirma	= gcFirma
		ENDIF
		If !Empty(Nvl(lnNumarTichete1,0))
			.nNumarTichete1 = lnNumarTichete1
			.nPretTichet1 = toHash.getValue("nPretTichet1")
		Endif
		If !Empty(Nvl(lnNumarTichete2,0))
			.nNumarTichete2 = lnNumarTichete2
			.nPretTichet2 = toHash.getValue("nPretTichet2")
		Endif
		If !Empty(Nvl(lnNumarTichete1,0)) Or !Empty(Nvl(lnNumarTichete2,0))
			.nValoareTichete = toHash.getValue("nValoareTichete")
		Endif

	Endwith
	Endproc
**********************************************************************************************************************************
	Procedure initializeaza_cai
	Local lcBonFile,lcRoaPrint, loEx
	Store "" To lcBonFile,lcRoaPrint
	DO case
		CASE Type('goApp.oSettings') <> 'U'
			lcBonFile = goApp.oSettings.GetValue("calebon")
			lcRoaPrint = goApp.oSettings.GetValue("caleroaprint")		
		CASE TYPE('gcGeneralIniFile') = 'C' AND FILE(m.gcGeneralIniFile)
			TRY 
				lcBonFile = getini(m.gcGeneralIniFile,"ecr","calebon")	
				lcRoaPrint = getini(m.gcGeneralIniFile,"ecr","caleroaprint")				
				
				IF EMPTY(m.lcRoaPrint)
					lcRoaPrint = Addbs(Left(gcAppPath,Rat("\",gcAppPath,2)-1)) + [ROAPRINT\ROAPRINT.EXE]
				ENDIF 
				IF EMPTY(m.lcBonFile) AND DIRECTORY('C:\temp')
					lcBonFile = [c:\temp\roa.bon]
				ENDIF 
			CATCH TO loEx
				lcRoaPrint = Addbs(Left(gcAppPath,Rat("\",gcAppPath,2)-1)) + [ROAPRINT\ROAPRINT.EXE]
			ENDTRY 	
	OTHERWISE 
		lcRoaPrint = Addbs(Left(gcAppPath,Rat("\",gcAppPath,2)-1)) + [ROAPRINT\ROAPRINT.EXE]
	ENDCASE 

	This.cCaleFisier = Iif(Empty(lcBonFile),This.cCaleFisierIni,lcBonFile)
	This.cCaleProgram = Iif(Empty(lcRoaPrint),This.cCaleProgramIni,lcRoaPrint)
	Endproc
**********************************************************************************************************************************
	Procedure imprima
	Lparameters toHash
	Local lcCursor
	lcCursor = toHash.GetValue("cCursor")
	If !Empty(Nvl(lcCursor,[])) And Used(lcCursor)
		With This
			.initializeaza_date(toHash)
			.initializeaza_cai()
			.bon2ecr()
			.Reset()
		Endwith
	Endif
	Endproc
**********************************************************************************************************************************
	Procedure bon2ecr
	Local lcRun, lcCurentDir
	Local lcSelect, lnSucces
	Local loEx As Exception
	Local lcBon, lcDirectoryCaleFisier
	lcBon = ""
	lnSucces = CT_INSUCCES

	lcSelect = Select()
*!*	ROAX	FIRMATEST	1;2;3;	MARIUS A	12:00:00 AM 7/7/2006
*!*	0;TIP N(2);DENUMIRE C(50);CANTITATE N(10,4);PRET N(10,4);UM C(10);COTATVA N(5,2);DEPARTAMENT N(2);

	lcBon = This.cNumeProgram + CTAB + Allt(This.cNumeFirma) + CTAB + Allt(this.cNrDispozitiv) + CTAB + Allt(This.cUtilizator) + CTAB + Ttoc(This.tDataOra) + ;
		CTAB + allt(NVL(This.cFirma,[])) + CTAB + allt(NVL(This.cDescriere,[])) + CTAB + allt(str(NVL(This.nNumarBon,0))) + CRLF
	lcBon = lcBon + This.cSchemaTip + CRLF
*!*	modificare 15.07.2010 : am adaugat coloanele PROCENT si DISCOUNT
*!* (lcCursor) trebuie sa aiba coloanele : DENUMIRE C(100), UM C(10), CANT N(10,4), PRET N(18,4), DEPARTAMENT N(2), PROC_TVAV N(5,2), PROCENT N(5,2), DISCOUNT N(2)
*!*	modificare 15.07.2010
*!*		Select (This.cCursor)
*!*		Scan
*!*		*!* 1;ARDEI GRAS CU LIPTAUER 120 GR;1;0.1;PORTIE;1.19;1;
*!*			
*!*			lcBon = lcBon + [1;] + Alltrim(DENUMIRE) + [;] + Alltrim(Transform(CANT)) + [;] + Alltrim(Transform(Pret)) + [;] + Alltrim(NVL(UM,[])) + [;] + Alltrim(Transform(PROC_TVAV)) + [;] + Alltrim(Transform(Nvl(DEPARTAMENT,1))) + [;] + CRLF
*!*			*!*	modificare 0.9.27 : NVL(um,[])
*!*		Endscan
	lnCampuri = AFIELDS(laColoaneBon,This.cCursor) 
	If lnCampuri = 8
		Select (This.cCursor)
		Scan
			Do Case
				Case discount = 1	&&	discount articol anterior
					lcBon = lcBon + [3;] + Alltrim(DENUMIRE) + [;] + Alltrim(Transform(CANT)) + [;] + Alltrim(Transform(procent)) + [;] + Alltrim(Nvl(UM,[BUC])) + [;] + Alltrim(Transform(PROC_TVAV)) + [;] + Alltrim(Transform(Nvl(DEPARTAMENT,1))) + [;] + CRLF
				Case discount = 2	&&	discount subtotal
					lcBon = lcBon + [4;] + Alltrim(DENUMIRE) + [;] + Alltrim(Transform(CANT)) + [;] + Alltrim(Transform(procent)) + [;] + Alltrim(Nvl(UM,[BUC])) + [;] + Alltrim(Transform(PROC_TVAV)) + [;] + Alltrim(Transform(Nvl(DEPARTAMENT,1))) + [;] + CRLF
				Case cant < 0 AND pret > 0 &&	discount valoric subtotal; cantitatea nu poate fi negativa la casa de marcat. o tratez ca o reducere valorica
					lcBon = lcBon + [41;] + Alltrim(DENUMIRE) + [;] + [1] + [;] + Alltrim(Transform(ABS(cant*pret))) + [;] + Alltrim(Nvl(UM,[BUC])) + [;] + Alltrim(Transform(PROC_TVAV)) + [;] + Alltrim(Transform(Nvl(DEPARTAMENT,1))) + [;] + CRLF				
				Case pret < 0 AND cant > 0 &&	discount valoric subtotal; bon fiscal conform factura, se cumuleaza articolele, iar discount-ul global apare cu cantitate = 1 si sum(pret) negativ
					lcBon = lcBon + [41;] + Alltrim(DENUMIRE) + [;] + [1] + [;] + Alltrim(Transform(ABS(cant*pret))) + [;] + Alltrim(Nvl(UM,[BUC])) + [;] + Alltrim(Transform(PROC_TVAV)) + [;] + Alltrim(Transform(Nvl(DEPARTAMENT,1))) + [;] + CRLF				
				Otherwise
					lcBon = lcBon + [1;] + Alltrim(DENUMIRE) + [;] + Alltrim(Transform(CANT)) + [;] + Alltrim(Transform(Pret)) + [;] + Alltrim(Nvl(UM,[BUC])) + [;] + Alltrim(Transform(PROC_TVAV)) + [;] + Alltrim(Transform(Nvl(DEPARTAMENT,1))) + [;] + CRLF
			Endcase
		Endscan
	Else
		Select (This.cCursor)
		Scan
			*!* 1;ARDEI GRAS CU LIPTAUER 120 GR;1;0.1;PORTIE;1.19;1;

			lcBon = lcBon + [1;] + Alltrim(DENUMIRE) + [;] + Alltrim(Transform(CANT)) + [;] + Alltrim(Transform(Pret)) + [;] + Alltrim(Nvl(UM,[BUC])) + [;] + Alltrim(Transform(PROC_TVAV)) + [;] + Alltrim(Transform(Nvl(DEPARTAMENT,1))) + [;] + CRLF
			*!*	modificare 0.9.27 : NVL(um,[])
		Endscan
	Endif
	*!*	modificare 15.07.2010	^
*!*	2;NUMERAR;0;0.5;
*!*	2;PROCENTDISCOUNT;0;10.50;

*!*	modificare	ROARESTAURANT v 0.9.30
*!*		If This.nProcentReducere > 0
*!*			lcBon = lcBon + [2;PROCENTDISCOUNT;0;] + ALLTRIM(TRANSFORM(This.nProcentReducere)) + [;] + CRLF
*!*	*!*			lcBon = lcBon + [2;REDUCERE;0;] + Alltrim(Transform(This.nValoareReducere)) + [;] + CRLF
*!*		Endif
	IF This.nProcentReducere > 0
		lcBon = lcBon + [2;DISCOUNT;0;] + ALLTRIM(TRANSFORM(This.nValoareReducere)) + [;] + CRLF
		lcBon = lcBon + [2;PROCENTDISCOUNT;0;] + ALLTRIM(TRANSFORM(This.nProcentReducere)) + [;] + CRLF
	*!*			lcBon = lcBon + [2;REDUCERE;0;] + Alltrim(Transform(This.nValoareReducere)) + [;] + CRLF
	ENDIF
*!*	modificare	ROARESTAURANT v 0.9.30	^

	If This.nNumarTichete1 > 0
		lcBon = lcBon + [2;NUMARTICHETE1;0;] + Alltrim(Transform(This.nNumarTichete1)) + [;] + CRLF
		lcBon = lcBon + [2;PRETTICHET1;0;] + Alltrim(Transform(This.nPretTichet1)) + [;] + CRLF
	Endif
	If This.nNumarTichete2 > 0
		lcBon = lcBon + [2;NUMARTICHETE2;0;] + Alltrim(Transform(This.nNumarTichete2)) + [;] + CRLF
		lcBon = lcBon + [2;PRETTICHET2;0;] + Alltrim(Transform(This.nPretTichet2)) + [;] + CRLF
	Endif

	If This.nValoareCard > 0
		lcBon = lcBon + [2;CARD;0;] + Alltrim(Transform(This.nValoareCard)) + [;] + CRLF
	Endif

	If This.nValoareNumerar > 0
		lcBon = lcBon + [2;NUMERAR;0;] + Alltrim(Transform(This.nValoareNumerar)) + [;] + CRLF
	Endif

	lcDirectoryCaleFisier = JUSTPATH(This.cCaleFisier)
	IF !DIRECTORY(m.lcDirectoryCaleFisier)
		TRY 
			MD (m.lcDirectoryCaleFisier)
		CATCH 	
			AMESSAGEBOX('Nu exista directorul ' + m.lcDirectoryCaleFisier + ' si nu s-a putut crea! Creati-l manual!',0+32,_screen.Caption)
		ENDTRY 

	ENDIF 
	Try
		Strtofile(lcBon, This.cCaleFisier)

		If File(This.cCaleProgram)
			lcCurentDir = Sys(5) + Curdir()
			Cd Justpath(This.cCaleProgram)
				
			lcRun = Juststem(This.cCaleProgram) + [("] + This.cCaleFisier + [")]
			llSucces = &lcRun

			Cd (lcCurentDir)
			lnSucces = Iif(llSucces, CT_SUCCES, CT_INSUCCES)
		Else
			lnSucces = CT_INSUCCES
			AMESSAGEBOX('Nu exista ' + This.cCaleProgram + ' !',16,'Eroare')
		Endif
	Catch To loEx
		goLog.Log(m.lcBon)
		goLog.log('Fisier bon this.cCaleFisier: ' + this.cCaleFisier + " exista: " + TRANSFORM(FILE(this.cCaleFisier)))
		goLog.Log('This.cCaleProgram: ' + This.cCaleProgram)
				
		lnSucces = CT_INSUCCES
		AMESSAGEBOX(loEx.Message + CRLF + loEx.Procedure + CRLF + loEx.LineContents + CRLF + loEx.LineContents + CRLF + ;
			'Fisier bon this.cCaleFisier: ' + this.cCaleFisier + " exista: " + TRANSFORM(FILE(this.cCaleFisier)),16,'Eroare')
	Endtry

	Select (lcSelect)

	Return lnSucces
	Endproc && bon2ecr
**********************************************************************************************************************************
Enddefine
