#DEFINE PNT  "."
SELECT SPACE(99) AS p1, SPACE(99) AS p2, SPACE(99) AS p3, SPACE(99) AS p4, SPACE(99) AS p5, SPACE(99) AS p6, SPACE(99) AS p7, SPACE(99) AS p8, SPACE(99) AS p9, SPACE(99) AS p10, SPACE(99) AS p11, SPACE(99) AS p12, SPACE(99) AS p13, SPACE(99) AS p14, SPACE(99) AS p15, SPACE(99) AS p16 FROM edihlav WHERE .F. INTO CURSOR prenos READWRITE
STRTOFILE(STRTRAN(m.obsah_souboru,'""',"'"),m.gcpri_path+"g1.csv")
SELECT prenos
APPEND FROM m.gcpri_path+"g1.csv" TYPE DELIMITED
ERASE (m.gcpri_path+"g1.csv")

**     FanyGastro DL  1
**     1                    2                     3                     4                  5                   6                  7                   8                      9                 10                11                    12              13               14          15             16
**"CisloDokladu","Polozky_Artikl_Katalog","Polozky_Artikl_Nazev","Polozky_SazbaDPH","Polozky_Jednotka","Polozky_Mnozstvi","Polozky_Vratka","Polozky_JednotkovaCena","Polozky_CelkovaCena","Polozky_DPH","Polozky_DPH_CelkemSDPH","MojeFirma_IC","MojeFirma_Nazev","DatumZavozu","Adresa_Kod","AdresaKoncovehoPrijemce_Nazev"

**     FanyGastro FA  2
**     1                    2                     3                     4                  5                   6                  7                   8                9                10                    11               12              13           14                 15
**"CisloDokladu","Polozky_Artikl_Katalog","Polozky_Artikl_Nazev","Polozky_SazbaDPH","Polozky_Jednotka","Polozky_Mnozstvi","Polozky_Vratka","Polozky_CelkovaCena","Polozky_DPH","Polozky_DPH_CelkemSDPH","MojeFirma_IC","MojeFirma_Nazev","DatumZavozu","Adresa_Kod","AdresaKoncovehoPrijemce_Nazev"

riadok=1000
vsechna_id_edi = "fanygastro ids:"
SCAN FOR ALLTRIM(p1)<>"CisloDokladu"
	IF !EMPTY(prenos.p1)
		hladajidedi = i_max("id_edi")
		vsechna_id_edi = m.vsechna_id_edi + ALLTRIM(STR(m.hladajidedi,10)) + " "
		insert_edihlav()
		SELECT edihlav
		IF m.txtfanygastro2
			REPLACE c_faktury WITH ALLTRIM(prenos.p1), ico WITH ALLTRIM(prenos.p11), partner WITH LEFT(ALLTRIM(prenos.p12),30), edi_stred WITH LEFT(ALLTRIM(prenos.p14),30)
			REPLACE dat_vyst WITH CTOD(LEFT(prenos.p13,10)), suma WITH VAL(STRTRAN(ALLTRIM(prenos.p10),",",pnt))
		ELSE
			REPLACE c_faktury WITH ALLTRIM(prenos.p1), ico WITH ALLTRIM(prenos.p12), partner WITH LEFT(ALLTRIM(prenos.p13),30), edi_stred WITH LEFT(ALLTRIM(prenos.p15),30)
			REPLACE dat_vyst WITH CTOD(LEFT(prenos.p14,10))
		ENDIF
		riadok=1000
	ENDIF
	mnozstvo = val_point (prenos.p6)
	cena     = val_point (prenos.p8)
	IF m.txtfanygastro2
		cena_celkem = IIF(m.hotel_platca_dph, val_point(prenos.p8), val_point(prenos.p10))
	ELSE
		cena_celkem = IIF(m.hotel_platca_dph, val_point(prenos.p9), val_point(prenos.p11))
		IF mnozstvo <> 0
			cena = m.cena_celkem / m.mnozstvo
		ENDIF
	ENDIF
	IF LOWER(ALLTRIM(prenos.p7))="ano"
		mnozstvo    = - mnozstvo
        cena_celkem = - ABS(m.cena_celkem)
	ENDIF
	INSERT INTO edipol (id_edi,  c_data,   riadok, ean, kod_zak,              mnozstvo,   cena,   cena_celk, dan, nazov) VALUES ;
                (m.hladajidedi, m.cdata, m.riadok,  "", ALLTRIM(prenos.p2), m.mnozstvo, m.cena, m.cena_celkem, ;
		VAL(STRTRAN(ALLTRIM(prenos.p4),",",pnt)), ALLTRIM(prenos.p3)+" "+ALLTRIM(prenos.p5))
	riadok=m.riadok+1
	SELECT edihlav
	IF .NOT. m.txtfanygastro2
		IF m.hotel_platca_dph
			REPLACE suma WITH suma + val_point(prenos.p9)
		ELSE
			REPLACE suma WITH suma + val_point(prenos.p11)
		ENDIF
	ENDIF
ENDSCAN
tux(.T.,.T.,"edihlav")
edi_zurnal (m.vsechna_id_edi)
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 edihlav
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()
