create or replace procedure raise_application_error(Code numeric, Message character varying)
language plpgsql
as
$$
begin
  raise exception '%', message;
end;
$$

create or replace function dbo.truncate(Value timestamp without time zone)
  returns timestamp without time zone
  immutable parallel safe
return date_trunc('day', Value); 

create or replace function dbo.truncate(Value timestamp with time zone)
  returns timestamp without time zone
  immutable parallel safe
return date_trunc('day', Value); 

create or replace function dbo.bom(Value timestamp with time zone)
  returns timestamp without time zone
  immutable parallel safe
return date_trunc('month', Value);

create or replace function dbo.bom(Value timestamp without time zone)
  returns timestamp without time zone
  immutable parallel safe
return date_trunc('month', Value);

create or replace function dbo.eom(Value timestamp without time zone)
  returns timestamp without time zone
  immutable parallel safe
return date_trunc('month', Value) + interval '1 month' - interval '1 day';

create or replace function dbo.eom(Value timestamp with time zone)
  returns timestamp without time zone
  immutable parallel safe
return date_trunc('month', Value) + interval '1 month' - interval '1 day';

create or replace function dbo.boq(Value timestamp with time zone)
  returns timestamp without time zone
  immutable parallel safe
return date_trunc('quarter', Value);

create or replace function dbo.boq(Value timestamp without time zone)
  returns timestamp without time zone
  immutable parallel safe
return date_trunc('quarter', Value);

create or replace function dbo.eoq(Value timestamp with time zone)
  returns timestamp without time zone
  immutable parallel safe
return date_trunc('quarter', Value) + interval '3 month' - interval '1 day';

create or replace function dbo.eoq(Value timestamp without time zone)
  returns timestamp without time zone
  immutable parallel safe
return date_trunc('quarter', Value) + interval '3 month' - interval '1 day';

create or replace function dbo.boy(Value timestamp with time zone)
  returns timestamp without time zone
  immutable parallel safe
return date_trunc('year', Value);

create or replace function dbo.boy(Value timestamp without time zone)
  returns timestamp without time zone
  immutable parallel safe
return date_trunc('year', Value);

create or replace function dbo.eoy(Value timestamp with time zone)
  returns timestamp without time zone
  immutable parallel safe
return date_trunc('year', Value) + interval '1 year' - interval '1 day';

create or replace function dbo.eoy(Value timestamp without time zone)
  returns timestamp without time zone
  immutable parallel safe
return date_trunc('year', Value) + interval '1 year' - interval '1 day';

create or replace function dbo.day(Value timestamp without time zone)
  returns integer
  immutable parallel safe
return extract(day from Value);

create or replace function dbo.day(Value timestamp with time zone)
  returns integer
  immutable parallel safe
return extract(day from Value);

create or replace function dbo.month(Value timestamp without time zone)
  returns integer
  immutable parallel safe
return extract(month from Value);

create or replace function dbo.month(Value timestamp with time zone)
  returns integer
  immutable parallel safe
return extract(month from Value);

create or replace function dbo.quarter(Value timestamp without time zone)
  returns integer
  immutable parallel safe
return extract(quarter from Value);

create or replace function dbo.quarter(Value timestamp with time zone)
  returns integer
  immutable parallel safe
return extract(quarter from Value);

create or replace function dbo.year(Value timestamp without time zone)
  returns integer
  immutable parallel safe
return extract(year from Value);

create or replace function dbo.year(Value timestamp with time zone)
  returns integer
  immutable parallel safe
return extract(year from Value);

create or replace function dbo.WeekOfMonth(Value timestamp with time zone)
  returns integer
  immutable parallel safe
return extract(week from Value) - extract(week from date_trunc('month', Value)) + 1;

create or replace function dbo.WeekOfMonth(Value timestamp without time zone)
  returns integer
  immutable parallel safe
return extract(week from Value) - extract(week from date_trunc('month', Value)) + 1;

create or replace function dbo.WeekOfYear(Value timestamp with time zone)
  returns integer
  immutable parallel safe
return extract(week from Value);

create or replace function dbo.WeekOfYear(Value timestamp without time zone)
  returns integer
  immutable parallel safe
return extract(week from Value);

create or replace function dbo.d4000()
  returns timestamp without time zone
  immutable parallel safe
return to_date('12.12.4000', 'DD.MM.YYYY')::timestamp

create or replace function dbo.IsNumeric(Value varchar)
  returns integer
  immutable parallel safe
return (Value ~ '^\d+(\.\d+)?$')::integer

create or replace function dbo.NewId()
  returns varchar
return uuid_generate_v4()::text;

create or replace function dbo.today()
  returns timestamp without time zone
  stable parallel safe
return date_trunc('day', current_timestamp);

create or replace function dbo.now()
  returns timestamp without time zone
  stable parallel safe
return current_timestamp;

create or replace function dbo.sysdate()
  returns timestamp without time zone
  stable parallel safe
return date_trunc('second', clock_timestamp());

create or replace procedure dbo.s_set_context(Name varchar, Value varchar)
  language plpgsql
as 
$body$ 
begin
  perform set_config('omega.' || Name, Value, false);
end;
$body$
;

create or replace function dbo.s_get_context(vName varchar)
  returns varchar
  stable parallel safe
return current_setting('omega.' || vName);

create or replace function dbo.s_get_context_num(vName varchar)
  returns bigint
  stable parallel safe
return cast(current_setting('omega.' || vName) as bigint);

create or replace function dbo.s_get_context_date(vName varchar)
  returns timestamp without time zone
  stable parallel safe
return cast(current_setting('omega.' || vName) as timestamp without time zone);

create or replace function dbo.s_context(mode integer)
  returns bigint
  stable parallel safe
return case when mode = 1 then S_GET_CONTEXT_NUM('USERID') when mode = 2 then S_GET_CONTEXT_NUM('ORGID') else null end;

create or replace function dbo.s_get_org()
  returns bigint
  stable parallel safe
return cast(current_setting('omega.orgid') as bigint);

create or replace function dbo.s_workdate()
  returns timestamp without time zone
  stable parallel safe
return cast(current_setting('omega.workdate') as timestamp);

create or replace function dbo.s_access(vArc bigint, vUserId bigint, vAction bigint, vDate timestamp without time zone)
  returns integer
  language  plpgsql
   stable parallel safe
as 
$body$
begin
  return 1;
end;
$body$
;

create or replace function dbo.s_access2(vArc bigint, vUserId bigint, vAction bigint, vDate timestamp without time zone, vDefault integer)
  returns integer
  language  plpgsql
   stable parallel safe
as 
$body$
begin
  return 1;
end;
$body$
;

create or replace function dbo.s_access3(vArc bigint, vUserId bigint, vAction bigint, vDate timestamp without time zone, vDefault integer, vCreatorId bigint default null)
  returns integer
  language  plpgsql
  stable parallel safe
as 
$body$
begin
  return 1;
end;
$body$;

create or replace function dbo.s_useringroup(vUserID bigint, vGroupID bigint, vDate timestamp without time zone) 
  returns integer 
  language  plpgsql
  stable parallel safe
as
$body$
begin 
  return 1;     
end;
$body$;

create or replace function dbo.s_get_context(vName varchar)
  returns varchar
  stable parallel safe
return current_setting('my.' || vName);

create or replace function dbo.s_get_context_num(vName varchar)
  returns bigint
  stable parallel safe
return cast(current_setting('my.' || vName) as bigint);

create or replace function dbo.s_get_context_date(vName varchar)
  returns timestamp without time zone
  stable parallel safe
return cast(current_setting('my.' || vName) as timestamp without time zone);

create or replace function dbo.s_context(mode integer)
  returns bigint
  stable parallel safe
return case when mode = 1 then S_GET_CONTEXT_NUM('USERID') when mode = 2 then S_GET_CONTEXT_NUM('ORGID') else null end;

create or replace function dbo.s_get_org()
  returns bigint
  stable parallel safe
return cast(current_setting('my.orgid') as bigint);

