Files
Database/BES/BES_TRI.SRC
Jos Groot Lipman f64ec0c724 FSN#1069
svn path=/Database/trunk/; revision=8855
2004-11-09 12:31:20 +00:00

351 lines
12 KiB
Plaintext

#ifdef BES // 03-11-2000 PF
/*
* $Revision: 27 $
* $Modtime: 27-10-04 13:45 $
*
* History
*
* 12.05.2003 RL triggers BES_T_BES_BESTELOPDR_ITEM_A_UT and
BES_T_BES_BESTELLING_ITEM_A_UT are removed
triggers bes_t_bes_bestelling_B_IU,
bes_t_bes_bestelling_item_B_IU,
bes_t_bes_bestelopdr_B_IU,
bes_t_bes_bestelopdr_item_B_IU,
BES_T_BES_BESTELOPDR_ITEM_B_U and
BES_T_BES_BESTELLING_ITEM_B_U are changed in order to
improve performance and eliminate table based cursors necessity
* 07.05.2003 RL bes_t_bes_bestelopdr_B_IU: changed cursor to insert-select
*
*/
#include "comsql.h"
CREATE_TRIGGER(bes_t_bes_bestelling_B_IU)
BEFORE INSERT OR UPDATE ON bes_bestelling
FOR EACH ROW
BEGIN
UPDATE_PRIMARY_KEY(bes_bestelling_key,bes_s_bes_bestelling_key);
IF NVL(:new.BES_BESTELLING_ITEM_CNT,0) = NVL(:new.BES_BESTELLING_ITEM_CNT_ACPT,0) AND
:new.BES_BESTELLING_STATUS IN (2,3) AND :old.BES_BESTELLING_KEY IS NOT NULL THEN
:new.BES_BESTELLING_STATUS := 4;
END IF;
IF NVL(:new.BES_BESTELLING_ITEM_CNT,0) = NVL(:new.BES_BESTELLING_ITEM_CNT_LEV,0) AND
:new.BES_BESTELLING_STATUS < 6 AND :old.BES_BESTELLING_KEY IS NOT NULL THEN
:new.BES_BESTELLING_AFGEMELD := SYSDATE;
:new.BES_BESTELLING_STATUS := 6;
END IF;
IF (:new.BES_BESTELLING_STATUS =4 and
(:old.BES_BESTELLING_STATUS IS NULL OR :old.BES_BESTELLING_STATUS < 4)) OR
(:new.BES_BESTELLING_FIAT_USER IS NOT NULL and
:old.BES_BESTELLING_FIAT_USER IS NULL) THEN
:new.BES_BESTELLING_GEACCEPTEERD := SYSDATE;
:new.BES_BESTELLING_ITEM_CNT_ACPT := :new.BES_BESTELLING_ITEM_CNT;
ELSIF :new.BES_BESTELLING_DATUM is not null and
:old.BES_BESTELLING_DATUM is null THEN
:new.BES_BESTELLING_STATUS := 2;
:new.BES_BESTELLING_ITEM_CNT := 0;
ELSIF (:new.BES_BESTELLING_AFGEWEZEN is not null and
:old.BES_BESTELLING_AFGEWEZEN is null) OR
(:new.BES_BESTELLING_AFGEWEZEN is not null and
:old.BES_BESTELLING_AFGEWEZEN is not null and
:new.BES_BESTELLING_AFGEWEZEN <> :old.BES_BESTELLING_AFGEWEZEN) THEN
BEGIN
IF :old.BES_BESTELLING_DATUM is null THEN
:new.BES_BESTELLING_DATUM := SYSDATE;
END IF;
IF :new.BES_BESTELLING_OPMERKING is not null THEN
:new.BES_BESTELLING_STATUS := 1;
ELSE
APPLICATION_ERROR_GOTO(-20000, 'BES_NO_REJECTED_MESSAGE');
END IF;
END;
ELSIF :new.BES_BESTELLING_VERWERKT is not null and
:old.BES_BESTELLING_VERWERKT is null THEN :new.BES_BESTELLING_STATUS := 7;
ELSIF :new.BES_BESTELLING_DATUM is not null and
:old.BES_BESTELLING_DATUM is not null and
:old.BES_BESTELLING_DATUM <> :new.BES_BESTELLING_DATUM and
:old.BES_BESTELLING_STATUS = 1 THEN :new.BES_BESTELLING_STATUS := 2;
END IF;
IF :new.BES_BESTELLING_STATUS = 5 and
:old.BES_BESTELLING_STATUS <> 5 THEN
BEGIN
_EXEC3(_MSSQL(WEB.)WEB_SEND_WEB_MESSAGE,:new.BES_BESTELLING_KEY, :new.PRS_PERSLID_KEY, 'BESBES');
END;
END IF;
IF :new.BES_BESTELLING_STATUS = 6 and
:old.BES_BESTELLING_STATUS <> 6 THEN
BEGIN
_EXEC3(_MSSQL(WEB.)WEB_SEND_WEB_MESSAGE,:new.BES_BESTELLING_KEY, :new.PRS_PERSLID_KEY, 'BESOTV');
END;
END IF;
IF (:new.BES_BESTELLING_FIAT_USER IS NOT NULL and
:old.BES_BESTELLING_FIAT_USER IS NULL) or
(:new.BES_BESTELLING_STATUS = 1 and
:old.BES_BESTELLING_STATUS <> 1) THEN
delete from WEB_USER_MESSAGES where WEB_USER_MESS_ACTIONS_KEY = 6 and
WEB_USER_MESS_ACTION_PARAMS = TO_CHAR(:new.BES_BESTELLING_KEY);
END IF;
#ifdef MS_SQL
lerror:
#endif
END;
/
CREATE_TRIGGER(bes_t_bes_bestelling_item_B_IU)
BEFORE INSERT OR UPDATE ON bes_bestelling_item
FOR EACH ROW
BEGIN
UPDATE_PRIMARY_KEY(bes_bestelling_item_key,bes_s_bes_bestelling_item_key);
IF :old.bes_bestelling_item_key is null THEN
UPDATE BES_BESTELLING SET BES_BESTELLING_ITEM_CNT = BES_BESTELLING_ITEM_CNT + 1
WHERE BES_BESTELLING_KEY = :new.BES_BESTELLING_KEY;
END IF;
END;
/
CREATE_TRIGGER(bes_t_bes_bestelopdr_B_IU)
BEFORE INSERT OR UPDATE ON bes_bestelopdr
FOR EACH ROW
BEGIN
UPDATE_PRIMARY_KEY(bes_bestelopdr_key,bes_s_bes_bestelopdr_key);
IF NVL(:new.BES_BESTELOPDR_ITEM_CNT,0) = NVL(:new.BES_BESTELOPDR_ITEM_CNT_LEV,0) AND
NOT (:NEW.BES_BESTELOPDR_ITEM_CNT =0 AND :NEW.BES_BESTELOPDR_ITEM_CNT_LEV= 0) AND
:new.BES_BESTELOPDR_STATUS < 6 AND :old.BES_BESTELOPDR_KEY IS NOT NULL THEN
:new.BES_BESTELOPDR_LEVERDATUM := SYSDATE;
:new.BES_BESTELOPDR_STATUS := 6;
END IF;
IF :old.BES_BESTELOPDR_DATUM is null THEN
:new.BES_BESTELOPDR_DATUM := SYSDATE;
END IF;
IF :old.bes_bestelopdr_status is null THEN
:new.bes_bestelopdr_status := 2;
END IF;
IF :old.bes_bestelopdr_status is not null and
:new.bes_bestelopdr_status = 5 and
:old.bes_bestelopdr_status <> 5 THEN
-- send notification to user into Facilitor News
declare
mes VARCHAR2(255);
actkey NUMBER;
code VARCHAR2(6);
keepmes NUMBER;
BEGIN
BEGIN
SELECT web_user_mess_text_dsc, web_user_mess_actions_key
INTO mes,actkey
FROM web_user_mess_text
WHERE web_user_mess_text_code = 'BESCFM';
EXCEPTION
WHEN NO_DATA_FOUND THEN NULL;
END;
IF actkey IS NOT NULL THEN
BEGIN
INSERT INTO web_user_messages
(prs_perslid_key_sender,
prs_perslid_key_receiver,
web_user_mess_actions_key,
web_user_mess_dsc,
web_user_mess_action_status,
web_user_mess_action_params) select
NULL,
prs_perslid_key,
actkey,
mes,
1,
b.bes_bestelling_key
FROM bes_bestelling b, bes_bestelling_item bi
WHERE b.bes_bestelling_key = bi.bes_bestelling_key
and bi.bes_bestelopdr_key = :new.bes_bestelopdr_key;
END;
END IF;
keepmes := 5;
DELETE FROM web_user_messages
WHERE web_user_mess_action_datum < (SYSDATE - KEEPMES);
END;
END IF;
END;
/
CREATE_TRIGGER(bes_t_bes_bestelopdr_item_B_IU)
BEFORE INSERT OR UPDATE ON BES_BESTELOPDR_ITEM FOR EACH ROW
BEGIN
UPDATE_PRIMARY_KEY(bes_bestelopdr_item_key,bes_s_bes_bestelopdr_item_key);
IF :old.bes_bestelopdr_item_key is null THEN
UPDATE BES_BESTELOPDR SET BES_BESTELOPDR_ITEM_CNT = BES_BESTELOPDR_ITEM_CNT + 1
WHERE BES_BESTELOPDR_KEY = :new.BES_BESTELOPDR_KEY;
END IF;
END;
/
CREATE_TRIGGER(BES_T_BES_BESTELOPDR_ITEM_B_U)
BEFORE UPDATE ON BES_BESTELOPDR_ITEM FOR EACH ROW
BEGIN
IF NVL(:new.bes_bestelopdr_item_aantalontv,0) = NVL(:new.bes_bestelopdr_item_aantal,0)
AND NVL(:old.bes_bestelopdr_item_aantalontv,0) <> NVL(:old.bes_bestelopdr_item_aantal,0)
THEN
UPDATE BES_BESTELOPDR SET BES_BESTELOPDR_ITEM_CNT_LEV = BES_BESTELOPDR_ITEM_CNT_LEV + 1
WHERE BES_BESTELOPDR_KEY = :new.BES_BESTELOPDR_KEY;
END IF;
END;
/
#ifndef MS_SQL
/*
CREATE_TRIGGER(BES_T_BES_BESTELOPDR_ITEM_A_UT)
after update on BES_BESTELOPDR_ITEM
begin
declare bokey number(10);
CURSOR query IS
select bkey from
(select
b1.bes_bestelopdr_key bkey,
sum(bes_bestelopdr_item_aantal) request,
sum(nvl(bes_bestelopdr_item_aantalontv,0)) deliver
from bes_bestelopdr_item b1,
(select bes_bestelopdr_key
from bes_bestelopdr_item b
where b.bes_bestelopdr_key in
(select bes_bestelopdr_key from bes_bestelopdrstate where bes_bestelopdrstate = 1)) b2
where b1.bes_bestelopdr_key = b2.bes_bestelopdr_key
group by b1.bes_bestelopdr_key) where deliver >= request;
BEGIN
bokey := 0;
FOR besitem_rec IN query() LOOP
update bes_bestelopdr set bes_bestelopdr_leverdatum = sysdate, bes_bestelopdr_status = 6
where bes_bestelopdr_key = besitem_rec.bkey;
bokey := besitem_rec.bkey;
END LOOP;
if bokey <> 0 then
DELETE FROM bes_bestelopdrstate;
end if;
END;
END;
/
*/
#endif
CREATE_TRIGGER(BES_T_BES_BESTELLING_ITEM_B_U)
BEFORE UPDATE ON BES_BESTELLING_ITEM FOR EACH ROW
BEGIN
IF :new.bes_bestelling_i_gcpt_us_key is not null and
:old.bes_bestelling_i_gcpt_us_key is null THEN
:new.bes_bestelling_item_status := 4;
UPDATE BES_BESTELLING SET BES_BESTELLING_ITEM_CNT_ACPT = BES_BESTELLING_ITEM_CNT_ACPT + 1
WHERE BES_BESTELLING_KEY = :new.BES_BESTELLING_KEY;
END IF;
IF :new.BES_BESTELOPDR_KEY is not null and
:old.BES_BESTELOPDR_KEY is null THEN
:new.bes_bestelling_item_status := 5;
update BES_BESTELLING set BES_BESTELLING_STATUS = 5
where BES_BESTELLING_KEY = :new.BES_BESTELLING_KEY;
END IF;
IF NVL(:new.bes_bestelling_item_aantalontv,0) = NVL(:new.bes_bestelling_item_aantal,0) AND
NVL(:old.bes_bestelling_item_aantalontv,0) <> NVL(:old.bes_bestelling_item_aantal,0)
THEN
:new.bes_bestelling_item_status := 6;
UPDATE BES_BESTELLING SET BES_BESTELLING_ITEM_CNT_LEV = BES_BESTELLING_ITEM_CNT_LEV + 1
WHERE BES_BESTELLING_KEY = :new.BES_BESTELLING_KEY;
END IF;
END;
/
#ifndef MS_SQL
/*
CREATE_TRIGGER(BES_T_BES_BESTELLING_ITEM_A_UT)
after update on BES_BESTELLING_ITEM
begin
declare bkey number(10); bstate number(1);
CURSOR query IS
select b_key, bstate, request, deliver from
(select
b2.bes_bestellingstate bstate, b1.bes_bestelling_key b_key,
count(distinct b1.bes_bestelling_item_key) request,
count(distinct b2.bes_bestelling_item_key) deliver
from bes_bestelling_item b1,
(select b.bes_bestelling_key, bes_bestellingstate, bes_bestelling_item_key
from bes_bestelling_item b, bes_bestellingstate bs
where bs.BES_BESTELLING_KEY = b.BES_BESTELLING_KEY
and b.bes_bestelling_item_status = 6 and
bes_bestellingstate = 1 ) b2
where b1.bes_bestelling_key = b2.bes_bestelling_key
group by b2.bes_bestellingstate, b1.bes_bestelling_key)
where deliver = request
union all
select b_key, bstate, request, deliver from
(select
b2.bes_bestellingstate bstate, b1.bes_bestelling_key b_key,
count(distinct b1.bes_bestelling_item_key) request,
count(distinct b2.bes_bestelling_item_key) deliver
from bes_bestelling_item b1,
(select b.bes_bestelling_key, bes_bestellingstate, bes_bestelling_item_key
from bes_bestelling_item b, bes_bestellingstate bs
where bs.BES_BESTELLING_KEY = b.BES_BESTELLING_KEY
and b.bes_bestelling_item_status = 4 and
bes_bestellingstate = 2 ) b2
where b1.bes_bestelling_key = b2.bes_bestelling_key
group by b2.bes_bestellingstate, b1.bes_bestelling_key)
where deliver = request;
BEGIN
bkey := 0;
FOR besitem_rec IN query() LOOP
if besitem_rec.bstate = 1 then
update bes_bestelling set bes_bestelling_afgemeld = sysdate,
bes_bestelling_status = 6
where bes_bestelling_key = besitem_rec.b_key;
bkey := besitem_rec.b_key;
end if;
if besitem_rec.bstate = 2 then
update bes_bestelling set bes_bestelling_geaccepteerd = sysdate,
bes_bestelling_status = 4
where bes_bestelling_key = besitem_rec.b_key;
bkey := besitem_rec.b_key;
end if;
END LOOP;
if bkey <> 0 then
DELETE FROM bes_bestellingstate;
end if;
END;
END;
/
*/
#endif
CREATE_TRIGGER(bes_t_bes_cXMLparams_B_IU)
BEFORE INSERT OR UPDATE ON bes_cXMLparams
FOR EACH ROW
BEGIN
UPDATE_PRIMARY_KEY(bes_cXMLparams_key,bes_s_bes_cXMLparams_key);
END;
/
CREATE_TRIGGER(BES_T_BES_BESTELOPDR_XML_A_IU)
AFTER INSERT OR UPDATE ON BES_BESTELOPDR_XML
BEGIN
DELETE FROM BES_BESTELOPDR_XML WHERE BES_BESTELOPDR_XML_DATUM < SYSDATE-7;
END;
/
CREATE_TRIGGER(BES_T_BES_DISC_PARAMS_B_IU)
BEFORE INSERT OR UPDATE ON bes_disc_params
FOR EACH ROW
BEGIN
UPDATE_PRIMARY_KEY(bes_disc_params_key, bes_s_bes_disc_params_key);
END;
/
CREATE_TRIGGER(bes_t_bes_impcatalogus_b_iu)
BEFORE INSERT OR UPDATE ON bes_impcatalogus
FOR EACH ROW
BEGIN
UPDATE_UPPER(bes_impcatalogus_id, bes_impcatalogus_id, bes_impcatalogus);
UPDATE_UPPER(ins_srtdeel_nr, ins_srtdeel_nr, bes_impcatalogus);
END;
/
#endif // BES