PARAMETERS menosuboru, poradie, povmenosuboru, m.kodedistred
IF VARTYPE(m.menosuboru)=="C" AND FILE(m.menosuboru)
	LOCAL m.suborxml
	m.suborxml=FILETOSTR(m.menosuboru)
	DO CASE 
		CASE "ITALMARKET"$m.suborxml
			RELEASE m.suborxml
			DO spracujitalmarket WITH m.menosuboru, m.poradie, m.povmenosuboru, m.kodedistred
 	ENDCASE 
ENDIF 




PROCEDURE spracujitalmarket
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
LOCAL m.oldpoint
m.oldpoint=SET("point")
SET POINT TO "."
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 LEFT(m.txt,3)="HDR"
			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, c_faktury, dat_vyst, edi_kod) VALUES (m.hladajidedi, "vypadok", m.cdata, SUBSTR(m.txt,4,10),CTOD(SUBSTR(m.txt,36,10)), m.kodedistred)
			ELSE
				INSERT INTO edihlav (id_edi, subor, c_data, c_faktury, dat_vyst, edi_kod) VALUES (m.hladajidedi, SUBSTR(m.orimeno, RAT("\",m.orimeno)+1), m.cdata, SUBSTR(m.txt,4,10),CTOD(SUBSTR(m.txt,36,10)), m.kodedistred)
			ENDIF 
			SELECT edihlav
			GO TOP 
			DO WHILE LEFT(m.txt,3)<>"ADR"
				m.txt=FGETS(m.subor)
				IF FEOF(m.subor)
					EXIT
				ENDIF 
			ENDDO 
			IF LEFT(m.txt,3)="ADR" AND "ITALMARKET"$m.txt
				REPLACE partner WITH SUBSTR(m.txt,27,30),ico WITH SUBSTR(m.txt,4,8), dic WITH SUBSTR(m.txt,12,10), ulica WITH SUBSTR(m.txt,77,50), mesto WITH SUBSTR(m.txt,133,50), psc WITH SUBSTR(m.txt,127,6)
				m.txt=FGETS(m.subor)
				REPLACE icdph WITH SUBSTR(m.txt,29,12)
			ENDIF 
			DO WHILE LEFT(m.txt,3)<>"ADR"
				m.txt=FGETS(m.subor)
				IF FEOF(m.subor)
					EXIT
				ENDIF 
			ENDDO 
			IF LEFT(m.txt,3)="ADR" AND SUBSTR(m.txt,4,8)==m.hotel_ico
				DO WHILE LEFT(m.txt,3)<>"SUM"
					m.txt=FGETS(m.subor)
					IF FEOF(m.subor)
						EXIT
					ENDIF 
				ENDDO 
				IF LEFT(m.txt,3)="SUM"
					IF m.hotel_platca_dph
						REPLACE suma WITH VAL(ALLTRIM(STRTRAN(SUBSTR(m.txt,4,15),",",SET("POINT"))))
					ELSE 
						REPLACE suma WITH VAL(ALLTRIM(STRTRAN(SUBSTR(m.txtriadok,34,15),",",SET("POINT"))))
					ENDIF 
				ENDIF 
				DO WHILE LEFT(m.txt,3)<>"LIN"
					m.txt=FGETS(m.subor)
					IF FEOF(m.subor)
						EXIT
					ENDIF 
				ENDDO 
				DO WHILE !FEOF(m.subor)
					IF LEFT(m.txt,3)="LIN"
						m.riadok=VAL(SUBSTR(m.txt,4,3))
						m.ean=SUBSTR(m.txt,7,20)
						m.mnozstvo=VAL(STRTRAN(SUBSTR(m.txt,61,15),",",SET("POINT")))
						m.dan=VAL(STRTRAN(SUBSTR(m.txt,143,5),",",SET("POINT")))
						IF m.hotel_platca_dph
							m.cena=VAL(STRTRAN(SUBSTR(m.txt,128,15),",",SET("POINT")))/m.mnozstvo
						ELSE 
							m.cena=VAL(STRTRAN(SUBSTR(m.txt,163,15),",",SET("POINT")))/m.mnozstvo
						ENDIF 
						m.kod_zak=SUBSTR(m.txt,177,20)
						m.nazov=SUBSTR(m.txt,235)
						m.txt=FGETS(m.subor)
						m.nazov=m.nazov+LEFT(m.txt,30)
						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.txt=FGETS(m.subor)
					ENDIF 
				ENDDO 
				=FCLOSE(m.subor)
                                druhy_doklad_z_minusu()
				=tux(.t.,.t.,"edihlav")
				=tux(.t.,.t.,"edipol")
				DO pozavirej WITH "edihlav, edipol"
			ELSE 
				m.chybatxt=m.chybatxt+CHR(13)+CHR(10)+"Doklad nie je urceny pre tento hotel (nesuhlasi ICO)"
				=FCLOSE(m.subor)	
			ENDIF 
		ELSE
			=FCLOSE(m.subor)
			m.chybatxt=m.chybatxt+CHR(13)+CHR(10)+"V subore nie je prvy riadok HDR."
		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),".dat",".ok"))
			TRY 
				ERASE (m.edimeno)
			CATCH
			ENDTRY 
			m.aa=FCREATE(STRTRAN(LOWER(m.orimeno),".dat",".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.edimeno)
			CATCH
			ENDTRY 
		ELSE
			=STRTOFILE(FILETOSTR(m.edimeno),STRTRAN(LOWER(m.orimeno),".dat",".er"))
			TRY 
				ERASE (m.edimeno)
			CATCH
			ENDTRY 
			m.aa=FCREATE(STRTRAN(LOWER(m.orimeno),".dat",".err"))
			=FPUTS(m.aa,m.chybatxt+CHR(13)+CHR(10))
			=FCLOSE(m.aa)	
		ENDIF 
	ENDIF 
ENDIF 
SET POINT TO (m.oldpoint)
