#DEFINE PNT  "."
***      1                        2                        3                                   4                 5              6             7         8      9         10            11              12             13                      14                             
***Dátum rozvozu;Odberateľ (podľa príjemcu) - IK;Odberateľ (podľa príjemcu) - názov;Číslo dodacieho listu;Číslo objednávky;Číslo faktúry;Množstvo v MJ;MJ;Cena za MJ;Kód produktu;Názov produktu;Príjemca - IK;Príjemca - názov skrátený;ičo-dodávateľa
***19.07.2019;OP714998R;MIRO TOURS s.r.o.;RD90156520;;RFI19069088;10,548;KG;3,4100;03007;Kuracie prsia natural - chladené VB, cca 5 kg;7149981R;Vývarovňa;17147522
    oc_faktury="qwqwqwqw"
    DO WHILE !FEOF(m.edisubor) OR !EMPTY(m.txtriadok)
        IF "Dátum rozvozu;Odberateľ (podľa príjemcu) - IK;"$txtriadok
        ELSE 
            c_faktury=SUBSTR(m.txtriadok,ATC(";",m.txtriadok,5)+1,ATC(";",m.txtriadok,6)-ATC(";",m.txtriadok,5)-1)
            IF m.c_faktury<>m.oc_faktury
                hladajidedi=i_max("id_edi") 
                insert_edihlav()
                SELECT edihlav
                REPLACE c_faktury WITH m.c_faktury FOR id_edi=m.hladajidedi
                txtdatum=LEFT(m.txtriadok,AT(";",m.txtriadok,1)-1)
                REPLACE dat_vyst WITH CTOD(m.txtdatum) FOR id_edi=m.hladajidedi
                REPLACE ico WITH SUBSTR(m.txtriadok,ATC(";",m.txtriadok,13)+1) FOR id_edi=m.hladajidedi
                REPLACE partner WITH "IČO:"+ALLTRIM(ico) FOR id_edi=m.hladajidedi
                REPLACE edi_stred WITH SUBSTR(m.txtriadok,ATC(";",m.txtriadok,1)+1,ATC(";",m.txtriadok,2)-ATC(";",m.txtriadok,1)-1) FOR id_edi=m.hladajidedi
                PRIVATE m.hladajico
                hladajico=SUBSTR(m.txtriadok,ATC(";",m.txtriadok,13)+1)
                IF !EMPTY(m.hladajico)
                    SELECT 0
                    IF m.issql
                        USE (m.sqlprefix+"rpartnerico") ALIAS rpart
                    ELSE
                        IF !USED("partneri")
                            USE (m.fn_partneri) IN 0 ALIAS partneri
                        ENDIF 
                        USE vpartnerico ALIAS rpart IN 0
                    ENDIF 
                    IF RECCOUNT("rpart")>0
                        SELECT rpart
                        GO TOP 
                        SELECT edihlav
                        REPLACE partner WITH rpart.partner FOR id_edi=m.hladajidedi
                    ELSE
                        USE IN rpart
                        IF m.issql
                            USE (m.sqlprefix+"rpartner_edikod") ALIAS rpart
                        ELSE
                            USE vpartner_edikod ALIAS rpart IN 0
                        ENDIF 
                        IF RECCOUNT("rpart")>0
                            SELECT rpart
                            GO TOP 
                            SELECT edihlav
                            REPLACE partner WITH rpart.partner FOR id_edi=m.hladajidedi
                        ENDIF 
                    ENDIF 
                ENDIF 
                DO pozavirej WITH "partneri, rpart"

                riadok=1
                oc_faktury=m.c_faktury
            ELSE
                riadok=m.riadok+1
            ENDIF 
            INSERT INTO edipol (id_edi, c_data, riadok, ean, kod_zak, mnozstvo, cena, nazov) VALUES ;
                (m.hladajidedi, m.cdata, m.riadok,;
                "",;
                SUBSTR(m.txtriadok,ATC(";",m.txtriadok,9)+1,ATC(";",m.txtriadok,10)-ATC(";",m.txtriadok,9)-1),;
                VAL(STRTRAN(SUBSTR(m.txtriadok,ATC(";",m.txtriadok,6)+1,ATC(";",m.txtriadok,7)-ATC(";",m.txtriadok,6)-1),",",PNT)),;
                VAL(STRTRAN(SUBSTR(m.txtriadok,ATC(";",m.txtriadok,8)+1,ATC(";",m.txtriadok,9)-ATC(";",m.txtriadok,8)-1),",",PNT)),;
                SUBSTR(m.txtriadok,ATC(";",m.txtriadok,10)+1,ATC(";",m.txtriadok,11)-ATC(";",m.txtriadok,10)-1)+" "+SUBSTR(m.txtriadok,ATC(";",m.txtriadok,7)+1,ATC(";",m.txtriadok,8)-ATC(";",m.txtriadok,7)-1))
        ENDIF 
        txtriadok=FGETS(m.edisubor)
    ENDDO
    SELECT edipol
    REPLACE ALL cena_celk WITH cena*mnozstvo
    SELECT disti id_edi INTO CURSOR idedi FROM edihlav READWRITE 
    SELECT edihlav
    SCAN ALL 
        SELECT idedi
        LOCATE FOR id_edi=edihlav.id_edi
        IF !FOUND()
            INSERT INTO idedi (id_edi) VALUES (edihlav.id_edi)
        ENDIF 
        SELECT edipol
        SUM cena_celk TO m.sumadokladu FOR id_edi=edihlav.id_edi
        SELECT edihlav 
        REPLACE suma WITH m.sumadokladu
    ENDSCAN 
    SELECT edipol
    SCAN FOR mnozstvo<0
        hladajidedi=edipol.id_edi
        SELECT idedi
        LOCATE FOR id_edi=m.hladajidedi
        IF !FOUND()
            INSERT INTO idedi (id_edi) VALUES (m.hladajidedi)
        ENDIF 
    ENDSCAN 
    SELECT idedi
    SCAN ALL 
        hladajidedi=idedi.id_edi
        druhy_doklad_z_minusu()
    ENDSCAN 

    uloz_a_zavri()
