*:*****************************************************************************
*: Procedure file: HK_IN
*: Uprava pro prenost dotaci (EGAP) 7.1.2008
*: Uprava pro kontrolu prenosu (30.3.2009)
*: Uprava indikace zapsano vzdy .T. (8/2011)
*: Uprava indikace zapsano, nechat nezmeneno, doposilani na web delat extra ulohou (6.11.2011)
*: Uprava s hledanim NOOPT kvuli chybnemu fungovani na DUU po migraci (2.5.2017)
*: Aktualizace _tmhkobr, jen kdyz parametr tmakt=.T. Nepouzivat na Centrale, jen na stanicich
*: Parametr codelat: nic = vse = PBKDV, P = dochazka(prezence), B = body, K = kredity, D = dotace, V = vybery odmen
*:*****************************************************************************
PARAMETERS sk_path, pripona, exp_path, tmakt, coDelat

IF PARAMETERS() < 5
  m.coDelat = "PBKDV"
ENDIF 
IF PARAMETERS() < 4
  m.tmakt = .F.
ENDIF 
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)="\", "", "\")
if ! directory(m.exp_path)
   mkdir (m.exp_path)
endif

CLOSE ALL
clos data
CLEAR WINDOW
set Safety Off
SET CPDIALOG OFF

vysledek = crlf
STORE "" TO seznam_appendovanych, seznam_neexistujicich

IF "P" $ UPPER(m.codelat)
  if file(m.exp_path + "hk_doch" + m.pripona + ".dbf")
    use (m.sk_path + "hk_doch")
    SET ORDER TO tag u_dat
    sele 0
    use (m.exp_path + "hk_doch" + m.pripona) Alias hk_doch_x Exclusive
    IF FSIZE('label') = 0
        ALTER TABLE (m.exp_path + "hk_doch" + m.pripona) ADD COLUMN label C(1)
    ENDIF     
    inde on DTOS(d_date) + cas + cis_kred + operace tag u_dat
    SELECT * from (m.exp_path + "hk_doch" + m.pripona) a INTO TABLE (m.exp_path+"pribylo");
     WHERE .NOT. exists (select * from (m.sk_path+"hk_doch") b;
     WHERE (a.d_date=b.d_date) AND (a.cas=b.cas) AND (a.cis_kred=b.cis_kred) AND (a.operace=b.operace))  
    sele hk_doch 
    appe from (m.exp_path + "pribylo")
    USE IN pribylo
    seznam_appendovanych = seznam_appendovanych + " " + "HK_DOCH" + m.pripona
  else
    seznam_neexistujicich = seznam_neexistujicich + " " + m.exp_path + "HK_DOCH" + m.pripona
  endif
  clos data
ENDIF 

IF "B" $ UPPER(m.codelat)
  if file(m.exp_path + "hk_bony" + m.pripona + ".dbf")
    use (m.sk_path + "hk_bony")
    SET ORDER TO TAG u_dat
    sele 0
    use (m.exp_path + "hk_bony" + m.pripona) Alias hk_bony_x Exclusive
    inde on datum + cis_kred + u_cis tag u_dat
    SELECT * from (m.exp_path + "hk_bony" + m.pripona) a INTO TABLE (m.exp_path+"pribylo");
     WHERE .NOT. exists (select * from (m.sk_path+"hk_bony") b WHERE (a.datum=b.datum) AND (a.cis_kred=b.cis_kred) AND (a.u_cis=b.u_cis))  
    DELETE FOR (u_cis = "ARCHIV")
    PACK 
    sele hk_bony 
    appe from (m.exp_path + "pribylo")
    seznam_appendovanych = seznam_appendovanych + " " + "HK_BONY" + m.pripona
  else
    seznam_neexistujicich = seznam_neexistujicich + " " + m.exp_path + "HK_BONY" + m.pripona
  endif
  clos data
ENDIF 

IF "K" $ UPPER(m.codelat)
  *SET ESCAPE  ON
  if file(m.exp_path + "hk_obrt" + m.pripona + ".dbf") AND !m.tmakt
	* Pokud se dela aktualizace zaznamu PRENOS v tmhkobr, neni treba prenaset komplet pohyby
    USE (m.sk_path + "hkredobr")
    IF FILE(m.sk_path + "_tmhkobr.dbf")
      SELECT 0
      use (m.sk_path + "_tmhkobr")
    ENDIF   
    sele 0
    use (m.exp_path + "hk_obrt" + m.pripona) Alias hk_obr_x Exclusive
    IF FSIZE('label') = 0
      ALTER TABLE (m.exp_path + "hk_obrt" + m.pripona) ADD COLUMN label C(1)
      repl all label with "X"
    ELSE 
      repl all label with .T.
    ENDIF 
    *inde on datum + cas + cis_kred + u_cis tag u_dat
    m.pzaznamu = RECCOUNT()
    SCAN
      WAIT STR(RECNO(),12)+" / " + STR(m.pzaznamu,12) WINDOW NOWAIT 
      sele hkredobr
      locate for datum + cas + cis_kred + u_cis = hk_obr_x.datum + hk_obr_x.cas + hk_obr_x.cis_kred + hk_obr_x.u_cis NOOPTIMIZE 
      if found()
        sele hk_obr_x
        IF TYPE('label') = 'C'
          repl label with ''
        ELSE
          REPLACE label WITH .F.
        ENDIF 
      else
        sele hk_obr_x
      endif
    ENDSCAN
    IF USED('_tmhkobr')
      SELECT hk_obr_x
      SCAN FOR !EMPTY(label)
        SELECT _tmhkobr
        locate for datum + cas + cis_kred + u_cis = hk_obr_x.datum + hk_obr_x.cas + hk_obr_x.cis_kred + hk_obr_x.u_cis NOOPTIMIZE 
        if found()
          sele hk_obr_x
          IF TYPE('label') = 'C'
            repl label with ''
          ELSE
            REPLACE label WITH .F.
          ENDIF 
        else
          sele hk_obr_x
        endif
      ENDSCAN 
    ENDIF 
    SELECT hk_obr_x
    dele for empt(label)
    PACK 
    IF USED('_tmhkobr')
      sele _tmhkobr 
    ELSE
      SELECT hkredobr
    ENDIF 
    appe from (m.exp_path + "hk_obrt" + m.pripona)
    seznam_appendovanych = seznam_appendovanych + " " + "HK_OBRT" + m.pripona
  else
    IF !m.tmakt
      seznam_neexistujicich = seznam_neexistujicich + " " + m.exp_path + "HK_OBR" + m.pripona
    ENDIF 
  endif
  clos data
ENDIF

IF "D" $ UPPER(m.codelat)
  if file(m.exp_path + "hk_dotc" + m.pripona + ".dbf")
    USE (m.sk_path + "hk_dotace")
    sele 0
    use (m.exp_path + "hk_dotc" + m.pripona) Alias hk_dotc_x Exclusive
    IF FSIZE('label') = 0
      ALTER TABLE (m.exp_path + "hk_dotc" + m.pripona) ADD COLUMN label C(1)
      repl all label with "X"
    ENDIF     
    INDEX ON datum + cas + cis_kred + u_cis TAG u_dat
    SCAN 
      SELECT hk_dotace
      locate for datum + cas + cis_kred + u_cis = hk_dotc_x.datum + hk_dotc_x.cas + hk_dotc_x.cis_kred + hk_dotc_x.u_cis NOOPTIMIZE 
      if found()
        sele hk_dotc_x
        IF TYPE('label') = 'C'
          repl label with ''
        ELSE
          REPLACE label WITH .F.
        ENDIF 
      else
        sele hk_dotc_x
      endif
    ENDSCAN
    SELECT hk_dotc_x
    DELETE FOR EMPTY(label)
    PACK 
    SELECT hk_dotace
    APPEND FROM (m.exp_path + "hk_dotc" + m.pripona)
  ENDIF
  CLOSE DATABASES 
ENDIF

IF "V" $ UPPER(m.codelat)
  if file(m.exp_path + "hk_odmv" + m.pripona + ".dbf") AND FILE(m.sk_path + "hk_odmvyb" + ".dbf")
    use (m.sk_path + "hk_odmvyb")
    * asi to neni treba
    *SET ORDER TO TAG ucdat
    sele 0
    use (m.exp_path + "hk_odmv" + m.pripona) Alias hk_odmvyb_x Exclusive
    inde on datum + cis_kred + u_cis tag u_dat
    SELECT * from (m.exp_path + "hk_odmv" + m.pripona) a INTO TABLE (m.exp_path+"pribylo");
     WHERE .NOT. exists (select * from (m.sk_path+"hk_odmvyb") b WHERE (a.datum=b.datum) AND (a.cis_kred=b.cis_kred) AND (a.u_cis=b.u_cis))  
    DELETE FOR (u_cis = "ARCHIV")
    PACK 
    sele hk_odmvyb 
    appe from (m.exp_path + "pribylo")
    seznam_appendovanych = seznam_appendovanych + " " + "HK_ODMVYB" + m.pripona
  else
    seznam_neexistujicich = seznam_neexistujicich + " " + m.exp_path + "HK_ODMV" + m.pripona
  endif
  CLOSE DATABASES 
ENDIF 

IF .not. EMPTY(m.seznam_appendovanych)
    vysledek = vysledek + "[1#G]       appendovano: " + UPPER(m.seznam_appendovanych) + crlf
ENDIF 
IF .not. EMPTY(m.seznam_neexistujicich)
    vysledek = vysledek + "[1B#B]       neexistuje soubor: " + UPPER(m.seznam_neexistujicich) + crlf
ENDIF 


IF FILE(m.sk_path+"hk_tmzust.dbf") AND m.tmakt
  SELECT 0
  USE (m.sk_path+"hk_tmzust.dbf") ORDER cis_kred
  IF !FILE(m.sk_path+"_tmhkobr.dbf")
    COPY TO (m.sk_path+"_tmhkobr.dbf") WITH CDX
  ELSE
    SELECT 0
    USE (m.sk_path+"_tmhkobr.dbf")
    LOCATE FOR _tmhkobr.u_cis = "PRENOS" NOOPTIMIZE 
    IF FOUND()
      *IF hk_tmzust.datum + hk_tmzust.cas > _tmhkobr.datum + _tmhkobr.cas
        DELETE FOR _tmhkobr.u_cis = "PRENOS"
        APPEND FROM (m.sk_path+"hk_tmzust.dbf")
      *ENDIF
    ELSE
      APPEND FROM (m.sk_path+"hk_tmzust.dbf")
    ENDIF  
  ENDIF 
  vysledek = vysledek + "[1#G]       aktualizovano " + "_TMHKOBR" + crlf
ENDIF 
CLOSE DATABASES 
