*!*****************************************************************************
*!      Procedure: ODP_POL
*!*****************************************************************************
PARAMETERS neodomykaj, cdokldo
PRIVATE m.hladajcstredisk, m.menobuffera_old, m.lic_dokl, m.hladajcdokl, m.nakl_celk, m.je_odecteno, m.pz1, m.pz2, m.menobuf_old
m.hladajcstredisk=m.gic_stredisk
m.menobuf_old=m.menobuffera
SELECT c_karty, SUM(mnozstvi) AS mnozstvi, 0 AS c_dokl, c_karty AS poradi, na_sklade, nazev;
       GROUP BY c_karty FROM buffer INTO CURSOR davka READWRITE 

DELETE FOR mnozstvi=0
IF !m.issql
	DELETE FOR (mnozstvi < na_sklade) .and. (na_sklade < 999999.99)
ENDIF 
IF prazdna()
	=MESSAGEBOX(jaz("form_nie_je_co_vyrabat"),16,jaz("form polotovary"))
	RETURN .f.
ENDIF 
		
had = CREATEOBJECT("had", jaz("prg_odpocet_davky") )
had.SHOW()

naz_davky  ="VYD_POLOT"
datum_dokl = DATE()
lcoperace  = "VP"
lic_dokl   = -8888
pz1        = "VYD_POLOT"
pz2        = ""
menobuffera=LOWER(SUBSTR(SYS(2015),4))

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
IF m.issql
	m.vysledok=sql_exec("SELECT food600.odpocet_polotovarov_buf('"+m.menobuffera+"',"+ALLTRIM(STR(m.cdata,2,0))+","+ALLTRIM(STR(m.hladajcstredisk,10,0))+","+ALLTRIM(STR(m.giakt_mena,2,0))+",'',0,'"+m.menobuf_old+"')")
	=REQUERY("buffer")
	=REQUERY("buf_dokl")
	SELECT buf_dokl
	REPLACE provedl WITH m.gcuser_id
	=tux(.t.,.t., "buf_dokl")
	=REQUERY("buf_dokl")
ELSE 
	=REQUERY("buffer")
	=REQUERY("buf_dokl")
	chyba_pri_otevirani = .F.
	TRY  
		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.hladajcstredisk 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
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
	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

	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
	SET RELATION TO
	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, poradi) SELECT c_karty, SUM(mnozstvi) AS dluh_mnoz, SUM(odecteno) AS mnozstvi, ;
			"Q" AS LABEL, -8888 AS c_dokl, m.lcoperace as operace, m.hladajcstredisk as c_stredisk, m.menobuffera as menobuf, m.cdata as c_data, m.lic_dokl as poradi FROM sk1 GROUP BY c_karty WHERE sk1.mnozstvi # 0 .OR. sk1.odecteno # 0
	ENDSCAN

	INSERT INTO buf_dokl (operace, c_dokl, datum_dokl, pz1, pz2, c_partner, menobuf, c_data, c_stredisk) ;
		VALUES (m.lcoperace, -8888, m.datum_dokl, m.pz1, m.pz2, m.hladajcstredisk, m.menobuffera, m.cdata, m.hladajcstredisk)
	=tux(.t.,.t.,"buffer")
	=REQUERY("buffer")

	DO corr_polot_vydej

	=tux(.t.,.t.,"buffer")
	=tux(.t.,.t.,"buf_dokl")
	=REQUERY("buffer")
	=REQUERY("buf_dokl")
	DO pozavirej WITH "sk1, sk2, qqq, kalk"
ENDIF 
WAIT CLEAR
RELEASE had
IF RECCOUNT("buffer")>0
	zrc_title = jaz("prg_rep_polotovary")+" " + m.pz1 
	je_odecteno = zrc_crea(.F.,.T.,m.cdokldo, m.menobuffera, m.lcoperace)
	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
ELSE
	m.je_odecteno=.t.
ENDIF 
DO vymaz_buffer WITH m.menobuffera
m.menobuffera=m.menobuf_old
=REQUERY("buffer")
=REQUERY("buf_dokl")
DO pozavirej WITH "obraty, prep, a_skl"
RETURN m.je_odecteno


*!*****************************************************************************
PROCEDURE corr_polot_vydej
PRIVATE mnozstvi, c_karty
SELECT davka
SCAN
   SCATTER FIELDS c_karty, mnozstvi MEMVAR 
   SELECT buffer
   LOCATE FOR c_karty = m.c_karty
   IF FOUND()
        ubrat = MIN(mnozstvi, m.mnozstvi)
        REPLACE mnozstvi WITH mnozstvi - m.ubrat
        SELECT davka 
        REPLACE mnozstvi WITH mnozstvi - m.ubrat
   ELSE
        SELECT davka 
   ENDIF 
ENDSCAN 
SELECT buffer
DELETE FOR mnozstvi = 0
