*FUNCTION vytvor_obr_uex
PARAMETERS dennyexport, stredisko, od, do, m.odfaktury, m.hladajcdata, nepytajsa
PRIVATE m.navrat, m.ostatni, m.old_errh, m.hladajplatba
PRIVATE m.hladajcdokl, m.hladajcstredisk, m.hladajidman, m.hladajpgmparam, m.hladajpartner, m.hladajdoklad, m.hladajdatumdokl, m.hladajpz1, m.hladajpz2, m.hladajcmena, m.hladajtyppohybu
LOCAL c_prev
IF EMPTY(m.hladajcdata)
	m.hladajcdata=m.cdata
ENDIF 
m.navrat=.t.
ostatni = ""
old_errh = ON("ERROR")
had_priprava = CREATEOBJECT("had", jaz("prg_priprava_dat_na_export"))
had_priprava.SHOW()
had_priprava.set_max(2)
IF !USED("stredy")
    USE (m.gcpri_path+"strediska") AGAIN ALIAS stredy IN 0 ORDER c_stredisk
ENDIF 
IF !m.issql
    SELECT 0
    IF ! FILE(m.gcucto_path + "u_ex_obr.dbf")
        DO creafile WITH (m.gcucto_path + "u_ex_obr.dbf")
    ENDIF
    IF !USED("u_ex_obr")
        USE (m.gcucto_path + "u_ex_obr.dbf") SHARED
        INDEX ON id_man + operace + cen_hlad + partner TO (m.gcpri_path + "u_ex_obr.idx")
    ENDIF 
    SET ORDER TO
    IF !USED("skupiny")
        USE (m.fn_skupiny) ORDER druh ALIAS skupiny AGAIN
    ENDIF 
    IF !USED("karty")
        USE (m.fn_karty) ORDER c_karty ALIAS karty AGAIN IN 0
    ENDIF 
    IF !USED("partneri")
        USE (m.fn_partneri) ORDER c_partner ALIAS partneri AGAIN IN 0
    ENDIF 
    IF !USED("obraty")
        USE (m.fn_obraty) ALIAS obraty AGAIN IN 0
    ENDIF 
    IF !USED("doklady")
        SELECT 0
        USE (fn_doklady) ORDER c_dokl ALIAS doklady AGAIN
    ELSE 
        SELECT doklady
        SET ORDER TO c_dokl
    ENDIF 
ELSE
    IF !USED("karty")
        USE (m.sqlprefix+"rkartyucto") ALIAS karty IN 0
        SELECT karty
        INDEX on c_karty TAG c_karty
    ELSE
        SELECT karty
    ENDIF 
    IF !USED("skupiny")
        USE (m.sqlprefix+"rskupinyucto") ALIAS skupiny IN 0
		=RECCOUNT("skupiny")
    ENDIF 
ENDIF 
SELECT uctexpo
SET RELATION TO c_stredisk INTO stredy
had_priprava.INCREMENT(jaz("form_zaciatok"))
*** Stiahnutie dat z postgresu alebo obratov
IF m.issql
    IF m.dennyexport
		DO CASE 
			CASE INLIST(LOWER(m.gcuct_typ),"pohoda")
	            *** Pre Pohodu sa nerobi denny export skladov iba denny export faktur. Tieto oznacujem iba v dokladoch ako uctprenos. Preto tu vytahujem iba este neprenesene doklady VE, SF, FA.
    	        lsql = "SELECT obraty.c_dokl, obraty.c_karty, obraty.mnozstvi, obraty.cena, obraty.cena_part, obraty.spdan, obraty.spstupne "
        	    lsql=lsql+"FROM food600.obraty WHERE c_data=?m.hladajcdata AND c_dokl IN (SELECT c_dokl FROM food600.doklady where c_data=?m.hladajcdata AND uctexport=0 AND ((operace IN ('VE','VR') and platba<>'SF') or operace='SF'))"
			CASE INLIST(LOWER(m.gcuct_typ),"orion","asseco")
	            *** Pre Orion sa nerobi denny export skladov iba denny export faktur. Tieto oznacujem iba v dokladoch ako uctprenos. Preto tu vytahujem iba este neprenesene doklady PE.
    	        lsql = "SELECT obraty.c_dokl, obraty.c_karty, obraty.mnozstvi, obraty.cena, obraty.cena_part, obraty.spdan, obraty.spstupne "
        	    lsql=lsql+"FROM food600.obraty WHERE c_data=?m.hladajcdata AND c_dokl IN (SELECT c_dokl FROM food600.doklady where c_data=?m.hladajcdata AND uctexport=0 AND (operace='PE' or (operace IN ('VE','VR') and platba<>'SF')))"
	        OTHERWISE 
	            lsql = 'SELECT obraty.c_dokl, obraty.c_karty, obraty.mnozstvi, obraty.cena, obraty.cena_part, obraty.spdan, obraty.spstupne '
	            lsql=lsql+'FROM food600.obraty WHERE c_data=?m.hladajcdata AND c_dokl IN (SELECT c_dokl FROM food600.doklady where c_data=?m.hladajcdata AND uctexport=0)'
        ENDCASE  
    ELSE 
        IF INLIST(LOWER(m.gcuct_typ),"orion","asseco")
            *** Pre Orion sa nerobi denny export skladov iba denny export faktur. Tieto oznacujem iba v dokladoch ako uctprenos. Preto tu vytahujem iba este neprenesene doklady.
            lsql = 'SELECT obraty.c_dokl, obraty.c_karty, obraty.mnozstvi, obraty.cena, obraty.cena_part, obraty.spdan, obraty.spstupne '
            lsql=lsql+'FROM food600.obraty WHERE c_data=?m.hladajcdata AND c_dokl IN (SELECT c_dokl FROM food600.doklady where c_data=?m.hladajcdata and c_stredisk= ?m.stredisko and c_dokl>= ?m.od and c_dokl<= ?m.do AND uctexport=0)'
        ELSE
            lsql = 'SELECT obraty.c_dokl, obraty.c_karty, obraty.mnozstvi, obraty.cena, obraty.cena_part, obraty.spdan, obraty.spstupne '
            lsql=lsql+'FROM food600.obraty WHERE c_data=?m.hladajcdata AND c_dokl IN (SELECT c_dokl FROM food600.doklady where c_data=?m.hladajcdata and c_stredisk= ?m.stredisko and c_dokl>= ?m.od and c_dokl<= ?m.do)'
        ENDIF 
    ENDIF 
    DO pozavirej WITH "obr1"
    sql_select(lsql,'obr1',"c_dokl I, c_karty I, mnozstvi Y, cena Y, cena_part Y, spdan Y, spstupne Y")
ELSE
*!*        IF LOWER(m.gcuct_typ)=="orion"
*!*            SELECT obraty.c_dokl, obraty.c_karty, obraty.mnozstvi, obraty.cena, obraty.cena_part, obraty.spdan, obraty.spstupne;
*!*                FROM obraty WHERE c_dokl IN (SELECT c_dokl FROM uctexpo WHERE !uctexport) INTO CURSOR obr1 READWRITE 
*!*        ELSE
        SELECT obraty.c_dokl, obraty.c_karty, obraty.mnozstvi, obraty.cena, obraty.cena_part, obraty.spdan, obraty.spstupne;
            FROM obraty WHERE c_dokl IN (SELECT c_dokl FROM uctexpo) INTO CURSOR obr1 READWRITE 
*    ENDIF 
ENDIF 
had_priprava.INCREMENT(jaz("form konec"))
SELECT c_dokl, c_karty, mnozstvi, cena, cena_part, spdan, spstupne, $0 as prachy, $0 as prachy_par, $0 as spotdan, $0 as objem, 0000000000 as c_druh, 0000000000 as c_druhs, 0000000000 as c_druhk FROM obr1 INTO CURSOR obr11 READWRITE 
USE IN obr1
RELEASE had_priprava
*** K obr11 doplnenie udajov z karty
had_priprava = CREATEOBJECT("had", jaz("prg_uprava_dat_na_export"))
had_priprava.SHOW()
had_priprava.set_max(5)
UPDATE obr11 SET objem=karty.objem, c_druh=karty.c_druh, c_druhs=karty.c_druhs, c_druhk=karty.c_druhk from karty WHERE obr11.c_karty=karty.c_karty 
SELECT obr11
REPLACE ALL prachy WITH mnozstvi*cena, prachy_par WITH mnozstvi*cena_part, spotdan WITH mnozstvi*spdan*spstupne*objem/100
had_priprava.INCREMENT("1/5")
SELECT uctexpo.c_mena, uctexpo.typ_pohybu, obr11.c_dokl, uctexpo.c_stredisk, uctexpo.partner, SPACE(2) as id_man, SPACE(10) as pgm_param, m.hladajcdata as c_data, uctexpo.operace, obr11.prachy, obr11.prachy_par, obr11.spotdan as spdan, obr11.c_druh, obr11.c_druhk, obr11.c_druhs, uctexpo.platba,;
    uctexpo.pz1, uctexpo.pz2,    SPACE(40) as druh, SPACE(8) as md, SPACE(8) as dal, SPACE(15) as hs_md, SPACE(15) as hs_dal, SPACE(3) as spart, 0.000 as dph, uctexpo.cen_hlad, .f. as exportuj, SPACE(15) as md2, SPACE(15) as dal2, uctexpo.doklad, uctexpo.datum_dokl, .f. as samozdan, 1.000 dan_samo   FROM obr11 ;
    INNER JOIN uctexpo ON obr11.c_dokl=uctexpo.c_dokl INTO CURSOR obrpol READWRITE 

IF INLIST(LOWER(m.gcuct_typ),"pohoda")
    IF m.dennyexport
		DELETE FOR !INLIST(LEFT(operace,1),"V","S","F")
	ELSE
		DELETE FOR !INLIST(LEFT(operace,1),"V","P")
	ENDIF 
ELSE
	DELETE FOR !INLIST(LEFT(operace,1),"V","P")
ENDIF 
had_priprava.INCREMENT("2/5")
UPDATE obrpol SET id_man=stredy.id, pgm_param=stredy.pgm_param from stredy WHERE obrpol.c_stredisk=stredy.c_stredisk
UPDATE obrpol SET cen_hlad="1" WHERE operace="VK" AND EMPTY(cen_hlad)
UPDATE obrpol SET cen_hlad="" WHERE !INLIST(operace,"VK","VE","VR","SF")
had_priprava.INCREMENT("3/5")
UPDATE obrpol SET cen_hlad='01' WHERE EMPTY(cen_hlad)
UPDATE obrpol SET cen_hlad=PADL(ALLTRIM(cen_hlad),2,"0")
SET ASSERTS ON
ASSERT .f.
FOR m.i=1 TO 40
	m.si=PADL(ALLTRIM(STR(m.i,2,0)),2,"0")
	UPDATE obrpol SET druh=skupiny.druh, spart=skupiny.spart, dph=skupiny.dan&si, samozdan=skupiny.samozdan, dan_samo=skupiny.dan_samo from skupiny WHERE obrpol.c_druhs=skupiny.c_druh AND "SKL" $ pgm_param and obrpol.cen_hlad=m.si
	UPDATE obrpol SET druh=skupiny.druh, spart=skupiny.spart, dph=skupiny.dan&si, samozdan=skupiny.samozdan, dan_samo=skupiny.dan_samo from skupiny WHERE obrpol.c_druhs=skupiny.c_druh AND "SKL" $ pgm_param and obrpol.cen_hlad=m.si
	UPDATE obrpol SET druh=skupiny.druh, spart=skupiny.spart, dph=skupiny.dan&si, samozdan=skupiny.samozdan, dan_samo=skupiny.dan_samo from skupiny WHERE obrpol.c_druh=skupiny.c_druh AND !( "SKL" $ pgm_param) and obrpol.cen_hlad=m.si
	UPDATE obrpol SET druh=skupiny.druh, spart=skupiny.spart, dph=skupiny.dan&si, samozdan=skupiny.samozdan, dan_samo=skupiny.dan_samo from skupiny WHERE obrpol.c_druh=skupiny.c_druh AND EMPTY(obrpol.druh) and obrpol.cen_hlad=m.si
	UPDATE obrpol SET druh=skupiny.druh, spart=skupiny.spart, dph=skupiny.dan&si, samozdan=skupiny.samozdan, dan_samo=skupiny.dan_samo from skupiny WHERE obrpol.c_druhs=skupiny.c_druh AND EMPTY(obrpol.druh) and obrpol.cen_hlad=m.si
	UPDATE obrpol SET druh=skupiny.druh, spart=skupiny.spart, dph=skupiny.dan&si, samozdan=skupiny.samozdan, dan_samo=skupiny.dan_samo from skupiny WHERE obrpol.c_druhk=skupiny.c_druh AND EMPTY(obrpol.druh) and obrpol.cen_hlad=m.si
ENDFOR 
UPDATE obrpol SET druh=skupiny.druh, spart=skupiny.spart, dph=skupiny.dan01, samozdan=skupiny.samozdan, dan_samo=skupiny.dan_samo from skupiny WHERE obrpol.c_druhs=skupiny.c_druh AND "SKL" $ pgm_param and (VAL(obrpol.cen_hlad)=0 or VAL(obrpol.cen_hlad)>40)
UPDATE obrpol SET druh=skupiny.druh, spart=skupiny.spart, dph=skupiny.dan01, samozdan=skupiny.samozdan, dan_samo=skupiny.dan_samo from skupiny WHERE obrpol.c_druhs=skupiny.c_druh AND "SKL" $ pgm_param and (VAL(obrpol.cen_hlad)=0 or VAL(obrpol.cen_hlad)>40)
UPDATE obrpol SET druh=skupiny.druh, spart=skupiny.spart, dph=skupiny.dan01, samozdan=skupiny.samozdan, dan_samo=skupiny.dan_samo from skupiny WHERE obrpol.c_druh=skupiny.c_druh AND !( "SKL" $ pgm_param) and (VAL(obrpol.cen_hlad)=0 or VAL(obrpol.cen_hlad)>40)
UPDATE obrpol SET druh=skupiny.druh, spart=skupiny.spart, dph=skupiny.dan01, samozdan=skupiny.samozdan, dan_samo=skupiny.dan_samo from skupiny WHERE obrpol.c_druh=skupiny.c_druh AND EMPTY(obrpol.druh) and (VAL(obrpol.cen_hlad)=0 or VAL(obrpol.cen_hlad)>40)
UPDATE obrpol SET druh=skupiny.druh, spart=skupiny.spart, dph=skupiny.dan01, samozdan=skupiny.samozdan, dan_samo=skupiny.dan_samo from skupiny WHERE obrpol.c_druhs=skupiny.c_druh AND EMPTY(obrpol.druh) and (VAL(obrpol.cen_hlad)=0 or VAL(obrpol.cen_hlad)>40)
UPDATE obrpol SET druh=skupiny.druh, spart=skupiny.spart, dph=skupiny.dan01, samozdan=skupiny.samozdan, dan_samo=skupiny.dan_samo from skupiny WHERE obrpol.c_druhk=skupiny.c_druh AND EMPTY(obrpol.druh) and (VAL(obrpol.cen_hlad)=0 or VAL(obrpol.cen_hlad)>40)
 
IF LOWER(m.gcuct_pole)="spart"
    REPLACE ALL druh WITH spart
ENDIF 
REPLACE FOR EMPTY(druh) druh WITH m.ostatni
REPLACE FOR EMPTY(dan_samo) dan_samo WITH 1
IF m.spotrebnadan<>0
	INSERT INTO obrpol (c_mena, typ_pohybu, c_dokl, c_stredisk, partner, id_man, pgm_param, c_data, operace, prachy, prachy_par, spdan, c_druh, c_druhk, c_druhs, platba,;
		pz1, pz2, druh, md, dal, hs_md, hs_dal, spart, dph, cen_hlad, exportuj, md2, dal2, doklad, datum_dokl, samozdan, dan_samo) ;
		SELECT c_mena, typ_pohybu, c_dokl, c_stredisk, partner, id_man, pgm_param, c_data, operace, spdan as prachy, spdan as prachy_par, spdan, c_druh, c_druhk, c_druhs, platba,;
			pz1, pz2, "SPOT_DAN" as druh, md, dal, hs_md, hs_dal, spart, dph, cen_hlad, exportuj, md2, dal2, doklad, datum_dokl, samozdan, dan_samo FROM obrpol
ENDIF 

had_priprava.INCREMENT("4/5")
IF INLIST(LOWER(m.gcuct_typ),"softip","ekoadam","pohoda","asseco","money","navision","kros")
    REPLACE ALL druh WITH platba+" "+druh FOR operace="PE"
ENDIF
IF USED("obr")
    INSERT INTO obr (c_mena, c_dokl, c_stredisk, partner, id_man, c_data, operace, prachy, prachy_par, spdan, c_druh, c_druhk, c_druhs,;
        pz1, pz2, druh, md, dal, hs_md, hs_dal, spart, dph, cen_hlad, exportuj, md2, dal2, doklad, datum_dokl, faktura, samozdan, dan_samo, typ_pohybu, platba);
        SELECT c_mena, c_dokl, c_stredisk, partner, id_man, c_data, operace, prachy, prachy_par, spdan, c_druh, c_druhk, c_druhs,;
            pz1, pz2, druh, md, dal, hs_md, hs_dal, spart, dph, cen_hlad, exportuj, md2, dal2, doklad, datum_dokl, pz1 as faktura, samozdan, dan_samo, typ_pohybu, platba FROM obrpol 
ELSE
    SELECT c_mena, c_dokl, c_stredisk, partner, id_man, c_data, operace, prachy, prachy_par, spdan, c_druh, c_druhk, c_druhs,;
        pz1, pz2, druh, md, dal, hs_md, hs_dal, spart, dph, cen_hlad, exportuj, md2, dal2, doklad, datum_dokl,;
        SPACE(30) as co, SPACE(50) as pz, SPACE(30) as poznamka,$0 as castka, pz1 as faktura, .f. as zaloha, samozdan, dan_samo, typ_pohybu, platba, 0000 as c_prev FROM obrpol INTO CURSOR obr READWRITE 
ENDIF 

IF ATC( "sushitime", m.wi_zakaz ) > 0
	SELECT disti c_partner FROM uctexpo INTO CURSOR uctpart
	SCAN ALL 
		m.hladajcpartner=uctpart.c_partner
		IF USED("dodpartner")
			=REQUERY("dodpartner")
		ELSE 
			IF m.issql
				USE (m.sqlprefix+"rediprevpartnera") IN 0 ALIAS dodpartner
			ELSE
				USE vediprevpartnera IN 0 ALIAS dodpartner
			ENDIF 
		ENDIF 
		IF RECCOUNT("dodpartner")>0
			SELECT dodpartner
			GO TOP 
			m.c_prev=dodpartner.id_ediprev
			SELECT obr
			UPDATE obr SET c_prev=m.c_prev WHERE c_dokl IN (select c_dokl FROM uctexpo where c_partner=m.hladajcpartner)
		ENDIF 
	ENDSCAN 
	DO pozavirej WITH "dodpartner, uctpart"
ENDIF 		

*** Spracovanie OBR - dohladanie uctov, pripadne ich vyplnenie
IF USED("obr")
	IF m.gcuct_inv_sumar 
	    SELECT c_mena, c_dokl, c_stredisk, partner, id_man, c_data, operace, SUM(prachy) as prachy, SUM(prachy_par) as prachy_par, SUM(spdan) as spdan, c_druh, c_druhk, c_druhs,;
	        pz1, pz2, druh, md, dal, hs_md, hs_dal, spart, dph, cen_hlad, exportuj, md2, dal2, doklad, datum_dokl,;
	        co, pz, poznamka, castka, faktura, zaloha, samozdan, dan_samo, typ_pohybu, platba, c_prev FROM obr INTO CURSOR obrx READWRITE GROUP BY c_dokl, c_stredisk, druh WHERE operace="PI"
	   	DELETE FROM obr WHERE operace="PI"
		SELECT obr
		APPEND FROM DBF("obrx")
		USE IN obrx
	ENDIF 
    SELECT disti druh, partner, id_man, cen_hlad, operace, typ_pohybu, platba FROM obr INTO CURSOR obrdruhy READWRITE 
    m.pokracuj=.t.
    had_priprava = CREATEOBJECT("had", jaz("prg_kontrola_nastavenia_uctov"))
    had_priprava.SHOW()
    had_priprava.set_max(RECCOUNT("obrdruhy"))
    SCAN ALL 
        m.novinka=.f.
        m.exportuj=.f.
        m.hladajdruh=obrdruhy.druh
        m.hladajpartner=obrdruhy.partner
        m.hladajidman=obrdruhy.id_man
        m.hladajcenhlad=obrdruhy.cen_hlad
        m.hladajoperace=obrdruhy.operace
        m.hladajtyppohybu=obrdruhy.typ_pohybu
        m.hladajplatba=obrdruhy.platba
        IF m.hladajoperace="PI" AND m.gcuct_inv_plus_i_min
            m.hladajcenhlad="+"
        ENDIF 
        had_priprava.INCREMENT(ALLTRIM(STR(RECNO("obrdruhy"),12,0))+"/"+ALLTRIM(STR(RECCOUNT("obrdruhy"),12,0)))
        IF testuexobr(m.hladajoperace, m.hladajdruh, m.hladajpartner, m.hladajidman, m.hladajcenhlad, m.hladajtyppohybu, m.hladajplatba)
            SELECT uexobr1
            GO TOP 
            IF uexobr1.stare
                SELECT obr
                IF m.hladajoperace="PI"
                    REPLACE md WITH uexobr1.md, dal WITH uexobr1.dal, hs_md WITH uexobr1.hs_md, hs_dal WITH uexobr1.hs_dal, md2 WITH uexobr1.md2, dal2 WITH uexobr1.dal2, ;
                        co WITH uexobr1.co, pz WITH uexobr1.pz, poznamka WITH uexobr1.poznamka, exportuj WITH .t. FOR druh=m.hladajdruh AND id_man=m.hladajidman AND ;
                        operace=m.hladajoperace AND partner=m.hladajpartner AND typ_pohybu=m.hladajtyppohybu
                ELSE 
                    REPLACE md WITH uexobr1.md, dal WITH uexobr1.dal, hs_md WITH uexobr1.hs_md, hs_dal WITH uexobr1.hs_dal, md2 WITH uexobr1.md2, dal2 WITH uexobr1.dal2, ;
                        co WITH uexobr1.co, pz WITH uexobr1.pz, poznamka WITH uexobr1.poznamka, exportuj WITH .t. FOR druh=m.hladajdruh AND id_man=m.hladajidman AND ;
                        operace=m.hladajoperace AND cen_hlad=m.hladajcenhlad AND partner=m.hladajpartner AND typ_pohybu=m.hladajtyppohybu
                ENDIF 
            ELSE 
                m.novinka=.t.
            ENDIF 
        ELSE
            m.novinka=.t.
        ENDIF 
        IF m.novinka AND m.dennyexport AND EMPTY(m.nepytajsa)
            IF MESSAGEBOX(jaz("form_nenastavene_uctovanie")+CHR(13)+jaz("form_chcete_nastavt_uctovanie"),4+32+256,"FoodMan")=6
                DO FORM set1ucet
                IF EMPTY(uexobr1.MD) .OR. EMPTY(uexobr1.dal)
                ELSE 
                    SELECT obr
                    IF m.hladajoperace="PI"
                        REPLACE md WITH uexobr1.md, dal WITH uexobr1.dal, hs_md WITH uexobr1.hs_md, hs_dal WITH uexobr1.hs_dal, md2 WITH uexobr1.md2, dal2 WITH uexobr1.dal2, ;
                            co WITH uexobr1.co, pz WITH uexobr1.pz, poznamka WITH uexobr1.poznamka, exportuj WITH .t. FOR druh=m.hladajdruh AND id_man=m.hladajidman AND ;
                            operace=m.hladajoperace AND partner=m.hladajpartner AND typ_pohybu=m.hladajtyppohybu
                    ELSE
                        REPLACE md WITH uexobr1.md, dal WITH uexobr1.dal, hs_md WITH uexobr1.hs_md, hs_dal WITH uexobr1.hs_dal, md2 WITH uexobr1.md2, dal2 WITH uexobr1.dal2, ;
                            co WITH uexobr1.co, pz WITH uexobr1.pz, poznamka WITH uexobr1.poznamka, exportuj WITH .t. FOR druh=m.hladajdruh AND id_man=m.hladajidman AND ;
                            operace=m.hladajoperace AND cen_hlad=m.hladajcenhlad AND partner=m.hladajpartner AND typ_pohybu=m.hladajtyppohybu
                    ENDIF 
                ENDIF
            ENDIF
        ENDIF 
        IF INLIST(m.hladajoperace,"VE", "VR", "SF") AND m.gcuct_fa_sklady
            IF testuexobr("FA", m.hladajdruh, m.hladajpartner, m.hladajidman, m.hladajcenhlad, m.hladajtyppohybu)
                SELECT uexobr1
                GO TOP 
                IF uexobr1.stare
                    *** tu musim z obr vybrat prislusne riadky pre operacie VE a VR, prerobit ich na FA, prepisat patricnymi udajmi z uexobr1 a vratit nazad
                    SELECT * FROM obr WHERE druh=m.hladajdruh AND id_man=m.hladajidman AND operace=m.hladajoperace AND cen_hlad=m.hladajcenhlad AND partner=m.hladajpartner AND typ_pohybu=m.hladajtyppohybu INTO CURSOR obrx READWRITE 
                    REPLACE ALL operace WITH "FA", md WITH uexobr1.md, dal WITH uexobr1.dal, hs_md WITH uexobr1.hs_md, hs_dal WITH uexobr1.hs_dal, md2 WITH uexobr1.md2, dal2 WITH uexobr1.dal2, ;
                        co WITH uexobr1.co, pz WITH uexobr1.pz, poznamka WITH uexobr1.poznamka, exportuj WITH .t., faktura WITH pz2
                    SELECT  obr
                    APPEND FROM DBF("obrx")
                    USE IN obrx 
                ELSE 
                    m.novinka=.t.
                ENDIF 
            ELSE
                m.novinka=.t.
            ENDIF 
            IF m.novinka AND m.dennyexport AND EMPTY(m.nepytajsa)
	            IF MESSAGEBOX(jaz("form_nenastavene_uctovanie")+CHR(13)+jaz("form_chcete_nastavt_uctovanie"),4+32+256,"FoodMan")=6
                    DO FORM set1ucet
                    IF EMPTY(uexobr1.MD) .OR. EMPTY(uexobr1.dal)
                        *** tu musim z obr vybrat prislusne riadky pre operacie VE a VR, prerobit ich na FA, zrusit export faktury kedze nie je nastavena a vratit nazad
                        SELECT obr
                        SELECT * FROM obr WHERE druh=m.hladajdruh AND id_man=m.hladajidman AND operace=m.hladajoperace AND cen_hlad=m.hladajcenhlad AND partner=m.hladajpartner AND typ_pohybu=m.hladajtyppohybu INTO CURSOR obrx READWRITE 
                        REPLACE ALL operace WITH "FA", md WITH uexobr1.md, dal WITH uexobr1.dal, hs_md WITH uexobr1.hs_md, hs_dal WITH uexobr1.hs_dal, md2 WITH uexobr1.md2, dal2 WITH uexobr1.dal2, ;
                            co WITH uexobr1.co, pz WITH uexobr1.pz, poznamka WITH uexobr1.poznamka, exportuj WITH .f. 
                        SELECT  obr
                        APPEND FROM DBF("obrx")
                        USE IN obrx 
                    ELSE 
                        *** tu musim z obr vybrat prislusne riadky pre operacie VE a VR, prerobit ich na FA, prepisat patricnymi udajmi z uexobr1 a vratit nazad
                        SELECT obr
                        SELECT * FROM obr WHERE druh=m.hladajdruh AND id_man=m.hladajidman AND operace=m.hladajoperace AND cen_hlad=m.hladajcenhlad AND partner=m.hladajpartner AND typ_pohybu=m.hladajtyppohybu INTO CURSOR obrx READWRITE 
                        REPLACE ALL operace WITH "FA", md WITH uexobr1.md, dal WITH uexobr1.dal, hs_md WITH uexobr1.hs_md, hs_dal WITH uexobr1.hs_dal, md2 WITH uexobr1.md2, dal2 WITH uexobr1.dal2, ;
                            co WITH uexobr1.co, pz WITH uexobr1.pz, poznamka WITH uexobr1.poznamka, exportuj WITH .t., faktura WITH pz2
                        SELECT  obr
                        APPEND FROM DBF("obrx")
                        USE IN obrx 
                    ENDIF
                ELSE 
                    *** tu musim z obr vybrat prislusne riadky pre operacie VE a VR, prerobit ich na FA, zrusit export faktury kedze nie je nastavena a vratit nazad
                    SELECT obr
                    SELECT * FROM obr WHERE druh=m.hladajdruh AND id_man=m.hladajidman AND operace=m.hladajoperace AND cen_hlad=m.hladajcenhlad AND partner=m.hladajpartner AND typ_pohybu=m.hladajtyppohybu INTO CURSOR obrx READWRITE 
                    REPLACE ALL operace WITH "FA", md WITH uexobr1.md, dal WITH uexobr1.dal, hs_md WITH uexobr1.hs_md, hs_dal WITH uexobr1.hs_dal, md2 WITH uexobr1.md2, dal2 WITH uexobr1.dal2, ;
                        co WITH uexobr1.co, pz WITH uexobr1.pz, poznamka WITH uexobr1.poznamka, exportuj WITH .f. 
                    SELECT  obr
                    APPEND FROM DBF("obrx")
                    USE IN obrx 
                ENDIF
            ENDIF 
        ENDIF 
        SELECT obrdruhy
    ENDSCAN 
    RELEASE had_priprava
    SELECT disti c_dokl FROM obr WHERE !exportuj INTO CURSOR nenastavene
    IF m.dennyexport
        IF RECCOUNT("nenastavene")>0
            =MESSAGEBOX(jaz("form_nenast_doklady_nebudu_exp"),64,"FoodMan",2000)
            DELETE FROM obr WHERE c_dokl IN (SELECT c_dokl FROM nenastavene)
        ENDIF 
        IF RECCOUNT("obr")>0
            SELECT obr 
            REPLACE ALL castka WITH EVALUATE(co), pz WITH EVALUATE(pz)
			IF !m.odfaktury
	            SELECT c_dokl, STR(c_dokl,10,0) as datum, id_man, partner, doklad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, castka, pz, poznamka, spdan, faktura, zaloha, operace, druh, dph, samozdan, dan_samo, c_prev FROM obr INTO CURSOR u_ex_obraty READWRITE 
	            DO gen_exp IN uctoexp WITH .F.,STR(YEAR(DATE()),4,0),STR(MONTH(DATE()),2,0)
	            IF m.issql
					IF USED("doklx")
		                UPDATE doklx SET uctexport=.t. WHERE c_dokl in (SELECT disti c_dokl FROM obr)
		                =tux(.t.,.t.,"doklx")
		            ENDIF 
	            ELSE 
	                UPDATE doklady SET uctexport=.t. WHERE c_dokl in (SELECT disti c_dokl FROM obr)
	            ENDIF 
	        ELSE 
	        	IF LOWER(m.gcuct_typ)=="pohoda"
                    IF USED("u_ex_obraty")
                        INSERT INTO u_ex_obraty (c_dokl, datum, id_man, partner, doklad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, castka, pz, poznamka, spdan, faktura, zaloha, operace, druh, dph, samozdan, dan_samo, c_prev);
                            SELECT c_dokl, STR(c_dokl,10,0) as datum, id_man, partner, doklad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, castka, pz, poznamka, spdan, faktura, zaloha, operace, druh, dph, samozdan, dan_samo, c_prev FROM obr WHERE !EMPTY(md) OR !EMPTY(dal)
                    ELSE 
			            SELECT c_dokl, STR(c_dokl,10,0) as datum, id_man, partner, doklad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, castka, pz, poznamka, spdan, faktura, zaloha, operace, druh, dph, samozdan, dan_samo, c_prev FROM obr INTO CURSOR u_ex_obraty READWRITE WHERE !EMPTY(md) OR !EMPTY(dal)
                    ENDIF 
	        	ELSE
		            SELECT c_dokl, STR(c_dokl,10,0) as datum, id_man, partner, doklad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, castka, pz, poznamka, spdan, faktura, zaloha, operace, druh, dph, samozdan, dan_samo, c_prev FROM obr INTO CURSOR u_ex_obraty READWRITE 
	        	ENDIF 
			ENDIF 
        ELSE 
            =MESSAGEBOX(jaz("form_nic_exp_obrat_nepribudlo"),64,"FoodMan", 2000)
        ENDIF 
    ELSE 
        IF RECCOUNT("nenastavene")>0
            m.navrat=.f.
        ELSE 
            IF RECCOUNT("obr")>0
                SELECT obr 
                REPLACE ALL castka WITH EVALUATE(co), pz WITH EVALUATE(pz)
                IF INLIST(LOWER(m.gcuct_typ),"pohoda")
                *** Pohoda používa predkontácie, takže mám vyplnený iba jeden účet
                    IF USED("u_ex_obraty")
                        INSERT INTO u_ex_obraty (c_dokl, datum, id_man, partner, doklad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, castka, pz, poznamka, spdan, faktura, zaloha, operace, druh, dph, c_prev);
                            SELECT c_dokl, STR(c_dokl,10,0) as datum, id_man, partner, doklad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, castka, pz, poznamka, spdan, faktura, zaloha, operace, druh, dph, c_prev FROM obr WHERE !EMPTY(md) OR !EMPTY(dal)
                    ELSE 
                        SELECT c_dokl, STR(c_dokl,10,0) as datum, id_man, partner, doklad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, castka, pz, poznamka, spdan, faktura, zaloha, operace, druh, dph, c_prev FROM obr WHERE !EMPTY(md) OR !EMPTY(dal) INTO CURSOR u_ex_obraty READWRITE 
                    ENDIF 
                ELSE 
                    IF USED("u_ex_obraty")
                        INSERT INTO u_ex_obraty (c_dokl, datum, id_man, partner, doklad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, castka, pz, poznamka, spdan, faktura, zaloha, operace, druh, dph, c_prev);
                            SELECT c_dokl, STR(c_dokl,10,0) as datum, id_man, partner, doklad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, castka, pz, poznamka, spdan, faktura, zaloha, operace, druh, dph, c_prev FROM obr WHERE !EMPTY(md) AND !EMPTY(dal)
                    ELSE 
                        SELECT c_dokl, STR(c_dokl,10,0) as datum, id_man, partner, doklad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, castka, pz, poznamka, spdan, faktura, zaloha, operace, druh, dph, c_prev FROM obr WHERE !EMPTY(md) AND !EMPTY(dal) INTO CURSOR u_ex_obraty READWRITE 
                    ENDIF 
                ENDIF 
            ENDIF 
        ENDIF         
    ENDIF 
    DO pozavirej WITH "nenastavene, obr, obr1, obr11"
ELSE
    IF m.dennyexport
		=MESSAGEBOX(jaz("form_nic_exp_obrat_nepribudlo"),64,"FoodMan", 2000)
    ENDIF 
ENDIF 
RETURN m.navrat
