
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
proc vyberzurnal
do clos_data
crea table (m.gcpri_path + "seznam_zurnalu") free (nazev C(50), zurnal C(55), odkud C(3))
USE (m.gcpri_path + "seznam_zurnalu") EXCLUSIVE
INDEX ON zurnal TAG zurnal UNIQUE 
IF m.issql
	DO open_database
	USE (m.sqlprefix+"rfooddat") IN 0 ALIAS food_dat
ELSE 
    USE (m.fn_food_dat) ALIAS food_dat NOUPDATE again 
ENDIF 

sele disti dat_path as kas_path, "Receptury a sklady" as id_zkratka, pgm, "MAN" as odkud where upper(pgm)       like "%MAN%"    from food_dat into table (m.gcpri_path + "master_kasy")
sele disti dat_path as kas_path,                         id_zkratka, pgm, "KAS" as odkud where upper(pgm_param) like "%MASTER%" from food_dat into table (m.gcpri_path + "zur_kas")
APPEND FROM (m.gcpri_path + "master_kasy")
scan
  scatt memvar
  kas_path = allt(kas_path)
  pocet = adir(zurnaly, m.kas_path + "zu????.txt")
  for iii = 1 to pocet
    insert into (m.gcpri_path + "seznam_zurnalu") (odkud, zurnal, nazev) values (m.odkud, m.kas_path + zurnaly(iii, 1), ;
           id_zkratka + mesic(subst(zurnaly(iii, 1), 5,2)) + " 20" + subst(zurnaly(iii, 1), 3,2) + str(zurnaly(iii, 2), 12) )
  next
endscan
sele seznam_zurnalu
*index on zurnal tag zurnal uniq
if ! prazdna(.t.)
  set confirm on
  defi popup zurnaly from 1, 55 prompt field nazev font "Courier New"
  on Selection popu zurnaly deacti popu
  ACTIVATE SCREEN 
  acti popup zurnaly
  if ! empt(prompt())
     gczurnal_vybran = seznam_zurnalu.odkud
     do crea_zurnal with allt(seznam_zurnalu.zurnal)
     do clos_data
  endif
  rele popu zurnaly
endif

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
proc view_zurnal
do clos_data
IF FILE(m.gcpri_path + "zurnal.dbf")
	TRY 
		use (m.gcpri_path + "zurnal") Alias zurnal
		set filter to (vyznam = "OPERA") .and. (pozn # "REC")
		do pr_report with "zur_all"
	CATCH
		=MESSAGEBOX(jaz("form_zurnal_je_nedostupny"),64,"FoodMan")
	ENDTRY 	
ELSE
	=MESSAGEBOX(jaz("form_zurnaly_cz_nevytvoreny"),64,"FoodMan")
ENDIF 
do clos_data

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
proc view_zurnal_inv
do clos_data
IF FILE(m.gcpri_path + "zurnal.dbf")
	TRY 
		use (m.gcpri_path + "zurnal") Alias zurnal
		set filter to (vyznam = "OPERA") .and. (pozn = "INVENTURA")
		do pr_report with "zur_all"
	CATCH
		=MESSAGEBOX(jaz("form_zurnal_je_nedostupny"),64,"FoodMan")
	ENDTRY 	
ELSE
	=MESSAGEBOX(jaz("form_zurnaly_cz_nevytvoreny"),64,"FoodMan")
ENDIF 
do clos_data

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
proc view_chgrecept
do clos_data
IF FILE(m.gcpri_path + "zurnal.dbf")
	IF m.issql
		DO open_database
		SELECT 0
		USE (m.sqlprefix+"rkarty") IN 0 ALIAS karty
		INDEX on c_karty TAG c_karty
	ELSE 
		SELECT 0
		use (m.fn_karty) Order c_karty Alias karty
	ENDIF 
	sele disti left(c1, 9) + left(c2,5) as cas, c3, c4, n1, n2, n3, pozn ;
	     where (vyznam = "OPERA" and pozn = "REC") OR (pozn = "KARTY") ;
	     from (m.gcpri_path + "zurnal") into table (m.gcpri_path + "zurnal1")
	sele cas, c3, c4, n1, n2, n3, pozn, 00000000 as c_karty, space(30) as nazev, space(30) as naz_poloz, space(30) as naz_poloz2, space(10) as txt, $0 as cena_prum ;
	     from (m.gcpri_path + "zurnal1") into table (m.gcpri_path + "zurnal2")

	repl all c_karty with val(c3)
	set Relation To c_karty into karty
	repl FOR pozn = "REC" nazev with karty.nazev, cena_prum WITH karty.cena_prum
	set Relation To n1 into karty
	repl all naz_poloz with karty.nazev
	set Relation To n3 into karty
	repl all naz_poloz2 with karty.nazev
	repl for "poloz" $ c3 txt        with allt(str(n2,10,4))
	repl for "spotr" $ c3 .and. n1 < n2 naz_poloz with allt(str(n1,10,4)) + " -// " + allt(str(n2,10,4))
	repl for "spotr" $ c3 .and. n1 > n2 naz_poloz with allt(str(n1,10,4)) + " -\\ " + allt(str(n2,10,4))

	repl for "poct"  $ c3 naz_poloz with allt(str(n1,10,4)) + " --> " + allt(str(n2,10,4))
	repl FOR pozn = "REC" c3 with substr(c3, at(" ", c3))
	repl ALL c3 with left(c3, 22)
	do pr_report with "zu_chrec"
	INDEX on nazev + cas TO qwe
	do pr_report with "zu_chrec"
	use
ELSE
	=MESSAGEBOX(jaz("form_zurnaly_cz_nevytvoreny"),64,"FoodMan")
ENDIF 
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
proc view_suplik
do clos_data
IF FILE(m.gcpri_path + "zurnal.dbf")
	use (m.gcpri_path + "zurnal") Alias zurnal
	set filter to (vyznam = "SUPLE")
	REPLACE ALL n2 WITH INT(n1/60), n3 WITH MOD(n1, 60)
	do pr_report with "zu_suplik"
	do clos_data
ELSE
	=MESSAGEBOX(jaz("form_zurnaly_cz_nevytvoreny"),64,"FoodMan")
ENDIF 

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
proc view_starty
do clos_data
IF FILE(m.gcpri_path + "starty.dbf")
	use (m.gcpri_path + "starty") exclusive
	index on c3 + cas tag c3_cas
	bacha = ""
	scan
	   if vyznam = "START"
	      gather field bacha memvar
	      bacha = "*"
	   else
	      bacha = " "
	   endif
	endscan
	count for bacha  = "*"     to pocet_vypadku
	count for vyznam = "START" to pocet_startu
	set order to cas
	do pr_report with "zu_start"

	SET FILTER TO bacha = "*"
	do pr_report with "zu_start"

	do clos_data
ELSE
	=MESSAGEBOX(jaz("form_zurnaly_cz_nevytvoreny"),64,"FoodMan")
ENDIF 

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
proc view_hodiny
IF FILE(m.gcpri_path + "ucty.dbf")
	crea table  (m.gcpri_path + "hodiny") free (nazev c(30), mxx Y, nejvic c(2), ;
	     m00 Y, m01 Y, m02 Y, m03 Y, m04 Y, m05 Y, m06 Y, m07 Y, m08 Y, m09 Y, ;
	     m10 Y, m11 Y, m12 Y, m13 Y, m14 Y, m15 Y, m16 Y, m17 Y, m18 Y, m19 Y, ;
	     m20 Y, m21 Y, m22 Y, m23 Y, m24 Y, ;
	     z00 L, z01 L, z02 L, z03 L, z04 L, z05 L, z06 L, z07 L, z08 L, z09 L, ;
	     z10 L, z11 L, z12 L, z13 L, z14 L, z15 L, z16 L, z17 L, z18 L, z19 L, ;
	     z20 L, z21 L, z22 L, z23 L, z24 L )

	sele co1 as nazev, subst(cas, 10, 2) as hodina, sum(mnozstvi) as mnozstvi ;
	     where left(vyznam, 1)= "O" group by 1, 2 from (m.gcpri_path + "ucty") into table (m.gcpri_path + "hodiny1")
	index on nazev + str(1000000 - mnozstvi) tag n_m
	o_nazev = "!@#$#@$!%^&*&^%$%^)^*$&#"
	o_mnozstvi = -9999.99

	scan
	  scatt memvar
	  sele hodiny
	  locate for nazev = m.nazev
	  if found()
	     xxx = "m" + m.hodina
	     repl &xxx with &xxx + m.mnozstvi
	  else
	     insert into (m.gcpri_path + "hodiny") (nazev, ("m" + m.hodina)) ;
	            values (m.nazev, m.mnozstvi)
	  endif

	  if nazev # m.o_nazev
	     xxx = "z" + m.hodina
	     repl &xxx with .t.
	     o_nazev = m.nazev
	     o_mnozstvi = m.mnozstvi
	  else
	     if mnozstvi > m.o_mnozstvi * 0.8
	        xxx = "z" + m.hodina
	        repl &xxx with .t.
	     endif
	  endif
	endscan
	sele hodiny
	repl all m00 with m00 + m24, mxx with ;
	         m00 + m01 + m02 + m03 + m04 + m05 + m06 + m07 + m08 + m09 ;
	       + m10 + m11 + m12 + m13 + m14 + m15 + m16 + m17 + m18 + m19 ;
	       + m20 + m21 + m22 + m23
	repo_txt = "OBJEDNÁVKY PO HODINÁCH"

	DO pr_report WITH "zu_hod_a"

	repl for mxx # 0 ;
	     m00 with round(100 * m00 / mxx, 0), m01 with round(100 * m01 / mxx, 0), m02 with round(100 * m02 / mxx, 0), m03 with round(100 * m03 / mxx, 0), m04 with round(100 * m04 / mxx, 0), ;
	     m05 with round(100 * m05 / mxx, 0), m06 with round(100 * m06 / mxx, 0), m07 with round(100 * m07 / mxx, 0), m08 with round(100 * m08 / mxx, 0), m09 with round(100 * m09 / mxx, 0), ;
	     m10 with round(100 * m10 / mxx, 0), m11 with round(100 * m11 / mxx, 0), m12 with round(100 * m12 / mxx, 0), m13 with round(100 * m13 / mxx, 0), m14 with round(100 * m14 / mxx, 0), ;
	     m15 with round(100 * m15 / mxx, 0), m16 with round(100 * m16 / mxx, 0), m17 with round(100 * m17 / mxx, 0), m18 with round(100 * m18 / mxx, 0), m19 with round(100 * m19 / mxx, 0), ;
	     m20 with round(100 * m20 / mxx, 0), m21 with round(100 * m21 / mxx, 0), m22 with round(100 * m22 / mxx, 0), m23 with round(100 * m23 / mxx, 0)

	repo_txt = "OBJEDNÁVKY PO HODINÁCH V PROCENTECH"
	do pr_report with "zu_hod_a"

	do pozavirej with "hodiny, hodiny1"
ELSE
	=MESSAGEBOX(jaz("form_zurnaly_cz_nevytvoreny"),64,"FoodMan")
ENDIF 

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
proc view_stoly
do clos_data
IF FILE(m.gcpri_path + "ucty.dbf")
	use (m.gcpri_path + "ucty")
	sele disti int(stul) as stul from (m.gcpri_path + "ucty") where vyznam = "OBJEDNAV" ;
	     order by 1 into table (m.gcpri_path + "stoly")
	set confirm on
	DEFINE POPUP stul FROM 1,50 PROMPT FIELD str(stul, 7) title "od stolu"  font "Courier New"
	ON SELE POPUP stul DEACTIVATE POPUP
	ACTIVATE SCREEN 
	ACTIVATE POPUP stul
	if ! empty(prompt())
	   stul1 = stoly.stul
	   set filter to stul >= m.stul1
	   set confirm on
	   DEFINE POPUP stul FROM 1,50 PROMPT FIELD str(stul, 7) title "do stolu" font "Courier New"
	   ON SELE POPUP stul DEACTIVATE POPUP
	   ACTIVATE SCREEN 
	   ACTIVATE POPUP stul
	   if ! empty(prompt())
	      stul2 = stoly.stul + 0.99
      select stul, co1, sum(mnozstvi) as mnozstvi, sum(mnozstvi*cena) as cena, "_OBJE_" as vyznam, stol, miestnost, ;
	             " " as u_cis, " " as cas, " " as kdo, " " as co2, " " as cen_hlad, 1 as dph, 1 as koef_slev, " " as pozn, " " as pozn_hl ;
	             group by stul, co1 where between(stul, m.stul1, m.stul2) and vyznam = "OBJEDNAV" ;
	             from (m.gcpri_path + "ucty") into table (m.gcpri_path + "totstoly")
	      replace for mnozstvi # 0 cena with cena / mnozstvi             
	      repo_txt = "O B J E D N Á V K Y  C E L K E M  na stolech " + str(m.stul1, 6,1) + " az " + str(m.stul2, 6,1)
	      do pr_report with "zu_ucty"
	      gctxt_summary = "SUMMARY"
	      do pr_report with "zu_ucty"
	      gctxt_summary = ""
	   endif
	endif
	do clos_data
ELSE
	=MESSAGEBOX(jaz("form_zurnaly_cz_nevytvoreny"),64,"FoodMan")
ENDIF 

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
proc view_stul
do clos_data
IF FILE(m.gcpri_path + "ucty.dbf")
	use (m.gcpri_path + "ucty")
	sele stul from (m.gcpri_path + "ucty") ;
	     union ;
	     sele val(subst(pozn_hl, 2)) as stul from (m.gcpri_path + "ucty") ;
	     order by 1 into table (m.gcpri_path + "stoly")
	DELETE FOR stul < 0.1 .or. stul > 99999

	set confirm on
	defi popup stoly from 1, 55 prompt field stul font "Courier New"
	on Selection popu stoly deacti popu
	ACTIVATE SCREEN 
	acti popup stoly
	do while ! empt(prompt())
	   stul = stoly.stul
	**   sele ucty
	**   set Filter To stul = m.stul .or. (vyznam = "O PREVOD" .and. val(subst(pozn_hl, 2)) = m.stul)

	   sele * from (m.gcpri_path + "ucty") into table (m.gcpri_path + "jedenstul") ;
	        where stul = m.stul or (vyznam = "O PREVOD" and val(subst(pozn_hl, 2)) = m.stul)
	   REPLACE for stul # m.stul mnozstvi WITH - mnozstvi

	   repo_txt = "O P E R A C E  na stole " + str(stoly.stul, 6,1)
	   do pr_report with "zu_ucty"
	   sele stoly
	   ACTIVATE SCREEN 
	   acti popup stoly
	enddo
	do clos_data
ELSE
	=MESSAGEBOX(jaz("form_zurnaly_cz_nevytvoreny"),64,"FoodMan")
ENDIF 


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
proc view_karta
do clos_data
IF FILE(m.gcpri_path + "ucty.dbf")
	use (m.gcpri_path + "ucty")
	sele disti co1 from (m.gcpri_path + "ucty") ;
	     order by 1 into table (m.gcpri_path + "stoly")
	dele for empt(left(co1, 15))
	set confirm on
	defi popup stoly from 1, 55 prompt field co1 font "Courier New"
	on Selection popu stoly deacti popu
	ACTIVATE SCREEN 
	acti popup stoly
	do while ! empt(prompt())
	   m.co1 = stoly.co1
	   sele ucty
	   set Filter To co1 = m.co1
	   repo_txt = "operace na kartě  " + uppe(m.co1)
	   do pr_report with "zu_karty"
	   sele stoly
	   ACTIVATE SCREEN 
	   acti popup stoly
	enddo
	do clos_data
ELSE
	=MESSAGEBOX(jaz("form_zurnaly_cz_nevytvoreny"),64,"FoodMan")
ENDIF 

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
proc view_ucty_a_objed
do clos_data
IF FILE(m.gcpri_path + "ucty.dbf")
	use (m.gcpri_path + "ucty")
	repo_txt = "O B J E D N Á V K Y  A  Ú Č T Y"
	do pr_report with "zu_ucty"
	do clos_data
ELSE
	=MESSAGEBOX(jaz("form_zurnaly_cz_nevytvoreny"),64,"FoodMan")
ENDIF 

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
proc view_ucty
do clos_data
IF FILE(m.gcpri_path + "ucty.dbf")
	use (m.gcpri_path + "ucty")
	set filt to left(vyznam, 1) = "U"
	repo_txt = "V Y S T A V E N É  Ú Č T Y"
	do pr_report with "zu_ucty"

	set filt to left(vyznam, 2) = "US" .and. left(pozn_hl, 1) # "Z"
	if prazdna()
	    insert into (m.gcpri_path + "ucty") (vyznam, pozn_hl) values ("US", " -* zadna  *-")
	endif
	repo_txt = "S T O R N A  Ú Č T U"
	do pr_report with "zu_ucty"
	set filter to
	delete for pozn_hl = " -* zadna  *-"
	do clos_data
ELSE
	=MESSAGEBOX(jaz("form_zurnaly_cz_nevytvoreny"),64,"FoodMan")
ENDIF 

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
proc view_objednavky
do clos_data
IF FILE(m.gcpri_path + "ucty.dbf")
	use (m.gcpri_path + "ucty")
	set Filter To left(vyznam, 1) = "O"
	repo_txt = "Objednávky komplet"
	do pr_report with "zu_objed"
	do clos_data
ELSE
	=MESSAGEBOX(jaz("form_zurnaly_cz_nevytvoreny"),64,"FoodMan")
ENDIF 

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
proc view_prevody_a_storna
do clos_data
IF FILE(m.gcpri_path + "ucty.dbf")
	use (m.gcpri_path + "ucty")
	set Filter To vyznam = "O STORNO"
	if prazdna()
	    insert into (m.gcpri_path + "ucty") (vyznam, pozn_hl) values ("O STORNO", " -* zadna  *-")
	endif
	repo_txt = "Storna objednávek"
	do pr_report with "zu_objed"

	set Filter To vyznam = "O PREVOD"
	if prazdna()
	    insert into (m.gcpri_path + "ucty") (vyznam, pozn_hl) values ("O PREVOD", " -* zadna  *-")
	endif
	repo_txt = "Převody mezi stoly"
	do pr_report with "zu_objed"

	set Filter To INLIST(vyznam, "ESCAPE", "CLEAR", "ESC+CLR")
	if prazdna()
	    insert into (m.gcpri_path + "ucty") (vyznam, pozn_hl) values ("ESC+CLR", " -* zadna  *-")
	endif
	repo_txt = "Escape a zrus.pred objed."
	do pr_report with "zu_objed"

	INDEX ON kdo + cas + u_cis TO (m.gcpri_path + "zu_obj_esc")
	DO pr_report WITH "zu_objed2"

	set filter to
	delete for pozn_hl = " -* zadna  *-"
	do clos_data
ELSE
	=MESSAGEBOX(jaz("form_zurnaly_cz_nevytvoreny"),64,"FoodMan")
ENDIF 


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
PROCEDURE view_cisnici
LOCAL pocetkas
DO clos_data
IF FILE(m.gcpri_path + "ucty.dbf") AND FILE(m.gcpri_path + "zurnal.dbf")
	USE (m.gcpri_path + "ucty") IN 0
	SELECT disti LEFT(u_cis,2) as id_kas FROM ucty INTO CURSOR kasy WHERE LEFT(vyznam, 1) = "O" and vyznam # "O PREVOD"
	m.pocetkas=RECCOUNT("kasy")
	USE IN kasy

	SELECT kdo, co1, SUM(mnozstvi) AS mnozstvi, SUM(mnozstvi*cena) AS prachy, SUM(mnozstvi*cena/dph) AS vynos ;
	     WHERE LEFT(vyznam, 1) = "O" and vyznam # "O PREVOD" GROUP BY 1,2 ;
	     FROM ucty INTO TABLE (m.gcpri_path + "cisnici")
	DO pr_report WITH "zu_cisn"

	IF m.pocetkas>1
		sele kdo, co1, LEFT(u_cis,2) as id_kas, sum(mnozstvi) as mnozstvi, sum(mnozstvi*cena) as prachy, sum(mnozstvi*cena/dph) as vynos ;
		     where left(vyznam, 1) = "O" and vyznam # "O PREVOD" group by 1,2,3 ;
		     from ucty into table (m.gcpri_path + "cisnici_kas")
		INDEX on id_kas+kdo TAG aaa
		do pr_report with "zu_cisn_kas"
	ENDIF 



	sele kdo, " " as co1, sum(mnozstvi) as mnozstvi, sum(prachy) as prachy, sum(vynos) as vynos ;
	     group by 1 from cisnici into table (m.gcpri_path + "cisnici_sum")
	do pr_report with "zu_cisn"
	IF m.pocetkas>1
		sele kdo, " " as co1, id_kas, sum(mnozstvi) as mnozstvi, sum(prachy) as prachy, sum(vynos) as vynos ;
	    	 group by kdo, id_kas from cisnici_kas into table (m.gcpri_path + "cisnici_kas_sum")
		INDEX on id_kas+kdo TAG aaa
		do pr_report with "zu_cisn_kas"
	ENDIF 

	sele kdo, cas + u_cis as cas, sum(abs(mnozstvi*cena)) as prachy ;
	     where vyznam == "O PREVOD" group by 1,2 ;
	     from (m.gcpri_path + "ucty") into table (m.gcpri_path + "q2")
	sele kdo, count(kdo) as poc_prevo, sum(prachy) as pr_prevo ;
	     group by 1 from (m.gcpri_path + "q2") into table (m.gcpri_path + "q3")

	sele kdo, cas + u_cis as cas, sum(abs(mnozstvi*cena)) as prachy ;
	     where vyznam == "O STORNO" group by 1,2 ;
	     from (m.gcpri_path + "ucty") into table (m.gcpri_path + "q2")
	sele kdo, count(kdo) as poc_stor, sum(prachy) as pr_stor, 00000 as poc_prevo, 000000.00 as pr_prevo ;
	     group by 1 from (m.gcpri_path + "q2") into table (m.gcpri_path + "q4")

	appe from (m.gcpri_path + "q3")

	select c4 as kdo, c3, n2, c2 from (m.gcpri_path + "zurnal") where vyznam = "UCHLA" and "Kopie uctu" $ pozn into table (m.gcpri_path + "kopie")

	sele kdo, sum(poc_stor) as poc_stor, sum(pr_stor) as pr_stor, sum(poc_prevo) as poc_prevo, sum(pr_prevo) as pr_prevo, ;
	     space(7) as c3, 0000000.00 as n2, space(5) as c2 ;
	     group by 1 from (m.gcpri_path + "q4") into table (m.gcpri_path + "q2")
	append from (m.gcpri_path + "kopie")
	index on kdo tag kdo
	do pr_report with "zu_stpre"
	do clos_data
ELSE
	=MESSAGEBOX(jaz("form_zurnaly_cz_nevytvoreny"),64,"FoodMan")
ENDIF 


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
proc crea_zurnal
param zurnal

zur_had = createobject("had")
zur_had.show()
IF m.issql
	DO open_database
	SELECT 0
	USE (m.sqlprefix+"rusers") alias uzivatele
	INDEX on user_id TAG user_id
ELSE 
	use (m.gchom_path + "users") Order user_id Alias uzivatele IN 0
ENDIF 

o_set_point = set("point")
set Point To "."
sele 0
IF m.gczurnal_vybran = "KAS"
    crea table (m.gcpri_path + "zurnal") free (vyznam c(5), c1 c(15), c2 c(30), c3 c(55), c4 c(30), n1 n(12,4), n2 n(12,4), n3 n(12,4), pozn c(33)) 
    appe from (m.zurnal) type delim
ELSE 
    crea table (m.gcpri_path + "zurnal") free (vyznam c(5), c1 c(15),           c3 c(55), c4 c(30), n1 n(12,4), n2 n(12,4), n3 n(12,4), pozn c(33))
    appe from (m.zurnal) type delim
    ALTER table (m.gcpri_path + "zurnal") ADD c2 C(5)
    REPLACE ALL c2 WITH SUBSTR(c1, 10), c1 WITH LEFT(c1, 8)
    IF "D_TELEVIZE" $ UPPE(m.gchom_path)
        REPLACE ALL pozn WITH STRTRAN(pozn, "michaelamac", "hhmm-mac")
    ENDIF 

ENDIF 
set point to (m.o_set_point)
zur_had.set_max(recc() * .4)
scan for inlist(vyznam, "START", "PRERU", "UZAVE") .or. "HLA" $ vyznam
   sele uzivatele
   locate for user_id = left(zurnal.c4, 3)
   if found()
      sele zurnal
      repl c4 with uzivatele.username
   else
      sele zurnal
   endif
endscan

crea table (m.gcpri_path + "hlavicky") free (vyznam c(5), cas c(14), u_cis c(7), kdo c(15), stul n(6,1), stul2 n(6,1), cena_slev n(10,2), cena_celk n(10,2), pozn_hl c(33)) 
appe blank
gather memvar
zap
zur_had.set_had(5)

*****************************         STARTY          *****************************
sele vyznam, left(c1, 9) + left(c2, 5) as cas, c3, c4 as kdo, pozn, " " as bacha ;
     where inlist(vyznam, "START", "PRERU", "UZAVE") ;
     from (m.gcpri_path + "zurnal") into table (m.gcpri_path + "starty")
index on cas tag cas
GO TOP 
zacatek_casu = LEFT(cas,6)
insert into (m.gcpri_path + "starty") (vyznam, cas) values ("ZACAT", m.zacatek_casu + "01 00:00")
insert into (m.gcpri_path + "starty") (vyznam, cas) values ("KONEC", m.zacatek_casu + "31 24:00")
zur_cas_od = "00000000 00:00"
zur_cas_do = "99999999 99:99"
zur_had.hide()
acti scree
set filt to vyznam # "UZAVE"
set confirm on
defi popup starty from 1, 77 prompt field vyznam + " | " + cas + " | " + c3 font "Courier New" title " Od "
on Selection popu starty deacti popu
ACTIVATE SCREEN 
acti popup starty
if ! empt(prompt())
  zur_cas_od = starty.cas
  set Filter To cas >= m.zur_cas_od .and. vyznam # "START"
  defi popup starty from 1, 77 prompt field vyznam + " | " + cas + " | " + c3 font "Courier New" title " Do "
  on Selection popu starty deacti popu
  ACTIVATE SCREEN 
  acti popup starty
  if ! empt(prompt())
    zur_cas_do = starty.cas
  endif
endif
rele popu starty
set filter to
dele for inlist(vyznam, "ZACAT", "KONEC")
delete for (cas < m.zur_cas_od) .or. (cas > m.zur_cas_do)
zur_had.show()

gczurnal_info = right(strtr(upper(m.zurnal), ".TXT", ""), 9) + "    " + m.zur_cas_od + " - " + m.zur_cas_do

if gczurnal_vybran = "KAS"

*****************************   OBJEDNAVKY  &  PREVODY  &  UCTY  *****************************
  zur_had.set_had(11)
  sele 0
  crea table (m.gcpri_path + "ucty") free;
       (vyznam c(8), u_cis c(7), cas c(14), kdo c(15), stul n(6,1), co1 c(30), co2 c(13), stol C(6), miestnost C(11), ;
       cen_hlad c(1), mnozstvi n(12,3), cena n(10,2), dph n(5,3), koef_slev n(10,7), pozn c(30), pozn_hl C(33))

  sele zurnal

  go top
  if m.zur_cas_od # m.zacatek_casu + "01 00:00"
     loca for left(c1,9) + left(c2, 5) = m.zur_cas_od
  endif

  copy next 99999999 for inlist(left(vyznam, 2), "OB", "OS", "PR", "UC", "US", "UP", "CL", "ES", "PI") to (m.gcpri_path + "polozky")
  sele 0
  use (m.gcpri_path + "polozky")
  scan
    scatt memvar
    if "HLA" $ vyznam
      do uloz_hlavicku

    else                        && polozky
      u_cis    = left(c1, 7)
      co1      = c2
      co2      = c3
      cen_hlad = c4
      mnozstvi = n1
*      cena     = abs(n2)
      cena     = n2
      dph      = n3
      sele hlavicky
      locate for u_cis = m.u_cis .and. LEFT(vyznam, 2) =  LEFT(m.vyznam, 2)
      scatt fields kdo, cas, stul, stul2, cena_slev, cena_celk, pozn_hl memvar
      koef_slev = iif(m.cena_slev = 0, 0.0000001, m.cena_celk / m.cena_slev)
      if between(m.cas, zur_cas_od, zur_cas_do)
         if left(vyznam, 2) = "PR"
           pozn_hl = "z " + str(stul2, 4,1) + " " + m.pozn_hl
         endif

         if left(vyznam, 2) = "OS"
              cena = ABS(cena)
         endif
         insert into (m.gcpri_path + "ucty") from memvar
      endif
    endif
  endscan
  sele ucty
  repl for left(vyznam, 2) = "PR"    vyznam with "O PREVOD"
  repl for left(vyznam, 2) = "OB"    vyznam with "OBJEDNAV"
  repl for left(vyznam, 2) = "OS"    vyznam with "O STORNO"
  repl for left(vyznam, 5) = "UCPOL" vyznam with "UCET"
  repl for left(vyznam, 5) = "USPOL" vyznam with "USTORNO"
  repl for left(vyznam, 2) = "UP"    vyznam with "PREDUCET"
  repl for left(vyznam, 2) = "ES"    vyznam with "ESCAPE"
  repl for left(vyznam, 2) = "CL"    vyznam with "CLEAR"
  repl for left(vyznam, 2) = "PI"    vyznam with "PRIL.MAZ"
  repl for "PLT" $ vyznam            vyznam with left(vyznam, 2) + "PLATBA", dph with 1, co1 with space(20) + upper(co1), mnozstvi with 0
  repl for vyznam = "UCTIP" co1 with "TIP " + co1, pozn with "Tip:" + str(dph, 10,2), mnozstvi with 0, cena with 0, dph with 1
  inde on cas tag cas
else

   SELECT 0
	IF m.issql
		USE (m.sqlprefix+"rpartneri") ALIAS partneri
		INDEX on c_partner TAG c_partner
	ELSE 
	   USE (m.fn_partneri) ALIAS partneri ORDER c_partner
	ENDIF 

   sele zurnal
   c1_start = "00001111"
   scan
      if inlist(vyznam, "START", "UZAVE")
           c1_start = zurnal.c1
      else
           replace c1 with m.c1_start
      endif
   endscan

   dele for ! between(left(c1, 9) + left(c2, 5), zur_cas_od, zur_cas_do)
   pack
   REPLACE ALL c1 WITH SUBSTR(c1, 5, 5) + c2
   SET RELATION TO n1 INTO partneri
   REPLACE FOR pozn = "INVENTURA" c2 WITH partneri.partner
   USE IN partneri
endif
release zur_had


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
proc uloz_hlavicku
cas       = left(c1, 9) + left(c2, 5)
u_cis     = left(c3, 7)
kdo       = c4
stul      = n1
cena_slev = n2
stul2     = n2
cena_celk = n3
pozn_hl   = pozn

sele hlavicky
locate for u_cis = m.u_cis .and. vyznam = m.vyznam
if found()
	TRY 
		gath memvar
	CATCH
	ENDTRY 
else
	TRY 
		insert into (m.gcpri_path + "hlavicky") from memvar
	CATCH
	ENDTRY 
endif
zur_had.increment()


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
proc mesic
param mes
do case
case mes = "01"
  retu PADR(jaz("januar"),10)
case mes = "02"
  retu PADR(jaz("februar"),10)
case mes = "03"
  retu PADR(jaz("marec"),10)
case mes = "04"
  retu PADR(jaz("april"),10)
case mes = "05"
  retu PADR(jaz("maj"),10)
case mes = "06"
  retu PADR(jaz("jun"),10)
case mes = "07"
  retu PADR(jaz("jul"),10)
case mes = "08"
  retu PADR(jaz("august"),10)
case mes = "09"
  retu PADR(jaz("september"),10)
case mes = "10"
  retu PADR(jaz("oktober"),10)
case mes = "11"
  retu PADR(jaz("november"),10)
case mes = "12"
  retu PADR(jaz("december"),10)
other
  retu "XXX " + m.mes
endcase
