PRIVATE aa, krok, old_rec, man_path
zacatek_prepoctu = DATETIME()
had = CREATEOBJECT("had")
had.SHOW()
DO clos_data
USE (m.fn_skupiny) ALIAS skupiny ORDER druh    IN 0 SHARED
USE (m.fn_polozky) ALIAS polozky ORDER c_karty IN 0 SHARED 
USE (m.fn_karty)   ALIAS karty   ORDER c_karty IN 0 SHARED 
USE (m.fn_sklad)   ALIAS sklad                 IN 0 SHARED 
USE (m.fn_obraty)  ALIAS obraty                IN 0 SHARED 


SELECT polozky
SET RELATION TO c_karty INTO karty
REPLACE ALL pocet WITH karty.pocet
SET RELATION TO
REPLACE pocet WITH 1 FOR pocet=0


#IF "SLOVAK"$UPPER(GETENV("language"))
	SELECT c_karty,zustatek AS mnozstvi, cena_prum as cena, c_mena FROM sklad WHERE cena_prum<>0;
		INTO TABLE (m.gcpri_path + "st1")
	REPLACE ALL cena WITH NTOM(MTON(cena)*en__meny(c_mena,m.giakt_mena)) 
	had.set_had(22)
	SELECT c_karty, SUM(mnozstvi) AS mnozstvi, SUM(mnozstvi * cena) AS hodnota, AVG(cena) AS cena_prum ;
		GROUP BY c_karty FROM (m.gcpri_path + "st1") INTO TABLE (m.gcpri_path + "st")
	REPLACE FOR mnozstvi # 0 cena_prum WITH hodnota / mnozstvi
#ELSE
	SELECT o.c_karty, o.c_dokl, o.operace, o.cena, d.c_mena, o.mnozstvi, d.datum_dokl, d.kdy ;
		WHERE o.operace IN ("PE", "PV", "PR", "PB", "PC") AND o.mnozstvi >= 0 AND o.cena > 0 ;
		FROM (m.fn_obraty) o LEFT JOIN (m.fn_doklady) d ON d.c_dokl == o.c_dokl INTO TABLE (m.gcpri_path + "st1")

	REPLACE ALL cena WITH NTOM(MTON(cena)*en__meny(c_mena,m.giakt_mena))
	REPLACE FOR datum_dokl > TTOD(kdy) + 5 datum_dokl WITH TTOD(kdy)
	REPLACE FOR datum_dokl > DATE()        datum_dokl WITH DATE()
*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
	IF "D_GROS" $ UPPER(m.gchom_path)
		had.set_txt2 ("---------- Groseto ----------")
		INDEX ON c_karty TAG c_karty
		INDEX ON datum_dokl TAG dd DESC
		SCAN
			cc_recn = RECNO()
			SCATTER FIELDS c_karty MEMVAR
			DELETE FOR (c_karty = m.c_karty) .AND. (RECNO() # m.cc_recn)
			GO (m.cc_recn)
		ENDSCAN
	ENDIF
*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *

	SELECT MAX(datum_dokl) AS max_datum_dokl FROM (m.gcpri_path + "st1") INTO CURSOR max_datum_dokl
	SCATTER MEMVAR

	SELECT st1
	REPLACE FOR datum_dokl < m.max_datum_dokl - 33 mnozstvi WITH MAX(mnozstvi * m.calccen_k, 0.001)
	REPLACE FOR datum_dokl < m.max_datum_dokl - 66 mnozstvi WITH MAX(mnozstvi * m.calccen_k, 0.001)
	REPLACE FOR datum_dokl < m.max_datum_dokl - 99 mnozstvi WITH MAX(mnozstvi * m.calccen_k, 0.001)

	had.set_had(22)
	SELECT c_karty, SUM(mnozstvi) AS mnozstvi, SUM(mnozstvi * cena) AS cena_prum ;
		GROUP BY c_karty FROM (m.gcpri_path + "st1") INTO TABLE (m.gcpri_path + "st")
	REPLACE FOR mnozstvi # 0 cena_prum WITH cena_prum / mnozstvi
#ENDIF

INDEX ON c_karty TAG c_karty

had.set_had(33)
SELECT c_karty, nazev, cena_prum, 99 AS poradi, pocet, druh, druh_skl, druh_kalk, "   " AS spart ;
	FROM karty INTO TABLE (m.gcpri_path + "hlavicky")

SET RELATION TO druh_kalk INTO skupiny
REPLACE ALL spart WITH skupiny.spart_kas
SET RELATION TO

SELECT c_karty, c_poloz FROM (m.fn_polozky) INTO TABLE (m.gcpri_path + "pol")
SELECT DISTI c_karty FROM (m.gcpri_path + "pol") INTO CURSOR kalkulac
INDEX ON c_karty TAG c_karty

had.set_had(44)
SELECT hlavicky
SET RELATION TO c_karty INTO kalkulac
REPL FOR EMPT(kalkulac.c_karty) poradi WITH 0
sET RELA TO
INDEX ON c_karty TAG c_karty
FOR iii = 0 TO 17
	had.set_had(3 * m.iii + 44)
	SELE pol
	SET RELA TO c_poloz INTO hlavicky
	DELE FOR hlavicky.poradi = m.iii
	SET RELA OFF INTO hlavicky
	PACK
	IF RECCOUNT()=0
		EXIT
	ENDIF
	SELE DISTI c_karty FROM (m.gcpri_path + "pol") INTO CURSOR kalkulac
	INDEX ON c_karty TAG c_karty
	SELE hlavicky
	SET FILT TO poradi > m.iii
	SET RELA TO c_karty INTO kalkulac
	REPL FOR EMPT(kalkulac.c_karty) poradi WITH m.iii+1
	SET RELA OFF INTO kalkulac
	SET FILT TO
ENDFOR

IF iii > 16
	?? CHR(7)
	= MESSAGEBOX(jaz("prg_existuje_rekursivni_kalk") , 0+64+256, "", 3333)
ENDIF

SELE hlavicky
SET RELA TO c_karty INTO st
REPL ALL cena_prum WITH st.cena_prum
DO pozavirej WITH "st, st1, kalkulac, skupiny"
had.set_had(77)

#IF	.T.		&&&&&&&&&&&&&&&&& "SLOVAK"$UPPER(GETENV("language"))
	SELECT DISTI poradi FROM hlavicky INTO CURSOR poradi ORDER BY poradi
	SCAN FOR poradi >0
		m.poradi=poradi.poradi
		SELECT hlavicky
		LOCATE FOR hlavicky.poradi=m.poradi .AND. hlavicky.spart # "FMN"
		IF FOUND()
			SELECT SUM(polozky.hruba*hlavicky.cena_prum/polozky.pocet) AS cena, polozky.c_karty FROM polozky LEFT JOIN hlavicky ON polozky.c_poloz=hlavicky.c_karty GROUP BY polozky.c_karty WHERE ;
				polozky.c_karty IN (SELECT c_karty FROM hlavicky WHERE hlavicky.poradi=m.poradi .AND. hlavicky.spart # "FMN") INTO CURSOR pre_m_poradi readwrite
	        DELETE FOR ISNULL(cena)
			INDEX ON c_karty TAG c_karty
			SELECT hlavicky
			SET RELATION TO c_karty INTO pre_m_poradi
			REPLACE hlavicky.cena_prum WITH pre_m_poradi.cena FOR !EMPTY(pre_m_poradi.c_karty)
			USE IN pre_m_poradi
		ENDIF 
		SELECT poradi
	ENDSCAN
	SELECT hlavicky
*   *   *   *   *  porizovaci cena menu/variaci
	SCAN FOR spart="FMN"
		old_rec = RECNO()
		SCATT MEMVAR
		IF SEEK(m.c_karty, "polozky") .AND. ! EMPT(m.pocet)
			SELECT pol.poznamka, pol.hruba * hlavicky.cena_prum / m.pocet AS cena_prum, COUNT(pol.poznamka) AS moznosti ;
				GROUP BY 1 WHERE pol.c_karty = m.c_karty ;
				FROM (m.fn_polozky) pol LEFT JOIN (m.gcpri_path + "hlavicky") ON hlavicky.c_karty = pol.c_poloz ;
				INTO TABLE (m.gcpri_path + "pol_var")
			SUM cena_prum / moznosti TO cena_prum
		ENDIF
		SELE hlavicky
		GO (m.old_rec)
		GATH FIELD cena_prum MEMVAR
	ENDSCAN
#ELSE
	INDEX ON poradi TAG poradi
	GO TOP
	DO WHILE ! EOF()
		old_rec = RECNO()
		SET ORDE TO c_karty
		SCATT MEMVAR
		IF SEEK(m.c_karty, "polozky") .AND. ! EMPT(m.pocet)
			IF m.spart # "FMN"
				SELE polozky
				SET RELA TO c_poloz INTO hlavicky
				SUM hruba * hlavicky.cena_prum / m.pocet FOR c_karty = m.c_karty TO cena_prum
				SET RELA TO
			ELSE
*   *   *   *   *  porizovaci cena menu/variaci
				SELECT pol.poznamka, pol.hruba * hlavicky.cena_prum / m.pocet AS cena_prum, COUNT(pol.poznamka) AS moznosti ;
					GROUP BY 1 WHERE pol.c_karty = m.c_karty ;
					FROM (m.fn_polozky) pol LEFT JOIN (m.gcpri_path + "hlavicky") ON hlavicky.c_karty = pol.c_poloz ;
					INTO TABLE (m.gcpri_path + "pol_var")
				SUM cena_prum / moznosti TO cena_prum
			ENDIF
		ENDIF

		SELE hlavicky
		GO (m.old_rec)
		GATH FIELD cena_prum MEMVAR
		SET ORDE TO poradi
		SKIP
	ENDDO
#ENDIF
INDEX on c_karty TAG c_karty
SELECT karty
SET ORDER TO c_karty
SET RELATION TO c_karty INTO hlavicky
REPLACE karty.cena_prum WITH hlavicky.cena_prum, karty.c_mena_n WITH m.giakt_mena FOR (karty.cena_prum<>hlavicky.cena_prum .or. karty.c_mena_n<>m.giakt_mena) .and. !EMPTY(hlavicky.c_karty)
SET RELATION OFF INTO hlavicky

*SET RELA TO c_karty INTO karty
*REPL ALL karty.cena_prum WITH cena_prum, karty.c_mena_n WITH m.giakt_mena
SELECT sklad
SET RELATION TO c_karty INTO karty
REPLACE cena_prum WITH karty.cena_prum, c_mena WITH karty.c_mena_n FOR (zustatek = 0 .and. (karty.cena_prum<>sklad.cena_prum .or. karty.c_mena_n<>sklad.c_mena))
*REPLACE FOR zustatek = 0 cena_prum WITH NTOM(MTON(karty.cena_prum)*en__meny(karty.c_mena_n,sklad.c_mena))
DO clos_data
DO zurnal_log WITH "OPERA", "VYPOCET PORIZ. CEN", "."
m.konec_prepoctu=DATETIME()
*WAIT m.konec_prepoctu-m.zacatek_prepoctu WINDOW 
*WAIT m.konec_prepoctu window
*WAIT m.zacatek_prepoctu window
RELEASE had


