PUBLIC m.ll_lze, m.ppartner
CLOSE DATABASES
chyba_pri_otevirani = .F.
TRY
	IF m.issql
		otevira_se = "milanovo SQL"
	ELSE
		otevira_se = m.fn_obraty
		USE (m.fn_obraty) ALIAS obraty IN 0
		otevira_se = m.fn_sklad
		USE (m.fn_sklad)    ALIAS sklad    IN 0
		otevira_se = m.fn_doklady
		USE (m.fn_doklady)  ALIAS doklady  IN 0
		otevira_se = m.fn_food_dat
		USE (m.fn_food_dat)  ALIAS food_dat  IN 0
	ENDIF
CATCH TO oErr 
    otevira_se = m.otevira_se + CHR(13) + ALLTRIM(STR(oErr.ErrorNo)) + " " + oErr.Message 
	chyba_pri_otevirani = .T.
ENDTRY
IF chyba_pri_otevirani
	=MESSAGEBOX(jaz("prg_dbf_neprist_skuste_pozdeji")+CHR(13)+m.otevira_se,64,"Food600",2000)
	DO zurnal_log WITH "OPERA", "O_PRI_B", jaz("nejde_otevrit") + m.otevira_se
	RETURN .F.
ENDIF

******  opravovatelnost jen od KK dale  ****** zacatek
min_c_dokl = od_kdy_smi_opravit_doklady()
******  opravovatelnost jen od KK dale  ****** konec

SELECT doklady.c_dokl, doklady.c_partner, doklady.doklad, doklady.datum_dokl, doklady.pz1, doklady.pz2, food_dat.id_zkratka, food_dat.id_zkratka AS partner, "PB" AS operace ;
	WHERE doklady.c_stredisk = m.gic_stredisk AND doklady.operace = "PB" AND doklady.c_dokl > m.min_c_dokl ;
	FROM doklady LEFT JOIN food_dat ON food_dat.c_stredisk = doklady.c_partner ;
	INTO TABLE (m.gcpri_path + "d_pri_i")
INDEX ON partner + STR(doklad) TAG partner
INDEX ON c_dokl TAG c_dokl
INDEX ON datum_dokl TAG datum_dokl
INDEX ON STR(doklad) TAG pd

IF ! prazdna(.T.)
	DO WHILE .T.
		IF USED("d_pri_i")
			SELECT d_pri_i
			SET FILTER TO
		ENDIF
		DO FORM oprava1 TO c_dokl
		IF ! EMPTY(m.c_dokl)
			c_dokl=VAL(LEFT(m.c_dokl,LEN(m.c_dokl)-2))
			IF USED("d_pri_i")
				SELECT d_pri_i
			ELSE
				SELECT 0
				USE (m.gcpri_path+"d_pri_i")
			ENDIF
			LOCATE FOR c_dokl=m.c_dokl
			m.ppartner=d_pri_i.partner
			IF !USED("sklad")
				SELECT 0
				USE (m.fn_sklad) ALIAS sklad
			ENDIF
			IF !USED("obraty")
				SELECT 0
				USE (m.fn_obraty) ALIAS obraty
			ENDIF
			IF !USED("doklady")
				SELECT 0
				USE (m.fn_doklady) ALIAS doklady
			ENDIF
			SELECT c_karty, SPACE(30) AS nazev, mnozstvi, cena, poradi, mnozstvi AS minimum, mnozstvi AS celkom,mnozstvi AS lze,mnozstvi*cena AS cena_celk,SPACE(10) AS druh,.F. AS zmaz WHERE c_dokl = m.c_dokl .AND. mnozstvi>0;
				FROM obraty INTO TABLE (m.gcpri_path + "prijemka") ORDER BY poradi
			INDEX ON poradi TAG poradi
			SELECT c_karty, SUM(mnozstvi) AS celkom FROM prijemka INTO CURSOR sumy GROUP BY c_karty
			INDEX ON c_karty TAG c_karty
			SELECT prijemka
			SET RELATION TO c_karty INTO sumy
			REPLACE ALL prijemka.celkom WITH sumy.celkom, minimum WITH 0
			SET RELATION OFF INTO sumy
			USE IN sumy
			SELECT c_karty, SPACE(30) AS nazev, mnozstvi, cena, poradi,-cena*mnozstvi AS celkom WHERE c_dokl = m.c_dokl .AND. mnozstvi<0;
				FROM obraty INTO TABLE (m.gcpri_path + "vydajka")
			INDEX ON poradi TAG poradi
			SELECT c_karty, SUM(mnozstvi) AS celkom FROM vydajka INTO CURSOR sumy GROUP BY c_karty
			INDEX ON c_karty TAG c_karty
			SELECT prijemka
			SET RELATION TO c_karty INTO sumy
			REPLACE ALL prijemka.lze WITH prijemka.celkom+sumy.celkom
			SET RELATION OFF INTO sumy
			USE IN sumy
			IF USED("karty")
				SELECT karty
			ELSE
				SELECT 0
				USE (m.fn_karty) ALIAS karty
			ENDIF
			SET ORDER TO c_karty
			SELECT prijemka
			SET RELATION TO c_karty INTO karty
			REPLACE ALL nazev WITH karty.nazev, druh WITH karty.druh, zmaz WITH (karty.nazev1=m.smazana_karta)
			SET RELATION OFF INTO karty
			LOCATE FOR zmaz
			IF FOUND()
				m.ll_lze=.F.
			ELSE
				m.ll_lze=.T.
			ENDIF
			SELECT vydajka
			SET RELATION TO c_karty INTO karty
			REPLACE ALL nazev WITH karty.nazev
			LOCATE FOR (karty.nazev1=m.smazana_karta)
			IF FOUND()
				m.ll_lze=.F.
			ENDIF
			SET RELATION OFF INTO karty
			*!*				SELECT c_karty, MIN(zustatek) AS zustatek GROUP BY c_karty FROM obraty INTO TABLE (m.gcpri_path + "zustatky") ;
			*!*					WHERE c_stredisk = m.gic_stredisk and c_dokl > m.c_dokl and INLIST(LEFT(operace,1), "P", "V")
			*!*				SELECT c_karty, 999999999 AS zustatek FROM (m.gcpri_path + "prijemka") INTO TABLE (m.gcpri_path + "zustatky2") ;
			*!*					WHERE c_karty  NOT IN ( SELECT c_karty FROM (m.gcpri_path + "zustatky") )
			*!*				SELECT zustatky
			*!*				APPEND FROM (m.gcpri_path + "zustatky2")
			*!*				INDEX ON c_karty TAG c_karty
			*!*				USE IN zustatky2
			*!*				SELECT prijemka
			*!*				SET RELATION TO c_karty INTO zustatky
			*!*				REPLACE ALL minimum WITH zustatky.zustatek
			*!*				SET RELATION OFF INTO zustatky
			SELECT prijemka
			SCAN ALL
				SCATTER MEMVAR FIELDS c_karty
				SELECT MIN(c_dokl) AS c_dokl FROM obraty WHERE c_karty=m.c_karty .AND. operace="PI" .AND. c_dokl>m.c_dokl .and. c_stredisk=m.gic_stredisk INTO CURSOR pic_dokl
				IF RECCOUNT()>0
					GO TOP
					m.pic_dokl=pic_dokl.c_dokl
				ELSE
					m.pic_dokl=999999999999
				ENDIF
				USE IN pic_dokl
				SELECT c_karty, MIN(zustatek) AS limit FROM obraty WHERE c_karty =m.c_karty .AND. c_dokl>m.c_dokl .AND. c_dokl<m.pic_dokl .and. c_stredisk=m.gic_stredisk INTO CURSOR minima
				IF RECCOUNT()>0
					SELECT prijemka
					REPLACE minimum WITH minima.limit
				ELSE
					SELECT prijemka
					REPLACE minimum WITH celkom
				ENDIF
				USE IN minima
			ENDSCAN
			REPLACE ALL prijemka.minimum WITH MAX(prijemka.celkom-prijemka.minimum,0)
			IF m.ll_lze
				SELECT prijemka
				LOCATE FOR lze>0 .AND. minimum>0
				IF FOUND()
					ll_lze = .F.
				ENDIF
			ENDIF
			*			SELECT prijemka
			*			SET RELATION TO poradi INTO vydajka
			*			GO TOP
			DO pozavirej WITH "prijemka, vydajka, zustatky, doklady, obraty, sklad, karty"
			DO FORM o_burak WITH m.c_dokl
			RELEASE o_pri_int_had
		ELSE
			EXIT
		ENDIF
		SELECT d_pri_i
		GO TOP
	ENDDO
ENDIF
CLOSE DATABASES
RELEASE m.ll_lze, m.ppartner
