*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *
*     Importuje klienty TGAS z CSV pro kredit 
*
*   2018-01-02
*
*   *   *   *   *   *     #> ver. 2018-01-02 <#     *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *

*

*!*    	opath = SET("default")
*!*    	SET DEFAULT TO (m.hom_path+"importy")
*!*    	m.appendfile = GETFILE("CSV", "Vybrat soubor k appendovani")
*!*    	SET DEFAULT TO (opath)
gcpri_path = ".\"


CLOSE DATABASES 
CLEAR 
#DEFINE ODDELOVAC ";"
#DEFINE POCET_SLOUPCU 9

soubor_importu = LOWER(GETFILE("csv", "Vyber soubor s klienty", "Vyber",0 , "Vyber soubor s klienty"))
fh = FOPEN( m.soubor_importu )
IF m.fh < 1
    MESSAGEBOX("nelze otevrit vstupni soubor" + CHR(13) + m.soubor_importu, 1+16, "", 3333)
    RETURN 
ENDIF 
DO importuj_csv

? CHR(13) + PADC("   HOTOVO   ", 99, "=")


*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** 
FUNCTION parsuj_datum      && "27/02/17"
at1 = AT("/", dat_zmeny, 1)
at2 = AT("/", dat_zmeny, 2)

den    = VAL(LEFT (dat_zmeny, at1 - 1))
mesic  = VAL(SUBST(dat_zmeny, at1 + 1, m.at2 - at1 -1))
rok    = VAL(SUBST(dat_zmeny, at2 + 1, 2))

IF at2 > 0 .and. AT(":", cas_zmeny) > 0
    RETURN "20" + TRANSFORM(m.rok, "@L 99") + "-" + TRANSFORM(m.mesic, "@L 99") + "-" + TRANSFORM(m.den, "@L 99") + "T" + cas_zmeny
ELSE 
    RETURN "1999-11-11T11:11"
ENDIF 

*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** 
FUNCTION get_1_item

pozice = AT(ODDELOVAC, m.radek)
retv   = ALLTRIM(LEFT(  m.radek, m.pozice - 1))
radek  = SUBSTR(m.radek, m.pozice + 1)
IF LEFT(m.retv, 1) = '"' .and. RIGHT(m.retv, 1) = '"'
    retv = SUBSTR(m.retv, 2, LEN(m.retv) - 2)
ENDIF 
RETURN m.retv


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

CREATE TABLE (m.gcpri_path +  "klienti") ( jmeno_prij C(55), email C(55), ean C(13), id_zakaz C(13), dat_zmeny C(19), cas_zmeny C(5), jmeno C(33), prijmeno C(33), updated_at C(20))

DO WHILE .not. FEOF(m.fh)
*  1     2      4              5               6      7  
*JMENO;EMAIL;CISLO KARTY;CISLO ZAKAZNIKA;DATUM ZMENY;CAS ZMENY
*"Katrin Hofmann";"ule-hofmann@t-online.de";"0000020700430";"K2070043";25/10/15;"19:15"

   radek = FGETS(m.fh,999) + REPLICATE(ODDELOVAC, POCET_SLOUPCU)
   IF ATC("uuid_zpravy", m.radek) = 0
      jmeno_prij = get_1_item()
      email      = get_1_item()
      ean        = get_1_item()
      id_zakaz   = get_1_item()
      dat_zmeny  = get_1_item()
      cas_zmeny  = get_1_item()

*      datum_uctu = parsuj_datum(m.dat_trzby)

      INSERT INTO (m.gcpri_path +  "klienti") FROM MEMVAR 
   ENDIF 
ENDDO 
FCLOSE(m.fh)
? CHR(13)

SCAN FOR .not. EMPTY(jmeno_prij)
   pozice = AT(" ", jmeno_prij)
   REPLACE jmeno WITH LEFT( jmeno_prij, m.pozice - 1), prijmeno WITH SUBSTR(jmeno_prij, m.pozice + 1)
ENDSCAN 

SCAN 
   REPLACE updated_at WITH parsuj_datum()
ENDSCAN 

BROWSE 
