1 -- @tag: delete_cvars_on_trans_deletion_add_shipto
 
   2 -- @description: Löschen von benutzerdefinierten Variablen via Triggerfunktionen auch für shipto
 
   3 -- @depends: delete_cvars_on_trans_deletion delete_cvars_on_trans_deletion_fix1
 
   5 -- 1.6 Alle benutzerdefinierten Variablen löschen, für die es keine
 
   6 -- Einträge in shipto mehr gibt.
 
   7 DELETE FROM custom_variables WHERE id IN
 
   8   (SELECT cv.id FROM custom_variables cv LEFT JOIN custom_variable_configs cvc ON (cv.config_id = cvc.id)
 
   9    WHERE module LIKE 'ShipTo'
 
  10      AND NOT EXISTS (SELECT shipto_id FROM shipto WHERE shipto_id = cv.trans_id));
 
  13 -- 2.2. Nun die Funktionen, die als Trigger aufgerufen wird und die
 
  14 -- entscheidet, wie genau zu löschen ist:
 
  15 CREATE OR REPLACE FUNCTION delete_custom_variables_trigger()
 
  18     IF (TG_TABLE_NAME IN ('orderitems', 'delivery_order_items', 'invoice')) THEN
 
  19       PERFORM delete_custom_variables_with_sub_module('IC', TG_TABLE_NAME, old.id);
 
  22     IF (TG_TABLE_NAME = 'parts') THEN
 
  23       PERFORM delete_custom_variables_with_sub_module('IC', '', old.id);
 
  26     IF (TG_TABLE_NAME IN ('customer', 'vendor')) THEN
 
  27       PERFORM delete_custom_variables_with_sub_module('CT', '', old.id);
 
  30     IF (TG_TABLE_NAME = 'contacts') THEN
 
  31       PERFORM delete_custom_variables_with_sub_module('Contacts', '', old.cp_id);
 
  34     IF (TG_TABLE_NAME = 'project') THEN
 
  35       PERFORM delete_custom_variables_with_sub_module('Projects', '', old.id);
 
  38     IF (TG_TABLE_NAME = 'shipto') THEN
 
  39       PERFORM delete_custom_variables_with_sub_module('ShipTo', '', old.shipto_id);
 
  46 -- 3. Die eigentlichen Trigger erstellen:
 
  49 DROP TRIGGER IF EXISTS shipto_delete_custom_variables_after_deletion ON shipto;
 
  51 CREATE TRIGGER shipto_delete_custom_variables_after_deletion
 
  52 AFTER DELETE ON shipto
 
  53 FOR EACH ROW EXECUTE PROCEDURE delete_custom_variables_trigger();