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=""
m.hladajidedi=0
*H;123137359;500171;13/06-23;27/06-23;13/06-23;442.33;0.00;0.00;590.60
*R;;13/06-23;;500171;310013;GG VEGAN VUNA A LA TUNIAK1*2KG;7290115204971;2.00;kg;33.76;20;16.88;16.88
*R;;13/06-23;;500171;321001;BRYNDZA MRAZENÁ MELIKO 3*1KG;;6.00;kg;54.58;20;9.10;9.10
*R;;13/06-23;;500171;340022;PAR.BUCHTY ÈOKOLÁD.80G 1*3,2KG;;6.40;kg;26.99;20;4.22;4.22

IF m.edisubor>0
	=FSEEK(m.edisubor,0)
	m.riadok=1
	m.txt=nacitajriadok(m.edisubor)
	DO WHILE !FEOF(m.edisubor) OR !EMPTY(m.txt)
		DO CASE 
			CASE LEFT(m.txt,2)='H;'
				m.partner="Bidfood Slovakia s. r. o. "
				m.dico="34152199"
				m.ddico="2020168359"
				m.dicdph="SK2020168359"
				m.cislofaktury=sss(m.txt,1)
				m.datum_dodania=CTOD(sss(m.txt,3))
				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
		
			CASE LEFT(m.txt,3)="R;;"
	**R;;13.06.2023;;500171;680660;YAMASA SÓJOVÁ OMÁÈKA 1*1L;4,903E+12;2.00;ks;9.00;20;IV.50;IV.50

				m.nazov=STRCONV(sss(m.txt,6)+" "+sss(m.txt,9),11)
				m.dan=v_sss(m.txt,11)/100+1
				m.kod_zak=sss(m.txt,5)
				m.mnozstvo=v_sss(m.txt,8)
				IF m.mnozstvo<>0
					m.cena=v_sss(m.txt,12)
					m.ean=sss(m.txt,7)
					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 
		ENDCASE 
		IF !FEOF(m.edisubor)
			m.txt=nacitajriadok(m.edisubor)
		ELSE
			m.txt=""
		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
	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

		
		
** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** 
FUNCTION v_sss (texticek, pozice, oddelovac)
LOCAL textik
m.textik=sss (texticek, pozice, oddelovac)
IF SET("point")="."
	RETURN VAL(STRTRAN( m.textik, "," ,"."))
ELSE
	RETURN VAL(STRTRAN( m.textik, "." ,","))
ENDIF 

** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** 
FUNCTION sss (texticek, pozice, oddelovac)
PRIVATE ALL 
IF EMPTY(m.oddelovac)
    oddelovac = ";"
ENDIF 
pom = SUBSTR(m.texticek, AT(m.oddelovac, m.texticek, m.pozice) + 1) + m.oddelovac
IF AT(m.oddelovac, m.pom,1)>0
	m.pom=LEFT(m.pom, AT(m.oddelovac, m.pom,1)-1)
ELSE 
	m.pom=STRTRAN(m.pom,CHR(13),"")
	m.pom=STRTRAN(m.pom,CHR(10),"")
ENDIF 
RETURN m.pom
		
