*m.gchom_path = "i:\d_panmtz\"
*m.gcdat_path = m.gchom_path + "data\"
*m.gcpri_path = "\tmp\"


datum1 = DATE(YEAR(DATE()), MONTH(DATE()), 25)
CREATE TABLE (m.gcpri_path + "mesice") (hranice D, hranice1 C(9))
FOR iii = 1 TO 11
     datum1 = GOMONTH(m.datum1, -1)
     INSERT INTO (m.gcpri_path + "mesice") (hranice, hranice1) ;
            VALUES (m.datum1, STR(YEAR(m.datum1),4) + " - " + TRANSFORM(month(m.datum1), "@L 99"))
ENDFOR

DEFINE POPUP datumy FROM 1,55 PROMPT FIELD hranice1 TITLE "Vyber měsíc"
ON SELECTION POPUP datumy deacti popup
ACTIVATE POPUP datumy
IF EMPTY(PROMPT())
   RELEASE POPUPS datumy
   do clos_data
   RETURN .F.
ENDIF 
SCATTER memvar

create table (m.gcpri_path + "konce") ( c_stredisk I, id_zkratka C(30), ;
             pz1_zac C(25), pz1_kon C(25), c_dokl_zac I, c_dokl_kon I, ddokl_zac D, ddokl_kon D)
INDEX on c_stredisk TAG c_stredisk 

select c_dokl, c_stredisk, pz1, datum_dokl where operace = "KK" ;
       order by c_dokl from (m.fn_doklady) into table (m.gcpri_path + "konce_mesice")
SCAN
    SCATTER MEMVAR 
    SELECT konce
    LOCATE FOR c_stredisk = m.c_stredisk
    IF .not. FOUND()
        APPEND BLANK 
        REPLACE c_stredisk WITH m.c_stredisk
    ENDIF 
    DO CASE 
    CASE m.datum_dokl < m.hranice
         REPLACE c_dokl_zac WITH m.c_dokl, pz1_zac WITH m.pz1, ddokl_zac WITH m.datum_dokl, c_dokl_kon WITH 0
    CASE EMPTY(c_dokl_kon) .and. m.c_dokl > c_dokl_zac
         REPLACE c_dokl_kon WITH m.c_dokl, pz1_kon WITH m.pz1, ddokl_kon WITH m.datum_dokl
    ENDCASE
ENDSCAN 
SELECT konce
REPLACE FOR EMPTY(c_dokl_kon) c_dokl_kon WITH 99999999, pz1_kon WITH "--- DO DNES ---"

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

SELECT d.c_stredisk, d.c_dokl, d.datum_dokl, d.pz1, fd.id_zkratka ;
       WHERE operace = "VV" ;
       FROM (m.gcdat_path + "doklady") d LEFT JOIN (m.gchom_path + "food_dat") fd ON fd.c_stredisk = d.c_partner ;
       INTO TABLE (m.gcpri_path + "dok")
SET RELATION TO c_stredisk INTO konce
DELETE FOR (c_dokl < konce.c_dokl_zac) .or. (c_dokl > konce.c_dokl_kon)

DELETE FOR c_stredisk # -72    && jen vydeje z tohoto skladu se posilaji do evidence dhim

USE IN konce
PACK 
INDEX on c_dokl TAG c_dokl

SELECT o.c_stredisk, o.c_dokl, o.c_karty, o.mnozstvi, o.cena, k.nazev, k.druh, ;
       DATE() as datum_dokl, SPACE(30) as pz1, SPACE(30) as id_zkratka ;
       WHERE operace = "VV" AND o.c_dokl IN (SELECT c_dokl FROM (m.gcpri_path + "dok")) ;
       FROM (m.gcdat_path + "obraty") o LEFT JOIN (m.gcdat_path + "karty") k ON k.c_karty = o.c_karty INTO TABLE obr
SET RELATION TO c_dokl INTO dok
REPLACE ALL datum_dokl WITH dok.datum_dokl, pz1 WITH dok.pz1, id_zkratka WITH dok.id_zkratka
DELETE FOR EMPTY(id_zkratka) .or. EMPTY(datum_dokl)
INDEX on STR(c_dokl) + STR(c_karty) TAG c_c

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

soubor = m.gcdat_path + "dhim.txt"
fh = FCREATE(m.soubor)
#DEFINE odd ", "
SCAN FOR c_karty # 0
   radek = txnum(STR(c_dokl)) + odd + txnum(STR(c_karty)) + odd + tx(nazev) + odd + txnum(STR(-mnozstvi,12,4)) + odd + txnum(STR(cena,12,4)) + odd + txnum(STR(c_stredisk)) + odd + tx(id_zkratka) + odd + tx(DTOS(datum_dokl)) + odd + tx(druh)
   ? m.radek
   FPUTS (fh, m.radek)
ENDSCAN
FCLOSE(fh)

********************************************
FUNCTION tx
param retez
retval = STRTRAN(retez, '"', "'")
RETURN '"' + ALLTRIM(retval) + '"'

********************************************
FUNCTION txnum
param retez
retval = STRTR(retez, ",", ".")
RETURN ALLTRIM(retval)
