-- Triggerfunktionen zu Waren/Warengruppen --
-- [
create function create_valid_produkt_typ_mask()
    returns TRIGGER as '
        DECLARE
            mask bit varying;
            freepos int4 := 0;
            err_msg text;
            new_len int4;
            old_len int4;
        BEGIN
            IF TG_OP <> ''UPDATE'' AND TG_OP <> ''INSERT'' THEN
                err_msg := TG_NAME || '': called for wrong action '' || TG_OP;
                RAISE EXCEPTION ''%'', err_msg;
            END IF;

            new_len := bit_length(NEW.produkt_typ);
            IF TG_OP = ''UPDATE'' THEN
                new_len := bit_length(OLD.konto_typ);
            ELSE
                old_len := fibu.konto_typ_min_len();
            END IF;

            IF new_len < old_len THEN
                NEW.konto_typ := NEW.konto_typ::BIT(old_len)>>old_len-new_len;
            END IF; 

            IF old_len < new_len THEN
                EXECUTE ''update fibu.konten_typen set konto_typ=konto_typ'' ||
                        ''::BIT('' || new_len || '')>>'' || new_len-old_len;
            END IF;

            RETURN NEW;
        END;
    ' language 'plpgsql';

create function set_tiefe()
    returns TRIGGER as '
        DECLARE
        BEGIN
            IF NEW.parent IS NOT NULL THEN
                select INTO NEW.tiefe tiefe+1 from 
                    waren.get_warengruppe(NEW.parent);

                update waren.warengruppe set is_parent=''true'' where 
                    _id_=NEW.parent;
            ELSE
                NEW.tiefe := 0;
            END IF;

            RETURN NEW;
        END;
    ' language 'plpgsql';

create function on_update_delete_check_is_parent()
    returns TRIGGER as '
        DECLARE
            i int4;
        BEGIN
            select INTO i count(*) from waren.warengruppe where 
                parent=OLD.parent;

            IF i > 0 THEN
                update waren.warengruppe set is_parent=''true'' where 
                    _id_=OLD.parent;
            ELSE
                update waren.warengruppe set is_parent=''false'' where
                    _id_=OLD.parent;
            END IF;

            RETURN OLD;
        END;
    ' language 'plpgsql';

create function on_update_check_tiefe()
    returns TRIGGER as '
        DECLARE
            row waren.warengruppe%ROWTYPE;
            parent_count int4;
        BEGIN
            IF NEW.parent IS NULL THEN
                NEW.tiefe := 0;
            ELSE
                IF OLD.parent IS NULL OR
                   OLD.parent <> NEW.parent THEN
                    select INTO NEW.tiefe tiefe+1 from 
                        waren.get_warengruppe(NEW.parent);
                END IF;
            END IF;

            IF NEW.tiefe <> OLD.tiefe THEN
                FOR row IN select * from waren.warengruppe where 
                    parent=NEW._id_ 
                LOOP
                    update waren.warengruppe set tiefe=NEW.tiefe+1 where 
                        _id_=row._id_;
                END LOOP;
            END IF;

            RETURN NEW;            
        END;
    ' language 'plpgsql';

create function on_delete_new_parent()
    returns TRIGGER as '
        DECLARE
            row waren.warengruppe%ROWTYPE;
        BEGIN
            FOR row IN select * from waren.warengruppe where 
                parent=OLD._id_ 
            LOOP
                update waren.warengruppe set parent=OLD.parent where 
                    _id_=row._id_;
            END LOOP;

            RETURN OLD;
        END;
    ' language 'plpgsql';
-- ]
-- Ende Triggerfunktionen zu Waren/Warengruppen --
