Files
mip/Modules/MIP_ENQUIRY.pck
hardya 24c31ebc45 Add calc_meter_reqs to mip_quotation.pck.
Ensure that all calls to add_quote_reason use correct parameters (mip_quotation.pck).

Change grant from 'CREATE ANY VIEW' to 'CREATE VIEW' in InstallMIP.sql following problems installing 'supporting objects' into MIP_TEST.


git-svn-id: http://locode01.ad.dom/svn/WEBMIP/trunk@3989 248e525c-4dfb-0310-94bc-949c084e9493
2008-03-13 18:36:18 +00:00

534 lines
18 KiB
Plaintext

CREATE OR REPLACE PACKAGE mip_enquiry IS
-- Author : PRIESTJ
-- Created : 15/11/2007 14:18:24
-- Purpose : Provides useful admin functions/procedures for enquiries
-- Updates : 21 November 2007 - MM - added copy_enquiry function
--
TYPE t_rec_meter_reqs IS RECORD(
existing_metr_code meters.code%TYPE
,qmax meters.qmax%TYPE
,existing_mesc_code meters.mesc_code%TYPE
,existing_mety_code meters.mety_code%TYPE
,existing_prty_id meters.prty_id%TYPE
,required_mesc_code meters.mesc_code%TYPE
);
-- Public function and procedure declarations
FUNCTION delete_enquiry(p_enquiryid IN NUMBER) RETURN BOOLEAN;
FUNCTION get_enquiry_status(p_enquiryid IN NUMBER) RETURN VARCHAR2;
FUNCTION get_enquiry_role(p_enquiryid IN NUMBER
,p_rolecode IN VARCHAR2) RETURN NUMBER;
FUNCTION set_enquiry_role(p_enquiryid IN NUMBER
,p_partyid IN NUMBER
,p_rolecode IN VARCHAR2) RETURN BOOLEAN;
/*
FUNCTION set_enquiry_role
- gets the enquiry id, party id and the role type and writes a record in the enquiry_roles table
- allowing you to assign agents, suppliers etc. to an enquiry.
%param p_enquiryid - the current enquiry to save the role against.
%param p_partyid - the user or party to assign to this role.
%param p_rolecode - what kind of role this will be.
%param p_description - a description for the enquiry role
*/
FUNCTION set_enquiry_role(p_enquiryid IN NUMBER
,p_partyid IN NUMBER
,p_rolecode IN VARCHAR2
,p_description IN VARCHAR2) RETURN BOOLEAN;
--
FUNCTION set_enquiry_event(p_enquiryid IN NUMBER
,p_eventcode IN VARCHAR2) RETURN BOOLEAN;
/*
FUNCTION copy_enquiry
copies the specified enquiry.
%param p_enquiry_id - the id of the current enquiry to copy.
%param p_enqu_owner - the ID of the enquiry owner
%param p_enqu_supp - the ID of the enquiry supplier
%return varchar2 - a success or error message.
-- NOTE: there is no need to copy any quote information. All quotes will be
-- regenerated for the new enquiry when submitted.
*/
FUNCTION copy_enquiry(p_enquiry_id enquiries.id%TYPE
,p_enqu_owner parties.id%TYPE
,p_enqu_supp parties.id%TYPE) RETURN VARCHAR2;
--
FUNCTION calc_meter_reqs(p_enty_code enquiry_types.code%TYPE
,p_existing_mesc_code meters.mesc_code%TYPE
,p_required_mesc_code meters.mesc_code%TYPE
,p_existing_metr_code meters.code%TYPE
,p_existing_mety_code meters.mety_code%TYPE
,p_qmax meters.qmax%TYPE)
RETURN t_rec_meter_reqs;
END mip_enquiry;
/
CREATE OR REPLACE PACKAGE BODY mip_enquiry IS
PROCEDURE pl(p_in VARCHAR2
,p_line IN NUMBER DEFAULT NULL) IS
BEGIN
NULL;
/* $IF mip_debug_constants.debugging OR mip_debug_constants.enquiry
$THEN*/
mip_debug.pl(p_unit => $$PLSQL_UNIT
,p_line => p_line
,p_in => p_in);
/* $END*/
END pl;
/*
FUNCTION delete_enquiry
- This function returns the status of the enquiry provided
%param p_enquiryid - the enquiry we want to delete.
%return boolean - whether the enquiry was deleted or not
*/
FUNCTION delete_enquiry(p_enquiryid IN NUMBER) RETURN BOOLEAN AS
l_uri documents.uri%TYPE;
l_doc_id documents.id%TYPE;
l_count NUMBER := 0;
CURSOR c_get_doc_id(cp_enq_id NUMBER) IS
SELECT id
FROM document_roles
WHERE enqu_id = cp_enq_id;
CURSOR c_get_uri(cp_id NUMBER) IS
SELECT uri
FROM documents
WHERE id = cp_id;
CURSOR c_get_document_files(cp_enq_id NUMBER) IS
SELECT documents.uri
FROM documents
,document_roles
WHERE document_roles.enqu_id = cp_enq_id
AND documents.id = document_roles.docu_id;
BEGIN
--select count(*) into l_count from quotes where quotes.enqu_id=p_enquiryid;
--if l_count > 0 then
-- return false;
--end if;
IF NOT c_get_doc_id%ISOPEN THEN
OPEN c_get_doc_id(p_enquiryid);
END IF;
FETCH c_get_doc_id
INTO l_doc_id;
CLOSE c_get_doc_id;
IF NOT c_get_uri%ISOPEN THEN
OPEN c_get_uri(l_doc_id);
END IF;
FETCH c_get_uri
INTO l_uri;
CLOSE c_get_uri;
DELETE enquiry_roles
WHERE enquiry_roles.enqu_id = p_enquiryid;
DELETE enquiry_events
WHERE enquiry_events.enqu_id = p_enquiryid;
FOR doc_rec IN c_get_document_files(p_enquiryid) LOOP
DELETE wwv_flow_files
WHERE wwv_flow_files.NAME = doc_rec.uri;
END LOOP;
DELETE document_roles
WHERE document_roles.enqu_id = p_enquiryid;
DELETE documents
WHERE documents.id = l_doc_id;
DELETE enquiries
WHERE enquiries.id = p_enquiryid;
RETURN TRUE;
END delete_enquiry;
/*
FUNCTION get_enquiry_status
- This function returns the status of the enquiry provided
%param p_enquiryid - the enquiry we want to find the role from.
%return varchar2 - the current status of the enquiry
*/
FUNCTION get_enquiry_status(p_enquiryid IN NUMBER) RETURN VARCHAR2 AS
-- cursor to get the current enquiry
CURSOR c_get_enquiry_status(cp_enqu_id NUMBER) IS
SELECT enst_code
FROM v_current_enquiry_status
WHERE enqu_id = cp_enqu_id;
l_enqu_status VARCHAR2(80);
BEGIN
IF NOT c_get_enquiry_status%ISOPEN THEN
OPEN c_get_enquiry_status(p_enquiryid);
END IF;
FETCH c_get_enquiry_status
INTO l_enqu_status;
CLOSE c_get_enquiry_status;
RETURN nvl(l_enqu_status
,'');
END get_enquiry_status;
/*
FUNCTION get_enquiry_role
- This function finds the enquiry and the role specified and returns the
- latest(current) user id for the specifed role
%param p_enquiryid - the enquiry we want to find the role from.
%param p_rolecode - the type of role we want to find
%return number - the party id of the party that currently assumes the specified role
*/
FUNCTION get_enquiry_role(p_enquiryid IN NUMBER
,p_rolecode IN VARCHAR2) RETURN NUMBER AS
-- cursor to get the current enquiry
CURSOR c_get_enquiry_role(cp_enqu_id NUMBER, cp_rolecode VARCHAR2) IS
SELECT prty_id
FROM v_latest_rt_code_for_enro
WHERE rt_code = cp_rolecode
AND enqu_id = cp_enqu_id;
l_enqu_role_row NUMBER;
BEGIN
IF NOT c_get_enquiry_role%ISOPEN THEN
OPEN c_get_enquiry_role(p_enquiryid
,p_rolecode);
END IF;
FETCH c_get_enquiry_role
INTO l_enqu_role_row;
CLOSE c_get_enquiry_role;
RETURN l_enqu_role_row;
END get_enquiry_role;
/*
FUNCTION set_enquiry_role
- gets the enquiry id, party id and the role type and writes a record in the enquiry_roles table
- allowing you to assign agents, suppliers etc. to an enquiry.
%param p_enquiryid - the current enquiry to save the role against.
%param p_partyid - the user or party to assign to this role.
%param p_rolecode - what kind of role this will be.
*/
FUNCTION set_enquiry_role(p_enquiryid IN NUMBER
,p_partyid IN NUMBER
,p_rolecode IN VARCHAR2) RETURN BOOLEAN AS
BEGIN
INSERT INTO enquiry_roles
(enqu_id
,prty_id
,start_date
,rt_code)
VALUES
(p_enquiryid
,p_partyid
,SYSDATE
,p_rolecode);
RETURN TRUE;
--
-- EXCEPTION
-- WHEN OTHERS THEN
-- RETURN FALSE;
-- RAISE;
END set_enquiry_role;
--
/*
FUNCTION set_enquiry_role
- gets the enquiry id, party id and the role type and writes a record in the enquiry_roles table
- allowing you to assign agents, suppliers etc. to an enquiry.
%param p_enquiryid - the current enquiry to save the role against.
%param p_partyid - the user or party to assign to this role.
%param p_rolecode - what kind of role this will be.
%param p_description - a description for the enquiry role
*/
FUNCTION set_enquiry_role(p_enquiryid IN NUMBER
,p_partyid IN NUMBER
,p_rolecode IN VARCHAR2
,p_description IN VARCHAR2) RETURN BOOLEAN AS
BEGIN
INSERT INTO enquiry_roles
(enqu_id
,prty_id
,start_date
,rt_code
,description)
VALUES
(p_enquiryid
,p_partyid
,SYSDATE
,p_rolecode
,p_description);
RETURN TRUE;
--
EXCEPTION
WHEN OTHERS THEN
RETURN FALSE;
END set_enquiry_role;
--
/*
FUNCTION set_enquiry_event
- gets the enquiry id and the event type and writes a record in the enquiry_roles table
- allowing you to assign in progress, submitted for quotation etc. to an enquiry.
%param p_enquiryid - the current enquiry to save the event role against.
%param p_eventcode - what kind of event role this will be.
*/
FUNCTION set_enquiry_event(p_enquiryid IN NUMBER
,p_eventcode IN VARCHAR2) RETURN BOOLEAN AS
BEGIN
INSERT INTO enquiry_events
(enqu_id
,event_date
,enst_code
,id)
VALUES
(p_enquiryid
,SYSDATE
,p_eventcode
,enev_seq.NEXTVAL);
RETURN TRUE;
--
-- EXCEPTION
-- WHEN OTHERS THEN
-- RETURN FALSE;
-- RAISE;
END set_enquiry_event;
--
/*
FUNCTION copy_enquiry
copies the specified enquiry.
%param p_enquiry_id - the id of the current enquiry to copy.
%param p_enqu_owner - the ID of the enquiry owner
%param p_enqu_supp - the ID of the enquiry supplier
%return varchar2 - a success or error message.
-- NOTE: there is no need to copy any quote information. All quotes will be
-- regenerated for the new enquiry when submitted.
*/
FUNCTION copy_enquiry(p_enquiry_id enquiries.id%TYPE
,p_enqu_owner parties.id%TYPE
,p_enqu_supp parties.id%TYPE) RETURN VARCHAR2 IS
--
-- cursor to get a new enquiry ID from the sequence
CURSOR c_get_new_id IS
SELECT enqu_seq.NEXTVAL
FROM dual;
-- cursor to get the current enquiry
CURSOR c_get_enquiry IS
SELECT *
FROM enquiries
WHERE id = p_enquiry_id;
-- variable to hold the enquiry details returned
l_enqu_row enquiries%ROWTYPE;
-- the new enquiry ID
l_enqu_id NUMBER;
--
l_dummy BOOLEAN;
--
BEGIN
-- first thing to do is check the user's role allows them to copy enquiries
-- only agents and I&C users.
IF NOT mip_parties.get_user_role(p_username => v('APP_USER')) IN
('AGENT', 'ICU', 'MIPADMIN') THEN
raise_application_error(-20100
,'User ' || v('APP_USER') ||
' is not permitted to copy enquiries.');
END IF;
--
-- try to get the enquiry based on the ID we've been given.
--
BEGIN
IF NOT c_get_enquiry%ISOPEN THEN
OPEN c_get_enquiry;
END IF;
FETCH c_get_enquiry
INTO l_enqu_row;
CLOSE c_get_enquiry;
EXCEPTION
WHEN no_data_found THEN
CLOSE c_get_enquiry;
-- couldn't find the enquiry, tell someone
raise_application_error(-20100
,'Unable to find the specified enquiry. Please try again.');
END;
--
-- get a new enquiry ID.
BEGIN
OPEN c_get_new_id;
FETCH c_get_new_id
INTO l_enqu_id;
CLOSE c_get_new_id;
EXCEPTION
WHEN no_data_found THEN
CLOSE c_get_new_id;
raise_application_error(-20101
,'Unable to retrieve a new ID for the enquiry.');
END;
l_enqu_row.id := l_enqu_id;
-- now do the insert (copy) for the enquiry
INSERT INTO enquiries
VALUES l_enqu_row;
--
-- create an enquiry supplier, add a description of where it was copied from
l_dummy := set_enquiry_role(p_enquiryid => l_enqu_id
,p_partyid => p_enqu_supp
,p_rolecode => 'ENQ SUPP'
,p_description => 'Enquiry copied from enquiry ' ||
p_enquiry_id || ' by user ' ||
v(':APP_USER'));
IF NOT l_dummy THEN
raise_application_error(-20102
,'Unable to set enquiry supplier.');
END IF;
-- create an enquiry owner
l_dummy := set_enquiry_role(p_enquiryid => l_enqu_id
,p_partyid => p_enqu_owner
,p_rolecode => 'ENQ OWN');
IF NOT l_dummy THEN
raise_application_error(-20103
,'Unable to set enquiry owner.');
END IF;
--
-- set the enquiry event to "In Progress"
l_dummy := set_enquiry_event(p_enquiryid => l_enqu_id
,p_eventcode => 'INP');
IF NOT l_dummy THEN
raise_application_error(-20104
,'Unable to set enquiry owner.');
END IF;
--
--
RETURN l_enqu_id;
--
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RETURN SQLERRM;
END copy_enquiry;
--
FUNCTION calc_meter_reqs(p_enty_code enquiry_types.code%TYPE
,p_existing_mesc_code meters.mesc_code%TYPE
,p_required_mesc_code meters.mesc_code%TYPE
,p_existing_metr_code meters.code%TYPE
,p_existing_mety_code meters.mety_code%TYPE
,p_qmax meters.qmax%TYPE)
RETURN t_rec_meter_reqs IS
l_dummy NUMBER;
l_existing_meter_enquiry BOOLEAN;
l_required_meter_enquiry BOOLEAN;
l_rec_meter_reqs t_rec_meter_reqs;
BEGIN
pl('calc_meter_reqs:entry:' || p_enty_code || ':' ||
p_existing_mesc_code || ':' || p_required_mesc_code || ':' ||
p_existing_metr_code || ':' || p_qmax);
-- is the enquiry one that uses an existing meter?
BEGIN
SELECT 1
INTO l_dummy
FROM data_item_roles
WHERE field_name = 'EXISTING_METR_CODE'
AND condition <> 'H'
AND enty_code = p_enty_code;
l_existing_meter_enquiry := TRUE;
EXCEPTION
WHEN no_data_found THEN
l_existing_meter_enquiry := FALSE;
END;
BEGIN
SELECT 1
INTO l_dummy
FROM data_item_roles
WHERE field_name = 'QMAX'
AND condition <> 'H'
AND enty_code = p_enty_code;
l_required_meter_enquiry := TRUE;
EXCEPTION
WHEN no_data_found THEN
l_required_meter_enquiry := FALSE;
END;
-- Consider existing meter
IF NOT l_existing_meter_enquiry THEN
l_rec_meter_reqs.existing_metr_code := NULL;
l_rec_meter_reqs.existing_mesc_code := NULL;
l_rec_meter_reqs.existing_mety_code := NULL;
ELSE
-- Existing Meter Query
-- if we have an existing meter model use the associated details
IF nvl(p_existing_metr_code
,'OTHER') <> 'OTHER' THEN
SELECT code
,CASE substr(mesc_code
,1
,1)
WHEN 'U' THEN
mesc_code
ELSE
'OTHER'
END
,mety_code
,prty_id
INTO l_rec_meter_reqs.existing_metr_code
,l_rec_meter_reqs.existing_mesc_code
,l_rec_meter_reqs.existing_mety_code
,l_rec_meter_reqs.existing_prty_id
FROM meters
WHERE code = p_existing_metr_code;
ELSE
-- we don't have real model details, chose the 'OTHER' meter
SELECT prty_id
INTO l_rec_meter_reqs.existing_prty_id
FROM meters
WHERE code = 'OTHER';
l_rec_meter_reqs.existing_metr_code := 'OTHER';
l_rec_meter_reqs.existing_mety_code := p_existing_mety_code;
l_rec_meter_reqs.existing_mesc_code := p_existing_mesc_code;
END IF;
END IF;
-- consider required meter
IF NOT l_required_meter_enquiry THEN
l_rec_meter_reqs.qmax := NULL;
l_rec_meter_reqs.required_mesc_code := NULL;
l_rec_meter_reqs.required_mesc_code := p_required_mesc_code;
ELSE
IF nvl(l_rec_meter_reqs.required_mesc_code
,'OTHER') <> 'OTHER' THEN
l_rec_meter_reqs.qmax := round(mip_quotation.get_qmax_from_mesc(p_mesc_code => l_rec_meter_reqs.required_mesc_code)
,3);
ELSE
l_rec_meter_reqs.required_mesc_code := nvl(p_required_mesc_code
,'OTHER');
l_rec_meter_reqs.qmax := p_qmax;
END IF;
END IF;
pl('calc_meter_reqs:exit:' || l_rec_meter_reqs.existing_mesc_code || ':' ||
l_rec_meter_reqs.required_mesc_code || ':' ||
l_rec_meter_reqs.existing_metr_code || ':' || l_rec_meter_reqs.qmax);
RETURN l_rec_meter_reqs;
END calc_meter_reqs;
END mip_enquiry;
/