/* HA Plus 9.01,9.10,9.11,9.20,9.21,9.30 */
/* Version Oracle */


/*========================================================================*/
/*========   TRIGGERS DE MAJ DE DATEMODI ET DE LA TABLE TRANSACT   =======*/
/*========================================================================*/


/*===========TRIGGER SUR ARTICLES======*/

CREATE OR REPLACE TRIGGER TRIG_ARTICLES_DATEMODI
BEFORE INSERT OR UPDATE ON ARTICLES
REFERENCING NEW AS NEW OLD AS OLD FOR EACH ROW
DECLARE

BEGIN
    :NEW.DATEMODI := to_char(sysdate,'YYYYMMDD');
END;


/*===========TRIGGER SUR FOURNISS======*/

CREATE OR REPLACE TRIGGER TRIG_FOURNISS_DATEMODI
BEFORE INSERT OR UPDATE ON FOURNISS
REFERENCING NEW AS NEW OLD AS OLD FOR EACH ROW
DECLARE

BEGIN
    :NEW.DATEMODI := to_char(sysdate,'YYYYMMDD');
END;


/*===========TRIGGER SUR ARTFOURN======*/

CREATE OR REPLACE TRIGGER TRIG_ARTFOURN_DATEMODI
BEFORE INSERT OR UPDATE ON ARTFOURN
REFERENCING NEW AS NEW OLD AS OLD FOR EACH ROW
DECLARE

BEGIN
    :NEW.DATEMODI := to_char(sysdate,'YYYYMMDD');
END;


/*===========TRIGGER SUR DA_ENTET======*/

CREATE OR REPLACE TRIGGER TRIG_DA_ENT_DATEMODI
AFTER INSERT OR UPDATE OR DELETE
ON DA_ENTET 
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
	n number;  
BEGIN
	 If INSERTING or UPDATING then	
   	 	UPDATE DA_PLANN SET DATEMODI=to_char(sysdate,'YYYYMMDD') WHERE :NEW.NO_DA=DA_PLANN.NO_DA   ;
	 end if;
	 if deleting  then
	 	select count(*) into n from TRANSACT where :OLD.NO_DA=IDNUM1;
		if n=0 then
	 	   INSERT INTO TRANSACT (TYPEDOC,TYPETRANS,IDNUM1) VALUES ('DA','DELETE',:OLD.NO_DA);
		end if;   
	 end if; 
END ;


/*===========TRIGGER SUR DA_LIGNE======*/

CREATE OR REPLACE TRIGGER TRIG_DA_LIGNE_DATEMODI
AFTER INSERT OR UPDATE OR DELETE
ON DA_LIGNE 
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
	n number;  
BEGIN
	 If INSERTING or UPDATING then	
   	 	UPDATE DA_PLANN SET DATEMODI=to_char(sysdate,'YYYYMMDD') WHERE :NEW.NO_DA=DA_PLANN.NO_DA   ;
	 end if;
	 if deleting  then
	 	select count(*) into n from TRANSACT where :OLD.NO_DA=IDNUM1;
		if n=0 then
	 	   INSERT INTO TRANSACT (TYPEDOC,TYPETRANS,IDNUM1) VALUES ('DA','DELETE',:OLD.NO_DA);
		end if;   
	 end if; 
	 
END ;


/*===========TRIGGER SUR DA_PLANN======*/

CREATE OR REPLACE TRIGGER TRIG_DA_PLANN_DATEMODI
BEFORE INSERT OR UPDATE OR DELETE
ON DA_PLANN 
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
	n number;  
BEGIN
	 If INSERTING or UPDATING then	
   	 	:NEW.DATEMODI := to_char(sysdate,'YYYYMMDD');
	 end if;
	 if deleting  then
	 	select count(*) into n from TRANSACT where :OLD.NO_DA=IDNUM1;
		if n=0 then
	 	   INSERT INTO TRANSACT (TYPEDOC,TYPETRANS,IDNUM1) VALUES ('DA','DELETE',:OLD.NO_DA);
		end if;   
	 end if; 
	 
END ;


/*===========TRIGGER SUR CA_ENTET======*/

CREATE OR REPLACE TRIGGER TRIG_CA_ENTET_DATEMODI
AFTER INSERT OR UPDATE OR DELETE
ON CA_ENTET 
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
	n number;  
BEGIN
	 If INSERTING or UPDATING then	
   	 	UPDATE CA_PL_CT SET DATEMODI=to_char(sysdate,'YYYYMMDD') WHERE :NEW.NO_CDE=CA_PL_CT.NO_CA   ;
	 end if;
	 if deleting  then
	 	select count(*) into n from TRANSACT where :OLD.NO_CDE=IDNUM1;
		if n=0 then
	 	   INSERT INTO TRANSACT (TYPEDOC,TYPETRANS,IDNUM1) VALUES ('CA','DELETE',:OLD.NO_CDE);
		end if;   
	 end if; 
END ;


/*===========TRIGGER SUR CA_LIGNE======*/

CREATE OR REPLACE TRIGGER TRIG_CA_LIGNE_DATEMODI
AFTER INSERT OR UPDATE OR DELETE
ON CA_LIGNE 
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
	n number;  
BEGIN
	 If INSERTING or UPDATING then	
   	 	UPDATE CA_PL_CT SET DATEMODI=to_char(sysdate,'YYYYMMDD') WHERE :NEW.NO_CDE=CA_PL_CT.NO_CA   ;
	 end if;
	 if deleting  then
	 	select count(*) into n from TRANSACT where :OLD.NO_CDE=IDNUM1;
		if n=0 then
	 	   INSERT INTO TRANSACT (TYPEDOC,TYPETRANS,IDNUM1) VALUES ('CA','DELETE',:OLD.NO_CDE);
		end if;   
	 end if; 
	 
END ;


/*===========TRIGGER SUR CA_PL_CT======*/

CREATE OR REPLACE TRIGGER TRIG_CA_PL_CT_DATEMODI
BEFORE INSERT OR UPDATE OR DELETE
ON CA_PL_CT 
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
	n number;  
BEGIN
	 If INSERTING or UPDATING then	
   	 	:NEW.DATEMODI := to_char(sysdate,'YYYYMMDD');
	 end if;
	 if deleting  then
	 	select count(*) into n from TRANSACT where :OLD.NO_CA=IDNUM1;
		if n=0 then
	 	   INSERT INTO TRANSACT (TYPEDOC,TYPETRANS,IDNUM1,DATETRANS) VALUES ('CA','DELETE',:OLD.NO_CA,to_char(sysdate,'YYYYMMDD'));
		end if;   
	 end if; 
	 
END ;


/*===========TRIGGER SUR HCA_ENTE======*/

CREATE OR REPLACE TRIGGER TRIG_HCA_ENTE_DATEMODI
AFTER INSERT OR UPDATE OR DELETE
ON HCA_ENTE 
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
	n number;  
BEGIN
	 If INSERTING or UPDATING then	
   	 	UPDATE HCA_PLCT SET DATEMODI=to_char(sysdate,'YYYYMMDD') WHERE :NEW.NO_CDE=HCA_PLCT.NUM_CDE   ;
	 end if;
	 if deleting  then
	 	select count(*) into n from TRANSACT where :OLD.NO_CDE=IDNUM1;
		if n=0 then
	 	   INSERT INTO TRANSACT (TYPEDOC,TYPETRANS,IDNUM1,DATETRANS) VALUES ('CA','DELETE',:OLD.NO_CDE,to_char(sysdate,'YYYYMMDD'));
		end if;   
	 end if; 
END ;


/*===========TRIGGER SUR HCA_LIGN======*/

CREATE OR REPLACE TRIGGER TRIG_HCA_LIGN_DATEMODI
AFTER INSERT OR UPDATE OR DELETE
ON HCA_LIGN 
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
	n number;  
BEGIN
	 If INSERTING or UPDATING then	
   	 	UPDATE HCA_PLCT SET DATEMODI=to_char(sysdate,'YYYYMMDD') WHERE :NEW.NUMCDE=HCA_PLCT.NUM_CDE   ;
	 end if;
	 if deleting  then
	 	select count(*) into n from TRANSACT where :OLD.NUMCDE=IDNUM1;
		if n=0 then
	 	   INSERT INTO TRANSACT (TYPEDOC,TYPETRANS,IDNUM1,DATETRANS) VALUES ('CA','DELETE',:OLD.NUMCDE,to_char(sysdate,'YYYYMMDD'));
		end if;   
	 end if; 
	 
END ;


/*===========TRIGGER SUR HCA_PLCT======*/

CREATE OR REPLACE TRIGGER TRIG_HCA_PLCT_DATEMODI
BEFORE INSERT OR UPDATE OR DELETE
ON HCA_PLCT 
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
	n number;  
BEGIN
	 If INSERTING or UPDATING then	
   	 	:NEW.DATEMODI := to_char(sysdate,'YYYYMMDD');
	 end if;
	 if deleting  then
	 	select count(*) into n from TRANSACT where :OLD.NUM_CDE=IDNUM1;
		if n=0 then
	 	   INSERT INTO TRANSACT (TYPEDOC,TYPETRANS,IDNUM1,DATETRANS) VALUES ('CA','DELETE',:OLD.NUM_CDE,to_char(sysdate,'YYYYMMDD'));
		end if;   
	 end if; 
	 
END ;


/*===========TRIGGER SUR BR_RECEP======*/

CREATE OR REPLACE TRIGGER TRIG_BR_RECEP_DATEMODI
AFTER INSERT OR UPDATE OR DELETE
ON BR_RECEP 
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
	n number;  
BEGIN
	 If INSERTING or UPDATING then	
   	 	UPDATE BR_LIGNE SET DATEMODI=to_char(sysdate,'YYYYMMDD') WHERE :NEW.NUM_BR=BR_LIGNE.NUM_BR   ;
	 end if;
	 if deleting  then
	 	select count(*) into n from TRANSACT where :OLD.NUM_BR=IDNUM1;
		if n=0 then
	 	   INSERT INTO TRANSACT (TYPEDOC,TYPETRANS,IDNUM1) VALUES ('BR','DELETE',:OLD.NUM_BR);
		end if;   
	 end if; 
END ;


/*===========TRIGGER SUR BR_LIGNE======*/

CREATE OR REPLACE TRIGGER TRIG_BR_LIGNE_DATEMODI
BEFORE INSERT OR UPDATE OR DELETE
ON BR_LIGNE 
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
	n number;  
BEGIN
	 If INSERTING or UPDATING then	
   	 	:NEW.DATEMODI := to_char(sysdate,'YYYYMMDD');
	 end if;
	 if deleting  then
	 	select count(*) into n from TRANSACT where :OLD.NUM_BR=IDNUM1;
		if n=0 then
	 	   INSERT INTO TRANSACT (TYPEDOC,TYPETRANS,IDNUM1) VALUES ('BR','DELETE',:OLD.NUM_BR);
		end if;   
	 end if; 
	 
END ;


/*===========TRIGGER SUR FAC_ENT======*/

CREATE OR REPLACE TRIGGER TRIG_FAC_RECEP_DATEMODI
AFTER INSERT OR UPDATE OR DELETE
ON FAC_ENT
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
	n number;  
BEGIN
	 If INSERTING or UPDATING then	
   	 	UPDATE FAC_LIG SET DATEMODI=to_char(sysdate,'YYYYMMDD') WHERE :NEW.NUM_FACT=FAC_LIG.NUM_FACT   ;
	 end if;
	 if deleting  then
	 	select count(*) into n from TRANSACT where :OLD.NUM_FACT=IDNUM1;
		if n=0 then
	 	   INSERT INTO TRANSACT (TYPEDOC,TYPETRANS,IDNUM1) VALUES ('FAC','DELETE',:OLD.NUM_FACT);
		end if;   
	 end if; 
END ;


/*===========TRIGGER SUR FAC_LIG======*/

CREATE OR REPLACE TRIGGER TRIG_FAC_LIG_DATEMODI
BEFORE INSERT OR UPDATE OR DELETE
ON FAC_LIG 
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
	n number;  
BEGIN
	 If INSERTING or UPDATING then	
   	 	:NEW.DATEMODI := to_char(sysdate,'YYYYMMDD');
	 end if;
	 if deleting  then
	 	select count(*) into n from TRANSACT where :OLD.NUM_FACT=IDNUM1;
		if n=0 then
	 	   INSERT INTO TRANSACT (TYPEDOC,TYPETRANS,IDNUM1,DATETRANS) VALUES ('FAC','DELETE',:OLD.NUM_FACT,to_char(sysdate,'YYYYMMDD'));
		end if;   
	 end if; 
	 
END ;

/*===========TRIGGER SUR EXPENTET======*/

CREATE OR REPLACE TRIGGER TRIG_EXPENTET_DATEMODI
BEFORE INSERT OR UPDATE OR DELETE
ON EXPENTET
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
	n number;  
BEGIN
	 If INSERTING or UPDATING then	
   	 	UPDATE DETCOLIS SET DATEMODI=to_char(sysdate,'YYYYMMDD') WHERE :NEW.NUM_EXP=DETCOLIS.NUM_EXP   ;
	 end if;
	 if deleting  then
	 	select count(*) into n from TRANSACT where :OLD.NUM_EXP=IDNUM1;
		if n=0 then
	 	   INSERT INTO TRANSACT (TYPEDOC,TYPETRANS,IDNUM1,DATETRANS) VALUES ('EXP','DELETE',:OLD.NUM_EXP,to_char(sysdate,'YYYYMMDD'));
		end if;   
	 end if; 
	 
END ;

/*===========TRIGGER SUR EXPCOLIS======*/

CREATE OR REPLACE TRIGGER TRIG_EXPCOLIS_DATEMODI
BEFORE INSERT OR UPDATE OR DELETE
ON EXPCOLIS
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
	n number;  
BEGIN
	 If INSERTING or UPDATING then	
   	 	UPDATE DETCOLIS SET DATEMODI=to_char(sysdate,'YYYYMMDD') WHERE :NEW.NUM_EXP=DETCOLIS.NUM_EXP   ;
	 end if;
	 if deleting  then
	 	select count(*) into n from TRANSACT where :OLD.NUM_EXP=IDNUM1;
		if n=0 then
	 	   INSERT INTO TRANSACT (TYPEDOC,TYPETRANS,IDNUM1,DATETRANS) VALUES ('EXP','DELETE',:OLD.NUM_EXP,to_char(sysdate,'YYYYMMDD'));
		end if;   
	 end if; 
	 
END ;


/*===========TRIGGER SUR DETCOLIS======*/

CREATE OR REPLACE TRIGGER TRIG_DETCOLIS_DATEMODI
BEFORE INSERT OR UPDATE OR DELETE
ON DETCOLIS
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
	n number;  
BEGIN
	 If INSERTING or UPDATING then	
   	 	:NEW.DATEMODI := to_char(sysdate,'YYYYMMDD');
	 end if;
	 if deleting  then
	 	select count(*) into n from TRANSACT where :OLD.NUM_EXP=IDNUM1;
		if n=0 then
	 	   INSERT INTO TRANSACT (TYPEDOC,TYPETRANS,IDNUM1,DATETRANS) VALUES ('EXP','DELETE',:OLD.NUM_EXP,to_char(sysdate,'YYYYMMDD'));
		end if;   
	 end if; 
	 
END ;



/*===========TRIGGER SUR SARTICLE (Stock)======*/

CREATE OR REPLACE TRIGGER TRIG_SARTICLE_DATEMODI
BEFORE INSERT OR UPDATE 
ON SARTICLE
REFERENCING NEW AS NEW OLD AS OLD 
FOR EACH ROW
DECLARE

BEGIN
    :NEW.DATEMODI := to_char(sysdate,'YYYYMMDD');
END;


    /*===========TRIGGER SUR SSTOCKLO (Stock Local)======*/

CREATE OR REPLACE TRIGGER TRIG_SSTOCKLO_DATEMODI
BEFORE INSERT OR UPDATE 
ON SSTOCKLO
REFERENCING NEW AS NEW OLD AS OLD 
FOR EACH ROW
DECLARE

BEGIN
    :NEW.DATEMODI := to_char(sysdate,'YYYYMMDD');
END;


    /*===========TRIGGER SUR SMOUVEME (Mouvements)=*/


CREATE OR REPLACE TRIGGER TRIG_SMOUVEME_DATEMODI
BEFORE INSERT OR UPDATE 
ON SMOUVEME
REFERENCING NEW AS NEW OLD AS OLD 
FOR EACH ROW
DECLARE

BEGIN
    :NEW.DATEMODI := to_char(sysdate,'YYYYMMDD');
END;



/*==========COMMIT==========*/
COMMIT;

