*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
*
* program cte CSV soubor klientu a posle je do datastore do tabulek clients, client_infos a users
*
* 2018-09-04 - skupina BoD interpretovana jako HPP
* 2018-10-11 - 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
* 2018-10-14 - maily jsou lowercasovany
* 2018-10-18 - pokud clovek nema mail nebo je chybny, do USERS je fejkovan z ID
*
*    *    *    *    *    *    *    *    *    *    *    *      #> ver. 2018-11-13 <#       *    *    *    *    *    *    *    *    *    *    *    *    *

#DEFINE ODD          ";"
#DEFINE IMPORT_FILE  "i:\stravnici_psp.csv"
#DEFINE CRLF         CHR(13)+CHR(10)

mail_domain = "@psp.cz"

o_set_point = SET("POINT")

HTTP_READ_ONLY_MODE = .T.

DO (LOWER(JUSTPATH(sys(16))) + "\http_ppgm") WITH "https://alto:1991.Altopraha@btr.psp.cz/"
DO (LOWER(JUSTPATH(sys(16))) + "\ppgm_klienti") WITH "loguj"

konec_txt = ',"expiration_date":"2055-05-25","company":{"dic":"","ico":"3","name":"2"}} '

RETURN 


ACTIVATE SCREEN 
IF importuj_csv() > 0
    DO stahni_webovy_klienty
    DO priprav_co_delat
    
    SELECT stravnici_karty_web
    REPLACE ALL                        c_hlad WITH 9, limit WITH 0    && default pro nenalezenou firmu
    REPLACE FOR ATC("HPP" , firma) > 0 c_hlad WITH 3, limit WITH -9999
*!*        REPLACE FOR ATC("brig", firma) > 0 c_hlad WITH 3, limit WITH 0

    ACTIVATE SCREEN 
    BROWSE 
*    SUSPEND 

    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)

*!*    Jirí   ;Hyb       ;TISK; Hyb Jirí PhDr.     ;2160; HybsJ@psp.cz       ;2; hybsj2018       ;HYB       ; 10085; 045D64D25A4E80
*!*    Radmila;Korencíková;HSO ; Korencíková Radmila ;2502; KorencikovaR@psp.cz;2; korencikovar2018;KORENCÍKOVÁ; 10336; 046511BA5E3F80

*!*    Jmeno;P°ÝjmenÝ;Odbor;CelÚ jmÚno;ID klienta/osobnÝ ŔÝslo;email;Group_num;Heslo;ID karty;ID ŔipovÚ karty
*!*    Jana;Achcenitovß;;Achcenitovß Jana;2529;Achcenitova@psp.cz;2;v8e84ngm;ACHCENITOV+;04924BBA5E3F80



        radek     = fgets(m.fh,333) + REPLICATE(ODD, 22)
        firstname =       vrat_item()
        surname   =       vrat_item()
        void      =       vrat_item()
        void      =       vrat_item()
        client_id =       vrat_item()
        email     = LOWER(vrat_item())
        firma     =       vrat_item()
        heslo     =       vrat_item()
        cislokhovnu =     vrat_item()
        card_id   =       m.client_id
        chip_id   = UPPER(vrat_item())
         
        IF ATC("2", m.firma) > 0
            firma = "HPP"
        ENDIF 
        IF EMPT(m.chip_id)
            chip_id = m.client_id
        ENDIF 

        IF VAL(m.client_id) # 0
            INSERT INTO stravnici_karty FROM MEMVAR 
        ENDIF 
        
    ENDDO 
    FCLOSE(m.fh)
ELSE
    WAIT "nelze otevrit soubor " + m.importni_soubor WINDOW TIMEOUT 3 AT 22,22
ENDIF 
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, '"', ''))
