*
*   zmensi archivni sk* kumulaci uctu se stejnymi parametry
*
*   zacne prohledanim sk_fin, kde oznaci storna a cisla uctu, na nez storna ukazuji (tech sice muze byt vic, ale to neni na zavadu, proste nebudou kumulovany)
*   pak scanuje sk_fin a oznacuje ucty se stejnymi parametry (id_kas, id_slave, autor, uzaverka, cas_atrib, mutace) cislem uctu prvniho v kazde takove skupine
*   tato cisla dotahne do ostatnich eskacek a selectem poscita
*


RETURN    && ani v globu se nepousti automaticky



IF ATC( "D_GLOBUS", m.gchom_path) + ATC( "D_xxx", m.gchom_path) + ATC( "D_yyy", m.gchom_path) = 0
    DELETE FILE (m.gccen_path + "sk_???_a2.*")
    RETURN 
ENDIF 

DO clos_data 

had = createobject("had", "zmensovac archivnich eskacek")
had.show()
had.set_max(15)

fn_plt_n = gcpri_path + "sk_plt_pom"
fn_pol_n = gcpri_path + "sk_pol_pom"
fn_fin_n = gcpri_path + "sk_fin_pom"
fn_sez_n = gcpri_path + "sk_sez_pom"
fn_txt_n = gcpri_path + "sk_txt_pom"

casova_znacka = TTOC(DATETIME(),1)
casova_znacka = "_" + LEFT(m.casova_znacka, 8) + "-" + SUBSTR(m.casova_znacka, 9)

*   *   *   *   *   *   *   *   *   *   SEZ      SEZ      SEZ      SEZ   *   *   *   *   *   *   *   *   *   *
SELECT * FROM (gccen_path + "sk_sez_a") INTO TABLE (fn_sez_n)
DO pom_to_a2 WITH "sk_sez"


*   *   *   *   *   *   *   *   *   *   FIN      FIN      FIN      FIN   *   *   *   *   *   *   *   *   *   *
had.increment("fin")
SELECT *, u_cis as u_cis_old, SPACE(7) as priznak FROM (gccen_path + "sk_fin_a") INTO TABLE (m.gcpri_path + "fin_all")     && bude pouzito pro nahrazeni cisla uctu v sk_pol a sk_plt
INDEX on u_cis TAG u_cis

had.increment("fin - scan slev")
SELECT SUBSTR(text_sleva, 2, 7) as ucet, u_cis as u_cis_slev WHERE LEFT(text_sleva,1)="@" FROM (m.gcpri_path + "fin_all") INTO TABLE (m.gcpri_path + "seznam_slev")
SCAN 
    SCATTER MEMVAR 
    SELECT fin_all
    REPLACE FOR u_cis = m.ucet priznak WITH m.u_cis_slev
ENDSCAN

had.increment("fin - scan")
STORE "(*&^^&*(" to id_kas, id_slave, autor, uzaverka, cas_atrib, mutace, u_cis 
STORE 0 to stul
SELECT fin_all
INDEX on id_kas + id_slave + uzaverka + u_cis_old + autor + cas_atrib + mutace + STR(stul, 7,1) TAG vsechno
SCAN FOR EMPTY(text_sleva + text_link + priznak) .and. EMPTY(id_limit) .and. EMPTY(id_den) .and. EMPTY(id_chod) .and. EMPTY(lim_datum) .and. EMPTY(sleva_abs)
    IF (id_kas = m.id_kas) .and. (id_slave = m.id_slave) .and. (uzaverka = m.uzaverka) .and. (autor = m.autor) .and. (cas_atrib = cas_atrib) .and. (mutace = m.mutace) .and. (stul = m.stul)
        REPLACE u_cis WITH m.u_cis  &&&, text_link WITH "[[" + u_cis + "]]"
    ELSE
        SCATTER FIELDS id_kas, id_slave, uzaverka, u_cis, autor, cas_atrib, mutace, stul MEMVAR 
    ENDIF 
ENDSCAN 
INDEX on uzaverka + u_cis_old TAG uz_ucet_o

had.increment("sum fin")
SELECT uzaverka, id_kas, id_slave, u_cis, autor, text_sleva, text_link, u_cas, stul, cas_atrib, mutace, u_open, id_limit, id_den, id_chod, lim_datum, uzcis, ;
       SUM(cena_celk) as cena_celk, SUM(sleva_abs) as sleva_abs, SUM(cena_slev) as cena_slev, SUM(pocet_h) as pocet_h, 1 as idriadok ;
       GROUP BY uzaverka, id_kas, u_cis ;
       FROM (m.gcpri_path + "fin_all") INTO TABLE (fn_fin_n)
ALTER table (fn_fin_n) alter pocet_h I
DO pom_to_a2 WITH "sk_fin"



*   *   *   *   *   *   *   *   *   *   PLT      PLT      PLT      PLT   *   *   *   *   *   *   *   *   *   *
had.increment("plt to plt_all")
SELECT * FROM (gccen_path + "sk_plt_a") INTO TABLE (m.gcpri_path + "plt_all")

had.increment("plt.u_cis")
SET RELATION TO uzaverka + u_cis INTO fin_all
REPLACE all u_cis with fin_all.u_cis

had.increment("sum plt")
SELECT uzaverka, u_cis, druh_pl, mena, kompez, ;
       SUM(cena_pl) as cena_pl, SUM(tip) as tip, SUM(cena_mena) as cena_mena, 1 as idriadok ;
       GROUP BY uzaverka, u_cis, druh_pl, mena, kompez ;
       FROM (m.gcpri_path + "plt_all") INTO TABLE (fn_plt_n)
DO pom_to_a2 WITH "sk_plt"


*   *   *   *   *   *   *   *   *   *   POL      POL      POL      POL   *   *   *   *   *   *   *   *   *   *
had.increment("pol to pol_all")
SELECT * FROM (gccen_path + "sk_pol_a") INTO TABLE (m.gcpri_path + "pol_all")

had.increment("pol.u_cis")
SET RELATION TO uzaverka + u_cis INTO fin_all
REPLACE all u_cis with fin_all.u_cis

had.increment("sum pol")
SELECT          uzaverka, u_cis, id_kas, id_man, c_karty, dan, cen_hlad, u_time, kompez, dotacia, id_fmenu, k_odpoctu, ;
       SUM(mnozstvi) as mnozstvi, SUM(prachy) as prachy, 1 as idriadok ;
       GROUP BY uzaverka, u_cis, id_kas, id_man, c_karty, dan, cen_hlad, u_time, kompez, dotacia, id_fmenu, k_odpoctu ;
       FROM (m.gcpri_path + "pol_all") INTO TABLE (fn_pol_n)
DO pom_to_a2 WITH "sk_pol"


*   *   *   *   *   *   *   *   *   *   TXT      TXT      TXT      TXT   *   *   *   *   *   *   *   *   *   *
had.increment("txt")
SELECT * FROM (gccen_path + "sk_txt_a") INTO TABLE (fn_txt_n)

SET RELATION TO uzaverka + u_cis INTO fin_all
had.increment("txt.u_cis")
DELE FOR u_cis # fin_all.u_cis

DO pom_to_a2 WITH "sk_txt"

DO clos_data 
RELEASE had


************************************************************************
PROCEDURE pom_to_a2
PARAMETERS soubor
IF FILE(m.gccen_path + m.soubor + "_a2.dbf")
    SELECT 0
    USE (m.gccen_path + m.soubor + "_a2.dbf") EXCLUSIVE 
    APPEND FROM (m.gcpri_path + m.soubor + "_pom")
ELSE
    SELECT * FROM (m.gcpri_path + m.soubor + "_pom") INTO TABLE (m.gccen_path + m.soubor + "_a2.dbf")
ENDIF 
DO indexuj IN indexy
USE 

SELECT * FROM (m.gccen_path + m.soubor + "_a") INTO TABLE (m.gccen_path + m.soubor + m.casova_znacka)
USE 
SELECT             (m.soubor + "_a")
USE (m.gccen_path + m.soubor + "_a") EXCLUSIVE 
ZAP
USE 

