*:*****************************************************************************
*: Procedure file: E:\FOOD500\SKL2\ZRC_CREA.PRG
*:*****************************************************************************
PARAMETERS odpdlhy, neodomykaj, m.cdokldo, m.menobuffera, m.lcoperace, m.neriesdlhy, m.od_polotovary_z_odpoctu
PRIVATE partner, dokl, datum_dokl, asponnieco, limitdlhov, pokracovat
LOCAL dokonc
had = CREATEOBJECT("had")
IF !m.issql
	SELECT karty
	SET ORDE TO c_karty
	SELECT sklad
	SET FILTER TO c_stredisk = gic_stredisk
	SET ORDER TO c_karty

	SELECT BUFFER
	SET RELATION TO c_karty INTO sklad
	REPLACE ALL cena WITH sklad.cena_prum*en__meny(sklad.c_mena,m.giakt_mena)
	REPLACE ALL zustatek WITH sklad.zustatek

	SET RELATION TO c_karty INTO karty            && pro dluhy se uzije alespon cena z karty
	REPLACE FOR mnozstvi=0 .AND. cena=0 cena WITH karty.cena_prum, serial_no WITH "CENA0"
	REPLACE ALL nazev WITH karty.nazev
	SET RELATION OFF INTO karty
	IF CURSORGETPROP("Buffering","buffer")>1
		tux(.T.,.T.,"buffer", "REQUERY")
	ENDIF
ENDIF
SELECT c_karty, nazev, zustatek, SUM(mnozstvi) AS mnoz_ok, SUM(dluh_mnoz) AS dluh_mnoz, cena AS cena_prum, 0.000 AS pridano, m.giakt_mena AS c_mena,;
	0000000.00 AS prachy_ok, 0000000.00 AS prachy_dl, .T. AS flg, 000000000000 AS kod, serial_no  ;
	FROM BUFFER GROUP BY c_karty INTO CURSOR zrc READWRITE
DELETE FOR mnoz_ok = 0 .AND. dluh_mnoz = 0

LOCATE FOR mnoz_ok#0
asponnieco=FOUND()
TRY 
	REPLACE ALL prachy_ok WITH mnoz_ok * cena_prum, prachy_dl WITH dluh_mnoz * cena_prum
CATCH
ENDTRY 
SUM prachy_ok, prachy_dl TO sum_ok, sum_dluh
proc_dlh = IIF(sum_ok + sum_dluh>0, ROUND(sum_dluh / (sum_ok + sum_dluh) * 100, 1), 0)
INDEX ON nazev TAG nazev
RELEASE had
view_mssg = " zrcadlo"
IF !prazdna()

	IF m.odpdlhy AND UPPER(m.actzrkadlo_typ)="NAHRADA"
		DO pr_report WITH "ZRC"
	ELSE 
		IF m.is_actzrkadlo
        
            SELECT c_karty, nazev, mnoz_ok, dluh_mnoz FROM zrc INTO CURSOR zrc_pred
            SELECT zrc 
        
			DO FORM zrkadlo WITH m.menobuffera, m.odpdlhy TO m.dokonc
			IF !m.dokonc
				RETURN .f.
			ENDIF 

            SELECT c_karty, nazev, $0 as mnoz_ok, $0 as dluh_mnoz, mnoz_ok AS mnoz_ok2, dluh_mnoz as dluh_mnoz2, "   " as pridano FROM zrc INTO CURSOR zrc_po READWRITE 
            SELECT zrc_pred
            SCAN 
                SCATTER MEMVAR 
                SELECT zrc_po
                LOCATE FOR c_karty = m.c_karty
                IF FOUND()
                    GATHER FIELDS mnoz_ok, dluh_mnoz MEMVAR 
                ELSE
                    INSERT INTO zrc_po (c_karty, nazev, mnoz_ok, dluh_mnoz, pridano) VALUES (m.c_karty, m.nazev, m.mnoz_ok, m.dluh_mnoz, "JOOO")
                ENDIF 
            ENDSCAN 
            ACTIVATE SCREEN 
            SELECT zrc_po
            SCAN FOR mnoz_ok # mnoz_ok2 .or. dluh_mnoz # dluh_mnoz2
                small_log(nazev + STR(mnoz_ok, 10, 2) + STR(dluh_mnoz, 10, 2) + "  -->  " + STR(mnoz_ok2, 10, 2) + STR(dluh_mnoz2, 10, 2), "zrc_nahrady")
            ENDSCAN 

            SELECT zrc 

		ELSE
			DO pr_report WITH "ZRC"
		ENDIF 
	ENDIF 
ENDIF 

IF m.neriesdlhy
	DELETE FROM buffer WHERE mnozstvi=0
	SELECT buffer
	IF CURSORGETPROP("Buffering","buffer")>1
		=tux(.T.,.T.,"buffer", "REQUERY")
	ENDIF
	DELETE FROM buf_dokl WHERE !c_dokl in (sele disti c_dokl FROM buffer)
	SELECT buf_dokl
	IF CURSORGETPROP("Buffering","buf_dokl")>1
		=tux(.T.,.T.,"buf_dokl", "REQUERY")
	ENDIF
ENDIF  

IF m.lcoperace="VP"
	limitdlhov=m.lim_polot
ELSE
	limitdlhov=m.lim_dluhu
ENDIF

IF IIF(m.odpdlhy, m.asponnieco,m.proc_dlh <= m.limitdlhov)
	IF MESSAGEBOX(jaz("rep_naklady")+"  "+ ALLTRIM(STR(m.sum_ok, 12,1)) + " "+en__meny(m.giakt_mena,3)+", " + TRANSFORM(m.proc_dlh, " 999.9% ")+jaz("form_dlhov") , ;
			4+32+256, jaz("odecist davku?")) = 6
        small_log(" proveden odpocet s nahradami", "zrc_nahrady")
		SELECT BUFFER
		REPLACE ALL mnozstvi WITH - mnozstvi
		IF CURSORGETPROP("Buffering","buffer")>1
			tux(.T.,.T.,"buffer", "REQUERY")
		ENDIF 
		IF ! prazdna()
			IF !m.issql
				IF !USED("obraty")
					USE (m.fn_obraty) ALIAS obraty IN 0
				ENDIF
			ENDIF
			SELECT BUFFER
			SET FILTER TO mnozstvi # 0
			PRIVATE m.cmena, m.cstr
			cmena=m.giakt_mena
			cstr=m.gic_stredisk
			IF !m.issql
				IF !USED("prep")
					SELECT c_karty, 000000.0000 AS mnozstvi, $0 AS zustatek,$0 AS cena_prum,0 AS c_mena, m.gic_stredisk as c_stredisk FROM buffer GROUP BY c_karty INTO CURSOR prep WHERE .f. READWRITE 
					INDEX ON c_karty TAG c_karty
					INDEX ON STR(c_stredisk,12,0)+STR(c_karty,12,0) TAG csck
				ENDIF 
				SELECT prep
				INSERT INTO prep SELECT c_karty, 0 AS mnozstvi, zustatek, cena, m.giakt_mena as c_mena, c_stredisk FROM BUFFER WHERE mnozstvi<>0
			ENDIF 
			IF m.lcoperace="VP"
				SELECT buf_dokl
				GO TOP
				SCATTER FIELD c_dokl MEMVAR
				had = CREATEOBJECT("had", "Odpočet dávky " + m.naz_davky + " - polotovary")
				had.SHOW()
				had.set_had(33)
				SELECT poradi, SUM(mnozstvi * cena) AS cena FROM buffer WHERE mnozstvi<>0 GROUP BY poradi INTO CURSOR ceny
				IF (! prazdna()) .or. ("D_GROS" $ UPPER(m.gchom_path))
					IF m.od_polotovary_z_odpoctu
						SELECT davka.c_karty, davka.mnozstvi FROM davka INNER JOIN ceny ON davka.c_karty=ceny.poradi WHERE davka.mnozstvi<>0 INTO CURSOR vyrobene
						UPDATE polotkarty SET vyrobene=vyrobene.mnozstvi FROM vyrobene WHERE polotkarty.c_karty=vyrobene.c_karty
						USE IN vyrobene
						SELECT polotkarty
						LOCATE FOR mnozstvi<>vyrobene
						IF FOUND()
							SET FILTER TO mnozstvi<>vyrobene
							DO FORM odp_dav_polot_yesno TO m.pokracovat
							IF !m.pokracovat
								RETURN .f.
							ENDIF 
						ENDIF 
					ENDIF 
                    IF "D_GROS" $ UPPER(m.gchom_path)
                          SELECT davka.c_karty, davka.mnozstvi, -ceny.cena as cena, davka.c_karty as poradi ;
                                 FROM davka LEFT JOIN ceny ON davka.c_karty=ceny.poradi WHERE davka.mnozstvi<>0 INTO CURSOR gros_polo READWRITE 
                          REPLACE FOR ISNULL(cena) cena WITH 0
                       INSERT INTO buffer (menobuf, c_data, c_stredisk, operace, c_karty, mnozstvi, cena, poradi, c_dokl) ;
                                       SELECT m.menobuffera as menobuf, m.cdata as c_data, m.gic_stredisk as c_stredisk, "PP" as operace, ;
                                              c_karty, mnozstvi, cena, poradi, m.c_dokl as c_dokl ;
                                              FROM gros_polo
                    ELSE
                       INSERT INTO buffer (menobuf, c_data, c_stredisk, operace, c_karty, mnozstvi, cena, poradi, c_dokl) ;
                                       SELECT m.menobuffera as menobuf, m.cdata as c_data, m.gic_stredisk as c_stredisk, "PP" as operace, ;
                                              davka.c_karty, davka.mnozstvi, -ceny.cena as cena, davka.c_karty as poradi, m.c_dokl as c_dokl ;
                                              FROM davka INNER JOIN ceny ON davka.c_karty=ceny.poradi WHERE davka.mnozstvi<>0
                    ENDIF 
					IF CURSORGETPROP("Buffering","buffer")>1
						tux(.t.,.t.,"buffer", "REQUERY")
					ENDIF 
					UPDATE buffer SET cena=cena/mnozstvi WHERE operace="PP" AND mnozstvi<>0
					IF CURSORGETPROP("Buffering","buffer")>1
						tux(.t.,.t.,"buffer", "REQUERY")
						RECCOUNT("buffer")
					ENDIF 

					IF !m.issql
						SELECT prep 
						DELETE ALL
						INSERT INTO prep (c_karty, c_stredisk) SELECT c_karty, m.gic_stredisk as c_stredisk FROM davka
						DO prepocet2 WITH "prep",0,m.cdokldo,.F.,.F.
						SELECT prep
						SET RELATION TO c_karty INTO a_skl
						SCAN ALL
							IF a_skl.k_odpoctu<>prep.zustatek
								REPLACE a_skl.k_odpoctu WITH prep.zustatek, a_skl.zustatek WITH prep.zustatek,a_skl.prepocet WITH .T., cena_prum WITH prep.cena_prum, c_mena WITH prep.c_mena
								SELECT sklad
								REPLACE zustatek WITH prep.zustatek, cena_prum WITH prep.cena_prum, c_mena WITH prep.c_mena FOR c_karty=prep.c_karty .AND. c_stredisk=prep.c_stredisk
								SELECT prep
							ELSE
								REPLACE a_skl.prepocet WITH .t.
							ENDIF 
						ENDSCAN 
						SELECT prep
						DELETE ALL
						INSERT INTO prep SELECT c_karty, 0 AS mnozstvi, zustatek, cena_prum, c_mena, c_stredisk FROM a_skl WHERE c_karty IN (SELECT DISTINCT c_karty FROM buffer)
					ENDIF
				ENDIF
				DO pozavirej WITH "ceny"
			ENDIF 
			DO buff_out2 WITH .F.,m.cmena,m.cstr,m.cdokldo,m.neodomykaj, m.menobuffera, m.neriesdlhy
			RELEASE m.cmena, m.cstr
			IF !m.issql
				SELECT  prep
				DELETE ALL
			ENDIF 
			DO pozavirej WITH "a_skl"
			IF !m.issql
				*		REPLACE ALL mnozstvi WITH dluh_mnoz
				SELECT buffer
				IF CURSORGETPROP("Buffering","buffer")>1
					=REQUERY("buffer")
				ENDIF 
				IF !m.neriesdlhy
					SELECT dluhy
					DO zamkni
					INSERT INTO dluhy (c_dokl, operace, c_karty, mnozstvi, c_stredisk) SELECT c_dokl, operace, c_karty, dluh_mnoz AS mnozstvi, c_stredisk FROM BUFFER WHERE dluh_mnoz<>0
					UNLOCK
				ENDIF 
			ENDIF
		ENDIF
		SELECT BUFFER
		SET FILTER TO
		GO TOP

		*		SET RELATION TO c_karty INTO karty				&& pro dluhy se uzije alespon cena z karty
		*		REPLACE FOR mnozstvi=0 .AND. cena=0 cena WITH karty.cena_prum
		*		SET RELATION TO
		RETURN .T.
	ELSE
		RETURN .F.
	ENDIF
ELSE
	IF m.odpdlhy
		MESSAGEBOX(jaz("form neni co odecist"),64,jaz("form_neodcitane"), 4444)
	ELSE
		 MESSAGEBOX(jaz("form_teoreticke_naklady")+": " + ALLTRIM(STR(sum_ok + sum_dluh, 12,2)) + ;
			CHR(13) + TRANSFORM(m.proc_dlh, "999.9% ")+jaz("form_dlhov"), 16, jaz("form_neodcitane"))
	ENDIF
	RETURN .F.
ENDIF


*:*****************************************************************************
PROCEDURE v_pridano
RETURN (pridano <= zustatek - mnoz_ok)
