*:*****************************************************************************
*: Procedure file: E:\FOOD500\SKL2\ZRCCR_OR.PRG
*:*****************************************************************************
PARAMETERS odpdlhy, neodomykaj, m.cdokldo, m.menobuffera
PRIVATE partner, dokl, datum_dokl,zamknute,tyka_sa
m.tyka_sa=""
had = CREATEOBJECT("had")
IF !m.issql
	SELECT karty
	SET ORDE TO c_karty

	SELECT BUFFER
	UPDATE buffer SET zustatek=sklad.zustatek, cena=sklad.cena_prum*en__meny(sklad.c_mena,m.giakt_mena) FROM sklad WHERE buffer.c_karty=sklad.c_karty AND buffer.c_stredisk=sklad.c_stredisk

	SET RELATION TO c_karty INTO karty            && pro dluhy se uzije alespon cena z karty
	REPLACE FOR mnozstvi=0 .AND. cena=0 cena WITH karty.cena_prum, serial_no WITH "CENA0"
	REPLACE ALL nazev WITH karty.nazev
	SET RELATION OFF INTO karty
ENDIF 
SELECT buffer
IF CURSORGETPROP("Buffering","buffer")>1
	=tux(.T.,.T.,"buffer")
	=REQUERY("buffer")
ENDIF
SELECT c_stredisk, nazev, c_karty, SUM(mnozstvi) AS mnoz_ok, SUM(dluh_mnoz) AS dluh_mnoz, cena AS cena_prum, 0.000 AS pridano,m.giakt_mena AS c_mena, ;
	zustatek, 0000000.00 AS prachy_ok, 0000000.00 AS prachy_dl, .T. AS flg, 000000000000 AS kod ;
	FROM buffer GROUP BY c_stredisk, c_karty INTO CURSOR zrc READWRITE 
DELETE FOR mnoz_ok=0 AND dluh_mnoz=0

REPLACE ALL prachy_ok WITH mnoz_ok * cena_prum, prachy_dl WITH dluh_mnoz * cena_prum
SUM prachy_ok, prachy_dl TO sum_ok, sum_dluh
proc_dlh = IIF(sum_ok + sum_dluh>0, ROUND(sum_dluh / (sum_ok + sum_dluh) * 100, 1), 0)
INDEX ON nazev TAG nazev
INDEX ON STR(c_stredisk)+nazev TAG vsetko
SELECT strediska
INDEX ON c_stredisk TO (m.gcpri_path + "st_c_str")
SELECT zrc
SET RELATION TO c_stredisk INTO strediska ADDITIVE
RELEASE had
view_mssg = " zrcadlo"
m.zrc_title="Multidávka:" + m.hladajdavku
DO pr_report WITH "ZRC_OR"
SET RELATION OFF INTO strediska
IF USED("davk_stred")
	SELECT davk_stred
	SCAN ALL
		m.tyka_sa=m.tyka_sa+","+davk_stred.id_sklad
	ENDSCAN
ENDIF
SELECT zrc
IF m.proc_dlh <= m.lim_dluhu
	IF MESSAGEBOX(jaz("rep_naklady")+"  "+ ALLTRIM(STR(m.sum_ok, 12,1)) + " " + en__meny(m.giakt_mena,3) + ", " + TRANSFORM(m.proc_dlh, " 999.9% ") + jaz("form_dlhov"), ;
			3+32+256, jaz("odecist davku?")) = 6
		SELECT BUFFER
		REPLACE ALL mnozstvi WITH - mnozstvi
		IF CURSORGETPROP("Buffering","buffer")>1
			=tux(.T.,.T.,"buffer")
			=REQUERY("buffer")
		ENDIF 
		IF ! prazdna()
			IF !m.issql
				IF !USED("obraty")
					SELECT 0
					USE (m.fn_obraty) ALIAS obraty
				ENDIF
			ENDIF
			SELECT BUFFER
			SET FILTER TO mnozstvi # 0
			PRIVATE m.cmena, m.cstr
			m.cmena=m.giakt_mena
			m.cstr=m.gic_stredisk
			IF !m.issql
				SELECT prep
				DELETE ALL
				INSERT INTO prep SELECT c_karty, 0 AS mnozstvi, zustatek, cena, m.giakt_mena as c_mena, c_stredisk FROM BUFFER WHERE mnozstvi<>0
			ENDIF 
			DO buff_out2 WITH .F.,m.cmena,m.cstr,m.cdokldo,m.neodomykaj, m.menobuffera, .f.
			RELEASE m.cmena, m.cstr
			DO pozavirej WITH "a_skl, prep"
			IF !m.issql
				SELECT buffer
				IF CURSORGETPROP("Buffering","buffer")>1
					=REQUERY("buffer")
				ENDIF 
				SELECT dluhy
				DO zamkni
				INSERT INTO dluhy (c_dokl, operace, c_karty, mnozstvi, c_stredisk) SELECT c_dokl, operace, c_karty, dluh_mnoz AS mnozstvi, c_stredisk FROM BUFFER WHERE dluh_mnoz<>0
				UNLOCK
			ENDIF 
		ENDIF
		SELECT BUFFER
		SET FILTER TO
		SELECT davk_stred
		SCAN ALL 
			= n_sunlock ("BUF_OUT_"+davk_stred.id_sklad)
		ENDSCAN 
		SELECT buffer 
		GO TOP
		RETURN .T.
	ELSE
		RETURN .F.
	ENDIF
ELSE
	= MESSAGEBOX(jaz("form_teoreticke_naklady")+": " + ALLT(STR(sum_ok + sum_dluh, 12,2)) + ;
		CHR(13) + TRANS(m.proc_dlh, "999.9% ")+jaz("form_dlhov"), 16, jaz("form_neodcitane"), 4444)
	RETURN .F.
ENDIF

