/**************************************/

CREATE TABLE IF NOT EXISTS public.client
(
    id integer NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ),
    clientname character(80) COLLATE pg_catalog."default",
    contact character(80) COLLATE pg_catalog."default",
    cactive boolean,
    address character(120),
    email character(80),
    phone character(30),
    CONSTRAINT client_pkey PRIMARY KEY (id)
)
TABLESPACE pg_default;

ALTER TABLE IF EXISTS public.client
    OWNER to postgres;

CREATE TABLE IF NOT EXISTS public.lookup
(
    id integer NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ),
    code character(10) COLLATE pg_catalog."default",
    description character(80) COLLATE pg_catalog."default",
    lactive boolean,
    ltype character(20) COLLATE pg_catalog."default",
    CONSTRAINT lookup_pkey PRIMARY KEY (id)
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS public.lookup
    OWNER to postgres;


-- Table: public.job

-- DROP TABLE IF EXISTS public.job;

CREATE TABLE IF NOT EXISTS public.job
(
    id integer NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ),
    code character(10) COLLATE pg_catalog."default",
    description character(80) COLLATE pg_catalog."default",
    jactive boolean,
    CONSTRAINT job_pkey PRIMARY KEY (id)
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS public.job
    OWNER to postgres;

-- Table: public.servicetask

-- DROP TABLE IF EXISTS public.servicetask;

CREATE TABLE IF NOT EXISTS public.servicetask
(
    id integer NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ),
    assignto integer,
    clientid integer,
    code character(10) COLLATE pg_catalog."default",
    description character(80) COLLATE pg_catalog."default",
    sdate date,
    serviceno character(20) COLLATE pg_catalog."default",
    staffid integer,
    status integer,
    CONSTRAINT servicetask_pkey PRIMARY KEY (id)
)
TABLESPACE pg_default;

ALTER TABLE IF EXISTS public.servicetask
    OWNER to postgres;


-- Table: public.staffwork

-- DROP TABLE IF EXISTS public.staffwork;

CREATE TABLE IF NOT EXISTS public.staffwork
(
    id integer NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ),
    description character(80) COLLATE pg_catalog."default",
    sactive boolean,
    staffid integer,
    startdate date,
    CONSTRAINT staffwork_pkey PRIMARY KEY (id)
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS public.staffwork
 OWNER to postgres;

-- Table: public.staffworkdetail

-- DROP TABLE IF EXISTS public.staffworkdetail;

CREATE TABLE IF NOT EXISTS public.staffworkdetail
(
    id integer NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ),
    jobid integer,
    sactive boolean,
    starttime timestamp without time zone,
    stoptime timestamp without time zone,
    task character(80) COLLATE pg_catalog."default",
    swid integer,
    CONSTRAINT staffworkdetail_pkey PRIMARY KEY (id)
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS public.staffworkdetail
    OWNER to postgres;

/******************************************/
ALTER TABLE servicetask
ADD FOREIGN KEY (clientid) REFERENCES client(id);

ALTER TABLE servicetask
ADD FOREIGN KEY (staffid) REFERENCES staff(id);

ALTER TABLE servicetask
ADD FOREIGN KEY (assignto) REFERENCES staff(id);

ALTER TABLE staffwork
ADD FOREIGN KEY (staffid) REFERENCES staff(id);

ALTER TABLE staffworkdetail
ADD FOREIGN KEY (swid) REFERENCES staffwork(id);

ALTER TABLE staffworkdetail
ADD FOREIGN KEY (jobid) REFERENCES job(id);

/************************************/

CREATE OR REPLACE FUNCTION usp_search_user (
	iemail character varying,
	ifirstname character varying,
	ilastname character varying)
    RETURNS TABLE(id integer, fistname character varying, lastname character varying, email character varying, stype integer, sactive boolean, srole integer, spassword character varying) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
	return query SELECT
		e.id, 
        e.firstname,
        e.lastname,
        e.email,
        e.stype,
        e.sactive,
		e.srole,
		e.spassword
       
       

	FROM  staff e		
	WHERE (iemail = '' or e.email ilike iemail || '%')
	and (ilastname = '' or e.lastname ilike ilastname || '%')
	and (ifirstname = '' or e.firstname ilike ifirstname || '%');

end; 
$BODY$;

CREATE OR REPLACE FUNCTION usp_staffwork_detail(
	ifromdate date,
	itodate date,
    istaffid integer)
    RETURNS TABLE(id integer, staffid integer, description character varying(80),
    startdate date, sactive boolean, did integer, starttime timestamp, stoptime timestamp, 
    task character varying(80)) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
	return query SELECT
		w.id, 
        w.staffid,
        w.description,
        w.startdate,
        w.sactive,
        d.id as did,
        d.starttime,
        d.stoptime,
        d.task
		
	FROM  staffwork w,	
    staffworkdetail d
    where w.id = d.swid
    and (istaffid = 0 or w.staffid = istaffid)
    and w.startdate >= ifromdate 
    and w.startdate <= itodate
    order by id, did;
 

end; 
$BODY$;


CREATE OR REPLACE FUNCTION usp_servicetask_sel(
	ifromdate date,
	itodate date,
    istaffid integer,
    iassignto integer,
    iclientid integer
    )
    RETURNS TABLE(id integer,
    staffid integer, 
    sdate date,
    description character varying(80),
    code character varying(10),
    status integer,
    assignTo integer,
    clientid integer,
    serviceno character varying(20)) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000


AS $BODY$
begin
	return query SELECT
        s.id, 
        s.staffid,
        s.sdate,       
        s.description,
        s.code,
        s.status,
        s.assignto,
        s.clientid,
        s."serviceNo"
	FROM  servicetask s
    
    where (istaffid < 1 or s.staffid = istaffid)
    and (iassignto < 1 or s.assignto = iassignto)
    and (iclientid < 1 or s.clientid = iclientid)
    and s.sdate >= ifromdate 
    and s.sdate <= itodate
    
    ;
end; 
$BODY$;
