had = CREATEOBJECT("had")
had.SHOW()

SELECT disti clientname, date, 0000.0 as cena, ;
       00 as  no_pre, 00 as  no_main, 00 as  no_side, 00 as  no_dinner, ;
       00 as ord_pre, 00 as ord_main, 00 as ord_side, 00 as ord_dinner, 000 as ord_lunch, ;
       00 as eat_pre, 00 as eat_main, 00 as eat_side, 00 as eat_dinner, 000 as eat_lunch  ;
       FROM orders INTO CURSOR orders_sum READWRITE 

SELECT orders
SCAN
    SCATTER MEMVAR 
    SELECT orders_sum
    DO CASE 
    CASE m.course = "lunch_pre"
        REPLACE FOR date = m.date .and. clientname = m.clientname no_pre  with m.number, ord_pre  with m.ordered, eat_pre  with m.eaten, ord_lunch WITH MAX(ord_pre, ord_main, ord_side), eat_lunch WITH MAX(eat_pre, eat_main, eat_side)
    CASE m.course = "lunch_main"
        REPLACE FOR date = m.date .and. clientname = m.clientname no_main with m.number, ord_main with m.ordered, eat_main with m.eaten, ord_lunch WITH MAX(ord_pre, ord_main, ord_side), eat_lunch WITH MAX(eat_pre, eat_main, eat_side)
    CASE m.course = "lunch_side"
        REPLACE FOR date = m.date .and. clientname = m.clientname no_side with m.number, ord_side with m.ordered, eat_side with m.eaten, ord_lunch WITH MAX(ord_pre, ord_main, ord_side), eat_lunch WITH MAX(eat_pre, eat_main, eat_side)
    OTHERWISE
        REPLACE FOR date = m.date .and. clientname = m.clientname no_dinner with m.number, ord_dinner with m.ordered, eat_dinner with m.eaten
    ENDCASE 
ENDSCAN 

DO spocti_cenu_surovin

SELECT date, SUM(eat_dinner) as eat_dinner, SUM(eat_lunch) as eat_lunch, $0 as naklady ;
       GROUP BY date FROM orders_sum INTO CURSOR orders_sumsum READWRITE 
SET RELATION TO date INTO naklady
REPLACE ALL naklady WITH naklady.prachy
RELEASE had 
DO pr_report WITH "stravovani_naklady"

*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE spocti_cenu_surovin

SELECT c_dokl, c_stredisk, datum_dokl, pz1, pz2 WHERE operace = "VK" AND cen_hlad = "3" ;
       FROM (m.fn_doklady) INTO cursor odpocty_dokl READWRITE 
INDEX on c_dokl TAG c_dokl

SELECT c_dokl, c_stredisk, SPACE(10) as date, SUM(mnozstvi*cena) as prachy GROUP BY 1 WHERE c_dokl IN (SELECT c_dokl FROM odpocty_dokl);
       FROM (m.fn_obraty) INTO cursor odpocty_obraty READWRITE 
SET RELATION TO c_dokl INTO odpocty_dokl
REPLACE ALL date WITH LEFT(TTOC(odpocty_dokl.datum_dokl, 3), 10)
ACTIVATE SCREEN 
BROWSE 

SELECT date, -SUM(prachy) as prachy GROUP BY 1 ;
       FROM odpocty_obraty INTO CURSOR naklady READWRITE 
INDEX on date TAG date
