*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
*
* program cte CSV soubor klientu a posle je na datastore do tabulek clients, client_infos a users
*
*
*    *    *    *    *    *    *    *    *    *    *    *      #> ver. 2022-10-04 <#       *    *    *    *    *    *    *    *    *    *    *    *    *
vysledek = IIF( VARTYPE(vysledek) = "C", m.vysledek, "" )      + "  v.2022-10-04"

#DEFINE ODD          ";"
#DEFINE IMPORT_FILE  "i:\Seznam osob_10_2022.csv"

mail_domain = "@fejk.cz"

o_set_point = SET("POINT")

*HTTP_READ_ONLY_MODE = .T.

DO (LOWER(JUSTPATH(sys(16))) + "\ppgm_stravnici") && WITH "logovat"

ACTIVATE SCREEN 
_screen.Cls

IF importuj_csv() > 0
    DO ppgm_http  WITH "https://admin:admin@d02-backend.production-d.asrv.cz/"

    DO stahni_webovy_klienty
    
    SELECT stravnici_karty_web
    REPLACE FOR ISNULL(card_id) card_id WITH client_id
    REPLACE FOR EMPTY(card_id)  card_id WITH client_id
    REPLACE FOR EMPTY(has_passwd) heslo WITH TRANSFORM(INT(RAND()*888)+101, "@L 999")
    REPLACE ALL c_hlad WITH 2, pl_c_hlad WITH 2, typ_slevy with 2, pl_typ_sl with 2, limit WITH     0   && default pro nenalezenou firmu
    REPLACE FOR ATC("xxxxxx" , firma) > 0                  co_s_tim WITH "vubec nic"

    DO priprav_co_delat
    SELECT stravnici_karty_web
    DELETE FOR "DELETE" 
    REPLACE FOR EMPTY(has_passwd) heslo WITH TRANSFORM(INT(RAND()*888)+101, "@L 999")
    REPLACE ALL c_hlad WITH 2, pl_c_hlad WITH 2, typ_slevy with 2, pl_typ_sl with 2, limit WITH     0   && default pro nenalezenou firmu
    REPLACE FOR ATC("xxxxxx" , firma) > 0                  co_s_tim WITH "vubec nic"

    SELECT client_id, surname, firstname, heslo FROM stravnici_karty_web INTO TABLE i:\seznam_s_heslama
    dbf2csv()

    DO posli_cloveky
ENDIF 

SET POINT TO (m.o_set_point)


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
PROCEDURE  importuj_csv

fh = FOPEN(IMPORT_FILE)
IF m.fh > 0 
    DO while ! feof(m.fh)

*  Príjmení Jméno, Titul;Osobní císlo; Karta  
*  HAVELKA Ondrej;6487;330785368
*  HRABOVÁ Katerina;6492;330784388
*  
        radek     = fgets(m.fh,333) + REPLICATE(ODD, 11)
        surname   =       vrat_item() + "  "
        client_id =       vrat_item()
        chip_id   = UPPER(vrat_item())
        
********    prekodovani z UTF nebo v cem to vlastne maji   *********
*        surname   = STRCONV(m.surname  , 11)
*        firstname = STRCONV(m.firstname, 11)
*        firma     = STRCONV(m.firma    , 11)

        firstname = ALLTRIM(SUBST(m.surname, AT(" ", m.surname)))
        surname   = ALLTRIM(LEFT (m.surname, AT(" ", m.surname)))

        comment   = "IMPORTED (Velim)"

        WAIT surname + firstname + client_id WINDOW NOWAIT 
        IF VAL(m.client_id) # 0
            SELECT stravnici_karty
            LOCATE FOR client_id = m.client_id
            IF .not. FOUND()
                INSERT INTO stravnici_karty FROM MEMVAR 
            ENDIF 
        ENDIF 
        
    ENDDO 
    FCLOSE(m.fh)

ELSE
    WAIT "nelze otevrit soubor karet.csv" WINDOW TIMEOUT 3 AT 22,22
ENDIF 
WAIT CLEAR 
RETURN m.fh


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
FUNCTION vrat_item
PRIVATE at1, retval
at1     = at(ODD, m.radek, 1)
retval  = LEFT(m.radek, m.at1-1)
radek   = SUBSTR(m.radek, m.at1+1)
RETURN ALLTRIM(STRTRAN(m.retval, '"', ''))
