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.chybatxt, 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
		m.txt=FGETS(m.subor)
		IF LOWER(LEFT(m.txt,6))="marko;"
			m.txt=SUBSTR(m.txt,7)
		ENDIF 
		IF LOWER(LEFT(m.txt,77))="plu;nazov;pocet;mj;cenapbezdph;cenapbezdphspolu;cenapsdph;cenapsdphspolu;pdph" OR LOWER(LEFT(m.txt,78))=";plu;nazov;pocet;mj;cenapbezdph;cenapbezdphspolu;cenapsdph;cenapsdphspolu;pdph"
			m.hladajidedi=i_max("id_edi") 
			IF m.issql
				USE (m.sqlprefix+"r1edihlav") ALIAS edihlav IN 0
				USE (m.sqlprefix+"r1edipol") ALIAS edipol IN 0
			ELSE 
				USE v1edihlav ALIAS edihlav IN 0
				USE v1edipol ALIAS edipol IN 0
			ENDIF 
			=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 "Marko Tatry s.r.o.",ico WITH "48324931", dic WITH " 2120143421", icdph WITH "SK2120143421", ulica WITH " Zimná 466/68", mesto WITH "Spišská Belá", psc WITH "05901"
			m.riadok=1
			DO WHILE !FEOF(m.subor)
				m.txt=FGETS(m.subor)
				DO CASE 
					CASE LEFT(m.txt,14)="Faktura cislo:"
						m.cislofaktury=SUBSTR(m.txt,15)
						SELECT edihlav
						REPLACE c_faktury WITH m.cislofaktury
					CASE LEN(m.txt)=0
					OTHERWISE 
*plu;nazov;pocet;mj;cenapbezdph;cenapbezdphspolu;cenapsdph;cenapsdphspolu;pdph
						m.kod_zak=LEFT(m.txt,AT(";", m.txt, 1)-1)
						m.nazov=SUBSTR(m.txt,AT(";",m.txt,1)+1, AT(";",m.txt,2)-AT(";",m.txt,1)-1)+" "+SUBSTR(m.txt,AT(";",m.txt,3)+1, AT(";",m.txt,4)-AT(";",m.txt,3)-1)
						m.mnozstvo=VAL(STRTRAN(SUBSTR(m.txt,AT(";",m.txt,2)+1, AT(";",m.txt,3)-AT(";",m.txt,2)-1),",",SET("POINT")))
						IF OCCURS(";",m.txt)=8
							m.dan=VAL(SUBSTR(m.txt,AT(";",m.txt,8)+1))
						ELSE
							m.dan=VAL(STRTRAN(SUBSTR(m.txt,AT(";",m.txt,8)+1, AT(";",m.txt,9)-AT(";",m.txt,8)-1),",",SET("POINT")))
						ENDIF 
						IF !m.hotel_platca_dph
							m.cena=VAL(STRTRAN(SUBSTR(m.txt,AT(";",m.txt,6)+1, AT(";",m.txt,7)-AT(";",m.txt,6)-1),",",SET("POINT")))
							m.cena_celk=VAL(STRTRAN(SUBSTR(m.txt,AT(";",m.txt,7)+1, AT(";",m.txt,8)-AT(";",m.txt,7)-1),",",SET("POINT")))
						ELSE 
							m.cena=VAL(STRTRAN(SUBSTR(m.txt,AT(";",m.txt,4)+1, AT(";",m.txt,5)-AT(";",m.txt,4)-1),",",SET("POINT")))
							m.cena_celk=VAL(STRTRAN(SUBSTR(m.txt,AT(";",m.txt,5)+1, AT(";",m.txt,6)-AT(";",m.txt,5)-1),",",SET("POINT")))
						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.kod_zak, m.mnozstvo, m.cena, m.mnozstvo*m.cena, m.dan, m.nazov)
							m.sumadokladu=m.sumadokladu+m.cena_celk
						m.riadok=m.riadok+1
				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 
			=tux(.t.,.t.,"edihlav")
			=tux(.t.,.t.,"edipol")
			DO pozavirej WITH "edihlav, edipol"
		ELSE
			=FCLOSE(m.subor)
			m.chybatxt=m.chybatxt+CHR(13)+CHR(10)+"V subore chyba prvy riadok s menami poli."
		ENDIF 
	ELSE
		m.nedasaotvorit=.t.
	ENDIF 
ELSE 
	m.chybatxt=m.chybatxt+CHR(13)+CHR(10)+"Subor neexistuje"
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 
