
#DEFINE CRLF CHR(13)+CHR(10)
SET PROCEDURE TO ppgm_lic ADDITIVE 

typ_inst = IIF( m.gityp_inst = 1, "*one*", "")
poslat_licenci = .F.
IF VARTYPE(lic_dic) = "U"
	lic_dic = ""
ENDIF
IF VARTYPE(lic_kod) = "U"
	lic_kod = "ZZZZZZZZZ"
ENDIF
IF VARTYPE(lic_konec) = "U"
    lic_konec = ""
ENDIF

l2_new = lic_kod
l2_dic = lic_dic

IF m.issql
	PRIVATE hladajmodul, hladajvarname, hladajsetupskup
	hladajmodul = "FOOD_SET"
	hladajsetupskup=""
	rv("defsubor","ddnn")
	=CURSORSETPROP("Buffering" ,5, "ddnn")
ELSE
	DO clos_data
	USE (m.gchom_path + "food_set") ALIAS ddnn
    
    IF BITAND(VAL(OS(10)), 16) # 0    && terminal server
        LOCATE FOR var_name = "lic_dic"
        IF FOUND()
            REPLACE FOR LOWER(var_name) = "novell_ver" var_value WITH '"TERMINAL SERVER ' + OS(1) + " " + LEFT(TTOC(DATE(),3), 10) + '"'
        ELSE 
            REPLACE FOR LOWER(var_name) = "novell_ver" var_name WITH "lic_dic", var_value WITH  '"' + LEFT(TTOC(DATE(),3), 10) + '"'
        ENDIF 
    ENDIF     
ENDIF

vvv_new = LOWER(m.hotel_name + m.typ_inst + m.hotel_ico + m.hotel_dico + m.strediska_sum)
vvv_dic = LOWER(m.hotel_name + m.typ_inst + m.hotel_ico + m.hotel_dico)
SELECT ddnn

lic_id = m.strediska_sum
update_or_insert("lic_id", m.strediska_sum, "ddnn")

IF EMPTY(l2_new)   &&&&&& vygenerovat licenci na omezenou dobu
	lic_konec = EVALUATE("{^" + LEFT(m.gcverzex,10) + "}") + 99
    lic_konec = TRANS(YEAR(m.lic_konec), "9999") + "-" + TRANSFORM(MONTH(m.lic_konec), "@L 99")
	vt = vvv_new + m.lic_konec
	l2_new = s_h_a_hex(m.vt)
	l2_new = LEFT(m.l2_new, 5) + "-" + SUBSTR(m.l2_new, 6, 5) + "-" + SUBSTR(m.l2_new, 11, 5)
    update_or_insert("lic_kod", m.l2_new, "ddnn")
ENDIF

v_thr = DATE() - 33
IF ATC("asrv.", m.sqladresa) + ATC("178.17.", m.sqladresa) > 0
    v_thr = DATE() + 333
    gcverze = LEFT(gcverze, 10) + " 4" + "4" + UPPER("x")
ENDIF 

IF DATE() > m.v_thr
    DO iteruj_obdobi WITH m.l2_new, m.vvv_new    && nova licence (food_set.lic_kod)
ENDIF

IF DATE() > m.v_thr
	DO iteruj_obdobi WITH m.l2_dic, m.vvv_dic    && licence, vazana jen na DIC bez ohledu na konfiguraci stredisek z food_dat
	gcverze = LOWER(gcverze)
ENDIF

DO CASE
CASE DATE() > m.v_thr		&&& licence nesedi nebo exspirovala, zastavit hned
    naozajspustit = .F.
	IF m.issql .and. m.slovenskaverzia
		DO FORM set_lic TO m.naozajspustit
	ENDIF 
	IF !m.naozajspustit
		DO pozavirej WITH "fn_hesla"
		USE (m.fn_hesla) ALIAS hesla IN 0
		UPDATE hesla SET j = "\" + j WHERE LEFT(klic, 3) = "mm_" AND ("y" $ klic OR "ul" $ klic OR "ed" $ klic)
		USE IN hesla

        PUBLIC vyprseno
        vyprseno = .T.

        MESSAGEBOX(jaz("vyprsela licence, volejte"), 1+16, "FoodMan", 3333)
        DO posli_licenci
	ENDIF 

CASE DATE() > m.v_thr - 44    && poslani s predstihem, nez zacna hlasit obsluze
    DO posli_licenci

CASE DATE() > m.v_thr - 22
    MESSAGEBOX(jaz("pocet dni do konce licence: ") + ALLTRIM(STR(m.v_thr-DATE())) ,1+48,"FoodMan")
    DO posli_licenci

OTHERWISE 

    jmeno_lic_souboru = m.gchom_path + get_jmeno_lic_souboru() + ".info"

    posledni_info = "1111-11-11"
    IF FILE(jmeno_lic_souboru)
        posledni_info = LEFT(FILETOSTR(jmeno_lic_souboru), 10)
    ENDIF 

    IF m.posledni_info < TTOC(DATE() - 7, 3) .or. m.posledni_info < "2022-09-19"
        DO posli_licenci WITH "info"
    ENDIF 

ENDCASE

IF m.issql
	tux(.T.,.T.,"ddnn")
ENDIF
DO clos_data
RELEASE PROCEDURE ppgm_lic 


*   *   *   *   *   *   *   *   *   *   *   *
PROCEDURE posli_licenci
LPARAMETERS pripona
IF EMPTY(m.pripona)
    pripona = "req"
ENDIF 
jmeno_lic_souboru = get_jmeno_lic_souboru()
jmeno_pocitace = m.machine
IF AT("@", jmeno_pocitace) > 1
    jmeno_pocitace = LEFT(m.jmeno_pocitace, AT("@", m.jmeno_pocitace) - 1)
ENDIF 
jmeno_pocitace = "(" + m.jmeno_pocitace + ")"

txt = TTOC(DATETIME(), 3) + "  " + m.jmeno_pocitace + CRLF
add_2_txt( '[' + LOWER(m.gchom_path ) + 'food_set]'  + CRLF )
add_2_txt('hotel_name= "' + m.hotel_name  + '"' )
add_2_txt('hotel_ico = "' + m.hotel_ico   + '"' )
add_2_txt('hotel_dico= "' + m.hotel_dico  + '"' )
add_2_txt('hotel_naz1= "' + m.hotel_naz1  + '"' )
add_2_txt('hotel_naz2= "' + m.hotel_naz2  + '"' )
add_2_txt('hotel_naz3= "' + m.hotel_naz3  + '"' )
add_2_txt('hotel_naz4= "' + m.hotel_naz4  + '"' + CRLF )

add_2_txt('lic_id    = "' + m.lic_id      + '"' )
add_2_txt('lic_konec = "' + m.lic_konec   + '"' )
add_2_txt('lic_kod   = "' + m.lic_kod     + '"' )
add_2_txt('lic_dic   = "' + m.lic_dic     + '"' + CRLF )
add_2_txt('verze     = "' + m.gcverze     + '"' )
add_2_txt('hom_path  = "' + m.gchom_path  + '"' )
add_2_txt('databaza  = "' + m.sqldatabaza + '"' + CRLF + CRLF)

add_2_txt("--------- postaru ---------")
add_2_txt("Meno:"      + ALLTRIM(m.hotel_name ) )
add_2_txt("ICO:"       + ALLTRIM(m.hotel_ico  ) )
add_2_txt("DIC:"       + ALLTRIM(m.hotel_dico ) )
add_2_txt("Strediska:" + ALLTRIM(m.lic_id     ) )
add_2_txt("ONE:FALSE"                           + CRLF )

IF m.slovenskaverzia
    RETURN 
ENDIF 

TRY
    txt = m.txt + CRLF + CRLF + "[jak to vypada]" + CRLF + CHRTRAN(FILETOSTR(m.gchom_path + JUSTFNAME(LEFT(m.gchom_path, LEN(m.gchom_path)-1)) + ".ver"), "[]", "<>")
CATCH
ENDTRY 

STRTOFILE(m.txt,   m.gcpri_path + "licencia.txt")
STRTOFILE(m.txt,   m.gchom_path + m.jmeno_lic_souboru + "." + m.pripona)
DO send_lic   WITH m.gchom_path + m.jmeno_lic_souboru,        m.pripona
DO nahraj_lic WITH m.gchom_path + m.jmeno_lic_souboru + ".lic"
    DO poslimaildoalta WITH "Licencia " + IIF(m.pripona = "info", "- INFO :", "  ") + m.hotel_name + " " + m.lic_konec + " " + m.jmeno_pocitace, m.gchom_path + m.jmeno_lic_souboru + "." + m.pripona, m.gchom_path + m.jmeno_lic_souboru + "." + m.pripona, m.gcpri_path + "licencia.txt"
IF m.pripona = "info"
    DO poslimaildoalta WITH "Licence - INFO :" + m.hotel_name + " " + m.lic_konec + " " + m.jmeno_pocitace, m.gchom_path + m.jmeno_lic_souboru + "." + m.pripona, m.gchom_path + m.jmeno_lic_souboru + ".info", m.gcpri_path + "licencia.txt"
    DO poslimaildoalta WITH "Licence - INFO jen s licencia" + m.hotel_name + m.jmeno_pocitace, "", m.gcpri_path + "licencia.txt"
ELSE 
    DO poslimaildoalta WITH "Licence         " + m.hotel_name + " " + m.lic_konec + " " + m.jmeno_pocitace, m.gchom_path + m.jmeno_lic_souboru + "." + m.pripona, m.gchom_path + m.jmeno_lic_souboru + ".req", m.gcpri_path + "licencia.txt"
ENDIF 


*   *   *   *   *   *   *   *   *   *   *   *
FUNCTION get_jmeno_lic_souboru
PRIVATE lic_path, jmeno_lic_souboru, zacatek_adresare
IF m.issql
    lic_path = m.sqldatabaza
ELSE
    lic_path = justpath(m.gchom_path)
    lic_path = SUBSTR(m.lic_path, RAT("\", m.lic_path))
ENDIF 
*jmeno_lic_souboru = alphanum(lower(m.lic_path + "__" + STRTRAN(m.hotel_name, " ", "-") + "[" + IIF(EMPTY(m.hotel_ico), "ico", m.hotel_ico) + "]" + m.lic_id), "_-][")
jmeno_lic_souboru  = alphanum(lower(m.lic_path + "__" + STRTRAN(m.hotel_name, " ", "-") + "[" + IIF(EMPTY(m.hotel_ico), "ico", m.hotel_ico) + "]"           ), "_-][")
zacatek_adresare = ATC("\food_", m.jmeno_lic_souboru )
IF m.zacatek_adresare > 0
    jmeno_lic_souboru = SUBSTR(m.jmeno_lic_souboru, m.zacatek_adresare + 6)
ENDIF
IF LEFT(m.jmeno_lic_souboru, 2) = "d_"
    jmeno_lic_souboru = SUBSTR(m.jmeno_lic_souboru, 3)
ENDIF
RETURN jmeno_lic_souboru 


*   *   *   *   *   *   *   *   *   *   *   *
FUNCTION nahraj_lic (souburek)
fh = FOPEN(m.souburek)
IF m.fh > 0
    DO WHILE ! FEOF(m.fh)
        retizek = ALLTRIM(FGETS(fh))
        pozice = AT("=", m.retizek)
        IF pozice > 0
            varname  = ALLTRIM(LEFT(m.retizek, m.pozice-1))
            varvalue = ALLTRIM(SUBSTR(m.retizek, m.pozice+1))
*            TRY 
                    UPDATE ddnn SET var_value = varvalue WHERE var_name = m.varname
*            CATCH
*            ENDTRY 
        ENDIF
    ENDDO
    FCLOSE(m.fh)
ENDIF 


*   *   *   *   *   *   *   *   *   *   *   *
PROCEDURE iteruj_obdobi
LPARAMETERS fs_hash1, fs_string, fs_lickonec
fs_hash = alphanum ( m.fs_hash1 )
IF EMPTY(m.fs_lickonec)
    fs_lickonec=""
	IF VARTYPE(m.lic_konec) = "C" .and. .not. EMPTY(m.lic_konec)
		fs_lickonec = m.lic_konec
	ENDIF 
ENDIF 

IF .not. EMPTY(m.fs_lickonec)
        vt = fs_string + m.fs_lickonec
        s1 = s_h_a_hex(m.vt)
        IF s1 == m.fs_hash
            rrr = VAL(LEFT(m.fs_lickonec, 4))
            mmm = VAL(RIGHT(m.fs_lickonec, 2))
            v_thr = DATE( rrr, mmm, 11 )
            gcverze = LEFT(gcverze, 10) + " " + RIGHT(STR(rrr), 2) + CHR(mmm + ASC("A")-1)
        ENDIF
ELSE 
    had = CREATEOBJECT("had")
    had.set_max(33)
    had.SHOW()
    FOR rrr = YEAR(DATE()) - 1 TO YEAR(DATE()) + 22
        had.INCREMENT
        FOR mmm = 1 TO 12
            vt = fs_string + TRANS(m.rrr, "9999") + "-" + TRANSFORM(m.mmm, "@L 99")
            s1 = s_h_a_hex(m.vt)
            IF s1 == m.fs_hash
                v_thr = DATE( m.rrr, m.mmm, 11 )
                gcverze = LEFT(gcverze, 10) + " " + RIGHT(STR(m.rrr), 2) + CHR(m.mmm + ASC("A")-1)
                update_or_insert("lic_konec", TRANS(m.rrr, "9999") + "-" + TRANSFORM(m.mmm, "@L 99"), "ddnn")
                STORE 3333 TO rrr, mmm
            ENDIF
        ENDFOR
    ENDFOR
    RELEASE had
ENDIF 


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
PROCEDURE skusvthr
PRIVATE l2
IF VARTYPE(lic_dic) = "U"
	lic_dic = ""
ENDIF
IF VARTYPE(lic_kod) = "U"
	lic_kod = "ZZZZZZZZZ"
ENDIF
IF VARTYPE(lic_konec) = "U"
    lic_konec = ""
ENDIF

IF VARTYPE(lic_kod) = "U"
    v_thr = DATE() - 1
ELSE 
    l2 = m.lic_kod 
    IF DATE() > m.v_thr
        DO iteruj_obdobi WITH m.l2, LOWER(m.hotel_name + m.hotel_ico + m.hotel_dico + m.strediska_sum)
        IF DATE() > m.v_thr
            DO iteruj_obdobi WITH m.l2, LOWER(m.hotel_name + "*one*" + m.hotel_ico + m.hotel_dico + m.strediska_sum)
            IF DATE() < m.v_thr
                gityp_inst = 1
            ENDIF 
        ENDIF
    ENDIF 
**    =STRTOFILE("FoodMan:" + LOWER(m.hotel_name + m.hotel_ico + m.hotel_dico + m.strediska_sum) + CRLF, "m:\skusvthr.txt", 1)
ENDIF


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
FUNCTION s_skusvthr (ico, dic, meno, ss, lic, lk)
PRIVATE l2, v_thr
IF VARTYPE(lic_dic) = "U"
	lic_dic = ""
ENDIF
IF VARTYPE(lic_kod) = "U"
	lic_kod = "ZZZZZZZZZ"
ENDIF
IF VARTYPE(m.lk) <> "C"
    m.lk = ""
ENDIF
v_thr = DATE() - 1
IF .not. EMPTY(m.lic)
    l2 = m.lic
	DO iteruj_obdobi WITH m.l2, LOWER(m.meno + m.ico + m.dic + m.ss), m.lk
	IF DATE() > m.v_thr
		DO iteruj_obdobi WITH m.l2, LOWER(m.meno +"*one*"+ m.ico + m.dic + m.ss), m.lk
		IF m.v_thr > DATE()
			gityp_inst = 1
		ENDIF 
	ENDIF
ENDIF
RETURN m.v_thr > DATE()
