*!*****************************************************************************
*!      Procedure: ODP_KASY
*!*****************************************************************************
*PROCEDURE odp_kasy
PRIVATE m.cdokldo
datum = ""
m.pri_o_oper = "PO"
prach_prod = 0
uzaverka = ""
odecist = .T.
pridej = .F.
lic_dokl = -9999
lcoperace = "VK"
ondruv_export = .F.

DO clos_data
DO buff_cre_x WITH 1
IF !m.issql
	chyba_pri_otevirani = .F.
	TRY
		otevira_se = m.fn_dluhy
		USE (m.fn_dluhy)    ALIAS dluhy		IN 0
		otevira_se = m.fn_karty
		USE (m.fn_karty)    ALIAS karty		IN 0 ORDER nazev
		otevira_se = m.fn_food_dat
		USE (m.fn_food_dat) ALIAS food_dat	IN 0
		otevira_se = m.fn_doklady
		USE (m.fn_doklady)  ALIAS doklady	IN 0
		otevira_se = m.fn_polozky
		USE (m.fn_polozky)  ALIAS polozky	IN 0 ORDER c_karty
		otevira_se = m.fn_sk_fin
		USE (m.fn_sk_fin)   ALIAS sk_fin	IN 0 ORDER uzuc
		otevira_se = m.fn_sk_pol
		USE (m.fn_sk_pol)   ALIAS sk_pol	IN 0
		otevira_se = m.fn_sk_sez
		USE (m.fn_sk_sez)   ALIAS sk_sez	IN 0
		otevira_se = m.fn_sklad
		USE (m.fn_sklad)   ALIAS sklad		IN 0
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, 16, "Food600",2222)
		DO zurnal_log WITH "OPERA", jaz( "m_odp_kasy"), jaz("nejde_otevrit") + m.otevira_se
		CLOSE DATABASES
		RETURN .F.
	ENDIF
ENDIF
SELECT 0
IF FILE(m.gccen_path + "hodiny_uzaverek.dbf")
    USE (m.gccen_path + "hodiny_uzaverek") ORDER id_kas
ELSE
    CREATE TABLE (m.gccen_path + "hodiny_uzaverek") (id_kas C(2), hodina C(2), n_hodina N(2) )
    INDEX on id_kas TAG id_kas
ENDIF 
REPLACE ALL n_hodina WITH VAL(hodina)
SELECT sk_sez
DO dopln IN dopln_uctden_do_sksez WITH m.fn_sk_sez
USE IN hodiny_uzaverek
#IF "SLOVAK"$UPPER(GETENV("LANGUAGE"))
	INDEX ON SUBSTR(uzaverka,3,12)+"/"+id_kas TO (m.gcpri_path+"sezdokl")
#ELSE
	INDEX ON uzaverka+"/"+id_kas TO (m.gcpri_path+"sezdokl")
#ENDIF
SELECT doklady
SET FILTER TO c_stredisk=m.gic_stredisk
SET RELATION TO pz2 INTO sk_sez
REPLACE sk_sez.odecteno WITH DTOS(doklady.kdy) + " " + STR(HOUR(doklady.kdy),2) + ":" + STR(MINUTE(doklady.kdy),2) + ":--" ;
	FOR EMPTY(sk_sez.odecteno) .AND. sk_sez.id_man==m.gcpgm_id
SET RELATION OFF INTO sk_sez

******************************************************************
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.cdokldo=aaa.c_dokl
USE IN aaa 

SELECT DISTINCT c_karty, zustatek + 0.0000 AS k_odpoctu, vycisleni, cena_prum, .F. AS prepocet, m.giakt_mena AS c_mena, zustatek, c_stredisk WHERE c_stredisk = gic_stredisk ;
	FROM sklad INTO TABLE (m.gcpri_path + "a_skl")
INDEX ON c_karty TAG c_karty

*!*	SELECT c_karty, 000000.0000 AS mnozstvi, $0 AS zustatek,$0 AS cena_prum,0 AS c_mena, c_stredisk FROM a_skl GROUP BY c_karty INTO TABLE (m.gcpri_path+"prep"+ALLTRIM(m.gcpgm_id)) WHERE .f.
*!*	USE
*!*	USE (m.gcpri_path+"prep"+ALLTRIM(m.gcpgm_id)) ALIAS prep EXCLUSIVE
SELECT c_karty, 000000.0000 AS mnozstvi, $0 AS zustatek,$0 AS cena_prum,0 AS c_mena, c_stredisk FROM a_skl GROUP BY c_karty INTO CURSOR prep WHERE .f. READWRITE 
INDEX ON c_karty TAG c_karty
INDEX ON STR(c_stredisk,12,0)+STR(c_karty,12,0) TAG csck

#IF "SLOVAK"$UPPER(GETENV("LANGUAGE"))
	SELECT uzaverka + "  " + id_kas AS druh, .F. AS lbl, lastuzav AS popis,lastuzav, uzaverka, id_kas, id_man, odecteno, uct_den ;
		WHERE id_man == gcpgm_id .AND. EMPT(odecteno) ;
		FROM sk_sez INTO TABLE (m.gcpri_path + "seldruhy")
#ELSE
	SELECT uzaverka + "      " + id_kas AS druh, .F. AS lbl, uzaverka, id_kas, id_man, odecteno, uct_den ;
		WHERE id_man == gcpgm_id .AND. EMPT(odecteno) ;
		FROM sk_sez INTO TABLE (m.gcpri_path + "seldruhy")
#ENDIF
IF ! prazdna(.T.)
	#IF "SLOVAK"$UPPER(GETENV("LANGUAGE"))
		REPLACE popis WITH "" FOR SUBSTR(lastuzav,2,1)<>"_"
		SELECT 0
		USE(m.gcdat_path+"limit_hl") ORDER id_limit
		SELECT seldruhy
		SET FILTER TO LEFT(lastuzav,2)="L_"
		SET RELATION TO VAL(SUBSTR(lastuzav,3)) INTO limit_hl
		REPLACE ALL seldruhy.popis WITH limit_hl.limit
		SET RELATION OFF INTO limit_hl
		USE IN limit_hl
		SELECT seldruhy
		SET FILTER TO
	#ENDIF
	INDEX ON id_kas + uzaverka TAG id_kas
	INDEX ON uzaverka + id_kas TAG uzaverka
	GO TOP
	DO FORM sel_uzav TO dlouhy_log
*=STRTOFILE(TTOC(DATETIME())+" "+"ODP_KAS odkliknuty OK"+CHR(13)+CHR(10),"M:\casy.txt",1)
	SELECT seldruhy
	#IF "SLOVAK"$UPPER(GETENV("LANGUAGE"))
		m.hlasenie=""
		SCAN FOR lbl .and. LEFT(DTOS(uct_den),6)>m.uctobd
			m.hlasenie=m.hlasenie+uzaverka+"/"+id_kas+CHR(13)+CHR(10)
			REPLACE lbl WITH .f.
		ENDSCAN 
		IF !EMPTY(m.hlasenie)
			=MESSAGEBOX(m.hlasenie,64,"Uzávierky z iného účtovného obdobia sa nedajú odpočítať")
		ENDIF 
	#ENDIF
	COUNT FOR lbl TO m.kolik
	IF (m.kolik > 0)
		had = CREATEOBJECT("had")
		had.SHOW()

		CREATE TABLE (m.gcpri_path + "odp_log") (c_karty i, hruba Y, o_c_karty i, o_mnozstvi Y, hloubka i, nazev c(30), o_nazev c(30))

		SELECT uzaverka, id_kas, id_man, odecteno WHERE lbl ;
			FROM (m.gcpri_path + "seldruhy") INTO TABLE (m.gcpri_path + "uzaverky")
		INDEX ON uzaverka + id_kas TAG uz_id_kas

		#IF "SLOVAK"$UPPER(GETENV("LANGUAGE"))
			SELECT uzaverka, u_cis, id_kas, c_karty, nazev, cen_hlad, INT(stor_mno) AS ondra_kod, SUM(mnozstvi) AS mnozstvi, SPACE(5) AS platba ;
				FROM (m.fn_sk_pol) INTO TABLE (m.gcpri_path + "sk_pri_a") ;
				GROUP BY uzaverka, id_kas, nazev, cen_hlad WHERE (! EMPTY(nazev)) AND (id_man = m.gcpgm_id) AND ;
				uzaverka + id_kas IN (SELECT uzaverka + id_kas FROM (m.gcpri_path + "uzaverky")) ;
				HAVING !EMPTY(k_odpoctu) .AND. k_odpoctu<>"F"
		#ELSE
			SELECT uzaverka, u_cis, id_kas, c_karty, nazev, cen_hlad, INT(stor_mno) AS ondra_kod, SUM(mnozstvi) AS mnozstvi, SPACE(5) AS platba ;
				FROM (m.fn_sk_pol) INTO TABLE (m.gcpri_path + "sk_pri_a") ;
				GROUP BY uzaverka, u_cis, id_kas, nazev, cen_hlad WHERE (! EMPTY(nazev)) AND (id_man = m.gcpgm_id) AND ;
				uzaverka + id_kas IN (SELECT uzaverka + id_kas FROM (m.gcpri_path + "uzaverky")) ;
				HAVING !EMPTY(k_odpoctu)
		#ENDIF
		REPLACE FOR EMPT(cen_hlad) cen_hlad WITH "1"

		*********   odpocet po casovych atributech   *********  BEGIN
		*    SET RELATION TO u_cis + uzaverka INTO sk_fin
		*    REPLACE ALL platba WITH sk_fin.cas_atrib
		*    USE IN sk_fin
		*********   odpocet po casovych atributech   *********  END
		SELECT uzaverka, id_kas, c_karty, nazev, cen_hlad, platba,  ondra_kod, SUM(mnozstvi) + $0 AS mnozstvi ;
			GROUP BY uzaverka, id_kas, nazev, cen_hlad, platba ;
			FROM (m.gcpri_path + "sk_pri_a") INTO TABLE (m.gcpri_path + "sk_pri")
		SET RELATION TO nazev INTO karty
		REPLACE c_karty WITH karty.c_karty FOR EMPTY(c_karty)
		SET RELATION OFF INTO karty

		************* odpocet z ondrovy recepce ************* BEGIN
		LOCATE FOR "#" $ uzaverka
		IF FOUND ()
			ondruv_export = .T.
		ENDIF
		REPLACE FOR "#" $ uzaverka c_karty WITH ondra_kod        && ondra ma v uzaverce "#" misto ":"
		************* odpocet z ondrovy recepce ************* END

		LOCATE FOR c_karty = 0
		IF FOUND()
			SELECT karty
			SET ORDER TO c_karty
			GO TOP
			nejmensi_c_karty = MIN(karty.c_karty, -100)
			SELECT sk_pri
			SCAN FOR c_karty = 0
				DO zurnal_log WITH "OPERA", "ODP_KAS", "nenalezena karta >" + nazev + "< v uzav. " + uzaverka
				nejmensi_c_karty = nejmensi_c_karty - 1
				REPLACE c_karty WITH m.nejmensi_c_karty
			ENDSCAN
		ENDIF
		SELECT karty
		SET ORDER TO c_karty
		SELECT polozky
		SET RELATION TO c_karty INTO karty
		REPLACE ALL pocet WITH karty.pocet
		SET RELATION TO

		SELECT DISTINCT cen_hlad, platba FROM (m.gcpri_path + "sk_pri") INTO TABLE (m.gcpri_path + "odp_c_hl")
		*    INSERT INTO (m.gcpri_path + "odp_c_hl") (cen_hlad) VALUES ("N")  &&& M.B.  15.5.2005 - Cenova hladina "N" - nakupne ceny z limitov
		SELECT uzaverky
		had.set_max (RECCOUNT())
		SCAN
			SCATTER FIELD uzaverka, id_kas MEMVAR
			had.INCREMENT("kasa " + m.id_kas + " / " + m.uzaverka)

			SELECT food_dat
			LOCATE FOR ID = m.id_kas
			c_partner = food_dat.c_stredisk
			#IF "SLOVAK"$UPPER(GETENV("LANGUAGE"))
				cmd = "dat_dokl =  {^" + LEFT(m.uzaverka,4) + ;
					"-" + SUBST(m.uzaverka, 5,2) + "-" + SUBST(m.uzaverka, 7,2) + "}"
				&cmd
				IF SUBSTR(m.uzaverka, 10, 2) < "10"        && do 10.hod patri uzaverka do vcerejska
					dat_dokl = m.dat_dokl - 1
				ENDIF
			#ELSE
				dat_dokl = DATE(2000 + VAL(STRTRAN(SUBSTR(m.uzaverka, 1,2), "O", "0")), ;
					VAL(SUBSTR(m.uzaverka, 3,2)), VAL(SUBSTR(m.uzaverka, 5,2)))
				IF SUBST(m.uzaverka, 8, 2) < "10"        && do 10.hod patri uzaverka do vcerejska
					dat_dokl = m.dat_dokl - 1
				ENDIF
			#ENDIF

			SELECT odp_c_hl
			* * * * * * * * * nejdriv se dela odpocet storen, aby se naskladnily potrebne karty
*=STRTOFILE(TTOC(DATETIME())+" "+"ODP_KAS zacina samotny odpocet"+CHR(13)+CHR(10),"M:\casy.txt",1)
			SCAN
				SCATTER FIELDS cen_hlad, platba MEMVAR
				SELECT c_karty, SUM(mnozstvi)+0.0000 AS mnozstvi, $0 AS odecteno ;
					GROUP BY c_karty WHERE uzaverka = m.uzaverka AND id_kas = m.id_kas AND cen_hlad = m.cen_hlad AND platba = m.platba AND mnozstvi < 0 ;
					FROM (m.gcpri_path + "sk_pri") INTO TABLE (m.gcpri_path + "sk1")
				DO odpocet_po_kouskach
				SELECT odp_c_hl
			ENDSCAN

			* * * * * * * * * potom kladne polozky, ktere pripadne sezerou suroviny ze storen
			SCAN
				SCATT FIELDS cen_hlad, platba MEMVAR
				SELECT c_karty, SUM(mnozstvi)+0.0000 AS mnozstvi, $0 AS odecteno ;
					GROUP BY c_karty WHERE uzaverka = m.uzaverka AND id_kas = m.id_kas AND cen_hlad = m.cen_hlad AND platba = m.platba AND mnozstvi > 0 ;
					FROM (m.gcpri_path + "sk_pri") INTO TABLE (m.gcpri_path + "sk1")
				DO odpocet_po_kouskach
				SELECT odp_c_hl
			ENDSCAN

			SELECT uzaverky
		ENDSCAN

		SELECT MIN(uzaverka) + " - " + MAX(uzaverka) AS zrc_title FROM(m.gcpri_path + "uzaverky") INTO CURSOR zrc_title
		SCATTER MEMVAR
		DO pozavirej WITH "sk_pri, sk1, sk2, qqq, kalk, a_skl"
		SELECT BUFFER
		REPLACE ALL poradi WITH RECNO()
		WAIT CLEAR
		RELEASE had
*=STRTOFILE(TTOC(DATETIME())+" "+"ODP_KAS Pred ZRC_CREA"+CHR(13)+CHR(10),"M:\casy.txt",1)
		IF zrc_crea(.f.,.f.,m.cdokldo)
			SELECT sk_sez
			SET RELATION TO uzaverka + id_kas INTO uzaverky
			REPLACE FOR id_man == m.gcpgm_id .AND. (! EMPT(uzaverky.odecteno)) odecteno WITH uzaverky.odecteno
		ENDIF
*=STRTOFILE(TTOC(DATETIME())+" "+"ODP_KAS Ukoncene"+CHR(13)+CHR(10),"M:\casy.txt",1)
*=STRTOFILE(CHR(13)+CHR(10),"M:\casy.txt",1)
		DO pozavirej WITH "prep, obraty"
		IF m.dlouhy_log
			SELECT hloubka, c_karty, o_c_karty, nazev, o_nazev, .F. AS z_nazev, .F. AS z_o_nazev, ;
				SUM(hruba) AS hruba, SUM(o_mnozstvi) AS o_mnozstvi GROUP BY 1,2,3 ;
				FROM (m.gcpri_path + "odp_log") INTO TABLE (m.gcpri_path + "odp_log2")

			REPLACE FOR hloubka = 0 o_c_karty WITH c_karty, c_karty WITH 0, o_nazev WITH nazev, nazev WITH "", o_mnozstvi WITH hruba, hruba WITH 0

			SET RELATION TO c_karty INTO karty
			REPLACE ALL nazev WITH karty.nazev
			SET RELATION TO o_c_karty INTO karty
			REPLACE FOR EMPTY(o_nazev) o_nazev WITH karty.nazev

			SELECT DISTINCT nazev ORDE BY nazev WHERE ! EMPTY(nazev) FROM (m.gcpri_path + "odp_log2") INTO TABLE (m.gcpri_path + "odp_nazvy")
			DEFINE POPUP odp_nazvy FROM 1, 34 TO SROWS()-2, 88 PROMPT FIELD nazev FONT (m.gcdefa_font)
			ON SELECTION POPUP odp_nazvy DEACTIVATE POPUP
			ACTIVATE SCREEN
			ACTIVATE POPUP odp_nazvy
			odp_nazev = odp_nazvy.nazev
			DO WHILE !EMPTY(PROMPT())
				SELECT odp_log2
				REPLACE FOR   nazev = m.odp_nazev   nazev WITH UPPER(  nazev), z_nazev   WITH .T.
				REPLACE FOR o_nazev = m.odp_nazev o_nazev WITH UPPER(o_nazev), z_o_nazev WITH .T.
				SELECT odp_nazvy
				ACTIVATE SCREEN
				ACTIVATE POPUP odp_nazvy
				odp_nazev = odp_nazvy.nazev
			ENDDO
			RELEASE POPUP odp_nazvy

			SELECT odp_log2
			INDEX ON STR(hloubka, 3) + LOWER(o_nazev) + LOWER(nazev) TAG poradi
			DO pr_report WITH "odp_log"
		ENDIF
	ENDIF
ENDIF
=n_sunlock("BUF_OUT_"+m.gcpgm_id)
DO clos_data
DO clos_data

*!*****************************************************************************
*!      Procedure: odpocet_po_kouskach
*!*****************************************************************************
PROCEDURE odpocet_po_kouskach
IF ! prazdna()
	INSERT INTO (m.gcpri_path + "odp_log") (hloubka, c_karty, nazev, hruba) ;
		SELECT 0, c_karty, nazev, SUM(mnozstvi) AS mnozstvi GROUP BY c_karty ;
		WHERE uzaverka = m.uzaverka AND id_kas = m.id_kas AND cen_hlad = m.cen_hlad AND platba = m.platba ;
		FROM (m.gcpri_path + "sk_pri")
	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")
	SELECT BUFFER
	APPEND FROM (m.gcpri_path + "sk2")
	REPLACE FOR EMPTY(nazev) nazev WITH m.uzaverka + "  " + m.cen_hlad

	#IF "SLOVAK"$UPPER(GETENV("LANGUAGE"))
		INSERT INTO (m.fn_bufdokl) (operace, c_dokl, datum_dokl, pz1, pz2, c_partner, cen_hlad) ;
			VALUES (m.lcoperace, m.lic_dokl, m.dat_dokl, "K" + m.id_kas, SUBSTR(m.uzaverka,3,12) + "/" + m.id_kas, m.c_partner, m.cen_hlad)
	#ELSE
		INSERT INTO (m.fn_bufdokl) (operace, c_dokl, datum_dokl, pz1, pz2, c_partner, cen_hlad, platba) ;
			VALUES (m.lcoperace, m.lic_dokl, m.dat_dokl, "K" + m.id_kas, m.uzaverka + "/" + m.id_kas, m.c_partner, m.cen_hlad, m.platba)
	#ENDIF
	lic_dokl = m.lic_dokl + 1
ENDIF

SELECT uzaverky
REPLACE odecteno WITH m.datum
