Sei sulla pagina 1di 2

set serveroutput on size 100000

DECLARE

CURSOR potoreset is
SELECT wf_item_type, wf_item_key, po_header_id, segment1,
revision_num, type_lookup_code
FROM po_headers_all
WHERE segment1 = '&po_number'
and org_id = &org_id
and authorization_status IN ('IN PROCESS', 'PRE-APPROVED')
and NVL(cancel_flag, 'N') = 'N'
and NVL(closed_code, 'OPEN') != 'FINALLY_CLOSED';

CURSOR maxseq(id number, subtype po_action_history.object_sub_type_code%type) is


SELECT nvl(max(sequence_num), 0)
FROM po_action_history
WHERE object_type_code IN ('PO', 'PA')
AND object_sub_type_code = subtype
AND object_id = id
AND action_code is NULL;

CURSOR poaction(id number, subtype po_action_history.object_sub_type_code%type) is


SELECT nvl(max(sequence_num), 0)
FROM po_action_history
WHERE object_type_code IN ('PO', 'PA')
AND object_sub_type_code = subtype
AND object_id = id
AND action_code = 'SUBMIT';

submitseq po_action_history.sequence_num%type;
nullseq po_action_history.sequence_num%type;

BEGIN
dbms_output.put_line('------------------------------------');
dbms_output.put_line('Data Manipulation Scripts Disclaimer');
dbms_output.put_line('------------------------------------');
dbms_output.put_line('As always please ask customer to run the scripts on their
test instance first ');
dbms_output.put_line('before applying it on production. Make sure the data is
validated for ');
dbms_output.put_line('correctness and related functionality is verified after the
script has been ');
dbms_output.put_line('run on a test instance. Customer is responsible to
authenticate and verify ');
dbms_output.put_line('correctness of data manipulation scripts.');

FOR pos in potoreset LOOP


dbms_output.put_line('Processing '||pos.type_lookup_code
||' PO Number: '
||pos.segment1);
dbms_output.put_line('......................................');

dbms_output.put_line('Closing Notifications...');
BEGIN

UPDATE wf_notifications set status = 'CANCELED'


WHERE notification_id in (
select ias.notification_id
from wf_item_activity_statuses ias,
wf_notifications ntf
where ias.item_type = pos.wf_item_type
and ias.item_key = pos.wf_item_key
and ntf.notification_id = ias.notification_id)
AND NVL(status, 'OPEN') = 'OPEN';

EXCEPTION
WHEN OTHERS THEN
null;
END;

dbms_output.put_line('Aborting Workflow...');
BEGIN
WF_Engine.AbortProcess(pos.wf_item_type, pos.wf_item_key);
EXCEPTION
WHEN OTHERS THEN
null;
END;

dbms_output.put_line('Updating PO Status...');
UPDATE po_headers_all
set authorization_status = decode(pos.revision_num, 0, 'INCOMPLETE',
'REQUIRES REAPPROVAL'),
wf_item_type = NULL,
wf_item_key = NULL,
approved_flag = decode(pos.revision_num, 0, 'N', 'R')
where po_header_id = pos.po_header_id;

OPEN maxseq(pos.po_header_id, pos.type_lookup_code);


FETCH maxseq into nullseq;
CLOSE maxseq;

OPEN poaction(pos.po_header_id, pos.type_lookup_code);


FETCH poaction into submitseq;
CLOSE poaction;

IF nullseq > submitseq THEN

dbms_output.put_line('Deleting PO Action History...');

DELETE FROM po_action_history


WHERE object_id = pos.po_header_id
AND object_type_code IN ('PO', 'PA')
AND object_sub_type_code = pos.type_lookup_code
AND sequence_num >= submitseq;
END IF;

dbms_output.put_line('Done Processing.');
dbms_output.put_line('................');
dbms_output.put_line('Please issue commit, if no errors found.');

END LOOP;
END;
/

Potrebbero piacerti anche