*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*
* Procedure: SK2HTML pro pivovar Kbely
* generuje webovou stranku s rychlym prehledem trzeb za poslednich par dni + dnesni rezmarkovane stoly i uzavrene ucty
*
*    2020-12-05 - vsechny hotovosti jsou poscitany
*    2024-02-05 - data bere z dbf, ktera byla do pri_path pripravena programkem sql2dbf
*
*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*
verze = "2024-02-05"

SET ENGINEBEHAVIOR 70
SET EXCL OFF

sk_path = m.pri_path
vysledek = " ver." + m.verze
pocet_dni = 13
dnesni_uzaverka = "Dnes do " + LEFT(TIME(), 5)
co_generovat = ""
jmeno_html = "kbelskypivovar.php"
heslo = "alca"

CLOSE DATABASES 
CLOSE DATABASES 

sk_otevreno = "OK"
TRY     
     sk_otevreno = m.sk_path + "SK_PLT "
     use (m.sk_path + "SK_PLT "   ) in 0 NOUPDA ALIAS sk_plt

     sk_otevreno = m.sk_path + "_TMSKPLT "
     use (m.sk_path + "_TMSKPLT " ) in 0 NOUPDA ALIAS _tmskplt

     sk_otevreno = m.sk_path + "\UCPOL "
     use (m.sk_path + "UCPOL ") in 0 NOUPDA SHARED ALIAS ucpol
CATCH 
     sk_otevreno = "CHYBA pri otvirani " + m.sk_otevreno
FINALLY
ENDTRY 

IF "CHYBA" $ m.sk_otevreno
    vysledek = m.vysledek + m.crlf + "[1B#R]  " + m.sk_otevreno + "  ... "
    WAIT m.sk_otevreno WINDOW TIMEOUT 4
    CLOSE DATABASES 
    RETURN 
ENDIF 

SELECT stul, SUM(kolik / delitel * cena) as cena_pl GROUP BY 1 WHERE stul > 0 FROM ucpol INTO TABLE (m.pri_path + "uc_pol1")
SELECT m.dnesni_uzaverka as uzaverka, "Na stolech" as druh_pl, SUM(cena_pl) as cena_pl, CNT(*) as pocet ;
       GROUP BY 1, 2 FROM uc_pol1 INTO TABLE (m.pri_path + "uc_pol")

SELECT m.dnesni_uzaverka as uzaverka, "Zaplaceno"  as druh_pl, SUM(cena_pl) as cena_pl, CNT(*) as pocet ;
       WHERE u_cis # "UZAVREN" GROUP BY 1, 2 FROM _tmskplt INTO TABLE (m.pri_path + "tm_plt")

SELECT PADR(uzaverka, 16) as uzaverka, PADR(druh_pl, 15) as druh_pl, SUM(cena_pl) as cena_pl, CNT(*) as pocet ;
       GROUP BY 1,2 FROM sk_plt INTO TABLE (m.pri_path + "platby_1") where (uct_den >= DATE()-m.pocet_dni)

APPEND FROM (m.pri_path + "tm_plt")
APPEND FROM (m.pri_path + "uc_pol")

REPLACE FOR ATC("p_pers", druh_pl)   > 0 druh_pl with "Personál"
REPLACE FOR ATC("n_repre" , druh_pl) > 0 druh_pl with "Reprefond"
* * * * tohle
REPLACE FOR ATC("hoto", druh_pl) > 0     druh_pl with "Hotově"
* * * * nebo tohle
*REPLACE FOR ATC("n_hotoss", druh_pl) > 0 druh_pl with "Hotově s sebou"
*REPLACE FOR ATC("n_hoto10", druh_pl) > 0 druh_pl with "Hotově -10%"
*REPLACE FOR ATC("n_hoto20", druh_pl) > 0 druh_pl with "Hotově -20%"
*REPLACE FOR ATC("n_hoto30", druh_pl) > 0 druh_pl with "Hotově -30%"
* * * * konec volby

SELECT uzaverka, druh_pl, SUM(cena_pl) as cena_pl, SUM(pocet) as pocet, "Kasa 01" as id_kas ;
       GROUP BY 1,2 FROM (m.pri_path + "platby_1") INTO TABLE (m.pri_path + "platby")
INDEX ON id_kas + uzaverka + str(999999999-ABS(cena_pl)) + str(3-sign(cena_pl),2) TAG kup


*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   
fh = fcreate(m.jmeno_html)
if m.fh > 0
     DO create_html_head

     SELECT platby
     DO create_html_platby

     DO create_html_tail
     vysledek = m.vysledek + " ... OK "
else
     vysledek = m.vysledek + m.crlf + "[1B#R]  " + " NEPROVEDENO, nelze vytvorit soubor " + upper(m.jmeno_html) + "  ... "
endif
CLOSE DATABASES 


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE create_html_platby

STORE "Z*Z" TO o_uzav, pozadi, o_kas
scan for (cena_pl # 0)
  if id_kas # m.o_kas
    DO zapis_zacatek_tabulky WITH "b"
    fputs(m.fh, '<TD class="center" width="80px"> Platba </TD>')
    fputs(m.fh, '<TD class="center" width="90px"> Po slevě </TD>')
    fputs(m.fh, '<TD class="center"> Účtů </TD></TR>')
  endif

  DO zapis_zacatek_radku_tabulky WITH "b"

  fputs(m.fh, '<TD class="smallcenter" >' + druh_pl  + '</TD>')
  fputs(m.fh, '<TD class="right" >' + formatuj(cena_pl)       + ' &nbsp; </TD>')
  fputs(m.fh, '<TD class="smallright" >' + trans(pocet, "999 999" ) + ' &nbsp; </TD></TR>')
endscan


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


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE zapis_zacatek_radku_tabulky (poz1)
if uzaverka # m.o_uzav
   o_uzav = uzaverka
   pozadi = IIF("1" $ m.pozadi, m.poz1 + "2", m.poz1 + "1" )

   fputs(m.fh, '<TR class="' + m.pozadi + '"><TD class="center" >' + uzaverka + '</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, '<link href="report.css" rel="stylesheet" type="text/css">' )
  fputs(m.fh, '</HEAD><BODY>' )

  fputs(m.fh, '<?php' )
  fputs(m.fh, '$show = $_POST["show"]; $pass = $_POST["pass"]; $password = "' + m.heslo + '"; ' )
  fputs(m.fh, 'if ($show != "1"): ?><br><br><br><br><center><form action="" method="post">' )
  fputs(m.fh, '<input type="hidden" value="1" name="show">' )
  fputs(m.fh, '<label>Heslo: </label><br><input type="password" name="pass" class="input">' )
  fputs(m.fh, '<br><br><input type="submit" value=" Vstoupit " /></form></center>' )
  fputs(m.fh, '<br><br><CENTER><IMG src="./logo.jpg"></CENTER>' )

  fputs(m.fh, '<?php endif; ?>' )
  fputs(m.fh, '<?php if ($show == "1"): ?>' )
  fputs(m.fh, '<?php if ($pass == $password): ?>' )

  fputs(m.fh, '<CENTER>' )
  fputs(m.fh, '<h4> KBELSKÝ PIVOVAR - TRBY </h4>')
  fputs(m.fh, '<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; &nbsp; ver. ' + m.verze + ' </h5>')
  fputs(m.fh, '<CENTER><IMG src="./logo.jpg" ></CENTER>' )

  fputs(m.fh, '<?php endif; ?>' )
  fputs(m.fh, '<?php if ($pass != $password): ?>' )
  fputs(m.fh, '<p>patné heslo.</p> <script type="text/javascript">history.back();</script>' )
  fputs(m.fh, '<?php endif; ?>' )
  fputs(m.fh, '<?php endif; ?>' )

  fputs(m.fh, '</BODY></HTML>' )
  fclose(m.fh)

*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
FUNCTION formatuj
PARAMETERS penizky
IF "1000" $ m.co_generovat
    RETURN TRANSFORM(ROUND(penizky/1000, 1), "99 999.9")
ELSE 
    RETURN TRANSFORM(ROUND(penizky, 0), "9 999 999")
ENDIF
