
#DEFINE CRLF CHR(13)+CHR(10)+SPACE(22)

IF EMPTY(m.edi_prijimat)     &&(m.man_edi_kod)      && na strediscich, ktera EDI neprijimaji, se vubec nespousti
    RETURN 
ENDIF 


IF VARTYPE(zacatek_casu_edi) = "U"         && zajisti, ze se drive spusti update struktur nez zpracovani EDI
    PUBLIC zacatek_casu_edi
    IF ATC("sushitime", m.wi_zakaz) = 0
	    RETURN 
	ENDIF 
ENDIF 
zacatek_casu_edi = SECONDS()

edi_machine = m.machine_semaf    && jmeno souboru na terminalovem serveru nesmi obsahovat PID, ktere je v m.machine za zavinacem
pozice_zavinace = AT("@", m.edi_machine)
IF pozice_zavinace > 0
    edi_machine = LEFT(m.edi_machine, m.pozice_zavinace - 1)
ENDIF 

IF mam_se_na_to_vykaslat()
    RETURN 
ENDIF 

FOR i=1 TO m.pocetedikodov
    edicesta = zoznamedikodov(m.i,2)
    edikod   = zoznamedikodov(m.i,1)
    IF zoznamedikodov(m.i,3)=m.cdata AND DIRECTORY(m.edicesta)
        prejmenuj_neduplicity(m.edicesta)
        LOCAL ediii, edipocet, hladajidedi, aa
        aa = ""
       
        pocet_starych = ADIR(qqq, m.edicesta + m.edi_machine + ".?6")
        FOR iii = 1 to m.pocet_starych
            IF qqq(iii, 2) < 5    && prilis male soubory
                ERASE (m.edicesta + qqq(iii, 1))
            ENDIF 
        ENDFOR 

        IF FILE(m.edicesta+m.edi_machine+".f6") AND pootvarajedi()
            puvodni_jmeno =   zjisti_puvodni_jmeno(".f6")
            spracuj_edi    (m.edicesta+m.edi_machine + ".f6", -1, m.puvodni_jmeno, m.edikod)
        ENDIF 
        IF FILE(m.edicesta+m.edi_machine+".x6") AND pootvarajedi()
            puvodni_jmeno =   zjisti_puvodni_jmeno(".x6")
            spracuj_xml    (m.edicesta+m.edi_machine + ".x6", -1, m.puvodni_jmeno, m.edikod)
        ENDIF 
        IF FILE(m.edicesta+m.edi_machine+".m6") AND pootvarajedi()
            puvodni_jmeno =   zjisti_puvodni_jmeno(".m6")
            spracujmegas   (m.edicesta+m.edi_machine + ".m6", -1, m.puvodni_jmeno, m.edikod)
        ENDIF 
        IF FILE(m.edicesta+m.edi_machine+".dmi6") AND pootvarajedi()
            puvodni_jmeno =   zjisti_puvodni_jmeno(".dmi6")
            spracujdmitrading  (m.edicesta+m.edi_machine + ".dmi6", -1, m.puvodni_jmeno, m.edikod)
        ENDIF 
        IF FILE(m.edicesta+m.edi_machine+".v6") AND pootvarajedi()
            puvodni_jmeno =   zjisti_puvodni_jmeno(".v6")
            spracujviktis  (m.edicesta+m.edi_machine + ".v6", -1, m.puvodni_jmeno, m.edikod)
        ENDIF 
        IF FILE(m.edicesta+m.edi_machine+".t6") AND pootvarajedi()
            puvodni_jmeno =   zjisti_puvodni_jmeno(".t6")
            spracujmarkotatry(m.edicesta+m.edi_machine+".t6", -1, m.puvodni_jmeno, m.edikod)
        ENDIF 
        IF FILE(m.edicesta+m.edi_machine+".d6") AND pootvarajedi()
            puvodni_jmeno =    zjisti_puvodni_jmeno(".d6")
            spracujdat      (m.edicesta+m.edi_machine + ".d6", -1, m.puvodni_jmeno, m.edikod)
        ENDIF 
        IF FILE(m.edicesta+m.edi_machine+".h6") AND pootvarajedi()
            puvodni_jmeno =    zjisti_puvodni_jmeno(".h6")
            spracujhope      (m.edicesta+m.edi_machine + ".h6", -1, m.puvodni_jmeno, m.edikod)
        ENDIF 
        
        IF !FILE(m.edicesta+m.edi_machine+".f6") AND !FILE(m.edicesta+m.edi_machine+".x6") AND !FILE(m.edicesta+m.edi_machine+".m6") AND !FILE(m.edicesta+m.edi_machine+".t6") AND !FILE(m.edicesta+m.edi_machine+".dmi6")

            DO spracuj_jednu_priponu WITH "vl*.txt"   		, "dmitrading"    , ".dmi6"
            DO spracuj_jednu_priponu WITH "*viktis*.csv"    , "viktis_csv"    , ".v6"
            DO spracuj_jednu_priponu WITH "*zeleninari*.csv", "viktis_csv"    , ".v6"

            DO spracuj_jednu_priponu WITH "mk_*.csv"        , "mk_csv"        , ".t6"
            DO spracuj_jednu_priponu WITH "megastrade*.csv" , "megastrade_csv", ".m6"
            DO spracuj_jednu_priponu WITH "*.dat"           , "x_dat"         , ".d6"

            DO spracuj_jednu_priponu WITH "edl_dl*.csv"     , "hope_csv"      , ".h6"
            DO spracuj_jednu_priponu WITH "*.csv"           , "x_edi"         , ".f6"
            DO spracuj_jednu_priponu WITH "*.edi"           , "x_edi"         , ".f6"
            DO spracuj_jednu_priponu WITH "*.inh"           , "x_edi"         , ".f6"
            DO spracuj_jednu_priponu WITH "*.txt"           , "x_edi"         , ".f6"

            DO spracuj_jednu_priponu WITH "*.xml"           , "x_xml"         , ".x6"
            DO spracuj_jednu_priponu WITH "*.isdoc"         , "x_xml"         , ".x6"

        ENDIF 
    ENDIF 
ENDFOR 

cas_spracovani   = SECONDS() - m.zacatek_casu_edi
zacatek_casu_edi = SECONDS()

DO testedistred 

cas_testedistred = SECONDS() - m.zacatek_casu_edi
IF m.cas_testedistred + m.cas_spracovani > 1
    edi_zurnal( "spracovani a testedistred trvalo:" + STR(cas_spracovani, 6,1) + STR(cas_testedistred, 6,1) + " sec" )
ELSE
    edi_zurnal( "" )
ENDIF 


*     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     
FUNCTION zjisti_puvodni_jmeno (priponka)
IF FILE                          (m.edicesta + m.edi_machine + m.priponka + "_info")
    RETURN m.edicesta + FILETOSTR(m.edicesta + m.edi_machine + m.priponka + "_info")
ELSE
    RETURN                        m.edicesta + m.edi_machine + m.priponka
ENDIF 


*     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     
PROCEDURE spracuj_jednu_priponu
PARAMETERS maska, koho_zavolat, pripona_tmp
edipocet = ADIR(dir_m , m.edicesta + maska)
kolkosanaslo=m.kolkosanaslo+m.edipocet
IF !FILE(m.edicesta + m.edi_machine + m.pripona_tmp)
    IF m.edipocet>0 AND pootvarajedi()
        FOR ediii = 1 TO m.edipocet
            puvodni_jmeno_souboru = LOWER(dir_m(m.ediii, 1))
            IF FILE(m.edicesta + m.puvodni_jmeno_souboru)

                IF ATC("x_xml", m.koho_zavolat) > 0
                    IF m.puvodni_jmeno_souboru = "vydaj.xml"
                        IF FILE(m.edicesta+"vydaj.okk")
							TRY 
	                           ERASE (m.edicesta+"vydaj.okk")
	                        CATCH
	                        ENDTRY    
                        ENDIF 
                        IF FILE(m.edicesta+"vydaj.ok")
                        	TRY 
	                            ERASE (m.edicesta+"vydaj.ok")
	                        CATCH
	                        ENDTRY 
                        ENDIF 
                    ENDIF 
                ENDIF 

                IF .not. FILE(m.edicesta + JUSTSTEM(m.puvodni_jmeno_souboru) + ".okk")
                    aa = FILETOSTR(m.edicesta + m.puvodni_jmeno_souboru)
                    TRY 
                        STRTOFILE (          m.puvodni_jmeno_souboru,     m.edicesta + m.edi_machine + m.pripona_tmp + "_info")
                        RENAME (m.edicesta + m.puvodni_jmeno_souboru) TO (m.edicesta + m.edi_machine + m.pripona_tmp)
                    CATCH
                    ENDTRY 
                    IF VARTYPE(m.hotel_edipreposlat)=="C" AND !EMPTY(m.hotel_edipreposlat) AND DIRECTORY(m.hotel_edipreposlat) AND m.hotel_edipreposlat # m.edicesta
                        hotel_edipreposlat=m.hotel_edipreposlat+IIF(RIGHT(ALLTRIM(m.hotel_edipreposlat),1)="\","","\")
                        TRY 
                            STRTOFILE(m.aa, m.hotel_edipreposlat + ALLTRIM(dir_m(m.ediii,1)))
                        CATCH
                        ENDTRY 
                    ENDIF 
                    
                    DO CASE 
                    CASE ATC("dmitrading", m.koho_zavolat) > 0
                         DO spracujdmitrading WITH m.edicesta + m.edi_machine + m.pripona_tmp, m.ediii, m.edicesta + m.puvodni_jmeno_souboru, m.edikod

                    CASE ATC("hope_csv", m.koho_zavolat) > 0
                         DO spracujhope WITH m.edicesta + m.edi_machine + m.pripona_tmp, m.ediii, m.edicesta + m.puvodni_jmeno_souboru, m.edikod

                    CASE ATC("viktis_csv", m.koho_zavolat) > 0
                         DO spracujviktis WITH m.edicesta + m.edi_machine + m.pripona_tmp, m.ediii, m.edicesta + m.puvodni_jmeno_souboru, m.edikod

                    CASE ATC("mk_csv", m.koho_zavolat) > 0
                         STRTOFILE(m.aa+"Faktura cislo:"+SUBSTR(STRTRAN(m.puvodni_jmeno_souboru,".csv",""),4)+CHR(13)+CHR(10),(m.edicesta+m.edi_machine+".t6"))
                         DO spracujmarkotatry WITH m.edicesta + m.edi_machine + m.pripona_tmp, m.ediii,m.edicesta+m.puvodni_jmeno_souboru, m.edikod

                    CASE ATC("megastrade_csv", m.koho_zavolat) > 0
                         DO spracujmegas WITH m.edicesta + m.edi_machine + m.pripona_tmp, m.ediii, m.edicesta + m.puvodni_jmeno_souboru, m.edikod

                    CASE ATC("x_dat", m.koho_zavolat) > 0
                         DO spracujdat   WITH m.edicesta + m.edi_machine + m.pripona_tmp, m.ediii, m.edicesta + m.puvodni_jmeno_souboru, m.edikod

                    CASE ATC("x_edi", m.koho_zavolat) > 0
                         DO spracuj_edi  WITH m.edicesta + m.edi_machine + m.pripona_tmp, m.ediii, m.edicesta + m.puvodni_jmeno_souboru, m.edikod

                    CASE ATC("x_xml", m.koho_zavolat) > 0
                         DO spracuj_xml  WITH m.edicesta + m.edi_machine + m.pripona_tmp, m.ediii, m.edicesta + m.puvodni_jmeno_souboru, m.edikod
                    ENDCASE 
                ENDIF 
            ENDIF 
        ENDFOR 
    ENDIF
ENDIF


*     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     
PROCEDURE pootvarajedi
DO pozavirej WITH "edihlav,edipol"
IF m.issql
    RETURN .t.
ENDIF 
IF !USED("edi_hlav")
    TRY 
       USE (m.fn_edihlav) ALIAS edi_hlav IN 0
    CATCH
    ENDTRY 
ENDIF 
IF USED("edi_hlav")
    IF !USED("edi_pol")
        TRY 
             USE (m.fn_edipol) ALIAS edi_pol IN 0
        CATCH 
        ENDTRY 
    ENDIF 
ENDIF 
RETURN USED("edi_hlav") AND USED("edi_pol")


*     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     
FUNCTION otevri_edi_hlavpol
IF m.issql
	IF !USED("edihlav")
		USE (m.sqlprefix+"r1edihlav") ALIAS edihlav IN 0
	ENDIF 
	IF !USED("edipol")
		USE (m.sqlprefix+"r1edipol")  ALIAS edipol  IN 0
	ENDIF 
ELSE 
	IF !USED("edihlav")
		USE v1edihlav                 ALIAS edihlav IN 0
	ENDIF 
	IF !USED("edipol")
		USE v1edipol                  ALIAS edipol  IN 0
	ENDIF 
ENDIF 

CURSORSETPROP("Buffering",5,"edihlav")
CURSORSETPROP("Buffering",5,"edipol")


*     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     
FUNCTION insert_edihlav

IF VARTYPE(m.provozovna) # "C"
    provozovna = ""
ENDIF
IF slovenskaverzia
    kod_edi_do_edihlav = IIF(EMPTY(provozovna), m.kodedistred, "")
ELSE
    kod_edi_do_edihlav = ""
ENDIF 

INSERT INTO edihlav (id_edi,   subor  ,  c_data,   edi_kod           ,   edi_stred ) ;
      VALUES (m.hladajidedi, m.orimeno, m.cdata, m.kod_edi_do_edihlav, m.provozovna)


*     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     
FUNCTION testedistred

LOCAL oldsel, zatvorfooddat, zatvorpartneri, zatvoredihlav
PRIVATE ico, hladajico, hladajcpartner, hladajedikod, nazev_partnera
oldsel=SELECT()
STORE .F. TO zatvorfooddat, zatvorpartneri, zatvoredihlav
STORE "xxx" TO nazev_partnera
DO open_database
DO pozavirej WITH "edi_hl"
IF m.issql
	USE (m.sqlprefix+"redihlavstrediska") IN 0 ALIAS edi_hl
ELSE
    IF !USED("edi_hlav")
        USE (m.gccen_path+"edi_hlav") IN 0 ALIAS edi_hlav
        zatvoredihlav=.t.
    ENDIF 
    USE vedihlavstrediska IN 0 ALIAS edi_hl
	****    v1edihlav
ENDIF 

IF RECCOUNT("edi_hl") > 0
    CURSORSETPROP("Buffering", 5, "edi_hl")
    IF m.issql
        USE (m.sqlprefix+"rpartneri")     IN 0 ALIAS part1
    ELSE
        IF !USED("part1")
            USE (m.gccen_path+"partneri") IN 0 ALIAS part1 AGAIN 
            CURSORSETPROP("Buffering",5,"part1")
            zatvorpartneri=.t.
        ENDIF 
    ENDIF     

    DO odstran_duplicity

    SELECT edi_hl
    REPLACE FOR EMPTY(edi_kod + edi_stred) edi_stred WITH JUSTPATH(subor)

    * z parsovani textovych souboru se do edi_hlav historicky vyplnuje edi_kod, tak ty, ktere jsou platne ve food_dat (cili v partneri.dbf), se zachovaji, 
    * ostatni se presunou do edi_stred, aby byly hledany v edi_part.dbf
    SELECT edi_hl
    SCAN FOR EMPTY(edi_stred) .and. .not. EMPTY(edi_kod) .and. edi_kod # m.hotel_edikod
        SELECT part1
        LOCATE FOR c_partner < 0 .and. ALLTRIM(edi_kod) + "<" = ALLTRIM(edi_hl.edi_kod) + "<"
        IF .not. FOUND()
            SELECT edi_hl
            REPLACE edi_stred WITH edi_kod, edi_kod WITH ""
        ENDIF 
    ENDSCAN 

    SCAN FOR .not. EMPTY(edi_stred) .and. EMPTY(edi_kod)
        hladajedikod   = ALLTRIM(edi_hl.edi_stred)
        hladajico      = ALLTRIM(IIF(EMPTY(edi_hl.ico), edi_hl.dic, edi_hl.ico))
        hladajcpartner = 0

        *                  nalezeni dodavatele           *
        SELECT part1
        IF !EMPTY(ALLTRIM(edi_hl.ico))
            IF LEFT(edi_hl.ico,1) == "E"
                LOCATE FOR ALLTRIM(edi_kod) == SUBSTR(edi_hl.ico, 2)      && hledam ICO v partneri.edi_kod
            ELSE 
                LOCATE FOR ALLTRIM(ico)==m.hladajico                      && hledam ICO nebo DIC v partneri.ico
            ENDIF 
        ELSE
             LOCATE FOR ALLTRIM(dic)==m.hladajico .AND. .not. EMPTY(dic)  && hledam ICO nebo DIC v partneri.dic
        ENDIF 
        IF FOUND()
            hladajcpartner = part1.c_partner 
            nazev_partnera = part1.partner
        ELSE                               && nenalezeno podle edi_kod, ico ani dic, musim hledat nazev partnera
            LOCATE FOR ALLTRIM(partner) + "<" == ALLT(edi_hl.partner) + "<"      && hledam partner v partneri.partner
            IF .not. FOUND()
                INSERT INTO part1 (c_partner  ,                  partner ,        ico,        dic,        icdph,        banka,        ucet,         naz1,         naz2,       naz3) ;
                      VALUES (i_max("c_partner"), ALLTRIM(edi_hl.partner), edi_hl.ico, edi_hl.dic, edi_hl.icdph, edi_hl.swift, edi_hl.iban, edi_hl.ulica, edi_hl.mesto, edi_hl.psc) 
                tux(.t.,.t.,"part1")
                IF m.issql
                    REQUERY("part1")
                ENDIF 
            ENDIF 
            LOCATE FOR ALLTRIM(partner) + "<" == ALLT(edi_hl.partner) + "<"      && ted uz se musi najit
            hladajcpartner = part1.c_partner 
            nazev_partnera = part1.partner
        ENDIF 

        *                  nalezeni komu to patri          *
        IF !EMPTY(m.hladajcpartner)
            IF USED("partkod")
                REQUERY("partkod")
            ELSE 
                IF m.issql
                    USE (m.sqlprefix+"redipartpartnerkod") IN 0 ALIAS partkod
                ELSE
                    IF !USED("edi_part")
                        USE (m.gccen_path + "edi_part") IN 0
                    ENDIF 
                    SELECT 0
                    USE vedipartpartnerkod ALIAS partkod
                    SET FILTER TO (c_partner = m.hladajcpartner) .and. (ALLTRIM(edi_kod)+"|" = ALLTRIM(m.hladajedikod)+"|")
                ENDIF 
            ENDIF 
            SELECT partkod
            COUNT TO pocet_partkod
            IF pocet_partkod > 0
                GO TOP 
                SCATTER FIELDS edi_kodstr MEMVAR 

                SELECT edi_hl
                IF EMPTY(iban)
                    REPLACE iban WITH "c_partner: " + STR(m.hladajcpartner, 9)
                ENDIF 
                IF EMPTY(partner)
                    REPLACE partner WITH m.nazev_partnera
                ENDIF 

                IF .not. EMPTY(m.edi_kodstr)
                    REPLACE edi_kod WITH m.edi_kodstr
                ELSE
                    DO najdi_id_ediprev
                ENDIF 
            ELSE 
                INSERT INTO  partkod (c_partner, edi_kod) VALUES (m.hladajcpartner, m.hladajedikod)
				SELECT partkod
                REPLACE idriadok WITH 0 FOR ISNULL(idriadok)
                tux(.t.,.t.,"partkod")
            ENDIF 


            * do food_set ulozi pocet EDI dodavatelu
*            put_ini("EDI", "edi_dodavatel_" + TRANSFORM(m.hladajcpartner, "@L 9999999"), LEFT(TTOC(DATE(), 3), 10) + " | " + m.nazev_partnera)
            jmeno_logovaciho_souboru = m.gchom_path + JUSTFNAME(LEFT(m.gchom_path, LEN(m.gchom_path)-1)) + ".ver"
            writeprivstr( "EDI", "edi_dodavatel_" + TRANSFORM(m.hladajcpartner, "@L 9999999"), LEFT(TTOC(DATE(), 3), 10) + " | " + m.nazev_partnera, m.jmeno_logovaciho_souboru )

            SELECT DISTINCT c_partner FROM partkod INTO CURSOR edi_dodavatele ORDER BY c_partner
            seznam_edi_dodav = ""
            pocet_edi_dodav  = 0
            SCAN
                seznam_edi_dodav = m.seznam_edi_dodav + ALLTRIM(STR(c_partner)) + "/"
                pocet_edi_dodav  = m.pocet_edi_dodav  + 1
            ENDSCAN 
            DO update_or_insert WITH "pocet_edi_dodav" ,    STR(m.pocet_edi_dodav)     , "FOOD_SET"
            * do food_set ulozi pocet EDI dodavatelu

        ENDIF 
    ENDSCAN 
    tux(.t.,.t.,"edi_hl")
ENDIF 
IF m.zatvoredihlav
    USE IN edi_hlav
ENDIF 
IF m.zatvorfooddat
    USE IN food_dat
ENDIF 
IF m.zatvorpartneri
    USE IN part1
ENDIF 
DO pozavirej WITH "part1, edi_hl, foodprev, partico, partkod"



*   *   *   *   *   *   *   *   *   *   *   *
FUNCTION update_or_insert (jmeno, hodnota, soubor_alias)
LOCAL zatvaraj
IF ! USED(soubor_alias)
    zatvaraj=.t.
    IF m.issql
        PRIVATE hladajmodul, hladajvarname, hladajsetupskup
        hladajmodul = "FOOD_SET"
        hladajsetupskup=""
        rv("defsubor", soubor_alias)
        CURSORSETPROP("Buffering" ,5, soubor_alias)
    ELSE
        SELECT 0
        USE (m.gchom_path + "food_set") 
    ENDIF 
ELSE
    zatvaraj=.f.
ENDIF 
IF USED(soubor_alias)
	SELECT (soubor_alias)
	LOCATE FOR var_name = m.jmeno
	IF FOUND()
	    REPLACE var_value WITH m.hodnota
	ELSE 
	    IF m.issql
	        INSERT INTO (soubor_alias) (var_name, var_value, modul) VALUES (m.jmeno, m.hodnota, "FOOD_SET")
	    ELSE
	        INSERT INTO (soubor_alias) (var_name, var_value)        VALUES (m.jmeno, m.hodnota)
	    ENDIF 
	ENDIF 
	IF zatvaraj
	    USE IN &soubor_alias
	ENDIF 
ENDIF 

*     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     
FUNCTION druhy_doklad_z_minusu()

* * * *           eliminace radku se stejnou kartou a plusovym/minusovym mnozstvim    ZACATEK
PRIVATE iiii
SELECT edipol
FOR iiii = 1 TO 333
    LOCATE FOR id_edi = m.hladajidedi .and. riadok = m.iiii .and. mnozstvo < 0
    IF FOUND()
        SCATTER FIELDS ean, kod_zak, dan, nazov, mnozstvo MEMVAR 
        LOCATE FOR id_edi = m.hladajidedi .and. riadok # m.iiii .and. ean = m.ean .and. kod_zak = m.kod_zak .and. dan = m.dan .and. nazov = m.nazov .and. mnozstvo > 0
        IF FOUND()
            suma_mnozstva = m.mnozstvo + edipol.mnozstvo
            IF m.suma_mnozstva > 0
                REPLACE                                                  mnozstvo WITH m.suma_mnozstva    &&  plusovy radek
                REPLACE FOR id_edi = m.hladajidedi .and. riadok = m.iiii mnozstvo WITH        0           && minusovy radek
            ENDIF 
        ENDIF 
    ENDIF 
ENDFOR 
DELETE FOR mnozstvo = 0
tux(.f.,.t.,"edipol")
* * * *           eliminace radku se stejnou kartou a plusovym/minusovym mnozstvim    KONEC

SUM cena_celk TO prachy_plus  FOR mnozstvo > 0 AND id_edi = m.hladajidedi
SUM cena_celk TO prachy_minus FOR mnozstvo < 0 AND id_edi = m.hladajidedi

COUNT TO plusy  FOR mnozstvo > 0 AND id_edi = m.hladajidedi
COUNT TO minusy FOR mnozstvo < 0 AND id_edi = m.hladajidedi

SELECT edihlav
LOCATE FOR id_edi = m.hladajidedi
REPLACE suma WITH m.prachy_plus

IF m.plusy > 0 AND m.minusy > 0
    idedi2 = i_max("id_edi")
    SELECT edihlav
    LOCATE FOR id_edi = m.hladajidedi
    SCATTER MEMVAR 
    INSERT INTO edihlav FROM MEMVAR 
    GO BOTTOM 
    REPLACE id_edi WITH m.idedi2, suma WITH prachy_minus
    SELECT edipol
    REPLACE id_edi WITH idedi2 FOR mnozstvo < 0 AND id_edi = m.hladajidedi

    tux(.f.,.t., "edipol" )
    tux(.f.,.t., "edihlav")
ENDIF 


*     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     
FUNCTION najdi_id_ediprev
IF slovenskaverzia
    id_ediprev=partkod.id_ediprev
    IF ISNULL(m.id_ediprev)
    	m.id_ediprev=0
    ENDIF 
    IF !USED("foodprev")
        IF m.issql
            USE (m.sqlprefix+"rfooddat_idediprev") IN 0 ALIAS foodprev
        ELSE
            IF !USED("food_dat")
                USE (m.fn_food_dat) IN 0 ALIAS food_dat
            ENDIF 
            USE vfooddat_idediprev IN 0 ALIAS foodprev
        ENDIF 
    ENDIF 
    SELECT foodprev
    LOCATE FOR id_ediprev = m.id_ediprev 
    IF FOUND()
        IF !EMPTY(foodprev.edi_kod)
            hladajedikod = ALLTRIM(foodprev.edi_kod)
        ELSE
            hladajedikod = ALLTRIM(STR(m.id_ediprev,10,0))
        ENDIF 
        SELECT edi_hl
        REPLACE edi_kod WITH m.hladajedikod, iban WITH "z foodprev"
        tux(.f.,.t.,"edi_hl")
    ENDIF 
ENDIF 


*     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     
FUNCTION edi_zurnal (texticek)
small_log(m.texticek, "edi")



*     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     
FUNCTION odstran_duplicity
SELECT edi_hl       && zruseni koncoveho (1) ze jmen souboru, opakovane downloadovanych

SCAN FOR AT(" (", subor) > 0
    pozice = AT(" (", JUSTstem(subor))
    REPLACE subor WITH JUSTPATH(subor) + "\" +  LEFT(JUSTSTEM(subor), m.pozice -1) + "." + JUSTEXT(subor)
ENDSCAN 

SELECT subor, c_faktury, edi_stred, suma, CNT(*) as pocet GROUP BY 1,2,3,4 FROM edi_hl INTO CURSOR dup_dokl HAVING pocet > 1
SCAN
    SCATTER MEMVAR 
    SELECT edi_hl
    LOCATE FOR subor = m.subor .AND. c_faktury = m.c_faktury .and. edi_stred = m.edi_stred .and. suma = m.suma .and. c_dokl = 0
    IF FOUND()
        REPLACE c_dokl WITH -999999, iban WITH ALLTRIM(iban) + " DUPLIC"
    ENDIF 
ENDSCAN 
USE IN dup_dokl
tux(.t.,.t.,"edi_hl")


*     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     
FUNCTION era_and_ok_or_err

IF VARTYPE(m.hladajidedi) # "N"
             hladajidedi = 0
ENDIF 

IF EMPTY(m.chybatxt)
    IF m.edipor # 0
        chybatxt = TTOC(DATETIME(), 3) + "          Food ver." + LEFT(m.gcverze, 10) + CRLF + "Import pod c.: " + ALLTRIM(STR(m.hladajidedi,10)) + CRLF + m.orimeno + CRLF + ktery_parser()
		IF FILE(m.edimeno)
	        RENAME   (m.edimeno) TO LEFT(m.orimeno, RAT(".", m.orimeno)) + "ok" )
	    ENDIF 
        STRTOFILE(m.chybatxt,   LEFT(m.orimeno, RAT(".", m.orimeno)) + "okk")
		IF FILE(m.edimeno + "_info")
			ERASE (m.edimeno + "_info")
		ENDIF 
        edi_zurnal (m.chybatxt)
    ENDIF 
ELSE
    chybatxt = TTOC(DATETIME(), 3) + "          Food ver." + LEFT(m.gcverze, 10) + m.chybatxt + CRLF + "Import pod c.: " + ALLTRIM(STR(m.hladajidedi,10)) + CRLF + m.orimeno + CRLF + ktery_parser()
	IF FILE(m.edimeno)
	    RENAME   (m.edimeno)      TO LEFT(m.orimeno, RAT(".", m.orimeno)) + "er" )
	ENDIF 
    STRTOFILE(m.chybatxt + CRLF, LEFT(m.orimeno, RAT(".", m.orimeno)) + "err")
ENDIF 


*     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     
FUNCTION era_and_ok (texticek)

zurnal_txt = "  Food ver." + LEFT(m.gcverze, 10) + "  Import pod c. " + m.texticek + CRLF + m.orimeno + CRLF + ktery_parser()
IF m.edipor # 0
    IF FILE( LEFT(m.orimeno, RAT(".", m.orimeno)) + "ok" )
       RENAME LEFT(m.orimeno, RAT(".", m.orimeno)) + "ok" TO LEFT(m.orimeno, RAT(".", m.orimeno)) + "ok" + CHRTRAN(TTOC(DATETIME(), 3), ":", "-")
    ENDIF 
    RENAME     (m.edimeno) TO                       LEFT(m.orimeno, RAT(".", m.orimeno)) + "ok"
    STRTOFILE  (TTOC(DATETIME(), 3) + m.zurnal_txt, LEFT(m.orimeno, RAT(".", m.orimeno)) + "okk")
    edi_zurnal (m.zurnal_txt)
ENDIF 


*     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     
FUNCTION ktery_parser
PRIVATE ktery_parser, ktery_parser1, ii
ktery_parser = ""
FOR ii = 2 TO 11
    ktery_parser1 = UPPER(PROGRAM(ii))
    IF "SPRACUJ" $ m.ktery_parser1
        ktery_parser = m.ktery_parser + " " + m.ktery_parser1
    ENDIF 
ENDFOR 
RETURN ktery_parser


*     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     
FUNCTION uloz_a_zavri_hlav_a_pol
tux(.t.,.t.,"edihlav")
tux(.t.,.t.,"edipol")
DO pozavirej WITH "edihlav, edipol"


*     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     
FUNCTION mam_se_na_to_vykaslat

prog_chain = progchain()

soubor_posledniho_obnov_edi = m.gchom_path + "edi_naposledny_obnoveno.txt"
IF ATC("TIMER", m.prog_chain) > 0    && je spusteno z timeru, nikoliv rucne

    naposledy_obnoveno = "2020-02-02"
    IF FILE(soubor_posledniho_obnov_edi)
        TRY 
            naposledy_obnoveno = FILETOSTR(m.soubor_posledniho_obnov_edi)
        CATCH
        ENDTRY 
    ENDIF 

    IF m.naposledy_obnoveno > TTOC(DATETIME() - m.edi_refresh * 60, 3)
        edi_zurnal("zpracovani vynechano, protoze nekdo jinej byl rychlejsi")
        RETURN .T.
    ENDIF 
ELSE 
    edi_zurnal("spusteno rucne")
ENDIF 
TRY 
    STRTOFILE(TTOC(DATETIME(), 3), m.soubor_posledniho_obnov_edi)
CATCH
ENDTRY 
RETURN .F.


*     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     
FUNCTION prejmenuj_neduplicity(m.edicesta)
tajm = "__" + TTOC(DATETIME(), 1)
pocet_s = ADIR(qqq, m.edicesta + "*.*")
*small_log(m.edicesta + STR(pocet_s)  , "edi")
FOR iii = 1 to m.pocet_s
    celejmeno = qqq(iii, 1)
    IF ATC("OK", JUSTEXT(m.celejmeno)) = 0 .and. FILE(m.edicesta + JUSTSTEM(m.celejmeno) + ".ok")
        obsah1 = FILETOSTR(m.edicesta + m.celejmeno)
        obsah2 = FILETOSTR(m.edicesta + JUSTSTEM(m.celejmeno) + ".ok")
        IF m.obsah1 # m.obsah2
            TRY 
                RENAME (m.edicesta + JUSTSTEM(m.celejmeno) + ".ok" ) TO (m.edicesta + JUSTSTEM(m.celejmeno) + m.tajm + ".ok" )
            CATCH
            ENDTRY 
            TRY 
                RENAME (m.edicesta + JUSTSTEM(m.celejmeno) + ".okk") TO (m.edicesta + JUSTSTEM(m.celejmeno) + m.tajm + ".okk")
            CATCH
            ENDTRY 
            small_log(m.edicesta + m.celejmeno + "    RENAME OK", "edi")
        ELSE 
*            small_log(m.edicesta + m.celejmeno + "    SAMOTNY"  , "edi")
        ENDIF 
    ENDIF 
ENDFOR 
