
PRIVATE m.c_ean,c_dokl, c_partner, datum_dokl, pz1, pz2, doklad, kdy, m.c_prev, m.prevadzkaren, pokracovat, c_prev_part, menopart, m.boladb
m.boladb=.f.
SELECT 0
DO open_database
IF !m.issql
	USE (m.fn_obr_alk) ALIAS obr_alk IN 0
ENDIF 
SELECT buf_dokl
SCAN FOR INLIST(operace,"PE","VE","VR","VV")
	m.pokracovat=.t.
	SCATTER MEMVAR 
	IF m.alkoprevadzky
		IF m.issql
			rv("prevadzky", "prevadzky")
			rv("prevadzkyusedstred", "prevadzkarne")
			SELECT prevadzkarne
			LOCATE FOR c_stredisk=m.c_stredisk
			IF FOUND()
				m.c_prev=prevadzkarne.c_prev
				SELECT prevadzky
				LOCATE FOR c_prev=m.c_prev AND !EMPTY(meno)
				m.prevadzkaren=prevadzky.meno
			ELSE
				m.c_prev=0
				m.prevadzkaren=m.hotel_name
			ENDIF 
			IF m.operace="VV"
				SELECT prevadzkarne
				LOCATE FOR c_stredisk=m.c_partner
				IF FOUND()
					m.c_prev_part=prevadzkarne.c_prev
					SELECT prevadzky
					LOCATE FOR c_prev=m.c_prev_part AND !EMPTY(meno)
					m.menopart=prevadzkarne.meno
				ELSE
					m.c_prev_part=0
					m.menopart=m.hotel_name
				ENDIF 
			ENDIF 		
			USE IN prevadzkarne
			USE IN prevadzky
		ELSE 
			SELECT 0
			USE (m.gchom_path+"prevadzkarne.dbf")
			LOCATE FOR c_stredisk=m.c_stredisk
			IF FOUND()
				m.c_prev=prevadzkarne.c_prev
				LOCATE FOR c_prev=m.c_prev AND !EMPTY(meno)
				m.prevadzkaren=prevadzkarne.meno
			ELSE
				m.c_prev=0
				m.prevadzkaren=m.hotel_name
			ENDIF 
			IF m.operace="VV"
				LOCATE FOR c_stredisk=m.c_partner
				IF FOUND()
					m.c_prev_part=prevadzkarne.c_prev
					LOCATE FOR c_prev=m.c_prev_part AND !EMPTY(meno)
					m.menopart=prevadzkarne.meno
				ELSE
					m.c_prev_part=0
					m.menopart=m.hotel_name
				ENDIF 
			ENDIF 		
			USE IN prevadzkarne
		ENDIF 
	ELSE
		m.c_prev=0
		m.prevadzkaren=m.hotel_name
		m.c_prev_part=0
		m.menopart=m.hotel_name
	ENDIF 
	IF operace="VV" 
		IF !m.alkoprevadzky
			m.pokracovat=.f.
		ELSE 
			IF m.c_prev=m.c_prev_part
				m.pokracovat=.f.
			ENDIF 
		ENDIF 
	ENDIF 
	IF m.pokracovat
		SELECT 0
		IF !m.issql
			USE valkobrdoklad 
		ELSE
			USE m.sqlprefix+"ralkobrdoklad" ALIAS valkobrdoklad
		ENDIF 	
		=CURSORSETPROP("Buffering",5,"valkobrdoklad")
		SELECT valkobrdoklad
		DELETE ALL 
		=tux(.t.,.t.,"valkobrdoklad")
		IF m.operace="VV"
			IF m.issql
				IF "SKL"$m.gcpgm_param
					SELECT buffer.nazev, buffer.c_karty, buffer.serial_no as ean, buffer.mnozstvi as pocet, buffer.pov_objem, buffer.lieh FROM buffer WHERE buffer.lieh<>0 AND mnozstvi<>0 INTO CURSOR alko
				ELSE
					SELECT buffer.nazev, buffer.c_karty, buffer.serial_no as ean, SIGN(buffer.mnozstvi)*IIF(INT(ABS(buffer.mnozstvi)/IIF(ISNULL(buffer.pov_objem) OR buffer.pov_objem=0,1,buffer.pov_objem))=ABS(buffer.mnozstvi)/IIF(ISNULL(buffer.pov_objem) OR buffer.pov_objem=0,1,buffer.pov_objem),ABS(buffer.mnozstvi)/IIF(ISNULL(buffer.pov_objem) OR buffer.pov_objem=0,1,buffer.pov_objem),ABS(buffer.mnozstvi)/IIF(ISNULL(buffer.pov_objem) OR buffer.pov_objem=0,1,buffer.pov_objem)+1) as pocet, buffer.pov_objem, buffer.lieh FROM buffer WHERE buffer.lieh<>0 AND mnozstvi<>0 INTO CURSOR alko
				ENDIF 
			ELSE 
				IF "SKL"$m.gcpgm_param
					SELECT buffer.nazev, buffer.c_karty, buffer.serial_no as ean, buffer.mnozstvi as pocet, buffer.pov_objem, buffer.lieh FROM buffer LEFT JOIN karty ON buffer.c_karty=karty.c_karty WHERE karty.lieh<>0 AND mnozstvi<>0 INTO CURSOR alko
				ELSE
					SELECT buffer.nazev, buffer.c_karty, buffer.serial_no as ean, SIGN(buffer.mnozstvi)*IIF(INT(ABS(buffer.mnozstvi)/IIF(ISNULL(buffer.pov_objem) OR buffer.pov_objem=0,1,buffer.pov_objem))=ABS(buffer.mnozstvi)/IIF(ISNULL(buffer.pov_objem) OR buffer.pov_objem=0,1,buffer.pov_objem),ABS(buffer.mnozstvi)/IIF(ISNULL(buffer.pov_objem) OR buffer.pov_objem=0,1,buffer.pov_objem),ABS(buffer.mnozstvi)/IIF(ISNULL(buffer.pov_objem) OR buffer.pov_objem=0,1,buffer.pov_objem)+1) as pocet, buffer.pov_objem, buffer.lieh FROM buffer LEFT JOIN karty ON buffer.c_karty=karty.c_karty WHERE karty.lieh<>0 AND mnozstvi<>0 INTO CURSOR alko
				ENDIF 
			ENDIF 
			IF RECCOUNT()>0
				DO pr_report WITH "alko_doklad"
				DO pr_report WITH "alko_doklad_pv"
			ENDIF 
		ELSE 
			IF m.issql
				IF m.is_sklextp
					SELECT buffer.nazev, buffer.c_karty, buffer.serial_no as ean, buffer.mnozstvi/IIF(EMPTY(buffer.pov_objem) OR ISNULL(buffer.pov_objem),1,buffer.pov_objem) as pocet, buffer.pov_objem, buffer.lieh FROM buffer  WHERE buffer.lieh<>0 AND mnozstvi<>0 INTO CURSOR alko
				ELSE
					SELECT buffer.nazev, buffer.c_karty, buffer.serial_no as ean, buffer.mnozstvi as pocet, buffer.pov_objem, buffer.lieh FROM buffer WHERE buffer.lieh<>0 AND mnozstvi<>0 INTO CURSOR alko
				ENDIF 
			ELSE
				IF m.is_sklextp
					SELECT buffer.nazev, buffer.c_karty, buffer.serial_no as ean, buffer.mnozstvi/IIF(EMPTY(buffer.pov_objem) OR ISNULL(buffer.pov_objem),1,buffer.pov_objem) as pocet, buffer.pov_objem, buffer.lieh FROM buffer LEFT JOIN karty ON buffer.c_karty=karty.c_karty WHERE karty.lieh<>0 AND mnozstvi<>0 INTO CURSOR alko
				ELSE
					SELECT buffer.nazev, buffer.c_karty, buffer.serial_no as ean, buffer.mnozstvi as pocet, buffer.pov_objem, buffer.lieh FROM buffer LEFT JOIN karty ON buffer.c_karty=karty.c_karty WHERE karty.lieh<>0 AND mnozstvi<>0 INTO CURSOR alko
				ENDIF 
			ENDIF 
			IF RECCOUNT()>0
				DO pr_report WITH "alko_doklad"
			ENDIF 
		ENDIF 
		IF m.operace="VV"
			SELECT alko
			GO TOP 
			SCAN FOR pocet<>0
				INSERT INTO valkobrdoklad (c_dokl, c_karty, ean, pocet, c_partner, datum, operace, autor, c_stredisk, c_prev) VALUES (m.c_dokl, alko.c_karty, alko.ean, alko.pocet, m.c_partner, m.datum_dokl, "VV", m.gcuser_id, m.gic_stredisk, m.c_prev)
			ENDSCAN 
			GO TOP 
			SCAN FOR pocet<>0
				INSERT INTO valkobrdoklad (c_dokl, c_karty, ean, pocet, c_partner, datum, operace, autor, c_stredisk, c_prev) VALUES (m.c_dokl, alko.c_karty, alko.ean, -alko.pocet, m.c_stredisk, m.datum_dokl, "PV", m.gcuser_id, m.c_partner, m.c_prev_part)
			ENDSCAN 
			=tux(.t.,.t.,"valkobrdoklad")
			SELECT alko
			SCAN ALL 
				DO prep1_alk WITH alko.ean, m.c_prev
				IF m.c_prev<>m.c_prev_part
					DO prep1_alk WITH alko.ean, m.c_prev_part
				ENDIF 
			ENDSCAN 
			USE IN alko
		ELSE
			SELECT alko
			GO TOP 
			SCAN FOR pocet<>0
				INSERT INTO valkobrdoklad (c_dokl, c_karty, ean, pocet, c_partner, datum, operace, autor, c_stredisk, c_prev) VALUES (m.c_dokl, alko.c_karty, alko.ean, alko.pocet, m.c_partner, m.datum_dokl, m.operace, m.gcuser_id, m.gic_stredisk, m.c_prev)
			ENDSCAN 
			=tux(.t.,.t.,"valkobrdoklad")
			SELECT alko
			SCAN ALL 
				DO prep1_alk WITH alko.ean, m.c_prev
			ENDSCAN 
			USE IN alko
		ENDIF 		
		USE IN valkobrdoklad
		SELECT buf_dokl
	ENDIF 
ENDSCAN 	
DO pozavirej WITH "obr_alk"