*:*****************************************************************************
*: Procedure file: HK_HOTIN
*: 
*: Import hot_kred, event.hk_oskar
*: Zmena 21.11.2009 - aktualizuje existujici zaznamy, pokud je to povoleno parametrem
*: Aktualizovat by se melo jen na centrale, takze parametr p_aktualizovat by mel byt nastaven pro provozovny
*: 05.04.2017 locate s nooptimize
*:*****************************************************************************
param sk_path, pripona, exp_path, p_aktualizovat

IF PCOUNT() < 4
  m.p_aktualizovat = .t.
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 file(m.exp_path + "hotout" + m.pripona + ".dbf")
    USE (m.sk_path + "hot_kred")
    sele 0
    use (m.exp_path + "hotout" + m.pripona) Alias hotout_x Exclusive
    IF FSIZE('label') = 0
      ALTER TABLE (m.exp_path + "hotout" + m.pripona) ADD COLUMN label C(1)
      repl all label with "X"
    ELSE 
      repl all label with .T.
    ENDIF     
    inde on cis_kred tag cis_kred
    SCAN 
      SCATTER MEMVAR 
      sele hot_kred
      locate for cis_kred = hotout_x.cis_kred NOOPTIMIZE 
      if found()
        IF m.p_aktualizovat AND hot_kred.datum < hotout_x.datum
          GATHER MEMVAR 
        ENDIF 
        sele hotout_x
        IF TYPE('label') = 'C'
          repl label with ''
        ELSE
          REPLACE label WITH .F.
        ENDIF 
      else
        sele hotout_x
      endif
    ENDSCAN
    SELECT hotout_x
    dele for empt(label)
    PACK 
    SELECT hot_kred
    APPEND FROM (m.exp_path + "hotout" + m.pripona + ".dbf")
    seznam_appendovanych = seznam_appendovanych + " " + m.exp_path + "hotout" + m.pripona
else
    seznam_neexistujicich = seznam_neexistujicich + " " + m.exp_path + "hotout" + m.pripona
endif

CLOSE DATABASES 

if file(m.exp_path + "oskout" + m.pripona + ".dbf")
    USE (m.sk_path + "hk_oskar")
    sele 0
    use (m.exp_path + "oskout" + m.pripona) Alias oskout_x Exclusive
    IF FSIZE('label') = 0
      ALTER TABLE (m.exp_path + "oskout" + m.pripona) ADD COLUMN label C(1)
      repl all label with "X"
    ELSE 
      repl all label with .T.
    ENDIF     
    *INDEX on kod_karty TAG kod_karty
    *inde on cis_kred tag cis_kred
    SCAN 
      sele hk_oskar
      locate for cis_kred = oskout_x.cis_kred AND datum = oskout_x.datum AND cas = oskout_x.cas NOOPTIMIZE 
      if found()
        sele oskout_x
        IF TYPE('label') = 'C'
          repl label with ''
        ELSE
          REPLACE label WITH .F.
        ENDIF 
      else
        sele oskout_x
      endif
    ENDSCAN
    SELECT oskout_x
    dele for empt(label)
    PACK 
    SELECT oskout_x
    SCAN
      SCATTER MEMVAR 
      SELECT hk_oskar
      REPLACE hk_oskar.storno WITH .T. FOR hk_oskar.cis_kred = m.cis_kred OR hk_oskar.kod_karty = m.kod_karty
      INSERT INTO (m.sk_path + "hk_oskar") FROM MEMVAR 
    ENDSCAN 
    seznam_appendovanych = seznam_appendovanych + " " + m.exp_path + "oskout" + m.pripona
else
    *seznam_neexistujicich = seznam_neexistujicich + " " + m.exp_path + "oskout" + m.pripona
endif

CLOSE DATABASES 

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 + "[2#R]       neexistuje soubor: " + UPPER(m.seznam_neexistujicich) + crlf
ENDIF 
