*************************************************************************************
*   program kontroluje, ze prislusne soubory maji idriadok typu autoinc, kdyz ne, nastavi
*
*   2017-01-26 - 
*
*************************************************************************************

IF m.issql
	RETURN
ENDIF 
IF EMPTY(m.gcpri_path)
    gcpri_path = ".\tmp\"
    DO mk_dir WITH m.gcpri_path
ENDIF 

DO autoincrementovac WITH m.gchom_path + "data\" + "davky"
DO autoincrementovac WITH m.gchom_path + "data\" + "dluhy"
DO autoincrementovac WITH m.gchom_path + "data\" + "doklady"
DO autoincrementovac WITH m.gchom_path + "data\" + "obraty"
DO autoincrementovac WITH m.gchom_path + "data\" + "sklad"
DO autoincrementovac WITH m.gchom_path + "data\" + "karty"
DO autoincrementovac WITH m.gchom_path + "data\" + "partneri"
DO autoincrementovac WITH m.gchom_path + "data\" + "umisteni"
DO autoincrementovac WITH m.gchom_path + "data\" + "polozky"


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
PROCEDURE autoincrementovac
PARAMETERS jmeno_souboru
DO clos_data
TRY 
   chybicka = ""
   SELECT 0
   USE (m.jmeno_souboru) ALIAS soubor 
   IF FSIZE("idriadok") > 0

      typ_idriadku = "X"
      COPY STRUCTURE EXTENDED FIELDS idriadok TO (m.gcpri_path + "struktuura")
      SELECT 0
      USE (m.gcpri_path + "struktuura")
      LOCATE FOR field_name = "IDRIADOK"
      typ_idriadku = field_type + IIF(field_step = 0, " ", "A")
      USE IN struktuura

      IF .not. "A" $ m.typ_idriadku
            chybicka = m.chybicka + JUSTSTEM(m.jmeno_souboru) + " nastaven autoinc "
            SELECT soubor
            USE (DBF()) EXCLUSIVE 
            IF ATC("_A.DBF", DBF()) > 0    && archivy
                  REPLACE ALL idriadok WITH RECNO() -2000999999
                  posledni = RECCOUNT() -2000999000
            ELSE 
                  REPLACE ALL idriadok WITH RECNO()
                  posledni = RECCOUNT() + 1
            ENDIF 

            ALTER TABLE (DBF()) ALTER COLUMN idriadok INTEGER AUTOINC NEXTVALUE m.posledni
            USE 
      ENDIF 
   ENDIF 

CATCH TO oErr 
    chybicka = m.chybicka + ALLTRIM(STR(oErr.ErrorNo)) + " " + oErr.Message + " Line:" + ALLTRIM(STR(oErr.LineNo))
FINALLY
    DO autoexec_log with m.chybicka
ENDTRY
