*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
*
* program zapuje/truncatuje archivni eskacka pripadne smaze z aktualnich uzaverky, starsi nez POCETDNI
*
* 2023-02-06 - opraveny drobne chyby
*
*    *    *    *    *    *    *    *    *    *    *    *      #> ver. 2023-02-06 <#       *    *    *    *    *    *    *    *    *    *    *    *    *


#DEFINE POCETDNI 39
#DEFINE JMENO_ZURNALU "sk_era"

posledni_datum = DATE() - POCETDNI
posledni_cas   = LEFT(TTOC(m.posledni_datum , 3), 10)

DO CASE 
CASE ATC("FOOD_KARLOVA", m.sqldatabaza) + ATC("FOOD_PETYNKA", m.sqldatabaza) > 0
    smaz_to()

CASE ATC("FOOD_FRIENDS", m.sqldatabaza) + ATC("FOOD_GLAMOUR", m.sqldatabaza) > 0
    open_database()
    truncate_archivu()
    smaz_stare_zurnaly()

CASE ATC("D_SKOREPKA", m.gchom_path) + ATC("D_VEJVOD", m.gchom_path) + ATC("D_MUSTH", m.gchom_path) > 0
    smaz_to()

CASE MESSAGEBOX("fakt chces odmazat stare trzby v " + UPPER(m.gchom_path + " / " + sqldatabaza) + " k " + m.posledni_cas + " ?",3,"mazani eskacek", 5555) = 6
    IF MESSAGEBOX("fakt se nebojis, ze prijdes o trzby do " + m.posledni_cas + "?",3,"mazani eskacek" + UPPER(m.gchom_path + " / " + sqldatabaza)) = 7
        smaz_to()
    ENDIF 

OTHERWISE 
    RETURN 
ENDCASE 

*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
PROCEDURE smaz_to
IF EMPTY(m.sqldatabaza)
    sk_era_dbf()
    smaz_stare_zurnaly()
ELSE
    open_database()
    truncate_archivu()
    sk_era_sql2()
    smaz_stare_zurnaly()
ENDIF 


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
PROCEDURE truncate_archivu
proved("TRUNCATE TABLE food600.sk__fin_a ;")
proved("TRUNCATE TABLE food600.sk__fis_a ;")
proved("TRUNCATE TABLE food600.sk__plt_a ;")
proved("TRUNCATE TABLE food600.sk__pol_a ;")
proved("TRUNCATE TABLE food600.sk__sez_a ;")
proved("TRUNCATE TABLE food600.sk__txt_a ;")

proved("TRUNCATE TABLE food600.obraty_a  ;")
proved("TRUNCATE TABLE food600.doklady_a ;")

proved("DELETE FROM food600.zurnal WHERE cas  < ?posledni_cas ;")


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
PROCEDURE sk_era_sql2

posledni_datum = DATE() - POCETDNI
podminka       = "c_data = ?m.cdata AND c_uzaverka IN (SELECT DISTINCT c_uzaverka from food600.sk__sez WHERE uct_den < ?m.posledni_datum AND c_data = ?m.cdata)"
proved("DELETE FROM food600.sk__fin WHERE " + m.podminka + " ;")
proved("DELETE FROM food600.sk__fis WHERE " + m.podminka + " ;")
proved("DELETE FROM food600.sk__plt WHERE " + m.podminka + " ;")
proved("DELETE FROM food600.sk__pol WHERE " + m.podminka + " ;")
proved("DELETE FROM food600.sk__txt WHERE " + m.podminka + " ;")

proved("DELETE FROM food600.sk__sez WHERE uct_den < ?m.posledni_datum AND c_data = ?m.cdata ;")

*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
FUNCTION proved (co_udelat)
WAIT m.co_udelat WINDOW NOWAIT 
vysledek = sql_exec( m.co_udelat )
zprava_do_logu = m.co_udelat + 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)
ELSE 
    zprava_do_logu = m.zprava_do_logu + " OK"
ENDIF
small_log (m.zprava_do_logu, JMENO_ZURNALU)
WAIT CLEAR 


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
PROCEDURE smaz_stare_zurnaly

ddd = DATE() - POCETDNI
FOR ii = 1 TO 5
    rrmm = SUBSTR(TTOC(m.ddd, 1), 3, 4)
    mm   = SUBSTR(m.rrmm        , 3, 2) 
    cmd = "run del /S /Q " + m.gchom_path + "zu" + m.mm + ".txt " + m.gchom_path + "z*" + m.rrmm + "*.txt "
    small_log (m.cmd, JMENO_ZURNALU)
    &cmd
    ddd  = GOMONTH(m.ddd, -1)
ENDFOR 


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
PROCEDURE sk_era_dbf
sk_path = m.gchom_path + "data\"

max_uzav = SUBSTR(DTOS(DATE() - POCETDNI), 3) + " 00:00"

CLOSE DATABASES 
do jedensoubor with "sk_plt"
do jedensoubor with "sk_pol"
do jedensoubor with "sk_fin"
do jedensoubor with "sk_sez"
do jedensoubor with "sk_txt"


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
FUNCTION jedensoubor (soubor)
zprava_do_logu = UPPER(m.soubor)
TRY     
   USE (m.sk_path + m.soubor) Excl
   DELETE FOR uzaverka < m.max_uzav
   PACK 
CATCH 
   zprava_do_logu = m.zprava_do_logu + " not truncated "
ENDTRY 
TRY     
   USE (m.sk_path + m.soubor + "_a") Excl 
   ZAP
CATCH 
   zprava_do_logu = m.zprava_do_logu + "_A not truncated "
ENDTRY 
USE 
small_log (m.zprava_do_logu, JMENO_ZURNALU)
