CLOSE DATABASES 

USE (m.gcdat_path + "karty")   IN 0 ORDER c_karty
USE (m.gcdat_path + "polozky") IN 0 ORDER c_karty

USE (m.gcdat_path + "obra_a")   IN 0
USE (m.gcdat_path + "dokl_a")   IN 0
USE (m.gcdat_path + "obraty")   IN 0
USE (m.gcdat_path + "doklady")  IN 0
USE (m.gcdat_path + "sk_pol")   IN 0
USE (m.gcdat_path + "sk_pol_a") IN 0

SELECT c_dokl, datum_dokl, kdy  FROM doklady INTO CURSOR doklady_all READWRITE 
APPEND FROM (m.gcdat_path + "dokl_a")


datum1 = DATE(YEAR(GOMONTH(DATE(), -3)), MONTH(GOMONTH(DATE(), -3)), 15)
CREATE CURSOR mesice (hranice D, hranice1 C(7))
krok = 1
FOR iii = 1 TO 15
     datum1 = GOMONTH(m.datum1, -krok)
     datum1 = DATE(YEAR(m.datum1), MONTH(m.datum1), 15)
     krok = ROUND(m.krok * 1.2, 2)
     INSERT INTO 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 počátek"
ON SELECTION POPUP datumy deacti popup
ACTIVATE POPUP datumy
IF .not. EMPTY(PROMPT())
   SCATTER MEMVAR
   SELECT doklady_all
* protoze v dokladech je obcas blby datum, zprumeruju par dni okolo
   CALCULATE AVG(c_dokl), COUNT() TO hranice_zac, pocet_dokladu FOR BETWEEN(datum_dokl, m.hranice-11, m.hranice+11)
   hranice_uzav = SUBSTR(ALLTRIM(STRTRAN(m.hranice1, "-", "")), 3)
ACTIVATE SCREEN 
   ? m.hranice_zac, pocet_dokladu, m.hranice, m.hranice1, hranice_uzav
   DO hledej_data
ENDIF 
RELEASE POPUPS datumy


*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   
PROCEDURE hledej_data

SELECT disti c_karty WHERE c_dokl > m.hranice_zac    FROM obra_a   INTO TABLE (m.gcpri_path + "obra_a_all") 
SELECT disti c_karty WHERE c_dokl > m.hranice_zac    FROM obraty   INTO TABLE (m.gcpri_path + "obraty_all") 

SELECT DISTI c_karty WHERE uzaverka > m.hranice_uzav FROM sk_pol_a INTO TABLE (m.gcpri_path + "sk_pol_a_all")
SELECT DISTI c_karty WHERE uzaverka > m.hranice_uzav FROM sk_pol   INTO TABLE (m.gcpri_path + "vsechny_karty")
APPEND FROM (m.gcpri_path + "sk_pol_a_all")
APPEND FROM (m.gcpri_path + "obra_a_all")
APPEND FROM (m.gcpri_path + "obraty_all")

SELECT disti c_karty, "    " as priznak, 1 as hloubka, SPACE(30) as nazev, SPACE(30) as druh ;
       FROM (m.gcpri_path + "vsechny_karty") INTO CURSOR debil4 READWRITE 
INDEX ON c_karty TAG c_karty

FOR hloubka = 1 TO 8
    WAIT STR(hloubka) WINDOW NOWAIT 
    SELECT c_poloz AS c_karty ;
           WHERE c_karty in (SELECT c_karty FROM debil4 WHERE hloubka = m.hloubka) ;
           FROM polozky INTO CURSOR debil44
    SCAN
        SCATTER FIELDS c_karty MEMVAR 
        SELECT debil4
        LOCATE FOR c_karty = m.c_karty
        IF .not. FOUND()
             INSERT INTO debil4 (c_karty, hloubka) VALUES (m.c_karty, m.hloubka + 1)
        ENDIF 
    ENDSCAN 
ENDFOR
WAIT CLEAR 
SELECT debil4
SET RELATION TO c_karty INTO karty
REPLACE ALL nazev WITH karty.nazev, druh WITH PADR(ALLTRIM(karty.druh_kalk), 13, "-") + karty.druh
SET RELATION TO c_karty INTO polozky
REPLACE ALL priznak WITH IIF(EMPTY(polozky.c_karty), "Sur", "Kalk")
INDEX on priznak + nazev TAG pr_nazev
export_filename = PUTFILE("kam to chces ulozit?", "pouzite_kalkulace_" + ALLTRIM(mesice.hranice1), "xls")
IF .not. EMPTY(m.export_filename)
    COPY ALL TO (export_filename) type xls field nazev, priznak, druh, c_karty, hloubka
ENDIF 
