From b2bf4db488611f8339626f2cbe55c833837db283 Mon Sep 17 00:00:00 2001 From: mgtennant <100305096+mgtennant@users.noreply.github.com> Date: Thu, 10 Oct 2024 13:13:02 -0700 Subject: [PATCH] Create release_5.5_ddl.sql --- .../db-scripts/releases/release_5.5_ddl.sql | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 spilo-db/db-scripts/releases/release_5.5_ddl.sql diff --git a/spilo-db/db-scripts/releases/release_5.5_ddl.sql b/spilo-db/db-scripts/releases/release_5.5_ddl.sql new file mode 100644 index 0000000..54bd76c --- /dev/null +++ b/spilo-db/db-scripts/releases/release_5.5_ddl.sql @@ -0,0 +1,119 @@ + +-- MALS-17 - New report - to show comments by license number and or IRMA number +-- Create a view and procedure to support Comments reporting. + +-- +-- TABLE: MAL_DAIRY_FARM_TRAILER +-- + +DROP PROCEDURE IF EXISTS mals_app.pr_generate_print_json_licence_comments; +DROP VIEW IF EXISTS mals_app.mal_licence_comment_vw; + +-- +-- VIEW: MAL_LICENCE_COMMENT_VW +-- + +CREATE VIEW mals_app.mal_licence_comment_vw +AS SELECT lic.id AS licence_id, + lic.licence_number, + lic.irma_number, + reg.last_name, + reg.first_name, + lic.company_name, + reg.email_address, + lictyp.licence_type, + com.create_timestamp, + com.licence_comment + FROM mals_app.mal_licence lic + JOIN mals_app.mal_licence_type_lu lictyp ON lic.licence_type_id = lictyp.id + LEFT JOIN mals_app.mal_registrant reg ON lic.primary_registrant_id = reg.id + LEFT JOIN mals_app.mal_licence_comment com ON lic.id=com.licence_id; + +-- Permissions + +ALTER TABLE mals_app.mal_licence_comment_vw OWNER TO mals; +GRANT ALL ON TABLE mals_app.mal_licence_comment_vw TO mals; +GRANT SELECT ON TABLE mals_app.mal_licence_comment_vw TO mals_app_role; + +-- +-- PROCEDURE: PR_GENERATE_PRINT_JSON_LICENCE_COMMENTS +-- + +CREATE PROCEDURE mals_app.pr_generate_print_json_licence_comments( + IN ip_licence_number integer, + INOUT iop_print_job_id integer) + LANGUAGE plpgsql +AS $procedure$ + declare + l_report_json_count integer default 0; + begin + -- + -- Start a row in the mal_print_job table + call pr_start_print_job( + ip_print_category => 'REPORT', + iop_print_job_id => iop_print_job_id + ); + -- + -- Insert the JSON into the output table + with licence_comments as ( + select + lic.licence_number, + json_agg(json_build_object('LicenceNumber', lic.licence_number, + 'Lastname', lic.last_name, + 'Firstname', lic.first_name, + 'Company', lic.company_name, + 'Email', lic.email_address, + 'LicenceType', lic.licence_type, + 'CommentDate', lic.comment_date, + 'Comment', lic.comment) + order by lic.comment_date) licence_json, + count(*) num_rows + from mal_licence_comment_vw lic + where lic.licence_number = ip_licence_number) + -- + -- MAIN QUERY + -- + insert into mal_print_job_output( + print_job_id, + licence_type, + licence_number, + document_type, + document_json, + document_binary, + create_userid, + create_timestamp, + update_userid, + update_timestamp) + select + iop_print_job_id, + licence_type, + null, + 'LICENCE_COMMENTS', + json_build_object('DateTime', to_char(current_timestamp, 'fmyyyy-mm-dd hh24mi'), + 'Licence_Number', licence_number, + 'Licence', licence_json, + 'RowCount', num_rows) report_json, + null, + current_user, + current_timestamp, + current_user, + current_timestamp + from licence_comments; + -- + GET DIAGNOSTICS l_report_json_count = ROW_COUNT; + -- + -- Update the Print Job table. + update mal_print_job set + job_status = 'COMPLETE', + json_end_time = current_timestamp, + report_json_count = l_report_json_count, + update_userid = current_user, + update_timestamp = current_timestamp + where id = iop_print_job_id; +end; +$procedure$ +; + +ALTER PROCEDURE mals_app.pr_generate_print_json_licence_comments(in int4, inout int4) OWNER TO mals; +GRANT ALL ON PROCEDURE mals_app.pr_generate_print_json_licence_comments(in int4, inout int4) TO mals; +GRANT EXECUTE ON PROCEDURE mals_app.pr_generate_print_json_licence_comments(in int4, inout int4) TO mals_app_role;