*     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     
*
*  aktualne selectovanou databazi prevede do CSV pod stejnym jmenem do stejneho adresare
*
* 2023-05-15 - parametr urci pouziti des.tecky ("POINT") nebo carky ("COMMA") 
*            -               netisknuti trailing zeroes ("BEZNUL")
*            -               netisknuti uvozovek kolem stringu ("NOQUOTE")
* 2023-05-32 - format generovani muze urcit promennna m.dbf2csv_format, pokud je typu char
*
*     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     

PARAMETERS jak
IF EMPTY(m.jak)
    IF VARTYPE(dbf2csv_format) = "C"
        jak = m.dbf2csv_format
    ELSE
        jak = ""
    ENDIF 
ENDIF 

beznul    = (   ATC("BEZNUL", m.jak) > 0 )
des_carka = (   ATC("COMMA" , m.jak) > 0 )
uvozovky  = IIF(ATC("NOQUO" , m.jak) > 0, "", '"') 

#DEFINE ODD ";"
#DEFINE CRLF CHR(13)+CHR(10)

sel = SELECT()
db  = DBF()

outfile = JUSTPATH(m.db) + "\" + JUSTSTEM(m.db) + ".csv"

IF USED("struktura")
    USE IN struktura
ENDIF 
COPY STRUCTURE EXTENDED TO struktura
SELECT 0
USE struktura            && field_name, field_type, field_len, field_dec
hlavicka = ""
cmd = 'radek = ""'
SCAN
    DO CASE 
    CASE field_type = "C"
        hlavicka = m.hlavicka + ALLTRIM(field_name) + ODD
        cmd = m.cmd + ' + tx(' + ALLTRIM(field_name) + ')'

    CASE field_type = "I"
        hlavicka = m.hlavicka + ALLTRIM(field_name) + ODD
        cmd = m.cmd + ' + nx(' + ALLTRIM(field_name) + ', 8)'

    CASE field_type = "Y"
        hlavicka = m.hlavicka + ALLTRIM(field_name) + ODD
        cmd = m.cmd + ' + nx(' + ALLTRIM(field_name) + ', 12,4)'

    CASE field_type = "N"
        hlavicka = m.hlavicka + ALLTRIM(field_name) + ODD
        cmd = m.cmd + ' + nx(' + ALLTRIM(field_name) + ', ' + ALLTRIM(STR(field_len)) + ', ' + ALLTRIM(STR(field_dec)) + ')'

    CASE field_type = "L"
        hlavicka = m.hlavicka + ALLTRIM(field_name) + ODD
        cmd = m.cmd + ' + lx(' + ALLTRIM(field_name) + ')'

    CASE field_type = "M"
        hlavicka = m.hlavicka + ALLTRIM(field_name) + ODD
        cmd = m.cmd + ' + tx(LEFT(' + ALLTRIM(field_name) + ', 22))'

    CASE field_type = "D"
        hlavicka = m.hlavicka + ALLTRIM(field_name) + ODD
        cmd = m.cmd + ' + dx(' + ALLTRIM(field_name) + ', 10)'

    CASE field_type = "T"
        hlavicka = m.hlavicka + ALLTRIM(field_name) + ODD
        cmd = m.cmd + ' + dx(' + ALLTRIM(field_name) + ', 16)'

    OTHERWISE 
        hlavicka = m.hlavicka + ALLTRIM(field_name) + " " + field_type + ALLTRIM(STR(field_len)) + "." + ALLTRIM(STR(field_dec)) + ODD
    ENDCASE 
ENDSCAN 

STRTOFILE(m.hlavicka + CRLF, m.outfile)

SELECT (m.sel)
WAIT m.outfile + "   " + m.jak WINDOW TIMEOUT 1
WAIT m.outfile + "   " + m.jak WINDOW NOWAIT 
SCAN
    &cmd
    STRTOFILE(m.radek + CRLF, m.outfile, 1)
ENDSCAN
WAIT CLEAR 





********** vraci formatovane cislo **********
FUNCTION nx (cislo, delka, des_mist)
IF EMPTY(m.des_mist)
    des_mist = 0
ENDIF 

IF m.des_carka
    pom = STRTRAN(STR(cislo, m.delka, m.des_mist), ".", ",")
ELSE
    pom = STRTRAN(STR(cislo, m.delka, m.des_mist), ",", ".")
ENDIF 

IF m.beznul .and. des_mist > 1
    IF RIGHT(m.pom, m.des_mist) = REPLICATE("0", m.des_mist)
        pom = LEFT(m.pom, m.delka - m.des_mist + 1) + SPACE(m.des_mist - 1)
    ENDIF 
ENDIF 

RETURN m.pom + ODD


********** vraci string + uvozovky  **********
FUNCTION tx (retez)
RETURN m.uvozovky + STRTRAN(STRTRAN(retez, m.uvozovky, "'"), ODD, ",") + m.uvozovky + ODD


********** vraci datum jako string YYYY-MM-DD + uvozovky  **********
FUNCTION dx (dat, delka)
RETURN m.uvozovky + LEFT(TTOC(m.dat, 3), m.delka) + m.uvozovky + ODD


********** vraci bool jako string  **********
FUNCTION lx (boo)
RETURN IIF(m.boo, "Yes", " n ") + ODD

