*!*****************************************************************************

*!*    SELECT nazev, SUM(mnozstvi) AS m_zacat, ROUND(SUM(mnozstvi * cena), 2) AS p_zacat ;
*!*           GROUP BY 1 FROM (m.fn_obraty) INTO TABLE (m.pri_path + "zacatek") ;
*!*           WHERE INLIST(LEFT(operace, 3), "PRI", "VYD") AND (stornovano = .F.) AND datum < m.d1

SELECT nazev, operace, SUM(mnozstvi) AS mnozstvi, SUM(prachy) AS prachy, partner ;
	GROUP BY 1, 2 WHERE INLIST(LEFT(operace, 1), "P", "V") ;
	FROM pom INTO CURSOR reg0 READWRITE

REPLACE FOR LOWER(partner) = "gastro" partner WITH "Hotel"
REPLACE FOR !INLIST(LOWER(partner), "budvar", "hotel") partner WITH "XXX"

SELECT nazev, operace, SUM(mnozstvi) AS mnozstvi, SUM(prachy) AS prachy, ;
	$0 AS m_zacat, $0 AS p_zacat, ;
	$0 AS m_prije, $0 AS p_prije, ;
	$0 AS m_privl, $0 AS p_privl, ;
	$0 AS m_vydej, $0 AS p_vydej, ;
	$0 AS m_vydvr, $0 AS p_vydvr, ;
	$0 AS m_vydvl, $0 AS p_vydvl, ;
	$0 AS m_pri_i, $0 AS p_pri_i, ;
	$0 AS m_pri_b, $0 AS p_pri_b, ;
	$0 AS m_pri_o, $0 AS p_pri_o, ;
	$0 AS m_pri_p, $0 AS p_pri_p  ;
	GROUP BY 1,2 FROM reg0 INTO CURSOR reg2 READWRITE 

REPLACE FOR operace = "PE" m_prije WITH mnozstvi, p_prije WITH prachy, mnozstvi WITH 0, prachy WITH 0
REPLACE FOR operace = "PV" m_privl WITH mnozstvi, p_privl WITH prachy, mnozstvi WITH 0, prachy WITH 0
REPLACE FOR operace = "VE" m_vydej WITH mnozstvi, p_vydej WITH prachy, mnozstvi WITH 0, prachy WITH 0
REPLACE FOR operace = "VR" m_vydvr WITH mnozstvi, p_vydvr WITH prachy, mnozstvi WITH 0, prachy WITH 0
REPLACE FOR operace = "VV" m_vydvl WITH mnozstvi, p_vydvl WITH prachy, mnozstvi WITH 0, prachy WITH 0
REPLACE FOR operace = "PI" m_pri_i WITH mnozstvi, p_pri_i WITH prachy, mnozstvi WITH 0, prachy WITH 0
REPLACE FOR operace = "PB" m_pri_b WITH mnozstvi, p_pri_b WITH prachy, mnozstvi WITH 0, prachy WITH 0
REPLACE FOR operace = "VB" m_pri_b WITH mnozstvi, p_pri_b WITH prachy, mnozstvi WITH 0, prachy WITH 0
REPLACE FOR operace = "PP" m_pri_o WITH mnozstvi, p_pri_o WITH prachy, mnozstvi WITH 0, prachy WITH 0
REPLACE FOR operace = "VP" m_pri_o WITH mnozstvi, p_pri_o WITH prachy, mnozstvi WITH 0, prachy WITH 0
REPLACE FOR operace = "PC" m_pri_p WITH mnozstvi, p_pri_p WITH prachy, mnozstvi WITH 0, prachy WITH 0
REPLACE FOR operace = "VC" m_pri_p WITH mnozstvi, p_pri_p WITH prachy, mnozstvi WITH 0, prachy WITH 0

*!*    appe from (m.gcpri_path + "zacatek")

SELECT nazev, SUM(mnozstvi) AS mnozstvi, ROUND(SUM(prachy), 2) AS prachy, ;
	SUM(m_zacat) AS m_zacat, SUM(p_zacat) AS p_zacat, ;
	SUM(m_prije) AS m_prije, SUM(p_prije) AS p_prije, ;
	SUM(m_privl) AS m_privl, SUM(p_privl) AS p_privl, ;
	SUM(m_vydej) AS m_vydej, SUM(p_vydej) AS p_vydej, ;
	SUM(m_vydvl) AS m_vydvl, SUM(p_vydvl) AS p_vydvl, ;
	SUM(m_vydvr) AS m_vydvr, SUM(p_vydvr) AS p_vydvr, ;
	SUM(m_pri_b) AS m_pri_b, SUM(p_pri_b) AS p_pri_b, ;
	SUM(m_pri_o) AS m_pri_o, SUM(p_pri_o) AS p_pri_o, ;
	SUM(m_pri_p) AS m_pri_p, SUM(p_pri_p) AS p_pri_p, ;
	SUM(m_pri_i) AS m_pri_i, SUM(p_pri_i) AS p_pri_i  ;
	GROUP BY nazev FROM reg2 INTO CURSOR reg3

DO pr_report WITH "regleta"

*    *    *    *    *    *    *    *    *    *    *    *

SELECT nazev, operace, partner, SUM(mnozstvi) AS mnozstvi, SUM(prachy) AS prachy, ;
	$0 AS m_prije   , $0 AS p_prije   , ;
	$0 AS m_prije_b , $0 AS p_prije_b , ;
	$0 AS m_prije_mp, $0 AS p_prije_mp, ;
	$0 AS m_privl   , $0 AS p_privl   , ;
	$0 AS m_pri_b   , $0 AS p_pri_b   , ;
	$0 AS m_pri_o   , $0 AS p_pri_o   , ;
	$0 AS m_pri_p   , $0 AS p_pri_p     ;
	WHERE LEFT(operace, 1) = "P" GROUP BY 1,2,3 ;
	FROM reg0 INTO CURSOR reg2 READWRITE 

REPLACE FOR operace = "PE" .AND. partner = "Budvar" m_prije_b  WITH mnozstvi, p_prije_b  WITH prachy, mnozstvi WITH 0, prachy WITH 0
REPLACE FOR operace = "PE" .AND. partner = "Hotel"  m_prije_mp WITH mnozstvi, p_prije_mp WITH prachy, mnozstvi WITH 0, prachy WITH 0
REPLACE FOR operace = "PE"                          m_prije    WITH mnozstvi, p_prije    WITH prachy, mnozstvi WITH 0, prachy WITH 0
REPLACE FOR operace = "PV"                          m_privl    WITH mnozstvi, p_privl    WITH prachy, mnozstvi WITH 0, prachy WITH 0
REPLACE FOR operace = "PP"                          m_pri_o    WITH mnozstvi, p_pri_o    WITH prachy, mnozstvi WITH 0, prachy WITH 0

SELECT nazev, SUM(mnozstvi) AS mnozstvi, ROUND(SUM(prachy), 2) AS prachy, ;
	SUM(m_prije)    AS m_prije   , SUM(p_prije)    AS p_prije   , ;
	SUM(m_prije_b)  AS m_prije_b , SUM(p_prije_b)  AS p_prije_b , ;
	SUM(m_prije_mp) AS m_prije_mp, SUM(p_prije_mp) AS p_prije_mp, ;
	SUM(m_privl)    AS m_privl   , SUM(p_privl)    AS p_privl   , ;
	SUM(m_pri_b)    AS m_pri_b   , SUM(p_pri_b)    AS p_pri_b   , ;
	SUM(m_pri_o)    AS m_pri_o   , SUM(p_pri_o)    AS p_pri_o   , ;
	SUM(m_pri_p)    AS m_pri_p   , SUM(p_pri_p)    AS p_pri_p     ;
	GROUP BY nazev FROM reg2 INTO CURSOR reg3

DO pr_report WITH "regleta_p"

*    *    *    *    *    *    *    *    *    *    *    *

SELECT nazev, operace, partner, SUM(mnozstvi) AS mnozstvi, SUM(prachy) AS prachy, ;
	$0 AS m_vydej   , $0 AS p_vydej   , ;
	$0 AS m_vydej_b , $0 AS p_vydej_b , ;
	$0 AS m_vydej_mp, $0 AS p_vydej_mp, ;
	$0 AS m_vydvr   , $0 AS p_vydvr   , ;
	$0 AS m_vydvr_b , $0 AS p_vydvr_b , ;
	$0 AS m_vydvr_mp, $0 AS p_vydvr_mp, ;
	$0 AS m_vydvl   , $0 AS p_vydvl   , ;
	$0 AS m_vyd_k   , $0 AS p_vyd_k     ;
	WHERE LEFT(operace, 1) = "V" GROUP BY 1,2,3 ;
	FROM reg0 INTO CURSOR reg2 READWRITE 

REPLACE FOR operace = "VE" .AND. partner = "Budvar" m_vydej_b  WITH mnozstvi, p_vydej_b  WITH prachy, mnozstvi WITH 0, prachy WITH 0
REPLACE FOR operace = "VE" .AND. partner = "Hotel"  m_vydej_mp WITH mnozstvi, p_vydej_mp WITH prachy, mnozstvi WITH 0, prachy WITH 0
REPLACE FOR operace = "VE"                          m_vydej    WITH mnozstvi, p_vydej    WITH prachy, mnozstvi WITH 0, prachy WITH 0

REPLACE FOR operace = "VR" .AND. partner = "Budvar" m_vydvr_b  WITH mnozstvi, p_vydvr_b  WITH prachy, mnozstvi WITH 0, prachy WITH 0
REPLACE FOR operace = "VR" .AND. partner = "Hotel"  m_vydvr_mp WITH mnozstvi, p_vydvr_mp WITH prachy, mnozstvi WITH 0, prachy WITH 0
REPLACE FOR operace = "VR"                          m_vydvr    WITH mnozstvi, p_vydvr    WITH prachy, mnozstvi WITH 0, prachy WITH 0

REPLACE FOR operace = "VV"                          m_vydvl    WITH mnozstvi, p_vydvl    WITH prachy, mnozstvi WITH 0, prachy WITH 0
REPLACE FOR operace = "VK"                          m_vyd_k    WITH mnozstvi, p_vyd_k    WITH prachy, mnozstvi WITH 0, prachy WITH 0

SELECT nazev, SUM(mnozstvi) AS mnozstvi, ROUND(SUM(prachy), 2) AS prachy, ;
	SUM(m_vydej)    AS m_vydej   , SUM(p_vydej)    AS p_vydej , ;
	SUM(m_vydej_b)  AS m_vydej_b , SUM(p_vydej_b)  AS p_vydej_b , ;
	SUM(m_vydej_mp) AS m_vydej_mp, SUM(p_vydej_mp) AS p_vydej_mp, ;
	SUM(m_vydvr)    AS m_vydvr   , SUM(p_vydvr)    AS p_vydvr , ;
	SUM(m_vydvr_b)  AS m_vydvr_b , SUM(p_vydvr_b)  AS p_vydvr_b , ;
	SUM(m_vydvr_mp) AS m_vydvr_mp, SUM(p_vydvr_mp) AS p_vydvr_mp, ;
	SUM(m_vydvl)    AS m_vydvl,    SUM(p_vydvl)    AS p_vydvl, ;
	SUM(m_vyd_k)    AS m_vyd_k,    SUM(p_vyd_k)    AS p_vyd_k ;
	GROUP BY nazev FROM reg2 INTO CURSOR reg3

DO pr_report WITH "regleta_v"


DO pozavirej WITH "reg0, reg1, reg2, reg3"
