Você está na página 1de 41

CREATE OR REPLACE PACKAGE BODY APCRW.

"APC_ORDER_SUMMARY_PKG" AS
/* This function is not used as of 08-JAN-2009*/
FUNCTION APC_order_summ_status_fn (
poid NUMBER)
RETURN VARCHAR2 IS
status
VARCHAR2 (100);
l_auth_status VARCHAR2 (100);
l_amt_paid
NUMBER;
l_po_amt
NUMBER;
l_flag
VARCHAR2 (1)
:= '0';
CURSOR shipmt_lines_cur IS
SELECT shipment_line_status_code status_code
FROM
rcv_shipment_lines
WHERE po_header_id = poid;
BEGIN
SELECT authorization_status
INTO
l_auth_status
FROM
po_headers_all
WHERE po_header_id = poid;
SELECT
INTO
FROM
WHERE

SUM (inv.amount_paid)
l_amt_paid
ap_invoices_all inv
inv.invoice_id IN (SELECT invoice_id
FROM
ap_invoice_lines_all invl
WHERE invl.po_header_id = poid);

SELECT
INTO
FROM
WHERE

SUM (pol1.unit_price * pol1.quantity)


l_po_amt
po_lines_all pol1
pol1.po_header_id = poid;

FOR cur_rec IN shipmt_lines_cur


LOOP
IF cur_rec.status_code <> 'FULLY RECEIVED' THEN
l_flag := '1';
EXIT;
ELSE
l_flag := '0';
END IF;
END LOOP;
IF l_auth_status = 'APPROVED' THEN
IF l_flag = '0' THEN
status := 'Receive Complete';
ELSIF l_flag = '1' THEN
status := 'Partial Receipts';
ELSE
status := 'PO Sent to Supplier';
END IF;
END IF;
IF l_auth_status = 'REJECTED' THEN

status := 'Rejected';
END IF;
IF l_auth_status = 'IN PROCESS' THEN
status := 'Pending Approval';
END IF;
IF l_auth_status = 'INCOMPLETE' THEN
status := 'PO Created/In Process';
END IF;
IF l_auth_status = 'REQUIRES REAPPROVAL' THEN
status := 'Requires Reapproval';
END IF;
IF l_auth_status = 'APPROVED' THEN
IF l_amt_paid > 0 THEN
IF l_amt_paid = l_po_amt THEN
status := 'Paid in Full';
ELSIF l_amt_paid < l_po_amt THEN
status := 'Partial Payment';
ELSE
status := 'Payment Pending';
END IF;
END IF;
END IF;
RETURN status;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_order_summ_status_fn;
/*Used to calculate the invoice status associated with a purchase
order */
FUNCTION APC_inv_status_fn (
poid NUMBER)
RETURN VARCHAR2 IS
status
VARCHAR2 (100);
l_amt_paid NUMBER;
l_inv_amt
NUMBER;
l_po_amt
NUMBER;
l_flag
VARCHAR2 (1)
:= '0';
BEGIN
SELECT SUM (inv.amount_paid)
INTO
l_amt_paid
FROM
ap_invoices_all inv
WHERE inv.invoice_id IN (SELECT invoice_id
FROM
ap_invoice_lines_all invl
WHERE invl.po_header_id = poid);
SELECT SUM (inv.invoice_amount)
INTO
l_inv_amt
FROM
ap_invoices_all inv

WHERE

inv.invoice_id IN (SELECT invoice_id


FROM
ap_invoice_lines_all invl
WHERE invl.po_header_id = poid);

SELECT
INTO
FROM
WHERE

SUM (pol1.unit_price * pol1.quantity)


l_po_amt
po_lines_all pol1
pol1.po_header_id = poid;

IF l_amt_paid > 0 THEN


IF
l_amt_paid = l_inv_amt
AND (
l_amt_paid > l_po_amt
OR l_amt_paid = l_po_amt) THEN
status := 'Paid in Full';
ELSIF l_amt_paid < l_inv_amt THEN
status := 'Partial Payment';
ELSIF l_amt_paid < l_po_amt THEN
status := 'Under Paid';
ELSIF l_amt_paid > l_inv_amt THEN
status := 'Over Paid';
ELSE
status := '';
END IF;
ELSE
status := ' ';
END IF;
RETURN status;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_inv_status_fn;
/* Used to report the receipt status of an order */
FUNCTION APC_receipt_status_fn (
poid NUMBER)
RETURN VARCHAR2 IS
status
VARCHAR2 (100);
l_auth_status VARCHAR2 (100);
l_amt_paid
NUMBER;
l_inv_amt
NUMBER;
l_po_amt
NUMBER;
l_flag
VARCHAR2 (1)
:= '0';
l_rct_flag
VARCHAR2 (1);
l_ins_flag
VARCHAR2 (1);
l_count
NUMBER;
CURSOR shipmt_lines_cur IS
SELECT shipment_line_status_code status_code
FROM
rcv_shipment_lines
WHERE po_header_id = poid
AND
quantity_received > 0;
BEGIN
SELECT COUNT (1)

INTO
FROM
WHERE
AND

l_count
rcv_shipment_lines
po_header_id = poid
quantity_received > 0;

SELECT DISTINCT
,
INTO
,
FROM
WHERE

receipt_required_flag
inspection_required_flag
l_rct_flag
l_ins_flag
po_line_locations_all
po_header_id = poid;

SELECT
INTO
FROM
WHERE

SUM (inv.amount_paid)
l_amt_paid
ap_invoices_all inv
inv.invoice_id IN (SELECT invoice_id
FROM
ap_invoice_lines_all invl
WHERE invl.po_header_id = poid);

SELECT
INTO
FROM
WHERE

SUM (inv.invoice_amount)
l_inv_amt
ap_invoices_all inv
inv.invoice_id IN (SELECT invoice_id
FROM
ap_invoice_lines_all invl
WHERE invl.po_header_id = poid);

FOR cur_rec IN shipmt_lines_cur


LOOP
IF cur_rec.status_code <> 'FULLY RECEIVED' THEN
l_flag := '1';
EXIT;
ELSE
l_flag := '0';
END IF;
END LOOP;
-IF l_count = 0 THEN status := 'Receiving not Required';
IF l_count > 0 THEN
IF l_ins_flag = 'N' THEN
IF l_rct_flag = 'N' THEN
status := 'Receive Complete'; -- 2-way matching
ELSIF
l_rct_flag = 'Y'
AND l_flag = '1' -- 3 way matching not fully recived
THEN
status := 'Partial Receipts';
ELSE
status := 'Receive Complete';
END IF;
END IF;
ELSIF
l_inv_amt > 0
AND l_inv_amt = l_amt_paid THEN
status := 'Receive Complete';
ELSE
status := '';

END IF;
RETURN status;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_receipt_status_fn;
FUNCTION APC_order_invamt_fn (
poid NUMBER)
RETURN NUMBER IS
status
VARCHAR2 (100);
l_auth_status VARCHAR2 (100);
l_amt_paid
NUMBER;
l_po_amt
NUMBER;
l_flag
VARCHAR2 (1)
:= '0';
BEGIN
SELECT NVL (SUM (inv.invoice_amount), 0)
INTO
l_amt_paid
FROM
ap_invoices_all inv
WHERE inv.invoice_id IN (SELECT invoice_id
FROM
ap_invoice_lines_all invl
WHERE invl.po_header_id = poid);
RETURN l_amt_paid;
EXCEPTION
WHEN OTHERS THEN
RETURN 0;
END APC_order_invamt_fn;
FUNCTION APC_order_invamtd_fn (
poid
NUMBER
, lineid NUMBER)
RETURN NUMBER IS
status
VARCHAR2 (100);
l_auth_status VARCHAR2 (100);
l_amt_paid
NUMBER;
l_po_amt
NUMBER;
l_flag
VARCHAR2 (1)
:= '0';
BEGIN
SELECT NVL (SUM (inv.amount), 0)
INTO
l_amt_paid
FROM
ap_invoice_lines_all inv
WHERE po_header_id = poid
AND
po_line_id = lineid
AND
NVL (inv.discarded_flag, 'N') <> 'Y';
RETURN l_amt_paid;
EXCEPTION
WHEN OTHERS THEN
RETURN 0;
END APC_order_invamtd_fn;
FUNCTION APC_order_invnum_fn (

poid
NUMBER
, lineid NUMBER)
RETURN VARCHAR2 IS
status
VARCHAR2
l_inv_num
VARCHAR2
l_amt_paid NUMBER;
l_inv_cur
VARCHAR2
l_flag
VARCHAR2
l_count
NUMBER;
l_tot_rows NUMBER;

(100);
(1050);
(1050);
(1)
:= '0';

CURSOR inv_cur IS
SELECT invoice_num
FROM
ap_invoices_all
WHERE invoice_id IN (SELECT invoice_id
FROM
ap_invoice_lines_all inv
WHERE po_header_id = poid
AND
po_line_id = lineid);
BEGIN
SELECT COUNT (DISTINCT invoice_num)
INTO
l_count
FROM
ap_invoices_all
WHERE invoice_id IN (SELECT invoice_id
FROM
ap_invoice_lines_all inv
WHERE po_header_id = poid
AND
po_line_id = lineid);
IF l_count = 1 THEN
SELECT invoice_num
INTO
l_inv_num
FROM
ap_invoices_all
WHERE invoice_id IN (SELECT invoice_id
FROM
ap_invoice_lines_all inv
WHERE po_header_id = poid
AND
po_line_id = lineid);
ELSIF l_count = 0 THEN
l_inv_num := '';
ELSE
-- l_inv_cur:=NULL;-- Added by Mayank S
FOR cur_rec IN inv_cur
LOOP
l_inv_cur := cur_rec.invoice_num || ',' || l_inv_cur;
END LOOP;
l_inv_num := l_inv_cur;
END IF;
RETURN l_inv_num;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_order_invnum_fn;
FUNCTION APC_order_poamt_fn (

poid NUMBER)
RETURN NUMBER IS
status
VARCHAR2 (100);
l_auth_status VARCHAR2 (100);
l_amt_paid
NUMBER;
l_po_amt
NUMBER;
l_flag
VARCHAR2 (1)
:= '0';
BEGIN
SELECT SUM (pol1.unit_price * pol1.quantity)
INTO
l_amt_paid
FROM
po_lines_all pol1
WHERE pol1.po_header_id = poid;
RETURN l_amt_paid;
EXCEPTION
WHEN OTHERS THEN
RETURN 0;
END APC_order_poamt_fn;
FUNCTION APC_order_amtpaidd_fn (
poid
NUMBER
, lineid NUMBER)
RETURN NUMBER IS
l_val
VARCHAR2 (100);
l_amt_paid
NUMBER;
l_po_amt
NUMBER;
l_freight
NUMBER;
l_line_count NUMBER;
BEGIN
-- check if there are any freight lines for this PO. l_amt_paid
will be zero if there frieght
SELECT
INTO
FROM
WHERE
AND

SUM (NVL (amount, 0) )


l_freight
ap_invoice_lines_all
line_type_lookup_code = 'FREIGHT'
invoice_id IN (SELECT invoice_id
FROM
ap_invoice_lines_all
WHERE po_header_id = poid
AND
po_line_id = lineid);

-- check if this line is put in po lines are freight line


SELECT
INTO
FROM
WHERE
AND
AND

COUNT (1)
l_line_count
po_lines_all
po_header_id = poid
po_line_id = lineid
UPPER (item_description) LIKE 'FREIGHT%';

SELECT SUM (NVL (amt, 0) )


INTO
l_amt_paid
FROM
(SELECT NVL (ipa.amount, 0) * (inv_line.amount /
invoice_amt.amt) amt

FROM

ap_invoice_lines_all inv_line
, ap_invoice_payments_all ipa
, ap.ap_checks_all ac
, (SELECT
ila.invoice_id
, SUM (ila.amount) amt
FROM
ap_invoice_lines_all ila
WHERE
NVL (ila.discarded_flag, 'N') <> 'Y'
AND
ila.invoice_id IN (SELECT DISTINCT

aila.invoice_id
FROM
ap_invoice_lines_all aila
WHERE
aila.po_header_id = poid
AND
aila.po_line_id = lineid)
GROUP BY invoice_id) invoice_amt
WHERE po_header_id = poid
AND
po_line_id = lineid
AND
ipa.check_id = ac.check_id
AND
NVL (ac.status_lookup_code, 'NULL') <> 'VOIDED'
AND
NVL (inv_line.discarded_flag, 'N') <> 'Y'
AND
inv_line.invoice_id = ipa.invoice_id
AND
invoice_amt.invoice_id = inv_line.invoice_id);
IF

l_amt_paid = 0
AND l_freight > 0
AND l_line_count > 0 THEN
l_val := 'There is freight for this PO on PO Line Level';
l_po_amt := l_freight;
ELSE
l_po_amt := l_amt_paid;
END IF;
RETURN

NVL (l_po_amt, 0)
+ NVL (APC_order_summary_pkg.APC_order_invtaxamt_fn (poid,

lineid), 0);
EXCEPTION
WHEN OTHERS THEN
RETURN 0;
END APC_order_amtpaidd_fn;
FUNCTION APC_order_amtpaid_fn (
poid NUMBER)
RETURN NUMBER IS
l_amt_paid NUMBER;
BEGIN
SELECT NVL (SUM (inv.amount_paid), 0)
INTO
l_amt_paid
FROM
ap_invoices_all inv
WHERE inv.invoice_id IN (SELECT invoice_id
FROM
ap_invoice_lines_all invl
WHERE invl.po_header_id = poid);
RETURN l_amt_paid;

EXCEPTION
WHEN OTHERS THEN
RETURN 0;
END APC_order_amtpaid_fn;
FUNCTION APC_order_exporg_fn (
poid NUMBER)
RETURN VARCHAR2 IS
l_exporg VARCHAR2 (300);
l_count
NUMBER;
l_org
VARCHAR2 (300);
BEGIN
SELECT COUNT (DISTINCT expenditure_organization_id)
INTO
l_count
FROM
po_distributions_all pod
WHERE pod.po_header_id = poid;
IF l_count = 1 THEN
SELECT DISTINCT op.NAME
INTO
l_org
FROM
po_distributions_all pod
, hr_all_organization_units op
WHERE
pod.po_header_id = poid
AND
pod.expenditure_organization_id =
op.organization_id;
END IF;
IF l_count > 1 THEN
l_exporg := 'Various';
ELSIF l_count = 0 THEN
l_exporg := '';
ELSE
l_exporg := l_org;
END IF;
RETURN l_exporg;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_order_exporg_fn;
FUNCTION APC_order_exptype_fn (
poid NUMBER)
RETURN VARCHAR2 IS
l_exptype VARCHAR2 (300);
l_count
NUMBER;
l_exp
VARCHAR2 (300);
BEGIN
SELECT COUNT (DISTINCT expenditure_type)
INTO
l_count
FROM
po_distributions_all pod
WHERE pod.po_header_id = poid;
IF l_count = 1 THEN

SELECT DISTINCT expenditure_type


INTO
l_exp
FROM
po_distributions_all pod
WHERE
pod.po_header_id = poid
AND
expenditure_type IS NOT NULL;
END IF;
IF l_count > 1 THEN
l_exptype := 'Various';
ELSIF l_count = 0 THEN
l_exptype := '';
ELSE
l_exptype := l_exp;
END IF;
RETURN l_exptype;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_order_exptype_fn;
FUNCTION APC_order_task_fn (
poid NUMBER)
RETURN VARCHAR2 IS
l_task
VARCHAR2 (300);
l_count
NUMBER;
l_task_num VARCHAR2 (300);
BEGIN
SELECT COUNT (DISTINCT task_id)
INTO
l_count
FROM
po_distributions_all pod
WHERE pod.po_header_id = poid;
IF l_count = 1 THEN
SELECT DISTINCT task_number
INTO
l_task
FROM
pa_tasks pap
, po_distributions_all pod
WHERE
pap.task_id = pod.task_id
AND
pod.po_header_id = poid;
END IF;
IF l_count > 1 THEN
l_task_num := 'Various';
ELSIF l_count = 0 THEN
l_task_num := '';
ELSE
l_task_num := l_task;
END IF;
RETURN l_task_num;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;

END APC_order_task_fn;
FUNCTION APC_order_project_fn (
poid NUMBER)
RETURN VARCHAR2 IS
l_project
VARCHAR2 (300);
l_proj_num VARCHAR2 (300);
l_count
NUMBER;
BEGIN
SELECT COUNT (DISTINCT project_id)
INTO
l_count
FROM
po_distributions_all pod
WHERE pod.po_header_id = poid;
IF l_count = 1 THEN
SELECT DISTINCT pap.segment1
INTO
l_project
FROM
pa_projects_all pap
, po_distributions_all pod
WHERE
pap.project_id = pod.project_id
AND
pod.po_header_id = poid;
END IF;
IF l_count > 1 THEN
l_proj_num := 'Various';
ELSIF l_count = 0 THEN
l_proj_num := '';
ELSE
l_proj_num := l_project;
END IF;
RETURN l_proj_num;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_order_project_fn;
FUNCTION APC_order_project_type_fn (
poid NUMBER)
RETURN VARCHAR2 IS
l_project
VARCHAR2 (300);
l_proj_num VARCHAR2 (300);
l_count
NUMBER;
BEGIN
SELECT COUNT (DISTINCT project_id)
INTO
l_count
FROM
po_distributions_all pod
WHERE pod.po_header_id = poid;
IF l_count = 1 THEN
SELECT DISTINCT CASE
WHEN pap.project_type LIKE 'TECH OPS%' THEN
SUBSTR (pap.NAME, 5)
ELSE pap.NAME

INTO
FROM
WHERE
AND
END IF;

END
l_project
pa_projects_all pap
, po_distributions_all pod
pap.project_id = pod.project_id
pod.po_header_id = poid;

IF l_count > 1 THEN


l_proj_num := 'Various';
ELSIF l_count = 0 THEN
l_proj_num := '';
ELSE
l_proj_num := l_project;
END IF;
RETURN l_proj_num;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_order_project_type_fn;
FUNCTION APC_order_receiptd_date_fn (
poid
NUMBER
, lineid NUMBER
, llid
NUMBER)
RETURN VARCHAR2 IS
l_date
VARCHAR2 (1000);
l_receipt VARCHAR2 (50);
l_count
NUMBER;
-- Start modification by Mayank S on 07-JUN-2011
CURSOR c_receipt_date IS
SELECT DISTINCT TO_CHAR (rsh.creation_date, 'DD-MON-YYYY')
receipt_date
, rsh.creation_date
FROM
rcv_transactions rcv
, rcv_shipment_headers rsh
, rcv_shipment_lines rsl
WHERE
rcv.shipment_header_id =
rsh.shipment_header_id(+)
AND
rsh.shipment_header_id =
rsl.shipment_header_id(+)
AND
rcv.shipment_line_id = rsl.shipment_line_id
AND
rsl.quantity_received > 0
AND
rsl.po_header_id = poid
AND
rcv.po_line_location_id = llid
AND
rsl.po_line_id = lineid
AND
rsl.po_line_location_id =
rcv.po_line_location_id
AND
rcv.transaction_type = 'RECEIVE'
ORDER BY
rsh.creation_date;
-- End modification by Mayank S on 07-JUN-2011
BEGIN

-- Start modification by Mayank S on 07-JUN-2011


l_date := NULL;
/*SELECT COUNT (DISTINCT rsh.receipt_num)
INTO
l_count
FROM
rcv_transactions rcv
, rcv_shipment_headers rsh
, rcv_shipment_lines rsl
WHERE rcv.shipment_header_id = rsh.shipment_header_id(+)
AND
rsh.shipment_header_id = rsl.shipment_header_id(+)
AND
rcv.shipment_line_id = rsl.shipment_line_id
AND
rsl.quantity_received > 0
AND
rsl.po_header_id = poid
AND
rcv.po_line_location_id = llid
AND
rsl.po_line_id = lineid
AND
rsl.po_line_location_id = rcv.po_line_location_id
AND
rcv.transaction_type = 'RECEIVE';
IF l_count = 1 THEN
SELECT DISTINCT TO_CHAR (rsh.creation_date, 'DD-MON-YYYY')
INTO
l_date
FROM
rcv_transactions rcv
, rcv_shipment_headers rsh
, rcv_shipment_lines rsl
WHERE
rcv.shipment_header_id =
rsh.shipment_header_id(+)
AND
rsh.shipment_header_id =
rsl.shipment_header_id(+)
AND
rcv.shipment_line_id = rsl.shipment_line_id
AND
rsl.quantity_received > 0
AND
rsl.po_header_id = poid
AND
rcv.po_line_location_id = llid
AND
rsl.po_line_id = lineid
AND
rsl.po_line_location_id =
rcv.po_line_location_id
AND
rcv.transaction_type = 'RECEIVE';
ELSIF l_count = 0 THEN
l_receipt := '';
ELSE
SELECT DISTINCT TO_CHAR (rsh.creation_date, 'DD-MON-YYYY')
INTO
l_date
FROM
rcv_transactions rcv
, rcv_shipment_headers rsh
, rcv_shipment_lines rsl
WHERE
rcv.shipment_header_id =
rsh.shipment_header_id(+)
AND
rsh.shipment_header_id =
rsl.shipment_header_id(+)
AND
rcv.shipment_line_id = rsl.shipment_line_id
AND
rsl.quantity_received > 0
AND
rsl.po_header_id = poid
AND
rcv.po_line_location_id = llid
AND
rsl.po_line_id = lineid

AND
rsl.po_line_location_id =
rcv.po_line_location_id
AND
rcv.transaction_type = 'RECEIVE'
AND
ROWNUM < 2;
END IF;*/
FOR receipt_date_rec IN c_receipt_date
LOOP
IF l_date IS NULL THEN
l_date := SUBSTR (receipt_date_rec.receipt_date, 1, 1000);
ELSE
l_date := SUBSTR (l_date || ',' ||
receipt_date_rec.receipt_date, 1, 1000);
END IF;
END LOOP;
RETURN l_date;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_order_receiptd_date_fn;
FUNCTION APC_order_receipt_date_fn (
poid NUMBER)
RETURN VARCHAR2 IS
l_date VARCHAR2 (15);
BEGIN
SELECT TO_CHAR (MAX (rsh.creation_date),
INTO
l_date
FROM
rcv_shipment_headers rsh
WHERE rsh.shipment_header_id IN (SELECT
FROM
WHERE
'RECEIVE'
AND
(SELECT line_location_id
FROM

po_line_locations_all

WHERE

po_header_id = poid) );

RETURN l_date;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_order_receipt_date_fn;
FUNCTION APC_order_receiptnum_fn (
poid
NUMBER
, lineid NUMBER
, llid
NUMBER)
RETURN VARCHAR2 IS
l_receipt VARCHAR2 (1000);
l_count
NUMBER;

'DD-MON-YYYY')
shipment_header_id
rcv_transactions
transaction_type(+) =
po_line_location_id IN

-- Start modification by Mayank S on 07-JUN-2011


CURSOR c_receipt_num IS
SELECT DISTINCT rsh.receipt_num receipt_number
, rsh.creation_date
FROM
rcv_transactions rcv
, rcv_shipment_headers rsh
, rcv_shipment_lines rsl
WHERE
rcv.shipment_header_id =
rsh.shipment_header_id(+)
AND
rsh.shipment_header_id =
rsl.shipment_header_id(+)
AND
rcv.shipment_line_id = rsl.shipment_line_id
AND
rsl.quantity_received > 0
AND
rsl.po_header_id = poid
AND
rcv.po_line_location_id = llid
AND
rsl.po_line_id = lineid
AND
rsl.po_line_location_id =
rcv.po_line_location_id
AND
rcv.transaction_type = 'RECEIVE'
ORDER BY
rsh.creation_date;
-- End modification by Mayank S on 07-JUN-2011
BEGIN
-- Start modification by Mayank S on 07-JUN-2011
l_receipt := NULL;
/*
SELECT
INTO
FROM
,
,
WHERE
AND
AND
AND
AND
AND
AND
AND
AND

COUNT (DISTINCT rsh.receipt_num)


l_count
rcv_transactions rcv
rcv_shipment_headers rsh
rcv_shipment_lines rsl
rcv.shipment_header_id = rsh.shipment_header_id(+)
rsh.shipment_header_id = rsl.shipment_header_id(+)
rcv.shipment_line_id = rsl.shipment_line_id
rsl.quantity_received > 0
rsl.po_header_id = poid
rcv.po_line_location_id = llid
rsl.po_line_id = lineid
rsl.po_line_location_id = rcv.po_line_location_id
rcv.transaction_type = 'RECEIVE';

IF l_count = 1 THEN
SELECT DISTINCT rsh.receipt_num
INTO
l_receipt
FROM
rcv_transactions rcv
, rcv_shipment_headers rsh
, rcv_shipment_lines rsl
WHERE
rcv.shipment_header_id =
rsh.shipment_header_id(+)
AND
rsh.shipment_header_id =
rsl.shipment_header_id(+)
AND
rcv.shipment_line_id =
rsl.shipment_line_id
AND
rsl.quantity_received > 0

AND
rsl.po_header_id = poid
AND
rcv.po_line_location_id = llid
AND
rsl.po_line_id = lineid
AND
rsl.po_line_location_id =
rcv.po_line_location_id
AND
rcv.transaction_type = 'RECEIVE';
ELSIF l_count = 0 THEN
l_receipt := '';
ELSE
SELECT DISTINCT rsh.receipt_num
INTO
l_receipt
FROM
rcv_transactions rcv
, rcv_shipment_headers rsh
, rcv_shipment_lines rsl
WHERE
rcv.shipment_header_id =
rsh.shipment_header_id(+)
AND
rsh.shipment_header_id =
rsl.shipment_header_id(+)
AND
rcv.shipment_line_id =
rsl.shipment_line_id
AND
rsl.quantity_received > 0
AND
rsl.po_header_id = poid
AND
rcv.po_line_location_id = llid
AND
rsl.po_line_id = lineid
AND
rsl.po_line_location_id =
rcv.po_line_location_id
AND
rcv.transaction_type = 'RECEIVE'
AND
ROWNUM < 2;
END IF;
*/
FOR receipt_num_rec IN c_receipt_num
LOOP
IF l_receipt IS NULL THEN
l_receipt := SUBSTR (receipt_num_rec.receipt_number, 1,
1000);
ELSE
l_receipt := SUBSTR (l_receipt || ',' ||
receipt_num_rec.receipt_number, 1, 1000);
END IF;
END LOOP;
-- End modification by Mayank S on 07-JUN-2011
RETURN l_receipt;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_order_receiptnum_fn;
--------------------------Added by Mayank S for Receipt Amount column
FUNCTION APC_order_receiptamt_fn (
poid
NUMBER
, lineid NUMBER

, llid
NUMBER)
RETURN NUMBER IS
l_receipt
VARCHAR2 (50);
l_count
NUMBER;
l_receipt_amt NUMBER;
BEGIN
-- Start modification by Mayank S 07-JUN-2011
/*
SELECT COUNT (DISTINCT rsh.receipt_num)
INTO
l_count
FROM
rcv_transactions rcv
, rcv_shipment_headers rsh
, rcv_shipment_lines rsl
WHERE rcv.shipment_header_id = rsh.shipment_header_id(+)
AND
rsh.shipment_header_id = rsl.shipment_header_id(+)
AND
rcv.shipment_line_id = rsl.shipment_line_id
AND
rsl.quantity_received > 0
AND
rsl.po_header_id = poid
AND
rcv.po_line_location_id = llid
AND
rsl.po_line_id = lineid
AND
rsl.po_line_location_id = rcv.po_line_location_id
AND
rcv.transaction_type = 'RECEIVE';
IF l_count = 1 THEN
SELECT DISTINCT rsh.receipt_num
, rcv.quantity * rcv.po_unit_price
INTO
l_receipt
, l_receipt_amt
FROM
rcv_transactions rcv
, rcv_shipment_headers rsh
, rcv_shipment_lines rsl
WHERE
rcv.shipment_header_id =
rsh.shipment_header_id(+)
AND
rsh.shipment_header_id =
rsl.shipment_header_id(+)
AND
rcv.shipment_line_id = rsl.shipment_line_id
AND
rsl.quantity_received > 0
AND
rsl.po_header_id = poid
AND
rcv.po_line_location_id = llid
AND
rsl.po_line_id = lineid
AND
rsl.po_line_location_id =
rcv.po_line_location_id
AND
rcv.transaction_type = 'RECEIVE';
ELSIF l_count = 0 THEN
l_receipt := '';
ELSE
SELECT DISTINCT rsh.receipt_num
, rcv.quantity * rcv.po_unit_price
INTO
l_receipt
, l_receipt_amt*/
SELECT SUM (rcv.quantity * rcv.po_unit_price)
INTO
l_receipt_amt
-- End modification by Mayank S on 07-JUN-2011
FROM
rcv_transactions rcv

,
,
WHERE
AND
AND
AND
AND
AND
AND
AND
AND

rcv_shipment_headers rsh
rcv_shipment_lines rsl
rcv.shipment_header_id = rsh.shipment_header_id(+)
rsh.shipment_header_id = rsl.shipment_header_id(+)
rcv.shipment_line_id = rsl.shipment_line_id
rsl.quantity_received > 0
rsl.po_header_id = poid
rcv.po_line_location_id = llid
rsl.po_line_id = lineid
rsl.po_line_location_id = rcv.po_line_location_id
rcv.transaction_type = 'RECEIVE';

--AND
JUN-2011
--END IF;

ROWNUM < 2;

-- Commented by Mayank S on 07-

RETURN l_receipt_amt;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_order_receiptamt_fn;
--------------------------------------- End of Modification
----------------Added by Mayank S for Catalog Type column
FUNCTION APC_order_catalogtype_fn (
poid NUMBER)
RETURN VARCHAR2 IS
l_count
NUMBER;
v_catalog
VARCHAR2 (50);
v_catalog_m VARCHAR (50);
BEGIN
v_catalog_m := NULL;
SELECT
INTO
FROM
,
,
,
WHERE
AND
AND
AND
AND

COUNT (DISTINCT prl.catalog_type)


l_count
po_headers_all poh
po_distributions_all pod
po_req_distributions_all prd
po_requisition_lines_all prl
poh.po_header_id = pod.po_header_id
pod.req_distribution_id = prd.distribution_id
prd.requisition_line_id = prl.requisition_line_id
poh.po_header_id = poid
NOT EXISTS (SELECT 'x'
FROM
po_reqexpress_lines rxl
WHERE prl.item_description =
rxl.item_description); --Added this condition to eliminate Shopping List
Items
IF l_count = 1 THEN
SELECT DISTINCT NVL (INITCAP (prl.catalog_type), 'Null')
INTO
v_catalog
FROM
po_headers_all poh

, po_distributions_all pod
, po_req_distributions_all prd
, po_requisition_lines_all prl
WHERE
poh.po_header_id = pod.po_header_id
AND
pod.req_distribution_id = prd.distribution_id
AND
prd.requisition_line_id =
prl.requisition_line_id
AND
poh.po_header_id = poid
AND
NOT EXISTS (SELECT 'x'
FROM
po_reqexpress_lines rxl
WHERE prl.item_description =
rxl.item_description);
--and pod.distribution_id=PoDId;
RETURN v_catalog;
ELSIF l_count = 0 THEN
v_catalog := INITCAP ('NULL');
RETURN v_catalog;
ELSE
FOR x IN (SELECT DISTINCT NVL (INITCAP (prl.catalog_type),
'Null') catalog_type
FROM
po_headers_all poh
, po_distributions_all pod
, po_req_distributions_all prd
, po_requisition_lines_all prl
WHERE
poh.po_header_id = pod.po_header_id
AND
pod.req_distribution_id =
prd.distribution_id
AND
prd.requisition_line_id =
prl.requisition_line_id
AND
poh.po_header_id = poid
AND
NOT EXISTS (SELECT 'x'
FROM
po_reqexpress_lines
rxl
WHERE
prl.item_description = rxl.item_description) )
-and pod.distribution_id=PoDId)
LOOP
v_catalog := x.catalog_type;
v_catalog_m := v_catalog_m || '' || v_catalog || ',';
END LOOP;
RETURN SUBSTR (v_catalog_m, 1, LENGTH (v_catalog_m) - 1);
-- return v_catalog_m;
END IF;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_order_catalogtype_fn;
-----------------------------------------------------------Added by Mayank S to extract Invoice Tax Amount

FUNCTION APC_order_invtaxamt_fn (
poid
NUMBER
, polineid NUMBER)
RETURN NUMBER IS
/*CURSOR inv_cur is select invoice_id ,line_number
from ap_invoice_lines_all inv
where inv.line_type_lookup_code = 'TAX'
and inv.invoice_id in (select invoice_id from
ap_invoice_lines_all
where po_header_id = poid
and po_line_id= polineid);
*/
CURSOR inv_cur IS
SELECT invoice_id
, line_number
FROM
ap_invoice_lines_all inv
WHERE inv.line_type_lookup_code = 'TAX'
AND
inv.invoice_id IN (SELECT invoice_id
FROM
ap_invoice_lines_all
WHERE po_header_id = poid
AND
po_line_id = polineid);
v_total_taxamt NUMBER;
v_tax_amt
NUMBER;
BEGIN
v_total_taxamt := 0;
FOR cur_rec IN inv_cur
LOOP
SELECT
NVL (ail.amount, 0)
* (SELECT
NVL (pla.unit_price, 0)
* NVL (pla.quantity, 0)
/ (SELECT SUM (ila.amount)
FROM
ap_invoice_lines_all ila
WHERE NVL (ila.discarded_flag, 'N') <> 'Y'
AND
NVL (ila.line_type_lookup_code,
'LINE') <> 'TAX'
AND
ila.invoice_id =
(SELECT DISTINCT aila.invoice_id
FROM
ap_invoice_lines_all aila
WHERE
aila.po_header_id
= poid
AND
aila.po_line_id =
polineid) ) amt
FROM
po_lines_all pla
WHERE pla.po_header_id = poid
AND
pla.po_line_id = polineid) tax
INTO
v_tax_amt
FROM
ap_invoice_lines_all ail
WHERE ail.invoice_id = cur_rec.invoice_id
AND
ail.line_number = cur_rec.line_number
AND
ail.line_type_lookup_code = 'TAX'
AND
NVL (ail.discarded_flag, 'N') <> 'Y';

v_total_taxamt := v_total_taxamt + v_tax_amt;


END LOOP;
RETURN v_total_taxamt;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END;
-----------------------------------------------------Added By Mayank S for Approver Name
FUNCTION APC_order_appr_name (
req_hdr IN NUMBER)
RETURN VARCHAR2 IS
v_approver
VARCHAR2 (100);
v_approver_m VARCHAR2 (200);
v_count
NUMBER;
BEGIN
BEGIN
SELECT DISTINCT COUNT (ppf.full_name)
INTO
v_count
FROM
po_approval_list_headers pah
, po_approval_list_lines pal
, po_requisition_headers prh
, per_all_people_f ppf
WHERE
pah.approval_list_header_id =
pal.approval_list_header_id
AND
prh.wf_item_key = pah.wf_item_key
AND
prh.wf_item_type = pah.wf_item_type
--AND PRH.ATTRIBUTE_CATEGORY = 'EPR'
AND
pal.approver_id = ppf.person_id
AND
prh.authorization_status = 'APPROVED'
AND
prh.requisition_header_id = req_hdr;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END;
IF v_count = 1 THEN
SELECT ppf.full_name
INTO
v_approver
FROM
po_approval_list_headers pah
, po_approval_list_lines pal
, po_requisition_headers prh
, per_all_people_f ppf
WHERE pah.approval_list_header_id = pal.approval_list_header_id
AND
prh.wf_item_key = pah.wf_item_key
AND
prh.wf_item_type = pah.wf_item_type
--AND PRH.ATTRIBUTE_CATEGORY = 'EPR'
AND
pal.approver_id = ppf.person_id
AND
prh.authorization_status = 'APPROVED'
AND
prh.requisition_header_id = req_hdr;

RETURN v_approver;
ELSIF v_count > 1 THEN
FOR x IN (SELECT ppf.full_name
FROM
po_approval_list_headers pah
, po_approval_list_lines pal
, po_requisition_headers prh
, per_all_people_f ppf
WHERE pah.approval_list_header_id =
pal.approval_list_header_id
AND
prh.wf_item_key = pah.wf_item_key
AND
prh.wf_item_type = pah.wf_item_type
--AND PRH.ATTRIBUTE_CATEGORY = 'EPR'
AND
pal.approver_id = ppf.person_id
AND
prh.authorization_status = 'APPROVED'
AND
prh.requisition_header_id = req_hdr)
LOOP
v_approver := x.full_name;
v_approver_m := v_approver_m || '' || v_approver || ';';
END LOOP;
RETURN SUBSTR (v_approver_m, 1, LENGTH (v_approver_m) - 1);
ELSE
RETURN NULL;
END IF;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END;
--------------------------------------------------------Added by Mayank S for Requisition Approver as on 08-SEP-2011
FUNCTION APC_req_appr_name (req_hdr IN number)
RETURN VARCHAR2
IS
v_approver
VARCHAR2 (100);
v_approver_m
VARCHAR2 (200);
v_count
NUMBER;
BEGIN
BEGIN
SELECT DISTINCT count(ppf.full_name)
INTO v_count
FROM po_approval_list_headers pah,
po_approval_list_lines pal,
po_requisition_headers prh,
per_all_people_f ppf
WHERE
pah.approval_list_header_id = pal.approval_list_header_id
AND prh.wf_item_key = pah.wf_item_key
AND prh.wf_item_type = pah.wf_item_type
--AND PRH.ATTRIBUTE_CATEGORY = 'EPR'
AND pal.approver_id = ppf.person_id
AND pah.document_id = prh.requisition_header_id
AND prh.requisition_header_id = req_hdr;
EXCEPTION
WHEN OTHERS
THEN

RETURN NULL;
END;
IF v_count = 1
THEN
SELECT ppf.full_name
INTO v_approver
FROM po_approval_list_headers pah,
po_approval_list_lines pal,
po_requisition_headers prh,
per_all_people_f ppf
WHERE
pah.approval_list_header_id = pal.approval_list_header_id
AND prh.wf_item_key = pah.wf_item_key
AND prh.wf_item_type = pah.wf_item_type
--AND PRH.ATTRIBUTE_CATEGORY = 'EPR'
AND pal.approver_id = ppf.person_id
AND pah.document_id = prh.requisition_header_id
AND prh.requisition_header_id =req_hdr;
RETURN v_approver;
ELSIF v_count > 1
THEN
FOR x
IN (SELECT ppf.full_name
FROM po_approval_list_headers pah,
po_approval_list_lines pal,
po_requisition_headers prh,
per_all_people_f ppf
WHERE
pah.approval_list_header_id =
pal.approval_list_header_id
AND prh.wf_item_key = pah.wf_item_key
AND prh.wf_item_type = pah.wf_item_type
--AND PRH.ATTRIBUTE_CATEGORY = 'EPR'
AND pal.approver_id = ppf.person_id
AND pah.document_id = prh.requisition_header_id
AND prh.requisition_header_id = req_hdr)
LOOP
v_approver
:= x.full_name;
v_approver_m
:= v_approver_m || '' || v_approver || ';';
END LOOP;
RETURN SUBSTR (v_approver_m, 1, LENGTH (v_approver_m) - 1);
ELSE
RETURN NULL;
END IF;
EXCEPTION
WHEN OTHERS
THEN
RETURN NULL;
END;
-------------------------------------------------------Added by Mayank S for PO Approver Name
FUNCTION APC_po_order_appr_name (
po_hdr IN NUMBER)

RETURN VARCHAR2 IS
v_approver
VARCHAR2 (100);
v_approver_m VARCHAR2 (200);
v_count
NUMBER;
BEGIN
BEGIN
SELECT DISTINCT COUNT (ppf.full_name)
INTO
v_count
FROM per_all_people_f ppf, po_action_history pah
WHERE
ppf.person_id = pah.employee_id
AND pah.action_code = 'APPROVE'
AND pah.object_type_code = 'PO'
AND trunc(pah.ACTION_DATE) between
ppf.effective_start_date and ppf.effective_end_date
AND pah.object_id =po_hdr;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END;
IF v_count = 1 THEN
SELECT ppf.full_name
INTO
v_approver
FROM per_all_people_f ppf, po_action_history pah
WHERE
ppf.person_id = pah.employee_id
AND pah.action_code = 'APPROVE'
AND pah.object_type_code = 'PO'
AND trunc(pah.ACTION_DATE) between
ppf.effective_start_date and ppf.effective_end_date
AND pah.object_id =po_hdr;
RETURN v_approver;
ELSIF v_count > 1 THEN
FOR x IN (SELECT ppf.full_name
FROM per_all_people_f ppf, po_action_history pah
WHERE
ppf.person_id = pah.employee_id
AND pah.action_code = 'APPROVE'
AND pah.object_type_code = 'PO'
AND trunc(pah.ACTION_DATE) between
ppf.effective_start_date and ppf.effective_end_date
AND pah.object_id =po_hdr)
LOOP
v_approver := x.full_name;
v_approver_m := v_approver_m || '' || v_approver || ';';
END LOOP;
RETURN SUBSTR (v_approver_m, 1, LENGTH (v_approver_m) - 1);
ELSE
RETURN NULL;
END IF;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END;

---Added by Mayank S for PO Approver Name


FUNCTION APC_order_req_num_fn (
poid NUMBER)
RETURN VARCHAR2 IS
l_date
VARCHAR2 (15);
l_count
NUMBER;
l_req_num VARCHAR2 (10) := '';
l_req
VARCHAR2 (10) := '';
BEGIN
SELECT COUNT (DISTINCT e.segment1)
INTO
l_count
FROM
po_lines_all a
, po_headers_all f
, po_distributions_all b
, po_req_distributions_all c
, po_requisition_lines_all d
, po_requisition_headers_all e
WHERE f.po_header_id = poid
AND
a.po_header_id = f.po_header_id
AND
a.po_line_id = b.po_line_id
AND
b.req_distribution_id = c.distribution_id
AND
c.requisition_line_id = d.requisition_line_id
AND
d.requisition_header_id = e.requisition_header_id;
IF l_count = 1 THEN
SELECT DISTINCT e.segment1
INTO
l_req
FROM
po_lines_all a
, po_headers_all f
, po_distributions_all b
, po_req_distributions_all c
, po_requisition_lines_all d
, po_requisition_headers_all e
WHERE
f.po_header_id = poid
AND
a.po_header_id = f.po_header_id
AND
a.po_line_id = b.po_line_id
AND
b.req_distribution_id = c.distribution_id
AND
c.requisition_line_id = d.requisition_line_id
AND
d.requisition_header_id =
e.requisition_header_id;
END IF;
IF l_count > 1 THEN
l_req_num := 'Various';
ELSIF l_count = 0 THEN
l_req_num := '';
ELSE
l_req_num := l_req;
END IF;
RETURN l_req_num;
EXCEPTION
WHEN OTHERS THEN

RETURN NULL;
END APC_order_req_num_fn;
FUNCTION APC_order_req_name_fn (
poid NUMBER)
RETURN VARCHAR2 IS
l_date
VARCHAR2 (15);
l_count
NUMBER;
l_req_name VARCHAR2 (50) := '';
l_req
VARCHAR2 (50) := '';
BEGIN
SELECT COUNT (DISTINCT e.segment1)
INTO
l_count
FROM
po_lines_all a
, po_headers_all f
, po_distributions_all b
, po_req_distributions_all c
, po_requisition_lines_all d
, po_requisition_headers_all e
WHERE f.po_header_id = poid
AND
a.po_header_id = f.po_header_id
AND
a.po_line_id = b.po_line_id
AND
b.req_distribution_id = c.distribution_id
AND
c.requisition_line_id = d.requisition_line_id
AND
d.requisition_header_id = e.requisition_header_id;
IF l_count = 1 THEN
SELECT DISTINCT papf2.full_name
INTO
l_req
FROM
po_lines_all a
, po_headers_all f
, po_distributions_all b
, po_req_distributions_all c
, po_requisition_lines_all d
, po_requisition_headers_all e
, per_all_people_f papf2
WHERE
f.po_header_id = poid
AND
a.po_header_id = f.po_header_id
AND
a.po_line_id = b.po_line_id
AND
b.req_distribution_id = c.distribution_id
AND
c.requisition_line_id = d.requisition_line_id
AND
d.requisition_header_id =
e.requisition_header_id
AND
d.to_person_id = papf2.person_id(+)
AND
d.creation_date BETWEEN
papf2.effective_start_date AND papf2.effective_end_date;
END IF;
IF l_count > 1 THEN
l_req_name := 'Various';
ELSIF l_count = 0 THEN
l_req_name := '';
ELSE
l_req_name := l_req;

END IF;
RETURN l_req_name;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_order_req_name_fn;
FUNCTION APC_order_req_apprdate_fn (
poid NUMBER)
RETURN VARCHAR2 IS
l_date
VARCHAR2 (15);
l_count
NUMBER;
l_req_apprdate VARCHAR2 (50) := '';
l_req
VARCHAR2 (50) := '';
BEGIN
SELECT COUNT (DISTINCT e.segment1)
INTO
l_count
FROM
po_lines_all a
, po_headers_all f
, po_distributions_all b
, po_req_distributions_all c
, po_requisition_lines_all d
, po_requisition_headers_all e
WHERE f.po_header_id = poid
AND
a.po_header_id = f.po_header_id
AND
a.po_line_id = b.po_line_id
AND
b.req_distribution_id = c.distribution_id
AND
c.requisition_line_id = d.requisition_line_id
AND
d.requisition_header_id = e.requisition_header_id;
IF l_count = 1 THEN
SELECT DISTINCT TO_CHAR (e.approved_date, 'DD-MON-YYYY')
INTO
l_req
FROM
po_lines_all a
, po_headers_all f
, po_distributions_all b
, po_req_distributions_all c
, po_requisition_lines_all d
, po_requisition_headers_all e
WHERE
f.po_header_id = poid
AND
a.po_header_id = f.po_header_id
AND
a.po_line_id = b.po_line_id
AND
b.req_distribution_id = c.distribution_id
AND
c.requisition_line_id = d.requisition_line_id
AND
d.requisition_header_id =
e.requisition_header_id;
END IF;
IF l_count > 1 THEN
l_req_apprdate := 'Various';
ELSIF l_count = 0 THEN
l_req_apprdate := '';
ELSE

l_req_apprdate := l_req;
END IF;
RETURN l_req_apprdate;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_order_req_apprdate_fn;
FUNCTION APC_order_prep_name_fn (
poid NUMBER)
RETURN VARCHAR2 IS
l_date
VARCHAR2 (15);
l_count
NUMBER;
l_prep_name VARCHAR2 (50) := '';
l_req
VARCHAR2 (50) := '';
BEGIN
SELECT COUNT (DISTINCT e.segment1)
INTO
l_count
FROM
po_lines_all a
, po_headers_all f
, po_distributions_all b
, po_req_distributions_all c
, po_requisition_lines_all d
, po_requisition_headers_all e
WHERE f.po_header_id = poid
AND
a.po_header_id = f.po_header_id
AND
a.po_line_id = b.po_line_id
AND
b.req_distribution_id = c.distribution_id
AND
c.requisition_line_id = d.requisition_line_id
AND
d.requisition_header_id = e.requisition_header_id;
IF l_count = 1 THEN
SELECT DISTINCT papf2.full_name
INTO
l_req
FROM
po_lines_all a
, po_headers_all f
, po_distributions_all b
, po_req_distributions_all c
, po_requisition_lines_all d
, po_requisition_headers_all e
, per_all_people_f papf2
WHERE
f.po_header_id = poid
AND
a.po_header_id = f.po_header_id
AND
a.po_line_id = b.po_line_id
AND
b.req_distribution_id = c.distribution_id
AND
c.requisition_line_id = d.requisition_line_id
AND
d.requisition_header_id =
e.requisition_header_id
AND
e.preparer_id = papf2.person_id(+)
AND
d.creation_date BETWEEN
papf2.effective_start_date AND papf2.effective_end_date;
END IF;

IF l_count > 1 THEN


l_prep_name := 'Various';
ELSIF l_count = 0 THEN
l_prep_name := '';
ELSE
l_prep_name := l_req;
END IF;
RETURN l_prep_name;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_order_prep_name_fn;
/
*-----------------------------------------------------------------------------------------------------------Functions specific to the dretail report are below
-------------------------------------------------------------------------------------------------------------*/
FUNCTION APC_order_req_needdate_fn (
poid
NUMBER
, lineid NUMBER)
RETURN VARCHAR2 IS
l_date
VARCHAR2 (15);
l_count
NUMBER;
l_req_apprdate VARCHAR2 (50) := '';
l_req
VARCHAR2 (50) := '';
BEGIN
SELECT DISTINCT TO_CHAR (d.need_by_date, 'DD-MON-YYYY')
INTO
l_req
FROM
po_lines_all a
, po_headers_all f
, po_distributions_all b
, po_req_distributions_all c
, po_requisition_lines_all d
, po_requisition_headers_all e
WHERE
f.po_header_id = poid
AND
a.po_header_id = f.po_header_id
AND
a.po_line_id = b.po_line_id
AND
a.po_line_id = lineid
AND
b.req_distribution_id = c.distribution_id
AND
c.requisition_line_id = d.requisition_line_id
AND
d.requisition_header_id = e.requisition_header_id;
l_req_apprdate := l_req;
RETURN l_req_apprdate;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_order_req_needdate_fn;
FUNCTION APC_order_req_createdate_fn (

poid
NUMBER
, lineid NUMBER)
RETURN VARCHAR2
l_date
l_count
l_req_apprdate
l_req
BEGIN
SELECT DISTINCT
INTO
FROM
,
,
,
,
,
WHERE
AND
AND
AND
AND
AND
AND

IS
VARCHAR2 (15);
NUMBER;
VARCHAR2 (50) := '';
VARCHAR2 (50) := '';
TO_CHAR (e.creation_date, 'DD-MON-YYYY')
l_req
po_lines_all a
po_headers_all f
po_distributions_all b
po_req_distributions_all c
po_requisition_lines_all d
po_requisition_headers_all e
f.po_header_id = poid
a.po_header_id = f.po_header_id
a.po_line_id = b.po_line_id
a.po_line_id = lineid
b.req_distribution_id = c.distribution_id
c.requisition_line_id = d.requisition_line_id
d.requisition_header_id = e.requisition_header_id;

l_req_apprdate := l_req;
RETURN l_req_apprdate;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_order_req_createdate_fn;
FUNCTION APC_order_req_amt_fn (
poid
NUMBER
, lineid NUMBER)
RETURN NUMBER IS
l_date
VARCHAR2 (15);
l_amt
NUMBER;
l_req_apprdate VARCHAR2 (50) := '';
l_req
VARCHAR2 (50) := '';
BEGIN
SELECT DISTINCT TO_CHAR (d.unit_price * d.quantity)
INTO
l_amt
FROM
po_lines_all a
, po_headers_all f
, po_distributions_all b
, po_req_distributions_all c
, po_requisition_lines_all d
, po_requisition_headers_all e
WHERE
f.po_header_id = poid
AND
a.po_header_id = f.po_header_id
AND
a.po_line_id = b.po_line_id
AND
a.po_line_id = lineid
AND
b.req_distribution_id = c.distribution_id
AND
c.requisition_line_id = d.requisition_line_id

AND

d.requisition_header_id = e.requisition_header_id;

RETURN l_amt;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_order_req_amt_fn;
FUNCTION APC_order_reqd_num_fn (
poid
NUMBER
, lineid NUMBER)
RETURN VARCHAR2 IS
l_date
VARCHAR2 (15);
l_count
NUMBER;
l_req_num VARCHAR2 (10) := '';
l_req
VARCHAR2 (10) := '';
BEGIN
SELECT DISTINCT e.segment1
INTO
l_req
FROM
po_lines_all a
, po_headers_all f
, po_distributions_all b
, po_req_distributions_all c
, po_requisition_lines_all d
, po_requisition_headers_all e
WHERE
f.po_header_id = poid
AND
a.po_header_id = f.po_header_id
AND
a.po_line_id = b.po_line_id
AND
b.po_line_id = lineid
AND
b.req_distribution_id = c.distribution_id
AND
c.requisition_line_id = d.requisition_line_id
AND
d.requisition_header_id = e.requisition_header_id;
l_req_num := l_req;
RETURN l_req_num;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_order_reqd_num_fn;
FUNCTION APC_order_reqd_name_fn (
poid
NUMBER
, lineid NUMBER)
RETURN VARCHAR2 IS
l_date
VARCHAR2 (15);
l_count
NUMBER;
l_req_name VARCHAR2 (50) := '';
l_req
VARCHAR2 (50) := '';
BEGIN
SELECT DISTINCT papf2.full_name
INTO
l_req
FROM
po_lines_all a
, po_headers_all f
, po_distributions_all b

,
,
,
,

po_req_distributions_all c
po_requisition_lines_all d
po_requisition_headers_all e
per_all_people_f papf2
WHERE
f.po_header_id = poid
AND
a.po_header_id = f.po_header_id
AND
a.po_line_id = b.po_line_id
AND
a.po_line_id = lineid
AND
b.req_distribution_id = c.distribution_id
AND
c.requisition_line_id = d.requisition_line_id
AND
d.requisition_header_id = e.requisition_header_id
AND
d.to_person_id = papf2.person_id(+)
AND
d.creation_date BETWEEN papf2.effective_start_date
AND papf2.effective_end_date;
l_req_name := l_req;
RETURN l_req_name;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_order_reqd_name_fn;
FUNCTION APC_order_prepd_name_fn (
poid
NUMBER
, lineid NUMBER)
RETURN VARCHAR2 IS
l_date
VARCHAR2 (15);
l_count
NUMBER;
l_prep_name VARCHAR2 (50) := '';
l_req
VARCHAR2 (50) := '';
BEGIN
SELECT DISTINCT papf2.full_name
INTO
l_req
FROM
po_lines_all a
, po_headers_all f
, po_distributions_all b
, po_req_distributions_all c
, po_requisition_lines_all d
, po_requisition_headers_all e
, per_all_people_f papf2
WHERE
f.po_header_id = poid
AND
a.po_header_id = f.po_header_id
AND
a.po_line_id = b.po_line_id
AND
a.po_line_id = lineid
AND
b.req_distribution_id = c.distribution_id
AND
c.requisition_line_id = d.requisition_line_id
AND
d.requisition_header_id = e.requisition_header_id
AND
e.preparer_id = papf2.person_id(+)
AND
d.creation_date BETWEEN papf2.effective_start_date
AND papf2.effective_end_date;
l_prep_name := l_req;
RETURN l_prep_name;
EXCEPTION

WHEN OTHERS THEN


RETURN NULL;
END APC_order_prepd_name_fn;
FUNCTION APC_order_reqd_apprdate_fn (
poid
NUMBER
, lineid NUMBER)
RETURN VARCHAR2 IS
l_date
VARCHAR2 (15);
l_count
NUMBER;
l_req_apprdate VARCHAR2 (50) := '';
l_req
VARCHAR2 (50) := '';
BEGIN
SELECT DISTINCT TO_CHAR (e.approved_date, 'DD-MON-YYYY')
INTO
l_req
FROM
po_lines_all a
, po_headers_all f
, po_distributions_all b
, po_req_distributions_all c
, po_requisition_lines_all d
, po_requisition_headers_all e
WHERE
f.po_header_id = poid
AND
a.po_header_id = f.po_header_id
AND
a.po_line_id = b.po_line_id
AND
a.po_line_id = lineid
AND
b.req_distribution_id = c.distribution_id
AND
c.requisition_line_id = d.requisition_line_id
AND
d.requisition_header_id = e.requisition_header_id;
l_req_apprdate := l_req;
RETURN l_req_apprdate;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_order_reqd_apprdate_fn;
FUNCTION APC_order_epr_flag_fn (
poid
NUMBER
, lineid NUMBER)
RETURN VARCHAR2 IS
l_date
VARCHAR2 (15);
l_count
NUMBER;
l_req_num VARCHAR2 (10) := '';
l_req
VARCHAR2 (10) := '';
BEGIN
SELECT DISTINCT CASE
WHEN e.attribute_category = 'EPR' THEN 'Y'
ELSE 'N'
END
INTO
l_req
FROM
po_lines_all a
, po_headers_all f
, po_distributions_all b
, po_req_distributions_all c

WHERE
AND
AND
AND
AND
AND
AND

, po_requisition_lines_all d
, po_requisition_headers_all e
f.po_header_id = poid
a.po_header_id = f.po_header_id
a.po_line_id = b.po_line_id
b.po_line_id = lineid
b.req_distribution_id = c.distribution_id
c.requisition_line_id = d.requisition_line_id
d.requisition_header_id = e.requisition_header_id;

l_req_num := l_req;
RETURN l_req_num;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_order_epr_flag_fn;
FUNCTION APC_order_checkdate_fn (
poid
NUMBER
, lineid NUMBER)
RETURN VARCHAR2 IS
status
VARCHAR2 (100);
l_chk_date VARCHAR2 (100);
l_amt_paid NUMBER;
l_chk_cur
VARCHAR2 (50) := '';
l_flag
VARCHAR2 (1)
:= '0';
l_count
NUMBER;
CURSOR chk_cur IS
SELECT DISTINCT ca.check_date check_date
FROM
ap_invoice_lines_all ila
, ap_invoice_payments_all ipa
, ap_checks_all ca
, po_vendor_sites_all pvsa
WHERE
ila.invoice_id = ipa.invoice_id
AND
ipa.check_id = ca.check_id
AND
ca.status_lookup_code = 'NEGOTIABLE'
AND
ila.po_header_id IS NOT NULL
AND
ila.po_header_id = poid
AND
ila.po_line_id = lineid
AND
ca.vendor_site_id = pvsa.vendor_site_id
AND
ca.vendor_id = pvsa.vendor_id;
BEGIN
SELECT COUNT (ca.check_date)
INTO
l_count
FROM
ap_invoice_lines_all ila
, ap_invoice_payments_all ipa
, ap_checks_all ca
, po_vendor_sites_all pvsa
WHERE ila.invoice_id = ipa.invoice_id
AND
ipa.check_id = ca.check_id
AND
ca.status_lookup_code = 'NEGOTIABLE'
AND
ila.po_header_id IS NOT NULL
AND
ila.po_header_id = poid

AND
AND
AND

ila.po_line_id = lineid
ca.vendor_site_id = pvsa.vendor_site_id
ca.vendor_id = pvsa.vendor_id;

IF l_count = 1 THEN
SELECT ca.check_date
INTO
l_chk_date
FROM
ap_invoice_lines_all ila
, ap_invoice_payments_all ipa
, ap_checks_all ca
, po_vendor_sites_all pvsa
WHERE ila.invoice_id = ipa.invoice_id
AND
ipa.check_id = ca.check_id
AND
ca.status_lookup_code = 'NEGOTIABLE'
AND
ila.po_header_id IS NOT NULL
AND
ila.po_header_id = poid
AND
ila.po_line_id = lineid
AND
ca.vendor_site_id = pvsa.vendor_site_id
AND
ca.vendor_id = pvsa.vendor_id;
ELSIF l_count = 0 THEN
l_chk_date := '';
ELSE
FOR cur_rec IN chk_cur
LOOP
l_chk_cur := cur_rec.check_date || ',' || l_chk_cur;
END LOOP;
l_chk_date := l_chk_cur;
END IF;
RETURN l_chk_date;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_order_checkdate_fn;
FUNCTION APC_order_checknum_fn (
poid
NUMBER
, lineid NUMBER)
RETURN VARCHAR2 IS
status
VARCHAR2 (100);
l_chk_num
VARCHAR2 (100) := '';
l_amt_paid NUMBER;
l_chk_cur
VARCHAR2 (50);
l_count
NUMBER;
CURSOR chk_cur IS
SELECT DISTINCT
FROM
,
,
,
WHERE
AND

ca.check_number check_num
ap_invoice_lines_all ila
ap_invoice_payments_all ipa
ap_checks_all ca
po_vendor_sites_all pvsa
ila.invoice_id = ipa.invoice_id
ipa.check_id = ca.check_id

AND
AND
AND
AND
AND
AND
BEGIN
SELECT
INTO
FROM
,
,
,
WHERE
AND
AND
AND
AND
AND
AND
AND

ca.status_lookup_code = 'NEGOTIABLE'
ila.po_header_id IS NOT NULL
ila.po_header_id = poid
ila.po_line_id = lineid
ca.vendor_site_id = pvsa.vendor_site_id
ca.vendor_id = pvsa.vendor_id;
COUNT (DISTINCT ca.check_number)
l_count
ap_invoice_lines_all ila
ap_invoice_payments_all ipa
ap_checks_all ca
po_vendor_sites_all pvsa
ila.invoice_id = ipa.invoice_id
ipa.check_id = ca.check_id
ca.status_lookup_code = 'NEGOTIABLE'
ila.po_header_id IS NOT NULL
ila.po_header_id = poid
ila.po_line_id = lineid
ca.vendor_site_id = pvsa.vendor_site_id
ca.vendor_id = pvsa.vendor_id;

IF l_count = 1 THEN
SELECT DISTINCT ca.check_number
INTO
l_chk_num
FROM
ap_invoice_lines_all ila
, ap_invoice_payments_all ipa
, ap_checks_all ca
, po_vendor_sites_all pvsa
WHERE
ila.invoice_id = ipa.invoice_id
AND
ipa.check_id = ca.check_id
AND
ca.status_lookup_code = 'NEGOTIABLE'
AND
ila.po_header_id IS NOT NULL
AND
ila.po_header_id = poid
AND
ila.po_line_id = lineid
AND
ca.vendor_site_id = pvsa.vendor_site_id
AND
ca.vendor_id = pvsa.vendor_id;
ELSIF l_count = 0 THEN
l_chk_num := '';
ELSE
FOR cur_rec IN chk_cur
LOOP
l_chk_cur := cur_rec.check_num || ',' || l_chk_cur;
END LOOP;
l_chk_num := l_chk_cur;
END IF;
RETURN l_chk_num;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_order_checknum_fn;

FUNCTION APC_invoice_acct_date_fn (
poid
NUMBER
, lineid NUMBER)
RETURN DATE IS
l_accounting_date DATE;
BEGIN
SELECT MIN (accounting_date)
INTO
l_accounting_date
FROM
ap_invoice_lines_all
WHERE po_line_id = lineid
AND
po_header_id = poid;
RETURN l_accounting_date;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_invoice_acct_date_fn;
FUNCTION APC_order_amtpaidd_r130_fn (
poid
NUMBER
, lineid
NUMBER
, inv_id
NUMBER
, line_no NUMBER)
RETURN NUMBER IS
l_val
VARCHAR2 (100);
l_amt_paid
NUMBER;
l_po_amt
NUMBER;
l_freight
NUMBER;
l_line_count NUMBER;
BEGIN
-- check if there are any freight lines for this PO. l_amt_paid
will be zero if there frieght
SELECT
INTO
FROM
WHERE
AND

SUM (NVL (amount, 0) )


l_freight
ap_invoice_lines_all
line_type_lookup_code = 'FREIGHT'
invoice_id IN (SELECT invoice_id
FROM
ap_invoice_lines_all
WHERE po_header_id = poid
AND
po_line_id = lineid
AND
invoice_id = inv_id);

-- check if this line is put in po lines are freight line


SELECT
INTO
FROM
WHERE
AND
AND

COUNT (1)
l_line_count
po_lines_all
po_header_id = poid
po_line_id = lineid
UPPER (item_description) LIKE 'FREIGHT%';

SELECT NVL (SUM (ipa.amount), 0)


INTO
l_amt_paid

FROM
,
,
WHERE
AND
AND
AND
AND
AND
AND
AND

ap_invoice_lines_all inv
ap_invoice_payments_all ipa
ap.ap_checks_all ac
po_header_id = poid
po_line_id = lineid
ipa.check_id = ac.check_id
inv.invoice_id = inv_id
inv.line_number = line_no
NVL (ac.status_lookup_code, 'NULL') <> 'VOIDED'
NVL (inv.discarded_flag, 'N') <> 'Y'
inv.invoice_id = ipa.invoice_id;

IF

l_amt_paid = 0
AND l_freight > 0
AND l_line_count > 0 THEN
l_val := 'There is freight for this PO on PO Line Level';
l_po_amt := l_freight;
ELSE
l_po_amt := l_amt_paid;
END IF;
RETURN l_po_amt;
EXCEPTION
WHEN OTHERS THEN
RETURN 0;
END APC_order_amtpaidd_r130_fn;
FUNCTION get_req_line_pctg (
p_req_header_id IN NUMBER
, p_req_line_id
IN NUMBER)
RETURN NUMBER IS
d_line_pctg NUMBER;
v_flag
VARCHAR2 (10);
BEGIN
BEGIN
SELECT NVL (attribute_category, 'N')
INTO
v_flag
FROM
po_requisition_headers_all
WHERE requisition_header_id = p_req_header_id;
EXCEPTION
WHEN OTHERS THEN
v_flag := 'N';
END;
IF v_flag = 'EPR' THEN
SELECT NVL (prl1.unit_price, 0) * NVL (prd1.req_line_quantity,
0)
/ NVL (total_po_amount, 1)
d_line_pctg
po_requisition_lines prl1
, po_req_distributions_all prd1
, (SELECT
prl.requisition_header_id
, SUM (NVL (prl.unit_price, 0) * NVL
(prd.req_line_quantity, 0) )
INTO
FROM

total_po_amount
FROM

po_requisition_lines prl
, po_req_distributions_all prd
prl.requisition_line_id =

WHERE
prd.requisition_line_id
AND
prl.requisition_header_id = p_req_header_id
GROUP BY prl.requisition_header_id) prl2
WHERE prl1.requisition_header_id = p_req_header_id
AND
prl1.requisition_line_id = p_req_line_id
AND
prl1.requisition_header_id = prl2.requisition_header_id
AND
prl1.requisition_line_id = prd1.requisition_line_id;
ELSE
d_line_pctg := NULL;
END IF;
RETURN d_line_pctg;
EXCEPTION
WHEN OTHERS THEN
RETURN 0;
END;
--------------------------------------------Added by Mayank S for Receipt Number column for Summary
FUNCTION APC_order_receiptnums_fn (
poid NUMBER)
RETURN VARCHAR2 IS
v_rcp_num
VARCHAR2 (100);
v_rcp_num_m VARCHAR2 (100);
BEGIN
FOR x IN (SELECT DISTINCT rsh.receipt_num
FROM
rcv_transactions rcv
, rcv_shipment_headers rsh
, rcv_shipment_lines rsl
WHERE
rcv.shipment_header_id =
rsh.shipment_header_id(+)
AND
rsh.shipment_header_id =
rsl.shipment_header_id(+)
AND
rcv.shipment_line_id =
rsl.shipment_line_id
AND
rsl.quantity_received > 0
AND
rsl.po_header_id = poid
AND
rcv.transaction_type = 'RECEIVE')
LOOP
v_rcp_num := x.receipt_num;
v_rcp_num_m := v_rcp_num_m || '' || v_rcp_num || ',';
END LOOP;
RETURN SUBSTR (v_rcp_num_m, 1, LENGTH (v_rcp_num_m) - 1);
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;

END APC_order_receiptnums_fn;
--------------------------Added by Mayank S for Receipt Amount column for Summary
FUNCTION APC_order_receiptamts_fn (
poid NUMBER)
RETURN NUMBER IS
l_receipt
VARCHAR2 (50);
l_count
NUMBER;
l_receipt_amt NUMBER;
v_rcp_amt_m
NUMBER
:= 0;
v_rcp_amt
NUMBER;
BEGIN
SELECT COUNT (DISTINCT rsh.receipt_num)
INTO
l_count
FROM
rcv_transactions rcv
, rcv_shipment_headers rsh
, rcv_shipment_lines rsl
WHERE rcv.shipment_header_id = rsh.shipment_header_id(+)
AND
rsh.shipment_header_id = rsl.shipment_header_id(+)
AND
rcv.shipment_line_id = rsl.shipment_line_id
AND
rsl.quantity_received > 0
AND
rsl.po_header_id = poid
AND
rsl.po_line_location_id = rcv.po_line_location_id
AND
rcv.transaction_type = 'RECEIVE';
IF l_count = 1 THEN
SELECT SUM (rcv.quantity * rcv.po_unit_price)
INTO
l_receipt_amt
FROM
rcv_transactions rcv
, rcv_shipment_headers rsh
, rcv_shipment_lines rsl
WHERE rcv.shipment_header_id = rsh.shipment_header_id(+)
AND
rsh.shipment_header_id = rsl.shipment_header_id(+)
AND
rcv.shipment_line_id = rsl.shipment_line_id
AND
rsl.quantity_received > 0
AND
rsl.po_header_id = poid
AND
rsl.po_line_location_id = rcv.po_line_location_id
AND
rcv.transaction_type = 'RECEIVE';
RETURN l_receipt_amt;
ELSIF l_count = 0 THEN
l_receipt := '';
RETURN 0;
ELSE
FOR x IN (SELECT DISTINCT rsh.receipt_num
, rcv.quantity * rcv.po_unit_price
receipt_amt -- into l_receipt,l_receipt_amt
FROM
rcv_transactions rcv
, rcv_shipment_headers rsh
, rcv_shipment_lines rsl
WHERE
rcv.shipment_header_id =
rsh.shipment_header_id(+)

AND
rsh.shipment_header_id =
rsl.shipment_header_id(+)
AND
rcv.shipment_line_id =
rsl.shipment_line_id
AND
rsl.quantity_received > 0
AND
rsl.po_header_id = poid
AND
rsl.po_line_location_id =
rcv.po_line_location_id
AND
rcv.transaction_type = 'RECEIVE')
-and
rownum < 2;
LOOP
v_rcp_amt := x.receipt_amt;
v_rcp_amt_m := v_rcp_amt_m + v_rcp_amt;
END LOOP;
RETURN v_rcp_amt_m;
END IF;
EXCEPTION
WHEN OTHERS THEN
RETURN 0;
END APC_order_receiptamts_fn;
--------------------------------------- End of Modification
--Added by Mayank S
FUNCTION APC_order_receiptqty_fn (
poid
NUMBER
, lineid NUMBER
, llid
NUMBER)
RETURN NUMBER IS
l_receipt_qty NUMBER;
BEGIN
SELECT SUM (rcv.quantity)
INTO
l_receipt_qty
FROM
rcv_transactions rcv
, rcv_shipment_headers rsh
, rcv_shipment_lines rsl
WHERE rcv.shipment_header_id = rsh.shipment_header_id(+)
AND
rsh.shipment_header_id = rsl.shipment_header_id(+)
AND
rcv.shipment_line_id = rsl.shipment_line_id
AND
rsl.quantity_received > 0
AND
rsl.po_header_id = poid
AND
rcv.po_line_location_id = llid
AND
rsl.po_line_id = lineid
AND
rsl.po_line_location_id = rcv.po_line_location_id
AND
rcv.transaction_type = 'RECEIVE';
RETURN l_receipt_qty;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END APC_order_receiptqty_fn;
END APC_order_summary_pkg;
/

Você também pode gostar