*     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     
*    importuje EDI prijemky 
*    dodavky od winefood se poznaji podle "T00:00:00Z" $ m.txtriadok, jinak je dodavatelem Cortelazzi
*
*  2021-12-20
*
*     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     


#DEFINE PNT  "."
#DEFINE ODD  ","

insert_edihlav()
DO WHILE !FEOF(m.edisubor) .OR. !EMPTY(m.txtriadok)
    IF OCCURS('"',m.txtriadok)=2                     && odstrani uvozovky a pokud mezi nimi byla carka, nahradi se teckou
        txtriadok = LEFT(m.txtriadok,AT('"',m.txtriadok,1)-1)+STRTRAN(SUBSTR(m.txtriadok,AT('"',m.txtriadok,1)+1,AT('"',m.txtriadok,2)-AT('"',m.txtriadok,1)-1),ODD,PNT)+SUBSTR(m.txtriadok,AT('"',m.txtriadok,2)+1)
    ENDIF 

    DO CASE 
    CASE ATC("CisloDokladu,DatRealizace,CisloOrg,NazevOrg,CisloStrediska,NazevStrediska,SumaBezDPH", m.txtriadok) > 0
        *nic
    CASE ATC("Poradi,IdentCode,Nazev,SazbaDPH,Mnozstvi,MJ,JCbezDPH,CCbezDPH,CCvcDPH"               , m.txtriadok) > 0
        *nic
        
    CASE OCCURS(",", m.txtriadok) = 6    && radek hlavicky
        SELECT edihlav
        REPLACE c_faktury WITH LEFT(m.txtriadok,AT(",",m.txtriadok,1)-1)
        txtdatum = SUBSTR(m.txtriadok, AT(",", m.txtriadok)+1, 10)
        REPLACE dat_vyst WITH DATE( VAL(LEFT(m.txtdatum,4)), VAL(SUBSTR(m.txtdatum,6,2)), VAL(SUBSTR(m.txtdatum,9,2)) )
        REPLACE edi_stred WITH get_item(m.txtriadok, 4)
        IF "T00:00:00Z" $ m.txtriadok
            REPLACE partner WITH "Wine Food Market s.r.o.", ulica WITH "Strakonická 1353/3", mesto WITH "Praha 5"  , psc WITH "15000", ico WITH "28218591"
        ELSE
            REPLACE partner WITH "Cortelazzi"             , ulica WITH "Chráštany 207"     , mesto WITH "Chráštany", psc WITH "25219", ico WITH "11112222"
            REPLACE edi_stred WITH ALLTRIM(edi_stred) + " " + get_item(m.txtriadok, 5)
        ENDIF 

    CASE OCCURS(",",m.txtriadok) > 6
        INSERT INTO edipol (id_edi, c_data, riadok, ean, kod_zak, mnozstvo, cena, cena_celk, dan, nazov) VALUES ;
                    (m.hladajidedi, m.cdata, 400+VAL(LEFT(m.txtriadok,ATC(",",m.txtriadok,1)-1)), "", get_item(m.txtriadok, 1),;
                    VAL(STRTRAN(get_item(m.txtriadok,4),",",PNT)),;
                    VAL(STRTRAN(get_item(m.txtriadok,6),",",PNT)),;
                    VAL(STRTRAN(get_item(m.txtriadok,7),",",PNT)),;
                    VAL(STRTRAN(get_item(m.txtriadok,3),",",PNT)),;
                        STRCONV(get_item(m.txtriadok,2) + " " + get_item(m.txtriadok,5),11))
        SELECT edihlav
        IF m.hotel_platca_dph
            REPLACE suma WITH suma+VAL(STRTRAN(get_item(m.txtriadok,6),",",PNT)) *  VAL(STRTRAN(get_item(m.txtriadok,4),",",PNT))
        ELSE
            REPLACE suma WITH suma+VAL(STRTRAN(get_item(m.txtriadok,6),",",PNT)) * (VAL(STRTRAN(get_item(m.txtriadok,3),",",PNT)) / 100 + 1) * VAL(STRTRAN(get_item(m.txtriadok,4),",",PNT))
        ENDIF  
    ENDCASE 
    txtriadok = FGETS(m.edisubor)
ENDDO

SELECT edipol
IF !m.hotel_platca_dph
    REPLACE ALL cena WITH cena * (dan/100+1), cena_celk WITH cena_celk * (dan/100+1), dan WITH 0
ENDIF 

druhy_doklad_z_minusu()
uloz_a_zavri( "winefood" )


*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** 
FUNCTION get_item(texticek, poradi)

pozice = AT(ODD, m.texticek, m.poradi) + 1
delka  = AT(ODD, m.texticek + ",", m.poradi + 1) - m.pozice
retv   = SUBSTR(m.texticek, m.pozice, m.delka)

IF LEFT(m.retv, 1) = '"' .and. RIGHT(m.retv, 1) = '"'
    retv = SUBSTR(m.retv, 2, LEN(m.retv) - 2)
ENDIF 
RETURN m.retv
