*:*****************************************************************************
*:
*: Procedure file: VAH_IN
*:
*: Upraveno z SK_IN pro import vazene inventury
*:
*:*****************************************************************************
param sk_path, pripona, imp_path

if empt(m.imp_path)
    imp_path = "c:\ftp\data\"
endif

vysledek = "   v.2012-1227: "

sk_path  = sk_path  + IIF(RIGHT(m.sk_path,1) ="\", "", "\")
imp_path = imp_path + IIF(RIGHT(m.imp_path,1)="\", "", "\")

clos data

otevreno = .F.
IF FILE(m.imp_path + "vahinv" + m.pripona + ".dbf")
    otevira_se = ""
    TRY     
        otevira_se = m.sk_path + "vahinv"
        use (m.otevira_se) exclusive

        sele 0
        otevira_se = m.imp_path + "vahinv" + m.pripona
        use (m.otevira_se) Alias vahinv_x1 Exclusive

        otevreno = .T.
    CATCH 
        vysledek = m.vysledek + m.crlf + "[1#R] nelze otevrit " + UPPER(m.otevira_se)
    FINALLY
    ENDTRY 
ELSE 
    vysledek = m.vysledek + m.crlf +  "[1#R]  neexistuje " + m.imp_path + "VAHINV" + UPPER(m.pripona)
ENDIF 

IF m.otevreno

     sele vahinv_x1
     IF FSIZE('label') = 0
          ALTER TABLE (m.imp_path + "vahinv" + m.pripona) ADD COLUMN label C(1)
     ENDIF 
     SELECT DISTINCT id_kas, uzaverka, "X" as label ;
            FROM (m.imp_path + "vahinv" + m.pripona) INTO TABLE (m.pri_path + "vahinv_x")

     scan
         sele vahinv
         locate for (uzaverka + id_kas) = (vahinv_x.uzaverka + vahinv_x.id_kas) nooptimize
         if found()
            sele vahinv_x
            repl label with ""
         endif
         sele vahinv_x
     endscan

     dele for empt(label)    && vahinv_x selected
     pack
     inde on id_kas + uzaverka tag kas_uz

     SELECT vahinv_x1
     SET RELATION TO id_kas + uzaverka INTO vahinv_x
     DELETE FOR EMPTY(vahinv_x.id_kas)
     SET RELATION TO 
     PACK 
     SELECT vahinv 
     APPEND FROM (m.imp_path + "vahinv" + m.pripona)

     vysledek = m.vysledek + " ... OK"
ENDIF 
CLOSE DATABASES 
