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

IF ATC("d_nostress", m.gchom_path) > 0
        SELECT orders
        REPLACE FOR "lunch" $ course course WITH "lunch_main"
ENDIF 

SELECT disti canteen FROM orders INTO CURSOR pocet_canteen
IF RECCOUNT() < 2
        SELECT orders
        REPLACE ALL canteen WITH 0
ENDIF 

SELECT disti clientname, client, client_id, date, den_tydne, number, 0000.0 as cena, ;
       000 as  no_pre, 000 as  no_main, 000 as  no_side, 000 as  no_dinner, ;
       000 as can_pre, 000 as can_main, 000 as can_side, 000 as can_dinner, ;
       000 as ord_pre, 000 as ord_main, 000 as ord_side, 000 as ord_dinner, 000 as ord_lunch, ;
       000 as eat_pre, 000 as eat_main, 000 as eat_side, 000 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. client = m.client  .and. number = m.number no_pre    with m.number, can_pre    with m.canteen, ord_pre  with ord_pre  + m.ordered, eat_pre  WITH eat_pre  + m.eaten, ord_lunch WITH ord_lunch + m.ordered, eat_lunch WITH eat_lunch + m.eaten
      CASE m.course = "lunch_main"
            REPLACE FOR date = m.date .and. client = m.client  .and. number = m.number no_main   with m.number, can_main   with m.canteen, ord_main with ord_main + m.ordered, eat_main WITH eat_main + m.eaten, ord_lunch WITH ord_lunch + m.ordered, eat_lunch WITH eat_lunch + m.eaten
      CASE m.course = "lunch_side"
            REPLACE FOR date = m.date .and. client = m.client  .and. number = m.number no_side   with m.number, can_side   with m.canteen, ord_side with ord_side + m.ordered, eat_side WITH eat_side + m.eaten, ord_lunch WITH ord_lunch + m.ordered, eat_lunch WITH eat_lunch + m.eaten
      OTHERWISE
            REPLACE FOR date = m.date .and. client = m.client  .and. number = m.number no_dinner with m.number, can_dinner with m.canteen,                                                                      ord_dinner WITH ord_dinner + m.ordered, eat_dinner WITH eat_dinner + m.eaten
      ENDCASE 
ENDSCAN 


SELECT orders_sum
INDEX on clientname + date TAG cli_da

had.HIDE()
DO pr_report WITH "stravovani_odber"

SELECT clientname, client, client_id, " " as date, " " as den_tydne, 0000.0 as cena, ;
           00 as  no_pre, 00 as  no_main, 00 as  no_side, 00 as  no_dinner, ;
           00 as can_pre, 00 as can_main, 00 as can_side, 00 as can_dinner, ;
           SUM(ord_pre) as ord_pre, SUM(ord_main) as ord_main, SUM(ord_side) as ord_side, SUM(ord_dinner) as ord_dinner, SUM(ord_lunch) as ord_lunch, ;
           SUM(eat_pre) as eat_pre, SUM(eat_main) as eat_main, SUM(eat_side) as eat_side, SUM(eat_dinner) as eat_dinner ;
           GROUP BY clientname FROM orders_sum INTO CURSOR orders_sumsum READWRITE 

DO pr_report WITH "stravovani_odber"

had.SHOW()
PRIVATE iiis
* (id I, client I, meal I, ordered I, eaten I, created_at C(16), canteen I, cant_name C(33), course C(33), group_name C(44), number I, name C(30), c_karty I, name_long C(99), clientname C(66), client_id C(12), date C(10), den_tydne C(3), firma C(33) NULL )

SELECT client, client_id, clientname, firma, date + " " + den_tydne as date, ordered, eaten, canteen, group_name, course, number, PADR(name_long, 99) AS name, c_karty, 00 as poradi, ;
       00 as num_side, SPACE(99) AS name_side, 0000000 AS c_kar_side, ;
       00 as num_pre , SPACE(99) AS name_pre , 0000000 AS c_kar_pre   ;
       ORDER BY client_id, date, group_name, course, number ;
       FROM orders INTO TABLE (m.gcpri_path + "tady_ssebou")
REPLACE ALL name WITH STRTRAN(name, " - ", "-")
REPLACE ALL poradi WITH SIGN(VAL(RIGHT( ALLTRIM(course), 1)))

had.set_had( 33 )

FOR iiis = 1 TO RECCOUNT()
    GO (m.iiis)
    DO CASE 
    CASE ATC("_side", course) > 0
        SCATTER MEMVAR 
        LOCATE FOR date = m.date .and. client = m.client .and. group_name = m.group_name .and. ATC("_main", course) > 0
        IF FOUND()
            REPLACE c_kar_side WITH m.c_karty, name_side WITH m.name, num_side WITH m.number
            GO (m.iiis)
            DELETE
        ENDIF 
    CASE ATC("_pre", course) > 0
        SCATTER MEMVAR 
        LOCATE FOR date = m.date .and. client = m.client .and. group_name = m.group_name .and. ATC("_main", course) > 0
        IF FOUND()
            REPLACE c_kar_pre WITH m.c_karty, name_pre WITH m.name, num_pre WITH m.number
            GO (m.iiis)
            DELETE
        ENDIF 
    ENDCASE 
ENDFOR 

had.set_had( 77 )

REPLACE FOR ATC("repre", clientname) > 0 poradi WITH 0, group_name WITH "Oběd"

FOR iiis = 1 TO RECCOUNT()
    GO (m.iiis)
    SCATTER MEMVAR 
    LOCATE FOR date = m.date .and. client = m.client .and. poradi = m.poradi .and. c_karty = m.c_karty .and. c_kar_pre = m.c_kar_pre .and. c_kar_side = m.c_kar_side .and. RECNO() # m.iiis
    IF FOUND()
        REPLACE ordered WITH ordered + m.ordered, eaten WITH eaten + m.eaten
        GO (m.iiis)
        DELETE
    ENDIF 
ENDFOR 

REPLACE FOR poradi > 0 group_name WITH "s sebou"
INDEX ON LEFT(clientname, 66) + date + LEFT(group_name, 11) + LEFT(course, 11) + STR(number, 5) TAG qqq

RELEASE had
DO pr_report WITH "stravovani_odber_detail"

SET FILTER TO eaten = 0
DO pr_report WITH "stravovani_odber_detail"
