*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
*
* program generuje webovou stranku z uc_pol, cili otevrene stoly
* z poslednich uzaverek v sk_sez najde jmeno kasy a generovaneho souboru
* z tmskfin bere i prachy za uavrene ucty
*
*  2023-02-13 - 
*
*    *    *    *    *    *    *    *    *    *    *    *      #> ver. 2023-02-14 <#       *    *    *    *    *    *    *    *    *    *    *    *    *
#DEFINE PGM_VERZE                                               "ver. 2023-02-14"


#DEFINE CRLF         CHR(13)+CHR(10)
jmenologu = PROGRAM()
small_log(PGM_VERZE, m.jmenologu)

DO open_database
jmeno_html = najdi_jmeno_kasy()

SET REPROCESS TO AUTOMATIC
DO clos_data
o_set_point = SET("POINT")
SET POINT TO "."
ACTIVATE SCREEN 

vezmi_ucpol()

generuj_html()

SET POINT TO (m.o_set_point)
small_log("hotovo", m.jmenologu)


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE vezmi_ucpol

small_log(PROGRAM(), m.jmenologu)

DO open_database

lsql =             "SELECT id_kas, cena_slev, 1 as pocet FROM food600._tmskfin "
sql_select(lsql, "tmskfin", "id_kas C(2), cena N(9,1), pocet N(4)")
SELECT id_kas as kas, "xx" as stul, "uzavřené účty celkem" AS nazev, SUM(pocet) as pocet, SUM(cena) AS cena GROUP BY kas, stul, nazev FROM tmskfin INTO CURSOR tmskfin_sum READWRITE 

lsql =             "SELECT kolik,        delitel     , nazev      , pc         , u_cis     , stul       , cidmaster FROM food600.uc_pol "
sql_select(lsql, "ucpol", "kolik N(9,2), delitel N(2), nazev C(30), cena N(9,1), u_cis C(7), stul N(6,1), kas N(5)")
DELETE FOR .not. EMPTY(u_cis)
REPLACE FOR EMPTY(delitel) delitel WITH 1

SELECT STR(ABS(kas), 4) as kas, STR(stul, 6,1) as stul, nazev, SUM(kolik/delitel) as pocet, cena GROUP BY kas, stul, nazev FROM ucpol INTO CURSOR ucpol_sum READWRITE 
APPEND FROM DBF("tmskfin_sum")
REPLACE ALL stul WITH STRTRAN(stul, ".0", "  ")


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

small_log(PROGRAM() + " generuju " + "pp_" + m.jmeno_html + ".htm", m.jmenologu)

fh = fcreate("pp_" + m.jmeno_html + ".htm")
IF m.fh > 0
     DO create_html_head

     DO create_html_stoly

     DO create_html_tail
ELSE
     small_log(" NEPROVEDENO, nelze vytvorit soubor PP_" + upper(m.jmeno_html), m.jmenologu)
ENDIF 
CLOSE DATABASES 


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE create_html_stoly

STORE "Z*Z" TO o_stul, o_kas, pozadi

SELECT ucpol_sum
INDEX on kas + stul + nazev TAG k_st_naz
SCAN 
  if kas # m.o_kas
    DO zapis_zacatek_tabulky WITH "d"
    fputs(m.fh, '<TD class="center" width="166px"> ' + UPPER(m.jmeno_html) + ' </TD>')
    fputs(m.fh, '<TD class="center" width="77px"> Cena </TD>')
    fputs(m.fh, '<TD class="center"> Počet </TD></TR>')
  endif

  DO zapis_zacatek_radku_tabulky WITH "d"
  fputs(m.fh, '<TD class="smallleft" > ' + nazev + '</TD>')
  fputs(m.fh, '<TD class="right" >' + trans(cena, "99 999.9" ) + ' &nbsp; </TD>'     )
  fputs(m.fh, '<TD class="right" >' + trans(pocet, "9999.9" )  + ' &nbsp; </TD></TR>')
ENDSCAN 


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE zapis_zacatek_tabulky ( poz1 )
  fputs(m.fh, '</TABLE></CENTER><CENTER><IMG src="./pozadi.jpg"  border="0" height="6" ></CENTER>')
  fputs(m.fh, '<CENTER><TABLE class="a" cellspacing="1" cellpadding="0"> <TR class="' + m.poz1 + 'h">' )
  fputs(m.fh, '<TD class="center" width="50px"> ' + 'Stůl' + ' </TD>')
  STORE "*&#$%" TO o_kas, pozadi
  o_kas = kas


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE zapis_zacatek_radku_tabulky (poz1)

if stul # m.o_stul
   o_stul = stul
   pozadi = IIF("1" $ m.pozadi, m.poz1 + "2", m.poz1 + "1")     && liche radky se zobrazi inverzne

   fputs(m.fh, '<TR class="' + m.pozadi + '"><TD class="center" >' + stul + '</TD>')
else
   fputs(m.fh, '<TR class="' + m.pozadi + '"><TD class="center" >' +  " "  + '</TD>')
endif


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE create_html_head

*  fputs(m.fh, '<!doctype html>' )
  fputs(m.fh, '<HTML><HEAD>' )
  fputs(m.fh, '<TITLE> Report trzeb ' + dtoc(date()) + '</TITLE>' )
  fputs(m.fh, '<meta http-equiv="content-type" content="text/html; charset=windows-1250" >' )
  fputs(m.fh, '<meta http-equiv="content-language" content="cs" >' )
  fputs(m.fh, '<meta http-equiv="refresh" content="33">' )
  fputs(m.fh, '<link href="report.css" rel="stylesheet" type="text/css">' )
  fputs(m.fh, '</HEAD><BODY>' )
  fputs(m.fh, '<CENTER><TABLE>' )


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE create_html_tail
  fputs(m.fh, '</TABLE></CENTER>')
  fputs(m.fh, '<h5> generated: ' + LEFT(TTOC(DATETIME(),3),16) +  ' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ' + PGM_VERZE)
  fputs(m.fh, '<br> from ' + GETENV("computername") + " &nbsp; " + m.sqladresa + "\" + UPPER(m.sqldatabaza) + ' </h5>')
  fputs(m.fh, '<CENTER><IMG src="./logo.jpg" ></CENTER>' )
  fputs(m.fh, '</BODY></HTML>' )
  fclose(m.fh)


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
FUNCTION najdi_jmeno_kasy
uz_max = SUBSTR(TTOC(DATE()-33, 1), 3)    &&& 230209 17:20:00
lsql =           "SELECT id_kas, uzaverka FROM food600.sk_sez WHERE uzaverka > ?uz_max "
sql_select(lsql, "fdd", "id_kas C(2), uzaverka C(17)")

SELECT id_kas, COUNT(*) as pocet FROM fdd INTO CURSOR pocetkasd
INDEX on pocet TAG pocet
GO BOTTOM 

DO CASE 
CASE id_kas = "A1"
    jmeno_html = "agnes"
CASE id_kas = "B1"
    jmeno_html = "bella"
CASE id_kas = "C1"
    jmeno_html = "rhapsody"
CASE id_kas = "D1"
    jmeno_html = "grand"
CASE id_kas = "E1"
    jmeno_html = "luznice"
CASE id_kas = "F1"
    jmeno_html = "marie"
CASE id_kas = "G1"
    jmeno_html = "pavla"
CASE id_kas = "H1"
    jmeno_html = "trieste"
CASE id_kas = "I1"
    jmeno_html = "valencie"
CASE id_kas = "J1"
    jmeno_html = "vysehrad"
CASE id_kas = "K1"
    jmeno_html = "vltava"
CASE id_kas = "L1"
    jmeno_html = "labe"
CASE id_kas = "M1"
    jmeno_html = "konigstein"
CASE id_kas = "N1"
    jmeno_html = "ponton"
CASE id_kas = "O1"
    jmeno_html = "danubio"
OTHERWISE 
    jmeno_html = "kasa_" + id_kas
ENDCASE 
small_log(m.jmeno_html + " " + id_kas, m.jmenologu)
RETURN m.jmeno_html 
