*:*********************************************************************************************************
*:     Procedure file: SK_Z_HUSY.PRG
*:
*:             append SK z textovych souboru ze Septimove kasy
*: 2013-02-12: 
*: 2013-06-04: v sk_pol se vyplnuje i nazev, protoze prazdny nazev nejde do odpoctu
*: 2013-10-21: v mnozstvi i prachy muze byt i desetinna cast za carkou
*:
*:*********************************************************************************************************
param sk_path, septim_path

sk_path      =     sk_path  + IIF(RIGHT(    m.sk_path,1) ="\", "", "\")
septim_path  = septim_path  + IIF(RIGHT(m.septim_path,1) ="\", "", "\")
id_kas_septim = "JB"
id_kuchyn     = "25"
id_bar        = "26"

vysledek = "    v.2013-1021: " + m.crlf
CLOSE DATABASES 

sk_otevreno = ""
LOCAL skin_oErr AS Exception
TRY     
    use (m.sk_path + "SK_FIN ")   in 0
    use (m.sk_path + "SK_SEZ ")   in 0
    use (m.sk_path + "SK_PLT ")   in 0
    use (m.sk_path + "SK_POL ")   in 0
    USE (m.sk_path + "umisteni") ORDER c_karty IN 0
    SET FILTER TO id_kas = m.id_kas_septim IN umisteni

CATCH TO skin_oErr 
    sk_otevreno = m.sk_otevreno + " nelze otevrit velka SK: " + ALLTRIM(STR(skin_oErr.ErrorNo)) + " " + skin_oErr.Message + " Line:" + ALLTRIM(STR(skin_oErr.LineNo))
FINALLY
ENDTRY 

IF EMPTY(m.sk_otevreno)    
    pocet_souboru = ADIR(soubory, m.septim_path + "prodeje20*.csv")
    FOR iii = 1 TO pocet_souboru
        DO vyrob_uzaverku WITH septim_path + LOWER(soubory(iii, 1))
    ENDFOR
ELSE
    vysledek = m.vysledek + m.sk_otevreno
ENDIF 
CLOSE DATABASES 


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE vyrob_uzaverku
PARAMETERS jmeno_souboru

fh = FOPEN(jmeno_souboru)
IF m.fh > 0 
    oddelovac = ";"
    uzaverka = SUBSTR(JUSTSTEM(m.jmeno_souboru), 10, 6) + " 23:77"
    SELECT sk_sez
    LOCATE FOR uzaverka = m.uzaverka .and. id_kas = m.id_kas_septim
    IF FOUND()
        RETURN
    ENDIF

    IF SELECT("pom_sk_pol") > 0
        SELE pom_sk_pol
        DELETE ALL
    ELSE 
        SELE sk_pol
        COPY STRUCTURE TO (m.pri_path + "pom_sk_pol")
        SELECT 0
        USE (m.pri_path + "pom_sk_pol")
    ENDIF 
    DO while ! feof(m.fh)
        radek    = fgets(m.fh,333)
        at1      = at(m.oddelovac, m.radek, 1)
        at2      = at(m.oddelovac, m.radek, 2)
        at3      = at(m.oddelovac, m.radek, 3)
        at4      = at(m.oddelovac, m.radek, 4)

        c_karty  = VAL(left(m.radek, m.at1-1))
        cen_hlad = UPPER(substr(m.radek, m.at1+1, m.at2-m.at1-1))
        mnozstvi = VAL(STRTRAN(substr(m.radek, m.at2+1,   m.at3-m.at2-1), ",", "."))
        prachy   = VAL(STRTRAN(substr(m.radek, m.at3+1,   m.at4-m.at3-1), ",", "."))
        dan      = VAL(substr(m.radek, m.at4+1)) / 100 + 1    &&& az do konce radku


STRTRAN("9,4", ",", ".")


        DO CASE
        CASE "VIP" $ cen_hlad
            cen_hlad = "2"
        CASE "PERSON" $ cen_hlad
            cen_hlad = "3"
        CASE " 50%" $ cen_hlad
            cen_hlad = "5"
        CASE " 20%" $ cen_hlad
            cen_hlad = "6"
        CASE "DN" $ cen_hlad
            cen_hlad = "8"
        OTHERWISE
            cen_hlad = "1"
        ENDCASE

        IF m.c_karty > 0 .and. m.mnozstvi # 0
           APPEND BLANK 
           GATHER FIELDS c_karty, cen_hlad, mnozstvi, prachy, dan MEMVAR
        ENDIF 
    ENDDO 
    FCLOSE(m.fh)
    SELECT pom_sk_pol
    SET RELATION TO c_karty INTO umisteni
    REPLACE ALL uzaverka WITH m.uzaverka, id_kas WITH m.id_kas_septim, id_man WITH umisteni.id_man, u_cis WITH m.id_kas_septim + "00001", k_odpoctu WITH "*", nazev WITH "=q1^W*3F(*6G&@ " + STR(c_karty) + STR(c_karty)
    REPLACE FOR EMPTY(id_man) id_man WITH m.id_kas_septim
    SUM prachy TO trzba_celkem
    SELECT sk_pol
    APPEND FROM (m.pri_path + "pom_sk_pol")

    INSERT INTO (m.sk_path + "sk_plt") (u_cis,     cena_pl,      druh_pl,   uzaverka, mena,    cena_mena) ;
                    VALUES (m.id_kas_septim + "00001", m.trzba_celkem, "Hotove", m.uzaverka, "KC_", m.trzba_celkem)

    INSERT INTO (m.sk_path + "sk_fin") (u_cis,   id_kas,           cena_celk,     uzaverka,    cena_slev) ;
            VALUES (m.id_kas_septim + "00001", m.id_kas_septim, m.trzba_celkem, m.uzaverka, m.trzba_celkem)

    SELECT disti id_kas, id_man, m.uzaverka as uzaverka, RIGHT(DTOS(DATE()),6)+" "+TIME() as lastuzav ;
           FROM (m.pri_path + "pom_sk_pol") INTO TABLE (m.pri_path + "pom_sez")
    SELECT sk_sez
    APPEND FROM (m.pri_path + "pom_sez")
    USE IN pom_sez
    vysledek = m.vysledek + m.jmeno_souboru + " "
    IF FILE(jmeno_souboru + "__")
      RENAME (m.jmeno_souboru) TO (jmeno_souboru + "__" + ALLTRIM(STR(RAND()*999)))
    ELSE
      RENAME (m.jmeno_souboru) TO (jmeno_souboru + "__")
    ENDIF

ELSE
    WAIT "nelze otevrit soubor " + jmeno_souboru WINDOW TIMEOUT 3 AT 22,22
ENDIF 
