
*    vygeneruje jidelak v senatu jako HTML a JSON
*    protoze v klapkach neni zadna datumova informace, pta se, na ktery z nasledujicich dni se generuje
*
*            - i do jidelak.htm se pridaly dlouhe nazvy a alergeny
* 2023-02-17 - jako course_id se posila druh1
* 2023-04-17 - course_id se posila bez leading zeroes


#DEFINE CRLF CHR(13)+CHR(10)

#DEFINE CONVERSION_SETTING 9                 && pro prekodovani do UTF-8


IF EMPTY(m.gcrautexportpath)
    gcrautexportpath = m.gcpri_path
ENDIF

generuj_html()
generuj_json()


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
PROCEDURE generuj_html

jmeno_jidelaku = m.gcrautexportpath + "jidelak" + m.lckasa + ".htm"
fh = FCREATE(m.jmeno_jidelaku)
FPUTS(m.fh, '<HTML>' )
FPUTS(m.fh, '<HEAD>' )
FPUTS(m.fh, '<TITLE> Jídelák pokladny ' + man_nazev + "           " + DTOC(DATE()) + '</TITLE>' )
FPUTS(m.fh, '</HEAD>' )
FPUTS(m.fh, '<BODY bgcolor="#ffffff"  text="#000000"  link="#FF0000"  vlink="#CCCC99"  alink="#0000FF" >' )
FPUTS(m.fh, '<CENTER><TABLE>' )

FPUTS(m.fh, '<CENTER><FONT size="5" face="Comic Sans MS, Arial"><B>' + 'Jídelní lístek ' + ALLTRIM(nazev_strany) + '</B></FONT></CENTER>')

FPUTS(m.fh, '<CENTER><FONT size="3" face="Comic Sans MS, Arial"><B>' + 'Informace o obsažených alergenech obdržíte v kancelári OGS na vyžádání.' + '</B></FONT></CENTER>')
FPUTS(m.fh, '<CENTER><FONT size="3" face="Comic Sans MS, Arial"><B>' + '(v souladu s Nařízením Evropského Parlamentu c. 2000/13 platným od 13.12.2014)' + '</B></FONT></CENTER>')

SELECT cenik
GO TOP
o_druh = "*&^%$"
SCAN
    IF druh1 # m.o_druh
        o_druh = druh1
        WAIT "generuje se " + m.jmeno_jidelaku + "    " + m.o_druh WINDOW AT 22,22 NOWAIT
        FPUTS(m.fh, '</TABLE></CENTER>' )
        FPUTS(m.fh, '<BR><BR>')
        FPUTS(m.fh, '<CENTER><FONT size="4" face="Comic Sans MS, Arial"><B>' + ALLTRIM(course) + '</B></FONT></CENTER>')
        FPUTS(m.fh, '<CENTER><TABLE width="666" border="1" cellspacing="1" cellpadding="0">' )
        FPUTS(m.fh, '</TR>')
    ENDIF

    FPUTS(m.fh, '<TR>')
    FPUTS(m.fh, '<TD align=center width="22"> ' + LEFT(druh1, 2) + '</TD>')
    FPUTS(m.fh, '<TD align=left width="570">&nbsp; &nbsp; &nbsp; ' + ALLTRIM(vaha) + ' <b> ' + ALLTRIM(meno1) + ' </b> ' + STRTRAN(ALLTRIM(alerg_txt), '"', '') + '</TD>')
    FPUTS(m.fh, '<TD align=right >' + IIF(cena1=0, "", STR(cena1,8,2)) + ' &nbsp; </TD>')
    FPUTS(m.fh, '</TR>')
ENDSCAN

FPUTS(m.fh, '</BODY></HTML>' )
FCLOSE(m.fh)
WAIT CLEAR 



*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
PROCEDURE generuj_json

datum_jidelaku = datum_jidelaku()

jmeno_jidelaku = m.gcrautexportpath + "jidelak" + m.lckasa + "_" + datum_jidelaku + ".json"

SELECT cenik
prvni_radek = .T.
COUNT TO delitel
IF m.delitel < 44
     delitel = 1
ENDIF 

WAIT "Generuje se " + m.jmeno_jidelaku WINDOW TIMEOUT 2
STRTOFILE('[', m.jmeno_jidelaku)
SCAN
    jidlo = jednojidlo()
    STRTOFILE(IIF(m.prvni_radek, "", ",") + CRLF + m.jidlo, m.jmeno_jidelaku, 1)
    prvni_radek = .F.
    WAIT "Generuje se " + m.jmeno_jidelaku + REPLICATE(".", RECNO() / m.delitel + 1) WINDOW NOWAIT 
ENDSCAN
STRTOFILE( CRLF + ']', m.jmeno_jidelaku, 1)
WAIT CLEAR 


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
FUNCTION jednojidlo()

zacatek_jidla = '{"id":' + nx(c_karty) + ', "stock_card_number":' + nx(c_karty) + ', "course":' + tx(course) + ', "course_id":' + ALLTRIM(STR(VAL(druh1))) + ', "date":' + tx(m.datum_jidelaku) + ', "number":' + nx(number)
names     = '"name":'        + tx(meno1) + ', "name2":'        + tx(meno3)
descripts = '"description":' + tx(meno2) + ', "description2":' + tx(meno4)

zacatek_jidla = STRCONV(m.zacatek_jidla, CONVERSION_SETTING)
names         = STRCONV(m.names        , CONVERSION_SETTING)
descripts     = STRCONV(m.descripts    , CONVERSION_SETTING)

prices = '"price":"' + nx(cena1,2) + '", "price2":"' + nx(cena2, 2) + '"'
konec_jidla   = '"range":0, "canteen":1, "show_on_web":true, "additional_course":null, "additional_course_default_number":null}'

RETURN m.zacatek_jidla  + ', ' + names  + ', ' + m.descripts + ', ' + m.prices + ', "alergens":[' + ALLTRIM(alerg_txt) + '], "weight":' + tx(vaha) + ', ' +  m.konec_jidla



********** vraci string + uvozovky  **********
FUNCTION tx (retez)
IF ISNULL(m.retez)
    RETURN '""'
ELSE 
    RETURN '"' + STRTRAN(STRTRAN(ALLTRIM(retez), '"', "'"), ",", " ") + '"'
ENDIF 


********** vraci datum jako string YYYY-MM-DD + uvozovky  **********
FUNCTION dx (dat, delka)
IF EMPTY(m.delka)
    delka = 10
ENDIF 
RETURN '"' + LEFT(TTOC(m.dat, 3), m.delka) + '"'

*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
FUNCTION datum_jidelaku
DEFINE POPUP scena FROM 2,5 FONT "Courier" TITLE "Vyber datum jidelaku"
define bar 1 of scena prompt "\<0. Dnes      " + LEFT(TTOC(DATE() + 0, 3), 10)
define bar 2 of scena prompt "\<1. Zitra     " + LEFT(TTOC(DATE() + 1, 3), 10)
define bar 3 of scena prompt "\<2. Pozitri   " + LEFT(TTOC(DATE() + 2, 3), 10)
define bar 4 of scena prompt "\<3. Za 3 dny  " + LEFT(TTOC(DATE() + 3, 3), 10)
define bar 5 of scena prompt "\<4. Za 4 dny  " + LEFT(TTOC(DATE() + 4, 3), 10)

ON SELECT POPUP scena deactivate popup scena
ACTIVATE POPUP scena BAR 2
aaa = RIGHT(PROMPT(), 10)
IF EMPTY(m.aaa)
    aaa = LEFT(TTOC(DATE() + 1, 3), 10)
ENDIF 
RETURN m.aaa
