*!*****************************************************************************
*!      Procedure: ODP_POL
*!*****************************************************************************
PARAMETERS naz_davky, datum_dokl, s_narustem, odkial, ododpoctu, m.mnb, m.cislouz
PRIVATE m.cdokldo, m.hladajcstredisk, m.menobuffera, m.lic_dokl, m.hladajcdokl, m.nakl_celk, m.je_odecteno, m.pz1, m.pz2
m.hladajcstredisk=m.gic_stredisk
IF EMPTY(m.odkial)
	m.odkial="buf_dav"
ENDIF 
SELECT c_karty, SUM(mnozstvi)+0.0000 AS mnozstvi, c_karty AS c_dokl, 0000000 AS poradi, nazev ;
	GROUP BY c_karty FROM (m.odkial) INTO CURSOR davka READWRITE 
SELECT davka
DELETE FOR mnozstvi=0
IF prazdna()
	=MESSAGEBOX(jaz("form_nie_je_co_vyrabat"),16,jaz("form polotovary"))
	RETURN .f.
ENDIF 
m.lic_dokl = -8888
m.nakl_celk = -11.11
lcoperace = "VP"
IF !m.ododpoctu
	m.menobuffera=LOWER(SUBSTR(SYS(2015),4))
	chyba_pri_otevirani = .F.
	TRY  
		IF m.issql
	    	otevira_se = "rbufferd"
			USE (m.sqlprefix+"rbufferd")	ALIAS buf_dokl IN 0
	    	otevira_se = "rbuffer"
			USE (m.sqlprefix+"rbuffer")	ALIAS buffer IN 0
		ELSE
	    	otevira_se = m.fn_buff_r
			USE (m.fn_buff_r)     ALIAS buff_r     IN 0 
	    	otevira_se = m.fn_buff_d
			USE (m.fn_buff_d)     ALIAS buff_d     IN 0 
	    	otevira_se = "vbuffer"
			USE vbuffer  ALIAS buffer IN 0
			otevira_se = "vbufferd"
			USE vbufferd ALIAS buf_dokl IN 0
			SELECT 0
			otevira_se = "vodpoctyaskl"
			USE vodpoctyaskl ALIAS a_skl
			INDEX on c_karty TAG c_karty
			otevira_se = "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 davka GROUP BY c_karty INTO CURSOR prep READWRITE 
			INDEX ON c_karty TAG c_karty
			INDEX ON STR(c_stredisk,12,0)+STR(c_karty,12,0) TAG csck
		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,"FoodMan",2000)
	     DO zurnal_log WITH "OPERA", "Odpočet polotovarov", jaz("nejde_otevrit") + m.otevira_se
	     RETURN .F.
	ENDIF
	=CURSORSETPROP("Buffering" ,5,"buffer")
	=CURSORSETPROP("Buffering" ,5,"buf_dokl")
ELSE
	m.menobuffera=m.mnb
ENDIF 
*!*	naz_davky2 = m.naz_davky
had = CREATEOBJECT("had", jaz("prg_odpocet_davky")+" " + m.naz_davky )
had.SHOW()

m.pz1 = m.naz_davky
m.pz2 = ""

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
DO pozavirej WITH "aaa"
IF m.issql
	SELECT 0
	USE (m.sqlprefix+"rmaxcdokl") ALIAS aaa
	m.cdokldo=aaa.c_dokl
	USE IN aaa
	IF m.ododpoctu
*** Ak cislouz>0 ide o klasicky odpocet, ak <0 (v odpocte boli upravene polozky), musim volat odpocet podla stlpca stor_mno (posledny parameter nastaveny na 1)
		IF m.cislouz>0
			m.vysledok=sql_exec("SELECT food600.odpocet_polotovarov_skpol('"+m.menobuffera+"',"+ALLTRIM(STR(m.cdata,2,0))+","+ALLTRIM(STR(m.gic_stredisk,10,0))+","+ALLTRIM(STR(m.giakt_mena,2,0))+","+ALLTRIM(STR(m.cislouz,5,0))+","+ALLTRIM(STR(IIF(m.s_narustem,1,0),1,0))+",0"+")")
		ELSE 
			m.vysledok=sql_exec("SELECT food600.odpocet_polotovarov_skpol('"+m.menobuffera+"',"+ALLTRIM(STR(m.cdata,2,0))+","+ALLTRIM(STR(m.gic_stredisk,10,0))+","+ALLTRIM(STR(m.giakt_mena,2,0))+","+ALLTRIM(STR(-m.cislouz,5,0))+","+ALLTRIM(STR(IIF(m.s_narustem,1,0),1,0))+",1"+")")
		ENDIF 
	ELSE 
		m.vysledok=sql_exec("SELECT food600.odpocet_polotovarov('"+m.menobuffera+"',"+ALLTRIM(STR(m.cdata,2,0))+","+ALLTRIM(STR(m.gic_stredisk,10,0))+","+ALLTRIM(STR(m.giakt_mena,2,0))+",'"+m.hladajdavku+"',"+ALLTRIM(STR(IIF(m.s_narustem,1,0),1,0))+",'')")
	ENDIF 
	=REQUERY("buffer")
	=REQUERY("buf_dokl")
	SELECT buf_dokl
	REPLACE provedl WITH m.gcuser_id, datum_dokl WITH m.datum_dokl
	=tux(.t.,.t., "buf_dokl")
	=REQUERY("buf_dokl")
ELSE 
	SET ORDER TO c_karty IN karty
	SELECT polozky
	SET RELATION TO c_karty INTO karty
	REPLACE ALL pocet WITH karty.pocet
	SET RELATION TO

	IF !USED("obraty")
		SELECT 0
		USE (m.fn_obraty) ALIAS obraty
	ENDIF

	SELECT 0
	USE vmaxcdokl ALIAS aaa
	m.cdokldo=aaa.c_dokl
	USE IN aaa 

	IF m.s_narustem
		SELECT a_skl
		REPLACE ALL k_odpoctu WITH 0
		UPDATE a_skl SET prepocet=.t. FROM prep WHERE a_skl.c_karty=prep.c_karty
	ELSE 
		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
		SET RELATION OFF INTO a_skl
	ENDIF
	SELECT prep
	DELETE ALL

	SELECT davka
	SCAN
		c_karty  = davka.c_karty
		lic_dokl = m.c_karty
		SELECT c_karty, mnozstvi, 0000000.0000 AS odecteno WHERE c_karty = m.c_karty ;
			FROM davka INTO CURSOR sk1 READWRITE 
		DO kalkuluj WITH m.hladajcstredisk, .f., m.cdokldo
		SELECT BUFFER
		INSERT INTO buffer (c_karty, dluh_mnoz, mnozstvi, label, c_dokl, operace, c_stredisk, menobuf, c_data) SELECT c_karty, SUM(mnozstvi) AS dluh_mnoz, SUM(odecteno) AS mnozstvi, ;
			"Q" AS LABEL, m.lic_dokl AS c_dokl, m.lcoperace as operace, m.gic_stredisk as c_stredisk, m.menobuffera as menobuf, m.cdata as c_data FROM sk1 GROUP BY c_karty WHERE sk1.mnozstvi # 0 .OR. sk1.odecteno # 0
	ENDSCAN

	INSERT INTO buf_dokl (c_stredisk, operace, c_dokl, datum_dokl, pz1, pz2, c_partner, menobuf, c_data) ;
		VALUES ( m.hladajcstredisk, m.lcoperace, -8888, m.datum_dokl, m.pz1, m.pz2, m.hladajcstredisk, m.menobuffera, m.cdata)
	=tux(.t.,.t.,"buffer")
	=tux(.t.,.t.,"buf_dokl")
	=REQUERY("buffer")
	=REQUERY("buf_dokl")
	SELECT BUFFER
	REPLACE ALL poradi WITH c_dokl, c_dokl WITH -8888
	DO pozavirej WITH "sk1, sk2, qqq, kalk"
ENDIF 
WAIT CLEAR
RELEASE had

IF !testujpravobuffera(m.menobuffera)
	=MESSAGEBOX(jaz("form_nepravo_doklad")+CHR(10)+jaz("form_neda_sa_pokracovat"),16,"FoodMan")
	DO vymaz_buffer WITH m.menobuffera
	RETURN .f.
ENDIF 

zrc_title = jaz("prg_rep_polotovary")+" " + m.pz1 + IIF(m.s_narustem, " "+jaz("prg_rep_s_narustem"), "")
je_odecteno = zrc_crea(.F.,.T.,m.cdokldo, m.menobuffera, m.lcoperace,.f.,m.ododpoctu)
IF je_odecteno
	SELECT zrc
	SUM mnoz_ok* cena_prum TO nakl_celk
	SELECT buf_dokl
	=REQUERY("buf_dokl")
	m.hladajcdokl=buf_dokl.c_dokl
	DO prn_obra WITH m.hladajcdokl
ENDIF
DO vymaz_buffer WITH m.menobuffera
IF !m.ododpoctu
	DO pozavirej WITH "obraty, prep, buffer, buf_dokl, buff_r, buff_d, a_skl"
ENDIF 
RETURN m.je_odecteno


