*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
*: Procedure: SK2HTML
*:
*: generuje webovou stranku s rychlym prehledem trzeb za poslednich par dni
*: paramatry: pocet dni - za kolik dni zpet se report generuje
*:            jmeno_html - jmeno generovaneho souboru. chybi-li pripona .php, bude doplnena
*:            heslo - pod nim bude stranka pristupna
*:            co generovat - 
*:     TRZ    - sestava celkovych trzeb
*:     PLAT   - sestava po platbach
*:     SLEV   - sestava slev
*:     HLAD   - sestava po hladinach
*:     PLHOT  - sestava jen za hotovost
*:     PLH_K  - trzby za platbu "Hotove" plus "K_KARTA"
*:     PIZZA  - pocty prodanych kusu v gastroskupine Pizza (grosseto)
*:     SKUP   - trzby po spartech
*:
*:     BEZDPH - sumarni report je v cenach bez dph
*:     1000   - castky se vypisuji v tisicich na 1 des.misto
*:
*:  z reportu po spartech se vyhazuji radky pod 500Kc
*:
*: 2022-09-03  pridany subtotaly po STREDSKUP
*:             php header charset
*: 2022-10-10  pro paroplavba se zobrazuje jen aktualni mesic (az do 1. dalsiho)
*: 2022-10-31  do uzaverky pridan den v tydnu
*:
*:
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  

param pocet_dni, jmeno_html, co_generovat, heslo

verze = "2022-10-31"

IF VARTYPE(pri_path) # "C"
    pri_path = ".\"
ENDIF 

cdata = 1
crlf = CHR(13)+CHR(10)
vysledek = crlf  + " ver." + m.verze
verze = PROGRAM() +  " v." + m.verze
STORE "" TO sqldatabaza, sqladresa

DO ftp_sql
issql = .T.

totalova_kasa = "   TOTALY"

nastav_defaulty_parametru()
bez_dph      = ("BEZDPH" $ m.co_generovat)
v_tisicich   = ("1000"   $ m.co_generovat)

co_generovat = ALLT(STRTR(m.co_generovat, "BEZDPH", ""))
co_generovat = ALLT(STRTR(m.co_generovat, "1000"  , ""))

SELECT c_druh, druh, spart, spart_kas FROM ps_rskupiny INTO CURSOR skupiny
INDEX on c_druh TAG c_druh 

SELECT c_karty, nazev, c_druhk, c_druh, SPACE(10) as druh, "   " AS spart FROM ps_rkarty INTO TABLE (m.pri_path + "karty")
SET RELATION TO c_druhk INTO skupiny
REPLACE FOR .not. ISNULL(skupiny.druh) druh WITH skupiny.druh, spart WITH ALLTRIM(skupiny.spart_kas) + ALLTRIM(skupiny.spart)
REPLACE FOR EMPTY(druh) druh WITH karty.druh, spart WITH skupiny.spart
INDEX on c_karty TAG c_karty

min_uct_den = DATE() - m.pocet_dni 

vysledek = m.vysledek + crlf + "min_uct_den: " + DTOS(m.min_uct_den)

lsql = "SELECT c_uzaverka, uzaverka, c_kas, id_kas, uct_den FROM food600.sk__sez WHERE c_data=?m.cdata AND uct_den > ?min_uct_den "
sql_select(lsql,"sql_sez","c_uzaverka I, uzaverka C(22), c_kas I, id_kas C(2), uct_den D")

SELECT sql_sez.*, SPACE(30) AS id_zkratka, "     " as stredskup FROM sql_sez INTO TABLE (m.pri_path + "sk_sez")

IF ATC("paroplavba", m.jmeno_html) > 0      &&&&&&& zobrazuje se jen aktualni mesic
    DELETE FOR MONTH(uct_den) # MONTH(DATE()-1)
ENDIF 

dopln_nazev_kasy()

DELETE FOR .not. ":" $ uzaverka
INDEX on c_uzaverka TAG c_uzaverka

SELECT MIN(c_uzaverka) as min_c_uzav FROM sk_sez INTO CURSOR min_c_uzav
SCATTER MEMVAR 
USE IN min_c_uzav

lsql = "SELECT             c_uzaverka,   ucet,   c_data,   druh_pl,       cena_pl,   tip,   mena,      cena_mena"
lsql = m.lsql + " FROM food600.sk__plt WHERE c_data=?m.cdata AND c_uzaverka > ?min_c_uzav "
sql_select(lsql,"sql_plt","c_uzaverka I, ucet I, c_data I, druh_pl C(22), cena_pl N(9,2), tip N(9,2), mena C(3), cena_mena N(9,2)")

SELECT c_uzaverka, ucet, druh_pl, cena_pl, mena, cena_mena, SPACE(14) as uzaverka, {} as uct_den, SPACE(30) as id_zkratka, "    " as stredskup, {} as datum_uz ;
       FROM sql_plt INTO TABLE (m.pri_path + "sk_plt") 
dopln_z_sk_sez()
dopln_nazev_platby()

REPLACE ALL uzaverka WITH LEFT(TTOC(datum_uz, 3), 10)

REPLACE ALL uzaverka WITH LEFT(uzaverka, 11) + dat_den_tydne(uzaverka)

SELECT uzaverka, id_zkratka, druh_pl, stredskup, SUM(cena_pl) AS cena_plt, datum_uz ;
       GROUP BY 1,2,3 FROM (m.pri_path + "sk_plt") INTO TABLE (m.pri_path + "plt")


lsql = "SELECT c_uzaverka, ucet, c_stredisk, c_karty, cen_hlad, k_odpoctu, prachy, prachy_puv, dan, dan_sazba, mnozstvi"
lsql = m.lsql + " FROM food600.sk__pol WHERE c_data=?m.cdata AND c_uzaverka > ?min_c_uzav "
sql_select(lsql,"sql_pol", "c_uzaverka I, ucet I, c_stredisk I, c_karty I, cen_hlad C(2), k_odpoctu C(1), prachy N(9,2), prachy_puv N(9,2), dan N(9,2), dan_sazba N(6,2), mnozstvi N(9,2)")


SELECT c_uzaverka, ucet, c_stredisk, c_karty, cen_hlad, prachy, prachy_puv, prachy-dan as dan_zakl, mnozstvi, ;
       SPACE(14) as uzaverka, {} as uct_den, SPACE(30) as id_zkratka, "    " as stredskup, {} as datum_uz, SPACE(10) as druh, "   " as spart ;
       FROM sql_pol INTO TABLE (m.pri_path + "sk_pol")
dopln_z_sk_sez()

SET RELATION TO c_karty INTO karty
REPLACE FOR .not. ISNULL(karty.druh) druh WITH karty.druh, spart WITH karty.spart
USE IN karty

REPLACE ALL uzaverka WITH LEFT(TTOC(datum_uz, 3), 10)

REPLACE ALL uzaverka WITH LEFT(uzaverka, 11) + dat_den_tydne(uzaverka)

SELECT uzaverka, id_zkratka, ucet, stredskup, SUM(prachy) AS cena_pol, SUM(dan_zakl) AS dan_zakl, $0 AS cena_plt, 1 AS pocet, datum_uz ;
       GROUP BY 1,2,3 FROM (m.pri_path + "sk_pol") INTO TABLE (m.pri_path + "pol")

APPE FROM (m.pri_path + "plt")


*   *   *   *   *   *   *   *   TRZBY CELKEM   *   *   *   *   *   *   
SELECT id_zkratka, uzaverka, datum_uz, stredskup, ;
       SUM(cena_pol) AS cena_pol, SUM(dan_zakl) AS dan_zakl, SUM(cena_plt) AS cena_plt, SUM(pocet) as pocet ;
       GROUP BY 1,2 FROM (m.pri_path + "pol") INTO TABLE (m.pri_path + "sk_chk")
IF m.bez_dph
    REPL FOR cena_pol # 0 cena_plt WITH cena_plt * dan_zakl/cena_pol
ENDIF


*       totaly po dnech
SELECT uzaverka, m.totalova_kasa as id_zkratka, ;
       SUM(cena_pol) AS cena_pol, SUM(dan_zakl) AS dan_zakl, SUM(cena_plt) AS cena_plt, SUM(pocet) as pocet ;
       GROUP BY 1,2 FROM (m.pri_path + "sk_chk") INTO TABLE (m.pri_path + "sk_total")

*       totaly po stredskup/dnech
SELECT uzaverka, "   skup. " + stredskup as id_zkratka, stredskup, ;
       SUM(cena_pol) AS cena_pol, SUM(dan_zakl) AS dan_zakl, SUM(cena_plt) AS cena_plt, SUM(pocet) as pocet ;
       GROUP BY 1,2 FROM (m.pri_path + "sk_chk") INTO TABLE (m.pri_path + "sk_total_stredskup")

SELECT sk_chk
APPEND FROM (m.pri_path + "sk_total")

SELECT disti stredskup FROM (m.pri_path + "sk_total_stredskup") INTO CURSOR pocet_stredskup
IF RECCOUNT() > 1
    SELECT sk_chk
    APPEND FROM (m.pri_path + "sk_total_stredskup")
ENDIF 


*   *   *   *   *   *   *   *   TRZBY PO PLATBACH   *   *   *   *   *   *   
SELECT id_zkratka, uzaverka, PADR(druh_pl,22) as druh_pl, SUM(cena_pl) as cena_plt, COUNT(*) as pocet, datum_uz ;
GROUP BY 1,2,3 FROM (m.pri_path + "sk_plt") INTO TABLE (m.pri_path + "platby")
DELE FOR druh_pl = "P_PREDPL"

*       pridani totalu po dnech
SELECT uzaverka, m.totalova_kasa as id_zkratka, druh_pl, ;
       SUM(cena_plt) AS cena_plt, SUM(pocet) as pocet ;
       GROUP BY 1,2,3 FROM (m.pri_path + "platby") INTO TABLE (m.pri_path + "sk_total")
SELECT platby
APPEND FROM (m.pri_path + "sk_total")


SELECT id_zkratka, uzaverka, "Hot a spol" as druh_pl, SUM(cena_plt) as cena_plt, SUM(pocet) as pocet, datum_uz ;
       WHERE INLIST(druh_pl, "Hotove  ", "K_KARTA", "N_ESHOP", "N_BEZHO6", "N_BEZHO7", "N_BEZHO4") GROUP BY 1,2 ;
       FROM (m.pri_path + "platby") INTO TABLE (m.pri_path + "platby_hot_a_karta")

SELECT id_zkratka, uzaverka, druh_pl                 , SUM(cena_plt) as cena_plt, SUM(pocet) as pocet, datum_uz ;
       WHERE INLIST(druh_pl, "Hotove  ", "xxxx") GROUP BY 1,2 ;
       FROM (m.pri_path + "platby") INTO TABLE (m.pri_path + "platby_hot")

SELECT platby


*   *   *   *   *   *   *   *   SLEVY   *   *   *   *   *   *   
*!*    SELECT id_kas, uzaverka, STRTR(LEFT(text_sleva,3), "_", "") + space(8) as text, sleva_abs ;
*!*           FROM (m.pri_path + "sk_fin") INTO TABLE (m.pri_path + "slevy_1") ;
*!*           WHERE "#" $ text_sleva AND sleva_abs > 9
*!*    SELECT id_kas + SPACE(28) as id_kas, uzaverka, text, SUM(sleva_abs) as sleva, COUNT(*) as pocet ;
*!*           GROUP BY 1,2,3 FROM (m.pri_path + "slevy_1") INTO TABLE (m.pri_path + "slevy")
*!*    repl for "#2" $ text text with "procentní"
*!*    repl for "#3" $ text text with "částkou"
*!*    DO dopln_nazev_kasy



*   *   *   *   *   *   *   *   TRZBY PO SKUPINACH   *   *   *   *   *   *   
*!*    SELECT uzaverka, u_cis, druh, SUM(prachy) as prachy, SUM(mnozstvi) as pocet ;
*!*           GROUP BY uzaverka, u_cis, druh FROM (m.pri_path + "sk_pol") INTO TABLE (pri_path + "pol_druh")
*!*    DELE FOR ATC("pizza", druh) = 0

*!*    SELECT LEFT(u_cis,2) + SPACE(28) as id_kas, uzaverka, druh, SUM(prachy) as prachy, SUM(pocet) as pocet ;
*!*    GROUP BY 1,2,3 FROM (m.pri_path + "pol_druh") INTO TABLE (m.pri_path + "skupiny")

SELECT platby
INDEX ON id_zkratka + uzaverka + str(999999999-ABS(cena_plt)) + str(3-sign(cena_plt),2) TAG kup

*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   
fh = fcreate(m.jmeno_html)
if m.fh > 0
     DO create_html_head
     IF "TRZ" $ m.co_generovat .or. EMPTY(m.co_generovat)
           vysledek = m.vysledek + m.crlf + "TRZ "
           DO create_html_trzby
     ENDIF

     IF "PLH_K" $ m.co_generovat .or. EMPTY(m.co_generovat)
           SELE platby_hot_a_karta
           INDEX ON id_zkratka + uzaverka + str(999999999-ABS(cena_plt)) + str(3-sign(cena_plt),2) TAG kup
           DO create_html_platby
     ENDIF 

     IF "PLAT" $ m.co_generovat .or. EMPTY(m.co_generovat) 
           vysledek = m.vysledek + "   PLAT "
           SELECT platby
           DO create_html_platby
     ENDIF 

     IF "PLHOT" $ m.co_generovat .or. EMPTY(m.co_generovat)
           vysledek = m.vysledek + "   PLHOT "
           SELE platby_hot
           INDEX ON id_zkratka + uzaverka + str(999999999-ABS(cena_plt)) + str(3-sign(cena_plt),2) TAG kup
           DO create_html_platby
     ENDIF 

     IF "SLEV" $ m.co_generovat .or. EMPTY(m.co_generovat)
***           DO create_html_slevy
     ENDIF 

     IF "HLAD" $ m.co_generovat .or. EMPTY(m.co_generovat) 
           SELECT cen_hlad, SUM(prachy) as prachy GROUP BY 1 FROM (m.pri_path + "sk_pol") INTO CURSOR pocet_hladin
           COUNT FOR prachy > 99 TO pocet_hladin
           IF m.pocet_hladin > 1
               vysledek = m.vysledek + "   HLAD "
               SELECT id_zkratka, uzaverka, cen_hlad, SUM(prachy) as prachy, COUNT(*) as pocet ;
                    GROUP BY 1,2,3 FROM (m.pri_path + "sk_pol") INTO TABLE (m.pri_path + "hladiny")
               DO create_html_hladiny
           ENDIF 
     ENDIF 

     IF "PIZZA" $ m.co_generovat .or. EMPTY(m.co_generovat) 
           vysledek = m.vysledek + "   PIZZA "
           DO create_html_skupiny
     ENDIF 

     IF "SKUP" $ m.co_generovat .or. EMPTY(m.co_generovat) 
         vysledek = m.vysledek + "   SKUP "
         SELECT id_zkratka, uzaverka, spart AS druh, SUM(prachy) as prachy, SUM(mnozstvi) as pocet ;
                GROUP BY 1,2,3 FROM (m.pri_path + "sk_pol") INTO TABLE (m.pri_path + "skupiny")

         DELETE FOR prachy < 555

         INDEX ON id_zkratka + uzaverka + str(999999999-ABS(prachy)) TAG xx
         DO create_html_skupiny
     ENDIF 

     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
? m.vysledek


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE create_html_trzby
SELECT sk_chk
inde on id_zkratka + uzaverka tag kas_uz

STORE "Z*Z" TO o_uzav, pozadi, o_kas
scan for (cena_pol # 0) .and. (cena_plt # 0)
  if id_zkratka # m.o_kas
    DO zapis_zacatek_tabulky WITH "a"
    fputs(m.fh, '<TD class="center" width="75px"> Pred slevou </TD>')
    fputs(m.fh, '<TD class="center" width="70px"> Po slevě </TD>')
    fputs(m.fh, '<TD class="center"> Účtů </TD></TR>')
  endif

  DO zapis_zacatek_radku_tabulky WITH "a"
  fputs(m.fh, '<TD class="right"  >' + formatuj(cena_pol)       + ' &nbsp; </TD>')
  fputs(m.fh, '<TD class="right"  >' + formatuj(cena_plt)       + ' &nbsp; </TD>')
  fputs(m.fh, '<TD class="right"  >' + trans(pocet, "999 999" ) + ' &nbsp; </TD></TR>')
endscan


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

STORE "Z*Z" TO o_uzav, pozadi, o_kas
scan for (cena_plt # 0)
  if id_zkratka # m.o_kas
    DO zapis_zacatek_tabulky WITH "b"
    fputs(m.fh, '<TD class="center" width="99px"> Platba </TD>')
    fputs(m.fh, '<TD class="center" width="70px"> 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_plt)       + ' &nbsp; </TD>')
  fputs(m.fh, '<TD class="smallright" >' + trans(pocet, "999 999" ) + ' &nbsp; </TD></TR>')
endscan


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE create_html_hladiny
SELECT hladiny
INDEX ON id_zkratka + uzaverka + cen_hlad TAG kup

STORE "Z*Z" TO o_uzav, pozadi, o_kas
scan for (prachy # 0)
  if id_zkratka # m.o_kas
    DO zapis_zacatek_tabulky WITH "d"
    fputs(m.fh, '<TD class="center" width="55px"> Hladina </TD>')
    fputs(m.fh, '<TD class="center" width="75px"> Po slevě </TD>')
    fputs(m.fh, '<TD class="center"> Účtů </TD></TR>')
  endif

  DO zapis_zacatek_radku_tabulky WITH "d"
  fputs(m.fh, '<TD class="center" >' + cen_hlad  + '</TD>')
  fputs(m.fh, '<TD class="right" >' + formatuj(prachy)         + ' &nbsp; </TD>')
  fputs(m.fh, '<TD class="right" >' + trans(pocet, "999 999" ) + ' &nbsp; </TD></TR>')
endscan


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE create_html_skupiny

STORE "Z*Z" TO o_uzav, pozadi, o_kas
scan for (prachy # 0)
  if id_zkratka # m.o_kas
    DO zapis_zacatek_tabulky WITH "b"
    fputs(m.fh, '<TD class="center" width="75px"> Skupina </TD>')
    fputs(m.fh, '<TD class="center" width="75px"> Pred slevou </TD>')
    fputs(m.fh, '<TD class="center"> Porcí </TD></TR>')
  endif

  DO zapis_zacatek_radku_tabulky WITH "b"
  fputs(m.fh, '<TD class="smallcenter" >' + druh  + '</TD>')
  fputs(m.fh, '<TD class="right" >' + formatuj(prachy) + ' &nbsp; </TD>')
  fputs(m.fh, '<TD class="right" >' + trans(pocet ,   "999 999"  ) + ' &nbsp; </TD></TR>')
endscan


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE create_html_slevy
SELECT slevy
INDEX ON id_zkratka + uzaverka + str(999999999-ABS(sleva)) + str(3-sign(sleva),2) TAG kup

STORE "Z*Z" TO o_uzav, pozadi, o_kas
scan for (sleva # 0)
  if id_zkratka # m.o_kas
    DO zapis_zacatek_tabulky WITH "c"
    fputs(m.fh, '<TD class="center" width="70px"> Typ slevy </TD>')
    fputs(m.fh, '<TD class="center" width="70px"> Sleva</TD>')
    fputs(m.fh, '<TD class="center"> Účtů </TD></TR>')
  endif

  DO zapis_zacatek_radku_tabulky WITH "c"
  fputs(m.fh, '<TD class="center" >' + text  + '</TD>')
  fputs(m.fh, '<TD class="right" >' + formatuj(sleva) + ' &nbsp; </TD>')
  fputs(m.fh, '<TD class="right" >' + 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_zkratka) + ' </TD>')
  STORE ")(*&#$%" TO o_uzav, pozadi
  o_kas = id_zkratka


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
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" + IIF(EMPTY(LEFT(id_zkratka, 2)), "i", ""))    && liche totalove radky se zobrazi inverzne

   fputs(m.fh, '<TR class="' + m.pozadi + '"><TD class="left" >' + uzaverka + '</TD>')
else
   fputs(m.fh, '<TR class="' + m.pozadi + '"><TD class="left" >' +   ""     + '</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 header("Content-type: text/html; charset=windows-1250");' )
  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><TABLE>' )

*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE create_html_tail
  fputs(m.fh, '</TABLE></CENTER>')
  fputs(m.fh, '<h5> generated: ' + SUBST(TTOC(DATETIME(),3), 3, 14) + ' &nbsp; ' + m.verze)
  fputs(m.fh, '<br> from ' + m.sqladresa + "\" + UPPER(m.sqldatabaza))
  fputs(m.fh, '<br> Trby ' + IIF(m.bez_dph, "BEZ DPH", "S DPH") + IIF(m.v_tisicich, " v tisících Kč", " v Kč") + ' </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)


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE dopln_nazev_kasy
IF "demo" $ LOWER(m.jmeno_html)
   REPL ALL id_zkratka WITH "Hospoda " + id_zkratka
ELSE

    o_sel_dnp = SELE()
    SELECT * FROM "ps_rfooddat" INTO CURSOR fdat
    INDEX on c_stredisk TAG c_stredisk
    SELE (m.o_sel_dnp)
    SET RELATION TO c_kas INTO fdat
    REPLACE FOR .NOT. EMPT(fdat.id_zkratka) id_zkratka WITH fdat.id_zkratka
    REPLACE ALL stredskup WITH fdat.stredskup
    USE IN fdat

    REPL ALL id_zkratka WITH strtr(id_zkratka, "POKLADNA", "")
    REPL ALL id_zkratka WITH strtr(id_zkratka, "KASA"    , "")
    REPL ALL id_zkratka WITH strtr(id_zkratka, "MASTER"  , "")
    REPL ALL id_zkratka WITH strtr(id_zkratka, " - "     , " ")
*    REPL for id_kas # m.totalova_kasa id_kas WITH proper(id_kas)
ENDIF 


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE dopln_nazev_platby
o_sel_dnp = SELE()

SELECT PADR(LOWER(kluc), 22) as klic, hodnota FROM ps_rtexty INTO cursor texty WHERE modul = "FOOD"
INDEX on klic TAG klic

SELE (m.o_sel_dnp)
SET RELA TO lower(LEFT(druh_pl,22)) INTO texty
REPL FOR .NOT. EMPT(texty.klic) druh_pl WITH LOWER(texty.hodnota)
USE IN texty


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
FUNCTION dopln_z_sk_sez()
SET RELATION TO c_uzaverka INTO sk_sez
REPLACE ALL uzaverka WITH sk_sez.uzaverka, uct_den WITH sk_sez.uct_den, id_zkratka WITH sk_sez.id_zkratka, datum_uz WITH sk_sez.uct_den, stredskup WITH sk_sez.stredskup
DELETE FOR EMPTY(uzaverka)
SET RELATION TO 


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


***********************************************************
* Vytvori kurzor z SQL selectu pomocou Cursoradapteru
***********************************************************
FUNCTION sql_select
PARAMETERS ip_select,ip_alias,ip_schema,ip_bez_hada, m.boladb
boladb = .f.
LOCAL l_prip, vysledok

WAIT LEFT('dotahujem ' + m.ip_alias, 254) WINDOW AT 22,22 NOWAIT 

IF NOT USED("teraz")
    USE 'ps_rteraz' ALIAS 'teraz' IN 0
ENDIF
l_prip =CURSORGETPROP("ConnectHandle","teraz")

lobj=IIF(TYPE('THISFORM')='O',THISFORM,_SCREEN)
lalmeno='aca_'+ip_alias
IF TYPE('lobj.'+lalmeno)<>'O'
    lobj.ADDPROPERTY(lalmeno)
ELSE
    lobj.&lalmeno=.NULL.
ENDIF
lobj.&lalmeno= CREATEOBJECT('cursoradapter')
lobj= lobj.&lalmeno

lobj.SELECTCMD=ip_select
lobj.ALIAS=ip_alias
lobj.DATASOURCE=l_prip
lobj.DATASOURCETYPE='ODBC'
lobj.FETCHMEMO=.T.
lobj.FETCHASNEEDED=.F.
lobj.FETCHSIZE=-1
lobj.MAXRECORDS=-1
IF TYPE('ip_schema')='C'
    lobj.USECURSORSCHEMA=.T.
    lobj.CURSORSCHEMA=ip_schema
ENDIF

vysledok = lobj.CURSORFILL()
IF m.vysledok
    IF USED('teraz')
        USE IN 'teraz'
    ENDIF
    SELECT(ip_alias)
ELSE
    AERROR(chybapole)
    MESSAGEBOX("SQL Chyba" + CHR(10) + chybapole(2),64)
    IF ".FXP" $ UPPER(SYS(16,0))
        DO FORM ca_error WITH ip_select,ip_alias,ip_schema
    ENDIF
ENDIF
WAIT CLEAR 
RETURN m.vysledok


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE nastav_defaulty_parametru
if empty(m.pocet_dni)
    pocet_dni = 5
endif

if empt(m.jmeno_html)
    jmeno_html = ".\report2.php"
endif

IF EMPTY(JUSTPATH(m.jmeno_html))
    jmeno_html = ".\" + m.jmeno_html
ENDIF 

if justext(m.jmeno_html) # "php"
    jmeno_html = JUSTPATH(m.jmeno_html) + "\" + JUSTSTEM(m.jmeno_html) + ".php"
endif

IF empty(m.co_generovat)
    co_generovat = ""
ELSE
    co_generovat = UPPER(m.co_generovat)
ENDIF

IF empty(m.heslo)
    heslo = allt(detxt("bpdphqlnepbgchdeefag"))
ELSE
    heslo = allt(detxt(m.heslo))
ENDIF 

*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
FUNCTION dat_den_tydne(ddd)

TRY 
    IF VARTYPE(ddd) = "D"
        downum = DOW(m.ddd, 2)
    ELSE
        downum = DOW(DATE(VAL(LEFT(m.ddd,4)), VAL(SUBST(m.ddd,6,2)), VAL(SUBST(m.ddd,9,2))), 2)
    ENDIF 
CATCH
    downum = 9
ENDTRY 
*                  1234567890123456789
#DEFINE DNY_TYDNE "-PoUtStCtPaSoNe--------"
RETURN SUBSTR(DNY_TYDNE, m.downum * 2, 2)
