datum = ""
m.pri_o_oper = "PO"
prach_prod = 0
uzaverka = ""
odecist = .T.
pridej = .F.
lic_dokl = -9999
lcoperace = "VK"

DO clos_data
DO buff_cre WITH 1
USE (m.fn_sk_pol)  ALIAS sk_pol  IN 0
USE (m.fn_sk_sez)  ALIAS sk_sez  IN 0
USE (m.fn_polozky) ALIAS polozky IN 0 ORDER c_karty
USE (m.fn_karty)   ALIAS karty   IN 0 ORDER c_karty
*use (m.fn_obraty)  alias obraty  in 0
USE (m.fn_doklady) ALIAS doklady IN 0 ORDER c_dokl
USE (m.fn_dluhy)   ALIAS dluhy   IN 0

SELE c_karty, zustatek + 0.0000 AS k_odpoctu, vycisleni WHERE c_stredisk = gic_stredisk ;
	FROM (m.gcdat_path + "sklad") INTO TABLE (m.gcpri_path + "a_skl")
INDEX ON c_karty TAG c_karty

SELECT c_dokl, c_karty, operace, DATE() AS datum_dokl, SUM(mnozstvi) AS mnozstvi, m.gcpgm_id AS id_man, ;
	SPACE(30) AS nazev, SPACE(15) AS pz1, SPACE(15) AS pz2, 00000000 AS c_partner, " " AS LABEL ;
	WHERE (c_stredisk = m.gic_stredisk) AND EMPT(provedl) ;
	GROUP BY c_dokl, c_karty ;
	FROM dluhy INTO TABLE (m.gcpri_path + "sk_pri")
IF ! prazdna(.T.)
	SET RELA TO c_karty INTO karty, c_dokl INTO doklady
	REPL ALL nazev WITH karty.nazev, pz1 WITH doklady.pz1, pz2 WITH doklady.pz2, ;
		datum_dokl WITH doklady.datum_dokl, c_partner WITH doklady.c_partner
	SET RELA TO
	DEFINE WINDOW brw FROM 1,2 TO SROWS()-3,99
	m.ttl = jaz("prg_vyber_k_odpoctu_ttl")
	DO setkeys
	BROWSE FIELDS c_karty, nazev, pz1, pz2, LABEL:h=" " FREEZE LABEL ;
		NOAPPEND NODELE WINDOW brw NORMAL TITLE jaz("prg_vyber_dluhy_ttl_popup")
	DO clrkeys
	RELEASE WINDOW brw
	DELE FOR EMPT(LABEL)
	IF ! prazdna(.T.) .AND. (LAST() # 27)
		DO working
		SELE DISTI pz1, pz2, operace, datum_dokl, c_partner ;
			FROM (m.gcpri_path + "sk_pri") INTO TABLE (m.gcpri_path + "uzaverky")
		INDEX ON pz1 + pz2 TAG parovaky

		SELECT uzaverky
		SCAN
			SCATTER FIELD pz1, pz2, operace, datum_dokl, c_partner MEMVAR


			SELECT c_karty, SUM(mnozstvi)+0.0000 AS mnozstvi, 0000000.0000 AS odecteno ;
				GROUP BY c_karty WHERE pz1 = m.pz1 AND pz2 = m.pz2 ;
				FROM (m.gcpri_path + "sk_pri") INTO TABLE (m.gcpri_path + "sk1")
*			SELECT 000000000000 AS c_karty,$0 AS hruba, $0 AS cista FROM sk1 INTO TABLE (m.gcpri_path + "kalk")

			DO kalkuluj

			SELECT c_karty, SUM(mnozstvi) AS dluh_mnoz, SUM(odecteno) AS mnozstvi, ;
				"Q" AS LABEL, m.lic_dokl AS c_dokl ;
				GROUP BY c_karty WHERE sk1.mnozstvi # 0 .OR. sk1.odecteno # 0 ;
				FROM (m.gcpri_path + "sk1") INTO TABLE (m.gcpri_path + "sk2")
			SELE BUFFER
			APPE FROM (m.gcpri_path + "sk2")

			INSERT INTO (m.fn_bufdokl) (operace, c_dokl, datum_dokl, pz1, pz2, c_partner) ;
				VALUES (m.operace, m.lic_dokl, m.datum_dokl, m.pz1, m.pz2, m.c_partner)
			lic_dokl = m.lic_dokl + 1
		ENDSCAN
		DO pozavirej WITH "sk1, sk2, qqq, kalk, a_skl"
		WAIT CLEAR
		zrc_title = ""
		IF zrc_crea()
			SELE sk_pri
			SCAN
				SELE dluhy
				REPL FOR c_dokl = sk_pri.c_dokl .AND. c_karty = sk_pri.c_karty ;
					provedl WITH m.gcuser_id, kdy WITH DATETIME()
				SELE sk_pri
			ENDSCAN
		ENDIF
	ENDIF
ENDIF

DO clos_data
