PRIVATE m.boliobraty, m.hladajcstredisk
m.hladajcstredisk=m.gic_stredisk
m.boliobraty=USED("obraty")
IF m.issql
	rv("zostobr_sbourvv","vydejky")
ELSE
	IF !m.boliobraty
		USE (m.fn_obraty) IN 0 ALIAS obraty
		USE (m.fn_doklady) ALIAS dokl IN 0
	ENDIF 	
	USE vzostobr_sbourvv ALIAS vydejky IN 0
	IF !m.boliobraty
		USE IN obraty
		USE IN dokl
	ENDIF
ENDIF 


SELECT obratyx.c_dokl, obratyx.c_stredisk, obratyx.operace, obratyx.c_karty, obratyx.mnozstvi, obratyx.cena, obratyx.poradi, INT(VAL(doklady.pz2)) AS c_dokl2 ; 
     WHERE obratyx.operace = "PV" AND BETWEEN(obratyx.c_dokl, m.d1, m.d2) ;
     FROM obratyx LEFT JOIN doklady ON doklady.c_dokl==obratyx.c_dokl ;
     INTO CURSOR obrx READWRITE 
INSERT INTO obrx (c_dokl, c_stredisk, operace, c_karty, mnozstvi, cena, poradi, c_dokl2) SELECT obratyx.c_dokl, obratyx.c_stredisk, obratyx.operace, obratyx.c_karty, obratyx.mnozstvi, obratyx.cena, obratyx.poradi, c_dokl AS c_dokl2 ; 
     FROM obratyx WHERE obratyx.operace = "PB" AND obratyx.mnozstvi>0 AND BETWEEN(obratyx.c_dokl, m.d1, m.d2) 

SELECT obratyx.c_dokl, obratyx.c_stredisk, obratyx.operace, obratyx.c_karty, obratyx.mnozstvi, obratyx.cena, obratyx.poradi ; 
     FROM obratyx WHERE obratyx.operace = "PB"  AND obratyx.mnozstvi<0 AND BETWEEN(obratyx.c_dokl, m.d1, m.d2) INTO CURSOR vyd_bour READWRITE
  
SELECT vyd_bour
IF USED("vydejky")
	INSERT INTO vyd_bour (c_dokl, c_stredisk, operace, c_karty, mnozstvi, cena, poradi) SELECT c_dokl, c_stredisk, operace, c_karty, mnozstvi, cena, poradi FROM vydejky
ENDIF 
INDEX ON STR(c_dokl, 12) + STR(poradi, 12) TAG dokl_por

SELECT c_dokl, c_dokl2 AS c_dokl_par, c_stredisk, c_stredisk AS c_stre_par, ;
	c_karty, c_karty AS c_kart_par, mnozstvi, mnozstvi AS mnoz_par, cena, cena AS cena_par, ; 
	operace, poradi, mnozstvi AS koef_mnoz, mnozstvi AS koef_ceny ;
	FROM obrx INTO CURSOR obr1 READWRITE 
REPLACE ALL c_stre_par WITH 0, c_kart_par WITH 0, cena_par WITH 0, mnoz_par WITH 0, koef_mnoz WITH 0, koef_ceny WITH 0

SET RELATION TO STR(c_dokl_par, 12) + STR(poradi, 12) INTO vyd_bour
REPLACE ALL c_dokl_par WITH vyd_bour.c_dokl, c_stre_par WITH vyd_bour.c_stredisk, ;
     c_kart_par WITH vyd_bour.c_karty, cena_par WITH vyd_bour.cena, mnoz_par WITH - vyd_bour.mnozstvi
SET RELATION TO 

REPLACE FOR mnoz_par#0 koef_mnoz WITH mnozstvi / mnoz_par
REPLACE FOR cena#0     koef_ceny WITH cena_par / cena
REPLACE FOR cena=0 .and. cena_par = 0 koef_ceny WITH 1

DELETE FOR c_karty = c_kart_par .and. koef_mnoz = 1

SELECT  obr1.c_dokl, obr1.c_stredisk, obr1.c_karty, obr1.c_kart_par, obr1.mnozstvi, obr1.mnoz_par, obr1.cena, obr1.cena_par, ; 
     obr1.operace, obr1.koef_mnoz, obr1.koef_ceny, space(30) as nazev, space(30) as nazev_par, doklady.datum_dokl, doklady.provedl ;
     FROM obr1 LEFT JOIN doklady ON doklady.c_dokl==obr1.c_dokl INTO CURSOR prijem READWRITE 

SET RELATION TO c_karty INTO karty
REPLACE ALL nazev WITH karty.nazev
SET RELATION TO c_kart_par INTO karty
REPLACE ALL nazev_par WITH karty.nazev
SET RELATION TO  
REPLACE FOR EMPTY(c_karty)    .or. EMPTY(nazev)     nazev     WITH "***** karta " + ALLTRIM(STR(c_karty,    12))
REPLACE FOR EMPTY(c_kart_par) .or. EMPTY(nazev_par) nazev_par WITH "***** karta " + ALLTRIM(STR(c_kart_par, 12))

INDEX ON nazev + nazev_par + STR(c_dokl, 12) TAG n1_n2_dokl


************* kontrola ruznych bourani techze karet    zacatek *****

SELECT nazev, nazev_par, SUM(mnozstvi) AS mnozstvi, CNT(nazev + nazev_par) AS pocet_bour ;
     GROUP BY 1,2 FROM prijem INTO CURSOR cntbour READWRITE 
INDEX ON (nazev + nazev_par) TAG kk

SELECT nazev, nazev_par, koef_mnoz, SUM(mnozstvi) AS mnozstvi, mnozstvi AS celkmnoz, ;
	CNT(nazev + nazev_par + STR(koef_mnoz)) AS pocet_bou1, 000000 AS pocet_bour, "  " AS bacha ;
	GROUP BY 1,2,3 FROM prijem INTO CURSOR cntbourk READWRITE 
SET RELATION TO nazev + nazev_par INTO cntbour
REPLACE ALL celkmnoz WITH cntbour.mnozstvi, pocet_bour WITH cntbour.pocet_bour

REPLACE for pocet_bour # pocet_bou1 bacha WITH "**"

IF "1"$m.dily
	DO pr_report WITH "s_bour2"
ENDIF 

************* kontrola ruznych bourani techze karet    konec   *****


IF "2"$m.dily
	SELECT DISTINCT nazev, bacha+bacha+bacha as bacha FROM cntbourk INTO CURSOR nazvy
	DO WHILE .t.
		DO FORM dajdruh WITH "", "nazvy", "nazev", 250, "bacha", 100 TO m.nazev
		IF !EMPTY(m.nazev)
			SELECT prijem
			SET FILTER TO nazev = m.nazev
			view_mssg = LEFT(m.nazev, 10)
			DO pr_report WITH "s_bour1"
			SELECT nazvy
		ELSE 
			EXIT
		ENDIF 
	ENDDO
ENDIF 

IF "3"$m.dily
	nazev = jaz("form vsechny karty")
	SELECT prijem
	SET FILTER TO
	DO pr_report WITH "s_bour1"
ENDIF 

DO pozavirej WITH "vydejky, vyd_bour, obr1, prijem, cntbour, cntbourk, nazvy, obrx"
