*:*****************************************************************************
*: Procedure file: FTP_HKINHDS
*: Uprava pro import z HDS
*:*****************************************************************************
param sk_path, pripona, exp_path, tmakt

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

m.myBonKoef = 0.07

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

vysledek = crlf

if file(m.exp_path + "zakexport.csv")
    use (m.sk_path + "hk_bony")
    SET ORDER TO TAG u_dat
    sele 0
    CREATE TABLE (m.exp_path + "zakexport") (pokl_nazev C(6), zu_karta C(6), typ C(1), ucet_id C(10),transdatet C(19), suma N(12,2), delta N(12,2))
    APPEND FROM (m.exp_path + "zakexport.csv")  DELIMITED WITH CHARACTER ";"
    GO TOP 
    DELETE 		&& smazat prvni radek s nazvy poli
    SELECT LEFT(transdatet,4)+SUBSTR(transdatet,6,2)+SUBSTR(transdatet, 9, 2) as datum, RIGHT(transdatet,8) as cas, PADR(zu_karta,12) as cis_kred,;
     LEFT(pokl_nazev,2)+RIGHT(ucet_id,5) as u_cis, .T. as zapsano, suma as castka, suma as cena_celk, "CZK" as hk_mena, 00.0000 as bon_koef, delta;
     FROM (m.exp_path + "zakexport") INTO TABLE (m.exp_path + "hk_bony_x") WHERE typ="C"
    use (m.exp_path + "hk_bony_x") Alias hk_bony_x Exclusive
    INDEX on datum + cis_kred + u_cis TAG u_dat
    SELECT * from (m.exp_path + "hk_bony_x") 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 
    * V CSV souboru je posledni sloupec zmena bodu, pri placeni bodama je castka = 0 a zmena # 0
    REPLACE bon_koef WITH IIF(castka # 0, delta / castka, 0) ALL 
    REPLACE bon_koef WITH 1 FOR castka = 0 AND delta # 0
    REPLACE castka WITH delta, cena_celk WITH delta FOR bon_koef = 1
    sele hk_bony 
    APPEND FROM (m.exp_path + "pribylo")
    vysledek = vysledek + "[1#G]       appendovano " + "HK_BONY" + "z HDS" + crlf
    *
    *
    * Import obratu pl. karty
    *
    USE (m.sk_path + "hkredobr")
    IF FILE(m.sk_path + "_tmhkobr.dbf")
      SELECT 0
      use (m.sk_path + "_tmhkobr")
    ENDIF   
    
    
    SELECT LEFT(transdatet,4)+SUBSTR(transdatet,6,2)+SUBSTR(transdatet, 9, 2) as datum, RIGHT(transdatet,8) as cas, PADR(zu_karta,12) as cis_kred,;
     LEFT(pokl_nazev,2)+RIGHT(ucet_id,5) as u_cis, .T. as zapsano, suma as castka, "CZK" as hk_mena, .T. as label, "  " as priznak, delta;
     FROM (m.exp_path + "zakexport") INTO TABLE (m.exp_path + "hk_obr_x") WHERE typ="1"
    use (m.exp_path + "hk_obr_x") Alias hk_obr_x Exclusive
    * V CSV souboru je posledni sloupec pri nabiti stejny jako castka, pri placeni je castka 0 a zmena zaporna
    REPLACE priznak WITH "KK" FOR delta # 0 AND (ABS(delta) <= ABS(castka))
    *REPLACE castka WITH delta FOR (castka = 0) OR (ABS(delta) < ABS(castka))	&& U nabiti muzou byt castky ruzne, pac se 
    *Zmena 19.4.2013
    REPLACE castka WITH delta ALL 
    *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 
    INDEX ON datum + cas + cis_kred + u_cis TAG  u_dat
    scan
      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
      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
        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 
    APPEND FROM (m.exp_path + "hk_obr_x")
    vysledek = vysledek + "[1#G]       appendovano " + "HK_OBR" + "  HDS" + crlf
else
    vysledek = vysledek + "[1#R]       ... " + "neexistuje soubor " + m.exp_path + "ZakExport" + " z HDS" + crlf
endif



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()