*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
*
* program cte CSV soubor klientu faurecia nyrany a posle je na datastore do tabulek clients, client_infos a users
*
* 2018-10-18 - uzivatele jsou deaktivovani jen v clients, smaze se jim chip_id a fejkuje mail. BACHA, FILTR na m.mail_domain V MAILU, aby se neodmazali administratori a altaci
*            - maily jsou lowercasovany
*            - pokud clovek nema mail nebo je chybny, do USERS je fejkovan z ID
* 2018-11-12 - zabraneno smazani vnitrnich karet
*
*    *    *    *    *    *    *    *    *    *    *    *      #> ver. 2023-03-24 <#       *    *    *    *    *    *    *    *    *    *    *    *    *
vysledek = IIF( VARTYPE(vysledek) = "C", m.vysledek, "" )      + "  v.2023-03-24"

#DEFINE ODD          ";"
#DEFINE IMPORT_FILE  "i:\stravnici_nyrany.csv"

mail_domain = "@nyrany.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 (LOWER(JUSTPATH(sys(16))) + "\ppgm_http") WITH "https://alto:1991.Altopraha@d03-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 client_id
    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("HPP"    , firma) > 0 c_hlad WITH 2, pl_c_hlad WITH 2, typ_slevy with 2, pl_typ_sl with 2, limit WITH -9999
    REPLACE FOR ATC("xxxxxx" , firma) > 0                  co_s_tim WITH "vubec nic"
    REPLACE ALL heslo WITH "999"

    DO priprav_co_delat

*SUSPEND 

    DO posli_cloveky
ENDIF 

SET POINT TO (m.o_set_point)


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

retv = -99
COPY FILE IMPORT_FILE to (m.gcpri_path + "import_file.csv")
retv = importuj_csv_pokrac(m.gcpri_path + "import_file.csv")
ERASE (m.gcpri_path + "import_file*.csv")
RETURN m.retv


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
FUNCTION importuj_csv_pokrac (jmeno_importovaneho_souboru)

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

*OsobnĘ ?Ęslo;JmÚno;NßkladovÚ st?edisko;Organiza?nĘ st?edisko;Zam?stnavatel;KĄd mÚdia
*18019158;Sultan-Beibars ABDULOV;UAP2 F 1SR+3SR;1SR D BartosA kmen - P;FAURECIA;008700A70BF0

        radek     = fgets(m.fh,333) + REPLICATE(ODD, 11)
        client_id =       vrat_item()
        surname   =       vrat_item()
        stred1    =       vrat_item()
        stred2    =       vrat_item()
        firma     =       vrat_item()
        chip_id   = UPPER(vrat_item())
        
        firstname =       LEFT(m.surname, RAT(" ", m.surname))
        surname   = ALLTRIM(SUBST(m.surname, RAT(" ", m.surname)))
*       surname   = STRCONV(m.surname  , 11)      &&&    prekodovani z UTF nebo v cem to vlastne maji

        comment   = "IMPORTED (Nyrany)"

        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 5 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, '"', ''))
