*:*****************************************************************************
*: Procedure file: E:\FOOD500\SKL2\S_TR_HR.PRG
*:*****************************************************************************
dir_export = "l:\fc\food500\export\"
DO mk_dir WITH m.dir_export
STORE SPACE(12) TO datsk1, datsk2

DO clos_data
IF  f_acc('(m.fn_karty) ORDE nazev NOUPDATE') .AND. ;
    f_acc('(m.fn_sk_pol) ALIAS sk NOUPDATE') .AND. ;
    f_acc('(m.fn_sk_fin) ALIAS sk_fin NOUPDATE') .AND. ;
    f_acc('(m.fn_sk_plt) ALIAS sk_plt NOUPDATE') .AND. ;
    f_acc('(m.fn_skupiny) ALIAS skupiny NOUPDATE ORDE druh') .AND. ;
    f_acc('(m.fn_sk_sez) ALIAS sk_sez NOUPDATE')

  sele sk_sez
  index on uzaverka + id_kas to (m.gcpri_path + "uz_uniq") uniq

  IF daj_datsk()
    _had = createobject("had", program())
    _had.show()

    *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
	IF m.slovenskaverzia
	    SELE LEFT(u_cis, 2) AS id_kas, druh_pl, SUM(cena_pl) AS prachy_sl, SUM(tip) AS tip ;
    	     WHERE BETWEEN(uzaverka, LEFT(datsk1, 17), LEFT(datsk2, 17)) ;
        	 GROUP BY 1, 2 FROM (m.fn_sk_plt) INTO TABLE (m.gcpri_path + "f5_paym")
	ELSE
	    SELE LEFT(u_cis, 2) AS id_kas, druh_pl, SUM(cena_pl) AS prachy_sl, SUM(tip) AS tip ;
	   	     WHERE BETWEEN(uzaverka, LEFT(datsk1, 12), LEFT(datsk2, 12)) ;
	       	 GROUP BY 1, 2 FROM (m.fn_sk_plt) INTO TABLE (m.gcpri_path + "f5_paym")
	ENDIF 
    copy all to (m.dir_export + "f5_paym") type fox2x as 852
    DO pozavirej WITH "f5_paym"

    *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
    _had.set_had(44)

	IF m.slovenskaverzia
	    SELE LEFT(u_cis, 2) AS id_kas, SUM(tip) AS sum_tip ;
    	     WHERE BETWEEN(uzaverka, LEFT(datsk1, 17), LEFT(datsk2, 17)) ;
        	 GROUP BY 1 FROM (m.fn_sk_plt) INTO TABLE (m.gcpri_path + "f5_tip")
    ELSE
	    SELE LEFT(u_cis, 2) AS id_kas, SUM(tip) AS sum_tip ;
    	     WHERE BETWEEN(uzaverka, LEFT(datsk1, 12), LEFT(datsk2, 12)) ;
        	 GROUP BY 1 FROM (m.fn_sk_plt) INTO TABLE (m.gcpri_path + "f5_tip")
    ENDIF
    copy all to (m.dir_export + "f5_tip") type fox2x as 852
    DO pozavirej WITH "f5_tip"

    *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
    _had.set_had(66)

    SELECT id_kas, u_cis, cas_atrib, cena_celk, cena_slev, pocet_h, ;
           dan1, dan2, dan3, dan4, trzba_dph1, trzba_dph2, trzba_dph3, trzba_dph4, ;
           00000000.00 AS trzba1, 00000000.00 AS trzba2, 00000000.00 AS trzba3, 00000000.00 AS trzba4 ;
           WHERE BETWEEN(uzaverka + id_kas, datsk1, datsk2) ;
           FROM (m.fn_sk_fin) INTO TABLE (m.gcpri_path + "sk_fi1")
    SCAN
      FOR iii=1 TO 4
        iiii = STR(iii, 1)
        dph_xx = m.dph&iiii
        pole_dbf = "trzba" + iiii
        DO CASE
        CASE dan1 = m.dph_xx
          REPL (pole_dbf) WITH trzba_dph1
        CASE dan2 = m.dph_xx
          REPL (pole_dbf) WITH trzba_dph2
        CASE dan3 = m.dph_xx
          REPL (pole_dbf) WITH trzba_dph3
        CASE dan4 = m.dph_xx
          REPL (pole_dbf) WITH trzba_dph4
        ENDCASE
      ENDFOR
    ENDSCAN

    SELECT id_kas, SUM(cena_celk) AS prachy, SUM(cena_slev) AS prachy_sl, SUM(pocet_h) AS hostu, ;
           SUM(trzba1) AS trzba1, SUM(trzba2) AS trzba2, SUM(trzba3) AS trzba3, SUM(trzba4) AS trzba4 ;
           GROUP BY 1 FROM (m.gcpri_path + "sk_fi1") INTO TABLE (m.gcpri_path + "f5_vat")
    copy all to (m.dir_export + "f5_vat") type fox2x as 852

    *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
    _had.set_had(88)

    SELECT id_kas, cas_atrib, SUM(cena_celk) AS prachy, SUM(cena_slev) AS prachy_sl, SUM(pocet_h) AS hostu ;
           GROUP BY 1,2 ;
           FROM (m.gcpri_path + "sk_fi1") INTO TABLE (m.gcpri_path + "f5_cover")
    copy all to (m.dir_export + "f5_cover") type fox2x as 852

    DO pozavirej WITH "sk_fi1, f5_vat, f5_cover"

    *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
    _had.set_had(95)

    SELECT id_kas, u_cis, cas_atrib, SUM(cena_celk) AS prachy, SUM(cena_slev) AS prachy_sl, ;
           000.0000000 AS slev_koef GROUP BY 1, 2 FROM (m.gcpri_path + "sk_fi1") INTO TABLE (m.gcpri_path + "sl_koef")
    REPL ALL slev_koef WITH IIF(prachy # 0, prachy_sl / prachy, 1)
    INDEX ON id_kas + u_cis TAG kas_ucet

    SELECT id_kas, uzaverka, u_cis, nazev, " " AS spart, prachy, 0000000.00 AS prachy_sl, " " AS cas_atrib ;
           WHERE BETWEEN(uzaverka + id_kas, datsk1, datsk2) AND !EMPT(k_odpoctu) ;
           FROM (m.fn_sk_pol) INTO TABLE (m.gcpri_path + "pol1")

    sele karty
    set rela to druh_kalk into skupiny addi
    sele pol1
    SET RELA TO nazev INTO karty
    REPL ALL spart WITH skupiny.spart
    use in skupiny
    SET RELA TO id_kas + u_cis INTO sl_koef
    REPL ALL prachy_sl WITH prachy * sl_koef.slev_koef, cas_atrib WITH sl_koef.cas_atrib
    SET RELA OFF INTO sl_koef

    SELE id_kas, spart, cas_atrib, SUM(prachy) AS prachy, SUM(prachy_sl) AS prachy_sl ;
         GROUP BY 1,2,3 FROM (m.gcpri_path + "pol1") INTO TABLE (m.gcpri_path + "f5_sp_ta")
    copy all to (m.dir_export + "f5_sp_ta") type fox2x as 852

  ENDIF
ELSE
  WAIT jaz("try-later") WINDOW TIME 3
ENDIF
DO clos_data
release  _had


*!*****************************************************************************
*!      Procedure: DAJ_DATSK
*!*****************************************************************************
PROCEDURE daj_datsk
PRIVATE ttl, ret
ret = .F.
SELE sk_sez
IF prazdna(.T.)
	IF m.slovenskaverzia
		STORE SPACE(17) TO datsk1, datsk2
	ELSE
		STORE SPACE(12) TO datsk1, datsk2
	ENDIF
ELSE
  SET ORDE TO uz_uniq
  ttl = " Pocatek "
	IF m.slovenskaverzia
		DEFI POPUP datumy FROM 1,58 TO MIN(SROW()-2, 2+RECC()),79 PROMPT FIELD uzaverka + "³" + id_kas TITLE m.ttl
	ELSE
		DEFI POPUP datumy FROM 1,63 TO MIN(SROW()-2, 2+RECC()),79 PROMPT FIELD uzaverka + "³" + id_kas TITLE m.ttl
	ENDIF
  ON SELE POPUP datumy DEACTI POPUP
  ACTI POPUP datumy
  IF ! EMPTY(PROMPT())
    datsk1 = uzaverka + id_kas
    SET FILTER TO uzaverka + id_kas >= m.datsk1
    ttl = " Konec "
*    DEFI POPUP datumy FROM 1,63 TO MIN(SROW()-2, 2+RECC()),79 PROMPT FIELD uzaverka + "³" + id_kas TITLE m.ttl
 	IF m.slovenskaverzia
		DEFI POPUP datumy FROM 1,58 TO MIN(SROW()-2, 2+RECC()),79 PROMPT FIELD uzaverka + "³" + id_kas TITLE m.ttl
	ELSE
		DEFI POPUP datumy FROM 1,63 TO MIN(SROW()-2, 2+RECC()),79 PROMPT FIELD uzaverka + "³" + id_kas TITLE m.ttl
	ENDIF
   ON SELE POPUP datumy DEACTI POPUP
    ACTI POPUP datumy
    IF ! EMPTY(PROMPT())
      ret = .T.
      datsk2 = uzaverka + id_kas
    ENDIF
  ENDIF
  SET ORDE TO
ENDIF
RETURN ret
