*:*************************************************************************************************************
*:
*: Procedure: SK_OUT
*:
*: 2012-08-09: kontroluje se soucet penez, zaroven se generuje soubor SK_CHK+pripona s kontrolnimi soucty, ktery se odesila na FTP
*:             default cesta exportu je privatni adresar
*:             exportovana eskacka.dbf se zapisuji do pri_path, jsou zazipovana a smazana
*:             pred zpracovanim je zipovy soubor smazan, aby se neposilal pripadny stary bordel
*:             interpretace nepovinneho ctvrteho parametru: 
*:             - pokud obsahuje ".zip", je interpretovan jako jmeno  zipoveho souboru vc. cesty
*:             - pokud obsahuje ".7z",  je interpretovan jako jmeno 7zipoveho souboru vc. cesty
*:             - pokud obsahuje "\", je interpretovan jako cesta, kam se data exportuji
*:             - jinak se interpretuje jako seznam kas, oddelenych mezerou nebo carkou, jejichz uzaverky se maji odeslat
*: 2012-11-13: do vysledku se zapisuji kody pro romankuv html kontrolor
*: 2012-11-16: on error nahrazeno try-catch
*:             zmena interpretace 4.parametru: pokud je uvedena cesta, soubory se vubec nezipuji
*:*************************************************************************************************************

param sk_path, pocet_dni, pripona, ctvrty_parametr

if empty(m.pripona)
  pripona = "_x"
endif

if empty(m.pocet_dni)
  m.pocet_dni = 10
endif

uz1 = RIGHT(DTOS(DATE() - m.pocet_dni), 8) + " 00:00"
vysledek = " v.12-1116: odesilano od " + m.uz1 + " "

seznam_kas = ""
exp_path = m.pri_path
zip_soubor = "----"

DO CASE 
CASE EMPTY(m.ctvrty_parametr)
    * nic

CASE ".zip" $ LOWER(m.ctvrty_parametr)    && parametrem jmeno zipoveho souboru vc. cesty
    zip_soubor =        LOWER(ALLTRIM(m.ctvrty_parametr))

CASE ".7z" $ LOWER(m.ctvrty_parametr)    && parametrem jmeno zipoveho souboru vc. cesty
    zip_soubor =        LOWER(ALLTRIM(m.ctvrty_parametr))

CASE "\" $ m.ctvrty_parametr    && parametrem je cesta, kam data exportovat
    exp_path = LOWER(ALLTRIM(m.ctvrty_parametr))
    exp_path = m.exp_path + IIF(RIGHT(m.exp_path,1)="\", "", "\")
    
OTHERWISE                       && parametrem je seznam kas
    seznam_kas = m.ctvrty_parametr
ENDCASE 

if ! directory(m.exp_path)
   mkdir (m.exp_path)
ENDIF

CLOSE ALL
CLOSE DATABASES 
CLEAR WINDOW
SET SAFETY OFF

ERASE (m.pri_path + "sk_???" + m.pripona)
ERASE (m.zip_soubor)

sk_path = sk_path + IIF(RIGHT(m.sk_path,1)="\", "", "\")

sk_otevreno = ""
TRY     
     use (m.sk_path + "SK_PLT ") in 0 excl
     use (m.sk_path + "SK_POL ") in 0 excl
     use (m.sk_path + "SK_FIN ") in 0 excl
     use (m.sk_path + "SK_TXT ") in 0 excl
     use (m.sk_path + "SK_SEZ ") in 0 excl
CATCH 
     sk_otevreno = m.sk_otevreno + " nelze otevrit velka SK"
FINALLY
ENDTRY 

IF EMPTY(m.sk_otevreno)
    IF EMPTY(m.seznam_kas)
       sele * where BETW(uzaverka, m.uz1, "999999") ;
            from sk_sez into table (m.pri_path + "sk_sez" + m.pripona)
    ELSE
       sele * where BETW(uzaverka, m.uz1, "999999") .and. (id_kas $ m.seznam_kas) ;
            from sk_sez into table (m.pri_path + "sk_sez" + m.pripona)
    ENDIF 
    
    SELECT * FROM sk_fin INTO TABLE (m.pri_path + "sk_fin" + m.pripona) ;
           WHERE uzaverka + LEFT(u_cis,2) in (SELECT uzaverka + id_kas FROM (m.pri_path + "sk_sez" + m.pripona))

    SELECT * FROM sk_plt INTO TABLE (m.pri_path + "sk_plt" + m.pripona) ;
           WHERE uzaverka + LEFT(u_cis,2) in (SELECT uzaverka + id_kas FROM (m.pri_path + "sk_sez" + m.pripona))

    SELECT * FROM sk_pol INTO TABLE (m.pri_path + "sk_pol" + m.pripona) ;
           WHERE uzaverka + LEFT(u_cis,2) in (SELECT uzaverka + id_kas FROM (m.pri_path + "sk_sez" + m.pripona))

    SELECT * FROM sk_txt INTO TABLE (m.pri_path + "sk_txt" + m.pripona) ;
           WHERE uzaverka + LEFT(u_cis,2) in (SELECT uzaverka + id_kas FROM (m.pri_path + "sk_sez" + m.pripona))


*    *    *    *    *    *    *    *    KONTROLA    *    *    *    *    *    *     zacatek
	SELECT uzaverka, u_cis, SUM(cena_slev) AS cena_fin, SUM(cena_celk) AS cena_celk, 1 as je_v_fin ;
	       GROUP BY 1,2 FROM (m.pri_path + "sk_fin" + m.pripona) INTO TABLE (m.pri_path + "fin")

	SELECT uzaverka, u_cis, SUM(cena_pl) AS cena_plt, 1 as je_v_plt ;
	       GROUP BY 1,2 FROM (m.pri_path + "sk_plt" + m.pripona) INTO TABLE (m.pri_path + "plt")

	SELECT uzaverka, u_cis, SUM(prachy) AS cena_pol, ;
	       $0 AS cena_plt, $0 AS cena_fin, $0 AS cena_celk, 01 as je_v_pol, 00 as je_v_fin, 00 as je_v_plt ;
	       WHERE ! EMPT(k_odpoctu) ;
	       GROUP BY 1,2 FROM (m.pri_path + "sk_pol" + m.pripona) INTO TABLE (m.pri_path + "pol")

	APPE FROM (m.pri_path + "fin")
	APPE FROM (m.pri_path + "plt")

	SELECT uzaverka, LEFT(u_cis,2) as id_kas, SUM(cena_pol) AS cena_pol, ;
	       SUM(cena_plt) AS cena_plt, SUM(cena_fin) AS cena_fin, SUM(cena_celk) AS cena_celk, ;
	       SUM(je_v_fin) AS je_v_fin, SUM(je_v_pol) AS je_v_pol, SUM(je_v_plt)  AS je_v_plt ;
	       GROUP BY 1,2 FROM (m.pri_path + "pol") INTO TABLE (m.pri_path + "sk_chk" + m.pripona)

    SET FILTER TO abs(cena_pol - cena_celk) > 9 .OR. abs(cena_plt - cena_fin) > 1         &&&&& zrusena kontrola poctu radku, protoze to nesedi pri uctech za nulovy prachy  .OR. (je_v_pol # je_v_fin) .OR. (je_v_pol # je_v_plt) .OR. (je_v_fin # je_v_plt)
    COUNT TO pocet_chyb
    IF pocet_chyb = 0
        vysledek = m.vysledek + " ... EXEC OK "
    ELSE 
        SCAN 
            vysledek = m.vysledek + m.crlf + "[2B#R]  " + "        bacha na uzaverku " + uzaverka + "/" + id_kas + " pol-celk=" + ALLTRIM(STR(cena_pol - cena_celk)) + " plt-fin=" + ALLTRIM(STR(cena_plt - cena_fin)) + " "
        ENDSCAN 
        vysledek = m.vysledek + m.crlf
    ENDIF 
*    *    *    *    *    *    *    *    KONTROLA    *    *    *    *    *    *     konec

    CLOSE DATABASES 
    FLUSH 
    DO CASE
    CASE ".zip" $ m.zip_soubor
        ERASE (m.zip_soubor)
        cmd = "run " + m.gcini_path + "7za a -r -tzip " + m.zip_soubor + " " + m.pri_path + "sk_???" + m.pripona + ".dbf"
        ? cmd
        &cmd

    CASE ".7z"  $ m.zip_soubor
        ERASE (m.zip_soubor)
        cmd = "run " + m.gcini_path + "7za a -r       " + m.zip_soubor + " " + m.pri_path + "sk_???" + m.pripona + ".dbf"
        ? cmd
        &cmd

    CASE exp_path # m.pri_path
        COPY FILE (m.pri_path + "sk_???" + m.pripona + ".dbf") TO (m.exp_path + "sk_???" + m.pripona + ".dbf")
        ERASE     (m.pri_path + "sk_???" + m.pripona + ".dbf")

    ENDCASE

else
    vysledek = m.vysledek + m.crlf + "[1B#R]  " + " NEPROVEDENO, nelze otevrit " + m.sk_otevreno + "  ... "
endif
CLOSE DATABASES 
