PARAMETERS ck
PRIVATE m.hladajckarty
*IF m.issql
IF .f.
	DO FORM snake NAME lsnake LINKED WITH 'Prepočet alergénov'
	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.newalergeny, m.oldalergeny, 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("valergenyx")
						SELECT valergenyx
						IF CURSORGETPROP("Buffering","valergenyx")>1
							=TABLEREVERT(.t.,"valergenyx")
						ENDIF 
						=REQUERY("valergenyx")
					ELSE 
						SELECT 0
						IF m.issql
							USE (m.sqlprefix+"ralergeny") ALIAS valergenyx
						ELSE 
							IF !USED("krest")
								USE (m.fn_krest) ALIAS krest IN 0
							ENDIF 
							SELECT 0
							USE valergeny ALIAS valergenyx
						ENDIF 
					ENDIF 
					=CURSORSETPROP("Buffering",5,"valergenyx")
					IF RECCOUNT("valergenyx")=0
						INSERT INTO valergenyx (c_data, c_karty, alergeny) VALUES (m.cdata, m.hladajckarty,0)
						=tux(.t.,.t.,"valergenyx")
						=REQUERY("valergenyx")
						=CURSORSETPROP("Buffering",5,"valergenyx")
					ENDIF 
					SELECT valergenyx
					GO TOP 
					m.oldalergeny=valergenyx.alergeny
					SELECT 0
					IF USED("valerpoly")
						=REQUERY("valerpoly")
					ELSE 
						SELECT 0
						IF m.issql
							USE (m.sqlprefix+"rpolozkysalergenmi") ALIAS valerpoly
						ELSE 
							USE vpolozkysalergenmi ALIAS valerpoly
						ENDIF 
					ENDIF
					m.newalergeny=0 
					IF RECCOUNT("valerpoly")>0
						SELECT valerpoly
						SCAN ALL 
							m.newalergeny=BITOR(m.newalergeny,valerpoly.alergeny)
						ENDSCAN 
					ENDIF
					IF m.oldalergeny<>m.newalergeny
						SELECT valergenyx
						REPLACE alergeny WITH m.newalergeny
						=tux(.t.,.t.,"valergenyx")
						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, valergenyx, valerpoly" 
ENDIF 
