PARAMETERS eedimeno, eedipor, oorimeno, m.kodedistred
PRIVATE m.hladajidedi, m.hladajedikod
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.dat_vzst, m.ico, m.suma
m.nedasaotvorit=.f.
m.sumadokladu=0
m.chybatxt=""
IF VARTYPE(m.eedimeno)=="C" AND FILE(m.eedimeno)
	=STRTOFILE(STRTRAN(FILETOSTR(m.eedimeno),"'",CHR(13)+CHR(10)),m.eedimeno,0)
	m.subor=FOPEN(m.eedimeno)
	IF m.subor>0
		IF LOWER(LEFT(FILETOSTR(m.eedimeno),4))="una:"
			hladajidedi=0
                        otevri_edi_hlavpol()
			DO WHILE !FEOF(m.subor)
				m.txt=FGETS(m.subor)
				DO CASE 
					CASE LOWER(LEFT(m.txt,4))="una:"
					CASE LOWER(LEFT(m.txt,4))="unh+"
					CASE LOWER(LEFT(m.txt,4))="unb+"
						m.txt=LOWER(m.txt)
						m.txt=SUBSTR(m.txt,12)
						m.hladajidedi=i_max("id_edi") 
                                                insert_edihlav()
						SELECT edihlav 
						m.ico=STRTRAN(LEFT(m.txt,AT("+",m.txt,1)-1),":14","")
						m.dat_vyst=LEFT(SUBSTR(m.txt,AT("+",m.txt,2)+1, AT("+",m.txt,3)-AT(";",m.txt,2)-1),6)
						m.dat_vyst=CTOD(RIGHT(m.dat_vyst,2)+"."+SUBSTR(m.dat_vyst,3,2)+".20"+LEFT(m.dat_vyst,2))
						m.cislofaktury=SUBSTR(m.txt,AT("+",m.txt,3)+1, AT("+",m.txt,4)-AT("+",m.txt,3)-1)
						REPLACE ico WITH "E"+m.ico, dat_vyst WITH m.dat_vyst, c_faktury WITH m.cislofaktury FOR id_edi=m.hladajidedi
					CASE LOWER(LEFT(m.txt,4))="bgm+"
						m.cislofaktury=SUBSTR(m.txt,AT("+",m.txt,2)+1, AT("+",m.txt,3)-AT("+",m.txt,2)-1)
						SELECT edihlav
						REPLACE c_faktury WITH m.cislofaktury FOR id_edi=m.hladajidedi
					CASE LOWER(LEFT(m.txt,4))="dtm+"
						m.typ_datumu=SUBSTR(m.txt,AT("+",m.txt,1)+1, AT(":",m.txt,1)-AT("+",m.txt,1)-1)
						DO CASE 
							CASE m.typ_datumu=="17" &&datum dodania
								m.dat_vyst=SUBSTR(m.txt,AT(":",m.txt,1)+1, AT(":",m.txt,2)-AT(":",m.txt,1)-1)
								m.dat_vyst=CTOD(SUBSTR(m.dat_vyst,7,2)+"."+SUBSTR(m.dat_vyst,5,2)+"."+LEFT(m.dat_vyst,4))
								SELECT edihlav
								REPLACE dat_vyst WITH m.dat_vyst FOR id_edi=m.hladajidedi
							CASE m.typ_datumu="137" &&datum vystavenia
						ENDCASE 
					CASE LOWER(LEFT(m.txt,4))="nad+"
						m.typ_dat=SUBSTR(m.txt,AT("+",m.txt,1)+1, AT("+",m.txt,2)-AT("+",m.txt,1)-1)
						m.ico=SUBSTR(m.txt,AT("+",m.txt,2)+1, AT(":",m.txt,1)-AT("+",m.txt,2)-1)
						DO CASE 
							CASE m.typ_dat="SU" &&dodavatel
								SELECT edihlav
								REPLACE ico WITH "E"+m.ico FOR id_edi=m.hladajidedi
							CASE m.typ_dat="BY" &&adresat
								SELECT edihlav
								REPLACE edi_kod WITH m.ico FOR id_edi=m.hladajidedi
							CASE m.typ_dat="DP" &&adresat cielove miesto
								SELECT edihlav
								REPLACE edi_kod WITH m.ico FOR id_edi=m.hladajidedi
						ENDCASE 
					CASE LOWER(LEFT(m.txt,7))="rff+dq:"
						m.cislofaktury=SUBSTR(m.txt,8)
						SELECT edihlav
						REPLACE c_faktury WITH m.cislofaktury FOR id_edi=m.hladajidedi
					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")))
						m.ean=SUBSTR(m.txt,AT("+",m.txt,3)+1, AT(":",m.txt,1)-AT("+",m.txt,3)-1)
						INSERT INTO edipol (id_edi, c_data, riadok, ean, dan) VALUES ;
							(m.hladajidedi, m.cdata,m.riadok, m.ean, 0)
					CASE LOWER(LEFT(m.txt,4))="pia+"
						m.ean=SUBSTR(m.txt,AT("+",m.txt,2)+1, AT(":",m.txt,1)-AT("+",m.txt,2)-1)
						SELECT edipol
						GO BOTTOM 
						REPLACE kod_zak WITH m.ean
					CASE LOWER(LEFT(m.txt,5))="imd+a" OR LOWER(LEFT(m.txt,5))="imd+f"
						m.ean=SUBSTR(m.txt,AT(":",m.txt,3)+1)
						SELECT edipol
						GO BOTTOM 
						REPLACE nazov WITH m.ean
					CASE LOWER(LEFT(m.txt,4))="qty+"
						m.typ_dat=SUBSTR(m.txt,AT("+",m.txt,1)+1, AT(":",m.txt,1)-AT("+",m.txt,1)-1)
						IF ":PCE"$m.txt
							m.suma=VAL(SUBSTR(m.txt,AT(":",m.txt,1)+1, AT(":PCE",m.txt,1)-AT(":",m.txt,1)-1))
						ELSE 
							m.suma=VAL(SUBSTR(m.txt,AT(":",m.txt,1)+1))
						ENDIF 
						DO CASE 
							CASE m.typ_dat="12" &&pocet baleni
								SELECT edipol
								GO BOTTOM 
								IF mnozstvo=0
									REPLACE mnozstvo WITH m.suma
								ELSE
									REPLACE mnozstvo WITH m.suma*mnozstvo
								ENDIF 
							CASE m.typ_dat="59" &&pocet kusov v baleni
								SELECT edipol
								GO BOTTOM 
								IF mnozstvo=0
									REPLACE mnozstvo WITH m.suma
								ELSE
									REPLACE mnozstvo WITH m.suma*mnozstvo
								ENDIF 
						ENDCASE 
					CASE LOWER(LEFT(m.txt,4))="moa+"
						m.typ_dat=LOWER(SUBSTR(m.txt,AT("+",m.txt,1)+1, AT(":",m.txt,1)-AT("+",m.txt,1)-1))
						m.suma=VAL(SUBSTR(m.txt,AT(":",m.txt,1)+1, AT(":",m.txt,2)-AT(":",m.txt,1)-1))
						DO CASE 
							CASE m.typ_dat="x33" &&suma celkom bez dph
								SELECT edipol
								GO BOTTOM 
								REPLACE cena WITH m.suma
						ENDCASE 
				ENDCASE 
			ENDDO 		
			=FCLOSE(m.subor)
			SELECT edihlav
			SCAN ALL 
				m.hladajidedi=edihlav.id_edi
				SELECT edipol
				REPLACE ALL cena_celk WITH cena*mnozstvo FOR id_edi=m.hladajidedi
				SUM cena_celk TO m.sumadokladu FOR id_edi=m.hladajidedi
				SELECT edihlav
				m.hladajedikod=ALLTRIM(SUBSTR(edihlav.ico,2))
				IF m.issql
					USE (m.sqlprefix+"rpartneredikod") ALIAS partico IN 0
				ELSE
					IF !USED("partneri")
						USE (m.fn_partneri) ALIAS partneri IN 0
					ENDIF 
					USE vpartneredikod ALIAS partico IN 0
				ENDIF 
				SELECT partico
				IF RECCOUNT("partico")>0
					GO TOP 
					IF !EMPTY(partico.ico)
						REPLACE edihlav.ico WITH partico.ico, edihlav.partner WITH partico.partner
					ELSE
						IF !EMPTY(partico.dic)
							REPLACE edihlav.dic WITH partico.dic, edihlav.partner WITH partico.partner
						ENDIF 
					ENDIF 
				ENDIF 
				USE IN partico
				SELECT edihlav
				REPLACE suma WITH m.sumadokladu 
            druhy_doklad_z_minusu()
			ENDSCAN 
			=tux(.t.,.t.,"edihlav")
			=tux(.t.,.t.,"edipol")
			DO pozavirej WITH "edihlav, edipol"
		ELSE 
			m.chybatxt=m.chybatxt+CHR(13)+CHR(10)+"Subor nema na zaciatku UNA"
		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.eedipor=0
			TRY 
				ERASE (m.eedimeno)
			CATCH
			ENDTRY 
		ELSE 
			=STRTOFILE(FILETOSTR(m.eedimeno),STRTRAN(LOWER(m.oorimeno),".txt",".ok"))
			TRY 
				ERASE (m.eedimeno)
			CATCH
			ENDTRY 
			m.aa=FCREATE(STRTRAN(LOWER(m.orimeno),".txt",".okk"))
			=FPUTS(m.aa,"Import pod cislom "+ALLTRIM(STR(m.hladajidedi,10,0))+CHR(13)+CHR(10) + m.orimeno)
			=FCLOSE(m.aa)
		ENDIF 
	ELSE
		IF m.edipor=0
			TRY 
				ERASE (m.eedimeno)
			CATCH
			ENDTRY 
		ELSE
			=STRTOFILE(FILETOSTR(m.eedimeno),STRTRAN(LOWER(m.oorimeno),".txt",".er"))
			TRY 
				ERASE (m.eedimeno)
			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 
