*:*****************************************************************************
*: Procedure file: FTP_EXPORT_PTACEK
*: Exportuje v dohodnutém formátu platby platební kartou do CSV souboru
*:*****************************************************************************
PARAMETERS sk_path, exp_path
LOCAL uzaverka, fn_skfin, fn_skplt, fn_skpol, fn_karty, pzaznamu
m.sk_path = ADDBS(m.sk_path)
m.exp_path = ADDBS(m.exp_path)
IF !DIRECTORY(m.exp_path)
   mkdir(m.exp_path)
ENDIF
*
m.uzaverka = SUBSTR(DTOS(DATE()), 3, 6) + " 01:00"
m.fn_skfin = m.sk_path+"sk_fin"
m.fn_skplt = m.sk_path+"sk_plt"
m.fn_skpol = m.sk_path+"sk_pol"
m.fn_karty = m.sk_path+"karty"
*
CLOSE ALL
CLOSE DATABASES
CLEAR WINDOW
SET SAFETY OFF 
IF VARTYPE(m.crlf) == 'U'
	m.crlf = CHR(13) + CHR(10)
ENDIF 
IF VARTYPE(m.vysledek) == 'U'
	PUBLIC vysledek
	vysledek = crlf
ENDIF 
*
USE (m.fn_skfin) ALIAS fin ORDER u_cis IN 0
SELECT fin
SET FILTER TO uzaverka >= m.uzaverka
USE (m.fn_skpol) ALIAS pol ORDER u_cis IN 0
USE (m.fn_karty) ALIAS karty ORDER nazev IN 0
*SELECT DISTINCT b.u_cis, b.uzaverka FROM (m.fn_skplt) b WHERE (uzaverka = m.uzaverka) AND (LEFT(b.druh_pl,2) = "K_") AND !INLIST(b.druh_pl, "K_HOTEL", "K_HOTEL2", "K_ZAVOD") INTO TABLE ("c:\alto\temp\platby_kartou")
*SELECT a.id_kas, a.u_cis, SPACE(8) as cdatum, SPACE(10) as druh, 000000.00 as bez_dph, a.dan, ROUND(((a.dan-1)/IIF(a.dan#0, a.dan, 1) * a.prachy), 2) as dph, a.prachy, a.nazev FROM (m.fn_skpol) a, ("c:\temp\platby_kartou") b;
* INTO TABLE ("c:\alo\temp\NAV"+DTOS(DATE())) WHERE (uzaverka = m.uzaverka) AND (a.u_cis = b.u_cis)
SELECT LEFT(a.u_cis,2) as id_kas, a.uzaverka, SPACE(8) as cdatum, a.u_cis, b.nazev, SPACE(10) as druh, b.prachy-ROUND(b.prachy*(b.dan-1)/b.dan,2) as bez_dph, b.dan, ROUND(b.prachy*(b.dan-1)/b.dan,2) as dph, b.prachy FROM (m.fn_skplt) a LEFT JOIN (m.fn_skpol) b ON (a.u_cis=b.u_cis) and (a.uzaverka = b.uzaverka);
 INTO TABLE (m.exp_path+"NAV") WHERE (LEFT(a.druh_pl,2)="K_") AND !INLIST(a.druh_pl, "K_HOTEL", "K_HOTEL2", "K_ZAVOD")  and (a.uzaverka >=m.uzaverka) ORDER BY a.uzaverka, a.u_cis
REPLACE ALL bez_dph WITH prachy - dph
SET RELATION TO u_cis INTO fin, nazev into karty
REPLACE ALL cdatum WITH fin.u_cas, druh with karty.druh_kalk
USE IN fin
*BROWSE 
*m.pzaznamu = RECCOUNT()
*IF m.pzaznamu > 0
	COPY TO (m.exp_path+"NAV"+LEFT(DTOS(DATE()), 8)+".txt") FIELDS id_kas, cdatum, u_cis, druh, bez_dph, dan, dph, prachy DELIMITED WITH CHARACTER ';' ALL
*ENDIF 
CLOSE DATABASES
ERASE (m.exp_path+"NAV.dbf")
*
m.vysledek = m.vysledek + "[1]" + "          Export platby kartou Ptáček:" + STR(m.pzaznamu) + " záznamů " + TTOC(DATETIME()) + " " + crlf
