*    *    *    *    *    *    *    *    *    *    *    *    *    *    
* export trzeb galant 
* exportuji se jen ucty s druhem platby K_KARTA
* 2017-04-15
*    *    *    *    *    *    *    *    *    *    *    *    *    *    

#DEFINE SAZ_G_DPH1  1.0
#DEFINE SAZ_G_DPH2  1.1
#DEFINE SAZ_G_DPH3  1.15
#DEFINE SAZ_G_DPH4  1.21
#DEFINE CRLF CHR(13)+CHR(10)

SELECT disti id_kas, uzaverka, uz_cislo, uct_den WHERE BETWEEN(uct_den, m.datum_uz1, m.datum_uz2) ;
       FROM (m.fn_sk_sez) INTO TABLE (m.gcpri_path + "uzaverky")

SELECT * ;
       WHERE uzaverka + LEFT(u_cis,2) in (sele uzaverka + id_kas from (m.gcpri_path + "uzaverky")) AND druh_pl = "K_KARTA " ;
       FROM (gcdat_path + "sk_plt") INTO TABLE (m.gcpri_path + "plt_galant")
INDEX on uzaverka + u_cis TO uz_ucis

SELECT uzaverka, u_cis, trzba_dph1, trzba_dph2, trzba_dph3, trzba_dph4, dan1, dan2, dan3, dan4, $0 as rozdil, ;
       $0 as dan_zakl1, $0 as dan_zakl2, $0 as dan_zakl3, $0 as dan_zakl4, ;
       $0 as trz1, $0 as trz2, $0 as trz3, $0 as trz4, ;
       $0 as dph1, $0 as dph2, $0 as dph3, $0 as dph4  ;
       WHERE uzaverka + u_cis IN (SELECT uzaverka + u_cis FROM (m.gcpri_path + "plt_galant")) ;
       FROM (gcdat_path + "sk_fin") INTO TABLE (m.gcpri_path + "fin_galant")

REPLACE FOR dan1 = SAZ_G_DPH1 trz1 WITH trzba_dph1
REPLACE FOR dan2 = SAZ_G_DPH1 trz1 WITH trzba_dph2
REPLACE FOR dan3 = SAZ_G_DPH1 trz1 WITH trzba_dph3
REPLACE FOR dan4 = SAZ_G_DPH1 trz1 WITH trzba_dph4

REPLACE FOR dan1 = SAZ_G_DPH2 trz2 WITH trzba_dph1
REPLACE FOR dan2 = SAZ_G_DPH2 trz2 WITH trzba_dph2
REPLACE FOR dan3 = SAZ_G_DPH2 trz2 WITH trzba_dph3
REPLACE FOR dan4 = SAZ_G_DPH2 trz2 WITH trzba_dph4

REPLACE FOR dan1 = SAZ_G_DPH3 trz3 WITH trzba_dph1
REPLACE FOR dan2 = SAZ_G_DPH3 trz3 WITH trzba_dph2
REPLACE FOR dan3 = SAZ_G_DPH3 trz3 WITH trzba_dph3
REPLACE FOR dan4 = SAZ_G_DPH3 trz3 WITH trzba_dph4

REPLACE FOR dan1 = SAZ_G_DPH4 trz4 WITH trzba_dph1
REPLACE FOR dan2 = SAZ_G_DPH4 trz4 WITH trzba_dph2
REPLACE FOR dan3 = SAZ_G_DPH4 trz4 WITH trzba_dph3
REPLACE FOR dan4 = SAZ_G_DPH4 trz4 WITH trzba_dph4

REPLACE ALL rozdil WITH trzba_dph1 + trzba_dph2 + trzba_dph3 + trzba_dph4 - trz1 - trz2 - trz3 - trz4

REPLACE ALL dan_zakl1 WITH ROUND(trz1 / SAZ_G_DPH1, 2), dph1 WITH trz1 - dan_zakl1
REPLACE ALL dan_zakl2 WITH ROUND(trz2 / SAZ_G_DPH2, 2), dph2 WITH trz2 - dan_zakl2
REPLACE ALL dan_zakl3 WITH ROUND(trz3 / SAZ_G_DPH3, 2), dph3 WITH trz3 - dan_zakl3
REPLACE ALL dan_zakl4 WITH ROUND(trz4 / SAZ_G_DPH4, 2), dph4 WITH trz4 - dan_zakl4

gal_exportfile = PUTFILE("kam to chcete ulozit?", "galant-trzby", "csv")
IF .not. EMPTY(m.gal_exportfile)
    STRTOFILE( '"uzaverka",       "u_cis", "dan_zakl1",      "dph1", "dan_zakl2",      "dph2", "dan_zakl3",      "dph3", "dan_zakl4",      "dph4"' + CRLF, m.gal_exportfile)
    SCAN
        STRTOFILE( '"' + uzaverka + '", "' + u_cis + '"' + nx(dan_zakl1) + nx(dph1) + nx(dan_zakl2) + nx(dph2) + nx(dan_zakl3) + nx(dph3) + nx(dan_zakl4) + nx(dph4) + CRLF, m.gal_exportfile, 1)
    ENDSCAN 
ENDIF 

********** vraci oddelovac + formatovane cislo  **********
FUNCTION nx (cislo, des_mist)
IF EMPTY(m.des_mist)
    des_mist = 2
ENDIF 
RETURN ', ' + STRTRAN(STR(cislo, 11, m.des_mist), ",", ".")
