*:*****************************************************************************
*: Procedure file: SK_KONTRLOA_UCTUUU
*:
*: kontroluje v prenasenych sk_fin, plt a pol existenci cisel uctu, odpovidajicich uzaverek
*: a financnich castek (fin.cena_celk proti pol.prachy, fin.cena_slev proti plt.cena_pl)
*:
*: 2012-11-30  upraveno formatovani vysledku
*: 2012-12-27  snese i chybejici backslash u sk_path
*: 2018-11-21  pridany indext k souboru, v nemz se locatuje
*:
*:*****************************************************************************

LPARAMETERS sk_path, pripona

IF EMPTY(m.pripona)
    pripona = ""
ENDIF 

sk_path = sk_path + IIF(RIGHT(m.sk_path,1)="\", "", "\")
IF .not. (FILE(m.sk_path + "sk_fin" + m.pripona + ".dbf") .and. FILE(m.sk_path + "sk_pol" + m.pripona + ".dbf") .and. FILE(m.sk_path + "sk_plt" + m.pripona + ".dbf"))
     vysledek = m.crlf + "[2#R] Soubory SK_xxx" + UPPER(m.pripona) + " neexistuji"
     RETURN 
ENDIF 

SELECT u_cis, PADR(uzaverka, 17) as uz_fin, SPACE(17) as uz_plt, SPACE(17) as uz_pol, ;
       SUM(cena_celk) as cena_celk, SUM(cena_slev) as cena_slev, 000000000.00 as prachy, 000000000.00 as cena_pl, ;
       000000000.00 as cena_celk2, 000000000.00 as cena_slev2, 000000000.00 as prachy2, 000000000.00 as cena_pl2 ;
       GROUP BY 1,2 ;
       FROM (m.sk_path + "sk_fin" + m.pripona) INTO TABLE (m.pri_path + "sk_kontrola")

INDEX on u_cis  TAG u_cis
INDEX on uz_fin TAG uz_fin
SET ORDER TO 


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
SELECT u_cis, PADR(uzaverka, 17) as uz_plt, SUM(cena_pl) as cena_pl ;
       GROUP BY 1,2 ;
       FROM (m.sk_path + "sk_plt" + m.pripona) INTO TABLE (m.pri_path + "sk_kontrola2")
SCAN
    SCATTER MEMVAR 
    SELECT sk_kontrola
    LOCATE FOR (u_cis = m.u_cis) .and. (uz_fin = m.uz_plt)
    IF FOUND()
        REPLACE uz_plt WITH m.uz_plt, cena_pl WITH cena_pl + m.cena_pl
    ELSE
        INSERT INTO (m.pri_path + "sk_kontrola") (u_cis, uz_plt, cena_pl) VALUES (m.u_cis, m.uz_plt, m.cena_pl)
    ENDIF 
    SELECT sk_kontrola2
ENDSCAN 

*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
SELECT u_cis, PADR(uzaverka, 17) as uz_pol, SUM(prachy) as prachy ;
       GROUP BY 1,2 WHERE NOT EMPTY(k_odpoctu);
       FROM (m.sk_path + "sk_pol" + m.pripona) INTO TABLE (m.pri_path + "sk_kontrola2")
SCAN
    SCATTER MEMVAR 
    SELECT sk_kontrola
    LOCATE FOR (u_cis = m.u_cis) .and. (uz_fin = m.uz_pol)
    IF FOUND()
        REPLACE uz_pol WITH m.uz_pol, prachy WITH prachy + m.prachy
    ELSE
        INSERT INTO (m.pri_path + "sk_kontrola") (u_cis, uz_pol, prachy) VALUES (m.u_cis, m.uz_pol, m.prachy)
    ENDIF 
    SELECT sk_kontrola2
ENDSCAN 

*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
SELECT sk_kontrola
REPL ALL cena_celk2 WITH cena_celk, cena_slev2 WITH cena_slev, prachy2 WITH prachy, cena_pl2 WITH cena_pl
REPLACE FOR ABS(cena_celk-prachy)  < 1 cena_celk WITH 0, prachy  WITH 0
REPLACE FOR ABS(cena_slev-cena_pl) < 1 cena_slev WITH 0, cena_pl WITH 0
DELETE FOR  cena_slev = 0 .and. cena_celk = 0 .and. prachy = 0 .and. cena_pl = 0

REPLACE FOR EMPTY(uz_fin) uz_fin WITH " --chybi FIN--"
REPLACE FOR EMPTY(uz_plt) uz_plt WITH " --chybi PLT--"
REPLACE FOR EMPTY(uz_pol) uz_pol WITH " --chybi POL--"
INDEX on u_cis + uz_fin + uz_plt + uz_pol TAG qwe UNIQUE 
COUNT ALL TO qwe
IF m.qwe > 0
    IF m.qwe > 33
        REPLACE ALL u_cis WITH LEFT(u_cis, 2) + "*****", cena_celk2 WITH 0, cena_slev2 WITH 0, cena_pl2 WITH 0, prachy2 WITH 0
    ENDIF 
    vysledek = ""
    SCAN
        vysledek = m.vysledek + m.crlf + "[2#R] <B>" + u_cis + "</B> fin:" + uz_fin + "<B>" + STR(cena_celk2, 9,1) + "/" + ALLTRIM(STR(cena_slev2, 9,1)) + "</B> plt:" + uz_plt + "<B>" + STR(cena_pl2, 9,1) + "</B> pol:" + uz_pol + "<B>" + STR(prachy2, 9,1) + "</B>"
    ENDSCAN 
ELSE
    vysledek = "  ... OK"
ENDIF 
