DBA Data[Home] [Help]

PACKAGE BODY: APPS.PO_CORE_S

Source


1 PACKAGE BODY PO_CORE_S AS
2 -- $Header: POXCOC1B.pls 120.24.12010000.2 2008/11/11 11:14:47 bisdas ship $
3 
4 
5 -----------------------------------------------------------------------------
6 -- Declare private package variables.
7 -----------------------------------------------------------------------------
8 
9 -- Debugging
10 
11 g_pkg_name                       CONSTANT
12    VARCHAR2(30)
13    := 'PO_CORE_S'
14    ;
15 g_log_head                       CONSTANT
16    VARCHAR2(50)
17    := 'po.plsql.' || g_pkg_name || '.'
18    ;
19 
20 g_debug_stmt                     CONSTANT
21    BOOLEAN
22    := PO_DEBUG.is_debug_stmt_on
23    ;
24 g_debug_unexp                    CONSTANT
25    BOOLEAN
26    := PO_DEBUG.is_debug_unexp_on
27    ;
28 
29 
30 D_PACKAGE_BASE CONSTANT VARCHAR2(100) := PO_LOG.get_package_base(g_pkg_name);
31 -----------------------------------------------------------------------------
32 -- Declare private package types.
33 -----------------------------------------------------------------------------
34 
35 
36 -- Bug 3292870
37 TYPE g_rowid_char_tbl IS TABLE OF VARCHAR2(18);
38 
39 
40 
41 -----------------------------------------------------------------------------
42 -- Define procedures.
43 -----------------------------------------------------------------------------
44 
45 PROCEDURE get_open_encumbrance_stats(
46    p_doc_type                       IN             VARCHAR2
47 ,  p_doc_level                      IN             VARCHAR2
48 ,  p_doc_level_id                   IN             NUMBER
49 ,  x_reserved_count                 OUT NOCOPY     NUMBER
50 ,  x_unreserved_count               OUT NOCOPY     NUMBER
51 ,  x_prevented_count                OUT NOCOPY     NUMBER
52 );
53 
54 -- <GC FPJ START>
55 -- Prototype of get_gc_amount_released
56 
57 FUNCTION get_gc_amount_released
58 (
59     p_po_header_id         IN NUMBER,
60     p_convert_to_base      IN BOOLEAN := FALSE
61 ) RETURN NUMBER;
62 
63 -- <GC FPJ END>
64 
65 
66 /* ===========================================================================
67 
68   FUNCTION NAME : get_ussgl_option
69 
70   DESCRIPTION   :
71 
72   CLIENT/SERVER : SERVER
73 
74   LIBRARY NAME  :
75 
76   OWNER         : SUBHAJIT PURKAYASTHA
77 
78   PARAMETERS    :
79 
80   ALGORITHM     : Use fnd_profile.get function to retreive the value
81 
82   NOTES         :  Add sqlcode param in  the call to po_message_c.sql_error
83                    - SI 04/08
84 
85 =========================================================================== */
86 FUNCTION get_ussgl_option RETURN VARCHAR2 is
87 
88   x_progress      VARCHAR2(3) := NULL;
89   x_option_value  VARCHAR2(1);
90 begin
91   x_progress := 10;
92 
93   fnd_profile.get('USSGL_OPTION',x_option_value);
94 
95   RETURN(x_option_value);
96 
97   EXCEPTION
98   WHEN OTHERS THEN
99      po_message_s.sql_error('get_ussgl_option', x_progress, sqlcode);
100   RAISE;
101 
102 end get_ussgl_option;
103 
104 /* ===========================================================================
105 
106   FUNCTION NAME : get_gl_set_of_bks_id
107 
108   DESCRIPTION   :
109 
110   CLIENT/SERVER : SERVER
111 
112   LIBRARY NAME  :
113 
114   OWNER         : SUBHAJIT PURKAYASTHA
115 
116   PARAMETERS    :
117 
118   ALGORITHM     : Use fnd_profile.get function to retreive the value
119 
120   NOTES         :
121 
122 =========================================================================== */
123 FUNCTION get_gl_set_of_bks_id RETURN VARCHAR2 is
124 
125   x_progress      VARCHAR2(3) := NULL;
126   x_option_value  VARCHAR2(3);
127 begin
128   x_progress := 10;
129 
130 /* Replaced the profile option name from 'GL_SET_OF_BKS_ID' to GL_ACCESS_SET_ID'*/
131 
132  fnd_profile.get('GL_ACCESS_SET_ID',x_option_value); --<R12 Ledger Architecture>
133 
134   RETURN(x_option_value);
135 
136   EXCEPTION
137   WHEN OTHERS THEN
138      po_message_s.sql_error('get_gl_set_of_bks_id', x_progress, sqlcode);
139   RAISE;
140 
141 end get_gl_set_of_bks_id;
142 
143 /* ===========================================================================
144   FUNCTION get_conversion_rate (
145 		x_set_of_books_id       NUMBER,
146 		x_from_currency		VARCHAR2,
147 		x_conversion_date	DATE,
148 		x_conversion_type	VARCHAR2 DEFAULT NULL ) RETURN NUMBER;
149 
150   DESCRIPTION    : Returns the rate between the two currencies for a
151                    given conversion date and conversion type.
152   CLIENT/SERVER  : SERVER
153 
154   LIBRARY NAME   :
155 
156   OWNER          : GKELLNER
157 
158   PARAMETERS     :   x_set_of_books_id		Set of Books you are in
159                      x_from_currency		From currency
160                      x_conversion_date	        Conversion date
161                      x_conversion_type	        Conversion type
162 
163   RETURN         :   Rate                       The conversion rate between
164                                                 the two currencies
165 
166   NOTES          : We need this cover on top of gl_currency_api.get_rate
167                    so that we can handle the gl_currency_api.no_rate and
168                    no_data_found exception properly
169 
170 =========================================================================== */
171   FUNCTION get_conversion_rate (
172 		x_set_of_books_id       NUMBER,
173 		x_from_currency		VARCHAR2,
174 		x_conversion_date	DATE,
175 		x_conversion_type	VARCHAR2 DEFAULT NULL )
176 
177   RETURN NUMBER IS
178 
179   x_conversion_rate NUMBER := 0;
180   x_progress  VARCHAR2(3) := '000';
181 
182   BEGIN
183 
184       x_conversion_rate := gl_currency_api.get_rate (
185           x_set_of_books_id,
186           x_from_currency  ,
187 	  x_conversion_date,
188 	  x_conversion_type);
189 
190       -- <2694908>: Truncate rate value (as done in PO_CURRENCY_SV.get_rate( ))
191       x_conversion_rate := round(x_conversion_rate, 15);
192 
193       return (x_conversion_rate);
194 
195    EXCEPTION
196    WHEN gl_currency_api.no_rate THEN
197        return(NULL);
198    WHEN no_data_found THEN
199        return(NULL);
200    WHEN OTHERS THEN
201        RAISE;
202 
203   END get_conversion_rate;
204 
205 /* ===========================================================================
206   PROCEDURE NAME : get_org_sob (x_org_id    OUT NOCOPY NUMBER,
207                                 x_org_name  OUT NOCOPY VARCHAR2,
208                                 x_sob_id    OUT NOCOPY NUMBER)
209 
210   DESCRIPTION    :
211 
212   CLIENT/SERVER  : SERVER
213 
214   LIBRARY NAME   :
215 
216   OWNER          : SUBHAJIT PURKAYASTHA
217 
218   PARAMETERS     : x_org_id   - Return's the Id of the organization
219                    x_org_name - Return's the name of the organization
220                    x_sob_id   - Return's the SOB id
221 
222   ALGORITHM      : AOL function fnd_profile.get('MFG_ORGANIZATION_ID') returns
223                    the default id of the org.
224                    If id <> 0 then
225                      retreive organization name and sob id from
226                      org_organization_definitions table based on org_id
227                    else
228                      retreive purchasing organization from
229                      financials_system_parameters and gl_sets_of_books table
230 
231   NOTES          :
232 
233 =========================================================================== */
234 
235 PROCEDURE get_org_sob (x_org_id    OUT NOCOPY NUMBER,
236                        x_org_name  OUT NOCOPY VARCHAR2,
237                        x_sob_id    OUT NOCOPY NUMBER) is
238     x_progress  VARCHAR2(3) := NULL;
239     org_id      NUMBER;
240 
241 /** <UTF8 FPI> **/
242 /** tpoon 9/27/2002 **/
243 /** Changed org_name to use %TYPE **/
244 --    org_name    VARCHAR2(60);
245     org_name    hr_all_organization_units.name%TYPE;
246 
247     sob_id      NUMBER;
248 begin
249   x_progress := 10;
250   fnd_profile.get('MFG_ORGANIZATION_ID',org_id);
251   if org_id <> 0 then
252 
253     x_progress := 20;
254     select  organization_name,
255             set_of_books_id
256     into    org_name,
257             sob_id
258     FROM   org_organization_definitions
259     WHERE  organization_id = org_id ;
260 
261   else
262     --Get purchasing organization
263     x_progress := 30;
264     SELECT  fsp.inventory_organization_id,
265             fsp.set_of_books_id,
266             sob.name
267     INTO    org_id,
268             sob_id,
269             org_name
270     FROM    financials_system_parameters fsp,
271             gl_sets_of_books sob
272     WHERE   fsp.set_of_books_id = sob.set_of_books_id;
273 
274   end if;
275   x_progress := 40;
276   --Associate each of the OUT NOCOPY variable with local variable
277 
278   x_org_id   := org_id;
279   x_org_name := org_name;
280   x_sob_id   := sob_id;
281 
282   EXCEPTION
283   WHEN OTHERS THEN
284     po_message_s.sql_error('get_org_sob', x_progress, sqlcode);
285     RAISE;
286   end get_org_sob;
287 
288 /* ===========================================================================
289   PROCEDURE NAME: get_po_parameters
290 
291   DESCRIPTION   :
292 
293   CLIENT/SERVER : SERVER
294 
295   LIBRARY NAME  :
296 
297   OWNER         : SUBHAJIT PURKAYASTHA
298 
299   PARAMETERS    : x_currency_code
300                   x_coa_id
301                   x_po_encumberance_flag
302                   x_req_encumberance_flag
303                   x_sob_id
304                   x_ship_to_location_id
305                   x_bill_to_location_id
306                   x_fob_lookup_code
307                   x_freight_terms_lookup_code
308                   x_terms_id
309                   x_default_rate_type
310                   x_taxable_flag
311                   x_receiving_flag
312                   x_enforce_buyer_name_flag
313                   x_enforce_buyer_auth_flag
314                   x_line_type_id
315                   x_manual_po_num_type
316                   x_po_num_code
317                   x_price_lookup_code
318                   x_invoice_close_tolerance
319                   x_receive_close_tolerance
320                   x_security_structure_id
321                   x_expense_accrual_code
322                   x_inv_org_id
323                   x_rev_sort_ordering
324                   x_min_rel_amount
325                   x_notify_blanket_flag
326                   x_budgetary_control_flag
327                   x_user_defined_req_num_code
328                   x_rfq_required_flag
329                   x_manual_req_num_type
330                   x_enforce_full_lot_qty
331                   x_disposition_warning_flag
332                   x_reserve_at_completion_flag
333                   x_user_defined_rcpt_num_code
334                   x_manual_rcpt_num_type
335 		  x_use_positions_flag
336 		  x_default_quote_warning_delay
337 		  x_inspection_required_flag
338 		  x_user_defined_quote_num_code
339 		  x_manual_quote_num_type
340 		  x_user_defined_rfq_num_code
341 		  x_manual_rfq_num_type
342 		  x_ship_via_lookup_code
343 		  x_qty_rcv_tolerance
344 
345 
346   ALGORITHM     : Retreive po parameters from
347                   financials_system_parameters,gl_sets_of_books,
348                   po_system_parameters and rcv_parameters tables.
349 
350   NOTES         :
351 
352 =========================================================================== */
353 PROCEDURE get_po_parameters (  x_currency_code                 OUT NOCOPY VARCHAR2,
354                                x_coa_id                        OUT NOCOPY NUMBER,
355                                x_po_encumberance_flag          OUT NOCOPY VARCHAR2,
356                                x_req_encumberance_flag         OUT NOCOPY VARCHAR2,
357                                x_sob_id                        OUT NOCOPY NUMBER,
358                                x_ship_to_location_id           OUT NOCOPY NUMBER,
359                                x_bill_to_location_id           OUT NOCOPY NUMBER,
360                                x_fob_lookup_code               OUT NOCOPY VARCHAR2,
361                                x_freight_terms_lookup_code     OUT NOCOPY VARCHAR2,
362                                x_terms_id                      OUT NOCOPY NUMBER,
363                                x_default_rate_type             OUT NOCOPY VARCHAR2,
364                                x_taxable_flag                  OUT NOCOPY VARCHAR2,
365                                x_receiving_flag                OUT NOCOPY VARCHAR2,
366                                x_enforce_buyer_name_flag       OUT NOCOPY VARCHAR2,
367                                x_enforce_buyer_auth_flag       OUT NOCOPY VARCHAR2,
368                                x_line_type_id                  OUT NOCOPY NUMBER,
369                                x_manual_po_num_type            OUT NOCOPY VARCHAR2,
370                                x_po_num_code                   OUT NOCOPY VARCHAR2,
371                                x_price_lookup_code             OUT NOCOPY VARCHAR2,
372                                x_invoice_close_tolerance       OUT NOCOPY NUMBER,
373                                x_receive_close_tolerance       OUT NOCOPY NUMBER,
374                                x_security_structure_id         OUT NOCOPY NUMBER,
375                                x_expense_accrual_code          OUT NOCOPY VARCHAR2,
376                                x_inv_org_id                    OUT NOCOPY NUMBER,
377                                x_rev_sort_ordering             OUT NOCOPY NUMBER,
378                                x_min_rel_amount                OUT NOCOPY NUMBER,
379                                x_notify_blanket_flag           OUT NOCOPY VARCHAR2,
380                                x_budgetary_control_flag        OUT NOCOPY VARCHAR2,
381                                x_user_defined_req_num_code     OUT NOCOPY VARCHAR2,
382                                x_rfq_required_flag             OUT NOCOPY VARCHAR2,
383                                x_manual_req_num_type           OUT NOCOPY VARCHAR2,
384                                x_enforce_full_lot_qty          OUT NOCOPY VARCHAR2,
385                                x_disposition_warning_flag      OUT NOCOPY VARCHAR2,
386                                x_reserve_at_completion_flag    OUT NOCOPY VARCHAR2,
387                                x_user_defined_rcpt_num_code    OUT NOCOPY VARCHAR2,
388                                x_manual_rcpt_num_type          OUT NOCOPY VARCHAR2,
389 			       x_use_positions_flag	       OUT NOCOPY VARCHAR2,
390 			       x_default_quote_warning_delay   OUT NOCOPY NUMBER,
391 		  	       x_inspection_required_flag      OUT NOCOPY VARCHAR2,
392 		  	       x_user_defined_quote_num_code   OUT NOCOPY VARCHAR2,
393 		  	       x_manual_quote_num_type	       OUT NOCOPY VARCHAR2,
394 		  	       x_user_defined_rfq_num_code     OUT NOCOPY VARCHAR2,
395 		  	       x_manual_rfq_num_type	       OUT NOCOPY VARCHAR2,
396 		  	       x_ship_via_lookup_code	       OUT NOCOPY VARCHAR2,
397 		  	       x_qty_rcv_tolerance	       OUT NOCOPY NUMBER
398                               ) is
399 
400   x_progress     VARCHAR2(3) := NULL;
401   x_acceptance_required_flag VARCHAR2(1) := NULL;
402 begin
403   x_progress := 10;
404 
405 
406           get_po_parameters (  x_currency_code               => x_currency_code
407                                ,x_coa_id                     => x_coa_id
408                                ,x_po_encumberance_flag       => x_po_encumberance_flag
409                                ,x_req_encumberance_flag      => x_req_encumberance_flag
410                                ,x_sob_id                     => x_sob_id
411                                ,x_ship_to_location_id        => x_ship_to_location_id
412                                ,x_bill_to_location_id        => x_bill_to_location_id
413                                ,x_fob_lookup_code            => x_fob_lookup_code
414                                ,x_freight_terms_lookup_code  => x_freight_terms_lookup_code
415                                ,x_terms_id                   => x_terms_id
416                                ,x_default_rate_type          => x_default_rate_type
417                                ,x_taxable_flag               => x_taxable_flag
418                                ,x_receiving_flag             => x_receiving_flag
419                                ,x_enforce_buyer_name_flag    => x_enforce_buyer_name_flag
420                                ,x_enforce_buyer_auth_flag    => x_enforce_buyer_auth_flag
421                                ,x_line_type_id               => x_line_type_id
422                                ,x_manual_po_num_type         => x_manual_po_num_type
423                                ,x_po_num_code                => x_po_num_code
424                                ,x_price_lookup_code          => x_price_lookup_code
425                                ,x_invoice_close_tolerance    => x_invoice_close_tolerance
426                                ,x_receive_close_tolerance    => x_receive_close_tolerance
427                                ,x_security_structure_id      => x_security_structure_id
428                                ,x_expense_accrual_code       => x_expense_accrual_code
429                                ,x_inv_org_id                 => x_inv_org_id
430                                ,x_rev_sort_ordering          => x_rev_sort_ordering
431                                ,x_min_rel_amount             => x_min_rel_amount
432                                ,x_notify_blanket_flag        => x_notify_blanket_flag
433                                ,x_budgetary_control_flag     => x_budgetary_control_flag
434                                ,x_user_defined_req_num_code  => x_user_defined_req_num_code
435                                ,x_rfq_required_flag          => x_rfq_required_flag
436                                ,x_manual_req_num_type        => x_manual_req_num_type
437                                ,x_enforce_full_lot_qty       => x_enforce_full_lot_qty
438                                ,x_disposition_warning_flag   => x_disposition_warning_flag
439                                ,x_reserve_at_completion_flag => x_reserve_at_completion_flag
440                                ,x_user_defined_rcpt_num_code => x_user_defined_rcpt_num_code
441                                ,x_manual_rcpt_num_type       => x_manual_rcpt_num_type
442 			       ,x_use_positions_flag	     => x_use_positions_flag
443 			       ,x_default_quote_warning_delay => x_default_quote_warning_delay
444 		  	       ,x_inspection_required_flag   => x_inspection_required_flag
445 		  	       ,x_user_defined_quote_num_code => x_user_defined_quote_num_code
446 		  	       ,x_manual_quote_num_type	     => x_manual_quote_num_type
447 		  	       ,x_user_defined_rfq_num_code  => x_user_defined_rfq_num_code
448 		  	       ,x_manual_rfq_num_type	     => x_manual_rfq_num_type
449 		  	       ,x_ship_via_lookup_code	     => x_ship_via_lookup_code
450 		  	       ,x_qty_rcv_tolerance	     => x_qty_rcv_tolerance
451 			       ,x_acceptance_required_flag   => x_acceptance_required_flag
452                               );
453 
454   EXCEPTION
455   WHEN OTHERS THEN
456     po_message_s.sql_error('get_po_parameters', x_progress, sqlcode);
457     RAISE;
458 
459 end get_po_parameters;
460 
461 /* Bug 7518967: ER Default Acceptance Required Check : Overloading the procedure
462   get_po_parameters to get the default acceptance_required_flag from
463   PO_SYSTEM_PARAMETERS_ALL */
464 
465 /* ===========================================================================
466   PROCEDURE NAME: get_po_parameters
467 
468   DESCRIPTION   :
469 
470   CLIENT/SERVER : SERVER
471 
472   LIBRARY NAME  :
473 
474   OWNER         : SUBHAJIT PURKAYASTHA
475 
476   PARAMETERS    : x_currency_code
477                   x_coa_id
478                   x_po_encumberance_flag
479                   x_req_encumberance_flag
480                   x_sob_id
481                   x_ship_to_location_id
482                   x_bill_to_location_id
483                   x_fob_lookup_code
484                   x_freight_terms_lookup_code
485                   x_terms_id
486                   x_default_rate_type
487                   x_taxable_flag
488                   x_receiving_flag
489                   x_enforce_buyer_name_flag
490                   x_enforce_buyer_auth_flag
491                   x_line_type_id
492                   x_manual_po_num_type
493                   x_po_num_code
494                   x_price_lookup_code
495                   x_invoice_close_tolerance
496                   x_receive_close_tolerance
497                   x_security_structure_id
498                   x_expense_accrual_code
499                   x_inv_org_id
500                   x_rev_sort_ordering
501                   x_min_rel_amount
502                   x_notify_blanket_flag
503                   x_budgetary_control_flag
504                   x_user_defined_req_num_code
505                   x_rfq_required_flag
506                   x_manual_req_num_type
507                   x_enforce_full_lot_qty
508                   x_disposition_warning_flag
509                   x_reserve_at_completion_flag
510                   x_user_defined_rcpt_num_code
511                   x_manual_rcpt_num_type
512 		  x_use_positions_flag
513 		  x_default_quote_warning_delay
514 		  x_inspection_required_flag
515 		  x_user_defined_quote_num_code
516 		  x_manual_quote_num_type
517 		  x_user_defined_rfq_num_code
518 		  x_manual_rfq_num_type
519 		  x_ship_via_lookup_code
520 		  x_qty_rcv_tolerance
521 		  x_acceptance_required_flag
522 
523 
524   ALGORITHM     : Retreive po parameters from
525                   financials_system_parameters,gl_sets_of_books,
526                   po_system_parameters and rcv_parameters tables.
527 
528   NOTES         :
529 
530 =========================================================================== */
531 PROCEDURE get_po_parameters (  x_currency_code                 OUT NOCOPY VARCHAR2,
532                                x_coa_id                        OUT NOCOPY NUMBER,
533                                x_po_encumberance_flag          OUT NOCOPY VARCHAR2,
534                                x_req_encumberance_flag         OUT NOCOPY VARCHAR2,
535                                x_sob_id                        OUT NOCOPY NUMBER,
536                                x_ship_to_location_id           OUT NOCOPY NUMBER,
537                                x_bill_to_location_id           OUT NOCOPY NUMBER,
538                                x_fob_lookup_code               OUT NOCOPY VARCHAR2,
539                                x_freight_terms_lookup_code     OUT NOCOPY VARCHAR2,
540                                x_terms_id                      OUT NOCOPY NUMBER,
541                                x_default_rate_type             OUT NOCOPY VARCHAR2,
542                                x_taxable_flag                  OUT NOCOPY VARCHAR2,
543                                x_receiving_flag                OUT NOCOPY VARCHAR2,
544                                x_enforce_buyer_name_flag       OUT NOCOPY VARCHAR2,
545                                x_enforce_buyer_auth_flag       OUT NOCOPY VARCHAR2,
546                                x_line_type_id                  OUT NOCOPY NUMBER,
547                                x_manual_po_num_type            OUT NOCOPY VARCHAR2,
548                                x_po_num_code                   OUT NOCOPY VARCHAR2,
549                                x_price_lookup_code             OUT NOCOPY VARCHAR2,
550                                x_invoice_close_tolerance       OUT NOCOPY NUMBER,
551                                x_receive_close_tolerance       OUT NOCOPY NUMBER,
552                                x_security_structure_id         OUT NOCOPY NUMBER,
553                                x_expense_accrual_code          OUT NOCOPY VARCHAR2,
554                                x_inv_org_id                    OUT NOCOPY NUMBER,
555                                x_rev_sort_ordering             OUT NOCOPY NUMBER,
556                                x_min_rel_amount                OUT NOCOPY NUMBER,
557                                x_notify_blanket_flag           OUT NOCOPY VARCHAR2,
558                                x_budgetary_control_flag        OUT NOCOPY VARCHAR2,
559                                x_user_defined_req_num_code     OUT NOCOPY VARCHAR2,
560                                x_rfq_required_flag             OUT NOCOPY VARCHAR2,
561                                x_manual_req_num_type           OUT NOCOPY VARCHAR2,
562                                x_enforce_full_lot_qty          OUT NOCOPY VARCHAR2,
563                                x_disposition_warning_flag      OUT NOCOPY VARCHAR2,
564                                x_reserve_at_completion_flag    OUT NOCOPY VARCHAR2,
565                                x_user_defined_rcpt_num_code    OUT NOCOPY VARCHAR2,
566                                x_manual_rcpt_num_type          OUT NOCOPY VARCHAR2,
567 			       x_use_positions_flag	       OUT NOCOPY VARCHAR2,
568 			       x_default_quote_warning_delay   OUT NOCOPY NUMBER,
569 		  	       x_inspection_required_flag      OUT NOCOPY VARCHAR2,
570 		  	       x_user_defined_quote_num_code   OUT NOCOPY VARCHAR2,
571 		  	       x_manual_quote_num_type	       OUT NOCOPY VARCHAR2,
572 		  	       x_user_defined_rfq_num_code     OUT NOCOPY VARCHAR2,
573 		  	       x_manual_rfq_num_type	       OUT NOCOPY VARCHAR2,
574 		  	       x_ship_via_lookup_code	       OUT NOCOPY VARCHAR2,
575 		  	       x_qty_rcv_tolerance	       OUT NOCOPY NUMBER,
576 			       x_acceptance_required_flag      OUT NOCOPY VARCHAR2
577                               ) is
578 
579   x_progress     VARCHAR2(3) := NULL;
580 begin
581   x_progress := 10;
582 
583   SELECT  sob.currency_code,
584           sob.chart_of_accounts_id,
585           nvl(fsp.purch_encumbrance_flag,'N'),
586           nvl(fsp.req_encumbrance_flag,'N'),
587           sob.set_of_books_id,
588           fsp.ship_to_location_id,
589           fsp.bill_to_location_id,
590           fsp.fob_lookup_code,
591           fsp.freight_terms_lookup_code,
592           aps.terms_id,     -- bug5701539
593           psp.default_rate_type,
594 	  --togeorge 07/03/2001
595 	  --Bug# 1839659
596 	  --We are no more using this flag from psp.
597           --psp.taxable_flag,
598 	  null,
599 	  --
600           psp.receiving_flag,
601           nvl(psp.enforce_buyer_name_flag, 'N'),
602           nvl(psp.enforce_buyer_authority_flag,'N'),
603           psp.line_type_id,
604           psp.manual_po_num_type,
605           psp.user_defined_po_num_code,
606           psp.price_type_lookup_code,
607           psp.invoice_close_tolerance,
608           psp.receive_close_tolerance,
609           psp.security_position_structure_id,
610           psp.expense_accrual_code,
611           fsp.inventory_organization_id,
612           fsp.revision_sort_ordering,
613           psp.min_release_amount,
614           nvl(psp.notify_if_blanket_flag,'N'),
615           nvl(sob.enable_budgetary_control_flag,'N'),
616           psp.user_defined_req_num_code,
617           nvl(psp.rfq_required_flag,'N'),
618           psp.manual_req_num_type,
619           psp.enforce_full_lot_quantities,
620           psp.disposition_warning_flag,
621           nvl(fsp.reserve_at_completion_flag,'N'),
622           psp.user_defined_receipt_num_code,
623           psp.manual_receipt_num_type,
624 	  fsp.use_positions_flag,
625 	  psp.default_quote_warning_delay,
626 	  psp.inspection_required_flag,
627 	  psp.user_defined_quote_num_code,
628 	  psp.manual_quote_num_type,
629 	  psp.user_defined_rfq_num_code,
630 	  psp.manual_rfq_num_type,
631 	  fsp.ship_via_lookup_code,
632 	  rcv.qty_rcv_tolerance,
633 	  psp.acceptance_required_flag
634   INTO    x_currency_code       ,
635           x_coa_id               ,
636           x_po_encumberance_flag  ,
637           x_req_encumberance_flag  ,
638           x_sob_id                  ,
639           x_ship_to_location_id      ,
640           x_bill_to_location_id       ,
641           x_fob_lookup_code         ,
642           x_freight_terms_lookup_code,
643           x_terms_id           ,
644           x_default_rate_type   ,
645           x_taxable_flag         ,
646           x_receiving_flag        ,
647           x_enforce_buyer_name_flag,
648           x_enforce_buyer_auth_flag,
649           x_line_type_id       ,
650           x_manual_po_num_type  ,
651           x_po_num_code          ,
652           x_price_lookup_code     ,
653           x_invoice_close_tolerance,
654           x_receive_close_tolerance,
655           x_security_structure_id,
656           x_expense_accrual_code,
657           x_inv_org_id      ,
658           x_rev_sort_ordering,
659           x_min_rel_amount    ,
660           x_notify_blanket_flag,
661           x_budgetary_control_flag,
662           x_user_defined_req_num_code,
663           x_rfq_required_flag,
664           x_manual_req_num_type,
665           x_enforce_full_lot_qty,
666           x_disposition_warning_flag,
667           x_reserve_at_completion_flag,
668           x_user_defined_rcpt_num_code,
669           x_manual_rcpt_num_type,
670 	  x_use_positions_flag,
671 	  x_default_quote_warning_delay,
672 	  x_inspection_required_flag,
673 	  x_user_defined_quote_num_code,
674 	  x_manual_quote_num_type,
675 	  x_user_defined_rfq_num_code,
676 	  x_manual_rfq_num_type,
677 	  x_ship_via_lookup_code,
678 	  x_qty_rcv_tolerance,
679 	  x_acceptance_required_flag
680   FROM    financials_system_parameters fsp,
681           gl_sets_of_books sob,
682           po_system_parameters psp,
683 	  rcv_parameters  rcv,
684           ap_product_setup aps
685   WHERE   fsp.set_of_books_id = sob.set_of_books_id
686   AND     rcv.organization_id (+) = fsp.inventory_organization_id;
687 
688 
689   EXCEPTION
690   WHEN OTHERS THEN
691     po_message_s.sql_error('get_po_parameters', x_progress, sqlcode);
692     RAISE;
693 
694 end get_po_parameters;
695 
696 /* ===========================================================================
697   PROCEDURE NAME: get_item_category_structure(x_category_set_id OUT NOCOPY NUMBER,
698                                               x_structure_id    OUT NOCOPY NUMBER)
699 
700   DESCRIPTION   :
701 
702   CLIENT/SERVER : SERVER
703 
704   LIBRARY NAME  :
705 
706   OWNER         : SUBHAJIT PURKAYASTHA
707 
708   PARAMETERS    : Category_set_id NUMBER
709                   structure_id    NUMBER
710 
711   ALGORITHM     : Retreive category_set_id and structure_id from
712                   mtl_default_sets_view for functional_area_id = 2
713 
714   NOTES         :
715 
716 =========================================================================== */
717 PROCEDURE get_item_category_structure (  x_category_set_id OUT NOCOPY NUMBER,
718                                          x_structure_id    OUT NOCOPY NUMBER ) is
719     x_progress  VARCHAR2(3) := NULL;
720 begin
721   x_progress := 10;
722   SELECT mdsv.category_set_id,
723          mdsv.structure_id
724   INTO   x_category_set_id,
725          x_structure_id
726   FROM   mtl_default_sets_view mdsv
727   WHERE  mdsv.functional_area_id = 2;
728 
729   EXCEPTION
730   WHEN OTHERS THEN
731     po_message_s.sql_error('get_item_category_structure', x_progress, sqlcode);
732     RAISE;
733 
734 end get_item_category_structure;
735 
736 /* ===========================================================================
737   FUNCTION NAME : get_product_install_status (x_product_name IN VARCHAR2)
738                                                 RETURN VARCHAR2
739 
740   DESCRIPTION   : Returns the product's installation status
741 
742   CLIENT/SERVER : SERVER
743 
744   LIBRARY NAME  :
745 
746   OWNER         : SUBHAJIT PURKAYASTHA
747 
748   PARAMETERS    : x_product_name - Name of the product
749                   For eg - 'INV','PO','ENG'
750 
751   ALGORITHM     : Use fnd_installation.get function to retreive
752                   the status of product installation.
753                   Function expects product id to be passed
754                   Product Id will be derived from FND_APPLICATION table
755                   Product       Product Id
756                   --------      -----------
757                   INV           401
758                   PO            201
759 
760   NOTES         : valid installation status:
761                   I - Product is installed
762                   S - Product is partially installed
763                   N - Product is not installed
764                   L - Product is a local (custom) application
765 
766 
767 =========================================================================== */
768 
769 FUNCTION get_product_install_status ( x_product_name IN VARCHAR2) RETURN VARCHAR2 IS
770   x_progress     VARCHAR2(3) := NULL;
771   x_app_id       NUMBER;
772   x_install      BOOLEAN;
773   x_status       VARCHAR2(1);
774   x_org          VARCHAR2(1);
775   x_temp_product_name varchar2(10);
776 begin
777   --Retreive product id from fnd_application based on product name
778   x_progress := 10;
779 
780   select application_id
781   into   x_app_id
782   from   fnd_application
783   where application_short_name = x_product_name ;
784 
785   --get product installation status
786   x_progress := 20;
787   x_install := fnd_installation.get(x_app_id,x_app_id,x_status,x_org);
788 
789   if x_product_name in ('OE', 'ONT') then
790 
791 	if Oe_install.get_active_product() in ('OE', 'ONT') then
792 		x_status := 'I';
793 	else
794 		x_status := 'N';
795 	end if;
796   end if;
797 
798   RETURN(x_status);
799 
800   EXCEPTION
801     WHEN NO_DATA_FOUND then
802       null;
803       RETURN(null);
804     WHEN OTHERS THEN
805     po_message_s.sql_error('get_product_install_status', x_progress, sqlcode);
806       RAISE;
807 
808 end get_product_install_status;
809 
810 /* ===========================================================================
811   PROCEDURE NAME: get_global_values(x_userid        OUT NOCOPY number,
812                                     x_logonid       OUT NOCOPY number,
813                                     x_last_upd_date OUT NOCOPY date,
814                                     x_current_date  OUT NOCOPY date )
815 
816   DESCRIPTION   :
817 
818   CLIENT/SERVER : SERVER
819 
820   LIBRARY NAME  :
821 
822   OWNER         : SUBHAJIT PURKAYASTHA
823 
824   PARAMETERS    : x_userid        - Returns fnd user_id
825                   x_logonid       - Return fnd logon id
826                   x_last_upd_date - Returns sysdate
827                   x_current_date  - Returns sysdate
828 
829   ALGORITHM     :
830 
831   NOTES         :
832 
833 =========================================================================== */
834 
835 PROCEDURE get_global_values(x_userid        OUT NOCOPY number,
836                             x_logonid       OUT NOCOPY number,
837                             x_last_upd_date OUT NOCOPY date,
838                             x_current_date  OUT NOCOPY date ) is
839   x_progress VARCHAR2(3) := NULL;
840 begin
841   x_progress := 10;
842   x_userid        := fnd_global.user_id;
843 
844   x_progress := 20;
845   x_logonid       := fnd_global.login_id;
846 
847   x_progress := 30;
848   x_last_upd_date := sysdate;
849 
850   x_progress := 40;
851   x_current_date  := sysdate;
852 
853   EXCEPTION
854   WHEN OTHERS THEN
855     po_message_s.sql_error('get_global_values', x_progress, sqlcode);
856     RAISE;
857 
858 end get_global_values;
859 
860 /*===========================================================================
861 
862   PROCEDURE NAME : GET_PERIOD_NAME
863 
864   DESCRIPTION   : Based on system date, function returns appropriate period
865                   and gl_date
866 
867   CLIENT/SERVER : SERVER
868 
869   LIBRARY NAME  :
870 
871   OWNER         : SUBHAJIT PURKAYASTHA
872 
873   PARAMETERS    : sob_id      - Set_of_books_id
874                   period_name - Period Name
875                   gl_date     - GL Date
876   ALGORITHM     :
877 
878   NOTES         :
879 
880 ===========================================================================*/
881 -------------------------------------------------------------------------------
882 --Start of Comments
883 --Name: get_period_name
884 --Pre-reqs:
885 --  None.
886 --Modifies:
887 --  None.
888 --Locks:
889 --  None.
890 --Function:
891 --  Retrieves the GL period name and date for SYSDATE,
892 --  if the date is in a usable period (valid for GL and PO).
893 --Parameters:
894 --IN:
895 --x_sob_id
896 --  Set of books.
897 --OUT:
898 --x_gl_period
899 --  The period name corresponding to SYSDATE.
900 --x_gl_date
901 --  SYSDATE.
902 --Notes:
903 --  This procedure was refactored in FPJ to call the more generalized
904 --  procedure get_period_info.  However, the parameter names were
905 --  not changed to meet standards, as that may have impacted calling code.
906 --Testing:
907 --
908 --End of Comments
909 -------------------------------------------------------------------------------
910 PROCEDURE get_period_name (
911    x_sob_id                         IN             NUMBER
912 ,  x_period                         OUT NOCOPY     VARCHAR2
913 ,  x_gl_date                        OUT NOCOPY     DATE
914 )
915 IS
916 
917 
918 l_log_head     CONSTANT VARCHAR2(100) := g_log_head||'GET_PERIOD_NAME';
919 l_progress     VARCHAR2(3) := '000';
920 
921 l_period_name_tbl       po_tbl_varchar30;
922 l_period_year_tbl       po_tbl_number;
923 l_period_num_tbl        po_tbl_number;
924 l_quarter_num_tbl       po_tbl_number;
925 l_invalid_period_flag   VARCHAR2(1);
926 
927 BEGIN
928 
929 IF g_debug_stmt THEN
930    PO_DEBUG.debug_begin(l_log_head);
931    PO_DEBUG.debug_var(l_log_head,l_progress,'x_sob_id',x_sob_id);
932 END IF;
933 
934 l_progress := '010';
935 
936 PO_PERIODS_SV.get_period_info(
937    p_roll_logic => NULL
938 ,  p_set_of_books_id => x_sob_id
939 ,  p_date_tbl => po_tbl_date( SYSDATE )
940 ,  x_period_name_tbl => l_period_name_tbl
941 ,  x_period_year_tbl => l_period_year_tbl
942 ,  x_period_num_tbl => l_period_num_tbl
943 ,  x_quarter_num_tbl => l_quarter_num_tbl
944 ,  x_invalid_period_flag => l_invalid_period_flag
945 );
946 
947 l_progress := '020';
948 
949 x_period := l_period_name_tbl(1);
950 
951 IF (l_invalid_period_flag = FND_API.G_FALSE) THEN
952 
953    l_progress := '030';
954    IF g_debug_stmt THEN
955       PO_DEBUG.debug_stmt(l_log_head,l_progress,'valid period');
956    END IF;
957 
958    x_gl_date := TRUNC(SYSDATE);
959 
960 END IF;
961 
962 l_progress := '900';
963 
964 IF g_debug_stmt THEN
965    PO_DEBUG.debug_var(l_log_head,l_progress,'x_period',x_period);
966    PO_DEBUG.debug_var(l_log_head,l_progress,'x_gl_date',x_gl_date);
967    PO_DEBUG.debug_end(l_log_head);
968 END IF;
969 
970 EXCEPTION
971 WHEN OTHERS THEN
972    IF g_debug_unexp THEN
973       PO_DEBUG.debug_exc(l_log_head,l_progress);
974    END IF;
975    RAISE;
976 
977 END get_period_name;
978 
979 
980 
981 
982 /* ===========================================================================
983   PROCEDURE NAME: get_displayed_value(x_lookup_type       IN  VARCHAR2,
984                                       x_lookup_code       IN  VARCHAR2,
985 				      x_disp_value	  OUT NOCOPY VARCHAR2,
986 				      x_description       OUT NOCOPY VARCHAR2,
987 				      x_validate  	  IN  BOOLEAN)
988 
989   DESCRIPTION   : Obtain the  displayed field and description. This procedure
990 		  also performs active date validation.
991 
992   CLIENT/SERVER : SERVER
993 
994   LIBRARY NAME  :
995 
996   OWNER         : Ramana Mulpury
997 
998   PARAMETERS    : lookup_type		VARCHAR2
999                   lookup_type		VARCHAR2
1000  		  displayed_field 	VARCHAR2
1001 		  descriptioni    	VARCHAR2
1002 		  validate		BOOLEAN
1003 
1004   ALGORITHM     : Get the displayed field and description from the
1005 		  table po_lookup_codes. These values are validated
1006 		  against the inactive date if x_validate is set to TRUE
1007 		  No validation is performed if x_validate is set to
1008 		  FALSE.
1009   NOTES         :
1010 
1011 =========================================================================== */
1012 PROCEDURE get_displayed_value (x_lookup_type       IN  VARCHAR2,
1013                                x_lookup_code       IN  VARCHAR2,
1014 			       x_disp_value	   OUT NOCOPY VARCHAR2,
1015 			       x_description       OUT NOCOPY VARCHAR2,
1016 			       x_validate	   IN  BOOLEAN) IS
1017     x_progress  VARCHAR2(3) := NULL;
1018 begin
1019 
1020   x_progress := 10;
1021 
1022   IF  (x_validate = TRUE) THEN
1023 
1024   SELECT plc.displayed_field,
1025          plc.description
1026   INTO   x_disp_value,
1027          x_description
1028   FROM   po_lookup_codes plc
1029   WHERE  plc.lookup_code = x_lookup_code
1030   AND    plc.lookup_type = x_lookup_type
1031   AND    sysdate < nvl(plc.inactive_date,sysdate + 1);
1032 
1033   ELSE
1034    get_displayed_value(x_lookup_type, x_lookup_code, x_disp_value, x_description);
1035 
1036   END IF;
1037 
1038   EXCEPTION
1039   WHEN NO_DATA_FOUND THEN
1040     x_disp_value   := NULL;
1041     x_description  := NULL;
1042 
1043   WHEN OTHERS THEN
1044     po_message_s.sql_error('get_displayed_value', x_progress, sqlcode);
1045     RAISE;
1046 
1047 end get_displayed_value;
1048 
1049 
1050 
1051 /* ===========================================================================
1052   PROCEDURE NAME: get_displayed_value(x_lookup_type       IN  VARCHAR2,
1053                                       x_lookup_code       IN  VARCHAR2,
1054 				      x_disp_value	  OUT NOCOPY VARCHAR2,
1055 				      x_description       OUT NOCOPY VARCHAR2)
1056 
1057   DESCRIPTION   : Obtain the  displayed field and description
1058 
1059   CLIENT/SERVER : SERVER
1060 
1061   LIBRARY NAME  :
1062 
1063   OWNER         : Ramana Mulpury
1064 
1065   PARAMETERS    : lookup_type		VARCHAR2
1066                   lookup_type		VARCHAR2
1067  		  displayed_field 	VARCHAR2
1068 		  descriptioni    	VARCHAR2
1069 
1070   ALGORITHM     : Get the displayed field and description from the
1071 		  table po_lookup_codes.
1072 
1073   NOTES         :
1074 
1075 =========================================================================== */
1076 PROCEDURE get_displayed_value (x_lookup_type       IN  VARCHAR2,
1077                                x_lookup_code       IN  VARCHAR2,
1078 			       x_disp_value	   OUT NOCOPY VARCHAR2,
1079 			       x_description       OUT NOCOPY VARCHAR2) IS
1080     x_progress  VARCHAR2(3) := NULL;
1081 begin
1082 
1083   x_progress := 10;
1084 
1085   SELECT plc.displayed_field,
1086          plc.description
1087   INTO   x_disp_value,
1088          x_description
1089   FROM   po_lookup_codes plc
1090   WHERE  plc.lookup_code = x_lookup_code
1091   AND    plc.lookup_type = x_lookup_type;
1092 
1093   EXCEPTION
1094   WHEN NO_DATA_FOUND THEN
1095     x_disp_value   := NULL;
1096     x_description  := NULL;
1097 
1098   WHEN OTHERS THEN
1099     po_message_s.sql_error('get_displayed_value', x_progress, sqlcode);
1100     RAISE;
1101 
1102 end get_displayed_value;
1103 
1104 
1105 /* ===========================================================================
1106   PROCEDURE NAME: get_displayed_value(x_lookup_type       IN  VARCHAR2,
1107                                       x_lookup_code       IN  VARCHAR2,
1108 				      x_disp_value	  OUT NOCOPY VARCHAR2)
1109 
1110   DESCRIPTION   : Obtain the  displayed field . This is an overloaded
1111 		  procedure
1112 
1113   CLIENT/SERVER : SERVER
1114 
1115   LIBRARY NAME  :
1116 
1117   OWNER         : Ramana Mulpury
1118 
1119   PARAMETERS    : lookup_type   VARCHAR2
1120                   lookup_code   VARCHAR2
1121  		  displayed_field VARCHAR2
1122 
1123 
1124   ALGORITHM     : Get the displayed field from the table po_lookup_codes.
1125 
1126   NOTES         :
1127 
1128 =========================================================================== */
1129 PROCEDURE get_displayed_value (x_lookup_type       IN  VARCHAR2,
1130                                x_lookup_code       IN  VARCHAR2,
1131 			       x_disp_value	   OUT NOCOPY VARCHAR2) IS
1132 
1133 x_progress  VARCHAR2(3) := NULL;
1134 
1135 begin
1136 
1137   x_progress := 10;
1138 
1139   SELECT plc.displayed_field
1140   INTO   x_disp_value
1141   FROM   po_lookup_codes plc
1142   WHERE  plc.lookup_code = x_lookup_code
1143   AND    plc.lookup_type = x_lookup_type;
1144 
1145   EXCEPTION
1146   WHEN NO_DATA_FOUND THEN
1147     x_disp_value   := NULL;
1148 
1149   WHEN OTHERS THEN
1150     po_message_s.sql_error('get_displayed_value', x_progress, sqlcode);
1151     RAISE;
1152 
1153 end get_displayed_value;
1154 
1155 /*===========================================================================
1156   FUNCTION NAME:	get_total
1157 
1158 ===========================================================================*/
1159 FUNCTION  get_total (x_object_type     IN VARCHAR2,
1160                      x_object_id       IN NUMBER) RETURN NUMBER IS
1161 	x_total		NUMBER;
1162 BEGIN
1163     x_total := get_total(x_object_type,
1164 		  	 x_object_id,
1165 		  	 NULL);
1166     return(x_total);
1167 END;
1168 
1169 
1170 -- Bug 5124868: as part of refactoring for this bug, the get_entity_org_id
1171 -- method was removed from this package and moved to PO_MOAC_UTILS_PVT.
1172 -- get_entity_org_id was originally created for bug 5092574
1173 
1174 
1175 /*===========================================================================
1176   FUNCTION NAME:	get_total
1177 
1178 ===========================================================================*/
1179 --<CONTERMS FPJ START>
1180 -- When making any change to this function please check if get_archive_total/get_archive_total_for_any_rev
1181 -- also needs to be change correspondigly
1182 --<CONTERMS FPJ END>
1183 FUNCTION  get_total (x_object_type     IN VARCHAR2,
1184                      x_object_id       IN NUMBER,
1185                      x_base_cur_result IN BOOLEAN) RETURN NUMBER IS
1186   x_progress       VARCHAR2(3) := NULL;
1187   x_base_currency  VARCHAR2(16);
1188   x_po_currency    VARCHAR2(16);
1189   x_min_unit       NUMBER;
1190   x_base_min_unit  NUMBER;
1191   x_precision      INTEGER;
1192   x_base_precision INTEGER;
1193   x_result_fld     NUMBER;
1194   l_org_id         HR_ALL_ORGANIZATION_UNITS.organization_id%type;
1195 BEGIN
1196   if (x_object_type in ('H','B') ) then
1197 
1198     if x_base_cur_result then
1199       /* Result should be returned in base currency. Get the currency code
1200          of the PO and the base currency code
1201       */
1202       x_progress := 10;
1203       po_core_s2.get_po_currency (x_object_id,
1204                        x_base_currency,
1205                        x_po_currency );
1206 
1207       /* Chk if base_currency = po_currency */
1208       if x_base_currency <> x_po_currency then
1209         /* Get precision and minimum accountable unit of the PO CURRENCY */
1210         x_progress := 20;
1211         po_core_s2.get_currency_info (x_po_currency,
1212                            x_precision,
1213                            x_min_unit );
1214 
1215         /* Get precision and minimum accountable unit of the base CURRENCY */
1216         x_progress := 30;
1217         po_core_s2.get_currency_info (x_base_currency,
1218                            x_base_precision,
1219                            x_base_min_unit );
1220 
1221 
1222 
1223         if X_base_min_unit is null  then
1224 
1225           if X_min_unit is null then
1226 
1227             x_progress := 40;
1228 
1229 /* 958792 kbenjami 8/25/99.  Proprogated fix from R11.
1230    849493 - SVAIDYAN: Do a sum(round()) instead of round(sum()) since what
1231                       we pass to GL is the round of individual dist. amounts
1232                       and the sum of these rounded values is what should be
1233                       displayed as the header total.
1234 */
1235             -- <SERVICES FPJ>
1236             -- For the new Services lines, quantity will be null.
1237             -- Hence, added a decode statement to use amount directly
1238             -- in the total amount calculation when quantity is null.
1239             --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1240             SELECT nvl(sum(round(
1241                    round(
1242                        (decode(POD.quantity_ordered,
1243                                null,
1244                                (nvl(POD.amount_ordered, 0) -
1245                                nvl(POD.amount_cancelled, 0)),
1246                                ((nvl(POD.quantity_ordered, 0) -
1247                                nvl(POD.quantity_cancelled, 0)) *
1248                                nvl(PLL.price_override, 0))
1249                               )
1250                        * POD.rate
1251                        )
1252                    , X_precision) ,
1253                    X_base_precision)),0)
1254             INTO   X_result_fld
1255             FROM   PO_DISTRIBUTIONS_ALL POD, PO_LINE_LOCATIONS_ALL PLL
1256             WHERE  PLL.po_header_id = X_object_id
1257             AND    PLL.shipment_type in ('STANDARD','PLANNED','BLANKET')
1258             AND    PLL.line_location_id = POD.line_location_id;
1259 
1260           else
1261             x_progress := 42;
1262 
1263             -- <SERVICES FPJ>
1264             -- For the new Services lines, quantity will be null.
1265             -- Hence, added a decode statement to use amount directly
1266             -- in the total amount calculation when quantity is null.
1267             --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1268             SELECT nvl(sum(round(
1269                    round(
1270                        decode(POD.quantity_ordered,
1271                               null,
1272                               (nvl(POD.amount_ordered, 0) -
1273                               nvl(POD.amount_cancelled, 0)),
1274                               ((nvl(POD.quantity_ordered, 0) -
1275                               nvl(POD.quantity_cancelled, 0)) *
1276                               nvl(PLL.price_override, 0))
1277                              )
1278                        * POD.rate / X_min_unit
1279                         )
1280                    * X_min_unit , X_base_precision)),0)
1281             INTO   X_result_fld
1282             FROM   PO_DISTRIBUTIONS_ALL POD, PO_LINE_LOCATIONS_ALL PLL
1283             WHERE  PLL.po_header_id = X_object_id
1284             AND    PLL.shipment_type in ('STANDARD','PLANNED','BLANKET')
1285             AND    PLL.line_location_id = POD.line_location_id;
1286 
1287           end if;
1288 
1289         else /* base_min_unit is NOT null */
1290 
1291           if X_min_unit is null then
1292             x_progress := 44;
1293 
1294             -- <SERVICES FPJ>
1295             -- For the new Services lines, quantity will be null.
1296             -- Hence, added a decode statement to use amount directly
1297             -- in the total amount calculation when quantity is null.
1298             --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1299             SELECT nvl(sum(round( round(
1300                    decode(POD.quantity_ordered,
1301                           null,
1302                           (nvl(POD.amount_ordered, 0) -
1303                           nvl(POD.amount_cancelled, 0)),
1304                           (nvl(POD.quantity_ordered, 0) -
1305                           nvl(POD.quantity_cancelled, 0))
1306                           * nvl(PLL.price_override, 0)
1307                          )
1308                    * POD.rate , X_precision)
1309                    / X_base_min_unit ) * X_base_min_unit) ,0)
1310             INTO   X_result_fld
1311             FROM   PO_DISTRIBUTIONS_ALL POD, PO_LINE_LOCATIONS_ALL PLL
1312             WHERE  PLL.po_header_id = X_object_id
1313             AND    PLL.shipment_type in ('STANDARD','PLANNED','BLANKET')
1314             AND    PLL.line_location_id = POD.line_location_id;
1315 
1316           else
1317             x_progress := 46;
1318 
1319             -- <SERVICES FPJ>
1320             -- For the new Services lines, quantity will be null.
1321             -- Hence, added a decode statement to use amount directly
1322             -- in the total amount calculation when quantity is null.
1323             --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1324             SELECT nvl(sum(round( round(
1325                    decode(POD.quantity_ordered,
1326                           null,
1327                           (nvl(POD.amount_ordered, 0) -
1328                           nvl(POD.amount_cancelled, 0)),
1329                           (nvl(POD.quantity_ordered, 0) -
1330                           nvl(POD.quantity_cancelled, 0))
1331                           * nvl(PLL.price_override, 0)
1332                          )
1333                    * POD.rate /
1334                    X_min_unit) * X_min_unit  / X_base_min_unit)
1335                    * X_base_min_unit) , 0)
1336             INTO   X_result_fld
1337             FROM   PO_DISTRIBUTIONS_ALL POD, PO_LINE_LOCATIONS_ALL PLL
1338             WHERE  PLL.po_header_id = X_object_id
1339             AND    PLL.shipment_type in ('STANDARD','PLANNED','BLANKET')
1340             AND    PLL.line_location_id = POD.line_location_id;
1341 
1342           end if;
1343 
1344         end if;
1345 
1346       end if;  /* x_base_currency <> x_po_currency */
1347 
1348     else
1349 
1350       /* if we donot want result converted to base currency or if
1351          the currencies are the same then do the check without
1352          rate conversion */
1353 
1354 /* 958792 kbenjami 8/25/99.  Proprogated fix from R11.
1355    849493 - SVAIDYAN: Do a sum(round()) instead of round(sum()) since what
1356                       we pass to GL is the round of individual dist. amounts
1357                       and the sum of these rounded values is what should be
1358                       displayed as the header total.
1359 */
1360 
1361       x_progress := 50;
1362 
1363       --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1364       SELECT c.minimum_accountable_unit,
1365              c.precision
1366       INTO   x_min_unit,
1367              x_precision
1368       FROM   FND_CURRENCIES C,
1369              PO_HEADERS_ALL     PH
1370       WHERE  PH.po_header_id  = x_object_id
1371       AND    C.currency_code  = PH.CURRENCY_CODE;
1372 
1373       if x_min_unit is null then
1374 	x_progress := 53;
1375 
1376 	-- <SERVICES FPJ>
1377         -- For the new Services lines, quantity will be null.
1378         -- Hence, added a decode statement to use amount directly
1379         -- in the total amount calculation when quantity is null.
1380         --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1381         select sum(round(
1382                decode(pll.quantity,
1383                       null,
1384                       (pll.amount - nvl(pll.amount_cancelled,0)),
1385                       (pll.quantity - nvl(pll.quantity_cancelled,0))
1386                       * nvl(pll.price_override,0)
1387                      )
1388                ,x_precision))
1389         INTO   x_result_fld
1390         FROM   PO_LINE_LOCATIONS_ALL PLL
1391         WHERE  PLL.po_header_id   = x_object_id
1392         AND    PLL.shipment_type in ('STANDARD','PLANNED','BLANKET');
1393 
1394       else
1395 		/* Bug 1111926: GMudgal 2/18/2000
1396 		** Incorrect placement of brackets caused incorrect rounding
1397 		** and consequently incorrect PO header totals
1398 		*/
1399         x_progress := 56;
1400 
1401         -- <SERVICES FPJ>
1402         -- For the new Services lines, quantity will be null.
1403         -- Hence, added a decode statement to use amount directly
1404         -- in the total amount calculation when quantity is null.
1405         --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1406         select sum(round(
1407                decode(pll.quantity,
1408                       null,
1409                       (pll.amount - nvl(pll.amount_cancelled, 0)),
1410                       (pll.quantity - nvl(pll.quantity_cancelled, 0))
1411                       * nvl(pll.price_override,0)
1412                      )
1413                / x_min_unit)
1414                * x_min_unit)
1415         INTO   x_result_fld
1416         FROM   PO_LINE_LOCATIONS_ALL PLL
1417         WHERE  PLL.po_header_id   = x_object_id
1418         AND    PLL.shipment_type in ('STANDARD','PLANNED','BLANKET');
1419 
1420       end if;
1421 
1422     end if;
1423 
1424     -- <GA FPI START>
1425     --
1426 
1427     -- <GC FPJ>
1428     -- Change x_object_type for GA from 'G' to 'GA'
1429 
1430     ELSIF ( x_object_type = 'GA' ) THEN                       -- Global Agreement
1431 
1432         x_result_fld := get_ga_amount_released( x_object_id, x_base_cur_result );
1433     --
1434     -- <GA FPI END>
1435 
1436    -- <GC FPJ START>
1437    ELSIF (x_object_type = 'GC') THEN                       -- Global Contract
1438        x_result_fld := get_gc_amount_released
1439                        (
1440                            p_po_header_id    => x_object_id,
1441                            p_convert_to_base => x_base_cur_result
1442                        );
1443 
1444    -- <GC FPJ END>
1445 
1446    elsif (x_object_type = 'P') then /* For PO Planned */
1447 
1448      if x_base_cur_result then
1449 
1450       /* Result should be returned in base currency. Get the currency code
1451          of the PO and the base currency code */
1452 
1453       x_progress := 60;
1454       po_core_s2.get_po_currency (x_object_id,
1455                        x_base_currency,
1456                        x_po_currency );
1457 
1458       /* Chk if base_currency = po_currency */
1459       if x_base_currency <> x_po_currency then
1460         /* Get precision and minimum accountable unit of the PO CURRENCY */
1461         x_progress := 70;
1462         po_core_s2.get_currency_info (x_po_currency,
1463                            x_precision,
1464                            x_min_unit );
1465 
1466         /* Get precision and minimum accountable unit of the base CURRENCY */
1467         x_progress := 80;
1468         po_core_s2.get_currency_info (x_base_currency,
1469                            x_base_precision,
1470                            x_base_min_unit );
1471 
1472 /* iali - Bug 482497 - 05/09/97
1473    For Planned PO the PLL.shipment_type should be 'PLANNED' and not
1474    'SCHEDULED' as it was before. Adding both in the where clause by replacing
1475    eqality check with in clause.
1476 */
1477 -- Bugs 482497 and 602664, lpo, 12/22/97
1478 -- Actually, for planned PO, the shipment_type should remain to be 'SCHEDULED'.
1479 -- This will calculate the total released amount. (a shipment type of 'PLANNED'
1480 -- indicates the lines in the planned PO, therefore using IN ('PLANNED',
1481 -- 'SCHEDULED') will calculate the total released amount plus the amount
1482 -- agreed, which is not what we want.
1483 -- Refer to POXBWN3B.pls for fix to bug 482497.
1484 
1485         if X_base_min_unit is null  then
1486 
1487           if X_min_unit is null then
1488 
1489             x_progress := 90;
1490 
1491 /* 958792 kbenjami 8/25/99.  Proprogated fix from R11.
1492    849493 - SVAIDYAN: Do a sum(round()) instead of round(sum()) since what
1493                       we pass to GL is the round of individual dist. amounts
1494                       and the sum of these rounded values is what should be
1495                       displayed as the header total.
1496 */
1497             --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1498             SELECT nvl(sum(round( round((nvl(POD.quantity_ordered, 0) -
1499                    nvl(POD.quantity_cancelled, 0)) *
1500                    nvl(PLL.price_override, 0) * POD.rate, X_precision) ,
1501                    X_base_precision)),0)
1502             INTO   X_result_fld
1503             FROM   PO_DISTRIBUTIONS_ALL POD, PO_LINE_LOCATIONS_ALL PLL
1504             WHERE  PLL.po_header_id     = X_object_id
1505 -- Bugs 482497 and 602664, lpo, 12/22/97
1506             AND    PLL.shipment_type    = 'SCHEDULED'
1507 -- End of fix. Bugs 482497 and 602664, lpo, 12/22/97
1508             AND    PLL.line_location_id = POD.line_location_id;
1509 
1510           else
1511             x_progress := 92;
1512             --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1513             SELECT nvl(sum(round( round((nvl(POD.quantity_ordered, 0) -
1514                    nvl(POD.quantity_cancelled, 0)) *
1515                    nvl(PLL.price_override, 0) * POD.rate /
1516                    X_min_unit) * X_min_unit , X_base_precision)),0)
1517             INTO   X_result_fld
1518             FROM   PO_DISTRIBUTIONS_ALL POD, PO_LINE_LOCATIONS_ALL PLL
1519             WHERE  PLL.po_header_id     = X_object_id
1520 -- Bugs 482497 and 602664, lpo, 12/22/97
1521             AND    PLL.shipment_type   	= 'SCHEDULED'
1522 -- End of fix. Bugs 482497 and 602664, lpo, 12/22/97
1523             AND    PLL.line_location_id = POD.line_location_id;
1524           end if;
1525 
1526         else /* base_min_unit is NOT null */
1527 
1528           if X_min_unit is null then
1529             x_progress := 94;
1530             --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1531             SELECT nvl(sum(round( round((nvl(POD.quantity_ordered, 0) -
1532                    nvl(POD.quantity_cancelled, 0)) *
1533                    nvl(PLL.price_override, 0) * POD.rate , X_precision)
1534                    / X_base_min_unit ) * X_base_min_unit) ,0)
1535             INTO   X_result_fld
1536             FROM   PO_DISTRIBUTIONS_ALL POD, PO_LINE_LOCATIONS_ALL PLL
1537             WHERE  PLL.po_header_id     = X_object_id
1538 -- Bugs 482497 and 602664, lpo, 12/22/97
1539             AND    PLL.shipment_type    = 'SCHEDULED'
1540 -- End of fix. Bugs 482497 and 602664, lpo, 12/22/97
1541             AND    PLL.line_location_id = POD.line_location_id;
1542 
1543           else
1544             x_progress := 96;
1545             --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1546             SELECT nvl(sum(round( round((nvl(POD.quantity_ordered, 0) -
1547                                          nvl(POD.quantity_cancelled, 0)) *
1548                    nvl(PLL.price_override, 0) * POD.rate /
1549                    X_min_unit) * X_min_unit  / X_base_min_unit)
1550                    * X_base_min_unit) , 0)
1551             INTO   X_result_fld
1552             FROM   PO_DISTRIBUTIONS_ALL POD, PO_LINE_LOCATIONS_ALL PLL
1553             WHERE  PLL.po_header_id     = X_object_id
1554 -- Bugs 482497 and 602664, lpo, 12/22/97
1555             AND    PLL.shipment_type    = 'SCHEDULED'
1556 -- End of fix. Bugs 482497 and 602664, lpo, 12/22/97
1557             AND    PLL.line_location_id = POD.line_location_id;
1558           end if;
1559 
1560         end if;
1561 
1562       end if;  /* x_base_currency <> x_po_currency */
1563 
1564     else
1565 
1566       /* if we donot want result converted to base currency or if
1567          the currencies are the same then do the check without
1568          rate conversion */
1569       x_progress := 100;
1570       --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1571       SELECT c.minimum_accountable_unit,
1572              c.precision
1573       INTO   x_min_unit,
1574              x_precision
1575       FROM   FND_CURRENCIES C,
1576              PO_HEADERS_ALL     PH
1577       WHERE  PH.po_header_id  = x_object_id
1578       AND    C.currency_code  = PH.CURRENCY_CODE;
1579 
1580 /* 958792 kbenjami 8/25/99.  Proprogated fix from R11.
1581    849493 - SVAIDYAN: Do a sum(round()) instead of round(sum()) since what
1582                       we pass to GL is the round of individual dist. amounts
1583                       and the sum of these rounded values is what should be
1584                       displayed as the header total.
1585 */
1586       if x_min_unit is null then
1587         x_progress := 103;
1588         --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1589         select sum(round((pll.quantity - nvl(pll.quantity_cancelled,0))*
1590                          nvl(pll.price_override,0),x_precision))
1591         INTO   x_result_fld
1592         FROM   PO_LINE_LOCATIONS_ALL PLL
1593         WHERE  PLL.po_header_id   = x_object_id
1594 -- Bugs 482497 and 602664, lpo, 12/22/97
1595         AND    PLL.shipment_type  = 'SCHEDULED';
1596 -- Bugs 482497 and 602664, lpo, 12/22/97
1597       else
1598 		/* Bug 1111926: GMudgal 2/18/2000
1599 		** Incorrect placement of brackets caused incorrect rounding
1600 		** and consequently incorrect PO header totals
1601 		*/
1602         x_progress := 106;
1603         --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1604         select sum(round((pll.quantity -
1605                           nvl(pll.quantity_cancelled,0)) *
1606                           nvl(pll.price_override,0)/x_min_unit)*
1607                           x_min_unit)
1608         INTO   x_result_fld
1609         FROM   PO_LINE_LOCATIONS_ALL PLL
1610         WHERE  PLL.po_header_id   = x_object_id
1611 -- Bugs 482497 and 602664, lpo, 12/22/97
1612         AND    PLL.shipment_type  = 'SCHEDULED';
1613 -- End of fix. Bugs 482497 and 602664, lpo, 12/22/97
1614       end if;
1615 
1616     end if;
1617 
1618   elsif (x_object_type = 'E' ) then /* Requisition Header */
1619     x_progress := 110;
1620     --bug#5092574 Retrieve the doc org id and pass this to retrieve
1621     --the document currency. Bug 5124868: refactored this call
1622     l_org_id := PO_MOAC_UTILS_PVT.get_entity_org_id(
1623                   PO_MOAC_UTILS_PVT.g_doc_type_REQUISITION
1624                 , PO_MOAC_UTILS_PVT.g_doc_level_HEADER
1625                 , x_object_id);--bug#5092574
1626 
1627     po_core_s2.get_req_currency (x_object_id,
1628                                  x_base_currency,
1629                                  l_org_id);--bug#5092574
1630 
1631     x_progress := 120;
1632     po_core_s2.get_currency_info (x_base_currency,
1633                        x_base_precision,
1634                        x_base_min_unit );
1635 
1636     if x_base_min_unit is null then
1637       x_progress := 130;
1638 
1639 /*    Bug No. 1431811 Changing the round of sum to sum of rounded totals
1640 
1641       round(sum((nvl(quantity,0) * nvl(unit_price,0))), x_base_precision)
1642 */
1643       -- <SERVICES FPJ>
1644       -- For the new Services lines, quantity will be null.
1645       -- Hence, added a decode statement to use amount directly
1646       -- in the total amount calculation when quantity is null.
1647       --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1648       -- <Bug 4036549, include cancelled lines with with delivered quantity>
1649 
1650       select sum(round(
1651                  decode(quantity,
1652                         null,
1653       	                nvl(amount, 0),
1654                         (
1655                           (nvl(quantity,0) - nvl(quantity_cancelled,0))* nvl(unit_price,0)
1656                         )
1657                        )
1658 	     , x_base_precision))
1659       INTO   x_result_fld
1660       FROM   PO_REQUISITION_LINES_ALL
1661       WHERE  requisition_header_id            = x_object_id
1662       AND    nvl(modified_by_agent_flag, 'N') = 'N';
1663 
1664     else
1665       x_progress := 135;
1666 
1667 /*    Bug No. 1431811 Changing the round of sum to sum of rounded totals
1668       select
1669       round(sum((nvl(quantity,0) * nvl(unit_price,0)/x_base_min_unit)*
1670                  x_base_min_unit))
1671 */
1672       -- <SERVICES FPJ>
1673       -- For the new Services lines, quantity will be null.
1674       -- Hence, added a decode statement to use amount directly
1675       -- in the total amount calculation when quantity is null.
1676       --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1677       -- <Bug 4036549, include cancelled lines with with delivered quantity>
1678       select sum(round(
1679              decode(quantity,
1680                     null,
1681                     nvl(amount, 0),
1682                     (
1683                       (nvl(quantity,0) - nvl(quantity_cancelled,0))* nvl(unit_price,0)
1684                     )
1685                    )
1686              /x_base_min_unit)*
1687              x_base_min_unit)
1688       INTO   x_result_fld
1689       FROM   PO_REQUISITION_LINES_ALL
1690       WHERE  requisition_header_id            = x_object_id
1691       AND    nvl(modified_by_agent_flag, 'N') = 'N';
1692 
1693     end if;
1694 
1695   elsif (x_object_type = 'I' ) then /* Requisition Line */
1696 
1697     x_progress := 140;
1698     --bug#5092574 Retrieve the doc org id and pass this to retrieve
1699     --the document currency. Bug 5124868: refactored this call
1700     l_org_id := PO_MOAC_UTILS_PVT.get_entity_org_id(
1701                   PO_MOAC_UTILS_PVT.g_doc_type_REQUISITION
1702                 , PO_MOAC_UTILS_PVT.g_doc_level_LINE
1703                 , x_object_id);--bug#5092574
1704 
1705     po_core_s2.get_req_currency (x_object_id,
1706                                  x_base_currency,
1707                                  l_org_id);--bug#5092574
1708 
1709     x_progress := 150;
1710     po_core_s2.get_currency_info (x_base_currency,
1711                        x_base_precision,
1712                        x_base_min_unit );
1713 
1714     if x_base_min_unit is null then
1715       x_progress := 160;
1716 
1717 /*    Bug No. 1431811 Changing the round of sum to sum of rounded totals
1718 
1719       select
1720       round(sum((nvl(quantity,0) * nvl(unit_price,0))), x_base_precision)
1721 */
1722       -- <SERVICES FPJ>
1723       -- For the new Services lines, quantity will be null.
1724       -- Hence, added a decode statement to use amount directly
1725       -- in the total amount calculation when quantity is null.
1726       --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1727       -- <Bug 4036549, include cancelled lines with with delivered quantity>
1728       select sum(round(
1729 	     decode(quantity,
1730                     null,
1731                     nvl(amount, 0),
1732                     (
1733                       (nvl(quantity,0) - nvl(quantity_cancelled,0))* nvl(unit_price,0)
1734                     )
1735               )
1736              , x_base_precision))
1737       INTO   x_result_fld
1738       FROM   PO_REQUISITION_LINES_ALL
1739       WHERE  requisition_line_id              = x_object_id
1740       AND    nvl(modified_by_agent_flag, 'N') = 'N';
1741 
1742     else
1743       x_progress := 165;
1744       -- <SERVICES FPJ>
1745       -- For the new Services lines, quantity will be null.
1746       -- Hence, added a decode statement to use amount directly
1747       -- in the total amount calculation when quantity is null.
1748       --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1749       -- <Bug 4036549, include cancelled lines with with delivered quantity>
1750       select round(sum((
1751              decode(quantity,
1752                     null,
1753                     nvl(amount, 0),
1754                     (
1755                       (nvl(quantity,0) - nvl(quantity_cancelled,0))* nvl(unit_price,0)
1756                     )
1757                     )
1758              /x_base_min_unit)*
1759              x_base_min_unit))
1760       INTO   x_result_fld
1761       FROM   PO_REQUISITION_LINES_ALL
1762       WHERE  requisition_line_id              = x_object_id
1763       AND    nvl(modified_by_agent_flag, 'N') = 'N';
1764 
1765     end if;
1766     x_progress := 160;
1767 
1768     elsif (x_object_type = 'J' ) then /* Requisition Distribution */
1769 
1770     x_progress := 162;
1771     --bug#5092574 Retrieve the doc org id and pass this to retrieve
1772     --the document currency. Bug 5124868: refactored this call
1773     l_org_id := PO_MOAC_UTILS_PVT.get_entity_org_id(
1774                   PO_MOAC_UTILS_PVT.g_doc_type_REQUISITION
1775                 , PO_MOAC_UTILS_PVT.g_doc_level_DISTRIBUTION
1776                 , x_object_id);--bug#5092574
1777 
1778     po_core_s2.get_req_currency (x_object_id,
1779                                  x_base_currency,
1780                                  l_org_id);--bug#5092574
1781     x_progress := 164;
1782     po_core_s2.get_currency_info (x_base_currency,
1783                        x_base_precision,
1784                        x_base_min_unit );
1785 
1786     x_progress := 166;
1787 
1788     -- <SERVICES FPJ>
1789     -- Modified the SELECT statement to take account into Services
1790     -- lines. For the new Services lines, quantity will be null.
1791     -- Hence, added decode statements to use amount directly
1792     -- in the total amount calculation when quantity is null.
1793     --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1794     SELECT
1795     sum( decode
1796        ( x_base_min_unit, NULL,
1797              decode(quantity, NULL,
1798                     round( nvl(PORD.req_line_amount, 0),
1799                            x_base_precision),
1800                     round( nvl(PORD.req_line_quantity, 0) *
1801                            nvl(PORL.unit_price, 0),
1802                            x_base_precision)
1803                     ),
1804              decode(quantity, NULL,
1805                     round((nvl(PORD.req_line_amount, 0) /
1806                            x_base_min_unit) *
1807                            x_base_min_unit),
1808                     round((nvl(PORD.req_line_quantity, 0) *
1809                            nvl(PORL.unit_price, 0) /
1810                            x_base_min_unit) *
1811                            x_base_min_unit)
1812                    )))
1813     INTO   x_result_fld
1814     FROM   PO_REQ_DISTRIBUTIONS_ALL PORD,
1815            PO_REQUISITION_LINES_ALL PORL
1816     WHERE  PORD.distribution_id             = x_object_id
1817     AND    PORD.requisition_line_id         = PORL.requisition_line_id;
1818 
1819   elsif (x_object_type = 'C' ) then /* Contract */
1820 
1821     x_progress := 170;
1822     --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1823     SELECT c.minimum_accountable_unit,
1824            c.precision
1825     INTO   x_min_unit,
1826            x_precision
1827     FROM   FND_CURRENCIES C,
1828            PO_HEADERS_ALL     PH
1829     WHERE  PH.po_header_id  = x_object_id
1830     AND    C.currency_code  = PH.CURRENCY_CODE;
1831 
1832 /* 716188 - SVAIDYAN : Changed the sql stmt to select only Standard and Planned
1833    POs that reference this contract and also to convert the amount into the
1834    Contract's currency. This is achieved by converting the PO amt first to the
1835    functional currency and then changing this to the Contract currency */
1836 
1837 /* 716188 - Added an outer join on PO_DISTRIBUTIONS */
1838 /* 866358 - BPESCHAN: Changed the sql stmt to select quantity_ordered and
1839    quantity_cancelled from PO_DISTRIBUTIONS instead of PO_LINE_LOCATIONS.
1840    This fix prevents incorrect calculation for amount release when more then
1841    one distribution exists. */
1842 
1843 /* 958792 kbenjami 8/25/99.  Proprogated fix from R11.
1844    849493 - SVAIDYAN: Do a sum(round()) instead of round(sum()) since what
1845                       we pass to GL is the round of individual dist. amounts
1846                       and the sum of these rounded values is what should be
1847                       displayed as the header total.
1848 */
1849 /*Bug3760487:Purchase Order form was displaying incorrect released
1850   amount for foreign currency contract when the PO currency is same
1851   as the contract currency and the rates were different.Added the decode
1852   to perform the currency conversion only when the currency code of
1853   PO and contract are different.
1854   Also removed the join to FND_CURRENCIES
1855 */
1856     if x_min_unit is null then
1857       x_progress := 172;
1858       --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1859       SELECT
1860       nvl(sum(decode(PH.currency_code, PH1.currency_code,
1861                       round((nvl(POD.quantity_ordered,0) -
1862                          nvl(POD.quantity_cancelled,0))
1863                          * nvl(pll.price_override,0),x_precision),
1864                       round((nvl(POD.quantity_ordered,0) -
1865                          nvl(POD.quantity_cancelled,0))
1866                          * nvl(pll.price_override,0)
1867                          * nvl(POD.rate, nvl(PH1.rate,1))/nvl(PH.rate,1),x_precision)
1868               )),0)
1869       INTO   x_result_fld
1870       FROM   PO_DISTRIBUTIONS_ALL POD,
1871              PO_LINE_LOCATIONS_ALL PLL,
1872              PO_LINES_ALL PL,
1873              PO_HEADERS_ALL PH,
1874              PO_HEADERS_ALL PH1
1875              --,FND_CURRENCIES C
1876       WHERE  PH.po_header_id      = x_object_id
1877       AND    PH.po_header_id      = PL.contract_id           -- <GC FPJ>
1878       --AND    PH.currency_code     = C.currency_code
1879       AND    PL.po_line_id        = PLL.po_line_id
1880       AND    PLL.shipment_type in ('STANDARD','PLANNED')
1881       AND    POD.line_location_id(+) = PLL.line_location_id
1882       AND    PH1.po_header_id = PL.po_header_id;
1883     else
1884 
1885 /* 958792 kbenjami 8/25/99.  Proprogated fix from R11.
1886    849493 - SVAIDYAN: Do a sum(round()) instead of round(sum()) since what
1887                       we pass to GL is the round of individual dist. amounts
1888                       and the sum of these rounded values is what should be
1889                       displayed as the header total.
1890 */
1891       x_progress := 174;
1892       --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1893       SELECT
1894       nvl(sum(decode(PH.currency_code, PH1.currency_code,
1895                  round((nvl(POD.quantity_ordered,0) -
1896                     nvl(POD.quantity_cancelled,0))
1897                     * nvl(pll.price_override,0)/x_min_unit),
1898                  round((nvl(POD.quantity_ordered,0) -
1899                     nvl(POD.quantity_cancelled,0))
1900                     * nvl(pll.price_override,0)
1901                     * nvl(POD.rate, nvl(PH1.rate,1))/nvl(PH.rate,1)/x_min_unit))
1902               * x_min_unit),0)
1903       INTO   x_result_fld
1904       FROM   PO_DISTRIBUTIONS_ALL POD,
1905              PO_LINE_LOCATIONS_ALL PLL,
1906              PO_LINES_ALL PL,
1907              PO_HEADERS_ALL PH,
1908              PO_HEADERS_ALL PH1
1909              --,FND_CURRENCIES C
1910       WHERE  PH.po_header_id      = x_object_id
1911       AND    PH.po_header_id      = PL.contract_id         -- <GC FPJ>
1912       --AND    PH.currency_code     = C.currency_code
1913       AND    PL.po_line_id        = PLL.po_line_id
1914       AND    PLL.shipment_type in ('STANDARD','PLANNED')
1915       AND    POD.line_location_id(+) = PLL.line_location_id
1916       AND    PH1.po_header_id = PL.po_header_id;
1917     end if;
1918 
1919 
1920   elsif (x_object_type = 'R' ) then /* Release */
1921 
1922     if x_base_cur_result then
1923       x_progress := 180;
1924       --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1925       SELECT GSB.currency_code,
1926              POH.currency_code
1927       INTO   x_base_currency,
1928              x_po_currency
1929       FROM   PO_HEADERS_ALL POH,
1930              FINANCIALS_SYSTEM_PARAMS_ALL FSP,
1931              GL_SETS_OF_BOOKS GSB,
1932              PO_RELEASES_ALL POR
1933       WHERE  POR.po_release_id   = x_object_id
1934       AND    POH.po_header_id    = POR.po_header_id
1935       AND    NVL(POR.org_id,-99) = NVL(FSP.org_id,-99)      --< Bug 3549096 >
1936       AND    FSP.set_of_books_id = GSB.set_of_books_id;
1937 
1938       if (x_base_currency <> x_po_currency) then
1939         /* Get precision and minimum accountable unit of the PO CURRENCY */
1940         x_progress := 190;
1941         po_core_s2.get_currency_info (x_po_currency,
1942                            x_precision,
1943                            x_min_unit );
1944 
1945         /* Get precision and minimum accountable unit of the base CURRENCY */
1946         x_progress := 200;
1947         po_core_s2.get_currency_info (x_base_currency,
1948                            x_base_precision,
1949                            x_base_min_unit );
1950 
1951         if x_base_min_unit is null then
1952           if x_min_unit is null then
1953             x_progress := 210;
1954 
1955 /* 958792 kbenjami 8/25/99.  Proprogated fix from R11.
1956    849493 - SVAIDYAN: Do a sum(round()) instead of round(sum()) since what
1957                       we pass to GL is the round of individual dist. amounts
1958                       and the sum of these rounded values is what should be
1959                       displayed as the header total.
1960 */
1961             -- <SERVICES FPJ>
1962             -- For the new Services lines, quantity will be null.
1963             -- Hence, added a decode statement to use amount directly
1964             -- in the total amount calculation when quantity is null.
1965             --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1966             select nvl(sum(round(round(
1967                    decode(POD.quantity_ordered,
1968                           null,
1969                           nvl(POD.amount_ordered, 0),
1970                           (nvl(POD.quantity_ordered,0) *
1971                           nvl(PLL.price_override,0))
1972                          )
1973                    * POD.rate
1974                    ,x_precision),x_base_precision)),0)
1975             INTO x_result_fld
1976             FROM PO_DISTRIBUTIONS_ALL POD, PO_LINE_LOCATIONS_ALL PLL
1977             WHERE PLL.po_release_id    = x_object_id
1978             AND   PLL.line_location_id = POD.line_location_id
1979             AND   PLL.shipment_type in ('SCHEDULED','BLANKET');
1980 
1981           else
1982             x_progress := 212;
1983             -- <SERVICES FPJ>
1984             -- For the new Services lines, quantity will be null.
1985             -- Hence, added a decode statement to use amount directly
1986             -- in the total amount calculation when quantity is null.
1987             --< Bug 3549096 > Use _ALL tables instead of org-striped views.
1988 	    SELECT nvl(sum(round( round(
1989                    decode(POD.quantity_ordered,
1990                           null,
1991                           nvl(POD.amount_ordered, 0),
1992                           (nvl(POD.quantity_ordered, 0) *
1993                           nvl(PLL.price_override, 0))
1994                          )
1995                    * POD.rate /
1996                    X_min_unit) * X_min_unit , X_base_precision)),0)
1997             INTO x_result_fld
1998             FROM PO_DISTRIBUTIONS_ALL POD, PO_LINE_LOCATIONS_ALL PLL
1999             WHERE PLL.po_release_id    = x_object_id
2000             AND   PLL.line_location_id = POD.line_location_id
2001             AND   PLL.shipment_type in ('SCHEDULED','BLANKET');
2002 
2003           end if;
2004         else
2005           if X_min_unit is null then
2006             x_progress := 214;
2007             -- <SERVICES FPJ>
2008             -- For the new Services lines, quantity will be null.
2009             -- Hence, added a decode statement to use amount directly
2010             -- in the total amount calculation when quantity is null.
2011             --< Bug 3549096 > Use _ALL tables instead of org-striped views.
2012             SELECT nvl(sum(round( round(
2013                    decode(POD.quantity_ordered,
2014                           null,
2015                           nvl(POD.amount_ordered, 0),
2016                           (nvl(POD.quantity_ordered, 0) *
2017                           nvl(PLL.price_override, 0))
2018                          )
2019                    * POD.rate
2020                    , X_precision)
2021                    / X_base_min_unit ) * X_base_min_unit) ,0)
2022             INTO   X_result_fld
2023             FROM PO_DISTRIBUTIONS_ALL POD, PO_LINE_LOCATIONS_ALL PLL
2024             WHERE PLL.po_release_id    = x_object_id
2025             AND   PLL.line_location_id = POD.line_location_id
2026             AND   PLL.shipment_type in ('SCHEDULED','BLANKET');
2027 
2028           else
2029             x_progress := 216;
2030             -- <SERVICES FPJ>
2031             -- For the new Services lines, quantity will be null.
2032             -- Hence, added a decode statement to use amount directly
2033             -- in the total amount calculation when quantity is null.
2034             --< Bug 3549096 > Use _ALL tables instead of org-striped views.
2035 	    SELECT nvl(sum(round( round(
2036                    decode(POD.quantity_ordered,
2037                           null,
2038                           nvl(POD.amount_ordered, 0),
2039                           (nvl(POD.quantity_ordered, 0) *
2040                           nvl(PLL.price_override, 0))
2041                          )
2042                    * POD.rate /
2043                    X_min_unit) * X_min_unit  / X_base_min_unit)
2044                    * X_base_min_unit) , 0)
2045             INTO   X_result_fld
2046             FROM PO_DISTRIBUTIONS_ALL POD, PO_LINE_LOCATIONS_ALL PLL
2047             WHERE PLL.po_release_id    = x_object_id
2048             AND   PLL.line_location_id = POD.line_location_id
2049             AND   PLL.shipment_type in ('SCHEDULED','BLANKET');
2050 
2051           end if;
2052         end if;
2053 
2054       end if;
2055     else
2056       x_progress := 220;
2057       --< Bug 3549096 > Use _ALL tables instead of org-striped views.
2058       SELECT c.minimum_accountable_unit,
2059              c.precision
2060       INTO   x_min_unit,
2061              x_precision
2062       FROM   FND_CURRENCIES C,
2063              PO_RELEASES_ALL    POR,
2064              PO_HEADERS_ALL     PH
2065       WHERE  POR.po_release_id = x_object_id
2066       AND    PH.po_header_id   = POR.PO_HEADER_ID
2067       AND    C.currency_code   = PH.CURRENCY_CODE;
2068 
2069       if x_min_unit is null then
2070 
2071 /* 958792 kbenjami 8/25/99.  Proprogated fix from R11.
2072    849493 - SVAIDYAN: Do a sum(round()) instead of round(sum()) since what
2073                       we pass to GL is the round of individual dist. amounts
2074                       and the sum of these rounded values is what should be
2075                       displayed as the header total.
2076 */
2077         x_progress := 222;
2078         -- <SERVICES FPJ>
2079         -- For the new Services lines, quantity will be null.
2080         -- Hence, added a decode statement to use amount directly
2081         -- in the total amount calculation when quantity is null.
2082         --< Bug 3549096 > Use _ALL tables instead of org-striped views.
2083         select sum(round(
2084                decode(pll.quantity,
2085                       null,
2086                       (pll.amount - nvl(pll.amount_cancelled,0)),
2087                       ((pll.quantity - nvl(pll.quantity_cancelled,0)) *
2088                       nvl(pll.price_override,0))
2089                      )
2090                ,x_precision))
2091         INTO   x_result_fld
2092         FROM   PO_LINE_LOCATIONS_ALL PLL
2093         WHERE  PLL.po_release_id   = x_object_id
2094         AND    PLL.shipment_type  in ( 'SCHEDULED','BLANKET');
2095 
2096       else
2097 		/* Bug 1111926: GMudgal 2/18/2000
2098 		** Incorrect placement of brackets caused incorrect rounding
2099 		** and consequently incorrect PO header totals
2100 		*/
2101         x_progress := 224;
2102 
2103         -- <SERVICES FPJ>
2104         -- For the new Services lines, quantity will be null.
2105         -- Hence, added a decode statement to use amount directly
2106         -- in the total amount calculation when quantity is null.
2107         --< Bug 3549096 > Use _ALL tables instead of org-striped views.
2108         select sum(round(
2109                decode(pll.quantity,
2110                       null,
2111                      (pll.amount - nvl(pll.amount_cancelled,0)),
2112                      ((pll.quantity - nvl(pll.quantity_cancelled,0)) *
2113                      nvl(pll.price_override,0))
2114                      )
2115                /x_min_unit) *
2116                x_min_unit)
2117         INTO   x_result_fld
2118         FROM   PO_LINE_LOCATIONS_ALL PLL
2119         WHERE  PLL.po_release_id   = x_object_id
2120         AND    PLL.shipment_type  in ( 'SCHEDULED','BLANKET');
2121 
2122       end if;
2123 
2124     end if;
2125 
2126   elsif (x_object_type = 'L' ) then /* Po Line */
2127     x_progress := 230;
2128     --< Bug 3549096 > Use _ALL tables instead of org-striped views.
2129     SELECT sum(c.minimum_accountable_unit),
2130            sum(c.precision)
2131     INTO   x_min_unit,
2132            x_precision
2133     FROM   FND_CURRENCIES C,
2134            PO_HEADERS_ALL     PH,
2135            PO_LINES_ALL POL
2136     WHERE  POL.po_line_id   = x_object_id
2137     AND    PH.po_header_id  = POL.po_header_id
2138     AND    C.currency_code  = PH.CURRENCY_CODE;
2139 
2140     if x_min_unit is null then
2141       x_progress := 232;
2142 
2143 /*    Bug No. 1431811 Changing the round of sum to sum of rounded totals
2144       select round(sum((pll.quantity - nvl(pll.quantity_cancelled,0))*
2145                        nvl(pll.price_override,0)),x_precision)
2146 */
2147 /*    Bug No. 1849112 In the previous fix of 143811 by mistake x_precision
2148       was not used.
2149 */
2150       -- <SERVICES FPJ>
2151       -- For the new Services lines, quantity will be null.
2152       -- Hence, added a decode statement to use amount directly
2153       -- in the total amount calculation when quantity is null.
2154       --< Bug 3549096 > Use _ALL tables instead of org-striped views.
2155       select sum(round((
2156              decode(pll.quantity,
2157                     null,
2158                     (pll.amount - nvl(pll.amount_cancelled, 0)),
2159                     (pll.quantity - nvl(pll.quantity_cancelled,0))
2160                     * nvl(pll.price_override,0)
2161                    )
2162              ),x_precision))
2163       INTO   x_result_fld
2164       FROM   PO_LINE_LOCATIONS_ALL PLL
2165       WHERE  PLL.po_line_id   = x_object_id
2166       AND    PLL.shipment_type  in ( 'STANDARD','BLANKET','PLANNED');
2167 
2168     else
2169       x_progress := 234;
2170 
2171 /*    Bug No. 1431811 Changing the round of sum to sum of rounded totals
2172 */
2173       -- <SERVICES FPJ>
2174       -- For the new Services lines, quantity will be null.
2175       -- Hence, added a decode statement to use amount directly
2176       -- in the total amount calculation when quantity is null.
2177       --< Bug 3549096 > Use _ALL tables instead of org-striped views.
2178       select sum(round((
2179              decode(pll.quantity,
2180                     null,
2181                     (pll.amount - nvl(pll.amount_cancelled,0)),
2182                     (pll.quantity - nvl(pll.quantity_cancelled,0))
2183                     * nvl(pll.price_override,0)
2184                    )
2185              / x_min_unit) * x_min_unit))
2186       INTO   x_result_fld
2187       FROM   PO_LINE_LOCATIONS_ALL PLL
2188       WHERE  PLL.po_line_id   = x_object_id
2189       AND    PLL.shipment_type  in ( 'STANDARD','BLANKET','PLANNED');
2190 
2191     end if;
2192 
2193   elsif (x_object_type = 'S' ) then /* PO Shipment */
2194     x_progress := 240;
2195     --< Bug 3549096 > Use _ALL tables instead of org-striped views.
2196     SELECT c.minimum_accountable_unit,
2197            c.precision
2198     INTO   x_min_unit,
2199            x_precision
2200     FROM   FND_CURRENCIES C,
2201            PO_HEADERS_ALL     PH,
2202            PO_LINE_LOCATIONS_ALL PLL
2203     WHERE  PLL.line_location_id   = x_object_id
2204     AND    PH.po_header_id        = PLL.po_header_id
2205     AND    C.currency_code        = PH.CURRENCY_CODE;
2206 
2207     if x_min_unit is null then
2208       x_progress := 242;
2209 
2210 /*    Bug No. 1431811 Changing the round of sum to sum of rounded totals
2211       select round(sum((pll.quantity - nvl(pll.quantity_cancelled,0))*
2212                        nvl(pll.price_override,0)),x_precision)
2213 */
2214       -- <SERVICES FPJ>
2215       -- For the new Services lines, quantity will be null.
2216       -- Hence, added a decode statement to use amount directly
2217       -- in the total amount calculation when quantity is null.
2218       --< Bug 3549096 > Use _ALL tables instead of org-striped views.
2219       select sum(round((
2220              decode(pll.quantity,
2221                     null,
2222                     (pll.amount - nvl(pll.amount_cancelled,0)),
2223                     (pll.quantity - nvl(pll.quantity_cancelled,0))
2224                     * nvl(pll.price_override,0)
2225                    )
2226              ),x_precision))
2227       INTO   x_result_fld
2228       FROM   PO_LINE_LOCATIONS_ALL PLL
2229       WHERE  PLL.line_location_id   = x_object_id;
2230 
2231     else
2232       x_progress := 244;
2233       -- <SERVICES FPJ>
2234       -- For the new Services lines, quantity will be null.
2235       -- Hence, added a decode statement to use amount directly
2236       -- in the total amount calculation when quantity is null.
2237       --< Bug 3549096 > Use _ALL tables instead of org-striped views.
2238       select round(sum((
2239              decode(pll.quantity,
2240                     null,
2241                     (pll.amount - nvl(pll.amount_cancelled,0)),
2242                     (pll.quantity - nvl(pll.quantity_cancelled,0))
2243                     * nvl(pll.price_override,0)
2244                    )
2245              /x_min_unit) * x_min_unit))
2246       INTO   x_result_fld
2247       FROM   PO_LINE_LOCATIONS_ALL PLL
2248       WHERE  PLL.line_location_id   = x_object_id;
2249 
2250     end if;
2251 
2252   end if; /* x_object_type */
2253 
2254   /* If x_result_fld has a null value, return 0 as the total. */
2255   IF x_result_fld IS NULL THEN
2256 	x_result_fld := 0;
2257   END IF;
2258 
2259   RETURN(x_result_fld);
2260 
2261   EXCEPTION
2262   WHEN OTHERS THEN
2263     RETURN(0);
2264     RAISE;
2265 
2266 END get_total;
2267 
2268 --<CONTERMS FPJ START>
2269 -------------------------------------------------------------------------------
2270 --Start of Comments
2271 --Name: GET_ARCHIVE_TOTAL
2272 --Pre-reqs:
2273 --  None
2274 --Modifies:
2275 --  None.
2276 --Locks:
2277 --  None.
2278 --Function:
2279 --   Returns total amount for latest archived revision for Standard Purchase order
2280 --Parameters:
2281 --IN:
2282 --  p_object_id
2283 --     PO header id
2284 --  p_doc_type
2285 --     The main doc type for PO. Valid values are 'PO'
2286 --  p_doc_subtype
2287 --     The lookup code of the document. Valid values are 'STANDARD'
2288 --  p_base_cur_result
2289 --     Whether result should be returned in base currency or transaction currency
2290 --      Valid Values are
2291 --      'Y'- Return result in Base/Functional Currency for the org
2292 --      'N'- Return result in Transaction currency in po
2293 --       Default 'N'
2294 --Testing:
2295 --  None
2296 --End of Comments
2297 ---------------------------------------------------------------------------
2298 FUNCTION  get_archive_total (p_object_id       IN NUMBER,
2299                              p_doc_type        IN VARCHAR2,
2300                              p_doc_subtype     IN VARCHAR2,
2301                              p_base_cur_result IN VARCHAR2) RETURN NUMBER IS
2302 
2303   l_base_currency       PO_HEADERS_ALL.CURRENCY_CODE%TYPE;
2304   l_po_currency         PO_HEADERS_ALL.CURRENCY_CODE%TYPE;
2305   l_min_unit            FND_CURRENCIES.MINIMUM_ACCOUNTABLE_UNIT%TYPE;
2306   l_base_min_unit       FND_CURRENCIES.MINIMUM_ACCOUNTABLE_UNIT%TYPE;
2307   l_precision           FND_CURRENCIES.PRECISION%TYPE;
2308   l_base_precision      FND_CURRENCIES.PRECISION%TYPE;
2309   l_archive_total_amt   NUMBER;
2310   l_progress            VARCHAR2(3):='000';
2311 BEGIN
2312   l_progress := '010';
2313   IF (p_doc_type = 'PO') AND (p_doc_subtype = 'STANDARD') then
2314 
2315         --Get the currency code of the PO and the base currency code
2316 
2317         po_core_s2.get_po_currency (x_object_id=>p_object_id,
2318                                     x_base_currency =>l_base_currency,
2319                                     x_po_currency  =>l_po_currency );
2320       l_progress := '020';
2321        --Get precision and minimum accountable unit of the PO CURRENCY
2322         po_core_s2.get_currency_info (x_currency_code => l_po_currency,
2323                                       x_precision=>l_precision,
2324                                       x_min_unit=>l_min_unit );
2325       l_progress := '030';
2326        -- Chk if base_currency = po_currency
2327        IF (p_base_cur_result = 'Y') AND (l_base_currency <> l_po_currency) then
2328 
2329           l_progress := '040';
2330             --Get precision and minimum accountable unit of the base CURRENCY
2331 
2332             po_core_s2.get_currency_info (x_currency_code => l_base_currency,
2333                                             x_precision=>l_base_precision,
2334                                              x_min_unit=>l_base_min_unit );
2335 
2336 
2337        ELSE -- if l_base_currency <> l_po_currency
2338          l_base_precision := l_precision;
2339          l_base_min_unit  := l_min_unit;
2340        END IF;  -- if l_base_currency <> l_po_currency
2341        l_progress := '050';
2342           --SQL WHAT- This query returns the total amount for an archived SPO
2343           --SQL WHY- To check if archived amount different from working copy amt. for a po in contract terms
2344           --SQL JOIN- Location id in PO_LINE_LOCATIONS_ARCHIVE_ALL and PO_DISTRIBUTIONS_ARCHIVE_ALL
2345           SELECT nvl(sum(round( ( (round( ( ( decode(POD.quantity_ordered, NULL,
2346                                                        (nvl(POD.amount_ordered,0) -
2347                                                          nvl(POD.amount_cancelled,0)
2348                                                        ),
2349                                                        ( (nvl(POD.quantity_ordered,0) -
2350                                                            nvl(POD.quantity_cancelled,0)
2351                                                          )*
2352                                                             nvl(PLL.price_override, 0)
2353                                                         )
2354                                                       ) *
2355                                                decode(p_base_cur_result,'Y',nvl(POD.rate,1),1)
2356                                             )/
2357                                              nvl(l_min_unit,1)
2358                                           ),decode(l_min_unit,null,l_precision,0)
2359                                         )*
2360                                          nvl(l_min_unit,1)
2361                                   )/
2362                                    nvl(l_base_min_unit,1)
2363                                 )
2364                                  ,decode(l_base_min_unit,null,l_base_precision,0)
2365                               )*
2366                                nvl(l_base_min_unit,1)
2367                         ), 0
2368                       )
2369             INTO   l_archive_total_amt
2370             FROM   PO_DISTRIBUTIONS_ARCHIVE_ALL POD, PO_LINE_LOCATIONS_ARCHIVE_ALL PLL
2371             WHERE  PLL.po_header_id = p_object_id
2372             AND    PLL.shipment_type in ('STANDARD')
2373             AND    PLL.line_location_id = POD.line_location_id
2374             AND    PLL.LATEST_EXTERNAL_FLAG = 'Y'
2375             AND    POD.LATEST_EXTERNAL_FLAG = 'Y';
2376 
2377         l_progress := '060';
2378   END if;-- (p_doc_type = 'PO') AND (p_doc_subtype = 'STANDARD')
2379   l_progress := '070';
2380   --If l_archive_total_amt has a null value, return 0 as the total.
2381   IF l_archive_total_amt IS NULL THEN
2382 	l_archive_total_amt := 0;
2383   END IF;
2384   l_progress := '080';
2385   RETURN(l_archive_total_amt);
2386 
2387 EXCEPTION
2388    WHEN OTHERS THEN
2389             IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_unexp_error)
2390             THEN
2391               fnd_msg_pub.add_exc_msg (g_pkg_name, 'GET_ARCHIVE_TOTAL',
2392                   SUBSTRB (SQLERRM , 1 , 200) || ' at location ' || l_progress);
2393             END IF;
2394             RETURN(0);
2395 END get_archive_total;
2396 --<CONTERMS FPJ END>
2397 
2398 
2399 
2400 --<POC FPJ START>
2401 
2402 
2403 FUNCTION  get_archive_total_for_any_rev (p_object_id       IN NUMBER,
2404                              p_object_type in varchar2,
2405                              p_doc_type        IN VARCHAR2,
2406                              p_doc_subtype     IN VARCHAR2,
2407                              p_doc_revision    IN NUMBER,
2408                              p_base_cur_result IN VARCHAR2) RETURN NUMBER IS
2409 
2410   l_base_currency       PO_HEADERS_ALL.CURRENCY_CODE%TYPE;
2411   l_po_currency         PO_HEADERS_ALL.CURRENCY_CODE%TYPE;
2412   l_min_unit            FND_CURRENCIES.MINIMUM_ACCOUNTABLE_UNIT%TYPE;
2413   l_base_min_unit       FND_CURRENCIES.MINIMUM_ACCOUNTABLE_UNIT%TYPE;
2414   l_precision           FND_CURRENCIES.PRECISION%TYPE;
2415   l_base_precision      FND_CURRENCIES.PRECISION%TYPE;
2416   l_archive_total_amt   NUMBER;
2417   l_progress            VARCHAR2(3):='000';
2418 
2419 BEGIN
2420   l_progress := '010';
2421   IF (p_doc_type = 'PO') AND (p_doc_subtype = 'STANDARD') then
2422 
2423     IF p_object_type = 'H' then
2424 
2425         --Get the currency code of the PO and the base currency code
2426 
2427         po_core_s2.get_po_currency (x_object_id=>p_object_id,
2428                                     x_base_currency =>l_base_currency,
2429                                     x_po_currency  =>l_po_currency );
2430         l_progress := '020';
2431         --Get precision and minimum accountable unit of the PO CURRENCY
2432         po_core_s2.get_currency_info (x_currency_code => l_po_currency,
2433                                       x_precision=>l_precision,
2434                                       x_min_unit=>l_min_unit );
2435         l_progress := '030';
2436         -- Chk if base_currency = po_currency
2437         IF (p_base_cur_result = 'Y') AND (l_base_currency <> l_po_currency) then
2438 
2439             l_progress := '040';
2440             --Get precision and minimum accountable unit of the base CURRENCY
2441 
2442             po_core_s2.get_currency_info (x_currency_code => l_base_currency,
2443                                           x_precision=>l_base_precision,
2444                                           x_min_unit=>l_base_min_unit );
2445 
2446 
2447         ELSE -- if l_base_currency <> l_po_currency
2448             l_base_precision := l_precision;
2449             l_base_min_unit  := l_min_unit;
2450         END IF;  -- if l_base_currency <> l_po_currency
2451         l_progress := '050';
2452           --SQL WHAT- This query returns the total amount for an archived SPO
2453           --SQL WHY- To derive the total amount for an archived PO revision
2454           --SQL JOIN- Location id in PO_LINE_LOCATIONS_ARCHIVE_ALL and PO_DISTRIBUTIONS_ARCHIVE_ALL
2455         SELECT nvl(sum(round( ( (round( ( ( decode(POD.quantity_ordered, NULL,
2456                                                        (nvl(POD.amount_ordered,0) -
2457                                                          nvl(POD.amount_cancelled,0)
2458                                                        ),
2459                                                        ( (nvl(POD.quantity_ordered,0) -
2460                                                            nvl(POD.quantity_cancelled,0)
2461                                                          )*
2462                                                             nvl(PLL.price_override, 0)
2463                                                         )
2464                                                       ) *
2465                                                decode(p_base_cur_result,'Y',nvl(POD.rate,1),1)
2466                                             )/
2467                                              nvl(l_min_unit,1)
2468                                           ),decode(l_min_unit,null,l_precision,0)
2469                                         )*
2470                                          nvl(l_min_unit,1)
2471                                   )/
2472                                    nvl(l_base_min_unit,1)
2473                                 )
2474                                  ,decode(l_base_min_unit,null,l_base_precision,0)
2475                               )*
2476                                nvl(l_base_min_unit,1)
2477                         ), 0
2478                       )
2479         INTO   l_archive_total_amt
2480         FROM   PO_DISTRIBUTIONS_ARCHIVE_ALL POD, PO_LINE_LOCATIONS_ARCHIVE_ALL PLL
2481         WHERE  PLL.po_header_id = p_object_id
2482         AND    PLL.shipment_type in ('STANDARD')
2483         AND    PLL.line_location_id = POD.line_location_id
2484         AND    PLL.REVISION_NUM = (SELECT MAX(pll1.revision_num) FROM PO_LINE_LOCATIONS_ARCHIVE_ALL PLL1
2485                                    WHERE pll1.line_location_id = pll.line_location_id AND
2486                                              pll1.revision_num <= p_doc_revision)
2487         AND    POD.REVISION_NUM = (SELECT MAX(pdd1.revision_num) FROM PO_DISTRIBUTIONS_ARCHIVE_ALL PDD1
2488                                    WHERE pdd1.po_distribution_id = pod.po_distribution_id AND
2489                                          pdd1.revision_num <= p_doc_revision);
2490 
2491         l_progress := '060';
2492 
2493 
2494     elsif (p_object_type = 'S' ) then /* PO Shipment */
2495         l_progress := '070';
2496         --< Bug 3549096 > Use _ALL tables instead of org-striped views.
2497         SELECT c.minimum_accountable_unit,
2498                c.precision
2499         INTO   l_min_unit,
2500                l_precision
2501         FROM   FND_CURRENCIES C,
2502                PO_HEADERS_ALL     PH,
2503                PO_LINE_LOCATIONS_ALL PLL
2504         WHERE  PLL.line_location_id   = p_object_id
2505         AND    PH.po_header_id        = PLL.po_header_id
2506         AND    C.currency_code        = PH.CURRENCY_CODE;
2507 
2508         if l_min_unit is null then
2509           l_progress := '80';
2510 
2511           --SQL WHAT- This query returns the amount for the appropriate revision of a line_location
2512           --SQL WHY-  To return the amount for line location
2513           --SQL JOIN- Use MAX to derive the appropriate revision of a line location
2514           -- <SERVICES FPJ>
2515           -- For the new Services lines, quantity will be null.
2516           -- Hence, added a decode statement to use amount directly
2517           -- in the total amount calculation when quantity is null.
2518           SELECT sum(round((
2519                  decode(pll.quantity,
2520                         null,
2521                         (pll.amount - nvl(pll.amount_cancelled,0)),
2522                         (pll.quantity - nvl(pll.quantity_cancelled,0))
2523                         * nvl(pll.price_override,0)
2524                        )
2525                  ),l_precision))
2526           INTO   l_archive_total_amt
2527           FROM   PO_LINE_LOCATIONS_ARCHIVE_ALL PLL
2528           WHERE  PLL.line_location_id   = p_object_id AND
2529           PLL.revision_num = (SELECT MAX(pll1.revision_num) FROM PO_LINE_LOCATIONS_ARCHIVE_ALL PLL1 WHERE
2530                               pll1.line_location_id = pll.line_location_id AND
2531                               pll1.revision_num <= p_doc_revision) ;
2532 
2533         else
2534           l_progress := '90';
2535           -- <SERVICES FPJ>
2536           -- For the new Services lines, quantity will be null.
2537           -- Hence, added a decode statement to use amount directly
2538           -- in the total amount calculation when quantity is null.
2539           select round(sum((
2540                  decode(pll.quantity,
2541                         null,
2542                         (pll.amount - nvl(pll.amount_cancelled,0)),
2543                         (pll.quantity - nvl(pll.quantity_cancelled,0))
2544                         * nvl(pll.price_override,0)
2545                    )
2546                  /l_min_unit) * l_min_unit))
2547           INTO   l_archive_total_amt
2548           FROM   PO_LINE_LOCATIONS_ARCHIVE_ALL PLL
2549           WHERE  PLL.line_location_id   = p_object_id AND
2550           PLL.revision_num = (SELECT MAX(pll1.revision_num) FROM PO_LINE_LOCATIONS_ARCHIVE_ALL PLL1 WHERE
2551                               pll1.line_location_id = pll.line_location_id AND
2552                               pll1.revision_num <= p_doc_revision) ;
2553 
2554         end if;
2555 
2556        elsif (p_object_type = 'L' ) then /* Po Line */
2557           l_progress := '100';
2558           --< Bug 3549096 > Use _ALL tables instead of org-striped views.
2559           SELECT sum(c.minimum_accountable_unit),
2560                  sum(c.precision)
2561           INTO   l_min_unit,
2562                  l_precision
2563           FROM   FND_CURRENCIES C,
2564                  PO_HEADERS_ALL     PH,
2565                  PO_LINES_ALL POL
2566           WHERE  POL.po_line_id   = p_object_id
2567           AND    PH.po_header_id  = POL.po_header_id
2568           AND    C.currency_code  = PH.CURRENCY_CODE;
2569 
2570           if l_min_unit is null then
2571             l_progress := '105';
2572 
2573       -- <SERVICES FPJ>
2574       -- For the new Services lines, quantity will be null.
2575       -- Hence, added a decode statement to use amount directly
2576       -- in the total amount calculation when quantity is null.
2577             select sum(round((
2578                    decode(pll.quantity,
2579                           null,
2580                           (pll.amount - nvl(pll.amount_cancelled, 0)),
2581                           (pll.quantity - nvl(pll.quantity_cancelled,0))
2582                           * nvl(pll.price_override,0)
2583                          )
2584                       ),l_precision))
2585             INTO   l_archive_total_amt
2586             FROM   PO_LINE_LOCATIONS_ARCHIVE_ALL PLL
2587             WHERE  PLL.po_line_id   = p_object_id
2588             AND    PLL.shipment_type  in ( 'STANDARD','BLANKET','PLANNED')
2589             AND    PLL.revision_num = (SELECT MAX(pll1.revision_num) FROM PO_LINE_LOCATIONS_ARCHIVE_ALL PLL1 WHERE
2590                                         pll1.line_location_id = pll.line_location_id AND
2591                                         pll1.revision_num <= p_doc_revision) ;
2592 
2593           else
2594             l_progress := '110';
2595 
2596       -- <SERVICES FPJ>
2597       -- For the new Services lines, quantity will be null.
2598       -- Hence, added a decode statement to use amount directly
2599       -- in the total amount calculation when quantity is null.
2600             select sum(round((
2601                    decode(pll.quantity,
2602                           null,
2603                           (pll.amount - nvl(pll.amount_cancelled,0)),
2604                           (pll.quantity - nvl(pll.quantity_cancelled,0))
2605                           * nvl(pll.price_override,0)
2606                          )
2607                    / l_min_unit) * l_min_unit))
2608             INTO   l_archive_total_amt
2609             FROM   PO_LINE_LOCATIONS_ARCHIVE_ALL PLL
2610             WHERE  PLL.po_line_id   = p_object_id
2611             AND    PLL.shipment_type  in ( 'STANDARD','BLANKET','PLANNED')
2612             AND    PLL.revision_num = (SELECT MAX(pll1.revision_num) FROM PO_LINE_LOCATIONS_ARCHIVE_ALL PLL1 WHERE
2613                                        pll1.line_location_id = pll.line_location_id AND
2614                                        pll1.revision_num <= p_doc_revision) ;
2615 
2616 
2617           end if;
2618       end if; /* p_object_type */
2619 
2620   elsif (p_doc_type = 'RELEASE') AND (p_doc_subtype = 'BLANKET') then
2621 
2622 
2623      if (p_object_type = 'R' ) then /* Release */
2624 
2625        if p_base_cur_result = 'Y' then
2626          l_progress := '180';
2627          --< Bug 3549096 > Use _ALL tables instead of org-striped views.
2628          SELECT GSB.currency_code,
2629                 POH.currency_code
2630          INTO   l_base_currency,
2631                 l_po_currency
2632          FROM   PO_HEADERS_ALL POH,
2633                 FINANCIALS_SYSTEM_PARAMS_ALL FSP,
2634                 GL_SETS_OF_BOOKS GSB,
2635                 PO_RELEASES_ALL POR
2636          WHERE  POR.po_release_id   = p_object_id
2637          AND    POH.po_header_id    = POR.po_header_id
2638          AND    NVL(POR.org_id,-99) = NVL(FSP.org_id,-99)   --< Bug 3549096 >
2639          AND    FSP.set_of_books_id = GSB.set_of_books_id;
2640 
2641          if (l_base_currency <> l_po_currency) then
2642            /* Get precision and minimum accountable unit of the PO CURRENCY */
2643            l_progress := '190';
2644            po_core_s2.get_currency_info (l_po_currency,
2645                               l_precision,
2646                               l_min_unit );
2647 
2648            /* Get precision and minimum accountable unit of the base CURRENCY */
2649            l_progress := '200';
2650            po_core_s2.get_currency_info (l_base_currency,
2651                               l_base_precision,
2652                               l_base_min_unit );
2653 
2654            if l_base_min_unit is null then
2655              if l_min_unit is null then
2656                l_progress := '210';
2657 
2658                -- <SERVICES FPJ>
2659                -- For the new Services lines, quantity will be null.
2660                -- Hence, added a decode statement to use amount directly
2661                -- in the total amount calculation when quantity is null.
2662                select nvl(sum(round(round(
2663                       decode(POD.quantity_ordered,
2664                              null,
2665                              nvl(POD.amount_ordered, 0),
2666                              (nvl(POD.quantity_ordered,0) *
2667                              nvl(PLL.price_override,0))
2668                             )
2669                       * POD.rate
2670                       ,l_precision),l_base_precision)),0)
2671                INTO l_archive_total_amt
2672                FROM PO_DISTRIBUTIONS_ARCHIVE_ALL POD, PO_LINE_LOCATIONS_ARCHIVE_ALL PLL
2673                WHERE PLL.po_release_id    = p_object_id
2674                AND   PLL.line_location_id = POD.line_location_id
2675                AND   PLL.shipment_type in ('SCHEDULED','BLANKET')
2676                AND   PLL.REVISION_NUM = (SELECT MAX(pll1.revision_num) FROM PO_LINE_LOCATIONS_ARCHIVE_ALL PLL1
2677                                          WHERE pll1.line_location_id = pll.line_location_id AND
2678                                                pll1.revision_num <= p_doc_revision)
2679                AND   POD.REVISION_NUM = (SELECT MAX(pdd1.revision_num) FROM PO_DISTRIBUTIONS_ARCHIVE_ALL PDD1
2680                                          WHERE pdd1.po_distribution_id = pod.po_distribution_id AND
2681                                                pdd1.revision_num <= p_doc_revision);
2682 
2683 
2684              else
2685                l_progress := '212';
2686                -- <SERVICES FPJ>
2687                -- For the new Services lines, quantity will be null.
2688                -- Hence, added a decode statement to use amount directly
2689                -- in the total amount calculation when quantity is null.
2690 	       SELECT nvl(sum(round( round(
2691                       decode(POD.quantity_ordered,
2692                              null,
2693                              nvl(POD.amount_ordered, 0),
2694                              (nvl(POD.quantity_ordered, 0) *
2695                              nvl(PLL.price_override, 0))
2696                             )
2697                       * POD.rate /
2698                       l_min_unit) * l_min_unit , l_base_precision)),0)
2699                INTO l_archive_total_amt
2700                FROM PO_DISTRIBUTIONS_ARCHIVE_ALL POD, PO_LINE_LOCATIONS_ARCHIVE_ALL PLL
2701                WHERE PLL.po_release_id    = p_object_id
2702                AND   PLL.line_location_id = POD.line_location_id
2703                AND   PLL.shipment_type in ('SCHEDULED','BLANKET')
2704                AND   PLL.REVISION_NUM = (SELECT MAX(pll1.revision_num) FROM PO_LINE_LOCATIONS_ARCHIVE_ALL PLL1
2705                                          WHERE pll1.line_location_id = pll.line_location_id AND
2706                                                pll1.revision_num <= p_doc_revision)
2707                AND   POD.REVISION_NUM = (SELECT MAX(pdd1.revision_num) FROM PO_DISTRIBUTIONS_ARCHIVE_ALL PDD1
2708                                          WHERE pdd1.po_distribution_id = pod.po_distribution_id AND
2709                                                pdd1.revision_num <= p_doc_revision);
2710 
2711              end if;
2712            else
2713              if l_min_unit is null then
2714                l_progress := '214';
2715                -- <SERVICES FPJ>
2716                -- For the new Services lines, quantity will be null.
2717                -- Hence, added a decode statement to use amount directly
2718                -- in the total amount calculation when quantity is null.
2719                SELECT nvl(sum(round( round(
2720                       decode(POD.quantity_ordered,
2721                              null,
2722                              nvl(POD.amount_ordered, 0),
2723                              (nvl(POD.quantity_ordered, 0) *
2724                              nvl(PLL.price_override, 0))
2725                             )
2726                       * POD.rate
2727                       , l_precision)
2728                       / l_base_min_unit ) * l_base_min_unit) ,0)
2729                INTO   l_archive_total_amt
2730                FROM PO_DISTRIBUTIONS_ARCHIVE_ALL POD, PO_LINE_LOCATIONS_ARCHIVE_ALL PLL
2731                WHERE PLL.po_release_id    = p_object_id
2732                AND   PLL.line_location_id = POD.line_location_id
2733                AND   PLL.shipment_type in ('SCHEDULED','BLANKET')
2734                AND   PLL.REVISION_NUM = (SELECT MAX(pll1.revision_num) FROM PO_LINE_LOCATIONS_ARCHIVE_ALL PLL1
2735                                          WHERE pll1.line_location_id = pll.line_location_id AND
2736                                                pll1.revision_num <= p_doc_revision)
2737                AND   POD.REVISION_NUM = (SELECT MAX(pdd1.revision_num) FROM PO_DISTRIBUTIONS_ARCHIVE_ALL PDD1
2738                                          WHERE pdd1.po_distribution_id = pod.po_distribution_id AND
2739                                                pdd1.revision_num <= p_doc_revision);
2740 
2741              else
2742                l_progress := '216';
2743             -- <SERVICES FPJ>
2744                -- For the new Services lines, quantity will be null.
2745                -- Hence, added a decode statement to use amount directly
2746                -- in the total amount calculation when quantity is null.
2747 	       SELECT nvl(sum(round( round(
2748                       decode(POD.quantity_ordered,
2749                              null,
2750                              nvl(POD.amount_ordered, 0),
2751                              (nvl(POD.quantity_ordered, 0) *
2752                              nvl(PLL.price_override, 0))
2753                             )
2754                       * POD.rate /
2755                       l_min_unit) * l_min_unit  / l_base_min_unit)
2756                       * l_base_min_unit) , 0)
2757                INTO   l_archive_total_amt
2758                FROM PO_DISTRIBUTIONS_ARCHIVE_ALL POD, PO_LINE_LOCATIONS_ARCHIVE_ALL PLL
2759                WHERE PLL.po_release_id    = p_object_id
2760                AND   PLL.line_location_id = POD.line_location_id
2761                AND   PLL.shipment_type in ('SCHEDULED','BLANKET')
2762                AND   PLL.REVISION_NUM = (SELECT MAX(pll1.revision_num) FROM PO_LINE_LOCATIONS_ARCHIVE_ALL PLL1
2763                                          WHERE pll1.line_location_id = pll.line_location_id AND
2764                                                pll1.revision_num <= p_doc_revision)
2765                AND   POD.REVISION_NUM = (SELECT MAX(pdd1.revision_num) FROM PO_DISTRIBUTIONS_ARCHIVE_ALL PDD1
2766                                          WHERE pdd1.po_distribution_id = pod.po_distribution_id AND
2767                                                pdd1.revision_num <= p_doc_revision);
2768 
2769              end if;
2770            end if;
2771 
2772          end if;
2773        else
2774          l_progress := '220';
2775          --< Bug 3549096 > Use _ALL tables instead of org-striped views.
2776          SELECT c.minimum_accountable_unit,
2777                 c.precision
2778          INTO   l_min_unit,
2779                 l_precision
2780          FROM   FND_CURRENCIES C,
2781                 PO_RELEASES_ALL    POR,
2782                 PO_HEADERS_ALL     PH
2783          WHERE  POR.po_release_id = p_object_id
2784          AND    PH.po_header_id   = POR.PO_HEADER_ID
2785          AND    C.currency_code   = PH.CURRENCY_CODE;
2786 
2787          if l_min_unit is null then
2788 
2789    /* 958792 kbenjami 8/25/99.  Proprogated fix from R11.
2790       849493 - SVAIDYAN: Do a sum(round()) instead of round(sum()) since what
2791                          we pass to GL is the round of individual dist. amounts
2792                          and the sum of these rounded values is what should be
2793                          displayed as the header total.
2794    */
2795            l_progress := '222';
2796            -- <SERVICES FPJ>
2797            -- For the new Services lines, quantity will be null.
2798            -- Hence, added a decode statement to use amount directly
2799            -- in the total amount calculation when quantity is null.
2800            select sum(round(
2801                   decode(pll.quantity,
2802                          null,
2803                          (pll.amount - nvl(pll.amount_cancelled,0)),
2804                          ((pll.quantity - nvl(pll.quantity_cancelled,0)) *
2805                          nvl(pll.price_override,0))
2806                         )
2807                   ,l_precision))
2808            INTO   l_archive_total_amt
2809            FROM   PO_LINE_LOCATIONS_ARCHIVE_ALL PLL
2810            WHERE  PLL.po_release_id   = p_object_id
2811            AND    PLL.shipment_type  in ( 'SCHEDULED','BLANKET')
2812            AND   PLL.REVISION_NUM = (SELECT MAX(pll1.revision_num) FROM PO_LINE_LOCATIONS_ARCHIVE_ALL PLL1
2813                                      WHERE pll1.line_location_id = pll.line_location_id AND
2814                                            pll1.revision_num <= p_doc_revision);
2815 
2816          else
2817 		   /* Bug 1111926: GMudgal 2/18/2000
2818 		   ** Incorrect placement of brackets caused incorrect rounding
2819 		   ** and consequently incorrect PO header totals
2820 		   */
2821            l_progress := '224';
2822 
2823            -- <SERVICES FPJ>
2824            -- For the new Services lines, quantity will be null.
2825            -- Hence, added a decode statement to use amount directly
2826            -- in the total amount calculation when quantity is null.
2827            select sum(round(
2828                   decode(pll.quantity,
2829                          null,
2830                         (pll.amount - nvl(pll.amount_cancelled,0)),
2831                         ((pll.quantity - nvl(pll.quantity_cancelled,0)) *
2832                         nvl(pll.price_override,0))
2833                         )
2834                   /l_min_unit) *
2835                   l_min_unit)
2836            INTO   l_archive_total_amt
2837            FROM   PO_LINE_LOCATIONS_ARCHIVE_ALL PLL
2838            WHERE  PLL.po_release_id   = p_object_id
2839            AND    PLL.shipment_type  in ( 'SCHEDULED','BLANKET')
2840            AND   PLL.REVISION_NUM = (SELECT MAX(pll1.revision_num) FROM PO_LINE_LOCATIONS_ARCHIVE_ALL PLL1
2841                                      WHERE pll1.line_location_id = pll.line_location_id AND
2842                                            pll1.revision_num <= p_doc_revision);
2843 
2844          end if;
2845 
2846        end if;
2847 
2848 
2849      end if;
2850   END IF;-- (p_doc_type = 'PO') AND (p_doc_subtype = 'STANDARD')
2851 
2852 
2853   l_progress := '250';
2854   --If l_archive_total_amt has a null value, return 0 as the total.
2855   IF l_archive_total_amt IS NULL THEN
2856 	l_archive_total_amt := 0;
2857   END IF;
2858   l_progress := '260';
2859   RETURN(l_archive_total_amt);
2860 
2861 EXCEPTION
2862    WHEN OTHERS THEN
2863             IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_unexp_error)
2864             THEN
2865                   fnd_msg_pub.add_exc_msg (g_pkg_name, 'GET_ARCHIVE_TOTAL_FOR_ANY_REV',
2866                   SUBSTRB (SQLERRM , 1 , 200) || ' at location ' || l_progress);
2867             END IF;
2868             RETURN(0);
2869 END get_archive_total_for_any_rev;
2870 
2871 --<POC FPJ END>
2872 /*===================================================================================
2873 
2874     FUNCTION:    GET_RELEASE_LINE_TOTAL                Bug#3771735
2875 
2876     DESCRIPTION: Function is being added to get the correct cumulative Amount of all
2877 		 shipments of  a  Release which correspond to the same line of a BPA.
2878 		 The function will be used during the PDF generation of a Incomplete
2879 		 Blanket Release.
2880 
2881 ===================================================================================*/
2882 FUNCTION  GET_RELEASE_LINE_TOTAL
2883 (
2884 	p_line_id      IN PO_LINES_ALL.PO_LINE_ID%TYPE ,
2885 	p_release_id   IN PO_RELEASES_ALL.PO_RELEASE_ID%TYPE
2886 )
2887 RETURN NUMBER
2888 IS
2889   x_min_unit       NUMBER;
2890   x_precision      INTEGER;
2891   x_result_fld     NUMBER;
2892 
2893 BEGIN
2894 
2895     SELECT sum(c.minimum_accountable_unit),
2896            sum(c.precision)
2897     INTO   x_min_unit,
2898            x_precision
2899     FROM   FND_CURRENCIES C,
2900            PO_HEADERS_ALL     PH,
2901            PO_LINES_ALL POL
2902     WHERE  POL.po_line_id   = p_line_id
2903     AND    PH.po_header_id  = POL.po_header_id
2904     AND    C.currency_code  = PH.CURRENCY_CODE;
2905 
2906     if x_min_unit is null then
2907       -- <SERVICES FPJ>
2908       -- For the new Services lines, quantity will be null.
2909       -- Hence, added a decode statement to use amount directly
2910       -- in the total amount calculation when quantity is null.
2911       --Use _ALL tables instead of org-striped views.
2912       select sum(round((
2913              decode(pll.quantity,
2914                     null,
2915                     (pll.amount - nvl(pll.amount_cancelled, 0)),
2916                     (pll.quantity - nvl(pll.quantity_cancelled,0))
2917                     * nvl(pll.price_override,0)
2918                    )
2919              ),x_precision))
2920       INTO   x_result_fld
2921       FROM   PO_LINE_LOCATIONS_ALL PLL
2922       WHERE  PLL.po_line_id   = p_line_id
2923       AND    PLL.po_release_id  = p_release_id
2924       AND    PLL.shipment_type  in ( 'STANDARD','BLANKET','PLANNED');
2925 
2926     else
2927       -- <SERVICES FPJ>
2928       -- For the new Services lines, quantity will be null.
2929       -- Hence, added a decode statement to use amount directly
2930       -- in the total amount calculation when quantity is null.
2931       -- Use _ALL tables instead of org-striped views.
2932       select sum(round((
2933              decode(pll.quantity,
2934                     null,
2935                     (pll.amount - nvl(pll.amount_cancelled,0)),
2936                     (pll.quantity - nvl(pll.quantity_cancelled,0))
2937                     * nvl(pll.price_override,0)
2938                    )
2939              / x_min_unit) * x_min_unit))
2940       INTO   x_result_fld
2941       FROM   PO_LINE_LOCATIONS_ALL PLL
2942       WHERE  PLL.po_line_id   = p_line_id
2943       AND    PLL.po_release_id   = p_release_id
2944       AND    PLL.shipment_type  in ( 'STANDARD','BLANKET','PLANNED');
2945 
2946     end if;
2947     return(x_result_fld);
2948 END GET_RELEASE_LINE_TOTAL;
2949 
2950 /*===================================================================================
2951 
2952     FUNCTION:    get_ga_amount_released                  <GA FPI>
2953 
2954     DESCRIPTION: Gets the total Amount Released for a particular Global Agreement.
2955                  That is, sum up the total for all uncancelled Standard PO lines
2956                  which reference that Global Agreement.
2957 
2958 ===================================================================================*/
2959 FUNCTION get_ga_amount_released
2960 (
2961     p_po_header_id             PO_HEADERS_ALL.po_header_id%TYPE,
2962     p_convert_to_base          BOOLEAN := FALSE
2963 )
2964 RETURN NUMBER
2965 IS
2966     l_base_currency            FND_CURRENCIES.currency_code%TYPE;
2967     l_base_precision           FND_CURRENCIES.precision%TYPE;
2968     l_base_ext_precision       FND_CURRENCIES.extended_precision%TYPE;
2969     l_base_min_unit            FND_CURRENCIES.minimum_accountable_unit%TYPE;
2970 
2971     l_po_currency              FND_CURRENCIES.currency_code%TYPE;
2972     l_po_precision             FND_CURRENCIES.precision%TYPE;
2973     l_po_ext_precision         FND_CURRENCIES.extended_precision%TYPE;
2974     l_po_min_unit              FND_CURRENCIES.minimum_accountable_unit%TYPE;
2975 
2976     l_rate                     PO_HEADERS_ALL.rate%TYPE;
2977 
2978     x_total                    NUMBER;
2979 
2980 BEGIN
2981 
2982     -- Get the functional currency code for the current org's Set of Books
2983     -- and the currency code defined for the PO
2984     PO_CORE_S2.get_po_currency( p_po_header_id,               -- IN
2985                                 l_base_currency,              --OUT
2986                                 l_po_currency );              --OUT
2987 
2988     -- Get the Precision (never NULL), Extended Precision, Minimum Accountable Unit
2989     -- for the Set of Books' currency code...
2990     FND_CURRENCY.get_info( l_base_currency,                   -- IN
2991                            l_base_precision,                  --OUT
2992                            l_base_ext_precision,              --OUT
2993                            l_base_min_unit );                 --OUT
2994 
2995     -- and for the PO's currency code...
2996     FND_CURRENCY.get_info( l_po_currency,                     -- IN
2997                            l_po_precision,                    --OUT
2998                            l_po_ext_precision,                --OUT
2999                            l_po_min_unit );                   --OUT
3000 
3001     -- NOTE: All Standard PO's must have the same currency code as the
3002     -- Global Agreement that they reference. The currency conversion rate used
3003     -- will be the rate between the Global Agreement's currency and the owning
3004     -- org's currency.
3005 
3006     IF  (   ( p_convert_to_base )               -- convert back to base currency
3007         AND ( l_base_currency <> l_po_currency ) ) THEN
3008 
3009         -- Get the rate from PO_HEADERS_ALL for the Global Agreement.
3010         SELECT     rate
3011         INTO       l_rate
3012         FROM       po_headers_all
3013         WHERE      po_header_id = p_po_header_id;
3014 
3015         IF ( l_base_min_unit IS NOT NULL ) THEN
3016 
3017             IF ( l_po_min_unit IS NOT NULL ) THEN
3018 
3019 	      -- <SERVICES FPJ>
3020               -- For the new Services lines, quantity will be null.
3021               -- Hence, added a decode statement to use amount directly
3022               -- in the total amount calculation when quantity is null.
3023 	      SELECT    sum ( round (  ( round (  (  ( decode(pol.quantity, null,
3024 		                                       (pod.amount_ordered -
3025 		                                       pod.amount_cancelled),
3026 		                                       (( pod.quantity_ordered
3027                                                        - pod.quantity_cancelled )
3028 		                                       * poll.price_override)
3029 							     )
3030 					             )
3031                                                     * l_rate )
3032                                                  / l_po_min_unit )
3033                                          * l_po_min_unit )
3034                                       / l_base_min_unit )
3035                               * l_base_min_unit )
3036                 INTO      x_total
3037                 FROM      po_distributions_all    pod,
3038                           po_line_locations_all   poll,
3039                           po_lines_all            pol
3040                 WHERE     pod.line_location_id = poll.line_location_id
3041                 AND       poll.po_line_id = pol.po_line_id
3042                 AND       pol.from_header_id = p_po_header_id;
3043 
3044             ELSE          -- ( l_po_min_unit IS NULL )
3045 
3046 	       -- <SERVICES FPJ>
3047                -- For the new Services lines, quantity will be null.
3048                -- Hence, added a decode statement to use amount directly
3049                -- in the total amount calculation when quantity is null.
3050 	       SELECT    sum ( round (  (  (decode(pol.quantity, null,
3051 		                                   (pod.amount_ordered -
3052 		                                   pod.amount_cancelled),
3053 		                                   (( pod.quantity_ordered
3054                                                    - pod.quantity_cancelled )
3055 		                                   * poll.price_override)
3056 						  )
3057 					   )
3058                                          * l_rate )
3059                                       / l_base_min_unit )
3060                               * l_base_min_unit )
3061                 INTO      x_total
3062                 FROM      po_distributions_all    pod,
3063                           po_line_locations_all   poll,
3064                           po_lines_all            pol
3065                 WHERE     pod.line_location_id = poll.line_location_id
3066                 AND       poll.po_line_id = pol.po_line_id
3067                 AND       pol.from_header_id = p_po_header_id;
3068 
3069             END IF;
3070 
3071         ELSE              -- ( l_base_min_unit IS NULL )
3072 
3073             IF ( l_po_min_unit IS NOT NULL ) THEN
3074 
3075                -- <SERVICES FPJ>
3076                -- For the new Services lines, quantity will be null.
3077                -- Hence, added a decode statement to use amount directly
3078                -- in the total amount calculation when quantity is null.
3079 	       SELECT    sum ( round (  (  (decode(pol.quantity, null,
3080 		                                   (pod.amount_ordered -
3081 		                                   pod.amount_cancelled),
3082 		                                   (( pod.quantity_ordered
3083                                                    - pod.quantity_cancelled )
3084 		                                   * poll.price_override)
3085 					          )
3086 					   )
3087                                          * l_rate )
3088                                       / l_po_min_unit )
3089                               * l_po_min_unit )
3090                 INTO      x_total
3091                 FROM      po_distributions_all    pod,
3092                           po_line_locations_all   poll,
3093                           po_lines_all            pol
3094                 WHERE     pod.line_location_id = poll.line_location_id
3095                 AND       poll.po_line_id = pol.po_line_id
3096                 AND       pol.from_header_id = p_po_header_id;
3097 
3098             ELSE          -- ( l_po_min_unit IS NULL )
3099 
3100                -- <SERVICES FPJ>
3101                -- For the new Services lines, quantity will be null.
3102                -- Hence, added a decode statement to use amount directly
3103                -- in the total amount calculation when quantity is null.
3104 	       SELECT    sum (  (decode(pol.quantity, null,
3105 		                        (pod.amount_ordered -
3106 		                        pod.amount_cancelled),
3107 	                                (( pod.quantity_ordered
3108                                         - pod.quantity_cancelled )
3109 		                        * poll.price_override)
3110                                        )
3111 				)
3112                               * l_rate )
3113                 INTO      x_total
3114                 FROM      po_distributions_all    pod,
3115                           po_line_locations_all   poll,
3116                           po_lines_all            pol
3117                 WHERE     pod.line_location_id = poll.line_location_id
3118                 AND       poll.po_line_id = pol.po_line_id
3119                 AND       pol.from_header_id = p_po_header_id;
3120 
3121             END IF;
3122 
3123         END IF;
3124 
3125     ELSE                  -- just get po_currency (no conversion necessary)
3126 
3127         IF ( l_po_min_unit IS NOT NULL ) THEN
3128 
3129 	   -- <SERVICES FPJ>
3130            -- For the new Services lines, quantity will be null.
3131            -- Hence, added a decode statement to use amount directly
3132            -- in the total amount calculation when quantity is null.
3133 	   SELECT    sum ( round (  (decode(pol.quantity, null,
3134                                             (pod.amount_ordered -
3135                                             pod.amount_cancelled),
3136                                             (( pod.quantity_ordered
3137                                             - pod.quantity_cancelled )
3138                                             * poll.price_override)
3139                                            )
3140 				     )
3141                                   / l_po_min_unit )
3142                           * l_po_min_unit )
3143             INTO      x_total
3144             FROM      po_distributions_all    pod,
3145                       po_line_locations_all   poll,
3146                       po_lines_all            pol
3147             WHERE     pod.line_location_id = poll.line_location_id
3148             AND       poll.po_line_id = pol.po_line_id
3149             AND       pol.from_header_id = p_po_header_id;
3150 
3151         ELSE              -- ( l_po_min_unit IS NULL )
3152 
3153            -- <SERVICES FPJ>
3154            -- For the new Services lines, quantity will be null.
3155            -- Hence, added a decode statement to use amount directly
3156            -- in the total amount calculation when quantity is null.
3157 	   SELECT    sum (decode(pol.quantity, null,
3158                                  (pod.amount_ordered -
3159                                  pod.amount_cancelled),
3160 		                 (( pod.quantity_ordered
3161                                  - pod.quantity_cancelled )
3162 		                 * poll.price_override)
3163 			        )
3164 			 )
3165             INTO      x_total
3166             FROM      po_distributions_all    pod,
3167                       po_line_locations_all   poll,
3168                       po_lines_all            pol
3169             WHERE     pod.line_location_id = poll.line_location_id
3170             AND       poll.po_line_id = pol.po_line_id
3171             AND       pol.from_header_id = p_po_header_id;
3172 
3173         END IF;
3174 
3175     END IF;
3176 
3177     return(x_total);
3178 
3179 EXCEPTION
3180     WHEN OTHERS THEN
3181         return(0);
3182 
3183 END get_ga_amount_released;
3184 
3185 
3186 /*===================================================================================
3187 
3188     FUNCTION:    get_ga_line_amount_released                <GA FPI>
3189 
3190     DESCRIPTION: Gets the total Amount Released for a Global Agreement line.
3191                  That is, sum up the total for all uncancelled Standard PO lines
3192                  which reference that Global Agreement line.
3193 
3194 ===================================================================================*/
3195 PROCEDURE get_ga_line_amount_released
3196 (
3197     p_po_line_id             IN       PO_LINES_ALL.po_line_id%TYPE,
3198     p_po_header_id           IN       PO_HEADERS_ALL.po_header_id%TYPE,
3199     x_quantity_released      OUT NOCOPY      NUMBER,
3200     x_amount_released        OUT NOCOPY      NUMBER
3201 )
3202 IS
3203     l_base_currency            FND_CURRENCIES.currency_code%TYPE;
3204 
3205     l_po_currency              FND_CURRENCIES.currency_code%TYPE;
3206     l_po_precision             FND_CURRENCIES.precision%TYPE;
3207     l_po_ext_precision         FND_CURRENCIES.extended_precision%TYPE;
3208     l_po_min_unit              FND_CURRENCIES.minimum_accountable_unit%TYPE;
3209 
3210     l_rate                     PO_HEADERS_ALL.rate%TYPE;
3211 
3212 BEGIN
3213 
3214     -- Get the currency code defined for the PO
3215     PO_CORE_S2.get_po_currency( p_po_header_id,               -- IN
3216                                 l_base_currency,              --OUT
3217                                 l_po_currency );              --OUT
3218 
3219     -- Get the Precision (never NULL), Extended Precision, Minimum Accountable Unit
3220     -- for the PO's currency code...
3221     FND_CURRENCY.get_info( l_po_currency,                     -- IN
3222                            l_po_precision,                    --OUT
3223                            l_po_ext_precision,                --OUT
3224                            l_po_min_unit );                   --OUT
3225 
3226     IF ( l_po_min_unit IS NOT NULL ) THEN
3227 
3228        -- <SERVICES FPJ>
3229        -- For the new Services lines, quantity will be null.
3230        -- Hence, added a decode statement to use amount directly
3231        -- in the total amount calculation when quantity is null.
3232        SELECT    sum ( round (  (decode(pol.quantity, null,
3233 		                        (pod.amount_ordered -
3234 		                        pod.amount_cancelled),
3235 		                        (( pod.quantity_ordered
3236                                         - pod.quantity_cancelled )
3237 		                        * poll.price_override))
3238                                        )
3239                               / l_po_min_unit )
3240                       * l_po_min_unit ),
3241                   sum ( pod.quantity_ordered - pod.quantity_cancelled )
3242         INTO      x_amount_released,
3243                   x_quantity_released
3244         FROM      po_distributions_all    pod,
3245                   po_line_locations_all   poll,
3246                   po_lines_all            pol
3247         WHERE     pod.line_location_id = poll.line_location_id
3248         AND       poll.po_line_id = pol.po_line_id
3249         AND       pol.from_line_id = p_po_line_id;
3250 
3251     ELSE              -- ( l_po_min_unit IS NULL )
3252 
3253        -- <SERVICES FPJ>
3254        -- For the new Services lines, quantity will be null.
3255        -- Hence, added a decode statement to use amount directly
3256        -- in the total amount calculation when quantity is null.
3257        SELECT    sum (decode(pol.quantity, null,
3258 		             (pod.amount_ordered -
3259                              pod.amount_cancelled),
3260 		             (( pod.quantity_ordered
3261                              - pod.quantity_cancelled )
3262 		             * poll.price_override)
3263 			    )
3264 		     ),
3265                   sum ( pod.quantity_ordered - pod.quantity_cancelled )
3266         INTO      x_amount_released,
3267                   x_quantity_released
3268         FROM      po_distributions_all    pod,
3269                   po_line_locations_all   poll,
3270                   po_lines_all            pol
3271         WHERE     pod.line_location_id = poll.line_location_id
3272         AND       poll.po_line_id = pol.po_line_id
3273         AND       pol.from_line_id = p_po_line_id;
3274 
3275     END IF;
3276 
3277 END get_ga_line_amount_released;
3278 
3279 
3280 -- <GC FPJ START>
3281 /**=========================================================================
3282 * Function: get_gc_amount_released
3283 * Effects:  Calculate amount released given a global contract
3284 * Requires: None
3285 * Modifies: None
3286 * Return:   amount released. Whether the amount is converted to base currency
3287 *           is determined by p_convert_to_base
3288 =========================================================================**/
3289 
3290 FUNCTION get_gc_amount_released
3291 (
3292     p_po_header_id         IN NUMBER,
3293     p_convert_to_base      IN BOOLEAN := FALSE
3294 ) RETURN NUMBER
3295 IS
3296 
3297 l_base_currency   FND_CURRENCIES.currency_code%TYPE;
3298 l_base_precision  FND_CURRENCIES.precision%TYPE;
3299 l_base_min_unit   FND_CURRENCIES.minimum_accountable_unit%TYPE;
3300 
3301 l_po_currency     FND_CURRENCIES.currency_code%TYPE;
3302 l_po_precision    FND_CURRENCIES.precision%TYPE;
3303 l_po_min_unit     FND_CURRENCIES.minimum_accountable_unit%TYPE;
3304 
3305 l_rate            PO_HEADERS_ALL.rate%TYPE;
3306 l_total           NUMBER;
3307 
3308 l_log_head     CONSTANT VARCHAR2(100) := g_log_head||'GET_GC_AMOUNT_RELEASED';
3309 l_progress     VARCHAR2(3) := '000';
3310 
3311 BEGIN
3312     IF g_debug_stmt THEN
3313          PO_DEBUG.debug_begin(l_log_head);
3314     END IF;
3315 
3316     l_progress := '010';
3317 
3318     PO_CORE_S2.get_po_currency ( x_object_id     => p_po_header_id,
3319                                  x_base_currency => l_base_currency,
3320                                  x_po_currency   => l_po_currency     );
3321 
3322     PO_CORE_S2.get_currency_info ( x_currency_code => l_base_currency,
3323                                    x_precision     => l_base_precision,
3324                                    x_min_unit      => l_base_min_unit  );
3325 
3326     PO_CORE_S2.get_currency_info ( x_currency_code => l_po_currency,
3327                                    x_precision     => l_po_precision,
3328                                    x_min_unit      => l_po_min_unit  );
3329 
3330     --Bug# 5391346 START:
3331     --Added the following to queries below as was coded in the local contract case (refer bug# 716188)
3332     --1. Where clause POLL.shipment_type in ('STANDARD','PLANNED')
3333     --2. Outer Join POD.line_location_id(+) = POLL.line_location_id instead of simple join
3334     --3. Proper rate conversions taking into account different currencies of GCPA and SPOs sourced to it.
3335 
3336 
3337 
3338     IF ( p_convert_to_base AND
3339          l_base_currency <> l_po_currency ) THEN
3340 
3341       --Bug# 5391346: Removed the query to get l_rate from GCPA header
3342       --as we now use rate on the PO header to convert it to base currency.
3343 
3344       l_progress := '020';
3345 
3346         IF (l_base_min_unit IS NOT NULL) THEN
3347 
3348           l_progress := '030';
3349 
3350             IF (l_po_min_unit IS NOT NULL) THEN
3351 
3352                 -- SQL What: calculate amount released from all lines across OU
3353                 --           referencing this GC, with the amount being
3354                 --           converted to base currency
3355                 -- SQL Why:  This is the return value
3356                 --<Complex Work R12>: added decode on value basis
3357 
3358                 --Bug# 5391346: Added Decode for currency match condition
3359                 --Also added a join with PO_HEADERS_ALL for rate in header of execution document
3360 
3361                l_progress := '040';
3362 
3363                SELECT  NVL(
3364                        SUM(
3365                         DECODE(l_base_currency, PH1.currency_code,
3366                                 ROUND(
3367                                   ROUND(
3368                                         DECODE(POLL.value_basis
3369                                         ,'FIXED PRICE',
3370                                          NVL(POD.amount_ordered, 0) -
3371                                          NVL(POD.amount_cancelled, 0)
3372                                         , 'RATE',
3373                                          NVL(POD.amount_ordered, 0)-
3374                                          NVL(POD.amount_cancelled, 0)
3375                                         , --Qty based
3376                                         (NVL(POD.quantity_ordered, 0) -
3377                                          NVL(POD.quantity_cancelled,0)
3378                                          ) * NVL(POLL.price_override, 0)
3379                                               )
3380                                         /l_po_min_unit)*l_po_min_unit
3381                                        /l_base_min_unit)*l_base_min_unit
3382                               ,
3383 
3384 
3385                                 ROUND(
3386                                   ROUND(
3387                                          DECODE(POLL.value_basis
3388                                           ,'FIXED PRICE',
3389                                            NVL(POD.amount_ordered, 0) -
3390                                            NVL(POD.amount_cancelled, 0)
3391                                           , 'RATE',
3392                                            NVL(POD.amount_ordered, 0)-
3393                                            NVL(POD.amount_cancelled, 0)
3394                                           , --Qty based
3395                                           (NVL(POD.quantity_ordered, 0) -
3396                                            NVL(POD.quantity_cancelled,0)
3397                                            ) * NVL(POLL.price_override, 0)
3398                                                )
3399                                       /l_po_min_unit)*l_po_min_unit
3400                                       * NVL(POD.rate, NVL(PH1.rate,1))
3401                                     /l_base_min_unit) * l_base_min_unit
3402                                 )
3403                                )
3404                             ,0)
3405               INTO   l_total
3406               FROM   PO_DISTRIBUTIONS_ALL POD,
3407                      PO_LINE_LOCATIONS_ALL POLL,
3408                      PO_LINES_ALL POL,
3409                      PO_HEADERS_ALL PH1
3410               WHERE  POL.contract_id = p_po_header_id
3411               AND    POL.po_line_id        = POLL.po_line_id
3412               AND    POLL.shipment_type in ('STANDARD','PLANNED')
3413               AND    POD.line_location_id(+) = POLL.line_location_id
3414               AND    PH1.po_header_id = POL.po_header_id;
3415 
3416             ELSE                  -- (l_po_min_unit is null)
3417 
3418                 -- SQL What: calculate amount released from all lines across OU
3419                 --           referencing this GC, with the amount being
3420                 --           converted to base currency
3421                 -- SQL Why:  This is the return value
3422                 --<Complex Work R12>: added decode on value basis
3423 
3424                 --Bug# 5391346: Added Decode for currency match condition
3425                 --Use PO precision (ie Contract currency precision) when l_po_min_unit is null (PO implies the Contract here)
3426                 --Also added a join with PO_HEADERS_ALL for rate in header of execution document
3427 
3428               l_progress := '050';
3429 
3430               SELECT NVL(
3431                      SUM(
3432                         DECODE(l_base_currency, PH1.currency_code,
3433                                ROUND(
3434                                  ROUND(
3435                                     DECODE(POLL.value_basis
3436                                     ,'FIXED PRICE',
3437                                      NVL(POD.amount_ordered, 0) -
3438                                      NVL(POD.amount_cancelled, 0)
3439                                     , 'RATE',
3440                                      NVL(POD.amount_ordered, 0)-
3441                                      NVL(POD.amount_cancelled, 0)
3442                                     , --Qty based
3443                                     (NVL(POD.quantity_ordered, 0) -
3444                                      NVL(POD.quantity_cancelled,0)
3445                                      ) * NVL(POLL.price_override, 0)
3446                                           )
3447                                     ,l_po_precision)
3448                                    /l_base_min_unit)*l_base_min_unit
3449                               ,
3450 
3451 
3452                                   ROUND(
3453                                     ROUND(
3454                                          DECODE(POLL.value_basis
3455                                           ,'FIXED PRICE',
3456                                            NVL(POD.amount_ordered, 0) -
3457                                            NVL(POD.amount_cancelled, 0)
3458                                           , 'RATE',
3459                                            NVL(POD.amount_ordered, 0)-
3460                                            NVL(POD.amount_cancelled, 0)
3461                                           , --Qty based
3462                                           (NVL(POD.quantity_ordered, 0) -
3463                                            NVL(POD.quantity_cancelled,0)
3464                                            ) * NVL(POLL.price_override, 0)
3465                                             ) * NVL(POD.rate, NVL(PH1.rate,1))
3466                                       ,l_po_precision)
3467                                     /l_base_min_unit) * l_base_min_unit
3468                                 )
3469                                )
3470                             ,0)
3471               INTO   l_total
3472               FROM   PO_DISTRIBUTIONS_ALL POD,
3473                      PO_LINE_LOCATIONS_ALL POLL,
3474                      PO_LINES_ALL POL,
3475                      PO_HEADERS_ALL PH1
3476               WHERE POL.contract_id = p_po_header_id
3477               AND    POL.po_line_id        = POLL.po_line_id
3478               AND    POLL.shipment_type in ('STANDARD','PLANNED')
3479               AND    POD.line_location_id(+) = POLL.line_location_id
3480               AND    PH1.po_header_id = POL.po_header_id;
3481             END IF;
3482 
3483         ELSE                     -- (l_base_min_unit IS NULL)
3484             l_progress := '060';
3485 
3486             IF (l_po_min_unit IS NOT NULL) THEN
3487 
3488                 -- SQL What: calculate amount released from all lines across OU
3489                 --           referencing this GC, with the amount being
3490                 --           converted to base currency
3491                 -- SQL Why:  This is the return value
3492                 --<Complex Work R12>: added decode on value basis
3493 
3494                 --Bug# 5391346: Added Decode for currency match condition
3495                 --use only PO min unit and not PO precision when l_po_min_unit is not null (PO implies the Contract here)
3496                 --Also added a join with PO_HEADERS_ALL for rate in header of execution document
3497 
3498               l_progress := '070';
3499 
3500               SELECT  NVL(
3501                        SUM(
3502                          DECODE(l_base_currency, PH1.currency_code,
3503                                  ROUND(
3504                                   ROUND(
3505                                      DECODE(POLL.value_basis
3506                                         ,'FIXED PRICE',
3507                                          NVL(POD.amount_ordered, 0) -
3508                                          NVL(POD.amount_cancelled, 0)
3509                                         , 'RATE',
3510                                          NVL(POD.amount_ordered, 0)-
3511                                          NVL(POD.amount_cancelled, 0)
3512                                         , --Qty based
3513                                         (NVL(POD.quantity_ordered, 0) -
3514                                          NVL(POD.quantity_cancelled,0)
3515                                          ) * NVL(POLL.price_override, 0)
3516                                         )
3517                                      /l_po_min_unit) * l_po_min_unit
3518                                     , l_base_precision),
3519                                   ROUND(
3520                                     ROUND(
3521                                      DECODE(POLL.value_basis
3522                                           ,'FIXED PRICE',
3523                                            NVL(POD.amount_ordered, 0) -
3524                                            NVL(POD.amount_cancelled, 0)
3525                                           , 'RATE',
3526                                            NVL(POD.amount_ordered, 0)-
3527                                            NVL(POD.amount_cancelled, 0)
3528                                           , --Qty based
3529                                           (NVL(POD.quantity_ordered, 0) -
3530                                            NVL(POD.quantity_cancelled,0)
3531                                            ) * NVL(POLL.price_override, 0)
3532                                           )* NVL(POD.rate, NVL(PH1.rate,1)
3533                                           )
3534                                        /l_po_min_unit) * l_po_min_unit
3535                                     , l_base_precision)
3536                                   )
3537                              ),0)
3538                 INTO   l_total
3539                 FROM   PO_DISTRIBUTIONS_ALL POD,
3540                        PO_LINE_LOCATIONS_ALL POLL,
3541                        PO_LINES_ALL PoL,
3542                        PO_HEADERS_ALL PH1
3543                 WHERE   POL.contract_id = p_po_header_id
3544                 AND    POL.po_line_id        = POLL.po_line_id
3545                 AND    POLL.shipment_type in ('STANDARD','PLANNED')
3546                 AND    POD.line_location_id(+) = POLL.line_location_id
3547                 AND    PH1.po_header_id = POL.po_header_id;
3548 
3549             ELSE                -- (l_po_min_unit IS NULL)
3550 
3551                 -- SQL What: calculate amount released from all lines across OU
3552                 --           referencing this GC, with the amount being
3553                 --           converted to base currency
3554                 -- SQL Why:  This is the return value
3555                 --<Complex Work R12>: added decode on value basis
3556 
3557                 --Bug# 5391346: Added Decode for currency match condition
3558                 --use PO precision when PO currency min unit is null
3559                 --Also added a join with PO_HEADERS_ALL for rate in header of execution document
3560 
3561               l_progress := '080';
3562 
3563               SELECT  NVL(
3564                        SUM(
3565                          DECODE(l_base_currency, PH1.currency_code,
3566                                  ROUND(
3567                                   ROUND(
3568                                      DECODE(POLL.value_basis
3569                                         ,'FIXED PRICE',
3570                                          NVL(POD.amount_ordered, 0) -
3571                                          NVL(POD.amount_cancelled, 0)
3572                                         , 'RATE',
3573                                          NVL(POD.amount_ordered, 0)-
3574                                          NVL(POD.amount_cancelled, 0)
3575                                         , --Qty based
3576                                         (NVL(POD.quantity_ordered, 0) -
3577                                          NVL(POD.quantity_cancelled,0)
3578                                          ) * NVL(POLL.price_override, 0)
3579                                         )
3580                                      ,l_po_precision)
3581                                     , l_base_precision),
3582                                   ROUND(
3583                                     ROUND(
3584                                      DECODE(POLL.value_basis
3585                                           ,'FIXED PRICE',
3586                                            NVL(POD.amount_ordered, 0) -
3587                                            NVL(POD.amount_cancelled, 0)
3588                                           , 'RATE',
3589                                            NVL(POD.amount_ordered, 0)-
3590                                            NVL(POD.amount_cancelled, 0)
3591                                           , --Qty based
3592                                           (NVL(POD.quantity_ordered, 0) -
3593                                            NVL(POD.quantity_cancelled,0)
3594                                            ) * NVL(POLL.price_override, 0)
3595                                           )* NVL(POD.rate, NVL(PH1.rate,1)
3596                                           )
3597                                        ,l_po_precision)
3598                                     , l_base_precision)
3599                                   )
3600                              ),0)
3601                 INTO   l_total
3602                 FROM   PO_DISTRIBUTIONS_ALL POD,
3603                        PO_LINE_LOCATIONS_ALL POLL,
3604                        PO_LINES_ALL POL,
3605                        PO_HEADERS_ALL PH1
3606                 WHERE   POL.contract_id = p_po_header_id
3607                 AND    POL.po_line_id        = POLL.po_line_id
3608                 AND    POLL.shipment_type in ('STANDARD','PLANNED')
3609                 AND    POD.line_location_id(+) = POLL.line_location_id
3610                 AND    PH1.po_header_id = POL.po_header_id;
3611 
3612             END IF;
3613 
3614         END IF;
3615 
3616     ELSE                   -- (no base currency conversion)
3617 
3618         l_progress := '090';
3619 
3620         IF (l_po_min_unit IS NOT NULL) THEN
3621 
3622             -- SQL What: calculate amount released from all lines across OU
3623             --           referencing this GC in GC currency
3624             -- SQL Why:  This is the return value
3625             --<Complex Work R12>: added decode on value basis
3626 
3627             --Bug# 5391346: Added Decode for currency match condition
3628             --Also added a join with PO_HEADERS_ALL for rate in header of execution document
3629           l_progress := '100';
3630 
3631           SELECT  NVL(
3632                     SUM(
3633                        DECODE(PH.currency_code, PH1.currency_code,
3634                                 ROUND (
3635                                         DECODE(POLL.value_basis
3636                                             ,'FIXED PRICE',
3637                                              NVL(POD.amount_ordered, 0) -
3638                                              NVL(POD.amount_cancelled, 0)
3639                                             , 'RATE',
3640                                              NVL(POD.amount_ordered, 0)-
3641                                              NVL(POD.amount_cancelled, 0)
3642                                             , --Qty based
3643                                             (NVL(POD.quantity_ordered, 0) -
3644                                              NVL(POD.quantity_cancelled,0)
3645                                              ) * NVL(POLL.price_override, 0)
3646                                               )
3647                                        /l_po_min_unit)* l_po_min_unit
3648                                           ,
3649 
3650                                 ROUND(
3651                                         DECODE(POLL.value_basis
3652                                             ,'FIXED PRICE',
3653                                              NVL(POD.amount_ordered, 0) -
3654                                              NVL(POD.amount_cancelled, 0)
3655                                             , 'RATE',
3656                                              NVL(POD.amount_ordered, 0)-
3657                                              NVL(POD.amount_cancelled, 0)
3658                                             , --Qty based
3659                                             (NVL(POD.quantity_ordered, 0) -
3660                                              NVL(POD.quantity_cancelled,0)
3661                                              ) * NVL(POLL.price_override, 0)
3662                                             )* NVL(POD.rate, NVL(PH1.rate,1))
3663                                             /NVL(PH.rate,1)
3664                                        /l_po_min_unit)* l_po_min_unit
3665 
3666                            )
3667                         )
3668                      ,0)
3669            INTO   l_total
3670            FROM   PO_DISTRIBUTIONS_ALL POD,
3671                   PO_LINE_LOCATIONS_ALL POLL,
3672                   PO_LINES_ALL POL,
3673                   PO_HEADERS_ALL PH,
3674                   PO_HEADERS_ALL PH1
3675            WHERE  PH.po_header_id      = p_po_header_id
3676            AND    PH.po_header_id      = POL.contract_id
3677            AND    POL.po_line_id        = POLL.po_line_id
3678            AND    POLL.shipment_type in ('STANDARD','PLANNED')
3679            AND    POD.line_location_id(+) = POLL.line_location_id
3680            AND    PH1.po_header_id = POL.po_header_id;
3681 
3682         ELSE               -- (l_po_min unit IS NULL)
3683 
3684             -- SQL What: calculate amount released from all lines across OU
3685             --           referencing this GC in GC currency
3686             -- SQL Why:  This is the return value
3687             --<Complex Work R12>: added decode on value basis
3688 
3689             --Bug# 5391346: Added Decode for currency match condition
3690             --Also added a join with PO_HEADERS_ALL for rate in header of execution document
3691 
3692           l_progress := '110';
3693 
3694           SELECT  NVL(
3695                     SUM(
3696 
3697                              DECODE(PH.currency_code, PH1.currency_code,
3698                                  ROUND(
3699                                      DECODE(POLL.value_basis
3700                                             ,'FIXED PRICE',
3701                                              NVL(POD.amount_ordered, 0) -
3702                                              NVL(POD.amount_cancelled, 0)
3703                                             , 'RATE',
3704                                              NVL(POD.amount_ordered, 0)-
3705                                              NVL(POD.amount_cancelled, 0)
3706                                             , --Qty based
3707                                             (NVL(POD.quantity_ordered, 0) -
3708                                              NVL(POD.quantity_cancelled,0)
3709                                              ) * NVL(POLL.price_override, 0)
3710                                               )
3711 
3712 
3713                                   , l_po_precision ) ,
3714 
3715                                   ROUND(
3716                                      DECODE(POLL.value_basis
3717                                             ,'FIXED PRICE',
3718                                              NVL(POD.amount_ordered, 0) -
3719                                              NVL(POD.amount_cancelled, 0)
3720                                             , 'RATE',
3721                                              NVL(POD.amount_ordered, 0)-
3722                                              NVL(POD.amount_cancelled, 0)
3723                                             , --Qty based
3724                                             (NVL(POD.quantity_ordered, 0) -
3725                                              NVL(POD.quantity_cancelled,0)
3726                                              ) * NVL(POLL.price_override, 0)
3727                                             )
3728 
3729                                           * NVL(POD.rate, NVL(PH1.rate,1))
3730                                           /NVL(PH.rate,1)
3731 
3732                                  , l_po_precision)
3733                             )
3734                         )
3735                      ,0)
3736          INTO   l_total
3737          FROM   PO_DISTRIBUTIONS_ALL POD,
3738                 PO_LINE_LOCATIONS_ALL POLL,
3739                 PO_LINES_ALL POL,
3740                 PO_HEADERS_ALL PH,
3741                 PO_HEADERS_ALL PH1
3742          WHERE  PH.po_header_id      = p_po_header_id
3743          AND    PH.po_header_id      = POL.contract_id         -- <GC FPJ>
3744          AND    POL.po_line_id        = POLL.po_line_id
3745          AND    POLL.shipment_type in ('STANDARD','PLANNED')
3746          AND    POD.line_location_id(+) = POLL.line_location_id
3747          AND    PH1.po_header_id = POL.po_header_id;
3748 
3749         END IF;
3750 
3751     END IF;
3752 
3753   IF g_debug_stmt THEN
3754      PO_DEBUG.debug_end(l_log_head);
3755   END IF;
3756 
3757   --Bug# 5391346 END
3758 
3759   RETURN l_total;
3760 
3761 EXCEPTION
3762     WHEN OTHERS THEN
3763         PO_DEBUG.debug_exc(l_log_head,l_progress);
3764         RETURN 0;
3765 END get_gc_amount_released;
3766 
3767 -- <GC FPJ END>
3768 
3769 /* ===========================================================================
3770   PROCEDURE NAME : validate_lookup_info (
3771                    p_lookup_rec IN OUT NOCOPY RCV_SHIPMENT_HEADER_SV.LookupRecType)
3772 
3773   DESCRIPTION    :
3774 
3775   CLIENT/SERVER  : SERVER
3776 
3777   LIBRARY NAME   :
3778 
3779   OWNER          : Raj Bhakta
3780 
3781   PARAMETERS     : p_lookup_rec IN OUT NOCOPY RCV_SHIPMENT_HEADER_SV.LookupRecType
3782 
3783   ALGORITHM      :
3784 
3785   NOTES          : Generic procedure which accepts the lookup record and
3786                    returns error status and error message depending on
3787                    business rules. The lookup record has type and code as
3788                    components.
3789 
3790   CHANGE HISTORY : Raj Bhakta 10/30/96 created
3791 
3792 =========================================================================== */
3793 
3794  PROCEDURE validate_lookup_info(
3795           p_lookup_rec IN OUT NOCOPY RCV_SHIPMENT_HEADER_SV.LookupRecType) IS
3796 
3797 
3798  cursor C is
3799    SELECT inactive_date
3800    FROM po_lookup_codes
3801    WHERE
3802        lookup_type = p_lookup_rec.lookup_type and
3803        lookup_code = p_lookup_rec.lookup_code;
3804 
3805  X_sysdate Date := sysdate;
3806 
3807  X_inactive_date   po_lookup_codes.INACTIVE_DATE%TYPE;
3808 
3809  BEGIN
3810 
3811    OPEN C;
3812    FETCH C INTO X_inactive_date;
3813 
3814    IF C%ROWCOUNT = 0 then
3815       CLOSE C;
3816       p_lookup_rec.error_record.error_status := 'E';
3817       p_lookup_rec.error_record.error_message := 'INVALID_LOOKUP';
3818       RETURN;
3819 
3820    ELSE
3821 
3822       IF X_sysdate > nvl(X_inactive_date,X_sysdate + 1) THEN
3823 
3824            CLOSE C;
3825            p_lookup_rec.error_record.error_status := 'E';
3826            p_lookup_rec.error_record.error_message := 'INACTIVE_LOOKUP';
3827            RETURN;
3828       END IF;
3829 
3830       LOOP
3831         FETCH C INTO X_inactive_date;
3832         IF C%NOTFOUND THEN
3833 
3834            p_lookup_rec.error_record.error_status := 'S';
3835            p_lookup_rec.error_record.error_message := NULL;
3836            EXIT;
3837 
3838         END IF;
3839         IF C%FOUND THEN
3840 
3841            p_lookup_rec.error_record.error_status := 'E';
3842            p_lookup_rec.error_record.error_message := 'TOOMANYROWS';
3843            CLOSE C;
3844            EXIT;
3845 
3846         END IF;
3847 
3848        END LOOP;
3849 
3850    END IF;
3851 
3852  EXCEPTION
3853     WHEN others THEN
3854            p_lookup_rec.error_record.error_status := 'U';
3855            p_lookup_rec.error_record.error_message := sqlerrm;
3856 
3857  END validate_lookup_info;
3858 
3859 
3860 
3861 
3862 
3863 -------------------------------------------
3864 -- Document id helper procedures
3865 -------------------------------------------
3866 
3867 
3868 
3869 
3870 -------------------------------------------------------------------------------
3871 --Start of Comments
3872 --Name: get_document_ids
3873 --Pre-reqs:
3874 --  None.
3875 --Modifies:
3876 --  None.
3877 --Locks:
3878 --  None.
3879 --Function:
3880 --  Returns the header-level document id for the given ids.
3881 --Parameters:
3882 --IN:
3883 --p_doc_type
3884 --  Document type.  Use the g_doc_type_<> variables, where <> is:
3885 --    REQUISITION
3886 --    PA
3887 --    PO
3888 --    RELEASE
3889 --p_doc_level
3890 --  The type of ids that are being passed.  Use g_doc_level_<>
3891 --    HEADER
3892 --    LINE
3893 --    SHIPMENT
3894 --    DISTRIBUTION
3895 --p_doc_level_id_tbl
3896 --  Ids of the doc level type of which to derive the document header id.
3897 --OUT:
3898 --x_doc_id_tbl
3899 --  Header-level ids of the input ids.
3900 --Testing:
3901 --
3902 --End of Comments
3903 -------------------------------------------------------------------------------
3904 PROCEDURE get_document_ids(
3905    p_doc_type                       IN             VARCHAR2
3906 ,  p_doc_level                      IN             VARCHAR2
3907 ,  p_doc_level_id_tbl               IN             po_tbl_number
3908 ,  x_doc_id_tbl                     OUT NOCOPY     po_tbl_number
3909 )
3910 IS
3911 
3912 l_log_head     CONSTANT VARCHAR2(100) := g_log_head||'GET_DOCUMENT_IDS';
3913 l_progress     VARCHAR2(3) := '000';
3914 
3915 l_id_key    NUMBER;
3916 
3917 -- Bug 3292870
3918 l_rowid_char_tbl    g_rowid_char_tbl;
3919 
3920 BEGIN
3921 
3922 IF g_debug_stmt THEN
3923    PO_DEBUG.debug_begin(l_log_head);
3924    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_type', p_doc_type);
3925    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level', p_doc_level);
3926    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level_id_tbl', p_doc_level_id_tbl);
3927 END IF;
3928 
3929 l_progress := '010';
3930 
3931 IF (p_doc_level = g_doc_level_HEADER) THEN
3932 
3933    l_progress := '020';
3934    IF g_debug_stmt THEN
3935       PO_DEBUG.debug_stmt(l_log_head,l_progress,'headers');
3936    END IF;
3937 
3938    x_doc_id_tbl := p_doc_level_id_tbl;
3939 
3940 ELSE
3941 
3942    l_progress := '100';
3943    IF g_debug_stmt THEN
3944       PO_DEBUG.debug_stmt(l_log_head,l_progress,'not headers');
3945    END IF;
3946 
3947    -- We need to get outthe header ids in the same ordering as the
3948    -- input id table.
3949    -- We can't do a FORALL ... SELECT (PL/SQL limitation),
3950    -- but we can to a FORALL ... INSERT ... RETURNING.
3951 
3952    ----------------------------------------------------------------
3953    -- PO_SESSION_GT column mapping
3954    --
3955    -- num1     doc level id
3956    -- num2     header id of num1
3957    ----------------------------------------------------------------
3958 
3959    l_id_key := get_session_gt_nextval();
3960 
3961    l_progress := '110';
3962 
3963    IF (p_doc_type = g_doc_type_REQUISITION) THEN
3964 
3965       l_progress := '120';
3966       IF g_debug_stmt THEN
3967          PO_DEBUG.debug_stmt(l_log_head,l_progress,'requisition');
3968       END IF;
3969 
3970       IF (p_doc_level = g_doc_level_LINE) THEN
3971 
3972          l_progress := '130';
3973          IF g_debug_stmt THEN
3974             PO_DEBUG.debug_stmt(l_log_head,l_progress,'line');
3975          END IF;
3976 
3977          /* Start Bug 3292870: Split query to make it compatible with 8i db. */
3978 
3979          FORALL i IN 1 .. p_doc_level_id_tbl.COUNT
3980          INSERT INTO PO_SESSION_GT ( key, num1 )
3981          VALUES
3982          (  l_id_key
3983          ,  p_doc_level_id_tbl(i)
3984          )
3985          RETURNING ROWIDTOCHAR(rowid)
3986          BULK COLLECT INTO l_rowid_char_tbl
3987          ;
3988 
3989          FORALL i IN 1 .. p_doc_level_id_tbl.COUNT
3990          UPDATE PO_SESSION_GT
3991          SET num2 =
3992             (
3993                SELECT PRL.requisition_header_id
3994                FROM PO_REQUISITION_LINES_ALL PRL
3995                WHERE PRL.requisition_line_id = p_doc_level_id_tbl(i)
3996             )
3997          WHERE rowid = CHARTOROWID(l_rowid_char_tbl(i))
3998          RETURNING num2
3999          BULK COLLECT INTO x_doc_id_tbl
4000          ;
4001 
4002          /* End Bug 3292870 */
4003 
4004          l_progress := '140';
4005 
4006       ELSIF (p_doc_level = g_doc_level_DISTRIBUTION) THEN
4007 
4008          l_progress := '150';
4009          IF g_debug_stmt THEN
4010             PO_DEBUG.debug_stmt(l_log_head,l_progress,'distribution');
4011          END IF;
4012 
4013          /* Start Bug 3292870: Split query to make it compatible with 8i db. */
4014 
4015          FORALL i IN 1 .. p_doc_level_id_tbl.COUNT
4016          INSERT INTO PO_SESSION_GT ( key, num1 )
4017          VALUES
4018          (  l_id_key
4019          ,  p_doc_level_id_tbl(i)
4020          )
4021          RETURNING ROWIDTOCHAR(rowid)
4022          BULK COLLECT INTO l_rowid_char_tbl
4023          ;
4024 
4025          FORALL i IN 1 .. p_doc_level_id_tbl.COUNT
4026          UPDATE PO_SESSION_GT
4027          SET num2 =
4028             (
4029                SELECT PRL.requisition_header_id
4030                FROM
4031                   PO_REQUISITION_LINES_ALL PRL
4032                ,  PO_REQ_DISTRIBUTIONS_ALL PRD
4033                WHERE PRL.requisition_line_id = PRD.requisition_line_id
4034                AND PRD.distribution_id = p_doc_level_id_tbl(i)
4035             )
4036          WHERE rowid = CHARTOROWID(l_rowid_char_tbl(i))
4037          RETURNING num2
4038          BULK COLLECT INTO x_doc_id_tbl
4039          ;
4040 
4041          /* End Bug 3292870 */
4042 
4043          l_progress := '160';
4044 
4045       ELSE
4046          l_progress := '170';
4047          RAISE g_INVALID_CALL_EXC;
4048       END IF;
4049 
4050       l_progress := '190';
4051 
4052    ELSE -- PO, PA, RELEASE
4053 
4054       l_progress := '200';
4055       IF g_debug_stmt THEN
4056          PO_DEBUG.debug_stmt(l_log_head,l_progress,'not req');
4057       END IF;
4058 
4059       IF (p_doc_level = g_doc_level_LINE) THEN
4060 
4061          l_progress := '210';
4062          IF g_debug_stmt THEN
4063             PO_DEBUG.debug_stmt(l_log_head,l_progress,'line');
4064          END IF;
4065 
4066          /* Start Bug 3292870: Split query to make it compatible with 8i db. */
4067 
4068          FORALL i IN 1 .. p_doc_level_id_tbl.COUNT
4069          INSERT INTO PO_SESSION_GT ( key, num1 )
4070          VALUES
4071          (  l_id_key
4072          ,  p_doc_level_id_tbl(i)
4073          )
4074          RETURNING ROWIDTOCHAR(rowid)
4075          BULK COLLECT INTO l_rowid_char_tbl
4076          ;
4077 
4078          FORALL i IN 1 .. p_doc_level_id_tbl.COUNT
4079          UPDATE PO_SESSION_GT
4080          SET num2 =
4081             (
4082                SELECT POL.po_header_id
4083                FROM PO_LINES_ALL POL
4084                WHERE POL.po_line_id = p_doc_level_id_tbl(i)
4085             )
4086          WHERE rowid = CHARTOROWID(l_rowid_char_tbl(i))
4087          RETURNING num2
4088          BULK COLLECT INTO x_doc_id_tbl
4089          ;
4090 
4091          /* End Bug 3292870 */
4092 
4093          l_progress := '220';
4094 
4095       ELSIF (p_doc_level = g_doc_level_SHIPMENT) THEN
4096 
4097          l_progress := '230';
4098          IF g_debug_stmt THEN
4099             PO_DEBUG.debug_stmt(l_log_head,l_progress,'shipment');
4100          END IF;
4101 
4102          /* Start Bug 3292870: Split query to make it compatible with 8i db. */
4103 
4104          FORALL i IN 1 .. p_doc_level_id_tbl.COUNT
4105          INSERT INTO PO_SESSION_GT ( key, num1 )
4106          VALUES
4107          (  l_id_key
4108          ,  p_doc_level_id_tbl(i)
4109          )
4110          RETURNING ROWIDTOCHAR(rowid)
4111          BULK COLLECT INTO l_rowid_char_tbl
4112          ;
4113 
4114          FORALL i IN 1 .. p_doc_level_id_tbl.COUNT
4115          UPDATE PO_SESSION_GT
4116          SET num2 =
4117             (
4118                SELECT DECODE( p_doc_type
4119                            ,  g_doc_type_RELEASE, POLL.po_release_id
4120                            ,  POLL.po_header_id
4121                            )
4122                FROM PO_LINE_LOCATIONS_ALL POLL
4123                WHERE POLL.line_location_id = p_doc_level_id_tbl(i)
4124             )
4125          WHERE rowid = CHARTOROWID(l_rowid_char_tbl(i))
4126          RETURNING num2
4127          BULK COLLECT INTO x_doc_id_tbl
4128          ;
4129 
4130          /* End Bug 3292870 */
4131 
4132          l_progress := '240';
4133 
4134       ELSIF (p_doc_level = g_doc_level_DISTRIBUTION) THEN
4135 
4136          l_progress := '250';
4137          IF g_debug_stmt THEN
4138             PO_DEBUG.debug_stmt(l_log_head,l_progress,'distribution');
4139          END IF;
4140 
4141          /* Start Bug 3292870: Split query to make it compatible with 8i db. */
4142 
4143          FORALL i IN 1 .. p_doc_level_id_tbl.COUNT
4144          INSERT INTO PO_SESSION_GT ( key, num1 )
4145          VALUES
4146          (  l_id_key
4147          ,  p_doc_level_id_tbl(i)
4148          )
4149          RETURNING ROWIDTOCHAR(rowid)
4150          BULK COLLECT INTO l_rowid_char_tbl
4151          ;
4152 
4153          FORALL i IN 1 .. p_doc_level_id_tbl.COUNT
4154          UPDATE PO_SESSION_GT
4155          SET num2 =
4156             (
4157                SELECT DECODE( p_doc_type
4158                            ,  g_doc_type_RELEASE, POD.po_release_id
4159                            ,  POD.po_header_id
4160                            )
4161                FROM PO_DISTRIBUTIONS_ALL POD
4162                WHERE POD.po_distribution_id = p_doc_level_id_tbl(i)
4163             )
4164          WHERE rowid = CHARTOROWID(l_rowid_char_tbl(i))
4165          RETURNING num2
4166          BULK COLLECT INTO x_doc_id_tbl
4167          ;
4168 
4169          /* End Bug 3292870 */
4170 
4171          l_progress := '260';
4172 
4173       ELSE
4174          l_progress := '270';
4175          RAISE g_INVALID_CALL_EXC;
4176       END IF;
4177 
4178       l_progress := '280';
4179 
4180    END IF;
4181 
4182    l_progress := '290';
4183 
4184 END IF;
4185 
4186 l_progress := '900';
4187 
4188 IF g_debug_stmt THEN
4189    PO_DEBUG.debug_var(l_log_head,l_progress,'x_doc_id_tbl',x_doc_id_tbl);
4190    PO_DEBUG.debug_end(l_log_head);
4191 END IF;
4192 
4193 EXCEPTION
4194 WHEN OTHERS THEN
4195    IF g_debug_unexp THEN
4196       PO_DEBUG.debug_exc(l_log_head,l_progress);
4197    END IF;
4198    RAISE;
4199 
4200 END get_document_ids;
4201 
4202 
4203 
4204 
4205 -------------------------------------------------------------------------------
4206 --Start of Comments
4207 --Name: get_line_ids
4208 --Pre-reqs:
4209 --  None.
4210 --Modifies:
4211 --  None.
4212 --Locks:
4213 --  None.
4214 --Function:
4215 --  Returns the line-level ids for the given ids.
4216 --Parameters:
4217 --IN:
4218 --p_doc_type
4219 --  Document type.  Use the g_doc_type_<> variables, where <> is:
4220 --    REQUISITION
4221 --    PA
4222 --    PO
4223 --    RELEASE
4224 --p_doc_level
4225 --  The type of ids that are being passed.  Use g_doc_level_<>
4226 --    HEADER
4227 --    LINE
4228 --    SHIPMENT
4229 --    DISTRIBUTION
4230 --p_doc_level_id_tbl
4231 --  Ids of the doc level type of which to derive the document header id.
4232 --OUT:
4233 --x_line_id_tbl
4234 --  Line-level ids of the input ids.
4235 --Testing:
4236 --
4237 --End of Comments
4238 -------------------------------------------------------------------------------
4239 PROCEDURE get_line_ids(
4240    p_doc_type                       IN             VARCHAR2
4241 ,  p_doc_level                      IN             VARCHAR2
4242 ,  p_doc_level_id_tbl               IN             po_tbl_number
4243 ,  x_line_id_tbl                    OUT NOCOPY     po_tbl_number
4244 )
4245 IS
4246 
4247 l_log_head     CONSTANT VARCHAR2(100) := g_log_head||'GET_LINE_IDS';
4248 l_progress     VARCHAR2(3) := '000';
4249 
4250 l_id_key    NUMBER;
4251 
4252 --Bug 3292870
4253 l_rowid_char_tbl     g_rowid_char_tbl;
4254 
4255 BEGIN
4256 
4257 IF g_debug_stmt THEN
4258    PO_DEBUG.debug_begin(l_log_head);
4259    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_type', p_doc_type);
4260    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level', p_doc_level);
4261    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level_id_tbl', p_doc_level_id_tbl);
4262 END IF;
4263 
4264 l_progress := '010';
4265 
4266 IF (p_doc_level = g_doc_level_LINE) THEN
4267 
4268    l_progress := '020';
4269    IF g_debug_stmt THEN
4270       PO_DEBUG.debug_stmt(l_log_head,l_progress,'lines');
4271    END IF;
4272 
4273    x_line_id_tbl := p_doc_level_id_tbl;
4274 
4275 ELSIF (p_doc_level = g_doc_level_HEADER) THEN
4276 
4277    l_progress := '100';
4278    IF g_debug_stmt THEN
4279       PO_DEBUG.debug_stmt(l_log_head,l_progress,'headers');
4280    END IF;
4281 
4282    ----------------------------------------------------
4283    --Algorithm:
4284    --
4285    -- 1. Load the ids into the scratchpad.
4286    -- 2. Join to the appropriate tables to bulk collect the line ids.
4287    ----------------------------------------------------
4288 
4289    ---------------------------------------
4290    -- PO_SESSION_GT column mapping
4291    --
4292    -- num1     doc_level_id
4293    ---------------------------------------
4294 
4295    l_id_key := get_session_gt_nextval();
4296 
4297    l_progress := '110';
4298 
4299    FORALL i IN 1 .. p_doc_level_id_tbl.COUNT
4300    INSERT INTO PO_SESSION_GT ( key, num1 )
4301    VALUES ( l_id_key, p_doc_level_id_tbl(i) )
4302    ;
4303 
4304    l_progress := '120';
4305 
4306    IF (p_doc_type = g_doc_type_REQUISITION) THEN
4307 
4308       l_progress := '130';
4309       IF g_debug_stmt THEN
4310          PO_DEBUG.debug_stmt(l_log_head,l_progress,'req headers');
4311       END IF;
4312 
4313       -- Gather all of the req line ids below this entity level.
4314 
4315       SELECT PRL.requisition_line_id
4316       BULK COLLECT INTO x_line_id_tbl
4317       FROM
4318          PO_REQUISITION_LINES_ALL PRL
4319       ,  PO_SESSION_GT IDS
4320       WHERE PRL.requisition_header_id = IDS.num1
4321       AND IDS.key = l_id_key
4322       ;
4323 
4324       l_progress := '140';
4325 
4326    ELSIF (p_doc_type IN (g_doc_type_PO, g_doc_type_PA)) THEN
4327 
4328       l_progress := '150';
4329       IF g_debug_stmt THEN
4330          PO_DEBUG.debug_stmt(l_log_head,l_progress,'po/pa headers');
4331       END IF;
4332 
4333       -- Gather all of the line ids below this entity level.
4334 
4335       SELECT POL.po_line_id
4336       BULK COLLECT INTO x_line_id_tbl
4337       FROM
4338          PO_LINES_ALL POL
4339       ,  PO_SESSION_GT IDS
4340       WHERE POL.po_header_id = IDS.num1
4341       AND IDS.key = l_id_key
4342       ;
4343 
4344       l_progress := '160';
4345 
4346    ELSE
4347       l_progress := '180';
4348       RAISE g_INVALID_CALL_EXC;
4349    END IF;
4350 
4351    l_progress := '190';
4352 
4353 ELSIF (p_doc_level = g_doc_level_SHIPMENT
4354       AND p_doc_type IN (g_doc_type_PO, g_doc_type_PA, g_doc_type_RELEASE))
4355 THEN
4356 
4357    l_progress := '200';
4358    IF g_debug_stmt THEN
4359       PO_DEBUG.debug_stmt(l_log_head,l_progress,'shipments');
4360    END IF;
4361 
4362    -- We need to get out the line ids in the same ordering as the
4363    -- input id table.
4364    -- We can't do a FORALL ... SELECT (PL/SQL limitation),
4365    -- but we can to a FORALL ... INSERT ... RETURNING.
4366 
4367    ----------------------------------------------------------------
4368    -- PO_SESSION_GT column mapping
4369    --
4370    -- num1     doc level id
4371    -- num2     line id of num1
4372    ----------------------------------------------------------------
4373 
4374    l_id_key := get_session_gt_nextval();
4375 
4376    l_progress := '210';
4377 
4378    /* Start Bug 3292870: Split query to make it compatible with 8i db. */
4379 
4380    FORALL i IN 1 .. p_doc_level_id_tbl.COUNT
4381    INSERT INTO PO_SESSION_GT ( key, num1 )
4382    VALUES
4383    (  l_id_key
4384    ,  p_doc_level_id_tbl(i)
4385    )
4386    RETURNING ROWIDTOCHAR(rowid)
4387    BULK COLLECT INTO l_rowid_char_tbl
4388    ;
4389 
4390    FORALL i IN 1 .. p_doc_level_id_tbl.COUNT
4391    UPDATE PO_SESSION_GT
4392    SET num2 =
4393       (
4394          SELECT POLL.po_line_id
4395          FROM PO_LINE_LOCATIONS_ALL POLL
4396          WHERE POLL.line_location_id = p_doc_level_id_tbl(i)
4397       )
4398    WHERE rowid = CHARTOROWID(l_rowid_char_tbl(i))
4399    RETURNING num2
4400    BULK COLLECT INTO x_line_id_tbl
4401    ;
4402 
4403    /* End Bug 3292870 */
4404 
4405    l_progress := '220';
4406 
4407 ELSIF (p_doc_level = g_doc_level_DISTRIBUTION) THEN
4408 
4409    l_progress := '300';
4410    IF g_debug_stmt THEN
4411       PO_DEBUG.debug_stmt(l_log_head,l_progress,'distributions');
4412    END IF;
4413 
4414    -- We need to get out the line ids in the same ordering as the
4415    -- input id table.
4416    -- We can't do a FORALL ... SELECT (PL/SQL limitation),
4417    -- but we can to a FORALL ... INSERT ... RETURNING.
4418 
4419    ----------------------------------------------------------------
4420    -- PO_SESSION_GT column mapping
4421    --
4422    -- num1     doc level id
4423    -- num2     line id of num1
4424    ----------------------------------------------------------------
4425 
4426    l_id_key := get_session_gt_nextval();
4427 
4428    l_progress := '310';
4429 
4430    IF (p_doc_type = g_doc_type_REQUISITION) THEN
4431 
4432       l_progress := '320';
4433       IF g_debug_stmt THEN
4434          PO_DEBUG.debug_stmt(l_log_head,l_progress,'req');
4435       END IF;
4436 
4437       /* Start Bug 3292870: Split query to make it compatible with 8i db. */
4438 
4439       FORALL i IN 1 .. p_doc_level_id_tbl.COUNT
4440       INSERT INTO PO_SESSION_GT ( key, num1 )
4441       VALUES
4442       (  l_id_key
4443       ,  p_doc_level_id_tbl(i)
4444       )
4445       RETURNING ROWIDTOCHAR(rowid)
4446       BULK COLLECT INTO l_rowid_char_tbl
4447       ;
4448 
4449       FORALL i IN 1 .. p_doc_level_id_tbl.COUNT
4450       UPDATE PO_SESSION_GT
4451       SET num2 =
4452          (
4453             SELECT PRD.requisition_line_id
4454             FROM PO_REQ_DISTRIBUTIONS_ALL PRD
4455             WHERE PRD.distribution_id = p_doc_level_id_tbl(i)
4456          )
4457       WHERE rowid = CHARTOROWID(l_rowid_char_tbl(i))
4458       RETURNING num2
4459       BULK COLLECT INTO x_line_id_tbl
4460       ;
4461 
4462       /* End Bug 3292870 */
4463 
4464       l_progress := '330';
4465 
4466    ELSE
4467 
4468       l_progress := '340';
4469       IF g_debug_stmt THEN
4470          PO_DEBUG.debug_stmt(l_log_head,l_progress,'not req');
4471       END IF;
4472 
4473       /* Start Bug 3292870: Split query to make it compatible with 8i db. */
4474 
4475       FORALL i IN 1 .. p_doc_level_id_tbl.COUNT
4476       INSERT INTO PO_SESSION_GT ( key, num1 )
4477       VALUES
4478       (  l_id_key
4479       ,  p_doc_level_id_tbl(i)
4480       )
4481       RETURNING ROWIDTOCHAR(rowid)
4482       BULK COLLECT INTO l_rowid_char_tbl
4483       ;
4484 
4485       FORALL i IN 1 .. p_doc_level_id_tbl.COUNT
4486       UPDATE PO_SESSION_GT
4487       SET num2 =
4488          (
4489             SELECT POD.po_line_id
4490             FROM PO_DISTRIBUTIONS_ALL POD
4491             WHERE POD.po_distribution_id = p_doc_level_id_tbl(i)
4492          )
4493       WHERE rowid = CHARTOROWID(l_rowid_char_tbl(i))
4494       RETURNING num2
4495       BULK COLLECT INTO x_line_id_tbl
4496       ;
4497 
4498       /* End Bug 3292870 */
4499 
4500       l_progress := '350';
4501 
4502    END IF;
4503 
4504    l_progress := '370';
4505 
4506 ELSE
4507    l_progress := '390';
4508    RAISE g_INVALID_CALL_EXC;
4509 END IF;
4510 
4511 l_progress := '900';
4512 
4513 IF g_debug_stmt THEN
4514    PO_DEBUG.debug_var(l_log_head,l_progress,'x_line_id_tbl',x_line_id_tbl);
4515    PO_DEBUG.debug_end(l_log_head);
4516 END IF;
4517 
4518 EXCEPTION
4519 WHEN OTHERS THEN
4520    IF g_debug_unexp THEN
4521       PO_DEBUG.debug_exc(l_log_head,l_progress);
4522    END IF;
4523    RAISE;
4524 
4525 END get_line_ids;
4526 
4527 
4528 
4529 
4530 -------------------------------------------------------------------------------
4531 --Start of Comments
4532 --Name: get_line_location_ids
4533 --Pre-reqs:
4534 --  None.
4535 --Modifies:
4536 --  None.
4537 --Locks:
4538 --  None.
4539 --Function:
4540 --  Retrieves the ids of shipments corresponding to the given doc level.
4541 --Parameters:
4542 --IN:
4543 --p_doc_type
4544 --  Document type.  Use the g_doc_type_<> variables, where <> is:
4545 --    PO
4546 --    PA
4547 --    RELEASE
4548 --    - REQUISITION not currently supported.
4549 --p_doc_level
4550 --  The type of ids that are being passed.  Use g_doc_level_<>
4551 --    HEADER
4552 --    LINE
4553 --    SHIPMENT
4554 --    DISTRIBUTION
4555 --p_doc_level_id_tbl
4556 --  Ids of the doc level type with which to find related shipments.
4557 --OUT:
4558 --x_line_location_id_tbl
4559 --  The ids of the related shipments that were found.
4560 --  If p_doc_level is DISTRIBUTION, the entries in this table
4561 --  will map one-to-one to the input id table.
4562 --Testing:
4563 --
4564 --End of Comments
4565 -------------------------------------------------------------------------------
4566 PROCEDURE get_line_location_ids(
4567    p_doc_type                       IN             VARCHAR2
4568 ,  p_doc_level                      IN             VARCHAR2
4569 ,  p_doc_level_id_tbl               IN             po_tbl_number
4570 ,  x_line_location_id_tbl           OUT NOCOPY     po_tbl_number
4571 )
4572 IS
4573 
4574 l_log_head     CONSTANT VARCHAR2(100) := g_log_head||'GET_LINE_LOCATION_IDS';
4575 l_progress     VARCHAR2(3) := '000';
4576 
4577 l_doc_level_id_key      NUMBER;
4578 
4579 -- Bug 3292870
4580 l_rowid_char_tbl   g_rowid_char_tbl;
4581 
4582 BEGIN
4583 
4584 IF g_debug_stmt THEN
4585    PO_DEBUG.debug_begin(l_log_head);
4586    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_type', p_doc_type);
4587    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level', p_doc_level);
4588    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level_id_tbl', p_doc_level_id_tbl);
4589 END IF;
4590 
4591 l_progress := '010';
4592 
4593 -----------------------------------------------------------------
4594 --Algorithm:
4595 --
4596 -- -  Insert the doc level ids into the scratchpad table.
4597 -- -  Join to the main doc tables against these doc level ids
4598 --       to retrieve the linked line location ids.
4599 -----------------------------------------------------------------
4600 
4601 IF (p_doc_level = g_doc_level_SHIPMENT) THEN
4602 
4603    l_progress := '100';
4604    IF g_debug_stmt THEN
4605       PO_DEBUG.debug_stmt(l_log_head,l_progress,'shipments');
4606    END IF;
4607 
4608    x_line_location_id_tbl := p_doc_level_id_tbl;
4609 
4610 ELSE
4611 
4612    l_progress := '200';
4613    IF g_debug_stmt THEN
4614       PO_DEBUG.debug_stmt(l_log_head,l_progress,'not shipments');
4615    END IF;
4616 
4617    -- Load the entity ids into the scratchpad.
4618 
4619    ---------------------------------------
4620    -- PO_SESSION_GT column mapping
4621    --
4622    -- num1     document_level_id
4623    ---------------------------------------
4624 
4625    l_doc_level_id_key := get_session_gt_nextval();
4626 
4627    l_progress := '210';
4628 
4629    FORALL i IN 1 .. p_doc_level_id_tbl.COUNT
4630    INSERT INTO PO_SESSION_GT ( key, num1 )
4631    VALUES ( l_doc_level_id_key, p_doc_level_id_tbl(i) )
4632    ;
4633 
4634    l_progress := '220';
4635 
4636    -- We need to derive the appropriate line location ids for the given
4637    -- entity id and entity level.
4638 
4639    -- NOT SUPPORTED FOR REQUISITIONS
4640 
4641    IF (  p_doc_type = g_doc_type_RELEASE
4642          AND p_doc_level = g_doc_level_HEADER)
4643    THEN
4644 
4645       l_progress := '410';
4646       IF g_debug_stmt THEN
4647          PO_DEBUG.debug_stmt(l_log_head,l_progress,'release');
4648       END IF;
4649 
4650       SELECT POLL.line_location_id
4651       BULK COLLECT INTO x_line_location_id_tbl
4652       FROM
4653          PO_LINE_LOCATIONS_ALL POLL
4654       ,  PO_SESSION_GT IDS
4655       WHERE POLL.po_release_id = IDS.num1
4656       AND IDS.key = l_doc_level_id_key
4657       ;
4658 
4659       l_progress := '420';
4660 
4661    ELSIF (p_doc_level = g_doc_level_HEADER) THEN
4662 
4663       l_progress := '430';
4664       IF g_debug_stmt THEN
4665          PO_DEBUG.debug_stmt(l_log_head,l_progress,'headers');
4666       END IF;
4667 
4668       SELECT POLL.line_location_id
4669       BULK COLLECT INTO x_line_location_id_tbl
4670       FROM
4671          PO_LINE_LOCATIONS_ALL POLL
4672       ,  PO_SESSION_GT IDS
4673       WHERE POLL.po_header_id = IDS.num1
4674       AND POLL.shipment_type <> g_ship_type_SCHEDULED
4675       AND POLL.shipment_type <> g_ship_type_BLANKET
4676          -- don't pick up release shipments for POs/PAs
4677       AND IDS.key = l_doc_level_id_key
4678       ;
4679 
4680       l_progress := '440';
4681 
4682    ELSIF (p_doc_level = g_doc_level_LINE) THEN
4683 
4684       l_progress := '450';
4685       IF g_debug_stmt THEN
4686          PO_DEBUG.debug_stmt(l_log_head,l_progress,'lines');
4687       END IF;
4688 
4689       SELECT POLL.line_location_id
4690       BULK COLLECT INTO x_line_location_id_tbl
4691       FROM
4692          PO_LINE_LOCATIONS_ALL POLL
4693       ,  PO_SESSION_GT IDS
4694       WHERE POLL.po_line_id = IDS.num1
4695       AND POLL.shipment_type <> g_ship_type_SCHEDULED
4696       AND POLL.shipment_type <> g_ship_type_BLANKET
4697          -- don't pick up release shipments for POs/PAs
4698       AND IDS.key = l_doc_level_id_key
4699       ;
4700 
4701       l_progress := '460';
4702 
4703    ELSIF (p_doc_level = g_doc_level_DISTRIBUTION) THEN
4704 
4705       -- We need to get out the header ids in the same ordering as the
4706       -- input id table.
4707       -- We can't do a FORALL ... SELECT (PL/SQL limitation),
4708       -- but we can to a FORALL ... INSERT ... RETURNING.
4709 
4710       ----------------------------------------------------------------
4711       -- PO_SESSION_GT column mapping
4712       --
4713       -- num1     doc level id
4714       -- num2     line location id of num1
4715       ----------------------------------------------------------------
4716 
4717       l_progress := '470';
4718       IF g_debug_stmt THEN
4719          PO_DEBUG.debug_stmt(l_log_head,l_progress,'distributions');
4720       END IF;
4721 
4722       /* Start Bug 3292870: Split query to make it compatible with 8i db. */
4723 
4724       FORALL i IN 1 .. p_doc_level_id_tbl.COUNT
4725       INSERT INTO PO_SESSION_GT ( key, num1 )
4726       VALUES
4727       (  l_doc_level_id_key
4728       ,  p_doc_level_id_tbl(i)
4729       )
4730       RETURNING ROWIDTOCHAR(rowid)
4731       BULK COLLECT INTO l_rowid_char_tbl
4732       ;
4733 
4734       FORALL i IN 1 .. p_doc_level_id_tbl.COUNT
4735       UPDATE PO_SESSION_GT
4736       SET num2 =
4737          (
4738             SELECT POD.line_location_id
4739             FROM PO_DISTRIBUTIONS_ALL POD
4740             WHERE POD.po_distribution_id = p_doc_level_id_tbl(i)
4741          )
4742       WHERE rowid = CHARTOROWID(l_rowid_char_tbl(i))
4743       RETURNING num2
4744       BULK COLLECT INTO x_line_location_id_tbl
4745       ;
4746 
4747       /* End Bug 3292870 */
4748 
4749       l_progress := '480';
4750 
4751    ELSE
4752       l_progress := '490';
4753       RAISE g_INVALID_CALL_EXC;
4754    END IF;
4755 
4756    l_progress := '500';
4757 
4758 END IF;
4759 
4760 l_progress := '900';
4761 
4762 IF g_debug_stmt THEN
4763    PO_DEBUG.debug_var(l_log_head,l_progress,'x_line_location_id_tbl',x_line_location_id_tbl);
4764    PO_DEBUG.debug_end(l_log_head);
4765 END IF;
4766 
4767 EXCEPTION
4768 WHEN OTHERS THEN
4769    IF g_debug_unexp THEN
4770       PO_DEBUG.debug_exc(l_log_head,l_progress);
4771    END IF;
4772    RAISE;
4773 
4774 END get_line_location_ids;
4775 
4776 
4777 
4778 
4779 -------------------------------------------------------------------------------
4780 --Start of Comments
4781 --Name: get_distribution_ids
4782 --Pre-reqs:
4783 --  None.
4784 --Modifies:
4785 --  None.
4786 --Locks:
4787 --  None.
4788 --Function:
4789 --  Retrieves the ids of distributions below the given doc level.
4790 --Parameters:
4791 --IN:
4792 --p_doc_type
4793 --  Document type.  Use the g_doc_type_<> variables, where <> is:
4794 --    REQUISITION
4795 --    PA
4796 --    PO
4797 --    RELEASE
4798 --p_doc_level
4799 --  The type of ids that are being passed.  Use g_doc_level_<>
4800 --    HEADER
4801 --    LINE
4802 --    SHIPMENT
4803 --    DISTRIBUTION
4804 --p_doc_level_id_tbl
4805 --  Ids of the doc level type with which to find related distributions.
4806 --OUT:
4807 --x_dist_id_tbl
4808 --  The ids of the related distributions that were found.
4809 --Testing:
4810 --
4811 --End of Comments
4812 -------------------------------------------------------------------------------
4813 PROCEDURE get_distribution_ids(
4814    p_doc_type                       IN             VARCHAR2
4815 ,  p_doc_level                      IN             VARCHAR2
4816 ,  p_doc_level_id_tbl               IN             po_tbl_number
4817 ,  x_distribution_id_tbl            OUT NOCOPY     po_tbl_number
4818 )
4819 IS
4820 
4821 l_log_head     CONSTANT VARCHAR2(100) := g_log_head||'GET_DISTRIBUTION_IDS';
4822 l_progress     VARCHAR2(3) := '000';
4823 
4824 l_doc_level_id_key      NUMBER;
4825 
4826 BEGIN
4827 
4828 IF g_debug_stmt THEN
4829    PO_DEBUG.debug_begin(l_log_head);
4830    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_type', p_doc_type);
4831    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level', p_doc_level);
4832    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level_id_tbl', p_doc_level_id_tbl);
4833 END IF;
4834 
4835 l_progress := '010';
4836 
4837 -----------------------------------------------------------------
4838 --Algorithm:
4839 --
4840 -- -  Insert the doc level ids into the scratchpad table.
4841 -- -  Join to the main doc tables against these doc level ids
4842 --       to retrieve the linked distribution ids.
4843 -----------------------------------------------------------------
4844 
4845 IF (p_doc_level = g_doc_level_DISTRIBUTION) THEN
4846 
4847    l_progress := '100';
4848    IF g_debug_stmt THEN
4849       PO_DEBUG.debug_stmt(l_log_head,l_progress,'distributions');
4850    END IF;
4851 
4852    x_distribution_id_tbl := p_doc_level_id_tbl;
4853 
4854 ELSE
4855 
4856    l_progress := '200';
4857    IF g_debug_stmt THEN
4858       PO_DEBUG.debug_stmt(l_log_head,l_progress,'not dists');
4859    END IF;
4860 
4861    -- Load the entity ids into the scratchpad.
4862 
4863    ---------------------------------------
4864    -- PO_SESSION_GT column mapping
4865    --
4866    -- num1     document_level_id
4867    ---------------------------------------
4868 
4869    l_doc_level_id_key := get_session_gt_nextval();
4870 
4871    l_progress := '210';
4872 
4873    FORALL i IN 1 .. p_doc_level_id_tbl.COUNT
4874    INSERT INTO PO_SESSION_GT ( key, num1 )
4875    VALUES ( l_doc_level_id_key, p_doc_level_id_tbl(i) )
4876    ;
4877 
4878    l_progress := '220';
4879 
4880    -- We need to derive the appropriate distribution ids for the given
4881    -- entity id and entity level.
4882 
4883    IF (p_doc_type = g_doc_type_REQUISITION) THEN
4884 
4885       l_progress := '300';
4886       IF g_debug_stmt THEN
4887          PO_DEBUG.debug_stmt(l_log_head,l_progress,'requisition');
4888       END IF;
4889 
4890       -- Gather all of the req distribution ids below this entity level.
4891 
4892       IF (p_doc_level = g_doc_level_HEADER) THEN
4893 
4894          l_progress := '310';
4895          IF g_debug_stmt THEN
4896             PO_DEBUG.debug_stmt(l_log_head,l_progress,'headers');
4897          END IF;
4898 
4899          SELECT PRD.distribution_id
4900          BULK COLLECT INTO x_distribution_id_tbl
4901          FROM
4902             PO_REQUISITION_LINES_ALL PRL
4903          ,  PO_REQ_DISTRIBUTIONS_ALL PRD
4904          ,  PO_SESSION_GT IDS
4905          WHERE PRL.requisition_header_id = IDS.num1
4906          AND PRD.requisition_line_id = PRL.requisition_line_id
4907          AND IDS.key = l_doc_level_id_key
4908          ;
4909 
4910          l_progress := '320';
4911 
4912       ELSIF (p_doc_level = g_doc_level_LINE) THEN
4913 
4914          l_progress := '330';
4915          IF g_debug_stmt THEN
4916             PO_DEBUG.debug_stmt(l_log_head,l_progress,'lines');
4917          END IF;
4918 
4919          SELECT PRD.distribution_id
4920          BULK COLLECT INTO x_distribution_id_tbl
4921          FROM
4922             PO_REQ_DISTRIBUTIONS_ALL PRD
4923          ,  PO_SESSION_GT IDS
4924          WHERE PRD.requisition_line_id = IDS.num1
4925          AND IDS.key = l_doc_level_id_key
4926          ;
4927 
4928          l_progress := '340';
4929 
4930       ELSE
4931          l_progress := '370';
4932          RAISE g_INVALID_CALL_EXC;
4933       END IF;
4934 
4935       l_progress := '390';
4936 
4937    ELSE -- PO, PA, Release, etc.
4938 
4939       l_progress := '400';
4940       IF g_debug_stmt THEN
4941          PO_DEBUG.debug_stmt(l_log_head,l_progress,'not req');
4942       END IF;
4943 
4944       -- Gather all of the distribution ids below this entity level.
4945 
4946       IF (  p_doc_type = g_doc_type_RELEASE
4947             AND p_doc_level = g_doc_level_HEADER
4948          )
4949       THEN
4950 
4951          l_progress := '410';
4952          IF g_debug_stmt THEN
4953             PO_DEBUG.debug_stmt(l_log_head,l_progress,'release');
4954          END IF;
4955 
4956          SELECT POD.po_distribution_id
4957          BULK COLLECT INTO x_distribution_id_tbl
4958          FROM
4959             PO_DISTRIBUTIONS_ALL POD
4960          ,  PO_SESSION_GT IDS
4961          WHERE POD.po_release_id = IDS.num1
4962          AND IDS.key = l_doc_level_id_key
4963          ;
4964 
4965          l_progress := '420';
4966 
4967       ELSIF (p_doc_level = g_doc_level_HEADER) THEN
4968 
4969          l_progress := '430';
4970          IF g_debug_stmt THEN
4971             PO_DEBUG.debug_stmt(l_log_head,l_progress,'headers');
4972          END IF;
4973 
4974          SELECT POD.po_distribution_id
4975          BULK COLLECT INTO x_distribution_id_tbl
4976          FROM
4977             PO_DISTRIBUTIONS_ALL POD
4978          ,  PO_SESSION_GT IDS
4979          WHERE POD.po_header_id = IDS.num1
4980          AND POD.po_release_id IS NULL
4981          -- Don't pick up Release distributions when acting on a PPO/BPA/GA.
4982          -- Not using distribution_type due to dependency issues.
4983          AND IDS.key = l_doc_level_id_key
4984          ;
4985 
4986          l_progress := '440';
4987 
4988       ELSIF (p_doc_level = g_doc_level_LINE) THEN
4989 
4990          l_progress := '450';
4991          IF g_debug_stmt THEN
4992             PO_DEBUG.debug_stmt(l_log_head,l_progress,'lines');
4993          END IF;
4994 
4995          SELECT POD.po_distribution_id
4996          BULK COLLECT INTO x_distribution_id_tbl
4997          FROM
4998             PO_DISTRIBUTIONS_ALL POD
4999          ,  PO_SESSION_GT IDS
5000          WHERE POD.po_line_id = IDS.num1
5001          AND POD.po_release_id IS NULL
5002          -- Don't pick up SR distributions when acting on a PPO.
5003          -- Not using distribution_type due to dependency issues.
5004          AND IDS.key = l_doc_level_id_key
5005          ;
5006 
5007          l_progress := '460';
5008 
5009       ELSIF (p_doc_level = g_doc_level_SHIPMENT) THEN
5010 
5011          l_progress := '470';
5012          IF g_debug_stmt THEN
5013             PO_DEBUG.debug_stmt(l_log_head,l_progress,'shipments');
5014          END IF;
5015 
5016          SELECT POD.po_distribution_id
5017          BULK COLLECT INTO x_distribution_id_tbl
5018          FROM
5019             PO_DISTRIBUTIONS_ALL POD
5020          ,  PO_SESSION_GT IDS
5021          WHERE POD.line_location_id = IDS.num1
5022          AND IDS.key = l_doc_level_id_key
5023          ;
5024 
5025          l_progress := '480';
5026 
5027       ELSE
5028          l_progress := '490';
5029          RAISE g_INVALID_CALL_EXC;
5030       END IF;
5031 
5032       l_progress := '500';
5033 
5034    END IF; -- Req vs. PO/PA/Release
5035 
5036    l_progress := '600';
5037 
5038 END IF; -- entity type <> DISTRIBUTION
5039 
5040 l_progress := '900';
5041 
5042 IF g_debug_stmt THEN
5043    PO_DEBUG.debug_var(l_log_head,l_progress,'x_distribution_id_tbl',x_distribution_id_tbl);
5044    PO_DEBUG.debug_end(l_log_head);
5045 END IF;
5046 
5047 EXCEPTION
5048 WHEN OTHERS THEN
5049    IF g_debug_unexp THEN
5050       PO_DEBUG.debug_exc(l_log_head,l_progress);
5051    END IF;
5052    RAISE;
5053 
5054 END get_distribution_ids;
5055 
5056 
5057 --<Complex Work R12 START>
5058 -------------------------------------------------------------------------------
5059 --Start of Comments
5060 --Name: get_dist_ids_from_archive
5061 --Pre-reqs:
5062 --  None.
5063 --Modifies:
5064 --  None.
5065 --Locks:
5066 --  None.
5067 --Function:
5068 --  Retrieves the ids of distributions below the given doc level for a
5069 --  particular revision of the document from the archive tables
5070 --Parameters:
5071 --IN:
5072 --p_doc_type
5073 --  Document type.  Use the g_doc_type_<> variables, where <> is:
5074 --    PO
5075 --    RELEASE
5076 --p_doc_level
5077 --  The type of ids that are being passed.  Use g_doc_level_<>
5078 --    HEADER
5079 --    LINE
5080 --    SHIPMENT
5081 --    DISTRIBUTION
5082 --p_doc_level_id_tbl
5083 --  Ids of the doc level type with which to find related distributions.
5084 --p_doc_revision_num
5085 --  The revision number of the header in the archive table.
5086 --  If this parameter is passed as null, the latest version in the table
5087 --  is assumed
5088 --OUT:
5089 --x_distribution_id_tbl
5090 --  The ids of the related distributions that were found.
5091 --x_distribution_rev_num_tbl
5092 --  The revision number of the distribution rows in the archive table
5093 --Testing:
5094 --
5095 --End of Comments
5096 -------------------------------------------------------------------------------
5097 PROCEDURE get_dist_ids_from_archive(
5098    p_doc_type                       IN             VARCHAR2
5099 ,  p_doc_level                      IN             VARCHAR2
5100 ,  p_doc_level_id_tbl               IN             po_tbl_number
5101 ,  p_doc_revision_num               IN             NUMBER
5102 ,  x_distribution_id_tbl            OUT NOCOPY     po_tbl_number
5103 ,  x_distribution_rev_num_tbl       OUT NOCOPY     po_tbl_number
5104 )
5105 IS
5106 
5107 l_log_head     CONSTANT VARCHAR2(100) := g_log_head||'GET_DIST_IDS_FROM_ARCHIVE';
5108 l_progress     VARCHAR2(3) := '000';
5109 
5110 l_doc_level_id_key      NUMBER;
5111 l_revision_specified_flag  VARCHAR2(1);
5112 
5113 BEGIN
5114 
5115 IF g_debug_stmt THEN
5116    PO_DEBUG.debug_begin(l_log_head);
5117    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_type', p_doc_type);
5118    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level', p_doc_level);
5119    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level_id_tbl', p_doc_level_id_tbl);
5120    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_revision_num', p_doc_revision_num);
5121 END IF;
5122 
5123   l_progress := '010';
5124 
5125 -----------------------------------------------------------------
5126 --Algorithm:
5127 --
5128 -- -  Insert the doc level ids into the scratchpad table.
5129 -- -  Join to the main doc tables against these doc level ids
5130 --       to retrieve the linked distribution ids.
5131 -----------------------------------------------------------------
5132 
5133    l_progress := '200';
5134    IF g_debug_stmt THEN
5135       PO_DEBUG.debug_stmt(l_log_head,l_progress,'not dists');
5136    END IF;
5137 
5138    -- Load the entity ids into the scratchpad.
5139 
5140    ---------------------------------------
5141    -- PO_SESSION_GT column mapping
5142    --
5143    -- num1     document_level_id
5144    ---------------------------------------
5145 
5146    l_doc_level_id_key := get_session_gt_nextval();
5147 
5148    l_progress := '210';
5149 
5150    FORALL i IN 1 .. p_doc_level_id_tbl.COUNT
5151    INSERT INTO PO_SESSION_GT ( key, num1 )
5152    VALUES ( l_doc_level_id_key, p_doc_level_id_tbl(i) )
5153    ;
5154 
5155    l_progress := '220';
5156 
5157    -- If a specific revision num is not passed in, set a flag to
5158    -- indicate we should use the latest revision in the archive table
5159    IF (p_doc_revision_num IS NULL) THEN
5160      l_revision_specified_flag := 'Y';
5161    ELSE
5162      l_revision_specified_flag := 'N';
5163    END IF;
5164 
5165 
5166    -- We need to derive the appropriate distribution ids for the given
5167    -- entity id and entity level.
5168 
5169    IF (p_doc_type = g_doc_type_REQUISITION) THEN
5170 
5171       l_progress := '300';
5172       IF g_debug_stmt THEN
5173          PO_DEBUG.debug_stmt(l_log_head,l_progress,'requisition');
5174       END IF;
5175 
5176       -- Requisitions are not archived, hence this doc type is not
5177       -- supported by this API
5178       RAISE g_INVALID_CALL_EXC;
5179 
5180    ELSE -- PO, Release
5181 
5182       l_progress := '400';
5183       IF g_debug_stmt THEN
5184          PO_DEBUG.debug_stmt(l_log_head,l_progress,'not req');
5185       END IF;
5186 
5187       -- Gather all of the distribution ids below this entity level.
5188 
5189       IF (  p_doc_type = g_doc_type_RELEASE
5190             AND p_doc_level = g_doc_level_HEADER
5191          )
5192       THEN
5193 
5194          l_progress := '410';
5195          IF g_debug_stmt THEN
5196             PO_DEBUG.debug_stmt(l_log_head,l_progress,'release');
5197          END IF;
5198 
5199          SELECT POD.po_distribution_id
5200               , POD.revision_num
5201          BULK COLLECT INTO
5202                 x_distribution_id_tbl
5203               , x_distribution_rev_num_tbl
5204          FROM
5205             PO_DISTRIBUTIONS_ARCHIVE_ALL POD
5206          ,  PO_SESSION_GT IDS
5207          WHERE POD.po_release_id = IDS.num1
5208          AND IDS.key = l_doc_level_id_key
5209          AND ( (l_revision_specified_flag = 'Y'
5210                 AND POD.latest_external_flag = 'Y')
5211              OR
5212                (l_revision_specified_flag = 'N'
5213                 AND POD.revision_num =
5214                    (SELECT max(POD2.revision_num)
5215                     FROM PO_DISTRIBUTIONS_ARCHIVE_ALL POD2
5216                     WHERE POD2.po_distribution_id = POD.po_distribution_id
5217                     AND POD2.revision_num <= p_doc_revision_num) )
5218               )
5219          ;
5220 
5221          l_progress := '420';
5222 
5223       ELSIF (p_doc_level = g_doc_level_HEADER) THEN
5224 
5225          l_progress := '430';
5226          IF g_debug_stmt THEN
5227             PO_DEBUG.debug_stmt(l_log_head,l_progress,'headers');
5228          END IF;
5229 
5230          SELECT POD.po_distribution_id
5231               , POD.revision_num
5232          BULK COLLECT INTO
5233                 x_distribution_id_tbl
5234               , x_distribution_rev_num_tbl
5235          FROM
5236             PO_DISTRIBUTIONS_ARCHIVE_ALL POD
5237          ,  PO_SESSION_GT IDS
5238          WHERE POD.po_header_id = IDS.num1
5239          AND POD.po_release_id IS NULL
5240          -- Don't pick up Release distributions when acting on a PPO/BPA/GA.
5241          -- Not using distribution_type due to dependency issues.
5242          AND IDS.key = l_doc_level_id_key
5243          AND ( (l_revision_specified_flag = 'Y'
5244                 AND POD.latest_external_flag = 'Y')
5245              OR
5246                (l_revision_specified_flag = 'N'
5247                 AND POD.revision_num =
5248                    (SELECT max(POD2.revision_num)
5249                     FROM PO_DISTRIBUTIONS_ARCHIVE_ALL POD2
5250                     WHERE POD2.po_distribution_id = POD.po_distribution_id
5251                     AND POD2.revision_num <= p_doc_revision_num) )
5252               )
5253          ;
5254 
5255          l_progress := '440';
5256 
5257       ELSIF (p_doc_level = g_doc_level_LINE) THEN
5258 
5259          l_progress := '450';
5260          IF g_debug_stmt THEN
5261             PO_DEBUG.debug_stmt(l_log_head,l_progress,'lines');
5262          END IF;
5263 
5264          SELECT POD.po_distribution_id
5265               , POD.revision_num
5266          BULK COLLECT INTO
5267                 x_distribution_id_tbl
5268               , x_distribution_rev_num_tbl
5269          FROM
5270             PO_DISTRIBUTIONS_ARCHIVE_ALL POD
5271          ,  PO_SESSION_GT IDS
5272          WHERE POD.po_line_id = IDS.num1
5273          AND POD.po_release_id IS NULL
5274          -- Don't pick up SR distributions when acting on a PPO.
5275          -- Not using distribution_type due to dependency issues.
5276          AND IDS.key = l_doc_level_id_key
5277          AND ( (l_revision_specified_flag = 'Y'
5278                 AND POD.latest_external_flag = 'Y')
5279              OR
5280                (l_revision_specified_flag = 'N'
5281                 AND POD.revision_num =
5282                    (SELECT max(POD2.revision_num)
5283                     FROM PO_DISTRIBUTIONS_ARCHIVE_ALL POD2
5284                     WHERE POD2.po_distribution_id = POD.po_distribution_id
5285                     AND POD2.revision_num <= p_doc_revision_num) )
5286               )
5287          ;
5288 
5289          l_progress := '460';
5290 
5291       ELSIF (p_doc_level = g_doc_level_SHIPMENT) THEN
5292 
5293          l_progress := '470';
5294          IF g_debug_stmt THEN
5295             PO_DEBUG.debug_stmt(l_log_head,l_progress,'shipments');
5296          END IF;
5297 
5298          SELECT POD.po_distribution_id
5299               , POD.revision_num
5300          BULK COLLECT INTO
5301                 x_distribution_id_tbl
5302               , x_distribution_rev_num_tbl
5303          FROM
5304             PO_DISTRIBUTIONS_ARCHIVE_ALL POD
5305          ,  PO_SESSION_GT IDS
5306          WHERE POD.line_location_id = IDS.num1
5307          AND IDS.key = l_doc_level_id_key
5308          AND ( (l_revision_specified_flag = 'Y'
5309                 AND POD.latest_external_flag = 'Y')
5310              OR
5311                (l_revision_specified_flag = 'N'
5312                 AND POD.revision_num =
5313                    (SELECT max(POD2.revision_num)
5314                     FROM PO_DISTRIBUTIONS_ARCHIVE_ALL POD2
5315                     WHERE POD2.po_distribution_id = POD.po_distribution_id
5316                     AND POD2.revision_num <= p_doc_revision_num) )
5317               )
5318          ;
5319 
5320          l_progress := '480';
5321 
5322       ELSIF (p_doc_level = g_doc_level_DISTRIBUTION) THEN
5323 
5324          l_progress := '490';
5325          IF g_debug_stmt THEN
5326             PO_DEBUG.debug_stmt(l_log_head,l_progress,'distributions');
5327          END IF;
5328 
5329          SELECT POD.po_distribution_id
5330               , POD.revision_num
5331          BULK COLLECT INTO
5332                 x_distribution_id_tbl
5333               , x_distribution_rev_num_tbl
5334          FROM
5335             PO_DISTRIBUTIONS_ARCHIVE_ALL POD
5336          ,  PO_SESSION_GT IDS
5337          WHERE POD.po_distribution_id = IDS.num1
5338          AND IDS.key = l_doc_level_id_key
5339          AND ( (l_revision_specified_flag = 'Y'
5340                 AND POD.latest_external_flag = 'Y')
5341              OR
5342                (l_revision_specified_flag = 'N'
5343                 AND POD.revision_num =
5344                    (SELECT max(POD2.revision_num)
5345                     FROM PO_DISTRIBUTIONS_ARCHIVE_ALL POD2
5346                     WHERE POD2.po_distribution_id = POD.po_distribution_id
5347                     AND POD2.revision_num <= p_doc_revision_num) )
5348               )
5349          ;
5350 
5351          l_progress := '500';
5352 
5353       ELSE
5354          l_progress := '510';
5355          RAISE g_INVALID_CALL_EXC;
5356       END IF;
5357 
5358       l_progress := '520';
5359 
5360    END IF; -- Req vs. PO/Release
5361 
5362    l_progress := '600';
5363 
5364 
5365 IF g_debug_stmt THEN
5366    PO_DEBUG.debug_var(l_log_head,l_progress,'x_distribution_id_tbl',x_distribution_id_tbl);
5367    PO_DEBUG.debug_var(l_log_head,l_progress,'x_distribution_rev_num_tbl',x_distribution_rev_num_tbl);
5368    PO_DEBUG.debug_end(l_log_head);
5369 END IF;
5370 
5371 EXCEPTION
5372 WHEN OTHERS THEN
5373    IF g_debug_unexp THEN
5374       PO_DEBUG.debug_exc(l_log_head,l_progress);
5375    END IF;
5376    RAISE;
5377 
5378 END get_dist_ids_from_archive;
5379 --<Complex Work R12 END>
5380 
5381 
5382 ------------------------------------------------------------------------------
5383 --Start of Comments
5384 --Name: is_encumbrance_on
5385 --Pre-reqs:
5386 --  Org context may need to be set prior to calling.
5387 --Modifies:
5388 --  None.
5389 --Locks:
5390 --  None.
5391 --Function:
5392 --  This procedure checks if encumbrance is ON for a document type in a
5393 --  specified org.
5394 --Parameters:
5395 --IN:
5396 --p_doc_type
5397 --  The type of doc to check.  Use g_doc_type_<> where <> is:
5398 --    REQUISITION       - req enc
5399 --    PO                - purch enc
5400 --    RELEASE           - purch enc
5401 --    PA                - both req and purch enc are on
5402 --    ANY               - either req or purch enc or both are on
5403 --p_org_id
5404 --  The org id to check the encumbrance status in.
5405 --  If NULL is passed, the org context is assumed to have been set
5406 --  by the caller.
5407 --Returns:
5408 --  FALSE   the encumbrance for p_doc_type is NOT on
5409 --  TRUE    the encumbrance for p_doc_type is on
5410 --Testing:
5411 --
5412 --End of Comments
5413 -------------------------------------------------------------------------------
5414 FUNCTION is_encumbrance_on(
5415    p_doc_type                       IN             VARCHAR2
5416 ,  p_org_id                         IN             NUMBER
5417 )  RETURN BOOLEAN
5418 IS
5419 
5420 l_log_head     CONSTANT VARCHAR2(100) := g_log_head||'IS_ENCUMBRANCE_ON';
5421 l_progress     VARCHAR2(3) := '000';
5422 
5423 l_req_enc_flag       FINANCIALS_SYSTEM_PARAMS_ALL.req_encumbrance_flag%TYPE;
5424 l_purch_enc_flag     FINANCIALS_SYSTEM_PARAMS_ALL.purch_encumbrance_flag%TYPE;
5425 
5426 l_req_encumbrance_on    BOOLEAN;
5427 l_po_encumbrance_on  BOOLEAN;
5428 
5429 l_enc_on    BOOLEAN;
5430 
5431 BEGIN
5432 
5433 IF g_debug_stmt THEN
5434    PO_DEBUG.debug_begin(l_log_head);
5435    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_type', p_doc_type);
5436    PO_DEBUG.debug_var(l_log_head,l_progress,'p_org_id', p_org_id);
5437 END IF;
5438 
5439 l_progress := '010';
5440 
5441 -- Get the FSP encumbrance status values.
5442 -- If org id is not passed in, use the org-striped table,
5443 -- otherwise use the _ALL table.
5444 
5445 IF (p_org_id IS NULL) THEN
5446 
5447    l_progress := '020';
5448    IF g_debug_stmt THEN
5449       PO_DEBUG.debug_stmt(l_log_head,l_progress,'using FINANCIALS_SYSTEM_PARAMETERS');
5450    END IF;
5451 
5452    SELECT FSP.req_encumbrance_flag, FSP.purch_encumbrance_flag
5453    INTO l_req_enc_flag, l_purch_enc_flag
5454    FROM FINANCIALS_SYSTEM_PARAMETERS FSP
5455    ;
5456 
5457    l_progress := '030';
5458 
5459 ELSE
5460 
5461    l_progress := '040';
5462    IF g_debug_stmt THEN
5463       PO_DEBUG.debug_stmt(l_log_head,l_progress,'using FINANCIALS_SYSTEM_PARAMS_ALL');
5464    END IF;
5465 
5466    SELECT FSP.req_encumbrance_flag, FSP.purch_encumbrance_flag
5467    INTO l_req_enc_flag, l_purch_enc_flag
5468    FROM FINANCIALS_SYSTEM_PARAMS_ALL FSP
5469    WHERE FSP.org_id = p_org_id
5470    ;
5471 
5472    l_progress := '050';
5473 
5474 END IF;
5475 
5476 l_progress := '060';
5477 
5478 IF g_debug_stmt THEN
5479    PO_DEBUG.debug_var(l_log_head,l_progress,'l_req_enc_flag',l_req_enc_flag);
5480    PO_DEBUG.debug_var(l_log_head,l_progress,'l_purch_enc_flag',l_purch_enc_flag);
5481 END IF;
5482 
5483 -- Set the vars for encumbrance checking.
5484 
5485 IF (l_req_enc_flag = 'Y') THEN
5486    l_req_encumbrance_on := TRUE;
5487 ELSE
5488    l_req_encumbrance_on := FALSE;
5489 END IF;
5490 
5491 l_progress := '070';
5492 
5493 IF (l_purch_enc_flag = 'Y') THEN
5494    l_po_encumbrance_on := TRUE;
5495 ELSE
5496    l_po_encumbrance_on := FALSE;
5497 END IF;
5498 
5499 l_progress := '080';
5500 
5501 IF g_debug_stmt THEN
5502    PO_DEBUG.debug_var(l_log_head,l_progress,'l_req_encumbrance_on',l_req_encumbrance_on);
5503    PO_DEBUG.debug_var(l_log_head,l_progress,'l_po_encumbrance_on',l_po_encumbrance_on);
5504 END IF;
5505 
5506 -- Set the return value for the appropriate doc type.
5507 
5508 IF (p_doc_type = g_doc_type_REQUISITION) THEN
5509 
5510    l_progress := '100';
5511 
5512    l_enc_on := l_req_encumbrance_on;
5513 
5514 ELSIF (p_doc_type IN (g_doc_type_PO, g_doc_type_RELEASE)) THEN
5515 
5516    l_progress := '110';
5517 
5518    l_enc_on := l_po_encumbrance_on;
5519 
5520 ELSIF (p_doc_type = g_doc_type_PA) THEN
5521 
5522    l_progress := '120';
5523 
5524    l_enc_on := (l_req_encumbrance_on AND l_po_encumbrance_on);
5525 
5526 ELSIF (p_doc_type = g_doc_type_ANY) THEN
5527 
5528    l_progress := '130';
5529 
5530    l_enc_on := (l_req_encumbrance_on OR l_po_encumbrance_on);
5531 
5532 ELSE
5533    l_progress := '170';
5534    RAISE g_INVALID_CALL_EXC;
5535 END IF;
5536 
5537 l_progress := '900';
5538 
5539 IF g_debug_stmt THEN
5540    PO_DEBUG.debug_var(l_log_head,l_progress,'l_enc_on',l_enc_on);
5541    PO_DEBUG.debug_end(l_log_head);
5542 END IF;
5543 
5544 RETURN(l_enc_on);
5545 
5546 EXCEPTION
5547 WHEN OTHERS THEN
5548    IF g_debug_unexp THEN
5549       PO_DEBUG.debug_var(l_log_head,l_progress,'l_enc_on',l_enc_on);
5550       PO_DEBUG.debug_exc(l_log_head,l_progress);
5551    END IF;
5552    RAISE;
5553 
5554 END is_encumbrance_on;
5555 
5556 
5557 -----------------------------------------------------------------<SERVICES FPJ>
5558 -------------------------------------------------------------------------------
5559 --Start of Comments
5560 --Name: get_translated_text
5561 --Pre-reqs:
5562 --  None.
5563 --Modifies:
5564 --  None.
5565 --Locks:
5566 --  None.
5567 --Function:
5568 --  This function takes in a message name and token-value pairs and returns
5569 --  the translated text as a VARCHAR2 String.
5570 --Parameters:
5571 --IN:
5572 --p_message_name
5573 --  Name of message in Message Dictionary.
5574 --p_token1
5575 --  Name of token variable in the message (only applies if a token exists).
5576 --p_value1
5577 --  Value to subsitute for token (only applies if a token exists).
5578 --Returns:
5579 --  VARCHAR2 - The translated and token-substituted message.
5580 --Testing:
5581 --  None.
5582 --End of Comments
5583 -------------------------------------------------------------------------------
5584 -------------------------------------------------------------------------------
5585 FUNCTION get_translated_text
5586 (   p_message_name    IN  VARCHAR2
5587 ,   p_token1          IN  VARCHAR2 -- := NULL
5588 ,   p_value1          IN  VARCHAR2 -- := NULL
5589 ,   p_token2          IN  VARCHAR2 -- := NULL
5590 ,   p_value2          IN  VARCHAR2 -- := NULL
5591 ,   p_token3          IN  VARCHAR2 -- := NULL
5592 ,   p_value3          IN  VARCHAR2 -- := NULL
5593 ,   p_token4          IN  VARCHAR2 -- := NULL
5594 ,   p_value4          IN  VARCHAR2 -- := NULL
5595 ,   p_token5          IN  VARCHAR2 -- := NULL
5596 ,   p_value5          IN  VARCHAR2 -- := NULL
5597 ) RETURN VARCHAR2
5598 IS
5599     l_application_name        VARCHAR2(3) := 'PO';
5600 
5601 BEGIN
5602 
5603     ---------------------------------------------------------------------------
5604     -- Set Message on Stack ---------------------------------------------------
5605     ---------------------------------------------------------------------------
5606 
5607     FND_MESSAGE.set_name(l_application_name, p_message_name);
5608 
5609     ---------------------------------------------------------------------------
5610     -- Substitute Tokens ------------------------------------------------------
5611     ---------------------------------------------------------------------------
5612 
5613     IF ( p_token1 IS NOT NULL ) THEN
5614         FND_MESSAGE.set_token(p_token1, p_value1);
5615     END IF;
5616 
5617     IF ( p_token2 IS NOT NULL ) THEN
5618         FND_MESSAGE.set_token(p_token2, p_value2);
5619     END IF;
5620 
5621     IF ( p_token3 IS NOT NULL ) THEN
5622         FND_MESSAGE.set_token(p_token3, p_value3);
5623     END IF;
5624 
5625     IF ( p_token4 IS NOT NULL ) THEN
5626         FND_MESSAGE.set_token(p_token4, p_value4);
5627     END IF;
5628 
5629     IF ( p_token5 IS NOT NULL ) THEN
5630         FND_MESSAGE.set_token(p_token5, p_value5);
5631     END IF;
5632 
5633     ---------------------------------------------------------------------------
5634     -- Return Translated Message ----------------------------------------------
5635     ---------------------------------------------------------------------------
5636 
5637     return (FND_MESSAGE.get);
5638 
5639     ---------------------------------------------------------------------------
5640 
5641 END get_translated_text;
5642 
5643 --<Shared Proc FPJ START>
5644 -------------------------------------------------------------------------------
5645 --Start of Comments
5646 --Name: CHECK_DOC_NUMBER_UNIQUE
5647 --Pre-reqs:
5648 --  None
5649 --Modifies:
5650 --  None.
5651 --Locks:
5652 --  None.
5653 --Function:
5654 --   Returns boolean indicating whether a segment1 value with given type lookup
5655 --   code passed in is unique in given operating unit. The uniqueness test is
5656 --   done in PO trasaction and history tables. The uniqueness test spans Sourcing's
5657 --   transaction tables also.
5658 --Parameters:
5659 --IN:
5660 --  p_segment1
5661 --     The doc number whose uniqueness needs to be tested
5662 --  p_org_id
5663 --     The operating unit where the uniqueness needs to be tested
5664 --  p_type_lookup_code
5665 --     The lookup code of the document. Valid values are 'STANDARD', 'PLANNED',
5666 --     'CONTRACT','BLANKET', 'RFQ', 'QUOTATION'
5667 --Testing:
5668 --  None
5669 --End of Comments
5670 ---------------------------------------------------------------------------
5671  FUNCTION Check_Doc_Number_Unique(p_Segment1 In VARCHAR2,
5672                                   p_org_id IN VARCHAR2,
5673 			          p_Type_lookup_code IN VARCHAR2)
5674  RETURN boolean  is
5675 
5676  l_Unique             boolean;
5677  l_non_unique_seg1    Varchar2(20);
5678  l_api_name           CONSTANT VARCHAR2(30) := 'Check_Doc_Number_Unique';
5679  l_progress           varchar2(3) := '000';
5680  l_duplicate_exists   varchar2(1);
5681  l_pon_install_status varchar2(1);
5682  l_status             varchar2(10);
5683  BEGIN
5684 
5685     IF p_Type_lookup_code NOT IN ('RFQ', 'QUOTATION') THEN
5686 
5687         l_progress := '010';
5688 
5689         SELECT 'N'
5690         into l_duplicate_exists
5691         from sys.dual
5692         where not exists
5693           (SELECT 'po number is not unique'
5694            FROM   po_headers_all ph
5695            WHERE  ph.segment1 = p_segment1
5696            AND    ph.type_lookup_code IN
5697                   ('STANDARD','CONTRACT','BLANKET','PLANNED')
5698            AND    nvl(ph.org_id, -99) = nvl(p_org_id, -99));
5699 
5700         l_Progress := '020';
5701 
5702         SELECT 'N'
5703         into l_duplicate_exists
5704         from sys.dual
5705         where not exists
5706            (SELECT 'po number is not unique'
5707            FROM   po_history_pos_all ph
5708            WHERE  ph.segment1 = p_segment1
5709            AND    ph.type_lookup_code IN
5710                 ('STANDARD','CONTRACT','BLANKET','PLANNED')
5711            AND    nvl(ph.org_id, -99) = nvl(p_org_id, -99));
5712 
5713       --Get the install status of Sourcing
5714       po_setup_s1.get_sourcing_startup(l_pon_install_status);
5715 
5716       if nvl(l_pon_install_status,'N') ='I' then
5717 	   if p_Type_lookup_code in ('STANDARD','BLANKET') then
5718 	      pon_auction_po_pkg.check_unique(p_org_id,p_segment1,l_status);
5719 	      if l_status = 'SUCCESS' then
5720 		    l_Unique :=TRUE;
5721           else
5722 		    raise no_data_found;
5723           end if;
5724         end if;
5725       end if;
5726 
5727         l_Unique:= TRUE;
5728 
5729        return(l_Unique);
5730 
5731     ELSIF  (p_Type_lookup_code = 'RFQ') THEN
5732 
5733 	-- RFQ specific processing
5734 
5735         l_progress := '030';
5736 
5737         SELECT 'N'
5738         into l_duplicate_exists
5739         from sys.dual
5740         where not exists
5741           (SELECT 'rfq number is not unique'
5742            FROM   po_headers_all ph
5743            WHERE  ph.segment1 = p_segment1
5744            AND    ph.type_lookup_code = 'RFQ'
5745            AND    nvl(ph.org_id, -99) = nvl(p_org_id, -99));
5746 
5747         l_Progress := '040';
5748 
5749         SELECT 'N'
5750         into l_duplicate_exists
5751         from sys.dual
5752         where not exists
5753            (SELECT 'rfq number is not unique'
5754            FROM   po_history_pos_all ph
5755            WHERE  ph.segment1 = p_segment1
5756            AND    ph.type_lookup_code = 'RFQ'
5757            AND    nvl(ph.org_id, -99) = nvl(p_org_id, -99));
5758 
5759         l_Unique:= TRUE;
5760 
5761         return(l_Unique);
5762 
5763     ELSIF  (p_Type_lookup_code = 'QUOTATION') THEN
5764 
5765 	-- Quotation specific processing
5766 
5767         l_progress := '050';
5768 
5769         SELECT 'N'
5770         into l_duplicate_exists
5771         from sys.dual
5772         where not exists
5773           (SELECT 'quote number is not unique'
5774            FROM   po_headers_all ph
5775            WHERE  ph.segment1 = p_segment1
5776            AND    ph.type_lookup_code = 'QUOTATION'
5777            AND    nvl(ph.org_id, -99) = nvl(p_org_id, -99));
5778 
5779         l_Progress := '060';
5780 
5781         SELECT 'N'
5782         into l_duplicate_exists
5783         from sys.dual
5784         where not exists
5785            (SELECT 'quote number is not unique'
5786            FROM   po_history_pos_all ph
5787            WHERE  ph.segment1 = p_segment1
5788            AND    ph.type_lookup_code = 'QUOTATION'
5789            AND    nvl(ph.org_id, -99) = nvl(p_org_id, -99));
5790 
5791         l_Unique:= TRUE;
5792 
5793         return(l_Unique);
5794 
5795     END IF;
5796 
5797 EXCEPTION
5798         WHEN NO_DATA_FOUND THEN
5799              --Bug 3417966 No need to set the message here
5800              --fnd_message.set_name('PO', 'PO_ALL_ENTER_UNIQUE_VAL');
5801              l_Unique:= FALSE;
5802              RETURN(l_Unique);
5803         WHEN OTHERS THEN
5804             l_unique := FALSE;
5805             IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_unexp_error)
5806             THEN
5807               fnd_msg_pub.add_exc_msg (g_pkg_name, l_api_name,
5808                   SUBSTRB (SQLERRM , 1 , 200) || ' at location ' || l_progress);
5809             END IF;
5810             RETURN(l_Unique);
5811 END Check_doc_number_Unique;
5812 
5813 --------------------------------------------------------------------------------
5814 --Start of Comments
5815 --Name: check_inv_org_in_sob
5816 --Pre-reqs:
5817 --  None.
5818 --Modifies:
5819 --  FND_LOG
5820 --  FND_MSG_PUB
5821 --Locks:
5822 --  None.
5823 --Function:
5824 --  Checks if p_inv_org_id is in the Set of Books p_sob_id. If p_sob_id
5825 --  is NULL, then defaults p_sob_id to be the current OU's SOB.  Appends to the
5826 --  API message list upon error.
5827 --Parameters:
5828 --IN:
5829 --p_inv_org_id
5830 --  The inventory organization ID.
5831 --p_sob_id
5832 --  The set of books ID, or NULL to default the current OU's SOB.
5833 --OUT:
5834 --x_return_status
5835 --  FND_API.g_ret_sts_success - if the procedure completed successfully
5836 --  FND_API.g_ret_sts_unexp_error - unexpected error occurred
5837 --x_in_sob
5838 --  TRUE if p_inv_org_id is within the set of books p_sob_id.
5839 --  FALSE otherwise.
5840 --End of Comments
5841 --------------------------------------------------------------------------------
5842 PROCEDURE check_inv_org_in_sob
5843 (
5844     x_return_status OUT NOCOPY VARCHAR2,
5845     p_inv_org_id    IN  NUMBER,
5846     p_sob_id        IN  NUMBER,
5847     x_in_sob        OUT NOCOPY BOOLEAN
5848 )
5849 IS
5850     l_progress VARCHAR2(3);
5851     l_in_sob VARCHAR2(1);
5852 BEGIN
5853     l_progress := '000';
5854     x_return_status := FND_API.g_ret_sts_success;
5855 
5856     IF g_debug_stmt THEN
5857         PO_DEBUG.debug_stmt
5858            (p_log_head => g_log_head||'check_inv_org_in_sob',
5859             p_token    => 'invoked',
5860             p_message  => 'inv org ID: '||p_inv_org_id||' sob ID: '||p_sob_id);
5861     END IF;
5862 
5863     BEGIN
5864 
5865         IF (p_sob_id IS NULL) THEN
5866             l_progress := '010';
5867 
5868             --SQL What: Check if inv org p_inv_org_id is in current set of books
5869             --SQL Why: Outcome determines output parameter x_in_sob
5870             SELECT 'Y'
5871               INTO l_in_sob
5872               FROM financials_system_parameters fsp,
5873                    hr_organization_information hoi,
5874                    mtl_parameters mp
5875              WHERE mp.organization_id = p_inv_org_id
5876                AND mp.organization_id = hoi.organization_id
5877                AND hoi.org_information_context = 'Accounting Information'
5878                AND hoi.org_information1 = TO_CHAR(fsp.set_of_books_id);
5879         ELSE
5880             l_progress := '020';
5881 
5882             --SQL What: Check if inv org p_inv_org_id is in SOB p_sob_id
5883             --SQL Why: Outcome determines output parameter x_in_sob
5884             SELECT 'Y'
5885               INTO l_in_sob
5886               FROM hr_organization_information hoi,
5887                    mtl_parameters mp
5888              WHERE mp.organization_id = p_inv_org_id
5889                AND mp.organization_id = hoi.organization_id
5890                AND hoi.org_information_context = 'Accounting Information'
5891                AND hoi.org_information1 = TO_CHAR(p_sob_id);
5892         END IF;
5893 
5894         -- Successful select means inv org is in the SOB.
5895         x_in_sob := TRUE;
5896 
5897     EXCEPTION
5898         WHEN NO_DATA_FOUND THEN
5899             l_progress := '030';
5900             x_in_sob := FALSE;
5901     END;
5902 
5903     IF g_debug_stmt THEN
5904         PO_DEBUG.debug_var
5905            (p_log_head => g_log_head||'check_inv_org_in_sob',
5906             p_progress => l_progress,
5907             p_name     => 'x_in_sob',
5908             p_value    => x_in_sob);
5909     END IF;
5910 
5911 EXCEPTION
5912     WHEN OTHERS THEN
5913         x_return_status := FND_API.g_ret_sts_unexp_error;
5914         x_in_sob := FALSE;
5915         FND_MSG_PUB.add_exc_msg(p_pkg_name       => g_pkg_name,
5916                                 p_procedure_name => 'check_inv_org_in_sob',
5917                                 p_error_text     => 'Progress: '||l_progress||
5918                                            ' Error: '||SUBSTRB(SQLERRM,1,215));
5919         IF g_debug_unexp THEN
5920             PO_DEBUG.debug_exc
5921                (p_log_head => g_log_head||'check_inv_org_in_sob',
5922                 p_progress => l_progress);
5923         END IF;
5924 END check_inv_org_in_sob;
5925 
5926 --------------------------------------------------------------------------------
5927 --Start of Comments
5928 --Name: get_inv_org_ou_id
5929 --Pre-reqs:
5930 --  None.
5931 --Modifies:
5932 --  FND_LOG
5933 --  FND_MSG_PUB
5934 --Locks:
5935 --  None.
5936 --Function:
5937 --  Gets the operating unit associated with p_inv_org_id.  If p_inv_org_id is
5938 --  NULL, then just return a NULL x_ou_id. Appends to the API message list upon
5939 --  error.
5940 --Parameters:
5941 --IN:
5942 --p_inv_org_id
5943 --  The inventory organization ID.
5944 --OUT:
5945 --x_return_status
5946 --  FND_API.g_ret_sts_success - if the procedure completed successfully
5947 --  FND_API.g_ret_sts_unexp_error - unexpected error occurred
5948 --x_ou_id
5949 --  The operating unit ID associated with the inventory org p_inv_org_id. This
5950 --  will be NULL if p_inv_org_id is passed in as NULL.
5951 --End of Comments
5952 --------------------------------------------------------------------------------
5953 PROCEDURE get_inv_org_ou_id
5954 (
5955     x_return_status OUT NOCOPY VARCHAR2,
5956     p_inv_org_id    IN  NUMBER,
5957     x_ou_id         OUT NOCOPY NUMBER
5958 )
5959 IS
5960     l_progress VARCHAR2(3);
5961 BEGIN
5962     l_progress := '000';
5963     x_return_status := FND_API.g_ret_sts_success;
5964 
5965     IF g_debug_stmt THEN
5966         PO_DEBUG.debug_stmt
5967            (p_log_head => g_log_head||'get_inv_org_ou_id',
5968             p_token    => 'invoked',
5969             p_message  => 'inv org ID: '||p_inv_org_id);
5970     END IF;
5971 
5972     --< Bug 3370735 Start >
5973     IF (p_inv_org_id IS NULL) THEN
5974         -- Null out x_ou_id and return when the inv org ID is NULL
5975         x_ou_id := NULL;
5976         RETURN;
5977     END IF;
5978     --< Bug 3370735 Start >
5979 
5980     l_progress := '010';
5981 
5982     --SQL What: Get the operating unit associated with p_inv_org_id
5983     --SQL Why: Return value as output parameter x_ou_id
5984     SELECT TO_NUMBER(hoi.org_information3)
5985       INTO x_ou_id
5986       FROM hr_organization_information hoi,
5987            mtl_parameters mp
5988      WHERE mp.organization_id = p_inv_org_id
5989        AND mp.organization_id = hoi.organization_id
5990        AND hoi.org_information_context = 'Accounting Information';
5991 
5992     l_progress := '020';
5993 
5994     IF g_debug_stmt THEN
5995         PO_DEBUG.debug_var
5996            (p_log_head => g_log_head||'get_inv_org_ou_id',
5997             p_progress => l_progress,
5998             p_name     => 'x_ou_id',
5999             p_value    => x_ou_id);
6000     END IF;
6001 
6002 EXCEPTION
6003     WHEN OTHERS THEN
6004         x_return_status := FND_API.g_ret_sts_unexp_error;
6005         FND_MSG_PUB.add_exc_msg(p_pkg_name       => g_pkg_name,
6006                                 p_procedure_name => 'get_inv_org_ou_id',
6007                                 p_error_text     => 'Progress: '||l_progress||
6008                                            ' Error: '||SUBSTRB(SQLERRM,1,215));
6009         IF g_debug_unexp THEN
6010             PO_DEBUG.debug_exc
6011                (p_log_head => g_log_head||'get_inv_org_ou_id',
6012                 p_progress => l_progress);
6013         END IF;
6014 END get_inv_org_ou_id;
6015 
6016 --------------------------------------------------------------------------------
6017 --Start of Comments
6018 --Name: get_inv_org_sob_id
6019 --Pre-reqs:
6020 --  None.
6021 --Modifies:
6022 --  FND_LOG
6023 --  FND_MSG_PUB
6024 --Locks:
6025 --  None.
6026 --Function:
6027 --  Gets the set of books ID associated with p_inv_org_id.  Appends to the
6028 --  API message list upon error.
6029 --Parameters:
6030 --IN:
6031 --p_inv_org_id
6032 --  The inventory organization ID.
6033 --OUT:
6034 --x_return_status
6035 --  FND_API.g_ret_sts_success - if the procedure completed successfully
6036 --  FND_API.g_ret_sts_unexp_error - unexpected error occurred
6037 --x_sob_id
6038 --  The set of books ID associated with the inventory org p_inv_org_id.
6039 --End of Comments
6040 --------------------------------------------------------------------------------
6041 PROCEDURE get_inv_org_sob_id
6042 (
6043     x_return_status OUT NOCOPY VARCHAR2,
6044     p_inv_org_id    IN  NUMBER,
6045     x_sob_id        OUT NOCOPY NUMBER
6046 )
6047 IS
6048     l_progress VARCHAR2(3);
6049 BEGIN
6050     l_progress := '000';
6051     x_return_status := FND_API.g_ret_sts_success;
6052 
6053     IF g_debug_stmt THEN
6054         PO_DEBUG.debug_stmt
6055            (p_log_head => g_log_head||'get_inv_org_sob_id',
6056             p_token    => 'invoked',
6057             p_message  => 'inv org ID: '||p_inv_org_id);
6058     END IF;
6059 
6060     l_progress := '010';
6061 
6062     --SQL What: Get the set of books ID associated with p_inv_org_id
6063     --SQL Why: Return value as output parameter x_sob_id
6064     SELECT TO_NUMBER(hoi.org_information1)
6065       INTO x_sob_id
6066       FROM hr_organization_information hoi,
6067            mtl_parameters mp
6068      WHERE mp.organization_id = p_inv_org_id
6069        AND mp.organization_id = hoi.organization_id
6070        AND hoi.org_information_context = 'Accounting Information';
6071 
6072     l_progress := '020';
6073 
6074     IF g_debug_stmt THEN
6075         PO_DEBUG.debug_var
6076            (p_log_head => g_log_head||'get_inv_org_sob_id',
6077             p_progress => l_progress,
6078             p_name     => 'x_sob_id',
6079             p_value    => x_sob_id);
6080     END IF;
6081 
6082 EXCEPTION
6083     WHEN OTHERS THEN
6084         x_return_status := FND_API.g_ret_sts_unexp_error;
6085         FND_MSG_PUB.add_exc_msg(p_pkg_name       => g_pkg_name,
6086                                 p_procedure_name => 'get_inv_org_sob_id',
6087                                 p_error_text     => 'Progress: '||l_progress||
6088                                            ' Error: '||SUBSTRB(SQLERRM,1,215));
6089         IF g_debug_unexp THEN
6090             PO_DEBUG.debug_exc
6091                (p_log_head => g_log_head||'get_inv_org_sob_id',
6092                 p_progress => l_progress);
6093         END IF;
6094 END get_inv_org_sob_id;
6095 
6096 --------------------------------------------------------------------------------
6097 --Start of Comments
6098 --Name: get_inv_org_info
6099 --Pre-reqs:
6100 --  None.
6101 --Modifies:
6102 --  FND_LOG
6103 --  FND_MSG_PUB
6104 --Locks:
6105 --  None.
6106 --Function:
6107 --  Gets the following information associated with p_inv_org_id:
6108 --      business group ID
6109 --      set of books ID
6110 --      chart of accounts ID
6111 --      operating unit ID
6112 --      legal entity ID
6113 --  Appends to the API message list upon error.
6114 --Parameters:
6115 --IN:
6116 --p_inv_org_id
6117 --  The inventory organization ID.
6118 --OUT:
6119 --x_return_status
6120 --  FND_API.g_ret_sts_success - if the procedure completed successfully
6121 --  FND_API.g_ret_sts_unexp_error - unexpected error occurred
6122 --x_business_group_id
6123 --  The business group ID associated with the inventory org p_inv_org_id.
6124 --x_set_of_books_id
6125 --  The set of books ID associated with the inventory org p_inv_org_id.
6126 --x_chart_of_accounts_id
6127 --  The chart of accounts ID associated with the inventory org p_inv_org_id.
6128 --x_operating_unit_id
6129 --  The operating unit ID associated with the inventory org p_inv_org_id.
6130 --x_legal_entity_id
6131 --  The legal entity ID associated with the inventory org p_inv_org_id.
6132 --End of Comments
6133 --------------------------------------------------------------------------------
6134 PROCEDURE get_inv_org_info
6135 (
6136     x_return_status         OUT NOCOPY VARCHAR2,
6137     p_inv_org_id            IN  NUMBER,
6138     x_business_group_id     OUT NOCOPY NUMBER,
6139     x_set_of_books_id       OUT NOCOPY NUMBER,
6140     x_chart_of_accounts_id  OUT NOCOPY NUMBER,
6141     x_operating_unit_id     OUT NOCOPY NUMBER,
6142     x_legal_entity_id       OUT NOCOPY NUMBER
6143 )
6144 IS
6145     l_progress VARCHAR2(3);
6146 BEGIN
6147     l_progress := '000';
6148     x_return_status := FND_API.g_ret_sts_success;
6149 
6150     IF g_debug_stmt THEN
6151         PO_DEBUG.debug_stmt
6152            (p_log_head => g_log_head||'get_inv_org_info',
6153             p_token    => 'invoked',
6154             p_message  => 'inv org ID: '||p_inv_org_id);
6155     END IF;
6156 
6157     l_progress := '010';
6158 
6159     --SQL What: Get the important ID's associated with p_inv_org_id. These ID's
6160     --  are the same as those returned by ORG_ORGANIZATION_DEFINITIONS.
6161     --SQL Why: Return value of ID's as output parameters to procedure.
6162     SELECT haou.business_group_id,
6163            gsob.set_of_books_id,
6164            gsob.chart_of_accounts_id,
6165            TO_NUMBER(hoi.org_information3),
6166            TO_NUMBER(hoi.org_information2)
6167       INTO x_business_group_id,
6168            x_set_of_books_id,
6169            x_chart_of_accounts_id,
6170            x_operating_unit_id,
6171            x_legal_entity_id
6172       FROM hr_organization_information hoi,
6173            hr_all_organization_units haou,
6174            mtl_parameters mp,
6175            gl_sets_of_books gsob
6176      WHERE mp.organization_id = p_inv_org_id
6177        AND mp.organization_id = haou.organization_id
6178        AND haou.organization_id = hoi.organization_id
6179        AND hoi.org_information_context = 'Accounting Information'
6180        AND TO_NUMBER(hoi.org_information1) = gsob.set_of_books_id;
6181 
6182     l_progress := '020';
6183 
6184     IF g_debug_stmt THEN
6185         PO_DEBUG.debug_stmt
6186            (p_log_head => g_log_head||'get_inv_org_info',
6187             p_token    => 'output',
6188             p_message  => 'bg ID: '||x_business_group_id||' sob ID: '||
6189                 x_set_of_books_id||' coa ID: '||x_chart_of_accounts_id||
6190                 ' ou ID: '||x_operating_unit_id||' le ID: '||
6191                 x_legal_entity_id);
6192     END IF;
6193 
6194 EXCEPTION
6195     WHEN OTHERS THEN
6196         x_return_status := FND_API.g_ret_sts_unexp_error;
6197         FND_MSG_PUB.add_exc_msg(p_pkg_name       => g_pkg_name,
6198                                 p_procedure_name => 'get_inv_org_info',
6199                                 p_error_text     => 'Progress: '||l_progress||
6200                                            ' Error: '||SUBSTRB(SQLERRM,1,215));
6201         IF g_debug_unexp THEN
6202             PO_DEBUG.debug_exc
6203                (p_log_head => g_log_head||'get_inv_org_info',
6204                 p_progress => l_progress);
6205         END IF;
6206 END get_inv_org_info;
6207 
6208 --<Shared Proc FPJ END>
6209 
6210 
6211 
6212 --------------------------------------------------------------------------------
6213 --Start of Comments
6214 --Name: get_open_encumbrance_stats
6215 --Pre-reqs:
6216 --  None.
6217 --Modifies:
6218 --  None.
6219 --Locks:
6220 --  None.
6221 --Function:
6222 --  Classifies the distributions below the given entity that are not
6223 --  cancelled or finally closed.
6224 --  This procedure is being used by functions that are embedded in SQL
6225 --  statements in other products (views, etc.), so it is not allowed
6226 --  to modify anything (except in an autonomous transaction).
6227 --  Because of this restriction, the utility procedures that use
6228 --  global temp tables cannot be leveraged.
6229 --Parameters:
6230 --IN:
6231 --p_doc_type
6232 --  Document type.  Use the g_doc_type_<> variables, where <> is:
6233 --    REQUISITION
6234 --    PA
6235 --    PO
6236 --    RELEASE
6237 --p_doc_level
6238 --  The type of ids that are being passed.  Use g_doc_level_<>
6239 --    HEADER
6240 --    LINE
6241 --    SHIPMENT
6242 --    DISTRIBUTION
6243 --p_doc_level_id
6244 --  Id of the doc level type on which the action is being taken.
6245 --OUT:
6246 --x_reserved_count
6247 --  The number of (non-prevent) distributions that are reserved.
6248 --x_unreserved_count
6249 --  The number of non-prevent distributions that are unreserved.
6250 --x_prevented_count
6251 --  The number of distributions that have prevent_encumbrance_flag set to 'Y'.
6252 --End of Comments
6253 --------------------------------------------------------------------------------
6254 PROCEDURE get_open_encumbrance_stats(
6255    p_doc_type                       IN             VARCHAR2
6256 ,  p_doc_level                      IN             VARCHAR2
6257 ,  p_doc_level_id                   IN             NUMBER
6258 ,  x_reserved_count                 OUT NOCOPY     NUMBER
6259 ,  x_unreserved_count               OUT NOCOPY     NUMBER
6260 ,  x_prevented_count                OUT NOCOPY     NUMBER
6261 )
6262 IS
6263 
6264 l_proc_name CONSTANT VARCHAR2(30) := 'GET_OPEN_ENCUMBRANCE_STATS';
6265 l_log_head  CONSTANT VARCHAR2(100) := g_log_head || l_proc_name;
6266 l_progress  VARCHAR2(3) := '000';
6267 
6268 BEGIN
6269 
6270 IF g_debug_stmt THEN
6271    PO_DEBUG.debug_begin(l_log_head);
6272    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_type',p_doc_type);
6273    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level',p_doc_level);
6274    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level_id',p_doc_level_id);
6275 END IF;
6276 
6277 l_progress := '010';
6278 
6279 IF (p_doc_type = g_doc_type_REQUISITION) THEN
6280 
6281    l_progress := '100';
6282 
6283    --SQL What:
6284    --    Count the distributions that fall into different categories.
6285    --SQL Where:
6286    --    Not Cancelled, not Finally Closed.
6287    --    The id passed in can be header, line, or dist. id.
6288    --    An inner query is used in the FROM clause to avoid duplicating
6289    --    any logic, while maintaining a decent SQL plan.
6290 
6291    SELECT
6292 
6293       -- reserved rows that are not prevent_encumbrance
6294       -- i.e., prevent_encumbrance_flag <> Y and encumbered_flag = Y
6295       COUNT(   DECODE(  PRD.prevent_encumbrance_flag
6296                      ,  'Y', NULL
6297                      ,  DECODE(  PRD.encumbered_flag
6298                               ,  'Y', 'Y'
6299                               ,  NULL
6300                               )
6301                      )
6302             )
6303 
6304       -- unreserved rows that are not prevent_encumbrance
6305       -- i.e., prevent_encumbrance_flag <> Y and encumbered_flag <> Y
6306    ,  COUNT(   DECODE(  PRD.prevent_encumbrance_flag
6307                      ,  'Y', NULL
6308                      ,  DECODE(  PRD.encumbered_flag
6309                               ,  'Y', NULL
6310                               ,  'N'
6311                               )
6312                      )
6313             )
6314 
6315       -- prevent_encumbrance rows
6316       -- i.e., prevent_encumbrance_flag = Y
6317    ,  COUNT(   DECODE(  PRD.prevent_encumbrance_flag
6318                      ,  'Y', 'Y'
6319                      ,  NULL
6320                      )
6321             )
6322 
6323    INTO
6324       x_reserved_count
6325    ,  x_unreserved_count
6326    ,  x_prevented_count
6327 
6328    FROM
6329       PO_REQ_DISTRIBUTIONS_ALL PRD
6330    ,  PO_REQUISITION_LINES_ALL PRL
6331    ,  (
6332          SELECT
6333             p_doc_level_id             dist_id
6334          FROM DUAL
6335          WHERE p_doc_level = g_doc_level_DISTRIBUTION
6336          UNION ALL
6337          SELECT
6338             PRD1.distribution_id       dist_id
6339          FROM PO_REQ_DISTRIBUTIONS_ALL PRD1
6340          WHERE p_doc_level = g_doc_level_LINE
6341          AND   PRD1.requisition_line_id = p_doc_level_id
6342          UNION ALL
6343          SELECT
6344             PRD2.distribution_id       dist_id
6345          FROM
6346             PO_REQ_DISTRIBUTIONS_ALL PRD2
6347          ,  PO_REQUISITION_LINES_ALL PRL2
6348          WHERE p_doc_level = g_doc_level_HEADER
6349          AND   PRD2.requisition_line_id = PRL2.requisition_line_id
6350          AND   PRL2.requisition_header_id = p_doc_level_id
6351       ) DIST_IDS
6352 
6353    WHERE PRL.requisition_line_id = PRD.requisition_line_id
6354    AND   NVL(PRL.cancel_flag,'N') <> 'Y'
6355    AND   NVL(PRL.closed_code,g_clsd_OPEN) <> g_clsd_FINALLY_CLOSED
6356    AND   PRD.distribution_id = DIST_IDS.dist_id
6357    ;
6358 
6359    l_progress := '190';
6360 
6361 ELSE  -- not a requisition
6362 
6363    l_progress := '200';
6364 
6365    --SQL What:
6366    --    Count the distributions that fall into different categories.
6367    --SQL Where:
6368    --    Not Cancelled, not Finally Closed.
6369    --    The id passed in can be header, line, shipment, dist., or release id.
6370 
6371    SELECT
6372 
6373       -- reserved rows that are not prevent_encumbrance
6374       -- i.e., prevent_encumbrance_flag <> Y and encumbered_flag = Y
6375       COUNT(   DECODE(  POD.prevent_encumbrance_flag
6376                      ,  'Y', NULL
6377                      ,  DECODE(  POD.encumbered_flag
6378                               ,  'Y', 'Y'
6379                               ,  NULL
6380                               )
6381                      )
6382             )
6383 
6384       -- unreserved rows that are not prevent_encumbrance
6385       -- i.e., prevent_encumbrance_flag <> Y and encumbered_flag <> Y
6386    ,  COUNT(   DECODE(  POD.prevent_encumbrance_flag
6387                      ,  'Y', NULL
6388                      ,  DECODE(  POD.encumbered_flag
6389                               ,  'Y', NULL
6390                               ,  'N'
6391                               )
6392                      )
6393             )
6394 
6395       -- prevent_encumbrance rows
6396       -- i.e., prevent_encumbrance_flag = Y
6397    ,  COUNT(   DECODE(  POD.prevent_encumbrance_flag
6398                      ,  'Y', 'Y'
6399                      ,  NULL
6400                      )
6401             )
6402 
6403    INTO
6404       x_reserved_count
6405    ,  x_unreserved_count
6406    ,  x_prevented_count
6407 
6408    FROM
6409       PO_DISTRIBUTIONS_ALL POD
6410    ,  PO_LINE_LOCATIONS_ALL POLL
6411    ,  PO_HEADERS_ALL POH
6412 
6413    WHERE POLL.line_location_id(+) = POD.line_location_id
6414    AND   POH.po_header_id = POD.po_header_id
6415    AND
6416       (   (p_doc_type <> g_doc_type_PA AND NVL(POLL.cancel_flag,'N') <> 'Y')
6417       OR  (p_doc_type = g_doc_type_PA AND NVL(POH.cancel_flag,'N') <> 'Y')
6418       )
6419    AND
6420       (
6421          (  p_doc_type <> g_doc_type_PA
6422             AND NVL(POLL.closed_code,g_clsd_OPEN) <> g_clsd_FINALLY_CLOSED
6423          )
6424       OR
6425          (  p_doc_type = g_doc_type_PA
6426             AND NVL(POH.closed_code,g_clsd_OPEN) <> g_clsd_FINALLY_CLOSED
6427          )
6428       )
6429    AND
6430       (
6431          (  p_doc_level = g_doc_level_DISTRIBUTION
6432             AND   POD.po_distribution_id = p_doc_level_id
6433          )
6434       OR
6435          (  p_doc_level = g_doc_level_SHIPMENT
6436             AND   POD.line_location_id = p_doc_level_id
6437          )
6438       OR
6439          (  p_doc_level = g_doc_level_LINE
6440             AND   POD.po_line_id = p_doc_level_id
6441          )
6442       OR
6443          (  p_doc_level = g_doc_level_HEADER
6444             AND   p_doc_type = g_doc_type_RELEASE
6445             AND   POD.po_release_id = p_doc_level_id
6446          )
6447       OR
6448          (  p_doc_level = g_doc_level_HEADER
6449             AND   p_doc_type <> g_doc_type_RELEASE
6450             AND   POD.po_header_id = p_doc_level_id
6451          )
6452       )
6453    -- Make sure that release dists are not picked up for BPAs, PPOs.
6454    AND
6455       (
6456          (  p_doc_type <> g_doc_type_RELEASE
6457             AND   POD.po_release_id IS NULL
6458          )
6459       OR
6460          (  p_doc_type = g_doc_type_RELEASE
6461             AND   POD.po_release_id IS NOT NULL
6462          )
6463       )
6464    ;
6465 
6466    l_progress := '290';
6467 
6468 END IF;
6469 
6470 IF g_debug_stmt THEN
6471    PO_DEBUG.debug_stmt(l_log_head,l_progress,'got encumbrance counts');
6472 END IF;
6473 
6474 l_progress := '900';
6475 IF g_debug_stmt THEN
6476    PO_DEBUG.debug_var(l_log_head,l_progress,'x_reserved_count',x_reserved_count);
6477    PO_DEBUG.debug_var(l_log_head,l_progress,'x_unreserved_count',x_unreserved_count);
6478    PO_DEBUG.debug_var(l_log_head,l_progress,'x_prevented_count',x_prevented_count);
6479    PO_DEBUG.debug_end(l_log_head);
6480 END IF;
6481 
6482 EXCEPTION
6483 WHEN OTHERS THEN
6484    IF g_debug_unexp THEN
6485       PO_DEBUG.debug_var(l_log_head,l_progress,'x_reserved_count',x_reserved_count);
6486       PO_DEBUG.debug_var(l_log_head,l_progress,'x_unreserved_count',x_unreserved_count);
6487       PO_DEBUG.debug_var(l_log_head,l_progress,'x_prevented_count',x_prevented_count);
6488       PO_DEBUG.debug_exc(l_log_head,l_progress);
6489    END IF;
6490    RAISE;
6491 
6492 END get_open_encumbrance_stats;
6493 
6494 
6495 
6496 
6497 --------------------------------------------------------------------------------
6498 --Start of Comments
6499 --Name: should_display_reserved
6500 --Pre-reqs:
6501 --  None.
6502 --Modifies:
6503 --  None.
6504 --Locks:
6505 --  None.
6506 --Function:
6507 --  Determines whether or not the status of the given entity should display
6508 --  as "Reserved".
6509 --  This procedure is being used by functions that are embedded in SQL
6510 --  statements in other products (views, etc.), so it is not allowed
6511 --  to modify anything (except in an autonomous transaction).
6512 --Parameters:
6513 --IN:
6514 --p_doc_type
6515 --  Document type.  Use the g_doc_type_<> variables, where <> is:
6516 --    REQUISITION
6517 --    PA
6518 --    PO
6519 --    RELEASE
6520 --p_doc_level
6521 --  The type of ids that are being passed.  Use g_doc_level_<>
6522 --    HEADER
6523 --    LINE
6524 --    SHIPMENT
6525 --    DISTRIBUTION
6526 --p_doc_level_id
6527 --  Id of the doc level type on which the action is being taken.
6528 --OUT:
6529 --x_display_reserved_flag
6530 --  VARCHAR2(1)
6531 --  'Y' - "Reserved" should be displayed
6532 --  'N' - don't display "Reserved"
6533 --End of Comments
6534 --------------------------------------------------------------------------------
6535 PROCEDURE should_display_reserved(
6536    p_doc_type                       IN             VARCHAR2
6537 ,  p_doc_level                      IN             VARCHAR2
6538 ,  p_doc_level_id                   IN             NUMBER
6539 ,  x_display_reserved_flag          OUT NOCOPY     VARCHAR2
6540 )
6541 IS
6542 
6543 l_proc_name CONSTANT VARCHAR2(30) := 'SHOULD_DISPLAY_RESERVED';
6544 l_log_head  CONSTANT VARCHAR2(100) := g_log_head || l_proc_name;
6545 l_progress  VARCHAR2(3) := '000';
6546 
6547 l_reserved_count   NUMBER;
6548 l_unreserved_count NUMBER;
6549 l_prevented_count  NUMBER;
6550 
6551 BEGIN
6552 
6553 IF g_debug_stmt THEN
6554    PO_DEBUG.debug_begin(l_log_head);
6555    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_type',p_doc_type);
6556    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level',p_doc_level);
6557    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level_id',p_doc_level_id);
6558 END IF;
6559 
6560 l_progress := '010';
6561 
6562 get_open_encumbrance_stats(
6563    p_doc_type           => p_doc_type
6564 ,  p_doc_level          => p_doc_level
6565 ,  p_doc_level_id       => p_doc_level_id
6566 ,  x_reserved_count     => l_reserved_count
6567 ,  x_unreserved_count   => l_unreserved_count
6568 ,  x_prevented_count    => l_prevented_count
6569 );
6570 
6571 l_progress := '020';
6572 
6573 IF (l_unreserved_count = 0 AND l_reserved_count > 0) THEN
6574    x_display_reserved_flag := 'Y';
6575 ELSE
6576    x_display_reserved_flag := 'N';
6577 END IF;
6578 
6579 l_progress := '900';
6580 IF g_debug_stmt THEN
6581    PO_DEBUG.debug_var(l_log_head,l_progress,'x_display_reserved_flag',x_display_reserved_flag);
6582    PO_DEBUG.debug_end(l_log_head);
6583 END IF;
6584 
6585 EXCEPTION
6586 WHEN OTHERS THEN
6587    IF g_debug_unexp THEN
6588 PO_DEBUG.debug_var(l_log_head,l_progress,'x_display_reserved_flag',x_display_reserved_flag);
6589       PO_DEBUG.debug_exc(l_log_head,l_progress);
6590    END IF;
6591    RAISE;
6592 
6593 END should_display_reserved;
6594 
6595 
6596 
6597 
6598 --------------------------------------------------------------------------------
6599 --Start of Comments
6600 --Name: is_fully_reserved
6601 --Pre-reqs:
6602 --  None.
6603 --Modifies:
6604 --  None.
6605 --Locks:
6606 --  None.
6607 --Function:
6608 --  Determines whether or not all of the distributions below the given entity that
6609 --  can be reserved are reserved.
6610 --Parameters:
6611 --IN:
6612 --p_doc_type
6613 --  Document type.  Use the g_doc_type_<> variables, where <> is:
6614 --    REQUISITION
6615 --    PA
6616 --    PO
6617 --    RELEASE
6618 --p_doc_level
6619 --  The type of ids that are being passed.  Use g_doc_level_<>
6620 --    HEADER
6621 --    LINE
6622 --    SHIPMENT
6623 --    DISTRIBUTION
6624 --p_doc_level_id
6625 --  Id of the doc level type on which the action is being taken.
6626 --OUT:
6627 --x_fully_reserved_flag
6628 --  VARCHAR2(1)
6629 --  'Y' - all of the dists that can be reserved are reserved
6630 --  'N' - there is at least one non-prevent dist that is not reserved
6631 --End of Comments
6632 --------------------------------------------------------------------------------
6633 PROCEDURE is_fully_reserved(
6634    p_doc_type                       IN             VARCHAR2
6635 ,  p_doc_level                      IN             VARCHAR2
6636 ,  p_doc_level_id                   IN             NUMBER
6637 ,  x_fully_reserved_flag            OUT NOCOPY     VARCHAR2
6638 )
6639 IS
6640 
6641 l_proc_name CONSTANT VARCHAR2(30) := 'IS_FULLY_RESERVED';
6642 l_log_head  CONSTANT VARCHAR2(100) := g_log_head || l_proc_name;
6643 l_progress  VARCHAR2(3) := '000';
6644 
6645 l_reserved_count   NUMBER;
6646 l_unreserved_count NUMBER;
6647 l_prevented_count  NUMBER;
6648 
6649 BEGIN
6650 
6651 IF g_debug_stmt THEN
6652    PO_DEBUG.debug_begin(l_log_head);
6653    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_type',p_doc_type);
6654    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level',p_doc_level);
6655    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level_id',p_doc_level_id);
6656 END IF;
6657 
6658 l_progress := '010';
6659 
6660 get_open_encumbrance_stats(
6661    p_doc_type           => p_doc_type
6662 ,  p_doc_level          => p_doc_level
6663 ,  p_doc_level_id       => p_doc_level_id
6664 ,  x_reserved_count     => l_reserved_count
6665 ,  x_unreserved_count   => l_unreserved_count
6666 ,  x_prevented_count    => l_prevented_count
6667 );
6668 
6669 l_progress := '020';
6670 
6671 IF (l_unreserved_count > 0) THEN
6672    x_fully_reserved_flag := 'N';
6673 ELSE
6674    x_fully_reserved_flag := 'Y';
6675 END IF;
6676 
6677 l_progress := '900';
6678 IF g_debug_stmt THEN
6679    PO_DEBUG.debug_var(l_log_head,l_progress,'x_fully_reserved_flag',x_fully_reserved_flag);
6680    PO_DEBUG.debug_end(l_log_head);
6681 END IF;
6682 
6683 EXCEPTION
6684 WHEN OTHERS THEN
6685    IF g_debug_unexp THEN
6686 PO_DEBUG.debug_var(l_log_head,l_progress,'x_fully_reserved_flag',x_fully_reserved_flag);
6687       PO_DEBUG.debug_exc(l_log_head,l_progress);
6688    END IF;
6689    RAISE;
6690 
6691 END is_fully_reserved;
6692 
6693 
6694 
6695 
6696 --------------------------------------------------------------------------------
6697 --Start of Comments
6698 --Name: are_any_dists_reserved
6699 --Pre-reqs:
6700 --  None.
6701 --Modifies:
6702 --  None.
6703 --Locks:
6704 --  None.
6705 --Function:
6706 --  Determines if any of the distributions below the given entity are reserved.
6707 --Parameters:
6708 --IN:
6709 --p_doc_type
6710 --  Document type.  Use the g_doc_type_<> variables, where <> is:
6711 --    REQUISITION
6712 --    PA
6713 --    PO
6714 --    RELEASE
6715 --p_doc_level
6716 --  The type of ids that are being passed.  Use g_doc_level_<>
6717 --    HEADER
6718 --    LINE
6719 --    SHIPMENT
6720 --    DISTRIBUTION
6721 --p_doc_level_id
6722 --  Id of the doc level type on which the action is being taken.
6723 --OUT:
6724 --x_some_dists_reserved_flag
6725 --  VARCHAR2(1)
6726 --  'Y' - at least one distribution is reserved
6727 --  'N' - no distributions are reserved
6728 --End of Comments
6729 --------------------------------------------------------------------------------
6730 PROCEDURE are_any_dists_reserved(
6731    p_doc_type                       IN             VARCHAR2
6732 ,  p_doc_level                      IN             VARCHAR2
6733 ,  p_doc_level_id                   IN             NUMBER
6734 ,  x_some_dists_reserved_flag       OUT NOCOPY     VARCHAR2
6735 )
6736 IS
6737 
6738 l_proc_name CONSTANT VARCHAR2(30) := 'ARE_ANY_DISTS_RESERVED';
6739 l_log_head  CONSTANT VARCHAR2(100) := g_log_head || l_proc_name;
6740 l_progress  VARCHAR2(3) := '000';
6741 
6742 l_reserved_count   NUMBER;
6743 l_unreserved_count NUMBER;
6744 l_prevented_count  NUMBER;
6745 
6746 BEGIN
6747 
6748 IF g_debug_stmt THEN
6749    PO_DEBUG.debug_begin(l_log_head);
6750    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_type',p_doc_type);
6751    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level',p_doc_level);
6752    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level_id',p_doc_level_id);
6753 END IF;
6754 
6755 l_progress := '010';
6756 
6757 get_open_encumbrance_stats(
6758    p_doc_type           => p_doc_type
6759 ,  p_doc_level          => p_doc_level
6760 ,  p_doc_level_id       => p_doc_level_id
6761 ,  x_reserved_count     => l_reserved_count
6762 ,  x_unreserved_count   => l_unreserved_count
6763 ,  x_prevented_count    => l_prevented_count
6764 );
6765 
6766 l_progress := '020';
6767 
6768 IF (l_reserved_count > 0) THEN
6769    x_some_dists_reserved_flag := 'Y';
6770 ELSE
6771    x_some_dists_reserved_flag := 'N';
6772 END IF;
6773 
6774 l_progress := '900';
6775 IF g_debug_stmt THEN
6776    PO_DEBUG.debug_var(l_log_head,l_progress,'x_some_dists_reserved_flag',x_some_dists_reserved_flag);
6777    PO_DEBUG.debug_end(l_log_head);
6778 END IF;
6779 
6780 EXCEPTION
6781 WHEN OTHERS THEN
6782    IF g_debug_unexp THEN
6783 PO_DEBUG.debug_var(l_log_head,l_progress,'x_some_dists_reserved_flag',x_some_dists_reserved_flag);
6784       PO_DEBUG.debug_exc(l_log_head,l_progress);
6785    END IF;
6786    RAISE;
6787 
6788 END are_any_dists_reserved;
6789 
6790 
6791 
6792 
6793 --------------------------------------------------------------------------------
6794 --Start of Comments
6795 --Name: get_reserved_lookup
6796 --Pre-reqs:
6797 --  None.
6798 --Modifies:
6799 --  None.
6800 --Locks:
6801 --  None.
6802 --Function:
6803 --  Gets the text to display for the "Reserved" keyword.
6804 --Parameters:
6805 --OUT:
6806 --x_displayed_field
6807 --  PO_LOOKUP_CODES.displayed_field%TYPE
6808 --  The text corresponding to the 'RESERVED' code.
6809 --End of Comments
6810 --------------------------------------------------------------------------------
6811 PROCEDURE get_reserved_lookup(
6812    x_displayed_field                  OUT NOCOPY     VARCHAR2
6813 )
6814 IS
6815 
6816 l_proc_name CONSTANT VARCHAR2(30) := 'GET_RESERVED_LOOKUP';
6817 l_log_head  CONSTANT VARCHAR2(100) := g_log_head || l_proc_name;
6818 l_progress  VARCHAR2(3) := '000';
6819 
6820 BEGIN
6821 
6822 IF g_debug_stmt THEN
6823    PO_DEBUG.debug_begin(l_log_head);
6824 END IF;
6825 
6826 l_progress := '010';
6827 
6828 SELECT POLC.displayed_field
6829 INTO x_displayed_field
6830 FROM PO_LOOKUP_CODES POLC
6831 WHERE POLC.lookup_type = 'DOCUMENT STATE'
6832 AND POLC.lookup_code = 'RESERVED'
6833 ;
6834 
6835 l_progress := '900';
6836 IF g_debug_stmt THEN
6837    PO_DEBUG.debug_var(l_log_head,l_progress,'x_displayed_field',x_displayed_field);
6838    PO_DEBUG.debug_end(l_log_head);
6839 END IF;
6840 
6841 EXCEPTION
6842 WHEN OTHERS THEN
6843    IF g_debug_unexp THEN
6844       PO_DEBUG.debug_var(l_log_head,l_progress,'x_displayed_field',x_displayed_field);
6845       PO_DEBUG.debug_exc(l_log_head,l_progress);
6846    END IF;
6847 
6848 END get_reserved_lookup;
6849 
6850 
6851 
6852 
6853 -- Bug 3373453 START
6854 -------------------------------------------------------------------------------
6855 --Start of Comments
6856 --Name: validate_yes_no_param
6857 --Pre-reqs:
6858 --  None.
6859 --Modifies:
6860 --  None.
6861 --Locks:
6862 --  None.
6863 --Function:
6864 --  Validates that the given parameter value is one of the following:
6865 --    null, G_PARAMETER_YES, or G_PARAMETER_NO
6866 --  Returns an error for any other value.
6867 --Parameters:
6868 --IN:
6869 --p_parameter_name
6870 --  Name of the parameter; used in the error message
6871 --p_parameter_value
6872 --  Parameter value to be validated
6873 --OUT:
6874 --x_return_status
6875 --  FND_API.G_RET_STS_SUCCESS if the parameter value is valid.
6876 --  FND_API.G_RET_STS_ERROR if the parameter value is not valid.
6877 --  FND_API.G_RET_STS_UNEXP_ERROR if an unexpected error occurred.
6878 --End of Comments
6879 -------------------------------------------------------------------------------
6880 PROCEDURE validate_yes_no_param (
6881   x_return_status   OUT NOCOPY VARCHAR2,
6882   p_parameter_name  IN VARCHAR2,
6883   p_parameter_value IN VARCHAR2
6884 ) IS
6885   l_proc_name CONSTANT VARCHAR2(30) := 'VALIDATE_YES_NO_PARAM';
6886 BEGIN
6887   x_return_status := FND_API.G_RET_STS_SUCCESS;
6888 
6889   IF (p_parameter_value IS NOT NULL)
6890      AND (p_parameter_value NOT IN (G_PARAMETER_YES, G_PARAMETER_NO)) THEN
6891 
6892     FND_MESSAGE.set_name('PO', 'PO_INVALID_YES_NO_PARAM');
6893     FND_MESSAGE.set_token('PARAMETER_NAME', p_parameter_name);
6894     FND_MESSAGE.set_token('PARAMETER_VALUE', p_parameter_value);
6895     FND_MSG_PUB.add;
6896 
6897     x_return_status := FND_API.G_RET_STS_ERROR;
6898     RETURN;
6899   END IF;
6900 
6901 EXCEPTION
6902   WHEN OTHERS THEN
6903     PO_DEBUG.handle_unexp_error(p_pkg_name => g_pkg_name,
6904                                 p_proc_name => l_proc_name );
6905     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6906 END validate_yes_no_param;
6907 -- Bug 3373453 END
6908 
6909 
6910 
6911 
6912 --------------------------------------------------------------------------------
6913 --Start of Comments
6914 --Name: get_session_gt_nextval
6915 --Pre-reqs:
6916 --  None.
6917 --Modifies:
6918 --  PO_SESSION_GT_S
6919 --Locks:
6920 --  None.
6921 --Function:
6922 --  Retrieves the next sequence number from the PO_SESSION_GT_S sequence.
6923 --Returns:
6924 --  PO_SESSION_GT_S.nextval
6925 --End of Comments
6926 --------------------------------------------------------------------------------
6927 FUNCTION get_session_gt_nextval
6928 RETURN NUMBER
6929 IS
6930 
6931 l_proc_name CONSTANT VARCHAR2(30) := 'GET_SESSION_GT_NEXTVAL';
6932 l_log_head  CONSTANT VARCHAR2(100) := g_log_head || l_proc_name;
6933 l_progress  VARCHAR2(3) := '000';
6934 
6935 x_nextval   NUMBER;
6936 
6937 BEGIN
6938 
6939 IF g_debug_stmt THEN
6940    PO_DEBUG.debug_begin(l_log_head);
6941 END IF;
6942 
6943 l_progress := '010';
6944 
6945 SELECT PO_SESSION_GT_S.nextval
6946 INTO x_nextval
6947 FROM DUAL
6948 ;
6949 
6950 l_progress := '900';
6951 
6952 IF g_debug_stmt THEN
6953    PO_DEBUG.debug_var(l_log_head,l_progress,'x_nextval',x_nextval);
6954    PO_DEBUG.debug_end(l_log_head);
6955 END IF;
6956 
6957 RETURN(x_nextval);
6958 
6959 EXCEPTION
6960 WHEN OTHERS THEN
6961    PO_MESSAGE_S.sql_error(g_pkg_name,l_proc_name,l_progress,SQLCODE,SQLERRM);
6962    IF g_debug_stmt THEN
6963       PO_DEBUG.debug_var(l_log_head,l_progress,'x_nextval',x_nextval);
6964    END IF;
6965    RAISE;
6966 
6967 END get_session_gt_nextval;
6968 
6969 
6970 -- <Doc Manager Rewrite 11.5.11 Start>
6971 -- port of deprecated user exit "REQUEST_INFO" for request code DOCUMENT_STATUS
6972 -- org_context should be set
6973 PROCEDURE get_document_status(
6974    p_document_id          IN      VARCHAR2
6975 ,  p_document_type        IN      VARCHAR2
6976 ,  p_document_subtype     IN      VARCHAR2
6977 ,  x_return_status        OUT NOCOPY VARCHAR2
6978 ,  x_document_status      OUT NOCOPY VARCHAR2
6979 )
6980 IS
6981 
6982 d_msg           VARCHAR2(200);
6983 d_progress      NUMBER;
6984 d_module        VARCHAR2(70) := 'po.plsql.PO_CORE_S.get_document_status';
6985 
6986 l_sep           VARCHAR2(8) := ', ';
6987 l_ret_sts       VARCHAR2(1);
6988 
6989 l_status_code     PO_LOOKUP_CODES.displayed_field%TYPE;
6990 l_cancel_status   PO_LOOKUP_CODES.displayed_field%TYPE;
6991 l_closed_status   PO_LOOKUP_CODES.displayed_field%TYPE;
6992 l_frozen_status   PO_LOOKUP_CODES.displayed_field%TYPE;
6993 l_hold_status     PO_LOOKUP_CODES.displayed_field%TYPE;
6994 l_auth_status     PO_HEADERS.authorization_status%TYPE;
6995 l_offline_status  PO_LOOKUP_CODES.displayed_field%TYPE;
6996 l_reserved_status    PO_LOOKUP_CODES.displayed_field%TYPE;
6997 
6998 l_cancel_flag     PO_HEADERS.cancel_flag%TYPE;
6999 l_closed_code     PO_HEADERS.closed_code%TYPE;
7000 l_frozen_flag     PO_HEADERS.frozen_flag%TYPE;
7001 l_user_hold_flag  PO_HEADERS.user_hold_flag%TYPE;
7002 l_offline_flag    PO_ACTION_HISTORY.offline_code%TYPE;
7003 l_reserved_flag      VARCHAR2(1);
7004 
7005 l_display_reserved   VARCHAR2(1);
7006 
7007 BEGIN
7008 
7009   d_progress := 0;
7010   IF (PO_LOG.d_proc) THEN
7011     PO_LOG.proc_begin(d_module);
7012     PO_LOG.proc_begin(d_module, 'p_document_id', p_document_id);
7013     PO_LOG.proc_begin(d_module, 'p_document_type', p_document_type);
7014     PO_LOG.proc_begin(d_module, 'p_document_subtype', p_document_subtype);
7015   END IF;
7016 
7017   BEGIN
7018 
7019     IF (p_document_type = 'REQUISITION')
7020     THEN
7021 
7022       d_progress := 20;
7023 
7024       SELECT plc_sta.displayed_field
7025           ,  prh.authorization_status
7026           ,  DECODE(nvl(prh.closed_code,'OPEN'), 'OPEN', NULL, plc_clo.displayed_field)
7027       INTO l_status_code, l_auth_status, l_closed_status
7028       FROM po_requisition_headers prh, po_lookup_codes plc_sta, po_lookup_codes plc_clo
7029       WHERE plc_sta.lookup_code = DECODE(prh.authorization_status,
7030                                     'SYSTEM_SAVED', 'INCOMPLETE',
7031                                     nvl(prh.authorization_status, 'INCOMPLETE')
7032                                   )
7033         AND plc_clo.lookup_code = nvl(prh.closed_code, 'OPEN')
7034         AND plc_clo.lookup_type = 'DOCUMENT STATE'
7035         AND plc_sta.lookup_type = 'AUTHORIZATION STATUS'
7036         AND prh.requisition_header_id = p_document_id;
7037 
7038       d_progress := 30;
7039 
7040       BEGIN
7041 
7042         SELECT polc.displayed_field, nvl(poah.offline_code, 'N')
7043         INTO l_offline_status, l_offline_flag
7044         FROM po_lookup_codes polc, po_action_history poah
7045         WHERE polc.lookup_type = 'DOCUMENT STATE'
7046           AND polc.lookup_code = poah.offline_code
7047           AND poah.object_id = p_document_id
7048           AND poah.object_type_code = 'REQUISITION'
7049           AND poah.action_code IS NULL;
7050 
7051       EXCEPTION
7052         WHEN no_data_found THEN
7053           IF (PO_LOG.d_stmt) THEN
7054             PO_LOG.stmt(d_module, d_progress, 'No data found');
7055           END IF;
7056       END;
7057 
7058     ELSIF (p_document_type IN ('PO', 'PA'))
7059     THEN
7060 
7061       d_progress := 40;
7062 
7063       SELECT plc_sta.displayed_field
7064           ,  DECODE(poh.cancel_flag, 'Y', plc_can.displayed_field, NULL)
7065           ,  DECODE(nvl(poh.closed_code,'OPEN'), 'OPEN', NULL, plc_clo.displayed_field)
7066           ,  DECODE(poh.frozen_flag, 'Y', plc_fro.displayed_field, NULL)
7067           ,  DECODE(poh.user_hold_flag, 'Y', plc_hld.displayed_field, NULL)
7068           ,  poh.authorization_status
7069           ,  nvl(poh.cancel_flag, 'N')
7070           ,  poh.closed_code
7071           ,  nvl(poh.frozen_flag, 'N')
7072           ,  nvl(poh.user_hold_flag,'N')
7073        INTO l_status_code
7074          ,  l_cancel_status
7075          ,  l_closed_status
7076          ,  l_frozen_status
7077          ,  l_hold_status
7078          ,  l_auth_status
7079          ,  l_cancel_flag
7080          ,  l_closed_code
7081          ,  l_frozen_flag
7082          ,  l_user_hold_flag
7083        FROM po_headers poh
7084          ,  po_lookup_codes plc_sta
7085 		   ,  po_lookup_codes plc_can
7086 		   ,  po_lookup_codes plc_clo
7087 		   ,  po_lookup_codes plc_fro
7088  		   ,  po_lookup_codes plc_hld
7089        WHERE plc_sta.lookup_code = DECODE(poh.approved_flag,
7090                                      'R', poh.approved_flag,
7091                                      nvl(poh.authorization_status, 'INCOMPLETE')
7092                                    )
7093          AND plc_sta.lookup_type in ('PO APPROVAL', 'DOCUMENT STATE')
7094 	      AND plc_can.lookup_code = 'CANCELLED'
7095          AND plc_can.lookup_type = 'DOCUMENT STATE'
7096          AND plc_clo.lookup_code = nvl(poh.closed_code, 'OPEN')
7097          AND plc_clo.lookup_type = 'DOCUMENT STATE'
7098          AND plc_fro.lookup_code = 'FROZEN'
7099          AND plc_fro.lookup_type = 'DOCUMENT STATE'
7100          AND plc_hld.lookup_code = 'ON HOLD'
7101          AND plc_hld.lookup_type = 'DOCUMENT STATE'
7102          AND poh.po_header_id    = p_document_id;
7103 
7104       d_progress := 50;
7105 
7106       BEGIN
7107 
7108         SELECT polc.displayed_field, nvl(poah.offline_code, 'N')
7109         INTO l_offline_status, l_offline_flag
7110         FROM po_lookup_codes polc, po_action_history poah
7111         WHERE polc.lookup_type = 'DOCUMENT STATE'
7112           AND polc.lookup_code = poah.offline_code
7113           AND poah.object_id = p_document_id
7114           AND poah.object_type_code IN ('PO', 'PA')
7115           AND poah.action_code IS NULL;
7116 
7117       EXCEPTION
7118         WHEN no_data_found THEN
7119           IF (PO_LOG.d_stmt) THEN
7120             PO_LOG.stmt(d_module, d_progress, 'No data found');
7121           END IF;
7122       END;
7123 
7124     ELSIF (p_document_type = 'RELEASE')
7125     THEN
7126 
7127       d_progress := 60;
7128 
7129       SELECT plc_sta.displayed_field
7130           ,  DECODE(por.cancel_flag, 'Y', plc_can.displayed_field, NULL)
7131           ,  DECODE(nvl(por.closed_code,'OPEN'), 'OPEN', NULL, plc_clo.displayed_field)
7132           ,  DECODE(por.frozen_flag, 'Y', plc_fro.displayed_field, NULL)
7133           ,  DECODE(por.hold_flag, 'Y', plc_hld.displayed_field, NULL)
7134           ,  por.authorization_status
7135           ,  nvl(por.cancel_flag, 'N')
7136           ,  por.closed_code
7137           ,  nvl(por.frozen_flag, 'N')
7138           ,  nvl(por.hold_flag,'N')
7139        INTO l_status_code
7140          ,  l_cancel_status
7141          ,  l_closed_status
7142          ,  l_frozen_status
7143          ,  l_hold_status
7144          ,  l_auth_status
7145          ,  l_cancel_flag
7146          ,  l_closed_code
7147          ,  l_frozen_flag
7148          ,  l_user_hold_flag
7149        FROM po_releases por
7150          ,  po_lookup_codes plc_sta
7151 		   ,  po_lookup_codes plc_can
7152 		   ,  po_lookup_codes plc_clo
7153 		   ,  po_lookup_codes plc_fro
7154  		   ,  po_lookup_codes plc_hld
7155        WHERE plc_sta.lookup_code = DECODE(por.approved_flag,
7156                                      'R', por.approved_flag,
7157                                      nvl(por.authorization_status, 'INCOMPLETE')
7158                                    )
7159          AND plc_sta.lookup_type in ('PO APPROVAL', 'DOCUMENT STATE')
7160 	      AND plc_can.lookup_code = 'CANCELLED'
7161          AND plc_can.lookup_type = 'DOCUMENT STATE'
7162          AND plc_clo.lookup_code = nvl(por.closed_code, 'OPEN')
7163          AND plc_clo.lookup_type = 'DOCUMENT STATE'
7164          AND plc_fro.lookup_code = 'FROZEN'
7165          AND plc_fro.lookup_type = 'DOCUMENT STATE'
7166          AND plc_hld.lookup_code = 'ON HOLD'
7167          AND plc_hld.lookup_type = 'DOCUMENT STATE'
7168          AND por.po_release_id    = p_document_id;
7169 
7170       d_progress := 70;
7171 
7172       BEGIN
7173 
7174         SELECT polc.displayed_field, nvl(poah.offline_code, 'N')
7175         INTO l_offline_status, l_offline_flag
7176         FROM po_lookup_codes polc, po_action_history poah
7177         WHERE polc.lookup_type = 'DOCUMENT STATE'
7178           AND polc.lookup_code = poah.offline_code
7179           AND poah.object_id = p_document_id
7180           AND poah.object_type_code = 'RELEASE'
7181           AND poah.action_code IS NULL;
7182 
7183       EXCEPTION
7184         WHEN no_data_found THEN
7185           IF (PO_LOG.d_stmt) THEN
7186             PO_LOG.stmt(d_module, d_progress, 'No data found');
7187           END IF;
7188       END;
7189 
7190     ELSE
7191 
7192       d_progress := 80;
7193       d_msg := 'Invalid document type';
7194       RAISE g_early_return_exc;
7195 
7196     END IF;
7197 
7198     IF (PO_LOG.d_stmt) THEN
7199       PO_LOG.stmt(d_module, d_progress, 'l_status_code', l_status_code);
7200       PO_LOG.stmt(d_module, d_progress, 'l_cancel_status', l_cancel_status);
7201       PO_LOG.stmt(d_module, d_progress, 'l_closed_status', l_closed_status);
7202       PO_LOG.stmt(d_module, d_progress, 'l_frozen_status', l_frozen_status);
7203       PO_LOG.stmt(d_module, d_progress, 'l_auth_status', l_auth_status);
7204       PO_LOG.stmt(d_module, d_progress, 'l_cancel_flag', l_cancel_flag);
7205       PO_LOG.stmt(d_module, d_progress, 'l_closed_code', l_closed_code);
7206       PO_LOG.stmt(d_module, d_progress, 'l_frozen_flag', l_frozen_flag);
7207       PO_LOG.stmt(d_module, d_progress, 'l_user_hold_flag', l_user_hold_flag);
7208       PO_LOG.stmt(d_module, d_progress, 'l_offline_status', l_offline_status);
7209       PO_LOG.stmt(d_module, d_progress, 'l_offline_flag', l_offline_flag);
7210     END IF;
7211 
7212     d_progress := 100;
7213 
7214     should_display_reserved(
7215        p_doc_type      => p_document_type
7216     ,  p_doc_level     => g_doc_level_HEADER
7217     ,  p_doc_level_id  => p_document_id
7218     ,  x_display_reserved_flag => l_display_reserved
7219     );
7220 
7221     IF (l_display_reserved = 'Y')
7222     THEN
7223 
7224       d_progress := 110;
7225       get_reserved_lookup(x_displayed_field => l_reserved_status);
7226 
7227     ELSE
7228 
7229       d_progress := 120;
7230       l_reserved_status := '';
7231 
7232     END IF;  -- l_display_reserved = 'Y';
7233 
7234     d_progress := 130;
7235 
7236     is_fully_reserved(
7237        p_doc_type      => p_document_type
7238     ,  p_doc_level     => g_doc_level_HEADER
7239     ,  p_doc_level_id  => p_document_id
7240     ,  x_fully_reserved_flag => l_reserved_flag
7241     );
7242 
7243     d_progress := 140;
7244 
7245     IF (PO_LOG.d_stmt) THEN
7246       PO_LOG.stmt(d_module, d_progress, 'l_display_reserved', l_display_reserved);
7247       PO_LOG.stmt(d_module, d_progress, 'l_reserved_status', l_reserved_status);
7248       PO_LOG.stmt(d_module, d_progress, 'l_reserved_flag', l_reserved_flag);
7249     END IF;
7250 
7251     x_document_status := l_status_code;
7252 
7253     IF (l_cancel_status IS NOT NULL) THEN
7254       x_document_status := x_document_status || l_sep || l_cancel_status;
7255     END IF;
7256 
7257     IF (l_closed_status IS NOT NULL) THEN
7258       x_document_status := x_document_status || l_sep || l_closed_status;
7259     END IF;
7260 
7261     IF (l_frozen_status IS NOT NULL) THEN
7262       x_document_status := x_document_status || l_sep || l_frozen_status;
7263     END IF;
7264 
7265     IF (l_hold_status IS NOT NULL) THEN
7266       x_document_status := x_document_status || l_sep || l_hold_status;
7267     END IF;
7268 
7269     IF (l_reserved_status IS NOT NULL) THEN
7270       x_document_status := x_document_status || l_sep || l_reserved_status;
7271     END IF;
7272 
7273     IF (l_offline_status IS NOT NULL) THEN
7274       x_document_status := x_document_status || l_sep || l_offline_status;
7275     END IF;
7276 
7277     l_ret_sts := 'S';
7278 
7279   EXCEPTION
7280     WHEN g_early_return_exc THEN
7281       l_ret_sts := 'U';
7282       IF (PO_LOG.d_exc) THEN
7283         PO_LOG.exc(d_module, d_progress, d_msg);
7284       END IF;
7285   END;
7286 
7287   x_return_status := l_ret_sts;
7288 
7289   IF (PO_LOG.d_proc) THEN
7290     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
7291     PO_LOG.proc_end(d_module, 'p_document_status', x_document_status);
7292     PO_LOG.proc_end(d_module);
7293   END IF;
7294 
7295   RETURN;
7296 
7297 EXCEPTION
7298   WHEN others THEN
7299     x_return_status := 'U';
7300     IF (PO_LOG.d_exc) THEN
7301       PO_LOG.exc(d_module, d_progress, SQLCODE || SQLERRM);
7302       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
7303       PO_LOG.proc_end(d_module);
7304     END IF;
7305 
7306     RETURN;
7307 
7308 END get_document_status;
7309 
7310 -- port of deprecated user exit "DEFAULT_PRICE"
7311 -- org_context must be set
7312 -- previously, if p_uom <> x_return_uom, then the form fields would not be updated
7313 -- with the pl/sql change, it is up to the caller to check this condition.
7314 PROCEDURE get_default_price(
7315    p_src_doc_header_id  IN     NUMBER
7316 ,  p_src_doc_line_num   IN     NUMBER
7317 ,  p_deliver_to_loc_id  IN     NUMBER
7318 ,  p_required_currency  IN     VARCHAR2
7319 ,  p_required_rate_type IN     VARCHAR2
7320 ,  p_quantity           IN     NUMBER
7321 ,  p_uom                IN     VARCHAR2
7322 ,  x_return_status      OUT NOCOPY  VARCHAR2
7323 ,  x_return_uom         OUT NOCOPY  VARCHAR2
7324 ,  x_base_price         OUT NOCOPY  NUMBER
7325 ,  x_currency_price     OUT NOCOPY  NUMBER
7326 ,  x_discount           OUT NOCOPY  NUMBER
7327 ,  x_currency_code      OUT NOCOPY  VARCHAR2
7328 ,  x_rate_type          OUT NOCOPY  VARCHAR2
7329 ,  x_rate_date          OUT NOCOPY  DATE
7330 ,  x_rate               OUT NOCOPY  NUMBER
7331 )
7332 IS
7333 
7334 d_progress        NUMBER;
7335 d_module          VARCHAR2(70) := 'po.plsql.PO_CORE_S.get_default_price';
7336 d_msg             VARCHAR2(200);
7337 
7338 l_ret_sts         VARCHAR2(1)   := 'S';
7339 l_ship_to_loc_id  HR_LOCATIONS.ship_to_location_id%TYPE;
7340 
7341 l_do_non_loc_cursor  BOOLEAN;
7342 
7343 l_quantity   NUMBER;
7344 
7345 CURSOR loc_unit_price_cur(  p_header_id NUMBER, p_line_num NUMBER
7346                           , p_required_curr VARCHAR2, p_required_rate_type VARCHAR2
7347                           , p_uom VARCHAR2, p_qty NUMBER, p_ship_to_loc_id NUMBER)
7348 IS
7349   SELECT ROUND(poll.price_override * DECODE(poh.rate, 0, 1, null, 1, ROUND(poh.rate,5)), 5)
7350 	    , poh.rate_date
7351 	    , poh.rate
7352 	    ,	poh.currency_code
7353        , poh.rate_type
7354        , poll.price_discount
7355        , poll.price_override
7356        , DECODE(poll.line_location_id, NULL, pol.unit_meas_lookup_code, poll.unit_meas_lookup_code)
7357   FROM po_headers poh, po_lines pol, po_line_locations poll
7358   WHERE poh.po_header_id = p_header_id
7359     AND poh.po_header_id = pol.po_header_id
7360     AND pol.line_num = p_line_num
7361     AND pol.po_line_id = poll.po_line_id (+)
7362     AND (p_required_curr IS NULL or poh.currency_code = p_required_curr)
7363     AND (p_required_rate_type is null or poh.rate_type = p_required_rate_type)
7364     AND NVL(poll.unit_meas_lookup_code, NVL(p_uom, pol.unit_meas_lookup_code))
7365           = NVL(p_uom, pol.unit_meas_lookup_code)
7366     AND trunc(sysdate) BETWEEN NVL(poll.start_date, trunc(sysdate)) AND NVL(poll.end_date, trunc(sysdate))
7367     AND poll.quantity <= p_qty
7368     AND poll.ship_to_location_id = p_ship_to_loc_id
7369     AND poll.shipment_type in ('PRICE BREAK', 'QUOTATION')
7370   ORDER BY 1 ASC;
7371 
7372 CURSOR unit_price_cur(  p_header_id NUMBER, p_line_num NUMBER
7373                       , p_required_curr VARCHAR2, p_required_rate_type VARCHAR2
7374                       , p_uom VARCHAR2, p_qty NUMBER)
7375 IS
7376   SELECT ROUND(DECODE(poll.shipment_type,
7377                   'PRICE BREAK', DECODE(poll.ship_to_location_id,
7378                                    NULL, poll.price_override,pol.unit_price),
7379                   'QUOTATION', DECODE(poll.ship_to_location_id,
7380                                  NULL,poll.price_override, pol.unit_price),
7381                   pol.unit_price)
7382                 * DECODE(poh.rate, 0, 1, null, 1, ROUND(poh.rate,5)), 5)
7383         , poh.rate_date
7384         , poh.rate
7385         , poh.currency_code
7386         , poh.rate_type
7387         , poll.price_discount
7388         , DECODE(poll.shipment_type,
7389             'PRICE BREAK', DECODE(poll.ship_to_location_id,
7390                              NULL, poll.price_override ,pol.unit_price),
7391             'QUOTATION', DECODE(poll.ship_to_location_id,
7392                              NULL,poll.price_override, pol.unit_price),
7393             pol.unit_price)
7394         , DECODE(poll.line_location_id, NULL, pol.unit_meas_lookup_code, poll.unit_meas_lookup_code)
7395   FROM po_headers poh, po_lines pol, po_line_locations poll
7396   WHERE poh.po_header_id = p_header_id
7397     AND poh.po_header_id = pol.po_header_id
7398     AND pol.line_num = p_line_num
7399     AND pol.po_line_id = poll.po_line_id (+)
7400     AND (p_required_curr IS NULL or poh.currency_code = p_required_curr)
7401     AND (p_required_rate_type is null or poh.rate_type = p_required_rate_type)
7402     AND NVL(poll.unit_meas_lookup_code, NVL(p_uom, pol.unit_meas_lookup_code))
7403           = NVL(p_uom, pol.unit_meas_lookup_code)
7404     AND trunc(sysdate) BETWEEN NVL(poll.start_date, trunc(sysdate)) AND NVL(poll.end_date, trunc(sysdate))
7405     AND poll.quantity <= p_qty
7406   ORDER BY 1 ASC;
7407 
7408 
7409 BEGIN
7410 
7411   d_progress := 0;
7412   IF (PO_LOG.d_proc) THEN
7413     PO_LOG.proc_begin(d_module);
7414     PO_LOG.proc_begin(d_module, 'p_src_doc_header_id', p_src_doc_header_id);
7415     PO_LOG.proc_begin(d_module, 'p_src_doc_line_num', p_src_doc_line_num);
7416     PO_LOG.proc_begin(d_module, 'p_deliver_to_loc_id', p_deliver_to_loc_id);
7417     PO_LOG.proc_begin(d_module, 'p_required_currency', p_required_currency);
7418     PO_LOG.proc_begin(d_module, 'p_required_rate_type', p_required_rate_type);
7419     PO_LOG.proc_begin(d_module, 'p_quantity', p_quantity);
7420     PO_LOG.proc_begin(d_module, 'p_uom', p_uom);
7421   END IF;
7422 
7423   d_progress := 5;
7424 
7425   l_quantity := NVL(p_quantity, -1);
7426 
7427   IF (PO_LOG.d_stmt) THEN
7428     PO_LOG.stmt(d_module, d_progress, 'l_quanity', l_quantity);
7429   END IF;
7430 
7431   d_progress := 10;
7432 
7433   BEGIN
7434 
7435     SELECT hrl.ship_to_location_id
7436     INTO l_ship_to_loc_id
7437     FROM hr_locations hrl
7438     WHERE hrl.location_id = p_deliver_to_loc_id;
7439 
7440     d_progress := 15;
7441 
7442   EXCEPTION
7443     WHEN no_data_found THEN
7444       d_progress := 17;
7445       l_ship_to_loc_id := NULL;
7446   END;
7447 
7448   IF (PO_LOG.d_stmt) THEN
7449     PO_LOG.stmt(d_module, d_progress, 'l_ship_to_loc_id', l_ship_to_loc_id);
7450   END IF;
7451 
7452   d_progress := 20;
7453 
7454   OPEN loc_unit_price_cur( p_src_doc_header_id, p_src_doc_line_num
7455                          , p_required_currency, p_required_rate_type
7456                          , p_uom, l_quantity, l_ship_to_loc_id);
7457 
7458   d_progress := 25;
7459 
7460   FETCH loc_unit_price_cur INTO
7461      x_base_price
7462   ,  x_rate_date
7463   ,  x_rate
7464   ,  x_currency_code
7465   ,  x_rate_type
7466   ,  x_discount
7467   ,  x_currency_price
7468   ,  x_return_uom;
7469 
7470   IF (loc_unit_price_cur%NOTFOUND)
7471   THEN
7472     l_do_non_loc_cursor := TRUE;
7473   ELSE
7474     l_do_non_loc_cursor := FALSE;
7475   END IF;
7476 
7477   d_progress := 30;
7478   IF (PO_LOG.d_stmt) THEN
7479     PO_LOG.stmt(d_module, d_progress, 'l_do_non_loc_cursor', l_do_non_loc_cursor);
7480   END IF;
7481 
7482   IF loc_unit_price_cur%ISOPEN THEN
7483     CLOSE loc_unit_price_cur;
7484   END IF;
7485 
7486   IF (l_do_non_loc_cursor)
7487   THEN
7488 
7489     d_progress := 40;
7490 
7491     OPEN unit_price_cur( p_src_doc_header_id, p_src_doc_line_num
7492                        , p_required_currency, p_required_rate_type
7493                        , p_uom, l_quantity);
7494 
7495     d_progress := 45;
7496 
7497     FETCH unit_price_cur INTO
7498        x_base_price
7499     ,  x_rate_date
7500     ,  x_rate
7501     ,  x_currency_code
7502     ,  x_rate_type
7503     ,  x_discount
7504     ,  x_currency_price
7505     ,  x_return_uom;
7506 
7507     --<Bug 4654432 Start>
7508     -- Commenting out the following IF block because this is not really an
7509     -- unhandled exception condition. IF no data is found from the above
7510     -- cursor, then the price would not default, but the API should return
7511     -- without error.
7512     --IF (unit_price_cur%NOTFOUND)
7513     --THEN
7514     --  l_ret_sts := 'U';
7515     --END IF;
7516     --<Bug 4654432 End>
7517 
7518     IF unit_price_cur%ISOPEN THEN
7519       CLOSE unit_price_cur;
7520     END IF;
7521 
7522   END IF;  -- IF l_do_non_loc_cursor
7523 
7524   d_progress := 50;
7525 
7526   x_return_status := l_ret_sts;
7527   IF (PO_LOG.d_proc) THEN
7528     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
7529     PO_LOG.proc_end(d_module, 'x_return_uom', x_return_uom);
7530     PO_LOG.proc_end(d_module, 'x_base_price', x_base_price);
7531     PO_LOG.proc_end(d_module, 'x_currency_price', x_currency_price);
7532     PO_LOG.proc_end(d_module, 'x_discount', x_discount);
7533     PO_LOG.proc_end(d_module, 'x_currency_code', x_currency_code);
7534     PO_LOG.proc_end(d_module, 'x_rate_type', x_rate_type);
7535     PO_LOG.proc_end(d_module, 'x_rate_date', x_rate_date);
7536     PO_LOG.proc_end(d_module, 'x_rate', x_rate);
7537     PO_LOG.proc_end(d_module);
7538   END IF;
7539 
7540 EXCEPTION
7541   WHEN OTHERS THEN
7542 
7543     IF loc_unit_price_cur%ISOPEN THEN
7544       CLOSE loc_unit_price_cur;
7545     END IF;
7546 
7547     IF unit_price_cur%ISOPEN THEN
7548       CLOSE unit_price_cur;
7549     END IF;
7550 
7551     x_return_status := 'U';
7552 
7553     IF (PO_LOG.d_exc) THEN
7554       PO_LOG.exc(d_module, d_progress, SQLCODE || SQLERRM);
7555       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
7556       PO_LOG.proc_end(d_module);
7557     END IF;
7558 
7559     RETURN;
7560 END get_default_price;
7561 
7562 
7563 -- <Doc Manager Rewrite 11.5.11 End>
7564 
7565 -- <HTML Orders R12 Start>
7566 --------------------------------------------------------------------------------
7567 --Start of Comments
7568 --Name: flag_to_boolean
7569 --Pre-reqs:
7570 --  None.
7571 --Modifies:
7572 --  None.
7573 --Locks:
7574 --  None.
7575 --Function:
7576 --  Converts the given flag value (i.e. 'Y' or 'N') to a boolean.
7577 --  This is useful when invoking PL/SQL procedures from Java, since the
7578 --  OracleCallableStatement does not support boolean parameters.
7579 --Returns:
7580 --  TRUE if the flag value is 'Y', FALSE otherwise.
7581 --End of Comments
7582 --------------------------------------------------------------------------------
7583 FUNCTION flag_to_boolean (
7584   p_flag_value IN VARCHAR2
7585 ) RETURN BOOLEAN
7586 IS
7587 BEGIN
7588   RETURN (p_flag_value = 'Y');
7589 END flag_to_boolean;
7590 
7591 --------------------------------------------------------------------------------
7592 --Start of Comments
7593 --Name: boolean_to_flag
7594 --Pre-reqs:
7595 --  None.
7596 --Modifies:
7597 --  None.
7598 --Locks:
7599 --  None.
7600 --Function:
7601 --  Converts the given boolean to a flag value (i.e. 'Y' or 'N').
7602 --  This is useful when invoking PL/SQL procedures from Java, since the
7603 --  OracleCallableStatement does not support boolean parameters.
7604 --Returns:
7605 --  'Y' if the boolean value is TRUE, 'N' otherwise.
7606 --End of Comments
7607 --------------------------------------------------------------------------------
7608 FUNCTION boolean_to_flag (
7609   p_boolean_value IN BOOLEAN
7610 ) RETURN VARCHAR2
7611 IS
7612 BEGIN
7613   IF (p_boolean_value) THEN
7614     RETURN 'Y';
7615   ELSE
7616     RETURN 'N';
7617   END IF;
7618 END;
7619 -- <HTML Orders R12 End>
7620 
7621 -- <R12 SLA>
7622 -------------------------------------------------------------------------------
7623 --Start of Comments
7624 --Name: Check_Federal_Instance
7625 --Pre-reqs:
7626 --  None.
7627 --Modifies:
7628 --  None.
7629 --Locks:
7630 --  None.
7631 --Function:
7632 -- This function will identify whether the instance identified by the Operating
7633 -- Unit specified by p_org_id is a federal instance or not.
7634 --Parameters:
7635 --IN:
7636 -- p_org_id
7637 --  This contains the org_id for which it needs to be verified whether it is
7638 --  a federal instance or not.
7639 --IN OUT:
7640 -- None
7641 --OUT:
7642 -- None
7643 --Returns:
7644 -- Returns 'Y'/'N'.
7645 --Notes:
7646 --
7647 --Testing:
7648 --
7649 --End of Comments
7650 -------------------------------------------------------------------------------
7651 FUNCTION Check_Federal_Instance (
7652   p_org_id        IN        hr_operating_units.organization_id%type
7653 )
7654 RETURN VARCHAR2
7655 IS
7656   x_progress      VARCHAR2(3) := NULL;
7657   x_option_value  VARCHAR2(1);
7658 BEGIN
7659   x_progress := 10;
7660 
7661   x_option_value := Boolean_To_Flag (FV_INSTALL.enabled(p_org_id)) ;
7662 
7663   RETURN(x_option_value);
7664 
7665 EXCEPTION
7666   WHEN OTHERS THEN
7667      po_message_s.sql_error('Check_Federal_Customer', x_progress, sqlcode);
7668   RAISE;
7669 END Check_Federal_Instance;
7670 
7671 --------------------------------------------------------------------------
7672 --Start of Comments
7673 --Name: GET_OUTSOURCED_ASSEMBLY
7674 --Pre-reqs:  None.
7675 --Modifies:  None.
7676 --Locks:     None.
7677 --Function:
7678 --         This function returns OUTSOURCED_ASSEMBLY value by calling the
7679 --         JMF_SHIKYU_GRP.Validate_Osa_Flag procedure.
7680 --         OUTSOURCED_ASSEMBLY will be either 1 (SHIKYU) or 2 (NON-SHIKYU).
7681 --IN:	p_item_id
7682 --	This contains unique item identifier.
7683 --IN:	p_ship_to_org_id
7684 --	This contains the ship_to_org_id value.
7685 --IN OUT:  None.
7686 --OUT:	   None.
7687 --Returns: The function returns OUTSOURCED_ASSEMBLY.
7688 --Notes:
7689 -- The algorithm of the function is as follows :
7690 -- Returns OUTSOURCED_ASSEMBLY for a given item_id and ship_to_org_id by calling
7691 -- JMF_SHIKYU_GRP.Validate_Osa_Flag procedure.
7692 --Testing:
7693 --
7694 --End of Comments
7695 -------------------------------------------------------------------------------
7696 FUNCTION get_outsourced_assembly( p_item_id IN NUMBER,
7697 				  p_ship_to_org_id IN NUMBER ) RETURN NUMBER IS
7698 	l_osa_flag varchar2(1) := 'N';
7699 	l_return_status varchar2(1);
7700 	l_outsourced_assembly NUMBER ;
7701 	l_progress      VARCHAR2(3) := NULL;
7702         l_msg_count     NUMBER;
7703         l_msg_data           VARCHAR2(2000);
7704 
7705 BEGIN
7706 
7707 	l_progress := 10;
7708 
7709         -- bug4958421
7710         -- do not call JMF API if item id is NULL
7711 
7712         IF (p_item_id IS NULL) THEN
7713           l_outsourced_assembly := 2;
7714 
7715         ELSE
7716 
7717           JMF_SHIKYU_GRP.Validate_Osa_Flag(
7718                                        p_api_version  =>  1.0,
7719                                        p_init_msg_list  => FND_API.G_FALSE,
7720                                        x_return_status =>  l_return_status,
7721                                        x_msg_count => l_msg_count,
7722                                        x_msg_data   => l_msg_data,
7723                                        p_inventory_item_id => p_item_id,
7724                                        p_vendor_id=> NULL,
7725                                        p_vendor_site_id => NULL ,
7726                                        p_ship_to_organization_id => p_ship_to_org_id,
7727                                        x_osa_flag => l_osa_flag );
7728 
7729 
7730           IF l_osa_flag = 'Y' THEN
7731                   l_outsourced_assembly := 1;
7732           ELSE
7733                   l_outsourced_assembly := 2;
7734           END IF;
7735 
7736         END IF;
7737 
7738 	return l_outsourced_assembly;
7739 
7740 	EXCEPTION
7741 	  WHEN OTHERS THEN
7742 		po_message_s.sql_error('Get_Outsourced_Assembly', l_progress, sqlcode);
7743 		return 2;
7744 END get_outsourced_assembly;
7745 
7746 --<R12 eTax Integration Start>
7747 -------------------------------------------------------------------------------
7748 --Start of Comments
7749 --Name: get_default_legal_entity_id
7750 --Pre-reqs:
7751 --  None
7752 --Modifies:
7753 --  None
7754 --Locks:
7755 --  None
7756 --Function:
7757 --  Return the default legal context given the org id
7758 --Parameters:
7759 --IN:
7760 --org_id
7761 --  Unique id for the operating unit
7762 --Returns:
7763 --  Default Legal Entity Id
7764 --End of Comments
7765 -------------------------------------------------------------------------------
7766 FUNCTION get_default_legal_entity_id(p_org_id IN NUMBER) RETURN NUMBER
7767 IS
7768   l_legal_entity_id NUMBER;
7769   l_module_name CONSTANT VARCHAR2(100) := 'GET_DEFAULT_LEGAL_ENTITY_ID';
7770   d_module_base CONSTANT VARCHAR2(100) := PO_LOG.get_subprogram_base(
7771                                             g_log_head, l_module_name);
7772   d_progress NUMBER;
7773 BEGIN
7774   IF (PO_LOG.d_proc) THEN
7775     PO_LOG.proc_begin(d_module_base);
7776     PO_LOG.proc_begin(d_module_base, 'p_org_id', p_org_id);
7777   END IF;
7778 
7779   d_progress := 0;
7780   l_legal_entity_id := XLE_UTILITIES_GRP.Get_DefaultLegalContext_OU(p_org_id);
7781 
7782   d_progress := 10;
7783   IF (PO_LOG.d_proc) THEN
7784     PO_LOG.proc_end(d_module_base);
7785     PO_LOG.proc_end(d_module_base, 'l_legal_entity_id', l_legal_entity_id);
7786   END IF;
7787 
7788   d_progress := 20;
7789   return l_legal_entity_id;
7790 EXCEPTION
7791   WHEN OTHERS THEN
7792     IF (PO_LOG.d_exc) THEN
7793       PO_LOG.exc(d_module_base, d_progress, SQLCODE || SQLERRM);
7794       PO_LOG.proc_end(d_module_base);
7795       PO_LOG.proc_end(d_module_base, 'l_legal_entity_id', l_legal_entity_id);
7796     END IF;
7797 
7798     RAISE;
7799 END get_default_legal_entity_id;
7800 --<R12 eTax Integration End>
7801 
7802 --<HTML Agreements R12 Start>
7803 -------------------------------------------------------------------------------
7804 --Start of Comments
7805 --Name: get_last_update_date_for_doc
7806 --Pre-reqs:
7807 --  None
7808 --Modifies:
7809 --  None
7810 --Locks:
7811 --  None
7812 --Function:
7813 --  It returns the max of the last_update_date column from all the levels for a
7814 --  given document
7815 --Parameters:
7816 --IN:
7817 --p_doc_header_id
7818 --  Document header Id
7819 --Returns:
7820 --  latest last_update_date for the entire document
7821 --End of Comments
7822 -------------------------------------------------------------------------------
7823 FUNCTION get_last_update_date_for_doc(p_doc_header_id IN NUMBER) RETURN DATE
7824 IS
7825   header_last_update_date PO_HEADERS_ALL.last_update_date%type;
7826   line_last_update_date PO_LINES_ALL.last_update_date%type;
7827   line_loc_last_update_date PO_LINE_LOCATIONS_ALL.last_update_date%type;
7828   dist_last_update_date PO_DISTRIBUTIONS_ALL.last_update_date%type;
7829   doc_last_update_date PO_HEADERS_ALL.last_update_date%type;
7830   --Lowest date supported by database as we need to handle null dates
7831   min_date PO_HEADERS_ALL.last_update_date%type := to_date('01/01/-4712','DD/MM/SYYYY');
7832 
7833   l_module_name CONSTANT VARCHAR2(100) := 'GET_LAST_UPDATE_DATE_FOR_DOC';
7834   d_module_base CONSTANT VARCHAR2(100) := PO_LOG.get_subprogram_base(g_log_head, l_module_name);
7835   d_pos NUMBER := 0;
7836 
7837 BEGIN
7838   IF (PO_LOG.d_proc) THEN
7839     PO_LOG.proc_begin(d_module_base); PO_LOG.proc_begin(d_module_base, 'p_doc_header_id', p_doc_header_id);
7840   END IF;
7841   d_pos := 10;
7842   select nvl(max(last_update_date), min_date) into header_last_update_date from po_headers_all where po_header_id = p_doc_header_id;
7843   d_pos := 20;
7844   select nvl(max(last_update_date), min_date) into line_last_update_date from  po_lines_all where po_header_id = p_doc_header_id;
7845   d_pos := 30;
7846   select nvl(max(last_update_date), min_date) into line_loc_last_update_date from po_line_locations_all where po_header_id = p_doc_header_id;
7847   d_pos := 40;
7848   select nvl(max(last_update_date), min_date)  into dist_last_update_date from  po_distributions_all where po_header_id = p_doc_header_id;
7849   d_pos := 50;
7850   doc_last_update_date := greatest( header_last_update_date
7851                                    ,line_last_update_date
7852                                    ,line_loc_last_update_date
7853                                    ,dist_last_update_date);
7854 
7855   IF (PO_LOG.d_stmt) THEN
7856     PO_LOG.stmt(d_module_base, d_pos, 'doc_last_update_date', doc_last_update_date);
7857   END IF;
7858   d_pos := 60;
7859   IF (PO_LOG.d_proc) THEN
7860     PO_LOG.proc_end(d_module_base);
7861   END IF;
7862   return(doc_last_update_date);
7863 EXCEPTION
7864   WHEN OTHERS THEN
7865     FND_MSG_PUB.add_exc_msg(g_pkg_name, l_module_name|| ':'||d_pos);
7866     IF (PO_LOG.d_exc) THEN
7867       PO_LOG.exc(d_module_base, d_pos, SQLCODE || SQLERRM);
7868     END IF;
7869     RAISE;
7870 END;
7871 --<HTML Agreements R12 End>
7872 
7873 -- <ACHTML R12 START>
7874 -------------------------------------------------------------------------------
7875 --Start of Comments
7876 --Name:  is_equal
7877 --Pre-reqs:
7878 --  None
7879 --Modifies:
7880 --  None
7881 --Locks:
7882 --  None
7883 --Function:
7884 --  Determines whether two numbers are equal. They are equal if they are both
7885 --  null or have the same value.
7886 --Parameters:
7887 --IN:
7888 --p_attribute_primary
7889 --  The first number to compare with.
7890 --p_attribute_secondary
7891 --  The second number to compare with.
7892 --RETURNS:
7893 --  Boolean TRUE or FALSE indicating whether the two numbers are equal.
7894 --Notes:
7895 --  None
7896 --Testing:
7897 --  None
7898 --End of Comments
7899 -------------------------------------------------------------------------------
7900 FUNCTION is_equal(
7901   p_attribute_primary NUMBER,
7902   p_attribute_secondary NUMBER
7903 ) RETURN BOOLEAN
7904 IS
7905   l_is_equal                    BOOLEAN;
7906 BEGIN
7907   IF ((p_attribute_primary IS NULL AND p_attribute_secondary IS NULL)
7908       OR p_attribute_primary = p_attribute_secondary)
7909   THEN
7910     l_is_equal := TRUE;
7911   ELSE
7912     l_is_equal := FALSE;
7913   END IF;
7914 
7915   RETURN l_is_equal;
7916 EXCEPTION
7917   WHEN OTHERS THEN
7918     RETURN FALSE;
7919 END is_equal;
7920 -- <ACHTML R12 END>
7921 
7922 -- <ACHTML R12 START>
7923 -------------------------------------------------------------------------------
7924 --Start of Comments
7925 --Name:  is_equal
7926 --Pre-reqs:
7927 --  None
7928 --Modifies:
7929 --  None
7930 --Locks:
7931 --  None
7932 --Function:
7933 --  Determines whether two varchars are equal. They are equal if they are both
7934 --  null or have the same value.
7935 --Parameters:
7936 --IN:
7937 --p_attribute_primary
7938 --  The first varchar to compare with.
7939 --p_attribute_secondary
7940 --  The second varchar to compare with.
7941 --RETURNS:
7942 --  Boolean TRUE or FALSE indicating whether the two varchars are equal.
7943 --Notes:
7944 --  None
7945 --Testing:
7946 --  None
7947 --End of Comments
7948 -------------------------------------------------------------------------------
7949 FUNCTION is_equal(
7950   p_attribute_primary VARCHAR2,
7951   p_attribute_secondary VARCHAR2
7952 ) RETURN BOOLEAN
7953 IS
7954   l_is_equal                    BOOLEAN;
7955 BEGIN
7956   IF ((p_attribute_primary IS NULL AND p_attribute_secondary IS NULL)
7957       OR p_attribute_primary = p_attribute_secondary)
7958   THEN
7959     l_is_equal := TRUE;
7960   ELSE
7961     l_is_equal := FALSE;
7962   END IF;
7963 
7964   RETURN l_is_equal;
7965 EXCEPTION
7966   WHEN OTHERS THEN
7967     RETURN FALSE;
7968 END is_equal;
7969 -- <ACHTML R12 END>
7970 
7971 -- <ACHTML R12 START>
7972 -------------------------------------------------------------------------------
7973 --Start of Comments
7974 --Name:  is_equal_minutes
7975 --Pre-reqs:
7976 --  None
7977 --Modifies:
7978 --  None
7979 --Locks:
7980 --  None
7981 --Function:
7982 --  Determines whether two dates are equal up to the minute. They are equal if
7983 -- they are both null or have a difference of less than a minute.
7984 --Parameters:
7985 --IN:
7986 --p_attribute_primary
7987 --  The first date to compare with.
7988 --p_attribute_secondary
7989 --  The second date to compare with.
7990 --RETURNS:
7991 --  Boolean TRUE or FALSE indicating whether the two dates are equal up to the
7992 --  minute.
7993 --Notes:
7994 --  None
7995 --Testing:
7996 --  None
7997 --End of Comments
7998 -------------------------------------------------------------------------------
7999 FUNCTION is_equal_minutes(
8000   p_attribute_primary DATE,
8001   p_attribute_secondary DATE
8002 ) RETURN BOOLEAN
8003 IS
8004   l_is_equal                    BOOLEAN;
8005 BEGIN
8006   IF ((p_attribute_primary IS NULL AND p_attribute_secondary IS NULL)
8007       OR trunc(p_attribute_primary, 'MI') = trunc(p_attribute_secondary, 'MI'))
8008   THEN
8009     l_is_equal := TRUE;
8010   ELSE
8011     l_is_equal := FALSE;
8012   END IF;
8013 
8014   RETURN l_is_equal;
8015 EXCEPTION
8016   WHEN OTHERS THEN
8017     RETURN FALSE;
8018 END is_equal_minutes;
8019 -- <ACHTML R12 END>
8020 
8021 END PO_CORE_S;