DROP VIEW contact_presence;
CREATE VIEW contact_presence AS
SELECT
 distinct me.id as contact, presence.id, presence.name, presence.status
FROM
 presence, contact as me
WHERE
     (me.status != 'deleted')
 AND ((presence.contact > 1 and presence.contact = me.id)
  OR  (presence.department > 1 and presence.department in (select department
                                           from contact_department
                                          where contact=me.id))
  OR  (presence.company > 1 and presence.company in (select company
                                     from department, contact_department
                                    where department.id=contact_department.department and contact_department.contact=me.id)));
GRANT UPDATE, SELECT, INSERT, DELETE ON contact_presence TO astium;

