*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
* smaze adresare vedle gcpri_path s pozustatky minulych spusteni vcetne prazdnych adresaru od predvcerejska dal
*
* 2016-02-15    - kazdy soubor zvlast se maze v try-catch
* 2020-06-04    - pridan uklid v datovych adresarich
* 2022-01-04    - v pri_path se mazou i podadresare ...\zaloha\...
* 2022-10-14    - zkracovani velkych zurnalu
* 2023-01-04    - smazou se stare EDI doklady v edi_hlav a edi_pol
* 2023-06-20    - smazou se docasne adresare po updateru "food600-temp-" do vcerejska
* 2023-09-19    - predelano odmazavani starych EDI
* 2024-01-19    - pridelano odmazavani starych zurnalu v sql
*
*
*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    


ae_had.set_txt2(PROGRAM() + "  uklid_pri_path")
DO uklid_pri_path

ae_had.set_txt2(PROGRAM() + "  uklid_datovy_adresar")
DO uklid_datovy_adresar

ae_had.set_txt2(PROGRAM() + "  uklid_zurnaly")
DO uklid_zurnaly

ae_had.set_txt2(PROGRAM() + "  promaz_stare_edi")
DO promaz_stare_edi

ae_had.set_txt2(PROGRAM() + "  uklid_po_updateru")
DO uklid_po_updateru

ae_had.set_txt2(PROGRAM() + "  promaz_sql_zurnal")
DO promaz_sql_zurnal


*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   
PROCEDURE uklid_po_updateru

ACTIVATE SCREEN 
adresar_ke_smazani = JUSTPATH(LEFT(m.gcini_path, LEN(m.gcini_path)-3))

pocet = adir(qqq, m.adresar_ke_smazani + "*.*", "D")
FOR ii = 1 to m.pocet
    IF "D" $ qqq(ii, 5) .and. ATC("food600-temp-", qqq(ii, 1)) > 0 .and. ATC("-" + SUBSTR(TTOC(DATE(), 1), 5, 4) + "-", qqq(ii, 1)) = 0

            co_udelat = "!rmdir " + m.adresar_ke_smazani + qqq(ii, 1) + " /s /q "
            ? m.co_udelat
            WAIT m.co_udelat WINDOW TIMEOUT 3
            &co_udelat

    ENDIF
ENDFOR 



*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   
PROCEDURE uklid_pri_path
crea table (m.gcpri_path + "z90soubory") free (adresar c(111), nazev c(44), atrib c(1), delka n(9), datum D )

ACTIVATE SCREEN 
adresar_ke_smazani = LEFT(m.gcpri_path, RAT("\", m.gcpri_path,2))

do jedenadresar with adresar_ke_smazani
SELECT z90soubory
DELETE FOR .not. ("D" $ atrib)
do while .t.
   locate for "D" $ atrib .and. delka = 0
   if found()
       repl delka with -1111111
       do jedenadresar with allt(adresar)
   else
       exit
   endif
enddo
DELETE FOR delka < 0


*  *  *  smazou se radky adresaru, ve kterych existuje soubor z poslednich dni  *  *  *
dnes = DATE() - 2
LOCATE FOR datum > m.dnes .and. ATC("\zaloha\", adresar) = 0
DO WHILE FOUND()
    SCATTER FIELDS adresar MEMVAR
    DELETE FOR adresar = m.adresar
    LOCATE FOR datum > m.dnes .and. ATC("\zaloha\", adresar) = 0
ENDDO 


SCAN FOR ATC("\zaloha\", adresar) + ATC("\tmp\", adresar) + ATC("\temp\", adresar) > 0
    cesta = ADDBS(ALLT(adresar))
    pokus_o_smazani (m.cesta + "*.*")
ENDSCAN 


SCAN FOR INLIST(nazev, "food600.dbc", "food600_ps.dbc")
    cesta = ADDBS(ALLT(adresar))
    cccc  = ADIR(dddd, m.cesta + "*.*")
    FOR iiii = 1 TO cccc
        pokus_o_smazani (m.cesta + dddd(iiii, 1))
    ENDFOR
ENDSCAN 


* smazani prazdnych adresaru
DELETE ALL 
DO jedenadresar WITH adresar_ke_smazani
DELETE FOR .not. ("D" $ atrib)
SCAN
    pokus_o_smazani (ALLTRIM(adresar))
ENDSCAN


*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   
FUNCTION jedenadresar (adresar)

pocet = adir(qqq, m.adresar + "*.*", "D")
*? m.adresar + STR(m.pocet)
for ii = 1 to m.pocet
  if left(qqq(ii, 1), 1) # "."
     IF "D" $ qqq(ii, 5)
         insert into z90soubory (adresar,                  atrib, delka, datum) ;
             value (lowe(m.adresar)+lowe(qqq(ii, 1))+"\", "D",   0, qqq(ii, 3))
     ELSE 
         insert into z90soubory (adresar,  nazev,         atrib, delka,      datum) ;
                value (lowe(m.adresar), lowe(qqq(ii, 1)), "", qqq(ii, 2), qqq(ii, 3))
     ENDIF
  endif
endfor


*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   
PROCEDURE uklid_datovy_adresar

pokus_o_smazani (m.gchom_path + "*.bak")
pokus_o_smazani (m.gccen_path + "*.bak")
pokus_o_smazani (m.gcdat_path + "*.bak")

pokus_o_smazani (m.gchom_path + "*.tbk")
pokus_o_smazani (m.gccen_path + "*.tbk")
pokus_o_smazani (m.gcdat_path + "*.tbk")

pokus_o_smazani (m.gchom_path + "ser_time.dbf")
pokus_o_smazani (m.gchom_path + "semafory.dbf")



*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   
PROCEDURE uklid_zurnaly

CREATE TABLE (m.gcpri_path + "z90soubory") free (adresar c(111), nazev c(44), atrib c(1), delka I, datum D )
ACTIVATE SCREEN 
do jedenadresar with m.gchom_path

SELECT z90soubory
DELETE FOR ATC(".txt", nazev) + ATC("zurnal_", nazev) = 0
ERASE (m.gchom_path + "zurnal_*.txt-2")
SCAN
    jmeno_zur = ALLTRIM(adresar) + ALLTRIM(nazev)
    DO CASE 
    CASE ATC("_sql_exec", nazev) > 0
        max_velikost = 4000111000
    CASE ATC("_verzia_bez", nazev) > 0
        max_velikost = 4000111000
    OTHERWISE 
        max_velikost = 222000
    ENDCASE 

    IF delka > max_velikost * 1.2
                
        small_log("zkraceni zurnalu " + m.jmeno_zur + " na " + nx(m.max_velikost), "uklid_pri_path")
        ? m.jmeno_zur
        RENAME (m.jmeno_zur) TO (m.jmeno_zur + "-2")
        fh = FOPEN( m.jmeno_zur + "-2" )
        IF m.fh > 0
            ?? " otevren"
            FSEEK(m.fh, - m.max_velikost, 2)        
            FGETS(m.fh,7777)                  && docte se do konce radku, aby zkraceny zurnal zacinal celym radkem
            DO WHILE .not. FEOF(m.fh)
                radek = FREAD(m.fh, 65000)
                STRTOFILE(m.radek, m.jmeno_zur, 1) 
            ENDDO 
            ? "hotovo"
            FCLOSE(m.fh)
        ELSE 
            ?? " NEJDE OTEVRIT"
        ENDIF

    ENDIF
ENDSCAN 
ERASE (m.gchom_path + "zurnal_*.txt-2")


*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   
FUNCTION pokus_o_smazani (co_mazat)
IF RIGHT(m.co_mazat, 1) = "\"
    TRY 
        RMDIR (m.co_mazat)
        small_log("rmdir " + m.co_mazat                   , "uklid_pri_path")
    CATCH 
        small_log("rmdir " + m.co_mazat + " .... neproslo", "uklid_pri_path")
    ENDTRY 
ELSE 
    TRY 
        DELETE FILE (m.co_mazat)
        small_log(m.co_mazat                   , "uklid_pri_path")
    CATCH 
        small_log(m.co_mazat + " .... neproslo", "uklid_pri_path")
    ENDTRY 
    pokus_o_smazani(JUSTPATH(m.co_mazat) +  "\")
ENDIF 


*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   
PROCEDURE promaz_stare_edi

#DEFINE POCET_DNI_KE_SMAZANI 333

TRY 
    USE (m.fn_edihlav) ALIAS edi_hlav
    SET FILTER TO  c_dokl # 0 .and. dat_vyst < DATE() - POCET_DNI_KE_SMAZANI .and. swift # "ODMAZANO"
    COUNT TO pocet_edi
    IF m.pocet_edi > 555
        
        SELECT 0
        USE (m.fn_edipol) ALIAS edi_pol
        COPY ALL to (m.fn_edipol + STRTRAN(TTOC(DATETIME(), 3), ":", "-"))

        SELECT edi_hlav
        ted = TTOC(DATETIME(), 3)
        SCAN
            SCATTER FIELDS id_edi, dat_vyst MEMVAR 
            small_log("id_edi=" + ALLTRIM(STR(m.id_edi)) + " z " + LEFT(TTOC(m.dat_vyst, 3), 10), PROGRAM())
            DELETE FROM (m.fn_edipol) WHERE id_edi = m.id_edi
            REPLACE swift WITH "ODMAZANO", mesto WITH "ODMAZANO " + m.ted
        ENDSCAN 

        USE (m.fn_edipol) EXCLUSIVE 
        PACK     
    ENDIF 
CATCH TO eeErr
    small_log( ALLTRIM(STR(eeErr.ErrorNo)) + eeErr.Message + "  " + DBF(), PROGRAM())
ENDTRY 
DO clos_data


*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   
PROCEDURE promaz_sql_zurnal
#DEFINE POCETDNI 222

IF m.issql
    DO open_database
       
    posledni_datum = DATE() - POCETDNI * 5
    zacatek_prace = DATETIME()
    DO WHILE posledni_datum < DATE() - POCETDNI .and. DATETIME() - m.zacatek_prace < 111  && sekund

        posledni_cas = LEFT(TTOC(m.posledni_datum, 3), 10)
        proved("DELETE FROM food600.zurnal  WHERE cas < ?posledni_cas ;")
        posledni_datum = m.posledni_datum + 99
    ENDDO 
ENDIF 


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
FUNCTION proved (co_udelat)
WAIT m.co_udelat WINDOW NOWAIT 
vysledek = sql_exec( m.co_udelat )
zprava_do_logu = m.co_udelat + " " + m.posledni_cas + STR(m.vysledek, 5)
IF m.vysledek < 0
    DIMENSION chybaexe(9)
    chybaexe(2) = "vsechno v poradku"
    AERROR(chybaexe)
    MESSAGEBOX("SQL Chyba" + CHR(10) + chybaexe(2),64)
    zprava_do_logu = m.zprava_do_logu + " " + chybaexe(2)
ENDIF
small_log (m.zprava_do_logu, "uklid_pri_path")
WAIT CLEAR 
