*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
*
* program cte CSV soubor klientu elfetex 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-01 - importni soubor kopirovan dvakrat s casovym odstupem, aby byla jistota, ze se stahnul cely
* 2018-11-12 - zabraneno smazani vnitrnich karet
* 2018-11-14 - importovane soubory zalohovany a pokud se nemeni, import skoci
* 2019-01-22 - prejmenovani souboru dano do try-catch
*            - posunuto volani ppgm_http, aby se nevytvarel zurnal http i kdyz se csv nebude importovat
* 2019-02-12 - odmaze stejne stare soubory
*              hlidano datum importovaneho souboru
* 2022-03-24 - prehozeni cifer v chip_id kvuli jejim cteckam
*
*    *    *    *    *    *    *    *    *    *    *    *      #> ver. 2022-03-24 <#       *    *    *    *    *    *    *    *    *    *    *    *    *
vysledek = IIF( VARTYPE(vysledek) = "C", m.vysledek, "" )      + "  v.2022-03-24"

#DEFINE ODD          ";"
#DEFINE IMPORT_FILE  "i:\stravnici.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 (LOWER(JUSTPATH(sys(16))) + "\ppgm_http") WITH "http://alto:1991.Altopraha@stravovani-b.elfetex.net/" 

    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("DPP"    , 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("Tøinec" , 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("01"     , 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("03"     , 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("04"     , 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("05"     , 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("06"     , 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("07"     , 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("duchod" , firma) > 0 c_hlad WITH 3, pl_c_hlad WITH 2, typ_slevy with 2, pl_typ_sl with 2, limit WITH 0
    REPLACE FOR ATC("xxxxxx" , firma) > 0                  co_s_tim WITH "vubec nic"

    DO priprav_co_delat
SUSPEND 
    DO posli_cloveky
ENDIF 

DO promaz_stare_importy

SET POINT TO (m.o_set_point)


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

** protoze neni zadny priznak, ze importni soubor je komplet k dispozici, nakopiruju ho dvakrat po 5 vterinach a porovnam, ze obe verze jsou stejne
retv = -99
FOR kop_ii = 1 TO 3
	COPY FILE IMPORT_FILE to (m.gcpri_path + "import_file.csv")
	WAIT "kopiruju importni soubor" + STR(m.kop_ii) WINDOW TIMEOUT 3 + m.kop_ii
	COPY FILE IMPORT_FILE to (m.gcpri_path + "import_file2.csv")

	pocetsouboru = ADIR(qqq, m.gcpri_path + "import_file*.csv")
    ACTIVATE SCREEN 
	IF m.pocetsouboru = 2
	    IF qqq(1,2) = qqq(2,2) .and. qqq(1,3) = qqq(2,3) .and. qqq(1,4) = qqq(2,4) .and. qqq(1,3) > DATE() - 11
            pocetstarychsouboru = ADIR(stare, m.gcpri_path + "import_file.csv_*")
            FOR tk_ii = 1 TO m.pocetstarychsouboru
                IF qqq(1,2) = stare(tk_ii,2) .and. qqq(1,3) = stare(tk_ii,3) .and. qqq(1,4) = stare(tk_ii,4)
                    WAIT "tenhle soubor uz je naimportovan " + stare(tk_ii,1) WINDOW time 2
                    vysledek = m.vysledek + CHR(13)+CHR(10) + "tenhle soubor uz je naimportovan " + stare(tk_ii,1)
                    RETURN -99
                ENDIF 
            ENDFOR 
	        retv = importuj_csv_pokrac(m.gcpri_path + "import_file.csv")
	        EXIT 
	    ENDIF 
	ENDIF 
ENDFOR 

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)

*Jmeno;Prijmeni;Os_cislo;IDmedia;Stredisko
*AntonÝn;Adßmek;1565;00001312BAFF;05- PoboÀka Plze²

        radek     = fgets(m.fh,333) + REPLICATE(ODD, 11)
        firstname =       vrat_item()
        surname   =       vrat_item()
        client_id =       vrat_item()
        chip_id   = UPPER(vrat_item())
        firma     =       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)

*   "chip_id":"00003227C9FF"   v souboru
*   "chip_id":"0000C92732FF"   z ctecky
        chip_id  = LEFT(m.chip_id, 4) + SUBSTR(m.chip_id, 9,2) + SUBSTR(m.chip_id, 7,2) + SUBSTR(m.chip_id, 5,2) + SUBSTR(m.chip_id, 11,2)   && prehozeni kvuli jejich cteckam

        comment   = "IMPORTED (Elfetex)"

        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)

    TRY
        RENAME (jmeno_importovaneho_souboru) TO (jmeno_importovaneho_souboru + "_" + SUBSTR(STRTRAN(ttoc(DATETIME(),3), ":", "-"), 6, 11))
    CATCH
    ENDTRY

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


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
PROCEDURE promaz_stare_importy
pocetstarychsouboru = ADIR(stare, m.gcpri_path + "import_file.csv_*")
FOR tk_ii = 1 TO m.pocetstarychsouboru-1
    FOR tk_jj = tk_ii+1 TO m.pocetstarychsouboru
         IF stare(tk_ii,2) = stare(tk_jj,2)
             ? stare(tk_jj,1), stare(tk_ii,1)
             checksum1 = SYS(2007, FILETOSTR(m.gcpri_path + stare(tk_ii,1)))
             checksum2 = SYS(2007, FILETOSTR(m.gcpri_path + stare(tk_jj,1)))
             IF m.checksum1 = m.checksum2
                 DELETE FILE (m.gcpri_path + stare(tk_jj,1))
                 stare(tk_jj,2) = - m.tk_jj
             ENDIF 
         ENDIF 
    ENDFOR 
ENDFOR 
