*!*****************************************************************************
*!      Procedure: KALKULUJ_MASO
*!*****************************************************************************

PRIVATE hloubka

* v ODECTENO je odectene mnozstvi, neodectene zustane v MNOZSTVI
SELECT 000000000000 as c_karty, $0 as hruba, $0 as cista FROM sk1 INTO CURSOR kalk READWRITE WHERE .f.
hloubka = 1
SELECT sk1
GO TOP
IF ! EOF()
    DO WHILE .T.
*        REPLACE odecteno WITH sk1.mnozstvi, mnozstvi WITH sk1.mnozstvi - sk1.odecteno
        IF sk1.mnozstvi > 0
            SCATTER FIELD c_karty, mnozstvi MEMVAR
            SELECT polozky
            LOCATE FOR c_karty = m.c_karty .AND. ! EMPT(c_poloz) .AND. ! EMPT(hruba)
            IF FOUND()
                SCAN FOR c_karty = m.c_karty .AND. ! EMPT(c_poloz) .AND. ! EMPT(hruba)
                    INSERT INTO kalk (c_karty, hruba, cista) ;
                        VALUE (polozky.c_poloz, ROUND(m.mnozstvi * polozky.hruba / IIF(polozky.pocet=0 OR ISNULL(polozky.pocet),1,polozky.pocet), 4), ;
                                                ROUND(m.mnozstvi * polozky.cista / IIF(polozky.pocet=0 OR ISNULL(polozky.pocet),1,polozky.pocet), 4))
                ENDSCAN
                SELECT sk1
                REPLACE mnozstvi WITH 0
            ELSE
                SELECT sk1
            ENDIF
        ENDIF
        SKIP
        IF EOF()
            hloubka = m.hloubka + 1
            SELECT c_karty, SUM(hruba) AS mnozstvi, SUM(hruba) AS hruba, SUM(cista) AS cista GROUP BY 1 ;
                FROM kalk INTO CURSOR qqq
            
            IF RECCOUNT() > 0
                SELECT kalk
                DELETE ALL 
                pozice = RECNO("sk1")
                INSERT INTO sk1 (c_karty, mnozstvi) SELECT c_karty, mnozstvi FROM qqq
                USE IN qqq
                SELECT sk1
                GOTO (m.pozice)
            ELSE
                EXIT
            ENDIF
        ENDIF
    ENDDO
ENDIF
USE IN kalk
datum = ser_time()
