CLOSE DATABASES 
chyba_pri_otevirani = .F.
TRY
	IF m.issql
		otevira_se = "milanovo SQL"
	ELSE
		otevira_se = m.fn_karty
		USE (m.fn_karty) ALIAS karty IN 0 
		otevira_se = m.fn_partneri
		USE (m.fn_partneri) ALIAS partneri IN 0 ORDER partner
		otevira_se = m.fn_sklad
		USE (m.fn_sklad)    ALIAS sklad    IN 0
		SELECT sklad
		SET FILTER TO c_stredisk = m.gic_stredisk
		otevira_se = m.fn_doklady
		USE (m.fn_doklady)  ALIAS doklady  IN 0
		otevira_se = m.fn_users
		USE (m.fn_users)  ALIAS users  IN 0
	ENDIF
CATCH TO oErr 
    otevira_se = m.otevira_se + CHR(13) + ALLTRIM(STR(oErr.ErrorNo)) + " " + oErr.Message 
	chyba_pri_otevirani = .T.
ENDTRY
IF chyba_pri_otevirani
	=MESSAGEBOX(jaz("prg_dbf_neprist_skuste_pozdeji")+CHR(13)+m.otevira_se,64,"Food600",2000)
	DO zurnal_log WITH "OPERA", "VYD_ZAD", jaz("nejde_otevrit") + m.otevira_se
	RETURN .F.
ENDIF


SELECT PADR(ALLTRIM(STR(doklady.c_dokl,12,0)),12," ") AS c_dokl, doklady.c_partner, STR(doklady.doklad)+" "+doklady.pz1 AS doklad, doklady.datum_dokl, doklady.pz1, doklady.pz2, partneri.partner ;
	WHERE operace = "ZV" .AND. EMPT(pz2) .AND. c_stredisk = m.gic_stredisk ;
	FROM doklady LEFT JOIN partneri ON partneri.c_partner = doklady.c_partner ;
	INTO TABLE (m.gcpri_path + "d_pri_i")

IF ! prazdna(.T.)
	REPLACE FOR ISNULL(partner) partner WITH "- - - zrusen - - -"
	INDEX ON partner + doklad TAG pd
	DO FORM dajdruh WITH "","d_pri_i","c_dokl",100, "partner",200,"datum_dokl",150,"doklad",150 TO m.druh
	IF ! EMPTY(m.druh)
		IF povoleny_vydaj()
			SELECT d_pri_i
			LOCATE FOR c_dokl=m.druh
			z_c_dokl    = VAL(d_pri_i.c_dokl)
			c_partner = d_pri_i.c_partner
			DO buff_cre
			IF !USED("obraty")
				SELECT 0
				USE (m.fn_obraty) ALIAS obraty
			ENDIF
			SELECT MAX(c_dokl) as c_dokl FROM obraty WHERE c_stredisk=m.gic_stredisk .AND. LEFT(obraty.operace, 1) $ "VP" INTO CURSOR aaa
			m.maxcdokl=aaa.c_dokl
			USE IN aaa 

			SELECT c_dokl, c_karty, SUM(mnozstvi) AS mnozstvi, "VV" AS operace, m.gic_stredisk AS c_stredisk, poradi ;
				GROUP BY 1, 2 WHERE c_dokl = m.z_c_dokl FROM obraty INTO TABLE (m.gcpri_path + "zadzad")
			SELECT BUFFER
			APPEND FROM (m.gcpri_path + "zadzad")
			REPLACE pov_objem WITH 1 FOR EMPTY(pov_objem)
*!*				SELECT c_karty, SUM(mnozstvi) as mnozstvi, $0 as zustatek,$0 as cena_prum,0 as c_mena, m.gic_stredisk as c_stredisk FROM buffer GROUP BY c_karty INTO TABLE (m.gcpri_path+"prep"+ALLTRIM(m.gcpgm_id))
*!*				USE
*!*				USE (m.gcpri_path+"prep"+ALLTRIM(m.gcpgm_id)) ALIAS prep EXCLUSIVE 
			SELECT c_karty, SUM(mnozstvi) as mnozstvi, $0 as zustatek,$0 as cena_prum,0 as c_mena, m.gic_stredisk as c_stredisk FROM buffer GROUP BY c_karty INTO CURSOR prep READWRITE 
			INDEX ON c_karty TAG c_karty
			INDEX ON STR(c_stredisk,12,0)+STR(c_karty,12,0) TAG csck 
			DO prepocet2 WITH "prep",0,m.maxcdokl,.F.,.T.
			SELECT prep
			SET ORDER TO c_karty
			SELECT BUFFER
			SET RELATION TO c_karty INTO prep
			REPLACE ALL cena WITH prep.cena_prum*en__meny(prep.c_mena,m.giakt_mena), mnozstvi WITH MIN(mnozstvi,prep.zustatek)
			SET RELATION OFF INTO prep
			DELETE FOR EMPTY(mnozstvi)
			REPLACE ALL mnozstvi WITH -mnozstvi
			SELECT buf_dokl
			REPLACE c_stredisk WITH m.gic_stredisk, c_dokl WITH m.z_c_dokl, c_partner WITH m.c_partner, operace WITH "VV"
			m.cmena=m.giakt_mena
			m.cstredisk=m.gic_stredisk
			DO buff_out2 WITH .f.,m.cmena, m.cstredisk, m.maxcdokl,.f.
			SELECT buf_dokl
			GO TOP
			DO prn_obra WITH buf_dokl.c_dokl
			DO pozavirej WITH "obraty, prep"
			SELECT doklady
			REPLACE FOR c_dokl = m.z_c_dokl pz2 WITH STR(buf_dokl.c_dokl)
		ENDIF 
	ENDIF
ENDIF
CLOSE DATABASES 
CLOSE DATABASES 
