*:*********************************************************************************************************
*:     Procedure file: SK_TMP_IN.PRG
*:     parametrem je cesta k velkym eskackum a cesta k rozzipovanym prijatym tmsk z jednotlivych kas
*:
*: 2018-05-24: 
*:
*:*********************************************************************************************************
param sk_path, imp_path

imp_path = imp_path + IIF(RIGHT(m.imp_path,1)="\", "", "\")
sk_path  = sk_path  + IIF(RIGHT(m.sk_path,1) ="\", "", "\")
vysledek = " . . v.2018-0524: " + m.crlf

CLOSE DATABASES 
sk_otevreno = ""
TRY
    USE (m.sk_path + "SK_FIN "  ) IN 0 NOUPDATE 
    USE (m.sk_path + "_TMSKPLT ") IN 0
    USE (m.sk_path + "_TMSKPOL ") IN 0
    USE (m.sk_path + "_TMSKFIN ") IN 0
    USE (m.sk_path + "_TMSKTXT ") IN 0
CATCH TO skin_oErr 
    sk_otevreno = m.sk_otevreno + " nelze otevrit TMSK " + ALLTRIM(STR(skin_oErr.ErrorNo)) + " " + skin_oErr.Message + " Line:" + ALLTRIM(STR(skin_oErr.LineNo))
    vysledek = vysledek + m.sk_otevreno + m.crlf
    RETURN 
ENDTRY 

koliksouboru = adir(seznam_souboru, m.imp_path + "_tmskfin_*.dbf")
FOR iii = 1 to m.koliksouboru
    pripona = SUBSTR(JUSTSTEM(seznam_souboru(iii, 1)), 9)
    ? m.iii, m.pripona

    LOCAL skin_oErr AS Exception
    TRY 
       use (m.imp_path + "_TMSKPLT" + m.pripona) excl ALIAS plt IN 0
       use (m.imp_path + "_TMSKPOL" + m.pripona) excl ALIAS pol IN 0
       use (m.imp_path + "_TMSKFIN" + m.pripona) excl ALIAS fin IN 0
       use (m.imp_path + "_TMSKTXT" + m.pripona) excl ALIAS txt IN 0
       DO zpracuj_jeden_soubor
    CATCH TO skin_oErr 
       sk_otevreno = m.sk_otevreno + " nepristupna importovana TMSK " + m.pripona + " " + ALLTRIM(STR(skin_oErr.ErrorNo)) + " " + skin_oErr.Message + " Line:" + ALLTRIM(STR(skin_oErr.LineNo))
       vysledek = vysledek + m.sk_otevreno + m.crlf
    FINALLY
       DO pozavirej WITH "plt, pol, fin, txt"
    ENDTRY 
ENDFOR 

SELECT _tmskfin    && odmazani uctu z tmsk, ktere se jiz objevily ve velkych eskackach
SCAN 
    SCATTER FIELDS u_cis, u_cas MEMVAR 
    SELECT sk_fin
    LOCATE FOR u_cis = m.u_cis .and. u_cas = m.u_cas
    IF FOUND()                  && pokud je ucet ve velkych eskackach, bude smazan z centralovych tmsk
        SELECT _tmskplt
        DELETE FOR u_cis = m.u_cis
        SELECT _tmskpol
        DELETE FOR u_cis = m.u_cis
        SELECT _tmsktxt
        DELETE FOR u_cis = m.u_cis
        SELECT _tmskfin
        REPLACE u_cis WITH "UZAVREN"
    ENDIF 
ENDSCAN 
SELECT _tmskfin
DELETE FOR u_cis = "UZAVREN"


*:*********************************************************************************************************
PROCEDURE zpracuj_jeden_soubor

SELECT fin
vysledek = vysledek + "appending from " + JUSTFNAME(DBF()) + ":" + m.crlf
SCAN
    SCATTER FIELDS u_cis, u_cas MEMVAR 
    SELECT sk_fin
    LOCATE FOR u_cis = m.u_cis .and. u_cas = m.u_cas
    IF .not. FOUND()                  && pokud je ucet ve velkych eskackach, nebude pridan do centralovych _tmsk
        SELECT _tmskfin
        LOCATE FOR u_cis = m.u_cis .and. u_cas = m.u_cas
        IF .not. FOUND()                  && pridat do tmsk
            SELECT _tmskplt
            APPEND FOR u_cis = m.u_cis FROM (m.imp_path + "_TMSKPLT" + m.pripona)
            SELECT _tmskpol
            APPEND FOR u_cis = m.u_cis FROM (m.imp_path + "_TMSKPOL" + m.pripona)
            SELECT _tmsktxt
            APPEND FOR u_cis = m.u_cis FROM (m.imp_path + "_TMSKTXT" + m.pripona)
            SELECT _tmskfin
            APPEND FOR u_cis = m.u_cis FROM (m.imp_path + "_TMSKFIN" + m.pripona)
            ? m.u_cis + " appended"
            vysledek = vysledek + m.u_cis + m.crlf
        ENDIF         
    ENDIF 
ENDSCAN 


*!*****************************************************************************
PROC pozavirej
PARAM pomocnytxt
PRIVATE m.soub_alias, m.por1
DO WHILE "," $ m.pomocnytxt
    m.por1 = AT(",", m.pomocnytxt, 1)
    m.soub_alias = LEFT(m.pomocnytxt, m.por1-1)
    m.pomocnytxt = ALLT(SUBST(m.pomocnytxt, m.por1+1))
    IF USED(m.soub_alias)
        USE IN (m.soub_alias)
    ENDIF
ENDDO
IF USED(m.pomocnytxt)
    USE IN (m.pomocnytxt)
ENDIF

