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=""
m.hladajidedi=0
IF m.edisubor>0
	m.txt=nacitajriadok(m.edisubor)
	IF LOWER(m.txt)="doklad;datum;artikel;nazov;match;velkost;ean;dph;mnozstvo;doporucena moc s dph;doporucena moc bez dph;nc s dph / mj;nc bez dph / mj;nc s dph / spolu;nc bez dph / spolu"
		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 "Golf Centrum s.r.o.",ico WITH "36258873", dic WITH " 2021792641", icdph WITH "SK 2021792641", ulica WITH "Rovná 11", mesto WITH "Sereď", psc WITH "92601"
		m.riadok=1
*** subor z roku 2020
*doklad;datum;artikel;nazov;match;WP skupina artikla;WP znacka;WP pohlavie;WP nadkategoria;WP kategoria;WP podkategoria;WP vekova kategoria;WP farba;WP material;velkost;ean;dph;mnozstvo;doporucena MOC s DPH;doporucena MOC bez DPH;NC s DPH / mj;NC bez DPH / mj;NC s DPH / spolu;NC bez DPH / spolu
*4111200143;23.6.2020;12000592;Rukavica pánska FJ Contour FLX - Ľavá;68850;Rukavice&Loptičky;Footjoy;Pánske;Golfové rukavice;Golfové rukavice klasické;pravácke - ľavá rukavica;Pánske;biela;;L;739248759905;20;3;23,9;19,92;17,93;14,94;53,78;44,81
*** subor z roku 2021
*doklad;datum;artikel;nazov;match;velkost;ean;dph;mnozstvo;doporucena MOC s DPH;doporucena MOC bez DPH;NC s DPH / mj;NC bez DPH / mj;NC s DPH / spolu;NC bez DPH / spolu
*4111210103;28.4.2021;6003601;Loptičky CG Warbird 21, 3-bal.;642145812;3bal.;194518236812;20;20;6,25;5,21;4,69;3,91;93,75;78,13
		DO WHILE !FEOF(m.edisubor)
			m.txt=nacitajriadok(m.edisubor)
			m.cislofaktury=LEFT(m.txt,AT(";", m.txt, 1)-1)
			m.datum_dodania=CTOD(SUBSTR(m.txt,AT(";",m.txt,1)+1, AT(";",m.txt,2)-AT(";",m.txt,1)-1))
			m.nazov=SUBSTR(m.txt,AT(";",m.txt,3)+1, AT(";",m.txt,4)-AT(";",m.txt,3)-1)
			m.dan=VAL(STRTRAN(SUBSTR(m.txt,AT(";",m.txt,7)+1, AT(";",m.txt,8)-AT(";",m.txt,7)-1),",",SET("POINT")))/100+1
			m.kod_zak=SUBSTR(m.txt,AT(";",m.txt,2)+1, AT(";",m.txt,3)-AT(";",m.txt,2)-1)
			IF !m.hotel_platca_dph
				m.cena=VAL(STRTRAN(SUBSTR(m.txt,AT(";",m.txt,11)+1, AT(";",m.txt,12)-AT(";",m.txt,11)-1),",",SET("POINT")))
			ELSE 
				m.cena=VAL(STRTRAN(SUBSTR(m.txt,AT(";",m.txt,12)+1, AT(";",m.txt,13)-AT(";",m.txt,12)-1),",",SET("POINT")))
			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.ean=SUBSTR(m.txt,AT(";",m.txt,6)+1, AT(";",m.txt,7)-AT(";",m.txt,6)-1)
			IF m.riadok=1
				SELECT edihlav
				REPLACE c_faktury WITH m.cislofaktury, dat_vyst WITH m.datum_dodania
			ENDIF
			IF mnozstvo<>0 	
				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 
		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)
		m.chybatxt=m.chybatxt+CHR(13)+CHR(10)+"V subore chyba prvy riadok s menami poli."
	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)
IF LEN(m.ret)>253
	DO WHILE !FEOF(m.subtxt)
		IF OCCURS(";",m.ret)<23
			m.ret=m.ret+FGETS(m.subtxt)
		ELSE
			EXIT
		ENDIF 
	ENDDO 
ENDIF 
RETURN m.ret

		
