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 "HOSSA FAMILY, s.r.o.",ico WITH "44360991", dic WITH "2120099113", icdph WITH "SK2120099113", ulica WITH "Priemyselná 4947", mesto WITH "Poprad - Matejovce", psc WITH "05951"
						REPLACE c_faktury WITH SUBSTR(m.txt,AT(";",m.txt,1)+1, AT(";",m.txt,2)-AT(";",m.txt,1)-1)
						REPLACE dat_vyst WITH CTOD(SUBSTR(m.txt,AT(";",m.txt,2)+1, AT(";",m.txt,3)-AT(";",m.txt,2)-1))
					CASE LOWER(LEFT(m.txt,4))="lin;"
						m.riadok=VAL(STRTRAN(SUBSTR(m.txt,AT(";",m.txt,1)+1, AT(";",m.txt,2)-AT(";",m.txt,1)-1),".",SET("POINT")))
						IF m.riadok>0
							m.nazov=SUBSTR(m.txt,AT(";",m.txt,2)+1, AT(";",m.txt,3)-AT(";",m.txt,2)-1)
							m.dan=VAL(STRTRAN(SUBSTR(m.txt,AT(";",m.txt,7)+1, AT(";",m.txt,8)-AT(";",m.txt,7)-1),".",SET("POINT")))
							m.cena=VAL(STRTRAN(SUBSTR(m.txt,AT(";",m.txt,10)+1, AT(";",m.txt,11)-AT(";",m.txt,10)-1),".",SET("POINT")))
							IF !m.hotel_platca_dph
								m.cena=m.cena*(1+m.dan/100)
							ENDIF 
							m.mnozstvo=VAL(STRTRAN(SUBSTR(m.txt,AT(";",m.txt,8)+1, AT(";",m.txt,9)-AT(";",m.txt,8)-1),".",SET("POINT")))
							m.kod_zak=SUBSTR(m.txt,AT(";",m.txt,3)+1, AT(";",m.txt,4)-AT(";",m.txt,3)-1)
							m.ean=SUBSTR(m.txt,AT(";",m.txt,4)+1, AT(";",m.txt,5)-AT(";",m.txt,4)-1)
							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()
