PUBLIC m.ll_lze, m.c_vydaj, 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_I", 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 c_dokl, c_partner, doklad, datum_dokl, pz1, pz2, food_dat.id_zkratka, food_dat.id_zkratka AS partner, "PV" AS operace ;
	WHERE doklady.c_stredisk = m.gic_stredisk and operace = "PV" and 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 partner + 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.c_vydaj=VAL(ALLTRIM(d_pri_i.pz2))
			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*cena AS cena_celk,SPACE(10) AS druh,.f. as zmazana WHERE c_dokl = m.c_dokl;
				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, m.giakt_mena as c_mena WHERE c_dokl = m.c_vydaj ;
				FROM obraty INTO TABLE (m.gcpri_path + "vydajka")
			INDEX ON poradi TAG poradi
			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,zmazana WITH (karty.nazev1=m.smazana_karta)
			SET RELATION OFF INTO karty
			LOCATE FOR zmazana
			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 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)
			SELECT doklady
			SET ORDER TO c_dokl
			IF SEEK(m.c_vydaj)
				SELECT vydajka
				REPLACE ALL c_mena WITH doklady.c_mena
				SELECT doklady
				SET ORDER TO 
				SELECT prijemka
				IF m.ll_lze
					LOCATE FOR celkom>0 .and. minimum<>0
					IF FOUND()
						m.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_privl WITH m.c_dokl
				RELEASE o_pri_int_had
			ELSE
				DO pozavirej WITH "prijemka, vydajka, zustatky, doklady"
				SET ORDER TO 
				=MESSAGEBOX("Príjemka sa nedá opraviť. Neviem nájsť výdajku.",16,"Food600")
			ENDIF 
		ELSE
			EXIT
		ENDIF 
		SELECT d_pri_i
		GO TOP 
	ENDDO 
ENDIF 
CLOSE DATABASES
RELEASE m.ll_lze, m.c_vydaj, m.ppartner
