PRIVATE aa, krok, old_rec, man_path, m.pol, m.boladb
m.boladb=.f.
IF m.issql
	RETURN
ENDIF 
had = CREATEOBJECT("had", "Výpočet nákupných cien")
had.SHOW()
CLOSE DATABASES 
*=STRTOFILE(TTOC(DATETIME())+" "+"Zaciatok "+CHR(13)+CHR(10),"M:\casy.txt",1)
*!*	OPEN DATABASE (m.gcini_path+"views\food600")
DO open_database
IF !m.issql
	chyba_pri_otevirani = .F.
	TRY  
    	otevira_se = m.fn_skupiny
		USE (m.fn_skupiny) ALIAS skupiny	IN 0 SHARED
    	otevira_se = m.fn_karty
		USE (m.fn_karty)   ALIAS karty		IN 0 SHARED 
    	otevira_se = m.fn_sklad
		USE (m.fn_sklad)   ALIAS sklad		IN 0 SHARED 
    	otevira_se = m.fn_polozky
		USE (m.fn_polozky) ALIAS polozky	IN 0 SHARED 
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",2)
    	 DO zurnal_log WITH "OPERA", jaz("m_kalk_calcceny"), jaz("nejde_otevrit") + m.otevira_se
		 CLOSE DATABASES 
    	 RETURN .F.
	ENDIF 
	SELECT karty
	SELECT sklad
	SELECT polozky
	DELETE FOR c_karty = 0 .or. c_poloz = 0
	SELECT 0
	USE vcalcpolpocet ALIAS rcalcpolpocet
	IF RECCOUNT()>0
		=CURSORSETPROP("Buffering",5,"rcalcpolpocet")
		REPLACE ALL pocet WITH kpocet
		REPLACE pocet WITH 1 FOR pocet=0
		=TABLEUPDATE(1,.T.,"rcalcpolpocet")
	ENDIF 
	USE IN rcalcpolpocet
*=STRTOFILE(TTOC(DATETIME())+" "+"rcalcpolpocet "+CHR(13)+CHR(10),"M:\casy.txt",1)
ENDIF 


SELECT 0
#IF "SLOVAK"$UPPER(GETENV("language"))
	IF m.issql
		USE (m.sqlprefix+"rscalcst1") ALIAS st1
	ELSE
		USE vscalcst1 ALIAS st1
	ENDIF 
	REPLACE cena WITH NTOM(MTON(cena)*en__meny(c_mena,m.giakt_mena)) FOR 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 st1 INTO CURSOR  st READWRITE 
	REPLACE FOR mnozstvi # 0 cena_prum WITH hodnota / mnozstvi
#ELSE
	IF m.issql
		USE (m.sqlprefix+"rccalcst1") ALIAS st1
	ELSE
		chyba_pri_otevirani = .F.
		TRY  
    		otevira_se = m.fn_obraty
			USE (m.fn_obraty)	ALIAS obraty	IN 0
    		otevira_se = m.fn_doklady
			USE (m.fn_doklady)  ALIAS doklady		IN 0 SHARED 
		CATCH 
    		chyba_pri_otevirani = .T.
		ENDTRY 
		IF chyba_pri_otevirani
			=MESSAGEBOX(jaz("prg_dbf_neprist_skuste_pozdeji")+CHR(13)+m.otevira_se,64,"Food600",2)
	    	 DO zurnal_log WITH "OPERA", jaz("m_kalk_calcceny"), jaz("nejde_otevrit") + m.otevira_se
			 CLOSE DATABASES 
    		 RETURN .F.
		ENDIF 

		SELECT 0
		USE vccalcst1 ALIAS st1
	ENDIF 

	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) .or. m.calccen_k < 0.1
		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 st1 INTO CURSOR max_datum_dokl
	SCATTER MEMVAR

	SELECT st1
	REPLACE FOR operace = "PR" mnozstvi WITH MAX(mnozstvi, 0.001)
	SCAN FOR operace = "PC"    && snizi mnozstvi na vsech pohybech pred precenenim
	SCATTER FIELDS c_stredisk, c_karty, c_dokl memvar
		o_recn = RECNO()
		REPLACE FOR c_stredisk = m.c_stredisk .and. c_karty = m.c_karty .and. c_dokl < m.c_dokl mnozstvi WITH .001
		GO (m.o_recn)
	ENDSCAN 

	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 st1 INTO CURSOR st READWRITE
	REPLACE FOR mnozstvi # 0 cena_prum WITH cena_prum / mnozstvi
#ENDIF

INDEX ON c_karty TAG c_karty

had.set_had(33)
IF m.issql
	USE (m.sqlprefix+"rcalchlavicky") ALIAS hlavicky IN 0
	USE (m.sqlprefix+"rcalcpolozky") ALIAS pol IN 0
ELSE
	USE vcalchlavicky ALIAS hlavicky IN 0
	USE vcalcpolozky ALIAS pol IN 0
ENDIF 
SELECT hlavicky
=CURSORSETPROP("Buffering",5,"hlavicky")
SELECT pol
INDEX ON c_karty TO (m.gcpri_path+"C"+SUBSTR(SYS(2015),4))
*=STRTOFILE(TTOC(DATETIME())+" "+"otvorenie cursorov "+CHR(13)+CHR(10),"M:\casy.txt",1)

SELECT DISTI c_karty FROM pol INTO CURSOR kalkulac
INDEX ON c_karty TAG c_karty

had.set_had(44)
SELECT hlavicky
SET RELATION TO c_karty INTO kalkulac
REPLACE FOR EMPT(kalkulac.c_karty) poradi WITH 0
SET RELATION OFF INTO kalkulac
=TABLEUPDATE(1,.t.,"hlavicky")
FOR iii = 0 TO 17
	had.set_had(3 * m.iii + 44)
    had.set_txt2(STR(m.iii) + STR(m.iii))
	SELECT c_karty FROM hlavicky WHERE poradi=m.iii INTO CURSOR poradia
	INDEX on c_karty TAG c_karty
	SELECT pol
	SET RELA TO c_poloz INTO poradia
	DELETE FOR !EMPTY(poradia.c_karty)
	SET RELATION OFF INTO poradia
	USE IN poradia
	SELECT pol
	COUNT TO m.pol FOR !DELETED()
	IF m.pol=0
		RECALL ALL 
		EXIT
	ENDIF
	SELE DISTI c_karty FROM pol INTO CURSOR kalkulac
	INDEX ON c_karty TAG c_karty
	SELECT hlavicky
	SET FILTER TO poradi > m.iii
	SET RELATION TO c_karty INTO kalkulac
	REPLACE FOR EMPTY(kalkulac.c_karty) poradi WITH m.iii+1
	SET RELATION OFF INTO kalkulac
	SET FILTER TO
	=TABLEUPDATE(1,.t.,"hlavicky")
ENDFOR
had.set_txt2("")

IF iii > 16
	?? CHR(7)
	= MESSAGEBOX(jaz("prg_existuje_rekursivni_kalk") , 0+64+256, "", 3333)
ENDIF
*=STRTOFILE(TTOC(DATETIME())+" "+"prvy for "+CHR(13)+CHR(10),"M:\casy.txt",1)
SELECT hlavicky
SET RELATION TO c_karty INTO st
REPLACE ALL cena_prum WITH st.cena_prum FOR hlavicky.cena_prum<>st.cena_prum
SET RELATION OFF INTO st
*!*	m.old_rec=GETNEXTMODIFIED(0,"hlavicky")
*!*	DO WHILE m.old_rec<>0 
*!*		GOTO (m.old_rec)
*!*		IF OLDVAL("cena_prum")<>cena_prum .or. OLDVAL("c_mena_n")<>c_mena_n
*!*			=TABLEUPDATE(0,.t., "hlavicky")
*!*		ENDIF 
*!*		m.old_rec=GETNEXTMODIFIED(m.old_rec,"hlavicky")
*!*	ENDDO 
=TABLEUPDATE(1,.t., "hlavicky")
SELECT hlavicky
DO pozavirej WITH "st, st1, kalkulac, skupiny"
*=STRTOFILE(TTOC(DATETIME())+" "+"update hlavicky "+CHR(13)+CHR(10),"M:\casy.txt",1)
had.set_had(77)
SELECT DISTI poradi FROM hlavicky INTO CURSOR poradi ORDER BY poradi
SCAN FOR poradi >0
	m.poradi=poradi.poradi
	SELECT hlavicky
	SELECT c_karty FROM hlavicky WHERE hlavicky.poradi=m.poradi .AND. hlavicky.spart # "FMN" INTO CURSOR calc
	IF RECCOUNT()>0
		IF m.issql
			SELECT SUM(pol.hruba*hlavicky.cena_prum/pol.pocet) AS cena, pol.c_karty FROM pol LEFT JOIN hlavicky ON pol.c_poloz=hlavicky.c_karty GROUP BY pol.c_karty WHERE ;
				pol.c_karty IN (SELECT c_karty FROM hlavicky WHERE hlavicky.poradi=m.poradi .AND. hlavicky.spart # "FMN") INTO CURSOR pre_m_poradi readwrite
		ELSE
			SELECT SUM(MTON(pol.hruba)*MTON(hlavicky.cena_prum)/MTON(pol.pocet)) AS cena, pol.c_karty FROM pol LEFT JOIN hlavicky ON pol.c_poloz=hlavicky.c_karty GROUP BY pol.c_karty WHERE ;
				pol.c_karty IN (SELECT c_karty FROM hlavicky WHERE hlavicky.poradi=m.poradi .AND. hlavicky.spart # "FMN") INTO CURSOR pre_m_poradi readwrite
		ENDIF 		
        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 
	USE IN calc
	m.old_rec=GETNEXTMODIFIED(0,"hlavicky")

	SELECT hlavicky

	DO WHILE m.old_rec<>0 
		GOTO (m.old_rec)
		IF OLDVAL("cena_prum")<>cena_prum .or. OLDVAL("c_mena_n")<>c_mena_n
			=TABLEUPDATE(0,.t., "hlavicky")
		ENDIF 
		m.old_rec=GETNEXTMODIFIED(m.old_rec,"hlavicky")
	ENDDO 
	SELECT poradi
ENDSCAN
*=STRTOFILE(TTOC(DATETIME())+" "+"prvy scan "+CHR(13)+CHR(10),"M:\casy.txt",1)

SELECT hlavicky
*   *   *   *   *  porizovaci cena menu/variaci
SELECT c_karty, pocet FROM hlavicky WHERE spart="FMN" INTO CURSOR fmn
IF RECCOUNT()>0
	SELECT fmn
	SCAN ALL 
		old_rec = fmn.c_karty
		SCATT MEMVAR
		IF SEEK(m.c_karty, "pol") .AND. ! EMPT(m.pocet)
			SELECT pol.poznamka, MTON(pol.hruba) * MTON(hlavicky.cena_prum) / MTON(m.pocet) AS cena_prum, COUNT(pol.poznamka) AS moznosti ;
				GROUP BY 1 WHERE pol.c_karty = m.c_karty ;
				FROM pol LEFT JOIN hlavicky ON hlavicky.c_karty = pol.c_poloz ;
				INTO TABLE (m.gcpri_path + "pol_var")
			SUM MTON(cena_prum) / MTON(moznosti) TO cena_prum
		ELSE
			m.cena_prum=0
		ENDIF
		SELE hlavicky
		REPLACE cena_prum WITH m.cena_prum FOR c_karty=m.old_rec
	ENDSCAN
ENDIF 
USE IN fmn
*=STRTOFILE(TTOC(DATETIME())+" "+"fmn "+CHR(13)+CHR(10),"M:\casy.txt",1)

SELECT hlavicky
REPLACE c_mena_n WITH m.giakt_mena FOR c_mena_n<>m.giakt_mena
GO TOP 
*!*	m.old_rec=GETNEXTMODIFIED(0,"hlavicky")
*!*	DO WHILE m.old_rec<>0 
*!*		GOTO (m.old_rec)
*!*		IF OLDVAL("cena_prum")<>cena_prum .or. OLDVAL("c_mena_n")<>c_mena_n
*!*			=TABLEUPDATE(0,.t., "hlavicky")
*!*		ENDIF 
*!*		m.old_rec=GETNEXTMODIFIED(m.old_rec,"hlavicky")
*!*	ENDDO 
=TABLEUPDATE(1,.t., "hlavicky")
USE IN hlavicky
*=STRTOFILE(TTOC(DATETIME())+" "+"update hlavicky "+CHR(13)+CHR(10),"M:\casy.txt",1)
SELECT 0
IF m.issql
	USE (m.sqlprefix+"rcalcsklad") ALIAS updsklad
ELSE
	USE vcalcsklad ALIAS updsklad
ENDIF
=CURSORSETPROP("buffering",5,"updsklad")
REPLACE ALL cena_prum WITH kcena, c_mena WITH c_mena_n 
=TABLEUPDATE(1,.t.,"updsklad")
USE IN updsklad
CLOSE DATABASES
CLOSE DATABASES
DO zurnal_log WITH "OPERA", "VYPOCET PORIZ. CEN", "."
RELEASE had


