*      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *
*
* 2014-06-23 - users.dbf se bere z hom_path, nikoli natvrdo z \d_strah
* 2014-12-15 - generuje se i soubor mistnosti
* 2015-10-23 - nastaveny cesty i pro krumlovske hotely
* 2015-11-11 - zruseno nastavovani cest, protoze se prece berou z food600.ini/[Config]EanPath
* 2017-07-03 - do eany.csv se zapisuje i nazev
* 2017-08-15 - k uzivatelum se posila i ciselne heslo (PIN) ze sloupce users.user_ean, posilaji se jen uzivatele s nejmene ctyrmistnym PINem
* 2017-11-15 - k uzivatelum se posila i cislo strediska, z nehoz smi uzivatel prijimat, bere se ze sloupce mzda
* 2017-12-03 - default pro ean_path
* 2017-12-05 - zrusena prebytecna uvozovka za users.ean
* 2018-01-19 - pri existenci databaze user_spart se users.csv generuje z ni
*
*      *      *      *      #> ver. 2018-01-19 <#       *      *      *      *      *      *      *      *

#DEFINE CRLF CHR(13)+CHR(10)

IF EMPTY(m.ean_path)
    m.ean_path = "i:\carozrout\"
ENDIF 
IF .not. DIRECTORY(m.ean_path)
    MKDIR (m.ean_path)
ENDIF 

DO uklid_stare_soubory

nepodarilo_se = ""

*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
WAIT REPLICATE("*", 11) WINDOW AT 22,22 NOWAIT 


fh = FCREATE(ean_path + "users.csv")
IF m.fh < 0
    nepodarilo_se = nepodarilo_se + "users "
ELSE 
    IF FILE (gchom_path + "user_spart.dbf")
        USE (gchom_path + "user_spart")
        SCAN FOR VAL(pin) > 999
            vysledek = '"' + ALLTRIM(user_id) + '";"' + ALLTRIM(username) + '";' + ALLTRIM(STR(pin)) + ';' + ALLTRIM(STR(-ABS(c_stredisk))) + CRLF
            FWRITE(m.fh, m.vysledek )
        ENDSCAN 
    ELSE
        USE (gchom_path + "users") ORDER user_id
        SCAN FOR VAL(user_ean) > 999
            vysledek = '"' + ALLTRIM(user_id) + '";"' + ALLTRIM(username) + '";' + ALLTRIM(STR(VAL(user_ean))) + ';' + ALLTRIM(STR(-ABS(mzda))) + CRLF
            FWRITE(m.fh, m.vysledek )
        ENDSCAN 
    ENDIF 
ENDIF 
fclose( m.fh )

*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
WAIT REPLICATE("*", 33) WINDOW AT 22,22 NOWAIT 
USE (gccen_path + "partneri") ORDER c_partner
fh = FCREATE(ean_path + "partneri.csv")
IF m.fh < 0
    nepodarilo_se = nepodarilo_se + "partneri "
ELSE 
    scan
        vysledek = ALLTRIM(STR(c_partner)) + ';"' + ALLTRIM(partner) + '"' + CRLF
        = fwrite(m.fh, m.vysledek )
    endscan
ENDIF
fclose( m.fh )

*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
WAIT REPLICATE("*", 40) WINDOW AT 22,22 NOWAIT 
IF FILE(gccen_path + "mistnosti.dbf")
    SELECT ALL kod_mist, IIF(ALLTRIM(kategorie) $ mistnost, "", ALLTRIM(kategorie) + " ") + mistnost as mistnost ;
           FROM (gccen_path + "mistnosti") INTO TABLE (m.gcpri_path + "mistnosti2")
    REPLACE ALL mistnost WITH STRTRAN(mistnost, '"', "'")

    fh = FCREATE(ean_path + "mistnosti.csv")
    IF m.fh < 0
        nepodarilo_se = nepodarilo_se + "mistnosti "
    ELSE 
        scan
            vysledek = '"' + ALLTRIM(kod_mist) + '";"' + ALLTRIM(mistnost) + '"' + CRLF
            = fwrite(m.fh, m.vysledek )
        endscan
    ENDIF 
    fclose( m.fh )
ELSE
    COPY FILE (ean_path + "partneri.csv") TO (ean_path + "mistnosti.csv")
ENDIF 

*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
WAIT REPLICATE("*", 44) WINDOW AT 22,22 NOWAIT 
SELECT c_karty, nazev, kod where nazev1 # "xxx SMAZANO xxx            " ;
       FROM (m.gccen_path + "karty") INTO TABLE (m.gcpri_path + "karty1")
INDEX on c_karty TAG c_karty

SELECT c_karty, PADR(ALLTRIM(ean),13) as kod, SPACE(30) as nazev, koef ;
       FROM (m.gccen_path + "eany") INTO TABLE (m.gcpri_path + "eeany")
SET RELATION TO c_karty INTO karty1
REPLACE ALL nazev WITH karty1.nazev
SET RELATION TO 
APPEND FROM (m.gcpri_path + "karty1")
DELETE FOR LEN(ALLTRIM(kod)) < 7
PACK

WAIT REPLICATE("*", 66) WINDOW AT 22,22 NOWAIT 
fh = FCREATE(ean_path + "eany.csv")
IF m.fh < 0
    nepodarilo_se = nepodarilo_se + "eany "
ELSE 
    SCAN
        vysledek = ALLTRIM(STR(RECNO())) + ";" + ALLTRIM(STR(c_karty)) + ';"' + ALLTRIM(kod) + '"' +  ';"' + ALLTRIM(nazev) + '";' + ALLTRIM(STR(koef,12,4))+ CRLF
        = fwrite(m.fh, m.vysledek )
    endscan
ENDIF 
fclose( m.fh )

INDEX on c_karty TAG c_karty UNIQUE 
fh = FCREATE(ean_path + "karty.csv")
IF m.fh < 0
    nepodarilo_se = nepodarilo_se + "karty "
ELSE 
    SCAN
        vysledek = ALLTRIM(STR(c_karty)) + ';"' + ALLTRIM(nazev) + '"' + CRLF
        = fwrite(m.fh, m.vysledek )
    ENDSCAN 
ENDIF
fclose( m.fh )

WAIT CLEAR 
IF .not. EMPTY(nepodarilo_se)
    = MESSAGEBOX("Nepodarilo se vytvorit soubory v adresari " + UPPER(m.ean_path) + CRLF + m.nepodarilo_se,1+48,"Karty Codeware", 6666)
ENDIF 



*  *  *  *  *       odsun starych souboru       *  *  *  *  * 
PROCEDURE uklid_stare_soubory
IF .not. DIRECTORY(m.ean_path + "stare\")
    MKDIR (m.ean_path + "stare\")
ENDIF 

pocet_souboru = ADIR(soubory, m.ean_path + "??_*.csv*")
FOR iii = 1 TO pocet_souboru
     IF soubory(iii, 3) < DATE() - 33
*          RENAME (m.ean_path + soubory(iii, 1)) TO (m.ean_path + "stare\" + soubory(iii, 1))
*          ? m.ean_path + soubory(iii, 1), m.ean_path + "stare\" + soubory(iii, 1)
     ENDIF 
ENDFOR
