#DEFINE INFILE  "i:\Nab088000001.XML"
#DEFINE CRLF  CHR(13) + CHR(10)
#DEFINE ICO_DODAVATELE "48109975"

SET SAFETY OFF 
SET DELETED ON 

obsah = FILETOSTR(INFILE)

CREATE TABLE (m.gcpri_path + "cenik") FREE (nazev1 C(99), nazev2 C(99), nazev3 C(99), barcode C(22), cena Y) 

ii = 0
DO WHILE m.ii < LEN(m.obsah)
    hlavicka = najdi_item_header()
    DO WHILE .not. m.hlavicka $ LOWER(" <nazev1> <nazev2> <nazev3> <barcode> <jcbezdanikc> <reference> ")
        hlavicka = najdi_item_header()
    ENDDO 
    policko = bez0(recode(najdi_text()))
    DO CASE 
    CASE m.hlavicka = "<nazev1>"
        APPEND BLANK
        REPLACE nazev1 WITH m.policko
        ? m.hlavicka + ": " + m.policko
    CASE m.hlavicka = "<nazev2>"
        REPLACE nazev2 WITH m.policko
    CASE m.hlavicka = "<nazev3>"
        REPLACE nazev3 WITH m.policko
    CASE m.hlavicka = "<barcode>"
        REPLACE barcode WITH LOWER(bez0(m.policko))
    CASE m.hlavicka = "<jcbezdanikc>"
        REPLACE cena WITH VAL(bez0(m.policko))
    CASE m.hlavicka = "<reference>"
        obsah = LOWER(bez0(SUBSTR(m.obsah, m.ii)))
        ii = LEN(obsah)
    OTHERWISE 
    ENDCASE 
ENDDO 

SCAN
    hleda_se = "<klic>" + allt(barcode) + "</klic><barcode>"
    delka = LEN(m.hleda_se)
    ii = AT(hleda_se, m.obsah) + m.delka
    REPLACE barcode WITH najdi_text()
    ? barcode
ENDSCAN 

zapis_sortiment(ICO_DODAVATELE)
? CRLF + PADC("   HOTOVO   ", 99, "=")


*    *    *    *    *    *    *    *    *    *    *    *    *    
FUNCTION najdi_item_header
    znak = SUBSTR(m.obsah, m.ii, 1)
    DO WHILE m.znak # "<"
        ii = m.ii + 1
        znak = SUBSTR(m.obsah, m.ii, 1)
    ENDDO 
    item_name = "<"

    DO WHILE m.znak # ">"
        ii = m.ii + 1
        znak = SUBSTR(m.obsah, m.ii, 1)
        IF m.znak # CHR(0)
            item_name = m.item_name + LOWER(m.znak)
        ENDIF 
    ENDDO 
    ii = m.ii + 1
    RETURN m.item_name


*    *    *    *    *    *    *    *    *    *    *    *    *    
FUNCTION najdi_text
    texticek = ""
    znak = SUBSTR(m.obsah, m.ii, 1)
    DO WHILE m.znak # "<"
        texticek = texticek + m.znak
        ii = m.ii + 1
        znak = SUBSTR(m.obsah, m.ii, 1)
    ENDDO 
    RETURN m.texticek


*    *    *    *    *    *    *    *    *    *    *    *    *    
FUNCTION pridej0 (txt)
    txt_s_nulama = ""
    FOR pni = 1 TO LEN(m.txt)
       txt_s_nulama = m.txt_s_nulama + CHR(0) + SUBSTR(m.txt, m.pni, 1)
    ENDFOR 
    RETURN m.txt_s_nulama

*    *    *    *    *    *    *    *    *    *    *    *    *    
FUNCTION bez0 (txt)
    RETURN STRTRAN(m.txt, CHR(0), "")


*    *    *    *    *    *    *    *    *    *    *    *    *    
FUNCTION recode (txt)
txt = strtr(m.txt, CHR( 97)+CHR(1), "š")
txt = strtr(m.txt, CHR( 89)+CHR(1), "ø")
txt = strtr(m.txt, CHR(126)+CHR(1), "ž")
txt = strtr(m.txt, CHR( 10)+CHR(1), "è")
txt = strtr(m.txt, CHR( 27)+CHR(1), "ì")
txt = strtr(m.txt, CHR( 72)+CHR(1), "ò")
txt = strtr(m.txt, CHR( 71)+CHR(1), "Ò")
txt = strtr(m.txt, CHR( 88)+CHR(1), "Ø")
txt = strtr(m.txt, CHR(111)+CHR(1), "ù")
RETURN m.txt


*    *    *    *    *    *    *    *    *    *    *    *    *    
PROCEDURE zapis_sortiment
   PARAMETERS ico_k_insertu
   USE (m.gccen_path + "sortiment_dodavatelu") ALIAS sortiment IN 0
   SELECT cenik    && (nazev1 C(99), nazev2 C(99), nazev3 C(99), barcode C(22), cena Y)
   SCAN
      popis     = ALLTRIM( nazev1 )
      ean       = ALLTRIM( barcode )
      kod       = ALLTRIM( barcode )
      cena      = cena

      IF (VAL(m.ean) > 0) .and. (.not. EMPTY(m.popis))
         ? m.popis
         SELECT sortiment
         LOCATE FOR ALLTRIM(ean) = m.ean .and. ALLTRIM(ico) = m.ico_k_insertu
         IF FOUND()
           REPLACE popis WITH m.popis, kod WITH m.kod, ean WITH m.ean, cena WITH m.cena
           ?? "update "
         ELSE
           INSERT INTO sortiment (ean,   kod,   popis,   cena,  skupina  , ico) ;
                        VALUES (m.ean, m.kod, m.popis, m.cena, "GOLD", m.ico_k_insertu)
           ?? "insert "
         ENDIF 
      ENDIF 
   ENDSCAN 
