PRIVATE m.hladajidedi
LOCAL m.subor, m.txt, m.nazov, m.cislofaktury, m.dan, m.kod_zak, m.datum_dodania, m.cena, m.ean, m.mnozstvo, m.sumadokladu, m.chybatxt, m.edi2
LOCAL nedasaotvorit, m.dico, m.ddico, m.dicdph, m.ico
m.nedasaotvorit=.f.
m.sumadokladu=0
m.chybatxt=""
IF m.edisubor>0
	=FSEEK(m.edisubor,0)
	m.txtriadok=nacitajriadok(m.edisubor)
	IF 'CisloDokladu;DatVyuctovani;DIC;NazevOrg;SumaBezDPH'$m.txtriadok
		m.txtriadok=nacitajriadok(m.edisubor)
****2023145249;2023-10-30;CZ27149897;MAX FOOD SE;667.14
		cislofaktury = LEFT(m.txtriadok, AT(";",m.txtriadok)-1)
		dat_vyst  = "-" + sss(m.txtriadok, 1)
		TRY 
			dat_vyst  = DATE(v_sss(m.dat_vyst, 1, "-"), v_sss(m.dat_vyst, 2, "-"), v_sss(m.dat_vyst, 3, "-"))
		CATCH
			dat_vyst=DATE()
		ENDTRY 
		m.dicdph  = sss(m.txtriadok, 2)
		m.ddico   = sss(m.txtriadok, 2)
		m.suma    = v_sss(m.txtriadok, 4)
		m.hladajidedi=i_max("id_edi") 
		=CURSORSETPROP("Buffering",5,"edihlav")
		=CURSORSETPROP("Buffering",5,"edipol")
		IF m.edipor=0
			INSERT INTO edihlav (id_edi, subor, c_data, edi_kod) VALUES (m.hladajidedi, "vypadok", m.cdata, m.kodedistred)
		ELSE
			INSERT INTO edihlav (id_edi, subor, c_data, edi_kod) VALUES (m.hladajidedi, SUBSTR(m.orimeno, RAT("\",m.orimeno)+1), m.cdata, m.kodedistred)
		ENDIF 
		SELECT edihlav
		GO TOP 
		REPLACE partner WITH "MAX FOOD SE",ico WITH "27149897", dic WITH m.ddico, icdph WITH m.dicdph, c_faktury WITH m.cislofaktury, dat_vyst WITH m.dat_vyst
		m.riadok=1
		DO WHILE !FEOF(m.edisubor)
			m.txtriadok=nacitajriadok(m.edisubor)
			IF "Poradi;KodZbozi;Nazev;SazbaDPH;Mnozstvi;MJ;JCbezDPH;CCbezDPH;CCvcDPH"$m.txtriadok
			ELSE 
*1;165521;GNOCCHI BRAMBOROVÉ GR.400 EMMA;;48.000;KS;1.93;92.55;92.55
*2;165522;GNOCCHI ŠPENÁTOVÉ S RICOTTOU GR.400 EMMA;;24.000;KS;2.08;50.01;50.01
				m.riadok=VAL(LEFT(m.txtriadok, AT(";",m.txtriadok)-1))
				m.nazov=sss(m.txtriadok,2)+" "+sss(m.txtriadok,5)
				m.dan=VAL(sss(m.txtriadok,3))/100+1
				m.kod_zak=sss(m.txtriadok,1)
				m.mnozstvo=VAL(STRTRAN(STRTRAN(sss(m.txtriadok,4),",",SET("POINT"))," ",""))
				IF m.mnozstvo<>0
					IF !m.hotel_platca_dph
						m.cena=VAL(STRTRAN(STRTRAN(sss(m.txtriadok,8),",",SET("POINT"))," ",""))/m.mnozstvo
					ELSE 
						m.cena=VAL(STRTRAN(STRTRAN(sss(m.txtriadok,6),",",SET("POINT"))," ",""))
					ENDIF 
					m.ean=""
					INSERT INTO edipol (id_edi, c_data, riadok, ean, kod_zak, mnozstvo, cena, cena_celk, dan, nazov) VALUES ;
						(m.hladajidedi, m.cdata,m.riadok, m.ean, m.kod_zak, m.mnozstvo, m.cena, m.mnozstvo*m.cena, m.dan, m.nazov)
						m.sumadokladu=m.sumadokladu+m.cena*m.mnozstvo
					m.riadok=m.riadok+1
				ENDIF 
			ENDIF 
		ENDDO 		
		=FCLOSE(m.edisubor)
		SELECT edihlav
		REPLACE suma WITH m.sumadokladu			
		SELECT edipol
		COUNT TO plusy FOR mnozstvo>0 AND id_edi=m.hladajidedi
		COUNT TO minusy FOR mnozstvo<0 AND id_edi=m.hladajidedi
		IF m.plusy>0 AND m.minusy>0
			m.idedi2=i_max("id_edi")
			SELECT edihlav
			SCATTER MEMVAR 
			INSERT INTO edihlav FROM MEMVAR 
			GO BOTTOM 
			REPLACE id_edi WITH m.idedi2
			SELECT edipol
			REPLACE id_edi WITH idedi2 FOR mnozstvo<0 AND id_edi=m.hladajidedi
		ENDIF 
		=tux(.t.,.t.,"edihlav")
		=tux(.t.,.t.,"edipol")
		DO pozavirej WITH "edihlav, edipol"
	ELSE
		=FCLOSE(m.edisubor)
		m.chybatxt=m.chybatxt+CHR(13)+CHR(10)+"Neznámy súbor"
	ENDIF 
ELSE
	m.nedasaotvorit=.t.
ENDIF 
IF !m.nedasaotvorit
	IF EMPTY(m.chybatxt)
		IF m.edipor=0
			TRY 
				ERASE (m.edimeno)
			CATCH
			ENDTRY 
		ELSE 
			=STRTOFILE(FILETOSTR(m.edimeno),STRTRAN(LOWER(m.orimeno),".csv",".ok"))
			TRY 
				ERASE (m.edimeno)
			CATCH
			ENDTRY 
			m.aa=FCREATE(STRTRAN(LOWER(m.orimeno),".txt",".okk"))
			=FPUTS(m.aa,"Prenos pod cislom "+ALLTRIM(STR(m.hladajidedi,10,0))+CHR(13)+CHR(10))
			=FCLOSE(m.aa)
		ENDIF 
	ELSE
		IF m.edipor=0
			TRY 
				ERASE (m.edimeno)
			CATCH
			ENDTRY 
		ELSE
			=STRTOFILE(FILETOSTR(m.edimeno),STRTRAN(LOWER(m.orimeno),".txt",".er"))
			TRY 
				ERASE (m.edimeno)
			CATCH
			ENDTRY 
			m.aa=FCREATE(STRTRAN(LOWER(m.orimeno),".txt",".err"))
			=FPUTS(m.aa,m.chybatxt+CHR(13)+CHR(10))
			=FCLOSE(m.aa)	
		ENDIF 
	ENDIF 
ENDIF 

FUNCTION nacitajriadok
PARAMETERS subtxt
LOCAL m.ret
m.ret=FGETS(m.subtxt,8192)
m.ret=STRTRAN(m.ret,CHR(237),"Ý")
m.ret=STRTRAN(m.ret,CHR(160),"á")
m.ret=STRTRAN(m.ret,CHR(150),"ľ")
m.ret=STRTRAN(m.ret,CHR(163),"ú")
m.ret=STRTRAN(m.ret,CHR(236),"ý")
m.ret=STRTRAN(m.ret,CHR(167),"ž")
m.ret=STRTRAN(m.ret,CHR(172),"Č")
m.ret=STRTRAN(m.ret,CHR(161),"í")
m.ret=STRTRAN(m.ret,CHR(130),"é")
m.ret=STRTRAN(m.ret,CHR(162),"ó")
m.ret=STRTRAN(m.ret,CHR(231),"š")
m.ret=STRTRAN(m.ret,CHR(166),"Ž")
m.ret=STRTRAN(m.ret,CHR(159),"č")
RETURN m.ret