*:*****************************************************************************
*: Procedure file: HK_AUTUC
*: Provede na zacatku mesice automat vyuctovani karet
*:*****************************************************************************
PARAMETERS sk_path, exp_path

IF EMPTY(exp_path)
  exp_path = "c:\ftp\data\"
ENDIF 
sk_path = sk_path + IIF(RIGHT(m.sk_path,1)="\", "", "\")
exp_path = exp_path + IIF(RIGHT(m.exp_path,1)="\", "", "\")	&& Pouziva se jako pri_path

CLOSE ALL
clos data
CLEAR WINDOW
set Safety Off

m.vysledek = CHR(13)+CHR(10)
IF FILE(m.sk_path + "hkredobr.dbf")
  use (m.sk_path + "hkredobr")
  SELECT DISTINCT datum FROM (m.sk_path+"hkredobr") INTO CURSOR datumy ORDER BY datum WHERE u_cis="VYUCT"
  GO BOTTOM 
  IF MONTH(DATE())-1 # VAL(SUBSTR(datumy.datum,5,2))
    * Jeste nebylo udelano automat. vyuct. ke konci predesleho mesice
    DO autvyuc
  ELSE
    *m.vysledek = m.vysledek + "[1]HK_AUTUC: Nema se jeste nulovat"
  ENDIF
ENDIF 

clos data

PROCEDURE autvyuc
* datum posledniho dne v predeslem mesici
m.dPoslVMes = TTOD(DATETIME(YEAR(DATE()),MONTH(DATE()), 1, 23, 59, 59))-1

*SELECT 0
*USE (m.sk_path+"hot_kred")

IF FILE(m.sk_path + "_tmhkobr.dbf")
  SELECT 0
  USE (m.sk_path + "_tmhkobr")
ENDIF
  *SELECT hot_kred
  *SET ORDER TO tag cis_kred
    m.pomdat = DTOS(m.dPoslVMes)
    m.pomcas = "23:59:59"
    SELECT dist datum,cas FROM (m.sk_path + "hkredobr") INTO TABLE (m.exp_path+"dat_vyuc") WHERE u_cis = "VYUCT" ORDER BY datum, cas
    SELECT * FROM (m.sk_path + "hkredobr") INTO TABLE (m.exp_path+"prihkobr")
    IF USED("_tmhkobr")
      SELECT * from (m.sk_path + "_tmhkobr") INTO TABLE (m.exp_path+"pritmobr") WHERE u_cis # "PRENOS"
      SELECT prihkobr
      APPEND FROM (m.exp_path+"pritmobr")
      SELECT dist datum,cas FROM (m.sk_path + "_tmhkobr") INTO TABLE (m.exp_path+"dat_vyuc1") WHERE u_cis = "VYUCT" ORDER BY datum, cas
      SELECT dat_vyuc
      APPEND FROM (m.exp_path+"dat_vyuc1")
    ENDIF 
    SELECT dat_vyuc
    LOCATE FOR datum+cas = m.pomdat+m.pomcas
    IF !FOUND()
      * Nebylo jeste udelano vyuct. k poslednimu dni v mesici
      select "VYUCT" as u_cis, m.pomdat as datum, m.pomcas as cas,;
       0 as zustatek, sum(castka) as castka, cis_kred;
       from (m.exp_path+"prihkobr") into table (m.exp_path+"zustatky") WHERE (datum+cas <= m.pomdat+m.pomcas);
       group by cis_kred
      use (m.exp_path+"zustatky") alias zustatky EXCLU
      DELETE FOR castka >= 0			&& nenulovat kladne zustatky!!!
      repl all castka with -castka
      *set rela to cis_kred into hot_kred
      * oproti pristupu z menu nuluje vsechny karty
      *dele for empty(hot_kred.label)	
      *set rela to
      pack
      IF USED("_tmhkobr")
        SELECT _tmhkobr
      ELSE 
        sele hkredobr
      ENDIF 
      append from (m.exp_path + "zustatky")
      m.vysledek = m.vysledek +  "[1#G] HK_AUTUC: Automaticke vynulovani zustatku na kartach OK"
    ELSE
      m.vysledek = m.vysledek + "[1#R] HK_AUTUC: Nenalezeno cic k nulovani"
    ENDIF 
