PARAMETERS m.hladajdavku, m.menobuffera
PRIVATE m.hladajcstredisk, m.je_odecteno, m.vysledok
m.hladajcstredisk=m.gic_stredisk
IF m.issql
	had = CREATEOBJECT("had", jaz("prg_odpocet_davky")+" " + m.hladajdavku )
	had.SHOW()
	m.vysledok=sql_exec("SELECT food600.odpocet_vyroba('"+m.menobuffera+"',"+ALLTRIM(STR(m.cdata,2,0))+","+ALLTRIM(STR(m.hladajcstredisk,10,0))+","+ALLTRIM(STR(m.giakt_mena,2,0))+",'"+m.hladajdavku+"')","buf_dav")
	RELEASE had
	WAIT CLEAR
	SELECT 0
	USE (m.sqlprefix+"rdavkazadanka") ALIAS buf2
	REPLACE c_druh WITH c_druhk FOR EMPTY(c_druh)
	REPLACE c_druh WITH c_druhs FOR EMPTY(c_druh)
	UPDATE buf2 SET druh=rskupiny.druh FROM rskupiny WHERE buf2.c_druh=rskupiny.c_druh
	INSERT INTO buffer (c_karty, mnozstvi, cena, serial_no, menobuf, c_stredisk, operace, c_data) SELECT c_karty, SUM(mnozstvi) as mnozstvi, cena, vycisleni, m.menobuffera as menobuf, m.hladajcstredisk as c_stredisk, "ZV" as operace, m.cdata as c_data FROM buf2 group by c_karty, vycisleni 
ELSE
	had = CREATEOBJECT("had", jaz("prg_odpocet_davky")+" " + m.hladajdavku )
	had.SHOW()
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	SELECT karty
	SET ORDER TO c_karty
	SELECT polozky
	SET RELATION TO c_karty INTO karty
	REPLACE ALL pocet WITH karty.pocet
	SET RELATION TO

	SELECT c_karty, $0 AS k_odpoctu, vycisleni, cena_prum, .T. AS prepocet, m.giakt_mena AS c_mena, $0 as zustatek, c_stredisk ;
	       WHERE c_stredisk = gic_stredisk FROM sklad INTO CURSOR a_skl READWRITE 
	INDEX ON c_karty TAG c_karty

	SELECT c_karty, 0000000.0000 AS mnozstvi, $0 AS zustatek,$0 AS cena_prum,0 AS c_mena, c_stredisk ;
	       FROM a_skl GROUP BY c_karty INTO CURSOR prep WHERE .f. READWRITE 
	INDEX ON c_karty TAG c_karty
	INDEX ON STR(c_stredisk,12,0)+STR(c_karty,12,0) TAG csck

	SELECT c_karty, SUM(mnozstvi)+0.0000 AS mnozstvi, 0000000.0000 AS odecteno ;
		GROUP BY c_karty FROM buf_dav INTO CURSOR sk1 READWRITE 

	DO kalkuluj WITH m.hladajcstredisk,.f.,0

	SELECT c_karty, SUM(mnozstvi) AS mnozstvi, $0 as cena, " " as serial_no ;
		GROUP BY c_karty WHERE sk1.mnozstvi # 0 ;
		FROM sk1 INTO CURSOR sk2 READWRITE 
	SET RELATION TO c_karty INTO a_skl
	REPLACE ALL serial_no WITH a_skl.vycisleni, cena WITH NTOM(MTON(a_skl.cena_prum)*en__meny(a_skl.c_mena,m.giakt_mena))

	SELECT BUFFER
	INSERT INTO buffer (c_karty, mnozstvi, cena, serial_no, menobuf, c_stredisk, operace, c_data) SELECT c_karty, mnozstvi, cena, serial_no, m.menobuffera as menobuf, m.hladajcstredisk as c_stredisk, "ZV" as operace, m.cdata as c_data FROM sk2
	DO pozavirej WITH "sk1, sk2, qqq, kalk, a_skl"
	WAIT CLEAR
	RELEASE had
ENDIF 
