PARAMETERS tabulka, operacia, prikaz, podmienka
LOCAL datcas,  pripojene, m.txtsubor, m.aaxx, m.pocet, m.impdatum, m.impsyncid, m.imptabulka
LOCAL m.impoperacia, m.impprikaz, m.imppodmienka
IF EMPTY(m.gcsyncid)
	**** Nechcem synchronizovat nic
	RETURN
ELSE
	**** vyrobim txt s aktualnou zmenou
	m.datcas=TTOC(DATETIME(),1)
	m.aaxx = FCREATE(m.gchom_path+"SYNC__"+m.datcas+"__"+SYS(2015)+".TXT")
	IF m.aaxx>0
		= FPUTS(m.aaxx, "Datum:"+m.datcas)
		= FPUTS(m.aaxx, "SyncID:"+m.gcsyncid)
		= FPUTS(m.aaxx, "Tabulka:"+m.tabulka)
		= FPUTS(m.aaxx, "Operacia:"+m.operacia)
		= FPUTS(m.aaxx, "Prikaz:"+m.prikaz)
		IF !EMPTY(m.podmienka)
			= FPUTS(m.aaxx, "Podmienka:"+m.podmienka)
		ENDIF
		= FCLOSE(m.aaxx)
	ENDIF
	DO sync_posielaj
ENDIF


FUNCTION dajriadok
PARAMETERS kde, co
LOCAL m.podkde,m.ret
IF AT(m.co,m.kde)>0
	m.podkde=SUBSTR(m.kde,AT(m.co,m.kde)+LEN(m.co))
	m.ret=LEFT(m.podkde,AT(CHR(13)+CHR(10),m.podkde))
	m.ret=ALLTRIM(STRTRAN(m.ret,CHR(13),""))
ELSE
	m.ret=""
ENDIF
RETURN m.ret


PROCEDURE sync_import
LOCAL xprikaz
SET DATABASE TO 
IF !FILE(m.gchom_path+"synchronizacia.dbf")
	CREATE TABLE (m.gchom_path+"synchronizacia.dbf") (tabulka c(20), datcas c(14))
	DO pozavirej WITH "synchronizacia"
ENDIF
DO open_database
TRY
	USE synctabulky ALIAS tabulky IN 0
	m.pripojene=.t.
CATCH
	m.pripojene=.F.
ENDTRY
IF m.pripojene
	IF !USED("synchronizacia")
		USE (m.gchom_path+"synchronizacia.dbf") IN 0
	ENDIF
	SELECT tabulky
	SCAN FOR INLIST(LOWER(tabulky.tabulka),"jidelak","objednan")
		m.tabulka=ALLTRIM(tabulky.tabulka)
		SELECT synchronizacia
		LOCATE FOR LOWER(ALLTRIM(tabulka))=LOWER(ALLTRIM(tabulky.tabulka))
		IF !FOUND()
			INSERT INTO synchronizacia (tabulka, datcas) VALUES (tabulky.tabulka,"")
			m.datcas=""
		ELSE
			m.datcas=synchronizacia.datcas
		ENDIF
		TRY
			USE synczmeny IN 0 ALIAS novinky
		CATCH
			m.pripojene=.F.
		ENDTRY
		IF m.pripojene
			SELECT novinky
			SCAN ALL
				IF ALLTRIM(novinky.tabulka)="jidelak"
					IF !USED("jidelak")
						USE (m.gchom_path+"jidelak") IN 0
					ENDIF
				ELSE
					IF !USED("objednan")
						USE (m.gchom_path+"objednan") IN 0
					ENDIF
				ENDIF
				IF novinky.operacia="INSERT"
					m.xprikaz=ALLTRIM(novinky.prikaz)
				ELSE
					m.xprikaz="UPDATE "+ALLTRIM(STRTRAN(novinky.tabulka,CHR(13),""))+" SET "+ALLTRIM(STRTRAN(novinky.prikaz,CHR(13),""))+" WHERE "+ALLTRIM(STRTRAN(novinky.podmienka,CHR(13),""))
				ENDIF
				&xprikaz
				SELECT synchronizacia
				REPLACE datcas WITH novinky.datcas FOR LOWER(ALLTRIM(tabulka))=LOWER(ALLTRIM(tabulky.tabulka))
			ENDSCAN
			DO pozavirej WITH "novinky"
		ENDIF
	ENDSCAN
	DO pozavirej WITH "tabulky, synchronizacia"
ENDIF
SET DATABASE TO 


PROCEDURE sync_posielaj
CREATE CURSOR dir_m (soubor c(100))
pocet = ADIR(dir_m,m.gchom_path + "SYNC*.TXT")
IF m.pocet>0
	FOR iii=1 TO m.pocet
		INSERT INTO dir_m (soubor) VALUE (ALLTRIM(dir_m(iii, 1)))
	ENDFOR
	m.pocet=0
	DO open_database
	TRY
		USE syncpridaj IN 0
		=CURSORSETPROP("Buffering",5,"syncpridaj")
		m.pripojene=.T.
	CATCH
		m.pripojene=.F.
	ENDTRY
	IF m.pripojene
		SELECT dir_m
		INDEX ON soubor TAG soubor
		SCAN ALL
			m.txtsubor=FILETOSTR(m.gchom_path+ALLTRIM(dir_m.soubor))
			m.impdatum=dajriadok(m.txtsubor,"Datum:")
			IF !EMPTY(m.impdatum)
				m.impsyncid=dajriadok(m.txtsubor,"SyncID:")
				IF !EMPTY(m.impsyncid)
					m.imptabulka=dajriadok(m.txtsubor,"Tabulka:")
					IF !EMPTY(m.imptabulka)
						m.impoperacia=dajriadok(m.txtsubor,"Operacia:")
						IF !EMPTY(m.impoperacia)
							m.impprikaz=dajriadok(m.txtsubor,"Prikaz:")
							IF !EMPTY(m.impprikaz)
								m.imppodmienka=dajriadok(m.txtsubor,"Podmienka:")
								INSERT INTO syncpridaj(datcas, ID, tabulka, prikaz, operacia, podmienka) VALUES (m.impdatum,m.impsyncid,m.imptabulka,m.impprikaz,m.impoperacia,m.imppodmienka)
								IF tux(.T.,.T.,"syncpridaj")
									=REQUERY("syncpridaj")
									ERASE (m.gchom_path+ALLTRIM(dir_m.soubor))
								ELSE
									=TABLEREVERT(.T.,"syncpridaj")
								ENDIF
							ELSE
								ERASE (m.gchom_path+ALLTRIM(dir_m.soubor))
							ENDIF
						ELSE
							ERASE (m.gchom_path+ALLTRIM(dir_m.soubor))
						ENDIF
					ELSE
						ERASE (m.gchom_path+ALLTRIM(dir_m.soubor))
					ENDIF
				ELSE
					ERASE (m.gchom_path+ALLTRIM(dir_m.soubor))
				ENDIF
			ELSE
				ERASE (m.gchom_path+ALLTRIM(dir_m.soubor))
			ENDIF
		ENDSCAN
		DO pozavirej WITH "syncpridaj, dir_m"

		DO sync_import

	ENDIF
	SET DATABASE TO
ENDIF
