CREATE OR REPLACE FUNCTION structure_points() RETURNS trigger AS $$
 BEGIN
  DELETE FROM object_points WHERE objectid = new.id AND functionid = 1;
  INSERT INTO object_points (objectid, functionid, points) VALUES (new.id, 1, 
   GREATEST(COALESCE(new.height_architectural, 0.0),COALESCE(new.floors_og,0.0)*10.0)
   + COALESCE(new.costs_construction,0.0)/1000000.0
   + GREATEST(COALESCE(new.area_grossfloor,0.0), COALESCE(new.area_usablefloor))/1000.0
   + CASE WHEN (ARRAY['feature.4.4']::TEXT[] <@ new.feature) THEN 100.0 ELSE 0.0 END
   + (SELECT votepoints FROM object WHERE objectid = new.id) * 11.0);
  RETURN NULL;
 END
$$ LANGUAGE plpgsql;

ALTER TABLE structure DROP TRIGGER structure_points_insert_trigger;
CREATE TRIGGER structure_points_insert_trigger AFTER INSERT ON structure FOR EACH ROW EXECUTE PROCEDURE structure_points();

ALTER TABLE structure DROP TRIGGER structure_points_update_trigger;
CREATE TRIGGER structure_points_update_trigger AFTER UPDATE OF floors_og, height_architectural, feature, area_grossfloor, area_usablefloor, costs_construction ON structure FOR EACH ROW EXECUTE PROCEDURE structure_points();
