

IF m.issql
    DO open_database
    otevira_se = "karty"
    rv("karty","karty")
    IF EMPTY(m.fn_karty)
        fn_karty = "karty"
    ENDIF 
ENDIF 


SELECT id, date, course, karty.c_karty, karty.nazev, karty.cena_prum FROM meals LEFT JOIN (m.fn_karty) ON karty.c_karty == meals.c_karty INTO CURSOR jidla READWRITE 
SELECT allt(clientname) + "  /" + client_id as clientname, orders.date, eaten, meal, orders.updated_at, jidla.nazev, ROUND(jidla.cena_prum,1) as cena_prum, jidla.course, $0 as cena_celk, 00 as pocetjidel, 00 as pocet_main ;
       WHERE eaten # 0 ORDER BY 1,2 FROM orders LEFT JOIN jidla ON jidla.id == orders.meal INTO TABLE (m.gcpri_path + "snedeno")
DELETE FOR cena_prum < .4

SELECT clientname, date, CNT(*) as pocetjidel, SUM(cena_prum) as cena_celk GROUP BY 1,2 FROM snedeno INTO TABLE (m.gcpri_path + "snedeno_sum") 
INDEX on clientname + date TAG clie_dat

SELECT clientname, date, CNT(*) as pocet_main WHERE ATC("main", course) > 0 AND cena_prum > 11.1 GROUP BY 1,2 FROM snedeno INTO TABLE (m.gcpri_path + "snedeno_main")
INDEX on clientname + date TAG clie_dat

SELECT snedeno
SET RELATION TO clientname + date INTO snedeno_sum
REPLACE ALL cena_celk WITH snedeno_sum.cena_celk, pocetjidel WITH snedeno_sum.pocetjidel

SET RELATION TO clientname + date INTO snedeno_main
REPLACE ALL pocet_main WITH snedeno_main.pocet_main

SET FILTER TO cena_celk > 55 &&& .or. pocetjidel > 3 .or. pocet_main > 1
DO pr_report WITH "stravovani_podezrele"
