*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
PARAMETERS xmlko, m.edipor, m.orimeno, m.edimeno, m.kodedistred
PRIVATE m.hladajidedi
LOCAL pocetfaktur, m.faktura1, m.odberatel, m.dodavatel, m.polozky, m.polozka, m.pocetpoloziek, m.sumadokladu, m.hlavicka, m.pocetparty
LOCAL pretentohotel, isorder
m.isorder=.f.
m.pretentohotel=.f.
#DEFINE CRLF CHR(13)+CHR(10)
pocetfaktur=hladajsekciu("orion_message",m.xmlko)
IF m.pocetfaktur>0
    FOR m.i=1 TO m.pocetfaktur
        hladajidedi=i_max("id_edi") 
        otevri_edi_hlavpol()
        insert_edihlav()
        faktura1=vratsekciu("orion_message", m.xmlko,m.i,"X")
        DO CASE 
        	CASE LOWER(vratsekciu("message_type",vratsekciu("header",m.faktura1,1,"X"),1,"C",m.kodovastranka))="orders"
				hlavicka=vratsekciu("doc_header",vratsekciu("body",m.faktura1,1,"X"),1,"X")
				pocetparty=hladajsekciu("party",m.hlavicka)
				m.isorder=.t.
				FOR m.ii=1 TO m.pocetparty
					IF LOWER(vratsekciu("party_qualifier",vratsekciu("party",m.hlavicka,m.ii,"X"),1,"C",m.kodovastranka))="su"
						m.dodavatel=vratsekciu("party",m.hlavicka,m.ii,"X")
						IF STRTRAN(vratsekciu("party_ean",m.dodavatel,1,"C")," ","")=m.hotel_ico OR STRTRAN(vratsekciu("party_ean",m.dodavatel,1,"C")," ","")=m.hotel_edikod OR (!EMPTY(m.hotel_edikod_list) AND STRTRAN(vratsekciu("party_ean",m.dodavatel,1,"C")," ","")$m.hotel_edikod_list)
							m.pretentohotel=.t.
						ENDIF 
						SELECT edihlav
	                    REPLACE c_faktury WITH vratsekciu("doc_number",m.hlavicka,1,"C"), is_order WITH .t.
	                    REPLACE dat_vyst WITH vratsekciu("delivery_date", m.hlavicka,1,"D")
*!*		                    REPLACE ico WITH STRTRAN(vratsekciu("company_id",m.dodavatel,1,"C")," ","")
*!*		                    REPLACE icdph WITH vratsekciu("tax_id",m.dodavatel,1,"C")
*!*		                    REPLACE dic WITH SUBSTR(vratsekciu("tax_id",m.dodavatel,1,"C"),3)
*!*		                    REPLACE partner WITH vratsekciu("party_name",m.dodavatel,1,"C",m.kodovastranka)
*!*		                    REPLACE ulica WITH vratsekciu("street",m.dodavatel,1,"C",m.kodovastranka)
*!*		                    REPLACE mesto WITH vratsekciu("place",m.dodavatel,1,"C",m.kodovastranka)
*!*		                    REPLACE psc WITH vratsekciu("post_code",m.dodavatel,1,"C",m.kodovastranka)
	                    RELEASE m.dodavatel
					ENDIF 
					IF LOWER(vratsekciu("party_qualifier",vratsekciu("party",m.hlavicka,m.ii,"X"),1,"C",m.kodovastranka))="by"
						m.odberatel=vratsekciu("party",m.hlavicka,m.ii,"X")
						SELECT edihlav
	                    REPLACE ico WITH "E"+STRTRAN(vratsekciu("party_ean",m.odberatel,1,"C")," ","")
	                    REPLACE partner WITH vratsekciu("party_name",m.odberatel,1,"C",m.kodovastranka)
					ENDIF 
					IF LOWER(vratsekciu("party_qualifier",vratsekciu("party",m.hlavicka,m.ii,"X"),1,"C",m.kodovastranka))="dp"
						m.odberatel=vratsekciu("party",m.hlavicka,m.ii,"X")
						SELECT edihlav
	                    REPLACE edi_kod WITH vratsekciu("party_ean",m.odberatel,1,"C")
	                    REPLACE ico WITH "E"+STRTRAN(vratsekciu("party_ean",m.odberatel,1,"C")," ","")
	                    REPLACE partner WITH vratsekciu("party_name",m.odberatel,1,"C",m.kodovastranka)
					ENDIF 
				ENDFOR 
				IF m.pretentohotel
					IF hladajsekciu("line_items", m.faktura1)>0
						polozky=vratsekciu("line_items", m.faktura1,1,"X")
						pocetpoloziek=hladajsekciu("item", m.polozky)
						sumadokladu=0
						FOR m.ii=1 TO m.pocetpoloziek
							polozka=vratsekciu("item", m.polozky,m.ii,"X")
							INSERT INTO edipol (id_edi, c_data, riadok, ean, kod_zak, mnozstvo, cena, dan, nazov) VALUES ;
								(m.hladajidedi, m.cdata,;
								m.ii,;
								vratsekciu("article_gtin",m.polozka,1,"C"),;
								vratsekciu("article_id_supplier",m.polozka,1,"C"),;
								vratsekciu("quantity",m.polozka,1,"N"),;
								vratsekciu("net_price",m.polozka,1,"N"),;
								vratsekciu("vat_rate",m.polozka,1,"N"),;
								vratsekciu("article_name",m.polozka,1,"C",m.kodovastranka)+" "+vratsekciu("unit",m.polozka,1,"C",m.kodovastranka))
							SELECT edipol
							IF !m.hotel_platca_dph
								REPLACE cena WITH cena*(dan/100+1)
							ENDIF 
							REPLACE cena_celk WITH cena*mnozstvo
							sumadokladu=m.sumadokladu+cena_celk
							RELEASE m.polozka 
						ENDFOR
						druhy_doklad_z_minusu()
						uloz_a_zavri_hlav_a_pol()
					ELSE
						chybatxt=m.chybatxt+CRLF+"Chybaju polozky v doklade "+ALLTRIM(edihlav.c_faktury)
					ENDIF 
				ELSE
					chybatxt=m.chybatxt+CRLF+"Faktura nie je pre toto ICO. Cislo faktury: "+ALLTRIM(edihlav.c_faktury)
				ENDIF
				DO pozavirej WITH "edihlav, edipol"
        	CASE LOWER(vratsekciu("message_type",vratsekciu("header",m.faktura1,1,"X"),1,"C",m.kodovastranka))="desadv"
				hlavicka=vratsekciu("message_header",vratsekciu("body",m.faktura1,1,"X"),1,"X")
				pocetparty=hladajsekciu("party",m.hlavicka)
				FOR m.ii=1 TO m.pocetparty
					IF LOWER(vratsekciu("party_qualifier",vratsekciu("party",m.hlavicka,m.ii,"X"),1,"C",m.kodovastranka))="su"
						m.dodavatel=vratsekciu("party",m.hlavicka,m.ii,"X")
						SELECT edihlav
	                    REPLACE c_faktury WITH vratsekciu("doc_number",m.hlavicka,1,"C")
	                    REPLACE dat_vyst WITH vratsekciu("doc_date_of_issue", m.hlavicka,1,"D")
	                    REPLACE ico WITH STRTRAN(vratsekciu("company_id",m.dodavatel,1,"C")," ","")
	                    REPLACE icdph WITH vratsekciu("tax_id",m.dodavatel,1,"C")
	                    REPLACE dic WITH SUBSTR(vratsekciu("tax_id",m.dodavatel,1,"C"),3)
	                    REPLACE partner WITH vratsekciu("party_name",m.dodavatel,1,"C",m.kodovastranka)
	                    REPLACE ulica WITH vratsekciu("street",m.dodavatel,1,"C",m.kodovastranka)
	                    REPLACE mesto WITH vratsekciu("place",m.dodavatel,1,"C",m.kodovastranka)
	                    REPLACE psc WITH vratsekciu("post_code",m.dodavatel,1,"C",m.kodovastranka)
	                    RELEASE m.dodavatel
					ENDIF 
					IF LOWER(vratsekciu("party_qualifier",vratsekciu("party",m.hlavicka,m.ii,"X"),1,"C",m.kodovastranka))="by"
						m.odberatel=vratsekciu("party",m.hlavicka,m.ii,"X")
						IF STRTRAN(vratsekciu("company_id",m.odberatel,1,"C")," ","")<>m.hotel_ico
							m.pretentohotel=.t.
						ENDIF 
					ENDIF 
					IF LOWER(vratsekciu("party_qualifier",vratsekciu("party",m.hlavicka,m.ii,"X"),1,"C",m.kodovastranka))="dp"
						m.odberatel=vratsekciu("party",m.hlavicka,m.ii,"X")
						SELECT edihlav
	                    REPLACE edi_kod WITH vratsekciu("party_ean",m.odberatel,1,"C")
					ENDIF 
				ENDFOR 
				IF m.pretentohotel
					IF hladajsekciu("line_items", m.faktura1)>0
						polozky=vratsekciu("articles",vratsekciu("item",vratsekciu("line_items", m.faktura1,1,"X"),1,"X"),1,"X")
						pocetpoloziek=hladajsekciu("article", m.polozky)
						sumadokladu=0
						FOR m.ii=1 TO m.pocetpoloziek
							polozka=vratsekciu("article", m.polozky,m.ii,"X")
							INSERT INTO edipol (id_edi, c_data, riadok, ean, kod_zak, mnozstvo, cena, dan, nazov) VALUES ;
								(m.hladajidedi, m.cdata,;
								m.ii,;
								vratsekciu("article_gtin",m.polozka,1,"C"),;
								vratsekciu("article_id_supplier",m.polozka,1,"C"),;
								vratsekciu("quantity",m.polozka,1,"N"),;
								vratsekciu("price",m.polozka,1,"N"),;
								vratsekciu("vat_rate",m.polozka,1,"N"),;
								vratsekciu("article_name",m.polozka,1,"C",m.kodovastranka)+" "+vratsekciu("unit",m.polozka,1,"C",m.kodovastranka))
							SELECT edipol
							IF !m.hotel_platca_dph
								REPLACE cena WITH cena*(dan/100+1)
							ENDIF 
							REPLACE cena_celk WITH cena*mnozstvo
							sumadokladu=m.sumadokladu+cena_celk
							RELEASE m.polozka 
						ENDFOR
						druhy_doklad_z_minusu()
						uloz_a_zavri_hlav_a_pol()
					ELSE
						chybatxt=m.chybatxt+CRLF+"Chybaju polozky v doklade "+ALLTRIM(edihlav.c_faktury)
					ENDIF 
				ELSE
					chybatxt=m.chybatxt+CRLF+"Faktura nie je pre toto ICO. Cislo faktury: "+ALLTRIM(edihlav.c_faktury)
				ENDIF
				DO pozavirej WITH "edihlav, edipol"
        	CASE LOWER(vratsekciu("message_type",vratsekciu("header",m.faktura1,1,"X"),1,"C",m.kodovastranka))="invoice"
				hlavicka=vratsekciu("message_header",vratsekciu("body",m.faktura1,1,"X"),1,"X")
				pocetparty=hladajsekciu("party",m.hlavicka)
				FOR m.ii=1 TO m.pocetparty
					IF LOWER(vratsekciu("party_qualifier",vratsekciu("party",m.hlavicka,m.ii,"X"),1,"C",m.kodovastranka))="su"
						m.dodavatel=vratsekciu("party",m.hlavicka,m.ii,"X")
						SELECT edihlav
	                    REPLACE c_faktury WITH vratsekciu("doc_number",m.hlavicka,1,"C")
	                    REPLACE dat_vyst WITH vratsekciu("issue_date", m.hlavicka,1,"D")
	                    REPLACE ico WITH STRTRAN(vratsekciu("company_id",m.dodavatel,1,"C")," ","")
	                    REPLACE icdph WITH vratsekciu("tax_id",m.dodavatel,1,"C")
	                    REPLACE dic WITH SUBSTR(vratsekciu("tax_id",m.dodavatel,1,"C"),3)
	                    REPLACE partner WITH vratsekciu("party_name",m.dodavatel,1,"C",m.kodovastranka)
	                    REPLACE ulica WITH vratsekciu("street",m.dodavatel,1,"C",m.kodovastranka)
	                    REPLACE mesto WITH vratsekciu("place",m.dodavatel,1,"C",m.kodovastranka)
	                    REPLACE psc WITH vratsekciu("post_code",m.dodavatel,1,"C",m.kodovastranka)
	                    RELEASE m.dodavatel
					ENDIF 
					IF LOWER(vratsekciu("party_qualifier",vratsekciu("party",m.hlavicka,m.ii,"X"),1,"C",m.kodovastranka))="by"
						m.odberatel=vratsekciu("party",m.hlavicka,m.ii,"X")
						IF STRTRAN(vratsekciu("company_id",m.odberatel,1,"C")," ","")<>m.hotel_ico
							m.pretentohotel=.t.
						ENDIF 
					ENDIF 
					IF LOWER(vratsekciu("party_qualifier",vratsekciu("party",m.hlavicka,m.ii,"X"),1,"C",m.kodovastranka))="dp"
						m.odberatel=vratsekciu("party",m.hlavicka,m.ii,"X")
						SELECT edihlav
	                    REPLACE edi_kod WITH vratsekciu("party_ean",m.odberatel,1,"C")
					ENDIF 
				ENDFOR 
				IF m.pretentohotel
					IF hladajsekciu("line_items", m.faktura1)>0
						polozky=vratsekciu("line_items", m.faktura1,1,"X")
						pocetpoloziek=hladajsekciu("item", m.polozky)
						sumadokladu=0
						FOR m.ii=1 TO m.pocetpoloziek
							polozka=vratsekciu("item", m.polozky,m.ii,"X")
							INSERT INTO edipol (id_edi, c_data, riadok, ean, kod_zak, mnozstvo, cena, dan, nazov) VALUES ;
								(m.hladajidedi, m.cdata,;
								m.ii,;
								vratsekciu("article_gtin",m.polozka,1,"C"),;
								vratsekciu("article_id_supplier",m.polozka,1,"C"),;
								vratsekciu("quantity",m.polozka,1,"N"),;
								vratsekciu("net_price",m.polozka,1,"N"),;
								vratsekciu("vat_rate",m.polozka,1,"N"),;
								vratsekciu("article_name",m.polozka,1,"C",m.kodovastranka)+" "+vratsekciu("unit",m.polozka,1,"C",m.kodovastranka))
							SELECT edipol
							IF !m.hotel_platca_dph
								REPLACE cena WITH cena*(dan/100+1)
							ENDIF 
							REPLACE cena_celk WITH cena*mnozstvo
							sumadokladu=m.sumadokladu+cena_celk
							RELEASE m.polozka 
						ENDFOR
						druhy_doklad_z_minusu()
						uloz_a_zavri_hlav_a_pol()
					ELSE
						chybatxt=m.chybatxt+CRLF+"Chybaju polozky v doklade "+ALLTRIM(edihlav.c_faktury)
					ENDIF 
				ELSE
					chybatxt=m.chybatxt+CRLF+"Faktura nie je pre toto ICO. Cislo faktury: "+ALLTRIM(edihlav.c_faktury)
				ENDIF
				DO pozavirej WITH "edihlav, edipol"
		ENDCASE  
	ENDFOR  
ELSE 
	m.chybatxt=m.chybatxt+CRLF+"Subor neobsahuje faktury"
ENDIF 
era_and_ok_or_err()

