*:*****************************************************************************
*:        Program: F:\FOOD500\CEN\ARCH_SK.PRG
*:*****************************************************************************
PRIVATE datum2

DO clos_data 
DO dopln_uctden_do_sksez
DO clos_data 
DO vypln_kod_v_sk_pol IN stru_upd
DO clos_data 

hranice1 = str(year(date()-66),4) + "-" + transform(month(date()-66), "@L 99") + "-01"
hranice1 = inputbox(jaz("prg_zadaj_obdobie"), jaz("m_ost archivace trzeb"), m.hranice1, 99999, "", "")

IF ! EMPTY(m.hranice1)
   hranice = DATE(VAL(LEFT(hranice1,4)), VAL(SUBSTR(hranice1,6,2)), VAL(SUBSTR(hranice1,9,2)))
   IF f_acc_ex(m.gccen_path + "sk_sez")
        select sk_sez.id_man, fd.id_zkratka, count(sk_sez.id_man) as pocet ;
			group by 1 where BETWEEN(uct_den, {^1111-11-11}, m.hranice) and empty(odecteno) ;
			from (m.gccen_path + "sk_sez") left join (m.fn_food_dat) fd on fd.id == sk_sez.id_man into cursor neodectene
		if prazdna()
            DO arch_sk_1 WITH m.hranice
		else
			neodectene = chr(13) + chr(13) + jaz("prg_aj_ked_neodtazene") + chr(13)
			scan
				neodectene = m.neodectene + chr(13) + "        " + id_man + ":  " + id_zkratka
                IF pocet > 1
                     neodectene = m.neodectene + "   (" + str(pocet,3) + "x)"
                 endif
			endscan
			if messagebox(jaz("prg_opravdu_archivovat_do") + " " + DTOC(m.hranice) + " ?" + m.neodectene,3+48+256, jaz("prg_ttl_archivace") + "  " + jaz("form_s_neodcitanymi_uz")) = 6
				DO arch_sk_1 WITH m.hranice
				do zurnal_log with "OPERA", "ARCHIVACE TRZEB k " + DTOC(m.hranice), "S NEODECTENYMI UZAVERKAMI"
			endif
		ENDIF
   ELSE
      = messagebox(jaz("prg_operace_nepristupna_mssg") , 0+64+256, "", 3333)
   ENDIF
ENDIF 
DO clos_data


*:*****************************************************************************
procedure auto_arch_sk
DO clos_data 
DO dopln_uctden_do_sksez
DO clos_data 
IF m.archiv_sk > 1 .and. f_acc_ex(m.gccen_path + "sk_sez")
    uzav_arch  = DATE() - (m.archiv_sk * 33)
    LOCATE FOR BETWEEN(uct_den, {^2000-01-01},  m.uzav_arch)
    IF FOUND()
         DO arch_sk_1 WITH m.uzav_arch + 15
    ENDIF 
ENDIF 


*:*****************************************************************************
procedure arch_sk_1
PARAMETERS datum2
DO clos_data

old_on_err = ON("error")
arch_sk_otevreno = ""
ON ERROR arch_sk_otevreno = m.arch_sk_otevreno + UPPER(m.otevira_se) + " "

otevira_se = m.gccen_path + "SK_PLT"
use (m.otevira_se) in 0 excl
otevira_se = m.gccen_path + "SK_POL"
use (m.otevira_se) in 0 excl
otevira_se = m.gccen_path + "SK_FIN"
use (m.otevira_se) in 0 excl
otevira_se = m.gccen_path + "SK_TXT"
use (m.otevira_se) in 0 excl
otevira_se = m.gccen_path + "SK_SEZ"
use (m.otevira_se) in 0 excl

ON ERROR &old_on_err

IF EMPTY(m.arch_sk_otevreno)
    do zurnal_log with "OPERA", "ARCHIVACE TRZEB", "k " + DTOC(m.datum2) + " - zacatek"
    had = CREATEOBJECT("had", jaz("prg_arch_trz_txtpltpolfinsez") + "  (" + DTOC(m.datum2) + ")")
    had.show()
    had.set_max( 22 )

    SELECT disti uzaverka, uct_den WHERE BETWEEN(uct_den, {^1111-11-11}, m.datum2) ;
           FROM (m.gccen_path + "sk_sez") INTO TABLE (m.gcpri_path + "zaarchivovat")

    DO jedensoubor WITH "sk_txt" 
    DO jedensoubor WITH "sk_plt"
    DO jedensoubor WITH "sk_pol"
    DO jedensoubor WITH "sk_fin"
    DO jedensoubor WITH "sk_sez"
    rele had
    do zurnal_log with "OPERA", "ARCHIVACE TRZEB", "k " + DTOC(m.datum2) + " - konec"

    DELETE FILE (m.gccen_path + "sk_???_a2.*")		&& redukovana archivni eskacka
    TRY
        do zmensi_archivni_sk
    CATCH
    ENDTRY


ELSE
    MESSAGEBOX("nelze archivovat, nejsou pristupne databaze" + CHR(13) + arch_sk_otevreno, 1+16,"archivace trzeb",4444)
ENDIF
DO clos_data


*:*****************************************************************************
PROC jedensoubor
PARAM soubor

had.increment
had.set_txt2 ( "copying from " + UPPE(m.soubor) )
SELECT * WHERE uzaverka in (SELECT uzaverka FROM (m.gcpri_path + "zaarchivovat")) ;
       FROM (m.soubor) INTO TABLE (m.gcpri_path + m.soubor + "_aa")

had.increment
had.set_txt2 ( "appending from " + UPPE(m.soubor) )
SELECT 0
USE (m.gccen_path + m.soubor + "_a") EXCL
APPE FROM (m.gcpri_path + m.soubor + "_aa")
USE 

had.increment
had.set_txt2 ( "deleting in " + UPPE(m.soubor) )
DELETE FROM (m.soubor) WHERE uzaverka in (SELECT uzaverka FROM (m.gcpri_path + "zaarchivovat"))
had.increment
had.set_txt2 ( "packing " + UPPE(m.soubor) )
SELECT (m.soubor)
PACK
