PARAMETERS edimeno, edipor, orimeno, m.kodedistred
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.edi2
LOCAL nedasaotvorit
m.nedasaotvorit=.f.
m.sumadokladu=0
m.chybatxt=""
IF VARTYPE(m.edimeno)=="C" AND FILE(m.edimeno)
	m.subor=FOPEN(m.edimeno)
	IF m.subor>0
		IF LOWER(LEFT(FILETOSTR(m.edimeno),4))="hdr;"
			DO WHILE !FEOF(m.subor)
				m.txt=FGETS(m.subor)
				DO CASE 
					CASE LOWER(LEFT(m.txt,4))="hdr;"
						m.hladajidedi=i_max("id_edi") 
				        otevri_edi_hlavpol()
						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 "DMI TRADING SK s.r.o., s.r.o.",ico WITH "35836075", dic WITH "2020247812", icdph WITH "SK2020247812", ulica WITH "Trenčianska 53", mesto WITH "Bratislava", psc WITH "82109"
						REPLACE c_faktury WITH sss(m.txt,1)
						REPLACE dat_vyst WITH CTOD(sss(m.txt,2))
					CASE LOWER(LEFT(m.txt,4))="lin;"
*LIN;13;Múka hladká 00 Extra 1kg;8586001080040;KS;20.0;2.0000;0.8300;1.6600
						m.riadok=v_sss(m.txt,1)
						IF m.riadok>0

							m.nazov=sss(m.txt,2)
							m.dan=v_sss(m.txt,5)
							m.cena=v_sss(m.txt,7)
							IF !m.hotel_platca_dph
								m.cena=m.cena*(1+m.dan/100)
							ENDIF 
							m.mnozstvo=v_sss(m.txt,6)
							m.kod_zak=sss(m.txt,3)
							m.ean=sss(m.txt,3)
							IF LEN(ALLTRIM(m.kod_zak))>7
								m.kod_zak=""
							ELSE
								m.ean=""
							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, 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
						ENDIF 
				ENDCASE 
			ENDDO 		
			=FCLOSE(m.subor)
			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 
			uloz_a_zavri_hlav_a_pol()
		ELSE 
			m.chybatxt=m.chybatxt+CHR(13)+CHR(10)+"Subor nema na zaciatku HDR"
		ENDIF 
	ELSE
		m.nedasaotvorit=.t.
	ENDIF 
ELSE 
	m.chybatxt=m.chybatxt+CHR(13)+CHR(10)+"Subor neexistuje"
ENDIF 
era_and_ok_or_err()


** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** 
FUNCTION v_sss (texticek, pozice, oddelovac)
LOCAL textik
m.textik=sss (texticek, pozice, oddelovac)
IF SET("point")="."
	RETURN VAL(STRTRAN( m.textik, "," ,"."))
ELSE
	RETURN VAL(STRTRAN( m.textik, "." ,","))
ENDIF 

** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** 
FUNCTION sss (texticek, pozice, oddelovac)
PRIVATE ALL 
IF EMPTY(m.oddelovac)
    oddelovac = ";"
ENDIF 
pom = SUBSTR(m.texticek, AT(m.oddelovac, m.texticek, m.pozice) + 1) + m.oddelovac
IF AT(m.oddelovac, m.pom,1)>0
	m.pom=LEFT(m.pom, AT(m.oddelovac, m.pom,1)-1)
ELSE 
	m.pom=STRTRAN(m.pom,CHR(13),"")
	m.pom=STRTRAN(m.pom,CHR(10),"")
ENDIF 
RETURN m.pom
