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.txt=nacitajriadok(m.edisubor)
	IF LOWER(m.txt)='"dodávateľ'
		m.partner=nacitajriadok(m.edisubor)
		DO WHILE .t.
			m.txt=nacitajriadok(m.edisubor)
			DO CASE 
				CASE LEFT(m.txt,4)="IČO:"
					m.dico=ALLTRIM(SUBSTR(m.txt,5))
				CASE LEFT(m.txt,4)="DIČ:"
					m.ddico=ALLTRIM(SUBSTR(m.txt,5))
				CASE LEFT(m.txt,7)="IČ DPH:"
					m.dicdph=ALLTRIM(SUBSTR(m.txt,8))
				CASE LEFT(m.txt,18)="Variabilný symbol:"
					m.cislofaktury=ALLTRIM(SUBSTR(m.txt,19))
				CASE LEFT(m.txt,19)=';;;;;;;;"Odberateľ:'
					EXIT
			ENDCASE 
		ENDDO
		DO WHILE .t.
			m.txt=nacitajriadok(m.edisubor)
			DO CASE 
				CASE LEFT(m.txt,4)="IČO:"
					m.ico=ALLTRIM(SUBSTR(m.txt,5))
					IF !INLIST(m.ico, m.hotel_ico,"")
						m.chybatxt=m.chybatxt+CHR(13)+CHR(10)+"Doklad nie je pre tento hotel. Je pre ICO:"+m.ico
						EXIT
					ENDIF 
				CASE LEFT(m.txt,15)="Dátum dodania :"
					m.datum_dodania=CTOD(ALLTRIM(SUBSTR(m.txt,16)))
				CASE m.txt="Kód produktu;;Názov produktu;;;Množstvo;;;;MJ;Cena bez DPH;;Cena bez DPH;;DPH;Cena"
					EXIT
			ENDCASE 
		ENDDO 
		IF EMPTY(m.chybatxt)
			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 m.partner,ico WITH m.dico, dic WITH m.ddico, icdph WITH m.dicdph, c_faktury WITH m.cislofaktury, dat_vyst WITH m.datum_dodania
			m.riadok=1
			DO WHILE !FEOF(m.edisubor)
				m.txt=nacitajriadok(m.edisubor)
				IF "%"$m.txt
					m.nazov=sss(m.txt,1)+" "+sss(m.txt,9)
					m.dan=VAL(STRTRAN(sss(m.txt,14),"%",""))/100+1
					m.kod_zak=""
					m.mnozstvo=VAL(STRTRAN(STRTRAN(sss(m.txt,7),",",SET("POINT"))," ",""))
					IF m.mnozstvo<>0
						IF !m.hotel_platca_dph
							m.cena=VAL(STRTRAN(STRTRAN(sss(m.txt,15),",",SET("POINT"))," ",""))/m.mnozstvo
						ELSE 
							m.cena=VAL(STRTRAN(STRTRAN(sss(m.txt,11),",",SET("POINT"))," ",""))
						ENDIF 
						m.ean=LEFT(m.txt,AT(";",m.txt,1)-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
						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)
		ENDIF 
	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),".csv",".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),".csv",".er"))
			TRY 
				ERASE (m.edimeno)
			CATCH
			ENDTRY 
			m.aa=FCREATE(STRTRAN(LOWER(m.orimeno),".csv",".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

		
