ean_path = "s:\dispos\"

***********  nastaveni nazvu souboru podle cisla strediska  ***********   zacatek
DO CASE 
CASE m.gic_stredisk = -151
    m.moje_cislo_skladu = "001"    && brno
CASE m.gic_stredisk = -41
    m.moje_cislo_skladu = "002"    && ccm
CASE m.gic_stredisk = -351
    m.moje_cislo_skladu = "003"    && zlicin
CASE m.gic_stredisk = -451
    m.moje_cislo_skladu = "004"    && pardubice
CASE m.gic_stredisk = -551
    m.moje_cislo_skladu = "005"    && cakovice
CASE m.gic_stredisk = -651
    m.moje_cislo_skladu = "006"    && liberec
CASE m.gic_stredisk = -751
    m.moje_cislo_skladu = "007"    && ostrava
CASE m.gic_stredisk = -851
    m.moje_cislo_skladu = "008"    && olomouc
CASE m.gic_stredisk = -951
    m.moje_cislo_skladu = "009"    && ceske budejovice
CASE m.gic_stredisk = -1051
    m.moje_cislo_skladu = "010"    && chomutov
CASE m.gic_stredisk = -1151
    m.moje_cislo_skladu = "011"    && plzen
CASE m.gic_stredisk = -1251
    m.moje_cislo_skladu = "012"    && opava
CASE m.gic_stredisk = -1351
    m.moje_cislo_skladu = "014"    && karlovy vary
CASE m.gic_stredisk = -1451
    m.moje_cislo_skladu = "015"    && usti n.l.
CASE m.gic_stredisk = -1951
    m.moje_cislo_skladu = "019"    && havirov
CASE m.gic_stredisk = -0
    m.moje_cislo_skladu = "042"    && BM brno
CASE m.gic_stredisk = -0
    m.moje_cislo_skladu = "045"    && BM pardubice
CASE m.gic_stredisk = -0
    m.moje_cislo_skladu = "051"    && BM chomutov
CASE m.gic_stredisk = -0
    m.moje_cislo_skladu = "075"    && LC jirny

OTHERWISE 
    m.moje_cislo_skladu = "xxx"
ENDCASE    
***********  nastaveni nazvu souboru podle cisla strediska  ***********    konec

DO clos_data
SET REPROCESS TO AUTOMATIC
chyba_pri_otevirani = .F.
TRY
    IF m.issql
        otevira_se = "milanovo SQL"
    ELSE
        otevira_se = m.fn_karty
        USE (m.fn_karty)    ORDER c_karty    ALIAS karty        IN 0 AGAIN
        otevira_se = m.fn_partneri
        USE (m.fn_partneri) ALIAS partneri IN 0 ORDER partner
        otevira_se = m.fn_skupiny
        USE (m.fn_skupiny)  ALIAS skupiny  IN 0 ORDER druh
        otevira_se = m.fn_bourak
        USE (m.fn_bourak)   ALIAS bourak   IN 0 ORDER nazev
        otevira_se = m.fn_sklad
        USE (m.fn_sklad)    ALIAS sklad    IN 0
        otevira_se = m.fn_doklady
        USE (m.fn_doklady)  ALIAS doklady  IN 0 ORDER c_dokl
        otevira_se = m.fn_obraty
        USE (m.fn_obraty)  ALIAS obraty    IN 0 ORDER c_dokl
        otevira_se = m.fn_eany
        USE (m.fn_eany)     ALIAS eany     IN 0 ORDER ean
    ENDIF
CATCH
    chyba_pri_otevirani = .T.
ENDTRY

IF chyba_pri_otevirani
    =MESSAGEBOX(jaz("prg_dbf_neprist_skuste_pozdeji")+CHR(13)+m.otevira_se,64,"Food600",2222)
    DO zurnal_log WITH "OPERA", "globus_imp", jaz("nejde_otevrit") + m.otevira_se
    DO clos_data
    RETURN .F.
ENDIF

SELE 0
DO buff_cre_x

*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** 

pocet_souboru = ADIR(soubory, m.ean_path + "GASTRO_" + m.moje_cislo_skladu + "_*.csv")
IF pocet_souboru > 0

    DEFINE POPUP ean_soubory MARGIN RELATIVE SHADOW COLOR SCHEME 4 FROM 5,55
    FOR iii = 1 TO pocet_souboru
        cmd = "DEFINE BAR " + STR(m.iii, 2) + " OF ean_soubory PROMPT '" + UPPER(soubory(iii, 1)) + "'"
        &cmd
    ENDFOR
    ON SELE POPUP ean_soubory deacti popup
    ACTIVATE POPUP ean_soubory
    IF .not. EMPTY(PROMPT())
        m.soubor_ean = m.ean_path + ALLTRIM(PROMPT())
        CREATE TABLE (m.gcpri_path + "pom_ex") ;
                     (c_karty I, ean C(30), mnozstvi Y, cena Y, nazev C(30), zid C(1), pz1 C(30), pz2 C(30), datum_dokl D, koef N(10,4), dodavatel C(30), c_dokl I, serial_no C(20), nazev2 C(30))
        fh = FOPEN(m.soubor_ean)
        IF m.fh > 0 
            neexistujici_eany = ""
            oddelovac = ";"
            DO while ! feof(m.fh)
*** "WGRNRCHILD";"WGRTXT";"ZID";"INFOWERT";"VANR";"ARTTXT";"ANZ";"HERSTELLER";"HERSTNAME";"DATUM";"VORGANGSNR";"LIEFNR";"MENGE1";"DUEKINV";"DUEKNETTONETTO";"VKBRUTTO"
*** 6152115;"SLANÉ OŘÍŠKY";7;"735";689980006;"G1 ARAŠÍDY SOL. 500G";1;80751;"ENCINGER S.R.O.";18.11.13;0;65000075;6;23,2072;23,2072;29,9
              radek      = fgets(m.fh,333)
              at1        = at(m.oddelovac, m.radek, 4)
              ean        = ALLTRIM(STR(INT(VAL(substr(m.radek, m.at1+1))),15))    && VANR

              at1        = at(m.oddelovac, m.radek, 12)
              mnozstvi   = VAL(substr(m.radek, m.at1+1))    && MENGE1

              at1        = at(m.oddelovac, m.radek, 14)
              cena       = VAL(substr(m.radek, m.at1+1))    && DUEKNETTONETTO

              at1        = at(';"', m.radek, 3)
              at2        = at('";', m.radek, 3)
              nazev      = substr(m.radek, m.at1+2, m.at2-m.at1-2)    && ARTTXT

              at1        = at(';', m.radek, 2)
              at2        = at(';', m.radek, 3)
              zid        = substr(m.radek, m.at1+1, m.at2-m.at1-1)    && ZID

              IF m.zid = "7"
                pz1      = ""
              ELSE 
                at1      = at(';"', m.radek, 2)
                at2      = at('";', m.radek, 2)
                pz1      = substr(m.radek, m.at1+2, m.at2-m.at1-2)    && INFOWERT
              ENDIF 

              at1        = at(';"', m.radek, 4)
              at2        = at('";', m.radek, 4)
              dodavatel  = substr(m.radek, m.at1+2, m.at2-m.at1-2)    && HERSTNAME

              at1        = at(';"', m.radek, 5)
              at2        = at('";', m.radek, 5)
              datum_dokl = substr(m.radek, m.at1+2, m.at2-m.at1-2)    && DATUM
              rok = VAL(LEFT(m.datum_dokl,4))
              mes = VAL(SUBSTR(m.datum_dokl,5,2))
              den = VAL(SUBSTR(m.datum_dokl,7,2))
              IF BETWEEN(m.rok, 2014,2222) .and. BETWEEN(m.mes,1,12) .and. BETWEEN(m.den,1,31)
                 datum_dokl = DATE(m.rok, m.mes, m.den)
              ELSE
                 datum_dokl = DATE()
              ENDIF 

              at1        = at(m.oddelovac, m.radek, 10)
              pz2        = ALLTRIM(STR(VAL(substr(m.radek, m.at1+1)),12))    && VORGANGSNR

              IF .not. EMPTY(m.ean) .and. m.mnozstvi # 0
                  SELECT eany
                  LOCATE FOR ALLTRIM(ean) = m.ean
                  IF FOUND()
                      SCATTER FIELDS c_karty, koef MEMVAR 
                      INSERT INTO (m.gcpri_path + "pom_ex") (c_karty,   ean,   mnozstvi,   cena,   nazev,   zid,   pz1,   pz2,   datum_dokl,   koef,   dodavatel) ;
                                                   values (m.c_karty, m.ean, m.mnozstvi, m.cena, m.nazev, m.zid, m.pz1, m.pz2, m.datum_dokl, m.koef, m.dodavatel)
                  ELSE
                      neexistujici_eany = m.neexistujici_eany + CHR(13) + m.nazev
                      INSERT INTO (m.gcdat_path + "eany") (ean, popis, c_karty, koef) VALUES (m.ean, m.nazev, 0, 1)
                  ENDIF 
              ENDIF 
            ENDDO 
            FCLOSE(m.fh)

            SELECT pom_ex
            SCAN
               SCATTER FIELDS dodavatel MEMVAR 
               SELECT partneri
               LOCATE FOR partner = m.dodavatel
               IF FOUND()
                  SCATTER FIELDS c_partner MEMVAR 
               ELSE
                  c_partner = i_max("c_partner")
                  insert into (m.fn_partneri) ( partner, c_partner ) values ( m.dodavatel, m.c_partner)
               ENDIF 
               SELECT pom_ex
               REPLACE c_dokl WITH m.c_partner
            ENDSCAN 
            REPLACE ALL mnozstvi WITH ROUND(mnozstvi*koef, 4), cena WITH ROUND(cena/koef, 4)
            REPLACE FOR koef # 1 serial_no WITH STRTRAN(STRTRAN(STRTRAN(ALLTRIM(STR(koef, 12,4)) + " x", "00 x", " x"),  "0 x", " x") + " " + ALLTRIM(STR(mnozstvi, 12,4)), "0"+SET("point"), SET("point"))
            SET RELATION TO c_karty INTO karty
            REPLACE ALL nazev2 WITH karty.nazev
            SET RELATION to
            BROWSE FIELDS nazev, nazev2, mnozstvi, cena, serial_no, dodavatel NOEDIT NODELETE NOAPPEND 

*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** 
            IF EMPTY(m.neexistujici_eany)
              IF MESSAGEBOX("Chcete prijmout?",3+32+256, "Import DISPOS") = 6
                SELECT disti c_dokl, datum_dokl, pz1, pz2, c_dokl as c_partner, "PE" as operace FROM (m.gcpri_path + "pom_ex") INTO TABLE (m.gcpri_path + "pom_ex_dokl")

                SELECT buf_dokl
                DELETE ALL
                APPEND FROM (m.gcpri_path + "pom_ex_dokl")

                SELECT BUFFER
                DELETE FOR mnozstvi=0 .OR. EMPTY(nazev) .OR. EMPTY(c_karty) .OR. zmaz
                APPEND FROM (m.gcpri_path + "pom_ex")
                REPLACE ALL poradi WITH RECNO(), cena_part WITH cena, operace WITH "PE"

                SELECT c_karty, SUM(mnozstvi) AS mnozstvi, $0 AS zustatek, $0 AS cena_prum, 0 AS c_mena, m.gic_stredisk AS c_stredisk ;
                       FROM BUFFER GROUP BY c_karty INTO CURSOR prep READWRITE 
                INDEX ON c_karty TAG c_karty
                INDEX ON STR(c_stredisk,12,0)+STR(c_karty,12,0) TAG csck

                gic = m.gic_stredisk
                mena = m.giakt_mena
                DO buff_out2 WITH .F., m.mena, m.gic, 0, .F.

                IF FILE(m.soubor_ean + "__")                                                && odmazani prijateho souboru
                  RENAME (m.soubor_ean) TO (m.soubor_ean + "__" + ALLTRIM(STR(RAND()*999)))
                ELSE
                  RENAME (m.soubor_ean) TO (m.soubor_ean + "__")
                ENDIF
              ENDIF 
            ELSE
              MESSAGEBOX("Nelze prijmout, protoze chybeji EANy" + CHR(13) + m.neexistujici_eany, 1+48, "Import DISPOS")
            ENDIF 
        ELSE
            WAIT "nelze otevrit soubor " + m.soubor_ean WINDOW TIMEOUT 3 AT 22,22
        ENDIF 
    ENDIF 
    RELEASE POPUP ean_soubory
ENDIF 
