param sk_path, exp_soubor

IF .not. DIRECTORY(JUSTPATH(m.exp_soubor))
    MKDIR (JUSTPATH(m.exp_soubor))
endif

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

vysledek = "    v.2013-0318: " + m.crlf

CLOSE DATABASES

sk_otevreno = ""
LOCAL skin_oErr AS Exception
TRY 
    USE (m.sk_path + "umisteni") ORDER c_karty
    SET FILTER TO id_kas = "JB"
    USE (m.sk_path + "skupiny") ORDER druh IN 0
    USE (m.sk_path + "karty")              IN 0
CATCH TO skin_oErr 
       sk_otevreno = ALLTRIM(STR(skin_oErr.ErrorNo)) + " " + skin_oErr.Message + " Line:" + ALLTRIM(STR(skin_oErr.LineNo))
FINALLY
ENDTRY 

IF EMPTY(m.sk_otevreno)    
    SELECT c_karty, nazev, druh_kalk as druh, "  " as id_man, cena34 as cena1, 00.0 as dph1, 00.0 as dph2, " " as tisk ;
           WHERE .not. EMPTY(druh_kalk) .and. nazev1 # "xxx SMAZANO xxx            " ;
           FROM karty INTO TABLE (m.pri_path + "karty_septim")
    SET RELATION TO druh INTO skupiny
    REPLACE ALL dph1 WITH 100*skupiny.dan34-100, dph2 WITH 100*skupiny.dan35-100

    SET RELATION TO c_karty INTO umisteni
    REPLACE ALL id_man WITH umisteni.id_man, nazev WITH STRTR(STRTR(nazev, '"', "'"), ";", ",")	&& v nazvu nesmi byt strednik a uvozovky kvuli CSV

    SET FILTER TO .not. EMPTY(id_man)
    REPLACE ALL tisk WITH IIF(id_man = "25", "K", "B")

    fh = FCREATE(m.exp_soubor)
    IF m.fh > 0
      scan
         radek = ALLTRIM(STR(c_karty)) + ';"' + ALLTRIM(nazev) + '";"' + ALLTRIM(tisk) + '";"' + ALLTRIM(druh) + '";' 
         radek = m.radek + STRTRAN(ALLTRIM(STR(cena1,8,2)), ",", ".") + ';'
         radek = m.radek + STRTRAN(ALLTRIM(STR(dph1,5,1)) , ",", ".") + ';'
         radek = m.radek + STRTRAN(ALLTRIM(STR(dph2,5,1)) , ",", ".")
         = fwrite(m.fh, m.radek + m.crlf)
      endscan
      fclose( m.fh )
      COUNT TO pocet
      vysledek = m.vysledek + STR(m.pocet,5) + " karet ... OK"
    ELSE
      vysledek = m.vysledek + "[2#R]  soubor " + m.exp_soubor + " nelze vytvorit"
    ENDIF 
ELSE
    vysledek = vysledek + "[2#R]  " + "       ... " + m.sk_otevreno + "  "
ENDIF

CLOSE DATABASES
