FUNCTION prihlasenie
PRIVATE m.httpodpoved, m.vysledok, m.xmltext, m.oldproc
*DO FORM snake NAME lsnakex LINKED WITH 'Prihlasujem sa'
*lsnakex.DRAW()
IF ".FXP"$UPPER(SYS(16,0))
	http.OPEN("POST","https://dev.mnamka.sk/export/?key="+eshop_kod, .F.)
	http.Option(4) = 13056 
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/xml;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV("mnamka:hamham",13))
ELSE
	http.OPEN("POST",m.eshop_url+"export/?key="+eshop_kod, .F.)
	http.Option(4) = 13056 
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/xml;charset=utf-8;")
ENDIF 
TRY 
	http.SEND("")
*	lsnakex.DRAW()
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_eshop_neodpoveda")
ENDTRY 
IF m.vysledok=200
	m.xmltext=http.responsetext
	IF hladajsekciu("mag_shop",m.xmltext)=1
		IF vratsekciu("status",vratsekciu("auth",vratsekciu("mag_shop",m.xmltext,1,"X"),1,"X"),1,"C")="login"
			m.vysledok=vratsekciu("token",vratsekciu("auth",vratsekciu("mag_shop",m.xmltext,1,"X"),1,"X"),1,"C")
		ELSE
			m.vysledok=""
		ENDIF 
	ELSE
		m.vysledok=""
	ENDIF 
ELSE
	m.vysledok=""
ENDIF 
*lsnakex.release()
RETURN m.vysledok


FUNCTION citajobjednavky
LOCAL magneticatoken, kodovastranka, m.chybatxt
LOCAL m.httpodpoved, m.vysledok, m.xmltext, m.oldproc
LOCAL m.hladajzmazane, m.mag_shop, m.objednavky, m.objednavka, pocetobjednavok, m.hladajextid, m.hladajpartner
LOCAL m.id_objednavky, m.obj_przv, m.obj_meno, m.obj_adresa, m.obj_mesto, m.obj_psc, m.obj_telefon, m.obj_email, m.obj_ico, m.obj_dic, m.obj_icdph
LOCAL m.obj_firma, m.doruc_przv, m.doruc_meno, m.doruc_adresa, m.doruc_mesto, m.doruc_psc, m.doruc_telefon, m.doruc_email, m.obj_stav, m.id_partnera
LOCAL m.id_objednavky, m.obj_kod, m.obj_nazev, m.obj_cena, m.obj_dph, m.obj_pocet, m.obj_ean, m.obj_datum
LOCAL m.hladajidlimit, m.hladajidden, m.hladajidrov, m.hladajidtyp, m.hladajidkas, m.hladajidtyp, m.hladajidstat
DO open_database
m.hladajzmazane=.f.
m.oldproc=SET("procedure")
SET PROCEDURE TO spracuj_xml ADDITIVE 
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
m.magneticatoken=prihlasenie()
IF !EMPTY(m.magneticatoken)
*	DO FORM snake NAME lsnakex LINKED WITH 'Čítam objednávky'
*	lsnakex.DRAW()
	IF ".FXP"$UPPER(SYS(16,0))
		http.OPEN("POST","https://dev.mnamka.sk/export/action/orders/?token="+m.magneticatoken, .F.)
		http.Option(4) = 13056 
		http.settimeouts(0, 0, 0, 0)
		http.setrequestheader("content-type", "application/xml;charset=utf-8;")
		http.setrequestheader("Authorization", "Basic " + STRCONV("mnamka:hamham",13))
	ELSE
		http.OPEN("POST",m.eshop_url+"export/action/orders/?token="+m.magneticatoken, .F.)
		Http.Option(4) = 13056 
		http.settimeouts(0, 0, 0, 0)
		http.setrequestheader("content-type", "application/xml;charset=utf-8;")
	ENDIF 
	TRY 
		http.SEND("")
*		lsnakex.DRAW()
		m.httpodpoved=http.statustext
		m.vysledok=http.status
	CATCH
		m.vysledok=-100
		m.httpodpoved=jaz("form_eshop_neodpoveda")
	ENDTRY 
	IF m.vysledok=200
		m.xmltext=http.responsetext
		IF hladajsekciu("mag_shop",m.xmltext)=1
			DO CASE
			CASE ATC('encoding="utf-8"',m.xmltext)>0
*				m.kodovastranka=11
				m.kodovastranka=0
			OTHERWISE
				m.kodovastranka=0
			ENDCASE
			m.chybatxt=""

			IF vratsekciu("status",vratsekciu("auth",vratsekciu("mag_shop",m.xmltext,1,"X"),1,"X"),1,"C")="error"
				m.chybatxt=vratsekciu("message",vratsekciu("auth",vratsekciu("mag_shop",m.xmltext,1,"X"),1,"X"),1,"C", m.kodovastranka)
***** Prisla chyba z eshopu
			ELSE 
				m.mag_shop=vratsekciu("mag_shop", m.xmltext,1,"X")
				IF hladajsekciu("orders",m.mag_shop)=1
					m.objednavky=vratsekciu("orders", m.mag_shop,1,"X")
					m.pocetobjednavok=hladajsekciu("order",m.objednavky)
					IF m.pocetobjednavok>0
						CREATE CURSOR dosleobjednavky (id I, priezvisko C(50), meno c(50), adresa c(50), mesto c(50), psc c(10), telefon c(50), email c(50),;
							ico c(20), dic c(20), icdph c(20),firma c(50), dor_przv c(50), dor_meno c(50), dor_adresa c(50), dor_mesto c(50), dor_psc c(50), dor_tel c(50), dor_mail c(50), stav I, id_limit I, id_den I, id_rov I, c_partner I, potvrdene L, ext_id I, datum D)
						CREATE CURSOR dosleobjpol (id I,c_karty I, nazev C(30), cena Y, dph N(5,2), mnozstvo Y, ean c(50))
						FOR m.i=1 TO m.pocetobjednavok
							m.objednavka=vratsekciu("order", m.objednavky,m.i,"X")
							m.id_objednavky=vratsekciu("uid", m.objednavka,1,"N")
							m.obj_przv=vratsekciu("last_name", m.objednavka,1,"C",m.kodovastranka)
							m.obj_meno=vratsekciu("first_name", m.objednavka,1,"C",m.kodovastranka)
							m.obj_adresa=vratsekciu("address", m.objednavka,1,"C",m.kodovastranka)
							m.obj_mesto=vratsekciu("city", m.objednavka,1,"C",m.kodovastranka)
							m.obj_psc=vratsekciu("zip", m.objednavka,1,"C",m.kodovastranka)
							m.obj_telefon=vratsekciu("telephone", m.objednavka,1,"C",m.kodovastranka)
							m.obj_email=vratsekciu("email", m.objednavka,1,"C",m.kodovastranka)
							m.obj_ico=vratsekciu("company_id", m.objednavka,1,"C",m.kodovastranka)
							m.obj_dic=vratsekciu("company_vat", m.objednavka,1,"C",m.kodovastranka)
							m.obj_icdph=vratsekciu("company_tax_id", m.objednavka,1,"C",m.kodovastranka)
							m.obj_firma=vratsekciu("company", m.objednavka,1,"C",m.kodovastranka)
							m.obj_datum=vratsekciu("delivery_date", m.objednavka,1,"D")
							m.doruc_przv=vratsekciu("delivery_last_name", m.objednavka,1,"C",m.kodovastranka)
							m.doruc_meno=vratsekciu("delivery_first_name", m.objednavka,1,"C",m.kodovastranka)
							m.doruc_adresa=vratsekciu("delivery_address", m.objednavka,1,"C",m.kodovastranka)
							m.doruc_mesto=vratsekciu("delivery_city", m.objednavka,1,"C",m.kodovastranka)
							m.doruc_psc=vratsekciu("delivery_zip", m.objednavka,1,"C",m.kodovastranka)
							m.doruc_telefon=vratsekciu("delivery_telephone", m.objednavka,1,"C",m.kodovastranka)
							m.doruc_email=vratsekciu("delivery_email", m.objednavka,1,"C",m.kodovastranka)
							m.id_partnera=vratsekciu("user", m.objednavka,1,"N")
							m.obj_stav=vratsekciu("uid", vratsekciu("status",m.objednavka,1,"X"),1,"N")
							IF EMPTY(m.doruc_przv) AND EMPTY(m.doruc_mesto) AND EMPTY(m.doruc_psc)
								*** Bolo vybrane odberne miesto - do pola firma pisem odberne miesto
								m.obj_firma=m.doruc_meno
								m.doruc_meno=""
							ENDIF 
							INSERT INTO dosleobjednavky(id, priezvisko, meno, adresa, mesto, psc, telefon, email,;
								ico, dic, icdph,firma, dor_przv, dor_meno, dor_adresa, dor_mesto, dor_psc, dor_tel, dor_mail, stav, ext_id, datum) VALUES (;
								m.id_objednavky, m.obj_przv, m.obj_meno, m.obj_adresa, m.obj_mesto, m.obj_psc, m.obj_telefon, m.obj_email, m.obj_ico, m.obj_dic, m.obj_icdph,;
								m.obj_firma, m.doruc_przv, m.doruc_meno, m.doruc_adresa, m.doruc_mesto, m.doruc_psc, m.doruc_telefon, m.doruc_email, m.obj_stav, m.id_partnera, m.obj_datum)
							IF hladajsekciu("products",m.objednavka)=1
								m.objednavka=vratsekciu("products", m.objednavka,1,"X")
								m.pocetproduktov=hladajsekciu("product",m.objednavka)
								FOR m.j=1 TO m.pocetproduktov
									m.jednaobjednavka=vratsekciu("product", m.objednavka,m.j,"X")
									IF vratsekciu("uid",vratsekciu("type",m.jednaobjednavka,1,"X"),1,"N")=0
										m.obj_nazev=vratsekciu("title",m.jednaobjednavka,2,"C", m.kodovastranka)
										m.obj_pocet=vratsekciu("number",m.jednaobjednavka,1,"N")
										m.obj_dph=vratsekciu("tax",m.jednaobjednavka,1,"N")
										m.obj_cena=vratsekciu("price",m.jednaobjednavka,1,"N")
										m.obj_kod=vratsekciu("code",m.jednaobjednavka,1,"N")
										m.obj_ean=vratsekciu("ean",m.jednaobjednavka,1,"C", m.kodovastranka)
										INSERT INTO dosleobjpol (id,c_karty, nazev, cena, dph, mnozstvo, ean) VALUES (m.id_objednavky, m.obj_kod, m.obj_nazev, m.obj_cena, m.obj_dph,;
											m.obj_pocet, m.obj_ean)
									ENDIF 
								ENDFOR 
							ELSE
								m.chybatxt="Ziadne polozky v objednavke"
							ENDIF 
						ENDFOR  
					ELSE 
						m.chybatxt="Ziadne objednavky v sprave"
					ENDIF 
				ELSE 
					m.chybatxt="Ziadne objednavky v sprave"
				ENDIF 
			ENDIF 
		ELSE
			m.vysledok=""
		ENDIF 
	ENDIF 
ELSE
	m.vysledok=""
ENDIF 
IF USED("dosleobjednavky") AND RECCOUNT("dosleobjednavky")>0
	IF m.issql
		USE (m.sqlprefix+"rpartneri") ALIAS zoznampartnerov IN 0
		USE (m.sqlprefix+"rlimhlzoznameshop") IN 0 ALIAS zoznamlimitov
		USE (m.sqlprefix+"rlimtypyzoznam") ALIAS typy IN 0
		USE (m.sqlprefix+"rlimchodyzoznam") ALIAS chody IN 0
		USE (m.sqlprefix+"rlimstatus")	ALIAS statusy IN 0
		USE (m.sqlprefix+"rstrediska") ALIAS strediska IN 0 
	ELSE
		USE (m.fn_food_dat) ALIAS strediska IN 0
		USE (m.fn_limit_hl) IN 0 ALIAS nlimit_hl
		USE (m.fn_limittypy) ALIAS nlimittypy IN 0  
		USE (m.fn_limity) ALIAS nlimity IN 0  
		USE (m.fn_limitrov) ALIAS nlimitrov IN 0  
		USE (m.fn_limittypchody) ALIAS nlimittypchody IN 0  
		USE (m.fn_limitchody) ALIAS nlimitchody IN 0  
		USE (m.fn_limitdni) ALIAS nlimitdni IN 0  
		USE (m.fn_limitstatus) ALIAS nlimitstatus IN 0 
		USE (m.fn_partneri) ALIAS partneri IN 0
		USE vpartneri ALIAS zoznampartnerov IN 0
		USE vlimtypyzoznam ALIAS typy IN 0
		USE vlimhlzoznameshop IN 0 ALIAS zoznamlimitov
		USE vlimstatus ALIAS statusy IN 0
		USE vlimchodyzoznam ALIAS chody IN 0
	ENDIF 

	SELECT chody
	LOCATE FOR !zmazane AND chod="E-shop"
	IF FOUND()
		m.hladajidchod=chody.id_chod
	ELSE
		m.hladajidchod=-111
		IF m.issql
			USE (m.sqlprefix+"rlimchod") ALIAS chod1 IN 0
		ELSE 
			USE vlimchod ALIAS chod1 IN 0
		ENDIF 
		=CURSORSETPROP("Buffering" ,5,"chod1")
		INSERT INTO chod1 (c_data, chod) VALUES (m.cdata, "E-shop")
		=tux(.t.,.t.,"chod1")
		=REQUERY("chody")
		SELECT chody
		LOCATE FOR ALLTRIM(chod)="E-shop"
		IF FOUND()
			m.hladajidchod=chody.id_chod
		ENDIF 
		USE IN chod1
	ENDIF
	DO pozavirej WITH "chody, chod1"
	SELECT typy
	LOCATE FOR menotyp="E-shop"
	IF FOUND()
		m.hladajidtyp=typy.id_typ
	ELSE
		m.hladajidtyp=-111
		IF m.issql
			USE (m.sqlprefix+"rlimtyp") ALIAS typ1 IN 0
		ELSE 
			USE vlimtyp ALIAS typ1 IN 0
		ENDIF 
		=CURSORSETPROP("Buffering" ,5,"typ1")
		INSERT INTO typ1 (c_data, menotyp) VALUES (m.cdata, "E-shop")
		=tux(.t.,.t.,"typ1")
		=REQUERY("typy")
		SELECT typy
		LOCATE FOR ALLTRIM(menotyp)="E-shop"
		IF FOUND()
			m.hladajidtyp=typy.id_typ
		ENDIF 
	ENDIF
	IF m.hladajidtyp>0 AND m.hladajidchod>0
		IF m.issql
			USE (m.sqlprefix+"rlimchodyktypu") ALIAS obsahtypu IN 0
		ELSE
			USE vlimchodyktypu ALIAS obsahtypu IN 0
		ENDIF 
		=CURSORSETPROP("Buffering",5,"obsahtypu")
		SELECT obsahtypu
		LOCATE FOR id_chod=m.hladajidchod
		IF !FOUND()
			INSERT INTO obsahtypu (id_typ, c_data, id_chod) VALUES (m.hladajidtyp, m.cdata, m.hladajidchod)
			=tux(.t.,.t.,"obsahtypu")
		ENDIF 
		USE IN obsahtypu
	ENDIF 
	DO pozavirej WITH "typy, typ1"
	
	SELECT strediska
	IF !EMPTY(m.lim_def_kasa)
		LOCATE FOR c_stredisk=m.lim_def_kasa
		IF !FOUND()
			IF !EMPTY(m.lim_idkas)
				LOCATE FOR id=m.lim_idkas
				IF FOUND()
					m.hladajidkas=strediska.c_stredisk
				ELSE
					m.hladajidkas=0
				ENDIF 
			ELSE
				m.hladajidkas=0
			ENDIF 
		ELSE
			m.hladajidkas=m.lim_def_kasa
		ENDIF 
	ELSE
		IF !EMPTY(m.lim_idkas)
			LOCATE FOR id=m.lim_idkas
			IF FOUND()
				m.hladajidkas=strediska.c_stredisk
			ELSE
				m.hladajidkas=0
			ENDIF 
		ELSE
			m.hladajidkas=0
		ENDIF 
	ENDIF
	USE IN strediska


	SELECT dosleobjednavky
	SCAN ALL 
		m.hladajpartner=LEFT(ALLTRIM(dosleobjednavky.priezvisko)+" "+ALLTRIM(dosleobjednavky.meno),30)
		m.id_objednavky=dosleobjednavky.id
		IF !EMPTY(ext_id)
			m.hladajextid=dosleobjednavky.ext_id
			SELECT zoznampartnerov
			LOCATE FOR ext_id=m.hladajextid
			IF FOUND()
				SELECT 0
				IF m.issql
					USE (m.sqlprefix+"rpartnerext_kros") ALIAS partedi
				ELSE
					USE vpartnerext_kros ALIAS partedi
				ENDIF 
				=CURSORSETPROP("Buffering",5,"partedi")
				SELECT dosleobjednavky
				REPLACE c_partner WITH partedi.c_partner
				SELECT partedi
				=porovnajpartnera(m.hladajpartner)
			ELSE
				LOCATE FOR ALLTRIM(partner)=ALLTRIM(m.hladajpartner)
				IF FOUND()
					SELECT 0
					IF m.issql
						USE (m.sqlprefix+"rpartnermeno") ALIAS partedi
					ELSE
						USE vpartnermeno ALIAS partedi
					ENDIF 
					=CURSORSETPROP("Buffering",5,"partedi")
					SELECT dosleobjednavky
					REPLACE c_partner WITH partedi.c_partner
					SELECT partedi
					=porovnajpartnera(m.hladajpartner)
				ELSE 
					SELECT 0
					IF m.issql
						USE (m.sqlprefix+"rpartnermeno") ALIAS partedi
					ELSE
						USE vpartnermeno ALIAS partedi
					ENDIF 
					=CURSORSETPROP("Buffering",5,"partedi")
					INSERT INTO partedi (c_partner, partner, ico, dic, icdph, telefon, naz1, naz2, naz3, naz4, naz5, ext_id) VALUES (i_max("c_partner"), m.hladajpartner, dosleobjednavky.ico, dosleobjednavky.dic, dosleobjednavky.icdph, dosleobjednavky.telefon,;
						dosleobjednavky.firma, dosleobjednavky.adresa, dosleobjednavky.mesto, dosleobjednavky.psc, dosleobjednavky.email, dosleobjednavky.ext_id)
					=tux(.t.,.t.,"partedi")
					=REQUERY("partedi")
					SELECT partedi
					GO TOP 
					SELECT dosleobjednavky
					REPLACE c_partner WITH partedi.c_partner
					=REQUERY("zoznampartnerov")
					USE IN partedi
				ENDIF 
			ENDIF 
		ELSE 
			SELECT zoznampartnerov
			LOCATE FOR ALLTRIM(partner)=ALLTRIM(m.hladajpartner)
			IF FOUND()
				SELECT 0
				IF m.issql
					USE (m.sqlprefix+"rpartnermeno") ALIAS partedi
				ELSE
					USE vpartnermeno ALIAS partedi
				ENDIF 
				=CURSORSETPROP("Buffering",5,"partedi")
				SELECT dosleobjednavky
				REPLACE c_partner WITH partedi.c_partner
				SELECT partedi
				=porovnajpartnera(m.hladajpartner)
			ELSE 
				SELECT 0
				IF m.issql
					USE (m.sqlprefix+"rpartnermeno") ALIAS partedi
				ELSE
					USE vpartnermeno ALIAS partedi
				ENDIF 
				=CURSORSETPROP("Buffering",5,"partedi")
				INSERT INTO partedi (c_partner, partner, ico, dic, icdph, telefon, naz1, naz2, naz3, naz4, naz5, ext_id) VALUES (i_max("c_partner"), m.hladajpartner, dosleobjednavky.ico, dosleobjednavky.dic, dosleobjednavky.icdph, dosleobjednavky.telefon,;
					dosleobjednavky.firma, dosleobjednavky.adresa, dosleobjednavky.mesto, dosleobjednavky.psc, dosleobjednavky.email, dosleobjednavky.ext_id)
				=tux(.t.,.t.,"partedi")
				=REQUERY("partedi")
				SELECT partedi
				GO TOP 
				SELECT dosleobjednavky
				REPLACE c_partner WITH partedi.c_partner
				=REQUERY("zoznampartnerov")
				USE IN partedi
			ENDIF 
		ENDIF 
		
		SELECT zoznamlimitov
		LOCATE FOR txt2="Objednávka č."+ALLTRIM(STR(dosleobjednavky.id,10,0))
		IF FOUND()
			m.hladajidlimit=zoznamlimitov.id_limit
		ELSE 
			m.hladajidlimit=-1000
		ENDIF 
		IF m.issql
			USE (m.sqlprefix+"rlimhl") ALIAS lim1 IN 0
		ELSE
			USE vlimhl ALIAS lim1 IN 0
		ENDIF 
		=CURSORSETPROP("Buffering",5,"lim1")
		IF RECCOUNT("lim1")=0
			INSERT INTO lim1 (c_data, menolimit, hladina, hladina1, hladina2, c_kasy, id_typ, c_mena, c_partner, kontakt, kontakt1, firma, adr1, adr2, adr3, adr4, txt2);
				VALUES (m.cdata, LEFT(ALLTRIM(STR(m.id_objednavky,10,0))+" "+ALLTRIM(dosleobjednavky.priezvisko)+" "+ALLTRIM(dosleobjednavky.meno),50), m.lim_def_chl, jaz("form_dospely"),jaz("form_dieta"), m.hladajidkas, m.hladajidtyp, m.giakt_mena,;
				dosleobjednavky.c_partner, dosleobjednavky.dor_tel, dosleobjednavky.dor_mail, dosleobjednavky.firma,LEFT(ALLTRIM(dosleobjednavky.dor_przv)+" "+ALLTRIM(dosleobjednavky.dor_meno),30), dosleobjednavky.dor_adresa, dosleobjednavky.dor_mesto, dosleobjednavky.dor_psc, "Objednávka č."+ALLTRIM(STR(dosleobjednavky.id,10,0))) 
			=tux(.t.,.t.,"lim1")
			USE IN lim1
			=REQUERY("zoznamlimitov")
			LOCATE FOR txt2="Objednávka č."+ALLTRIM(STR(dosleobjednavky.id,10,0))
			IF FOUND()
				m.hladajidlimit=zoznamlimitov.id_limit
			ELSE
				m.hladajidlimit=0
			ENDIF 
		ELSE
			SELECT lim1
			GO TOP 
			IF ALLTRIM(menolimit)<>ALLTRIM(LEFT(ALLTRIM(STR(m.id_objednavky,10,0))+" "+ALLTRIM(dosleobjednavky.priezvisko)+" "+ALLTRIM(dosleobjednavky.meno),50))
				REPLACE menolimit WITH LEFT(ALLTRIM(STR(m.id_objednavky,10,0))+" "+ALLTRIM(dosleobjednavky.priezvisko)+" "+ALLTRIM(dosleobjednavky.meno),50)
			ENDIF 
			IF ALLTRIM(kontakt)<>ALLTRIM(dosleobjednavky.dor_tel)
				REPLACE kontakt WITH dosleobjednavky.dor_tel
			ENDIF 
			IF ALLTRIM(kontakt1)<>ALLTRIM(dosleobjednavky.dor_mail)
				REPLACE kontakt1 WITH dosleobjednavky.dor_mail
			ENDIF 
			IF ALLTRIM(firma)<>ALLTRIM(dosleobjednavky.firma)
				REPLACE firma WITH dosleobjednavky.firma
			ENDIF 
			IF ALLTRIM(adr1)<>ALLTRIM(LEFT(ALLTRIM(dosleobjednavky.dor_przv)+" "+ALLTRIM(dosleobjednavky.dor_meno),30))
				REPLACE adr1 WITH LEFT(ALLTRIM(dosleobjednavky.dor_przv)+" "+ALLTRIM(dosleobjednavky.dor_meno),30)
			ENDIF 
			IF ALLTRIM(adr2)<>ALLTRIM(dosleobjednavky.dor_adresa)
				REPLACE adr2 WITH dosleobjednavky.dor_adresa
			ENDIF 
			IF ALLTRIM(adr3)<>ALLTRIM(dosleobjednavky.dor_mesto)
				REPLACE adr3 WITH dosleobjednavky.dor_mesto
			ENDIF 
			IF ALLTRIM(adr4)<>ALLTRIM(dosleobjednavky.dor_psc)
				REPLACE adr4 WITH dosleobjednavky.dor_psc
			ENDIF 
			IF GETNEXTMODIFIED(0,"lim1")<>0
				=tux(.t.,.t.,"lim1")
			ENDIF 
			USE IN lim1
			=REQUERY("zoznamlimitov")
		ENDIF 

		IF m.hladajidlimit>0
			IF m.issql
				USE (m.sqlprefix+"rlimdnizoznameshop") ALIAS dnizoznam IN 0 
			ELSE 
				USE vlimdnizoznameshop ALIAS dnizoznam IN 0 
			ENDIF 
			=CURSORSETPROP("Buffering",5,"dnizoznam")
			SELECT dnizoznam
			LOCATE FOR datum=dosleobjednavky.datum
			IF FOUND()
				m.hladajidden=dnizoznam.id_den
			ELSE
				INSERT INTO dnizoznam (id_limit, datum, c_data, c_mena) VALUES (m.hladajidlimit, dosleobjednavky.datum, m.cdata, m.giakt_mena)
				=tux(.t.,.t.,"dnizoznam")
				=REQUERY("dnizoznam")
				SELECT dnizoznam
				LOCATE FOR datum=dosleobjednavky.datum
				IF FOUND()
					m.hladajidden=dnizoznam.id_den
				ELSE
					m.hladajidden=0
				ENDIF 
			ENDIF
			USE IN dnizoznam
			IF m.hladajidden>0
				 IF m.issql
				 	USE (m.sqlprefix+"rlimchodyden") ALIAS chodyden IN 0
				 	USE (m.sqlprefix+"rlimrovhladiny") ALIAS denhladiny IN 0
				 ELSE
				 	IF !USED("nlimitrov_hladiny")
				 		USE (m.fn_limitrovhladiny) IN 0 ALIAS nlimitrov_hladiny
				 	ENDIF 
				 	USE vlimrovhladiny ALIAS denhladiny IN 0
				 	USE vlimchodyden ALIAS chodyden IN 0
				 ENDIF 
				 =CURSORSETPROP("Buffering",5,"chodyden")
				 =CURSORSETPROP("Buffering",5,"denhladiny")
				SELECT chodyden
				LOCATE FOR id_den=m.hladajidden AND id_chod=m.hladajidchod
				IF FOUND()
					m.hladajidrov=IIF(EMPTY(chodyden.c_dokl),chodyden.id_rov,0)
				ELSE
					INSERT INTO chodyden (id_limit, id_den, id_chod, c_kasy, hladina, c_data) VALUES (m.hladajidlimit, m.hladajidden, m.hladajidchod, m.hladajidkas, m.lim_def_chl, m.cdata)
					=tux(.t.,.t.,"chodyden")
					=REQUERY("chodyden")
					SELECT chodyden
					LOCATE FOR id_den=m.hladajidden AND id_chod=m.hladajidchod
					IF FOUND()
						m.hladajidrov=IIF(EMPTY(chodyden.c_dokl),chodyden.id_rov,0)
						SELECT denhladiny
						LOCATE FOR id_rov=m.hladajidrov
						IF !FOUND()
							INSERT INTO denhladiny (id_den, id_rov, id_chod, pocet, hodnota) VALUES (m.hladajidden, m.hladajidrov, m.hladajidchod, 1, 1)
							=tux(.t.,.t.,"denhladiny")
						ENDIF 
						m.hladajidrov=0
					ENDIF 
				ENDIF 
				DO pozavirej WITH "chodyden, denhladiny, nlimitrov_hladiny"
				IF m.hladajidrov>0
					IF m.issql
						USE (m.sqlprefix+"rlimpolozkyeshop") ALIAS polozky IN 0
					ELSE
						USE vlimpolozkyeshop ALIAS polozky IN 0
					ENDIF 
					=CURSORSETPROP("Buffering" ,5, "polozky")
					SELECT polozky
					DELETE ALL 
					INSERT INTO polozky (id_limit, id_den, id_rov, c_karty, cena_prod, dan, mnozstvi, c_stredisk, vlastnik, c_hlad) SELECT m.hladajidlimit as id_limit, m.hladajidden as id_den,;
						m.hladajidrov as id_rov, c_karty, cena as cena_prod, (dph/100+1) as dan, mnozstvo as mnozstvi, m.eshop_sklad as c_stredisk, m.lim_idkas as vlastnik, 1 FROM dosleobjpol WHERE id=m.id_objednavky
					=tux(.t.,.t.,"polozky")
					USE IN polozky
					SELECT dosleobjednavky
					REPLACE id_limit WITH m.hladajidlimit, id_den WITH m.hladajidden, id_rov WITH m.hladajidrov
				ELSE
					SELECT dosleobjednavky
					REPLACE id_limit WITH m.hladajidlimit, id_den WITH m.hladajidden
				ENDIF 
			ELSE
				SELECT dosleobjednavky
				REPLACE id_limit WITH m.hladajidlimit
			ENDIF 
		ENDIF
		SELECT dosleobjednavky 			
	ENDSCAN
	LOCATE FOR !EMPTY(id_limit) AND !EMPTY(id_den) AND !EMPTY(id_rov)
	IF FOUND()
		m.potvrdenie=FCREATE(m.gcpri_path+"potvrdenie.xml")
		IF m.potvrdenie>0
			=FPUTS(m.potvrdenie,'<?xml version="1.0" encoding="utf-8"?>')
			=FPUTS(m.potvrdenie,'<mag_shop version="1.0">')
			=FPUTS(m.potvrdenie,"	<orders_response>")
			=FPUTS(m.potvrdenie,"		<status>ok</status>")
			=FPUTS(m.potvrdenie,"	</orders_response>")
			=FPUTS(m.potvrdenie,"	<orders>")
			SCAN FOR !EMPTY(id_limit) AND !EMPTY(id_den) AND !EMPTY(id_rov)
				=FPUTS(m.potvrdenie,'		<order uid="'+ALLTRIM(STR(dosleobjednavky.id,10,0))+'" />')
			ENDSCAN 
			=FPUTS(m.potvrdenie,"	</orders>")
			=FPUTS(m.potvrdenie,"</mag_shop>")
			=FCLOSE(m.potvrdenie)
*			DO FORM snake NAME lsnakex LINKED WITH 'Potvrdzujem prijatie objednávky'
*			lsnakex.DRAW()
			IF ".FXP"$UPPER(SYS(16,0))
				http.OPEN("POST","https://dev.mnamka.sk/export/action/ordersResponse/?token="+m.magneticatoken, .F.)
				http.Option(4) = 13056 
				http.settimeouts(0, 0, 0, 0)
				http.setrequestheader("content-type", "application/xml;charset=utf-8;")
				http.setrequestheader("Authorization", "Basic " + STRCONV("mnamka:hamham",13))
			ELSE
				http.OPEN("POST",m.eshop_url+"export/action/ordersResponse/?token="+m.magneticatoken, .F.)
				Http.Option(4) = 13056 
				http.settimeouts(0, 0, 0, 0)
				http.setrequestheader("content-type", "application/xml;charset=utf-8;")
			ENDIF 
			TRY 
				http.SEND(FILETOSTR(m.gcpri_path+"potvrdenie.xml"))
*				lsnakex.DRAW()
				m.httpodpoved=http.statustext
				m.vysledok=http.status
			CATCH
				m.vysledok=-100
				m.httpodpoved=jaz("form_hores_neodpoveda")
			ENDTRY 
			IF m.vysledok=200
				m.xmltext=http.responsetext
				IF LOWER(vratsekciu("status",vratsekciu("orders_response",vratsekciu("mag_shop",m.xmltext,1,"X"),1,"X"),1,"C"))="ok"
					SELECT dosleobjednavky
					REPLACE potvrdene WITH .t. FOR  !EMPTY(id_limit) AND !EMPTY(id_den) AND !EMPTY(id_rov)
				ENDIF 
			ENDIF 
*			RELEASE lsnakex
		ENDIF 
	ENDIF 
	DO pozavirej WITH "polozky"
	IF m.issql
		USE (m.sqlprefix+"rmagnetica_hlav") ALIAS hlavicky IN 0
		USE (m.sqlprefix+"rmagnetica_pol") ALIAS polozky IN 0
	ELSE
		IF !FILE(m.gccen_path+"magnetica_hlav.dbf")
			DO creafile WITH m.gccen_path+"magnetica_hlav"
		ELSE 
			USE (m.gccen_path+"magnetica_hlav") IN 0
		ENDIF 
		IF !FILE(m.gccen_path+"magnetica_pol.dbf")
			DO creafile WITH m.gccen_path+"magnetica_pol"
		ELSE 
			USE (m.gccen_path+"magnetica_pol") IN 0
		ENDIF 
		USE vmagnetica_hlav ALIAS hlavicky IN 0
		USE vmagnetica_pol ALIAS polozky IN 0
	ENDIF 
	=CURSORSETPROP("Buffering",5,"hlavicky")
	=CURSORSETPROP("Buffering",5,"polozky")
	SELECT hlavicky
	APPEND FROM DBF("dosleobjednavky")
	=tux(.t.,.t.,"hlavicky")
	SELECT polozky
	APPEND FROM DBF("dosleobjpol")
	=tux(.t.,.t.,"polozky")
	DO pozavirej WITH "hlavicky, polozky, magnetica_hlav, magnetica_pol, dosleobjednavky, dosleobjpol"
ENDIF  
DO pozavirej WITH "nlimity, nlimitrov, nlimitdni, nlimitchody, nlimittypy, polozky, strediska, nlimit_hl"
DO pozavirej WITH "nlimittypchody, nlimitstatus, partneri, zoznampartnerov, typy, zoznamlimitov, statusy, chody"
RELEASE m.http
SET PROCEDURE TO &oldproc


FUNCTION porovnajpartnera
PARAMETERS meno
IF ALLTRIM(partedi.partner)<>m.meno
	REPLACE partner WITH m.meno
ENDIF 
IF ALLTRIM(partedi.ico)<>ALLTRIM(dosleobjednavky.ico)
	REPLACE ico WITH ALLTRIM(dosleobjednavky.ico)
ENDIF 
IF ALLTRIM(partedi.dic)<>ALLTRIM(dosleobjednavky.dic)
	REPLACE dic WITH ALLTRIM(dosleobjednavky.dic)
ENDIF 
IF ALLTRIM(partedi.icdph)<>ALLTRIM(dosleobjednavky.icdph)
	REPLACE icdph WITH ALLTRIM(dosleobjednavky.icdph)
ENDIF 
IF ALLTRIM(partedi.telefon)<>ALLTRIM(dosleobjednavky.telefon)
	REPLACE telefon WITH ALLTRIM(dosleobjednavky.telefon)
ENDIF 
IF ALLTRIM(partedi.naz5)<>ALLTRIM(dosleobjednavky.email)
	REPLACE naz5 WITH ALLTRIM(dosleobjednavky.email)
ENDIF 
IF ALLTRIM(partedi.naz1)<>ALLTRIM(dosleobjednavky.firma)
	REPLACE naz1 WITH ALLTRIM(dosleobjednavky.firma)
ENDIF 
IF ALLTRIM(partedi.naz2)<>ALLTRIM(dosleobjednavky.adresa)
	REPLACE naz2 WITH ALLTRIM(dosleobjednavky.adresa)
ENDIF 
IF ALLTRIM(partedi.naz3)<>ALLTRIM(dosleobjednavky.mesto)
	REPLACE naz3 WITH ALLTRIM(dosleobjednavky.mesto)
ENDIF 
IF ALLTRIM(partedi.naz4)<>ALLTRIM(dosleobjednavky.psc)
	REPLACE naz4 WITH ALLTRIM(dosleobjednavky.psc)
ENDIF 
IF GETNEXTMODIFIED(0,"partedi")<>0
	=tux(.t.,.t.,"partedi")
ENDIF 
USE IN partedi
RETURN .t.


FUNCTION zmenastavuobjednavky
PARAMETERS cisloobj, novystav, datum
PRIVATE m.oldproc
m.oldproc=SET("procedure")
SET PROCEDURE TO spracuj_xml ADDITIVE 
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
m.magneticatoken=prihlasenie()
IF !EMPTY(m.magneticatoken)
	DO FORM snake NAME lsnakex LINKED WITH 'Odosielam zmenu stavu objednávky'
	lsnakex.DRAW()
	IF FILE(m.gcpri_path+"potvrdenie.xml")
		ERASE (m.gcpri_path+"potvrdenie.xml")
	ENDIF 
	m.potvrdenie=FCREATE(m.gcpri_path+"potvrdenie.xml")
	IF m.potvrdenie>0
		=FPUTS(m.potvrdenie,'<?xml version="1.0" encoding="utf-8"?>')
		=FPUTS(m.potvrdenie,'<mag_shop version="1.0">')
		=FPUTS(m.potvrdenie,"	<orders>")
		=FPUTS(m.potvrdenie,'		<order uid="'+ALLTRIM(STR(m.cisloobj,10,0))+'">')
		=FPUTS(m.potvrdenie,'			<uid>'+ALLTRIM(STR(m.cisloobj,10,0))+'</uid>')
		=FPUTS(m.potvrdenie,"			<status>")
		=FPUTS(m.potvrdenie,'				<uid>'+ALLTRIM(STR(m.novystav,10,0))+'</uid>')
		=FPUTS(m.potvrdenie,"			</status>")
		IF !EMPTY(m.datum)
			=FPUTS(m.potvrdenie,"			<delivery_date>"+STR(YEAR(m.datum),4,0)+"-"+PADL(ALLTRIM(STR(MONTH(m.datum),2,0)),2,"0")+"-"+PADL(ALLTRIM(STR(DAY(m.datum),2,0)),2,"0")+"</delivery_date>")
		ENDIF 
		=FPUTS(m.potvrdenie,'		</order>')
		=FPUTS(m.potvrdenie,"	</orders>")
		=FPUTS(m.potvrdenie,"</mag_shop>")
		=FCLOSE(m.potvrdenie)
		lsnakex.DRAW()
		IF ".FXP"$UPPER(SYS(16,0))
			http.OPEN("POST","https://dev.mnamka.sk/export/action/orderUpdate/?token="+m.magneticatoken, .F.)
			http.Option(4) = 13056 
			http.settimeouts(0, 0, 0, 0)
			http.setrequestheader("content-type", "application/xml;charset=utf-8;")
			http.setrequestheader("Authorization", "Basic " + STRCONV("mnamka:hamham",13))
		ELSE
			http.OPEN("POST",m.eshop_url+"export/action/orderUpdate/?token="+m.magneticatoken, .F.)
			Http.Option(4) = 13056 
			http.settimeouts(0, 0, 0, 0)
			http.setrequestheader("content-type", "application/xml;charset=utf-8;")
		ENDIF 
		TRY 
			http.SEND(FILETOSTR(m.gcpri_path+"potvrdenie.xml"))
			lsnakex.DRAW()
			m.httpodpoved=http.statustext
			m.vysledok=http.status
		CATCH
			m.vysledok=-100
			m.httpodpoved=jaz("form_eshop_neodpoveda")
		ENDTRY 
		IF m.vysledok=200
			m.xmltext=http.responsetext
			=STRTOFILE(http.ResponseText,m.gcpri_path+"odpoved.xml")
			IF LOWER(vratsekciu("status",vratsekciu("order_response",vratsekciu("mag_shop",m.xmltext,1,"X"),1,"X"),1,"C"))="ok"
				=MESSAGEBOX("Zmena zapísaná do e-shopu",64,2000)
			ENDIF 
		ELSE 
			=MESSAGEBOX(m.httpodpoved,64)
		ENDIF 
		RELEASE lsnakex, m.http
	ENDIF 
ELSE
	=MESSAGEBOX(jaz("form_eshop_neodpoveda"),64)
ENDIF 
SET PROCEDURE TO &oldproc
RETURN .t.


FUNCTION odoslicennik
PRIVATE m.oldproc
m.oldproc=SET("procedure")
SET PROCEDURE TO spracuj_xml ADDITIVE 
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
m.magneticatoken=prihlasenie()
IF !EMPTY(m.magneticatoken)
	DO FORM snake NAME lsnakex LINKED WITH 'Odosielam cenník'
	lsnakex.DRAW()
	IF FILE(m.gcpri_path+"potvrdenie.xml")
		ERASE (m.gcpri_path+"potvrdenie.xml")
	ENDIF 
	m.potvrdenie=FCREATE(m.gcpri_path+"potvrdenie.xml")
	IF m.potvrdenie>0
		=FPUTS(m.potvrdenie,'<?xml version="1.0" encoding="utf-8"?>')
		=FPUTS(m.potvrdenie,'<mag_shop version="1.0">')
		=FPUTS(m.potvrdenie,'	<products>')
		SELECT cennikeshop
		SCAN ALL 
			=FPUTS(m.potvrdenie,'		<product identifier="code">')
*			=FPUTS(m.potvrdenie,'			<title>'+STRCONV(ALLTRIM(cenik.nazev),9)+'</title>')
			=FPUTS(m.potvrdenie,'			<title>'+ALLTRIM(cennikeshop.nazev1)+'</title>')
			=FPUTS(m.potvrdenie,'			<price>'+STRTRAN(ALLTRIM(STR(cennikeshop.cena1,12,2)),",",".")+'</price>')
			=FPUTS(m.potvrdenie,'			<tax>1</tax>')
			=FPUTS(m.potvrdenie,'			<code>'+ALLTRIM(STR(cennikeshop.c_karty,10,0))+'</code>')
*			=FPUTS(m.potvrdenie,'			<available>'+ALLTRIM(STR(0,10,0))+'</available>')
			=FPUTS(m.potvrdenie,'		</product>')
		ENDSCAN 
		=FPUTS(m.potvrdenie,'	</products>')
		=FPUTS(m.potvrdenie,'</mag_shop>')
		=FCLOSE(m.potvrdenie)
		lsnakex.DRAW()
		IF ".FXP"$UPPER(SYS(16,0))
			http.OPEN("POST","https://dev.mnamka.sk/api/action/products/?token="+m.magneticatoken, .F.)
			http.Option(4) = 13056 
			http.settimeouts(0, 0, 0, 0)
			http.setrequestheader("content-type", "application/xml;charset=utf-8;")
			http.setrequestheader("Authorization", "Basic " + STRCONV("mnamka:hamham",13))
		ELSE
			http.OPEN("POST",m.eshop_url+"api/action/products/?token="+m.magneticatoken, .F.)
			Http.Option(4) = 13056 
			http.settimeouts(0, 0, 0, 0)
			http.setrequestheader("content-type", "application/xml;charset=utf-8;")
		ENDIF 
		TRY 
			http.SEND(FILETOSTR(m.gcpri_path+"potvrdenie.xml"))
			lsnakex.DRAW()
			m.httpodpoved=http.statustext
			m.vysledok=http.status
		CATCH
			m.vysledok=-100
			m.httpodpoved=jaz("form_eshop_neodpoveda")
		ENDTRY 
		IF m.vysledok=200
			m.xmltext=http.responsetext
			=STRTOFILE(http.ResponseText,m.gcpri_path+"odpoved.xml")
			IF LOWER(vratsekciu("status",vratsekciu("products",vratsekciu("mag_shop",m.xmltext,1,"X"),1,"X"),1,"C"))="ok"
				=MESSAGEBOX("Zmeny zapísané do e-shopu",64,2000)
			ENDIF 
		ELSE 
			=MESSAGEBOX(m.httpodpoved,64)
		ENDIF 
		RELEASE lsnakex, m.http
	ENDIF 
ELSE
	=MESSAGEBOX(jaz("form_eshop_neodpoveda"),64)
ENDIF 
SET PROCEDURE TO &oldproc
RETURN .t.
