-- trigger on NEWS_USER_tracking
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[NEWS_USER_tracking_insert_trigger] ON [dbo].[NEWS_USER_tracking] FOR INSERT AS
BEGIN
	UPDATE dbo.NEWS set NEW_ACTIVE = NEW_ACTIVE where NEW_ID = (select NEU_ID_NEW from INSERTED)
END;
GO

CREATE TRIGGER [dbo].[NEWS_USER__tracking_update_trigger] ON [dbo].[NEWS_USER_tracking] FOR UPDATE AS
BEGIN
  UPDATE dbo.NEWS set NEW_ACTIVE = NEW_ACTIVE where NEW_ID in (select NEU_ID_NEW from INSERTED)
END;
GO
-- trigger on CUSTOMER_USER_tracking

CREATE TRIGGER [dbo].[CUSTOMER_USER_tracking_insert_trigger] ON [dbo].[CUSTOMER_USER_tracking] FOR INSERT AS
BEGIN
	UPDATE dbo.CUSTOMER set CUS_ACTIVE = CUS_ACTIVE 
		where CUS_GUID in (select CUU_GUID_CUS from INSERTED);
		
	UPDATE dbo.CUSTOMER_ORGANIZATION set CUO_EXID_CUS = CUO_EXID_CUS 
		where CUO_GUID_CUS in (select CUU_GUID_CUS from INSERTED);
	
	UPDATE dbo.CUSTOMER_RELATION set CUR_GUID_CUS2 = CUR_GUID_CUS2 
		where CUR_GUID_CUS1 in (select CUU_GUID_CUS from INSERTED);	
		
	UPDATE dbo.CUSTOMER_RELATION set CUR_GUID_CUS2 = CUR_GUID_CUS2 
		where CUR_GUID_CUS2 in (select CUU_GUID_CUS from INSERTED);		
		
	UPDATE dbo.ADDRESS set ADR_ACTIVE = ADR_ACTIVE 
		where ADR_GUID_CUS in (select CUU_GUID_CUS from INSERTED);			
END;	
GO

CREATE TRIGGER [dbo].[CUSTOMER_USER__tracking_update_trigger] ON [dbo].[CUSTOMER_USER_tracking] FOR UPDATE AS
BEGIN
	UPDATE dbo.CUSTOMER set CUS_ACTIVE = CUS_ACTIVE 
		where CUS_GUID in (select CUU_GUID_CUS from INSERTED);

	UPDATE dbo.CUSTOMER_ORGANIZATION set CUO_EXID_CUS = CUO_EXID_CUS 
		where CUO_GUID_CUS in (select CUU_GUID_CUS from INSERTED);
	
	UPDATE dbo.CUSTOMER_RELATION set CUR_GUID_CUS2 = CUR_GUID_CUS2 
		where CUR_GUID_CUS1 in (select CUU_GUID_CUS from INSERTED);	
		
	UPDATE dbo.CUSTOMER_RELATION set CUR_GUID_CUS2 = CUR_GUID_CUS2 
		where CUR_GUID_CUS2 in (select CUU_GUID_CUS from INSERTED);		
		
	UPDATE dbo.ADDRESS set ADR_ACTIVE = ADR_ACTIVE 
		where ADR_GUID_CUS in (select CUU_GUID_CUS from INSERTED);
END;
GO

CREATE TRIGGER [dbo].[USER_ORGANIZATION_tracking_insert_trigger] ON [dbo].[USER_ORGANIZATION_tracking] FOR INSERT AS
BEGIN
	UPDATE dbo.ORGANIZATION set ORG_ACTIVE = ORG_ACTIVE 
		where ORG_ID = (select USO_ID_ORG from INSERTED);
		
	UPDATE dbo.VISIT_DEFINITION set VID_ACTIVE = VID_ACTIVE 
		where VID_ID_ORG in (select USO_ID_ORG from INSERTED);		
		
	UPDATE dbo.TASK_DEFINITION set TAD_ACTIVE = TAD_ACTIVE 
		where TAD_VISIT_DEFINITION in (select VID_ID from INSERTED i 
		                              inner join dbo.VISIT_DEFINITION v on i.USO_ID_ORG = v.VID_ID_ORG);	
		
	UPDATE dbo.TASK_DEFINITION_CUSTOMER set TDC_ID_TAD = TDC_ID_TAD 
		where TDC_ID_TAD in (select TAD_ID from INSERTED i 		
		                              inner join dbo.VISIT_DEFINITION v on i.USO_ID_ORG = v.VID_ID_ORG
		                              inner join dbo.TASK_DEFINITION on TAD_VISIT_DEFINITION = VID_ID);	
		                              
	UPDATE dbo.TASK_DEFINITION_PRODUCT set TDP_ID_PRO = TDP_ID_PRO 
		where TDP_ID_TAD in (select TAD_ID from INSERTED i 		
		                              inner join dbo.VISIT_DEFINITION v on i.USO_ID_ORG = v.VID_ID_ORG
		                              inner join dbo.TASK_DEFINITION on TAD_VISIT_DEFINITION = VID_ID);	
		   
	UPDATE dbo.TASK_DEFINITION_QUESTION set TDQ_ACTIVE = TDQ_ACTIVE 
		where TDQ_ID_TAD in (select TAD_ID from INSERTED i 		
		                              inner join dbo.VISIT_DEFINITION v on i.USO_ID_ORG = v.VID_ID_ORG
		                              inner join dbo.TASK_DEFINITION on TAD_VISIT_DEFINITION = VID_ID);	

	UPDATE dbo.TASK_DEFINITION_CATALOG set TDC_ACTIVE = TDC_ACTIVE 
		where TDC_ID in (select TDQ_ASWER_DICT_TYPE from INSERTED i 		
		                              inner join dbo.VISIT_DEFINITION v on i.USO_ID_ORG = v.VID_ID_ORG
		                              inner join dbo.TASK_DEFINITION on TAD_VISIT_DEFINITION = VID_ID
		                              inner join dbo.TASK_DEFINITION_QUESTION on TDQ_ID_TAD = TAD_ID);	

	UPDATE dbo.PRODUCT_ORGANIZATION set POR_ID_PRO = POR_ID_PRO 
		where POR_ID_ORG in (select USO_ID_ORG from INSERTED);		                              
		
	UPDATE dbo.PRODUCT set PRO_ACTIVE = PRO_ACTIVE 
		where PRO_ID in (select POR_ID_PRO 
								from INSERTED
								inner join dbo.PRODUCT_ORGANIZATION on POR_ID_ORG = USO_ID_ORG);		
	
	                              		                              		                              		                              			
END;
GO

CREATE TRIGGER [dbo].[USER_ORGANIZATION__tracking_update_trigger] ON [dbo].[USER_ORGANIZATION_tracking] FOR UPDATE AS
BEGIN
  	UPDATE dbo.ORGANIZATION set ORG_ACTIVE = ORG_ACTIVE 
		where ORG_ID in (select USO_ID_ORG from INSERTED);
		
	UPDATE dbo.VISIT_DEFINITION set VID_ACTIVE = VID_ACTIVE 
		where VID_ID_ORG in (select USO_ID_ORG from INSERTED);		
		
	UPDATE dbo.TASK_DEFINITION set TAD_ACTIVE = TAD_ACTIVE 
		where TAD_VISIT_DEFINITION in (select VID_ID from INSERTED i 
		                              inner join dbo.VISIT_DEFINITION v on i.USO_ID_ORG = v.VID_ID_ORG);	
		
	UPDATE dbo.TASK_DEFINITION_CUSTOMER set TDC_ID_TAD = TDC_ID_TAD 
		where TDC_ID_TAD in (select TAD_ID from INSERTED i 		
		                              inner join dbo.VISIT_DEFINITION v on i.USO_ID_ORG = v.VID_ID_ORG
		                              inner join dbo.TASK_DEFINITION on TAD_VISIT_DEFINITION = VID_ID);	
		                              
	UPDATE dbo.TASK_DEFINITION_PRODUCT set TDP_ID_PRO = TDP_ID_PRO 
		where TDP_ID_TAD in (select TAD_ID from INSERTED i 		
		                              inner join dbo.VISIT_DEFINITION v on i.USO_ID_ORG = v.VID_ID_ORG
		                              inner join dbo.TASK_DEFINITION on TAD_VISIT_DEFINITION = VID_ID);	
		   
	UPDATE dbo.TASK_DEFINITION_QUESTION set TDQ_ACTIVE = TDQ_ACTIVE 
		where TDQ_ID_TAD in (select TAD_ID from INSERTED i 		
		                              inner join dbo.VISIT_DEFINITION v on i.USO_ID_ORG = v.VID_ID_ORG
		                              inner join dbo.TASK_DEFINITION on TAD_VISIT_DEFINITION = VID_ID);	

	UPDATE dbo.TASK_DEFINITION_CATALOG set TDC_ACTIVE = TDC_ACTIVE 
		where TDC_ID in (select TDQ_ASWER_DICT_TYPE from INSERTED i 		
		                              inner join dbo.VISIT_DEFINITION v on i.USO_ID_ORG = v.VID_ID_ORG
		                              inner join dbo.TASK_DEFINITION on TAD_VISIT_DEFINITION = VID_ID
		                              inner join dbo.TASK_DEFINITION_QUESTION on TDQ_ID_TAD = TAD_ID);	

	UPDATE dbo.PRODUCT_ORGANIZATION set POR_ID_PRO = POR_ID_PRO 
		where POR_ID_ORG in (select USO_ID_ORG from INSERTED);		                              
		
	UPDATE dbo.PRODUCT set PRO_ACTIVE = PRO_ACTIVE 
		where PRO_ID in (select POR_ID_PRO 
								from INSERTED
								inner join dbo.PRODUCT_ORGANIZATION on POR_ID_ORG = USO_ID_ORG);		
	
	
END;
GO