*    2023-10-30

#DEFINE CRLF CHR(13)+CHR(10)
LOCAL dbciel, dbfrom, prik_from, prik_where
SELECT zoznam
o_karty_filter = SET ("filter")
SELECT rk
RECCOUNT() 
SELECT rc
RECCOUNT() 
chyby = ""
zurnal_vypoc()
lohad = CREATEOBJECT("had", jaz("form_vypocet_vyrobnych_cien" ))
lohad.set_max(99)
lohad.SHOW()

SELECT rvypocty
INDEX on c_druh TAG c_druh

vyrob_tabulku_jednoho_vypoctu()

FOR poradi_vypoctu = 1 TO 90
    sufix_vypoctu = TRANSFORM(m.poradi_vypoctu, "@L 99")
    lohad.INCREMENT()
    SET ORDER to c_druh IN rvypocty
    SELECT pc_skup 
    SET RELATION TO c_druh INTO rvypocty
    REPLACE ALL vypocet WITH EVALUATE("rvypocty.vypocet" + m.sufix_vypoctu)

    LOCATE FOR .not. EMPTY(vypocet) AND .not. ISNULL(vypocet)
    IF FOUND()
        lohad.set_txt1 ("Vypocet prodejnich cen " + m.sufix_vypoctu)
	    jeden_vypocet()
        tux(.t.,.t., "rk", "REQUERY")
        tux(.t.,.t., "rc", "REQUERY")
    ELSE
        lohad.set_txt1 ("Vypocet prodejnich cen " + m.sufix_vypoctu + " - neprovadi se")
        lohad.set_txt2 (" ")
	ENDIF

ENDFOR 
tux(.t.,.t., "rk", "REQUERY")
tux(.t.,.t., "rc", "REQUERY")

DO pozavirej WITH "pc_skup"
SELECT rk
SET FILTER TO 
RELEASE lohad

IF ! EMPTY(m.chyby)
    STRTOFILE(m.chyby, "pri_prepoctech_nastaly_chyby.txt")
    MODIFY        FILE "pri_prepoctech_nastaly_chyby.txt" NOEDIT 
ENDIF
zurnal_vypoc ("---- HOTOVO -----")


*************************************************************************************
PROCEDURE jeden_vypocet

lohad.set_txt2 (" ")
txt_do_zurnalu1 = " Vypocet" + m.sufix_vypoctu + CRLF
txt_do_zurnalu2 = CRLF

SELECT pc_skup
SCAN FOR .not. EMPTY(vypocet) AND .not. ISNULL(vypocet)
    SCATTER MEMVAR
    puvodni_vypocet = ALLTRIM(m.vypocet)
    strtr_vypoctu()
    pokracuj = .t.
    cil   = ALLTRIM(  LEFT(m.vypocet, AT("=", m.vypocet)-1))
    dbciel=IIF("rk" $ m.cil, "rk", "rc")
    IF dbciel="rk" AND "rc" $ m.cil
        pripis_k_chybam ("nepovoleny cil " + m.cil + " Vyp" + m.sufix_vypoctu)
    ELSE 
        zdroj = ALLTRIM(SUBSTR(m.vypocet, AT("=", m.vypocet)+1))
        dbfrom=IIF("rk" $ m.zdroj, "rk", "rc")
        IF dbfrom="rk" AND "rc"$m.zdroj
            pripis_k_chybam ("nepovoleny zdroj " + m.zdroj + " Vyp" + m.sufix_vypoctu)
        ELSE
            prikaz = "UPDATE " + m.dbciel + " SET "
            DO WHILE "CENA" $ cil
                pozice_mezery = AT(" ", m.cil + " ")
                jedencil = ALLTRIM( LEFT(m.cil, pozice_mezery-1))
                cil      = ALLTRIM(SUBST(m.cil, pozice_mezery+1))
                IF BETWEEN(SUBSTR(m.jedencil,4), "CENA01", "CENAP9")
                    prikaz = m.prikaz + m.jedencil + "=" + m.zdroj + ", "
                    pokracuj = .t.
                ENDIF 
            ENDDO 
            prikaz = LEFT(m.prikaz, LEN(m.prikaz)-2)
            lohad.set_txt2 (ALLTRIM(m.druh) + ": " + m.prikaz)

            txt_dan = ""
            pozice_dane = ATC("m.dan", m.zdroj)
            IF m.pozice_dane > 0
                txt_dan = SUBSTR(m.zdroj, m.pozice_dane, 7)
                txt_dan = "    &"+"& " + m.txt_dan + "=" + nx(&txt_dan, 3)
            ENDIF 

            IF m.pokracuj
                IF m.dbciel="rc" OR m.dbciel<>m.dbfrom
                    IF m.dbciel="rc"
                        prikaz = m.prikaz + " FROM rk WHERE rc.c_karty = rk.c_karty and rk.c_druhk = " + nx(m.c_druh) + " .or. (rk.c_druhs = " + nx(m.c_druh) + " .and. EMPTY(rk.c_druhk) )"
                    ELSE
                        prikaz = m.prikaz + " FROM rc WHERE rk.c_karty = rc.c_karty and rk.c_druhk = " + nx(m.c_druh) + " .or. (rk.c_druhs = " + nx(m.c_druh) + " .and. EMPTY(rk.c_druhk) )"
                    ENDIF 
                ELSE 
                        prikaz = m.prikaz +                                     " WHERE rk.c_druhk = " + nx(m.c_druh) + " .or. (rk.c_druhs = " + nx(m.c_druh) + " .and. EMPTY(rk.c_druhk) )"
                ENDIF                                 

                chyba = ""
                TRY 
                    &prikaz
                CATCH TO oErr 
                    chyba = " " + nx(oErr.ErrorNo) + ":" + UPPER(oErr.Message)
                    prikaz = LOWER(m.prikaz)
                ENDTRY 

                DO zurnal_log WITH "OPERA", "Prodejni ceny", m.druh + ": " + m.prikaz + LEFT(m.chyba, 55) + " Vyp" + m.sufix_vypoctu + ": " + m.puvodni_vypocet + " -> " + m.vypocet

                txt_do_zurnalu1 = m.txt_do_zurnalu1 + CRLF + m.puvodni_vypocet    + "    " + m.druh
                txt_do_zurnalu2 = m.txt_do_zurnalu2 + CRLF + m.prikaz + m.txt_dan + "    " + m.druh + LEFT(m.chyba, 55)

                IF ! EMPTY(m.chyba)
                    pripis_k_chybam (LEFT(m.chyba, 44) + " Vyp" + m.sufix_vypoctu + ": " + m.vypocet)
                ENDIF
            ELSE 
                pripis_k_chybam ("nepovoleny cil " + m.cil + " Vyp" + m.sufix_vypoctu)
            ENDIF
        ENDIF
    ENDIF 
ENDSCAN    && pc_skup
zurnal_vypoc (m.txt_do_zurnalu1 + CRLF + m.txt_do_zurnalu2 + CRLF)


*************************************************************************************
PROCEDURE vyrob_tabulku_jednoho_vypoctu
lohad.set_txt2 ("vyroba tabulky dani")
cmd = "SELECT c_druh, druh, SPACE(222) AS vypocet"
SELECT skp
FOR jjj = 1 TO FCOUNT()
    policko = FIELD(jjj)
    IF LEFT(policko, 3) = "DAN"
        cmd = cmd + ", " + m.policko
    ENDIF
ENDFOR
cmd = m.cmd + " FROM skp INTO CURSOR pc_skup READWRITE "
&cmd


*************************************************************************************
FUNCTION strtr_vypoctu ()
vypocet = STRTRAN(UPPER(ALLTRIM(m.vypocet)), "CENA", "XXXX")    && tohle proto, aby se nenahradil substring "CENA" prodejkama, na konci z toho udelam korektni nazev fieldu

vypocet = STRTRAN(m.vypocet, "C0", "XXXX0")
vypocet = STRTRAN(m.vypocet, "C1", "XXXX1")
vypocet = STRTRAN(m.vypocet, "C2", "XXXX2")
vypocet = STRTRAN(m.vypocet, "C3", "XXXX3")
vypocet = STRTRAN(m.vypocet, "C4", "XXXX4")
vypocet = STRTRAN(m.vypocet, "C5", "XXXX5")
vypocet = STRTRAN(m.vypocet, "C6", "XXXX6")
vypocet = STRTRAN(m.vypocet, "C7", "XXXX7")
vypocet = STRTRAN(m.vypocet, "C8", "XXXX8")
vypocet = STRTRAN(m.vypocet, "C9", "XXXX9")

vypocet = STRTRAN(m.vypocet, "CA", "XXXXA")
vypocet = STRTRAN(m.vypocet, "CB", "XXXXB")
vypocet = STRTRAN(m.vypocet, "CC", "XXXXC")
vypocet = STRTRAN(m.vypocet, "CD", "XXXXD")
vypocet = STRTRAN(m.vypocet, "CE", "XXXXE")
vypocet = STRTRAN(m.vypocet, "CF", "XXXXF")
vypocet = STRTRAN(m.vypocet, "CG", "XXXXG")
vypocet = STRTRAN(m.vypocet, "CH", "XXXXH")
vypocet = STRTRAN(m.vypocet, "CI", "XXXXI")
vypocet = STRTRAN(m.vypocet, "CJ", "XXXXJ")
vypocet = STRTRAN(m.vypocet, "CK", "XXXXK")
vypocet = STRTRAN(m.vypocet, "CL", "XXXXL")
vypocet = STRTRAN(m.vypocet, "CM", "XXXXM")
vypocet = STRTRAN(m.vypocet, "CN", "XXXXN")
vypocet = STRTRAN(m.vypocet, "CO", "XXXXO")
vypocet = STRTRAN(m.vypocet, "CP", "XXXXP")

vypocet = STRTRAN(m.vypocet, "XXXX", "CENA")

* pridani aliasu K NAZVU PROMENNE: nejdriv se vsem da alias tabulky kceny, pak se tem s malymi cisly (01-40) a nakupce prepise na alias tabulky karty
vypocet = STRTRAN(m.vypocet, "CENA", "rc.CENA")

vypocet = STRTRAN(m.vypocet, "rc.CENA0" , "rk.CENA0")
vypocet = STRTRAN(m.vypocet, "rc.CENA1" , "rk.CENA1")
vypocet = STRTRAN(m.vypocet, "rc.CENA2" , "rk.CENA2")
vypocet = STRTRAN(m.vypocet, "rc.CENA3" , "rk.CENA3")
vypocet = STRTRAN(m.vypocet, "rc.CENA40", "rk.CENA40")

vypocet = STRTRAN(m.vypocet, "rc.CENA_PRUM", "rk.CENA_PRUM")
vypocet = STRTRAN(m.vypocet, "NAKUPKA"     , "rk.CENA_PRUM")
vypocet = STRTRAN(m.vypocet, "NC"          , "rk.CENA_PRUM")

vypocet = STRTRAN(m.vypocet, "*1*"          , "*")


*************************************************************************************
FUNCTION pripis_k_chybam (texticek)
chyby = m.chyby + CHR(13)+CHR(10) + m.druh + ": " + m.texticek


*************************************************************************************
FUNCTION rnd0 (cenicka)
RETURN ROUND(m.cenicka + .49, 0)

*************************************************************************************
FUNCTION zurnal_vypoc (texticek)

IF EMPTY(m.texticek)
    texticek = CRLF + CRLF + REPLICATE("=", 99) + m.gcuser_id
ENDIF 
small_log(m.texticek, "vypocet_prodejnich_cen")
