Original query:

-- SELECT set_session(11296)
SELECT * 
FROM
(
-- WY 180914
-- PASTED v_use_doc_attribute View Query HERE
 WITH vudp AS (
         SELECT v_ud_permission.org_id,
            v_ud_permission.document_code,
            v_ud_permission.document_no,
            v_ud_permission.status_order,
            v_ud_permission.doc_status,
            v_ud_permission.status_desc,
            v_ud_permission.approval_roles,
            v_ud_permission.authorized_roles,
            v_ud_permission.next_status,
            v_ud_permission.allow_unbalanced,
            v_ud_permission.show_in_doc,
            v_ud_permission.approved_by,
            v_ud_permission.approved_on,
            v_ud_permission.is_readonly,
            v_ud_permission.is_rating_enabled,
            v_ud_permission.is_show_rating_enabled,
            v_ud_permission.event_id,
            v_ud_permission.xref_document_no
           FROM v_ud_permission
		WHERE document_no = 354122
        )
 SELECT alldata.org_id,
    alldata.document_code,
    alldata.document_no,
    alldata.attrib_code,
    alldata.attrib_desc,
    alldata.attrib_data_type,
    alldata.attrib_value,
    alldata.is_required,
    alldata.cascade_to_line,
    alldata.entered_by,
    alldata.entered_on,
    alldata.last_updated_by,
    alldata.last_updated_on,
    alldata.attrib_object,
    alldata.attrib_parms,
    alldata.attrib_image,
    alldata.attrib_type,
    alldata.validation_expression,
    alldata.validation_message,
    alldata.attrib_group_name,
    alldata.attrib_order,
    alldata.label_format,
    alldata.field_format,
    alldata.custom_exp_attrib,
    alldata.block_format,
    alldata.attrib_lookup,
    alldata.attrib_section_name,
    alldata.field_code,
    alldata.attrib_data_format_client,
    alldata.attrib_data_format_db,
    alldata.attrib_value_decode,
    alldata.is_readonly,
    alldata.attrib_position,
    alldata.is_visible
   FROM ( SELECT DISTINCT uda.org_id,
            uda.document_code,
            uda.document_no,
            uda.attrib_code,
            uda.attrib_desc,
            uda.attrib_data_type,
            uda.attrib_value,
            uda.is_required,
            uda.cascade_to_line,
            uda.entered_by,
            uda.entered_on,
            uda.last_updated_by,
            uda.last_updated_on,
            uda.attrib_object,
            replace(uda.attrib_parms::text, '##attrib_value##'::text, COALESCE(uda.attrib_value, ''::character varying)::text) AS attrib_parms,
            uda.attrib_image,
            uda.attrib_type,
            uda.validation_expression,
            uda.validation_message,
            uda.attrib_group_name,
            uda.attrib_order,
            uda.label_format,
            uda.field_format,
            uda.custom_exp_attrib,
                CASE
                    WHEN uda.attrib_type::text = 'checkbox'::text AND COALESCE(uda.attrib_value, ''::character varying)::text = split_part(uda.attrib_lookup, '~r'::text, 1) THEN replace(uda.block_format, ('value="'::text || split_part(uda.attrib_lookup, '~r'::text, 1)) || '"'::text, ('value="'::text || split_part(uda.attrib_lookup, '~r'::text, 1)) || '" checked="checked"'::text)
                    WHEN uda.attrib_type::text = 'List'::text THEN replace(uda.block_format, ('value="'::text || COALESCE(uda.attrib_value, ''::character varying)::text) || '"'::text, ('value="'::text || COALESCE(uda.attrib_value, ''::character varying)::text) || '" selected="selected"'::text)
                    WHEN uda.attrib_type::text = 'MLIST'::text THEN events.select_multiple_option_value(uda.block_format::character varying, uda.attrib_value)::text
                    WHEN uda.attrib_type::text = 'radio'::text THEN replace(uda.block_format, ('value="'::text || COALESCE(uda.attrib_value, ''::character varying)::text) || '"'::text, ('value="'::text || COALESCE(uda.attrib_value, ''::character varying)::text) || '" checked="checked"'::text)
                    WHEN uda.attrib_type::text = ANY (ARRAY['DBLIST'::character varying, 'DBLISTS'::character varying, 'docstatuslist'::character varying]::text[]) THEN replace(uda.block_format, ('value="'::text || COALESCE(uda.attrib_value, ''::character varying)::text) || '"'::text, COALESCE(('value="'::text || COALESCE(uda.attrib_value, ''::character varying)::text) || '" selected'::text, ''::text))
                    WHEN uda.attrib_type::text = 'MDBLIST'::text THEN events.select_multiple_option_value(uda.block_format::character varying, uda.attrib_value)::text
                    ELSE replace(replace(uda.block_format, '##attrib_value##'::text, COALESCE(uda.attrib_value, ''::character varying)::text), '##attrib_data_format##'::text,
                    CASE
                        WHEN COALESCE(uda.attrib_data_format_client, ''::character varying)::text = ''::text AND lower(uda.attrib_type::text) = 'datepicker'::text THEN 'dd-MMM-yyyy'::character varying
                        ELSE COALESCE(uda.attrib_data_format_client, ''::character varying)
                    END::text)
                END AS block_format,
            uda.attrib_lookup,
            uda.attrib_section_name,
            uda.field_code,
            uda.attrib_data_format_client,
            uda.attrib_data_format_db,
                CASE
                    WHEN uda.attrib_type::text = ANY (ARRAY['DBLIST'::character varying, 'DBLISTS'::character varying, 'docstatuslist'::character varying]::text[]) THEN COALESCE(events.field_decode(uda.org_id, uda.document_no, uda.attrib_lookup::character varying, uda.attrib_value), ''::character varying)
                    WHEN uda.attrib_type::text = 'List'::text THEN COALESCE(split_part(split_part(uda.attrib_lookup, uda.attrib_value::text || '~t'::text, 2), '~r'::text, 1), ''::text)::character varying
                    ELSE uda.attrib_value
                END AS attrib_value_decode,
            uda.is_readonly,
            uda.attrib_position,
            uda.is_visible
           FROM ( SELECT ud_attribvaluefixed.org_id,
                    ud_attribvaluefixed.document_code,
                    ud_attribvaluefixed.document_no,
                    ud_attribvaluefixed.attrib_code,
                        CASE
                            WHEN strpos(ud_attribvaluefixed.attrib_desc::text, '!!FIELDGET:'::text) > 0 THEN events.fields_replace(ud_attribvaluefixed.org_id, ud_attribvaluefixed.document_no, ud_attribvaluefixed.attrib_code, ud_attribvaluefixed.attrib_type, ud_attribvaluefixed.attrib_desc, ud_attribvaluefixed.attrib_value)
                            ELSE ud_attribvaluefixed.attrib_desc
                        END::character varying(2000) AS attrib_desc,
                    ud_attribvaluefixed.attrib_data_type,
                        CASE
                            WHEN ud_attribvaluefixed.attrib_data_type::text = 'date'::text AND COALESCE(ud_attribvaluefixed.attrib_data_format_db, ''::character varying)::text <> ''::text AND COALESCE(ud_attribvaluefixed.attrib_value, ''::character varying)::text <> ''::text THEN to_char(ud_attribvaluefixed.attrib_value::date::timestamp with time zone, ud_attribvaluefixed.attrib_data_format_db::text)::character varying
                            ELSE ud_attribvaluefixed.attrib_value
                        END AS attrib_value,
                    ud_attribvaluefixed.is_required,
                    ud_attribvaluefixed.cascade_to_line,
                    ud_attribvaluefixed.entered_by,
                    ud_attribvaluefixed.entered_on,
                    ud_attribvaluefixed.last_updated_by,
                    ud_attribvaluefixed.last_updated_on,
                    ud_attribvaluefixed.attrib_object,
                    ud_attribvaluefixed.attrib_parms,
                    ud_attribvaluefixed.attrib_image,
                    ud_attribvaluefixed.attrib_type,
                    ud_attribvaluefixed.validation_expression,
                    ud_attribvaluefixed.validation_message,
                    replace(ud_attribvaluefixed.attrib_group_name::text, '##pt_desc##'::text, COALESCE(ud_attribvaluefixed.pt_desc, ''::character varying)::text)::character varying(100) AS attrib_group_name,
                    ud_attribvaluefixed.attrib_order,
                    ud_attribvaluefixed.label_format,
                    ud_attribvaluefixed.field_format,
                    ud_attribvaluefixed.custom_exp_attrib,
                    replace(replace(replace(replace(replace(
                        CASE
                            WHEN (ud_attribvaluefixed.is_readonly = 1 OR ud_attribvaluefixed.is_cedit_allowed = 1) AND (lower(ud_attribvaluefixed.attrib_type::text) = ANY (ARRAY['dblist'::text, 'dblists'::text, 'list'::text, 'mdblist'::text, 'mlist'::text, 'docstatuslist'::text])) THEN replace(ud_attribvaluefixed.block_format, '<select'::text, '<select disabled'::text)
                            WHEN (ud_attribvaluefixed.is_readonly = 1 OR ud_attribvaluefixed.is_cedit_allowed = 1) AND (lower(ud_attribvaluefixed.attrib_type::text) = ANY (ARRAY['jqdatetimepicker'::text, 'radio'::text, 'checkbox'::text])) THEN replace(ud_attribvaluefixed.block_format, '<input'::text, '<input disabled'::text)
                            WHEN (ud_attribvaluefixed.is_readonly = 1 OR ud_attribvaluefixed.is_cedit_allowed = 1) AND (lower(ud_attribvaluefixed.attrib_type::text) = ANY (ARRAY['customdatetimepicker'::text])) THEN replace(replace(ud_attribvaluefixed.block_format, '<div'::text, '<div disabled="disabled"'::text), '<input'::text, '<input disabled'::text)
                            WHEN (ud_attribvaluefixed.is_readonly = 1 OR ud_attribvaluefixed.is_cedit_allowed = 1) AND (lower(ud_attribvaluefixed.attrib_type::text) = ANY (ARRAY['weburl'::text, 'text'::text, 'email'::text, 'date'::text, 'time'::text, 'datepicker'::text, 'ejnumerictextbox'::text, 'numerictextbox'::text, 'phone'::text])) THEN replace(ud_attribvaluefixed.block_format, '<input'::text, '<input readonly'::text)
                            WHEN (ud_attribvaluefixed.is_readonly = 1 OR ud_attribvaluefixed.is_cedit_allowed = 1) AND (lower(ud_attribvaluefixed.attrib_type::text) = ANY (ARRAY['textarealimit'::text, 'textarea'::text])) THEN replace(ud_attribvaluefixed.block_format, '<textarea'::text, '<textarea readonly'::text)
                            WHEN lower(ud_attribvaluefixed.attrib_type::text) = 'iframe'::text THEN
                            CASE
                                WHEN ud_attribvaluefixed.is_readonly = 1 OR ud_attribvaluefixed.is_cedit_allowed = 1 THEN replace(replace(ud_attribvaluefixed.block_format, '<iframe'::text, '<iframe readonly'::text), '##pt_id##'::text, COALESCE(ud_attribvaluefixed.pt_id::character varying, ''::character varying)::text)
                                ELSE replace(ud_attribvaluefixed.block_format, '##pt_id##'::text, COALESCE(ud_attribvaluefixed.pt_id::character varying, ''::character varying)::text)
                            END
                            ELSE ud_attribvaluefixed.block_format
                        END, '##pt_id##'::text, COALESCE(ud_attribvaluefixed.pt_id::character varying, '##pt_id##'::character varying)::text), '##bp_id##'::text, COALESCE(ud_attribvaluefixed.bp_id::character varying, ''::character varying)::text), '##xref_pt_id##'::text, COALESCE(ud_attribvaluefixed.xref_pt_id::character varying, ''::character varying)::text), 'form-control'::text,
                        CASE
                            WHEN ud_attribvaluefixed.is_cedit_allowed = 1 THEN 'form-control pl-cedit'::text
                            ELSE 'form-control'::text
                        END), '##org_id##'::text, ud_attribvaluefixed.org_id::character varying::text) AS block_format,
                    ud_attribvaluefixed.attrib_lookup,
                    replace(replace(ud_attribvaluefixed.attrib_section_name::text, '##pt_desc##'::text, COALESCE(ud_attribvaluefixed.pt_desc, ''::character varying)::text), ':'::text, ' -'::text)::character varying(100) AS attrib_section_name,
                    ud_attribvaluefixed.field_code,
                    ud_attribvaluefixed.attrib_data_format_client,
                    ud_attribvaluefixed.attrib_data_format_db,
                    ud_attribvaluefixed.is_readonly,
                    ud_attribvaluefixed.attrib_position,
                    ud_attribvaluefixed.is_visible
                   FROM ( SELECT ud.org_id,
                            ud.document_code,
                            ud.document_no,
                            uda_1.attrib_code,
                            use_doc.xref_pt_id,
                            replace(replace(uda_1.attrib_desc::text, '##pt_id##'::text, COALESCE(use_doc.pt_id::character varying, ''::character varying)::text), '##bp_id##'::text, COALESCE(use_doc.bp_id::character varying, ''::character varying)::text)::character varying(2000) AS attrib_desc,
                            uda_1.attrib_data_type,
                            replace(replace(replace(replace(replace(replace(
                                CASE
                                    WHEN uda_1.field_code IS NOT NULL AND uda_1.field_code::text <> ''::text THEN
                                    CASE
WHEN NULLIF(uda_1.attrib_value::text, ''::text) <> ''::text AND btrim(lower(uda_1.attrib_type::text)) = 'htmledit'::text THEN
CASE
 WHEN NOT udarr.privilege ~~ '%W%'::text THEN replace(replace(replace(events.field_get(ud.org_id, ud.document_no, uda_1.field_code, upper(uda_1.attrib_type::text)::character varying,
 CASE
  WHEN uda_1.field_code::text = 'SQLQUERY'::text THEN COALESCE((xpath('//sqlquery/text()'::text, uda_1.xmldata))[1]::text, uda_1.attrib_value::text)::character varying
  ELSE uda_1.attrib_value
 END, uda_1.attrib_code)::text, '<input'::text, '<input disabled'::text), '<textarea'::text, '<textarea disabled'::text), '<select'::text, '<select disabled'::text)::character varying
 ELSE events.field_get(ud.org_id, ud.document_no, uda_1.field_code, upper(uda_1.attrib_type::text)::character varying,
 CASE
  WHEN uda_1.field_code::text = 'SQLQUERY'::text THEN COALESCE((xpath('//sqlquery/text()'::text, uda_1.xmldata))[1]::text, uda_1.attrib_value::text)::character varying
  ELSE uda_1.attrib_value
 END, uda_1.attrib_code)
END::text
ELSE COALESCE(NULLIF(uda_1.attrib_value::text, ''::text), events.field_get(ud.org_id, ud.document_no, uda_1.field_code, upper(uda_1.attrib_type::text)::character varying,
CASE
 WHEN uda_1.field_code::text = 'SQLQUERY'::text THEN COALESCE((xpath('//sqlquery/text()'::text, uda_1.xmldata))[1]::text, uda_1.attrib_value::text)::character varying
 ELSE uda_1.attrib_value
END, uda_1.attrib_code)::text)
                                    END::character varying
                                    ELSE uda_1.attrib_value
                                END::text, '##xref_document_no##'::text, ud.xref_document_no::character varying::text), '##pt_id##'::text, COALESCE(use_doc.pt_id::character varying, '##pt_id##'::character varying)::text), '##org_id##'::text, uda_1.org_id::character varying::text), '##org_code##'::text, (( SELECT o.org_code
                                   FROM organization o
                                  WHERE o.org_id = uda_1.org_id))::text), '##bp_id##'::text, COALESCE(use_doc.bp_id::character varying, '##bp_id##'::character varying)::text), '##xref_pt_id##'::text, COALESCE(use_doc.xref_pt_id::character varying, '##xref_pt_id##'::character varying)::text)::character varying AS attrib_value,
                            admin.is_required,
                            uda_1.cascade_to_line,
                            uda_1.entered_by,
                            uda_1.entered_on,
                            uda_1.last_updated_by,
                            uda_1.last_updated_on,
                            uda_1.attrib_object,
                            replace(replace(replace(replace(replace(replace(uda_1.attrib_parms::text, '##xref_document_no##'::text, ud.xref_document_no::character varying::text), '##pt_id##'::text, COALESCE(use_doc.pt_id::character varying, ''::character varying)::text), '##org_id##'::text, uda_1.org_id::character varying::text), '##org_code##'::text, (( SELECT o.org_code
                                   FROM organization o
                                  WHERE o.org_id = uda_1.org_id))::text), '##bp_id##'::text, COALESCE(use_doc.bp_id::character varying, ''::character varying)::text), '##xref_pt_id##'::text, COALESCE(use_doc.xref_pt_id::character varying, ''::character varying)::text)::character varying(2000) AS attrib_parms,
                            uda_1.attrib_image,
                            uda_1.attrib_type,
                            uda_1.validation_expression,
                            uda_1.validation_message,
                            uda_1.attrib_group_name,
                            uda_1.attrib_order,
                            uda_1.label_format,
                            uda_1.field_format,
                                CASE
                                    WHEN COALESCE(admin.is_admin, 0) = 1 AND COALESCE(admin.is_admin_not_required, 0) = 1 THEN replace(uda_1.custom_exp_attrib::text, 'data-setrequired'::text, 'data-adminsetrequired'::text)::character varying
                                    ELSE uda_1.custom_exp_attrib
                                END::character varying(2000) AS custom_exp_attrib,
                            replace(replace(replace(replace(
                                CASE
                                    WHEN uda_1.attrib_type::text = ANY (ARRAY['DBLIST'::character varying, 'DBLISTS'::character varying, 'docstatuslist'::character varying]::text[]) THEN replace(uda_1.block_format, '##DBLISTLOOKUP##'::text, COALESCE(events.field_get(ud.org_id, ud.document_no, uda_1.attrib_lookup, upper(uda_1.attrib_type::text)::character varying,
                                    CASE
WHEN uda_1.field_code::text = 'SQLQUERY'::text THEN COALESCE((xpath('//sqlquery/text()'::text, uda_1.xmldata))[1]::text, uda_1.attrib_value::text)::character varying
ELSE uda_1.attrib_value
                                    END, uda_1.attrib_code), ''::character varying)::text)
                                    WHEN uda_1.attrib_type::text = 'MDBLIST'::text THEN replace(uda_1.block_format, '##MDBLISTLOOKUP##'::text, events.field_get(ud.org_id, ud.document_no, uda_1.attrib_lookup, upper(uda_1.attrib_type::text)::character varying,
                                    CASE
WHEN uda_1.field_code::text = 'SQLQUERY'::text THEN COALESCE((xpath('//sqlquery/text()'::text, uda_1.xmldata))[1]::text, uda_1.attrib_value::text)::character varying
ELSE uda_1.attrib_value
                                    END, uda_1.attrib_code)::text)
                                    WHEN uda_1.attrib_type::text = 'ejimagedropdown'::text THEN replace(uda_1.block_format, '##DBLISTLOOKUP##'::text, events.field_get(ud.org_id, ud.document_no, uda_1.attrib_lookup, upper(uda_1.attrib_type::text)::character varying,
                                    CASE
WHEN uda_1.field_code::text = 'SQLQUERY'::text THEN COALESCE((xpath('//sqlquery/text()'::text, uda_1.xmldata))[1]::text, uda_1.attrib_value::text)::character varying
ELSE uda_1.attrib_value
                                    END, uda_1.attrib_code)::text)
                                    ELSE uda_1.block_format
                                END, '##ATTRIBDESC##'::text, '##ATTRIBDESC##</label>'::text || COALESCE(
                                CASE
                                    WHEN uda_1.attrib_detail IS NOT NULL AND uda_1.attrib_detail::text <> ''::text THEN ('<div class="pl-attrib-detail">'::text ||
                                    CASE
WHEN strpos(uda_1.attrib_detail::text, '!!FIELDGET:'::text) > 0 THEN events.fields_replace(uda_1.org_id, uda_1.document_no, uda_1.attrib_code, uda_1.attrib_type, uda_1.attrib_detail, uda_1.attrib_value)::text::character varying
ELSE COALESCE(uda_1.attrib_detail, ''::character varying)
                                    END::text) || '</div>'::text
                                    ELSE NULL::text
                                END, ''::text)), '##ATTRIBDESC##'::text,
                                CASE
                                    WHEN strpos(uda_1.attrib_desc::text, '!!FIELDGET:'::text) > 0 THEN events.fields_replace(uda_1.org_id, uda_1.document_no, uda_1.attrib_code, uda_1.attrib_type, uda_1.attrib_desc, uda_1.attrib_value)
                                    ELSE uda_1.attrib_desc
                                END::text ||
                                CASE
                                    WHEN admin.is_required > 0::numeric THEN '<span class="required_indicator" style="color: rgb(255, 0, 0);"> *</span>'::text
                                    ELSE ''::text
                                END), ('is_required="'::text || uda_1.is_required) || '"'::text, ('is_required="'::text || admin.is_required) || '"'::text), 'data-setrequired'::text,
                                CASE
                                    WHEN COALESCE(admin.is_admin, 0) = 1 AND COALESCE(admin.is_admin_not_required, 0) = 1 THEN 'data-adminsetrequired'::text
                                    ELSE 'data-setrequired'::text
                                END) AS block_format,
                                CASE
                                    WHEN upper(uda_1.attrib_type::text) = 'CONTAINER'::text AND upper(uda_1.attrib_object::text) = 'CHECKBOXLIST'::text AND strpos(uda_1.attrib_lookup::text, '~r'::text) = 0 THEN events.field_get(ud.org_id, ud.document_no, uda_1.attrib_lookup, upper(uda_1.attrib_object::text)::character varying,
                                    CASE
WHEN uda_1.field_code::text = 'SQLQUERY'::text THEN COALESCE((xpath('//sqlquery/text()'::text, uda_1.xmldata))[1]::text, uda_1.attrib_value::text)::character varying
ELSE uda_1.attrib_value
                                    END, uda_1.attrib_code)
                                    WHEN upper(uda_1.attrib_type::text) = 'CONTAINER'::text AND upper(uda_1.attrib_object::text) = 'CHECKBOXLIST'::text AND strpos(uda_1.attrib_lookup::text, '~r'::text) > 0 THEN (( SELECT b.listdata
                                       FROM ( SELECT string_agg(c.cdata, ''::text) AS listdata
    FROM ( SELECT ((((
     CASE
      WHEN data_decode.data_value = ANY (string_to_array(uda_1.attrib_value::text, '~r'::text)) THEN '1'::text
      ELSE '0'::text
     END || '~t'::text) || data_decode.data_value) || '~t'::text) || data_decode.data_value) || '~r'::text AS cdata
      FROM ( SELECT (string_to_array(xct.xc, '~t'::text))[1] AS data_code,
         (string_to_array(xct.xc, '~t'::text))[2] AS data_value
        FROM ( SELECT unnest(string_to_array(uda_1.attrib_lookup::text, '~r'::text)) AS xc) xct) data_decode) c) b))::character varying
                                    ELSE uda_1.attrib_lookup
                                END::text AS attrib_lookup,
                            uda_1.attrib_section_name,
                            uda_1.field_code,
                            uda_1.attrib_data_format_client,
                            uda_1.attrib_data_format_db,
                                CASE
                                    WHEN pt.reg_open = '-9'::integer::numeric THEN 1
                                    WHEN COALESCE(admin.is_admin, 0) = 0 AND use_doc.document_code::text = pt.atd_reg_doc_code::text AND pt.reg_open = '-6'::integer::numeric THEN 1
                                    WHEN udarr.privilege ~~ '%W%'::text THEN 0
                                    ELSE 1
                                END AS is_readonly,
                                CASE
                                    WHEN
                                    CASE
WHEN pt.reg_open = '-9'::integer::numeric THEN 1
WHEN COALESCE(admin.is_admin, 0) = 0 AND use_doc.document_code::text = pt.atd_reg_doc_code::text AND pt.reg_open = '-6'::integer::numeric THEN 1
WHEN udarr.privilege ~~ '%W%'::text THEN 0
ELSE 1
                                    END = 0 AND uda_1.custom_exp_attrib::text ~~ '%data-cedit=%mode_:1%'::text THEN 1
                                    ELSE 0
                                END AS is_cedit_allowed,
                            uda_1.attrib_position,
                            use_doc.pt_id,
                            use_doc.bp_id,
                            pt.pt_desc,
                            uda_1.is_visible
                           FROM use_doc
                             LEFT JOIN project_task pt ON pt.org_id = use_doc.org_id AND pt.pt_id = use_doc.pt_id,
                            use_doc_attribute uda_1
                             CROSS JOIN LATERAL ( SELECT
CASE
 WHEN strpos(COALESCE(uda_1.custom_exp_attrib, ''::character varying)::text, 'is_admin_not_required="1"'::text) > 0 AND (is_uca.is_uca = 1 OR is_uea.is_uea = 1) THEN 0::numeric
 ELSE uda_1.is_required
END::numeric(1,0) AS is_required,
CASE
 WHEN is_uca.is_uca = 1 OR is_uea.is_uea = 1 THEN 1
 ELSE 0
END AS is_admin,
CASE
 WHEN COALESCE(uda_1.custom_exp_attrib, ''::character varying)::text ~~ '%is_admin_not_required=_1%'::text THEN 1
 ELSE 0
END AS is_admin_not_required
                                   FROM ( SELECT LEAST(count(*), 1::bigint) AS is_uca
   FROM bp_emp_role
  WHERE bp_emp_role.role_code::text = 'UCA'::text AND bp_emp_role.org_id = use_doc.org_id AND bp_emp_role.bp_id = (( SELECT v_current_user.user_id
     FROM v_current_user))) is_uca,
                                    ( SELECT LEAST(count(*), 1::bigint) AS is_uea
   FROM pt_team
  WHERE pt_team.role_code::text = 'UEA'::text AND pt_team.org_id = use_doc.org_id AND pt_team.pt_id = use_doc.pt_id AND pt_team.bp_id = (( SELECT v_current_user.user_id
     FROM v_current_user))) is_uea) admin,
                            vudp ud,
                            ( SELECT
CASE
 WHEN p.is_readonly = 1 THEN 'R'::text
 ELSE 'RW'::text
END AS privilege,
                                    uda_2.attrib_code,
                                    uda_2.org_id,
                                    uda_2.document_no,
                                    uda_2.document_code
                                   FROM use_doc_attribute uda_2,
                                    vudp p
                                  WHERE p.document_no = uda_2.document_no AND NOT (EXISTS ( SELECT DISTINCT 1
   FROM uda_rules udar
  WHERE udar.rule_type::text = 'INCLUDE'::text AND udar.visibility_mode = 'R'::bpchar AND udar.org_id = uda_2.org_id AND udar.document_code::text = uda_2.document_code::text AND udar.document_no = uda_2.document_no AND uda_2.attrib_code::text ~~ COALESCE(NULLIF(udar.attrib_code::text, ''::text), '%'::text) AND COALESCE(uda_2.attrib_group_name, '%'::character varying)::text ~~ COALESCE(NULLIF(udar.attrib_group_name::text, ''::text), '%'::text)))
                                UNION
                                 SELECT max(uda_x.max) AS max,
                                    uda_x.attrib_code,
                                    uda_x.org_id,
                                    uda_x.document_no,
                                    uda_x.document_code
                                   FROM ( SELECT max(udar.privilege) AS max,
    uda_2.attrib_code,
    uda_2.org_id,
    uda_2.document_no,
    uda_2.document_code
   FROM use_doc_attribute uda_2,
    ( SELECT x.privilege,
      uda_rules.org_id,
      uda_rules.document_code,
      uda_rules.document_no,
      uda_rules.attrib_code,
      uda_rules.rule_type,
      uda_rules.status_codes,
      uda_rules.role_codes,
      uda_rules.category,
      uda_rules.subcategory,
      uda_rules.attrib_group_name
     FROM uda_rules,
      ( SELECT 'R'::text AS privilege
    UNION
     SELECT 'RW'::text AS privilege) x
    WHERE x.privilege ~~ uda_rules.privilege::text) udar,
    vudp p
     LEFT JOIN ( SELECT udt_1.org_id,
      udt_1.document_code,
      udt_1.document_no,
      udt_1.role_code,
      udt_1.bp_id,
      udt_1.start_date,
      udt_1.end_date,
      udt_1.is_primary
     FROM use_doc_team udt_1
    WHERE udt_1.bp_id = (( SELECT v_current_user.user_id
       FROM v_current_user))
  UNION
   SELECT udt_1.org_id,
      udt_1.document_code,
      udt_1.document_no,
      udt_1.role_code,
      udt_1.bp_id,
      udt_1.start_date,
      udt_1.end_date,
      udt_1.is_primary
     FROM use_doc_team udt_1,
      bp_relation bpr
    WHERE bpr.org_id = udt_1.org_id AND bpr.parent_bp_id = udt_1.bp_id AND bpr.role_code::text <> '$BPEDIT'::text AND bpr.bp_id = (( SELECT v_current_user.user_id
       FROM v_current_user))) udt ON udt.org_id = p.org_id AND udt.document_code::text = p.document_code::text AND udt.document_no = p.document_no,
    project_task pt_1
     LEFT JOIN ( SELECT ptt_1.org_id,
      ptt_1.pt_id,
      ptt_1.role_code,
      ptt_1.is_primary,
      ptt_1.bp_id,
      ptt_1.category,
      ptt_1.subcategory,
      ptt_1.status
     FROM pt_team ptt_1
  UNION
   SELECT ptt_1.org_id,
      ptt_1.pt_id,
      ptt_1.role_code,
      ptt_1.is_primary,
      bpr.bp_id,
      ptt_1.category,
      ptt_1.subcategory,
      ptt_1.status
     FROM pt_team ptt_1,
      bp_relation bpr
    WHERE bpr.org_id = ptt_1.org_id AND bpr.parent_bp_id = ptt_1.bp_id) ptt ON ptt.org_id = pt_1.org_id AND ptt.pt_id = pt_1.pt_id AND (ptt.bp_id IN ( SELECT v_current_user.user_id
     FROM v_current_user)),
    v_current_user vcu
  WHERE p.org_id = pt_1.org_id AND pt_1.top_pt_id = p.event_id AND udar.rule_type::text = 'INCLUDE'::text AND udar.org_id = uda_2.org_id AND udar.document_code::text = uda_2.document_code::text AND udar.document_no = uda_2.document_no AND uda_2.attrib_code::text ~~ COALESCE(NULLIF(udar.attrib_code::text, ''::text), '%'::text) AND COALESCE(uda_2.attrib_group_name, '%'::character varying)::text ~~ COALESCE(NULLIF(udar.attrib_group_name::text, ''::text), '%'::text) AND p.org_id = uda_2.org_id AND p.document_code::text = uda_2.document_code::text AND p.document_no = uda_2.document_no AND (p.doc_status::text ~~ ANY (string_to_array(udar.status_codes::text, ','::text))) AND ((EXISTS ( SELECT 1
     FROM bp_emp_role bper
    WHERE (bper.role_code::text ~~ ANY (string_to_array(udar.role_codes::text, ','::text))) AND bper.bp_id = vcu.user_id)) OR (ptt.role_code::text ~~ ANY (string_to_array(udar.role_codes::text, ','::text))) OR (udt.role_code::text ~~ ANY (string_to_array(udar.role_codes::text, ','::text)))) AND (COALESCE(ptt.category, '%'::character varying)::text ~~ ANY (string_to_array(udar.category::text, ','::text))) AND (COALESCE(ptt.subcategory, '%'::character varying)::text ~~ ANY (string_to_array(udar.subcategory::text, ','::text)))
  GROUP BY uda_2.attrib_code, uda_2.org_id, uda_2.document_no, uda_2.document_code
UNION ALL
 SELECT max(udar.privilege) AS max,
    uda_2.attrib_code,
    uda_2.org_id,
    uda_2.document_no,
    uda_2.document_code
   FROM use_doc_attribute uda_2,
    ( SELECT x.privilege,
      uda_rules.org_id,
      uda_rules.document_code,
      uda_rules.document_no,
      uda_rules.attrib_code,
      uda_rules.rule_type,
      uda_rules.status_codes,
      uda_rules.role_codes,
      uda_rules.category,
      uda_rules.subcategory,
      uda_rules.attrib_group_name
     FROM uda_rules,
      ( SELECT 'R'::text AS privilege
    UNION
     SELECT 'RW'::text AS privilege) x
    WHERE x.privilege ~~ uda_rules.privilege::text) udar,
    vudp p
     LEFT JOIN ( SELECT udt_1.org_id,
      udt_1.document_code,
      udt_1.document_no,
      udt_1.role_code,
      udt_1.bp_id,
      udt_1.start_date,
      udt_1.end_date,
      udt_1.is_primary
     FROM use_doc_team udt_1
    WHERE udt_1.bp_id = (( SELECT v_current_user.user_id
       FROM v_current_user))
  UNION
   SELECT udt_1.org_id,
      udt_1.document_code,
      udt_1.document_no,
      udt_1.role_code,
      udt_1.bp_id,
      udt_1.start_date,
      udt_1.end_date,
      udt_1.is_primary
     FROM use_doc_team udt_1,
      bp_relation bpr
    WHERE bpr.org_id = udt_1.org_id AND bpr.parent_bp_id = udt_1.bp_id AND bpr.role_code::text <> '$BPEDIT'::text AND bpr.bp_id = (( SELECT v_current_user.user_id
       FROM v_current_user))) udt ON udt.org_id = p.org_id AND udt.document_code::text = p.document_code::text AND udt.document_no = p.document_no,
    v_current_user vcu
  WHERE udar.rule_type::text = 'INCLUDE'::text AND udar.org_id = uda_2.org_id AND udar.document_code::text = uda_2.document_code::text AND udar.document_no = uda_2.document_no AND uda_2.attrib_code::text ~~ COALESCE(NULLIF(udar.attrib_code::text, ''::text), '%'::text) AND COALESCE(uda_2.attrib_group_name, '%'::character varying)::text ~~ COALESCE(NULLIF(udar.attrib_group_name::text, ''::text), '%'::text) AND udar.category::text = '%'::text AND udar.subcategory::text = '%'::text AND p.org_id = uda_2.org_id AND p.document_code::text = uda_2.document_code::text AND p.document_no = uda_2.document_no AND (p.doc_status::text ~~ ANY (string_to_array(udar.status_codes::text, ','::text))) AND ((EXISTS ( SELECT 1
     FROM bp_emp_role bper
    WHERE (bper.role_code::text ~~ ANY (string_to_array(udar.role_codes::text, ','::text))) AND bper.bp_id = vcu.user_id)) OR (udt.role_code::text ~~ ANY (string_to_array(udar.role_codes::text, ','::text))))
  GROUP BY uda_2.attrib_code, uda_2.org_id, uda_2.document_no, uda_2.document_code) uda_x
                                  GROUP BY uda_x.attrib_code, uda_x.org_id, uda_x.document_no, uda_x.document_code) udarr
                          WHERE use_doc.org_id = ud.org_id AND use_doc.document_code::text = ud.document_code::text AND use_doc.document_no = ud.document_no AND use_doc.org_id = uda_1.org_id AND use_doc.document_code::text = uda_1.document_code::text AND use_doc.document_no = uda_1.document_no AND uda_1.org_id = udarr.org_id AND uda_1.document_code::text = udarr.document_code::text AND uda_1.document_no = udarr.document_no AND uda_1.attrib_code::text = udarr.attrib_code::text AND NOT (EXISTS ( SELECT 1
                                   FROM use_doc_attribute udaexcl,
                                    ( SELECT x.privilege,
    uda_rules.org_id,
    uda_rules.document_code,
    uda_rules.document_no,
    uda_rules.attrib_code,
    uda_rules.rule_type,
    uda_rules.status_codes,
    uda_rules.role_codes,
    uda_rules.category,
    uda_rules.subcategory
   FROM uda_rules,
    ( SELECT 'R'::text AS privilege
  UNION
   SELECT 'RW'::text AS privilege) x
  WHERE x.privilege ~~ uda_rules.privilege::text) udar,
                                    ( SELECT pt_2.top_pt_id AS event_id,
    uds.doc_status,
    udx.org_id,
    udx.document_code,
    udx.document_no,
    udx.document_user_code,
    udx.transaction_date,
    udx.mr_no,
    udx.document_desc,
    udx.document_date,
    udx.doc_status_date,
    udx.pccc_id,
    udx.bp_id,
    udx.pt_id,
    udx.dim1,
    udx.dim2,
    udx.dim3,
    udx.locked,
    udx.posted,
    udx.current_status_order,
    udx.show_pccc,
    udx.show_bp,
    udx.show_pt,
    udx.show_dim1,
    udx.show_dim2,
    udx.show_dim3,
    udx.show_cheque_no,
    udx.is_line_cr,
    udx.is_line_dr,
    udx.is_hlinked_dr,
    udx.show_hpccc,
    udx.show_hbp,
    udx.show_hpt,
    udx.show_hdim1,
    udx.show_hdim2,
    udx.show_hdim3,
    udx.show_hcheque_no,
    udx.doc_code_format,
    udx.is_pccc_required,
    udx.is_bp_required,
    udx.is_pt_required,
    udx.is_dim1_required,
    udx.is_dim2_required,
    udx.is_dim3_required,
    udx.is_cheque_required,
    udx.is_hpccc_required,
    udx.is_hbp_required,
    udx.is_hpt_required,
    udx.is_hdim1_required,
    udx.is_hdim2_required,
    udx.is_hdim3_required,
    udx.is_hcheque_required,
    udx.print_object,
    udx.edit_object,
    udx.associated,
    udx.selected_document_code,
    udx.parent_org_id,
    udx.parent_document_code,
    udx.parent_document_no,
    udx.remarks,
    udx.entered_by,
    udx.entered_on,
    udx.last_updated_by,
    udx.last_updated_on,
    udx.xref_org_id,
    udx.xref_document_code,
    udx.xref_document_no,
    udx.reversal_flag,
    udx.due_date,
    udx.show_qty_in,
    udx.show_qty_in_purchased,
    udx.show_qty_in_transferred,
    udx.show_qty_in_built,
    udx.show_qty_in_other1,
    udx.show_qty_in_other2,
    udx.show_qty_in_other3,
    udx.show_qty_out,
    udx.show_qty_out_sold,
    udx.show_qty_out_transferred,
    udx.show_qty_out_consumed,
    udx.show_qty_out_damaged,
    udx.show_qty_out_returned,
    udx.show_qty_out_other1,
    udx.show_qty_out_other2,
    udx.show_qty_out_other3,
    udx.show_qty_ordered,
    udx.show_qty_total,
    udx.is_doc_editable,
    udx.is_sync_udl,
    udx.client_doc_ref,
    udx.show_client_doc_ref,
    udx.is_client_doc_ref_required,
    udx.mt_debit_account,
    udx.mt_credit_account,
    udx.is_process_cogs,
    udx.xref_bp_id,
    udx.xref_pt_id,
    udx.xref_store_id,
    udx.show_store,
    udx.show_xref_bp,
    udx.show_xref_pt,
    udx.show_xref_store,
    udx.post_save_function,
    udx.tx_type_list,
    udx.tx_type,
    udx.show_htx_details,
    udx.is_htx_details_required,
    udx.show_hdue_date,
    udx.is_hdue_date_required,
    udx.tx_type_default,
    udx.store_id,
    udx.ref_f_bp_id,
    udx.ref_s_bp_id,
    udx.show_qty_in_adjustment,
    udx.show_qty_out_adjustment,
    udx.start_date,
    udx.end_date,
    udx.bp_filter,
    udx.pt_filter,
    udx.mt_filter,
    udx.account_filter,
    udx.account_search_g2,
    udx.bp_search_g2,
    udx.pt_search_g2,
    udx.mt_search_g2,
    udx.account_lock_g2,
    udx.bp_lock_g2,
    udx.pt_lock_g2,
    udx.mt_lock_g2,
    udx.show_unit_price,
    udx.show_total_amount,
    udx.is_editable_unit_price,
    udx.allow_diff_line_bp,
    udx.allow_diff_line_pt,
    udx.allow_diff_line_dim1,
    udx.allow_diff_line_dim2,
    udx.allow_diff_line_dim3,
    udx.pre_save_function_bt,
    udx.pre_save_function_at,
    udx.show_pt_balance,
    udx.show_parent_pt_balance,
    udx.show_top_pt_balance,
    udx.show_mat_pt,
    udx.show_mat_bp,
    udx.show_is_material,
    udx.show_start_date,
    udx.show_end_date,
    udx.show_est_qty,
    udx.show_remaining_qty,
    udx.show_available_qty,
    udx.show_buying_unit,
    udx.is_start_date_required,
    udx.is_end_date_required,
    udx.pt_search_g1,
    udx.mt_search_g1,
    udx.bp_search_g1,
    udx.price_type,
    udx.qty_invalid_condition,
    udx.qty_invalid_msg,
    udx.xmldata,
    udx.document_type,
    udx.pt_type_id,
    udx.auto_create_pt,
    udx.pt_template_id,
    udx.auto_create_bp,
    udx.bp_template_id,
    udx.auto_transition_status,
    udx.auto_add_ptt_roles,
    udx.auto_add_udt_roles,
    udx.tx_mode,
    udx.tx_paygw,
    udx.auto_add_pt_access,
    udx.xml_temp,
    udx.trigger_status_email,
    udx.count_registered,
    udx.trigger_email
   FROM use_doc udx,
    use_doc_status uds,
    project_task pt_2
  WHERE pt_2.org_id = udx.org_id AND pt_2.pt_id = udx.pt_id AND uds.org_id = udx.org_id AND uds.document_code::text = udx.document_code::text AND uds.document_no = udx.document_no AND uds.status_order = udx.current_status_order) p,
                                    project_task pt_1
                                     LEFT JOIN ( SELECT ptt_1.org_id,
    ptt_1.pt_id,
    ptt_1.role_code,
    ptt_1.is_primary,
    ptt_1.bp_id,
    ptt_1.category,
    ptt_1.subcategory,
    ptt_1.status
   FROM pt_team ptt_1
UNION
 SELECT ptt_1.org_id,
    ptt_1.pt_id,
    ptt_1.role_code,
    ptt_1.is_primary,
    bpr.bp_id,
    ptt_1.category,
    ptt_1.subcategory,
    ptt_1.status
   FROM pt_team ptt_1,
    bp_relation bpr
  WHERE bpr.org_id = ptt_1.org_id AND bpr.parent_bp_id = ptt_1.bp_id) ptt ON ptt.org_id = pt_1.org_id AND ptt.pt_id = pt_1.pt_id AND (ptt.bp_id IN ( SELECT v_current_user.user_id
   FROM v_current_user)),
                                    bp_emp_role bper,
                                    v_current_user vcu
                                  WHERE p.org_id = pt_1.org_id AND pt_1.top_pt_id = p.event_id AND udar.rule_type::text = 'EXCLUDE'::text AND udar.org_id = udaexcl.org_id AND udar.document_code::text = udaexcl.document_code::text AND udar.document_no = udaexcl.document_no AND udar.attrib_code::text = udaexcl.attrib_code::text AND p.org_id = udaexcl.org_id AND p.document_code::text = udaexcl.document_code::text AND p.document_no = udaexcl.document_no AND (p.doc_status::text ~~ ANY (string_to_array(udar.status_codes::text, ','::text))) AND (bper.bp_id = vcu.user_id AND (bper.role_code::text ~~ ANY (string_to_array(udar.role_codes::text, ','::text))) OR (ptt.role_code::text ~~ ANY (string_to_array(udar.role_codes::text, ','::text)))) AND (COALESCE(ptt.category, '%'::character varying)::text ~~ ANY (string_to_array(udar.category::text, ','::text))) AND (COALESCE(ptt.subcategory, '%'::character varying)::text ~~ ANY (string_to_array(udar.subcategory::text, ','::text))) AND udaexcl.org_id = uda_1.org_id AND udaexcl.document_code::text = uda_1.document_code::text AND udaexcl.document_no = uda_1.document_no AND udaexcl.attrib_code::text = uda_1.attrib_code::text))) ud_attribvaluefixed) uda) alldata

) vquery
WHERE document_no = 354122
	;