*:*****************************************************************************
*: Procedure file: HK_HODN misto HK_ODM
*: Generuje sumar bodu + upraven algoritmus obli.jidel
*: Uprava vypisu pro kontrolu 30.3.2009
*: 07.02.2018 vyhozeno vyhodnocovani oblibene polozky...
*:*****************************************************************************
param sk_path, cen_path, exp_path, p_desmbonus
PRIVATE ppar
m.ppar = PARAMETERS()
IF m.ppar < 4
  m.p_desmbonus = 0
ENDIF 
IF m.ppar < 3
  m.exp_path = "c:\ftp\data\"
ENDIF 
IF EMPTY(m.exp_path)
  m.exp_path = "c:\ftp\data\"
ENDIF 
sk_path = sk_path + IIF(RIGHT(m.sk_path,1)="\", "", "\")
cen_path = cen_path + IIF(RIGHT(m.cen_path,1)="\", "", "\")
exp_path = exp_path + IIF(RIGHT(m.exp_path,1)="\", "", "\")
if ! directory(m.exp_path)
   mkdir (m.exp_path)
ENDIF

CLOSE ALL
clos data
CLEAR WINDOW
set Safety Off

vysledek = crlf

IF FILE(m.sk_path+"hk_bony.dbf")
  SELECT 0
  USE (m.sk_path+"hot_kred")
  SET ORDER TO tag cis_kred
  SELECT 0
  USE (m.sk_path+"hk_bony")
*!*	  SELECT 0
*!*	  USE (m.cen_path+"sk_pol")
*!*	  SELECT 0
*!*	  USE (m.cen_path+"skupiny")
*!*	  SET ORDER TO  tag DRUH
*!*	  SELECT 0
*!*	  USE (m.cen_path+"karty")
*!*	  SET ORDER TO tag nazev
  m.d_date = DATE()
  m.cas = LEFT(TIME(),5)
  SELECT a.cis_kred, ' ' as typ, SUM(round_bonus(a.castka, a.bon_koef, m.p_desmbonus)) as body_suma, sum(a.cena_celk) as utrata,;
   m.d_date as d_date, m.cas as cas, SPACE(30) as obl_jid,;
   SPACE(30) as nazodmjarm, SPACE(30) as nazodmcolo;
   FROM (m.sk_path+"hk_bony") a INTO TABLE (m.exp_path+"hodnoc");
   GROUP BY a.cis_kred
  SET RELATION TO cis_kred INTO hot_kred
  REPLACE ALL typ WITH hot_kred.typ
  SET RELATION TO 
  DELETE FOR typ # "C"
  *PACK
  INDEX ON cis_kred TAG cis_kred
  *
*!*	  SELECT b.cis_kred, a.u_cis, sum(a.mnozstvi) as mnozstvi, a.nazev;
*!*	   from (m.cen_path+"sk_pol") a, (m.sk_path+"hk_bony") b ;
*!*	   INTO TABLE (m.exp_path+"polhk") WHERE a.u_cis = b.u_cis GROUP BY cis_kred, nazev
*!*	  INDEX ON cis_kred + STR(mnozstvi,8) TAG pomind DESCENDING
*!*	  SET RELATION TO nazev INTO karty
*!*	  SELECT karty
*!*	  SET RELATION TO druh_kalk INTO skupiny
*!*	  SELECT  polhk
*!*	  DELETE FOR !(ALLTRIM(skupiny.spart_kas) $ "J")
*!*	  USE IN karty
*!*	  USE IN skupiny
*!*	  SELECT cis_kred, nazev, MAX(mnozstvi) as mnozstvi FROM (m.exp_path+"polhk") INTO TABLE (m.exp_path+"polhk1") GROUP BY cis_kred 
*!*	  INDEX ON cis_kred TAG cis_kred
*!*	  SELECT hodnoc
*!*	  SET RELATION TO cis_kred INTO polhk1
*!*	  REPLACE ALL hodnoc.obl_jid WITH polhk1.nazev
*!*	  USE IN polhk1
  COPY all to (m.sk_path+"hk_hodn") with cdx type fox2x 
  GO BOTTOM 
  vysledek = vysledek + "[2#g]          Sumar bodu (HK_HODN) " + DTOC(d_date) + " " + cas + crlf

*!*	  SELECT hot_kred
*!*	  SET ORDER TO 
*!*	  GO BOTTOM 
*!*	  m.pomdate = DATE(VAL(LEFT(datum,4)), VAL(SUBSTR(datum,5,2)), VAL(RIGHT(datum,2)))
*!*	  IF ("D_STRAH" $ UPPER(m.sk_path)) AND .F.
*!*	    * Sereme na to
*!*	    DO CASE 
*!*	    CASE m.pomdate < DATE()-10
*!*	      vysledek = vysledek + "[2#R]          Posledni karta z (HOT_KRED) " + str_datum(datum) + " " + LEFT(cas,5) + crlf
*!*	    CASE m.pomdate < DATE()-5
*!*	      vysledek = vysledek + "[2#r]          Posledni karta z (HOT_KRED) " + str_datum(datum) + " " + LEFT(cas,5) + crlf
*!*	    OTHERWISE 
*!*	      vysledek = vysledek + "[2#g]          Posledni karta z (HOT_KRED) " + str_datum(datum) + " " + LEFT(cas,5) + crlf
*!*	    ENDCASE   
*!*	  ENDIF 
  CLOSE DATABASES 
ENDIF 
IF FILE(m.sk_path+"_tmhkobr.dbf")
  m.fn_hkobr = m.sk_path+"_tmhkobr.dbf"
ELSE
  m.fn_hkobr = m.sk_path+"hkredobr.dbf"
ENDIF 
USE (m.fn_hkobr) ALIAS hkobr
COPY STRUCTURE TO (m.sk_path+"hk_tmzust") WITH CDX 
m.datstr = DTOS(DATE())
m.casstr = TIME()
SELECT m.datstr AS datum, m.casstr AS cas, cis_kred, "PRENOS " AS u_cis, SUM(castka) AS castka, 000000.00 AS zustatek,;
  .T. as zapsano FROM (m.fn_hkobr) INTO TABLE (m.exp_path+"hkprenos1") GROUP BY cis_kred
SELECT 0
USE (m.sk_path+"hk_tmzust")
APPEND FROM (m.exp_path+"hkprenos1")
GO BOTTOM 
vysledek = vysledek + "[1]          Sumar kreditu (HK_TMZUST) " + str_datum(datum) + " " + LEFT(cas,5) + crlf
CLOSE DATABASES 


*!*****************************************************************************

function zamkni
private _iii
_iii = 1
do while ! flock() .and. m._iii < 33 .and. ! empt(dbf())
    wait "Cekejte, zamykam " + dbf() + str(m._iii,6) wind time m._iii
    _iii = m._iii + 1
enddo
return flock()

FUNCTION str_datum
PARAMETERS c_date
PRIVATE cs_datum
* c_date ve tvaru rrrrmmdd
 m.cs_datum = RIGHT(m.c_date,2) + "." + SUBSTR(c_date,5,2) + "." + LEFT(c_date,4)
RETURN m.cs_datum

*!*********************************************************************
*!      Function ROUND_BONUS
*!     
*!*********************************************************************
FUNCTION round_bonus
PARAMETERS p_cislo, p_koef, p_mist
PRIVATE p_pom
DO CASE 
CASE m.p_mist = 0
  m.p_pom = INT(m.p_cislo*m.p_koef)
CASE m.p_mist = 1
  m.p_pom = ROUND(m.p_cislo*m.p_koef, m.p_mist)
CASE m.p_mist = 2
  m.p_pom = ROUND(m.p_cislo*m.p_koef, m.p_mist)
OTHERWISE
  m.p_pom = INT(m.p_cislo*m.p_koef)
ENDCASE
RETURN m.p_pom
