PARAMETERS ck
PRIVATE m.hladajckarty
*IF m.issql
IF .f.
	DO FORM snake NAME lsnake LINKED WITH jaz('form_prepocet_kalorii')
	lsnake.DRAW()
	m.vysledok=sql_exec("SELECT food600.prepocetalergenov("+ALLTRIM(STR(m.hladajckarty,2,0))+","+ALLTRIM(STR(m.cdata,2,0))+")")
	lsnake.release()
ELSE 
	LOCAL m.newkalorie, m.oldkalorie, m.pocetcyklov
	m.pocetcyklov=0
	SELECT 0
	CREATE CURSOR nakx (c_karty I)
	SELECT  * FROM nakx INTO CURSOR nakx1 WHERE .f. READWRITE 
	IF VARTYPE(m.ck)="N"
		INSERT INTO nakx (c_karty) VALUES (m.ck)
	ENDIF 
	DO WHILE RECCOUNT("nakx")>0
		m.pocetcyklov=m.pocetcyklov+1
		IF m.pocetcyklov>15
			EXIT
		ENDIF 
		SELECT nakx
		SCAN ALL 
			m.hladajckarty=nakx.c_karty
			SELECT 0
			IF USED("nakontrolu")
				=REQUERY("nakontrolu")
			ELSE 
				IF m.issql
					USE (m.sqlprefix+"rkdejekartapolozkoustrucna") ALIAS nakontrolu
				ELSE
					USE vkdejekartapolozkoustrucna ALIAS nakontrolu
				ENDIF 
			ENDIF 
			IF RECCOUNT("nakontrolu")>0
				SELECT nakontrolu
				SCAN ALL 
					m.hladajckarty=nakontrolu.c_karty 
					IF USED("vkaloriex")
						SELECT vkaloriex
						IF CURSORGETPROP("Buffering","vkaloriex")>1
							=TABLEREVERT(.t.,"vkaloriex")
						ENDIF 
						=REQUERY("vkaloriex")
					ELSE 
						SELECT 0
						IF m.issql
							USE (m.sqlprefix+"rkalorie") ALIAS vkaloriex
						ELSE 
							IF !USED("krest")
								USE (m.fn_krest) ALIAS krest IN 0
							ENDIF 
							SELECT 0
							USE vkalorie ALIAS vkaloriex
						ENDIF 
					ENDIF 
					=CURSORSETPROP("Buffering",5,"vkaloriex")
					IF RECCOUNT("vkaloriex")=0
						INSERT INTO vkaloriex (c_data, c_karty, kalorie, kal_zad) VALUES (m.cdata, m.hladajckarty, 0, 0)
						=tux(.t.,.t.,"vkaloriex")
						=REQUERY("vkaloriex")
						=CURSORSETPROP("Buffering",5,"vkaloriex")
					ENDIF 
					SELECT vkaloriex
					GO TOP 
					m.oldkalorie=vkaloriex.kalorie
					SELECT 0
					IF USED("vkalopoly")
						=REQUERY("vkalopoly")
					ELSE 
						SELECT 0
						IF m.issql
							USE (m.sqlprefix+"rpolozkyskaloriami") ALIAS vkalopoly
						ELSE 
							USE vpolozkyskaloriami ALIAS vkalopoly
						ENDIF 
					ENDIF
					m.newkalorie=0 
					IF RECCOUNT("vkalopoly")>0
						SELECT vkalopoly
						SCAN FOR !ISNULL(vkalopoly.kalorie) AND vkalopoly.kalorie>0 OR !ISNULL(vkalopoly.kal_zad) AND vkalopoly.kal_zad>0
							IF !ISNULL(vkalopoly.kal_zad) AND vkalopoly.kal_zad>0
								m.newkalorie=m.newkalorie+vkalopoly.hruba*vkalopoly.kal_zad/vkalopoly.pocet
							ELSE
								m.newkalorie=m.newkalorie+vkalopoly.hruba*vkalopoly.kalorie/vkalopoly.pocet
							ENDIF 							
						ENDSCAN 
					ENDIF
					IF m.oldkalorie<>m.newkalorie
						SELECT vkaloriex
						REPLACE kalorie WITH m.newkalorie
						IF EMPTY(vkaloriex.kal_zad)
							REPLACE kal_zad WITH m.newkalorie
						ENDIF 
						=tux(.t.,.t.,"vkaloriex")
						INSERT INTO nakx1 (c_karty) VALUES (m.hladajckarty)
					ENDIF 
					SELECT nakontrolu
				ENDSCAN 
			ENDIF 
			SELECT nakx
		ENDSCAN 
		USE IN nakx
		SELECT * FROM nakx1 INTO CURSOR nakx READWRITE 
		USE IN nakx1
		SELECT  * FROM nakx INTO CURSOR nakx1 WHERE .f. READWRITE 
	ENDDO 	
	DO pozavirej WITH "nakontrolu, nakx, nakx1, vkaloriex, vkalopoly" 
ENDIF 
