PARAMETERS jakasestava, zarchivu, sessionid
SET COLLATE TO (m.gccollate)
ostatni = "ZZZostatni"
DO (m.jakasestava)

****************************************
PROCEDURE s_obr_or2
PARAMETERS sessionid
SELECT selkasy
INDEX ON id_zkratka TAG id_zkratka
SELECT seldruhy
INDEX ON druh TAG druh

SELECT pom
SET RELATION TO partner INTO selkasy
REPLACE FOR .NOT. selkasy.lbl  partner WITH m.nechci, c_partner WITH 9999999 * SIGN(c_partner), c_dokl WITH 9999999 * SIGN(c_partner), doklad WITH 9999999 * SIGN(c_partner), c_karty WITH 9999999, nazev WITH m.nechci, druh WITH m.nechci, spart WITH m.nechci

SET RELATION TO druh INTO seldruhy
REPLACE FOR .NOT. seldruhy.lbl partner WITH m.nechci, c_partner WITH 9999999 * SIGN(c_partner), c_dokl WITH 9999999 * SIGN(c_partner), doklad WITH 9999999 * SIGN(c_partner), c_karty WITH 9999999, nazev WITH m.nechci, druh WITH m.nechci, spart WITH m.nechci
SET RELATION TO

SELECT c_stredisk, c_dokl, operace, c_karty, nazev, mnozstvi, MTON(cena)*en__meny(c_mena,m.girep_mena) AS cena, MTON(cena_part)*en__meny(c_mena,m.girep_mena) AS cena_part, MTON(cen_pardph)*en__meny(c_mena,m.girep_mena) AS cen_pardph, ;
	SUM(mnozstvi*MTON(cena)*en__meny(c_mena,m.girep_mena)) AS prachy, SUM(mnozstvi*MTON(cena_part)*en__meny(c_mena,m.girep_mena)) AS prachy_par, SUM(mnozstvi*MTON(cen_pardph)*en__meny(c_mena,m.girep_mena)) AS prachysdph, ;
	c_partner, partner, datum_dokl, doklad, pz1, pz2,m.girep_mena AS c_mena, ;
	druh, spart, stredisko, cena1, kod, stredskup, platba, provedl, cen_hlad ;
	WHERE partner = m.nechci OR nazev = m.nechci ;
	GROUP BY c_partner, c_dokl, druh FROM pom INTO CURSOR pom22 READWRITE 
REPLACE FOR mnozstvi # 0 cena WITH prachy/mnozstvi, cena_part WITH prachy_par/mnozstvi, cen_pardph WITH prachysdph/mnozstvi

SELECT pom
DELETE FOR partner = m.nechci .OR. nazev = m.nechci
INSERT INTO pom (c_stredisk, c_dokl, operace, c_karty, nazev, mnozstvi, cena, cena_part, cen_pardph, prachy, prachy_par, prachysdph, c_partner, partner, datum_dokl, doklad, pz1, pz2, c_mena,;
	druh, spart, stredisko, cena1, kod, stredskup, platba, provedl, cen_hlad) SELECT c_stredisk, c_dokl, operace, c_karty, nazev, mnozstvi, cena, cena_part, cen_pardph, prachy, prachy_par, prachysdph, c_partner, partner, datum_dokl, doklad, pz1, pz2, c_mena,;
	druh, spart, stredisko, cena1, kod, stredskup, platba, provedl, cen_hlad FROM pom22

USE IN pom22
radek_v_sestavach = 1
pocet_radku_v_sestavach = 99

DO WHILE radek_v_sestavach <= pocet_radku_v_sestavach
	SELECT sestavy
	pocet_radku_v_sestavach = RECCOUNT()
	GOTO (m.radek_v_sestavach)
	IF ! EMPTY(lbl)
		SCATTER FIELD cmd, dily MEMVAR
		&cmd
	ENDIF
	radek_v_sestavach = radek_v_sestavach + 1
ENDDO

*!*****************************************************************************
PROCEDURE daj_datum
PRIVATE m.hladajcstredisk, m.hladajcdoklod, m.hladajcdokldo
m.hladajcstredisk=m.gic_stredisk
STORE "NIC" TO d1_txt, d2_txt

IF EMPTY(n_checks("POHYBY_EXCL_"))
	CREATE CURSOR  operace (operace c(2), popis c(25))
	INSERT INTO operace (operace, popis) VALUE ("VE", lng_oper("VE") )
	INSERT INTO operace (operace, popis) VALUE ("SF", lng_oper("SF") )
	INSERT INTO operace (operace, popis) VALUE ("__", "-- Všechny pohyby --" )
	INDEX ON operace TAG operace
	DO FORM dajdruh WITH "","operace","operace",50,"popis",250 TO m.operace
	IF EMPTY(m.operace)
		m.operace="__"
		m.operace_txt=""
	ELSE
		operace_txt = IIF( m.operace = "__", "", ALLTRIM(operace.popis))
	ENDIF
	USE IN operace
	IF m.issql
		SELECT doklady.*,partneri.partner  FROM doklady LEFT JOIN partneri ON doklady.c_partner=partneri.c_partner WHERE (operace LIKE  m.operace) OR (operace = "KK") ;
			ORDER BY c_dokl INTO CURSOR dokl READWRITE
	ELSE
		SELECT doklady.*,partneri.partner FROM doklady LEFT JOIN partneri ON doklady.c_partner=partneri.c_partner  WHERE c_stredisk=m.gic_stredisk AND ((operace LIKE  m.operace) OR (operace = "KK")) ;
			ORDER BY c_dokl INTO CURSOR dokl READWRITE
	ENDIF
	INDEX on c_dokl TAG c_dokl
	DELETE FOR .NOT. INLIST(operace, "KK", "VE", "SF")
	DELETE FOR INLIST(platba, "S-FA ", "SF   ")

	REPLACE FOR operace = "KK" partner WITH REPLICATE("*", 31)&&, doklad WITH " |" + partner
	REPLACE FOR EMPTY(datum_dokl) datum_dokl WITH {^1888-11-11}

	GO TOP
	LOCATE FOR c_dokl >=m.d1

	WAIT CLEAR
	DO FORM dajdokl1 WITH "dokl",m.operace,"",.f. TO m.d1

	IF !EMPTY(m.d1)
		SELECT dokl
		SET ORDER TO c_dokl
		=SEEK(m.d1)	
		d1 = c_dokl
		d1_txt = operace + " " + ALLT(STR(c_dokl)) + " " + DTOC(datum_dokl)
		d1_dat_dokl = datum_dokl
		DO FORM dajdokl1 WITH "dokl",m.operace,"",.f.,m.d1 TO m.d2
		IF !EMPTY(m.d2)
			SELECT dokl
			SET ORDER TO c_dokl
			=SEEK(m.d2)	
			d2 = c_dokl
			d2_txt = operace + " " + ALLTRIM(STR(c_dokl)) + " " + DTOC(datum_dokl)
			d2_dat_dokl = datum_dokl
			m.hladajcdoklod=m.d1
			m.hladajcdokldo=m.d2
			IF m.zarchivu
				IF m.issql
					rv("zostfak_obraty_a","obra")
					SELECT m.gic_stredisk as c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part,zustatek, cena_prum, dan, serial_no,poradi, idriadok, spdan, spstupne ;
						FROM obra INTO CURSOR obratyx READWRITE 
				ELSE
					USE (m.fn_obra_a) IN 0 ALIAS obra
					SELECT c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part,zustatek, cena_prum, dan, serial_no,poradi, idriadok, spdan, spstupne ;
						FROM obra WHERE c_dokl>=m.d1 AND c_dokl<=m.d2 AND INLIST(operace, "SF", "VE") AND c_stredisk=m.gic_stredisk INTO CURSOR obratyx READWRITE 

				ENDIF
				DO pozavirej WITH "obra"
			ELSE
				IF m.issql
					rv("zostfak_obraty","obra")
					SELECT m.gic_stredisk as c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part,zustatek, cena_prum, dan, serial_no,poradi, idriadok, spdan, spstupne ;
						FROM obra  INTO CURSOR obratyx READWRITE 
				ELSE 
					USE (m.fn_obraty) IN 0 ALIAS obra
					SELECT c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part,zustatek, cena_prum, dan, serial_no,poradi, idriadok, spdan, spstupne ;
						FROM obra WHERE c_dokl>=m.d1 AND c_dokl<=m.d2 AND INLIST(operace, "SF", "VE") AND c_stredisk=m.gic_stredisk INTO CURSOR obratyx READWRITE 
				ENDIF 
				DO pozavirej WITH "obra, obra_a"
			ENDIF
			SELECT obratyx
			INDEX ON c_dokl TAG c_dokl
			m.fn_obra_s="obratyx"
			m.fn_dokl_dokl="doklady"
			m.fn_dokl_s="doklady"
			DO s_obr_xxx
		ENDIF
	ENDIF

ELSE
	= MESSAGEBOX(jaz("ted nelze, zkuste pozdeji"),0+16+256, jaz("m_s_obr vyber obdobi"), 3333)
ENDIF

*!*****************************************************************************
PROCEDURE s_obr4
PARAMETERS dily
SELECT * FROM pom INTO CURSOR  faaa ORDER BY c_dokl WHERE platba # "S-FA " READWRITE 
REPLACE ALL prachysdph WITH ROUND(prachy_par * dan, 4)
INDEX ON c_dokl TAG ind1
IF "1" $ m.dily
	txt_repo = ALLTRIM(jaz("rep_ttl_seznam faktur")) + " (detail)"
	DO pr_report WITH "s_faktur"
ENDIF 
IF "2" $ m.dily
	SELECT c_stredisk, c_dokl, nazev, operace, c_partner, partner, datum_dokl, c_mena, ;
		doklad, pz1, pz2, platba, kod, provedl, " " AS spart, ;
		-SUM(mnozstvi) AS mnozstvi, -SUM(prachy) AS prachy, -SUM(prachy_par) AS prachy_par, -SUM(prachysdph) AS prachysdph ;
		GROUP BY c_dokl, nazev ORDER BY pz2 ;
		FROM faaa INTO CURSOR fa_nazev

	txt_repo = ALLTRIM(jaz("rep_ttl_seznam faktur")) + " (po polozkach)"
	DO pr_report WITH "s_faktur"
ENDIF 
IF "3" $ m.dily
	SELECT faaa
	REPLACE ALL nazev WITH STR(100*dan-100, 19,2)

	SELECT c_stredisk, c_dokl, nazev, operace, c_partner, partner, datum_dokl, c_mena, ;
		doklad, pz1, pz2, platba, kod, provedl, " " AS spart, ;
		-SUM(mnozstvi) AS mnozstvi, -SUM(prachy) AS prachy, -SUM(prachy_par) AS prachy_par, -SUM(prachysdph) AS prachysdph ;
		GROUP BY c_dokl, nazev ORDER BY pz2 ;
		FROM faaa INTO CURSOR fa_dph
	txt_repo = ALLTRIM(jaz("rep_ttl_seznam faktur")) + " (po DPH)"
	DO pr_report WITH "s_faktur"
ENDIF
IF "4" $ m.dily
	PRIVATE m.iba_fakturu
	m.iba_fakturu=.t.
	SELECT distinct c_dokl FROM pom INTO CURSOR  faaa ORDER BY c_dokl WHERE platba # "S-FA " READWRITE 
	IF RECCOUNT()>0
		PRIVATE losession
		losession=EVALUATE([xfrx("XFRX#INIT")])
		lnretval = losession.setparams(m.gcpri_path+LOWER(SUBSTR(SYS(2015),4))+'.xff',,,,,.T.,"XFF")
		IF lnretval = 0
			SCAN ALL
				IF !EMPTY(m.sessionid) 
					DO FORM prn_obra WITH faaa.c_dokl,.f.,.f.,.f.,m.sessionid
				ELSE
					DO FORM prn_obra WITH faaa.c_dokl,.f.,.f.,.f.
				ENDIF 
			ENDSCAN 
			lofinal = losession.finalize('NOPAGEEJECT')
			DO pr_report WITH lofinal,.F.,.T.
		ENDIF 
	ENDIF 
	RELEASE m.iba_fakturu
ENDIF 

DO pozavirej WITH "pom, par_dokl, s_obr4, faaa, fa_dph"


*!*****************************************************************************
PROC s_obr_xxx
sox_had = CREATEOBJECT("had")
sox_had.SHOW()
SELECT c_stredisk, c_dokl, operace, c_karty, SPACE(30) AS nazev, mnozstvi, cena, cena_part * m.s_cen_part AS cena_part, $0 AS cen_pardph, ;
	mnozstvi * cena AS prachy, mnozstvi * cena_part * m.s_cen_part AS prachy_par, $0 AS prachysdph, m.giakt_mena AS c_mena, ;
	00000000 AS c_partner, SPACE(30) AS partner, {^1111-01-01} AS datum_dokl, 0000000000 AS doklad, SPACE(15) AS pz1, SPACE(15) AS pz2, dan, ;
	SPACE(10) AS druh, SPACE(30) AS stredisko, $0 AS cena1, "   " AS spart, SPACE(13) AS kod, "    " AS stredskup, "     " AS platba, "   " AS provedl, " " AS cen_hlad, ;
	00000000 AS c_druh, m.giakt_mena AS c_menap;
	ORDER BY c_stredisk, c_dokl FROM obratyx INTO CURSOR pomx READWRITE  

sox_had.set_had(66)
SET RELATION TO c_dokl INTO doklady
REPLACE ALL c_partner WITH doklady.c_partner, doklad WITH doklady.doklad, datum_dokl WITH doklady.datum_dokl,c_mena WITH doklady.c_mena, ;
	pz1 WITH doklady.pz1, pz2 WITH doklady.pz2, platba WITH doklady.platba, provedl WITH doklady.provedl, cen_hlad WITH doklady.cen_hlad
DELETE FOR INLIST(platba, "S-FA ", "SF   ")

SET RELATION TO c_partner INTO partneri
REPLACE FOR ! EMPT(partneri.partner) partner WITH partneri.partner

REPLACE FOR "SKUP" $ partneri.naz6 stredskup WITH partneri.naz6

SET RELATION TO c_stredisk INTO partneri
REPLACE FOR ! EMPTY(partneri.partner) stredisko WITH partneri.partner
REPLACE FOR EMPTY(partner) partner WITH "##  " + ALLT(STR(c_partner)) + "  ##"

sox_had.set_had(88)
SET RELATION TO c_karty INTO karty
IF "SKL" $ m.gcpgm_param
	REPLACE FOR EMPTY(nazev) nazev WITH karty.nazev, c_druh WITH karty.c_druhs, kod WITH karty.kod
	REPLACE FOR EMPTY(c_druh) c_druh WITH karty.c_druh
ELSE
	REPLACE FOR EMPTY(nazev) nazev WITH karty.nazev, c_druh WITH karty.c_druh, kod WITH karty.kod
	REPLACE FOR EMPTY(c_druh) c_druh WITH karty.c_druhs
ENDIF
IF m.issql
	SET RELATION OFF INTO karty 
	UPDATE pomx SET cena1=ckarty.cena, c_menap=ckarty.c_mena from ckarty WHERE pomx.c_karty=ckarty.c_karty
ELSE 
	REPLACE ALL cena1 WITH karty.&cena_teor, c_menap WITH karty.c_mena
	SET RELATION OFF INTO karty 
ENDIF 
REPLACE ALL cena1 WITH NTOM(MTON(cena1)*en__meny(c_menap,c_mena))

SET RELATION TO c_druh INTO skupiny
*REPLACE ALL spart WITH skupiny.spart, dan WITH skupiny.dan01, druh WITH skupiny.druh
REPLACE ALL spart WITH skupiny.spart,                         druh WITH skupiny.druh

UPDATE pomx SET stredskup=food_dat.stredskup from food_dat WHERE pomx.c_partner=food_dat.c_stredisk
REPLACE FOR EMPTY(druh) druh WITH m.ostatni
REPLACE FOR EMPTY(spart) spart WITH m.ostatni
REPLACE FOR EMPTY(cen_hlad) cen_hlad WITH "1"
REPLACE FOR c_partner > 0 .AND. EMPTY(stredskup) stredskup WITH "_Ext"
REPLACE FOR EMPTY(stredskup) stredskup WITH " Int"

SELECT DISTINCT c_partner, partner AS id_zkratka, .T. AS lbl, stredskup ;
	FROM pomx INTO CURSOR selkasy READWRITE 
REPLACE FOR ISNULL(id_zkratka) id_zkratka WITH "-- " + ALLTRIM(STR(c_partner)) + " --"
REPLACE FOR ISNULL(stredskup) stredskup WITH ""
INDEX ON stredskup + id_zkratka TAG ss_part
IF prazdna()
	INSERT INTO selkasy (c_partner, id_zkratka) VALUE (918273, "-- nic nevybrano --")
ENDIF

SELECT DISTINCT druh, .T. AS lbl ORDER BY druh ;
	FROM pomx INTO CURSOR seldruhy READWRITE 

SELECT pomx
pom_sel = SELECT()
RELEASE sox_had


*!*****************************************************************************
PROCEDURE s_obr_vrat
PARAMETERS dily
SELECT c_stredisk, c_dokl, operace, c_karty, nazev, SUM(-mnozstvi) as mnozstvi, cena, cena_part, cen_pardph, ;
	$0 as prachy, $0 as prachy_par, $0 as prachysdph, c_mena, ;
	c_partner, partner, datum_dokl, doklad, pz1, pz2, dan, ;
	druh, stredisko, cena1, spart, kod, stredskup, platba, provedl, cen_hlad, ;
	c_druh, c_menap, SUM(-mnozstvi) as mn1, SUM(-mnozstvi) as mn_vrat FROM pom INTO CURSOR faaa GROUP BY c_stredisk, c_dokl, c_karty READWRITE  WHERE platba # "S-FA " ORDER BY c_dokl
SELECT faaa
REPLACE ALL mn1 WITH 0, mn_vrat WITH 0
SELECT c_stredisk, c_dokl, operace, c_karty, nazev, SUM(-mnozstvi) as mnozstvi, cena, cena_part, cen_pardph ;
	FROM pom INTO CURSOR faaa1 GROUP BY c_stredisk, c_dokl, c_karty WHERE platba # "S-FA " AND cena_part<>0
UPDATE faaa SET mn1=faaa1.mnozstvi, cena_part=faaa1.cena_part, cen_pardph=faaa1.cen_pardph from faaa1 where faaa.c_stredisk=faaa1.c_stredisk AND faaa.c_dokl=faaa1.c_dokl AND faaa.c_karty=faaa1.c_karty
SELECT c_stredisk, c_dokl, operace, c_karty, nazev, SUM(-mnozstvi) as mnozstvi, cena, cena_part, cen_pardph ;
	FROM pom INTO CURSOR faaa1 GROUP BY c_stredisk, c_dokl, c_karty WHERE platba # "S-FA " AND cena_part=0
UPDATE faaa SET mn_vrat=faaa1.mnozstvi from faaa1 where faaa.c_stredisk=faaa1.c_stredisk AND faaa.c_dokl=faaa1.c_dokl AND faaa.c_karty=faaa1.c_karty
SELECT faaa
REPLACE ALL prachy WITH mn1*cena, prachy_par WITH mn1*cena_part
REPLACE ALL prachysdph WITH ROUND(prachy_par * dan, 4)
INDEX ON c_dokl TAG ind1
IF "1" $ m.dily
	txt_repo = ALLTRIM(jaz("rep_ttl_seznam faktur")) + " (detail)"
	DO pr_report WITH "s_faktur_sushi"
ENDIF 

DO pozavirej WITH "pom, faaa, faaa1"


