
insert into sequpdate_state(updateid,uid) values('fixdashboardalert',4.0);

  create or replace view v_alert_staff_formation as
  SELECT (staff_member.lastname::text || ' '::text) || staff_member.firstname::text AS name,
         CASE
             WHEN now()::date >= staff_formation.planned_date::date THEN 3
             WHEN (now()::date + '3 days'::interval)::date >= staff_formation.planned_date::date THEN 2
             ELSE 1
         END AS level,
     staff_formation.planned_date,
     staff_formation.title,
     staff_member.id AS staff_id
    FROM staff_formation,
     staff_member
   WHERE staff_member.id = staff_formation.staff_id AND staff_formation.status_id = 3 AND staff_formation.planned_date IS NOT null and staff_formation.removed =false and staff_member.removed = false;


   CREATE OR REPLACE VIEW public.v_alert_device_inspection
  AS SELECT device.id,
      device.name,
          CASE
              WHEN now()::date >= device_inspection.planned_date::date THEN 3
              WHEN (now()::date + '3 days'::interval)::date >= device_inspection.planned_date::date THEN 2
              ELSE 1
          END AS level,
      device_inspection.planned_date,
      ''::character varying AS title
     FROM device_inspection,
      device
    WHERE device.removed = false and device_inspection.removed = false AND device.id = device_inspection.device_id AND device_inspection.status_id = 3 AND device_inspection.planned_date IS NOT null ;


    CREATE OR REPLACE VIEW public.v_alert_device_maintenance
      AS SELECT device.id,
          device.name,
              CASE
                  WHEN now()::date >= device_maintenance.planned_date::date THEN 3
                  WHEN (now()::date + '3 days'::interval)::date >= device_maintenance.planned_date::date THEN 2
                  ELSE 1
              END AS level,
          device_maintenance.planned_date,
          ''::character varying AS title
         FROM device_maintenance,
          device
        WHERE device.removed = false and device_maintenance.removed = false AND device.id = device_maintenance.device_id AND device_maintenance.status_id = 3 AND device_maintenance.planned_date IS NOT null ;


alter table document add column upload_date timestamptz NOT NULL DEFAULT now();
alter table document add column valid_until_date timestamptz NOT NULL DEFAULT to_timestamp('29991231235959','YYYYMMDDHH24MISS');
--update document set upload_date = creation_date;
