*!*****************************************************************************
*!       Function: i_MAX()
*!*****************************************************************************
PARAMETERS soubor, pridaj
IF EMPTY(m.pridaj)
    pridaj = IIF(m.soubor = "c_dokl", 10, 1)
ENDIF

imx_trvani = SECONDS()
lg("CRLF --zacatek-- " + m.soubor + " " + progchain())

PRIVATE i_max, stim_sel, boladb, l_prip, l_prikaz, l_vysledok
stim_sel = SELECT()
SELECT 0
boladb=.F.
IF m.issql
    i_max = 0
    IF ATC("idriadok", m.soubor) = 0
        DO open_database

        IF !USED("teraz")
            rv("teraz","teraz", 5)
        ENDIF 
        l_prip = CURSORGETPROP("ConnectHandle","teraz")
        lg(" l_prip: " + nx(l_prip))
        IF m.l_prip > 0

            l_prikaz = "SELECT food600.m_max("+ nx(m.pridaj) + ", '" + m.soubor + "')"
            lg(" l_prikaz: " + m.l_prikaz)

            err_h_info = "pred sqlexec"
            l_vysledok = SQLEXEC(m.l_prip, m.l_prikaz, "sqlmaxima")
            err_h_info = "po sqlexec"

            IF m.l_vysledok > 0
                i_max = sqlmaxima.m_max
            ELSE
                AERROR(chybapole)
                lg(" chyba: ")
                lg(chybapole(2))
                MESSAGEBOX("SQL Chyba - M_MAX" + CHR(10) + chybapole(2), 64)
            ENDIF
        ELSE
            lg(" CURSORGETPROP(ConnectHandle,teraz) JE ZAPORNE!!! : " + nx(l_prip) )
        ENDIF

        IF m.i_max < 0
             i_max = 0
        ENDIF
        DO pozavirej WITH "sqlmaxima, teraz"
        IF .not. m.boladb
            CLOSE DATABASES
        ENDIF
    ENDIF
ELSE

    IF !FILE(m.gchom_path + "maxima.dbf")
        DO creafile WITH m.gchom_path + "maxima"
        USE
    ENDIF

    err_h_old = ON ("ERROR")

    _neotevreno = .T.
    _zacatek_otvirani = SECONDS()
    DO WHILE m._neotevreno .AND. BETWEEN(SECONDS() - _zacatek_otvirani, 0, 99)
        _neotevreno = .F.
        ON ERROR _neotevreno = .T.
        USE (m.gchom_path + "maxima.dbf") EXCLUSIVE
    ENDDO

    ON ERROR &err_h_old

    IF _neotevreno
        DO zurnal_log WITH "OPERA", "I_MAX", m.soubor
        DO CASE
            CASE  m.soubor = "c_dokl"
                SELECT MAX(c_dokl) AS max_c FROM (m.fn_doklady) INTO CURSOR ii_max
                i_max  = ii_max.max_c + 22
                USE
            CASE  m.soubor = "karty"
                SELECT MAX(c_karty) AS max_c FROM (m.fn_karty) INTO CURSOR ii_max
                i_max  = ii_max.max_c + 22
                USE
            OTHERWISE
                i_max = 12345
        ENDCASE
    ELSE
        LOCATE FOR soubor = m.soubor
        IF FOUND()
            i_max  = maxima.i_max + m.pridaj

            _nezapsano = .T.
            _zacatek_otvirani = SECONDS()
            DO WHILE m._nezapsano .AND. BETWEEN(SECONDS() - _zacatek_otvirani, 0, 5)
                _nezapsano = .F.
                ON ERROR _nezapsano = .T.
                GATHER FIELD i_max MEMVAR
            ENDDO

            IF m._nezapsano
                DO zurnal_log WITH "OPERA", "I_MAX - nelze zapsat", m.soubor, m.i_max
                i_max  = m.i_max + (9 * m.pridaj)
            ENDIF
            ON ERROR &err_h_old
        ELSE
            i_max = m.pridaj
            INSERT INTO (m.gchom_path + "maxima") FROM MEMVAR
        ENDIF
    ENDIF

    DO pozavirej WITH "maxima"
ENDIF

lg("          --hotovo i_max(" + m.soubor + ") = " + nx(m.i_max))
SELE (stim_sel)
RETURN m.i_max



*!*****************************************************************************
FUNCTION lg (txtt)
small_log(STR(SECONDS() - imx_trvani, 7,2) + m.txtt, "i_max")
