DBA Data[Home] [Help]

PACKAGE BODY: APPS.PO_DOCUMENT_CHECKS_PVT

Source


1 PACKAGE BODY PO_DOCUMENT_CHECKS_PVT AS
2 /* $Header: POXVDCKB.pls 120.64.12010000.11 2009/02/10 06:07:31 rakchakr ship $*/
3 
4 
5 
6 
7 -----------------------------------------------------------------------------
8 -- Define private package constants.
9 -----------------------------------------------------------------------------
10 
11 -- This is used as a delimiter in constructing the error msgs
12 g_delim CONSTANT VARCHAR2(1) := ' ';
13 
14 --Contains message 'Line#'
15 g_linemsg CONSTANT VARCHAR2(75) := substr(FND_MESSAGE.GET_STRING('PO', 'PO_ZMVOR_LINE'), 1,25);
16 
17 --Contains message 'Schedule#'
18 g_shipmsg CONSTANT VARCHAR2(75) := substr(FND_MESSAGE.GET_STRING('PO', 'PO_ZMVOR_SCHEDULE'), 1,25);
19 
20 --Contains message 'Distribution#'
21 g_distmsg CONSTANT VARCHAR2(75) := substr(FND_MESSAGE.GET_STRING('PO', 'PO_ZMVOR_DISTRIBUTION'), 1,25);
22 
23 --Contains message 'Price Break#'
24 g_price_breakmsg CONSTANT VARCHAR2(75) := substr(FND_MESSAGE.GET_STRING('PO', 'PO_ZMVOR_PRICE_BREAK'), 1,25);
25 
26 --Contains message 'Quantity'
27 g_qtymsg CONSTANT VARCHAR2(75) := substr(FND_MESSAGE.GET_STRING('PO', 'PO_SUB_TOKEN_QUANTITY'),1,25);
28 
29 --<Bug 2790228>
30 --Contains message 'Shipment Quantity'
31 g_shipqtymsg CONSTANT VARCHAR2(40) := substrb(FND_MESSAGE.GET_STRING('PO', 'PO_SUB_TOKEN_SHIP_QUANTITY'),1,40);
32 
33 --Contains message 'Distribution Quantity'
34 g_distqtymsg CONSTANT VARCHAR2(40) := substrb(FND_MESSAGE.GET_STRING('PO', 'PO_SUB_TOKEN_DIST_QUANTITY'),1,40);
35 
36 G_PKG_NAME CONSTANT varchar2(30) := 'PO_DOCUMENT_CHECKS_PVT';
37 
38 g_log_head    CONSTANT VARCHAR2(50) := 'po.plsql.'|| G_PKG_NAME || '.';
39 
40 
41 
42 
43 -----------------------------------------------------------------------------
44 -- Declare private package variables.
45 -----------------------------------------------------------------------------
46 
47 -- Refactored debugging
48 g_debug_stmt  CONSTANT BOOLEAN := (PO_DEBUG.is_debug_stmt_on And (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL));   /* In Bug# 5028960, Modified to Avoid File.sql.46 error*/
49 g_debug_unexp CONSTANT BOOLEAN := (PO_DEBUG.is_debug_unexp_on AND (FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL)); /* In Bug# 5028960, Modified to Avoid File.sql.46 error*/
50 
51 
52 
53 
54 --------------------------------------------------------------------------------
55 -- Forward procedure declarations
56 --------------------------------------------------------------------------------
57 
58 PROCEDURE populate_global_temp_tables(
59    x_return_status                  OUT NOCOPY     VARCHAR2
60 ,  p_doc_type                       IN             VARCHAR2
61 ,  p_doc_level                      IN             VARCHAR2
62 ,  p_doc_level_id                   IN             NUMBER
63 );
64 
65 -- <Doc Manager Rewrite 11.5.11 Start>
66 PROCEDURE populate_po_lines_gt(
67    p_doc_type                       IN             VARCHAR2
68 ,  p_doc_level                      IN             VARCHAR2
69 ,  p_doc_level_id                   IN             NUMBER
70 ,  x_return_status                  OUT NOCOPY     VARCHAR2
71 );
72 -- <Doc Manager Rewrite 11.5.11 End>
73 
74 PROCEDURE populate_line_locations_gt(
75    p_doc_type                       IN             VARCHAR2
76 ,  p_doc_level                      IN             VARCHAR2
77 ,  p_doc_level_id                   IN             NUMBER
78 );
79 
80 PROCEDURE populate_distributions_gt(
81    p_doc_type                       IN             VARCHAR2
82 ,  p_doc_level                      IN             VARCHAR2
83 ,  p_doc_level_id                   IN             NUMBER
84 );
85 
86 PROCEDURE check_unreserve(
87    p_online_report_id               IN             NUMBER
88 ,  p_document_type                  IN             VARCHAR2 --Bug#5462677
89 ,  p_document_subtype               IN             VARCHAR2 --Bug#5462677
90 ,  p_document_level                 IN             VARCHAR2 --Bug#5462677
91 ,  p_doc_level_id                   IN             NUMBER   --Bug#5462677
92 ,  p_user_id                        IN             NUMBER
93 ,  p_login_id                       IN             NUMBER
94 ,  p_sequence                       IN OUT NOCOPY  NUMBER
95 );
96 
97 PROCEDURE check_gl_date(
98    p_doc_type                       IN             VARCHAR2
99 ,  p_online_report_id               IN             NUMBER
100 ,  p_login_id                       IN             NUMBER
101 ,  p_user_id                        IN             NUMBER
102 ,  p_sequence                       IN OUT NOCOPY  NUMBER
103 );
104 
105 PROCEDURE check_blanket_agreement(p_document_id IN NUMBER,
106                        p_online_report_id IN NUMBER,
107                        p_user_id IN NUMBER,
108                        p_login_id IN NUMBER,
109                        p_check_asl IN BOOLEAN,                     -- <2757450>
110                        p_sequence IN OUT NOCOPY NUMBER,
111                        x_return_status OUT NOCOPY VARCHAR2);
112 
113 -- bug3592160 START
114 PROCEDURE complete_po_header_id_tbl
115 ( p_count            IN NUMBER,
116   p_header_id        IN PO_TBL_NUMBER,
117   p_release_id       IN PO_TBL_NUMBER,
118   p_vendor_order_num IN PO_TBL_VARCHAR30,
119   p_document_num     IN PO_TBL_VARCHAR30,
120   p_type_lookup_code IN PO_TBL_VARCHAR30,
121   x_header_id        OUT NOCOPY PO_TBL_NUMBER
122 );
123 -- bug3592160 END
124 
125 -- <Doc Manager Rewrite 11.5.11 Start>
126 PROCEDURE check_final_close(
127    p_document_type        IN VARCHAR2
128 ,  p_document_subtype     IN VARCHAR2
129 ,  p_document_level       IN VARCHAR2
130 ,  p_document_id          IN NUMBER
131 ,  p_online_report_id     IN NUMBER
132 ,  p_user_id              IN NUMBER
133 ,  p_login_id             IN NUMBER
134 ,  p_origin_doc_id        IN NUMBER := NULL --Bug#5462677
135 ,  p_doc_level_id         IN NUMBER
136 ,  p_sequence             IN OUT NOCOPY NUMBER
137 ,  x_return_status        OUT NOCOPY VARCHAR2
138 );
139 
140 PROCEDURE check_rcv_trans_interface(
141    p_document_type        IN VARCHAR2
142 ,  p_document_level       IN VARCHAR2  --<Bug 4118145, Issue 7>: Corrected type
143 ,  p_online_report_id     IN NUMBER
144 ,  p_user_id              IN NUMBER
145 ,  p_login_id             IN NUMBER
146 ,  p_document_id          IN NUMBER
147 ,  p_sequence             IN OUT NOCOPY NUMBER
148 ,  x_return_status        OUT NOCOPY VARCHAR2
149 );
150 
151 PROCEDURE check_asn_not_fully_received(
152    p_document_type        IN VARCHAR2
153 ,  p_online_report_id     IN NUMBER
154 ,  p_user_id              IN NUMBER
155 ,  p_login_id             IN NUMBER
156 ,  p_sequence             IN OUT NOCOPY NUMBER
157 ,  x_return_status        OUT NOCOPY VARCHAR2
158 );
159 
160 PROCEDURE check_qty_rcv_but_not_deliv(
161    p_document_type        IN VARCHAR2
162 ,  p_online_report_id     IN NUMBER
163 ,  p_user_id              IN NUMBER
164 ,  p_login_id             IN NUMBER
165 ,  p_sequence             IN OUT NOCOPY NUMBER
166 ,  x_return_status        OUT NOCOPY VARCHAR2
167 );
168 
169 PROCEDURE check_amt_rcv_but_not_deliv(
170    p_document_type        IN VARCHAR2
171 ,  p_online_report_id     IN NUMBER
172 ,  p_user_id              IN NUMBER
173 ,  p_login_id             IN NUMBER
174 ,  p_sequence             IN OUT NOCOPY NUMBER
175 ,  x_return_status        OUT NOCOPY VARCHAR2
176 );
177 
178 PROCEDURE check_invalid_acct_flex(
179    p_document_type        IN VARCHAR2
180 ,  p_action_requested     IN VARCHAR2
181 ,  p_action_date          IN DATE
182 ,  p_online_report_id     IN NUMBER
183 ,  p_user_id              IN NUMBER
184 ,  p_login_id             IN NUMBER
185 ,  p_document_id          IN NUMBER
186 ,  p_sequence             IN OUT NOCOPY NUMBER
187 ,  x_return_status        OUT NOCOPY VARCHAR2
188 );
189 
190 PROCEDURE check_bpa_has_open_release(
191    p_online_report_id     IN NUMBER
192 ,  p_user_id              IN NUMBER
193 ,  p_login_id             IN NUMBER
194 ,  p_sequence             IN OUT NOCOPY NUMBER
195 ,  x_return_status        OUT NOCOPY VARCHAR2
196 );
197 
198 PROCEDURE check_bpa_has_open_stdref(
199    p_online_report_id     IN NUMBER
200 ,  p_user_id              IN NUMBER
201 ,  p_login_id             IN NUMBER
202 ,  p_sequence             IN OUT NOCOPY NUMBER
203 ,  x_return_status        OUT NOCOPY VARCHAR2
204 );
205 
206 PROCEDURE check_cpa_has_open_stdref(
207    p_online_report_id     IN NUMBER
208 ,  p_user_id              IN NUMBER
209 ,  p_login_id             IN NUMBER
210 ,  p_sequence             IN OUT NOCOPY NUMBER
211 ,  x_return_status        OUT NOCOPY VARCHAR2
212 );
213 
214 PROCEDURE check_ppo_has_open_release(
215    p_online_report_id     IN NUMBER
216 ,  p_user_id              IN NUMBER
217 ,  p_login_id             IN NUMBER
218 ,  p_sequence             IN OUT NOCOPY NUMBER
219 ,  x_return_status        OUT NOCOPY VARCHAR2
220 );
221 
222 --<Complex Work R12 START>
223 PROCEDURE check_po_qty_amt_rollup(
224    p_online_report_id     IN NUMBER
225 ,  p_document_id          IN NUMBER
226 ,  p_login_id             IN NUMBER
227 ,  p_user_id              IN NUMBER
228 ,  x_sequence             IN OUT NOCOPY NUMBER
229 );
230 PROCEDURE check_unvalidated_invoices(p_document_type    IN VARCHAR2,
231                                      p_document_subtype     IN VARCHAR2,
232                                      p_action_requested IN VARCHAR2,
233                                      p_action_date      IN DATE,
234                                      p_online_report_id IN NUMBER,
235                                      p_user_id          IN NUMBER,
236                                      p_login_id         IN NUMBER,
237                                      p_document_level   IN VARCHAR2,
238                                      p_origin_doc_id    IN NUMBER,
239                                      p_doc_level_id     IN NUMBER,
240                                      p_sequence         IN OUT NOCOPY NUMBER,
241                                      x_return_status    OUT NOCOPY VARCHAR2);
242 
243 PROCEDURE get_message_info(p_document_type    IN VARCHAR2,
244                            p_document_subtype IN VARCHAR2,
245                            p_action_requested IN VARCHAR2,
246                            p_document_level   IN VARCHAR2,
247                            p_doc_level_id      IN NUMBER,
248                            x_text_line        OUT  NOCOPY VARCHAR2,
249                            x_message_name     OUT  NOCOPY VARCHAR2,
250                            x_invoice_type     OUT  NOCOPY VARCHAR2,
251                            x_calling_sequence OUT  NOCOPY VARCHAR2,
252                            x_return_status    OUT  NOCOPY VARCHAR2);
253 
254 --<Complex Work R12 END>
255 
256 
257 -- <Doc Manager Rewrite 11.5.11 End>
258 
259 /* CONTERMS FPJ START*/
260 -------------------------------------------------------------------------------
261 --Start of Comments
262 --Name: check_terms
263 --Pre-reqs:
264 --  This procedure is dependent on the Contracts group API OKC_TERMS_QA_GRP and
265 --  calls procedure qa_doc. It also subtypes the qa_result_tbl_type to store
266 --  returned error messages.
267 --Modifies:
268 --  po_online_report_text_gt
269 --Locks:
270 --  None.
271 --Function:
272 -- This procedure is used to validate contract articles and deliverables and is
273 -- called during the submission check. The contracts APi qa_doc is called in
274 -- normal or amend mode based on the status of the document. The resulting errors/
275 -- warnings if any are stored in the global temp table.
276 --Parameters:
277 --  Otherwise, include the IN:, IN OUT:, and/or OUT: sections as needed.
278 --IN:
279 --p_document_id
280 --  po_header_id
281 --p_document_type
282 --  po document type PO or PA
283 --p_document_subtype
284 --  po_document subtype 'STANDARD', 'CONTRACT' or 'BLANKET'
285 --p_online_report_id
286 --  unique error report id
287 --p_user_id
288 --  user id
289 --p_login_id
290 --  login id
291 --IN OUT:
292 --p_sequence
293 --  Description of why/how the parameter is used.
294 --OUT:
295 --x_return_status
296 --  return status of the procedure
297 --Notes:
298 --Testing:
299 --  test the qa doc is called in the correct mode and that the returned errors
300 -- are correctly populated in the error table.
301 --End of Comments
302 -------------------------------------------------------------------------------
303 
304 PROCEDURE check_terms(
305    p_document_id          IN NUMBER,
306    p_document_type        IN VARCHAR2,
307    p_document_subtype     IN VARCHAR2,
308    p_online_report_id     IN NUMBER,
309    p_user_id              IN NUMBER,
310    p_login_id             IN NUMBER,
311    p_sequence             IN OUT NOCOPY NUMBER,
312    x_return_status        OUT NOCOPY VARCHAR2) IS
313 
314 -- declare cursor to fetch the revision, authorization status and approved flag
315 CURSOR c_po_status (p_document_id NUMBER) IS
316   SELECT revision_num
317          ,start_date
318    ,end_date
319   FROM   po_headers_gt
320   WHERE  po_header_id = p_document_id;
321 
322 -- contracts dependency
323 SUBTYPE qa_result_tbl_type IS OKC_TERMS_QA_GRP.qa_result_tbl_type;
324 l_qa_result_tbl qa_result_tbl_type;
325 l_qa_mode VARCHAR2(30);
326 l_contracts_document_type VARCHAR2(150);
327 SUBTYPE Event_tbl_type IS OKC_TERMS_QA_GRP.busdocdates_tbl_type;
328 l_event_tbl Event_tbl_type;
329 
330 
331 l_revision_num po_headers.revision_num%TYPE;
332 l_authorization_status po_headers.authorization_status%TYPE;
333 l_approved_flag po_headers.approved_flag%TYPE;
334 l_po_start_date po_headers.start_date%TYPE;
335 l_po_end_date po_headers.end_date%TYPE;
336 
337 l_api_name CONSTANT VARCHAR2(30) := 'Check Terms';
338 l_progress VARCHAR2(3);
339 l_row_index PLS_INTEGER;
340 
341 l_return_status VARCHAR2(1);
342 l_qa_return_status VARCHAR2(1);
343 l_msg_count NUMBER;
344 l_msg_data VARCHAR2(2000);
345 
346 
347 BEGIN
348 
349   x_return_status := FND_API.G_RET_STS_SUCCESS;
350 
351   -- contracts document type
352   l_contracts_document_type := p_document_type||'_'||p_document_subtype;
353 
354   -- fetch the document status indicators
355   OPEN c_po_status(p_document_id);
356   FETCH c_po_status INTO l_revision_num, l_po_start_date, l_po_end_date;
357   CLOSE c_po_status;
358 
359   -- decode status indicators to decide qa mode
360   -- Migrate PO:
361   -- Now that conterms can be added at any revision of the PO
362   -- We need to check if the current rev is the first one with
363   -- contracts and if so do the normal qa
364   IF (l_revision_num > 0) AND
365      (PO_CONTERMS_UTL_GRP.get_archive_conterms_flag (p_po_header_id => p_document_id) = 'Y')
366   THEN
367     l_qa_mode := 'AMEND';
368   ELSE
369     l_qa_mode := 'NORMAL';
370   END IF;
371 
372   l_event_tbl(1).event_code := 'PO_START_DATE';
373   l_event_tbl(1).event_date := l_po_start_date;
374   l_event_tbl(2).event_code := 'PO_END_DATE';
375   l_event_tbl(2).event_date := l_po_end_date;
376 
377   l_progress := '001';
378 
379   -- call the contracts QA
380   OKC_TERMS_QA_GRP.qa_Doc(
381     p_api_version      => 1.0,
382     p_init_msg_list    => FND_API.G_FALSE,
383     x_return_status    => l_return_status,
384     x_msg_data       => l_msg_data,
385     x_msg_count      => l_msg_count,
386     p_qa_mode        => l_qa_mode,
387     p_doc_type       => l_contracts_document_type,
388     p_doc_id           => p_document_id,
389     p_bus_doc_date_events_tbl => l_event_tbl,
390     x_qa_result_tbl    => l_qa_result_tbl,
391     x_qa_return_status => l_qa_return_status
392   );
393 
394   IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
395     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
396   END IF;
397 
398   -- if successful update error table with the messages if qa returned errors
399   IF (l_qa_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
400     l_progress := '002';
401     l_row_index := l_qa_result_tbl.FIRST;
402     LOOP
403     EXIT WHEN l_row_index IS NULL;
404       INSERT INTO po_online_report_text_gt
405         (online_report_id
406         ,last_updated_by
407         ,last_update_date
408         ,created_by
409         ,creation_date
410         ,line_num
411         ,shipment_num
412         ,distribution_num
413         ,sequence
414         ,text_line
415         ,message_name
416         ,message_type
417         )
418       VALUES
419         (p_online_report_id
420         ,p_login_id
421         ,sysdate
422         ,p_user_id
423         ,sysdate
424         ,0 ,0 ,0
425         ,p_sequence+1
426         ,l_qa_result_tbl(l_row_index).problem_details
427         ,l_qa_result_tbl(l_row_index).message_name
428         ,l_qa_result_tbl(l_row_index).error_severity
429         );
430       l_row_index := l_qa_result_tbl.NEXT(l_row_index);
431       --increment the sequence by the error count
432       p_sequence := p_sequence + 1;
433     END LOOP;
434   END IF;
435 
436 EXCEPTION
437     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
438         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
439     WHEN OTHERS THEN
440         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
441   IF ( g_debug_unexp ) THEN
442             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
443               FND_LOG.string( FND_LOG.level_unexpected,
444                             G_PKG_NAME||l_api_name|| '.others_exception',
445                             'EXCEPTION: Location is '|| l_progress || ' SQL CODE is '||sqlcode);
446             END IF;
447         END IF;
448 
449 END check_terms;
450 /* CONTERMS FPJ END*/
451 
452 /**
453 * Public Procedure: PO_SUBMISSION_CHECK
454 * Requires:
455 *   IN PARAMETERS:
456 *     p_api_version:       Version number of API that caller expects.
457 *     p_action_requested:  The action to perform
458 *     p_document_type:     The type of the document to perform
459 *                          the submission check on.
460 *     p_document_subtype:  The subtype of the document.
461 *                          Valid Document types and Document subtypes are
462 *                          Document Type      Document Subtype
463 *                          REQUISITION  --->
464 *                          RELEASE      --->  SCHEDULED/BLANKET
465 *                          PO           --->  PLANNED/STANDARD
466 *                          PA           --->  CONTRACT/BLANKET
467 --
468 --  <FPJ ENCUMBRANCE>
469 --    The following 2 parameters replace the p_document_id parameter.
470 --p_document_level
471 --  The type of id that is being passed.  Use g_doc_level_<>
472 --  The following is supported for all actions:
473 --    HEADER
474 --  The following are also supported for UNRESERVE checks (PO/RELEASE):
475 --    LINE
476 --    SHIPMENT
477 --    DISTRIBUTION
478 --  The following are also supported for FINAL_CLOSE checks (PO/RELEASE):
479 --    LINE
480 --    SHIPMENT
481 --p_document_level_id
482 --  Id of the doc level type on which to perform the check.
483 --
484 *     p_requested_changes: This object contains all the requested changes to
485 *                          the document. It contains 5 objects. These objects
486 *                          are: 1.Header_Changes 2.Release_Changes 3.Line_
487 *                          Changes 4.Shipment_Changes 5.Distribution_Changes.
488 *                          In FPI, following change requests are allowed:
489 *                          1. HEADER_CHANGES: None
490 *                          2. RELEASE_CHANGES: None
491 *                          3. LINE_CHANGES: unit_price, vendor_product_num
492 *                          4. SHIPMENT_CHANGES: quantity, promised_date,
493 *                             price_override
494 *                          5. DISTRIBUTION_CHANGES: quantity_ordered
495 *     p_check_asl:         Determines whether or not to perform the checks:
496 *                          PO_SUB_ITEM_NOT_APPROVED / PO_SUB_ITEM_ASL_DEBARRED
497 *                          (a) TRUE  : Perform check
498 *                          (b) FALSE : Do not perform check
499 *     p_req_chg_initiator: Caller of the change request if its a change request
500 *
501 * Modifies: Inserts error msgs in online_report_text table, uses global_temp
502 *           tables for processing
503 * Effects:  This procedure runs the document submission checks on passed in
504 *           document.
505 * Returns:
506 *  x_return_status:    FND_API.G_RET_STS_SUCCESS if API succeeds
507 *                      FND_API.G_RET_STS_ERROR if API fails
508 *                      FND_API.G_RET_STS_UNEXP_ERROR if unexpected error occurs
509 *  x_sub_check_status: FND_API.G_RET_STS_SUCCESS if document passes all
510 *                      submission checks, even if warnings are found
511 *                      FND_API.G_RET_STS_ERROR if document fails atleast one
512 *                      submission check
513 *  x_has_warnings:     FND_API.G_TRUE if submission check returns warnings
514 *                      FND_API.G_FALSE if no warnings are found
515 *  x_msg_data:         Contains error msg in case x_return_status returned
516 *                      FND_API.G_RET_STS_ERROR or FND_API.G_RET_STS_UNEXP_ERROR
517 *  x_online_report_id: This id can be used to get all submission check errors
518 *                      for given document from online_report_text table
519 *  x_doc_check_error_record: If x_sub_check_status returned G_RET_STS_ERROR
520 *                      then this object of tables will contain information about
521 *                      all submission check errors for given document including
522 *                      message_name and text_line.
523 *  NOTE: This package does no validation of INPUT parameters. That is taken
524 *        care of in Group package PO_DOCUMENT_CHECKS_GRP
525 */
526 PROCEDURE po_submission_check(
527    p_api_version                    IN             NUMBER
528 ,  p_action_requested               IN             VARCHAR2
529 ,  p_document_type                  IN             VARCHAR2
530 ,  p_document_subtype               IN             VARCHAR2
531 -- <ENCUMBRANCE FPJ> Replaced p_document_id with doc_level, doc_level_id
532 ,  p_document_level                 IN             VARCHAR2
533 ,  p_document_level_id              IN             NUMBER
534 -- <PO_CHANGE_API FPJ> Renamed the type to PO_CHANGES_REC_TYPE:
535 ,  p_requested_changes              IN             PO_CHANGES_REC_TYPE
536 ,  p_check_asl                      IN             BOOLEAN  -- bug 2757450
537 ,  p_req_chg_initiator              IN             VARCHAR2 -- bug 4957243
538 ,  p_origin_doc_id                  IN             NUMBER := NULL --Bug#5462677
539 ,  x_return_status                  OUT NOCOPY     VARCHAR2
540 ,  x_sub_check_status               OUT NOCOPY     VARCHAR2
541 ,  x_has_warnings                   OUT NOCOPY     VARCHAR2  -- bug3574165
542 ,  x_msg_data                       OUT NOCOPY     VARCHAR2
543 ,  x_online_report_id               OUT NOCOPY     NUMBER
544 ,  x_doc_check_error_record         OUT NOCOPY     doc_check_Return_Type
545 )
546 IS
547 
548 l_api_name              CONSTANT varchar2(30) := 'PO_SUBMISSION_CHECK';
549 l_api_version           CONSTANT NUMBER       := 2.0;
550 l_log_head     CONSTANT VARCHAR2(100) := g_log_head||l_api_name;
551 l_progress              VARCHAR2(3);
552 
553 l_document_id  NUMBER;
554 l_id_tbl    po_tbl_number;
555 
556 l_num_messages NUMBER := 0;  -- bug3574165: Changed l_num_errors to l_num_messages
557 l_num_warnings NUMBER := 0;  -- bug3574165
558 
559 l_online_report_id  NUMBER;
560 l_user_id    po_lines.last_updated_by%TYPE := -1;
561 l_login_id   po_lines.last_update_login%TYPE := -1;
562 p_sequence   po_online_report_text.sequence%TYPE :=0;
563 
564 l_return_status varchar2(1);
565 
566 l_conterms_exist_flag VARCHAR2(1); -- <CONTERMS FPJ>
567 l_po_header_id PO_HEADERS_ALL.po_header_id%TYPE; --<JFMIP Vendor Registration FPJ>
568 
569 BEGIN
570 
571 IF g_debug_stmt THEN
572    PO_DEBUG.debug_begin(l_log_head);
573    PO_DEBUG.debug_var(l_log_head,l_progress,'p_api_version',p_api_version);
574    PO_DEBUG.debug_var(l_log_head,l_progress,'p_action_requested',p_action_requested);
575    PO_DEBUG.debug_var(l_log_head,l_progress,'p_document_type',p_document_type);
576    PO_DEBUG.debug_var(l_log_head,l_progress,'p_document_subtype',p_document_subtype);
577    PO_DEBUG.debug_var(l_log_head,l_progress,'p_document_level',p_document_level);
578    PO_DEBUG.debug_var(l_log_head,l_progress,'p_document_level_id',p_document_level_id);
579    PO_DEBUG.debug_var(l_log_head,l_progress,'p_check_asl',p_check_asl);
580 END IF;
581 
582 --Standard Start og API savepoint
583 SAVEPOINT PO_SUBMISSION_CHECK_SP;
584 
585 l_progress := '000';
586     -- Standard call to check for call compatibility
587     IF NOT FND_API.Compatible_API_Call(l_api_version, p_api_version, l_api_name, G_PKG_NAME)
588     THEN
589           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
590     END IF;
591 
592 l_progress := '001';
593 
594 IF g_debug_stmt THEN
595    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
596      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
597           || l_progress,'Populating global temp tables');
598    END IF;
599 END IF;
600 
601 
602 PO_CORE_S.get_document_ids(
603    p_doc_type => p_document_type
604 ,  p_doc_level => p_document_level
605 ,  p_doc_level_id_tbl => po_tbl_number( p_document_level_id )
606 ,  x_doc_id_tbl => l_id_tbl
607 );
608 
609 l_document_id := l_id_tbl(1);
610 
611 IF g_debug_stmt THEN
612    PO_DEBUG.debug_var(l_log_head,l_progress,'l_document_id',l_document_id);
613 END IF;
614 
615 
616 -- bug3413891
617 -- GT table cleanup has moved to populate_global_temp_tables procedure
618 
619 --Populate global temp tables with data from main PO tables for
620 --given PO_HEADER_ID
621 
622 populate_global_temp_tables(
623    x_return_status => l_return_status
624 ,  p_doc_type => p_document_type
625 ,  p_doc_level => p_document_level
626 ,  p_doc_level_id => p_document_level_id
627 );
628 
629     IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
630         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
631     END IF;
632 
633 l_progress := '002';
634     --if the p_requested_changes is NOT NULL then update the global temp
635     --tables with these changes
636     IF p_requested_changes IS NOT NULL THEN
637 
638 IF g_debug_stmt THEN
639    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
640      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
641           || l_progress,'update requested, calling updating global tables');
642    END IF;
643 END IF;
644 
645         update_global_temp_tables(p_document_type,
646                                 p_document_subtype,
647                                 l_document_id,
648                                 p_requested_changes,
649                                 l_return_status);
650 
651         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
652                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
653         END IF;
654 
655     END IF;
656 
657 IF g_debug_stmt THEN
658    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
659      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
660           || l_progress,'Getting online_report_id');
661    END IF;
662 END IF;
663 
664 l_progress := '003';
665     --Get the unique id to be used for this document
666     SELECT PO_ONLINE_REPORT_TEXT_S.nextval
667     INTO   l_online_report_id
668     FROM   sys.dual;
669 
670 l_progress := '004';
671     --Get User ID and Login ID
672     l_user_id := FND_GLOBAL.USER_ID;
673     IF (FND_GLOBAL.CONC_LOGIN_ID >= 0) THEN
674         l_login_id := FND_GLOBAL.CONC_LOGIN_ID;
675     ELSE
676         l_login_id := FND_GLOBAL.LOGIN_ID;
677     END IF;
678 
679 IF g_debug_stmt THEN
680      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
681        FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name||'.'
682           || l_progress,'user_id '|| to_char(l_user_id) || 'Login_id ' || to_char(l_login_id));
683      END IF;
684 END IF;
685 
686     -- <Bug 7655436 Start>
687     -- Call the custom code hook
688     PO_CUSTOM_SUBMISSION_CHECK_PVT.do_pre_submission_check(
689       p_api_version         => 1.0,
690       P_document_id         => l_document_id,
691       p_action_requested    => p_action_requested,
692       p_document_type       => p_document_type,
693       p_document_subtype    => p_document_subtype,
694       p_document_level      => p_document_level,
695       p_document_level_id   => p_document_level_id,
696       p_requested_changes   => p_requested_changes,
697       p_check_asl           => p_check_asl,
698       p_req_chg_initiator   => p_req_chg_initiator,
699       p_origin_doc_id       => p_origin_doc_id,
700       p_online_report_id    => l_online_report_id,
701       p_user_id             => l_user_id,
702       p_login_id            => l_login_id,
703       p_sequence            => p_sequence,
704       x_return_status       => l_return_status
705     );
706 
707     IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR
708     THEN
709       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
710     END IF;
711     -- <Bug 7655436 End>
712 
713 l_progress := '005';
714     IF p_document_type = 'REQUISITION' THEN
715 
716 l_progress := '006';
717 IF g_debug_stmt THEN
718    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
719      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
720           || l_progress,'REQUISITION checks');
721    END IF;
722 END IF;
723 
724         --check REQUISITIONS
725         check_requisitions(l_document_id,
726                            l_online_report_id,
727                            l_user_id,
728                            l_login_id,
729                            p_sequence,
730                            l_return_status);
731 
732         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
733                  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
734         END IF;
735 
736 l_progress := '007';
737         --CBC header check for REQ
738         do_cbc_related_validations(p_document_type ,
739                        p_document_subtype,
740                        l_document_id ,
741                        l_online_report_id ,
742                        l_user_id ,
743                        l_login_id ,
744                        p_sequence,
745                        l_return_status);
746 
747         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
748              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
749         END IF;
750 
751     ELSIF p_document_type = 'RELEASE' THEN
752 
753        IF p_action_requested = g_action_UNRESERVE THEN
754 
755 l_progress := '008';
756 IF g_debug_stmt THEN
757    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
758      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
759           || l_progress,'RELEASE: Calling UNRESERVE checks');
760    END IF;
761 END IF;
762 
763       check_unreserve(
764          p_online_report_id => l_online_report_id
765       ,  p_document_type    => p_document_type       -- Bug#5462677
766       ,  p_document_subtype => p_document_subtype -- Bug#5462677
767       ,  p_document_level   => p_document_level     -- Bug#5462677
768       ,  p_doc_level_id     => p_document_level_id         --Bug#5462677
769       ,  p_user_id          => l_user_id
770       ,  p_login_id         => l_login_id
771       ,  p_sequence         => p_sequence);
772 
773        -- <Doc Manager Rewrite 11.5.11 Start>
774        ELSIF (p_action_requested = g_action_FINAL_CLOSE_CHECK) THEN
775 
776          l_progress := '008';
777          IF g_debug_stmt THEN
778             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
779               FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
780             || l_progress,'RELEASE: Calling FINAL CLOSE checks');
781             END IF;
782          END IF;
783 
784             check_final_close(
785                p_document_type     =>p_document_type
786             ,  p_document_subtype  =>p_document_subtype
787             ,  p_document_level    =>p_document_level
788             ,  p_document_id       =>l_document_id
789             ,  p_online_report_id  =>l_online_report_id
790             ,  p_user_id           =>l_user_id
791             ,  p_login_id          =>l_login_id
792             ,  p_origin_doc_id     =>p_origin_doc_id --Bug#5462677
793             ,  p_doc_level_id       =>p_document_level_id
794             ,  p_sequence          =>p_sequence
795             ,  x_return_status     =>l_return_status);
796 
797          IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
798            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
799          END IF;
800 
801        -- <Doc Manager Rewrite 11.5.11 End>
802 
803        ELSE --its 'DOC_SUBMISSION_CHECK'
804 
805 l_progress := '009';
806 IF g_debug_stmt THEN
807    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
808      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
809           || l_progress,'RELEASE checks');
810    END IF;
811 END IF;
812            --check RELEASES
813            check_releases(l_document_id,
814                        l_online_report_id,
815                        l_user_id,
816                        l_login_id, p_sequence,
817                        l_return_status);
818 
819            IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
820                  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
821            END IF;
822 
823 l_progress := '010';
824 
825            -- bug4957243
826            --Do requisition price within tolerance check for PO, RELEASES
827            -- This is not done for requester change orders
828             IF p_req_chg_initiator is null or
829                p_req_chg_initiator <> 'REQUESTER'
830             THEN
831 
832               check_po_rel_reqprice(p_document_type,
833                        l_document_id,
834                        l_online_report_id,
835                        l_user_id,
836                        l_login_id, p_sequence,
837                        l_return_status);
838             END IF;
839 
840             IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
841                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
842             END IF;
843 
844 l_progress := '011';
845             --CBC header check for Releases
846             do_cbc_related_validations(p_document_type ,
847                        p_document_subtype,
848                        l_document_id ,
849                        l_online_report_id ,
850                        l_user_id ,
851                        l_login_id ,
852                        p_sequence,
853                        l_return_status);
854 
855             IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
856                  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
857             END IF;
858 
859        END IF; --check action requested
860 
861     ELSIF p_document_type = 'PO' THEN
862        IF p_action_requested = g_action_UNRESERVE THEN
863 
864 l_progress := '012';
865 IF g_debug_stmt THEN
866    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
867      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
868           || l_progress,'PO: Calling UNRESERVE checks ');
869    END IF;
870 END IF;
871 
872          check_unreserve(
873             p_online_report_id  => l_online_report_id
874          ,  p_document_type     => p_document_type      -- Bug#5462677
875          ,  p_document_subtype  => p_document_subtype   -- Bug#5462677
876          ,  p_document_level    => p_document_level     -- Bug#5462677
877          ,  p_doc_level_id      => p_document_level_id  -- Bug#5462677
878          ,  p_user_id           => l_user_id
879          ,  p_login_id          => l_login_id
880          ,  p_sequence          => p_sequence );
881 
882        -- <Doc Manager Rewrite 11.5.11 Start>
883        ELSIF (p_action_requested = g_action_FINAL_CLOSE_CHECK) THEN
884 
885          l_progress := '012';
886          IF g_debug_stmt THEN
887             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
888               FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
889             || l_progress,'PO: Calling FINAL CLOSE checks');
890             END IF;
891          END IF;
892 
893             check_final_close(
894                p_document_type     =>p_document_type
895             ,  p_document_subtype  =>p_document_subtype
896             ,  p_document_level    =>p_document_level
897             ,  p_document_id       =>l_document_id
898             ,  p_online_report_id  =>l_online_report_id
899             ,  p_user_id           =>l_user_id
900             ,  p_login_id          =>l_login_id
901             ,  p_origin_doc_id     =>p_origin_doc_id --Bug#5462677
902             ,  p_doc_level_id      =>p_document_level_id
903             ,  p_sequence          =>p_sequence
904             ,  x_return_status     =>l_return_status);
905 
906          IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
907            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
908          END IF;
909 
910        -- <Doc Manager Rewrite 11.5.11 End>
911 
912        ELSE --its 'DOC_SUBMISSION_CHECK'
913 
914 l_progress := '013';
915 IF g_debug_stmt THEN
916    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
917      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
918           || l_progress,'PO checks');
919    END IF;
920 END IF;
921             --First do all checks at header level
922             check_po_pa_header(l_document_id,
923                        l_online_report_id,
924                        l_user_id,
925                        l_login_id, p_sequence,
926                        l_return_status);
927 
928             IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
929                  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
930             END IF;
931 
932 l_progress := '014';
933             --Do all checks common to Standard and PLanned POs
934             check_po(l_document_id,
935                        l_online_report_id,
936                        l_user_id,
937                        l_login_id,
938 --                       p_check_asl,                                -- <2757450>
939                        p_sequence,
940                        l_return_status);
941 
942             IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
943                   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
944             END IF;
945 
946 l_progress := '015';
947             --CBC header check for POs
948             do_cbc_related_validations(p_document_type ,
949                        p_document_subtype,
950                        l_document_id ,
951                        l_online_report_id ,
952                        l_user_id ,
953                        l_login_id ,
954                        p_sequence,
955                        l_return_status);
956 
957             IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
958                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
959             END IF;
960 
961 l_progress := '016';
962 
963             -- bug4957243
964             --Do requisition price within tolerance check for PO, RELEASES
965             -- This is not done for requester change orders
966             IF p_req_chg_initiator is null or
967                p_req_chg_initiator <> 'REQUESTER'
968             THEN
969 
970               check_po_rel_reqprice(p_document_type,
971                        l_document_id,
972                        l_online_report_id,
973                        l_user_id,
974                        l_login_id, p_sequence,
975                        l_return_status);
976             END IF;
977 
978             IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
979                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
980             END IF;
981 
982             --Do some additional checks if its PLANNED PO
983             IF p_document_subtype = 'PLANNED' THEN
984                 --check planned PO
985 l_progress := '017';
986 IF g_debug_stmt THEN
987    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
988      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
989           || l_progress,'PO: PLANNED checks');
990    END IF;
991 END IF;
992                 --Call checks common to Planned PO and Blanket PA
993                 check_planned_po_blanket_pa(l_document_id,
994                              l_online_report_id,
995                              l_user_id,
996                              l_login_id, p_sequence,
997                              l_return_status);
998 
999                  IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1000                       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1001                  END IF;
1002 
1003             ELSIF p_document_subtype = 'STANDARD' THEN
1004 
1005 l_progress := '018';
1006 IF g_debug_stmt THEN
1007    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1008      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
1009           || l_progress,'PO: STANDARD checks');
1010    END IF;
1011 END IF;
1012                 --Call additional checks for Standard PO
1013                 check_standard_po(l_document_id,
1014                              l_online_report_id,
1015                              l_user_id,
1016                              l_login_id,
1017                              p_sequence,
1018                              l_return_status);
1019 
1020                 IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1021                      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1022                 END IF;
1023 
1024             END IF; --check doc_subtype
1025 
1026 
1027        END IF; --check action requested
1028     ELSIF  p_document_type = 'PA' THEN
1029 
1030       -- <Doc Manager Rewrite 11.5.11 Start>
1031       IF (p_action_requested = g_action_FINAL_CLOSE_CHECK) THEN
1032 
1033         l_progress := '019';
1034         IF g_debug_stmt THEN
1035           IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1036             FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
1037           || l_progress,'PA: Calling FINAL CLOSE checks');
1038           END IF;
1039         END IF;
1040 
1041         check_final_close(
1042            p_document_type     =>p_document_type
1043         ,  p_document_subtype  =>p_document_subtype
1044         ,  p_document_level    =>p_document_level
1045         ,  p_document_id       =>l_document_id
1046         ,  p_online_report_id  =>l_online_report_id
1047         ,  p_user_id           =>l_user_id
1048         ,  p_login_id          =>l_login_id
1049         ,  p_origin_doc_id     =>p_origin_doc_id --Bug#5462677
1050         ,  p_doc_level_id      =>p_document_level_id
1051         ,  p_sequence          =>p_sequence
1052         ,  x_return_status     =>l_return_status);
1053 
1054         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1055           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1056         END IF;
1057 
1058       ELSE
1059       -- <Doc Manager Rewrite 11.5.11 End>
1060 
1061         -- It's  'DOC_SUBMISSION_CHECK'
1062         --check PAs
1063         l_progress := '019';
1064         IF g_debug_stmt THEN
1065           IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1066             FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
1067             || l_progress,'PA checks');
1068           END IF;
1069         END IF;
1070 
1071         --First do all checks at header level
1072         check_po_pa_header(l_document_id,
1073                        l_online_report_id,
1074                        l_user_id,
1075                        l_login_id,
1076                        p_sequence,
1077                        l_return_status);
1078 
1079         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1080               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1081         END IF;
1082 
1083         IF p_document_subtype = 'BLANKET' THEN
1084 
1085           l_progress := '020';
1086           IF g_debug_stmt THEN
1087             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1088               FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
1089               || l_progress,'PA: BLANKET checks');
1090             END IF;
1091           END IF;
1092 
1093           --check blanket agreement
1094           check_blanket_agreement(l_document_id,
1095                                   l_online_report_id,
1096                                   l_user_id,
1097                                   l_login_id,
1098                                   p_check_asl,                   -- <2757450>
1099                                   p_sequence,
1100                                   l_return_status);
1101 
1102           IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1103             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1104           END IF;
1105 
1106           l_progress := '021';
1107           --Call checks common to Planned PO and Blanket PA
1108           check_planned_po_blanket_pa(l_document_id,
1109                            l_online_report_id,
1110                            l_user_id,
1111                            l_login_id,
1112                            p_sequence,
1113                            l_return_status);
1114 
1115           IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1116               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1117           END IF;
1118 
1119         ELSIF p_document_subtype = 'CONTRACT' THEN
1120 
1121           l_progress := '022';
1122           IF g_debug_stmt THEN
1123             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1124               FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
1125               || l_progress,'PA: CONTRACT checks');
1126             END IF;
1127           END IF;
1128 
1129           --check contract agreement
1130           check_contract_agreement(l_document_id,
1131                                    l_online_report_id,
1132                                    l_user_id,
1133                                    l_login_id,
1134                                    p_sequence,
1135                                    l_return_status);
1136 
1137           IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1138             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1139           END IF;
1140         END IF;  --PA p_document_subtype
1141       END IF; -- PA p_action_requested
1142 
1143     END IF; --document_type lookup
1144 
1145 l_progress := '023';
1146 IF g_debug_stmt THEN
1147    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1148      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
1149           || l_progress,'Done with all checks');
1150    END IF;
1151 END IF;
1152 
1153 /* CONTERMS FPJ START*/
1154 
1155   -- <Doc Manager Rewrite 11.5.11> : Only check okc terms for 'DOC_SUBMISSION_CHECK'
1156 
1157 IF (p_document_type IN ('PO', 'PA')) AND
1158    (p_document_subtype IN ('STANDARD', 'BLANKET', 'CONTRACT'))
1159    AND (p_action_requested = g_action_DOC_SUBMISSION_CHECK) THEN
1160 
1161     -- SQL What: conterms exist flag
1162     -- SQL why : need to check before calling contracts qa
1163     -- SQL join: po_header_id
1164     SELECT conterms_exist_flag
1165     INTO   l_conterms_exist_flag
1166     FROM   po_headers_gt
1167     WHERE  po_header_id = l_document_id;
1168 
1169     IF (NVL(l_conterms_exist_flag, 'N')='Y') THEN
1170       check_terms(
1171         p_document_id          => l_document_id,
1172         p_document_type        => p_document_type,
1173         p_document_subtype     => p_document_subtype,
1174         p_online_report_id     => l_online_report_id,
1175         p_user_id              => l_user_id,
1176         p_login_id             => l_login_id,
1177         p_sequence             => p_sequence,
1178         x_return_status        => l_return_status);
1179 
1180       IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
1181         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1182       END IF;
1183     END IF; --conterms exist
1184 END IF; -- document type is PO or PA
1185 
1186     l_progress := '024';
1187     IF g_debug_stmt THEN
1188       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1189         FND_LOG.string(FND_LOG.LEVEL_STATEMENT
1190                     ,g_log_head || '.'||l_api_name||'.'|| l_progress
1191                     ,'Done with contracts qa');
1192       END IF;
1193     END IF;
1194     /* CONTERMS FPJ END */
1195 
1196   -- <Doc Manager Rewrite 11.5.11> : Do vendor registration check only
1197   -- for DOC_SUBMISSION_CHECK and UNRESERVE
1198 
1199   IF (p_action_requested IN (g_action_DOC_SUBMISSION_CHECK, g_action_UNRESERVE))
1200   THEN
1201 
1202     --<JFMIP Vendor Registration FPJ Start>
1203     -- This part of the code is called when the action requested is
1204     -- DOC_SUBMISSION_CHECK, as well as UNRESERVE. This is because if
1205     -- vendor site does not have a valid registration, reserve/unreserve
1206     -- actions should be prevented.
1207     IF (p_document_type = 'RELEASE') THEN
1208         -- SQL What: Retrieve the blanket header id based on the release id
1209         -- SQL Why:  This header id is used in check_vendor_site_ccr_regis to
1210         --           retrieve the vendor and vendor site id
1211        BEGIN
1212          SELECT  po_header_id
1213          INTO    l_po_header_id
1214          FROM    PO_RELEASES_ALL
1215          WHERE   po_release_id = l_document_id;
1216       EXCEPTION
1217          WHEN OTHERS THEN
1218            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1219       END;
1220     ELSE
1221        l_po_header_id := l_document_id;
1222     END IF;
1223 
1224     IF p_document_type IN ('RELEASE', 'PO', 'PA') THEN
1225      check_vendor_site_ccr_regis(
1226                                   p_document_id      => l_po_header_id,
1227           p_online_report_id => l_online_report_id,
1228           p_user_id          => l_user_id,
1229           p_login_id         => l_login_id,
1230           p_sequence         => p_sequence,
1231           x_return_status    => l_return_status);
1232 
1233         IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
1234      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1235         END IF;
1236     END IF;
1237 
1238     l_progress := '025';
1239     IF g_debug_stmt THEN
1240        IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1241          FND_LOG.string(FND_LOG.LEVEL_STATEMENT
1242                     ,g_log_head || '.'||l_api_name||'.'|| l_progress,
1243                     'Done with vendor site registration check');
1244        END IF;
1245     END IF;
1246     --<JFMIP Vendor Registration FPJ End>
1247 
1248   END IF; -- p_action_requested NOT IN ('DOC_SUBMISSION_CHECK', 'UNRESREVE')
1249 
1250     -- <Bug 7655436 Start>
1251     -- Call the custom code hook
1252     PO_CUSTOM_SUBMISSION_CHECK_PVT.do_post_submission_check(
1253       p_api_version         => 1.0,
1254       P_document_id         => l_document_id,
1255       p_action_requested    => p_action_requested,
1256       p_document_type       => p_document_type,
1257       p_document_subtype    => p_document_subtype,
1258       p_document_level      => p_document_level,
1259       p_document_level_id   => p_document_level_id,
1260       p_requested_changes   => p_requested_changes,
1261       p_check_asl           => p_check_asl,
1262       p_req_chg_initiator   => p_req_chg_initiator,
1263       p_origin_doc_id       => p_origin_doc_id,
1264       p_online_report_id    => l_online_report_id,
1265       p_user_id             => l_user_id,
1266       p_login_id            => l_login_id,
1267       p_sequence            => p_sequence,
1268       x_return_status       => l_return_status
1269     );
1270 
1271     IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR
1272     THEN
1273       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1274     END IF;
1275     -- <Bug 7655436 End>
1276 
1277 
1278     --Done with CHECKS, now prepare OUT parameters to pass back
1279 
1280     -- bug3574165
1281     -- Count number of warning messages as well.
1282 
1283     SELECT count(text_line),
1284            COUNT(DECODE(message_type, 'W', 'W', NULL))
1285     INTO   l_num_messages,
1286            l_num_warnings
1287     FROM   po_online_report_text_gt
1288     WHERE  online_report_id = l_online_report_id;
1289 
1290 
1291     l_progress := '026';
1292     IF g_debug_stmt THEN
1293       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1294         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
1295           || l_progress,'Number of messagess reported ' || l_num_messages);
1296       END IF;
1297     END IF;
1298 
1299     x_online_report_id := l_online_report_id;
1300 
1301     IF l_num_messages > 0 THEN
1302 
1303         -- bug3574165
1304         -- 1) Return Error only if there are messages that are not warnings
1305         -- 2) Set x_has_warnings flag to TRUE if there are warnings reported
1306 
1307         IF (l_num_messages > l_num_warnings) THEN
1308           x_sub_check_status := FND_API.G_RET_STS_ERROR;
1309         ELSE
1310           x_sub_check_status := FND_API.G_RET_STS_SUCCESS;
1311         END IF;
1312 
1313         IF ( l_num_warnings > 0 ) THEN
1314           x_has_warnings := FND_API.G_TRUE;
1315         END IF;
1316 
1317         --Bulk update online_report_text table with errors
1318         INSERT INTO po_online_report_text(online_report_id,
1319             last_update_login,
1320             last_updated_by,
1321             last_update_date,
1322             created_by,
1323             creation_date,
1324             line_num,
1325             shipment_num,
1326             distribution_num,
1327             sequence,
1328             text_line,
1329                         message_type) --<CONTERMS FPJ>
1330             SELECT online_report_id,
1331             last_update_login,
1332             last_updated_by,
1333             last_update_date,
1334             created_by,
1335             creation_date,
1336             line_num,
1337             shipment_num,
1338             distribution_num,
1339             sequence,
1340             text_line,
1341                         message_type --<CONTERMS FPJ>
1342             FROM po_online_report_text_gt
1343             WHERE online_report_id = x_online_report_id;
1344 
1345 select count(*) into l_num_messages from po_online_report_text_gt where online_report_id = x_online_report_id;
1346 
1347 l_progress := '027';
1348 
1349         -- SQL What: Gets the relevent messages from global temp table
1350         -- SQL Why: Need to collect all errors in x_doc_check_error_record
1351         SELECT  online_report_id, sequence, text_line,
1352             line_num, shipment_num, distribution_num, message_name, message_type
1353         BULK COLLECT INTO x_doc_check_error_record.online_report_id,
1354                           x_doc_check_error_record.sequence_num,
1355                           x_doc_check_error_record.text_line,
1356                           x_doc_check_error_record.line_num,
1357                           x_doc_check_error_record.shipment_num,
1358                           x_doc_check_error_record.distribution_num,
1359                           x_doc_check_error_record.message_name,
1360                           x_doc_check_error_record.message_type --<CONTERMS FPJ>
1361         FROM  po_online_report_text_gt
1362         WHERE online_report_id = x_online_report_id;
1363 
1364     ELSE
1365         x_sub_check_status := FND_API.G_RET_STS_SUCCESS;
1366     END IF;
1367 
1368 l_progress := '027';
1369     x_return_status := FND_API.G_RET_STS_SUCCESS;
1370 
1371     -- LCM ER start. If all the submission checks have been passed successfully then update the lcm_flag of po_line_locations_all and po_distributions_all.
1372         IF p_document_type in ('PO','RELEASE') THEN
1373 
1374         	FOR ship_rec in (select line_location_id from po_line_locations_gt)
1375 
1376         	LOOP
1377         		set_lcm_flag(ship_rec.line_location_id,'AFTER',l_return_status);
1378         	END LOOP;
1379 
1380         	IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
1381     	     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1382             END IF;
1383         END IF;
1384     --LCM ER end
1385 
1386     -- bug3413891
1387     -- We are now deleting data from GT tables at the beginning of submission
1388     -- check. Deletion at the end is no longer needed
1389 
1390 
1391 l_progress := '029';
1392 IF g_debug_stmt THEN
1393    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1394      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
1395           || l_progress,'Returning from PVT package');
1396    END IF;
1397 END IF;
1398 
1399 EXCEPTION
1400     WHEN FND_API.G_EXC_ERROR THEN
1401         ROLLBACK TO PO_SUBMISSION_CHECK_SP;
1402         x_return_status := FND_API.G_RET_STS_ERROR;
1403         x_sub_check_status := FND_API.G_RET_STS_ERROR;
1404         x_msg_data := FND_MSG_PUB.GET(p_msg_index => FND_MSG_PUB.G_LAST,
1405                                         p_encoded => 'F');
1406 
1407     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1408         ROLLBACK TO PO_SUBMISSION_CHECK_SP;
1409         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1410         x_sub_check_status := FND_API.G_RET_STS_ERROR;
1411         x_msg_data := FND_MSG_PUB.GET(p_msg_index => FND_MSG_PUB.G_LAST,
1412                                     p_encoded => 'F');
1413 
1414     WHEN OTHERS THEN
1415         ROLLBACK TO PO_SUBMISSION_CHECK_SP;
1416         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1417         x_sub_check_status := FND_API.G_RET_STS_ERROR;
1418         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
1419             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
1420         END IF;
1421 
1422         IF (g_debug_unexp) THEN
1423                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
1424                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
1425                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
1426                        || l_progress || ' SQL CODE is '||sqlcode);
1427                 END IF;
1428         END IF;
1429 
1430 END PO_SUBMISSION_CHECK;
1431 
1432 
1433 PROCEDURE post_submission_check                                   -- <2757450>
1434 (
1435     p_api_version             IN            NUMBER
1436 ,   p_document_type           IN            VARCHAR2
1437 ,   p_document_subtype        IN            VARCHAR2
1438 ,   p_document_id             IN            NUMBER
1439 ,   x_return_status              OUT NOCOPY VARCHAR2
1440 ,   x_sub_check_status           OUT NOCOPY VARCHAR2
1441 ,   x_online_report_id           OUT NOCOPY NUMBER
1442 )
1443 IS
1444     l_api_name                CONSTANT varchar2(30) := 'POST_SUBMISSION_CHECK';
1445     l_api_version             CONSTANT NUMBER       := 1.0;
1446     l_progress                VARCHAR2(3);
1447 
1448     l_num_errors              NUMBER := 0;
1449     l_online_report_id        NUMBER;
1450     l_user_id                 PO_LINES.last_updated_by%TYPE := -1;
1451     l_login_id                PO_LINES.last_update_login%TYPE := -1;
1452     p_sequence                PO_ONLINE_REPORT_TEXT.sequence%TYPE :=0;
1453 
1454     l_return_status           VARCHAR2(1);
1455 
1456 BEGIN
1457 
1458 SAVEPOINT POST_SUBMISSION_CHECK_SP;
1459 
1460 l_progress := '000';
1461 
1462     -- Standard call to check for call compatibility
1463     IF NOT FND_API.Compatible_API_Call(l_api_version, p_api_version, l_api_name, G_PKG_NAME)
1464     THEN
1465         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1466     END IF;
1467 
1468 l_progress := '001';
1469 
1470 IF g_debug_stmt THEN
1471     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1472       FND_LOG.string( FND_LOG.LEVEL_STATEMENT,
1473                     g_log_head || '.'||l_api_name||'.' || l_progress,
1474                     'Populating global temp tables');
1475     END IF;
1476 END IF;
1477 
1478    --Populate global temp tables with data from main PO tables for
1479    --given PO_HEADER_ID
1480    populate_global_temp_tables(
1481       x_return_status => l_return_status
1482    ,  p_doc_type => p_document_type
1483    ,  p_doc_level => g_document_level_HEADER
1484    ,  p_doc_level_id => p_document_id
1485    );
1486 
1487     IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1488         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1489     END IF;
1490 
1491 IF g_debug_stmt THEN
1492    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1493      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
1494           || l_progress,'Getting online_report_id');
1495    END IF;
1496 END IF;
1497 
1498 l_progress := '003';
1499 
1500     --Get the unique id to be used for this document
1501     SELECT PO_ONLINE_REPORT_TEXT_S.nextval
1502     INTO   l_online_report_id
1503     FROM   sys.dual;
1504 
1505 l_progress := '004';
1506 
1507     --Get User ID and Login ID
1508     l_user_id := FND_GLOBAL.USER_ID;
1509 
1510     IF ( FND_GLOBAL.CONC_LOGIN_ID >= 0 )
1511     THEN
1512         l_login_id := FND_GLOBAL.CONC_LOGIN_ID;
1513     ELSE
1514         l_login_id := FND_GLOBAL.LOGIN_ID;
1515     END IF;
1516 
1517 IF g_debug_stmt THEN
1518      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1519        FND_LOG.string( FND_LOG.LEVEL_STATEMENT,
1520                      g_log_head || l_api_name||'.'|| l_progress,
1521                      'user_id '|| to_char(l_user_id) ||
1522                      'Login_id ' || to_char(l_login_id));
1523      END IF;
1524 END IF;
1525 
1526     --============================ CHECKS =====================================
1527     --Bug 4943365 We should not do any ASL checking for Blanket agreements
1528     --Removed the call to check_asl
1529 
1530 l_progress := '007';
1531 
1532 IF g_debug_stmt THEN
1533    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1534      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
1535           || l_progress,'Done with all checks');
1536    END IF;
1537 END IF;
1538 
1539     --Done with CHECKS, now prepare OUT parameters to pass back
1540     SELECT count(text_line)
1541     INTO   l_num_errors
1542     FROM   po_online_report_text_gt
1543     WHERE  online_report_id = l_online_report_id;
1544 
1545 l_progress := '008';
1546 
1547 IF g_debug_stmt THEN
1548    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1549      FND_LOG.string( FND_LOG.LEVEL_STATEMENT,
1550                    g_log_head || '.'||l_api_name||'.'|| l_progress,
1551                    'Number of errors reported ' || l_num_errors);
1552    END IF;
1553 END IF;
1554 
1555     x_online_report_id := l_online_report_id;
1556 
1557     IF l_num_errors > 0 THEN
1558 
1559         x_sub_check_status := FND_API.G_RET_STS_ERROR;
1560 
1561         --Bulk update online_report_text table with errors
1562         INSERT INTO po_online_report_text(
1563                     online_report_id,
1564             last_update_login,
1565             last_updated_by,
1566             last_update_date,
1567             created_by,
1568               creation_date,
1569             line_num,
1570             shipment_num,
1571             distribution_num,
1572             sequence,
1573             text_line)
1574              SELECT online_report_id,
1575             last_update_login,
1576             last_updated_by,
1577             last_update_date,
1578             created_by,
1579               creation_date,
1580             line_num,
1581             shipment_num,
1582             distribution_num,
1583             sequence,
1584             text_line
1585                FROM po_online_report_text_gt
1586               WHERE online_report_id = x_online_report_id;
1587 
1588 l_progress := '009';
1589 
1590     ELSE
1591         x_sub_check_status := FND_API.G_RET_STS_SUCCESS;
1592     END IF;
1593 
1594 l_progress := '010';
1595 
1596     x_return_status := FND_API.G_RET_STS_SUCCESS;
1597 
1598 -- bug3413891
1599 -- Deletion of GT tables has been moved to populate_global_temp_tables procedure
1600 
1601 l_progress := '011';
1602 
1603 IF g_debug_stmt THEN
1604     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1605       FND_LOG.string( FND_LOG.LEVEL_STATEMENT,
1606                     g_log_head || '.'||l_api_name||'.'|| l_progress,
1607                     'Returning from PVT package');
1608     END IF;
1609 END IF;
1610 
1611 EXCEPTION
1612 
1613     WHEN FND_API.G_EXC_ERROR THEN
1614         ROLLBACK TO POST_SUBMISSION_CHECK_SP;
1615         x_return_status := FND_API.G_RET_STS_ERROR;
1616         x_sub_check_status := FND_API.G_RET_STS_ERROR;
1617 
1618     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1619         ROLLBACK TO POST_SUBMISSION_CHECK_SP;
1620         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1621         x_sub_check_status := FND_API.G_RET_STS_ERROR;
1622 
1623     WHEN OTHERS THEN
1624         ROLLBACK TO POST_SUBMISSION_CHECK_SP;
1625         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1626         x_sub_check_status := FND_API.G_RET_STS_ERROR;
1627         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
1628             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
1629         END IF;
1630 
1631         IF ( g_debug_unexp ) THEN
1632             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
1633               FND_LOG.string( FND_LOG.level_unexpected,
1634                             g_log_head || l_api_name || '.others_exception',
1635                             'EXCEPTION: Location is ' || l_progress || ' SQL CODE is '||sqlcode);
1636             END IF;
1637         END IF;
1638 
1639 END post_submission_check;
1640 
1641  --Bug 4943365 Removed the check_asl procedure because blankets
1642  --should not do the asl checks.
1643  --PROCEDURE check_asl
1644 
1645 /**
1646 * Private Procedure: CHECK_REQUISITIONS
1647 * Requires:
1648 *   IN PARAMETERS:
1649 *       p_document_id:      The requisition_header_id of submitted document
1650 *       p_online_report_id: Id used to INSERT INTO online_report_text table
1651 *       p_user_id:          User performing the action
1652 *       p_login_id:         Last update login_id
1653 *   IN OUT PARAMETERS
1654 *       p_sequence:          Sequence number of last reported error
1655 * Modifies: Updates the PO_REQUISITION_LINES table with RATE information.
1656 *           Inserts error msgs in online_report_text_gt table, uses global_temp
1657 *           tables for processing
1658 * Effects:  This procedure runs the document submission checks for
1659 *           REQUISITIONS
1660 * Returns:
1661 *  p_sequence: This parameter contains the current count of number of error
1662 *              messages inserted
1663 */
1664 PROCEDURE check_requisitions(p_document_id IN NUMBER,
1665                        p_online_report_id IN NUMBER,
1666                        p_user_id IN NUMBER,
1667                        p_login_id IN NUMBER,
1668                        p_sequence IN OUT NOCOPY NUMBER,
1669                        x_return_status OUT NOCOPY VARCHAR2) IS
1670 
1671 l_textline  po_online_report_text.text_line%TYPE := NULL;
1672 l_api_name  CONSTANT varchar2(40) := 'CHECK_REQUISITIONS';
1673 l_progress VARCHAR2(3);
1674 
1675 TYPE NumTab is TABLE of NUMBER INDEX by BINARY_INTEGER;
1676 l_quantity1 NumTab;
1677 l_quantity2 NumTab;
1678 l_line_qty_tbl   NumTab;                                      -- <SERVICES FPJ>
1679 l_line_amt_tbl   NumTab;                                      -- <SERVICES FPJ>
1680 l_dist_qty_tbl   NumTab;                                      -- <SERVICES FPJ>
1681 l_dist_amt_tbl   NumTab;                                      -- <SERVICES FPJ>
1682 l_line_num   NumTab;
1683 l_shipment_num NumTab;
1684 l_dist_num NumTab;
1685 l_rowcount NumTab;
1686 
1687 TYPE value_basis_tbl_type IS
1688     TABLE OF PO_LINE_TYPES_B.order_type_lookup_code%TYPE;     -- <SERVICES FPJ>
1689 l_value_basis_tbl         value_basis_tbl_type;               -- <SERVICES FPJ>
1690 
1691 --<R12 eTax Integration Start>
1692 l_return_status    VARCHAR2(1);
1693 l_msg_count        NUMBER;
1694 l_msg_data         VARCHAR2(2000);
1695 l_tax_status       VARCHAR2(1);
1696 l_tax_message      fnd_new_messages.message_text%TYPE;
1697 --<R12 eTax Integration End>
1698 
1699 BEGIN
1700 
1701 l_progress := '000';
1702 -- BUG 2687600 mbhargav
1703 --Removed Update statement to update rate in po_requistion_lines
1704 
1705 l_progress := '001';
1706 IF g_debug_stmt THEN
1707    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1708      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head||l_api_name||'.'
1709           || l_progress,'REQ 1: No lines ');
1710    END IF;
1711 END IF;
1712 
1713     -- Check 1: Each Requisition Header must have atleast one line
1714     -- PO_SUB_REQ_HEADER_NO_LINES
1715   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REQ_HEADER_NO_LINES');
1716   INSERT INTO po_online_report_text_gt(online_report_id,
1717         last_update_login,
1718         last_updated_by,
1719         last_update_date,
1720         created_by,
1721         creation_date,
1722         line_num,
1723         shipment_num,
1724         distribution_num,
1725         sequence,
1726         text_line,
1727                 message_name)
1728   SELECT  p_online_report_id,
1729         p_login_id,
1730         p_user_id,
1731           sysdate,
1732         p_user_id,
1733         sysdate,
1734         0,
1735         0,
1736         0,
1737         p_sequence + ROWNUM,
1738         substr(l_textline,1,240),
1739             'PO_SUB_REQ_HEADER_NO_LINES'
1740      FROM   PO_REQ_HEADERS_GT PRH
1741      WHERE  PRH.requisition_header_id = p_document_id
1742      AND    NOT EXISTS (SELECT 'Lines Exist'
1743                         FROM   PO_REQ_LINES_GT PRL
1744                         WHERE  PRL.requisition_header_id = PRH.requisition_header_id
1745                         AND    nvl(PRL.cancel_flag,'N') = 'N');
1746 
1747     --Increment the p_sequence with number of errors reported in last query
1748     p_sequence := p_sequence + SQL%ROWCOUNT;
1749 ------------------------------------------------
1750 
1751 l_progress := '002';
1752 IF g_debug_stmt THEN
1753    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1754      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head||l_api_name||'.'
1755           || l_progress,'REQ 2: No distributions');
1756    END IF;
1757 END IF;
1758 
1759     -- Check 2: Each Requisition line must have atleast one distribution
1760     -- PO_SUB_REQ_LINE_NO_DIST
1761 
1762   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REQ_LINE_NO_DIST');
1763   INSERT INTO po_online_report_text_gt(online_report_id,
1764         last_update_login,
1765         last_updated_by,
1766         last_update_date,
1767         created_by,
1768         creation_date,
1769         line_num,
1770         shipment_num,
1771         distribution_num,
1772         sequence,
1773         text_line,
1774                 message_name)
1775   SELECT  p_online_report_id,
1776         p_login_id,
1777         p_user_id,
1778           sysdate,
1779         p_user_id,
1780         sysdate,
1781         PRL.line_num,
1782         0,
1783         0,
1784         p_sequence + ROWNUM,
1785         substr(g_linemsg||g_delim||PRL.line_num||g_delim||l_textline,1,240),
1786             'PO_SUB_REQ_LINE_NO_DIST'
1787      FROM  PO_REQ_LINES_GT PRL
1788      WHERE PRL.requisition_header_id = p_document_id AND
1789            nvl(PRL.cancel_flag,'N') = 'N' AND
1790            nvl(PRL.closed_code,'OPEN') <> 'FINALLY CLOSED' AND
1791            nvl(PRL.modified_by_agent_flag,'N') = 'N' AND
1792            NOT EXISTS (SELECT 'Dist Exist'
1793                        FROM PO_REQ_DISTRIBUTIONS_GT PRD
1794                        WHERE PRD.requisition_line_id = PRL.requisition_line_id);
1795 
1796     --Increment the p_sequence with number of errors reported in last query
1797     p_sequence := p_sequence + SQL%ROWCOUNT;
1798 -------------------------------------------------
1799 
1800 l_progress := '003';
1801 IF g_debug_stmt THEN
1802    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1803      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head||l_api_name||'.'
1804           || l_progress,'REQ 3: Line qty does not match dist qty');
1805    END IF;
1806 END IF;
1807 
1808     -- Check 3: The sum of all distribution quantities/amounts must equal their
1809     -- corresponding line quantity/amount.
1810 
1811     SELECT
1812         PRL.line_num
1813     ,   PLT.order_type_lookup_code                            -- <SERVICES FPJ>
1814     ,   PRL.quantity
1815     ,   PRL.amount                                            -- <SERVICES FPJ>
1816     ,   sum(nvl(PRD.req_line_quantity, 0))                    -- <SERVICES FPJ>
1817     ,   sum(nvl(PRD.req_line_amount, 0))                      -- <SERVICES FPJ>
1818     BULK COLLECT INTO
1819         l_line_num
1820     ,   l_value_basis_tbl                                     -- <SERVICES FPJ>
1821     ,   l_line_qty_tbl                                        -- <SERVICES FPJ>
1822     ,   l_line_amt_tbl                                        -- <SERVICES FPJ>
1823     ,   l_dist_qty_tbl                                        -- <SERVICES FPJ>
1824     ,   l_dist_amt_tbl                                        -- <SERVICES FPJ>
1825     FROM
1826         PO_REQ_DISTRIBUTIONS_GT PRD
1827     ,   PO_REQ_LINES_GT         PRL
1828     ,   PO_LINE_TYPES_B         PLT                           -- <SERVICES FPJ>
1829     WHERE
1830         PRL.requisition_line_id = PRD.requisition_line_id
1831     AND PRL.requisition_header_id = p_document_id
1832     AND nvl(PRL.cancel_flag,'N') = 'N'
1833     AND nvl(PRL.closed_code,'OPEN') <> 'FINALLY CLOSED'
1834     AND nvl(PRL.modified_by_agent_flag,'N') = 'N'
1835     AND PRL.line_type_id = PLT.line_type_id                   -- <SERVICES FPJ>
1836     AND                                                       -- <SERVICES FPJ>
1837         (   (   ( PLT.order_type_lookup_code IN ('QUANTITY','AMOUNT')
1838             AND ( PRL.quantity <> ( SELECT nvl(sum(PRD2.req_line_quantity),0)
1839                                     FROM   PO_REQ_DISTRIBUTIONS_GT PRD2
1840                                     WHERE  PRD2.requisition_line_id = PRL.requisition_line_id ) ) )
1841         OR  (   ( PLT.order_type_lookup_code IN ('RATE','FIXED PRICE') )
1842             AND ( PRL.amount <>   ( SELECT nvl(sum(PRD2.req_line_amount),0)
1843                                     FROM   PO_REQ_DISTRIBUTIONS_GT PRD2
1844                                     WHERE  PRD2.requisition_line_id = PRL.requisition_line_id ) ) ) )
1845         )
1846     GROUP BY
1847         PRL.line_num
1848     ,   PLT.order_type_lookup_code                            -- <SERVICES FPJ>
1849     ,   PRL.quantity
1850     ,   PRL.amount;                                           -- <SERVICES FPJ>
1851 
1852     FOR i IN 1..l_line_num.COUNT LOOP
1853         l_rowCount(i) := i;
1854     END LOOP;
1855 
1856     FORALL i IN 1..l_line_num.COUNT
1857         INSERT INTO po_online_report_text_gt (online_report_id,
1858         last_update_login,
1859         last_updated_by,
1860         last_update_date,
1861         created_by,
1862         creation_date,
1863         line_num,
1864         shipment_num,
1865         distribution_num,
1866         sequence,
1867         text_line,
1868                 message_name)
1869         VALUES(
1870             p_online_report_id,
1871             p_login_id,
1872             p_user_id,
1873             sysdate,
1874             p_user_id,
1875             sysdate,
1876             l_line_num(i),
1877             NULL,                                             -- <SERVICES FPJ>
1878             NULL,                                             -- <SERVICES FPJ>
1879             p_sequence+l_rowCount(i),
1880             decode ( l_value_basis_tbl(i)                     -- <SERVICES FPJ>
1881                    , 'RATE'        , PO_CORE_S.get_translated_text
1882                                      (   'PO_SUB_REQ_LINE_NE_DIST_AMT'
1883                                      ,   'LINE_NUM', l_line_num(i)
1884                                      ,   'LINE_AMT', l_line_amt_tbl(i)
1885                                      ,   'DIST_AMT', l_dist_amt_tbl(i)
1886                                      )
1887                    , 'FIXED PRICE' , PO_CORE_S.get_translated_text
1888                                      (   'PO_SUB_REQ_LINE_NE_DIST_AMT'
1889                                      ,   'LINE_NUM', l_line_num(i)
1890                                      ,   'LINE_AMT', l_line_amt_tbl(i)
1891                                      ,   'DIST_AMT', l_dist_amt_tbl(i)
1892                                      )
1893                                    , PO_CORE_S.get_translated_text
1894                                      (   'PO_SUB_REQ_LINE_NE_DIST_QTY'
1895                                      ,   'LINE_NUM', l_line_num(i)
1896                                      ,   'LINE_QTY', l_line_qty_tbl(i)
1897                                      ,   'DIST_QTY', l_dist_qty_tbl(i)
1898                                      )
1899                    ),
1900             decode ( l_value_basis_tbl(i)                     -- <SERVICES FPJ>
1901                    , 'RATE'        , 'PO_SUB_REQ_LINE_NE_DIST_AMT'
1902                    , 'FIXED PRICE' , 'PO_SUB_REQ_LINE_NE_DIST_AMT'
1903                                    , 'PO_SUB_REQ_LINE_NE_DIST_QTY'
1904                    )
1905           );
1906 
1907     --Increment the p_sequence with number of errors reported in last query
1908     p_sequence := p_sequence + l_line_num.COUNT;
1909 ------------------------------------------------
1910 
1911 l_progress := '004';
1912 IF g_debug_stmt THEN
1913    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1914      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head||l_api_name||'.'
1915           || l_progress,'REQ 4: More than one dist for INVENTORY src type');
1916    END IF;
1917 END IF;
1918 
1919   -- Check 4:Lines with SOURCE type as INVENTORY can have one only one dist
1920     -- PO_SUB_REQ_SOURCE_ONE_DIST
1921 
1922   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REQ_SOURCE_ONE_DIST');
1923   INSERT INTO po_online_report_text_gt(online_report_id,
1924         last_update_login,
1925         last_updated_by,
1926         last_update_date,
1927         created_by,
1928         creation_date,
1929         line_num,
1930         shipment_num,
1931         distribution_num,
1932         sequence,
1933         text_line,
1934                 message_name)
1935   SELECT  p_online_report_id,
1936         p_login_id,
1937         p_user_id,
1938           sysdate,
1939         p_user_id,
1940         sysdate,
1941         PRL.line_num,
1942         0,
1943         0,
1944         p_sequence + ROWNUM,
1945         substr(g_linemsg||g_delim||PRL.line_num||g_delim||l_textline,1,240),
1946             'PO_SUB_REQ_SOURCE_ONE_DIST'
1947      FROM  PO_REQ_LINES_GT PRL
1948      WHERE PRL.requisition_header_id = p_document_id AND
1949            PRL.source_type_code = 'INVENTORY' AND
1950            nvl(PRL.cancel_flag,'N') = 'N' AND
1951            nvl(PRL.closed_code, 'OPEN') <> 'FINALLY CLOSED' AND
1952            1 < (SELECT count(PRD.requisition_line_id)
1953                 FROM  PO_REQ_DISTRIBUTIONS_GT PRD
1954                 WHERE PRD.requisition_line_id = PRL.requisition_line_id);
1955 
1956     --Increment the p_sequence with number of errors reported in last query
1957     p_sequence := p_sequence + SQL%ROWCOUNT;
1958 -----------------------------------------------------
1959 
1960 l_progress := '005';
1961 IF g_debug_stmt THEN
1962    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1963      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head||l_api_name||'.'
1964           || l_progress,'REQ 5: Rate cannot be nULL for foreign currency vendor');
1965    END IF;
1966 END IF;
1967 
1968   -- Check 5: Requistion Rate cannot be NULL if using a foreign currency vendor
1969     -- PO_SUB_REQ_RATE_NULL
1970   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REQ_RATE_NULL');
1971   INSERT INTO po_online_report_text_gt(online_report_id,
1972         last_update_login,
1973         last_updated_by,
1974         last_update_date,
1975         created_by,
1976         creation_date,
1977         line_num,
1978         shipment_num,
1979         distribution_num,
1980         sequence,
1981         text_line,
1982                 message_name)
1983   SELECT  p_online_report_id,
1984         p_login_id,
1985         p_user_id,
1986           sysdate,
1987         p_user_id,
1988         sysdate,
1989         PRL.line_num,
1990         0,
1991         0,
1992         p_sequence + ROWNUM,
1993         substr(g_linemsg||g_delim||PRL.line_num||g_delim||l_textline,1,240),
1994             'PO_SUB_REQ_RATE_NULL'
1995      FROM PO_REQ_LINES_GT PRL, FINANCIALS_SYSTEM_PARAMETERS FSP,
1996           GL_SETS_OF_BOOKS SOB
1997      WHERE PRL.requisition_header_id = p_document_id AND
1998            nvl(PRL.cancel_flag, 'N') = 'N' AND
1999            nvl(PRL.closed_code, 'OPEN') <> 'FINALLY CLOSED' AND
2000            SOB.set_of_books_id = FSP.set_of_books_id AND
2001            SOB.currency_code <> PRL.currency_code AND
2002            (PRL.rate is NULL OR
2003             PRL.rate_type is NULL OR
2004            (PRL.rate_type <> 'User' AND PRL.rate_date is NULL));
2005 
2006     --Increment the p_sequence with number of errors reported in last query
2007     p_sequence := p_sequence + SQL%ROWCOUNT;
2008 ----------------------------------------------
2009 
2010 l_progress := '006';
2011 
2012 -- Check 6: The Req GL date should be within an open purchasing period
2013 -- PO_SUB_REQ_INVALID_GL_DATE
2014 
2015 --<FPJ ENCUMBRANCE>
2016 
2017 IF (  PO_CORE_S.is_encumbrance_on(
2018          p_doc_type => g_document_type_REQUISITION
2019       ,  p_org_id => NULL
2020       )
2021    )
2022 THEN
2023 
2024    l_progress := '061';
2025    IF g_debug_stmt THEN
2026       PO_DEBUG.debug_stmt(g_log_head||l_api_name,
2027               l_progress,'REQ 6: GL date within open purchasing period check');
2028    END IF;
2029 
2030    check_gl_date(
2031       p_doc_type => g_document_type_REQUISITION
2032    ,  p_online_report_id => p_online_report_id
2033    ,  p_login_id => p_login_id
2034    ,  p_user_id => p_user_id
2035    ,  p_sequence => p_sequence
2036    );
2037 
2038    l_progress := '062';
2039 
2040 ELSE
2041    l_progress := '063';
2042    IF g_debug_stmt THEN
2043       PO_DEBUG.debug_stmt(g_log_head||l_api_name,
2044               l_progress,'REQ 6: Req encumbrance not on');
2045    END IF;
2046 END IF;
2047 
2048 l_progress := '007';
2049 IF g_debug_stmt THEN
2050    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2051      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head||l_api_name||'.'
2052           || l_progress,'REQ 7: Requisitions cannot have ATO/CTO model items');
2053    END IF;
2054 END IF;
2055 
2056   -- Check 7: Requisitions cannot have ATO/CTO model items (Bug 3362369)
2057         -- PO_ATO_ITEM_NA
2058   l_textline := FND_MESSAGE.GET_STRING('PO','PO_ATO_ITEM_NA');
2059   INSERT INTO po_online_report_text_gt(online_report_id,
2060         last_update_login,
2061         last_updated_by,
2062         last_update_date,
2063         created_by,
2064         creation_date,
2065         line_num,
2066         shipment_num,
2067         distribution_num,
2068         sequence,
2069         text_line,
2070                                 message_name)
2071   SELECT  p_online_report_id,
2072         p_login_id,
2073         p_user_id,
2074                   sysdate,
2075         p_user_id,
2076         sysdate,
2077         PRL.line_num,
2078         0,
2079         0,
2080         p_sequence + ROWNUM,
2081         substr(g_linemsg||g_delim||PRL.line_num||g_delim||l_textline,1,240),
2082                    'PO_ATO_ITEM_NA'
2083      FROM PO_REQ_LINES_GT PRL, FINANCIALS_SYSTEM_PARAMETERS FSP,
2084           MTL_SYSTEM_ITEMS MSI
2085      WHERE PRL.requisition_header_id = p_document_id AND
2086            nvl(PRL.cancel_flag, 'N') = 'N' AND
2087            nvl(PRL.closed_code, 'OPEN') <> 'FINALLY CLOSED' AND
2088            PRL.item_id is not null AND
2089            PRL.item_id = MSI.inventory_item_id AND
2090            FSP.inventory_organization_id = MSI.organization_id AND
2091            MSI.bom_item_type in (1,2);
2092 
2093     --Increment the p_sequence with number of errors reported in last query
2094     p_sequence := p_sequence + SQL%ROWCOUNT;
2095 -------------------------------------------------------------------------
2096 
2097     l_progress := '008';
2098 -- Check 8:
2099 --<R12 eTax Integration Start>
2100 -----------------------------------------------------------------------------
2101 
2102     l_tax_status := po_tax_interface_pvt.calculate_tax_yes_no(p_po_header_id   => NULL,
2103                                                               p_po_release_id  => NULL,
2104                                                               p_req_header_id  => p_document_id);
2105     IF g_debug_stmt THEN
2106         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2107           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head ||l_api_name||'.'
2108                         || l_progress,'REQ 8: Recalculate tax before approval = ' || l_tax_status);
2109         END IF;
2110     END IF;
2111     l_progress := '009';
2112     IF  l_tax_status = 'Y' THEN
2113       IF g_debug_stmt THEN
2114           IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2115             FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
2116                          l_api_name||'.' || l_progress,
2117                          'REQ 9: Calculate tax as the current one is not correct');
2118           END IF;
2119       END IF;
2120       l_progress := '010';
2121       po_tax_interface_pvt.calculate_tax_requisition( x_return_status         => l_return_status,
2122                                                       p_requisition_header_id => p_document_id,
2123                                                       p_calling_program       => g_action_DOC_SUBMISSION_CHECK);
2124 
2125       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2126         l_progress := '011';
2127         IF g_debug_stmt THEN
2128              IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2129                FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
2130                             l_api_name||'.' || l_progress,
2131                             'REQ 10: Calculate tax has errored out');
2132              END IF;
2133         END IF;
2134 
2135         l_tax_message := fnd_message.get_string('PO','PO_TAX_CALCULATION')||' : ' ;
2136 
2137         FORALL i IN 1..po_tax_interface_pvt.G_TAX_ERRORS_TBL.MESSAGE_TEXT.COUNT
2138            INSERT INTO po_online_report_text_gt
2139            (
2140             online_report_id,
2141             last_update_login,
2142             last_updated_by,
2143             last_update_date,
2144             created_by,
2145             creation_date,
2146             line_num,
2147             shipment_num,
2148             distribution_num,
2149             sequence,
2150             text_line,
2151             message_name,
2152             message_type
2153            )
2154            VALUES
2155            (
2156              p_online_report_id,
2157              p_login_id,
2158              p_user_id,
2159              sysdate,
2160              p_user_id,
2161              sysdate,
2162              po_tax_interface_pvt.G_TAX_ERRORS_TBL.line_num(i),
2163              po_tax_interface_pvt.G_TAX_ERRORS_TBL.shipment_num(i),
2164              po_tax_interface_pvt.G_TAX_ERRORS_TBL.distribution_num(i),
2165              p_sequence + ROWNUM,
2166              l_tax_message || po_tax_interface_pvt.G_TAX_ERRORS_TBL.message_text(i),
2167              'PO_TAX_CALCULATION_FAILED',
2168              'E'
2169            );
2170         l_progress := '012';
2171       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2172         l_progress := '013';
2173         IF g_debug_stmt THEN
2174            IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2175                FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
2176                             l_api_name||'.' || l_progress,
2177                             'REQ 11: Calculate tax raised unexpected error');
2178            END IF;
2179         END IF;
2180         l_textline := l_progress ||' - ';
2181         IF po_tax_interface_pvt.G_TAX_ERRORS_TBL.MESSAGE_TEXT.COUNT > 0 THEN
2182           l_textline := l_textline || po_tax_interface_pvt.G_TAX_ERRORS_TBL.MESSAGE_TEXT(1);
2183         ELSE
2184           l_textline := l_textline || SQLERRM();
2185         END IF;
2186         fnd_message.set_name('PO','PO_TAX_CALC_UNEXPECTED_ERROR');
2187         fnd_message.set_token('ERROR',l_textline);
2188         FND_MSG_PUB.Add;
2189         l_progress := '014';
2190         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2191       END IF;
2192     END IF;
2193 
2194     p_sequence := p_sequence + SQL%ROWCOUNT;
2195 
2196 -----------------------------------------------------------------------------
2197 
2198     l_progress := '015';
2199 --<R12 eTax Integration End>
2200     x_return_status := FND_API.G_RET_STS_SUCCESS;
2201 EXCEPTION
2202     WHEN FND_API.G_EXC_ERROR THEN
2203         x_return_status := FND_API.G_RET_STS_ERROR;
2204 
2205     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2206         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2207 
2208     WHEN OTHERS THEN
2209         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2210         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
2211             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
2212         END IF;
2213 
2214         IF (g_debug_unexp) THEN
2215                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
2216                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
2217                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
2218                        || l_progress || ' SQL CODE is '||sqlcode);
2219                 END IF;
2220         END IF;
2221 
2222 END CHECK_REQUISITIONS;
2223 --------------------------------------------------
2224 
2225 --For RELEASES
2226 /**
2227 * Private Procedure: CHECK_RELEASES
2228 * Requires:
2229 *   IN PARAMETERS:
2230 *       p_document_id:      The requisition_header_id of submitted document
2231 *       p_online_report_id: Id used to INSERT INTO online_report_text table
2232 *       p_user_id:          User performing the action
2233 *       p_login_id:         Last update login_id
2234 *   IN OUT PARAMETERS
2235 *       p_sequence:          Sequence number of last reported error
2236 * Modifies: Inserts error msgs in online_report_text_gt table, uses
2237 *           global_temp tables for processing
2238 * Effects:  This procedure runs the document submission checks for RELEASES
2239 * Returns:
2240 *  p_sequence: This parameter contains the current count of number of error
2241 *              messages inserted
2242 */
2243 PROCEDURE check_releases(p_document_id IN NUMBER,
2244                        p_online_report_id IN NUMBER,
2245                        p_user_id IN NUMBER,
2246                        p_login_id IN NUMBER,
2247                        p_sequence IN OUT NOCOPY NUMBER,
2248                        x_return_status OUT NOCOPY VARCHAR2) IS
2249 
2250 l_textline  po_online_report_text.text_line%TYPE := NULL;
2251 l_api_name  CONSTANT varchar2(40) := 'CHECK_RELEASES';
2252 l_progress VARCHAR2(3);
2253 
2254 --<Bug 2800804, 2792477 mbhargav START>
2255 l_total_rel_amount NUMBER :=0;
2256 l_this_rel_amount NUMBER :=0;
2257 l_previous_rel_amount NUMBER :=0;
2258 --<Bug 2800804, 2792477 mbhargav END>
2259 
2260 TYPE NumTab is TABLE of NUMBER INDEX by BINARY_INTEGER;
2261 l_quantity1 NumTab;
2262 l_quantity2 NumTab;
2263 l_line_num   NumTab;
2264 l_shipment_num NumTab;
2265 l_dist_num NumTab;
2266 l_rowcount NumTab;
2267 l_ship_qty_tbl    NumTab;                                     -- <SERVICES FPJ>
2268 l_ship_amt_tbl    NumTab;                                     -- <SERVICES FPJ>
2269 l_dist_qty_tbl    NumTab;                                     -- <SERVICES FPJ>
2270 l_dist_amt_tbl    NumTab;                                     -- <SERVICES FPJ>
2271 
2272 -- bug 6530879 Releases < Var addition START>
2273 l_agreement_id    PO_HEADERS_ALL.po_header_id%TYPE;
2274 l_vendor_id       PO_HEADERS_ALL.vendor_id%TYPE;
2275 l_vendor_site_id  PO_HEADERS_ALL.vendor_site_id%TYPE;
2276 l_vendor_contact_id PO_HEADERS_ALL.vendor_contact_id%TYPE;
2277 -- bug 6530879 Releases < Var addition END>
2278 
2279 --<R12 eTax Integration Start>
2280 l_return_status    VARCHAR2(1);
2281 l_tax_status       VARCHAR2(1);
2282 l_msg_count        NUMBER;
2283 l_msg_data         VARCHAR2(2000);
2284 l_tax_message      fnd_new_messages.message_text%TYPE;
2285 --<R12 eTax Integration End>
2286 
2287 --bug#3987438
2288 --A new table to hold the invalid ship to location codes
2289 TYPE CharTab is TABLE of HR_LOCATIONS_ALL_TL.location_code%type INDEX by BINARY_INTEGER;
2290 l_ship_to_location_tbl CharTab;
2291 --bug#3987438
2292 
2293 BEGIN
2294 
2295 l_progress := '000';
2296 -- BUG 2687600 mbhargav
2297 --Removed update statement to update rate in po_distributions
2298 
2299 l_progress := '001';
2300 IF g_debug_stmt THEN
2301    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2302      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2303           || l_progress,'REL 1: PA should be approved');
2304    END IF;
2305 END IF;
2306 
2307     -- Check 1: The Purchase Agreement associated with the Release
2308     -- must be Approved.
2309     -- PO_SUB_REL_PA_APPROVED
2310     --< Bug 3422733 > Only do this check if the BPA is not ON HOLD. The ON HOLD
2311     -- check is done later. Avoids showing 2 msgs for BPA that is ON HOLD.
2312 
2313   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REL_PA_APPROVED');
2314   INSERT INTO po_online_report_text_gt(online_report_id,
2315         last_update_login,
2316         last_updated_by,
2317         last_update_date,
2318         created_by,
2319         creation_date,
2320         line_num,
2321         shipment_num,
2322         distribution_num,
2323         sequence,
2324         text_line,
2325                 message_name)
2326   SELECT  p_online_report_id,
2327         p_login_id,
2328         p_user_id,
2329           sysdate,
2330         p_user_id,
2331         sysdate,
2332         0,0,0,
2333         p_sequence + ROWNUM,
2334         substr(l_textline,1,240),
2335             'PO_SUB_REL_PA_APPROVED'
2336     FROM   PO_RELEASES_GT POR,PO_HEADERS_GT POH
2337     WHERE  POR.po_header_id = POH.po_header_id
2338     AND    POR.po_release_id = p_document_id
2339     AND    NVL(POH.approved_flag, 'N') <> 'Y'
2340     AND    NVL(POH.user_hold_flag, 'N') <> 'Y';     --< Bug 3422733 >
2341 
2342     --Increment the p_sequence with number of errors reported in last query
2343     p_sequence := p_sequence + SQL%ROWCOUNT;
2344 ---------------------------------------------------------
2345 
2346 l_progress := '002';
2347 IF g_debug_stmt THEN
2348    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2349      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2350           || l_progress,'REL 2: Within PA effective dates ');
2351    END IF;
2352 END IF;
2353 
2354   -- Check 2: (Bug3199869)
2355         -- If release has not been approved before, the blanket
2356         -- cannot be expired already
2357         -- PO_SUB_REL_PA_EXPIRED
2358 
2359        -- bug3199869
2360        -- Modified Check 2 so that an error will be thrown only when
2361        -- 1) Release has not been approved once yet, AND
2362        -- 2) SYSDATE is after expiration date on the blanket header
2363 
2364   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REL_PA_EXPIRED');
2365   INSERT INTO po_online_report_text_gt(online_report_id,
2366         last_update_login,
2367         last_updated_by,
2368         last_update_date,
2369         created_by,
2370         creation_date,
2371         line_num,
2372         shipment_num,
2373         distribution_num,
2374         sequence,
2375         text_line,
2376                 message_name)
2377   SELECT  p_online_report_id,
2378         p_login_id,
2379         p_user_id,
2380           sysdate,
2381         p_user_id,
2382         sysdate,
2383         0,
2384         0,
2385         0,
2386         p_sequence + ROWNUM,
2387         substr(l_textline,1,240),
2388             'PO_SUB_REL_PA_EXPIRED'                    -- bug3199869
2389     FROM   PO_RELEASES_GT POR,PO_HEADERS_GT POH
2390     WHERE  POR.po_header_id = POH.po_header_id
2391     AND    POR.po_release_id = p_document_id
2392     AND    POR.approved_date IS NULL                   -- bug3199869
2393     AND    TRUNC(SYSDATE) >                            -- bug3199869
2394            TRUNC(NVL(POH.end_date, SYSDATE + 1));      -- bug3199869
2395 
2396      --Increment the p_sequence with number of errors reported in last query
2397     p_sequence := p_sequence + SQL%ROWCOUNT;
2398 ----------------------------------------
2399 
2400 l_progress := '003';
2401 IF g_debug_stmt THEN
2402    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2403      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2404           || l_progress,'REL 3: PA should not be ON HOLD');
2405    END IF;
2406 END IF;
2407 
2408   -- Check 3: Purchase Agreement assocaited with this release must not be
2409     -- on hold
2410     -- PO_SUB_REL_PA_ON_HOLD
2411 
2412   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REL_PA_ON_HOLD');
2413   INSERT INTO po_online_report_text_gt(online_report_id,
2414         last_update_login,
2415         last_updated_by,
2416         last_update_date,
2417         created_by,
2418         creation_date,
2419         line_num,
2420         shipment_num,
2421         distribution_num,
2422         sequence,
2423         text_line,
2424                 message_name)
2425   SELECT  p_online_report_id,
2426         p_login_id,
2427         p_user_id,
2428           sysdate,
2429         p_user_id,
2430         sysdate,
2431         0,
2432         0,
2433         0,
2434         p_sequence + ROWNUM,
2435         substr(l_textline,1,240),
2436             'PO_SUB_REL_PA_ON_HOLD'
2437     FROM  PO_RELEASES_GT POR,PO_HEADERS_GT POH
2438     WHERE  POR.po_header_id = POH.po_header_id
2439     AND    POR.po_release_id = p_document_id
2440     AND    nvl(POH.user_hold_flag, 'N') = 'Y';
2441 
2442 
2443      --Increment the p_sequence with number of errors reported in last query
2444     p_sequence := p_sequence + SQL%ROWCOUNT;
2445 -------------------------------------------------
2446 
2447 l_progress := '004';
2448 IF g_debug_stmt THEN
2449    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2450      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2451           || l_progress,'REL 4: PAs Supplier should not be ON HOLD');
2452    END IF;
2453 END IF;
2454 
2455   -- Check 4: Associated Purchase Agreement's supplier should not be on hold
2456     -- PO_SUB_REL_VENDOR_ON_HOLD
2457 
2458   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REL_VENDOR_ON_HOLD');
2459   INSERT INTO po_online_report_text_gt(online_report_id,
2460         last_update_login,
2461         last_updated_by,
2462         last_update_date,
2463         created_by,
2464         creation_date,
2465         line_num,
2466         shipment_num,
2467         distribution_num,
2468         sequence,
2469         text_line,
2470                 message_name)
2471     SELECT  p_online_report_id,
2472         p_login_id,
2473         p_user_id,
2474           sysdate,
2475         p_user_id,
2476         sysdate,
2477         0,
2478         0,
2479         0,
2480         p_sequence + ROWNUM,
2481         substr(l_textline,1,240),
2482             'PO_SUB_REL_VENDOR_ON_HOLD'
2483     FROM  PO_RELEASES_GT POR,PO_HEADERS_GT POH,PO_VENDORS POV,
2484           PO_SYSTEM_PARAMETERS PSP
2485     WHERE  POR.po_header_id  = POH.po_header_id
2486     AND    POV.vendor_id     = POH.vendor_id
2487     AND    POR.po_release_id = p_document_id
2488     AND    nvl(PSP.ENFORCE_VENDOR_HOLD_FLAG,'N') = 'Y'
2489     AND    nvl(POV.hold_flag,'N') = 'Y';
2490 
2491 
2492      --Increment the p_sequence with number of errors reported in last query
2493     p_sequence := p_sequence + SQL%ROWCOUNT;
2494 ---------------------------------------------------
2495 
2496 l_progress := '005';
2497 IF g_debug_stmt THEN
2498    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2499      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2500           || l_progress,'REL 5: Release must have atleast one shipment');
2501    END IF;
2502 END IF;
2503 
2504   -- Check 5: Every Release must have atleast one shipment
2505     -- PO_SUB_REL_NO_SHIP
2506 
2507   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REL_NO_SHIP');
2508   INSERT INTO po_online_report_text_gt(online_report_id,
2509         last_update_login,
2510         last_updated_by,
2511         last_update_date,
2512         created_by,
2513         creation_date,
2514         line_num,
2515         shipment_num,
2516         distribution_num,
2517         sequence,
2518         text_line,
2519                 message_name)
2520   SELECT  p_online_report_id,
2521         p_login_id,
2522         p_user_id,
2523           sysdate,
2524         p_user_id,
2525         sysdate,
2526         0,
2527         0,
2528         0,
2529         p_sequence + ROWNUM,
2530         substr(l_textline,1,240),
2531             'PO_SUB_REL_NO_SHIP'
2532     FROM  PO_RELEASES_GT POR
2533     WHERE  POR.po_release_id = p_document_id
2534     AND    NOT EXISTS
2535        (SELECT 'Shipment Exist'
2536         FROM   PO_LINE_LOCATIONS_GT PLL
2537         WHERE  PLL.po_release_id = POR.po_release_id);
2538         -- AND    nvl(PLL.cancel_flag,'N') = 'N');
2539         -- bug 3305488
2540 
2541      --Increment the p_sequence with number of errors reported in last query
2542     p_sequence := p_sequence + SQL%ROWCOUNT;
2543 
2544 -------------------------------------------
2545 --Bug5075191
2546 --Following submission check should exclude cancelled/finally closed lines.
2547 --Adding those conditions.
2548 --bug#3987438
2549 --Added a new submission check to validate the
2550 --ship to location at the shipment level.
2551 
2552 IF g_debug_stmt THEN
2553    FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2554           || l_progress,'REL : Invalid Ship To Locations');
2555 END IF;
2556 
2557     SELECT POLG.shipment_num,
2558            HLT.location_code
2559     BULK COLLECT INTO
2560            l_line_num,
2561            l_ship_to_location_tbl
2562     FROM  PO_LINE_LOCATIONS_GT POLG,
2563           HR_LOCATIONS_ALL HLA,
2564           HR_LOCATIONS_ALL_TL HLT
2565     WHERE POLG.po_release_id = p_document_id
2566     AND   POLG.ship_to_location_id=HLA.location_id
2567     AND   nvl(POLG.cancel_flag,'N') = 'N'
2568     AND   nvl(POLG.closed_code,'OPEN') <> 'FINALLY CLOSED'
2569     AND   NVL (TRUNC (HLA.INACTIVE_DATE), TRUNC (SYSDATE)+1 )<= TRUNC (SYSDATE)
2570     AND   HLA.location_id=HLT.location_id
2571     AND   HLT.language=USERENV('LANG');
2572 
2573     FOR i IN 1..l_line_num.COUNT LOOP
2574         l_rowCount(i) := i;
2575     END LOOP;
2576 
2577 FORALL i IN 1..l_line_num.COUNT
2578     INSERT INTO po_online_report_text_gt (
2579             online_report_id,
2580 			last_update_login,
2581 			last_updated_by,
2582 			last_update_date,
2583 			created_by,
2584 			creation_date,
2585 			line_num,
2586 			shipment_num,
2587 			distribution_num,
2588 			sequence,
2589 			text_line,
2590             message_name)
2591     VALUES(
2592             p_online_report_id,
2593             p_login_id,
2594             p_user_id,
2595             sysdate,
2596             p_user_id,
2597             sysdate,
2598             0,
2599             0,
2600             0,                                             -- <SERVICES FPJ>
2601             p_sequence+l_rowCount(i),
2602             substr(PO_CORE_S.get_translated_text
2603                         ( 'PO_SUB_REL_INVALID_SHIP_TO',
2604                           'LINE_NUM',
2605                           l_line_num(i),
2606                           'SHIP_TO_LOC',
2607                           l_ship_to_location_tbl(i)
2608                           ),1,240),
2609             'PO_SUB_REL_INVALID_SHIP_TO'
2610         );
2611 
2612     p_sequence := p_sequence + l_line_num.COUNT;
2613 --bug#3987438
2614 
2615 -------------------------------------------
2616 
2617 l_progress := '006';
2618 IF g_debug_stmt THEN
2619    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2620      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2621           || l_progress,'REL 6: check Price tolerance exceed on line');
2622    END IF;
2623 END IF;
2624 
2625   -- Check 6: Release Shipment Price/Amount should follow the pricing rules
2626     -- designated on the Purchase Agreement Line.
2627     -- Details: If price override is equal to 'Y', the Release Shipment
2628     -- Price/Amount must be less than the Price Override.
2629     --
2630     -- Bug 3177525: This check should not be done if allow_price_override_flag
2631     -- is 'N' because the user can never modify the release price/amount; it is
2632     -- automatically defaulted.
2633 
2634   INSERT INTO po_online_report_text_gt(online_report_id,
2635         last_update_login,
2636         last_updated_by,
2637         last_update_date,
2638         created_by,
2639         creation_date,
2640         line_num,
2641         shipment_num,
2642         distribution_num,
2643         sequence,
2644         text_line,
2645                 message_name)
2646   SELECT  p_online_report_id,
2647         p_login_id,
2648         p_user_id,
2649           sysdate,
2650         p_user_id,
2651         sysdate,
2652         0,
2653         PLL.shipment_num,
2654         0,
2655         p_sequence + ROWNUM,
2656             decode ( POL.order_type_lookup_code                -- <BUG 3262859>
2657                    , 'FIXED PRICE' , PO_CORE_S.get_translated_text
2658                                    (   'PO_SUB_REL_PRICE_GT_LIMIT'
2659                                    ,   'SHIP_NUM'   , PLL.shipment_num
2660                                    ,   'SHIP_PRICE' , nvl( PLL.price_override,PLL.amount)
2661                                    ,   'LINE_PRICE' , nvl ( POL.not_to_exceed_price
2662                                                           , PLL.price_override )
2663                                    )
2664                    ,               PO_CORE_S.get_translated_text
2665                                    (   'PO_SUB_REL_AMT_GT_LIMIT'
2666                                    ,   'SHIP_NUM'   , PLL.shipment_num
2667                                    ,   'SHIP_AMT'   , PLL.amount
2668                                    ,   'LINE_AMT'   , nvl ( POL.not_to_exceed_price
2669                                                           , PLL.amount )
2670                                    )
2671                    ),
2672             decode ( POL.order_type_lookup_code                -- <BUG 3262859>
2673                    , 'FIXED PRICE' , 'PO_SUB_REL_SHIP_PRICE_GT_LIMIT'
2674                    ,                 'PO_SUB_REL_SHIP_AMT_GT_LIMIT'
2675                    )
2676     FROM PO_LINE_LOCATIONS_GT PLL,PO_LINES POL
2677     WHERE PLL.po_line_id = POL.po_line_id
2678     AND   PLL.po_release_id = p_document_id
2679     AND   nvl(PLL.cancel_flag,'N')= 'N'
2680     AND   nvl(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
2681     AND   POL.allow_price_override_flag = 'Y'                 -- Bug 3177525
2682     AND   (                                                   -- <SERVICES FPJ>
2683               (   ( POL.order_type_lookup_code IN ('QUANTITY','AMOUNT','RATE'))-- <BUG 3262859>
2684               AND ( PLL.price_override > nvl ( POL.not_to_exceed_price
2685                                              , PLL.price_override ) )
2686               )
2687           OR
2688               (   ( POL.order_type_lookup_code IN ('FIXED PRICE') )               -- <BUG 3262859>
2689               AND ( PLL.amount > nvl ( POL.not_to_exceed_price, PLL.amount ) ) )
2690           )
2691     ;
2692 
2693      --Increment the p_sequence with number of errors reported in last query
2694     p_sequence := p_sequence + SQL%ROWCOUNT;
2695 ----------------------------------------
2696 
2697 l_progress := '007';
2698 IF g_debug_stmt THEN
2699    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2700      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2701           || l_progress,'REL 7: Rel shipment qty should match dist qty');
2702    END IF;
2703 END IF;
2704 
2705   -- Check 7: Quantities/Amounts must match between Release Shipments and
2706     -- Distributions
2707 
2708     SELECT
2709         0
2710     ,   PLL.shipment_num
2711     ,   0
2712     ,   PLL.quantity
2713     ,   PLL.amount                                            -- <SERVICES FPJ>
2714     ,   nvl(sum(POD.quantity_ordered),0)                      -- <SERVICES FPJ>
2715     ,   nvl(sum(POD.amount_ordered),0)                        -- <SERVICES FPJ>
2716     BULK COLLECT INTO
2717         l_line_num
2718     ,   l_shipment_num
2719     ,   l_dist_num
2720     ,   l_ship_qty_tbl                                        -- <SERVICES FPJ>
2721     ,   l_ship_amt_tbl                                        -- <SERVICES FPJ>
2722     ,   l_dist_qty_tbl                                        -- <SERVICES FPJ>
2723     ,   l_dist_amt_tbl                                        -- <SERVICES FPJ>
2724     FROM
2725         PO_DISTRIBUTIONS_GT POD                       -- <PO_CHANGE_API FPJ>
2726     ,   PO_LINE_LOCATIONS_GT PLL
2727     WHERE
2728         PLL.line_location_id = POD.line_location_id
2729     AND PLL.po_release_id = p_document_id
2730     AND nvl(PLL.cancel_flag,'N') = 'N'
2731     AND nvl(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
2732     AND (                                                     -- <SERVICES FPJ>
2733             (   ( PLL.quantity IS NOT NULL )
2734             AND ( PLL.quantity <> ( SELECT sum(POD2.quantity_ordered)
2735                                     FROM   PO_DISTRIBUTIONS_GT POD2
2736                                     WHERE  POD2.line_location_id = PLL.line_location_id ) ) )
2737         OR  (   ( PLL.amount IS NOT NULL )
2738             AND ( PLL.amount <> ( SELECT sum(POD2.amount_ordered)
2739                                   FROM   PO_DISTRIBUTIONS_GT POD2
2740                                   WHERE  POD2.line_location_id = PLL.line_location_id ) ) )
2741         )
2742     GROUP BY
2743         PLL.shipment_num
2744     ,   PLL.quantity
2745     ,   PLL.amount;                                           -- <SERVICES FPJ>
2746 
2747     FOR i IN 1..l_line_num.COUNT LOOP
2748         l_rowCount(i) := i;
2749     END LOOP;
2750 
2751     FORALL i IN 1..l_line_num.COUNT
2752         INSERT INTO po_online_report_text_gt(online_report_id,
2753         last_update_login,
2754         last_updated_by,
2755         last_update_date,
2756         created_by,
2757         creation_date,
2758         line_num,
2759         shipment_num,
2760         distribution_num,
2761         sequence,
2762         text_line,
2763                 message_name)
2764         VALUES(
2765             p_online_report_id,
2766             p_login_id,
2767             p_user_id,
2768             sysdate,
2769             p_user_id,
2770             sysdate,
2771             NULL,                                             -- <SERVICES FPJ>
2772             l_shipment_num(i),
2773             NULL,                                             -- <SERVICES FPJ>
2774             p_sequence+l_rowCount(i),
2775             decode ( l_ship_qty_tbl(i)                        -- <SERVICES FPJ>
2776                    , NULL , PO_CORE_S.get_translated_text
2777                             (   'PO_SUB_REL_SHIP_NE_DIST_AMT'
2778                             ,   'SHIP_NUM', l_shipment_num(i)
2779                             ,   'SHIP_AMT', l_ship_amt_tbl(i)
2780                             ,   'DIST_AMT', l_dist_amt_tbl(i)
2781                             )
2782                    ,        PO_CORE_S.get_translated_text
2783                             (   'PO_SUB_REL_SHIP_NE_DIST_QTY'
2784                             ,   'SHIP_NUM', l_shipment_num(i)
2785                             ,   'SHIP_QTY', l_ship_qty_tbl(i)
2786                             ,   'DIST_QTY', l_dist_qty_tbl(i)
2787                             )
2788                    ),
2789             decode ( l_ship_qty_tbl(i)                        -- <SERVICES FPJ>
2790                    , NULL , 'PO_SUB_REL_SHIP_NE_DIST_AMT'
2791                    ,        'PO_SUB_REL_SHIP_NE_DIST_QTY'
2792                    )
2793         );
2794 
2795     --Increment the p_sequence with number of errors reported in last query
2796     p_sequence := p_sequence + l_line_num.COUNT;
2797 ---------------------------------------------------------
2798 
2799 l_progress := '008';
2800 IF g_debug_stmt THEN
2801    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2802      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2803           || l_progress,'REL 8: Rel shipment should have atleast one dist ');
2804    END IF;
2805 END IF;
2806 
2807   -- Check 8: All Release shipments should have atleast one distribution
2808     -- PO_SUB_REL_SHIP_NO_DIST
2809 
2810   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REL_SHIP_NO_DIST');
2811   INSERT INTO po_online_report_text_gt(online_report_id,
2812         last_update_login,
2813         last_updated_by,
2814         last_update_date,
2815         created_by,
2816         creation_date,
2817         line_num,
2818         shipment_num,
2819         distribution_num,
2820         sequence,
2821         text_line,
2822                 message_name)
2823   SELECT  p_online_report_id,
2824         p_login_id,
2825         p_user_id,
2826           sysdate,
2827         p_user_id,
2828         sysdate,
2829         0,
2830         PLL.shipment_num,
2831         0,
2832         p_sequence + ROWNUM,
2833         substr(g_shipmsg||g_delim||PLL.shipment_num||g_delim
2834                    ||l_textline,1,240),
2835             'PO_SUB_REL_SHIP_NO_DIST'
2836     FROM PO_LINE_LOCATIONS_GT PLL
2837     WHERE PLL.po_release_id = p_document_id
2838     AND nvl(PLL.cancel_flag,'N') = 'N'
2839     AND nvl(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
2840     AND NOT EXISTS
2841        (SELECT 'Distribution Exists'
2842         FROM PO_DISTRIBUTIONS_GT POD                  -- <PO_CHANGE_API FPJ>
2843         WHERE POD.line_location_id = PLL.line_location_id);
2844 
2845 
2846      --Increment the p_sequence with number of errors reported in last query
2847     p_sequence := p_sequence + SQL%ROWCOUNT;
2848 -----------------------------------------------------
2849 
2850 l_progress := '009';
2851 IF g_debug_stmt THEN
2852    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2853      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2854           || l_progress,'REL 9: Dist rate cannot be NULL if using foreign currency vendor');
2855    END IF;
2856 END IF;
2857 
2858   -- Check 9: Rate on the Release shipment must not be NULL if using a foreign
2859     -- currency vendor. We are using foreign currency if the currency on PO
2860     -- Header does not match Set of Books currency
2861     -- PO_SUB_REL_RATE_NULL
2862   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REL_RATE_NULL');
2863     INSERT INTO po_online_report_text_gt(online_report_id,
2864         last_update_login,
2865         last_updated_by,
2866         last_update_date,
2867         created_by,
2868         creation_date,
2869         line_num,
2870         shipment_num,
2871         distribution_num,
2872         sequence,
2873         text_line,
2874                 message_name)
2875     SELECT  p_online_report_id,
2876         p_login_id,
2877         p_user_id,
2878           sysdate,
2879         p_user_id,
2880         sysdate,
2881         0,
2882         POLL.shipment_num,
2883             POD.distribution_num,
2884         p_sequence + ROWNUM,
2885         substr(g_shipmsg||g_delim||POLL.shipment_num||g_delim
2886                    ||g_distmsg||g_delim||POD.distribution_num||g_delim
2887                    ||l_textline,1,240),
2888             'PO_SUB_REL_RATE_NULL'
2889     FROM PO_DISTRIBUTIONS_GT POD,                     -- <PO_CHANGE_API FPJ>
2890          PO_LINE_LOCATIONS_GT POLL,PO_LINES POL,
2891          PO_RELEASES_GT POR,PO_HEADERS_GT POH,GL_SETS_OF_BOOKS SOB,
2892          FINANCIALS_SYSTEM_PARAMETERS FSP
2893     WHERE POLL.po_release_id = POR.po_release_id
2894     AND POD.line_location_id = POLL.line_location_id
2895     AND POLL.po_line_id = POL.po_line_id
2896     AND POH.po_header_id = POR.po_header_id
2897     AND POR.po_release_id = p_document_id
2898     AND SOB.set_of_books_id = FSP.set_of_books_id
2899     AND nvl(POLL.cancel_flag, 'N') = 'N'
2900     AND nvl(POLL.closed_code, 'OPEN') <> 'FINALLY CLOSED'
2901     AND SOB.currency_code <> POH.currency_code
2902     AND POD.rate is null;
2903 
2904 
2905      --Increment the p_sequence with number of errors reported in last query
2906     p_sequence := p_sequence + SQL%ROWCOUNT;
2907 -----------------------------------------------
2908 
2909 l_progress := '010';
2910 IF g_debug_stmt THEN
2911    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2912      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2913           || l_progress,'REL 10: Amount should be greater than min release amount ');
2914    END IF;
2915 END IF;
2916 
2917   -- Check 10:Release total amount must be greater than Purchase Agreement
2918     -- minimum release amount
2919     -- PO_SUB_REL_AMT_LESS_MINREL_AMT
2920 
2921   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REL_AMT_LESS_MINREL_AMT');
2922   INSERT INTO po_online_report_text_gt(online_report_id,
2923         last_update_login,
2924         last_updated_by,
2925         last_update_date,
2926         created_by,
2927         creation_date,
2928         line_num,
2929         shipment_num,
2930         distribution_num,
2931         sequence,
2932         text_line,
2933                 message_name)
2934   SELECT  p_online_report_id,
2935         p_login_id,
2936         p_user_id,
2937           sysdate,
2938         p_user_id,
2939         sysdate,
2940         0,
2941         0,
2942         0,
2943         p_sequence + ROWNUM,
2944         substr(l_textline,1,240),
2945             'PO_SUB_REL_AMT_LESS_MINREL_AMT'
2946     FROM  PO_HEADERS_GT POH,PO_RELEASES_GT POR
2947     WHERE POR.po_release_id = p_document_id
2948     AND   POH.po_header_id  = POR.po_header_id
2949     AND   POH.min_release_amount IS NOT NULL
2950     AND   POH.min_release_amount >                            -- <SERVICES FPJ>
2951               ( SELECT decode ( sum( decode ( PLL2.quantity
2952                                             , NULL , ( PLL2.amount - nvl(PLL2.amount_cancelled,0) )
2953                                             ,        ( PLL2.quantity - nvl(PLL2.quantity_cancelled,0) )
2954                                             )
2955                                    )
2956                               , 0 , POH.min_release_amount
2957                               ,     sum ( decode ( PLL2.quantity
2958                                                  , NULL , ( PLL2.amount - nvl(PLL2.amount_cancelled,0) )
2959                                                  ,        (   ( PLL2.quantity - nvl(PLL2.quantity_cancelled,0) )
2960                                                           *   PLL2.price_override )
2961                                                  )
2962                                         )
2963                               )
2964                 FROM   PO_LINE_LOCATIONS_GT PLL2
2965                 WHERE  PLL2.po_release_id = POR.po_release_id
2966                 AND    PLL2.shipment_type IN ('BLANKET', 'SCHEDULED')
2967               );
2968 
2969      --Increment the p_sequence with number of errors reported in last query
2970     p_sequence := p_sequence + SQL%ROWCOUNT;
2971 -------------------------------------------------
2972 -- Bug 7188760
2973 -- Added the POH.Amount_Limit is Not Null condition in the following sqls
2974 l_progress := '011';
2975 IF g_debug_stmt THEN
2976    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2977      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2978           || l_progress,'REL 11: Not to exceed Amount Limit');
2979    END IF;
2980 END IF;
2981 
2982   -- Check 11: The amount being released plus the amount released to-date
2983     -- against the PA must be less than the amount limit for the agreement
2984     -- PO_SUB_REL_AMT_GRT_LIMIT_AMT
2985 
2986 
2987        --<Bug 2800804, 2792477 mbhargav START>
2988        --Split the amount calculation to two select statements,
2989        --this was required because iSP is sending some chnage_requests
2990        --which are only in GT tables so need to get the amount for
2991        --current document from GT table and not the base tables
2992 
2993        --This select statement gets the amount on current document
2994 
2995        SELECT                                                 -- <SERVICES FPJ>
2996               sum ( decode ( PLL1.quantity
2997                            , NULL , ( PLL1.amount
2998                                     - nvl(PLL1.amount_cancelled,0) )
2999                            ,        (   ( PLL1.quantity
3000                                         - nvl(PLL1.quantity_cancelled,0) )
3001                                     * PLL1.price_override ) ) )
3002        INTO  l_this_rel_amount
3003        FROM PO_LINE_LOCATIONS_GT PLL1, PO_RELEASES_GT POR1, PO_HEADERS_GT POH
3004        WHERE  POR1.po_release_id = p_document_id
3005        AND    POR1.po_header_id  = POH.po_header_id	-- Bug 7188760
3006        AND    POH.amount_limit is Not Null		-- Bug 7188760
3007        AND    PLL1.po_release_id = POR1.po_release_id;
3008 
3009        --This select statement adds amount from all OTHER approved releases
3010        --for this Blanket
3011 
3012        SELECT                                                 -- <SERVICES FPJ>
3013               /* FULL(POH)*/
3014               nvl ( sum ( decode ( PLL2.quantity
3015                                  , NULL , ( PLL2.amount
3016                                           - nvl(PLL2.amount_cancelled,0) )
3017                                  ,        (   ( PLL2.quantity
3018                                           - nvl(PLL2.quantity_cancelled,0) )
3019                                           * PLL2.price_override ) ) )
3020                   , 0 )
3021        INTO l_previous_rel_amount
3022        FROM PO_LINE_LOCATIONS PLL2, PO_RELEASES POR2, PO_HEADERS_GT POH
3023        WHERE PLL2.po_release_id = POR2.po_release_id
3024        AND POR2.po_header_id = POH.po_header_id
3025        AND nvl(POR2.approved_flag, 'N') = 'Y'
3026        AND POH.amount_limit is Not Null		-- Bug 7188760
3027        AND POR2.po_release_id <> p_document_id;
3028 
3029 
3030        --Get the total amount released/to be released for this shipment
3031        l_total_rel_amount := l_this_rel_amount + l_previous_rel_amount;
3032        --<Bug 2800804, 2792477 mbhargav END>
3033 
3034 
3035   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REL_AMT_GRT_LIMIT_AMT');
3036   INSERT INTO po_online_report_text_gt (online_report_id,
3037         last_update_login,
3038         last_updated_by,
3039         last_update_date,
3040         created_by,
3041         creation_date,
3042         line_num,
3043         shipment_num,
3044         distribution_num,
3045         sequence,
3046         text_line,
3047                 message_name)
3048   SELECT  p_online_report_id,
3049         p_login_id,
3050         p_user_id,
3051           sysdate,
3052         p_user_id,
3053         sysdate,
3054         0,
3055         0,
3056         0,
3057         p_sequence + ROWNUM,
3058         substr(l_textline,1,240),
3059             'PO_SUB_REL_AMT_GRT_LIMIT_AMT'
3060     FROM   PO_HEADERS_GT POH,PO_RELEASES_GT POR
3061     WHERE  POR.po_release_id = p_document_id
3062     AND    POH.po_header_id  = POR.po_header_id
3063 
3064     AND    POH.amount_limit is not null
3065     --<Bug 2800804, 2792477 mbhargav START>
3066     --Compare it to total of current release amount and
3067     --already released amount calculated above the INSERT statement
3068     AND    POH.amount_limit < l_total_rel_amount;
3069 
3070      --Increment the p_sequence with number of errors reported in last query
3071     p_sequence := p_sequence + SQL%ROWCOUNT;
3072 ------------------------------------------------
3073 
3074 l_progress := '012';
3075 IF g_debug_stmt THEN
3076    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3077      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
3078           || l_progress,
3079           'REL 12: Amount released check for min release amt');
3080    END IF;
3081 END IF;
3082 
3083   -- Check 12: The Amount being released for all shipments for a particular line
3084     -- must be greater than the min release amount specified in agreement line
3085     -- PO_SUB_REL_SHIPAMT_LESS_MINREL
3086 
3087     l_textline := FND_MESSAGE.GET_STRING('PO','PO_SUB_REL_SHIPAMT_LESS_MINREL');
3088     SELECT
3089         POL.line_num,
3090         0,
3091         0,
3092         POL.min_release_amount,
3093         0
3094     BULK COLLECT INTO
3095         l_line_num,
3096         l_shipment_num,
3097         l_dist_num,
3098         l_quantity1,
3099         l_quantity2
3100     FROM   PO_LINES_ALL POL,PO_RELEASES_GT POR,PO_LINE_LOCATIONS_GT PLL
3101     WHERE  PLL.po_release_id = POR.po_release_id
3102     AND    PLL.po_release_id = p_document_id
3103     AND    POL.po_line_id  = PLL.po_line_id
3104     AND    POL.min_release_amount is not null
3105     AND    POL.min_release_amount >
3106            (   SELECT
3107                   decode ( sum ( decode ( PLL2.quantity                   /*Bug 5028960 pol.quantity */
3108                                          , NULL , PLL2.amount - nvl(PLL2.amount_cancelled,0)
3109                                          ,        PLL2.quantity - nvl(PLL2.quantity_cancelled,0)
3110                                          )
3111                                 )
3112                           , 0 , POL.min_release_amount
3113                           ,     sum ( decode ( PLL2.quantity     /*Bug 5028960  pol.quantity */
3114                                              , NULL , PLL2.amount - nvl(PLL2.amount_cancelled,0)
3115                                              ,        (  ( PLL2.quantity - nvl(PLL2.quantity_cancelled,0) )
3116                                                       *  PLL2.price_override )
3117                                              )
3118                                     )
3119                           )
3120                --<Bug 2792477 mbhargav>
3121                --Change the table in from clause from PO_LINE_LOCATIONS to PO_LINE_LOCATIONS_GT
3122                FROM PO_LINE_LOCATIONS_GT PLL2
3123                WHERE PLL2.po_line_id = POL.po_line_id
3124                AND PLL2.po_release_id = POR.po_release_id
3125                AND PLL2.shipment_type in ('BLANKET', 'SCHEDULED')
3126           )
3127     GROUP BY POL.line_num,POL.min_release_amount;
3128 
3129     FOR i IN 1..l_line_num.COUNT LOOP
3130         l_rowCount(i) := i;
3131     END LOOP;
3132 
3133     FORALL i IN 1..l_line_num.COUNT
3134         INSERT INTO po_online_report_text_gt(online_report_id,
3135         last_update_login,
3136         last_updated_by,
3137         last_update_date,
3138         created_by,
3139         creation_date,
3140         line_num,
3141         shipment_num,
3142         distribution_num,
3143         sequence,
3144         text_line,
3145                 message_name)
3146         VALUES(
3147             p_online_report_id,
3148             p_login_id,
3149             p_user_id,
3150             sysdate,
3151             p_user_id,
3152             sysdate,
3153             0,0,0,
3154             p_sequence+l_rowCount(i),
3155             substr(l_textline||g_delim||l_quantity1(i),1,240),
3156             'PO_SUB_REL_SHIPAMT_LESS_MINREL');
3157 
3158     --Increment the p_sequence with number of errors reported in last query
3159     p_sequence := p_sequence + l_line_num.COUNT;
3160 ----------------------------------------------
3161 
3162 l_progress := '600';
3163 
3164 -- Check 13: The Release GL date should be within an open purchasing period
3165 -- PO_SUB_REL_INVALID_GL_DATE
3166 
3167 --<FPJ ENCUMBRANCE>
3168 
3169 IF (  PO_CORE_S.is_encumbrance_on(
3170          p_doc_type => g_document_type_RELEASE
3171       ,  p_org_id => NULL
3172       )
3173    )
3174 THEN
3175 
3176    l_progress := '610';
3177    IF g_debug_stmt THEN
3178       PO_DEBUG.debug_stmt(g_log_head || '.'||l_api_name||'.',
3179                           l_progress,'REL 13: GL date check ');
3180    END IF;
3181 
3182    check_gl_date(
3183       p_doc_type => g_document_type_RELEASE
3184    ,  p_online_report_id => p_online_report_id
3185    ,  p_login_id => p_login_id
3186    ,  p_user_id => p_user_id
3187    ,  p_sequence => p_sequence
3188    );
3189 
3190    l_progress := '620';
3191 
3192 ELSE
3193    l_progress := '630';
3194    IF g_debug_stmt THEN
3195       PO_DEBUG.debug_stmt(g_log_head || '.'||l_api_name||'.',
3196                           l_progress,'REL 13: release encumbrance not on');
3197    END IF;
3198 END IF;
3199 
3200 ----------------------------------------------
3201 
3202 l_progress := '014';
3203 IF g_debug_stmt THEN
3204    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3205      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
3206           || l_progress,'Rel 14: UOM Interclass conversions check');
3207    END IF;
3208 END IF;
3209 
3210   -- Check 14: Invalid Interclass conversions between UOMs should not be allowed
3211     -- PO_SUB_UOM_CLASS_CONVERSION, PO_SUB_REL_INVALID_CLASS_CONV
3212     -- Message inserted is:
3213     --'Shipment# <ShipNum> Following Interclass UOM conversion is not defined or
3214     -- is disabled <UOM1> <UOM2>'
3215     --   Bug #1630662
3216   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_UOM_CLASS_CONVERSION');
3217     INSERT INTO po_online_report_text_gt(online_report_id,
3218         last_update_login,
3219         last_updated_by,
3220         last_update_date,
3221         created_by,
3222         creation_date,
3223         line_num,
3224         shipment_num,
3225         distribution_num,
3226         sequence,
3227         text_line,
3228                                 message_name)
3229   SELECT  p_online_report_id,
3230         p_login_id,
3231         p_user_id,
3232           sysdate,
3233         p_user_id,
3234         sysdate,
3235             0,
3236             POLL.shipment_num,
3237             0,
3238             p_sequence + ROWNUM,
3239             substr(g_shipmsg||g_delim||POLL.shipment_num||g_delim||l_textline||
3240                    MTL1.uom_class||' , '||MTL2.uom_class,1,240),
3241             'PO_SUB_UOM_CLASS_CONVERSION'
3242     FROM MTL_UOM_CLASS_CONVERSIONS MOU, PO_LINE_LOCATIONS_GT POLL,
3243          PO_LINES POL, MTL_UOM_CLASSES_TL MTL1,
3244          MTL_UOM_CLASSES_TL MTL2
3245     WHERE MOU.inventory_item_id = POL.item_id
3246     AND   (NVL(MOU.disable_date, TRUNC(SYSDATE)) + 1) < TRUNC(SYSDATE)
3247     AND   POL.po_line_id = POLL.po_line_id
3248     AND   POLL.po_release_id = p_document_id
3249     AND   MOU.from_uom_class = MTL1.uom_class
3250     AND   MOU.to_uom_class = MTL2.uom_class
3251     AND EXISTS
3252        (SELECT 'uom conversion exists'
3253         FROM MTL_UNITS_OF_MEASURE MUM
3254         WHERE POL.unit_meas_lookup_code = MUM.unit_of_measure
3255         AND   MOU.to_uom_class = MUM.uom_class);
3256 
3257     --Increment the p_sequence with number of errors reported in last query
3258     p_sequence := p_sequence + SQL%ROWCOUNT;
3259 
3260 --------------------------------------------------
3261 
3262 l_progress := '015';
3263 IF g_debug_stmt THEN
3264    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3265      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
3266           || l_progress,'REL 15: Item restricted check ');
3267    END IF;
3268 END IF;
3269 
3270   -- Check 15:  If an item is restricted then the Purchase Order Vendor
3271     -- must be listed in the Approved Suppliers List table and must be approved.
3272     -- PO_SUB_ITEM_NOT_APPROVED_REL
3273     -- Bug# 2461828
3274     /*Bug5597639 Modifying the below sql to ensure that whenever the item
3275      is restricted by checking 'Use approved supplier list' there should be
3276      atlease one approved ASL either at item level or at category level
3277      if there is no item ASL*/
3278 
3279   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_ITEM_NOT_APPROVED_REL');
3280   INSERT INTO po_online_report_text_gt(online_report_id,
3281         last_update_login,
3282         last_updated_by,
3283         last_update_date,
3284         created_by,
3285         creation_date,
3286         line_num,
3287         shipment_num,
3288         distribution_num,
3289         sequence,
3290         text_line,
3291                 message_name)
3292   SELECT  p_online_report_id,
3293         p_login_id,
3294         p_user_id,
3295           sysdate,
3296         p_user_id,
3297         sysdate,
3298         POL.line_num, --<Bug 3123365>
3299         PLL.shipment_num,  --<Bug 3123365>
3300         0,
3301         p_sequence + ROWNUM,
3302                     --<Bug 3123365 mbhargav START>
3303                     substr(g_linemsg||g_delim||POL.line_num||g_delim
3304                    ||g_shipmsg||g_delim||PLL.shipment_num||g_delim
3305                    ||l_textline,1,240),
3306                    --<Bug 3123365 mbhargav END>
3307             'PO_SUB_ITEM_NOT_APPROVED_REL'
3308     FROM MTL_SYSTEM_ITEMS MSI, PO_LINE_LOCATIONS_GT PLL,
3309          PO_RELEASES_GT POR,PO_LINES POL, PO_HEADERS_GT POH,
3310          FINANCIALS_SYSTEM_PARAMETERS FSP
3311     WHERE POR.po_release_id = p_document_id
3312     AND POR.po_header_id = POH.po_header_id
3313     AND POR.po_header_id = POL.po_header_id
3314     AND POL.po_line_id = PLL.po_line_id
3315     AND POR.po_release_id = PLL.po_release_id
3316     AND MSI.organization_id = PLL.SHIP_TO_ORGANIZATION_id
3317     AND MSI.inventory_item_id = POL.item_id
3318     AND POL.item_id is not null
3319     AND nvl(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
3320     AND nvl(POL.cancel_flag,'N') = 'N'
3321     AND nvl(PLL.cancel_flag,'N') = 'N'
3322     AND nvl(MSI.must_use_approved_vendor_flag,'N') = 'Y'
3323     AND not exists
3324        (SELECT 1
3325         FROM PO_APPROVED_SUPPLIER_LIS_VAL_V ASL, PO_ASL_STATUS_RULES ASR
3326         WHERE  ASL.using_organization_id in (PLL.ship_to_organization_id, -1)
3327         AND    ASL.vendor_id = POH.vendor_id
3328         AND    nvl(ASL.vendor_site_id, POH.vendor_site_id) = POH.vendor_site_id
3329         AND   ASL.item_id = POL.item_id
3330         AND    ASL.asl_status_id = ASR.status_id
3331         AND    ASR.business_rule = '1_PO_APPROVAL'
3332 	AND    ASR.allow_action_flag = 'Y'        --Bug5597639
3333         UNION ALL
3334         SELECT 1
3335         FROM PO_APPROVED_SUPPLIER_LIS_VAL_V ASL, PO_ASL_STATUS_RULES ASR
3336         WHERE  ASL.using_organization_id in (PLL.ship_to_organization_id , -1)
3337         AND    ASL.vendor_id = POH.vendor_id
3338         AND    nvl(ASL.vendor_site_id, POH.vendor_site_id) = POH.vendor_site_id
3339         AND    ASL.item_id is NULL
3340         AND    not exists
3341            (SELECT ASL1.ASL_ID
3342             FROM PO_APPROVED_SUPPLIER_LIS_VAL_V ASL1
3343             WHERE ASL1.ITEM_ID = POL.item_id
3344             AND ASL1.using_organization_id in (PLL.ship_to_organization_id, -1))
3345         AND    ASL.category_id in
3346            (SELECT MIC.category_id
3347             FROM   MTL_ITEM_CATEGORIES MIC
3348             WHERE MIC.inventory_item_id = POL.item_id
3349             AND MIC.organization_id = PLL.ship_to_organization_id)
3350         AND    ASL.asl_status_id = ASR.status_id
3351         AND    ASR.business_rule = '1_PO_APPROVAL'
3352         AND    ASR.allow_action_flag = 'Y') ;  --Bug5597639
3353 
3354      --Increment the p_sequence with number of errors reported in last query
3355     p_sequence := p_sequence + SQL%ROWCOUNT;
3356 ---------------------------------------------
3357 
3358 l_progress := '016';
3359 IF g_debug_stmt THEN
3360    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3361      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
3362           || l_progress,'REL 16: ASL Debarred check ');
3363    END IF;
3364 END IF;
3365 
3366   -- Check 16: Determine if an item is restricted.  If it is restricted the
3367     -- Purchase Order Vendor must be listed in the Approved Suppliers
3368     -- List table and must be approved for release to get approved.
3369     -- Bug 839743
3370     -- PO_SUB_ITEM_ASL_DEBARRED_REL
3371 
3372       /*Bug5597639 This check would throw an error message if atleast one ASL
3373        entry is debarred either for item /Category irrespective of 'Use approved
3374        supplier flag'. This check would apply even for one time items.
3375        If supplier is debarred in any of the ASL item/category (Global/Local)
3376        (Suplier/Supplier+site) then the approval of the PO will not be allowed
3377        Need to remove the join with mtl_item_categories for one
3378        time items as there will not be any record
3379  */
3380 
3381   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_ITEM_ASL_DEBARRED_REL');
3382   INSERT INTO po_online_report_text_gt(online_report_id,
3383         last_update_login,
3384         last_updated_by,
3385         last_update_date,
3386         created_by,
3387         creation_date,
3388         line_num,
3389         shipment_num,
3390         distribution_num,
3391         sequence,
3392         text_line,
3393                 message_name)
3394   SELECT  p_online_report_id,
3395         p_login_id,
3396         p_user_id,
3397           sysdate,
3398         p_user_id,
3399         sysdate,
3400                     POL.line_num, --<Bug 3123365>
3401                     PLL.shipment_num,  --<Bug 3123365>
3402                     0,
3403                     p_sequence + ROWNUM,
3404                     --<Bug 3123365 mbhargav START>
3405                     substr(g_linemsg||g_delim||POL.line_num||g_delim
3406                    ||g_shipmsg||g_delim||PLL.shipment_num||g_delim
3407                    ||l_textline,1,240),
3408                    --<Bug 3123365 mbhargav END>
3409             'PO_SUB_ITEM_ASL_DEBARRED_REL'
3410     FROM PO_LINE_LOCATIONS_GT PLL,
3411          PO_RELEASES_GT POR,PO_LINES POL, PO_HEADERS_GT POH,
3412          FINANCIALS_SYSTEM_PARAMETERS FSP
3413     WHERE POR.po_release_id = p_document_id
3414     AND POR.po_header_id = POH.po_header_id
3415     AND POR.po_header_id = POL.po_header_id
3416     AND POL.po_line_id = PLL.po_line_id
3417     AND POR.po_release_id = PLL.po_release_id
3418     AND nvl(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
3419     AND nvl(POL.cancel_flag,'N') = 'N'
3420     AND nvl(PLL.cancel_flag,'N') = 'N'
3421     AND exists
3422       (SELECT 1
3423         FROM PO_APPROVED_SUPPLIER_LIS_VAL_V ASL, PO_ASL_STATUS_RULES ASR,
3424 	 MTL_SYSTEM_ITEMS MSI  --Bug5597639
3425          WHERE  ASL.using_organization_id in (PLL.ship_to_organization_id, -1)
3426         /*Bug5553138 Adding the below three conditions */
3427 	AND MSI.organization_id = FSP.inventory_organization_id
3428 	AND MSI.inventory_item_id = POL.item_id
3429 	AND  POL.item_id is not null
3430         AND    ASL.vendor_id = POH.vendor_id
3431         AND    nvl(ASL.vendor_site_id, POH.vendor_site_id) = POH.vendor_site_id
3432         AND   ASL.item_id = POL.item_id
3433         AND    ASL.asl_status_id = ASR.status_id
3434         AND    ASR.business_rule = '1_PO_APPROVAL'
3435  	AND   ASR.allow_action_flag <> 'Y'   --Bug5597639
3436         UNION ALL
3437          SELECT 1
3438         FROM PO_APPROVED_SUPPLIER_LIS_VAL_V ASL, PO_ASL_STATUS_RULES ASR
3439         WHERE  ASL.using_organization_id in (PLL.ship_to_organization_id , -1)
3440         AND    ASL.vendor_id = POH.vendor_id
3441         AND    nvl(ASL.vendor_site_id, POH.vendor_site_id) = POH.vendor_site_id
3442         AND    ASL.item_id is NULL
3443 	AND ASL.category_id = POL.category_id --Bug5597639
3444         AND    ASL.asl_status_id = ASR.status_id
3445         AND    ASR.business_rule = '1_PO_APPROVAL'
3446         AND    ASR.allow_action_flag <> 'Y' );  --Bug5597639
3447      --Increment the p_sequence with number of errors reported in last query
3448     p_sequence := p_sequence + SQL%ROWCOUNT;
3449 -------------------------------------------------------------------------
3450 
3451  l_progress := '017';
3452 
3453     IF g_debug_stmt THEN
3454         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3455           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
3456                        l_api_name||'.' || l_progress,
3457                        'REL 17: ATO/CTO Model items not allowed on Releases');
3458         END IF;
3459     END IF;
3460 
3461     -- Check 17: ATO/CTO Model items not allowed on Releases (Bug 3362369)
3462 
3463     l_textline := FND_MESSAGE.get_string('PO', 'PO_ATO_ITEM_NA');
3464 
3465     INSERT INTO po_online_report_text_gt(
3466        online_report_id,
3467        last_update_login,
3468        last_updated_by,
3469        last_update_date,
3470        created_by,
3471        creation_date,
3472        line_num,
3473        shipment_num,
3474        distribution_num,
3475        sequence,
3476        text_line,
3477        message_name
3478     )
3479    SELECT   p_online_report_id,
3480         p_login_id,
3481         p_user_id,
3482           sysdate,
3483         p_user_id,
3484         sysdate,
3485                     POL.line_num,
3486                     PLL.shipment_num,
3487                     0,
3488                     p_sequence + ROWNUM,
3489                     substr(g_linemsg||g_delim||POL.line_num||g_delim
3490                    ||g_shipmsg||g_delim||PLL.shipment_num||g_delim
3491                    ||l_textline,1,240),
3492            'PO_ATO_ITEM_NA'
3493     FROM   po_lines POL,
3494            po_line_locations_gt PLL,
3495            financials_system_parameters FSP,
3496            mtl_system_items MSI
3497     WHERE  PLL.po_release_id = p_document_id
3498     AND    PLL.po_line_id = POL.po_line_id
3499     AND    POL.item_id is not null
3500     AND    nvl(POL.cancel_flag, 'N') = 'N'                     --Bug5353423
3501     AND    nvl(POL.closed_code, 'OPEN') <> 'FINALLY CLOSED'    --Bug5353423
3502     AND    POL.item_id = MSI.inventory_item_id
3503     AND    MSI.organization_id = FSP.inventory_organization_id
3504     AND    MSI.bom_item_type in (1,2);
3505 
3506     --Increment the p_sequence with number of errors reported in last query
3507     p_sequence := p_sequence + SQL%ROWCOUNT;
3508 --------------------------------------------------------------------
3509 
3510 
3511  /* Start Bug #3512688
3512     To check the validity of the item added at the line level*/
3513       l_progress := '018';
3514       IF g_debug_stmt THEN
3515       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3516         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
3517        || l_progress,'REL 018: Item has to purchasable');
3518       END IF;
3519        END IF;
3520 
3521        -- Check 018: Item has to purchasable
3522 
3523        l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_ALL_NO_ITEM');
3524        INSERT INTO po_online_report_text_gt(online_report_id,
3525        last_update_login,
3526        last_updated_by,
3527        last_update_date,
3528        created_by,
3529        creation_date,
3530        line_num,
3531        shipment_num,
3532        distribution_num,
3533        sequence,
3534        text_line,
3535        message_name)
3536        SELECT  p_online_report_id,
3537        p_login_id,
3538        p_user_id,
3539        sysdate,
3540        p_user_id,
3541        sysdate,
3542        0,
3543        pll.shipment_num,
3544        0,
3545        p_sequence + ROWNUM,
3546         substr(g_shipmsg||g_delim||pll.shipment_num||g_delim||l_textline,1,240),
3547        'PO_ALL_NO_ITEM'
3548        from po_releases_gt por,po_lines pl,po_line_locations_gt pll,mtl_system_items  itm,po_line_types_b plt
3549        where itm.inventory_item_id  = pl.item_id
3550        and   pl.item_id is not null
3551 
3552       and   pl.po_line_id   = pll.po_line_id
3553        and   itm.organization_id    = pll.ship_to_organization_id
3554        and   itm.purchasing_enabled_flag = 'N'
3555        and   pll.po_release_id   = por.po_release_id
3556        and   por.po_release_id = p_document_id
3557        and   pll.po_release_id is not null
3558        and   pl.line_type_id = plt.line_type_id
3559        and   nvl(plt.outside_operation_flag,'N')=nvl(itm.outside_operation_flag,'N')
3560        and   (pll.creation_date >= nvl(por.approved_date,pll.creation_date));
3561 
3562        --Increment the p_sequence with number of errors reported in last query
3563        p_sequence := p_sequence + SQL%ROWCOUNT;
3564      --End Bug #3512688
3565 
3566 l_progress := '019';
3567 IF g_debug_stmt THEN
3568    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3569      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
3570           || l_progress,'Rel 19: Cannot approve documents on hold');
3571    END IF;
3572 END IF;
3573     -- Check 19: Release should not be on hold (Bug 3678912)
3574         -- PO_ON_HOLD_CANNOT_APPROVE
3575 
3576   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_ON_HOLD_CANNOT_APPROVE');
3577   INSERT INTO po_online_report_text_gt(online_report_id,
3578         last_update_login,
3579         last_updated_by,
3580         last_update_date,
3581         created_by,
3582         creation_date,
3583         line_num,
3584         shipment_num,
3585         distribution_num,
3586         sequence,
3587         text_line,
3588                                 message_name)
3589   SELECT  p_online_report_id,
3590         p_login_id,
3591         p_user_id,
3592                   sysdate,
3593         p_user_id,
3594         sysdate,
3595         0,
3596         0,
3597         0,
3598         p_sequence + ROWNUM,
3599         substr(l_textline,1,240),
3600                    'PO_ON_HOLD_CANNOT_APPROVE'
3601     FROM  PO_RELEASES_GT POR
3602     WHERE  POR.po_release_id = p_document_id
3603     AND    nvl(POR.HOLD_FLAG,'N') = 'Y';
3604 
3605     --Increment the p_sequence with number of errors reported in last query
3606     p_sequence := p_sequence + SQL%ROWCOUNT;
3607 
3608     l_progress := '020';
3609 
3610 ---------------------------------------------------------------------------------
3611 -- bug 6530879 Releases <Start>
3612 select poh.po_header_id,poh.vendor_id, poh.vendor_site_id, poh.vendor_contact_id
3613   into l_agreement_id, l_vendor_id,l_vendor_site_id,l_vendor_contact_id
3614   from po_headers_gt poh, po_releases_gt por
3615   where poh.po_header_id = por.po_header_id
3616     and por.po_release_id = p_document_id;
3617 -- bug 6530879
3618 -- Check 20: Vendor should be valid when approving the document.
3619 -- Important for reapproval, to avoid the case when the vendor has
3620 -- been invalidated by first successful approval.
3621 
3622 l_textline :=  FND_MESSAGE.GET_STRING('PO', 'PO_PDOI_INVALID_VENDOR');
3623 ---------------------------------------------------------------------------
3624   if (l_vendor_id is NOT NULL) then
3625 	fnd_message.set_name('PO', 'PO_PDOI_INVALID_VENDOR');
3626         fnd_message.set_token('VALUE', to_char(l_vendor_id), FALSE);
3627 	l_textline := substr(fnd_message.get, 1, 240);
3628 
3629 
3630  	INSERT INTO po_online_report_text_gt(online_report_id,
3631  				last_update_login,
3632  				last_updated_by,
3633  				last_update_date,
3634  				created_by,
3635  				creation_date,
3636  				line_num,
3637  				shipment_num,
3638  				distribution_num,
3639  				sequence,
3640  				text_line,
3641                                 message_name)
3642  	SELECT 	p_online_report_id,
3643  		    p_login_id,
3644  		    p_user_id,
3645      	            sysdate,
3646  		    p_user_id,
3647  		    sysdate,
3648  		    0,
3649  		    0,
3650  		    0,
3651  		    p_sequence + ROWNUM,
3652  		    substr(l_textline,1,240),
3653                    'PO_PDOI_INVALID_VENDOR'
3654 	FROM  dual
3655     where not exists (select 'Y'
3656 			from PO_HEADERS_GT POH, po_vendors pov
3657 		       WHERE  POH.po_header_id = l_agreement_id
3658 		         AND  pov.vendor_id = poh.vendor_id
3659 			 AND  pov.enabled_flag = 'Y'
3660 			 AND  SYSDATE BETWEEN nvl(pov.start_date_active, SYSDATE-1)
3661                                   AND nvl(pov.end_date_active, SYSDATE+1));
3662  --Increment the p_sequence with number of errors reported in last query
3663         p_sequence := p_sequence + SQL%ROWCOUNT;
3664 
3665  end if;
3666 
3667 -------------------------------------------------------------------------------------
3668 -- Check 21: check the validity of the vendor site.
3669  if (l_vendor_site_id is not null) then
3670 
3671   	fnd_message.set_name('PO', 'PO_PDOI_INVALID_VENDOR_SITE');
3672  	fnd_message.set_token('VALUE', to_char(l_vendor_site_id), FALSE);
3673 	l_textline := substr(fnd_message.get, 1, 240);
3674 
3675  	INSERT INTO po_online_report_text_gt(online_report_id,
3676  				last_update_login,
3677  				last_updated_by,
3678  				last_update_date,
3679  				created_by,
3680  				creation_date,
3681  				line_num,
3682  				shipment_num,
3683  				distribution_num,
3684  				sequence,
3685  				text_line,
3686                                 message_name)
3687  	SELECT 	p_online_report_id,
3688  		    p_login_id,
3689  		    p_user_id,
3690      	            sysdate,
3691  		    p_user_id,
3692  		    sysdate,
3693  		    0,
3694  		    0,
3695  		    0,
3696  		    p_sequence + ROWNUM,
3697  		    substr(l_textline,1,240),
3698                    'PO_PDOI_INVALID_VENDOR_SITE'
3699     FROM  dual
3700     where not exists (select 'Y'
3701 			from PO_HEADERS_GT POH, po_vendor_sites povs
3702 		        WHERE  POH.po_header_id = l_agreement_id
3703 		        AND  povs.vendor_site_id = poh.vendor_site_id
3704 			AND    nvl(povs.rfq_only_site_flag,'N') <> 'Y'
3705 			AND    povs.purchasing_site_flag = 'Y'
3706 		        AND    SYSDATE < nvl(povs.inactive_date, SYSDATE + 1));
3707  --Increment the p_sequence with number of errors reported in last query
3708         p_sequence := p_sequence + SQL%ROWCOUNT;
3709 end if;
3710 
3711 -------------------------------------------------------------------------------------
3712 -- check 22: validate vendor contact
3713 if (l_vendor_contact_id is not null) then
3714 
3715 	fnd_message.set_name('PO', 'PO_PDOI_INVALID_VDR_CNTCT');
3716  	fnd_message.set_token('VALUE', to_char(l_vendor_contact_id), FALSE);
3717 	l_textline := substr(fnd_message.get, 1, 240);
3718 
3719 	INSERT INTO po_online_report_text_gt(online_report_id,
3720  				last_update_login,
3721  				last_updated_by,
3722  				last_update_date,
3723  				created_by,
3724  				creation_date,
3725  				line_num,
3726  				shipment_num,
3727  				distribution_num,
3728  				sequence,
3729  				text_line,
3730                                 message_name)
3731  	SELECT 	p_online_report_id,
3732  		    p_login_id,
3733  		    p_user_id,
3734      	            sysdate,
3735  		    p_user_id,
3736  		    sysdate,
3737  		    0,
3738  		    0,
3739  		    0,
3740  		    p_sequence + ROWNUM,
3741  		    substr(l_textline,1,240),
3742                    'PO_PDOI_INVALID_VDR_CNTCT'
3743 	FROM  dual
3744 	WHERE NOT EXISTS (SELECT  'Y'
3745 			    FROM   PO_VENDOR_CONTACTS pvc, po_headers_gt poh
3746   			   WHERE    POH.po_header_id = l_agreement_id
3747 			     AND    pvc.vendor_contact_id =poh.vendor_contact_id
3748 			     AND    SYSDATE < nvl(pvc.inactive_date, SYSDATE+1));
3749 
3750    --Increment the p_sequence with number of errors reported in last query
3751         p_sequence := p_sequence + SQL%ROWCOUNT;
3752 end if;
3753 
3754 -- bug 6530879 Releases <END>
3755 
3756 
3757 --<R12 eTax Integration Start>
3758 -----------------------------------------------------------------------------
3759 
3760     l_tax_status := po_tax_interface_pvt.calculate_tax_yes_no(p_po_header_id  => NULL,
3761                                                               p_po_release_id => p_document_id,
3762                                                               p_req_header_id => NULL);
3763     l_progress := '021';
3764     IF g_debug_stmt THEN
3765         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3766           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head ||l_api_name||'.'
3767                         || l_progress, 'Rel 21: Recalculate tax before approval = ' || l_tax_status);
3768         END IF;
3769     END IF;
3770 
3771     IF l_tax_status = 'Y' THEN
3772       IF g_debug_stmt THEN
3773           IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3774             FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
3775                          l_api_name||'.' || l_progress,
3776                          'Rel 22: Calculate tax as the current one is not correct');
3777           END IF;
3778       END IF;
3779       l_progress := '021';
3780       po_tax_interface_pvt.calculate_tax( x_return_status    => l_return_status,
3781                                           p_po_header_id     => NULL,
3782                                           p_po_release_id    => p_document_id,
3783                                           p_calling_program  => g_action_DOC_SUBMISSION_CHECK);
3784       l_progress := '022';
3785       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3786          IF g_debug_stmt THEN
3787              IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3788                FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
3789                             l_api_name||'.' || l_progress,
3790                             'Rel 23: Calculate tax has errored out');
3791              END IF;
3792          END IF;
3793          l_progress := '023';
3794 
3795          l_tax_message := fnd_message.get_string('PO','PO_TAX_CALCULATION')||' : ' ;
3796 
3797          FORALL i IN 1..po_tax_interface_pvt.G_TAX_ERRORS_TBL.MESSAGE_TEXT.COUNT
3798            INSERT INTO po_online_report_text_gt
3799            (
3800             online_report_id,
3801             last_update_login,
3802             last_updated_by,
3803             last_update_date,
3804             created_by,
3805             creation_date,
3806             line_num,
3807             shipment_num,
3808             distribution_num,
3809             sequence,
3810             text_line,
3811             message_name,
3812             message_type
3813            )
3814            VALUES
3815            (
3816              p_online_report_id,
3817              p_login_id,
3818              p_user_id,
3819              sysdate,
3820              p_user_id,
3821              sysdate,
3822              po_tax_interface_pvt.G_TAX_ERRORS_TBL.line_num(i),
3823              po_tax_interface_pvt.G_TAX_ERRORS_TBL.shipment_num(i),
3824              po_tax_interface_pvt.G_TAX_ERRORS_TBL.distribution_num(i),
3825              p_sequence + ROWNUM,
3826              l_tax_message || po_tax_interface_pvt.G_TAX_ERRORS_TBL.message_text(i),
3827              'PO_TAX_CALCULATION_FAILED',
3828              'E'
3829            );
3830          l_progress := '024';
3831       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3832         IF g_debug_stmt THEN
3833            IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3834                FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
3835                             l_api_name||'.' || l_progress,
3836                             'Rel 24: Calculate tax raised unexpected error');
3837            END IF;
3838         END IF;
3839         l_textline := l_progress ||' - ';
3840         IF po_tax_interface_pvt.G_TAX_ERRORS_TBL.MESSAGE_TEXT.COUNT > 0 THEN
3841           l_textline := l_textline || po_tax_interface_pvt.G_TAX_ERRORS_TBL.MESSAGE_TEXT(1);
3842         ELSE
3843           l_textline := l_textline || SQLERRM();
3844         END IF;
3845         fnd_message.set_name('PO','PO_TAX_CALC_UNEXPECTED_ERROR');
3846         fnd_message.set_token('ERROR',l_textline);
3847         FND_MSG_PUB.Add;
3848         l_progress := '025';
3849         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3850       END IF;
3851     END IF;
3852 
3853     p_sequence := p_sequence + SQL%ROWCOUNT;
3854 
3855 -----------------------------------------------------------------------------
3856 
3857     l_progress := '026';
3858 --<R12 eTax Integration End>
3859 
3860 IF g_debug_stmt THEN
3861     	IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3862         	FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
3863          		       || l_progress,'Rel 25: LCM enabled release shipment should have invoice match option as receipt');
3864    	END IF;
3865     END IF;
3866     -- Check 25: Validation for LCM enabled release to check whether its shipment has invoice match option as 'Receipt'
3867     l_textline := FND_MESSAGE.GET_STRING('PO','PO_SUB_REL_SHIP_INV_MATCH_NE_R');
3868     INSERT INTO po_online_report_text_gt (online_report_id,
3869 	 				  last_update_login,
3870 	 				  last_updated_by,
3871 	 				  last_update_date,
3872 	 				  created_by,
3873 	 				  creation_date,
3874 	 				  line_num,
3875 	 				  shipment_num,
3876 	 				  distribution_num,
3877 	 				  sequence,
3878 	 				  text_line,
3879 	                                  message_name)
3880     SELECT p_online_report_id,
3881 	   p_login_id,
3882 	   p_user_id,
3883 	   sysdate,
3884 	   p_user_id,
3885 	   sysdate,
3886 	   0,
3887 	   PLL.shipment_num,
3888            0,
3889 	   p_sequence + ROWNUM,
3890 	   substr (g_shipmsg||g_delim||PLL.shipment_num||g_delim||l_textline,1,240),
3891 	   'PO_SUB_REL_SHIP_INV_MATCH_NE_R'
3892       FROM PO_RELEASES_GT POR,
3893            PO_LINE_LOCATIONS_GT PLL
3894      WHERE POR.po_release_id = PLL.po_release_id
3895        AND POR.po_release_id = p_document_id
3896        AND Nvl(PLL.LCM_FLAG,'N') = 'Y'
3897        AND Nvl(PLL.match_option,'P') <> 'R';
3898 
3899     --Increment the p_sequence with number of errors reported in last query
3900     p_sequence := p_sequence + SQL%ROWCOUNT;
3901 
3902     -------------------------------------------------------------------------------------
3903     l_progress := '027';
3904     IF g_debug_stmt THEN
3905     	IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3906         	FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
3907          		       || l_progress,'Rel 26: LCM enabled release distribution should have destination type as Inventory');
3908    	END IF;
3909     END IF;
3910     -- Check 26: Validation for LCM enabled release to check whether its distribution has destination type as 'Inventory'
3911     l_textline := FND_MESSAGE.GET_STRING('PO','PO_SUB_REL_DIST_DEST_TYPE_NE_I');
3912     INSERT INTO po_online_report_text_gt (online_report_id,
3913 	 				  last_update_login,
3914 	 				  last_updated_by,
3915 	 				  last_update_date,
3916 	 				  created_by,
3917 	 				  creation_date,
3918 	 				  line_num,
3919 	 				  shipment_num,
3920 	 				  distribution_num,
3921 	 				  sequence,
3922 	 				  text_line,
3923 	                                  message_name)
3924     SELECT p_online_report_id,
3925 	   p_login_id,
3926 	   p_user_id,
3927 	   sysdate,
3928 	   p_user_id,
3929 	   sysdate,
3930 	   0,
3931 	   PLL.shipment_num,
3932 	   POD.distribution_num,
3933 	   p_sequence + ROWNUM,
3934 	   substr (g_shipmsg||g_delim||PLL.shipment_num||g_delim||g_distmsg||g_delim||
3935                    POD.distribution_num||g_delim||l_textline, 1,240),
3936 	   'PO_SUB_REL_DIST_DEST_TYPE_NE_I'
3937       FROM PO_RELEASES_GT POR,
3938            PO_LINE_LOCATIONS_GT PLL,
3939            PO_DISTRIBUTIONS_GT POD
3940      WHERE POR.po_release_id = POD.po_release_id
3941        AND POD.line_location_id = PLL.line_location_id
3942        AND POR.po_release_id = p_document_id
3943        AND Nvl(POD.LCM_FLAG,'N') = 'Y'
3944        AND POD.DESTINATION_TYPE_CODE <> 'INVENTORY';
3945 
3946     --Increment the p_sequence with number of errors reported in last query
3947     p_sequence := p_sequence + SQL%ROWCOUNT;
3948     ----------------------------------------------------------------------------------------
3949 
3950     x_return_status := FND_API.G_RET_STS_SUCCESS;
3951 EXCEPTION
3952     WHEN FND_API.G_EXC_ERROR THEN
3953         x_return_status := FND_API.G_RET_STS_ERROR;
3954 
3955     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3956         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3957 
3958     WHEN OTHERS THEN
3959 
3960         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3961         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
3962             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
3963         END IF;
3964 
3965         IF (g_debug_unexp) THEN
3966                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
3967                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
3968                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
3969                        || l_progress || ' SQL CODE is '||sqlcode);
3970                 END IF;
3971         END IF;
3972 END CHECK_RELEASES;
3973 
3974 --For RELEASES, PO
3975 /**
3976 * Private Procedure: CHECK_PO_REL_REQPRICE
3977 * Requires:
3978 *   IN PARAMETERS:
3979 *       p_document_id:      The requisition_header_id of submitted document
3980 *       p_online_report_id: Id used to INSERT INTO online_report_text table
3981 *       p_user_id:          User performing the action
3982 *       p_login_id:         Last update login_id
3983 *   IN OUT PARAMETERS
3984 *       p_sequence:          Sequence number of last reported error
3985 * Modifies: Inserts error msgs in online_report_text_gt table, uses
3986 *           global_temp tables for processing
3987 * Effects:  This procedure runs the document submission checks for PO and
3988 *           RELEASES. This procedure compares the price of the PO or Release
3989 *           Shipment to the price of the Requisition Line. The Shipment
3990 *           Price should be within the tolerance of the Requisition Line
3991 * Returns:
3992 *  p_sequence: This parameter contains the current count of number of error
3993 *              messages inserted
3994 */
3995 PROCEDURE check_po_rel_reqprice(p_document_type IN VARCHAR2,
3996                        p_document_id IN NUMBER,
3997                        p_online_report_id IN NUMBER,
3998                        p_user_id IN NUMBER,
3999                        p_login_id IN NUMBER,
4000                        p_sequence IN OUT NOCOPY NUMBER,
4001                        x_return_status OUT NOCOPY VARCHAR2) IS
4002 
4003 l_textline  po_online_report_text.text_line%TYPE := NULL;
4004 l_api_name  CONSTANT varchar2(40) := 'CHECK_PO_REL_REQPRICE';
4005 l_progress VARCHAR2(3);
4006 
4007 l_enforce_price_tolerance po_system_parameters.enforce_price_change_allowance%TYPE;
4008 l_enforce_price_amount  po_system_parameters.enforce_price_change_amount%TYPE;
4009 l_amount_tolerance po_system_parameters.price_change_amount%TYPE;
4010 
4011 TYPE unit_of_measure IS TABLE of PO_LINES.unit_meas_lookup_code%TYPE;
4012 TYPE NumTab IS TABLE of NUMBER;
4013 l_ship_price_in_base_curr NumTab;
4014 l_ship_unit_of_measure unit_of_measure;
4015 l_ship_num NumTab;
4016 l_line_num NumTab;
4017 l_quantity NumTab;
4018 l_item_id NumTab;
4019 l_line_location_id NumTab;
4020 
4021 --For Req Cursor
4022 l_req_unit_of_measure unit_of_measure;
4023 l_req_line_unit_price NumTab;
4024 l_po_req_line_num NumTab;
4025 l_po_req_ship_num NumTab;
4026 l_po_req_quantity NumTab;
4027 
4028 l_ship_price_ext_precn NUMBER;
4029 l_shipment_to_req_rate NUMBER := 0;
4030 l_price_tolerance_allowed NUMBER := 0;
4031 
4032 --<Bug 3266272 mbhargav START>
4033 l_pou_func_curr                FND_CURRENCIES.currency_code%TYPE;
4034 l_pou_func_curr_ext_precn      FND_CURRENCIES.extended_precision%TYPE;
4035 l_po_curr                      FND_CURRENCIES.currency_code%TYPE;
4036 l_req_ou_func_curr             FND_CURRENCIES.currency_code%TYPE;
4037 l_rate_date                    DATE;
4038 l_rate                         NUMBER;
4039 l_rate_type                    PO_SYSTEM_PARAMETERS_ALL.default_rate_type%TYPE;
4040 l_requesting_org_id            NumTab;
4041 l_display_rate                 NUMBER;
4042 l_return_status                VARCHAR2(1);
4043 l_error_message_name           FND_NEW_MESSAGES.message_name%TYPE;
4044 l_req_line_price_pou_base_curr PO_REQUISITION_LINES_ALL.unit_price%TYPE;
4045 l_req_line_price_ext_precn     PO_REQUISITION_LINES_ALL.unit_price%TYPE;
4046 --<Bug 3266272 mbhargav END>
4047 
4048 l_is_complex_po BOOLEAN := FALSE; --<Complex Work R12>
4049 
4050 /*
4051 ** Setup the PO select cursor
4052 ** Select shipment price and convert it to base currency.
4053 ** this is done by taking the distribution rate and applying
4054 ** it evenly over all distributions.  Additionally get the
4055 ** shipment unit of measure, quantity, and item_id to be
4056 ** passed to the UomC function.
4057 */
4058 
4059 /*Bug4302950 :The shipments were updated with wrong price when supplier submits
4060 change request from ISP to split shipment quantity between two individual shipments.
4061 As the ISP doesnot handle change in distributions,replacing the shipments
4062 quantity with the sum of the distributions quantity in the calculation
4063 of price override so that the shipment price will reflect the correct value.*/
4064 
4065 CURSOR po_shipment_cursor (p_document_id NUMBER) IS
4066     SELECT nvl(max(POLL.price_override ) *
4067         sum(decode(plt.order_type_lookup_code,'AMOUNT',1,nvl(POD.rate,1))*
4068                   (POD.quantity_ordered -
4069                    nvl(POD.quantity_cancelled, 0))) /
4070             /*	 (max(POLL.quantity) -
4071 		 nvl(max(POLL.quantity_cancelled),0)), -1) Price, */ --Bug4302950
4072                 (sum(POD.quantity_ordered -
4073                  nvl(POD.quantity_cancelled,0))), -1) Price,
4074         POL.unit_meas_lookup_code uom,
4075         nvl(POLL.shipment_num,0) ship_num,
4076         nvl(POL.line_num,0) line_num,
4077         nvl(POLL.quantity,0) quantity,
4078         nvl(POL.item_id,-1) item_id,
4079         nvl( POLL.line_location_id,0) line_loc_id
4080     FROM   PO_LINE_LOCATIONS_GT POLL,
4081         PO_LINE_TYPES_B PLT,            -- bug3413891
4082         PO_LINES_GT POL,
4083         PO_DISTRIBUTIONS_GT POD
4084      WHERE  POLL.po_line_id    = POL.po_line_id
4085      AND    POLL.line_location_id = POD.line_location_id
4086      AND    POLL.po_header_id = p_document_id
4087      AND    POL.line_type_id = PLT.line_type_id
4088      AND    nvl(POLL.cancel_flag,'N') <> 'Y'
4089      AND    nvl(POLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
4090      AND    POLL.shipment_type in ('PLANNED', 'STANDARD')
4091      GROUP BY POL.unit_meas_lookup_code, nvl(POLL.shipment_num,0),
4092               nvl(POL.line_num,0), nvl(POLL.quantity,0),
4093               nvl(POL.item_id,-1), POLL.price_override,
4094               nvl(POLL.line_location_id,0);
4095 
4096 
4097 -- <Complex Work R12 START>
4098 /* Setup the Complex Work PO select cursor
4099 ** Select Complex Work PO Line information and the
4100 ** line loc ID for the first STANDARD pay item for
4101 ** that line
4102 */
4103 CURSOR po_pay_item_cursor (p_document_id NUMBER) IS
4104     SELECT (POL.unit_price
4105               *  (sum(POD.rate
4106                    * (POD.quantity_ordered - nvl(POD.quantity_cancelled,0)) ))
4107               / POLL.quantity - nvl(POLL.quantity_cancelled, 0)
4108             ) price,
4109         POL.unit_meas_lookup_code uom,
4110         POLL.shipment_num ship_num,
4111         POL.line_num line_num,
4112         POL.quantity quantity,
4113         nvl(POL.item_id,-1) item_id,
4114         POLL.line_location_id line_loc_id
4115     FROM   PO_LINE_LOCATIONS_GT POLL,
4116         PO_LINES_GT POL,
4117         PO_DISTRIBUTIONS_GT POD
4118     WHERE POL.po_header_id = p_document_id
4119     AND   POD.line_location_id = POLL.line_location_id
4120     AND   POLL.line_location_id =
4121            (SELECT min(POLL2.line_location_id)
4122             FROM PO_LINE_LOCATIONS_GT POLL2
4123             WHERE POLL2.po_line_id = POL.po_line_id
4124             AND POLL2.shipment_type = 'STANDARD'
4125            )
4126     GROUP BY POL.unit_price, POLL.quantity, nvl(POLL.quantity_cancelled, 0),
4127              POL.unit_meas_lookup_code,
4128              POLL.shipment_num,
4129              POL.line_num,
4130              POL.quantity,
4131              nvl(POL.item_id,-1),
4132              POLL.line_location_id;
4133 -- <Complex Work R12 END>
4134 
4135 
4136 /*
4137 ** Setup the Release select cursor
4138 ** Select shipment price and convert it to base currency.
4139 ** this is done by taking the distribution rate and applying
4140 ** it evenly over all distributions.  Additionally get the
4141 ** shipment unit of measure, quantity, and item_id to be
4142 ** passed to the UomC function.  Get the shipment_num and
4143 ** line_num to be passed to the pooinsingle function.
4144 */
4145 CURSOR rel_shipment_cursor (p_document_id NUMBER) IS
4146     SELECT /*+ FULL(POLL) */                          -- bug3413891
4147         nvl(max(POLL.price_override) *
4148         sum(decode(plt.order_type_lookup_code,'AMOUNT',1,nvl(POD.rate,1))*
4149                   (POD.quantity_ordered -
4150                    nvl(POD.quantity_cancelled, 0))) /
4151             /*	 (max(POLL.quantity) -
4152 		 nvl(max(POLL.quantity_cancelled),0)), -1) Price, */ --Bug4302950
4153                 (sum(POD.quantity_ordered -
4154                  nvl(POD.quantity_cancelled,0))), -1) Price,
4155         POL.unit_meas_lookup_code uom,
4156         nvl(POLL.shipment_num,0) ship_num,
4157         nvl(POL.line_num,0) line_num,
4158         nvl(POLL.quantity,0) quantity,
4159         nvl(POL.item_id,0) item_id,
4160         nvl( POLL.line_location_id,0) line_loc_id
4161     FROM   PO_LINE_LOCATIONS_GT POLL,
4162         PO_LINE_TYPES_B PLT,                          -- bug3413891
4163         PO_LINES POL,
4164         PO_DISTRIBUTIONS_GT POD                       -- <PO_CHANGE_API FPJ>
4165     WHERE  POLL.po_line_id    = POL.po_line_id
4166      AND    POLL.line_location_id = POD.line_location_id
4167      AND    POLL.po_release_id = p_document_id
4168      AND    POL.line_type_id = PLT.line_type_id
4169      AND    nvl(POLL.cancel_flag,'N') <> 'Y'
4170      AND    nvl(POLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
4171     GROUP BY POL.unit_meas_lookup_code, nvl(POLL.shipment_num,0),
4172               nvl(POL.line_num,0), nvl(POLL.quantity,0),
4173               nvl(POL.item_id,0), POLL.price_override,
4174               nvl(POLL.line_location_id,0);
4175 
4176  CURSOR req_price_tol_cursor(p_line_location_id  NUMBER) IS
4177          SELECT min(PRL.unit_price),
4178                 PRL.unit_meas_lookup_code,
4179                 min(POL.line_num),
4180                 min(POLL.shipment_num),
4181                 min(PRL.org_id)               --<Bug 3266272>
4182          FROM   PO_REQUISITION_LINES_ALL PRL, --<Bug 3266272>
4183                 PO_LINE_LOCATIONS_GT POLL,
4184                 PO_LINES          POL
4185          WHERE  PRL.line_location_id  = POLL.line_location_id
4186          AND    POLL.line_location_id = p_line_location_id
4187          AND    PRL.unit_price        >= 0
4188          AND    POLL.po_line_id       = POL.po_line_id
4189          GROUP BY PRL.unit_meas_LOOKUP_code;
4190 
4191 CURSOR req_price_amt_cursor(p_line_location_id  NUMBER) IS
4192          SELECT min(PRL.unit_price),
4193                  PRL.unit_meas_lookup_code,
4194                  sum(PD.quantity_ordered),
4195                  min(POL.line_num),
4196                  min(POLL.shipment_num),
4197                  min(PRL.org_id)              --<Bug 3266272>
4198          FROM   PO_REQUISITION_LINES_ALL PRL, --<Bug 3266272>
4199                  PO_LINE_LOCATIONS_GT POLL,
4200                  PO_LINES          POL,
4201                  PO_DISTRIBUTIONS  PD,
4202                  PO_REQ_DISTRIBUTIONS_ALL PRD --<Bug 3266272>
4203          WHERE  POLL.line_location_id = p_line_location_id
4204           AND    POLL.po_line_id = POL.po_line_id
4205           AND    PRL.unit_price >= 0
4206           AND    POLL.line_location_id = PD.line_location_id
4207           AND    PD.req_distribution_id = PRD.distribution_id
4208           AND    PRD.requisition_line_id = PRL.requisition_line_id
4209          GROUP BY PRL.requisition_line_id, PRL.unit_meas_lookup_code;
4210 
4211 BEGIN
4212 l_progress := '000';
4213 IF g_debug_stmt THEN
4214    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4215      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name||'.'
4216           || l_progress,'PO REQ: Price, Amount Toleance check');
4217    END IF;
4218 END IF;
4219 
4220     --check if this check is enforced
4221     SELECT nvl(enforce_price_change_allowance, 'N'),
4222                     nvl(enforce_price_change_amount, 'N'),
4223                     nvl(price_change_amount, -1)
4224     INTO   l_enforce_price_tolerance,
4225            l_enforce_price_amount,
4226            l_amount_tolerance
4227     FROM   po_system_parameters;
4228 
4229 l_progress := '001';
4230 IF g_debug_stmt THEN
4231    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4232      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name||'.'
4233        || l_progress,'Is price tol check enforced '||l_enforce_price_tolerance
4234        || ' Is price amount check enforced ' || l_enforce_price_amount);
4235    END IF;
4236 END IF;
4237 
4238     --if we are not enforcing the price tolerance checks then return success
4239     IF  l_enforce_price_tolerance = 'N' AND l_enforce_price_amount = 'N' THEN
4240         RETURN;
4241     END IF;
4242 
4243      /*Depending on the document type execute a different sql
4244           statement to fetch all of the line_location_ids, prices, and
4245           uom_code for a particular document_id and document type.
4246         Use above information to determine if the po shipment
4247           price is within the requisition price + tolerance.
4248     */
4249     IF p_document_type = 'PO' THEN
4250 l_progress := '002';
4251 
4252   --<Complex Work R12>: use a different cursor for Complex Work
4253         l_is_complex_po := PO_COMPLEX_WORK_PVT.is_complex_work_po(p_document_id);
4254 
4255         IF (l_is_complex_po) THEN
4256           --The PO is a Complex Work PO
4257           --Use the Pay Item cursor
4258 
4259           OPEN po_pay_item_cursor(p_document_id);
4260 
4261           FETCH po_pay_item_cursor BULK COLLECT INTO
4262                 l_ship_price_in_base_curr,
4263                 l_ship_unit_of_measure,
4264                 l_ship_num,
4265                 l_line_num,
4266                 l_quantity,
4267                 l_item_id,
4268                 l_line_location_id;
4269 
4270           CLOSE po_pay_item_cursor;
4271 
4272         ELSE
4273           --The PO is not Complex Work.
4274           --Use the Shipment cursor
4275 
4276           OPEN po_shipment_cursor(p_document_id);
4277 
4278           FETCH po_shipment_cursor BULK COLLECT INTO
4279                 l_ship_price_in_base_curr,
4280                 l_ship_unit_of_measure,
4281                 l_ship_num,
4282                 l_line_num,
4283                 l_quantity,
4284                 l_item_id,
4285                 l_line_location_id;
4286 
4287           CLOSE po_shipment_cursor;
4288 
4289         END IF; -- l_is_complex_po check
4290 
4291     ELSIF p_document_type = 'RELEASE' THEN
4292 l_progress := '003';
4293         OPEN rel_shipment_cursor(p_document_id);
4294 
4295         FETCH rel_shipment_cursor BULK COLLECT INTO
4296                 l_ship_price_in_base_curr,
4297                 l_ship_unit_of_measure,
4298                 l_ship_num,
4299                 l_line_num,
4300                 l_quantity,
4301                 l_item_id,
4302                 l_line_location_id;
4303 
4304         CLOSE rel_shipment_cursor;
4305     END IF;
4306 
4307     --<Bug 3266272 mbhargav START>
4308     --Bug 1991546
4309     --Obtain extended precision of PO/Release functional currency which is
4310     --used for rounding while checking for tolerance
4311       BEGIN
4312         SELECT  FND.currency_code, nvl(FND.extended_precision,5)
4313         INTO  l_pou_func_curr, l_pou_func_curr_ext_precn
4314         FROM  fnd_currencies FND, financials_system_parameters FSP,
4315               gl_sets_of_books SOB
4316         WHERE  FSP.set_of_books_id = SOB.set_of_books_id
4317          AND  SOB.currency_code = FND.currency_code;
4318       EXCEPTION
4319         WHEN OTHERS THEN
4320             RAISE;
4321       END;
4322       --<Bug 3266272 mbhargav END>
4323 
4324 
4325 l_progress := '004';
4326     FOR shipment_line IN 1..l_line_location_id.COUNT LOOP
4327 
4328        --<Bug 3266272 mbhargav START>
4329        --Round off the shipment price (in functional currency of Purchasing
4330        --Operating Unit) to the extended precision of the functional currency
4331        l_ship_price_ext_precn :=
4332           round(l_ship_price_in_base_curr(shipment_line),l_pou_func_curr_ext_precn);
4333 
4334       --Obtain the currency and rate_date from PO. If a rate_date exists on PO
4335       --Header then it means PO Currency is different from POU functional
4336       --currency. In this case take the rate_date on PO Header as rate date.
4337       --If rate_date on PO Header is NULL then it means PO Currency is same as
4338       --POU functional currency. Use the Shipment creation date as rate_date for
4339       --such cases. This l_rate_date will be used to get rate between POU
4340       --functional currency and ROU functional currency for currency conversions
4341       BEGIN
4342         SELECT POH.currency_code, nvl(trunc(POH.rate_date), trunc(POLL.creation_date))
4343         INTO l_po_curr, l_rate_date
4344         FROM PO_HEADERS POH, PO_LINE_LOCATIONS_GT POLL
4345         WHERE POLL.line_location_id = l_line_location_id(shipment_line)
4346          AND POLL.po_header_id = POH.po_header_id;
4347       EXCEPTION
4348         WHEN OTHERS THEN
4349              RAISE;
4350       END;
4351       --<Bug 3266272 mbhargav END>
4352 
4353 l_progress := '005';
4354         --Do price tolerance check
4355         IF l_enforce_price_tolerance = 'Y' THEN
4356 
4357 IF g_debug_stmt THEN
4358    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4359      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name||'.'
4360           || l_progress,'Doing Price Tolerance check');
4361    END IF;
4362 END IF;
4363             OPEN req_price_tol_cursor(l_line_location_id(shipment_line));
4364 
4365             FETCH req_price_tol_cursor BULK COLLECT INTO
4366                     l_req_line_unit_price,
4367                     l_req_unit_of_measure,
4368                     l_po_req_line_num,
4369                     l_po_req_ship_num,
4370                     l_requesting_org_id; --Bug 3266272
4371 
4372             CLOSE req_price_tol_cursor;
4373 
4374 l_progress := '006';
4375             FOR req_line IN 1..l_req_line_unit_price.COUNT LOOP
4376 
4377           /*
4378            ** If a row was returned then the PO or Release is associated
4379            ** with a requisition and you should continue with the logic.
4380            ** If a row was not returned.  It does not mean that an error
4381            ** occurred, it meas that the submission check does not apply
4382            ** to this document.
4383            */
4384                 --<Bug 3266272 mbhargav START>
4385                 /* Bug 4537974: while comparing org_id we need an NVL condition
4386 		   around both the operands of = since for single org installations
4387 		   the org_id can be null */
4388 
4389                 IF l_req_ou_func_curr IS NULL THEN
4390                   BEGIN
4391                     SELECT  SOB.currency_code
4392                     INTO  l_req_ou_func_curr
4393                     FROM  financials_system_params_all FSP, gl_sets_of_books SOB
4394                     WHERE  FSP.set_of_books_id = SOB.set_of_books_id
4395                       AND  NVL(FSP.org_id, -99) = NVL(l_requesting_org_id(req_line),-99);
4396                   EXCEPTION
4397                     WHEN OTHERS THEN
4398                        RAISE;
4399                   END;
4400                 END IF;
4401 
4402                 IF l_req_ou_func_curr <> l_pou_func_curr THEN
4403 
4404                    --Obtain the conversion rate between two functional currencies
4405                    --using the rate type from POU setup.
4406                    IF l_rate IS NULL THEN
4407                       BEGIN
4408                         SELECT default_rate_type
4409                         INTO   l_rate_type
4410                         FROM   po_system_parameters;
4411                       EXCEPTION
4412                         WHEN OTHERS THEN
4413                           RAISE;
4414                       END;
4415 
4416                       --Get the conversion rate between Purchasing Operating Unit func
4417                       --currency and Req Operating Unit functional currency
4418                       po_currency_sv.get_rate(
4419                               p_from_currency => l_req_ou_func_curr,
4420                               p_to_currency   => l_pou_func_curr,
4421                               p_rate_type     => l_rate_type,
4422                               p_rate_date     => l_rate_date,
4423                               p_inverse_rate_display_flag => 'N',
4424                               x_rate          => l_rate,
4425                               x_display_rate  => l_display_rate,
4426                               x_return_status => l_return_status,
4427                               x_error_message_name => l_error_message_name);
4428                    END IF; --rate check
4429 
4430                    --Convert the Req line price (which is in Req OU func currency)
4431                    --to Purchasing OU functional currency for comparison
4432                    l_req_line_price_pou_base_curr :=
4433                            l_req_line_unit_price(req_line) * nvl(l_rate,1);
4434 
4435 IF g_debug_stmt THEN
4436    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4437      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name||'.'
4438        || l_progress,'POU Func Currency and ROU Func Curr are different'
4439        || ' l_pou_func_curr= '||l_pou_func_curr
4440        || ' l_req_ou_func_curr= ' || l_req_ou_func_curr
4441        || ' l_req_line_unit_price= ' || l_req_line_unit_price(req_line)
4442        || ' l_rate_type= ' ||  l_rate_type
4443        || ' l_rate_date= ' || l_rate_date
4444        || ' l_rate= ' || l_rate
4445        || ' l_req_line_price_pou_base_curr= ' ||l_req_line_price_pou_base_curr);
4446    END IF;
4447 END IF;
4448                    --Round off the Req line price (in functional currency of
4449                    --Purchasing Operating Unit) to the extended precision of
4450                    --the functional currency
4451                    l_req_line_price_ext_precn :=
4452                        round(l_req_line_price_pou_base_curr,l_pou_func_curr_ext_precn);
4453                 ELSE  --POU func curr <> ROU func curr
4454                    l_req_line_price_ext_precn :=
4455                       round(l_req_line_unit_price(req_line),l_pou_func_curr_ext_precn);
4456                 END IF; --func curr check
4457                 --<Bug 3266272 mbhargav END>
4458 
4459                 --Call function that returns the shipment price
4460                 --converted to the correct UOM.
4461                 po_uom_s.po_uom_conversion(
4462                     l_ship_unit_of_measure(shipment_line),
4463                     l_req_unit_of_measure(req_line),
4464                     l_item_id(shipment_line),
4465                     l_shipment_to_req_rate);
4466 
4467                 IF l_shipment_to_req_rate = 0.0 THEN
4468                     l_shipment_to_req_rate :=1.0;
4469                 END IF;
4470 l_progress := '007';
4471                 --Get the tolerance allowed.  This is the tolerance
4472                 --allowed between the requisition price and
4473                 --shipment price.
4474                 -- bug 432746.
4475                 SELECT NVL(MSI.price_tolerance_percent/100,
4476                            NVL(POSP.price_change_allowance/100,-1))
4477                 INTO   l_price_tolerance_allowed
4478                 FROM   MTL_SYSTEM_ITEMS MSI,
4479                        PO_SYSTEM_PARAMETERS POSP,
4480                        FINANCIALS_SYSTEM_PARAMETERS FSP
4481                 WHERE  msi.inventory_item_id(+) = l_item_id(shipment_line)
4482                 AND  MSI.organization_id(+) = FSP.inventory_organization_id;
4483 
4484 l_progress := '008';
4485                 IF l_price_tolerance_allowed <> -1 AND
4486                     l_req_line_unit_price(req_line) <> 0 THEN
4487 
4488 IF g_debug_stmt THEN
4489    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4490      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name||'.'
4491        || l_progress,'l_ship_price_ext_precn= '||l_ship_price_ext_precn
4492        || ' l_req_line_price_ext_precn= ' || l_req_line_price_ext_precn
4493        || ' l_shipment_to_req_rate= ' || l_shipment_to_req_rate
4494        || ' l_price_tolerance_allowed= ' ||  l_price_tolerance_allowed);
4495    END IF;
4496 END IF;
4497 
4498                    /*
4499                    **  Check to see if the rate returned from the function
4500                    **  multiplied by the shipment price in base currency and
4501                    **  then divided by the requisition price is less then
4502                    **  the tolerance.  If not, call the function to
4503                    **  insert into the Online Report Text Table.
4504                    **
4505                    ** The following formula will cost precision erro when the
4506                    ** increase equals to the tolerance.
4507                    ** Patched as part of bug 432746.
4508                    **
4509                    **if ((((ship_price_in_base_curr * rate) /
4510                    **   req_line_unit_price[i]) -1) <= tolerance)
4511                    */
4512 
4513                    /* Bug 638073
4514                       the formula for tolerance check should be
4515                       ship_price_in_base_curr/ req_line_unit_pric e[i] *rate
4516                       since rate is the conversion from shipment uom to req uom
4517                     */
4518 
4519                    /*    svaidyan 09/10/98   726568  Modified the price tolerance
4520                       to check against tolerance + 1.000001. This is because,
4521                       the reqs sourced to a blanket store the unit price rounded
4522                       to 5 decimal places and hence we compare only upto the 5th
4523                       decimal place.
4524                     */
4525 
4526                     /* Bug 3262304, 3266272 mbhargav Using the req price to
4527                        the ext_precn of the currency. Also replacing the division
4528                         by multiplication on the other side
4529                        IF (((l_ship_price_ext_precn) /
4530                         (l_req_line_unit_price(req_line) *
4531                             l_shipment_to_req_rate ))
4532                                   > (l_price_tolerance_allowed + 1.000001))
4533                        THEN
4534                    */
4535                    --<Bug 3266272 mbhargav START>
4536                    IF (l_ship_price_ext_precn >
4537                           ( (l_req_line_price_ext_precn * l_shipment_to_req_rate )
4538                               * (l_price_tolerance_allowed + 1.000001)
4539                           )
4540                    ) THEN
4541                    --<Bug 3266272 mbhargav END>
4542 l_progress := '009';
4543                       --Report the price tolerance error
4544                       l_textline := FND_MESSAGE.GET_STRING('PO',
4545                                           'PO_SUB_REQ_PRICE_TOL_EXCEED');
4546                       INSERT into po_online_report_text_gt(
4547                             online_report_id,
4548                         last_update_login,
4549                         last_updated_by,
4550                         last_update_date,
4551                         created_by,
4552                         creation_date,
4553                         line_num,
4554                         shipment_num,
4555                         distribution_num,
4556                         sequence,
4557                         text_line,
4558                                 message_name)
4559                       VALUES ( p_online_report_id,
4560                             p_login_id,
4561                             p_user_id,
4562                             sysdate,
4563                             p_user_id,
4564                             sysdate,
4565                             l_po_req_line_num(req_line),
4566                             l_po_req_ship_num(req_line),
4567                             0,
4568                             p_sequence +1,
4569                             substr(g_linemsg||g_delim||
4570                                 l_po_req_line_num(req_line)||g_delim||
4571                                 g_shipmsg||g_delim||l_po_req_ship_num(req_line)
4572                                 ||g_delim||l_textline,1,240),
4573                             'PO_SUB_REQ_PRICE_TOL_EXCEED');
4574 
4575                       p_sequence := p_sequence +1;
4576 
4577                      END IF; --check for tolerance
4578 
4579                  END IF; --check l_price_tolerance_allowed
4580 
4581              END LOOP; --req line
4582 
4583         END IF; --price tolerance check
4584 
4585 l_progress := '010';
4586 
4587         --Do price 'not to exceed' amount check
4588         IF l_enforce_price_amount = 'Y' THEN
4589 IF g_debug_stmt THEN
4590    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4591      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name||'.'
4592           || l_progress,'Doing Not to exceed amt check');
4593    END IF;
4594 END IF;
4595 
4596             OPEN req_price_amt_cursor(l_line_location_id(shipment_line));
4597 
4598             FETCH req_price_amt_cursor BULK COLLECT INTO
4599                     l_req_line_unit_price,
4600                     l_req_unit_of_measure,
4601                     l_po_req_quantity,
4602                     l_po_req_line_num,
4603                     l_po_req_ship_num,
4604                     l_requesting_org_id; --Bug 3266272
4605 
4606             CLOSE req_price_amt_cursor;
4607 
4608 l_progress := '011';
4609             FOR req_line IN 1..l_req_line_unit_price.COUNT LOOP
4610 
4611           /*
4612            ** If a row was returned then the PO or Release is associated
4613            ** with a requisition and you should continue with the logic.
4614            ** If a row was not returned.  It does not mean that an error
4615            ** occurred, it meas that the submission check does not apply
4616            ** to this document.
4617            */
4618 
4619                 --<Bug 3266272 mbhargav START>
4620                 /* Bug 4537974: while comparing org_id we need an NVL condition
4621 		   around both the operands of = since for single org installations
4622 		   the org_id can be null */
4623                 IF l_req_ou_func_curr IS NULL THEN
4624                   BEGIN
4625                     SELECT  SOB.currency_code
4626                     INTO  l_req_ou_func_curr
4627                     FROM  financials_system_params_all FSP, gl_sets_of_books SOB
4628                     WHERE  FSP.set_of_books_id = SOB.set_of_books_id
4629                       AND  NVL(FSP.org_id, -99) = NVL(l_requesting_org_id(req_line),-99);
4630                   EXCEPTION
4631                     WHEN OTHERS THEN
4632                        RAISE;
4633                   END;
4634                 END IF;
4635 
4636                 IF l_req_ou_func_curr <> l_pou_func_curr THEN
4637 
4638                    --Obtain the conversion rate between two functional currencies
4639                    --using the rate type from POU setup.
4640                    IF l_rate IS NULL THEN
4641                       BEGIN
4642                         SELECT default_rate_type
4643                         INTO   l_rate_type
4644                         FROM   po_system_parameters;
4645                       EXCEPTION
4646                         WHEN OTHERS THEN
4647                           RAISE;
4648                       END;
4649 
4650                       --Get the conversion rate between Purchasing Operating Unit func
4651                       --currency and Req Operating Unit functional currency
4652                       po_currency_sv.get_rate(
4653                               p_from_currency => l_req_ou_func_curr,
4654                               p_to_currency   => l_pou_func_curr,
4655                               p_rate_type     => l_rate_type,
4656                               p_rate_date     => l_rate_date,
4657                               p_inverse_rate_display_flag => 'N',
4658                               x_rate          => l_rate,
4659                               x_display_rate  => l_display_rate,
4660                               x_return_status => l_return_status,
4661                               x_error_message_name => l_error_message_name);
4662                    END IF; --rate check
4663 
4664                    --Convert the Req line price (which is in Req OU func currency)
4665                    --to Purchasing OU functional currency for comparison
4666                    l_req_line_price_pou_base_curr :=
4667                            l_req_line_unit_price(req_line) * nvl(l_rate,1);
4668 
4669                    --Round off the Req line price (in functional currency of
4670                    --Purchasing Operating Unit) to the extended precision of
4671                    --the functional currency
4672                    l_req_line_price_ext_precn :=
4673                        round(l_req_line_price_pou_base_curr,l_pou_func_curr_ext_precn);
4674                 ELSE  --POU func curr <> ROU func curr
4675                    l_req_line_price_ext_precn :=
4676                       round(l_req_line_unit_price(req_line),l_pou_func_curr_ext_precn);
4677                 END IF; --func curr check
4678                 --<Bug 3266272 mbhargav END>
4679 
4680                 --Call function that returns the shipment price
4681                 --converted to the correct UOM.
4682                 po_uom_s.po_uom_conversion(
4683                     l_ship_unit_of_measure(shipment_line),
4684                     l_req_unit_of_measure(req_line),
4685                     l_item_id(shipment_line),
4686                     l_shipment_to_req_rate);
4687 
4688                 IF l_shipment_to_req_rate = 0.0 THEN
4689                     l_shipment_to_req_rate :=1.0;
4690                 END IF;
4691 
4692 
4693                 IF l_amount_tolerance >= 0 AND
4694                     l_req_line_unit_price(req_line) <> 0 THEN
4695 
4696 IF g_debug_stmt THEN
4697    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4698      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name||'.'
4699        || l_progress,'l_ship_price_ext_precn= '||l_ship_price_ext_precn
4700        || ' l_req_line_price_ext_precn= ' || l_req_line_price_ext_precn
4701        || ' l_shipment_to_req_rate= ' || l_shipment_to_req_rate
4702        || ' l_po_req_quantity= ' ||  l_po_req_quantity(req_line)
4703        || ' l_amount_tolerance= ' || l_amount_tolerance);
4704    END IF;
4705 END IF;
4706                    --do the amount check
4707                    --makes sure the requisition amount and
4708                    --PO amount for each shipment line is within the value
4709                    --defined in the column PRICE_CHANGE_AMOUNT of table
4710                    --PO_SYSTEM_PARAMETERS.
4711                     --Bug 3262304, 3266272 mbhargav Using the Req price which is
4712                     --rounded to the ext_precn of the its currency.
4713                    IF ((l_ship_price_ext_precn -
4714                          (l_req_line_price_ext_precn * l_shipment_to_req_rate)
4715                         ) * l_po_req_quantity(req_line)
4716                                            > l_amount_tolerance
4717                        )
4718                    THEN
4719 l_progress := '012';
4720                       --Report the price amount exceeded error
4721                       l_textline :=
4722                    FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REQ_AMT_TOL_EXCEED');
4723                       INSERT into po_online_report_text_gt(
4724                             online_report_id,
4725                         last_update_login,
4726                         last_updated_by,
4727                         last_update_date,
4728                         created_by,
4729                         creation_date,
4730                         line_num,
4731                         shipment_num,
4732                         distribution_num,
4733                         sequence,
4734                         text_line,
4735                                 message_name)
4736                       VALUES ( p_online_report_id,
4737                             p_login_id,
4738                             p_user_id,
4739                             sysdate,
4740                             p_user_id,
4741                             sysdate,
4742                             l_po_req_line_num(req_line),
4743                             l_po_req_ship_num(req_line),
4744                             0,
4745                             p_sequence +1,
4746                             substr(g_linemsg||g_delim||
4747                                 l_po_req_line_num(req_line)||g_delim||
4748                                 g_shipmsg||g_delim||l_po_req_ship_num(req_line)
4749                                 ||g_delim||l_textline,1,240),
4750                             'PO_SUB_REQ_AMT_TOL_EXCEED');
4751 
4752                       p_sequence := p_sequence +1;
4753 
4754                      END IF; --amount check
4755 
4756                  END IF; --check l_amount_tolerance_allowed
4757 
4758              END LOOP; --req line
4759 
4760         END IF; --not to exceed amount check
4761 
4762    END LOOP;  --for shipment_line
4763 
4764 l_progress := '013';
4765     x_return_status := FND_API.G_RET_STS_SUCCESS;
4766 EXCEPTION
4767     WHEN FND_API.G_EXC_ERROR THEN
4768         x_return_status := FND_API.G_RET_STS_ERROR;
4769 
4770     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4771         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4772 
4773     WHEN OTHERS THEN
4774         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4775         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
4776             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
4777         END IF;
4778 
4779         IF (g_debug_unexp) THEN
4780                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
4781                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
4782                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
4783                        || l_progress || ' SQL CODE is '||sqlcode);
4784                 END IF;
4785         END IF;
4786 END CHECK_PO_REL_REQPRICE;
4787 
4788 
4789 --For PO,PA: Header Checks
4790 /**
4791 * Private Procedure: CHECK_PO_PA_HEADER
4792 * Requires:
4793 *   IN PARAMETERS:
4794 *       p_document_id:      The requisition_header_id of submitted document
4795 *       p_online_report_id: Id used to INSERT INTO online_report_text table
4796 *       p_user_id:          User performing the action
4797 *       p_login_id:         Last update login_id
4798 *   IN OUT PARAMETERS
4799 *       p_sequence:          Sequence number of last reported error
4800 * Modifies: Inserts error msgs in online_report_text_gt table, uses
4801 *           global_temp tables for processing
4802 * Effects:  This procedure runs the document submission checks for HEADERS
4803 *           of POs and PAs
4804 * Returns:
4805 *  p_sequence: This parameter contains the current count of number of error
4806 *              messages inserted
4807 */
4808 PROCEDURE check_po_pa_header(p_document_id IN NUMBER,
4809                        p_online_report_id IN NUMBER,
4810                        p_user_id IN NUMBER,
4811                        p_login_id IN NUMBER,
4812                        p_sequence IN OUT NOCOPY NUMBER,
4813                        x_return_status OUT NOCOPY VARCHAR2) IS
4814 
4815 l_textline            po_online_report_text.text_line%TYPE := NULL;
4816 l_api_name  CONSTANT varchar2(40) := 'CHECK_PO_PA_HEADER';
4817 l_progress VARCHAR2(3);
4818 
4819 l_vendor_id           po_headers.vendor_id%TYPE;
4820 l_vendor_site_id      po_headers.vendor_site_id%TYPE;
4821 l_vendor_contact_id   po_headers.vendor_contact_id%TYPE; /*bug 6530879*/
4822 l_ship_to_location_id po_headers.ship_to_location_id%TYPE;
4823 l_bill_to_location_id po_headers.bill_to_location_id%TYPE;
4824 l_currency_code       po_headers.currency_code%TYPE;
4825 l_sob_currency_code   po_headers.currency_code%TYPE;
4826 l_rate_type           po_headers.rate_type%TYPE;
4827 l_rate                po_headers.rate%TYPE;
4828 l_rate_date           po_headers.rate_date%TYPE;
4829 
4830 --bug#3987438
4831 --Adding a new variable that would hold the name of the invalid
4832 --ship-to or bill-to location
4833 l_invalid_location   HR_LOCATIONS_ALL_TL.location_code%type;
4834 --bug#3987438
4835 
4836 BEGIN
4837 
4838 l_progress := '000';
4839 
4840 l_progress := '001';
4841 IF g_debug_stmt THEN
4842    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4843      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
4844           || l_progress,'PO/PA Header 1: Vendor On Hold ');
4845    END IF;
4846 END IF;
4847 
4848   -- Check 1: Purchase Order vendor should not be on hold
4849     -- PO_SUB_VENDOR_ON_HOLD
4850 
4851   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_VENDOR_ON_HOLD');
4852   INSERT INTO po_online_report_text_gt(online_report_id,
4853         last_update_login,
4854         last_updated_by,
4855         last_update_date,
4856         created_by,
4857         creation_date,
4858         line_num,
4859         shipment_num,
4860         distribution_num,
4861         sequence,
4862         text_line,
4863                 message_name)
4864   SELECT  p_online_report_id,
4865         p_login_id,
4866         p_user_id,
4867           sysdate,
4868         p_user_id,
4869         sysdate,
4870         0,
4871         0,
4872         0,
4873         p_sequence + ROWNUM,
4874         substr(l_textline,1,240),
4875             'PO_SUB_VENDOR_ON_HOLD'
4876     FROM  PO_HEADERS_GT POH, PO_VENDORS POV, PO_SYSTEM_PARAMETERS PSP
4877     WHERE  POV.vendor_id     = POH.vendor_id
4878     AND    POH.po_header_id = p_document_id
4879     AND    nvl(PSP.ENFORCE_VENDOR_HOLD_FLAG,'N') = 'Y'
4880     AND    nvl(POV.hold_flag,'N') = 'Y';
4881 
4882 
4883      --Increment the p_sequence with number of errors reported in last query
4884     p_sequence := p_sequence + SQL%ROWCOUNT;
4885 ---------------------------------------------
4886 l_progress := '002';
4887     BEGIN
4888         SELECT POH.vendor_id,
4889                     POH.vendor_site_id,
4890                     POH.vendor_contact_id,
4891 		    POH.ship_to_location_id,
4892                     POH.bill_to_location_id,
4893                     POH.currency_code,
4894                     SOB.currency_code,
4895                     POH.rate_type,
4896                     POH.rate,
4897                     POH.rate_date
4898         INTO   l_vendor_id,
4899                     l_vendor_site_id,
4900 		    l_vendor_contact_id,
4901                     l_ship_to_location_id,
4902                     l_bill_to_location_id,
4903                     l_currency_code,
4904                     l_sob_currency_code,
4905                     l_rate_type,
4906                     l_rate,
4907                     l_rate_date
4908         FROM   PO_HEADERS_GT POH,
4909                GL_SETS_OF_BOOKS SOB,
4910                FINANCIALS_SYSTEM_PARAMETERS FSP
4911         WHERE  POH.po_header_id    = p_document_id
4912         AND    SOB.set_of_books_id = FSP.set_of_books_id;
4913     EXCEPTION
4914         WHEN NO_DATA_FOUND THEN
4915 l_progress := '003';
4916 IF g_debug_stmt THEN
4917    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4918      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
4919           || l_progress,'PO/PA Header 2:System setup check');
4920    END IF;
4921 END IF;
4922             -- Check 2: When no rows are returned, its likely that there
4923             --are problems with system setup
4924             --<NOTE> See if we need to stop further processing
4925             l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_SYSTEM_SETUP');
4926             INSERT INTO po_online_report_text_gt(online_report_id,
4927         last_update_login,
4928         last_updated_by,
4929         last_update_date,
4930         created_by,
4931         creation_date,
4932         line_num,
4933         shipment_num,
4934         distribution_num,
4935         sequence,
4936         text_line,
4937                 message_name)
4938           VALUES (p_online_report_id,
4939         p_login_id,
4940         p_user_id,
4941           sysdate,
4942         p_user_id,
4943         sysdate,
4944         0,
4945         0,
4946         0,
4947         p_sequence + 1,
4948         substr(l_textline,1,240),
4949             'PO_SUB_SYSTEM_SETUP');
4950 
4951         --Increment the p_sequence with number of errors reported in last query
4952         p_sequence := p_sequence + 1;
4953 ---------------------------------------------
4954     END;
4955 
4956     --Check to see if the fields are null.  If the are null copy then
4957     --call the online report function.  Only print the message for the
4958     -- rate type if the rate type is null and they are using a foreign
4959     -- currency.  You know if a foreign currency is used if the currency
4960     -- code and sob_currency_code do not match.
4961 
4962 l_progress := '004';
4963 IF g_debug_stmt THEN
4964    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4965      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
4966           || l_progress,'PO/PA Header 3: No Vendor');
4967    END IF;
4968 END IF;
4969 
4970     --Check 3: vendor_id is NULL
4971     IF l_vendor_id IS NULL THEN
4972          l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_ENTER_VENDOR');
4973          INSERT INTO po_online_report_text_gt(online_report_id,
4974         last_update_login,
4975         last_updated_by,
4976         last_update_date,
4977         created_by,
4978         creation_date,
4979         line_num,
4980         shipment_num,
4981         distribution_num,
4982         sequence,
4983         text_line,
4984                 message_name)
4985         VALUES (p_online_report_id,
4986         p_login_id,
4987         p_user_id,
4988           sysdate,
4989         p_user_id,
4990         sysdate,
4991         0,
4992         0,
4993         0,
4994         p_sequence + 1,
4995         substr(l_textline,1,240),
4996             'PO_SUB_ENTER_VENDOR');
4997 
4998          --Increment the p_sequence with number of errors reported in last query
4999          p_sequence := p_sequence + 1;
5000     END IF; --vendor_id
5001 
5002 l_progress := '005';
5003 IF g_debug_stmt THEN
5004    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5005      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
5006           || l_progress,'PO/PA Header 4: No Vendor Site');
5007    END IF;
5008 END IF;
5009 
5010     --Check 4: vendor_site_id is NULL
5011     IF l_vendor_site_id IS NULL THEN
5012          l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_ENTER_VENDOR_SITE');
5013          INSERT INTO po_online_report_text_gt(online_report_id,
5014         last_update_login,
5015         last_updated_by,
5016         last_update_date,
5017         created_by,
5018         creation_date,
5019         line_num,
5020         shipment_num,
5021         distribution_num,
5022         sequence,
5023         text_line,
5024                 message_name)
5025         VALUES (p_online_report_id,
5026         p_login_id,
5027         p_user_id,
5028           sysdate,
5029         p_user_id,
5030         sysdate,
5031         0,
5032         0,
5033         0,
5034         p_sequence + 1,
5035         substr(l_textline,1,240),
5036             'PO_SUB_ENTER_VENDOR_SITE');
5037 
5038          --Increment the p_sequence with number of errors reported in last query
5039          p_sequence := p_sequence + 1;
5040     END IF; --vendor_site_id
5041 
5042 l_progress := '006';
5043 IF g_debug_stmt THEN
5044    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5045      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
5046           || l_progress,'PO/PA Header 5: Ship_to_loc_id check ');
5047    END IF;
5048 END IF;
5049 
5050     --Check 5: ship_to_location_id is NULL
5051     IF l_ship_to_location_id IS NULL THEN
5052          l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_NO_SHIP_TO_LOC_ID');
5053          INSERT INTO po_online_report_text_gt(online_report_id,
5054         last_update_login,
5055         last_updated_by,
5056         last_update_date,
5057         created_by,
5058         creation_date,
5059         line_num,
5060         shipment_num,
5061         distribution_num,
5062         sequence,
5063         text_line,
5064                 message_name)
5065         VALUES (p_online_report_id,
5066         p_login_id,
5067         p_user_id,
5068           sysdate,
5069         p_user_id,
5070         sysdate,
5071         0,
5072         0,
5073         0,
5074         p_sequence + 1,
5075         substr(l_textline,1,240),
5076             'PO_SUB_NO_SHIP_TO_LOC_ID');
5077 
5078          --Increment the p_sequence with number of errors reported in last query
5079          p_sequence := p_sequence + 1;
5080 
5081 --bug#3987438 Added a check to verify that the ship to location is active
5082     ELSE
5083         BEGIN
5084             SELECT hlt.location_code
5085             INTO l_invalid_location
5086             FROM hr_locations_all hla,
5087                  hr_locations_all_tl hlt
5088             WHERE hla.location_id = l_ship_to_location_id
5089             AND NVL(TRUNC(hla.inactive_date), TRUNC(SYSDATE)+1 ) <= TRUNC(SYSDATE)
5090             AND hlt.location_id=hla.location_id
5091             AND hlt.language=USERENV('LANG');
5092 
5093             INSERT INTO po_online_report_text_gt(online_report_id,
5094                 last_update_login,
5095                 last_updated_by,
5096                 last_update_date,
5097                 created_by,
5098                 creation_date,
5099                 line_num,
5100                 shipment_num,
5101                 distribution_num,
5102                 sequence,
5103                 text_line,
5104                 message_name)
5105             VALUES
5106                (
5107                 p_online_report_id,
5108                 p_login_id,
5109                 p_user_id,
5110                 SYSDATE,
5111                 p_user_id,
5112                 SYSDATE,
5113                 0,
5114                 0,
5115                 0,
5116                 p_sequence + 1,
5117                 substr(PO_CORE_S.get_translated_text
5118                 ( 'PO_SUB_INVALID_SHIP_TO_LOC',
5119                   'SHIP_TO_LOC',
5120                   l_invalid_location),1,240),
5121                 'PO_SUB_INVALID_SHIP_TO_LOC'
5122                 );
5123                  p_sequence := p_sequence + 1;
5124         EXCEPTION
5125             WHEN NO_DATA_FOUND THEN
5126                 null;
5127         END;
5128 --bug#3987438
5129 
5130     END IF; --ship_to_loc_id
5131 
5132 l_progress := '007';
5133 IF g_debug_stmt THEN
5134    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5135      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
5136           || l_progress,'PO/PA Header 6: Bill_to_loc check');
5137    END IF;
5138 END IF;
5139 
5140     --Check 6: bill_to_location_id is NULL
5141     IF l_bill_to_location_id IS NULL THEN
5142 
5143          l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_NO_BILL_TO_LOC_ID');
5144          INSERT INTO po_online_report_text_gt(online_report_id,
5145         last_update_login,
5146         last_updated_by,
5147         last_update_date,
5148         created_by,
5149         creation_date,
5150         line_num,
5151         shipment_num,
5152         distribution_num,
5153         sequence,
5154         text_line,
5155                 message_name)
5156         VALUES (p_online_report_id,
5157         p_login_id,
5158         p_user_id,
5159           sysdate,
5160         p_user_id,
5161         sysdate,
5162         0,
5163         0,
5164         0,
5165         p_sequence + 1,
5166         substr(l_textline,1,240),
5167             'PO_SUB_NO_BILL_TO_LOC_ID');
5168 
5169          --Increment the p_sequence with number of errors reported in last query
5170          p_sequence := p_sequence + 1;
5171 
5172 --bug#3987438 Added a check to verify that the bill to location is active
5173     ELSE
5174         BEGIN
5175             SELECT hlt.location_code
5176             INTO l_invalid_location
5177             FROM hr_locations_all hla,
5178                  hr_locations_all_tl hlt
5179             WHERE hla.location_id = l_bill_to_location_id
5180             AND NVL(TRUNC(hla.inactive_date), TRUNC(SYSDATE)+1 ) <= TRUNC(SYSDATE)
5181             AND hlt.location_id=hla.location_id
5182             AND hlt.language=USERENV('LANG');
5183 
5184             INSERT INTO po_online_report_text_gt(online_report_id,
5185                 last_update_login,
5186                 last_updated_by,
5187                 last_update_date,
5188                 created_by,
5189                 creation_date,
5190                 line_num,
5191                 shipment_num,
5192                 distribution_num,
5193                 sequence,
5194                 text_line,
5195                 message_name)
5196             VALUES
5197                (
5198                 p_online_report_id,
5199                 p_login_id,
5200                 p_user_id,
5201                 SYSDATE,
5202                 p_user_id,
5203                 SYSDATE,
5204                 0,
5205                 0,
5206                 0,
5207                 p_sequence + 1,
5208                 substr(PO_CORE_S.get_translated_text('PO_SUB_INVALID_BILL_TO_LOC','BILL_TO_LOC',l_invalid_location),1,240),
5209                 'PO_SUB_INVALID_BILL_TO_LOC'
5210                 );
5211                  p_sequence := p_sequence + 1;
5212         EXCEPTION
5213             WHEN NO_DATA_FOUND THEN
5214                     null;
5215         END;
5216 --bug#3987438
5217 
5218     END IF; --bill_to_loc_id
5219 
5220 l_progress := '008';
5221 IF g_debug_stmt THEN
5222    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5223      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
5224           || l_progress,'PO/PA Header 7: No currency code');
5225    END IF;
5226 END IF;
5227 
5228     --Check 7: currency_code is NULL
5229     IF l_currency_code IS NULL THEN
5230          l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_NO_CURRENCY_CODE');
5231          INSERT INTO po_online_report_text_gt(online_report_id,
5232         last_update_login,
5233         last_updated_by,
5234         last_update_date,
5235         created_by,
5236         creation_date,
5237         line_num,
5238         shipment_num,
5239         distribution_num,
5240         sequence,
5241         text_line,
5242                 message_name)
5243         VALUES (p_online_report_id,
5244         p_login_id,
5245         p_user_id,
5246           sysdate,
5247         p_user_id,
5248         sysdate,
5249         0,
5250         0,
5251         0,
5252         p_sequence + 1,
5253         substr(l_textline,1,240),
5254             'PO_SUB_NO_CURRENCY_CODE');
5255 
5256          --Increment the p_sequence with number of errors reported in last query
5257          p_sequence := p_sequence + 1;
5258     END IF; --currency_code
5259 
5260 l_progress := '009';
5261 IF g_debug_stmt THEN
5262    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5263      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
5264           || l_progress,'PO/PA Header 8: Rate related checks');
5265    END IF;
5266 END IF;
5267 
5268     --Check 8: rate
5269     IF l_currency_code <> l_sob_currency_code AND
5270         (l_rate_type IS NULL OR l_rate IS NULL OR
5271             (l_rate_type <> 'User' AND l_rate_date IS NULL)) -- Bug 3759198
5272     THEN
5273          l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_NO_RATE_TYPE');
5274          INSERT INTO po_online_report_text_gt(online_report_id,
5275         last_update_login,
5276         last_updated_by,
5277         last_update_date,
5278         created_by,
5279         creation_date,
5280         line_num,
5281         shipment_num,
5282         distribution_num,
5283         sequence,
5284         text_line,
5285                 message_name)
5286         VALUES (p_online_report_id,
5287         p_login_id,
5288         p_user_id,
5289           sysdate,
5290         p_user_id,
5291         sysdate,
5292         0,
5293         0,
5294         0,
5295         p_sequence + 1,
5296         substr(l_textline,1,240),
5297             'PO_SUB_NO_RATE_TYPE');
5298 
5299          --Increment the p_sequence with number of errors reported in last query
5300          p_sequence := p_sequence + 1;
5301 
5302     END IF; --rate related
5303 
5304 -- <SERVICES FPJ START>
5305 
5306 l_progress := '015';
5307 IF g_debug_stmt THEN
5308    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5309      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
5310           || l_progress,'PO/PA Header 9: Currency Rate Type');
5311    END IF;
5312 END IF;
5313 
5314     -- CHECK 9: The Currency Rate Type cannot be "User" if the document
5315     --          contains any Lines with Value Basis of "Rate".
5316 
5317     IF ( l_rate_type = 'User' ) THEN
5318 
5319         l_textline := FND_MESSAGE.get_string('PO','PO_SUB_USER_RATE_TYPE');
5320 
5321         INSERT INTO po_online_report_text_gt
5322         (   online_report_id
5323         ,   last_update_login
5324         ,   last_updated_by
5325         ,   last_update_date
5326         ,   created_by
5327         ,   creation_date
5328         ,   line_num
5329         ,   shipment_num
5330         ,   distribution_num
5331         ,   sequence
5332         ,   text_line
5333         ,   message_name
5334         )
5335         SELECT p_online_report_id
5336         ,      p_login_id
5337         ,      p_user_id
5338         ,      sysdate
5339         ,      p_user_id
5340         ,      sysdate
5341         ,      NULL
5342         ,      NULL
5343         ,      NULL
5344         ,      p_sequence + ROWNUM
5345         ,      l_textline
5346         ,      'PO_SUB_USER_RATE_TYPE'
5347         FROM   dual
5348         WHERE  exists ( SELECT 'Rate-based lines exist'
5349                         FROM   po_lines_gt      POL
5350                         ,      po_line_types_b  PLT
5351                         WHERE  p_document_id = POL.po_header_id
5352                         AND    POL.line_type_id = PLT.line_type_id
5353                         AND    PLT.order_type_lookup_code = 'RATE'
5354                       );
5355 
5356         --Increment the p_sequence with number of errors reported in last query
5357         p_sequence := p_sequence + SQL%ROWCOUNT;
5358 
5359     END IF; -- ( l_rate_type = 'User' )
5360 
5361 -- <SERVICES FPJ END>
5362 
5363 l_progress := '020';
5364 IF g_debug_stmt THEN
5365    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5366      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
5367           || l_progress,'PO/PA Header 10: Cannot approve documents on hold');
5368    END IF;
5369 END IF;
5370     -- Check 10: Purchase Order should not be on hold (Bug 3678912)
5371         -- PO_ON_HOLD_CANNOT_APPROVE
5372 
5373   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_ON_HOLD_CANNOT_APPROVE');
5374   INSERT INTO po_online_report_text_gt(online_report_id,
5375         last_update_login,
5376         last_updated_by,
5377         last_update_date,
5378         created_by,
5379         creation_date,
5380         line_num,
5381         shipment_num,
5382         distribution_num,
5383         sequence,
5384         text_line,
5385                                 message_name)
5386   SELECT  p_online_report_id,
5387         p_login_id,
5388         p_user_id,
5389                   sysdate,
5390         p_user_id,
5391         sysdate,
5392         0,
5393         0,
5394         0,
5395         p_sequence + ROWNUM,
5396         substr(l_textline,1,240),
5397                    'PO_ON_HOLD_CANNOT_APPROVE'
5398     FROM  PO_HEADERS_GT POH
5399     WHERE  POH.po_header_id = p_document_id
5400     AND    nvl(POH.USER_HOLD_FLAG,'N') = 'Y';
5401 
5402     --Increment the p_sequence with number of errors reported in last query
5403         p_sequence := p_sequence + SQL%ROWCOUNT;
5404 
5405 -- bug 6530879
5406 -- Check 11: Vendor should be valid when approving the document.
5407 -- Important for reapproval, to avoid the case when the vendor has
5408 -- been invalidated by first successful approval.
5409 
5410 /* Need to find out.
5411 
5412  fnd_message.set_name('PO', 'PO_PDOI_INVALID_VENDOR');
5413  fnd_message.set_token('VALUE', to_char(x_vendor_id), FALSE);
5414 l_textline := fnd_message.get;
5415 
5416 */
5417 
5418 l_textline :=  FND_MESSAGE.GET_STRING('PO', 'PO_PDOI_INVALID_VENDOR');
5419 
5420   if (l_vendor_id is NOT NULL) then
5421 	fnd_message.set_name('PO', 'PO_PDOI_INVALID_VENDOR');
5422         fnd_message.set_token('VALUE', to_char(l_vendor_id), FALSE);
5423 	l_textline := substr(fnd_message.get, 1, 240);
5424 
5425 
5426  	INSERT INTO po_online_report_text_gt(online_report_id,
5427  				last_update_login,
5428  				last_updated_by,
5429  				last_update_date,
5430  				created_by,
5431  				creation_date,
5432  				line_num,
5433  				shipment_num,
5434  				distribution_num,
5435  				sequence,
5436  				text_line,
5437                                 message_name)
5438  	SELECT 	p_online_report_id,
5439  		    p_login_id,
5440  		    p_user_id,
5441      	            sysdate,
5442  		    p_user_id,
5443  		    sysdate,
5444  		    0,
5445  		    0,
5446  		    0,
5447  		    p_sequence + ROWNUM,
5448  		    substr(l_textline,1,240),
5449                    'PO_PDOI_INVALID_VENDOR'
5450 	FROM  dual
5451     where not exists (select 'Y'
5452 			from PO_HEADERS_GT POH, po_vendors pov
5453 		       WHERE  POH.po_header_id = p_document_id
5454 		         AND  pov.vendor_id = poh.vendor_id
5455 			 AND  pov.enabled_flag = 'Y'
5456 			 AND  SYSDATE BETWEEN nvl(pov.start_date_active, SYSDATE-1)
5457                                   AND nvl(pov.end_date_active, SYSDATE+1));
5458  --Increment the p_sequence with number of errors reported in last query
5459         p_sequence := p_sequence + SQL%ROWCOUNT;
5460 
5461  end if;
5462 
5463 -- Check 12: check the validity of the vendor site.
5464  if (l_vendor_site_id is not null) then
5465 
5466   	fnd_message.set_name('PO', 'PO_PDOI_INVALID_VENDOR_SITE');
5467  	fnd_message.set_token('VALUE', to_char(l_vendor_site_id), FALSE);
5468 	l_textline := substr(fnd_message.get, 1, 240);
5469 
5470  	INSERT INTO po_online_report_text_gt(online_report_id,
5471  				last_update_login,
5472  				last_updated_by,
5473  				last_update_date,
5474  				created_by,
5475  				creation_date,
5476  				line_num,
5477  				shipment_num,
5478  				distribution_num,
5479  				sequence,
5480  				text_line,
5481                                 message_name)
5482  	SELECT 	p_online_report_id,
5483  		    p_login_id,
5484  		    p_user_id,
5485      	            sysdate,
5486  		    p_user_id,
5487  		    sysdate,
5488  		    0,
5489  		    0,
5490  		    0,
5491  		    p_sequence + ROWNUM,
5492  		    substr(l_textline,1,240),
5493                    'PO_PDOI_INVALID_VENDOR_SITE'
5494     FROM  dual
5495     where not exists (select 'Y'
5496 			from PO_HEADERS_GT POH, po_vendor_sites povs
5497 		        WHERE  POH.po_header_id = p_document_id
5498 		        AND  povs.vendor_site_id = poh.vendor_site_id
5499 			AND    nvl(povs.rfq_only_site_flag,'N') <> 'Y'
5500 			AND    povs.purchasing_site_flag = 'Y'
5501 		        AND    SYSDATE < nvl(povs.inactive_date, SYSDATE + 1));
5502  --Increment the p_sequence with number of errors reported in last query
5503         p_sequence := p_sequence + SQL%ROWCOUNT;
5504 end if;
5505 
5506 -- check 13: validate vendor contact
5507 if (l_vendor_contact_id is not null) then
5508 
5509 	fnd_message.set_name('PO', 'PO_PDOI_INVALID_VDR_CNTCT');
5510  	fnd_message.set_token('VALUE', to_char(l_vendor_contact_id), FALSE);
5511 	l_textline := substr(fnd_message.get, 1, 240);
5512 
5513 	INSERT INTO po_online_report_text_gt(online_report_id,
5514  				last_update_login,
5515  				last_updated_by,
5516  				last_update_date,
5517  				created_by,
5518  				creation_date,
5519  				line_num,
5520  				shipment_num,
5521  				distribution_num,
5522  				sequence,
5523  				text_line,
5524                                 message_name)
5525  	SELECT 	p_online_report_id,
5526  		    p_login_id,
5527  		    p_user_id,
5528      	            sysdate,
5529  		    p_user_id,
5530  		    sysdate,
5531  		    0,
5532  		    0,
5533  		    0,
5534  		    p_sequence + ROWNUM,
5535  		    substr(l_textline,1,240),
5536                    'PO_PDOI_INVALID_VDR_CNTCT'
5537 	FROM  dual
5538 	WHERE NOT EXISTS (SELECT  'Y'
5539 			    FROM   PO_VENDOR_CONTACTS pvc, po_headers_gt poh
5540   			   WHERE    POH.po_header_id = p_document_id
5541 			     AND    pvc.vendor_contact_id =poh.vendor_contact_id
5542 			     AND    SYSDATE < nvl(pvc.inactive_date, SYSDATE+1));
5543 
5544    --Increment the p_sequence with number of errors reported in last query
5545         p_sequence := p_sequence + SQL%ROWCOUNT;
5546 end if;
5547 
5548     l_progress := '050';
5549     x_return_status := FND_API.G_RET_STS_SUCCESS;
5550 EXCEPTION
5551     WHEN FND_API.G_EXC_ERROR THEN
5552         x_return_status := FND_API.G_RET_STS_ERROR;
5553 
5554     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5555         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5556 
5557     WHEN OTHERS THEN
5558         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5559         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
5560             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
5561         END IF;
5562 
5563         IF (g_debug_unexp) THEN
5564                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
5565                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
5566                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
5567                        || l_progress || ' SQL CODE is '||sqlcode);
5568                 END IF;
5569         END IF;
5570 
5571 END CHECK_PO_PA_HEADER;
5572 
5573 --For PO
5574 /**
5575 * Private Procedure: CHECK_PO
5576 * Requires:
5577 *   IN PARAMETERS:
5578 *       p_document_id:      The requisition_header_id of submitted document
5579 *       p_online_report_id: Id used to INSERT INTO online_report_text table
5580 *       p_user_id:          User performing the action
5581 *       p_login_id:         Last update login_id
5582 *       p_check_asl:        Determines whether to perform ASL checks...
5583 *                           PO_SUB_ITEM_NOT_APPROVED, PO_SUB_ITEM_ASL_DEBARRED
5584 *   IN OUT PARAMETERS
5585 *       p_sequence:          Sequence number of last reported error
5586 * Modifies: Updates PO_DISTRIBUTIONS table with RATE information.
5587 *           Inserts error msgs in online_report_text_gt table, uses
5588 *           global_temp tables for processing
5589 * Effects:  This procedure runs the document submission checks for POs
5590 * Returns:
5591 *  p_sequence: This parameter contains the current count of number of error
5592 *              messages inserted
5593 */
5594 PROCEDURE check_po(p_document_id IN NUMBER,
5595                        p_online_report_id IN NUMBER,
5596                        p_user_id IN NUMBER,
5597                        p_login_id IN NUMBER,
5598                        p_sequence IN OUT NOCOPY NUMBER,
5599                        x_return_status OUT NOCOPY VARCHAR2) IS
5600 
5601 l_textline  po_online_report_text.text_line%TYPE := NULL;
5602 l_api_name  CONSTANT varchar2(40) := 'CHECK_PO';
5603 l_progress VARCHAR2(3);
5604 l_is_complex_po     boolean;
5605 l_line_loc_token_value fnd_new_messages.message_text%TYPE;
5606 
5607 TYPE NumTab is TABLE of NUMBER INDEX by BINARY_INTEGER;
5608 l_quantity1 NumTab;
5609 l_quantity2 NumTab;
5610 l_line_num   NumTab;
5611 l_shipment_num NumTab;
5612 l_dist_num NumTab;
5613 l_line_qty_tbl    NumTab;                                     -- <SERVICES FPJ>
5614 l_line_amt_tbl    NumTab;                                     -- <SERVICES FPJ>
5615 l_ship_qty_tbl    NumTab;                                     -- <SERVICES FPJ>
5616 l_ship_amt_tbl    NumTab;                                     -- <SERVICES FPJ>
5617 l_dist_qty_tbl    NumTab;                                     -- <SERVICES FPJ>
5618 l_dist_amt_tbl    NumTab;                                     -- <SERVICES FPJ>
5619 l_rowcount NumTab;
5620 
5621 l_val_contract_limit NUMBER;                                  -- bug3673292
5622 
5623 --bug#3987438
5624 --A new table to hold the invalid ship to location codes
5625 TYPE CharTab is TABLE of HR_LOCATIONS_ALL_TL.location_code%type INDEX by BINARY_INTEGER;
5626 l_ship_to_location_tbl CharTab;
5627 --bug#3987438
5628 
5629 --<R12 eTax Integration Start>
5630 l_return_status    VARCHAR2(1);
5631 l_tax_status       VARCHAR2(1);
5632 l_msg_count        NUMBER;
5633 l_msg_data         VARCHAR2(2000);
5634 l_tax_message      fnd_new_messages.message_text%TYPE;
5635 --<R12 eTax Integration End>
5636 
5637 l_po_vmi_display_warning VARCHAR2(1);
5638 
5639 BEGIN
5640 
5641 l_progress := '000';
5642 -- BUG 2687600 mbhargav
5643 --Removed Update statement to update rate in po_distributions
5644 
5645 l_progress := '001';
5646 IF g_debug_stmt THEN
5647    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5648      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
5649           || l_progress,'PO 1: No lines');
5650    END IF;
5651 END IF;
5652 
5653     -- Check 1: Header must have at least one line
5654     -- PO_SUB_HEADER_NO_LINES
5655     -- Message inserted is 'Purchase Document has no lines'
5656     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_HEADER_NO_LINES');
5657     INSERT INTO po_online_report_text_gt(online_report_id,
5658         last_update_login,
5659         last_updated_by,
5660         last_update_date,
5661         created_by,
5662           creation_date,
5663         line_num,
5664         shipment_num,
5665         distribution_num,
5666         sequence,
5667         text_line,
5668                 message_name)
5669     SELECT  p_online_report_id,
5670             p_login_id,
5671             p_user_id,
5672             sysdate,
5673             p_user_id,
5674             sysdate,
5675             0, 0, 0,
5676             p_sequence + ROWNUM,
5677             substr(l_textline,1,240),
5678             'PO_SUB_HEADER_NO_LINES'
5679     FROM   PO_HEADERS_GT POH
5680     WHERE  POH.po_header_id = p_document_id AND
5681            NOT EXISTS (SELECT 'Lines Exist'
5682                        FROM   PO_LINES_GT POL
5683                        WHERE  POL.po_header_id = POH.po_header_id
5684                        --Bug 3289638 Check for any line to exist irrespective of cancel_flag
5685                        --AND    nvl(POL.cancel_flag,'N') = 'N'
5686                        );
5687 
5688     --Increment the p_sequence with number of errors reported in last query
5689     p_sequence := p_sequence + SQL%ROWCOUNT;
5690 --------------------------------------------------
5691 
5692 l_progress := '002';
5693 IF g_debug_stmt THEN
5694    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5695      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
5696           || l_progress,'PO 2: No shipment ');
5697    END IF;
5698 END IF;
5699 
5700   -- Check 2: Each Purchase Order Line must have at least one shipment
5701     -- PO_SUB_LINE_NO_SHIP
5702 
5703   l_is_complex_po := PO_COMPLEX_WORK_PVT.is_complex_work_po(p_document_id);
5704 
5705   IF (l_is_complex_po) THEN
5706     l_line_loc_token_value := FND_MESSAGE.GET_STRING('PO', 'PO_LINE_LOC_TYPE_LOW_PAYITEMS');
5707   ELSE
5708     l_line_loc_token_value := FND_MESSAGE.GET_STRING('PO', 'PO_LINE_LOC_TYPE_LOW_SCHEDULES');
5709   END IF;
5710 
5711   FND_MESSAGE.SET_NAME('PO','PO_SUB_LINE_NO_SHIP');
5712   FND_MESSAGE.SET_TOKEN('LINE_LOCATION_TYPE', l_line_loc_token_value);
5713 
5714   l_textline := FND_MESSAGE.GET;
5715   INSERT INTO po_online_report_text_gt(online_report_id,
5716         last_update_login,
5717         last_updated_by,
5718         last_update_date,
5719         created_by,
5720         creation_date,
5721         line_num,
5722         shipment_num,
5723         distribution_num,
5724         sequence,
5725         text_line,
5726                 message_name)
5727   SELECT  p_online_report_id,
5728         p_login_id,
5729         p_user_id,
5730           sysdate,
5731         p_user_id,
5732         sysdate,
5733         POL.line_num,
5734         0,
5735         0,
5736         p_sequence + ROWNUM,
5737         substr(g_linemsg||g_delim||POL.line_num||g_delim||l_textline,1,240),
5738             'PO_SUB_LINE_NO_SHIP'
5739   FROM    PO_LINES_GT POL
5740     WHERE   POL.po_header_id = p_document_id AND
5741         nvl(POL.cancel_flag,'N') = 'N' AND
5742         nvl(POL.closed_code,'OPEN') <> 'FINALLY CLOSED' AND
5743         NOT EXISTS (SELECT 'Shipments Exist'
5744                   FROM   PO_LINE_LOCATIONS_GT PLL
5745                   WHERE  PLL.po_line_id = POL.po_line_id AND
5746                        PLL.shipment_type in ('STANDARD','PLANNED'));
5747 
5748     --Increment the p_sequence with number of errors reported in last query
5749     p_sequence := p_sequence + SQL%ROWCOUNT;
5750 -------------------------------------------------
5751 
5752 l_progress := '003';
5753 IF g_debug_stmt THEN
5754    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5755      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
5756           || l_progress,'PO 3: Atleast One dist ');
5757    END IF;
5758 END IF;
5759 
5760      --Check 3: Each PO shipment must have atleast one distribution
5761      --PO_SUB_SHIP_NO_DIST
5762 
5763      IF (l_is_complex_po) THEN
5764        l_line_loc_token_value := FND_MESSAGE.GET_STRING('PO', 'PO_LINE_LOC_TYPE_LOW_P_PAYITEM');
5765      ELSE
5766        l_line_loc_token_value := FND_MESSAGE.GET_STRING('PO', 'PO_LINE_LOC_TYPE_LOWER_S_SCH');
5767      END IF;
5768 
5769      FND_MESSAGE.SET_NAME('PO', 'PO_SUB_SHIP_NO_DIST');
5770      FND_MESSAGE.SET_TOKEN('LINE_LOCATION_TYPE', l_line_loc_token_value);
5771      l_textline := FND_MESSAGE.GET;
5772      INSERT INTO po_online_report_text_gt(online_report_id,
5773         last_update_login,
5774         last_updated_by,
5775         last_update_date,
5776         created_by,
5777         creation_date,
5778         line_num,
5779         shipment_num,
5780         distribution_num,
5781         sequence,
5782         text_line,
5783                 message_name)
5784      SELECT p_online_report_id,
5785             p_login_id,
5786             p_user_id,
5787             sysdate,
5788             p_user_id,
5789             sysdate,
5790             POL.line_num,
5791             PLL.shipment_num,
5792             0,
5793             p_sequence + ROWNUM,
5794             substr(g_linemsg||g_delim||POL.line_num||g_delim||
5795                    g_shipmsg||g_delim||PLL.shipment_num||g_delim||l_textline,1,240),
5796             'PO_SUB_SHIP_NO_DIST'
5797      FROM   PO_LINE_LOCATIONS_GT PLL,PO_LINES_GT POL
5798      WHERE  PLL.po_line_id   = POL.po_line_id AND
5799             PLL.po_header_id = p_document_id AND
5800             nvl(PLL.cancel_flag, 'N')  = 'N' AND
5801             nvl(POL.cancel_flag, 'N')  = 'N' AND
5802             nvl(PLL.closed_code, 'OPEN') <> 'FINALLY CLOSED' AND
5803             PLL.shipment_type in ('STANDARD', 'PLANNED', 'PREPAYMENT') --<Complex Work R12>
5804             AND NOT EXISTS (SELECT 'Distribution Exists'
5805                             FROM   PO_DISTRIBUTIONS_GT POD
5806                             WHERE  POD.line_location_id = PLL.line_location_id);
5807 
5808     --Increment the p_sequence with number of errors reported in last query
5809     p_sequence := p_sequence + SQL%ROWCOUNT;
5810 -----------------------------------------------
5811 
5812 l_progress := '004';
5813 IF g_debug_stmt THEN
5814    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5815      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
5816           || l_progress,'PO 4/5: Qty/Amt rollup checks');
5817    END IF;
5818 END IF;
5819 
5820     -- Check 4: Quantities/Amounts between Purchase Order Line and Shipments
5821     -- must match.
5822     -- Check 5: The sum of the distribution quantities/amounts should match the
5823     -- shipment quantity/amount.
5824 
5825     --<Complex Work R12>: moved the rollup checks into a separate
5826     -- subprocedure call
5827     check_po_qty_amt_rollup(
5828        p_online_report_id => p_online_report_id
5829     ,  p_document_id => p_document_id
5830     ,  p_login_id => p_login_id
5831     ,  p_user_id => p_user_id
5832     ,  x_sequence => p_sequence --in out param
5833     );
5834 
5835 -----------------------------------------------
5836 l_progress := '006';
5837 IF g_debug_stmt THEN
5838    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5839      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
5840           || l_progress,'PO 6: Dist rate NULL' );
5841    END IF;
5842 END IF;
5843 
5844   -- Check 6:  The rate cannot be NULL for the distribution if we are
5845     -- using a foreign currency.  We are using a foreign currency
5846     -- if the po header currency code is not the same as the sets of
5847     -- books currency code.
5848     -- PO_SUB_DIST_RATE_NULL
5849 
5850   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_DIST_RATE_NULL');
5851   INSERT INTO po_online_report_text_gt(online_report_id,
5852         last_update_login,
5853         last_updated_by,
5854         last_update_date,
5855         created_by,
5856         creation_date,
5857         line_num,
5858         shipment_num,
5859         distribution_num,
5860         sequence,
5861         text_line,
5862                 message_name)
5863   SELECT  p_online_report_id,
5864         p_login_id,
5865         p_user_id,
5866           sysdate,
5867         p_user_id,
5868         sysdate,
5869             POL.line_num,
5870             PLL.shipment_num,
5871             POD.distribution_num,
5872         p_sequence + ROWNUM,
5873           substr(g_linemsg||g_delim||POL.line_num||g_delim
5874                    ||g_shipmsg||g_delim||PLL.shipment_num||g_delim
5875                    ||g_distmsg||g_delim||POD.distribution_num||g_delim
5876                    ||l_textline,1,240),
5877             'PO_SUB_DIST_RATE_NULL'
5878     FROM PO_DISTRIBUTIONS_GT POD,PO_LINE_LOCATIONS_GT PLL,PO_LINES_GT POL,
5879          PO_HEADERS_GT POH,GL_SETS_OF_BOOKS SOB,FINANCIALS_SYSTEM_PARAMETERS FSP
5880     WHERE POD.po_header_id = POH.po_header_id
5881     AND POD.line_location_id = PLL.line_location_id
5882     AND PLL.po_line_id = POL.po_line_id
5883     AND POH.po_header_id = p_document_id
5884     AND nvl(PLL.cancel_flag,'N') = 'N'
5885     AND nvl(POL.cancel_flag,'N') = 'N'
5886     AND nvl(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
5887     AND PLL.shipment_type in ('STANDARD', 'PLANNED')
5888     AND SOB.set_of_books_id = FSP.set_of_books_id
5889     AND SOB.currency_code <> POH.currency_code
5890     AND (POD.rate is null
5891         OR (POH.rate_type <> 'User'
5892             AND POD.rate_date is null));
5893 
5894 
5895      --Increment the p_sequence with number of errors reported in last query
5896     p_sequence := p_sequence + SQL%ROWCOUNT;
5897 --------------------------------------------
5898 
5899 l_progress := '007';
5900 IF g_debug_stmt THEN
5901    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5902      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
5903           || l_progress,'PO 7: Dist rate NOT NULL');
5904    END IF;
5905 END IF;
5906 
5907   -- Check 7: If using functional currency then rate has to be null.
5908     -- PO_SUB_DIST_RATE_NOT_NULL
5909   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_DIST_RATE_NOT_NULL');
5910   INSERT INTO po_online_report_text_gt(online_report_id,
5911         last_update_login,
5912         last_updated_by,
5913         last_update_date,
5914         created_by,
5915         creation_date,
5916         line_num,
5917         shipment_num,
5918         distribution_num,
5919         sequence,
5920         text_line,
5921                 message_name)
5922   SELECT  p_online_report_id,
5923         p_login_id,
5924         p_user_id,
5925           sysdate,
5926         p_user_id,
5927         sysdate,
5928             POL.line_num,
5929             PLL.shipment_num,
5930             POD.distribution_num,
5931         p_sequence + ROWNUM,
5932           substr(g_linemsg||g_delim||POL.line_num||g_delim
5933                    ||g_shipmsg||g_delim||PLL.shipment_num||g_delim
5934                    ||g_distmsg||g_delim||POD.distribution_num||g_delim
5935                    ||l_textline,1,240),
5936             'PO_SUB_DIST_RATE_NOT_NULL'
5937     FROM PO_DISTRIBUTIONS_GT POD,PO_LINE_LOCATIONS_GT PLL,PO_LINES_GT POL,
5938          PO_HEADERS_GT POH,GL_SETS_OF_BOOKS SOB,FINANCIALS_SYSTEM_PARAMETERS FSP
5939     WHERE POD.po_header_id = POH.po_header_id
5940     AND POD.line_location_id = PLL.line_location_id
5941     AND PLL.po_line_id = POL.po_line_id
5942     AND POH.po_header_id = p_document_id
5943     AND nvl(PLL.cancel_flag,'N') = 'N'
5944     AND nvl(POL.cancel_flag,'N') = 'N'
5945     AND nvl(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
5946     AND PLL.shipment_type in ('STANDARD', 'PLANNED')
5947     AND SOB.set_of_books_id = FSP.set_of_books_id
5948     AND SOB.currency_code = POH.currency_code
5949     AND POD.rate is not null;
5950 
5951      --Increment the p_sequence with number of errors reported in last query
5952     p_sequence := p_sequence + SQL%ROWCOUNT;
5953 ----------------------------------------------------
5954 
5955 l_progress := '008';
5956 IF g_debug_stmt THEN
5957    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5958      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
5959           || l_progress,'PO 8: Contract Amount exceed check');
5960    END IF;
5961 END IF;
5962 
5963   -- Check 8:  The amount of all standard purchase orders
5964     -- for a contract should not exceed the amount limit of the contract.
5965     -- PO_SUB_STD_CONTRACT_AMT_LIMIT
5966 
5967     -- bug3673292
5968     -- Take out val_contract_amount procedure call from the query.
5969     -- By doing that we can be sure that we call insert statement only when
5970     -- contract limit is exceeded.
5971 
5972     l_val_contract_limit := PO_CONTRACTS_S.val_contract_amount
5973                             ( x_po_header_id => p_document_id
5974                             );
5975 
5976     IF (l_val_contract_limit = 0) THEN
5977 
5978   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_STD_CONTRACT_AMT_LIMIT');
5979         INSERT INTO po_online_report_text_gt(online_report_id,
5980         last_update_login,
5981         last_updated_by,
5982         last_update_date,
5983         created_by,
5984         creation_date,
5985         line_num,
5986         shipment_num,
5987         distribution_num,
5988         sequence,
5989         text_line,
5990                 message_name)
5991   SELECT  p_online_report_id,
5992         p_login_id,
5993         p_user_id,
5994           sysdate,
5995         p_user_id,
5996         sysdate,
5997         0,
5998         0,
5999         0,
6000         p_sequence + ROWNUM,
6001         substr(l_textline,1,240),
6002             'PO_SUB_STD_CONTRACT_AMT_LIMIT'
6003         FROM DUAL;
6004 
6005         --Increment the p_sequence with number of errors reported in last query
6006         p_sequence := p_sequence + SQL%ROWCOUNT;
6007 
6008     END IF;
6009 
6010 
6011 
6012 -------------------------------------------------------------------------------
6013 
6014 l_progress := '009';
6015 IF g_debug_stmt THEN
6016    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6017      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
6018           || l_progress,'PO 9: Ref to unapproved contract check ');
6019    END IF;
6020 END IF;
6021 
6022   -- Check 9: Any of the standard PO's lines should not references an
6023     -- unapproved contract.
6024     -- PO_SUB_REF_UNAPPROVED_CONTRACT
6025     --< Bug 3422733 > Only do this check if Contract is not ON HOLD. The ON HOLD
6026     -- check is done later. Avoids showing 2 msgs for Contract that is ON HOLD.
6027 
6028   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REF_UNAPPROVED_CONTRACT');
6029   INSERT INTO po_online_report_text_gt(online_report_id,
6030         last_update_login,
6031         last_updated_by,
6032         last_update_date,
6033         created_by,
6034         creation_date,
6035         line_num,
6036         shipment_num,
6037         distribution_num,
6038         sequence,
6039         text_line,
6040                 message_name)
6041   SELECT  p_online_report_id,
6042         p_login_id,
6043         p_user_id,
6044           sysdate,
6045         p_user_id,
6046         sysdate,
6047         POL.line_num,
6048         0,
6049         0,
6050         p_sequence + ROWNUM,
6051         substr(g_linemsg||g_delim||POL.line_num||g_delim||l_textline,1,240),
6052             'PO_SUB_REF_UNAPPROVED_CONTRACT'
6053     FROM PO_LINES_GT POL,
6054          PO_HEADERS_ALL POC   -- <GC FPJ> : use all table
6055     WHERE POL.po_header_id = p_document_id
6056     AND   POL.contract_id = POC.po_header_id    -- <GC FPJ>
6057     AND   POC.type_lookup_code = 'CONTRACT'
6058   /* R12 GCPA
6059   + If Profile ALLOW_REFERENCING_CPA_UNDER_AMENDMENT is Y, then we can refer any Contract Which is approved Once
6060   + Else Contract should be in APPROVED state  */
6061   AND    ( (NVL(FND_PROFILE.VALUE('ALLOW_REFERENCING_CPA_UNDER_AMENDMENT'),'N') = 'Y'
6062            AND POC.Approved_Date Is Null
6063 	    )
6064 	 or (  NVL(FND_PROFILE.VALUE('ALLOW_REFERENCING_CPA_UNDER_AMENDMENT'),'N') = 'N'
6065           and nvl(POC.APPROVED_FLAG,'N') <> 'Y')
6066 	 )
6067     AND   NVL(POC.user_hold_flag, 'N') <> 'Y';      --< Bug 3422733 >
6068 
6069 
6070     --Increment the p_sequence with number of errors reported in last query
6071     p_sequence := p_sequence + SQL%ROWCOUNT;
6072 -------------------------------------------------------------------------------
6073 
6074 l_progress := '010';
6075 IF g_debug_stmt THEN
6076    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6077      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
6078           || l_progress,'PO 10: Ref contract diff vendor');
6079    END IF;
6080 END IF;
6081 
6082   -- Check 10: Any of the standard PO's lines should not reference a
6083     -- contract whose vendor is different than the one on PO header.
6084     -- PO_SUB_LINE_CONTRACT_MISMATCH
6085 
6086   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_LINE_CONTRACT_MISMATCH');
6087   INSERT INTO po_online_report_text_gt(online_report_id,
6088         last_update_login,
6089         last_updated_by,
6090         last_update_date,
6091         created_by,
6092         creation_date,
6093         line_num,
6094         shipment_num,
6095         distribution_num,
6096         sequence,
6097         text_line,
6098                 message_name)
6099   SELECT  p_online_report_id,
6100         p_login_id,
6101         p_user_id,
6102           sysdate,
6103         p_user_id,
6104         sysdate,
6105         POL.line_num,
6106         0,
6107         0,
6108         p_sequence + ROWNUM,
6109         substr(g_linemsg||g_delim||POL.line_num||g_delim||l_textline,1,240),
6110             'PO_SUB_LINE_CONTRACT_MISMATCH'
6111     FROM PO_LINES_GT POL,
6112          PO_HEADERS_ALL POC,  -- <GC FPJ>: Use _ALL table
6113          PO_HEADERS_GT POH
6114     WHERE POH.po_header_id = p_document_id
6115     AND   POL.po_header_id = POH.po_header_id
6116     AND   POL.contract_id = POC.po_header_id  -- <GC FPJ>
6117     AND   POC.type_lookup_code = 'CONTRACT'
6118     AND   nvl(POC.cancel_flag , 'N') = 'N'
6119     AND   POC.vendor_id <> POH.vendor_id;
6120 
6121      --Increment the p_sequence with number of errors reported in last query
6122     p_sequence := p_sequence + SQL%ROWCOUNT;
6123 -----------------------------------
6124 
6125 l_progress := '600';
6126 
6127 -- Check 11: The PO GL date should be within an open purchasing period
6128 -- PO_SUB_PO_INVALID_GL_DATE
6129 
6130 --<FPJ ENCUMBRANCE>
6131 
6132 IF (  PO_CORE_S.is_encumbrance_on(
6133          p_doc_type => g_document_type_PO
6134       ,  p_org_id => NULL
6135       )
6136    )
6137 THEN
6138 
6139    l_progress := '610';
6140    IF g_debug_stmt THEN
6141       PO_DEBUG.debug_stmt(g_log_head || '.'||l_api_name||'.',
6142                           l_progress,'PO 11: GL date');
6143    END IF;
6144 
6145    check_gl_date(
6146       p_doc_type => g_document_type_PO
6147    ,  p_online_report_id => p_online_report_id
6148    ,  p_login_id => p_login_id
6149    ,  p_user_id => p_user_id
6150    ,  p_sequence => p_sequence
6151    );
6152 
6153    l_progress := '620';
6154 
6155 ELSE
6156    l_progress := '630';
6157    IF g_debug_stmt THEN
6158       PO_DEBUG.debug_stmt(g_log_head || '.'||l_api_name||'.',
6159                           l_progress,'PO 11: PO encumbrance not on');
6160    END IF;
6161 END IF;
6162 
6163 ---------------------------------------
6164 
6165 l_progress := '012';
6166 IF g_debug_stmt THEN
6167    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6168      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
6169           || l_progress,'PO 12: UOM Interclass coversions');
6170    END IF;
6171 END IF;
6172 
6173     -- Check 12: Invalid Interclass conversions between UOMs should not be allowed
6174     -- PO_SUB_UOM_CLASS_CONVERSION, PO_SUB_PO_INVALID_CLASS_CONV
6175     -- Message inserted is:
6176     --'Line# <LineNum> Following Interclass UOM conversion is not defined or
6177     -- is disabled <UOM1> <UOM2>'
6178     -- Bug #1630662
6179   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_UOM_CLASS_CONVERSION');
6180     INSERT INTO po_online_report_text_gt(online_report_id,
6181         last_update_login,
6182         last_updated_by,
6183         last_update_date,
6184         created_by,
6185         creation_date,
6186         line_num,
6187         shipment_num,
6188         distribution_num,
6189         sequence,
6190         text_line,
6191                 message_name)
6192   SELECT  /*+ FULL(POL) */  -- bug3413891
6193                  p_online_report_id,
6194         p_login_id,
6195         p_user_id,
6196           sysdate,
6197         p_user_id,
6198         sysdate,
6199             POL.line_num,
6200             0,
6201             0,
6202             p_sequence + ROWNUM,
6203             substr(g_linemsg||g_delim||POL.line_num||g_delim||l_textline||
6204                    MTL1.uom_class||' , '||MTL2.uom_class,1,240),
6205             'PO_SUB_UOM_CLASS_CONVERSION'
6206     FROM MTL_UOM_CLASS_CONVERSIONS MOU, PO_LINES_GT POL,
6207          MTL_UOM_CLASSES_TL MTL1, MTL_UOM_CLASSES_TL MTL2
6208     WHERE POL.item_id = MOU.inventory_item_id
6209     AND   (nvl(MOU.disable_date, TRUNC(SYSDATE))+1) < TRUNC(SYSDATE)
6210     AND   POL.po_header_id = p_document_id
6211     AND   MOU.from_uom_class = MTL1.uom_class
6212     AND   MOU.to_uom_class = MTL2.uom_class
6213     AND EXISTS
6214        (SELECT 'uom conversion exists'
6215         FROM MTL_UNITS_OF_MEASURE MUM
6216         WHERE POL.unit_meas_lookup_code = MUM.unit_of_measure
6217         AND   MOU.to_uom_class = MUM.uom_class);
6218 
6219     --Increment the p_sequence with number of errors reported in last query
6220     p_sequence := p_sequence + SQL%ROWCOUNT;
6221 --------------------------------------------------
6222 
6223 l_progress := '013';
6224 IF g_debug_stmt THEN
6225    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6226      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
6227           || l_progress,'PO 13: Item restricyed');
6228    END IF;
6229 END IF;
6230 
6231   -- Check 13: If an item is restricted then the Purchase Order Vendor
6232     -- must be listed in the Approved Suppliers List table and must be approved.
6233     -- PO_SUB_ITEM_NOT_APPROVED
6234     -- History:
6235     /*
6236     ** Determine if an item is restricted.  If it is restricted the
6237     ** Purchase Order Vendor must be listed in the Approved Suppliers
6238     ** List table and must be approved.
6239     ** Bug 918932 sugupta
6240     ** Submission check for Blanket PO will follow the following ASL rules when
6241     **Use approved supplier flag is checked ON:
6242     ** 1) Global ASL's  with non-debarred supplier will allow approval all times.
6243     ** 2) Global ASL's with debarred supplier will disallow approval at all times.
6244     ** 3) If Global ASL is absent, any Local ASL with approved supplier will allow
6245     ** approval
6246     ** 4) If Global ASL is absent and all local ASL's have debarred supplier, approval
6247     **will fail.
6248     */
6249     /* Bug# 1109001: kagarwal
6250     ** Split the ASL check to ASL check for Blanket and PO.
6251     ** Also modified the query for performance.
6252     ** UNION is replaced with UNION ALL in all ASL checks
6253     ** In PO_SUB_ITEM_NOT_APPROVED the Sum clause has also been modified
6254     ** PO_SUB_ITEM_NOT_APPROVED for Standard PO
6255     ** PA_SUB_ITEM_NOT_APPROVED for Blanket/Contract PO
6256     */
6257     /* Bug# 1570115:          While checking for Restricted items the query was
6258     ** checking with FSP.inventory_organization_id to instead of checking with the
6259     ** PLL.SHIP_TO_ORGANIZATION_ID and not allowing the user to approve the
6260     ** document though it is not restricted in the ship to org which user has
6261     ** entered.  Changed the FSP.inventory_organization_id to
6262     ** PLL.SHIP_TO_ORGANIZATION_ID the query which check for PO.
6263     */
6264     /* Bug# 1761513: kagarwal
6265     ** Desc: Changed the ASL Rules:
6266     ** 1. If the ASL is defined at the commodity level and also at the item level,
6267     ** the item level ASL will be considered. But if the ASL is not defined at the
6268     ** item level then the ASL at the commodity level will be considered for that
6269     ** item.
6270     **
6271     ** Eg i): If A, B and C are approved suppliers at the commodity level and A and D are
6272     ** the approved suppliera at the item level, then A and D will be considered as
6273     ** approved suppliers for this item. Hence a PO for this item will get approved
6274     ** for Suppliers A and D but not for suppliers B and C.
6275     **
6276     ** Eg ii): If A, B and C are approved suppliers at the commodity level and there is
6277     ** no ASL defined at the item level, then A, B and C will be considered as approved
6278     ** supplier for this item. Hence a PO for this item will get approved for
6279     ** Suppliers A, B and C.
6280     **
6281     ** 2. If a Supplier is debarred at the commodity level even if it is approved
6282     ** supplier at the item level then this supplier will be considered debarred for
6283     ** this item.
6284     */
6285      /*Bug5597639 Modifying the below sql to ensure that whenever the item
6286      is restricted by checking 'Use approved supplier list' there should be
6287      atlease one approved ASL either at item level or at category level
6288      if there is no item ASL*/
6289 
6290   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_ITEM_NOT_APPROVED');
6291   INSERT INTO po_online_report_text_gt(online_report_id,
6292         last_update_login,
6293         last_updated_by,
6294         last_update_date,
6295         created_by,
6296         creation_date,
6297         line_num,
6298         shipment_num,
6299         distribution_num,
6300         sequence,
6301         text_line,
6302                 message_name)
6303   SELECT  p_online_report_id,
6304         p_login_id,
6305         p_user_id,
6306           sysdate,
6307         p_user_id,
6308         sysdate,
6309         POL.line_num,
6310         PLL.shipment_num,
6311         0,
6312         p_sequence + ROWNUM,
6313         substr(g_linemsg||g_delim||POL.line_num||g_delim||
6314                    g_shipmsg||g_delim||PLL.shipment_num||g_delim
6315                    ||l_textline,1,240),
6316             'PO_SUB_ITEM_NOT_APPROVED'
6317     FROM MTL_SYSTEM_ITEMS MSI, PO_LINE_LOCATIONS_GT PLL,
6318          PO_LINES_GT POL, PO_HEADERS_GT POH,
6319          FINANCIALS_SYSTEM_PARAMETERS FSP
6320     WHERE POH.po_header_id = p_document_id
6321     AND POH.po_header_id = POL.po_header_id
6322     AND PLL.po_line_id(+) = POL.po_line_id
6323     AND PLL.po_release_id IS NULL
6324     AND MSI.organization_id = PLL.ship_to_organization_id
6325     AND MSI.inventory_item_id = POL.item_id
6326     AND POL.item_id is not null
6327     AND nvl(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
6328     AND nvl(POL.cancel_flag,'N') = 'N'
6329     AND nvl(PLL.cancel_flag,'N') = 'N'
6330     AND nvl(MSI.must_use_approved_vendor_flag,'N') = 'Y'
6331     AND NOT exists
6332       (SELECT 1
6333         FROM PO_APPROVED_SUPPLIER_LIS_VAL_V ASL, PO_ASL_STATUS_RULES ASR
6334         WHERE  ASL.using_organization_id in (PLL.ship_to_organization_id, -1)
6335         AND    ASL.vendor_id = POH.vendor_id
6336         AND    nvl(ASL.vendor_site_id, POH.vendor_site_id) = POH.vendor_site_id
6337         AND  ASL.item_id = POL.item_id
6338         AND    ASL.asl_status_id = ASR.status_id
6339         AND    ASR.business_rule = '1_PO_APPROVAL'
6340 	AND   ASR.allow_action_flag = 'Y'    --Bug5597639
6341         UNION ALL
6342        SELECT  1
6343         FROM PO_APPROVED_SUPPLIER_LIS_VAL_V ASL, PO_ASL_STATUS_RULES ASR
6344         WHERE  ASL.using_organization_id in (PLL.ship_to_organization_id, -1)
6345         AND    ASL.vendor_id = POH.vendor_id
6346         AND    nvl(ASL.vendor_site_id, POH.vendor_site_id) = POH.vendor_site_id
6347         AND    ASL.item_id is NULL
6348         AND    not exists
6349            (SELECT ASL1.ASL_ID
6350             FROM PO_APPROVED_SUPPLIER_LIS_VAL_V ASL1
6351             WHERE ASL1.ITEM_ID = POL.item_id
6352             AND ASL1.using_organization_id in
6353                 (PLL.ship_to_organization_id, -1))
6354         AND    ASL.category_id in
6355            (SELECT MIC.category_id
6356             FROM   MTL_ITEM_CATEGORIES MIC
6357             WHERE MIC.inventory_item_id = POL.item_id
6358             AND MIC.organization_id = PLL.ship_to_organization_id)
6359         AND    ASL.asl_status_id = ASR.status_id
6360         AND    ASR.business_rule = '1_PO_APPROVAL'
6361         AND   ASR.allow_action_flag = 'Y');    --Bug5597639
6362 
6363      --Increment the p_sequence with number of errors reported in last query
6364     p_sequence := p_sequence + SQL%ROWCOUNT;
6365 
6366 -------------------------------------------------------------------------------
6367 
6368 l_progress := '014';
6369 IF g_debug_stmt THEN
6370    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6371      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
6372           || l_progress,'PO 14: ASL Debarred');
6373    END IF;
6374 END IF;
6375 
6376     -- Check 14: If an item is restricted then the Purchase Order Vendor
6377     -- must be listed in the Approved Suppliers List table and must not be
6378     -- DEBARRED.
6379     -- PO_SUB_ITEM_ASL_DEBARRED
6380 
6381    /*Bug5597639 This check would throw an error message if atleast one ASL
6382     entry is debarred either for item /Category irrespective of 'Use approved
6383     supplier flag'. This check would apply even for one time items.
6384     If supplier is debarred in any of the ASL item/category (Global/Local)
6385     (Suplier/Supplier+site) then the approval of the PO will not be allowed */
6386 
6387 l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_ITEM_ASL_DEBARRED');
6388   INSERT INTO po_online_report_text_gt(online_report_id,
6389         last_update_login,
6390         last_updated_by,
6391         last_update_date,
6392         created_by,
6393         creation_date,
6394         line_num,
6395         shipment_num,
6396         distribution_num,
6397         sequence,
6398         text_line,
6399                 message_name)
6400   SELECT  p_online_report_id,
6401         p_login_id,
6402         p_user_id,
6403           sysdate,
6404         p_user_id,
6405         sysdate,
6406         POL.line_num,
6407         PLL.shipment_num,
6408         0,
6409         p_sequence + ROWNUM,
6410         substr(g_linemsg||g_delim||POL.line_num||g_delim
6411                    ||g_shipmsg||g_delim||PLL.shipment_num||g_delim
6412                    ||l_textline,1,240),
6413             'PO_SUB_ITEM_ASL_DEBARRED'
6414      FROM PO_LINE_LOCATIONS_GT PLL,
6415          PO_LINES_GT POL, PO_HEADERS_GT POH,
6416          FINANCIALS_SYSTEM_PARAMETERS FSP
6417     WHERE POH.po_header_id = p_document_id
6418     AND POH.po_header_id = POL.po_header_id
6419     AND PLL.po_line_id(+) = POL.po_line_id
6420     AND PLL.po_release_id IS NULL
6421     AND nvl(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
6422     AND nvl(POL.cancel_flag,'N') = 'N'
6423     AND nvl(PLL.cancel_flag,'N') = 'N'
6424     AND exists
6425        (SELECT 1
6426         FROM PO_APPROVED_SUPPLIER_LIS_VAL_V ASL, PO_ASL_STATUS_RULES ASR,
6427 	MTL_SYSTEM_ITEMS MSI  --Bug5597639
6428         WHERE  ASL.using_organization_id in (PLL.ship_to_organization_id, -1)
6429 	/*Bug5597639 Adding the below three conditions */
6430 	AND MSI.organization_id = FSP.inventory_organization_id
6431 	AND MSI.inventory_item_id = POL.item_id
6432 	AND POL.item_id is not null
6433         AND    ASL.vendor_id = POH.vendor_id
6434         AND    nvl(ASL.vendor_site_id, POH.vendor_site_id) = POH.vendor_site_id
6435         AND  ASL.item_id = POL.item_id
6436         AND    ASL.asl_status_id = ASR.status_id
6437         AND    ASR.business_rule = '1_PO_APPROVAL'
6438         AND    ASR.allow_action_flag <> 'Y' -- Bug 5724696
6439         UNION ALL
6440         SELECT 1
6441         FROM PO_APPROVED_SUPPLIER_LIS_VAL_V ASL, PO_ASL_STATUS_RULES ASR
6442         WHERE  ASL.using_organization_id in (PLL.ship_to_organization_id, -1)
6443         AND    ASL.vendor_id = POH.vendor_id
6444         AND    nvl(ASL.vendor_site_id, POH.vendor_site_id) = POH.vendor_site_id
6445         AND    ASL.item_id is NULL
6446 	AND  POL.category_id = ASL.category_id  --Bug5597639
6447         AND    ASL.asl_status_id = ASR.status_id
6448         AND    ASR.business_rule = '1_PO_APPROVAL'
6449         AND    ASR.allow_action_flag <> 'Y' );  --Bug5597639
6450 
6451      --Increment the p_sequence with number of errors reported in last query
6452     p_sequence := p_sequence + SQL%ROWCOUNT;
6453 
6454 
6455 ----------------------------------------------------------------------------
6456 
6457 -- <GC FPJ START>
6458 
6459     l_progress := '015';
6460 
6461     IF g_debug_stmt THEN
6462         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6463           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
6464                        l_api_name||'.' || l_progress,
6465                        'PO 15: Contract ref on hold');
6466         END IF;
6467     END IF;
6468 
6469     -- Check 15: Contract referenced on a PO line should not be on hold
6470     --           (If contract is in incomplete status)
6471 
6472     l_textline := FND_MESSAGE.get_string('PO', 'PO_SUB_LINE_CONTRACT_HOLD');
6473 
6474     INSERT INTO po_online_report_text_gt(
6475        online_report_id,
6476        last_update_login,
6477        last_updated_by,
6478        last_update_date,
6479        created_by,
6480        creation_date,
6481        line_num,
6482        shipment_num,
6483        distribution_num,
6484        sequence,
6485        text_line,
6486        message_name
6487     )
6488     SELECT p_online_report_id,
6489            p_login_id,
6490            p_user_id,
6491            sysdate,
6492            p_user_id,
6493            sysdate,
6494            POL.line_num,
6495            0,
6496            0,
6497            p_sequence + ROWNUM,
6498            SUBSTR (g_linemsg || g_delim || POL.line_num ||g_delim ||
6499                    l_textline,1,240),
6500            'PO_SUB_LINE_CONTRACT_HOLD'
6501     FROM   po_lines_gt POL,
6502            po_headers_gt POH,
6503            po_headers_all POC
6504     WHERE  POH.po_header_id = p_document_id
6505     AND    NVL(POH.authorization_status, 'INCOMPLETE') = 'INCOMPLETE'
6506     AND    POL.po_header_id = POH.po_header_id
6507     AND    POC.po_header_id = POL.contract_id
6508     AND    POC.user_hold_flag = 'Y';
6509 
6510     p_sequence := p_sequence + SQL%ROWCOUNT;
6511 
6512 -----------------------------------------------------------------------------
6513 --Bug 5525381
6514 /* Commented the validation of checking for the contract start date.
6515     l_progress := '016';
6516 
6517     IF g_debug_stmt THEN
6518         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6519           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
6520                        l_api_name||'.' || l_progress,
6521                        'PO 16: Contract ref effective dates');
6522         END IF;
6523     END IF;
6524 
6525     -- Check 16: Creation date of the contract should be within the effective
6526     --           dates of the contracts referenced
6527 
6528     l_textline := FND_MESSAGE.get_string('PO', 'PO_SUB_LINE_CONTRACT_EXP');
6529 
6530     INSERT INTO po_online_report_text_gt(
6531        online_report_id,
6532        last_update_login,
6533        last_updated_by,
6534        last_update_date,
6535        created_by,
6536        creation_date,
6537        line_num,
6538        shipment_num,
6539        distribution_num,
6540        sequence,
6541        text_line,
6542        message_name
6543     )
6544     SELECT p_online_report_id,
6545            p_login_id,
6546            p_user_id,
6547            sysdate,
6548            p_user_id,
6549            sysdate,
6550            POL.line_num,
6551            0,
6552            0,
6553            p_sequence + ROWNUM,
6554            SUBSTR (g_linemsg || g_delim || POL.line_num ||g_delim ||
6555                    l_textline,1,240),
6556            'PO_SUB_LINE_CONTRACT_EXP'
6557     FROM   po_lines_gt POL,
6558            po_headers_gt POH,
6559            po_headers_all POC
6560     WHERE  POH.po_header_id = p_document_id
6561     AND    NVL(POH.authorization_status, 'INCOMPLETE') = 'INCOMPLETE'
6562     AND    POL.po_header_id = POH.po_header_id
6563     AND    POC.po_header_id = POL.contract_id
6564     AND    TRUNC(POL.creation_date) NOT BETWEEN NVL(TRUNC(POC.start_date),
6565                                                     POL.creation_date-1)
6566                                         AND     NVL(TRUNC(POC.end_date),
6567                                                     POL.creation_date+1);
6568 
6569     p_sequence := p_sequence + SQL%ROWCOUNT;*/
6570 
6571 -----------------------------------------------------------------------------
6572  l_progress := '017';
6573 
6574     IF g_debug_stmt THEN
6575         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6576           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
6577                        l_api_name||'.' || l_progress,
6578                        'PO 17: ATO/CTO Model items not allowed on POs');
6579         END IF;
6580     END IF;
6581 
6582     -- Check 17: ATO/CTO Model items not allowed on PO's (Bug 3362369)
6583 
6584     l_textline := FND_MESSAGE.get_string('PO', 'PO_ATO_ITEM_NA');
6585 
6586     INSERT INTO po_online_report_text_gt(
6587        online_report_id,
6588        last_update_login,
6589        last_updated_by,
6590        last_update_date,
6591        created_by,
6592        creation_date,
6593        line_num,
6594        shipment_num,
6595        distribution_num,
6596        sequence,
6597        text_line,
6598        message_name
6599     )
6600     SELECT p_online_report_id,
6601            p_login_id,
6602            p_user_id,
6603            sysdate,
6604            p_user_id,
6605            sysdate,
6606            POL.line_num,
6607            0,
6608            0,
6609            p_sequence + ROWNUM,
6610            SUBSTR (g_linemsg || g_delim || POL.line_num ||g_delim ||
6611                    l_textline,1,240),
6612            'PO_ATO_ITEM_NA'
6613     FROM   po_lines_gt POL,
6614            po_headers_gt POH,
6615            financials_system_parameters FSP,
6616            mtl_system_items MSI
6617     WHERE  POH.po_header_id = p_document_id
6618     AND    POL.po_header_id = POH.po_header_id
6619     AND    POL.item_id is not null
6620     AND    nvl(POL.cancel_flag, 'N') = 'N'                   --5353423
6621     AND    nvl(POL.closed_code, 'OPEN') <> 'FINALLY CLOSED'  --5353423
6622     AND    POL.item_id = MSI.inventory_item_id
6623     AND    MSI.organization_id = FSP.inventory_organization_id
6624     AND    MSI.bom_item_type in (1,2);
6625 
6626     p_sequence := p_sequence + SQL%ROWCOUNT;
6627 
6628 -----------------------------------------------------------------------------
6629 
6630 
6631 /*Start Bug #3512688 */
6632       /* Check 18 : To check the validity of the item at line level for newly added  line */
6633       l_progress := '018';
6634       IF g_debug_stmt  THEN
6635       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6636         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
6637       l_api_name||'.' || l_progress,
6638       'PO 18: Non Purchasable Item is not allowed');
6639       END IF;
6640       END IF;
6641       l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_ALL_NO_ITEM');
6642       INSERT INTO po_online_report_text_gt(online_report_id,
6643       last_update_login,
6644       last_updated_by,
6645       last_update_date,
6646       created_by,
6647       creation_date,
6648       line_num,
6649       shipment_num,
6650       distribution_num,
6651       sequence,
6652       text_line,
6653       message_name)
6654       SELECT  p_online_report_id,
6655       p_login_id,
6656       p_user_id,
6657 
6658      sysdate,
6659       p_user_id,
6660       sysdate,
6661       pl.line_num,
6662       0,
6663       0,
6664       p_sequence + ROWNUM,
6665        substr(g_linemsg||g_delim||pl.line_num||g_delim||l_textline,1,240),
6666       'PO_ALL_NO_ITEM'
6667       from po_headers_gt ph, po_lines_gt pl,mtl_system_items  itm,financials_system_parameters fsp,po_line_types_b plt
6668       where itm.inventory_item_id  = pl.item_id
6669       and   pl.item_id is not null
6670       and   itm.organization_id    = fsp.inventory_organization_id
6671       and   itm.purchasing_enabled_flag = 'N'
6672       and   ph.po_header_id = p_document_id
6673       and   pl.po_header_id = ph.po_header_id
6674       and   pl.line_type_id = plt.line_type_id
6675       and   nvl(plt.outside_operation_flag,'N') =  nvl(itm.outside_operation_flag,'N')
6676       and   (pl.creation_date >= nvl(ph.approved_date ,pl.creation_date));
6677       --Increment the p_sequence with number of errors reported in last query
6678       p_sequence := p_sequence + SQL%ROWCOUNT;
6679 
6680       -----------------------5601    /* Check 19 : To check the validity of the item at shipment level for newly added  line */
6681       l_progress := '019';
6682       IF g_debug_stmt THEN
6683       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6684         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
6685      l_api_name||'.' || l_progress,
6686       'PO 19: Non Purchasable Item is not allowed');     END IF;
6687       END IF;
6688       l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_ALL_NO_ITEM');
6689       INSERT INTO po_online_report_text_gt(online_report_id,
6690       last_update_login,
6691       last_updated_by,
6692       last_update_date,
6693       created_by,
6694       creation_date,
6695       line_num,
6696       shipment_num,
6697       distribution_num,
6698       sequence,
6699       text_line,
6700       message_name)
6701       SELECT  p_online_report_id,
6702       p_login_id,
6703       p_user_id,
6704       sysdate,
6705       p_user_id,
6706       sysdate,
6707       pl.line_num,
6708       pll.shipment_num,
6709       0,
6710       p_sequence + ROWNUM,
6711       substr(g_linemsg||g_delim||pl.line_num||g_delim||
6712        g_shipmsg||g_delim||pll.shipment_num||g_delim||l_textline,1,240),
6713       'PO_ALL_NO_ITEM'
6714       from po_headers_gt ph,po_lines_gt pl,po_line_locations_gt  pll,mtl_system_items itm,po_line_types_b plt
6715     where itm.inventory_item_id  = pl.item_id
6716     and   pl.item_id is not null
6717       and   itm.organization_id    = pll.ship_to_organization_id
6718       and   itm.purchasing_enabled_flag = 'N'
6719       and   pl.po_line_id   = pll.po_line_id
6720       and   ph.po_header_id = pll.po_header_id
6721       and   ph.po_header_id = p_document_id
6722       and   pl.po_header_id = ph.po_header_id
6723       and   pl.line_type_id = plt.line_type_id
6724       and   nvl(plt.outside_operation_flag,'N') =  nvl(itm.outside_operation_flag,'N')
6725       and   (pl.creation_date >= nvl(ph.approved_date,pl.creation_date))
6726       and   pll.shipment_type <> 'PREPAYMENT' --<Complex Work R12>
6727       and   pll.po_release_id is null;
6728 
6729 
6730       --Increment the p_sequence with number of errors reported in last query
6731       p_sequence := p_sequence + SQL%ROWCOUNT;
6732 
6733 --End Bug #3512688
6734 ------------------------------------------------------------------------------------------
6735 
6736  l_progress := '020';
6737 
6738     IF g_debug_stmt THEN
6739         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6740           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
6741                        l_api_name||'.' || l_progress,
6742                        'PO 20: Either Promised/Need by date required for planned items');
6743         END IF;
6744     END IF;
6745 
6746     -- Check 20: Either Promised Date or Need by date is required for planned
6747     -- items (ECO 4503425 for HTML Orders R12)
6748 
6749     l_textline := FND_MESSAGE.get_string('PO', 'PO_PO_PLANNED_ITEM_DATE_REQ');
6750 
6751     INSERT INTO po_online_report_text_gt(
6752        online_report_id,
6753        last_update_login,
6754        last_updated_by,
6755        last_update_date,
6756        created_by,
6757        creation_date,
6758        line_num,
6759        shipment_num,
6760        distribution_num,
6761        sequence,
6762        text_line,
6763        message_name
6764     )
6765     SELECT p_online_report_id,
6766            p_login_id,
6767            p_user_id,
6768            sysdate,
6769            p_user_id,
6770            sysdate,
6771            POL.line_num,
6772            PLL.shipment_num,
6773            0,
6774            p_sequence + ROWNUM,
6775            SUBSTR (g_linemsg || g_delim || POL.line_num ||g_delim ||
6776              g_shipmsg || g_delim || PLL.shipment_num ||g_delim || l_textline,1,240),
6777            'PO_PO_PLANNED_ITEM_DATE_REQ'
6778     FROM   po_lines_gt POL,
6779            po_headers_gt POH,
6780            po_line_locations_gt PLL,
6781            financials_system_parameters FSP,
6782            mtl_system_items MSI
6783     WHERE  POH.po_header_id = p_document_id
6784     AND    POL.po_header_id = POH.po_header_id
6785     AND    POL.po_line_id = PLL.po_line_id
6786     AND    PLL.po_header_id = POH.po_header_id
6787     AND    POL.item_id is not null
6788     AND    PLL.need_by_date is null
6789     AND    PLL.promised_date is null
6790     AND    PLL.shipment_type <> 'PREPAYMENT' --bug 4997671 <Complex Work R12>
6791     AND    POL.item_id = MSI.inventory_item_id
6792     AND    MSI.organization_id = FSP.inventory_organization_id
6793     AND    (MSI.mrp_planning_code IN (3,4,7,8,9) OR
6794             MSI.inventory_planning_code IN (1,2) );
6795 
6796     p_sequence := p_sequence + SQL%ROWCOUNT;
6797 
6798 
6799 -----------------------------------------------
6800 --Bug5075191
6801 --Following submission check should exclude cancelled/finally closed lines.
6802 --Adding those conditions.
6803 --bug#3987438
6804 --Added a new submission check to validate the
6805 --ship to location at the shipment level.
6806 
6807 IF g_debug_stmt THEN
6808    FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
6809           || l_progress,'PO : Invalid Ship To Locations');
6810 END IF;
6811 
6812     SELECT  pol.line_num,
6813             poll.shipment_num,
6814             hlat.location_code
6815     BULK COLLECT INTO
6816             l_line_num,
6817             l_shipment_num,
6818             l_ship_to_location_tbl
6819     FROM
6820             po_lines_gt pol,
6821             po_line_locations_gt poll,
6822             hr_locations_all hla,
6823             hr_locations_all_tl hlat
6824     WHERE poll.po_header_id=p_document_id
6825     and pol.po_Header_id=p_document_id
6826     and pol.po_line_id = poll.po_line_id
6827     and poll.ship_to_location_id = hla.location_id
6828     and nvl(poll.cancel_flag,'N') = 'N'
6829     and nvl(poll.closed_code,'OPEN') <> 'FINALLY CLOSED'
6830     and hla.location_id = hlat.location_id
6831     and nvl (trunc (hla.inactive_date), trunc (sysdate)+1 )<= trunc (sysdate)
6832     and hlat.language=userenv('lang');
6833 
6834     FOR i IN 1..l_line_num.COUNT LOOP
6835         l_rowCount(i) := i;
6836     END LOOP;
6837 
6838     FORALL i IN 1..l_line_num.COUNT
6839     INSERT INTO po_online_report_text_gt (online_report_id,
6840       last_update_login,
6841       last_updated_by,
6842       last_update_date,
6843       created_by,
6844       creation_date,
6845       line_num,
6846       shipment_num,
6847       distribution_num,
6848       sequence,
6849       text_line,
6850       message_name)
6851     VALUES(
6852             p_online_report_id,
6853             p_login_id,
6854             p_user_id,
6855             sysdate,
6856             p_user_id,
6857             sysdate,
6858             l_line_num(i),
6859             l_shipment_num(i),
6860             NULL,                                             -- <SERVICES FPJ>
6861             p_sequence+l_rowCount(i),
6862             PO_CORE_S.get_translated_text
6863                 (   'PO_SUB_SHIPTO_LOC_INVALID'
6864                 ,   'LINE_NUM', l_line_num(i)
6865                 ,   'SHIPMENT_NUM', l_shipment_num(i)
6866                 ,   'SHIP_TO_LOC', l_ship_to_location_tbl(i)
6867                 ),
6868             'PO_SUB_PO_SHIPTO_LOC_INVALID'
6869         );
6870 
6871     p_sequence := p_sequence + l_line_num.COUNT;
6872 
6873 --bug#3987438 END
6874 
6875 -----------------------------------------------------------------------------
6876 
6877     l_progress := '021';
6878 --<R12 eTax Integration Start>
6879 -----------------------------------------------------------------------------
6880 
6881     l_tax_status := po_tax_interface_pvt.calculate_tax_yes_no(p_po_header_id    => p_document_id,
6882                                                               p_po_release_id   => NULL,
6883                                                               p_req_header_id   => NULL);
6884     l_progress := '022';
6885     IF g_debug_stmt THEN
6886         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6887           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head ||l_api_name||'.' ||
6888                         l_progress, 'PO 21: Recalculate tax before approval = ' || l_tax_status);
6889         END IF;
6890     END IF;
6891 
6892     IF l_tax_status = 'Y' THEN
6893       IF g_debug_stmt THEN
6894           IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6895             FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head ||
6896                          l_api_name||'.' || l_progress,
6897                          'PO 22: Calculate tax as the current one is not correct');
6898           END IF;
6899       END IF;
6900       l_progress := '023';
6901       po_tax_interface_pvt.calculate_tax( x_return_status    => l_return_status,
6902                                           p_po_header_id     => p_document_id,
6903                                           p_po_release_id    => NULL,
6904                                           p_calling_program  => g_action_DOC_SUBMISSION_CHECK);
6905 
6906       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
6907         IF g_debug_stmt THEN
6908              IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6909                FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head ||
6910                             l_api_name||'.' || l_progress,
6911                             'PO 23: Calculate tax has errored out');
6912              END IF;
6913          END IF;
6914          l_tax_message := fnd_message.get_string('PO','PO_TAX_CALCULATION')||' : ' ;
6915          FORALL i IN 1..po_tax_interface_pvt.G_TAX_ERRORS_TBL.MESSAGE_TEXT.COUNT
6916             INSERT INTO po_online_report_text_gt
6917             (
6918              online_report_id,
6919              last_update_login,
6920              last_updated_by,
6921              last_update_date,
6922              created_by,
6923              creation_date,
6924              line_num,
6925              shipment_num,
6926              distribution_num,
6927              sequence,
6928              text_line,
6929              message_name,
6930              message_type
6931             )
6932             VALUES
6933             (
6934               p_online_report_id,
6935               p_login_id,
6936               p_user_id,
6937               sysdate,
6938               p_user_id,
6939               sysdate,
6940               po_tax_interface_pvt.G_TAX_ERRORS_TBL.line_num(i),
6941               po_tax_interface_pvt.G_TAX_ERRORS_TBL.shipment_num(i),
6942               po_tax_interface_pvt.G_TAX_ERRORS_TBL.distribution_num(i),
6943               p_sequence + ROWNUM,
6944               l_tax_message || po_tax_interface_pvt.G_TAX_ERRORS_TBL.message_text(i),
6945               'PO_TAX_CALCULATION_FAILED',
6946               'E'
6947             );
6948         l_progress := '024';
6949       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6950         IF g_debug_stmt THEN
6951            IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6952                FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head ||
6953                             l_api_name||'.' || l_progress,
6954                             'PO 24: Calculate tax raised unexpected error');
6955            END IF;
6956         END IF;
6957         l_textline := l_progress ||' - ';
6958         IF po_tax_interface_pvt.G_TAX_ERRORS_TBL.MESSAGE_TEXT.COUNT > 0 THEN
6959           l_textline := l_textline || po_tax_interface_pvt.G_TAX_ERRORS_TBL.MESSAGE_TEXT(1);
6960         ELSE
6961           l_textline := l_textline || SQLERRM();
6962         END IF;
6963         fnd_message.set_name('PO','PO_TAX_CALC_UNEXPECTED_ERROR');
6964         fnd_message.set_token('ERROR',l_textline);
6965         FND_MSG_PUB.Add;
6966         l_progress := '025';
6967         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6968       END IF;
6969     END IF;
6970 
6971     p_sequence := p_sequence + SQL%ROWCOUNT;
6972 
6973 --<R12 eTax Integration End>
6974 
6975 -----------------------------------------------------------------------------
6976 
6977  l_progress := '026';
6978 
6979     IF g_debug_stmt THEN
6980         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6981           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
6982                        l_api_name||'.' || l_progress,
6983                        'PO 25: A VMI relationship exists for this item');
6984         END IF;
6985     END IF;
6986 
6987     -- Check 22: If the profile option PO_VMI_DISPLAY_WARNING is on,
6988     -- display a warning if the item has a VMI relationship.
6989 
6990     -- Flag indicating if profile PO_VMI_DISPLAY_WARNING is on
6991     fnd_profile.get(PO_PROFILES.PO_VMI_DISPLAY_WARNING,l_po_vmi_display_warning);
6992 
6993     IF (l_po_vmi_display_warning = 'Y') THEN
6994 
6995       l_textline := FND_MESSAGE.get_string('PO', 'PO_SUB_VMI_ASL_EXISTS');
6996 
6997       INSERT INTO po_online_report_text_gt(
6998          online_report_id,
6999          last_update_login,
7000          last_updated_by,
7001          last_update_date,
7002          created_by,
7003          creation_date,
7004          line_num,
7005          shipment_num,
7006          distribution_num,
7007          sequence,
7008          text_line,
7009          message_name,
7010          message_type
7011       )
7012       SELECT p_online_report_id,
7013              p_login_id,
7014              p_user_id,
7015              sysdate,
7016              p_user_id,
7017              sysdate,
7018              POL.line_num,
7019              PLL.shipment_num,
7020              0,
7021              p_sequence + ROWNUM,
7022              SUBSTR (g_linemsg || POL.line_num || g_delim ||
7023                      g_shipmsg || PLL.shipment_num || g_delim ||
7024                      l_textline,
7025                      1,240),
7026              'PO_SUB_VMI_ASL_EXISTS',
7027              'W'
7028       FROM   PO_LINES_GT POL,
7029              PO_HEADERS_GT POH,
7030              PO_LINE_LOCATIONS_GT PLL,
7031              PO_APPROVED_SUPPLIER_LIS_VAL_V PASL,
7032              PO_ASL_ATTRIBUTES PAA,
7033              PO_ASL_STATUS_RULES_V PASR
7034 
7035       WHERE  POH.po_header_id = p_document_id
7036       AND    POL.po_header_id = POH.po_header_id
7037       AND    PLL.po_header_id = POH.po_header_id
7038       AND    PLL.po_line_id   = POL.po_line_id
7039 
7040       -- item is not null
7041       AND    POL.item_id IS NOT NULL
7042 
7043       -- Document is standard PO
7044       AND    POH.type_lookup_code = 'STANDARD'
7045 
7046       --VMI is enabled
7047       AND    PASL.item_id = POL.item_id
7048       AND    PASL.vendor_id = POH.vendor_id
7049       AND    nvl(PASL.vendor_site_id,-1) = nvl(POH.vendor_site_id,-1)
7050       AND    PASL.using_organization_id IN (PLL.ship_to_organization_id, -1)
7051       AND    PASR.status_id = PASL.asl_status_id
7052       AND    PASR.business_rule = '2_SOURCING'
7053       AND    PASR.allow_action_flag = 'Y'
7054       AND    PASL.asl_id = PAA.asl_id
7055       AND    PAA.enable_vmi_flag = 'Y'
7056       AND    PAA.using_organization_id =
7057                (SELECT max(paa2.using_organization_id)
7058                 FROM   po_asl_attributes paa2
7059                 WHERE  paa2.asl_id = pasl.asl_id
7060                 AND    paa2.using_organization_id IN (-1, PLL.ship_to_organization_id));
7061 
7062     END IF;
7063 
7064     p_sequence := p_sequence + SQL%ROWCOUNT;
7065 
7066 -------------------------------------------------------------------------
7067 
7068     l_progress := '027';
7069     IF g_debug_stmt THEN
7070         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
7071           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
7072                        l_api_name||'.' || l_progress,
7073                        'PO 23: Validate OKE Contract Details');
7074         END IF;
7075     END IF;
7076 
7077     -- Bug 7001748: Added new validation check
7078     -- Check 23: Validate OKE Contract Version, Line Id and Deliverable Id.
7079 
7080     -- Check 23a: Validate Contract Version
7081     INSERT INTO po_online_report_text_gt
7082                (online_report_id,
7083                 last_update_login,
7084                 last_updated_by,
7085                 last_update_date,
7086                 created_by,
7087                 creation_date,
7088                 line_num,
7089                 shipment_num,
7090                 distribution_num,
7091                 SEQUENCE,
7092                 text_line,
7093                 message_name)
7094     SELECT p_online_report_id,
7095            p_login_id,
7096            p_user_id,
7097            SYSDATE,
7098            p_user_id,
7099            SYSDATE,
7100            pol.line_num,
7101            0,
7102            0,
7103            p_sequence + ROWNUM,
7104            po_core_s.GET_TRANSLATED_TEXT('PO_INVALID_OKE_CONTRACT_VER_ID',
7105                                          'LINE_NUM',pol.line_num),
7106            'PO_INVALID_OKE_CONTRACT_VER_ID'
7107     FROM   po_lines_gt pol
7108     WHERE  pol.oke_contract_version_id IS NOT NULL
7109            AND pol.oke_contract_version_id NOT IN (SELECT major_version
7110                                                    FROM   oke_k_vers_numbers_v
7111                                                    WHERE  chr_id = pol.oke_contract_header_id
7112                                                    UNION
7113                                                    SELECT major_version
7114                                                    FROM   okc_k_vers_numbers_h
7115                                                    WHERE  chr_id = pol.oke_contract_header_id);
7116 
7117     --Increment the p_sequence with number of errors reported in last query
7118     p_sequence := p_sequence + SQL%ROWCOUNT;
7119 
7120     -- Check 23b: Validate Contract Line Id
7121     INSERT INTO po_online_report_text_gt
7122                (online_report_id,
7123                 last_update_login,
7124                 last_updated_by,
7125                 last_update_date,
7126                 created_by,
7127                 creation_date,
7128                 line_num,
7129                 shipment_num,
7130                 distribution_num,
7131                 SEQUENCE,
7132                 text_line,
7133                 message_name)
7134     SELECT p_online_report_id,
7135            p_login_id,
7136            p_user_id,
7137            SYSDATE,
7138            p_user_id,
7139            SYSDATE,
7140            pol.line_num,
7141            pos.shipment_num,
7142            pod.distribution_num,
7143            p_sequence + ROWNUM,
7144            po_core_s.GET_TRANSLATED_TEXT('PO_INVALID_OKE_CONTRACT_LNE_ID',
7145                                          'LINE_NUM',pol.line_num,
7146                                          'SHIP_NUM',pos.shipment_num,
7147                                          'DIST_NUM',pod.distribution_num),
7148            'PO_INVALID_OKE_CONTRACT_LNE_ID'
7149     FROM   po_distributions_gt pod,
7150            po_line_locations_gt pos,
7151            po_lines_gt pol
7152     WHERE  pod.po_line_id = pol.po_line_id
7153            AND pod.line_location_id = pos.line_location_id
7154            AND pod.oke_contract_line_id IS NOT NULL
7155            AND pod.oke_contract_line_id NOT IN (SELECT id
7156                                                 FROM   okc_k_lines_b
7157                                                 WHERE  dnz_chr_id = pol.oke_contract_header_id
7158                                                 -- <Bug 7695529>
7159                                                 -- Look for contract lines in oke_deliverables_b also (DTS flow)
7160                                                 UNION
7161                                                 SELECT deliverable_id
7162                                                 FROM   oke_deliverables_b
7163                                                 WHERE  source_header_id = pol.oke_contract_header_id);
7164 
7165     --Increment the p_sequence with number of errors reported in last query
7166     p_sequence := p_sequence + SQL%ROWCOUNT;
7167 
7168     -- Check 23c: Validate Contract Deliverable Id
7169     INSERT INTO po_online_report_text_gt
7170                (online_report_id,
7171                 last_update_login,
7172                 last_updated_by,
7173                 last_update_date,
7174                 created_by,
7175                 creation_date,
7176                 line_num,
7177                 shipment_num,
7178                 distribution_num,
7179                 SEQUENCE,
7180                 text_line,
7181                 message_name)
7182     SELECT p_online_report_id,
7183            p_login_id,
7184            p_user_id,
7185            SYSDATE,
7186            p_user_id,
7187            SYSDATE,
7188            pol.line_num,
7189            pos.shipment_num,
7190            pod.distribution_num,
7191            p_sequence + ROWNUM,
7192            po_core_s.GET_TRANSLATED_TEXT('PO_INVALID_OKE_CONTRACT_DLV_ID',
7193                                          'LINE_NUM',pol.line_num,
7194                                          'SHIP_NUM',pos.shipment_num,
7195                                          'DIST_NUM',pod.distribution_num),
7196            'PO_INVALID_OKE_CONTRACT_DLV_ID'
7197     FROM   po_distributions_gt pod,
7198            po_line_locations_gt pos,
7199            po_lines_gt pol
7200     WHERE  pod.po_line_id = pol.po_line_id
7201            AND pod.line_location_id = pos.line_location_id
7202            AND pod.oke_contract_deliverable_id IS NOT NULL
7203            AND pod.oke_contract_deliverable_id NOT IN (SELECT deliverable_id
7204                                                        FROM   oke_k_deliverables_b
7205                                                        WHERE  k_line_id = pod.oke_contract_line_id
7206                                                        -- <Bug 7695529>
7207                                                        -- Look for contract deliverables in
7208                                                        -- oke_deliverable_actions also (DTS flow)
7209                                                        UNION
7210                                                        SELECT action_id
7211                                                        FROM   oke_deliverable_actions
7212                                                        WHERE  deliverable_id = pod.oke_contract_line_id);
7213 
7214     --Increment the p_sequence with number of errors reported in last query
7215     p_sequence := p_sequence + SQL%ROWCOUNT;
7216 
7217     ----------------------------------------------------------------------------
7218     l_progress := '028';
7219     IF g_debug_stmt THEN
7220             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
7221               FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
7222                            l_api_name||'.' || l_progress,'PO 24: Validate the LCM enabled PO to check whether its invoice match option is set as Receipt');
7223             END IF;
7224         END IF;
7225         -- Check 24: Validate the LCM enabled PO to check whether its invoice match option is set as 'Receipt'.
7226         l_textline := FND_MESSAGE.GET_STRING('PO','PO_SUB_PO_SHIP_INV_MATCH_NE_R');
7227         INSERT INTO po_online_report_text_gt (online_report_id,
7228       		                          last_update_login,
7229     	 				  last_updated_by,
7230     	 				  last_update_date,
7231     	 				  created_by,
7232     	 				  creation_date,
7233     	 				  line_num,
7234     	 				  shipment_num,
7235     	 				  distribution_num,
7236     	 				  sequence,
7237     	 				  text_line,
7238     	          			  message_name)
7239         SELECT p_online_report_id,
7240     	   p_login_id,
7241     	   p_user_id,
7242     	   sysdate,
7243     	   p_user_id,
7244     	   sysdate,
7245     	   pol.line_num,
7246     	   pll.shipment_num, 0,
7247     	   p_sequence + ROWNUM,
7248     	   substr(g_linemsg||g_delim||POL.line_num||g_delim||g_shipmsg||g_delim||
7249     		  PLL.shipment_num||g_delim||l_textline,1,240),
7250     	   'PO_SUB_PO_SHIP_INV_MATCH_NE_R'
7251           FROM PO_HEADERS_GT POH,
7252                PO_LINES_GT POL,
7253     	   PO_LINE_LOCATIONS_GT PLL
7254          WHERE POH.po_header_id = POL.po_header_id
7255            AND POL.po_line_id = PLL.po_line_id
7256            AND POH.po_header_id  = p_document_id
7257            AND Nvl(PLL.LCM_FLAG,'N') = 'Y'
7258            AND Nvl(PLL.match_option,'P') <> 'R';
7259 
7260         --Increment the p_sequence with number of errors reported in last query
7261         p_sequence := p_sequence + SQL%ROWCOUNT;
7262 
7263         -----------------------------------------------------------------------------------
7264 
7265         l_progress := '029';
7266         IF g_debug_stmt THEN
7267             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
7268               FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
7269                            l_api_name||'.' || l_progress,'PO 25: Validate the LCM enabled PO to check whether its destination type is set as Inventory');
7270 
7271             END IF;
7272 
7273 
7274         END IF;
7275         -- Check 25: Validate the LCM enabled PO to check whether its destination type is set as 'Inventory'
7276         l_textline := FND_MESSAGE.GET_STRING('PO','PO_SUB_PO_DIST_DEST_TYPE_NE_IN');
7277         INSERT INTO po_online_report_text_gt (online_report_id,
7278     	 				  last_update_login,
7279     	 				  last_updated_by,
7280     	 				  last_update_date,
7281     	 				  created_by,
7282     	 				  creation_date,
7283     	 				  line_num,
7284     	 				  shipment_num,
7285     	 				  distribution_num,
7286     	 				  sequence,
7287     	 				  text_line,
7288     	                                  message_name)
7289         SELECT p_online_report_id,
7290     	   p_login_id,
7291     	   p_user_id,
7292     	   sysdate,
7293     	   p_user_id,
7294     	   sysdate,
7295     	   pol.line_num,
7296     	   pll.shipment_num,
7297     	   pod.distribution_num,
7298     	   p_sequence + ROWNUM,
7299     	   substr(g_linemsg||g_delim||POL.line_num||g_delim||g_shipmsg||g_delim||PLL.shipment_num||g_delim
7300     	          ||g_distmsg||g_delim||POD.distribution_num||g_delim||l_textline,1,240),
7301     	   'PO_SUB_PO_DIST_DEST_TYPE_NE_IN'
7302           FROM PO_HEADERS_GT POH,
7303                PO_LINES_GT POL,
7304     	   PO_LINE_LOCATIONS_GT PLL,
7305     	   PO_DISTRIBUTIONS_GT POD
7306          WHERE POH.po_header_id = POD.po_header_id
7307            AND POD.line_location_id = PLL.line_location_id
7308            AND PLL.po_line_id = POL.po_line_id
7309            AND POH.po_header_id = p_document_id
7310            AND Nvl(POD.LCM_FLAG,'N') = 'Y'
7311            AND POD.DESTINATION_TYPE_CODE <> 'INVENTORY';
7312 
7313 
7314         --Increment the p_sequence with number of errors reported in last query
7315         p_sequence := p_sequence + SQL%ROWCOUNT;
7316 
7317     -------------------------------------------------------------------------------------------------------
7318     x_return_status := FND_API.G_RET_STS_SUCCESS;
7319 
7320 EXCEPTION
7321     WHEN FND_API.G_EXC_ERROR THEN
7322         x_return_status := FND_API.G_RET_STS_ERROR;
7323 
7324     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7325         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7326 
7327     WHEN OTHERS THEN
7328         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7329         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
7330             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
7331         END IF;
7332 
7333         IF (g_debug_unexp) THEN
7334                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
7335                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
7336                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
7337                        || l_progress || ' SQL CODE is '||sqlcode);
7338                 END IF;
7339         END IF;
7340 END CHECK_PO;
7341 
7342 --For Planned POs and Blanket PAs
7343 /**
7344 * Private Procedure: CHECK_PLANNED_PO_BLANKET_PA
7345 * Requires:
7346 *   IN PARAMETERS:
7347 *       p_document_id:      The requisition_header_id of submitted document
7348 *       p_online_report_id: Id used to INSERT INTO online_report_text table
7349 *       p_user_id:          User performing the action
7350 *       p_login_id:         Last update login_id
7351 *   IN OUT PARAMETERS
7352 *       p_sequence:          Sequence number of last reported error
7353 * Modifies: Inserts error msgs in online_report_text_gt table, uses
7354 *           global_temp tables for processing
7355 * Effects:  This procedure runs the document submission checks for PLANNED POs
7356 *           and BLANKET PAs
7357 * Returns:
7358 *  p_sequence: This parameter contains the current count of number of error
7359 *              messages inserted
7360 */
7361 PROCEDURE check_planned_po_blanket_pa(p_document_id IN NUMBER,
7362                        p_online_report_id IN NUMBER,
7363                        p_user_id IN NUMBER,
7364                        p_login_id IN NUMBER,
7365                        p_sequence IN OUT NOCOPY NUMBER,
7366                        x_return_status OUT NOCOPY VARCHAR2) IS
7367 
7368 l_textline  po_online_report_text.text_line%TYPE := NULL;
7369 l_api_name  CONSTANT varchar2(40) := 'CHECK_PLANNED_PO_BLANKET_PA';
7370 l_progress VARCHAR2(3);
7371 
7372 BEGIN
7373 l_progress := '000';
7374 
7375 l_progress := '001';
7376 IF g_debug_stmt THEN
7377    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
7378      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
7379           || l_progress,
7380           'PA/PO BLANKET/PLANNED 1: Amount agreed greater than Amount limit');
7381    END IF;
7382 END IF;
7383 
7384   -- Check 1: The amount agreed specified on the planned po and blanket
7385     -- pa should be less than the amount limit.
7386     -- PO_SUB_AGREED_GRT_LIMIT
7387 
7388   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_AGREED_GRT_LIMIT');
7389   INSERT INTO po_online_report_text_gt(online_report_id,
7390         last_update_login,
7391         last_updated_by,
7392         last_update_date,
7393         created_by,
7394         creation_date,
7395         line_num,
7396         shipment_num,
7397         distribution_num,
7398         sequence,
7399         text_line,
7400                 message_name)
7401   SELECT  p_online_report_id,
7402         p_login_id,
7403         p_user_id,
7404           sysdate,
7405         p_user_id,
7406         sysdate,
7407         0,
7408         0,
7409         0,
7410         p_sequence + ROWNUM,
7411         substr(l_textline,1,240),
7412             'PO_SUB_AGREED_GRT_LIMIT'
7413     FROM  PO_HEADERS_GT POH
7414     WHERE  POH.po_header_id = p_document_id
7415     AND    POH.blanket_total_amount is not null
7416     AND    POH.amount_limit is not null
7417     AND    POH.blanket_total_amount > POH.amount_limit;
7418 
7419 
7420     --Increment the p_sequence with number of errors reported in last query
7421     p_sequence := p_sequence + SQL%ROWCOUNT;
7422 ------------------------------------------
7423 
7424 l_progress := '002';
7425 IF g_debug_stmt THEN
7426    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
7427      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
7428           || l_progress,
7429           'PA/PO BLANKET/PLANNED 2: Min Rel Amount greater than Amount limit');
7430    END IF;
7431 END IF;
7432 
7433   -- Check 2: The min release amount specified on the planned po and
7434     -- blanket pa should be less than the amount limit.
7435     -- PO_SUB_MINREL_GRT_LIMIT
7436 
7437   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_MINREL_GRT_LIMIT');
7438   INSERT INTO po_online_report_text_gt(online_report_id,
7439         last_update_login,
7440         last_updated_by,
7441         last_update_date,
7442         created_by,
7443         creation_date,
7444         line_num,
7445         shipment_num,
7446         distribution_num,
7447         sequence,
7448         text_line,
7449                 message_name)
7450   SELECT  p_online_report_id,
7451         p_login_id,
7452         p_user_id,
7453           sysdate,
7454         p_user_id,
7455         sysdate,
7456         0,
7457         0,
7458         0,
7459         p_sequence + ROWNUM,
7460         substr(l_textline,1,240),
7461             'PO_SUB_MINREL_GRT_LIMIT'
7462     FROM  PO_HEADERS_GT POH
7463     WHERE  POH.po_header_id = p_document_id
7464     AND    POH.min_release_amount is not null
7465     AND    POH.amount_limit is not null
7466     AND    POH.min_release_amount > POH.amount_limit;
7467 
7468 
7469      --Increment the p_sequence with number of errors reported in last query
7470     p_sequence := p_sequence + SQL%ROWCOUNT;
7471 ------------------------------------------
7472 
7473 l_progress := '003';
7474 IF g_debug_stmt THEN
7475    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
7476      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
7477           || l_progress,
7478           'PA/PO BLANKET/PLANNED 3: Amount released greater than Amount limit');
7479    END IF;
7480 END IF;
7481 
7482   -- Check 3: The Amount Limit should be greater than the total of all
7483     -- Releases.
7484     -- PO_SUB_LIMIT_GRT_REL_AMT
7485 
7486   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_LIMIT_GRT_REL_AMT');
7487   INSERT INTO po_online_report_text_gt(online_report_id,
7488         last_update_login,
7489         last_updated_by,
7490         last_update_date,
7491         created_by,
7492         creation_date,
7493         line_num,
7494         shipment_num,
7495         distribution_num,
7496         sequence,
7497         text_line,
7498                 message_name)
7499   SELECT  p_online_report_id,
7500         p_login_id,
7501         p_user_id,
7502           sysdate,
7503         p_user_id,
7504         sysdate,
7505         0,
7506         0,
7507         0,
7508         p_sequence + ROWNUM,
7509         substr(l_textline,1,240),
7510             'PO_SUB_LIMIT_GRT_REL_AMT'
7511      FROM   PO_HEADERS_GT POH
7512     WHERE  POH.po_header_id = p_document_id
7513     AND    POH.amount_limit is not null
7514     AND ( (NVL(POH.global_agreement_flag, 'N') = 'N'
7515            AND
7516            POH.amount_limit <
7517            (   SELECT                                         -- <SERVICES FPJ>
7518                    sum ( decode ( PLL2.quantity
7519                                 , NULL , ( PLL2.amount
7520                                          - nvl(PLL2.amount_cancelled,0)
7521                                          )
7522                                 ,        (   ( PLL2.quantity
7523                                              - nvl(PLL2.quantity_cancelled,0) )
7524                                          * PLL2.price_override
7525                                          )
7526                                 )
7527                        )
7528                FROM PO_LINE_LOCATIONS PLL2
7529                WHERE PLL2.po_header_id = POH.po_header_id
7530                AND PLL2.shipment_type in ('BLANKET', 'SCHEDULED')
7531            )
7532           )
7533          OR
7534          --bug2969379
7535          --GA should have amount limit checked in a different way
7536           ( POH.global_agreement_flag = 'Y'
7537             AND
7538             POH.amount_limit <
7539             (   SELECT
7540                    sum ( decode ( PLL3.quantity
7541                                 , NULL , ( PLL3.amount
7542                                          - nvl(PLL3.amount_cancelled,0)
7543                                          )
7544                                 ,        (   ( PLL3.quantity
7545                                              - nvl(PLL3.quantity_cancelled,0) )
7546                                          * PLL3.price_override
7547                                          )
7548                                 )
7549                        )
7550                FROM PO_LINE_LOCATIONS_ALL PLL3
7551                WHERE PLL3.from_header_id = POH.po_header_id
7552             )
7553           )
7554         );
7555 
7556 --<FPI commented>
7557 --    GROUP BY POH.amount_limit);
7558 
7559      --Increment the p_sequence with number of errors reported in last query
7560     p_sequence := p_sequence + SQL%ROWCOUNT;
7561 
7562 ------------------------------------------
7563 
7564 /* Start Bug 3286940 */
7565 
7566 
7567 l_progress := '004';
7568 IF g_debug_stmt THEN
7569    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
7570      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
7571           || l_progress,
7572           'PA/PO BLANKET/PLANNED 4: Price Limit less than Line Price');
7573    END IF;
7574 END IF;
7575 
7576 
7577   -- Check 4: The price limit when price_override_flag = 'Y' should be
7578    --          at least as large as the unit price for that line.
7579     -- PO_SVC_PRICE_LIMIT_LT_PRICE
7580 
7581     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SVC_PRICE_LIMIT_LT_PRICE');
7582 
7583 
7584       INSERT into po_online_report_text_gt(online_report_id,
7585                                            last_update_login,
7586                                            last_updated_by,
7587                                            last_update_date,
7588                                            created_by,
7589                                            creation_date,
7590                                            line_num,
7591                                            shipment_num,
7592                                            distribution_num,
7593                                            sequence,
7594                                            text_line,
7595                                            message_name)
7596       SELECT  p_online_report_id,
7597               p_login_id,
7598               p_user_id,
7599               sysdate,
7600               p_user_id,
7601               sysdate,
7602               POL.line_num,
7603               0,
7604               0,
7605               p_sequence + ROWNUM,
7606               substr(g_linemsg||g_delim||POL.line_num
7607                          ||g_delim||l_textline,1,240),
7608               'PO_SVC_PRICE_LIMIT_LT_PRICE'
7609       FROM PO_LINES_GT POL
7610       WHERE POL.po_header_id = p_document_id
7611       AND trunc(sysdate) <= trunc(nvl(POL.expiration_date, sysdate + 1)) -- bug 3449694
7612       AND nvl(POL.cancel_flag,'N')= 'N'
7613       AND nvl(POL.closed_code,'OPEN') <> 'FINALLY CLOSED'
7614       AND nvl(POL.allow_price_override_flag, 'N') = 'Y'
7615       AND POL.not_to_exceed_price IS NOT NULL
7616       AND ((POL.unit_price IS NOT NULL and POL.not_to_exceed_price < POL.unit_price)
7617            or
7618           (POL.amount IS NOT NULL and POL.not_to_exceed_price < POL.amount));
7619 
7620 
7621 
7622      --Increment the p_sequence with number of errors reported in last query
7623     p_sequence := p_sequence + SQL%ROWCOUNT;
7624 
7625 
7626 /* End Bug 3286940 */
7627 
7628 
7629     x_return_status := FND_API.G_RET_STS_SUCCESS;
7630 EXCEPTION
7631     WHEN FND_API.G_EXC_ERROR THEN
7632         x_return_status := FND_API.G_RET_STS_ERROR;
7633 
7634     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7635         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7636 
7637     WHEN OTHERS THEN
7638         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7639         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
7640             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
7641         END IF;
7642 
7643         IF (g_debug_unexp) THEN
7644                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
7645                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
7646                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
7647                        || l_progress || ' SQL CODE is '||sqlcode);
7648                 END IF;
7649         END IF;
7650 
7651 END CHECK_PLANNED_PO_BLANKET_PA;
7652 
7653 --For Blanket PA
7654 /**
7655 * Private Procedure: CHECK_BLANKET_AGREEMENT
7656 * Requires:
7657 *   IN PARAMETERS:
7658 *       p_document_id:      The requisition_header_id of submitted document
7659 *       p_online_report_id: Id used to INSERT INTO online_report_text table
7660 *       p_user_id:          User performing the action
7661 *       p_login_id:         Last update login_id
7662 *   IN OUT PARAMETERS
7663 *       p_sequence:          Sequence number of last reported error
7664 * Modifies: Inserts error msgs in online_report_text_gt table, uses
7665 *           global_temp tables for processing
7666 * Effects:  This procedure runs the document submission checks for BLANKETS
7667 * Returns:
7668 *  p_sequence: This parameter contains the current count of number of error
7669 *              messages inserted
7670 */
7671 PROCEDURE check_blanket_agreement(p_document_id IN NUMBER,
7672                        p_online_report_id IN NUMBER,
7673                        p_user_id IN NUMBER,
7674                        p_login_id IN NUMBER,
7675                        p_check_asl IN BOOLEAN,                     -- <2757450>
7676                        p_sequence IN OUT NOCOPY NUMBER,
7677                        x_return_status OUT NOCOPY VARCHAR2) IS
7678 
7679 l_textline  po_online_report_text.text_line%TYPE := NULL;
7680 l_api_name  CONSTANT varchar2(40) := 'CHECK_BLANKET_AGREEMENT';
7681 l_progress VARCHAR2(3);
7682 
7683 BEGIN
7684 l_progress := '000';
7685 
7686 l_progress := '001';
7687 IF g_debug_stmt THEN
7688    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
7689      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
7690           || l_progress,'PA BLANKET 1: No lines');
7691    END IF;
7692 END IF;
7693 
7694     -- Check 1: Blanket Header must have at least one line
7695     -- PO_SUB_HEADER_NO_LINES
7696     -- Message inserted is 'Purchase Document has no lines'
7697     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_HEADER_NO_LINES');
7698     INSERT INTO po_online_report_text_gt(online_report_id,
7699         last_update_login,
7700         last_updated_by,
7701         last_update_date,
7702         created_by,
7703           creation_date,
7704         line_num,
7705         shipment_num,
7706         distribution_num,
7707         sequence,
7708         text_line,
7709                 message_name)
7710     SELECT  p_online_report_id,
7711             p_login_id,
7712             p_user_id,
7713             sysdate,
7714             p_user_id,
7715             sysdate,
7716             0, 0, 0,
7717             p_sequence + ROWNUM,
7718             substr(l_textline,1,240),
7719             'PO_SUB_HEADER_NO_LINES'
7720     FROM   PO_HEADERS_GT POH
7721     WHERE  POH.po_header_id = p_document_id AND
7722            NOT EXISTS (SELECT 'Lines Exist'
7723                        FROM   PO_LINES_GT POL
7724                        WHERE  POL.po_header_id = POH.po_header_id);
7725                        --AND    nvl(POL.cancel_flag,'N') = 'N');
7726                        -- bug 3300632
7727 
7728     --Increment the p_sequence with number of errors reported in last query
7729     p_sequence := p_sequence + SQL%ROWCOUNT;
7730 ----------------------------------------------------
7731 
7732 --Bug 4943365 We should not make any ASL checks for Blanket agreements.
7733 --Removed the call to check_asl
7734 
7735 ---------------------------------------------
7736 
7737 --<FPJ ENCUMBRANCE>
7738 
7739 l_progress := '400';
7740 
7741 -- Check 4: The PA GL date should be within an open purchasing period
7742 -- PO_SUB_PA_INVALID_GL_DATE ?
7743 
7744 IF (  PO_CORE_S.is_encumbrance_on(
7745          p_doc_type => g_document_type_PA
7746       ,  p_org_id => NULL
7747       )
7748    )
7749 THEN
7750 
7751    l_progress := '410';
7752    IF g_debug_stmt THEN
7753       PO_DEBUG.debug_stmt(g_log_head||'.'||l_api_name||'.',
7754                            l_progress,'PA BLANKET 4: GL date');
7755    END IF;
7756 
7757    check_gl_date(
7758       p_doc_type => g_document_type_PA
7759    ,  p_online_report_id => p_online_report_id
7760    ,  p_login_id => p_login_id
7761    ,  p_user_id => p_user_id
7762    ,  p_sequence => p_sequence
7763    );
7764 
7765    l_progress := '420';
7766 
7767 ELSE
7768    l_progress := '430';
7769    IF g_debug_stmt THEN
7770       PO_DEBUG.debug_stmt(g_log_head||'.'||l_api_name||'.',
7771                            l_progress,'PA BLANKET 4: PA encumbrance not on');
7772    END IF;
7773 END IF;
7774 
7775 
7776   /* Start bug #3512688*/
7777       /* Check 05 : To check the validity of the item at line level for newly added  line */
7778       l_progress := '04';
7779       IF g_debug_stmt  THEN
7780       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
7781         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
7782       l_api_name||'.' || l_progress,
7783       'PO 04: Non Purchasable Item is not allowed');
7784       END IF;
7785       END IF;
7786       l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_ALL_NO_ITEM');
7787       INSERT INTO po_online_report_text_gt(online_report_id,
7788       last_update_login,
7789       last_updated_by,
7790       last_update_date,
7791       created_by,
7792       creation_date,
7793       line_num,
7794       shipment_num,
7795       distribution_num,
7796       sequence,
7797       text_line,
7798       message_name)
7799       SELECT  p_online_report_id,
7800       p_login_id,
7801       p_user_id,
7802       sysdate,
7803       p_user_id,
7804       sysdate,
7805       pl.line_num,
7806       0,
7807       0,
7808       p_sequence + ROWNUM,
7809        substr(g_linemsg||g_delim||pl.line_num||g_delim||l_textline,1,240),
7810       'PO_ALL_NO_ITEM'
7811       from po_headers_gt ph,po_lines_gt pl,mtl_system_items  itm,financials_system_parameters fsp,po_line_types_b plt
7812       where itm.inventory_item_id  = pl.item_id
7813       and   pl.item_id is not null
7814       and   itm.organization_id    = fsp.inventory_organization_id
7815       and   itm.purchasing_enabled_flag = 'N'
7816       and   ph.po_header_id = p_document_id
7817       and   pl.po_header_id = ph.po_header_id
7818       and   pl.line_type_id = plt.line_type_id
7819 
7820      and   nvl(plt.outside_operation_flag,'N') =  nvl(itm.outside_operation_flag,'N')
7821       and   (pl.creation_date >= nvl(ph.approved_date ,pl.creation_date)) ;
7822 
7823       --Increment the p_sequence with number of errors reported in last query
7824       p_sequence := p_sequence + SQL%ROWCOUNT;
7825       --End Bug #3512688
7826 
7827 
7828 
7829 --<Begin Bug#: 5132541> EFFECTIVE DATE ON THE HEADER IS NOT VALIDATED AGAINST THAT OF PRICE BREAK
7830     /*Check 06: Check if the effective range on the price break is within the effective range
7831                 of the header and before the expiration date of the line.*/
7832 
7833     l_progress := '06';
7834     IF g_debug_stmt  THEN
7835       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
7836         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
7837                         l_api_name||'.' || l_progress,
7838                         'BPA 06: Checking price breaks effective date range');
7839       END IF;
7840     END IF;
7841 
7842     INSERT INTO po_online_report_text_gt(online_report_id,
7843     last_update_login,
7844     last_updated_by,
7845     last_update_date,
7846     created_by,
7847     creation_date,
7848     line_num,
7849     shipment_num,
7850     distribution_num,
7851     sequence,
7852     text_line,
7853     message_name)
7854     SELECT  p_online_report_id,
7855     p_login_id,
7856     p_user_id,
7857     sysdate,
7858     p_user_id,
7859     sysdate,
7860     pl.line_num,
7861     pll.shipment_num,
7862     0,
7863     p_sequence + ROWNUM,
7864     CASE
7865         WHEN pll.start_date < ph.start_date
7866         THEN substr(g_linemsg||g_delim||pl.line_num||g_delim
7867                    ||g_price_breakmsg||g_delim||pll.shipment_num||g_delim
7868                    ||FND_MESSAGE.GET_STRING('PO', PO_MESSAGE_S.POX_EFFECTIVE_DATES1),1,240)
7869         WHEN pll.end_date > ph.end_date
7870         THEN substr(g_linemsg||g_delim||pl.line_num||g_delim
7871                    ||g_price_breakmsg||g_delim||pll.shipment_num||g_delim
7872                    ||FND_MESSAGE.GET_STRING('PO', PO_MESSAGE_S.POX_EFFECTIVE_DATES),1,240)
7873         WHEN pll.start_date > ph.end_date
7874         THEN substr(g_linemsg||g_delim||pl.line_num||g_delim
7875                    ||g_price_breakmsg||g_delim||pll.shipment_num||g_delim
7876                    ||FND_MESSAGE.GET_STRING('PO', PO_MESSAGE_S.POX_EFFECTIVE_DATES4),1,240)
7877         WHEN pll.end_date < ph.start_date
7878         THEN substr(g_linemsg||g_delim||pl.line_num||g_delim
7879                    ||g_price_breakmsg||g_delim||pll.shipment_num||g_delim
7880                    ||FND_MESSAGE.GET_STRING('PO', PO_MESSAGE_S.POX_EFFECTIVE_DATES5),1,240)
7881         WHEN pll.start_date > pl.expiration_date
7882         THEN substr(g_linemsg||g_delim||pl.line_num||g_delim
7883                    ||g_price_breakmsg||g_delim||pll.shipment_num||g_delim
7884                    ||FND_MESSAGE.GET_STRING('PO', PO_MESSAGE_S.POX_EFFECTIVE_DATES6),1,240)
7885         WHEN pll.end_date > pl.expiration_date
7886         THEN substr(g_linemsg||g_delim||pl.line_num||g_delim
7887                    ||g_price_breakmsg||g_delim||pll.shipment_num||g_delim
7888                    ||FND_MESSAGE.GET_STRING('PO', PO_MESSAGE_S.POX_EFFECTIVE_DATES2),1,240)
7889      END
7890     ,
7891     CASE
7892         WHEN pll.start_date < ph.start_date
7893         THEN PO_MESSAGE_S.POX_EFFECTIVE_DATES1
7894         WHEN pll.end_date > ph.end_date
7895         THEN PO_MESSAGE_S.POX_EFFECTIVE_DATES
7896         WHEN pll.start_date > ph.end_date
7897         THEN PO_MESSAGE_S.POX_EFFECTIVE_DATES4
7898         WHEN pll.end_date < ph.start_date
7899         THEN PO_MESSAGE_S.POX_EFFECTIVE_DATES5
7900         WHEN pll.start_date > pl.expiration_date
7901         THEN PO_MESSAGE_S.POX_EFFECTIVE_DATES6
7902         WHEN pll.end_date > pl.expiration_date
7903         THEN PO_MESSAGE_S.POX_EFFECTIVE_DATES2
7904     END
7905     FROM po_headers_gt ph, po_lines_gt pl, po_line_locations_gt pll
7906     WHERE ph.po_header_id = p_document_id
7907     AND pl.po_header_id = ph.po_header_id
7908     AND pll.po_line_id = pl.po_line_id
7909     AND pll.shipment_type = 'PRICE BREAK'
7910     AND (pll.start_date < ph.start_date
7911       or pll.end_date > ph.end_date
7912       or pll.start_date > ph.end_date
7913       or pll.end_date < ph.start_date
7914       or pll.start_date > pl.expiration_date
7915       or pll.end_date > pl.expiration_date);
7916 
7917     p_sequence := p_sequence + SQL%ROWCOUNT;
7918 
7919     /*Check 07: Check if the expiration date on the line is within the effective range
7920                 of the header.*/
7921 
7922     l_progress := '07';
7923     IF g_debug_stmt  THEN
7924       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
7925         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
7926                         l_api_name||'.' || l_progress,
7927                         'BPA 07: Checking the lines expiration date');
7928       END IF;
7929     END IF;
7930 
7931     l_textline := FND_MESSAGE.GET_STRING('PO', PO_MESSAGE_S.POX_EXPIRATION_DATES);
7932     INSERT INTO po_online_report_text_gt(online_report_id,
7933     last_update_login,
7934     last_updated_by,
7935     last_update_date,
7936     created_by,
7937     creation_date,
7938     line_num,
7939     shipment_num,
7940     distribution_num,
7941     sequence,
7942     text_line,
7943     message_name)
7944     SELECT  p_online_report_id,
7945     p_login_id,
7946     p_user_id,
7947     sysdate,
7948     p_user_id,
7949     sysdate,
7950     pl.line_num,
7951     0,
7952     0,
7953     p_sequence + ROWNUM,
7954     substr(g_linemsg||g_delim||pl.line_num||g_delim||l_textline,1,240),
7955     PO_MESSAGE_S.POX_EXPIRATION_DATES
7956     FROM po_headers_gt ph, po_lines_gt pl
7957     WHERE ph.po_header_id = p_document_id
7958     AND pl.po_header_id = ph.po_header_id
7959     AND (pl.expiration_date < ph.start_date
7960       or pl.expiration_date > ph.end_date);
7961 
7962     p_sequence := p_sequence + SQL%ROWCOUNT;
7963     --<End Bug#: 5132541>
7964 
7965 
7966 l_progress := '008';
7967     x_return_status := FND_API.G_RET_STS_SUCCESS;
7968 EXCEPTION
7969     WHEN FND_API.G_EXC_ERROR THEN
7970         x_return_status := FND_API.G_RET_STS_ERROR;
7971 
7972     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7973         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7974 
7975     WHEN OTHERS THEN
7976         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7977         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
7978             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
7979         END IF;
7980 
7981         IF (g_debug_unexp) THEN
7982                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
7983                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
7984                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
7985                        || l_progress || ' SQL CODE is '||sqlcode);
7986                 END IF;
7987         END IF;
7988 
7989 END CHECK_BLANKET_AGREEMENT;
7990 
7991 --For Standard POs
7992 /**
7993 * Private Procedure: CHECK_STANDARD_PO
7994 * Requires:
7995 *   IN PARAMETERS:
7996 *       p_document_id:      The requisition_header_id of submitted document
7997 *       p_online_report_id: Id used to INSERT INTO online_report_text table
7998 *       p_user_id:          User performing the action
7999 *       p_login_id:         Last update login_id
8000 *   IN OUT PARAMETERS
8001 *       p_sequence:          Sequence number of last reported error
8002 * Modifies: Inserts error msgs in online_report_text_gt table, uses
8003 *           global_temp tables for processing
8004 * Effects:  This procedure runs the document submission checks for Standard
8005 *           POs including GLOBAL AGREEMENTS reference checks and Consigned
8006 *           Inventory checks
8007 * Returns:
8008 *  p_sequence: This parameter contains the current count of number of error
8009 *              messages inserted
8010 */
8011 PROCEDURE check_standard_po(p_document_id IN NUMBER,
8012                        p_online_report_id IN NUMBER,
8013                        p_user_id IN NUMBER,
8014                        p_login_id IN NUMBER,
8015                        p_sequence IN OUT NOCOPY NUMBER,
8016                        x_return_status OUT NOCOPY VARCHAR2) IS
8017 
8018 l_is_ga_referenced VARCHAR2(1) := NULL;
8019 l_api_name  CONSTANT varchar2(40) := 'CHECK_STANDARD_PO';
8020 l_progress VARCHAR2(3);
8021 
8022 l_is_gc_referenced VARCHAR2(1) := NULL;         -- <GC FPJ>
8023 l_textline PO_ONLINE_REPORT_TEXT.text_line%TYPE := NULL;  --< Shared Proc FPJ >
8024 l_return_status VARCHAR2(1);
8025 
8026 -- Bug 2818810. Added extra join to alias POHA to return 'Y' only if at least
8027 -- one line references a GA.
8028 CURSOR std_ga_ref_cursor(p_document_id NUMBER) IS
8029     SELECT 'Y'
8030     FROM PO_HEADERS_GT POH, PO_LINES_GT POL, PO_HEADERS_ALL POHA
8031     WHERE POH.po_header_id = p_document_id
8032      AND  POH.po_header_id = POL.po_header_id
8033      AND  POL.from_header_id = POHA.po_header_id
8034      AND  POHA.type_lookup_code = 'BLANKET'
8035      AND  POHA.global_agreement_flag = 'Y';
8036 
8037 -- <GC FPJ START>
8038 CURSOR std_gc_ref_cursor (p_doc_id NUMBER) IS
8039   SELECT 'Y'
8040   FROM   po_lines_gt POL,
8041          po_headers_all POHA
8042   WHERE  POL.po_header_id = p_doc_id
8043   AND    POL.contract_id = POHA.po_header_id
8044   AND    POHA.global_agreement_flag = 'Y';
8045 -- <GC FPJ END>
8046 
8047 BEGIN
8048 
8049 l_progress := '000';
8050 
8051     --check if atleast one line of Standard PO has Global Agreement reference
8052     --if so then call check_std_global_ref
8053     OPEN std_ga_ref_cursor(p_document_id);
8054 
8055     FETCH std_ga_ref_cursor INTO l_is_ga_referenced;
8056 
8057     CLOSE std_ga_ref_cursor;
8058 
8059     IF l_is_ga_referenced = 'Y' THEN
8060 l_progress := '001';
8061 IF g_debug_stmt THEN
8062    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
8063      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
8064           || l_progress,'PO STD: GA is referenced. Call GA checks');
8065    END IF;
8066 END IF;
8067 
8068        check_std_global_ref(p_document_id ,
8069                        p_online_report_id ,
8070                        p_user_id ,
8071                        p_login_id ,
8072                        p_sequence,
8073                        l_return_status);
8074 
8075         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
8076              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8077         END IF;
8078 
8079     END IF;
8080 
8081     -- <GC FPJ START>
8082     -- If there exists a line referencing global contract, make sure that
8083     -- the reference is valid by calling check_std_gc_ref
8084 
8085     OPEN std_gc_ref_cursor (p_document_id);
8086     FETCH std_gc_ref_cursor INTO l_is_gc_referenced;
8087     CLOSE std_gc_ref_cursor;
8088 
8089     IF (l_is_gc_referenced = 'Y') THEN
8090 
8091         check_std_gc_ref
8092         (  p_document_id      => p_document_id,
8093            p_online_report_id => p_online_report_id,
8094            p_user_id          => p_user_id,
8095            p_login_id         => p_login_id,
8096            x_sequence         => p_sequence,
8097            x_return_status    => l_return_status
8098         );
8099 
8100         IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
8101             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8102         END IF;
8103     END IF;
8104 
8105     -- <GC FPJ END>
8106 
8107 l_progress := '002';
8108 
8109     --Call Consinged checks
8110     check_std_consigned_ref(p_document_id ,
8111                        p_online_report_id ,
8112                        p_user_id ,
8113                        p_login_id ,
8114                        p_sequence,
8115                        l_return_status);
8116 
8117     IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
8118         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8119     END IF;
8120 
8121 l_progress := '003';
8122 
8123     -- Bug 3379488: Removed the Dest OU check for shipments with expense
8124     -- destinations with project-specified, because this scenario is prevented
8125     -- at an early stage and would not happen here
8126 
8127     x_return_status := FND_API.G_RET_STS_SUCCESS;
8128 EXCEPTION
8129     WHEN FND_API.G_EXC_ERROR THEN
8130         x_return_status := FND_API.G_RET_STS_ERROR;
8131 
8132     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8133         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8134 
8135     WHEN OTHERS THEN
8136         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8137         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
8138             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
8139         END IF;
8140 
8141         IF (g_debug_unexp) THEN
8142                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
8143                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
8144                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
8145                        || l_progress || ' SQL CODE is '||sqlcode);
8146                 END IF;
8147         END IF;
8148 END CHECK_STANDARD_PO;
8149 
8150 
8151 /**
8152 * Private Procedure: CHECK_STD_CONSIGNED_REF
8153 * Requires:
8154 *   IN PARAMETERS:
8155 *       p_document_id:      The requisition_header_id of submitted document
8156 *       p_online_report_id: Id used to INSERT INTO online_report_text table
8157 *       p_user_id:          User performing the action
8158 *       p_login_id:         Last update login_id
8159 *   IN OUT PARAMETERS
8160 *       p_sequence:          Sequence number of last reported error
8161 * Modifies: Inserts error msgs in online_report_text_gt table, uses
8162 *           global_temp tables for processing
8163 * Effects:  This procedure performs checks for the Consigned from Supplier
8164 *           project in order to enforce the following:
8165 *           1) Consigned status on any new or modified shipment that is not
8166 *              partially received or partially invoiced should match the
8167 *              consigned setting on the corresponding ASL entry.
8168 *           2) Document references cannot exist for PO lines with one or
8169 *              more consigned shipments
8170 * Returns:
8171 *  p_sequence: This parameter contains the current count of number of error
8172 *              messages inserted
8173 *  x_return_status: This parameter signifies whether the checks contained in
8174 *                   this procedure completed successfully.
8175 */
8176 PROCEDURE check_std_consigned_ref(p_document_id IN NUMBER,
8177                        p_online_report_id IN NUMBER,
8178                        p_user_id IN NUMBER,
8179                        p_login_id IN NUMBER,
8180                        p_sequence IN OUT NOCOPY NUMBER,
8181                        x_return_status OUT NOCOPY VARCHAR2) IS
8182 
8183 l_api_name  CONSTANT varchar2(40) := 'CHECK_STD_CONSIGNED_REF';
8184 l_progress VARCHAR2(3);
8185 
8186 l_textline  po_online_report_text.text_line%TYPE    := NULL;
8187 l_consigned_from_supplier_flag
8188 po_asl_attributes.consigned_from_supplier_flag%TYPE := NULL;
8189 l_enable_vmi_flag
8190 po_asl_attributes.enable_vmi_flag%TYPE              := NULL;
8191 l_last_billing_date            DATE                 := NULL;
8192 l_consigned_billing_cycle      NUMBER               := NULL;
8193 l_consigned_mismatch_found     BOOLEAN              := FALSE;
8194 l_count_expense_dist           NUMBER               := NULL;
8195 
8196 TYPE NumTab is TABLE of NUMBER INDEX by BINARY_INTEGER;
8197 TYPE AslConsignedFlagTab is TABLE of
8198 po_asl_attributes.consigned_from_supplier_flag%TYPE
8199 INDEX by BINARY_INTEGER;
8200 TYPE ConsignedFlagTab is TABLE of
8201 po_line_locations.consigned_flag%TYPE
8202 INDEX by BINARY_INTEGER;
8203 
8204 l_vendor_id          NumTab;
8205 l_vendor_site_id     NumTab;
8206 l_item_id            NumTab;
8207 l_ship_to_org_id     NumTab;
8208 l_line_num           NumTab;
8209 l_shipment_num       NumTab;
8210 l_line_location_id   NumTab;
8211 l_consigned_flag     ConsignedFlagTab;
8212 l_asl_consigned_flag AslConsignedFlagTab;
8213 l_return_status      varchar2(1)            := NULL;
8214 l_msg_count          number                 := NULL;
8215 l_msg_data           varchar2(2000)         := NULL;
8216 
8217 BEGIN
8218 
8219 l_progress := '000';
8220 IF g_debug_stmt THEN
8221    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
8222      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
8223           || l_progress,
8224           'PO STD CONSIGNED 1: Doc Ref and Consigned ship cannot coexist');
8225    END IF;
8226 END IF;
8227 
8228   -- Check 1: PO_SUP_CONS_DOC_REF_COEXIST
8229   -- Organization cannot be used to create a consigned shipment because
8230   -- order line contains a document reference. Enter a new order line
8231   -- for this shipment.
8232 
8233   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUP_CONS_DOC_REF_COEXIST');
8234 
8235   INSERT INTO
8236     po_online_report_text_gt(online_report_id,
8237                        last_update_login,
8238            last_updated_by,
8239                  last_update_date,
8240            created_by,
8241            creation_date,
8242            line_num,
8243            shipment_num,
8244            distribution_num,
8245            sequence,
8246            text_line,
8247                              message_name)
8248      -- SQL What: Selects any PO Line with at least one consigned shipment
8249      --           and a document reference or contract
8250      -- SQL Why: Document references and consigned shipments cannot exist
8251      --          for the same PO Line
8252      -- SQL Join: po_line_id
8253     SELECT p_online_report_id,
8254      p_login_id,
8255      p_user_id,
8256          sysdate,
8257      p_user_id,
8258      sysdate,
8259            pol.line_num,
8260      pll.shipment_num,
8261      0,
8262            p_sequence + ROWNUM,
8263      substr(g_linemsg||g_delim||POL.line_num||g_delim||g_shipmsg
8264      ||g_delim||PLL.shipment_num||':'||g_delim||l_textline,1,240),
8265      'PO_DOC_REF_SUP_CONS_COEXIST'
8266     FROM   po_lines_gt pol,
8267      po_line_locations_gt pll
8268     WHERE  pol.po_header_id = p_document_id
8269     AND    pol.po_line_id = pll.po_line_id
8270     AND    pll.shipment_type = 'STANDARD'
8271     AND    pll.consigned_flag = 'Y'
8272     AND    nvl(pol.cancel_flag,'N') = 'N'
8273     AND    nvl(pol.closed_code,'OPEN') <> 'FINALLY CLOSED'
8274     AND   (pol.oke_contract_header_id is not null or
8275            pol.oke_contract_version_id is not null or
8276      pol.from_header_id is not null or
8277      pol.from_line_id is not null or
8278      pol.contract_num is not null);
8279 
8280   --Increment the p_sequence with number of errors reported in last query
8281   p_sequence := p_sequence + SQL%ROWCOUNT;
8282 --------------------------------------
8283 
8284 l_progress := '002';
8285 IF g_debug_stmt THEN
8286    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
8287      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
8288           || l_progress,
8289           'PO STD CONSIGNED 2: Consigned status on shipments and ASL must match');
8290    END IF;
8291 END IF;
8292 
8293   -- Check 2: PO_SUP_CONS_STATUS_MISMATCH
8294   -- Consigned attribute and the ASL consigned attribute do not match.
8295   -- Shipment line must be cancelled and re-entered.'
8296 
8297   -- SQL What: Collect the vendor_id, vendor_site_id, item_id, ship_to_organization_id and
8298   --           consigned_flag into pl/sql tables, get the consigned_from_supplier_flag on
8299   --           the corresponding ASL entry for each shipment and match it to the consigned
8300   --           flag of the shipment.  The consigned_flag of the shipments are stored in a
8301   --           PL/SQL table.  If the item/supplier/supplier site/organization combination
8302   --           of the current shipment matches with that of the previous shipment, get the
8303   --           consigned_flag from the previous shipment instead of calling
8304   --           get_asl_attributes.  This can be done since the info of each shipment of
8305   --           the current PO to be approved is ordered by the vendor_id, vendor_site_id,
8306   --           item_id and ship_to_organization_id, in the bulk collct select statement.
8307   -- SQL Why: Consigned status on shipments and that on ASL must match
8308   -- SQL Join: po_header_id, po_line_id
8309 
8310   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUP_CONS_STATUS_MISMATCH');
8311 
8312   SELECT  POH.vendor_id,
8313           POH.vendor_site_id,
8314           POL.item_id,
8315     PLL.ship_to_organization_id,
8316     PLL.consigned_flag,
8317     POL.line_num,
8318     PLL.shipment_num,
8319           PLL.line_location_id
8320   BULK COLLECT INTO
8321           l_vendor_id,
8322           l_vendor_site_id,
8323           l_item_id,
8324           l_ship_to_org_id,
8325           l_consigned_flag,
8326     l_line_num,
8327     l_shipment_num,
8328           l_line_location_id
8329   FROM    PO_HEADERS_GT POH,
8330           PO_LINE_LOCATIONS_GT PLL,
8331           PO_LINES_GT POL
8332   WHERE   POH.po_header_id = p_document_id AND
8333           POH.po_header_id = POL.po_header_id AND
8334 --          POH.po_header_id = PLL.po_header_id AND
8335           POL.po_line_id = PLL.po_line_id AND
8336           PLL.shipment_type = 'STANDARD' AND
8337           nvl(PLL.cancel_flag,'N') = 'N' AND
8338           nvl(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED' AND
8339           -- Bug fix for #2733398
8340           -- nvl(PLL.approved_flag, 'N') IN ('N','R') AND
8341           nvl(PLL.approved_flag, 'N') = 'N' AND
8342           PLL.quantity_received <= 0 AND
8343           PLL.quantity_billed <= 0
8344   ORDER BY
8345           POH.vendor_id,
8346           POH.vendor_site_id,
8347           POL.item_id,
8348     PLL.ship_to_organization_id;
8349 
8350 l_progress := '003';
8351   FOR i IN 1..l_line_num.COUNT
8352   LOOP
8353 
8354     IF(i > 1 AND
8355        l_vendor_id(i) = l_vendor_id(i - 1) AND
8356        l_vendor_site_id(i) = l_vendor_site_id(i - 1) AND
8357        l_item_id(i) = l_item_id(i - 1) AND
8358        l_ship_to_org_id(i) = l_ship_to_org_id(i - 1))
8359     THEN
8360       l_asl_consigned_flag(i) := l_asl_consigned_flag(i - 1);
8361     ELSE
8362       -- getting the consigned setting of the ASL entry for the
8363       -- item/supplier/supplier site/organization combination,
8364       -- if there exists one
8365       po_third_party_stock_grp.get_asl_attributes
8366       (p_api_version                  => 1.0                           ,
8367        p_init_msg_list                => NULL                          ,
8368        x_return_status                => l_return_status               ,
8369        x_msg_count                    => l_msg_count                   ,
8370        x_msg_data                     => l_msg_data                    ,
8371        p_inventory_item_id            => l_item_id(i)                  ,
8372        p_vendor_id                    => l_vendor_id(i)                ,
8373        p_vendor_site_id               => l_vendor_site_id(i)           ,
8374        p_using_organization_id        => l_ship_to_org_id(i)           ,
8375        x_consigned_from_supplier_flag => l_consigned_from_supplier_flag,
8376        x_enable_vmi_flag              => l_enable_vmi_flag             ,
8377        x_last_billing_date            => l_last_billing_date           ,
8378        x_consigned_billing_cycle      => l_consigned_billing_cycle     );
8379 
8380       l_asl_consigned_flag(i) := l_consigned_from_supplier_flag;
8381     END IF;
8382 
8383 l_progress := '004';
8384     IF(NVL(l_consigned_flag(i), 'N') <> NVL(l_asl_consigned_flag(i), 'N'))
8385     THEN
8386       -- Bug fix for #2701648
8387       -- Do not report consigned status mismatch for the case when the ASL
8388       -- is consigned while the shipment is not consigned, if there exists
8389       -- distributions with destination type being EXPENSE
8390       IF(NVL(l_consigned_flag(i), 'N') = 'N' AND
8391          NVL(l_asl_consigned_flag(i), 'N') = 'Y')
8392       THEN
8393         SELECT count('Y')
8394         INTO   l_count_expense_dist
8395         FROM   DUAL
8396         WHERE  EXISTS(SELECT 'Y'
8397                       FROM   PO_DISTRIBUTIONS_GT
8398                       WHERE  LINE_LOCATION_ID = l_line_location_id(i)
8399                       AND    DESTINATION_TYPE_CODE = 'EXPENSE');
8400 
8401         IF(l_count_expense_dist <= 0)
8402         THEN
8403           l_consigned_mismatch_found := TRUE;
8404         ELSE
8405           l_consigned_mismatch_found := FALSE;
8406         END IF;
8407       ELSE
8408         l_consigned_mismatch_found := TRUE;
8409       END IF;
8410     -- if the consigned status matches
8411     ELSE
8412       l_consigned_mismatch_found := FALSE;
8413     END IF;
8414 
8415     -- insert an error into the report table if the consigned status on the
8416     -- ASL and the shipment does not match
8417     IF(l_consigned_mismatch_found)
8418     THEN
8419       INSERT INTO
8420         po_online_report_text_gt
8421         (online_report_id,
8422          last_update_login,
8423    last_updated_by,
8424    last_update_date,
8425    created_by,
8426    creation_date,
8427    line_num,
8428    shipment_num,
8429    distribution_num,
8430    sequence,
8431    text_line,
8432          message_name)
8433       VALUES
8434   (p_online_report_id,
8435    p_login_id,
8436    p_user_id,
8437    sysdate,
8438    p_user_id,
8439    sysdate,
8440          l_line_num(i),
8441    l_shipment_num(i),
8442    0,
8443    p_sequence + i,
8444          substr(g_linemsg||g_delim||l_line_num(i)||g_delim||g_shipmsg
8445                 ||g_delim||l_shipment_num(i)||':'||g_delim||l_textline,1,240),
8446    'PO_SUP_CONS_STATUS_MISMATCH');
8447 
8448     END IF;
8449 
8450   END LOOP;
8451 
8452   --Increment the p_sequence with number of errors reported in last query
8453   p_sequence := p_sequence + l_line_num.COUNT;
8454 
8455 l_progress := '005';
8456     x_return_status := FND_API.G_RET_STS_SUCCESS;
8457 EXCEPTION
8458     WHEN FND_API.G_EXC_ERROR THEN
8459         x_return_status := FND_API.G_RET_STS_ERROR;
8460 
8461     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8462         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8463 
8464     WHEN OTHERS THEN
8465         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8466         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
8467             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
8468         END IF;
8469 
8470         IF (g_debug_unexp) THEN
8471                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
8472                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
8473                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
8474                        || l_progress || ' SQL CODE is '||sqlcode);
8475                 END IF;
8476         END IF;
8477 
8478 END CHECK_STD_CONSIGNED_REF;
8479 
8480 
8481 --For Standard POs
8482 /**
8483 * Private Procedure: CHECK_STD_GLOBAL_REF
8484 * Requires:
8485 *   IN PARAMETERS:
8486 *       p_document_id:      The requisition_header_id of submitted document
8487 *       p_online_report_id: Id used to INSERT INTO online_report_text table
8488 *       p_user_id:          User performing the action
8489 *       p_login_id:         Last update login_id
8490 *   IN OUT PARAMETERS
8491 *       p_sequence:          Sequence number of last reported error
8492 * Modifies: Inserts error msgs in online_report_text_gt table, uses
8493 *           global_temp tables for processing
8494 * Effects:  This procedure runs the document submission checks for Standard
8495 *           POs which have GLOBAL AGREEMENTS reference
8496 * Returns:
8497 *  p_sequence: This parameter contains the current count of number of error
8498 *              messages inserted
8499 */
8500 PROCEDURE check_std_global_ref(p_document_id IN NUMBER,
8501                        p_online_report_id IN NUMBER,
8502                        p_user_id IN NUMBER,
8503                        p_login_id IN NUMBER,
8504                        p_sequence IN OUT NOCOPY NUMBER,
8505                        x_return_status OUT NOCOPY VARCHAR2) IS
8506 
8507 l_textline  po_online_report_text.text_line%TYPE := NULL;
8508 l_api_name  CONSTANT varchar2(40) := 'CHECK_STD_GLOBAL_REF';
8509 l_progress VARCHAR2(3);
8510 l_currency_mismatch BOOLEAN := FALSE; -- Bug 2716769
8511 
8512 --<Bug 2800804, 2792477 mbhargav START>
8513 TYPE NumTab is TABLE of NUMBER INDEX by BINARY_INTEGER;
8514 
8515   l_curr_doc_line_num NumTab;
8516   l_prev_doc_line_num NumTab;
8517   l_rowcount Number :=0;
8518 
8519   l_po_amount NumTab;
8520   l_prev_rel_amount NumTab;
8521   l_amount_limit NumTab;
8522 --<Bug 2800804, 2792477 mbhargav END>
8523 
8524 BEGIN
8525 l_progress := '000';
8526 
8527 l_progress := '001';
8528 IF g_debug_stmt THEN
8529    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
8530      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
8531           || l_progress,'PO STD GA 1: GA enabled in current OU');
8532    END IF;
8533 END IF;
8534 
8535     --<Shared Proc FPJ>
8536     --Check 1: The GA should be enabled for purchasing in the current OU.
8537     --< Shared Proc FPJ > Bug 3301427: Only do this check for new SPO lines
8538     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_STD_GA_DISABLED');
8539     INSERT into po_online_report_text_gt(online_report_id,
8540                                          last_update_login,
8541                                          last_updated_by,
8542                                          last_update_date,
8543                                          created_by,
8544                                          creation_date,
8545                                          line_num,
8546                                          shipment_num,
8547                                          distribution_num,
8548                                          sequence,
8549                                          text_line,
8550                                          message_name)
8551     --<Shared Proc FPJ>
8552     --SQL Querying for PO lines that reference GAs that are not enabled
8553     --SQL for purchasing in this org, to report an error message.
8554     SELECT  p_online_report_id,
8555             p_login_id,
8556             p_user_id,
8557             sysdate,
8558             p_user_id,
8559             sysdate,
8560             POL.line_num,
8561             0,
8562             0,
8563             p_sequence + ROWNUM,
8564             substr(g_linemsg||g_delim||POL.line_num||g_delim
8565                    ||l_textline,1,240),
8566             'PO_SUB_STD_GA_DISABLED'
8567     FROM PO_HEADERS_GT POH1, PO_LINES_GT POL, PO_HEADERS_ALL POH
8568     WHERE POH1.po_header_id = p_document_id
8569     AND POL.po_header_id = POH1.po_header_id
8570     AND POL.from_header_id = POH.po_header_id  --JOIN
8571     AND POH.type_lookup_code = 'BLANKET'
8572     AND POH.global_agreement_flag = 'Y'
8573     --<Shared Proc FPJ START>
8574     AND NOT EXISTS                                  --< Bug 3301427 Start >
8575         (SELECT 'previously approved shipment'
8576            FROM po_line_locations_gt pllg
8577           WHERE pllg.po_line_id = pol.po_line_id
8578             AND pllg.approved_date IS NOT NULL)     --< Bug 3301427 End >
8579     AND NOT EXISTS
8580         --SQL Query enabled org assignments of this current purchasing org
8581        (SELECT 'Enabled purchasing org'
8582           FROM PO_GA_ORG_ASSIGNMENTS PGOA
8583          WHERE PGOA.po_header_id = POH.po_header_id
8584            AND PGOA.purchasing_org_id = POH1.org_id
8585            AND PGOA.enabled_flag = 'Y');
8586 
8587     --<Shared Proc FPJ END>
8588 
8589      --Increment the p_sequence with number of errors reported in last query
8590     p_sequence := p_sequence + SQL%ROWCOUNT;
8591 
8592 -----------------------------
8593 l_progress := '002';
8594 IF g_debug_stmt THEN
8595    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
8596      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
8597           || l_progress,'PO STD GA 2: Ref GA approved');
8598    END IF;
8599 END IF;
8600 
8601     --Check 2: If the standard PO line is referencing a GA then that GA
8602     --should be in an approved status.
8603     --< Shared Proc FPJ > Bug 3301427: Only do this check for new SPO lines
8604     --< Bug 3422733 > Only do this check if GA is not ON HOLD. The ON HOLD check
8605     -- is done later. Avoids showing 2 msgs for Contract that is ON HOLD.
8606 
8607     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_STD_GA_APPROVED');
8608     INSERT into po_online_report_text_gt(online_report_id,
8609                                          last_update_login,
8610                                          last_updated_by,
8611                                          last_update_date,
8612                                          created_by,
8613                                          creation_date,
8614                                          line_num,
8615                                          shipment_num,
8616                                          distribution_num,
8617                                          sequence,
8618                                          text_line,
8619                                          message_name)
8620     --SQL What: Querying for PO lines that reference GAs that are not approved.
8621     --SQL Why: Add appropriate error message to po_online_report_text_gt
8622     SELECT  p_online_report_id,
8623             p_login_id,
8624             p_user_id,
8625             sysdate,
8626             p_user_id,
8627             sysdate,
8628             POL.line_num,
8629             0,
8630             0,
8631             p_sequence + ROWNUM,
8632             substr(g_linemsg||g_delim||POL.line_num||g_delim
8633                    ||l_textline,1,240),
8634             'PO_SUB_STD_GA_APPROVED'
8635     FROM PO_HEADERS_GT POH1, PO_LINES_GT POL, PO_HEADERS_ALL POH2
8636     WHERE POH1.po_header_id = p_document_id
8637     AND POL.po_header_id = POH1.po_header_id   --JOIN
8638     AND POH2.po_header_id = POL.from_header_id --JOIN
8639     AND POH2.type_lookup_code = 'BLANKET'
8640     AND POH2.global_agreement_flag = 'Y'
8641     AND NVL(POH2.approved_flag, 'N') <> 'Y'
8642     AND NVL(POH2.user_hold_flag, 'N') <> 'Y'        --< Bug 3422733 >
8643     AND NOT EXISTS                                  --< Bug 3301427 Start >
8644         (SELECT 'previously approved shipment'
8645            FROM po_line_locations_gt pllg
8646           WHERE pllg.po_line_id = pol.po_line_id
8647             AND pllg.approved_date IS NOT NULL);    --< Bug 3301427 End >
8648 
8649     --Increment the p_sequence with number of errors reported in last query
8650     p_sequence := p_sequence + SQL%ROWCOUNT;
8651 
8652 --------------------------------------
8653 
8654 l_progress := '003';
8655 IF g_debug_stmt THEN
8656    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
8657      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
8658           || l_progress,'PO STD GA 3: Ref GA ON HOLD');
8659    END IF;
8660 END IF;
8661 
8662     --Check 3: The GA should not be on hold.
8663     --< Shared Proc FPJ > Bug 3301427: Only do this check for new SPO lines
8664     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_STD_GA_ON_HOLD');
8665     INSERT into po_online_report_text_gt(online_report_id,
8666                                          last_update_login,
8667                                          last_updated_by,
8668                                          last_update_date,
8669                                          created_by,
8670                                          creation_date,
8671                                          line_num,
8672                                          shipment_num,
8673                                          distribution_num,
8674                                          sequence,
8675                                          text_line,
8676                                          message_name)
8677     --SQL What: Querying for PO lines that reference GAs that are on hold
8678     --SQL Why: Add appropriate error message to po_online_report_text_gt
8679     SELECT  p_online_report_id,
8680             p_login_id,
8681             p_user_id,
8682             sysdate,
8683             p_user_id,
8684             sysdate,
8685             POL.line_num,
8686             0,
8687             0,
8688             p_sequence + ROWNUM,
8689             substr(g_linemsg||g_delim||POL.line_num||g_delim
8690                    ||l_textline,1,240),
8691             'PO_SUB_STD_GA_ON_HOLD'
8692     FROM PO_HEADERS_GT POH1, PO_LINES_GT POL, PO_HEADERS_ALL POH2
8693     WHERE POH1.po_header_id = p_document_id
8694     AND POL.po_header_id = POH1.po_header_id   --JOIN
8695     AND POH2.po_header_id = POL.from_header_id --JOIN
8696     AND POH2.type_lookup_code = 'BLANKET'
8697     AND POH2.global_agreement_flag = 'Y'
8698     AND POH2.user_hold_flag = 'Y'
8699     AND NOT EXISTS                                  --< Bug 3301427 Start >
8700         (SELECT 'previously approved shipment'
8701            FROM po_line_locations_gt pllg
8702           WHERE pllg.po_line_id = pol.po_line_id
8703             AND pllg.approved_date IS NOT NULL);    --< Bug 3301427 End >
8704 
8705      --Increment the p_sequence with number of errors reported in last query
8706     p_sequence := p_sequence + SQL%ROWCOUNT;
8707 
8708 ------------------------------------------
8709 
8710    -- Bug 2716769 tpoon 12/26/2002
8711    -- In version 115.8, moved Checks 4 and 5 to after the currency check.
8712    -- Re-numbered checks 4-10 accordingly.
8713 
8714 l_progress := '004';
8715 IF g_debug_stmt THEN
8716    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
8717      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
8718           || l_progress,'PO STD GA 4: Vendor match check');
8719    END IF;
8720 END IF;
8721 
8722     --Check 4: The vendor on the PO should match the GA.
8723     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_STD_GA_VENDOR_MISMATCH');
8724     INSERT into po_online_report_text_gt(online_report_id,
8725                                          last_update_login,
8726                                          last_updated_by,
8727                                          last_update_date,
8728                                          created_by,
8729                                          creation_date,
8730                                          line_num,
8731                                          shipment_num,
8732                                          distribution_num,
8733                                          sequence,
8734                                          text_line,
8735                                          message_name)
8736     --SQL What: Querying for PO lines whose vendor does not
8737     --SQL       match the vendor of the referenced GA
8738     --SQL Why: Add appropriate error message to po_online_report_text_gt
8739     SELECT  p_online_report_id,
8740             p_login_id,
8741             p_user_id,
8742             sysdate,
8743             p_user_id,
8744             sysdate,
8745             POL.line_num,
8746             0,
8747             0,
8748             p_sequence + ROWNUM,
8749             substr(g_linemsg||g_delim||POL.line_num||g_delim
8750                    ||l_textline,1,240),
8751             'PO_SUB_STD_GA_VENDOR_MISMATCH'
8752     FROM PO_LINES_GT POL, PO_HEADERS_GT POH1, PO_HEADERS_ALL POH2
8753     WHERE POL.po_header_id = p_document_id
8754     AND POL.po_header_id = POH1.po_header_id      --JOIN
8755     AND POL.from_header_id = POH2.po_header_id    --JOIN
8756     AND POH2.type_lookup_code = 'BLANKET'
8757     AND POH2.global_agreement_flag = 'Y'
8758     AND POH1.vendor_id <> POH2.vendor_id;
8759 
8760      --Increment the p_sequence with number of errors reported in last query
8761     p_sequence := p_sequence + SQL%ROWCOUNT;
8762 
8763 -------------------------------------
8764 
8765 l_progress := '005';
8766 IF g_debug_stmt THEN
8767    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
8768      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
8769           || l_progress,'PO STD GA 5: Vendor Site match check');
8770    END IF;
8771 END IF;
8772 
8773     --Check 5: The vendor site on the PO should match a vendor site on one of
8774     -- the GA's enabled org assignments.
8775     --< Shared Proc FPJ > Bug 3301427: Only do this check for new SPO lines
8776     l_textline :=
8777         FND_MESSAGE.GET_STRING('PO', 'PO_SUB_STD_GA_VDR_SITE_MISMT');
8778     INSERT into po_online_report_text_gt(online_report_id,
8779                                          last_update_login,
8780                                          last_updated_by,
8781                                          last_update_date,
8782                                          created_by,
8783                                          creation_date,
8784                                          line_num,
8785                                          shipment_num,
8786                                          distribution_num,
8787                                          sequence,
8788                                          text_line,
8789                                          message_name)
8790     --SQL What: Querying for PO lines whose vendor_site_id does not
8791     --SQL       match a valid vendor_site_id in the GA's org assignments
8792     --SQL Why: Add appropriate error message to po_online_report_text_gt
8793     SELECT  p_online_report_id,
8794             p_login_id,
8795             p_user_id,
8796             sysdate,
8797             p_user_id,
8798             sysdate,
8799             POL.line_num,
8800             0,
8801             0,
8802             p_sequence + ROWNUM,
8803             substr(g_linemsg||g_delim||POL.line_num||g_delim
8804                    ||l_textline,1,240),
8805             'PO_SUB_STD_GA_VDR_SITE_MISMT'
8806     FROM PO_LINES_GT POL, PO_HEADERS_GT POH1, PO_HEADERS_ALL POH2
8807     WHERE POL.po_header_id = p_document_id
8808     AND POL.po_header_id = POH1.po_header_id            --JOIN
8809     AND POL.from_header_id = POH2.po_header_id          --JOIN
8810     AND POH2.type_lookup_code = 'BLANKET'
8811     AND POH2.global_agreement_flag = 'Y'
8812     --<Shared Proc FPJ START>
8813     AND NOT EXISTS                                  --< Bug 3301427 Start >
8814         (SELECT 'previously approved shipment'
8815            FROM po_line_locations_gt pllg
8816           WHERE pllg.po_line_id = pol.po_line_id
8817             AND pllg.approved_date IS NOT NULL)     --< Bug 3301427 End >
8818     AND NOT EXISTS
8819         (SELECT 'Enabled vendor site'
8820            FROM PO_GA_ORG_ASSIGNMENTS pgoa
8821           WHERE PGOA.po_header_id = POH2.po_header_id
8822             AND PGOA.vendor_site_id = POH1.vendor_site_id
8823             AND PGOA.enabled_flag = 'Y');
8824 
8825     --<Shared Proc FPJ END>
8826 
8827      --Increment the p_sequence with number of errors reported in last query
8828     p_sequence := p_sequence + SQL%ROWCOUNT;
8829 
8830 ---------------------------------------
8831 
8832 l_progress := '006';
8833 IF g_debug_stmt THEN
8834    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
8835      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
8836           || l_progress,'PO STD GA 6: Creation date check on header');
8837    END IF;
8838 END IF;
8839 
8840     --Check 6: The PO creation date should be before Blanket(GA) or
8841     -- corresponding Line expiration date
8842     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_STD_AFTER_GA_DATE');
8843     INSERT into po_online_report_text_gt(online_report_id,
8844                                          last_update_login,
8845                                          last_updated_by,
8846                                          last_update_date,
8847                                          created_by,
8848                                          creation_date,
8849                                          line_num,
8850                                          shipment_num,
8851                                          distribution_num,
8852                                          sequence,
8853                                          text_line,
8854                                          message_name)
8855     --SQL What: Querying for PO lines that were not created within
8856     --SQL       the effective dates of the referenced GA
8857     --SQL Why: Add appropriate error message to po_online_report_text_gt
8858     SELECT  p_online_report_id,
8859             p_login_id,
8860             p_user_id,
8861             sysdate,
8862             p_user_id,
8863             sysdate,
8864             POL.line_num,
8865             PLL.shipment_num, -- Bug #5415428 As Need by date also forms part of the checks
8866             0,
8867             p_sequence + ROWNUM,
8868             substr(g_linemsg||g_delim||POL.line_num||g_delim
8869                    ||l_textline,1,240),
8870             'PO_SUB_STD_AFTER_GA_DATE'
8871     FROM PO_LINES_GT POL, PO_HEADERS_GT POH, PO_HEADERS_ALL POH2, PO_LINES_ALL POL2, PO_LINE_LOCATIONS_GT PLL
8872     WHERE POL.po_header_id = p_document_id
8873     AND POL.po_header_id = POH.po_header_id -- JOIN
8874     AND PLL.po_line_id = POL.po_line_id  --JOIN, Bug #5415428 - Get the Need by date
8875     AND POL.from_header_id = POH2.po_header_id --JOIN
8876     AND POL.from_line_id = POL2.po_line_id     --JOIN
8877     AND POH2.type_lookup_code = 'BLANKET'
8878     AND POH2.global_agreement_flag = 'Y'
8879 --Bug #2699630: Adding trunc on both sides of the check
8880 --Bug #5415428: Start date and need by date also needs to be considered
8881     AND (NVL(TRUNC(PLL.need_by_date),NVL(TRUNC(POL.start_date), TRUNC(POH.creation_date)))
8882          > NVL ( TRUNC(POL2.expiration_date) , TRUNC(POH2.end_date)));
8883 
8884      --Increment the p_sequence with number of errors reported in last query
8885     p_sequence := p_sequence + SQL%ROWCOUNT;
8886 
8887 ------------------------------------
8888 l_progress := '007';
8889 IF g_debug_stmt THEN
8890    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
8891      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
8892           || l_progress,'PO STD GA 7: Need-by-date and Creation date check');
8893    END IF;
8894 END IF;
8895 
8896     --Check 7: The Need-by-date (or if NULL the PO creation date) should be
8897     -- after the start dates of the GA
8898     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_STD_BEFORE_GA_DATE');
8899     INSERT into po_online_report_text_gt(online_report_id,
8900                                          last_update_login,
8901                                          last_updated_by,
8902                                          last_update_date,
8903                                          created_by,
8904                                          creation_date,
8905                                          line_num,
8906                                          shipment_num,
8907                                          distribution_num,
8908                                          sequence,
8909                                          text_line,
8910                                          message_name)
8911     --SQL What: Querying for PO lines that were not created within
8912     --SQL       the effective dates of the referenced GA
8913     --SQL Why: Add appropriate error message to po_online_report_text_gt
8914     SELECT  p_online_report_id,
8915             p_login_id,
8916             p_user_id,
8917             sysdate,
8918             p_user_id,
8919             sysdate,
8920             POL.line_num,
8921             PLL.shipment_num,
8922             0,
8923             p_sequence + ROWNUM,
8924             substr(g_linemsg||g_delim||POL.line_num||g_delim||
8925                    g_shipmsg||g_delim||PLL.shipment_num||g_delim||l_textline,1,240),
8926             'PO_SUB_STD_BEFORE_GA_DATE'
8927     FROM PO_LINES_GT POL, PO_HEADERS_GT POH, PO_HEADERS_ALL POH2, PO_LINE_LOCATIONS_GT PLL
8928     WHERE POL.po_header_id = p_document_id
8929     AND POL.po_header_id = POH.po_header_id
8930     AND   PLL.po_line_id = POL.po_line_id  --JOIN
8931     AND POL.from_header_id = POH2.po_header_id --JOIN
8932     AND POH2.type_lookup_code = 'BLANKET'
8933     AND POH2.global_agreement_flag = 'Y'
8934 --Bug #2699630: Adding trunc on both sides of the check
8935 --Bug #5415428: Start date also needs to be considered
8936     AND NVL(TRUNC(PLL.need_by_date), NVL(TRUNC(POL.start_date),TRUNC(POH.creation_date))) < TRUNC(POH2.start_date);
8937 
8938      --Increment the p_sequence with number of errors reported in last query
8939     p_sequence := p_sequence + SQL%ROWCOUNT;
8940 
8941 ---------------------------------
8942 l_progress := '008';
8943 IF g_debug_stmt THEN
8944    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
8945      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
8946           || l_progress,'PO STD GA 8: Currency code mismatch');
8947    END IF;
8948 END IF;
8949 
8950     --Check 8: The currency_code on the PO should match the GA.
8951     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_STD_GA_CURR_MISMATCH');
8952     INSERT into po_online_report_text_gt(online_report_id,
8953                                          last_update_login,
8954                                          last_updated_by,
8955                                          last_update_date,
8956                                          created_by,
8957                                          creation_date,
8958                                          line_num,
8959                                          shipment_num,
8960                                          distribution_num,
8961                                          sequence,
8962                                          text_line,
8963                                          message_name)
8964     --SQL What: Querying for PO lines whose currency code does not
8965     --SQL       match the currency code of the referenced GA
8966     --SQL Why: Add appropriate error message to po_online_report_text_gt
8967     SELECT  p_online_report_id,
8968             p_login_id,
8969             p_user_id,
8970             sysdate,
8971             p_user_id,
8972             sysdate,
8973             POL.line_num,
8974             0,
8975             0,
8976             p_sequence + ROWNUM,
8977             substr(g_linemsg||g_delim||POL.line_num||g_delim
8978                    ||l_textline,1,240),
8979             'PO_SUB_STD_GA_CURR_MISMATCH'
8980     FROM PO_LINES_GT POL, PO_HEADERS_GT POH1, PO_HEADERS_ALL POH2
8981     WHERE POL.po_header_id = p_document_id
8982     AND POL.po_header_id = POH1.po_header_id      --JOIN
8983     AND POL.from_header_id = POH2.po_header_id    --JOIN
8984     AND POH2.type_lookup_code = 'BLANKET'
8985     AND POH2.global_agreement_flag = 'Y'
8986     AND POH1.currency_code <> POH2.currency_code;
8987 
8988      --Increment the p_sequence with number of errors reported in last query
8989     p_sequence := p_sequence + SQL%ROWCOUNT;
8990 
8991     -- Bug 2716769 tpoon 12/26/2002
8992     -- If there is a currency mismatch, do not perform the currency-dependent
8993     -- checks below. These checks assume that the PO and the GA use the same
8994     -- currency.
8995     IF (SQL%ROWCOUNT > 0) THEN
8996       l_currency_mismatch := TRUE;
8997     END IF;
8998 
8999     -- Bug 2716769 tpoon 12/26/2002
9000     -- In version 115.8, the following checks were moved here. They will
9001     -- only be performed if the currency check is successful.
9002     --   Check 4: Total amount on PO shipments should be less than the GA
9003     --     amount limit.
9004     --   Check 5: If price override is yes, then the line unit price cannot
9005     --     be more than the price tolerance on the GA line.
9006     -- Re-numbered checks 4-10 accordingly.
9007 
9008 -----------------------------------------
9009 
9010     -- Bug 2716769 Only perform this check if PO and GA use the same currency
9011     IF (NOT l_currency_mismatch) THEN
9012 
9013       l_progress := '009';
9014 
9015       IF g_debug_stmt THEN
9016         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9017           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
9018           || l_progress,'PO STD GA 9: Amt limit check for GA header');
9019         END IF;
9020       END IF;
9021 
9022       --Check 9: The total amount on all the standard PO SHIPMENT lines in the
9023       --current setup referencing the same GA should be less than the
9024       --amount limit on the GA header
9025 
9026      --<Bug 2800804, 2792477 mbhargav END>
9027       --Split the amount calculation to two select statements,
9028       --this was required because iSP is sending some chnage_requests
9029       --which are only in GT tables so need to get the amount for
9030       --current document from GT table and not the base tables
9031 
9032       --Get the sum of all shipments of this PO which refer to same GA
9033       --as is referenced on given line
9034       -- Bug 2818810. Also get the amount limits for those GA's here. Removed
9035       -- the extra select statement that was retrieving the amount limit.
9036       SELECT /*+ FULL(POL) ORDERED */                         -- bug3413891
9037              POL.line_num
9038       ,      nvl ( decode ( POL.quantity                      -- <SERVICES FPJ>
9039                           , NULL , sum ( PLL1.amount
9040                                        - nvl(PLL1.amount_cancelled,0) )
9041                           ,        sum (   ( PLL1.quantity
9042                                            - nvl(PLL1.quantity_cancelled,0) )
9043                                        * PLL1.price_override )
9044                           )
9045                  , 0 )
9046       ,      POH1.amount_limit
9047       BULK COLLECT INTO l_curr_doc_line_num,l_po_amount,l_amount_limit
9048       FROM   PO_LINES_GT POL, PO_HEADERS_ALL POH1, PO_LINE_LOCATIONS_GT PLL1
9049       WHERE  PLL1.from_header_id = POL.from_header_id
9050       AND    POL.po_header_id = p_document_id
9051       AND    POL.from_header_id = POH1.po_header_id
9052       AND    POH1.type_lookup_code = 'BLANKET'
9053       AND    POH1.global_agreement_flag = 'Y'
9054       AND    POH1.amount_limit IS NOT NULL
9055       GROUP BY
9056              POL.line_num
9057       ,      POL.quantity                                     -- <SERVICES FPJ>
9058       ,      POH1.amount_limit;
9059 
9060       --Get the sum of all shipments of approved POs in current setup which refer to same
9061       --GA as is referenced on given line
9062       -- Bug 2818810. Added extra join to aliases POH1 and PLL to return rows of
9063       -- given lines with shipments that reference GA's with amount limits only.
9064 
9065       -- bug3413891
9066       -- 1) Added optimizer hint FULL(POL)
9067       -- 2) Removed the join to PO_LINE_LOCATIONS_GT as it will bring in wrong
9068       --    result if the standard PO line has multiple shipments
9069 
9070       SELECT /*+ FULL(POL) */
9071              POL.line_num
9072       ,      nvl ( decode ( POL.quantity                      -- <SERVICES FPJ>
9073                           , NULL , sum ( PLL2.amount
9074                                        - nvl(PLL2.amount_cancelled,0) )
9075                           ,        sum (   ( PLL2.quantity
9076                                            - nvl(PLL2.quantity_cancelled,0) )
9077                                        * PLL2.price_override )
9078                           )
9079                  , 0 )
9080       BULK COLLECT INTO l_prev_doc_line_num, l_prev_rel_amount
9081       FROM PO_LINE_LOCATIONS_ALL PLL2, PO_HEADERS_ALL POH1,
9082            PO_HEADERS_ALL POH2, PO_LINES_GT POL
9083       WHERE POL.po_header_id = p_document_id
9084       AND   POL.from_header_id = POH1.po_header_id    --JOIN
9085       AND   POH1.type_lookup_code = 'BLANKET'
9086       AND   POH1.global_agreement_flag = 'Y'
9087       AND   POH1.amount_limit IS NOT NULL
9088       AND   PLL2.from_header_id = POL.from_header_id  --JOIN
9089       AND   POH2.po_header_id = PLL2.po_header_id     --JOIN
9090       AND   nvl(POH2.approved_flag, 'N') = 'Y'
9091       AND   PLL2.po_header_id <> p_document_id
9092       GROUP BY
9093             POL.line_num
9094       ,     POL.quantity;                                     -- <SERVICES FPJ>
9095 
9096       --Addup the released amount as obtained from above sqls. This loop goes by prev_doc_line_num.COUNT
9097       --as its possible we may not have any previous POs cut against those GAs.
9098       FOR l_prev_index IN 1..l_prev_doc_line_num.COUNT LOOP
9099 
9100           -- Bug 2818810. Fixed looping and the indexing used to ensure that
9101           -- correct PO amounts are matched with existing PO amounts by adding
9102           -- an inner loop and correcting the IF condition.
9103           FOR l_index IN 1..l_curr_doc_line_num.COUNT LOOP
9104               --first check for line number matching before adding up
9105               IF l_curr_doc_line_num(l_index) = l_prev_doc_line_num(l_prev_index) THEN
9106                   -- Found match, so add prev amount and exit inner loop
9107                   l_po_amount(l_index) := l_po_amount(l_index)+l_prev_rel_amount(l_prev_index);
9108                   EXIT;
9109               END IF;
9110           END LOOP;
9111 
9112       END LOOP;
9113 
9114       l_textline :=
9115           FND_MESSAGE.GET_STRING('PO', 'PO_SUB_STD_AMT_GRT_GA_AMT_LMT');
9116 
9117       --Go through all lines in current PO (which refer to a GA)
9118       FOR l_curr_doc_line_index IN 1..l_curr_doc_line_num.COUNT LOOP
9119 
9120          --If the amount released and current amount is greater than amount limit then
9121          --raise an error for that line
9122          IF (l_amount_limit(l_curr_doc_line_index) < l_po_amount(l_curr_doc_line_index)) THEN
9123              l_rowcount := l_rowcount +1;
9124              -- Bug 2818810. Use l_curr_doc_line_num to retrieve the line number
9125              INSERT INTO po_online_report_text_gt(online_report_id,
9126                                 last_update_login,
9127                                 last_updated_by,
9128                                 last_update_date,
9129                                 created_by,
9130                                 creation_date,
9131                                 line_num,
9132                                 shipment_num,
9133                                 distribution_num,
9134                                 sequence,
9135                                 text_line,
9136                 message_name)
9137              VALUES(p_online_report_id,
9138                      p_login_id,
9139                      p_user_id,
9140                      sysdate,
9141                      p_user_id,
9142                      sysdate,
9143                      l_curr_doc_line_num(l_curr_doc_line_index),
9144                      0,
9145                      0,
9146                      p_sequence+ l_rowcount,
9147                      substr(g_linemsg||g_delim||l_curr_doc_line_num(l_curr_doc_line_index)||g_delim
9148                             ||l_textline,1,240),
9149                      'PO_SUB_STD_AMT_GRT_GA_AMT_LMT');
9150 
9151           END IF; --amount limit check
9152 
9153        END LOOP;  --end of going through each line on current PO
9154 
9155        --Increment the p_sequence with number of errors reported in last query
9156        p_sequence := p_sequence + l_rowcount;
9157        --<Bug 2800804, 2792477 mbhargav END>
9158 
9159    END IF; -- NOT l_currency_mismatch
9160 
9161 ------------------------------------------
9162 
9163     -- Bug 2716769 Only perform this check if PO and GA use the same currency
9164     IF (NOT l_currency_mismatch) THEN
9165 
9166       l_progress := '010';
9167       IF g_debug_stmt THEN
9168         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9169           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
9170           || l_progress,'PO STD GA 10: Price Tolerance check');
9171         END IF;
9172       END IF;
9173 
9174       --Check 10: If price override is yes then the line unit price cannot be more
9175       --than the price tolerance on the GA line.
9176       --
9177       -- Bug 3177525: This check should not be done if allow_price_override_flag
9178       -- is 'N' because the user can never modify the standard PO price/amount;
9179       -- it is automatically defaulted.
9180 
9181       INSERT into po_online_report_text_gt(online_report_id,
9182                                            last_update_login,
9183                                            last_updated_by,
9184                                            last_update_date,
9185                                            created_by,
9186                                            creation_date,
9187                                            line_num,
9188                                            shipment_num,
9189                                            distribution_num,
9190                                            sequence,
9191                                            text_line,
9192                                            message_name)
9193       --SQL What: Querying for PO line unit prices that exceed the
9194       --SQL       price tolerance on the GA line
9195       --SQL Why: Add appropriate error message to po_online_report_text_gt
9196       SELECT  p_online_report_id,
9197               p_login_id,
9198               p_user_id,
9199               sysdate,
9200               p_user_id,
9201               sysdate,
9202               POL1.line_num,
9203               0,
9204               0,
9205               p_sequence + ROWNUM,
9206               decode ( POL1.order_type_lookup_code             -- <BUG 3262859>
9207                      , 'FIXED PRICE' , PO_CORE_S.get_translated_text
9208                                        (   'PO_SUB_PO_LINE_GT_GA_AMT_TOL'
9209                                        ,   'LINE_NUM', POL1.line_num
9210                                        ,   'LINE_AMT', POL1.amount
9211                                        ,   'AMT_TOL' , nvl ( POL2.not_to_exceed_price
9212                                                      , POL1.amount )
9213                                        )
9214                      ,                 PO_CORE_S.get_translated_text
9215                                        (   'PO_SUB_PO_LINE_GT_GA_PRICE_TOL'
9216                                        ,   'LINE_NUM'  , POL1.line_num
9217                                        ,   'LINE_PRICE', POL1.unit_price
9218                                        ,   'PRICE_TOL' , nvl ( POL2.not_to_exceed_price
9219                                        , POL1.unit_price )
9220                                        )
9221                      ),
9222               decode ( POL1.order_type_lookup_code             -- <BUG 3262859>
9223                      , 'FIXED PRICE' , 'PO_SUB_PO_LINE_GT_GA_AMT_TOL'
9224                      ,                 'PO_SUB_PO_LINE_GT_GA_PRICE_TOL'
9225                      )
9226       FROM PO_LINES_GT POL1, PO_LINES_ALL POL2, PO_HEADERS_ALL POH
9227       WHERE POL1.po_header_id = p_document_id
9228       AND POH.po_header_id = POL1.from_header_id --JOIN
9229       AND POH.type_lookup_code = 'BLANKET'
9230       AND POH.global_agreement_flag = 'Y'
9231       AND POL1.from_line_id = POL2.po_line_id     --JOIN
9232       AND nvl(POL1.cancel_flag,'N')= 'N'
9233       AND nvl(POL1.closed_code,'OPEN') <> 'FINALLY CLOSED'
9234       AND POL2.allow_price_override_flag = 'Y'                -- Bug 3177525
9235       AND (                                                   -- <SERVICES FPJ>
9236               (   ( POL1.order_type_lookup_code IN ('QUANTITY','AMOUNT','RATE') ) -- <BUG 3262859>
9237               AND ( POL1.unit_price > nvl ( POL2.not_to_exceed_price
9238                                           , POL1.unit_price )
9239                   )
9240               )
9241           OR
9242               (   ( POL1.order_type_lookup_code IN ('FIXED PRICE') )           -- <BUG 3262859>
9243               AND ( POL1.amount > nvl( POL2.not_to_exceed_price, POL1.amount ) )
9244               )
9245           );
9246 
9247        --Increment the p_sequence with number of errors reported in last query
9248       p_sequence := p_sequence + SQL%ROWCOUNT;
9249 
9250     END IF; -- NOT l_currency_mismatch
9251 
9252 -------------------------------------------
9253 
9254     -- Bug 2716769 Only perform this check if PO and GA use the same currency
9255     IF (NOT l_currency_mismatch) THEN
9256 
9257       l_progress := '011';
9258       IF g_debug_stmt THEN
9259         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9260           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
9261           || l_progress,'PO STD GA 11: Min Release Amount at line level check');
9262         END IF;
9263       END IF;
9264 
9265       --Check 11: The total amount on all the lines on the standard PO
9266       --referencing same GA line should be greater than the minimum release amount
9267       --on the GA line.
9268       l_textline :=                                        -- <2710030>
9269           FND_MESSAGE.GET_STRING('PO','PO_SUB_STD_GA_LINE_LESS_MINREL');
9270       INSERT into po_online_report_text_gt(online_report_id,
9271                                            last_update_login,
9272                                            last_updated_by,
9273                                            last_update_date,
9274                                            created_by,
9275                                            creation_date,
9276                                            line_num,
9277                                            shipment_num,
9278                                            distribution_num,
9279                                            sequence,
9280                                            text_line,
9281                                            message_name)
9282       --SQL What: Querying for PO shipment totals that fail to meet the
9283       --SQL       minimum release amount of the GA line
9284       --SQL Why: Add appropriate error message to po_online_report_text_gt
9285       SELECT  p_online_report_id,
9286               p_login_id,
9287               p_user_id,
9288               sysdate,
9289               p_user_id,
9290               sysdate,
9291               POL1.line_num,
9292               0,
9293               0,
9294               p_sequence + ROWNUM,
9295               substr(g_linemsg||g_delim||POL1.line_num||g_delim
9296                      ||l_textline,1,240),
9297               'PO_SUB_STD_GA_LINE_LESS_MINREL'                      -- <2710030>
9298       FROM PO_LINES_GT POL1, PO_LINES_ALL POL2, PO_HEADERS_ALL POH
9299       WHERE POL1.po_header_id = p_document_id
9300       AND POL1.from_header_id = POL2.po_header_id  --JOIN
9301       AND POL1.from_line_id = POL2.po_line_id      --JOIN
9302       AND POL1.from_header_id = POH.po_header_id   --JOIN
9303       AND POH.type_lookup_code = 'BLANKET'
9304       AND POH.global_agreement_flag = 'Y'
9305       AND POL2.min_release_amount IS NOT NULL
9306       AND POL2.min_release_amount >
9307           --SQL What: Querying PO_LINE_LOCATIONS for the total amount of the
9308           --SQL       shipments in this PO that reference the current GA line
9309           --SQL Why: This sum determines whether the minimum release amount
9310           --SQL      for the GA line has been met
9311           (   SELECT                                          -- <SERVICES FPJ>
9312                   decode ( POL1.quantity
9313                          , NULL , decode ( sum ( PLL.amount
9314                                                - nvl(PLL.amount_cancelled,0) )
9315                                          , 0 , POL2.min_release_amount
9316                                          ,     sum ( PLL.amount
9317                                                    - nvl(PLL.amount_cancelled,0) )
9318                                          )
9319                          ,        decode ( sum ( PLL.quantity
9320                                                - nvl(PLL.quantity_cancelled,0) )
9321                                          , 0 , POL2.min_release_amount
9322                                          ,     sum ( ( PLL.quantity
9323                                                      - nvl(PLL.quantity_cancelled,0) )
9324                                                    * PLL.price_override )
9325                                          )
9326                          )
9327               --<Bug 2792477 mbhargav>
9328               --Changing the query to go to PO_LINE_LOCATIONS_GT instead of PO_LINE_LOCATIONS
9329               FROM PO_LINE_LOCATIONS_GT PLL
9330               WHERE PLL.po_header_id = p_document_id
9331               AND PLL.from_line_id = POL2.po_line_id);
9332 
9333        --Increment the p_sequence with number of errors reported in last query
9334       p_sequence := p_sequence + SQL%ROWCOUNT;
9335 
9336     END IF; -- NOT l_currency_mismatch
9337 
9338 ------------------------------------
9339 
9340 l_progress := '012';
9341     x_return_status := FND_API.G_RET_STS_SUCCESS;
9342 EXCEPTION
9343     WHEN FND_API.G_EXC_ERROR THEN
9344         x_return_status := FND_API.G_RET_STS_ERROR;
9345 
9346     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
9347         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9348 
9349     WHEN OTHERS THEN
9350         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9351         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
9352             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
9353         END IF;
9354 
9355         IF (g_debug_unexp) THEN
9356                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
9357                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
9358                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
9359                        || l_progress || ' SQL CODE is '||sqlcode);
9360                 END IF;
9361         END IF;
9362 
9363 END CHECK_STD_GLOBAL_REF;
9364 
9365 -- <GC FPJ START>
9366 /**
9367 * Private Procedure: CHECK_STD_GC_REF
9368 * Requires:
9369 *   IN PARAMETERS:
9370 *       p_document_id:      The requisition_header_id of submitted document
9371 *       p_online_report_id: Id used to INSERT INTO online_report_text table
9372 *       p_user_id:          User performing the action
9373 *       p_login_id:         Last update login_id
9374 *   IN OUT PARAMETERS
9375 *       x_sequence:          Sequence number of last reported error
9376 * Modifies: Inserts error msgs in online_report_text_gt table, uses
9377 *           global_temp tables for processing
9378 * Effects:  This procedure performs checks for the lines referencing global
9379 *           contracts to enforce the following:
9380 *           1) Contract is still enabled for purchasing in current OU
9381 *           2) Supplier Site is still enabled on the referenced GC
9382 *           3) Amount released should be less than amount limit on GC
9383 * Returns:
9384 *  x_sequence: This parameter contains the current count of number of error
9385 *              messages inserted
9386 *  x_return_status: This parameter signifies whether the checks contained in
9387 *                   this procedure completed successfully.
9388 */
9389 
9390 PROCEDURE check_std_gc_ref
9391 ( p_document_id IN NUMBER,
9392    p_online_report_id IN NUMBER,
9393    p_user_id IN NUMBER,
9394    p_login_id IN NUMBER,
9395    x_sequence IN OUT NOCOPY NUMBER,
9396    x_return_status OUT NOCOPY VARCHAR2
9397 ) IS
9398 
9399 l_textline          PO_ONLINE_REPORT_TEXT.text_line%TYPE := NULL;
9400 l_api_name          CONSTANT VARCHAR2(40) := 'CHECK_STD_GC_REF';
9401 l_progress          VARCHAR2(3);
9402 l_currency_mismatch VARCHAR2(1) := FND_API.G_FALSE;
9403 
9404 TYPE NumTab is TABLE of NUMBER INDEX BY BINARY_INTEGER;
9405 l_curr_doc_line_num NumTab;
9406 l_prev_doc_line_num NumTab;
9407 l_rowcount          NUMBER := 0;
9408 l_po_amount         NumTab;
9409 l_prev_rel_amount   NumTab;
9410 l_amount_limit      NumTab;
9411 l_module            FND_LOG_MESSAGES.module%TYPE;
9412 BEGIN
9413 
9414     l_progress := '010';
9415     l_module := g_log_head || '.' || l_api_name || '.' || l_progress;
9416 
9417     IF g_debug_stmt THEN
9418        IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9419          FND_LOG.string(FND_LOG.LEVEL_STATEMENT, l_module,
9420                       'PO STD GC 1: GC enabled for purchasing in current OU');
9421        END IF;
9422     END IF;
9423 
9424     -- Check 1: Current OU should still be enabled for purchasing on the GC
9425     --          being referenced
9426     --< Shared Proc FPJ > Bug 3301427: Only do this check for new SPO lines
9427 
9428     l_textline := FND_MESSAGE.get_string ('PO', 'PO_SUB_STD_GC_NOT_EN_PUR');
9429 
9430     INSERT INTO po_online_report_text_gt (
9431         online_report_id,
9432         last_update_login,
9433         last_updated_by,
9434         last_update_date,
9435         created_by,
9436         creation_date,
9437         line_num,
9438         shipment_num,
9439         distribution_num,
9440         sequence,
9441         text_line,
9442         message_name
9443     )
9444     SELECT
9445         p_online_report_id,
9446         p_login_id,
9447         p_user_id,
9448         SYSDATE,
9449         p_user_id,
9450         SYSDATE,
9451         POL.line_num,
9452         0,
9453         0,
9454         x_sequence + ROWNUM,
9455         SUBSTR (g_linemsg || g_delim || POL.line_num || g_delim || l_textline,
9456                  1,
9457                  240),
9458         'PA_SUB_STD_GC_NOT_EN_PUR'
9459     FROM
9460         po_headers_gt  POH,
9461         po_lines_gt    POL,
9462         po_headers_all POHA
9463     WHERE
9464         POH.po_header_id = p_document_id
9465     AND POL.po_header_id = POH.po_header_id
9466     AND POL.contract_id = POHA.po_header_id
9467     AND POHA.global_agreement_flag = 'Y'
9468     AND NOT EXISTS                                  --< Bug 3301427 Start >
9469         (SELECT 'previously approved shipment'
9470            FROM po_line_locations_gt pllg
9471           WHERE pllg.po_line_id = pol.po_line_id
9472             AND pllg.approved_date IS NOT NULL)     --< Bug 3301427 End >
9473     AND NOT EXISTS (SELECT 1
9474                     FROM   po_ga_org_assignments PGOA,
9475                            po_system_parameters  PSP
9476                     WHERE  PGOA.po_header_id = POHA.po_header_id
9477                     AND    PGOA.purchasing_org_id = PSP.org_id
9478                     AND    PGOA.enabled_flag = 'Y');
9479 
9480     x_sequence := x_sequence + SQL%ROWCOUNT;
9481 
9482     ----------------------------------
9483 
9484     l_progress := '020';
9485     l_module := g_log_head || '.' || l_api_name || '.' || l_progress;
9486 
9487     IF g_debug_stmt THEN
9488        IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9489          FND_LOG.string(FND_LOG.LEVEL_STATEMENT, l_module,
9490                       'PO STD GC 2: supplier site en for pur in current OU');
9491        END IF;
9492     END IF;
9493 
9494     -- Check 2: Supplier Site should be a purchasing site defined in GC
9495     --          Org Assignments
9496     --< Shared Proc FPJ > Bug 3301427: Only do this check for new SPO lines
9497     /* R12 GCPA
9498     Skip Vendor Site validation for Contracts having "Enable All Sites" is set to Y
9499     */
9500 
9501     l_textline := FND_MESSAGE.get_string ('PO', 'PO_SUB_STD_GC_INVALID_SITE');
9502 
9503     INSERT INTO po_online_report_text_gt (
9504         online_report_id,
9505         last_update_login,
9506         last_updated_by,
9507         last_update_date,
9508         created_by,
9509         creation_date,
9510         line_num,
9511         shipment_num,
9512         distribution_num,
9513         sequence,
9514         text_line,
9515         message_name
9516     )
9517     SELECT
9518         p_online_report_id,
9519         p_login_id,
9520         p_user_id,
9521         SYSDATE,
9522         p_user_id,
9523         SYSDATE,
9524         POL.line_num,
9525         0,
9526         0,
9527         x_sequence + ROWNUM,
9528         SUBSTR  (g_linemsg || g_delim || POL.line_num || g_delim || l_textline,
9529                  1,
9530                  240),
9531         'PA_SUB_STD_GC_INVALID_SITE'
9532     FROM
9533         po_headers_gt  POH,
9534         po_lines_gt    POL,
9535         po_headers_all POHA
9536     WHERE
9537         POH.po_header_id = p_document_id
9538     AND POL.po_header_id = POH.po_header_id
9539     AND POL.contract_id = POHA.po_header_id
9540     AND POHA.global_agreement_flag = 'Y'
9541     AND NOT EXISTS                                  --< Bug 3301427 Start >
9542         (SELECT 'previously approved shipment'
9543            FROM po_line_locations_gt pllg
9544           WHERE pllg.po_line_id = pol.po_line_id
9545             AND pllg.approved_date IS NOT NULL)     --< Bug 3301427 End >
9546     AND NOT EXISTS (SELECT 1
9547                     FROM   po_ga_org_assignments PGOA
9548                     WHERE  PGOA.po_header_id = POHA.po_header_id
9549                     AND    PGOA.vendor_site_id = Decode( Nvl ( poha.Enable_All_Sites,'N'),'N',POH.vendor_site_id,pgoa.Vendor_Site_Id)
9550                     AND    PGOA.enabled_flag = 'Y');
9551 
9552     x_sequence := x_sequence + SQL%ROWCOUNT;
9553 
9554     ----------------------------------
9555 
9556     l_progress := '030';
9557     l_module := g_log_head || '.' || l_api_name || '.' || l_progress;
9558 
9559     IF g_debug_stmt THEN
9560        IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9561          FND_LOG.string(FND_LOG.LEVEL_STATEMENT, l_module,
9562                       'PO STD GC 3: currency check against referenced GC');
9563        END IF;
9564     END IF;
9565 
9566     -- bug5153099
9567     -- Removed Check 3 as different currencies are being allowed if OU is same.
9568     -- Renumbered Check 4 as Check 3.
9569     -- In the new Check 3, Removed the IF condition as currency mismatch is allowed.
9570     -- Also multiplied amount values by rate for handling currency mismatch.
9571     -- Added a join with po_headers_all POHA1 for getting rate.
9572 
9573     -- Check 3: Amount Released should not exceed the amount limit on the GC
9574 
9575     -- bug3251561
9576     -- Need to check amount based lines as well.
9577 
9578     -- SQL What: For each line that has a contract reference, get the
9579     --           total amount released for that contract on this PO
9580     -- SQL Why:  Need to calculate the total amount released for the
9581     --           contract and we are calculating current PO and other
9582     --           POs separately because current PO lines (stored in
9583     --           po_lines_gt) may not go to po_lines_all eventually
9584     --<Complex Work R12>: changed query to use Line qty/amt/price
9585     --instead of summing up the Line Loc values.  The results should be
9586     --equivalent for SPOs.
9587 
9588     SELECT /*+ FULL(POL) ORDERED */   -- bug3413891
9589            POL.line_num,
9590            sum(DECODE (POL1.order_type_lookup_code
9591                    , 'RATE', POL1.amount
9592                    , 'FIXED PRICE', POL1.amount
9593                    , POL1.quantity * POL1.unit_price)* NVL(POHA1.rate,1)),
9594            POHA.amount_limit* NVL(POHA.rate,1)
9595     BULK COLLECT INTO l_curr_doc_line_num,
9596                       l_po_amount,
9597                       l_amount_limit
9598     FROM po_lines_gt          POL,     -- target_line
9599          po_headers_all       POHA,    -- global contract
9600          po_lines_gt          POL1,     -- all lines in current doc
9601                                        -- having the same contract ref
9602          po_headers_all       POHA1    -- document
9603     WHERE
9604          POL.po_header_id = p_document_id
9605     AND  POL.po_header_id = POHA1.po_header_id
9606     AND  POL.contract_id  = POHA.po_header_id
9607     AND  POHA.global_agreement_flag = 'Y'
9608     AND  POHA.amount_limit IS NOT NULL
9609     AND  POL.contract_id = POL1.contract_id
9610     GROUP BY POL.line_num,POHA.amount_limit* NVL(POHA.rate,1);
9611 
9612     -- Get the sum of all other shipments of approved POs referring
9613     -- to the same GC
9614 
9615     -- bug5153099
9616     -- Multiplied amount values by rate for handling currency mismatch.
9617 
9618     -- bug3251561
9619     -- Need to check amount based lines as well.
9620 
9621     -- SQL What: For each line referencing a global contract, get total
9622     --           amount released for the contract, without including
9623     --           the current PO
9624     -- SQL Why:  Need to calculate the total amount released of the GC
9625     --           to determine whether amount limit is exceeded
9626     --<Complex Work R12>: changed query to use Line qty/amt/price
9627     --instead of summing up the Line Loc values.  The results should be
9628     --equivalent for SPOs.
9629 
9630     SELECT /*+ FULL(POL) */     -- bug3413891
9631            POL.line_num,
9632            sum(DECODE (POL1.order_type_lookup_code
9633                    , 'RATE', POL1.amount
9634                    , 'FIXED PRICE', POL1.amount
9635                    , POL1.quantity * POL1.unit_price)*NVL(POH1.rate,1))
9636     BULK COLLECT INTO l_prev_doc_line_num,
9637                       l_prev_rel_amount
9638     FROM po_lines_gt           POL,     -- target line
9639          po_lines_all          POL1,    -- all lines from other doc with
9640                                         -- the same GC ref
9641          po_headers_all        POH1,    -- headers of lines in POL1
9642          po_headers_all        POHA     -- global contract
9643     WHERE
9644          POL.po_header_id = p_document_id
9645     AND  POL.contract_id = POHA.po_header_id
9646     AND  POHA.global_agreement_flag = 'Y'
9647     AND  POHA.amount_limit IS NOT NULL
9648     AND  POL1.contract_id = POL.contract_id
9649     AND  POL1.po_header_id <> POL.po_header_id
9650     AND  POH1.po_header_id = POL1.po_header_id
9651     AND  POH1.approved_flag = 'Y'
9652     GROUP BY POL.line_num;
9653 
9654     -- For each line having GC reference, the line number will be stored
9655     -- in l_curr_doc_line_num and the amount released of the GC from the
9656     -- curent document will be stored in l_po_amount. Also, for the same
9657     -- line there will be a corresponding entry in p_prev_doc_line_num,
9658     -- which stores the line number, and l_prev_rel_amount, which stores
9659     -- the amount released from all other lines referencing the same
9660     -- GC. The loop below is to add up l_po_amount and l_prev_rel_amount
9661     -- to get the total amount released for the GC
9662 
9663     FOR l_prev_index IN 1..l_prev_doc_line_num.COUNT LOOP
9664         FOR l_index IN 1..l_curr_doc_line_num.COUNT LOOP
9665             -- first check for line number matching before adding up
9666             IF l_curr_doc_line_num(l_index) =
9667                l_prev_doc_line_num(l_prev_index) THEN
9668 
9669                 l_po_amount(l_index) := l_po_amount(l_index) +
9670                                         l_prev_rel_amount(l_prev_index);
9671                 EXIT;
9672             END IF;
9673         END LOOP;
9674     END LOOP;
9675 
9676     IF g_debug_stmt THEN
9677         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9678           FND_LOG.string(FND_LOG.LEVEL_STATEMENT, l_module,
9679                       'PO STD GC 3: Amount Released Check');
9680         END IF;
9681     END IF;
9682 
9683     l_textline := FND_MESSAGE.get_string('PO',
9684                                           'PO_SUB_STD_CONTRACT_AMT_LIMIT');
9685 
9686     -- Go through all lines in current PO (which refer to GCs)
9687     FOR l_curr_doc_line_index IN 1..l_curr_doc_line_num.COUNT LOOP
9688 
9689         IF (l_amount_limit(l_curr_doc_line_index) <
9690             l_po_amount(l_curr_doc_line_index))
9691         THEN
9692             l_rowcount := l_rowcount + 1;
9693 
9694             INSERT INTO po_online_report_text_gt (
9695                 online_report_id,
9696                 last_update_login,
9697                 last_updated_by,
9698                 last_update_date,
9699                 created_by,
9700                 creation_date,
9701                 line_num,
9702                 shipment_num,
9703                 distribution_num,
9704                 sequence,
9705                 text_line,
9706                 message_name
9707             ) VALUES (
9708                 p_online_report_id,
9709                 p_login_id,
9710                 p_user_id,
9711                 SYSDATE,
9712                 p_user_id,
9713                 SYSDATE,
9714                 l_curr_doc_line_num(l_curr_doc_line_index),
9715                 0,
9716                 0,
9717                 x_sequence + l_rowcount,
9718                 SUBSTR (g_linemsg || g_delim ||
9719                         l_curr_doc_line_num(l_curr_doc_line_index) ||
9720                         g_delim || l_textline, 1, 240),
9721                 'PO_SUB_STD_CONTRACT_AMT_LIMIT'
9722             );
9723 
9724         END IF; -- if amount limit < amount released
9725     END LOOP;
9726 
9727     x_sequence := x_sequence + l_rowcount;
9728 
9729 
9730     l_progress := '999';
9731     x_return_status := FND_API.G_RET_STS_SUCCESS;
9732 
9733 EXCEPTION
9734     WHEN FND_API.G_EXC_ERROR THEN
9735         x_return_status := FND_API.G_RET_STS_ERROR;
9736 
9737     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
9738         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9739 
9740     WHEN OTHERS THEN
9741         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9742         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
9743             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
9744         END IF;
9745 
9746         IF (g_debug_unexp) THEN
9747                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
9748                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
9749                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
9750                        || l_progress || ' SQL CODE is '||sqlcode);
9751                 END IF;
9752         END IF;
9753 
9754 END CHECK_STD_GC_REF;
9755 
9756 -- <GC FPJ END>
9757 
9758 --For Contract PA
9759 /**
9760 * Private Procedure: CHECK_CONTRACT_AGREEMENT
9761 * Requires:
9762 *   IN PARAMETERS:
9763 *       p_document_id:      The requisition_header_id of submitted document
9764 *       p_online_report_id: Id used to INSERT INTO online_report_text table
9765 *       p_user_id:          User performing the action
9766 *       p_login_id:         Last update login_id
9767 *   IN OUT PARAMETERS
9768 *       p_sequence:          Sequence number of last reported error
9769 * Modifies: Inserts error msgs in online_report_text_gt table, uses
9770 *           global_temp tables for processing
9771 * Effects:  This procedure runs the document submission checks for CONTRACT
9772 *           PAs
9773 * Returns:
9774 *  p_sequence: This parameter contains the current count of number of error
9775 *              messages inserted
9776 */
9777 PROCEDURE check_contract_agreement(p_document_id IN NUMBER,
9778                        p_online_report_id IN NUMBER,
9779                        p_user_id IN NUMBER,
9780                        p_login_id IN NUMBER,
9781                        p_sequence IN OUT NOCOPY NUMBER,
9782                        x_return_status OUT NOCOPY VARCHAR2) IS
9783 
9784 l_textline  po_online_report_text.text_line%TYPE := NULL;
9785 l_api_name  CONSTANT varchar2(40) := 'CHECK_CONTRACT_AGREEMENT';
9786 l_progress VARCHAR2(3);
9787 
9788 BEGIN
9789 
9790 l_progress := '000';
9791 
9792 l_progress := '001';
9793 IF g_debug_stmt THEN
9794    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9795      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
9796           || l_progress,'PA CONTRACT 1: Released Amount exceeds Amount Limit');
9797    END IF;
9798 END IF;
9799 
9800     -- Check 1: The amount of all standard purchase orders
9801     -- for a contract should not exceed the amount limit of the contract.
9802     -- PO_SUB_CONTRACT_AMT_LIMIT
9803     --<Complex Work R12>: changed query to use Line qty/amt/price
9804     --instead of summing up the Line Loc values.  The results should be
9805     --equivalent for SPOs.
9806     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_CONTRACT_AMT_LIMIT');
9807     INSERT INTO po_online_report_text_gt(online_report_id,
9808                                          last_update_login,
9809                                          last_updated_by,
9810                                          last_update_date,
9811                                          created_by,
9812                                          creation_date,
9813                                          line_num,
9814                                          shipment_num,
9815                                          distribution_num,
9816                                          sequence,
9817                                          text_line,
9818                                          message_name)
9819   SELECT  p_online_report_id,
9820                 p_login_id,
9821                 p_user_id,
9822                 sysdate,
9823                 p_user_id,
9824                 sysdate,
9825                 0,
9826                 0,
9827                 0,
9828                 p_sequence + ROWNUM,
9829                 substr(l_textline,1,240),
9830                 'PO_SUB_CONTRACT_AMT_LIMIT'
9831        -- <GC FPJ START>
9832        -- For local contract, need to take care of currency conversion
9833        -- since the std PO referencing a contract may be in a different
9834        -- currency
9835        --<Bug#4619187>
9836        --Added a sum function to the sql's select clause
9837 
9838        -- bug5138959
9839        -- Added a sum function to the second select clause
9840        -- bug5153099
9841        -- Removed group by clause in subquery.Also removed the checking for
9842        -- global_agreement_flag and the corresponding OR case logic.
9843 
9844        FROM PO_HEADERS_GT POH
9845        WHERE POH.po_header_id = p_document_id
9846        AND   POH.type_lookup_code = 'CONTRACT'
9847        AND   POH.amount_limit IS NOT NULL
9848        AND   ((POH.amount_limit * NVL(POH.rate, 1))  -- amt limit in fn currency --<SERVICES FPJ>
9849                        <
9850                        (SELECT SUM(                           --Bug#4619187
9851                                     DECODE (POL1.order_type_lookup_code
9852                                            , 'RATE', POL1.amount
9853                                            , 'FIXED PRICE', POL1.amount
9854                                            , POL1.quantity * POL1.unit_price)
9855                                    * NVL(POH1.rate,1)
9856                                   )
9857                         FROM   po_headers POH1,
9858                                po_lines   POL1
9859                         WHERE  POL1.contract_id = POH.po_header_id
9860                         AND    POL1.po_header_id = POH1.po_header_id
9861                         AND    NVL(POL1.cancel_flag, 'N') = 'N'
9862                         )  -- amt released in fn currency
9863              );
9864 
9865        -- <GC FPJ END>
9866 
9867      --Increment the p_sequence with number of errors reported in last query
9868     p_sequence := p_sequence + SQL%ROWCOUNT;
9869 
9870 l_progress := '002';
9871     x_return_status := FND_API.G_RET_STS_SUCCESS;
9872 EXCEPTION
9873     WHEN FND_API.G_EXC_ERROR THEN
9874         x_return_status := FND_API.G_RET_STS_ERROR;
9875 
9876     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
9877         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9878 
9879     WHEN OTHERS THEN
9880         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9881         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
9882             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
9883         END IF;
9884 
9885         IF (g_debug_unexp) THEN
9886                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
9887                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
9888                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
9889                        || l_progress || ' SQL CODE is '||sqlcode);
9890                 END IF;
9891         END IF;
9892 
9893 END CHECK_CONTRACT_AGREEMENT;
9894 
9895 --For PO, REQ, REL
9896 /**
9897 * Private Procedure: DO_CBC_RELATED_VALIDATIONS
9898 * Requires:
9899 *   IN PARAMETERS:
9900 *       p_document_id:      The requisition_header_id of submitted document
9901 *       p_online_report_id: Id used to INSERT INTO online_report_text table
9902 *       p_user_id:          User performing the action
9903 *       p_login_id:         Last update login_id
9904 *   IN OUT PARAMETERS
9905 *       p_sequence:          Sequence number of last reported error
9906 * Modifies: Inserts error msgs in online_report_text_gt table, uses
9907 *           global_temp tables for processing
9908 * Effects:  This procedure runs the document submission checks for HEADER
9909 *           level CBC (FPI Project) validations for PO, REQ, REL
9910 * Returns:
9911 *  p_sequence: This parameter contains the current count of number of error
9912 *              messages inserted
9913 */
9914 PROCEDURE do_cbc_related_validations(p_document_type IN VARCHAR2,
9915                        p_document_subtype IN VARCHAR2,
9916                        p_document_id IN NUMBER,
9917                        p_online_report_id IN NUMBER,
9918                        p_user_id IN NUMBER,
9919                        p_login_id IN NUMBER,
9920                        p_sequence IN OUT NOCOPY NUMBER,
9921                        x_return_status OUT NOCOPY VARCHAR2) IS
9922 
9923 l_msg_count NUMBER;
9924 l_msg_data  VARCHAR2(2400);
9925 l_return_status VARCHAR2(1);
9926 l_cbc_enabled VARCHAR2(1);
9927 l_result NUMBER :=0;
9928 
9929 l_textline po_online_report_text.text_line%TYPE := NULL;
9930 l_api_name  CONSTANT varchar2(40) := 'DO_CBC_RELATED_VALIDATIONS';
9931 l_progress VARCHAR2(3);
9932 
9933 BEGIN
9934 
9935 l_progress := '000';
9936 IF g_debug_stmt THEN
9937    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9938      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
9939           || l_progress,'CBC 1: Check CBC');
9940    END IF;
9941 END IF;
9942 
9943      IGC_CBC_PO_GRP.IS_CBC_ENABLED ( p_api_version       => 1.0,
9944                                  p_init_msg_list     => FND_API.G_FALSE,
9945                                  p_commit            => FND_API.G_FALSE,
9946                                  p_validation_level  => 100,
9947                                  x_return_status    => l_return_status,
9948                                  x_msg_count         => l_msg_count,
9949                                  x_msg_data          => l_msg_data,
9950                                  x_cbc_enabled       => l_cbc_enabled);
9951 
9952      IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
9953         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9954      END IF;
9955 
9956 l_progress := '001';
9957 IF g_debug_stmt THEN
9958    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9959      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
9960           || l_progress,'Is CBC enabled '|| l_cbc_enabled);
9961    END IF;
9962 END IF;
9963 
9964      IF  l_cbc_enabled = 'Y' THEN
9965 
9966           IGC_CBC_PO_GRP.CBC_HEADER_VALIDATIONS(
9967             p_api_version   => 1.0,
9968             p_init_msg_list => FND_API.G_FALSE,
9969             p_commit        => FND_API.G_FALSE,
9970             p_validation_level => FND_API.G_VALID_LEVEL_FULL,
9971             x_return_status => l_return_status,
9972             x_msg_count => l_msg_count,
9973             x_msg_data =>l_msg_data,
9974             p_document_id           => p_document_id,
9975             p_document_type         => p_document_type,
9976             p_document_sub_type     => p_document_subtype);
9977 
9978 l_progress := '002';
9979 IF g_debug_stmt THEN
9980    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9981      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
9982           || l_progress,'CBC call returned '|| l_return_status);
9983    END IF;
9984 END IF;
9985 
9986           IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
9987                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9988           END IF;
9989 
9990           IF l_return_status = FND_API.G_RET_STS_ERROR
9991           THEN
9992                 l_textline := FND_MSG_PUB.GET(p_msg_index => FND_MSG_PUB.G_LAST,
9993                                         p_encoded => 'F');
9994 
9995                 IF l_textline = NULL THEN
9996                     l_textline :=  FND_MESSAGE.GET_STRING('IGC',
9997                                           'IGC_MULT_FISCAL_YEARS');
9998                 END IF;
9999                 --Deleting msg so that we can always use G_Last to get the
10000                 --message we want
10001                 FND_MSG_PUB.Delete_Msg(p_msg_index => FND_MSG_PUB.G_LAST);
10002 l_progress := '003';
10003                 INSERT INTO PO_ONLINE_REPORT_TEXT_GT (online_report_id,
10004                         last_update_login,
10005                 last_updated_by,
10006                 last_update_date,
10007                 created_by,
10008                 creation_date,
10009                 line_num,
10010                 shipment_num,
10011                 distribution_num,
10012                 sequence,
10013                 text_line,
10014                         message_name)
10015                 VALUES (p_online_report_id,
10016                     p_login_id,
10017                     p_user_id,
10018                       sysdate,
10019                     p_user_id,
10020                     sysdate,
10021                     0,
10022                     0,
10023                     0,
10024                     p_sequence + 1,
10025                     substr(l_textline,1,240),
10026                         'IGC_MULT_FISCAL_YEARS');
10027 
10028                 p_sequence := p_sequence + 1;
10029           END IF; --expected error
10030 
10031     END IF; --cbc is enabled
10032 
10033 l_progress := '004';
10034     x_return_status := FND_API.G_RET_STS_SUCCESS;
10035 EXCEPTION
10036     WHEN FND_API.G_EXC_ERROR THEN
10037         x_return_status := FND_API.G_RET_STS_ERROR;
10038 
10039     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
10040         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10041 
10042     WHEN OTHERS THEN
10043         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10044         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
10045             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
10046         END IF;
10047 
10048         IF (g_debug_unexp) THEN
10049                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
10050                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
10051                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
10052                        || l_progress || ' SQL CODE is '||sqlcode);
10053                 END IF;
10054         END IF;
10055 
10056 END DO_CBC_RELATED_VALIDATIONS;
10057 
10058 
10059 
10060 
10061 -------------------------------------------------------------------------------
10062 --Start of Comments
10063 --Name: populate_global_temp_tables
10064 --Pre-reqs:
10065 --  None.
10066 --Modifies:
10067 --  See the called procedures.
10068 --Locks:
10069 --  None.
10070 --Function:
10071 --  Populates the global temp tables for submission checks.
10072 --Parameters:
10073 --IN:
10074 --p_doc_type
10075 --  Document type.  Use the g_doc_type_<> variables, where <> is:
10076 --    REQUISITION
10077 --    PA
10078 --    PO
10079 --    RELEASE
10080 --p_doc_level
10081 --  The type of id that is being passed.  Use g_doc_level_<>
10082 --  The following is supported for all actions:
10083 --    HEADER
10084 --  The following are also supported for UNRESERVE checks (PO/RELEASE):
10085 --    LINE
10086 --    SHIPMENT
10087 --    DISTRIBUTION
10088 --  The following are also supported for FINAL_CLOSE checks (PO/RELEASE):
10089 --    LINE
10090 --    SHIPMENT
10091 --p_doc_level_id
10092 --  Id of the doc level type of which to populate the tables.
10093 --OUT:
10094 --x_return_status
10095 --  APPS standard parameter.
10096 --Testing:
10097 --
10098 --End of Comments
10099 -------------------------------------------------------------------------------
10100 PROCEDURE populate_global_temp_tables(
10101    x_return_status                  OUT NOCOPY     VARCHAR2
10102 ,  p_doc_type                       IN             VARCHAR2
10103 ,  p_doc_level                      IN             VARCHAR2
10104 ,  p_doc_level_id                   IN             NUMBER
10105 )
10106 IS
10107 
10108 l_api_name  CONSTANT varchar2(40) := 'POPULATE_GLOBAL_TEMP_TABLES';
10109 l_log_head CONSTANT VARCHAR2(100) := g_log_head||l_api_name;
10110 l_progress VARCHAR2(3);
10111 
10112 l_blanket_header_id po_headers.po_header_id%TYPE;
10113 l_return_status VARCHAR2(1);
10114 
10115 l_doc_id    NUMBER;
10116 
10117 l_id_tbl    po_tbl_number;
10118 
10119 BEGIN
10120 l_progress := '000';
10121 
10122 IF g_debug_stmt THEN
10123    PO_DEBUG.debug_begin(l_log_head);
10124    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_type', p_doc_type);
10125    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level', p_doc_level);
10126    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level_id',p_doc_level_id);
10127 END IF;
10128 
10129 l_progress := '500';
10130 
10131 -- bug3413891
10132 -- Clean up gt tables to make sure that all the records in GT tables are from
10133 -- the same document
10134 
10135 DELETE FROM po_headers_gt;
10136 DELETE FROM po_lines_gt;
10137 DELETE FROM po_line_locations_gt;
10138 DELETE FROM po_distributions_gt;
10139 DELETE FROM po_releases_gt;
10140 DELETE FROM po_req_headers_gt;
10141 DELETE FROM po_req_lines_gt;
10142 DELETE FROM po_req_distributions_gt;
10143 DELETE FROM po_online_report_text_gt;
10144 
10145 
10146 PO_CORE_S.get_document_ids(
10147    p_doc_type => p_doc_type
10148 ,  p_doc_level => p_doc_level
10149 ,  p_doc_level_id_tbl => po_tbl_number( p_doc_level_id )
10150 ,  x_doc_id_tbl => l_id_tbl
10151 );
10152 
10153 l_progress := '510';
10154 
10155 l_doc_id := l_id_tbl(1);
10156 
10157 
10158     IF p_doc_type = 'REQUISITION' THEN
10159 
10160 l_progress := '001';
10161 IF g_debug_stmt THEN
10162    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
10163      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
10164           || l_progress,'Populating from REQS');
10165    END IF;
10166 END IF;
10167 
10168         --populate the global REQ headers table
10169         populate_req_headers_gt(l_doc_id, l_return_status);
10170 
10171         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
10172                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10173         END IF;
10174 
10175 l_progress := '002';
10176         --populate the global REQ line table
10177         populate_req_lines_gt(l_doc_id, l_return_status);
10178 
10179         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
10180                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10181         END IF;
10182 
10183     ELSIF p_doc_type in ('PO', 'PA') THEN
10184 l_progress := '004';
10185 IF g_debug_stmt THEN
10186    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
10187      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
10188           || l_progress,'Populating from PO tables for PO/PA');
10189    END IF;
10190 END IF;
10191 
10192         --populate the global headers table
10193         populate_po_headers_gt(l_doc_id, l_return_status);
10194 
10195         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
10196                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10197         END IF;
10198 
10199 l_progress := '005';
10200 
10201         --populate the global line table
10202         -- <Doc Manager Rewrite 11.5.11>
10203         -- Change populate logic for po_lines_gt to handle document levels
10204 
10205         populate_po_lines_gt(
10206            p_doc_type => p_doc_type
10207         ,  p_doc_level => p_doc_level
10208         ,  p_doc_level_id => p_doc_level_id
10209         ,  x_return_status => l_return_status
10210         );
10211 
10212         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
10213                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10214         END IF;
10215 
10216     ELSIF p_doc_type = 'RELEASE' THEN
10217 l_progress := '008';
10218 IF g_debug_stmt THEN
10219    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
10220      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
10221           || l_progress,'Populating RELEASES');
10222    END IF;
10223 END IF;
10224 
10225         --populate the global release table
10226         populate_releases_gt(l_doc_id, l_return_status);
10227 
10228         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
10229                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10230         END IF;
10231 
10232 l_progress := '009';
10233         -- Get the header_id of the relevent Blanket
10234         SELECT po_header_id
10235         INTO l_blanket_header_id
10236         FROM po_releases_gt
10237         WHERE po_release_id = l_doc_id;
10238 
10239 l_progress := '010';
10240         --populate the global headers table with header of blanket/planned PO
10241         --for which this is a Release
10242         populate_po_headers_gt(l_blanket_header_id, l_return_status);
10243 
10244         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
10245                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10246         END IF;
10247 
10248     END IF;
10249 
10250 --<FPJ ENCUMBRANCE>
10251 
10252 l_progress := '100';
10253 
10254 IF (p_doc_type <> g_document_type_REQUISITION) THEN
10255 
10256    populate_line_locations_gt(
10257       p_doc_type => p_doc_type
10258    ,  p_doc_level => p_doc_level
10259    ,  p_doc_level_id => p_doc_level_id
10260    );
10261 
10262 END IF;
10263 
10264 l_progress := '200';
10265 
10266 populate_distributions_gt(
10267    p_doc_type => p_doc_type
10268 ,  p_doc_level => p_doc_level
10269 ,  p_doc_level_id => p_doc_level_id
10270 );
10271 
10272 l_progress := 300;
10273 
10274 --LCM ER start. Populate the lcm flag in the GT tables first
10275 IF p_doc_type in ('PO','RELEASE') THEN
10276 
10277 	FOR ship_rec in (select line_location_id from po_line_locations_gt)
10278 	LOOP
10279 		set_lcm_flag(ship_rec.line_location_id,'BEFORE',l_return_status);
10280 	END LOOP;
10281 END IF;
10282 
10283 --LCM ER end.
10284 l_progress := '900';
10285 
10286     x_return_status := FND_API.G_RET_STS_SUCCESS;
10287 EXCEPTION
10288     WHEN FND_API.G_EXC_ERROR THEN
10289         x_return_status := FND_API.G_RET_STS_ERROR;
10290 
10291     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
10292         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10293 
10294     WHEN OTHERS THEN
10295         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10296         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
10297             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
10298         END IF;
10299 
10300         IF (g_debug_unexp) THEN
10301                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
10302                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
10303                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
10304                        || l_progress || ' SQL CODE is '||sqlcode);
10305                 END IF;
10306         END IF;
10307 
10308 END populate_global_temp_tables;
10309 
10310 /**
10311 * Private Procedure: UPDATE_GLOBAL_TEMP_TABLES
10312 * Requires:
10313 *   IN PARAMETERS:
10314 *     p_document_type:     Type of submitted document
10315 *     p_document_subtype:  Subtype of submitted document
10316 *     p_document_id:       Id of submitted document
10317 *     p_requested_changes: This object contains all the requested changes to
10318 *                          the document. It contains 5 objects. These objects
10319 *                          are: 1.Header_Changes 2.Release_Changes 3.Line_
10320 *                          Changes 4.Shipment_Changes 5.Distribution_Changes.
10321 *                          In FPI, following change requests are allowed:
10322 *                          1. HEADER_CHANGES: None
10323 *                          2. RELEASE_CHANGES: None
10324 *                          3. LINE_CHANGES: unit_price, vendor_product_num
10325 *                          4. SHIPMENT_CHANGES: quantity, promised_date,
10326 *                             price_override
10327 *                          5. DISTRIBUTION_CHANGES: quantity_ordered
10328 * Modifies:
10329 * Effects:  Updates the global temp tables with the changes in object
10330 *           p_requested_changes
10331 * Returns:
10332 */
10333 PROCEDURE update_global_temp_tables(p_document_type IN VARCHAR2,
10334                      p_document_subtype IN VARCHAR2,
10335                      p_document_id IN NUMBER,
10336                 -- <PO_CHANGE_API FPJ> Renamed the type to PO_CHANGES_REC_TYPE:
10337                      p_requested_changes  IN PO_CHANGES_REC_TYPE,
10338                      x_return_status OUT NOCOPY VARCHAR2) IS
10339 
10340 l_api_name  CONSTANT varchar2(40) := 'UPDATE_GLOBAL_TEMP_TABLES';
10341 l_progress VARCHAR2(3);
10342 
10343 BEGIN
10344 
10345 l_progress := '000';
10346     IF p_document_type = 'REQUISITION' THEN
10347         --right now no updates to requisitions are allowed
10348         return;
10349     END IF;
10350 
10351     IF p_requested_changes.line_changes IS NOT NULL THEN
10352 l_progress := '001';
10353 IF g_debug_stmt THEN
10354    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
10355      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
10356           || l_progress,'Making Line Chnages');
10357    END IF;
10358 END IF;
10359         -- SQL What: Updating the changeable quantities with either passed
10360         --           in values or if NULL then with existing values in the table
10361         -- SQL Why: Need to apply requested line level changes to global temp table
10362         -- SQL Join: po_line_id
10363         FORALL i IN 1..p_requested_changes.line_changes.po_line_id.COUNT
10364            UPDATE po_lines_gt
10365             SET unit_price = nvl(p_requested_changes.line_changes.unit_price(i),unit_price),
10366                 vendor_product_num = nvl(p_requested_changes.line_changes.vendor_product_num(i),
10367                                             vendor_product_num),
10368                -- <PO_CHANGE_API FPJ START>
10369                -- Added several changeable fields:
10370                quantity = nvl(p_requested_changes.line_changes.quantity(i),
10371                               quantity),
10372                start_date = nvl(p_requested_changes.line_changes.start_date(i),
10373                                 start_date),
10374                expiration_date =
10375                  nvl(p_requested_changes.line_changes.expiration_date(i),
10376                      expiration_date),
10377                amount = nvl(p_requested_changes.line_changes.amount(i),
10378                             amount)
10379                -- <PO_CHANGE_API FPJ END>
10380              WHERE po_line_id = p_requested_changes.line_changes.po_line_id(i);
10381 
10382         --To propogate line price change to shipment level for Standard PO
10383         IF (p_document_type = 'PO' AND p_document_subtype = 'STANDARD') THEN
10384 
10385 l_progress := '002';
10386             -- SQL What: Setting the priceoverride at Shipment level
10387             -- SQL Why: Need to propogate line price change to shipment level
10388             --          for Standard PO
10389             -- SQL Join: po_line_id
10390             FORALL i IN 1..p_requested_changes.line_changes.po_line_id.COUNT
10391                 UPDATE po_line_locations_gt
10392                  SET price_override = nvl(p_requested_changes.line_changes.unit_price(i),price_override)
10393                  WHERE po_line_id = p_requested_changes.line_changes.po_line_id(i)
10394                  AND nvl(payment_type, 'NULL') NOT IN ('MILESTONE', 'ADVANCE')
10395                  -- <Complex Work R12>: do not carry line price down in Qty Milestone case
10396                  ;
10397         END IF;
10398     END IF;
10399 
10400     IF p_requested_changes.shipment_changes IS NOT NULL THEN
10401 l_progress := '003';
10402 IF g_debug_stmt THEN
10403    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
10404      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
10405           || l_progress,'Making Shipment chanbges');
10406    END IF;
10407 END IF;
10408         -- SQL What: Updating the changeable quantities with either passed
10409         --           in values or if NULL then with existing values in the table
10410         -- SQL Why: Need to apply requested line level changes to global temp table
10411         -- SQL Join: po_line_location_id
10412         FORALL i IN 1..p_requested_changes.shipment_changes.po_line_location_id.COUNT
10413            UPDATE po_line_locations_gt
10414             SET quantity = nvl(p_requested_changes.shipment_changes.quantity(i),quantity),
10415                 promised_date = nvl(p_requested_changes.shipment_changes.promised_date(i),
10416                                             promised_date),
10417                 price_override = nvl(p_requested_changes.shipment_changes.price_override(i),
10418                                             price_override),
10419                 -- <PO_CHANGE_API FPJ START>
10420                 -- Added several changeable fields:
10421                 need_by_date =
10422                   nvl(p_requested_changes.shipment_changes.need_by_date(i),
10423                       need_by_date),
10424                 ship_to_location_id =
10425                   nvl(p_requested_changes.shipment_changes.ship_to_location_id(i),
10426                       ship_to_location_id),
10427                 amount = nvl(p_requested_changes.shipment_changes.amount(i),
10428                              amount),
10429                 -- <PO_CHANGE_API FPJ END>
10430                 -- <Complex Work R12 START>
10431                 payment_type = nvl(p_requested_changes.shipment_changes.payment_type(i),
10432                                    payment_type),
10433                 description = nvl(p_requested_changes.shipment_changes.description(i),
10434                                   description),
10435                 value_basis = DECODE(p_requested_changes.shipment_changes.payment_type(i)
10436                                      , NULL, value_basis
10437                                      , 'RATE', 'QUANTITY'
10438                                      , 'LUMPSUM', 'FIXED PRICE'
10439                                      , 'MILESTONE', 'FIXED PRICE'
10440                               )
10441                 -- Note: the value basis decode assumes Milestone Pay Items are Amount
10442                 -- Milestones, since payment type is not changeable on Qty-based lines
10443                 -- <Complex Work R12 END>
10444             WHERE line_location_id = p_requested_changes.shipment_changes.po_line_location_id(i)
10445             AND   p_requested_changes.shipment_changes.parent_line_location_id(i) IS NULL;
10446 
10447 l_progress := '004';
10448 
10449         --<case of split shipment>
10450         FORALL i IN 1..p_requested_changes.shipment_changes.po_line_location_id.COUNT
10451             INSERT INTO po_line_locations_gt(
10452                  LINE_LOCATION_ID     ,
10453                  LAST_UPDATE_DATE      ,
10454                  LAST_UPDATED_BY        ,
10455                  PO_HEADER_ID            ,
10456                  PO_LINE_ID               ,
10457                  LAST_UPDATE_LOGIN         ,
10458                  CREATION_DATE              ,
10459                  CREATED_BY                  ,
10460                  QUANTITY                     ,
10461                  QUANTITY_RECEIVED             ,
10462                  QUANTITY_ACCEPTED              ,
10463                  QUANTITY_REJECTED               ,
10464                  QUANTITY_BILLED                  ,
10465                  QUANTITY_CANCELLED                ,
10466                  UNIT_MEAS_LOOKUP_CODE              ,
10467                  PO_RELEASE_ID                       ,
10468                  SHIP_TO_LOCATION_ID                  ,
10469                  SHIP_VIA_LOOKUP_CODE                  ,
10470                  NEED_BY_DATE                           ,
10471                  PROMISED_DATE                           ,
10472                  LAST_ACCEPT_DATE                         ,
10473                  PRICE_OVERRIDE                          ,
10474                  ENCUMBERED_FLAG                        ,
10475                  ENCUMBERED_DATE                         ,
10476                  UNENCUMBERED_QUANTITY                    ,
10477                  FOB_LOOKUP_CODE                         ,
10478                  FREIGHT_TERMS_LOOKUP_CODE                ,
10479                  TAXABLE_FLAG                            ,
10480                  ESTIMATED_TAX_AMOUNT                    ,
10481                  FROM_HEADER_ID                          ,
10482                  FROM_LINE_ID                             ,
10483                  FROM_LINE_LOCATION_ID                   ,
10484                  START_DATE                               ,
10485                  END_DATE                                ,
10486                  LEAD_TIME                              ,
10487                  LEAD_TIME_UNIT                          ,
10488                  PRICE_DISCOUNT                           ,
10489                  TERMS_ID                                 ,
10490                  APPROVED_FLAG                            ,
10491                  APPROVED_DATE                            ,
10492                  CLOSED_FLAG                              ,
10493                  CANCEL_FLAG                              ,
10494                  CANCELLED_BY                             ,
10495                  CANCEL_DATE                              ,
10496                  CANCEL_REASON                            ,
10497                  FIRM_STATUS_LOOKUP_CODE                  ,
10498                  FIRM_DATE                                ,
10499                  ATTRIBUTE_CATEGORY                       ,
10500                  ATTRIBUTE1                               ,
10501                  ATTRIBUTE2                               ,
10502                  ATTRIBUTE3                               ,
10503                  ATTRIBUTE4                               ,
10504                  ATTRIBUTE5                               ,
10505                  ATTRIBUTE6                               ,
10506                  ATTRIBUTE7                               ,
10507                  ATTRIBUTE8                               ,
10508                  ATTRIBUTE9                               ,
10509                  ATTRIBUTE10                              ,
10510                  UNIT_OF_MEASURE_CLASS                    ,
10511                  ENCUMBER_NOW                             ,
10512                  ATTRIBUTE11                              ,
10513                  ATTRIBUTE12                              ,
10514                  ATTRIBUTE13                              ,
10515                  ATTRIBUTE14                              ,
10516                  ATTRIBUTE15                              ,
10517                  INSPECTION_REQUIRED_FLAG                 ,
10518                  RECEIPT_REQUIRED_FLAG                    ,
10519                  QTY_RCV_TOLERANCE                       ,
10520                  QTY_RCV_EXCEPTION_CODE                   ,
10521                  ENFORCE_SHIP_TO_LOCATION_CODE            ,
10522                  ALLOW_SUBSTITUTE_RECEIPTS_FLAG           ,
10523                  DAYS_EARLY_RECEIPT_ALLOWED               ,
10524                  DAYS_LATE_RECEIPT_ALLOWED                ,
10525                  RECEIPT_DAYS_EXCEPTION_CODE             ,
10526                  INVOICE_CLOSE_TOLERANCE                  ,
10527                  RECEIVE_CLOSE_TOLERANCE                  ,
10528                  SHIP_TO_ORGANIZATION_ID                 ,
10529                  SHIPMENT_NUM                            ,
10530                  SOURCE_SHIPMENT_ID                      ,
10531                  SHIPMENT_TYPE                     ,
10532                  CLOSED_CODE                        ,
10533                  REQUEST_ID                          ,
10534                  PROGRAM_APPLICATION_ID               ,
10535                  PROGRAM_ID                            ,
10536                  PROGRAM_UPDATE_DATE                    ,
10537                  GOVERNMENT_CONTEXT                      ,
10538                  RECEIVING_ROUTING_ID                     ,
10539                  ACCRUE_ON_RECEIPT_FLAG                  ,
10540                  CLOSED_REASON                           ,
10541                  CLOSED_DATE                              ,
10542                  CLOSED_BY                               ,
10543                  ORG_ID                                  ,
10544                  GLOBAL_ATTRIBUTE1                        ,
10545                  GLOBAL_ATTRIBUTE2                        ,
10546                  GLOBAL_ATTRIBUTE3                        ,
10547                  GLOBAL_ATTRIBUTE4                        ,
10548                  GLOBAL_ATTRIBUTE5                        ,
10549                  GLOBAL_ATTRIBUTE6                        ,
10550                  GLOBAL_ATTRIBUTE7                        ,
10551                  GLOBAL_ATTRIBUTE8                        ,
10552                  GLOBAL_ATTRIBUTE9                        ,
10553                  GLOBAL_ATTRIBUTE10                       ,
10554                  GLOBAL_ATTRIBUTE11                       ,
10555                  GLOBAL_ATTRIBUTE12                       ,
10556                  GLOBAL_ATTRIBUTE13                       ,
10557                  GLOBAL_ATTRIBUTE14                       ,
10558                  GLOBAL_ATTRIBUTE15                       ,
10559                  GLOBAL_ATTRIBUTE16                       ,
10560                  GLOBAL_ATTRIBUTE17                       ,
10561                  GLOBAL_ATTRIBUTE18                       ,
10562                  GLOBAL_ATTRIBUTE19                       ,
10563                  GLOBAL_ATTRIBUTE20                       ,
10564                  GLOBAL_ATTRIBUTE_CATEGORY                ,
10565                  QUANTITY_SHIPPED                        ,
10566                  COUNTRY_OF_ORIGIN_CODE                   ,
10567                  TAX_USER_OVERRIDE_FLAG                  ,
10568                  MATCH_OPTION                            ,
10569                  TAX_CODE_ID                              ,
10570                  CALCULATE_TAX_FLAG                      ,
10571                  CHANGE_PROMISED_DATE_REASON            ,
10572                  NOTE_TO_RECEIVER                        ,
10573                  SECONDARY_QUANTITY                      ,
10574                  SECONDARY_UNIT_OF_MEASURE               ,
10575                  PREFERRED_GRADE                         ,
10576                  SECONDARY_QUANTITY_RECEIVED             ,
10577                  SECONDARY_QUANTITY_ACCEPTED              ,
10578                  SECONDARY_QUANTITY_REJECTED             ,
10579                  SECONDARY_QUANTITY_CANCELLED             ,
10580                  VMI_FLAG                                 ,
10581                  CONSIGNED_FLAG                           ,
10582                  RETROACTIVE_DATE                         ,
10583                  AMOUNT                                   , -- <PO_CHANGE_API FPJ>
10584                  DESCRIPTION                              , --<Complex Work R12>
10585                  PAYMENT_TYPE                             , --<Complex Work R12>
10586                  VALUE_BASIS                                --<Complex Work R12>
10587              )
10588              SELECT
10589                  -- bug3611217
10590                  -- Use a sequence number rather than FND_API.G_MISS_NUM
10591                  -- since we have added unique constraint on LINE_LOCATION_ID
10592                  PO_LINE_LOCATIONS_S.nextval,
10593                  poll.LAST_UPDATE_DATE      ,
10594                  poll.LAST_UPDATED_BY        ,
10595                  poll.PO_HEADER_ID            ,
10596                  poll.PO_LINE_ID               ,
10597                  poll.LAST_UPDATE_LOGIN         ,
10598                  poll.CREATION_DATE              ,
10599                  poll.CREATED_BY                  ,
10600                  nvl(p_requested_changes.shipment_changes.quantity(i),poll.quantity),
10601                  -- Bug 3322019 START
10602                  -- Quantity received, accepted, cancelled, etc. should be
10603                  -- NULL or 0 on the split shipment.
10604                  decode(poll.quantity_received,NULL,NULL,0),
10605                  decode(poll.quantity_accepted,NULL,NULL,0),
10606                  decode(poll.quantity_rejected,NULL,NULL,0),
10607                  decode(poll.quantity_billed,NULL,NULL,0),
10608                  decode(poll.quantity_cancelled,NULL,NULL,0),
10609                  -- Bug 3322019 END
10610                  poll.UNIT_MEAS_LOOKUP_CODE              ,
10611                  poll.PO_RELEASE_ID                       ,
10612                  -- <PO_CHANGE_API FPJ> Added as a changeable field:
10613                  nvl(p_requested_changes.shipment_changes.ship_to_location_id(i),
10614                      poll.ship_to_location_id),
10615                  poll.SHIP_VIA_LOOKUP_CODE                  ,
10616                  -- <PO_CHANGE_API FPJ> Added as a changeable field:
10617                  nvl(p_requested_changes.shipment_changes.need_by_date(i),
10618                      poll.need_by_date),
10619                  nvl(p_requested_changes.shipment_changes.promised_date(i),
10620                                             poll.promised_date),
10621                  poll.LAST_ACCEPT_DATE                         ,
10622                  nvl(p_requested_changes.shipment_changes.price_override(i),
10623                                             poll.price_override),
10624                  -- Bug 3322019 START
10625                  NULL, -- ENCUMBERED_FLAG
10626                  NULL, -- ENCUMBERED_DATE
10627                  NULL, -- UNENCUMBERED_QUANTITY
10628                  -- Bug 3322019 END
10629                  poll.FOB_LOOKUP_CODE                         ,
10630                  poll.FREIGHT_TERMS_LOOKUP_CODE                ,
10631                  poll.TAXABLE_FLAG                            ,
10632                  0, -- ESTIMATED_TAX_AMOUNT (Bug 3322019)
10633                  poll.FROM_HEADER_ID                          ,
10634                  poll.FROM_LINE_ID                             ,
10635                  poll.FROM_LINE_LOCATION_ID                   ,
10636                  poll.START_DATE                               ,
10637                  poll.END_DATE                                ,
10638                  poll.LEAD_TIME                              ,
10639                  poll.LEAD_TIME_UNIT                          ,
10640                  poll.PRICE_DISCOUNT                           ,
10641                  poll.TERMS_ID                                 ,
10642                  -- Bug 3322019 START
10643                  'N', -- APPROVED_FLAG
10644                  NULL, -- APPROVED_DATE
10645                  NULL, -- CLOSED_FLAG
10646                  'N', -- CANCEL_FLAG
10647                  NULL, -- CANCELLED_BY
10648                  NULL, -- CANCEL_DATE
10649                  NULL, -- CANCEL_REASON
10650                  -- Bug 3322019 END
10651                  poll.FIRM_STATUS_LOOKUP_CODE                  ,
10652                  poll.FIRM_DATE                                ,
10653                  poll.ATTRIBUTE_CATEGORY                       ,
10654                  poll.ATTRIBUTE1                               ,
10655                  poll.ATTRIBUTE2                               ,
10656                  poll.ATTRIBUTE3                               ,
10657                  poll.ATTRIBUTE4                               ,
10658                  poll.ATTRIBUTE5                               ,
10659                  poll.ATTRIBUTE6                               ,
10660                  poll.ATTRIBUTE7                               ,
10661                  poll.ATTRIBUTE8                               ,
10662                  poll.ATTRIBUTE9                               ,
10663                  poll.ATTRIBUTE10                              ,
10664                  poll.UNIT_OF_MEASURE_CLASS                    ,
10665                  poll.ENCUMBER_NOW                             ,
10666                  poll.ATTRIBUTE11                              ,
10667                  poll.ATTRIBUTE12                              ,
10668                  poll.ATTRIBUTE13                              ,
10669                  poll.ATTRIBUTE14                              ,
10670                  poll.ATTRIBUTE15                              ,
10671                  poll.INSPECTION_REQUIRED_FLAG                 ,
10672                  poll.RECEIPT_REQUIRED_FLAG                    ,
10673                  poll.QTY_RCV_TOLERANCE                       ,
10674                  poll.QTY_RCV_EXCEPTION_CODE                   ,
10675                  poll.ENFORCE_SHIP_TO_LOCATION_CODE            ,
10676                  poll.ALLOW_SUBSTITUTE_RECEIPTS_FLAG           ,
10677                  poll.DAYS_EARLY_RECEIPT_ALLOWED               ,
10678                  poll.DAYS_LATE_RECEIPT_ALLOWED                ,
10679                  poll.RECEIPT_DAYS_EXCEPTION_CODE             ,
10680                  poll.INVOICE_CLOSE_TOLERANCE                  ,
10681                  poll.RECEIVE_CLOSE_TOLERANCE                  ,
10682                  poll.SHIP_TO_ORGANIZATION_ID                 ,
10683 
10684                  --<Bug 2798040 mbhargav START>
10685                  --iSP is passing shipment_num now
10686                  nvl(p_requested_changes.shipment_changes.split_shipment_num(i),
10687                                             FND_API.G_MISS_NUM),
10688                  --SHIPMENT_NUM                            ,
10689                  --<Bug 2798040 mbhargav START>
10690 
10691                  poll.SOURCE_SHIPMENT_ID                      ,
10692                  poll.SHIPMENT_TYPE                     ,
10693                  -- Bug 3322019 START
10694                  'OPEN', -- CLOSED_CODE
10695                  NULL, -- REQUEST_ID
10696                  NULL, -- PROGRAM_APPLICATION_ID
10697                  NULL, -- PROGRAM_ID
10698                  NULL, -- PROGRAM_UPDATE_DATE
10699                  -- Bug 3322019 START
10700                  poll.GOVERNMENT_CONTEXT                      ,
10701                  poll.RECEIVING_ROUTING_ID                     ,
10702                  poll.ACCRUE_ON_RECEIPT_FLAG                  ,
10703                  -- Bug 3322019 START
10704                  NULL, -- CLOSED_REASON
10705                  NULL, -- CLOSED_DATE
10706                  NULL, -- CLOSED_BY
10707                  -- Bug 3322019 END
10708                  poll.ORG_ID                                  ,
10709                  poll.GLOBAL_ATTRIBUTE1                        ,
10710                  poll.GLOBAL_ATTRIBUTE2                        ,
10711                  poll.GLOBAL_ATTRIBUTE3                        ,
10712                  poll.GLOBAL_ATTRIBUTE4                        ,
10713                  poll.GLOBAL_ATTRIBUTE5                        ,
10714                  poll.GLOBAL_ATTRIBUTE6                        ,
10715                  poll.GLOBAL_ATTRIBUTE7                        ,
10716                  poll.GLOBAL_ATTRIBUTE8                        ,
10717                  poll.GLOBAL_ATTRIBUTE9                        ,
10718                  poll.GLOBAL_ATTRIBUTE10                       ,
10719                  poll.GLOBAL_ATTRIBUTE11                       ,
10720                  poll.GLOBAL_ATTRIBUTE12                       ,
10721                  poll.GLOBAL_ATTRIBUTE13                       ,
10722                  poll.GLOBAL_ATTRIBUTE14                       ,
10723                  poll.GLOBAL_ATTRIBUTE15                       ,
10724                  poll.GLOBAL_ATTRIBUTE16                       ,
10725                  poll.GLOBAL_ATTRIBUTE17                       ,
10726                  poll.GLOBAL_ATTRIBUTE18                       ,
10727                  poll.GLOBAL_ATTRIBUTE19                       ,
10728                  poll.GLOBAL_ATTRIBUTE20                       ,
10729                  poll.GLOBAL_ATTRIBUTE_CATEGORY                ,
10730                  decode(poll.quantity_shipped,NULL,NULL,0), -- Bug 3322019
10731                  poll.COUNTRY_OF_ORIGIN_CODE                   ,
10732                  poll.TAX_USER_OVERRIDE_FLAG                  ,
10733                  poll.MATCH_OPTION                            ,
10734                  poll.TAX_CODE_ID                              ,
10735                  poll.CALCULATE_TAX_FLAG                      ,
10736                  poll.CHANGE_PROMISED_DATE_REASON            ,
10737                  poll.NOTE_TO_RECEIVER                        ,
10738                  decode(poll.secondary_quantity,NULL,NULL,0), -- Bug 3322019
10739                  poll.SECONDARY_UNIT_OF_MEASURE               ,
10740                  poll.PREFERRED_GRADE                         ,
10741                  -- Bug 3322019 START
10742                  decode(poll.secondary_quantity_received,NULL,NULL,0),
10743                  decode(poll.secondary_quantity_accepted,NULL,NULL,0),
10744                  decode(poll.secondary_quantity_rejected,NULL,NULL,0),
10745                  decode(poll.secondary_quantity_cancelled,NULL,NULL,0),
10746                  -- Bug 3322019 END
10747                  poll.VMI_FLAG                                 ,
10748                  poll.CONSIGNED_FLAG                           ,
10749                  poll.RETROACTIVE_DATE                         ,
10750                  -- <PO_CHANGE_API FPJ START> Added a changeable field:
10751                  NVL(p_requested_changes.shipment_changes.amount(i), poll.amount),
10752                  -- <PO_CHANGE_API FPJ END>
10753                  --<Complex Work R12 START>
10754                  poll.DESCRIPTION,
10755                  poll.PAYMENT_TYPE,
10756                  DECODE(p_requested_changes.shipment_changes.payment_type(i)
10757                        , 'RATE', 'QUANTITY'
10758                        , 'LUMPSUM', 'FIXED PRICE'
10759                        , POL.order_type_lookup_code
10760                  )
10761                 -- Note: the value basis decode assumes Milestone Pay Items are Amount
10762                 -- Milestones, since payment type is not changeable on Qty-based lines
10763                  --<Complex Work R12 END>
10764               FROM po_line_locations poll
10765                  , po_lines_all pol --<Complex Work R12>
10766               WHERE poll.line_location_id =
10767                     p_requested_changes.shipment_changes.parent_line_location_id(i)
10768               AND   p_requested_changes.shipment_changes.po_line_location_id(i) IS NULL
10769               AND   poll.po_line_id = pol.po_line_id;  --<Complex Work R12>
10770 
10771     END IF;
10772 
10773     IF p_requested_changes.distribution_changes IS NOT NULL THEN
10774 l_progress := '005';
10775 IF g_debug_stmt THEN
10776    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
10777      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
10778           || l_progress,'Making Distribution changes');
10779    END IF;
10780 END IF;
10781         -- SQL What: Updating the changeable quantities with either passed
10782         --           in values or if NULL then with existing values in the table
10783         -- SQL Why: Need to apply requested line level changes to global temp table
10784         -- SQL Join: po_line_id
10785         FORALL i IN 1..p_requested_changes.distribution_changes.po_distribution_id.COUNT
10786            UPDATE po_distributions_gt
10787             SET quantity_ordered = nvl(p_requested_changes.distribution_changes.quantity_ordered(i),
10788                                         quantity_ordered),
10789                 -- <PO_CHANGE_API FPJ START>
10790                 -- Added amount_ordered as a changeable field:
10791                 amount_ordered =
10792                   nvl(p_requested_changes.distribution_changes.amount_ordered(i),
10793                       amount_ordered)
10794                 -- <PO_CHANGE_API FPJ END>
10795             WHERE po_distribution_id = p_requested_changes.distribution_changes.po_distribution_id(i);
10796     END IF;
10797 
10798 l_progress := '006';
10799     x_return_status := FND_API.G_RET_STS_SUCCESS;
10800 EXCEPTION
10801     WHEN FND_API.G_EXC_ERROR THEN
10802         x_return_status := FND_API.G_RET_STS_ERROR;
10803 
10804     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
10805         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10806 
10807     WHEN OTHERS THEN
10808         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10809         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
10810             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
10811         END IF;
10812 
10813         IF (g_debug_unexp) THEN
10814                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
10815                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
10816                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
10817                        || l_progress || ' SQL CODE is '||sqlcode);
10818                 END IF;
10819         END IF;
10820 
10821 END UPDATE_GLOBAL_TEMP_TABLES;
10822 
10823 /**
10824 * Private Procedure: POPULATE_PO_HEADERS_GT
10825 * Requires:
10826 *   IN PARAMETERS:
10827 *       p_document_id: Id of submitted document
10828 * Modifies:
10829 * Effects:  Populates the global temp tables po_headers_gt
10830 * Returns:
10831 */
10832 PROCEDURE populate_po_headers_gt(p_document_id IN number,
10833                                     x_return_status OUT NOCOPY VARCHAR2)IS
10834 
10835 l_api_name  CONSTANT varchar2(40) := 'POPULATE_PO_HEADERS_GT';
10836 l_progress VARCHAR2(3);
10837 
10838 t_po_header_id NUMBER;
10839 t_segment1 po_headers.segment1%TYPE;
10840 
10841 BEGIN
10842 
10843 l_progress := '000';
10844 IF g_debug_stmt THEN
10845    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
10846      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
10847           || l_progress,'Populate HEADERS');
10848    END IF;
10849 END IF;
10850 
10851     INSERT INTO po_headers_gt(
10852             PO_HEADER_ID,
10853           AGENT_ID,
10854           TYPE_LOOKUP_CODE,
10855           LAST_UPDATE_DATE,
10856           LAST_UPDATED_BY,
10857           SEGMENT1,
10858           SUMMARY_FLAG,
10859           ENABLED_FLAG,
10860           SEGMENT2,
10861           SEGMENT3,
10862           SEGMENT4,
10863           SEGMENT5,
10864           START_DATE_ACTIVE,
10865           END_DATE_ACTIVE,
10866           LAST_UPDATE_LOGIN,
10867           CREATION_DATE ,
10868           CREATED_BY,
10869           VENDOR_ID,
10870           VENDOR_SITE_ID,
10871           VENDOR_CONTACT_ID,
10872           SHIP_TO_LOCATION_ID,
10873           BILL_TO_LOCATION_ID ,
10874           TERMS_ID             ,
10875           SHIP_VIA_LOOKUP_CODE  ,
10876           FOB_LOOKUP_CODE        ,
10877           FREIGHT_TERMS_LOOKUP_CODE,
10878           STATUS_LOOKUP_CODE,
10879           CURRENCY_CODE,
10880           RATE_TYPE,
10881           RATE_DATE,
10882           RATE,
10883           FROM_HEADER_ID,
10884           FROM_TYPE_LOOKUP_CODE,
10885           START_DATE,
10886           END_DATE,
10887           BLANKET_TOTAL_AMOUNT,
10888           AUTHORIZATION_STATUS,
10889           REVISION_NUM,
10890           REVISED_DATE,
10891           APPROVED_FLAG,
10892           APPROVED_DATE,
10893           AMOUNT_LIMIT,
10894           MIN_RELEASE_AMOUNT,
10895           NOTE_TO_AUTHORIZER,
10896           NOTE_TO_VENDOR,
10897           NOTE_TO_RECEIVER,
10898           PRINT_COUNT,
10899           PRINTED_DATE,
10900           VENDOR_ORDER_NUM,
10901           CONFIRMING_ORDER_FLAG,
10902           COMMENTS,
10903           REPLY_DATE,
10904           REPLY_METHOD_LOOKUP_CODE,
10905           RFQ_CLOSE_DATE,
10906           QUOTE_TYPE_LOOKUP_CODE,
10907           QUOTATION_CLASS_CODE,
10908           QUOTE_WARNING_DELAY_UNIT,
10909           QUOTE_WARNING_DELAY,
10910           QUOTE_VENDOR_QUOTE_NUMBER,
10911           ACCEPTANCE_REQUIRED_FLAG,
10912           ACCEPTANCE_DUE_DATE,
10913           CLOSED_DATE,
10914           USER_HOLD_FLAG,
10915           APPROVAL_REQUIRED_FLAG,
10916           CANCEL_FLAG,
10917           FIRM_STATUS_LOOKUP_CODE,
10918           FIRM_DATE,
10919           FROZEN_FLAG,
10920           ATTRIBUTE_CATEGORY,
10921           ATTRIBUTE1,
10922           ATTRIBUTE2,
10923           ATTRIBUTE3,
10924           ATTRIBUTE4,
10925           ATTRIBUTE5,
10926           ATTRIBUTE6,
10927           ATTRIBUTE7,
10928           ATTRIBUTE8,
10929           ATTRIBUTE9,
10930           ATTRIBUTE10,
10931           ATTRIBUTE11,
10932           ATTRIBUTE12,
10933             ATTRIBUTE13,
10934           ATTRIBUTE14,
10935           ATTRIBUTE15,
10936           CLOSED_CODE,
10937           GOVERNMENT_CONTEXT,
10938           REQUEST_ID,
10939           PROGRAM_APPLICATION_ID,
10940           PROGRAM_ID,
10941           PROGRAM_UPDATE_DATE,
10942           ORG_ID,
10943           SUPPLY_AGREEMENT_FLAG,
10944           EDI_PROCESSED_FLAG,
10945           EDI_PROCESSED_STATUS,
10946           GLOBAL_ATTRIBUTE_CATEGORY,
10947           GLOBAL_ATTRIBUTE1,
10948           GLOBAL_ATTRIBUTE2,
10949           GLOBAL_ATTRIBUTE3,
10950           GLOBAL_ATTRIBUTE4,
10951           GLOBAL_ATTRIBUTE5,
10952           GLOBAL_ATTRIBUTE6,
10953           GLOBAL_ATTRIBUTE7,
10954           GLOBAL_ATTRIBUTE8,
10955           GLOBAL_ATTRIBUTE9,
10956           GLOBAL_ATTRIBUTE10,
10957           GLOBAL_ATTRIBUTE11,
10958           GLOBAL_ATTRIBUTE12,
10959           GLOBAL_ATTRIBUTE13,
10960           GLOBAL_ATTRIBUTE14,
10961           GLOBAL_ATTRIBUTE15,
10962           GLOBAL_ATTRIBUTE16,
10963           GLOBAL_ATTRIBUTE17,
10964           GLOBAL_ATTRIBUTE18,
10965           GLOBAL_ATTRIBUTE19,
10966           GLOBAL_ATTRIBUTE20,
10967           INTERFACE_SOURCE_CODE,
10968           REFERENCE_NUM,
10969           WF_ITEM_TYPE,
10970           WF_ITEM_KEY,
10971           MRC_RATE_TYPE,
10972           MRC_RATE_DATE,
10973           MRC_RATE,
10974           PCARD_ID,
10975           PRICE_UPDATE_TOLERANCE,
10976           PAY_ON_CODE,
10977           XML_FLAG,
10978           XML_SEND_DATE,
10979           XML_CHANGE_SEND_DATE,
10980           GLOBAL_AGREEMENT_FLAG,
10981           CONSIGNED_CONSUMPTION_FLAG,
10982           CBC_ACCOUNTING_DATE,
10983             CONTERMS_EXIST_FLAG --<CONTERMS FPJ>
10984          ,  encumbrance_required_flag  --<ENCUMBRANCE FPJ>
10985           ,enable_all_sites          --<R12GCPA>
10986             )
10987           SELECT
10988             PO_HEADER_ID,
10989           AGENT_ID,
10990           TYPE_LOOKUP_CODE,
10991           LAST_UPDATE_DATE,
10992           LAST_UPDATED_BY,
10993           SEGMENT1,
10994           SUMMARY_FLAG,
10995           ENABLED_FLAG,
10996           SEGMENT2,
10997           SEGMENT3,
10998           SEGMENT4,
10999           SEGMENT5,
11000           START_DATE_ACTIVE,
11001           END_DATE_ACTIVE,
11002           LAST_UPDATE_LOGIN,
11003           CREATION_DATE ,
11004           CREATED_BY,
11005           VENDOR_ID,
11006           VENDOR_SITE_ID,
11007           VENDOR_CONTACT_ID,
11008           SHIP_TO_LOCATION_ID,
11009           BILL_TO_LOCATION_ID ,
11010           TERMS_ID             ,
11011           SHIP_VIA_LOOKUP_CODE  ,
11012           FOB_LOOKUP_CODE        ,
11013           FREIGHT_TERMS_LOOKUP_CODE,
11014           STATUS_LOOKUP_CODE,
11015           CURRENCY_CODE,
11016           RATE_TYPE,
11017           RATE_DATE,
11018           RATE,
11019           FROM_HEADER_ID,
11020           FROM_TYPE_LOOKUP_CODE,
11021           START_DATE,
11022           END_DATE,
11023           BLANKET_TOTAL_AMOUNT,
11024           AUTHORIZATION_STATUS,
11025           REVISION_NUM,
11026           REVISED_DATE,
11027           APPROVED_FLAG,
11028           APPROVED_DATE,
11029           AMOUNT_LIMIT,
11030           MIN_RELEASE_AMOUNT,
11031           NOTE_TO_AUTHORIZER,
11032           NOTE_TO_VENDOR,
11033           NOTE_TO_RECEIVER,
11034           PRINT_COUNT,
11035           PRINTED_DATE,
11036           VENDOR_ORDER_NUM,
11037           CONFIRMING_ORDER_FLAG,
11038           COMMENTS,
11039           REPLY_DATE,
11040           REPLY_METHOD_LOOKUP_CODE,
11041           RFQ_CLOSE_DATE,
11042           QUOTE_TYPE_LOOKUP_CODE,
11043           QUOTATION_CLASS_CODE,
11044           QUOTE_WARNING_DELAY_UNIT,
11045           QUOTE_WARNING_DELAY,
11046           QUOTE_VENDOR_QUOTE_NUMBER,
11047           ACCEPTANCE_REQUIRED_FLAG,
11048           ACCEPTANCE_DUE_DATE,
11049           CLOSED_DATE,
11050           USER_HOLD_FLAG,
11051           APPROVAL_REQUIRED_FLAG,
11052           CANCEL_FLAG,
11053           FIRM_STATUS_LOOKUP_CODE,
11054           FIRM_DATE,
11055           FROZEN_FLAG,
11056           ATTRIBUTE_CATEGORY,
11057           ATTRIBUTE1,
11058           ATTRIBUTE2,
11059           ATTRIBUTE3,
11060           ATTRIBUTE4,
11061           ATTRIBUTE5,
11062           ATTRIBUTE6,
11063           ATTRIBUTE7,
11064           ATTRIBUTE8,
11065           ATTRIBUTE9,
11066           ATTRIBUTE10,
11067           ATTRIBUTE11,
11068           ATTRIBUTE12,
11069             ATTRIBUTE13,
11070           ATTRIBUTE14,
11071           ATTRIBUTE15,
11072           CLOSED_CODE,
11073           GOVERNMENT_CONTEXT,
11074           REQUEST_ID,
11075           PROGRAM_APPLICATION_ID,
11076           PROGRAM_ID,
11077           PROGRAM_UPDATE_DATE,
11078           ORG_ID,
11079           SUPPLY_AGREEMENT_FLAG,
11080           EDI_PROCESSED_FLAG,
11081           EDI_PROCESSED_STATUS,
11082           GLOBAL_ATTRIBUTE_CATEGORY,
11083           GLOBAL_ATTRIBUTE1,
11084           GLOBAL_ATTRIBUTE2,
11085           GLOBAL_ATTRIBUTE3,
11086           GLOBAL_ATTRIBUTE4,
11087           GLOBAL_ATTRIBUTE5,
11088           GLOBAL_ATTRIBUTE6,
11089           GLOBAL_ATTRIBUTE7,
11090           GLOBAL_ATTRIBUTE8,
11091           GLOBAL_ATTRIBUTE9,
11092           GLOBAL_ATTRIBUTE10,
11093           GLOBAL_ATTRIBUTE11,
11094           GLOBAL_ATTRIBUTE12,
11095           GLOBAL_ATTRIBUTE13,
11096           GLOBAL_ATTRIBUTE14,
11097           GLOBAL_ATTRIBUTE15,
11098           GLOBAL_ATTRIBUTE16,
11099           GLOBAL_ATTRIBUTE17,
11100           GLOBAL_ATTRIBUTE18,
11101           GLOBAL_ATTRIBUTE19,
11102           GLOBAL_ATTRIBUTE20,
11103           INTERFACE_SOURCE_CODE,
11104           REFERENCE_NUM,
11105           WF_ITEM_TYPE,
11106           WF_ITEM_KEY,
11107           MRC_RATE_TYPE,
11108           MRC_RATE_DATE,
11109           MRC_RATE,
11110           PCARD_ID,
11111           PRICE_UPDATE_TOLERANCE,
11112           PAY_ON_CODE,
11113           XML_FLAG,
11114           XML_SEND_DATE,
11115           XML_CHANGE_SEND_DATE,
11116           GLOBAL_AGREEMENT_FLAG,
11117           CONSIGNED_CONSUMPTION_FLAG,
11118           CBC_ACCOUNTING_DATE,
11119             CONTERMS_EXIST_FLAG  --<CONTERMS FPJ>
11120          ,  encumbrance_required_flag  --<ENCUMBRANCE FPJ>
11121          , enable_all_sites  --<R12GCPA>
11122           FROM po_headers
11123           WHERE po_header_id = p_document_id;
11124 
11125 l_progress := '001';
11126 --SANITY check
11127 SELECT po_header_id, segment1 into t_po_header_id, t_segment1
11128 from po_headers_gt where po_header_id = p_document_id;
11129 
11130 IF g_debug_stmt THEN
11131    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
11132      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
11133           || l_progress,'HEADER ' || to_char(t_po_header_id) ||
11134           'PO NUM ' || t_segment1);
11135    END IF;
11136 END IF;
11137 
11138 l_progress := '002';
11139  x_return_status := FND_API.G_RET_STS_SUCCESS;
11140 EXCEPTION
11141     WHEN FND_API.G_EXC_ERROR THEN
11142         x_return_status := FND_API.G_RET_STS_ERROR;
11143 
11144     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
11145         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11146 
11147     WHEN OTHERS THEN
11148 
11149         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11150         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
11151             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
11152         END IF;
11153 
11154         IF (g_debug_unexp) THEN
11155                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
11156                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
11157                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
11158                        || l_progress || ' SQL CODE is '||sqlcode);
11159                 END IF;
11160         END IF;
11161 
11162 END POPULATE_PO_HEADERS_GT;
11163 
11164 
11165 -- <Doc Manager Rewrite 11.5.11 Start>
11166 -------------------------------------------------------------------------------
11167 --Start of Comments
11168 --Name: populate_po_lines_gt
11169 --Pre-reqs:
11170 --  None.
11171 --Modifies:
11172 --  PO_LINES_GT
11173 --Locks:
11174 --  None.
11175 --Function:
11176 --  Populates the lines GTT for submission checks.
11177 --Parameters:
11178 --IN:
11179 --p_doc_type
11180 --  Document type.  Use the g_doc_type_<> variables, where <> is:
11181 --    PA
11182 --    PO
11183 --p_doc_level
11184 --  The type of id that is being passed.  Use g_doc_level_<>
11185 --    HEADER
11186 --    LINE
11187 --    SHIPMENT
11188 --    DISTRIBUTION
11189 --p_doc_level_id
11190 --  Id of the doc level type of which to populate the table.
11191 --Testing:
11192 --
11193 --End of Comments
11194 -------------------------------------------------------------------------------
11195 PROCEDURE populate_po_lines_gt(
11196    p_doc_type                       IN             VARCHAR2
11197 ,  p_doc_level                      IN             VARCHAR2
11198 ,  p_doc_level_id                   IN             NUMBER
11199 ,  x_return_status                  OUT NOCOPY     VARCHAR2
11200 )
11201 IS
11202 
11203 l_api_name  CONSTANT varchar2(40) := 'POPULATE_PO_LINES_GT';
11204 l_progress VARCHAR2(3);
11205 
11206 l_line_id_tbl    po_tbl_number;
11207 
11208 BEGIN
11209 
11210   l_progress := '000';
11211 
11212   IF g_debug_stmt THEN
11213      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
11214        FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
11215             || l_progress,'Popluate LINES');
11216      END IF;
11217   END IF;
11218 
11219   l_progress := '010';
11220 
11221   PO_CORE_S.get_line_ids(
11222      p_doc_type => p_doc_type
11223   ,  p_doc_level => p_doc_level
11224   ,  p_doc_level_id_tbl => po_tbl_number( p_doc_level_id )
11225   ,  x_line_id_tbl => l_line_id_tbl
11226   );
11227 
11228   l_progress := '020';
11229 
11230   FORALL i IN 1 .. l_line_id_tbl.COUNT
11231     INSERT INTO po_lines_gt(
11232          PO_LINE_ID       ,
11233          LAST_UPDATE_DATE  ,
11234          LAST_UPDATED_BY    ,
11235          PO_HEADER_ID        ,
11236          LINE_TYPE_ID         ,
11237          LINE_NUM              ,
11238          LAST_UPDATE_LOGIN      ,
11239          CREATION_DATE           ,
11240          CREATED_BY               ,
11241          ITEM_ID                   ,
11242          ITEM_REVISION              ,
11243          CATEGORY_ID                 ,
11244          ITEM_DESCRIPTION             ,
11245          UNIT_MEAS_LOOKUP_CODE         ,
11246          QUANTITY_COMMITTED             ,
11247          COMMITTED_AMOUNT                ,
11248          ALLOW_PRICE_OVERRIDE_FLAG        ,
11249          NOT_TO_EXCEED_PRICE               ,
11250          LIST_PRICE_PER_UNIT                ,
11251          UNIT_PRICE                          ,
11252          QUANTITY                             ,
11253          UN_NUMBER_ID                          ,
11254          HAZARD_CLASS_ID                        ,
11255          NOTE_TO_VENDOR                          ,
11256          FROM_HEADER_ID                          ,
11257          FROM_LINE_ID                            ,
11258          MIN_ORDER_QUANTITY                      ,
11259          MAX_ORDER_QUANTITY                      ,
11260          QTY_RCV_TOLERANCE                       ,
11261          OVER_TOLERANCE_ERROR_FLAG               ,
11262          MARKET_PRICE                            ,
11263          UNORDERED_FLAG                          ,
11264          CLOSED_FLAG                              ,
11265          USER_HOLD_FLAG                          ,
11266          CANCEL_FLAG                              ,
11267          CANCELLED_BY                             ,
11268          CANCEL_DATE                             ,
11269          CANCEL_REASON                            ,
11270          FIRM_STATUS_LOOKUP_CODE                  ,
11271          FIRM_DATE                                ,
11272          VENDOR_PRODUCT_NUM                       ,
11273          CONTRACT_NUM                             ,
11274          TAXABLE_FLAG                             ,
11275          TAX_NAME                                 ,
11276          TYPE_1099                                ,
11277          CAPITAL_EXPENSE_FLAG                     ,
11278          NEGOTIATED_BY_PREPARER_FLAG              ,
11279          ATTRIBUTE_CATEGORY                       ,
11280          ATTRIBUTE1                               ,
11281          ATTRIBUTE2                               ,
11282          ATTRIBUTE3                               ,
11283          ATTRIBUTE4                               ,
11284          ATTRIBUTE5                               ,
11285          ATTRIBUTE6                               ,
11286          ATTRIBUTE7                               ,
11287          ATTRIBUTE8                               ,
11288          ATTRIBUTE9                              ,
11289          ATTRIBUTE10                              ,
11290          REFERENCE_NUM                            ,
11291          ATTRIBUTE11                              ,
11292          ATTRIBUTE12                              ,
11293          ATTRIBUTE13                              ,
11294          ATTRIBUTE14                              ,
11295          ATTRIBUTE15                              ,
11296          MIN_RELEASE_AMOUNT                       ,
11297          PRICE_TYPE_LOOKUP_CODE                   ,
11298          CLOSED_CODE                              ,
11299          PRICE_BREAK_LOOKUP_CODE                  ,
11300          GOVERNMENT_CONTEXT                       ,
11301          REQUEST_ID                               ,
11302          PROGRAM_APPLICATION_ID                   ,
11303          PROGRAM_ID                               ,
11304          PROGRAM_UPDATE_DATE                      ,
11305          CLOSED_DATE                              ,
11306          CLOSED_REASON                            ,
11307          CLOSED_BY                                ,
11308          TRANSACTION_REASON_CODE                 ,
11309          ORG_ID                                   ,
11310          QC_GRADE                                 ,
11311          BASE_UOM                                 ,
11312          BASE_QTY                                 ,
11313          SECONDARY_UOM                            ,
11314          SECONDARY_QTY                            ,
11315          GLOBAL_ATTRIBUTE_CATEGORY                ,
11316          GLOBAL_ATTRIBUTE1                        ,
11317          GLOBAL_ATTRIBUTE2                        ,
11318          GLOBAL_ATTRIBUTE3                        ,
11319          GLOBAL_ATTRIBUTE4                        ,
11320          GLOBAL_ATTRIBUTE5                        ,
11321          GLOBAL_ATTRIBUTE6                        ,
11322          GLOBAL_ATTRIBUTE7                        ,
11323          GLOBAL_ATTRIBUTE8                        ,
11324          GLOBAL_ATTRIBUTE9                        ,
11325          GLOBAL_ATTRIBUTE10                       ,
11326          GLOBAL_ATTRIBUTE11                       ,
11327          GLOBAL_ATTRIBUTE12                       ,
11328          GLOBAL_ATTRIBUTE13                       ,
11329          GLOBAL_ATTRIBUTE14                       ,
11330          GLOBAL_ATTRIBUTE15                       ,
11331          GLOBAL_ATTRIBUTE16                       ,
11332          GLOBAL_ATTRIBUTE17                      ,
11333          GLOBAL_ATTRIBUTE18                      ,
11334          GLOBAL_ATTRIBUTE19                      ,
11335          GLOBAL_ATTRIBUTE20                      ,
11336          LINE_REFERENCE_NUM                       ,
11337          PROJECT_ID                               ,
11338          TASK_ID                                  ,
11339          EXPIRATION_DATE                          ,
11340          TAX_CODE_ID                              ,
11341          OKE_CONTRACT_HEADER_ID                   ,
11342          OKE_CONTRACT_VERSION_ID                  ,
11343          SECONDARY_QUANTITY                       ,
11344          SECONDARY_UNIT_OF_MEASURE               ,
11345          PREFERRED_GRADE                          ,
11346          AUCTION_HEADER_ID                       ,
11347          AUCTION_DISPLAY_NUMBER                  ,
11348          AUCTION_LINE_NUMBER                     ,
11349          BID_NUMBER                              ,
11350          BID_LINE_NUMBER                          ,
11351          RETROACTIVE_DATE                         ,
11352          CONTRACT_ID                              ,   -- <GC FPJ>
11353          START_DATE                               , -- <PO_CHANGE_API FPJ>
11354          AMOUNT                                   , -- <PO_CHANGE_API FPJ>
11355          ORDER_TYPE_LOOKUP_CODE                   ,            -- <BUG 3262859>
11356          PURCHASE_BASIS                           ,            -- <BUG 3262859>
11357          MATCHING_BASIS                                        -- <BUG 3262859>
11358      )
11359      SELECT
11360          PO_LINE_ID                              ,
11361          LAST_UPDATE_DATE                        ,
11362          LAST_UPDATED_BY                 ,
11363          PO_HEADER_ID                    ,
11364          LINE_TYPE_ID                   ,
11365          LINE_NUM                        ,
11366          LAST_UPDATE_LOGIN                ,
11367          CREATION_DATE                     ,
11368          CREATED_BY                         ,
11369          ITEM_ID                             ,
11370          ITEM_REVISION                        ,
11371          CATEGORY_ID                           ,
11372          ITEM_DESCRIPTION                       ,
11373          UNIT_MEAS_LOOKUP_CODE                   ,
11374          QUANTITY_COMMITTED                      ,
11375          COMMITTED_AMOUNT                         ,
11376          ALLOW_PRICE_OVERRIDE_FLAG               ,
11377          NOT_TO_EXCEED_PRICE                      ,
11378          LIST_PRICE_PER_UNIT                      ,
11379          UNIT_PRICE                               ,
11380          QUANTITY                                 ,
11381          UN_NUMBER_ID                             ,
11382          HAZARD_CLASS_ID                          ,
11383          NOTE_TO_VENDOR                           ,
11384          FROM_HEADER_ID                          ,
11385          FROM_LINE_ID                            ,
11386          MIN_ORDER_QUANTITY                      ,
11387          MAX_ORDER_QUANTITY                      ,
11388          QTY_RCV_TOLERANCE                       ,
11389          OVER_TOLERANCE_ERROR_FLAG               ,
11390          MARKET_PRICE                            ,
11391          UNORDERED_FLAG                          ,
11392          CLOSED_FLAG                              ,
11393          USER_HOLD_FLAG                          ,
11394          CANCEL_FLAG                              ,
11395          CANCELLED_BY                             ,
11396          CANCEL_DATE                             ,
11397          CANCEL_REASON                            ,
11398          FIRM_STATUS_LOOKUP_CODE                  ,
11399          FIRM_DATE                                ,
11400          VENDOR_PRODUCT_NUM                       ,
11401          CONTRACT_NUM                             ,
11402          TAXABLE_FLAG                             ,
11403          TAX_NAME                                 ,
11404          TYPE_1099                                ,
11405          CAPITAL_EXPENSE_FLAG                     ,
11406          NEGOTIATED_BY_PREPARER_FLAG              ,
11407          ATTRIBUTE_CATEGORY                       ,
11408          ATTRIBUTE1                               ,
11409          ATTRIBUTE2                               ,
11410          ATTRIBUTE3                               ,
11411          ATTRIBUTE4                               ,
11412          ATTRIBUTE5                               ,
11413          ATTRIBUTE6                               ,
11414          ATTRIBUTE7                               ,
11415          ATTRIBUTE8                               ,
11416          ATTRIBUTE9                              ,
11417          ATTRIBUTE10                              ,
11418          REFERENCE_NUM                            ,
11419          ATTRIBUTE11                              ,
11420          ATTRIBUTE12                              ,
11421          ATTRIBUTE13                              ,
11422          ATTRIBUTE14                              ,
11423          ATTRIBUTE15                              ,
11424          MIN_RELEASE_AMOUNT                       ,
11425          PRICE_TYPE_LOOKUP_CODE                   ,
11426          CLOSED_CODE                              ,
11427          PRICE_BREAK_LOOKUP_CODE                  ,
11428          GOVERNMENT_CONTEXT                       ,
11429          REQUEST_ID                               ,
11430          PROGRAM_APPLICATION_ID                   ,
11431          PROGRAM_ID                               ,
11432          PROGRAM_UPDATE_DATE                      ,
11433          CLOSED_DATE                              ,
11434          CLOSED_REASON                            ,
11435          CLOSED_BY                                ,
11436          TRANSACTION_REASON_CODE                 ,
11437          ORG_ID                                   ,
11438          QC_GRADE                                 ,
11439          BASE_UOM                                 ,
11440          BASE_QTY                                 ,
11441          SECONDARY_UOM                            ,
11442          SECONDARY_QTY                            ,
11443          GLOBAL_ATTRIBUTE_CATEGORY                ,
11444          GLOBAL_ATTRIBUTE1                        ,
11445          GLOBAL_ATTRIBUTE2                        ,
11446          GLOBAL_ATTRIBUTE3                        ,
11447          GLOBAL_ATTRIBUTE4                        ,
11448          GLOBAL_ATTRIBUTE5                        ,
11449          GLOBAL_ATTRIBUTE6                        ,
11450          GLOBAL_ATTRIBUTE7                        ,
11451          GLOBAL_ATTRIBUTE8                        ,
11452          GLOBAL_ATTRIBUTE9                        ,
11453          GLOBAL_ATTRIBUTE10                       ,
11454          GLOBAL_ATTRIBUTE11                       ,
11455          GLOBAL_ATTRIBUTE12                       ,
11456          GLOBAL_ATTRIBUTE13                       ,
11457          GLOBAL_ATTRIBUTE14                       ,
11458          GLOBAL_ATTRIBUTE15                       ,
11459          GLOBAL_ATTRIBUTE16                       ,
11460          GLOBAL_ATTRIBUTE17                      ,
11461          GLOBAL_ATTRIBUTE18                      ,
11462          GLOBAL_ATTRIBUTE19                      ,
11463          GLOBAL_ATTRIBUTE20                      ,
11464          LINE_REFERENCE_NUM                       ,
11465          PROJECT_ID                               ,
11466          TASK_ID                                  ,
11467          EXPIRATION_DATE                          ,
11468          TAX_CODE_ID                              ,
11469          OKE_CONTRACT_HEADER_ID                   ,
11470          OKE_CONTRACT_VERSION_ID                  ,
11471          SECONDARY_QUANTITY                       ,
11472          SECONDARY_UNIT_OF_MEASURE               ,
11473          PREFERRED_GRADE                          ,
11474          AUCTION_HEADER_ID                       ,
11475          AUCTION_DISPLAY_NUMBER                  ,
11476          AUCTION_LINE_NUMBER                     ,
11477          BID_NUMBER                              ,
11478          BID_LINE_NUMBER                          ,
11479          RETROACTIVE_DATE                        ,
11480          CONTRACT_ID                              , -- <GC FPJ>
11481          START_DATE                               , -- <PO_CHANGE_API FPJ>
11482          AMOUNT                                   , -- <PO_CHANGE_API FPJ>
11483          ORDER_TYPE_LOOKUP_CODE                   ,            -- <BUG 3262859>
11484          PURCHASE_BASIS                           ,            -- <BUG 3262859>
11485          MATCHING_BASIS                                        -- <BUG 3262859>
11486       FROM po_lines_all pol
11487       WHERE pol.po_line_id = l_line_id_tbl(i)
11488       ;
11489 
11490   l_progress := '030';
11491   x_return_status := FND_API.G_RET_STS_SUCCESS;
11492 
11493 EXCEPTION
11494     WHEN FND_API.G_EXC_ERROR THEN
11495         x_return_status := FND_API.G_RET_STS_ERROR;
11496 
11497     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
11498         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11499 
11500     WHEN OTHERS THEN
11501         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11502         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
11503             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
11504         END IF;
11505 
11506         IF (g_debug_unexp) THEN
11507                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
11508                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
11509                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
11510                        || l_progress || ' SQL CODE is '||sqlcode);
11511                 END IF;
11512         END IF;
11513 
11514 END POPULATE_PO_LINES_GT;
11515 -- <End Doc Manager Rewrite 11.5.11>
11516 
11517 
11518 /**
11519 * Private Procedure: POPULATE_RELEASES_GT
11520 * Requires:
11521 *   IN PARAMETERS:
11522 *       p_document_id: Id of submitted document
11523 * Modifies:
11524 * Effects:  Populates the global temp tables po_headers_gt
11525 * Returns:
11526 */
11527 PROCEDURE populate_releases_gt(p_document_id IN NUMBER,
11528                  x_return_status OUT NOCOPY VARCHAR2) IS
11529 
11530 l_api_name  CONSTANT varchar2(40) := 'POPULATE_RELEASES_GT';
11531 l_progress VARCHAR2(3);
11532 
11533 BEGIN
11534 l_progress := '000';
11535 IF g_debug_stmt THEN
11536    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
11537      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
11538           || l_progress,'Populate PO Releases');
11539    END IF;
11540 END IF;
11541 
11542     INSERT INTO po_releases_gt(
11543          PO_RELEASE_ID                   ,
11544          LAST_UPDATE_DATE                ,
11545          LAST_UPDATED_BY                ,
11546          PO_HEADER_ID                  ,
11547          RELEASE_NUM                    ,
11548          AGENT_ID                       ,
11549          RELEASE_DATE                  ,
11550          LAST_UPDATE_LOGIN              ,
11551          CREATION_DATE                  ,
11552          CREATED_BY                      ,
11553          REVISION_NUM                     ,
11554          REVISED_DATE                      ,
11555          APPROVED_FLAG                      ,
11556          APPROVED_DATE                       ,
11557          PRINT_COUNT                          ,
11558          PRINTED_DATE                          ,
11559          ACCEPTANCE_REQUIRED_FLAG               ,
11560          ACCEPTANCE_DUE_DATE                     ,
11561          HOLD_BY                                  ,
11562          HOLD_DATE                                ,
11563          HOLD_REASON                              ,
11564          HOLD_FLAG                                ,
11565          CANCEL_FLAG                              ,
11566          CANCELLED_BY                             ,
11567          CANCEL_DATE                              ,
11568          CANCEL_REASON                            ,
11569          FIRM_STATUS_LOOKUP_CODE                  ,
11570          FIRM_DATE                                ,
11571          ATTRIBUTE_CATEGORY                       ,
11572          ATTRIBUTE1                               ,
11573          ATTRIBUTE2                               ,
11574          ATTRIBUTE3                               ,
11575          ATTRIBUTE4                               ,
11576          ATTRIBUTE5                               ,
11577          ATTRIBUTE6                               ,
11578          ATTRIBUTE7                               ,
11579          ATTRIBUTE8                               ,
11580          ATTRIBUTE9                               ,
11581          ATTRIBUTE10                              ,
11582          ATTRIBUTE11                              ,
11583          ATTRIBUTE12                              ,
11584          ATTRIBUTE13                              ,
11585          ATTRIBUTE14                              ,
11586          ATTRIBUTE15                              ,
11587          AUTHORIZATION_STATUS                     ,
11588          GOVERNMENT_CONTEXT                       ,
11589          REQUEST_ID                               ,
11590          PROGRAM_APPLICATION_ID                   ,
11591          PROGRAM_ID                               ,
11592          PROGRAM_UPDATE_DATE                      ,
11593          CLOSED_CODE                              ,
11594          FROZEN_FLAG                              ,
11595          RELEASE_TYPE                             ,
11596          NOTE_TO_VENDOR                           ,
11597          ORG_ID                                   ,
11598          EDI_PROCESSED_FLAG                       ,
11599          GLOBAL_ATTRIBUTE_CATEGORY                ,
11600          GLOBAL_ATTRIBUTE1                        ,
11601          GLOBAL_ATTRIBUTE2                        ,
11602          GLOBAL_ATTRIBUTE3                        ,
11603          GLOBAL_ATTRIBUTE4                        ,
11604          GLOBAL_ATTRIBUTE5                        ,
11605          GLOBAL_ATTRIBUTE6                        ,
11606          GLOBAL_ATTRIBUTE7                        ,
11607          GLOBAL_ATTRIBUTE8                        ,
11608          GLOBAL_ATTRIBUTE9                        ,
11609          GLOBAL_ATTRIBUTE10                       ,
11610          GLOBAL_ATTRIBUTE11                       ,
11611          GLOBAL_ATTRIBUTE12                       ,
11612          GLOBAL_ATTRIBUTE13                       ,
11613          GLOBAL_ATTRIBUTE14                       ,
11614          GLOBAL_ATTRIBUTE15                       ,
11615          GLOBAL_ATTRIBUTE16                       ,
11616          GLOBAL_ATTRIBUTE17                       ,
11617          GLOBAL_ATTRIBUTE18                       ,
11618          GLOBAL_ATTRIBUTE19                       ,
11619          GLOBAL_ATTRIBUTE20                       ,
11620          WF_ITEM_TYPE                             ,
11621          WF_ITEM_KEY                              ,
11622          PCARD_ID                                ,
11623          PAY_ON_CODE                             ,
11624          XML_FLAG                                 ,
11625          XML_SEND_DATE                            ,
11626          XML_CHANGE_SEND_DATE                     ,
11627          CONSIGNED_CONSUMPTION_FLAG               ,
11628          CBC_ACCOUNTING_DATE)
11629       SELECT
11630                   PO_RELEASE_ID                   ,
11631          LAST_UPDATE_DATE                ,
11632          LAST_UPDATED_BY                ,
11633          PO_HEADER_ID                  ,
11634          RELEASE_NUM                    ,
11635          AGENT_ID                       ,
11636          RELEASE_DATE                  ,
11637          LAST_UPDATE_LOGIN              ,
11638          CREATION_DATE                  ,
11639          CREATED_BY                      ,
11640          REVISION_NUM                     ,
11641          REVISED_DATE                      ,
11642          APPROVED_FLAG                      ,
11643          APPROVED_DATE                       ,
11644          PRINT_COUNT                          ,
11645          PRINTED_DATE                          ,
11646          ACCEPTANCE_REQUIRED_FLAG               ,
11647          ACCEPTANCE_DUE_DATE                     ,
11648          HOLD_BY                                  ,
11649          HOLD_DATE                                ,
11650          HOLD_REASON                              ,
11651          HOLD_FLAG                                ,
11652          CANCEL_FLAG                              ,
11653          CANCELLED_BY                             ,
11654          CANCEL_DATE                              ,
11655          CANCEL_REASON                            ,
11656          FIRM_STATUS_LOOKUP_CODE                  ,
11657          FIRM_DATE                                ,
11658          ATTRIBUTE_CATEGORY                       ,
11659          ATTRIBUTE1                               ,
11660          ATTRIBUTE2                               ,
11661          ATTRIBUTE3                               ,
11662          ATTRIBUTE4                               ,
11663          ATTRIBUTE5                               ,
11664          ATTRIBUTE6                               ,
11665          ATTRIBUTE7                               ,
11666          ATTRIBUTE8                               ,
11667          ATTRIBUTE9                               ,
11668          ATTRIBUTE10                              ,
11669          ATTRIBUTE11                              ,
11670          ATTRIBUTE12                              ,
11671          ATTRIBUTE13                              ,
11672          ATTRIBUTE14                              ,
11673          ATTRIBUTE15                              ,
11674          AUTHORIZATION_STATUS                     ,
11675          GOVERNMENT_CONTEXT                       ,
11676          REQUEST_ID                               ,
11677          PROGRAM_APPLICATION_ID                   ,
11678          PROGRAM_ID                               ,
11679          PROGRAM_UPDATE_DATE                      ,
11680          CLOSED_CODE                              ,
11681          FROZEN_FLAG                              ,
11682          RELEASE_TYPE                             ,
11683          NOTE_TO_VENDOR                           ,
11684          ORG_ID                                   ,
11685          EDI_PROCESSED_FLAG                       ,
11686          GLOBAL_ATTRIBUTE_CATEGORY                ,
11687          GLOBAL_ATTRIBUTE1                        ,
11688          GLOBAL_ATTRIBUTE2                        ,
11689          GLOBAL_ATTRIBUTE3                        ,
11690          GLOBAL_ATTRIBUTE4                        ,
11691          GLOBAL_ATTRIBUTE5                        ,
11692          GLOBAL_ATTRIBUTE6                        ,
11693          GLOBAL_ATTRIBUTE7                        ,
11694          GLOBAL_ATTRIBUTE8                        ,
11695          GLOBAL_ATTRIBUTE9                        ,
11696          GLOBAL_ATTRIBUTE10                       ,
11697          GLOBAL_ATTRIBUTE11                       ,
11698          GLOBAL_ATTRIBUTE12                       ,
11699          GLOBAL_ATTRIBUTE13                       ,
11700          GLOBAL_ATTRIBUTE14                       ,
11701          GLOBAL_ATTRIBUTE15                       ,
11702          GLOBAL_ATTRIBUTE16                       ,
11703          GLOBAL_ATTRIBUTE17                       ,
11704          GLOBAL_ATTRIBUTE18                       ,
11705          GLOBAL_ATTRIBUTE19                       ,
11706          GLOBAL_ATTRIBUTE20                       ,
11707          WF_ITEM_TYPE                             ,
11708          WF_ITEM_KEY                              ,
11709          PCARD_ID                                ,
11710          PAY_ON_CODE                             ,
11711          XML_FLAG                                 ,
11712          XML_SEND_DATE                            ,
11713          XML_CHANGE_SEND_DATE                     ,
11714          CONSIGNED_CONSUMPTION_FLAG               ,
11715          CBC_ACCOUNTING_DATE
11716      FROM po_releases
11717      WHERE po_release_id = p_document_id;
11718 
11719 l_progress := '001';
11720     x_return_status := FND_API.G_RET_STS_SUCCESS;
11721 EXCEPTION
11722     WHEN FND_API.G_EXC_ERROR THEN
11723         x_return_status := FND_API.G_RET_STS_ERROR;
11724 
11725     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
11726         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11727 
11728     WHEN OTHERS THEN
11729         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11730         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
11731             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
11732         END IF;
11733 
11734         IF (g_debug_unexp) THEN
11735                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
11736                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
11737                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
11738                        || l_progress || ' SQL CODE is '||sqlcode);
11739                 END IF;
11740         END IF;
11741 
11742 END POPULATE_RELEASES_GT;
11743 
11744 
11745 /**
11746 * Private Procedure: POPULATE_REQ_HEADERS_GT
11747 * Requires:
11748 *   IN PARAMETERS:
11749 *       p_document_id: Id of submitted document
11750 * Modifies:
11751 * Effects:  Populates the global temp tables po_headers_gt
11752 * Returns:
11753 */
11754 PROCEDURE populate_req_headers_gt(p_document_id IN NUMBER,
11755                         x_return_status OUT NOCOPY VARCHAR2) IS
11756 
11757 l_api_name  CONSTANT varchar2(40) := 'POPULATE_REQ_HEADERS_GT';
11758 l_progress VARCHAR2(3);
11759 
11760 BEGIN
11761 
11762 l_progress := '000';
11763 IF g_debug_stmt THEN
11764    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
11765      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
11766           || l_progress,'Populate Req Headers');
11767    END IF;
11768 END IF;
11769 
11770     INSERT INTO po_req_headers_gt(
11771          PROGRAM_UPDATE_DATE                      ,
11772          INTERFACE_SOURCE_CODE                    ,
11773          INTERFACE_SOURCE_LINE_ID                 ,
11774          CLOSED_CODE                              ,
11775          ORG_ID                                   ,
11776          DESCRIPTION                              ,
11777          AUTHORIZATION_STATUS                     ,
11778          NOTE_TO_AUTHORIZER                       ,
11779          TYPE_LOOKUP_CODE                         ,
11780          TRANSFERRED_TO_OE_FLAG                   ,
11781          ATTRIBUTE_CATEGORY                       ,
11782          ATTRIBUTE1                               ,
11783          ATTRIBUTE2                               ,
11784          ATTRIBUTE3                               ,
11785          ATTRIBUTE4                               ,
11786          ATTRIBUTE5                               ,
11787          ON_LINE_FLAG                             ,
11788          PRELIMINARY_RESEARCH_FLAG                ,
11789          RESEARCH_COMPLETE_FLAG                   ,
11790          PREPARER_FINISHED_FLAG                   ,
11791          PREPARER_FINISHED_DATE                   ,
11792          AGENT_RETURN_FLAG                        ,
11793          AGENT_RETURN_NOTE                        ,
11794          CANCEL_FLAG                              ,
11795          ATTRIBUTE6                               ,
11796          ATTRIBUTE7                               ,
11797          ATTRIBUTE8                               ,
11798          ATTRIBUTE9                               ,
11799          ATTRIBUTE10                              ,
11800          ATTRIBUTE11                              ,
11801          ATTRIBUTE12                              ,
11802          ATTRIBUTE13                              ,
11803          ATTRIBUTE14                              ,
11804          ATTRIBUTE15                              ,
11805          GOVERNMENT_CONTEXT                       ,
11806          REQUEST_ID                               ,
11807          PROGRAM_APPLICATION_ID                   ,
11808          PROGRAM_ID                               ,
11809          REQUISITION_HEADER_ID             ,
11810          PREPARER_ID                       ,
11811          LAST_UPDATE_DATE                   ,
11812          LAST_UPDATED_BY                   ,
11813          SEGMENT1                          ,
11814          SUMMARY_FLAG                       ,
11815          ENABLED_FLAG                       ,
11816          SEGMENT2                                 ,
11817          SEGMENT3                                 ,
11818          SEGMENT4                                 ,
11819          SEGMENT5                                 ,
11820          START_DATE_ACTIVE                        ,
11821          END_DATE_ACTIVE                          ,
11822          LAST_UPDATE_LOGIN                        ,
11823          CREATION_DATE                            ,
11824          CREATED_BY                               ,
11825          WF_ITEM_TYPE                             ,
11826          WF_ITEM_KEY                              ,
11827          EMERGENCY_PO_NUM                          ,
11828          PCARD_ID                                    ,
11829          APPS_SOURCE_CODE                          ,
11830          CBC_ACCOUNTING_DATE)
11831      SELECT
11832          PROGRAM_UPDATE_DATE                      ,
11833          INTERFACE_SOURCE_CODE                    ,
11834          INTERFACE_SOURCE_LINE_ID                 ,
11835          CLOSED_CODE                              ,
11836          ORG_ID                                   ,
11837          DESCRIPTION                              ,
11838          AUTHORIZATION_STATUS                     ,
11839          substr(NOTE_TO_AUTHORIZER,1,480)         , -- Bug4443295(added substr)
11840          TYPE_LOOKUP_CODE                         ,
11841          TRANSFERRED_TO_OE_FLAG                   ,
11842          ATTRIBUTE_CATEGORY                       ,
11843          ATTRIBUTE1                               ,
11844          ATTRIBUTE2                               ,
11845          ATTRIBUTE3                               ,
11846          ATTRIBUTE4                               ,
11847          ATTRIBUTE5                               ,
11848          ON_LINE_FLAG                             ,
11849          PRELIMINARY_RESEARCH_FLAG                ,
11850          RESEARCH_COMPLETE_FLAG                   ,
11851          PREPARER_FINISHED_FLAG                   ,
11852          PREPARER_FINISHED_DATE                   ,
11853          AGENT_RETURN_FLAG                        ,
11854          AGENT_RETURN_NOTE                        ,
11855          CANCEL_FLAG                              ,
11856          ATTRIBUTE6                               ,
11857          ATTRIBUTE7                               ,
11858          ATTRIBUTE8                               ,
11859          ATTRIBUTE9                               ,
11860          ATTRIBUTE10                              ,
11861          ATTRIBUTE11                              ,
11862          ATTRIBUTE12                              ,
11863          ATTRIBUTE13                              ,
11864          ATTRIBUTE14                              ,
11865          ATTRIBUTE15                              ,
11866          GOVERNMENT_CONTEXT                       ,
11867          REQUEST_ID                               ,
11868          PROGRAM_APPLICATION_ID                   ,
11869          PROGRAM_ID                               ,
11870          REQUISITION_HEADER_ID             ,
11871          PREPARER_ID                       ,
11872          LAST_UPDATE_DATE                   ,
11873          LAST_UPDATED_BY                   ,
11874          SEGMENT1                          ,
11875          SUMMARY_FLAG                       ,
11876          ENABLED_FLAG                       ,
11877          SEGMENT2                                 ,
11878          SEGMENT3                                 ,
11879          SEGMENT4                                 ,
11880          SEGMENT5                                 ,
11881          START_DATE_ACTIVE                        ,
11882          END_DATE_ACTIVE                          ,
11883          LAST_UPDATE_LOGIN                        ,
11884          CREATION_DATE                            ,
11885          CREATED_BY                               ,
11886          WF_ITEM_TYPE                             ,
11887          WF_ITEM_KEY                              ,
11888          EMERGENCY_PO_NUM                          ,
11889          PCARD_ID                                    ,
11890          APPS_SOURCE_CODE                          ,
11891          CBC_ACCOUNTING_DATE
11892     FROM po_requisition_headers
11893     WHERE requisition_header_id = p_document_id;
11894 
11895 l_progress := '001';
11896     x_return_status := FND_API.G_RET_STS_SUCCESS;
11897 EXCEPTION
11898     WHEN FND_API.G_EXC_ERROR THEN
11899         x_return_status := FND_API.G_RET_STS_ERROR;
11900 
11901     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
11902         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11903 
11904     WHEN OTHERS THEN
11905         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11906         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
11907             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
11908         END IF;
11909 
11910         IF (g_debug_unexp) THEN
11911                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
11912                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
11913                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
11914                        || l_progress || ' SQL CODE is '||sqlcode);
11915                 END IF;
11916         END IF;
11917 
11918 END POPULATE_REQ_HEADERS_GT;
11919 
11920 /**
11921 * Private Procedure: POPULATE_REQ_LINES_GT
11922 * Requires:
11923 *   IN PARAMETERS:
11924 *       p_document_id: Id of submitted document
11925 * Modifies:
11926 * Effects:  Populates the global temp tables po_headers_gt
11927 * Returns:
11928 */
11929 PROCEDURE populate_req_lines_gt(p_document_id IN NUMBER,
11930                        x_return_status OUT NOCOPY VARCHAR2) IS
11931 
11932 l_api_name  CONSTANT varchar2(40) := 'POPULATE_REQ_LINES_GT';
11933 l_progress VARCHAR2(3);
11934 
11935 BEGIN
11936 
11937 l_progress := '000';
11938 IF g_debug_stmt THEN
11939    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
11940      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
11941           || l_progress,'Populate Req Lines');
11942    END IF;
11943 END IF;
11944 
11945     INSERT INTO po_req_lines_gt(
11946          REQUEST_ID                               ,
11947          PROGRAM_APPLICATION_ID                   ,
11948          PROGRAM_ID                               ,
11949          PROGRAM_UPDATE_DATE                      ,
11950          GOVERNMENT_CONTEXT                       ,
11951          CLOSED_REASON                            ,
11952          CLOSED_DATE                              ,
11953          TRANSACTION_REASON_CODE                  ,
11954          QUANTITY_RECEIVED                        ,
11955          SOURCE_REQ_LINE_ID                       ,
11956          ORG_ID                                   ,
11957          CANCEL_REASON                            ,
11958          CLOSED_CODE                              ,
11959          AGENT_RETURN_NOTE                        ,
11960          CHANGED_AFTER_RESEARCH_FLAG              ,
11961          VENDOR_ID                                ,
11962          VENDOR_SITE_ID                           ,
11963          VENDOR_CONTACT_ID                        ,
11964          RESEARCH_AGENT_ID                        ,
11965          ON_LINE_FLAG                             ,
11966          WIP_ENTITY_ID                            ,
11967          WIP_LINE_ID                              ,
11968          WIP_REPETITIVE_SCHEDULE_ID               ,
11969          WIP_OPERATION_SEQ_NUM                    ,
11970          WIP_RESOURCE_SEQ_NUM                     ,
11971          ATTRIBUTE_CATEGORY                       ,
11972          DESTINATION_CONTEXT                      ,
11973          INVENTORY_SOURCE_CONTEXT                 ,
11974          VENDOR_SOURCE_CONTEXT                    ,
11975          ATTRIBUTE1                               ,
11976          ATTRIBUTE2                               ,
11977          ATTRIBUTE3                               ,
11978          ATTRIBUTE4                               ,
11979          ATTRIBUTE5                               ,
11980          ATTRIBUTE6                               ,
11981          ATTRIBUTE7                               ,
11982          ATTRIBUTE8                               ,
11983          ATTRIBUTE9                               ,
11984          ATTRIBUTE10                              ,
11985          ATTRIBUTE11                              ,
11986          ATTRIBUTE12                              ,
11987          ATTRIBUTE13                              ,
11988          ATTRIBUTE14                              ,
11989          ATTRIBUTE15                              ,
11990          BOM_RESOURCE_ID                          ,
11991          PARENT_REQ_LINE_ID                       ,
11992          JUSTIFICATION                            ,
11993          NOTE_TO_AGENT                            ,
11994          NOTE_TO_RECEIVER                         ,
11995          PURCHASING_AGENT_ID                      ,
11996          DOCUMENT_TYPE_CODE                       ,
11997          BLANKET_PO_HEADER_ID                     ,
11998          BLANKET_PO_LINE_NUM                      ,
11999          CURRENCY_CODE                            ,
12000          RATE_TYPE                                ,
12001          RATE_DATE                                ,
12002          RATE                                     ,
12003          CURRENCY_UNIT_PRICE                      ,
12004          SUGGESTED_VENDOR_NAME                    ,
12005          SUGGESTED_VENDOR_LOCATION                ,
12006          SUGGESTED_VENDOR_CONTACT                 ,
12007          SUGGESTED_VENDOR_PHONE                   ,
12008          SUGGESTED_VENDOR_PRODUCT_CODE            ,
12009          UN_NUMBER_ID                             ,
12010          HAZARD_CLASS_ID                          ,
12011          MUST_USE_SUGG_VENDOR_FLAG                ,
12012          REFERENCE_NUM                            ,
12013          ON_RFQ_FLAG                              ,
12014          URGENT_FLAG                              ,
12015          CANCEL_FLAG                              ,
12016          SOURCE_ORGANIZATION_ID                   ,
12017          SOURCE_SUBINVENTORY                      ,
12018          DESTINATION_TYPE_CODE                    ,
12019          DESTINATION_ORGANIZATION_ID              ,
12020          DESTINATION_SUBINVENTORY                 ,
12021          QUANTITY_CANCELLED                       ,
12022          CANCEL_DATE                              ,
12023          REQUISITION_LINE_ID               ,
12024          REQUISITION_HEADER_ID             ,
12025          LINE_NUM                          ,
12026          LINE_TYPE_ID                      ,
12027          CATEGORY_ID                       ,
12028          ITEM_DESCRIPTION                  ,
12029          UNIT_MEAS_LOOKUP_CODE             ,
12030          UNIT_PRICE                        ,
12031          QUANTITY                          ,
12032          AMOUNT                            ,                  -- <SERVICES FPJ>
12033          DELIVER_TO_LOCATION_ID           ,
12034          TO_PERSON_ID                     ,
12035          LAST_UPDATE_DATE                 ,
12036          LAST_UPDATED_BY                   ,
12037          SOURCE_TYPE_CODE                  ,
12038          LAST_UPDATE_LOGIN                        ,
12039          CREATION_DATE                            ,
12040          CREATED_BY                               ,
12041          ITEM_ID                                  ,
12042          ITEM_REVISION                            ,
12043          QUANTITY_DELIVERED                       ,
12044          SUGGESTED_BUYER_ID                       ,
12045          ENCUMBERED_FLAG                          ,
12046          RFQ_REQUIRED_FLAG                        ,
12047          NEED_BY_DATE                             ,
12048          LINE_LOCATION_ID                         ,
12049          MODIFIED_BY_AGENT_FLAG                   ,
12050          KANBAN_CARD_ID                           ,
12051          CATALOG_TYPE                             ,
12052          CATALOG_SOURCE                           ,
12053          MANUFACTURER_ID                          ,
12054          MANUFACTURER_NAME                        ,
12055          MANUFACTURER_PART_NUMBER                 ,
12056          REQUESTER_EMAIL                          ,
12057          REQUESTER_FAX                            ,
12058          REQUESTER_PHONE                          ,
12059          UNSPSC_CODE                              ,
12060          OTHER_CATEGORY_CODE                      ,
12061          SUPPLIER_DUNS                            ,
12062          TAX_STATUS_INDICATOR                     ,
12063          PCARD_FLAG                               ,
12064          NEW_SUPPLIER_FLAG                        ,
12065          AUTO_RECEIVE_FLAG                        ,
12066          TAX_USER_OVERRIDE_FLAG                   ,
12067          TAX_CODE_ID                              ,
12068          NOTE_TO_VENDOR                           ,
12069          OKE_CONTRACT_HEADER_ID                   ,
12070          OKE_CONTRACT_VERSION_ID                  ,
12071          ITEM_SOURCE_ID                           ,
12072          SUPPLIER_REF_NUMBER                      ,
12073          SECONDARY_UNIT_OF_MEASURE                ,
12074          SECONDARY_QUANTITY                       ,
12075          PREFERRED_GRADE                          ,
12076          SECONDARY_QUANTITY_RECEIVED              ,
12077          SECONDARY_QUANTITY_CANCELLED             ,
12078          AUCTION_HEADER_ID                        ,
12079          AUCTION_DISPLAY_NUMBER                   ,
12080          AUCTION_LINE_NUMBER                      ,
12081          REQS_IN_POOL_FLAG                        ,
12082          VMI_FLAG                                 ,
12083          BID_NUMBER                               ,
12084          BID_LINE_NUMBER)
12085     SELECT
12086          REQUEST_ID                               ,
12087          PROGRAM_APPLICATION_ID                   ,
12088          PROGRAM_ID                               ,
12089          PROGRAM_UPDATE_DATE                      ,
12090          GOVERNMENT_CONTEXT                       ,
12091          CLOSED_REASON                            ,
12092          CLOSED_DATE                              ,
12093          TRANSACTION_REASON_CODE                  ,
12094          QUANTITY_RECEIVED                        ,
12095          SOURCE_REQ_LINE_ID                       ,
12096          ORG_ID                                   ,
12097          CANCEL_REASON                            ,
12098          CLOSED_CODE                              ,
12099          AGENT_RETURN_NOTE                        ,
12100          CHANGED_AFTER_RESEARCH_FLAG              ,
12101          VENDOR_ID                                ,
12102          VENDOR_SITE_ID                           ,
12103          VENDOR_CONTACT_ID                        ,
12104          RESEARCH_AGENT_ID                        ,
12105          ON_LINE_FLAG                             ,
12106          WIP_ENTITY_ID                            ,
12107          WIP_LINE_ID                              ,
12108          WIP_REPETITIVE_SCHEDULE_ID               ,
12109          WIP_OPERATION_SEQ_NUM                    ,
12110          WIP_RESOURCE_SEQ_NUM                     ,
12111          ATTRIBUTE_CATEGORY                       ,
12112          DESTINATION_CONTEXT                      ,
12113          INVENTORY_SOURCE_CONTEXT                 ,
12114          VENDOR_SOURCE_CONTEXT                    ,
12115          ATTRIBUTE1                               ,
12116          ATTRIBUTE2                               ,
12117          ATTRIBUTE3                               ,
12118          ATTRIBUTE4                               ,
12119          ATTRIBUTE5                               ,
12120          ATTRIBUTE6                               ,
12121          ATTRIBUTE7                               ,
12122          ATTRIBUTE8                               ,
12123          ATTRIBUTE9                               ,
12124          ATTRIBUTE10                              ,
12125          ATTRIBUTE11                              ,
12126          ATTRIBUTE12                              ,
12127          ATTRIBUTE13                              ,
12128          ATTRIBUTE14                              ,
12129          ATTRIBUTE15                              ,
12130          BOM_RESOURCE_ID                          ,
12131          PARENT_REQ_LINE_ID                       ,
12132          JUSTIFICATION                            ,
12133          NOTE_TO_AGENT                            ,
12134          NOTE_TO_RECEIVER                         ,
12135          PURCHASING_AGENT_ID                      ,
12136          DOCUMENT_TYPE_CODE                       ,
12137          BLANKET_PO_HEADER_ID                     ,
12138          BLANKET_PO_LINE_NUM                      ,
12139          CURRENCY_CODE                            ,
12140          RATE_TYPE                                ,
12141          RATE_DATE                                ,
12142          RATE                                     ,
12143          CURRENCY_UNIT_PRICE                      ,
12144          SUGGESTED_VENDOR_NAME                    ,
12145          SUGGESTED_VENDOR_LOCATION                ,
12146          SUGGESTED_VENDOR_CONTACT                 ,
12147          SUGGESTED_VENDOR_PHONE                   ,
12148          SUGGESTED_VENDOR_PRODUCT_CODE            ,
12149          UN_NUMBER_ID                             ,
12150          HAZARD_CLASS_ID                          ,
12151          MUST_USE_SUGG_VENDOR_FLAG                ,
12152          REFERENCE_NUM                            ,
12153          ON_RFQ_FLAG                              ,
12154          URGENT_FLAG                              ,
12155          CANCEL_FLAG                              ,
12156          SOURCE_ORGANIZATION_ID                   ,
12157          SOURCE_SUBINVENTORY                      ,
12158          DESTINATION_TYPE_CODE                    ,
12159          DESTINATION_ORGANIZATION_ID              ,
12160          DESTINATION_SUBINVENTORY                 ,
12161          QUANTITY_CANCELLED                       ,
12162          CANCEL_DATE                              ,
12163          REQUISITION_LINE_ID               ,
12164          REQUISITION_HEADER_ID             ,
12165          LINE_NUM                          ,
12166          LINE_TYPE_ID                      ,
12167          CATEGORY_ID                       ,
12168          ITEM_DESCRIPTION                  ,
12169          UNIT_MEAS_LOOKUP_CODE             ,
12170          UNIT_PRICE                        ,
12171          QUANTITY                          ,
12172          AMOUNT                            ,                  -- <SERVICES FPJ>
12173          DELIVER_TO_LOCATION_ID           ,
12174          TO_PERSON_ID                     ,
12175          LAST_UPDATE_DATE                 ,
12176          LAST_UPDATED_BY                   ,
12177          SOURCE_TYPE_CODE                  ,
12178          LAST_UPDATE_LOGIN                        ,
12179          CREATION_DATE                            ,
12180          CREATED_BY                               ,
12181          ITEM_ID                                  ,
12182          ITEM_REVISION                            ,
12183          QUANTITY_DELIVERED                       ,
12184          SUGGESTED_BUYER_ID                       ,
12185          ENCUMBERED_FLAG                          ,
12186          RFQ_REQUIRED_FLAG                        ,
12187          NEED_BY_DATE                             ,
12188          LINE_LOCATION_ID                         ,
12189          MODIFIED_BY_AGENT_FLAG                   ,
12190          KANBAN_CARD_ID                           ,
12191          CATALOG_TYPE                             ,
12192          CATALOG_SOURCE                           ,
12193          MANUFACTURER_ID                          ,
12194          MANUFACTURER_NAME                        ,
12195          MANUFACTURER_PART_NUMBER                 ,
12196          REQUESTER_EMAIL                          ,
12197          REQUESTER_FAX                            ,
12198          REQUESTER_PHONE                          ,
12199          UNSPSC_CODE                              ,
12200          OTHER_CATEGORY_CODE                      ,
12201          SUPPLIER_DUNS                            ,
12202          TAX_STATUS_INDICATOR                     ,
12203          PCARD_FLAG                               ,
12204          NEW_SUPPLIER_FLAG                        ,
12205          AUTO_RECEIVE_FLAG                        ,
12206          TAX_USER_OVERRIDE_FLAG                   ,
12207          TAX_CODE_ID                              ,
12208          NOTE_TO_VENDOR                           ,
12209          OKE_CONTRACT_HEADER_ID                   ,
12210          OKE_CONTRACT_VERSION_ID                  ,
12211          ITEM_SOURCE_ID                           ,
12212          SUPPLIER_REF_NUMBER                      ,
12213          SECONDARY_UNIT_OF_MEASURE                ,
12214          SECONDARY_QUANTITY                       ,
12215          PREFERRED_GRADE                          ,
12216          SECONDARY_QUANTITY_RECEIVED              ,
12217          SECONDARY_QUANTITY_CANCELLED             ,
12218          AUCTION_HEADER_ID                        ,
12219          AUCTION_DISPLAY_NUMBER                   ,
12220          AUCTION_LINE_NUMBER                      ,
12221          REQS_IN_POOL_FLAG                        ,
12222          VMI_FLAG                                 ,
12223          BID_NUMBER                               ,
12224          BID_LINE_NUMBER
12225     FROM po_requisition_lines
12226     WHERE requisition_header_id = p_document_id;
12227 
12228 l_progress := '001';
12229     x_return_status := FND_API.G_RET_STS_SUCCESS;
12230 EXCEPTION
12231     WHEN FND_API.G_EXC_ERROR THEN
12232         x_return_status := FND_API.G_RET_STS_ERROR;
12233 
12234     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
12235         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12236 
12237     WHEN OTHERS THEN
12238         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12239         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
12240             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
12241         END IF;
12242 
12243         IF (g_debug_unexp) THEN
12244                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
12245                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
12246                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
12247                        || l_progress || ' SQL CODE is '||sqlcode);
12248                 END IF;
12249         END IF;
12250 
12251 END POPULATE_REQ_LINES_GT;
12252 
12253 /**
12254 * Private Procedure: POPULATE_REQ_DISTRIBUTIONS_GT
12255 * Requires:
12256 *   IN PARAMETERS:
12257 *       p_document_id: Id of submitted document
12258 * Modifies:
12259 * Effects:  Populates the global temp tables po_headers_gt
12260 * Returns:
12261 */
12262 PROCEDURE populate_req_distributions_gt(
12263    p_document_id                    IN             NUMBER
12264 )
12265 IS
12266 
12267 l_api_name  CONSTANT varchar2(40) := 'POPULATE_REQ_DISTRIBUTIONS_GT';
12268 l_progress VARCHAR2(3);
12269 
12270 BEGIN
12271 
12272 l_progress := '000';
12273 IF g_debug_stmt THEN
12274    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
12275      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
12276           || l_progress,'Populate Req Distributions');
12277    END IF;
12278 END IF;
12279 
12280     INSERT INTO po_req_distributions_gt(
12281          DISTRIBUTION_ID                   ,
12282          LAST_UPDATE_DATE                  ,
12283          LAST_UPDATED_BY                   ,
12284          REQUISITION_LINE_ID               ,
12285          SET_OF_BOOKS_ID                   ,
12286          CODE_COMBINATION_ID               ,
12287          REQ_LINE_QUANTITY                 ,
12288          REQ_LINE_AMOUNT                   ,                  -- <SERVICES FPJ>
12289          LAST_UPDATE_LOGIN                        ,
12290          CREATION_DATE                            ,
12291          CREATED_BY                               ,
12292          ENCUMBERED_FLAG                          ,
12293          GL_ENCUMBERED_DATE                       ,
12294          GL_ENCUMBERED_PERIOD_NAME                ,
12295          GL_CANCELLED_DATE                        ,
12296          FAILED_FUNDS_LOOKUP_CODE                 ,
12297          ENCUMBERED_AMOUNT                        ,
12298          BUDGET_ACCOUNT_ID                        ,
12299          ACCRUAL_ACCOUNT_ID                       ,
12300          ORG_ID                                   ,
12301          VARIANCE_ACCOUNT_ID                      ,
12302          PREVENT_ENCUMBRANCE_FLAG                 ,
12303          ATTRIBUTE_CATEGORY                       ,
12304          ATTRIBUTE1                               ,
12305          ATTRIBUTE2                               ,
12306          ATTRIBUTE3                               ,
12307          ATTRIBUTE4                               ,
12308          ATTRIBUTE5                               ,
12309          ATTRIBUTE6                               ,
12310          ATTRIBUTE7                               ,
12311          ATTRIBUTE8                               ,
12312          ATTRIBUTE9                               ,
12313          ATTRIBUTE10                              ,
12314          ATTRIBUTE11                              ,
12315          ATTRIBUTE12                              ,
12316          ATTRIBUTE13                              ,
12317          ATTRIBUTE14                              ,
12318          ATTRIBUTE15                              ,
12319          GOVERNMENT_CONTEXT                       ,
12320          REQUEST_ID                               ,
12321          PROGRAM_APPLICATION_ID                   ,
12322          PROGRAM_ID                               ,
12323          PROGRAM_UPDATE_DATE                      ,
12324          PROJECT_ID                               ,
12325          TASK_ID                                  ,
12326          EXPENDITURE_TYPE                         ,
12327          PROJECT_ACCOUNTING_CONTEXT               ,
12328          EXPENDITURE_ORGANIZATION_ID              ,
12329          GL_CLOSED_DATE                           ,
12330          SOURCE_REQ_DISTRIBUTION_ID               ,
12331          DISTRIBUTION_NUM                         ,
12332          PROJECT_RELATED_FLAG                     ,
12333          EXPENDITURE_ITEM_DATE                    ,
12334          ALLOCATION_TYPE                          ,
12335          ALLOCATION_VALUE                         ,
12336          AWARD_ID                                 ,
12337          END_ITEM_UNIT_NUMBER                     ,
12338          RECOVERABLE_TAX                          ,
12339          NONRECOVERABLE_TAX                       ,
12340          RECOVERY_RATE                            ,
12341          TAX_RECOVERY_OVERRIDE_FLAG               ,
12342          OKE_CONTRACT_LINE_ID                     ,
12343          OKE_CONTRACT_DELIVERABLE_ID)
12344     SELECT
12345          PRD.DISTRIBUTION_ID                   ,
12346          PRD.LAST_UPDATE_DATE                  ,
12347          PRD.LAST_UPDATED_BY                   ,
12348          PRD.REQUISITION_LINE_ID               ,
12349          PRD.SET_OF_BOOKS_ID                   ,
12350          PRD.CODE_COMBINATION_ID               ,
12351          PRD.REQ_LINE_QUANTITY                 ,
12352          PRD.REQ_LINE_AMOUNT                   ,              -- <SERVICES FPJ>
12353          PRD.LAST_UPDATE_LOGIN                        ,
12354          PRD.CREATION_DATE                            ,
12355          PRD.CREATED_BY                               ,
12356          PRD.ENCUMBERED_FLAG                          ,
12357          PRD.GL_ENCUMBERED_DATE                       ,
12358          PRD.GL_ENCUMBERED_PERIOD_NAME                ,
12359          PRD.GL_CANCELLED_DATE                        ,
12360          PRD.FAILED_FUNDS_LOOKUP_CODE                 ,
12361          PRD.ENCUMBERED_AMOUNT                        ,
12362          PRD.BUDGET_ACCOUNT_ID                        ,
12363          PRD.ACCRUAL_ACCOUNT_ID                       ,
12364          PRD.ORG_ID                                   ,
12365          PRD.VARIANCE_ACCOUNT_ID                      ,
12366          PRD.PREVENT_ENCUMBRANCE_FLAG                 ,
12367          PRD.ATTRIBUTE_CATEGORY                       ,
12368          PRD.ATTRIBUTE1                               ,
12369          PRD.ATTRIBUTE2                               ,
12370          PRD.ATTRIBUTE3                               ,
12371          PRD.ATTRIBUTE4                               ,
12372          PRD.ATTRIBUTE5                               ,
12373          PRD.ATTRIBUTE6                               ,
12374          PRD.ATTRIBUTE7                               ,
12375          PRD.ATTRIBUTE8                               ,
12376          PRD.ATTRIBUTE9                               ,
12377          PRD.ATTRIBUTE10                              ,
12378          PRD.ATTRIBUTE11                              ,
12379          PRD.ATTRIBUTE12                              ,
12380          PRD.ATTRIBUTE13                              ,
12381          PRD.ATTRIBUTE14                              ,
12382          PRD.ATTRIBUTE15                              ,
12383          PRD.GOVERNMENT_CONTEXT                       ,
12384          PRD.REQUEST_ID                               ,
12385          PRD.PROGRAM_APPLICATION_ID                   ,
12386          PRD.PROGRAM_ID                               ,
12387          PRD.PROGRAM_UPDATE_DATE                      ,
12388          PRD.PROJECT_ID                               ,
12389          PRD.TASK_ID                                  ,
12390          PRD.EXPENDITURE_TYPE                         ,
12391          PRD.PROJECT_ACCOUNTING_CONTEXT               ,
12392          PRD.EXPENDITURE_ORGANIZATION_ID              ,
12393          PRD.GL_CLOSED_DATE                           ,
12394          PRD.SOURCE_REQ_DISTRIBUTION_ID               ,
12395          PRD.DISTRIBUTION_NUM                         ,
12396          PRD.PROJECT_RELATED_FLAG                     ,
12397          PRD.EXPENDITURE_ITEM_DATE                    ,
12398          PRD.ALLOCATION_TYPE                          ,
12399          PRD.ALLOCATION_VALUE                         ,
12400          PRD.AWARD_ID                                 ,
12401          PRD.END_ITEM_UNIT_NUMBER                     ,
12402          PRD.RECOVERABLE_TAX                          ,
12403          PRD.NONRECOVERABLE_TAX                       ,
12404          PRD.RECOVERY_RATE                            ,
12405          PRD.TAX_RECOVERY_OVERRIDE_FLAG               ,
12406          PRD.OKE_CONTRACT_LINE_ID                     ,
12407          PRD.OKE_CONTRACT_DELIVERABLE_ID
12408     FROM po_req_distributions PRD, po_requisition_lines PRL
12409     WHERE PRD.requisition_line_id = PRL.requisition_line_id AND
12410           PRL.requisition_header_id = p_document_id;
12411 
12412 l_progress := '001';
12413 
12414 END POPULATE_REQ_DISTRIBUTIONS_GT;
12415 
12416 -- <FPJ, Refactor Security API START>
12417 /**
12418 * Public Procedure: PO_Security_Check
12419 * Requires:
12420 *   IN PARAMETERS:
12421 *     p_api_version:          Version number of API that caller expects. It
12422 *                             should match the l_api_version defined in the
12423 *                             procedure
12424 *     p_query_table:          Table you want to check
12425 *     p_owner_id_column:      Owner id column of the table
12426 *     p_employee_id:          User id to access the document
12427 *     p_minimum_access_level: Minimum access level to the document
12428 *     p_document_type:        The type of the document to perform
12429 *                             the security check on
12430 *     p_document_subtype:     The subtype of the document.
12431 *     p_type_clause:          The document type clause to be used in
12432 *                             constructing where clause
12433 *
12434 * Modifies: None
12435 * Effects:  This procedure builds dynamic WHERE clause fragments based on
12436 *           document security parameters.
12437 * Returns:
12438 *   x_return_status: FND_API.G_RET_STS_SUCCESS if API succeeds
12439 *                    FND_API.G_RET_STS_ERROR if API fails
12440 *                    FND_API.G_RET_STS_UNEXP_ERROR if unexpected error
12441 *   x_msg_data:      Contains error msg in case x_return_status returned
12442 *                    FND_API.G_RET_STS_ERROR or
12443 *                    FND_API.G_RET_STS_UNEXP_ERROR
12444 *   x_where_clause:  The constructed where clause
12445 */
12446 
12447 PROCEDURE PO_SECURITY_CHECK (p_api_version          IN NUMBER,
12448                              p_query_table          IN VARCHAR2,
12449                              p_owner_id_column      IN VARCHAR2,
12450                              p_employee_id          IN VARCHAR2,
12451                              p_minimum_access_level IN VARCHAR2,
12452                              p_document_type        IN VARCHAR2,
12453                              p_document_subtype     IN VARCHAR2,
12454                              p_type_clause          IN VARCHAR2,
12455                              x_return_status        OUT NOCOPY VARCHAR2,
12456                              x_msg_data             OUT NOCOPY VARCHAR2,
12457                              x_where_clause         OUT NOCOPY VARCHAR2)
12458 IS
12459 
12460   l_api_name    CONSTANT varchar2(30) := 'PO_SECURITY_CHECK';
12461   l_api_version   CONSTANT NUMBER       := 1.0;
12462   l_progress    VARCHAR2(3);
12463   l_access_level  PO_DOCUMENT_TYPES.access_level_code%TYPE;
12464   l_security_level  PO_DOCUMENT_TYPES.security_level_code%TYPE;
12465   l_security_hierarchy  NUMBER;
12466   l_id_column           varchar2(30);
12467   l_doctype_column      varchar2(30); /*Bug 7229262/7239696*/
12468 
12469 BEGIN
12470 
12471   l_progress := '000';
12472   -- Standard call to check for call compatibility
12473   IF NOT FND_API.Compatible_API_Call(l_api_version, p_api_version, l_api_name, G_PKG_NAME)
12474   THEN
12475     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12476   END IF;
12477 
12478   l_progress := '010';
12479   IF g_debug_stmt THEN
12480      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
12481        FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
12482             || l_progress, 'deciding security level');
12483      END IF;
12484   END IF;
12485 
12486    l_progress := '020';
12487 
12488   -- Bug 3663057
12489   -- Get the id column based on the document type being queried
12490   -- Bug 5054685: Performance issue: Added 'PO_PA' as another parameter to collapse
12491   -- multiple security related where-clauses into one.
12492   -- Bug 4082310: Handling the missed case of QUOTATION and RFQ.
12493     -- l_id_column is used to construct the where clause of the pre-query
12494     -- in join condition against po_action_history table. This fix
12495     -- is just to make the where clause valid even though QUOTATION
12496     -- and RFQ are not logged in po_action_history table. Avoiding
12497     -- adding logic to omit the condition in these cases as it will
12498     -- unnecessarily complicate the code.
12499 
12500    /* Bug 7229262/7239696 */
12501    l_doctype_column := 'TYPE_LOOKUP_CODE';
12502    /* End Bug 7229262/7239696 */
12503   IF p_document_type in ('PO','PA', 'PO_PA','QUOTATION','RFQ') THEN
12504      l_id_column := 'PO_HEADER_ID';
12505   ELSIF p_document_type = 'RELEASE' THEN
12506      l_id_column := 'PO_RELEASE_ID';
12507   ELSIF p_document_type = 'REQUISITION' THEN
12508      l_id_column := 'REQUISITION_HEADER_ID';
12509   ELSE
12510     l_id_column := 'PO_HEADER_ID';
12511   END IF;
12512   -- <R12 MOAC start>
12513   IF (p_query_table = 'PO_WF_NOTIFICATIONS_V' ) THEN
12514     l_id_column := 'OBJECT_ID' ;
12515 	l_doctype_column := 'DOC_TYPE'; /*Bug 7229262/7239696*/
12516   END IF;
12517   -- <R12 MOAC end>
12518 
12519 
12520   l_progress := '060';
12521   IF g_debug_stmt THEN
12522      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
12523        FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
12524             || l_progress, 'deciding WHERE clause');
12525      END IF;
12526   END IF;
12527 
12528    -- Construct WHERE clause with type_clause
12529   x_where_clause := '(NOT(' || p_type_clause || ') OR (';
12530 
12531   /*Bug6640107 Removed the check for the access level here. First we check for all the security levels and
12532     then finally AND with the access level check
12533     For security level of private only the owner should be able to view the document on the enter PO form
12534     irrespective of the access level*/
12535 
12536   x_where_clause := x_where_clause ||
12537                    '(('||p_query_table||'.security_level_code = ''PUBLIC'' )';
12538 
12539   x_where_clause := x_where_clause ||
12540                     ' OR '||'('||p_query_table || '.security_level_code = ''PRIVATE'' AND '||
12541          p_employee_id || '=' || p_owner_id_column ||')';
12542 	/* OR (' ||
12543         'EXISTS (SELECT ''Y'' FROM PO_ACTION_HISTORY POAH2 ' ||
12544         'WHERE POAH2.employee_id = ' || p_employee_id ||
12545         ' AND POAH2.object_type_code = (DECODE(' || p_query_table || '.type_lookup_code, ''BLANKET'', ''PA'', ''STANDARD'', ''PO'' , ''PLANNED'' , ''PO'' , ''CONTRACT'' , ''PA'', ''RELEASE'' , ''RELEASE'' ) )' ||
12546         ' AND POAH2.object_id = ' || p_query_table || '.' || l_id_column || '))))';*/
12547 
12548   x_where_clause := x_where_clause ||
12549                    ' OR '||'('||p_query_table || '.security_level_code = ''HIERARCHY'' AND '||
12550        '((' || p_employee_id || '=' || p_owner_id_column ||') OR (' ||
12551        'EXISTS (SELECT ''Y'' FROM PO_ACTION_HISTORY POAH2 ' ||
12552        'WHERE POAH2.employee_id =' || p_employee_id ||
12553 	   /* Bug 7229262/7239696
12554 	   ' AND POAH2.object_type_code = (DECODE(' || p_query_table || '.type_lookup_code, ''BLANKET'', ''PA'', ''STANDARD'', ''PO'' , ''PLANNED'' , ''PO'' , ''CONTRACT'' , ''PA'', ''RELEASE'' , ''RELEASE'' ) ) ' ||
12555 	   */
12556 	   ' AND POAH2.object_type_code = (DECODE(' || p_query_table || '.'||l_doctype_column||', ''BLANKET'', ''PA'', ''STANDARD'', ''PO'' , ''PLANNED'' , ''PO'' , ''CONTRACT'' , ''PA'', ''RELEASE'' , ''RELEASE'' ) )' ||
12557 		/* End Bug 7229262/7239696 */
12558 
12559        ' AND POAH2.object_id = ' || p_query_table || '.' || l_id_column || ')) OR (' ||
12560        p_employee_id || ' IN (SELECT H.superior_id ' ||
12561        ' FROM PO_EMPLOYEE_HIERARCHIES H, PO_SYSTEM_PARAMETERS PSP WHERE H.employee_id = ' ||
12562        p_query_table || '.' || p_owner_id_column ||
12563        ' AND H.position_structure_id = NVL(PSP.SECURITY_POSITION_STRUCTURE_ID,-1) '
12564        ||'AND PSP.ORG_ID = '||p_query_table || '.ORG_ID '
12565        ||'))))';
12566 
12567   x_where_clause := x_where_clause ||
12568                    ' OR '||'('||p_query_table || '.security_level_code = ''PURCHASING''  AND '||
12569        '((' || p_employee_id || '=' || p_owner_id_column ||') OR (' ||
12570        'EXISTS (SELECT ''Y'' FROM PO_ACTION_HISTORY POAH2 ' ||
12571        'WHERE POAH2.employee_id =' || p_employee_id ||
12572 	   /* Bug 7229262/7239696
12573 	   ' AND POAH2.object_type_code = (DECODE(' || p_query_table || '.type_lookup_code, ''BLANKET'', ''PA'', ''STANDARD'', ''PO'' , ''PLANNED'' , ''PO'' , ''CONTRACT'' , ''PA'', ''RELEASE'' , ''RELEASE'' ) ) ' ||
12574 	   */
12575        ' AND POAH2.object_type_code = (DECODE(' || p_query_table || '.'||l_doctype_column||', ''BLANKET'', ''PA'', ''STANDARD'', ''PO'' , ''PLANNED'' , ''PO'' , ''CONTRACT'' , ''PA'', ''RELEASE'' , ''RELEASE'' ) ) ' ||
12576 		/* End Bug 7229262/7239696 */
12577        ' AND POAH2.object_id = ' || p_query_table || '.' || l_id_column || ')) OR (' ||
12578        'EXISTS(SELECT NULL FROM PO_AGENTS WHERE agent_id= ' ||
12579        p_employee_id || ' AND SYSDATE BETWEEN NVL(start_date_active, ' ||
12580        'SYSDATE) AND NVL(end_date_active, SYSDATE+1))))' ||')'
12581        ||')';
12582 /*Bug6640107 : Here the document access level is the access level set in po_headers_v and the required access level is p_minimum_access_level.
12583 The following allows us to either view the document or to open and mofify it depending on the document access level*/
12584 
12585   x_where_clause := x_where_clause || 'AND (('''||p_minimum_access_level||''' = ''VIEW_ONLY'' ) OR ( '''|| p_minimum_access_level||''' = ''MODIFY'' AND ' || p_query_table||'.access_level_code IN (''MODIFY'',''FULL'') ) OR ( '''
12586   || p_minimum_access_level||''' = ''FULL'' AND '|| p_query_table||'.access_level_code = ''FULL'' ) OR ( '||p_employee_id ||' = '|| p_owner_id_column ||'))))';
12587 
12588 
12589 
12590   l_progress := '070';
12591   x_return_status := FND_API.G_RET_STS_SUCCESS;
12592   x_msg_data := NULL;
12593 
12594   l_progress := '100';
12595   IF g_debug_stmt THEN
12596     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
12597       FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
12598                    || l_progress,'Returning from PVT package: ' ||
12599                    x_where_clause);
12600     END IF;
12601   END IF;
12602 
12603 EXCEPTION
12604   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
12605     x_msg_data := FND_MSG_PUB.GET(p_msg_index => FND_MSG_PUB.G_LAST,
12606                                   p_encoded => 'F');
12607     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12608     x_where_clause := NULL;
12609   WHEN FND_API.G_EXC_ERROR THEN
12610     x_msg_data := FND_MSG_PUB.GET(p_msg_index => FND_MSG_PUB.G_LAST,
12611                                   p_encoded => 'F');
12612     x_return_status := FND_API.G_RET_STS_ERROR;
12613     x_where_clause := NULL;
12614   WHEN OTHERS THEN
12615     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
12616       FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
12617     END IF;
12618 
12619     IF (g_debug_unexp) THEN
12620       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
12621         FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
12622                      l_api_name || '.others_exception', 'EXCEPTION: Location is '
12623                      || l_progress || ' SQL CODE is '||sqlcode);
12624       END IF;
12625     END IF;
12626 
12627     x_msg_data := FND_MSG_PUB.GET(p_msg_index => FND_MSG_PUB.G_LAST,
12628                                   p_encoded => 'F');
12629     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12630     x_where_clause := NULL;
12631 
12632 END PO_SECURITY_CHECK;
12633 
12634 -- <FPJ Refactor Security API END>
12635 
12636 -- The following new procedures for status check added in DropShip FPJ project
12637 
12638 -------------------------------------------------------------------------------
12639 --Start of Comments
12640 --Name: check_updatable
12641 --Pre-reqs:
12642 --  None.
12643 --Modifies:
12644 --  None.
12645 --Locks:
12646 --  None.
12647 --Function:
12648 --  Finds if a PurchaseOrder/Release, or Line/Shipment is updatable based on status.
12649 --  A Header or Release has to be specified. Line/Shipment are optional.
12650 --  A Purchase Order/Release is updatable if
12651 --    it is not Pre Approved, not In Process, not canceled, not finally closed, not frozen.
12652 --  A Line or Shipment is updatable if it is not canceled, not finally closed.
12653 --Parameters:
12654 --IN:
12655 --p_count
12656 --  Specifies the number of entities in table IN parameters like p_header_id, p_release_id
12657 --  Other IN parameters are detailed in main procedure po_status_check
12658 --OUT:
12659 --x_return_status
12660 --  Indicates API return status as 'S', 'E' or 'U'.
12661 --x_po_status_rec
12662 --  Table x_po_status_rec.updateable_flag will be 'Y' or 'N' for each input entity
12663 --Notes:
12664 --  The implementation of updatable_flag involves a fake "update dual" statement to
12665 --    optimize performance.
12666 --End of Comments
12667 -------------------------------------------------------------------------------
12668 
12669 PROCEDURE check_updatable (
12670     p_count               IN NUMBER,
12671     p_header_id           IN PO_TBL_NUMBER,
12672     p_release_id          IN PO_TBL_NUMBER,
12673     p_document_type       IN PO_TBL_VARCHAR30,
12674     p_document_subtype    IN PO_TBL_VARCHAR30,
12675     p_document_num        IN PO_TBL_VARCHAR30,
12676     p_vendor_order_num    IN PO_TBL_VARCHAR30,
12677     p_line_id             IN PO_TBL_NUMBER,
12678     p_line_location_id    IN PO_TBL_NUMBER,
12679     p_distribution_id     IN PO_TBL_NUMBER,
12680     p_lock_flag           IN VARCHAR2 := 'N',
12681     p_calling_module      IN VARCHAR2 := NULL,  -- PDOI Rewrite R12
12682     p_role                IN VARCHAR2 := NULL,  -- PDOI Rewrite R12
12683     p_skip_cat_upload_chk IN VARCHAR2 := NULL,  -- PDOI Rewrite R12
12684     x_po_status_rec       IN OUT NOCOPY PO_STATUS_REC_TYPE,
12685     x_return_status       IN OUT NOCOPY VARCHAR2
12686 ) IS
12687 
12688 l_api_name       CONSTANT VARCHAR(30) := 'CHECK_UPDATABLE';
12689 l_progress       VARCHAR2(3) := '000';
12690 l_document_id    PO_HEADERS.po_header_id%TYPE;
12691 l_document_type  PO_DOCUMENT_TYPES.DOCUMENT_TYPE_CODE%TYPE;
12692 
12693 -- bug3592160 START
12694 l_header_id   PO_TBL_NUMBER;
12695 l_procedure_id PO_SESSION_GT.key%TYPE;
12696 -- bug3592160 END
12697 
12698 -- <PDOI Rewrite R12 START>
12699 l_role PO_DRAFTS.owner_role%TYPE := NVL(p_role, PO_GLOBAL.g_ROLE_BUYER);
12700 l_skip_cat_upload_chk VARCHAR2(1) := NVL(p_skip_cat_upload_chk, FND_API.G_FALSE);
12701 
12702 l_update_allowed VARCHAR2(1);
12703 l_locking_applicable VARCHAR2(1);
12704 l_unlock_required VARCHAR2(1);
12705 l_message VARCHAR2(30);
12706 -- <PDOI Rewrite R12 END>
12707 BEGIN
12708 
12709 IF g_debug_stmt THEN
12710     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
12711       FND_LOG.string(FND_LOG.LEVEL_STATEMENT, g_log_head || '.'||l_api_name||'.'
12712           || l_progress, 'Entering Procedure '||l_api_name);
12713     END IF;
12714 END IF;
12715 
12716 --To obtimize performance, Execute a fake "update dual" in BULK. The WHERE clause
12717 -- of the fake update statement checks if the current entity is updatable or not.
12718 -- One dual row updated <==> where clause is true <==> current entity is updatable.
12719 -- Later, Examine BULK_ROWCOUNT in a loop to determine updatable_flag
12720 l_progress := '010';
12721 
12722 -- bug3592160 START
12723 
12724 -- For some records, po_header_id needs to be derived (e.g. document_num is
12725 -- passed in instead of po_header_id). The following the procedure is to
12726 -- fill out po_header_ids, if missing
12727 complete_po_header_id_tbl
12728 ( p_count            => p_count,
12729   p_header_id        => p_header_id,
12730   p_release_id       => p_release_id,
12731   p_vendor_order_num => p_vendor_order_num,
12732   p_document_num     => p_document_num,
12733   p_type_lookup_code => p_document_subtype,
12734   x_header_id        => l_header_id
12735 );
12736 
12737 l_progress := '015';
12738 
12739 -- The original approach was to do a fake UPDATE on DUAL table. However, this
12740 -- is causing locking issue. Therefore, BULK INSERT is used instead of
12741 -- BULK UPDATE
12742 l_procedure_id := PO_CORE_S.get_session_gt_nextval;
12743 
12744 FORALL i IN 1..p_count
12745   INSERT INTO PO_SESSION_GT
12746   ( key,
12747     num1
12748   )
12749   SELECT l_procedure_id,
12750          1                 -- Dummy Value
12751   FROM DUAL
12752   WHERE  (p_release_id(i) IS NOT NULL
12753       --Case 1: No Release is specified, PO Header has to be specified
12754       --   Through any of HeaderId, DocNum-and-SubType, or VendorOrderNum
12755       OR EXISTS (select null from po_headers_all h
12756         WHERE h.po_header_id = l_header_id(i)
12757         AND (h.authorization_status is NULL
12758              OR h.authorization_status NOT IN ('PRE-APPROVED', 'IN PROCESS'))
12759         AND (h.cancel_flag is null or h.cancel_flag <> 'Y')
12760         AND (h.closed_code is NULL or h.closed_code NOT IN ('FINALLY CLOSED'))
12761         AND (h.frozen_flag is NULL or h.frozen_flag <> 'Y')))
12762     AND (p_release_id(i) IS NULL
12763       --Case 2: PO Release is specified
12764       OR EXISTS (select null from po_releases_all h
12765         WHERE h.po_release_id = p_release_id(i)
12766         AND (h.authorization_status is NULL
12767              OR h.authorization_status NOT IN ('PRE-APPROVED', 'IN PROCESS'))
12768         AND (h.cancel_flag is null or h.cancel_flag <> 'Y')
12769         AND (h.closed_code is NULL or h.closed_code NOT IN ('FINALLY CLOSED'))
12770         AND (h.frozen_flag is NULL or h.frozen_flag <> 'Y')))
12771     AND (p_line_id(i) IS NULL
12772       --Case 3: Optionally, Line is specified
12773       OR EXISTS (SELECT null from po_lines_all l
12774         WHERE l.po_line_id = p_line_id(i)
12775         AND (l.cancel_flag is null or l.cancel_flag <> 'Y')
12776         AND (l.closed_code is NULL or l.closed_code NOT IN ('FINALLY CLOSED'))))
12777     AND (p_line_location_id(i) IS NULL
12778       --Case 4: Optionally, Line Location is specified
12779       OR EXISTS (SELECT null from po_line_locations_all l
12780         WHERE l.line_location_id = p_line_location_id(i)
12781         AND (l.cancel_flag is null or l.cancel_flag <> 'Y')
12782         AND (l.closed_code is NULL or l.closed_code NOT IN ('FINALLY CLOSED'))))    ;
12783 -- bug3592160 END
12784 
12785 -- Allocate memory for updatable_flag Table to p_count size
12786 l_progress := '020';
12787 x_po_status_rec.updatable_flag := po_tbl_varchar1();
12788 x_po_status_rec.updatable_flag.extend(p_count);
12789 
12790 -- Set Updatable_flag for each Entity using BULK_ROWCOUNT
12791 l_progress := '030';
12792 FOR i IN 1..p_count LOOP
12793 
12794     IF SQL%BULK_ROWCOUNT(i) > 0 THEN
12795         -- Updateable Header/Line/Shipment found in the fake "update dual" stmt
12796         x_po_status_rec.updatable_flag(i) := 'Y';
12797 
12798         -- This document is updatable, lock the document if p_lock_flag=Y
12799         l_progress := '040';
12800         IF p_lock_flag = 'Y' THEN
12801             IF p_release_id(i) is not null THEN
12802                 l_document_id := p_release_id(i);
12803                 l_document_type := 'RELEASE';
12804             ELSE
12805                 l_document_id := p_header_id(i);
12806                 IF p_document_type(i) is null THEN
12807                     l_document_type := 'PO';
12808                 ELSE
12809                     l_document_type := p_document_type(i);
12810                 END IF;
12811             END IF;
12812             PO_DOCUMENT_LOCK_GRP.LOCK_DOCUMENT (
12813                 p_api_version => 1.0,
12814                 P_INIT_MSG_LIST => FND_API.G_FALSE,
12815                 P_DOCUMENT_TYPE => l_document_type,
12816                 P_DOCUMENT_ID => l_document_id,
12817                 x_return_status  => x_return_status);
12818 
12819             IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
12820                 return;
12821             END IF;
12822         END IF;
12823     ELSE
12824         x_po_status_rec.updatable_flag(i) := 'N';
12825     END IF; --END of IF SQL%BULK_ROWCOUNT(i) > 0
12826 
12827 END LOOP;
12828 
12829 -- bug3592160 START
12830 -- Remove everything that has been inserted into PO_SESSION_GT
12831 DELETE FROM po_session_gt
12832 WHERE key = l_procedure_id;
12833 -- bug3592160 END
12834 
12835 -- <PDOI Rewrite R12 START>
12836 -- For all the documents being checked, we also need to make sure that
12837 -- current draft status allows the document to be updated
12838 
12839 FOR i IN 1..p_count LOOP
12840   l_progress := 40;
12841   IF (x_po_status_rec.updatable_flag(i) = 'Y' AND
12842       p_release_id(i) IS NULL) THEN
12843 
12844     PO_DRAFTS_PVT.update_permission_check
12845     ( p_calling_module => p_calling_module,
12846       p_po_header_id => p_header_id(i),
12847       p_role => l_role,
12848       p_skip_cat_upload_chk => l_skip_cat_upload_chk,
12849       x_update_allowed => l_update_allowed,
12850       x_locking_applicable => l_locking_applicable,
12851       x_unlock_required => l_unlock_required,
12852       x_message => l_message
12853     );
12854 
12855     IF (l_update_allowed = FND_API.G_FALSE) THEN
12856       x_po_status_rec.updatable_flag(i) := 'N';
12857     END IF;
12858   END IF;
12859 END LOOP;
12860 
12861 -- <PDOI Rewrite R12 END>
12862 
12863 
12864 x_return_status := FND_API.G_RET_STS_SUCCESS;
12865 
12866 EXCEPTION
12867     WHEN FND_API.G_EXC_ERROR THEN
12868         x_return_status := FND_API.G_RET_STS_ERROR;
12869     WHEN OTHERS THEN
12870         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12871         FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name || '.' || l_progress);
12872 
12873 END check_updatable;
12874 
12875 -------------------------------------------------------------------------------
12876 --Start of Comments
12877 --Name: check_reservable
12878 --Pre-reqs:
12879 --  None.
12880 --Modifies:
12881 --  None.
12882 --Locks:
12883 --  None.
12884 --Function:
12885 --  Finds if a PurchaseOrder/Release, or Line/Shipment is reservable based on status.
12886 --  A Header or Release has to be specified. Line/Shipment are optional.
12887 --  A Purchase Order/Release Header/Line/Shipment is reservable if
12888 --    Authorization Status not APPROVED, AND Closed Code is CLOSED or OPEN,
12889 --    AND Frozen Flag is N, AND User Hold Flag is N
12890 --Parameters:
12891 --IN:
12892 --p_count
12893 --  Specifies the number of entities in table IN parameters like p_header_id, p_release_id
12894 --  Other IN parameters are detailed in main procedure po_status_check
12895 --OUT:
12896 --x_return_status
12897 --  Indicates API return status as 'S', 'E' or 'U'.
12898 --x_po_status_rec
12899 --  Table x_po_status_rec.updateable_flag will be 'Y' or 'N' for each input entity
12900 --Notes:
12901 --  The implementation of reservable_flag involves a fake "update dual" statement to
12902 --    optimize performance.
12903 --End of Comments
12904 -------------------------------------------------------------------------------
12905 
12906 PROCEDURE check_reservable (
12907     p_count               IN NUMBER,
12908     p_header_id           IN PO_TBL_NUMBER,
12909     p_release_id          IN PO_TBL_NUMBER,
12910     p_document_type       IN PO_TBL_VARCHAR30,
12911     p_document_subtype    IN PO_TBL_VARCHAR30,
12912     p_document_num        IN PO_TBL_VARCHAR30,
12913     p_vendor_order_num    IN PO_TBL_VARCHAR30,
12914     p_line_id             IN PO_TBL_NUMBER,
12915     p_line_location_id    IN PO_TBL_NUMBER,
12916     p_distribution_id     IN PO_TBL_NUMBER,
12917     p_lock_flag           IN VARCHAR2 := 'N',
12918     x_po_status_rec       IN OUT NOCOPY PO_STATUS_REC_TYPE,
12919     x_return_status       IN OUT NOCOPY VARCHAR2
12920 ) IS
12921 
12922 l_api_name       CONSTANT VARCHAR(30) := 'check_reservable';
12923 l_progress       VARCHAR2(3) := '000';
12924 l_document_id    PO_HEADERS.po_header_id%TYPE;
12925 l_document_type  PO_DOCUMENT_TYPES.DOCUMENT_TYPE_CODE%TYPE;
12926 
12927 -- bug3592160 START
12928 l_header_id   PO_TBL_NUMBER;
12929 l_procedure_id PO_SESSION_GT.key%TYPE;
12930 -- bug3592160 END
12931 
12932 BEGIN
12933 
12934 IF g_debug_stmt THEN
12935     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
12936       FND_LOG.string(FND_LOG.LEVEL_STATEMENT, g_log_head || '.'||l_api_name||'.'
12937           || l_progress, 'Entering Procedure '||l_api_name);
12938     END IF;
12939 END IF;
12940 
12941 --To obtimize performance, Execute a fake "update dual" in BULK. The WHERE clause
12942 -- of the fake update statement checks if the current entity is reservable or not.
12943 -- One dual row updated <==> where clause is true <==> current entity is reservable.
12944 -- Later, Examine BULK_ROWCOUNT in a loop to determine reservable_flag
12945 l_progress := '010';
12946 
12947 -- bug3592160 START
12948 
12949 -- For some records, po_header_id needs to be derived (e.g. document_num is
12950 -- passed in instead of po_header_id). The following the procedure is to
12951 -- fill out po_header_ids, if missing
12952 complete_po_header_id_tbl
12953 ( p_count            => p_count,
12954   p_header_id        => p_header_id,
12955   p_release_id       => p_release_id,
12956   p_vendor_order_num => p_vendor_order_num,
12957   p_document_num     => p_document_num,
12958   p_type_lookup_code => p_document_subtype,
12959   x_header_id        => l_header_id
12960 );
12961 
12962 l_progress := '015';
12963 
12964 
12965 -- The original approach was to do a fake UPDATE on DUAL table. However, this
12966 -- is causing locking issue. Therefore, BULK INSERT is used instead of
12967 -- BULK UPDATE
12968 l_procedure_id := PO_CORE_S.get_session_gt_nextval;
12969 
12970 FORALL i IN 1..p_count
12971   INSERT INTO PO_SESSION_GT
12972   ( key,
12973     num1
12974   )
12975   SELECT l_procedure_id,
12976          1                 -- Dummy Value
12977   FROM DUAL
12978   WHERE (p_release_id(i) IS NOT NULL
12979       --Case 1: No Release is specified, PO Header has to be specified
12980       --   Through any of HeaderId, DocNum-and-SubType, or VendorOrderNum
12981       -- Bug 3407980: Modified closed_code condition so that it only discards 'FINALLY CLOSED'
12982       OR EXISTS (select null from po_headers_all h
12983         WHERE h.po_header_id = l_header_id(i)
12984         AND (h.authorization_status is NULL
12985              OR h.authorization_status NOT IN ('APPROVED'))
12986         AND (h.closed_code is NULL or h.closed_code <> 'FINALLY CLOSED')
12987         AND (h.frozen_flag is NULL or h.frozen_flag <> 'Y')
12988         AND (h.user_hold_flag is NULL or h.user_hold_flag <> 'Y')))
12989     AND (p_release_id(i) IS NULL
12990       --Case 2: PO Release is specified
12991       -- Bug 3407980: Modified closed_code condition so that it only discards 'FINALLY CLOSED'
12992       OR EXISTS (select null from po_releases_all h
12993         WHERE h.po_release_id = p_release_id(i)
12994         AND (h.authorization_status is NULL
12995              OR h.authorization_status NOT IN ('APPROVED'))
12996         AND (h.closed_code is NULL or h.closed_code <>  'FINALLY CLOSED')
12997         AND (h.frozen_flag is NULL or h.frozen_flag <> 'Y')
12998         AND (h.hold_flag is NULL or h.hold_flag <> 'Y')))
12999     AND (p_line_id(i) IS NULL
13000       --Case 3: Optionally, Line is specified
13001       -- Bug 3407980: Modified closed_code condition so that it only discards 'FINALLY CLOSED'
13002       OR EXISTS (SELECT null from po_lines_all l
13003         WHERE l.po_line_id = p_line_id(i)
13004         AND (l.closed_code is NULL or l.closed_code <> 'FINALLY CLOSED')))
13005     AND (p_line_location_id(i) IS NULL
13006       --Case 4: Optionally, Line Location is specified
13007       -- Bug 3407980: Modified closed_code condition so that it only discards 'FINALLY CLOSED'
13008       OR EXISTS (SELECT null from po_line_locations_all l
13009         WHERE l.line_location_id = p_line_location_id(i)
13010         AND (l.closed_code is NULL or l.closed_code <> 'FINALLY CLOSED')))
13011     ;
13012 
13013 -- Allocate memory for reservable_flag Table to p_count size
13014 l_progress := '020';
13015 x_po_status_rec.reservable_flag := po_tbl_varchar1();
13016 x_po_status_rec.reservable_flag.extend(p_count);
13017 
13018 -- Set reservable_flag for each Entity using BULK_ROWCOUNT
13019 l_progress := '030';
13020 FOR i IN 1..p_count LOOP
13021 
13022     IF SQL%BULK_ROWCOUNT(i) > 0 THEN
13023         -- Reservable Header/Line/Shipment found in the fake "update dual" stmt
13024         x_po_status_rec.reservable_flag(i) := 'Y';
13025     ELSE
13026         x_po_status_rec.reservable_flag(i) := 'N';
13027     END IF; --END of IF SQL%BULK_ROWCOUNT(i) > 0
13028 
13029 END LOOP;
13030 
13031 -- bug3592160 START
13032 -- Remove everything that has been inserted into PO_SESSION_GT
13033 DELETE FROM po_session_gt
13034 WHERE key = l_procedure_id;
13035 -- bug3592160 END
13036 
13037 x_return_status := FND_API.G_RET_STS_SUCCESS;
13038 
13039 EXCEPTION
13040     WHEN FND_API.G_EXC_ERROR THEN
13041         x_return_status := FND_API.G_RET_STS_ERROR;
13042     WHEN OTHERS THEN
13043         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13044         FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name || '.' || l_progress);
13045 
13046 END check_reservable;
13047 
13048 -------------------------------------------------------------------------------
13049 --Start of Comments
13050 --Name: check_unreservable
13051 --Pre-reqs:
13052 --  None.
13053 --Modifies:
13054 --  None.
13055 --Locks:
13056 --  None.
13057 --Function:
13058 --  Finds if a PurchaseOrder/Release, or Line/Shipment is unreservable based on status.
13059 --  A Header or Release has to be specified. Line/Shipment are optional.
13060 --  A Purchase Order/Release Header/Line/Shipment is unreservable if
13061 --    Any Authorization Status, Closed Code is CLOSED or OPEN,
13062 --    AND Frozen Flag is N, AND User Hold Flag is N
13063 --Parameters:
13064 --IN:
13065 --p_count
13066 --  Specifies the number of entities in table IN parameters like p_header_id, p_release_id
13067 --  Other IN parameters are detailed in main procedure po_status_check
13068 --OUT:
13069 --x_return_status
13070 --  Indicates API return status as 'S', 'E' or 'U'.
13071 --x_po_status_rec
13072 --  Table x_po_status_rec.updateable_flag will be 'Y' or 'N' for each input entity
13073 --Notes:
13074 --  The implementation of unreservable_flag involves a fake "update dual" statement to
13075 --    optimize performance.
13076 --End of Comments
13077 -------------------------------------------------------------------------------
13078 
13079 PROCEDURE check_unreservable (
13080     p_count               IN NUMBER,
13081     p_header_id           IN PO_TBL_NUMBER,
13082     p_release_id          IN PO_TBL_NUMBER,
13083     p_document_type       IN PO_TBL_VARCHAR30,
13084     p_document_subtype    IN PO_TBL_VARCHAR30,
13085     p_document_num        IN PO_TBL_VARCHAR30,
13086     p_vendor_order_num    IN PO_TBL_VARCHAR30,
13087     p_line_id             IN PO_TBL_NUMBER,
13088     p_line_location_id    IN PO_TBL_NUMBER,
13089     p_distribution_id     IN PO_TBL_NUMBER,
13090     p_lock_flag           IN VARCHAR2 := 'N',
13091     x_po_status_rec       IN OUT NOCOPY PO_STATUS_REC_TYPE,
13092     x_return_status       IN OUT NOCOPY VARCHAR2
13093 ) IS
13094 
13095 l_api_name       CONSTANT VARCHAR(30) := 'check_unreservable';
13096 l_progress       VARCHAR2(3) := '000';
13097 l_document_id    PO_HEADERS.po_header_id%TYPE;
13098 l_document_type  PO_DOCUMENT_TYPES.DOCUMENT_TYPE_CODE%TYPE;
13099 
13100 -- bug3592160 START
13101 l_header_id   PO_TBL_NUMBER;
13102 l_procedure_id PO_SESSION_GT.key%TYPE;
13103 -- bug3592160 END
13104 
13105 BEGIN
13106 
13107 IF g_debug_stmt THEN
13108     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
13109       FND_LOG.string(FND_LOG.LEVEL_STATEMENT, g_log_head || '.'||l_api_name||'.'
13110           || l_progress, 'Entering Procedure '||l_api_name);
13111     END IF;
13112 END IF;
13113 
13114 --To obtimize performance, Execute a fake "update dual" in BULK. The WHERE clause
13115 -- of the fake update statement checks if the current entity is unreservable or not.
13116 -- One dual row updated <==> where clause is true <==> current entity is unreservable.
13117 -- Later, Examine BULK_ROWCOUNT in a loop to determine unreservable_flag
13118 l_progress := '010';
13119 
13120 -- bug3592160 START
13121 
13122 -- For some records, po_header_id needs to be derived (e.g. document_num is
13123 -- passed in instead of po_header_id). The following the procedure is to
13124 -- fill out po_header_ids, if missing
13125 complete_po_header_id_tbl
13126 ( p_count            => p_count,
13127   p_header_id        => p_header_id,
13128   p_release_id       => p_release_id,
13129   p_vendor_order_num => p_vendor_order_num,
13130   p_document_num     => p_document_num,
13131   p_type_lookup_code => p_document_subtype,
13132   x_header_id        => l_header_id
13133 );
13134 
13135 l_progress := '015';
13136 
13137 -- SQL What: Checks if current PO Header/Line/Shipment is in unreservable status
13138 -- The original approach was to do a fake UPDATE on DUAL table. However, this
13139 -- is causing locking issue. Therefore, BULK INSERT is used instead of
13140 -- BULK UPDATE
13141 l_procedure_id := PO_CORE_S.get_session_gt_nextval;
13142 
13143 FORALL i IN 1..p_count
13144   INSERT INTO PO_SESSION_GT
13145   ( key,
13146     num1
13147   )
13148   SELECT l_procedure_id,
13149          1                 -- Dummy Value
13150   FROM DUAL
13151   WHERE (p_release_id(i) IS NOT NULL
13152       --Case 1: No Release is specified, PO Header has to be specified
13153       --   Through any of HeaderId, DocNum-and-SubType, or VendorOrderNum
13154       -- Bug 3407980: Modified closed_code condition so that it only discards 'FINALLY CLOSED'
13155       OR EXISTS (select null from po_headers_all h
13156         WHERE h.po_header_id = l_header_id(i)
13157         AND (h.closed_code is NULL or h.closed_code <> 'FINALLY CLOSED')
13158         AND (h.frozen_flag is NULL or h.frozen_flag <> 'Y')
13159         AND (h.user_hold_flag is NULL or h.user_hold_flag <> 'Y')))
13160     AND (p_release_id(i) IS NULL
13161       --Case 2: PO Release is specified
13162       -- Bug 3407980: Modified closed_code condition so that it only discards 'FINALLY CLOSED'
13163       OR EXISTS (select null from po_releases_all h
13164         WHERE h.po_release_id = p_release_id(i)
13165         AND (h.closed_code is NULL or h.closed_code <> 'FINALLY CLOSED')
13166         AND (h.frozen_flag is NULL or h.frozen_flag <> 'Y')
13167         AND (h.hold_flag is NULL or h.hold_flag <> 'Y')))
13168     AND (p_line_id(i) IS NULL
13169       --Case 3: Optionally, Line is specified
13170       -- Bug 3407980: Modified closed_code condition so that it only discards 'FINALLY CLOSED'
13171       OR EXISTS (SELECT null from po_lines_all l
13172         WHERE l.po_line_id = p_line_id(i)
13173         AND (l.closed_code is NULL or l.closed_code <> 'FINALLY CLOSED')))
13174     AND (p_line_location_id(i) IS NULL
13175       --Case 4: Optionally, Line Location is specified
13176       -- Bug 3407980: Modified closed_code condition so that it only discards 'FINALLY CLOSED'
13177       OR EXISTS (SELECT null from po_line_locations_all l
13178         WHERE l.line_location_id = p_line_location_id(i)
13179         AND (l.closed_code is NULL or l.closed_code <> 'FINALLY CLOSED')))
13180     ;
13181 
13182 -- Allocate memory for unreservable_flag Table to p_count size
13183 l_progress := '020';
13184 x_po_status_rec.unreservable_flag := po_tbl_varchar1();
13185 x_po_status_rec.unreservable_flag.extend(p_count);
13186 
13187 -- Set unreservable_flag for each Entity using BULK_ROWCOUNT
13188 l_progress := '030';
13189 FOR i IN 1..p_count LOOP
13190 
13191     IF SQL%BULK_ROWCOUNT(i) > 0 THEN
13192         -- Unreservable Header/Line/Shipment found in the fake "update dual" stmt
13193         x_po_status_rec.unreservable_flag(i) := 'Y';
13194     ELSE
13195         x_po_status_rec.unreservable_flag(i) := 'N';
13196     END IF; --END of IF SQL%BULK_ROWCOUNT(i) > 0
13197 
13198 END LOOP;
13199 
13200 -- bug3592160 START
13201 -- Remove everything that has been inserted into PO_SESSION_GT
13202 DELETE FROM po_session_gt
13203 WHERE key = l_procedure_id;
13204 -- bug3592160 END
13205 
13206 x_return_status := FND_API.G_RET_STS_SUCCESS;
13207 
13208 EXCEPTION
13209     WHEN FND_API.G_EXC_ERROR THEN
13210         x_return_status := FND_API.G_RET_STS_ERROR;
13211     WHEN OTHERS THEN
13212         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13213         FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name || '.' || l_progress);
13214 
13215 END check_unreservable;
13216 
13217 -------------------------------------------------------------------------------
13218 --Start of Comments
13219 --Name: get_status
13220 --Pre-reqs:
13221 --  None.
13222 --Modifies:
13223 --  None.
13224 --Locks:
13225 --  None.
13226 --Function:
13227 --  Helper to po_status_check to find status of a Purchase Order/Release/Line/Shipment
13228 --  The following status fields of PO Header or Release are put into
13229 --     the OUT parameter x_po_status_rec
13230 --   AUTHORIZATION_STATUS, APPROVED_FLAG, CLOSED_CODE, CANCEL_FLAG, FROZEN_FLAG, HOLD_FLAG
13231 --  When an optional Line specified, following Line level values are overwritten
13232 --   CLOSED_CODE, CANCEL_FLAG, HOLD_FLAG
13233 --  When an optional Shipment specified, following Shipment level values are overwritten
13234 --   APPROVED_FLAG, CLOSED_CODE, CANCEL_FLAG
13235 --Parameters:
13236 --IN:
13237 --p_count
13238 --  Specifies the number of entities in table IN parameters like p_header_id, p_release_id
13239 --    All the table IN parameters are assumed to be of the same size
13240 --  Other IN parameters are detailed in main procedure po_status_check
13241 --OUT:
13242 --x_return_status
13243 --  Indicates API return status as 'S', 'E' or 'U'.
13244 --x_po_status_rec
13245 --  The various status fields would have the PO/Rel Line/Shipment status values
13246 --End of Comments
13247 -------------------------------------------------------------------------------
13248 
13249 PROCEDURE get_status (
13250     p_count               IN NUMBER,
13251     p_header_id           IN PO_TBL_NUMBER,
13252     p_release_id          IN PO_TBL_NUMBER,
13253     p_document_type       IN PO_TBL_VARCHAR30,
13254     p_document_subtype    IN PO_TBL_VARCHAR30,
13255     p_document_num        IN PO_TBL_VARCHAR30,
13256     p_vendor_order_num    IN PO_TBL_VARCHAR30,
13257     p_line_id             IN PO_TBL_NUMBER,
13258     p_line_location_id    IN PO_TBL_NUMBER,
13259     p_distribution_id     IN PO_TBL_NUMBER,
13260     x_po_status_rec       IN OUT NOCOPY PO_STATUS_REC_TYPE,
13261     x_return_status       IN OUT NOCOPY VARCHAR2
13262 ) IS
13263 
13264 l_api_name    CONSTANT VARCHAR(30) := 'GET_STATUS';
13265 l_progress    VARCHAR2(3) := '000';
13266 l_sequence    PO_TBL_NUMBER := PO_TBL_NUMBER();
13267 
13268 BEGIN
13269 
13270 IF g_debug_stmt THEN
13271     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
13272       FND_LOG.string(FND_LOG.LEVEL_STATEMENT, g_log_head || '.'||l_api_name||'.'
13273           || l_progress, 'Entering Procedure '||l_api_name);
13274     END IF;
13275 END IF;
13276 
13277 --Use sequence(i) to simulate i inside FORALL as direct reference to i is not allowed
13278 --Initialize sequence array to contain 1,2,3, ..., p_count
13279 l_progress := '010';
13280 l_sequence.extend(p_count);
13281 FOR i IN 1..p_count LOOP
13282   l_sequence(i) := i;
13283 END LOOP;
13284 
13285 l_progress := '020';
13286 
13287 delete from po_headers_gt;
13288 
13289 -- For all the entities with null p_release_id(i), get Header status fields into
13290 -- global temprary table while storing sequence into po_headers_gt.PO_HEADER_ID column
13291 l_progress := '030';
13292 -- bug 4931241 modified the sql query to avoid FTS on PO_HEADERS_ALL
13293 -- Removed the where clause for document num and vendor_order_num
13294 -- They do not uniquely identify the document. Added validation in group package
13295 FORALL i IN 1..p_count
13296     INSERT
13297       INTO po_headers_gt
13298       ( AGENT_ID, TYPE_LOOKUP_CODE, LAST_UPDATE_DATE, LAST_UPDATED_BY,
13299         SEGMENT1, SUMMARY_FLAG, ENABLED_FLAG,
13300         authorization_status, approved_flag,
13301         closed_code, cancel_flag,
13302         frozen_flag, user_hold_flag, PO_HEADER_ID)
13303     SELECT
13304       AGENT_ID, TYPE_LOOKUP_CODE, LAST_UPDATE_DATE, LAST_UPDATED_BY,
13305       SEGMENT1, SUMMARY_FLAG, ENABLED_FLAG,
13306       NVL(authorization_status, 'INCOMPLETE'), nvl(approved_flag, 'N'),
13307       nvl(closed_code, 'OPEN'), NVL(cancel_flag, 'N'),
13308       NVL(frozen_flag, 'N'), NVL(user_hold_flag, 'N'), l_sequence(i)
13309       FROM po_headers_all h
13310       WHERE p_release_id(i) is null
13311         AND h.po_header_id = p_header_id(i)
13312     ;
13313 
13314 -- For all the entities with non-null p_release_id(i), get Relase status fields into
13315 -- global temprary table while storing sequence into po_headers_gt.PO_HEADER_ID column
13316 l_progress := '035';
13317 FORALL i IN 1..p_count
13318     INSERT
13319       INTO po_headers_gt
13320       ( AGENT_ID, TYPE_LOOKUP_CODE, LAST_UPDATE_DATE, LAST_UPDATED_BY,
13321         SEGMENT1, SUMMARY_FLAG, ENABLED_FLAG,
13322         authorization_status, approved_flag,
13323         closed_code, cancel_flag,
13324         frozen_flag, user_hold_flag, PO_HEADER_ID)
13325     SELECT
13326       0 dummy, ' ' dummy, LAST_UPDATE_DATE, LAST_UPDATED_BY,
13327       ' ' dummy, ' ' dummy, ' ' dummy,
13328       NVL(authorization_status, 'INCOMPLETE'), nvl(approved_flag, 'N'),
13329       nvl(closed_code, 'OPEN'), NVL(cancel_flag, 'N'),
13330       NVL(frozen_flag, 'N'), NVL(hold_flag, 'N'), l_sequence(i)
13331       FROM po_releases_all h
13332       WHERE h.po_release_id = p_release_id(i)
13333     ;
13334 
13335 --IF line ID present at an index, overwrite the status fields with Line Level status
13336 l_progress := '040';
13337 FORALL i IN 1..p_count
13338     UPDATE po_headers_gt gt
13339       SET (closed_code, cancel_flag, user_hold_flag)
13340       =
13341       (SELECT nvl(closed_code, 'OPEN'), NVL(cancel_flag, 'N'), NVL(user_hold_flag, 'N')
13342       FROM po_lines_all s
13343       WHERE s.po_line_id = p_line_id(i))
13344     WHERE p_line_id(i) is not null and gt.po_header_id = l_sequence(i)
13345     ;
13346 
13347 --IF line location present at an index, overwrite status fields with Shipment Level status
13348 l_progress := '050';
13349 FORALL i IN 1..p_count
13350     UPDATE po_headers_gt gt
13351       SET (approved_flag, closed_code, cancel_flag)
13352       =
13353       (SELECT nvl(approved_flag, 'N'), nvl(closed_code, 'OPEN'), NVL(cancel_flag, 'N')
13354       FROM po_line_locations_all s
13355       WHERE s.line_location_id = p_line_location_id(i))
13356     WHERE p_line_location_id(i) is not null and gt.po_header_id = l_sequence(i)
13357     ;
13358 
13359 -- Fetch status fields from global temporary table into pl/sql table.
13360 -- Order by sequence (stored in PO_HEADER_ID column) ensures
13361 --   that input tables like p_header_id are in sync with
13362 --   output status field tables like x_po_status_rec.authorization_status
13363 l_progress := '060';
13364 SELECT
13365   authorization_status, approved_flag, closed_code, cancel_flag, frozen_flag, user_hold_flag
13366 BULK COLLECT INTO
13367   x_po_status_rec.authorization_status, x_po_status_rec.approval_flag, x_po_status_rec.closed_code,
13368   x_po_status_rec.cancel_flag, x_po_status_rec.frozen_flag, x_po_status_rec.hold_flag
13369 FROM po_headers_gt
13370 ORDER BY PO_HEADER_ID;
13371 
13372 x_return_status := FND_API.G_RET_STS_SUCCESS;
13373 
13374 EXCEPTION
13375     WHEN FND_API.G_EXC_ERROR THEN
13376         x_return_status := FND_API.G_RET_STS_ERROR;
13377     WHEN OTHERS THEN
13378         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13379         FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name || '.' || l_progress);
13380 
13381 END get_status;
13382 
13383 -------------------------------------------------------------------------------
13384 --Start of Comments
13385 --Name: po_status_check
13386 --Pre-reqs:
13387 --  None.
13388 --Modifies:
13389 --  None.
13390 --Locks:
13391 --  None.
13392 --Function:
13393 --  Finds the status of a Purchase Order or a Release. Refer to p_mode parameter
13394 --  and PO_STATUS_REC_TYPE for various status information this procedure can find out.
13395 --  A unique header OR Release has to be specified through various input combinations.
13396 --  A line or shipment can optionally be specified to check status at that level also.
13397 --Parameters:
13398 --IN:
13399 --p_api_version
13400 --  Specifies API version.
13401 --p_header_id
13402 --  Specifies Purchase Order Header ID. This is ignored if p_release_id is not NULL
13403 --p_release_id
13404 --  Specifies Purchase Order Release ID.
13405 --p_document_type
13406 --  Specifies the document type: PA, PO, or RELEASE
13407 --p_document_subtype
13408 --  Specifies the document subtype.
13409 --p_document_num
13410 --  Document Number and Document Type together identify a unique document
13411 --p_vendor_order_num
13412 --  Document Vendor Order Number uniquely identifies a document
13413 --p_line_id
13414 --  Optionally Specifies Purchase Order Line ID to check status at line level
13415 --p_line_location_id := NULL
13416 --  Optionally Specifies Purchase Order Shipment ID to check status at shipment level
13417 --p_distribution_id := NULL
13418 --  Specifies Purchase Order Distribution ID, currently not used. May be used in future.
13419 --p_mode
13420 --  Indicates what status to check.
13421 --    Can contain one or more of the following requests to check status
13422 --      CHECK_UPDATEABLE to check if the current PO Header/Line/Shipment is updatable
13423 --      GET_STATUS to return various statuses of the current PO Header/Release
13424 --p_calling_module
13425 --  To be used by updatalbe chk only.
13426 --  String specifying where this API is calling from
13427 --p_role
13428 --  To be used by updatalbe chk only.
13429 --  Role of the suer calling this API. (BUYER, SUPPLIER, CAT ADMIN, etc.)
13430 --p_skip_cat_upload_chk
13431 --  To be used by updatalbe chk only.
13432 --  FND_API.G_TRUE if catalog upload status check should not be performed
13433 --  FND_API.G_FALSE otherwise
13434 --OUT:
13435 --x_return_status
13436 --  Indicates API return status as 'S', 'E' or 'U'.
13437 --x_po_status_rec
13438 --  Contains the returned status elements
13439 --  If p_mode contains CHECK_UPDATEABLE,
13440 --    the updateable_flag would have 'Y' or 'N' for each entity in the Table
13441 --  If p_mode contains GET_APPROVAL_STATUS,
13442 --    the various status fields Header/Release/Line/Shipment status values
13443 --Testing:
13444 --  All the input table parameters should have the exact same length.
13445 --    They may have null values at some indexes, but need to identify an entity uniquely
13446 --  Call the API when only Requisition Exist, PO/Release Exist
13447 --    and for all the combinations of attributes.
13448 --End of Comments
13449 -------------------------------------------------------------------------------
13450 
13451 PROCEDURE po_status_check (
13452     p_api_version         IN NUMBER,
13453     p_header_id           IN PO_TBL_NUMBER,
13454     p_release_id          IN PO_TBL_NUMBER,
13455     p_document_type       IN PO_TBL_VARCHAR30,
13456     p_document_subtype    IN PO_TBL_VARCHAR30,
13457     p_document_num        IN PO_TBL_VARCHAR30,
13458     p_vendor_order_num    IN PO_TBL_VARCHAR30,
13459     p_line_id             IN PO_TBL_NUMBER,
13460     p_line_location_id    IN PO_TBL_NUMBER,
13461     p_distribution_id     IN PO_TBL_NUMBER,
13462     p_mode                IN VARCHAR2,
13463     p_lock_flag           IN VARCHAR2 := 'N',
13464     p_calling_module      IN VARCHAR2 := NULL,  -- PDOI Rewrite R12
13465     p_role                IN VARCHAR2 := NULL,  -- PDOI Rewrite R12
13466     p_skip_cat_upload_chk IN VARCHAR2 := NULL,  -- PDOI Rewrite R12
13467     x_po_status_rec       OUT NOCOPY PO_STATUS_REC_TYPE,
13468     x_return_status       OUT NOCOPY VARCHAR2
13469 ) IS
13470 
13471 l_api_name    CONSTANT VARCHAR(30) := 'PO_STATUS_CHECK';
13472 l_api_version CONSTANT NUMBER := 1.0;
13473 l_progress    VARCHAR2(3) := '000';
13474 l_count       NUMBER;
13475 
13476 BEGIN
13477 
13478 IF g_debug_stmt THEN
13479     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
13480       FND_LOG.string(FND_LOG.LEVEL_STATEMENT, g_log_head || '.'||l_api_name||'.'
13481           || l_progress, 'Entering Procedure '||l_api_name);
13482     END IF;
13483 END IF;
13484 
13485 -- Standard call to check for call compatibility
13486 l_progress := '010';
13487 IF NOT FND_API.Compatible_API_Call(l_api_version, p_api_version, l_api_name, G_PKG_NAME) THEN
13488     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13489 END IF;
13490 
13491 l_progress := '020'; -- Initialize Local/Output Variables
13492 l_count := p_header_id.count;
13493 x_po_status_rec := PO_STATUS_REC_TYPE(null, null, null, null, null, null, null, null, null);
13494 
13495 l_progress := '030';
13496 
13497 IF INSTR(p_mode, G_CHECK_UPDATEABLE) > 0 THEN --{
13498 
13499     check_updatable (
13500         p_count => l_count,
13501         p_header_id => p_header_id,
13502         p_release_id => p_release_id,
13503         p_document_type => p_document_type,
13504         p_document_subtype => p_document_subtype,
13505         p_document_num => p_document_num,
13506         p_vendor_order_num => p_vendor_order_num,
13507         p_line_id => p_line_id,
13508         p_line_location_id => p_line_location_id,
13509         p_distribution_id => p_distribution_id,
13510         p_lock_flag => p_lock_flag,
13511         p_calling_module   => p_calling_module,          -- PDOI Rewrite R12
13512         p_role             => p_role,                    -- PDOI Rewrite R12
13513         p_skip_cat_upload_chk => p_skip_cat_upload_chk,  -- PDOI Rewrite R12
13514         x_po_status_rec => x_po_status_rec,
13515         x_return_status  => x_return_status);
13516 
13517     IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
13518         return;
13519     END IF;
13520 
13521 END IF; --}END of IF INSTR(p_mode, G_CHECK_UPDATEABLE) > 0
13522 
13523 l_progress := '033';
13524 
13525 IF INSTR(p_mode, G_CHECK_RESERVABLE) > 0 THEN --{
13526 
13527     check_reservable (
13528         p_count => l_count,
13529         p_header_id => p_header_id,
13530         p_release_id => p_release_id,
13531         p_document_type => p_document_type,
13532         p_document_subtype => p_document_subtype,
13533         p_document_num => p_document_num,
13534         p_vendor_order_num => p_vendor_order_num,
13535         p_line_id => p_line_id,
13536         p_line_location_id => p_line_location_id,
13537         p_distribution_id => p_distribution_id,
13538         p_lock_flag => p_lock_flag,
13539         x_po_status_rec => x_po_status_rec,
13540         x_return_status  => x_return_status);
13541 
13542     IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
13543         return;
13544     END IF;
13545 
13546 END IF; --}END of IF INSTR(p_mode, G_CHECK_RESERVABLE) > 0
13547 
13548 l_progress := '036';
13549 
13550 IF INSTR(p_mode, G_CHECK_UNRESERVABLE) > 0 THEN --{
13551 
13552     check_unreservable (
13553         p_count => l_count,
13554         p_header_id => p_header_id,
13555         p_release_id => p_release_id,
13556         p_document_type => p_document_type,
13557         p_document_subtype => p_document_subtype,
13558         p_document_num => p_document_num,
13559         p_vendor_order_num => p_vendor_order_num,
13560         p_line_id => p_line_id,
13561         p_line_location_id => p_line_location_id,
13562         p_distribution_id => p_distribution_id,
13563         p_lock_flag => p_lock_flag,
13564         x_po_status_rec => x_po_status_rec,
13565         x_return_status  => x_return_status);
13566 
13567     IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
13568         return;
13569     END IF;
13570 
13571 END IF; --}END of IF INSTR(p_mode, G_CHECK_UNRESERVABLE) > 0
13572 
13573 l_progress := '040';
13574 
13575 IF INSTR(p_mode, G_GET_STATUS) > 0 THEN --{ Get Header/Release status fields
13576 
13577     get_status (
13578         p_count => l_count,
13579         p_header_id => p_header_id,
13580         p_release_id => p_release_id,
13581         p_document_type => p_document_type,
13582         p_document_subtype => p_document_subtype,
13583         p_document_num => p_document_num,
13584         p_vendor_order_num => p_vendor_order_num,
13585         p_line_id => p_line_id,
13586         p_line_location_id => p_line_location_id,
13587         p_distribution_id => p_distribution_id,
13588         x_po_status_rec => x_po_status_rec,
13589         x_return_status  => x_return_status);
13590 
13591     IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
13592         return;
13593     END IF;
13594 
13595 END IF; --}END of IF INSTR(p_mode, G_GET_STATUS) > 0
13596 
13597 l_progress := '050';
13598 
13599 IF x_return_status is null THEN -- no valid check status request specified
13600     FND_MESSAGE.set_name('PO', 'PO_STATCHK_GENERAL_ERROR');
13601     FND_MESSAGE.set_token('ERROR_TEXT', 'No Valid p_mode specified !');
13602     FND_MSG_PUB.Add;
13603     RAISE FND_API.G_EXC_ERROR;
13604 END IF;
13605 
13606 EXCEPTION
13607     WHEN FND_API.G_EXC_ERROR THEN
13608         x_return_status := FND_API.G_RET_STS_ERROR;
13609     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
13610         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13611     WHEN OTHERS THEN
13612         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13613         FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name || '.' || l_progress);
13614 
13615 END po_status_check;
13616 
13617 
13618 -------------------------------------------------------------------------------
13619 --Start of Comments
13620 --Name: check_unreserve
13621 --Pre-reqs:
13622 --  The global temp tables are populated with exactly those rows
13623 --  that should undergo the checks.
13624 --Modifies:
13625 --  PO_ONLINE_REPORT_TEXT_GT
13626 --Locks:
13627 --  None.
13628 --Function:
13629 --  This procedure performs the document checks for an UNRESERVE action.
13630 --Parameters:
13631 --IN:
13632 --p_online_report_id
13633 --  ID used to insert into PO_ONLINE_REPORT_TEXT_GT.
13634 --p_user_id
13635 --  User performing the action.
13636 --p_login_id
13637 --  Last update login_id.
13638 --IN OUT:
13639 --p_sequence
13640 --  Contains the running count of error messages inserted.
13641 --Testing:
13642 --
13643 --End of Comments
13644 -------------------------------------------------------------------------------
13645 PROCEDURE check_unreserve(
13646    p_online_report_id               IN             NUMBER
13647 ,  p_document_type                  IN             VARCHAR2 --Bug#5462677
13648 ,  p_document_subtype               IN             VARCHAR2 --Bug#5462677
13649 ,  p_document_level                 IN             VARCHAR2 --Bug#5462677
13650 ,  p_doc_level_id                   IN             NUMBER   --Bug#5462677
13651 ,  p_user_id                        IN             NUMBER
13652 ,  p_login_id                       IN             NUMBER
13653 ,  p_sequence                       IN OUT NOCOPY  NUMBER
13654 )
13655 IS
13656 
13657 l_log_head     CONSTANT VARCHAR2(100) := g_log_head||'CHECK_UNRESERVE';
13658 l_progress     VARCHAR2(3) := '000';
13659 
13660 l_textline  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE := NULL;
13661 l_ret_sts VARCHAR2(1);
13662 BEGIN
13663 
13664 IF g_debug_stmt THEN
13665    PO_DEBUG.debug_begin(l_log_head);
13666    PO_DEBUG.debug_var(l_log_head,l_progress,'p_online_report_id',p_online_report_id);
13667    PO_DEBUG.debug_var(l_log_head,l_progress,'p_user_id',p_user_id);
13668    PO_DEBUG.debug_var(l_log_head,l_progress,'p_login_id',p_login_id);
13669    PO_DEBUG.debug_var(l_log_head,l_progress,'p_sequence',p_sequence);
13670    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level_id',p_doc_level_id);
13671    PO_DEBUG.debug_var(l_log_head,l_progress,'p_document_type',p_document_type);
13672 END IF;
13673 
13674 l_progress := '010';
13675 
13676 IF g_debug_stmt THEN
13677    PO_DEBUG.debug_stmt(l_log_head,l_progress,
13678       'UNRESERVE 1: Ship Qty billed greater than qty ordered');
13679 END IF;
13680 
13681 -- Check 1: Unreserve action not allowed if shipment quantity/amount billed is
13682 -- greater than quantity/amount ordered
13683 -- Bug# 1286701
13684 
13685 l_progress := '040';
13686 
13687 -- Perform the check.
13688 
13689 INSERT INTO PO_ONLINE_REPORT_TEXT_GT
13690 (  online_report_id
13691 ,  last_update_login
13692 ,  last_updated_by
13693 ,  last_update_date
13694 ,  created_by
13695 ,  creation_date
13696 ,  line_num
13697 ,  shipment_num
13698 ,  distribution_num
13699 ,  sequence
13700 ,  text_line
13701 ,  message_name
13702 )
13703 SELECT
13704    p_online_report_id
13705 ,  p_login_id
13706 ,  p_user_id
13707 ,  sysdate
13708 ,  p_user_id
13709 ,  sysdate
13710 ,  POL.line_num
13711 ,  POLL.shipment_num
13712 ,  0
13713 ,  p_sequence + ROWNUM
13714 ,  decode ( POL.quantity                                      -- <SERVICES FPJ>
13715           , NULL , PO_CORE_S.get_translated_text
13716                    (   'PO_SUB_SHIP_BILL_GT_ORD_AMT'
13717                    ,   'LINE_NUM' , POL.line_num
13718                    ,   'SHIP_NUM' , POLL.shipment_num
13719                    ,   'AMT_BILLED' , nvl(POLL.amount_billed, 0)
13720                    ,   'AMT_ORDERED' , nvl(POLL.amount, 0)
13721                    )
13722                  , PO_CORE_S.get_translated_text
13723                    (   'PO_SUB_SHIP_BILL_GT_ORD_QTY'
13724                    ,   'LINE_NUM' , POL.line_num
13725                    ,   'SHIP_NUM' , POLL.shipment_num
13726                    ,   'QTY_BILLED' , nvl(POLL.quantity_billed, 0)
13727                    ,   'QTY_ORDERED' , nvl(POLL.quantity, 0)
13728                    )
13729           )
13730 ,  decode ( POL.quantity                                      -- <SERVICES FPJ>
13731           , NULL , 'PO_SUB_SHIP_BILL_GT_ORD_AMT'
13732                  , 'PO_SUB_SHIP_BILL_GT_ORD_QTY'
13733           )
13734 FROM
13735    PO_LINE_LOCATIONS_GT POLL
13736 ,  PO_LINES_ALL POL  -- For releases, PO_LINES_GT table isn't populated.
13737 WHERE POLL.po_line_id = POL.po_line_id
13738 AND   nvl(POLL.cancel_flag, 'N') = 'N'
13739 AND   nvl(POLL.closed_code, 'OPEN') <> 'FINALLY CLOSED'
13740 AND   (                                                       -- <SERVICES FPJ>
13741           --<Complex Work R12 START>: Use POLL value basis
13742           (   ( POLL.value_basis IN ('QUANTITY', 'AMOUNT') )
13743           AND (nvl(POLL.quantity_billed,0) > nvl(POLL.quantity,0) ) )
13744       OR  (   ( POLL.value_basis IN ('FIXED PRICE', 'RATE') )
13745           AND (nvl(POLL.amount_billed,0) > nvl(POLL.amount,0) ) )
13746           --<Complex Work R12 END>
13747       )
13748 ;
13749 
13750 l_progress := '050';
13751 
13752 --Increment the p_sequence with number of errors reported in last query
13753 p_sequence := p_sequence + SQL%ROWCOUNT;
13754 
13755 IF g_debug_stmt THEN
13756    PO_DEBUG.debug_var(l_log_head,l_progress,'p_sequence',p_sequence);
13757 END IF;
13758 
13759 --------------------------------------------------------
13760 
13761 l_progress := '100';
13762 
13763 IF g_debug_stmt THEN
13764    PO_DEBUG.debug_stmt(l_log_head,l_progress,
13765       'UNRESERVE 2: Dist qty billed greater than qty ordered');
13766 END IF;
13767 
13768 -- Check 2: Unreserve action not allowed if distribution quantity/amount billed
13769 -- is greater than quantity/amount ordered
13770 
13771 l_progress := '110';
13772 
13773 -- Perform the check.
13774 
13775 INSERT INTO PO_ONLINE_REPORT_TEXT_GT
13776 (  online_report_id
13777 ,  last_update_login
13778 ,  last_updated_by
13779 ,  last_update_date
13780 ,  created_by
13781 ,  creation_date
13782 ,  line_num
13783 ,  shipment_num
13784 ,  distribution_num
13785 ,  sequence
13786 ,  text_line
13787 ,  message_name
13788 )
13789 SELECT
13790    p_online_report_id
13791 ,  p_login_id
13792 ,  p_user_id
13793 ,  sysdate
13794 ,  p_user_id
13795 ,  sysdate
13796 ,  POL.line_num
13797 ,  POLL.shipment_num
13798 ,  POD.distribution_num
13799 ,  p_sequence + ROWNUM
13800 ,  decode ( POL.quantity                                      -- <SERVICES FPJ>
13801           , NULL , PO_CORE_S.get_translated_text
13802                    (   'PO_SUB_DIST_BILL_GT_ORD_AMT'
13803                    ,   'LINE_NUM'    , POL.line_num
13804                    ,   'SHIP_NUM'    , POLL.shipment_num
13805                    ,   'DIST_NUM'    , POD.distribution_num
13806                    ,   'AMT_BILLED'  , nvl(POD.amount_billed, 0)
13807                    ,   'AMT_ORDERED' , nvl(POD.amount_ordered, 0)
13808                    )
13809                  , PO_CORE_S.get_translated_text
13810                    (   'PO_SUB_DIST_BILL_GT_ORD_QTY'
13811                    ,   'LINE_NUM'    , POL.line_num
13812                    ,   'SHIP_NUM'    , POLL.shipment_num
13813                    ,   'DIST_NUM'    , POD.distribution_num
13814                    ,   'QTY_BILLED'  , nvl(POD.quantity_billed, 0)
13815                    ,   'QTY_ORDERED' , nvl(POD.quantity_ordered, 0)
13816                    )
13817           )
13818 ,  decode ( POL.quantity                                      -- <SERVICES FPJ>
13819           , NULL , 'PO_SUB_DIST_BILL_GT_ORD_AMT'
13820                  , 'PO_SUB_DIST_BILL_GT_ORD_QTY'
13821           )
13822 FROM
13823    PO_DISTRIBUTIONS_GT POD
13824 ,  PO_LINE_LOCATIONS_GT POLL
13825 ,  PO_LINES_ALL POL  -- For releases, PO_LINES_GT table isn't populated.
13826 WHERE POD.line_location_id = POLL.line_location_id
13827 AND   POL.po_line_id = POLL.po_line_id
13828 AND   nvl(POLL.cancel_flag, 'N') = 'N'
13829 AND   nvl(POLL.closed_code, 'OPEN') <> 'FINALLY CLOSED'
13830 AND   (                                                       -- <SERVICES FPJ>
13831           (   ( POL.quantity IS NOT NULL )
13832           AND ( nvl(POD.quantity_billed,0) > nvl(POD.quantity_ordered,0) ) )
13833       OR  (   ( POL.amount IS NOT NULL )
13834           AND ( nvl(POD.amount_billed,0) > nvl(POD.amount_ordered,0) ) )
13835       )
13836 ;
13837 
13838 l_progress := '150';
13839 
13840 --Increment the p_sequence with number of errors reported in last query
13841 p_sequence := p_sequence + SQL%ROWCOUNT;
13842 
13843 IF g_debug_stmt THEN
13844    PO_DEBUG.debug_var(l_log_head,l_progress,'p_sequence',p_sequence);
13845 END IF;
13846 
13847 ----------------------------------------------------------
13848 
13849 l_progress := '200';
13850 
13851 IF g_debug_stmt THEN
13852    PO_DEBUG.debug_stmt(l_log_head,l_progress,
13853       'UNRESERVE 3: Dist qty delivered greater than qty ordered');
13854 END IF;
13855 
13856 -- Check 3: Unreserve action not allowed if distribution quantity/amount
13857 -- delivered is greater than the quantity/amount ordered
13858 
13859 l_progress := '210';
13860 
13861 -- Perform the check.
13862 
13863 INSERT INTO PO_ONLINE_REPORT_TEXT_GT
13864 (  online_report_id
13865 ,  last_update_login
13866 ,  last_updated_by
13867 ,  last_update_date
13868 ,  created_by
13869 ,  creation_date
13870 ,  line_num
13871 ,  shipment_num
13872 ,  distribution_num
13873 ,  sequence
13874 ,  text_line
13875 ,  message_name
13876 )
13877 SELECT
13878    p_online_report_id
13879 ,  p_login_id
13880 ,  p_user_id
13881 ,  sysdate
13882 ,  p_user_id
13883 ,  sysdate
13884 ,  POL.line_num
13885 ,  POLL.shipment_num
13886 ,  POD.distribution_num
13887 ,  p_sequence + ROWNUM
13888 ,  decode ( POL.quantity                                      -- <SERVICES FPJ>
13889           , NULL , PO_CORE_S.get_translated_text
13890                    (   'PO_SUB_DIST_DLVR_GT_ORD_AMT'
13891                    ,   'LINE_NUM'      , POL.line_num
13892                    ,   'SHIP_NUM'      , POLL.shipment_num
13893                    ,   'DIST_NUM'      , POD.distribution_num
13894                    ,   'AMT_DELIVERED' , nvl(POD.amount_delivered, 0)
13895                    ,   'AMT_ORDERED'   , nvl(POD.amount_ordered, 0)
13896                    )
13897                  , PO_CORE_S.get_translated_text
13898                    (   'PO_SUB_DIST_DLVR_GT_ORD_QTY'
13899                    ,   'LINE_NUM'      , POL.line_num
13900                    ,   'SHIP_NUM'      , POLL.shipment_num
13901                    ,   'DIST_NUM'      , POD.distribution_num
13902                    ,   'QTY_DELIVERED' , nvl(POD.quantity_delivered, 0)
13903                    ,   'QTY_ORDERED'   , nvl(POD.quantity_ordered, 0)
13904                    )
13905           )
13906 ,  decode ( POL.quantity                                      -- <SERVICES FPJ>
13907           , NULL , 'PO_SUB_DIST_DLVR_GT_ORD_AMT'
13908                  , 'PO_SUB_DIST_DLVR_GT_ORD_QTY'
13909           )
13910 FROM
13911    PO_DISTRIBUTIONS_GT POD
13912 ,  PO_LINE_LOCATIONS_GT POLL
13913 ,  PO_LINES_ALL POL  -- For releases, PO_LINES_GT table isn't populated.
13914 WHERE POD.line_location_id = POLL.line_location_id
13915 AND   POL.po_line_id = POLL.po_line_id
13916 AND   nvl(POLL.cancel_flag,'N') = 'N'
13917 AND   nvl(POLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
13918 AND   (                                                       -- <SERVICES FPJ>
13919           (   ( POL.quantity IS NOT NULL )
13920           AND ( nvl(POD.quantity_delivered,0) > nvl(POD.quantity_ordered,0) ) )
13921       OR  (   ( POL.amount IS NOT NULL )
13922           AND ( nvl(POD.amount_delivered,0) > nvl(POD.amount_ordered,0) ) )
13923       )
13924 ;
13925 
13926 l_progress := '250';
13927 -- Check 4: Check if there are unvalidated invoices/credit memo
13928 -- BuG#5462677
13929 -- Per Bug#4155351 p_origin_doc_id need to be passed only for finally close
13930 -- "..PO will pass invoice_id during final close due to a final match. "
13931       check_unvalidated_invoices(
13932          p_document_type     => p_document_type
13933       ,  p_document_subtype  => p_document_subtype
13934       ,  p_action_requested  => g_action_UNRESERVE
13935       ,  p_action_date       => SYSDATE
13936       ,  p_online_report_id  => p_online_report_id
13937       ,  p_user_id           => p_user_id
13938       ,  p_login_id          => p_login_id
13939       ,  p_document_level    => p_document_level
13940       ,  p_origin_doc_id     => NULL
13941       ,  p_doc_level_id      => p_doc_level_id
13942       ,  p_sequence          => p_sequence
13943       ,  x_return_status     => l_ret_sts
13944       );
13945 
13946       IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
13947         --d_msg := 'check_unvalidated_invoices not successful';
13948         l_progress := 110;
13949         RAISE PO_CORE_S.g_early_return_exc;
13950       END IF;
13951 
13952 
13953 l_progress := '260';
13954 
13955 --Increment the p_sequence with number of errors reported in last query
13956 p_sequence := p_sequence + SQL%ROWCOUNT;
13957 
13958 IF g_debug_stmt THEN
13959    PO_DEBUG.debug_var(l_log_head,l_progress,'p_sequence',p_sequence);
13960 END IF;
13961 
13962 l_progress := '300';
13963 
13964 IF g_debug_stmt THEN
13965    PO_DEBUG.debug_end(l_log_head);
13966 END IF;
13967 
13968 EXCEPTION
13969 WHEN OTHERS THEN
13970    IF g_debug_unexp THEN
13971       PO_DEBUG.debug_exc(l_log_head,l_progress);
13972    END IF;
13973    RAISE;
13974 
13975 END check_unreserve;
13976 
13977 
13978 
13979 
13980 -------------------------------------------------------------------------------
13981 --Start of Comments
13982 --Name: populate_line_locations_gt
13983 --Pre-reqs:
13984 --  None.
13985 --Modifies:
13986 --  PO_LINE_LOCATIONS_GT
13987 --Locks:
13988 --  None.
13989 --Function:
13990 --  Populates the line locations GTT for submission checks.
13991 --Parameters:
13992 --IN:
13993 --p_doc_type
13994 --  Document type.  Use the g_doc_type_<> variables, where <> is:
13995 --    PA
13996 --    PO
13997 --    RELEASE
13998 --p_doc_level
13999 --  The type of id that is being passed.  Use g_doc_level_<>
14000 --    HEADER
14001 --    LINE
14002 --    SHIPMENT
14003 --    DISTRIBUTION
14004 --p_doc_level_id
14005 --  Id of the doc level type of which to populate the table.
14006 --Testing:
14007 --
14008 --End of Comments
14009 -------------------------------------------------------------------------------
14010 PROCEDURE populate_line_locations_gt(
14011    p_doc_type                       IN             VARCHAR2
14012 ,  p_doc_level                      IN             VARCHAR2
14013 ,  p_doc_level_id                   IN             NUMBER
14014 )
14015 IS
14016 
14017 l_log_head CONSTANT VARCHAR2(100) := g_log_head||'POPULATE_LINE_LOCATIONS_GT';
14018 l_progress VARCHAR2(3) := '000';
14019 
14020 l_line_location_id_tbl  po_tbl_number;
14021 
14022 BEGIN
14023 
14024 IF g_debug_stmt THEN
14025    PO_DEBUG.debug_begin(l_log_head);
14026    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_type', p_doc_type);
14027    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level', p_doc_level);
14028    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level_id',p_doc_level_id);
14029 END IF;
14030 
14031 l_progress := '010';
14032 
14033 PO_CORE_S.get_line_location_ids(
14034    p_doc_type => p_doc_type
14035 ,  p_doc_level => p_doc_level
14036 ,  p_doc_level_id_tbl => po_tbl_number( p_doc_level_id )
14037 ,  x_line_location_id_tbl => l_line_location_id_tbl
14038 );
14039 
14040 l_progress := '020';
14041 
14042 FORALL i IN 1 .. l_line_location_id_tbl.COUNT
14043       INSERT INTO PO_LINE_LOCATIONS_GT (
14044          line_location_id     ,
14045          last_update_date      ,
14046          last_updated_by        ,
14047          po_header_id            ,
14048          po_line_id               ,
14049          last_update_login         ,
14050          creation_date              ,
14051          created_by                  ,
14052          quantity                     ,
14053          quantity_received             ,
14054          quantity_accepted              ,
14055          quantity_rejected               ,
14056          quantity_billed                  ,
14057          quantity_cancelled                ,
14058          amount,                                         -- <PO_CHANGE_API FPJ>
14059          amount_received,                                     -- <SERVICES FPJ>
14060          amount_accepted,                                     -- <SERVICES FPJ>
14061          amount_rejected,                                     -- <SERVICES FPJ>
14062          amount_billed,                                       -- <SERVICES FPJ>
14063          amount_cancelled,                                    -- <SERVICES FPJ>
14064          unit_meas_lookup_code              ,
14065          po_release_id                       ,
14066          ship_to_location_id                  ,
14067          ship_via_lookup_code                  ,
14068          need_by_date                           ,
14069          promised_date                           ,
14070          last_accept_date                         ,
14071          price_override                          ,
14072          encumbered_flag                        ,
14073          encumbered_date                         ,
14074          unencumbered_quantity                    ,
14075          fob_lookup_code                         ,
14076          freight_terms_lookup_code                ,
14077          taxable_flag                            ,
14078          estimated_tax_amount                    ,
14079          from_header_id                          ,
14080          from_line_id                             ,
14081          from_line_location_id                   ,
14082          start_date                               ,
14083          end_date                                ,
14084          lead_time                              ,
14085          lead_time_unit                          ,
14086          price_discount                           ,
14087          terms_id                                 ,
14088          approved_flag                            ,
14089          approved_date                            ,
14090          closed_flag                              ,
14091          cancel_flag                              ,
14092          cancelled_by                             ,
14093          cancel_date                              ,
14094          cancel_reason                            ,
14095          firm_status_lookup_code                  ,
14096          firm_date                                ,
14097          attribute_category                       ,
14098          attribute1                               ,
14099          attribute2                               ,
14100          attribute3                               ,
14101          attribute4                               ,
14102          attribute5                               ,
14103          attribute6                               ,
14104          attribute7                               ,
14105          attribute8                               ,
14106          attribute9                               ,
14107          attribute10                              ,
14108          unit_of_measure_class                    ,
14109          encumber_now                             ,
14110          attribute11                              ,
14111          attribute12                              ,
14112          attribute13                              ,
14113          attribute14                              ,
14114          attribute15                              ,
14115          inspection_required_flag                 ,
14116          receipt_required_flag                    ,
14117          qty_rcv_tolerance                       ,
14118          qty_rcv_exception_code                   ,
14119          enforce_ship_to_location_code            ,
14120          allow_substitute_receipts_flag           ,
14121          days_early_receipt_allowed               ,
14122          days_late_receipt_allowed                ,
14123          receipt_days_exception_code             ,
14124          invoice_close_tolerance                  ,
14125          receive_close_tolerance                  ,
14126          ship_to_organization_id                 ,
14127          shipment_num                            ,
14128          source_shipment_id                      ,
14129          shipment_type                     ,
14130          closed_code                        ,
14131          request_id                          ,
14132          program_application_id               ,
14133          program_id                            ,
14134          program_update_date                    ,
14135          government_context                      ,
14136          receiving_routing_id                     ,
14137          accrue_on_receipt_flag                  ,
14138          closed_reason                           ,
14139          closed_date                              ,
14140          closed_by                               ,
14141          org_id                                  ,
14142          global_attribute1                        ,
14143          global_attribute2                        ,
14144          global_attribute3                        ,
14145          global_attribute4                        ,
14146          global_attribute5                        ,
14147          global_attribute6                        ,
14148          global_attribute7                        ,
14149          global_attribute8                        ,
14150          global_attribute9                        ,
14151          global_attribute10                       ,
14152          global_attribute11                       ,
14153          global_attribute12                       ,
14154          global_attribute13                       ,
14155          global_attribute14                       ,
14156          global_attribute15                       ,
14157          global_attribute16                       ,
14158          global_attribute17                       ,
14159          global_attribute18                       ,
14160          global_attribute19                       ,
14161          global_attribute20                       ,
14162          global_attribute_category                ,
14163          quantity_shipped                        ,
14164          country_of_origin_code                   ,
14165          tax_user_override_flag                  ,
14166          match_option                            ,
14167          tax_code_id                              ,
14168          calculate_tax_flag                      ,
14169          change_promised_date_reason            ,
14170          note_to_receiver                        ,
14171          secondary_quantity                      ,
14172          secondary_unit_of_measure               ,
14173          preferred_grade                         ,
14174          secondary_quantity_received             ,
14175          secondary_quantity_accepted              ,
14176          secondary_quantity_rejected             ,
14177          secondary_quantity_cancelled             ,
14178          vmi_flag                                 ,
14179          consigned_flag                           ,
14180          retroactive_date                         ,
14181          payment_type                             , --<Complex Work R12>
14182          description                              , --<Complex Work R12>
14183          value_basis                                --<Complex Work R12>
14184      )
14185      SELECT
14186          line_location_id     ,
14187          last_update_date      ,
14188          last_updated_by        ,
14189          po_header_id            ,
14190          po_line_id               ,
14191          last_update_login         ,
14192          creation_date              ,
14193          created_by                  ,
14194          quantity                     ,
14195          quantity_received             ,
14196          quantity_accepted              ,
14197          quantity_rejected               ,
14198          quantity_billed                  ,
14199          quantity_cancelled                ,
14200          amount,                                         -- <PO_CHANGE_API FPJ>
14201          amount_received,                                     -- <SERVICES FPJ>
14202          amount_accepted,                                     -- <SERVICES FPJ>
14203          amount_rejected,                                     -- <SERVICES FPJ>
14204          amount_billed,                                       -- <SERVICES FPJ>
14205          amount_cancelled,                                    -- <SERVICES FPJ>
14206          unit_meas_lookup_code              ,
14207          po_release_id                       ,
14208          ship_to_location_id                  ,
14209          ship_via_lookup_code                  ,
14210          need_by_date                           ,
14211          promised_date                           ,
14212          last_accept_date                         ,
14213          price_override                          ,
14214          encumbered_flag                        ,
14215          encumbered_date                         ,
14216          unencumbered_quantity                    ,
14217          fob_lookup_code                         ,
14218          freight_terms_lookup_code                ,
14219          taxable_flag                            ,
14220          estimated_tax_amount                    ,
14221          from_header_id                          ,
14222          from_line_id                             ,
14223          from_line_location_id                   ,
14224          start_date                               ,
14225          end_date                                ,
14226          lead_time                              ,
14227          lead_time_unit                          ,
14228          price_discount                           ,
14229          terms_id                                 ,
14230          approved_flag                            ,
14231          approved_date                            ,
14232          closed_flag                              ,
14233          cancel_flag                              ,
14234          cancelled_by                             ,
14235          cancel_date                              ,
14236          cancel_reason                            ,
14237          firm_status_lookup_code                  ,
14238          firm_date                                ,
14239          attribute_category                       ,
14240          attribute1                               ,
14241          attribute2                               ,
14242          attribute3                               ,
14243          attribute4                               ,
14244          attribute5                               ,
14245          attribute6                               ,
14246          attribute7                               ,
14247          attribute8                               ,
14248          attribute9                               ,
14249          attribute10                              ,
14250          unit_of_measure_class                    ,
14251          encumber_now                             ,
14252          attribute11                              ,
14253          attribute12                              ,
14254          attribute13                              ,
14255          attribute14                              ,
14256          attribute15                              ,
14257          inspection_required_flag                 ,
14258          receipt_required_flag                    ,
14259          qty_rcv_tolerance                       ,
14260          qty_rcv_exception_code                   ,
14261          enforce_ship_to_location_code            ,
14262          allow_substitute_receipts_flag           ,
14263          days_early_receipt_allowed               ,
14264          days_late_receipt_allowed                ,
14265          receipt_days_exception_code             ,
14266          invoice_close_tolerance                  ,
14267          receive_close_tolerance                  ,
14268          ship_to_organization_id                 ,
14269          shipment_num                            ,
14270          source_shipment_id                      ,
14271          shipment_type                     ,
14272          closed_code                        ,
14273          request_id                          ,
14274          program_application_id               ,
14275          program_id                            ,
14276          program_update_date                    ,
14277          government_context                      ,
14278          receiving_routing_id                     ,
14279          accrue_on_receipt_flag                  ,
14280          closed_reason                           ,
14281          closed_date                              ,
14282          closed_by                               ,
14283          org_id                                  ,
14284          global_attribute1                        ,
14285          global_attribute2                        ,
14286          global_attribute3                        ,
14287          global_attribute4                        ,
14288          global_attribute5                        ,
14289          global_attribute6                        ,
14290          global_attribute7                        ,
14291          global_attribute8                        ,
14292          global_attribute9                        ,
14293          global_attribute10                       ,
14294          global_attribute11                       ,
14295          global_attribute12                       ,
14296          global_attribute13                       ,
14297          global_attribute14                       ,
14298          global_attribute15                       ,
14299          global_attribute16                       ,
14300          global_attribute17                       ,
14301          global_attribute18                       ,
14302          global_attribute19                       ,
14303          global_attribute20                       ,
14304          global_attribute_category                ,
14305          quantity_shipped                        ,
14306          country_of_origin_code                   ,
14307          tax_user_override_flag                  ,
14308          match_option                            ,
14309          tax_code_id                              ,
14310          calculate_tax_flag                      ,
14311          change_promised_date_reason            ,
14312          note_to_receiver                        ,
14313          secondary_quantity                      ,
14314          secondary_unit_of_measure               ,
14315          preferred_grade                         ,
14316          secondary_quantity_received             ,
14317          secondary_quantity_accepted              ,
14318          secondary_quantity_rejected             ,
14319          secondary_quantity_cancelled             ,
14320          vmi_flag                                 ,
14321          consigned_flag                           ,
14322          retroactive_date                         ,
14323          payment_type                             , --<Complex Work R12>
14324          description                              , --<Complex Work R12>
14325          value_basis                                --<Complex Work R12>
14326       FROM PO_LINE_LOCATIONS_ALL POLL
14327       WHERE POLL.line_location_id = l_line_location_id_tbl(i)
14328       ;
14329 
14330 l_progress := '900';
14331 
14332 IF g_debug_stmt THEN
14333    PO_DEBUG.debug_end(l_log_head);
14334 END IF;
14335 
14336 EXCEPTION
14337 WHEN OTHERS THEN
14338    IF g_debug_unexp THEN
14339       PO_DEBUG.debug_exc(l_log_head,l_progress);
14340    END IF;
14341    RAISE;
14342 
14343 END populate_line_locations_gt;
14344 
14345 
14346 
14347 
14348 -------------------------------------------------------------------------------
14349 --Start of Comments
14350 --Name: populate_distributions_gt
14351 --Pre-reqs:
14352 --  None.
14353 --Modifies:
14354 --  PO_DISTRIBTIONS_GT
14355 --Locks:
14356 --  None.
14357 --Function:
14358 --  Populates the distributions GTT for submission checks.
14359 --Parameters:
14360 --IN:
14361 --p_doc_type
14362 --  Document type.  Use the g_doc_type_<> variables, where <> is:
14363 --    REQUISITION
14364 --    PA
14365 --    PO
14366 --    RELEASE
14367 --p_doc_level
14368 --  The type of id that is being passed.  Use g_doc_level_<>
14369 --    HEADER
14370 --    LINE
14371 --    SHIPMENT
14372 --    DISTRIBUTION
14373 --p_doc_level_id
14374 --  Id of the doc level type of which to populate the distributions table.
14375 --Testing:
14376 --
14377 --End of Comments
14378 -------------------------------------------------------------------------------
14379 PROCEDURE populate_distributions_gt(
14380    p_doc_type                       IN             VARCHAR2
14381 ,  p_doc_level                      IN             VARCHAR2
14382 ,  p_doc_level_id                   IN             NUMBER
14383 )
14384 IS
14385 
14386 l_log_head CONSTANT VARCHAR2(100) := g_log_head||'POPULATE_DISTRIBUTIONS_GT';
14387 l_progress VARCHAR2(3) := '000';
14388 
14389 l_dist_id_tbl  po_tbl_number;
14390 
14391 BEGIN
14392 
14393 IF g_debug_stmt THEN
14394    PO_DEBUG.debug_begin(l_log_head);
14395    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_type', p_doc_type);
14396    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level', p_doc_level);
14397    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level_id',p_doc_level_id);
14398 END IF;
14399 
14400 l_progress := '010';
14401 
14402 IF (p_doc_type = g_document_type_REQUISITION) THEN
14403 
14404    l_progress := '020';
14405    IF g_debug_stmt THEN
14406       PO_DEBUG.debug_stmt(l_log_head,l_progress,'requisition');
14407    END IF;
14408 
14409    populate_req_distributions_gt(
14410       p_document_id => p_doc_level_id
14411    );
14412 
14413    l_progress := '030';
14414 
14415 ELSE
14416 
14417    l_progress := '040';
14418    IF g_debug_stmt THEN
14419       PO_DEBUG.debug_stmt(l_log_head,l_progress,'not requisition');
14420    END IF;
14421 
14422    PO_CORE_S.get_distribution_ids(
14423       p_doc_type => p_doc_type
14424    ,  p_doc_level => p_doc_level
14425    ,  p_doc_level_id_tbl => po_tbl_number( p_doc_level_id )
14426    ,  x_distribution_id_tbl => l_dist_id_tbl
14427    );
14428 
14429    l_progress := '050';
14430 
14431    FORALL i IN 1 .. l_dist_id_tbl.COUNT
14432    INSERT INTO PO_DISTRIBUTIONS_GT
14433    (
14434          po_distribution_id    ,
14435          last_update_date       ,
14436          last_updated_by         ,
14437          po_header_id             ,
14438          po_line_id                ,
14439          line_location_id           ,
14440          set_of_books_id            ,
14441          code_combination_id         ,
14442          quantity_ordered             ,
14443          amount_ordered,                                 -- <PO_CHANGE_API FPJ>
14444          last_update_login             ,
14445          creation_date                  ,
14446          created_by                      ,
14447          po_release_id                    ,
14448          quantity_delivered                ,
14449          quantity_billed                    ,
14450          quantity_cancelled                  ,
14451          amount_delivered,                                    -- <SERVICES FPJ>
14452          amount_billed                            ,
14453          amount_cancelled,                                    -- <SERVICES FPJ>
14454          req_header_reference_num             ,
14455          req_line_reference_num                ,
14456          req_distribution_id                    ,
14457          deliver_to_location_id                  ,
14458          deliver_to_person_id                    ,
14459          rate_date                               ,
14460          rate                                    ,
14461          accrued_flag                             ,
14462          encumbered_flag                          ,
14463          encumbered_amount                        ,
14464          unencumbered_quantity                    ,
14465          unencumbered_amount                      ,
14466          failed_funds_lookup_code                 ,
14467          gl_encumbered_date                       ,
14468          gl_encumbered_period_name                ,
14469          gl_cancelled_date                        ,
14470          destination_type_code                    ,
14471          destination_organization_id              ,
14472          destination_subinventory                ,
14473          attribute_category                       ,
14474          attribute1                               ,
14475          attribute2                               ,
14476          attribute3                               ,
14477          attribute4                               ,
14478          attribute5                               ,
14479          attribute6                               ,
14480          attribute7                               ,
14481          attribute8                               ,
14482          attribute9                               ,
14483          attribute10                              ,
14484          attribute11                              ,
14485          attribute12                              ,
14486          attribute13                              ,
14487          attribute14                              ,
14488          attribute15                              ,
14489          wip_entity_id                            ,
14490          wip_operation_seq_num                    ,
14491          wip_resource_seq_num                     ,
14492          wip_repetitive_schedule_id               ,
14493          wip_line_id                              ,
14494          bom_resource_id                          ,
14495          budget_account_id                        ,
14496          accrual_account_id                       ,
14497          variance_account_id                      ,
14498          prevent_encumbrance_flag                ,
14499          government_context                      ,
14500          destination_context                     ,
14501          distribution_num                  ,
14502          source_distribution_id             ,
14503          request_id                         ,
14504          program_application_id              ,
14505          program_id                           ,
14506          program_update_date                   ,
14507          project_id                             ,
14508          task_id                                 ,
14509          expenditure_type                         ,
14510          project_accounting_context              ,
14511          expenditure_organization_id              ,
14512          gl_closed_date                           ,
14513          accrue_on_receipt_flag                  ,
14514          expenditure_item_date                   ,
14515          org_id                                   ,
14516          kanban_card_id                           ,
14517          award_id                                ,
14518          mrc_rate_date                           ,
14519          mrc_rate                                 ,
14520          mrc_encumbered_amount                   ,
14521          mrc_unencumbered_amount                  ,
14522          end_item_unit_number                     ,
14523          tax_recovery_override_flag               ,
14524          recoverable_tax                          ,
14525          nonrecoverable_tax                       ,
14526          recovery_rate                            ,
14527          oke_contract_line_id                     ,
14528          oke_contract_deliverable_id
14529       ,  distribution_type
14530       ,  amount_to_encumber
14531    )
14532    SELECT
14533          po_distribution_id    ,
14534          last_update_date       ,
14535          last_updated_by         ,
14536          po_header_id             ,
14537          po_line_id                ,
14538          line_location_id           ,
14539          set_of_books_id            ,
14540          code_combination_id         ,
14541          quantity_ordered             ,
14542          amount_ordered,                                 -- <PO_CHANGE_API FPJ>
14543          last_update_login             ,
14544          creation_date                  ,
14545          created_by                      ,
14546          po_release_id                    ,
14547          quantity_delivered                ,
14548          quantity_billed                    ,
14549          quantity_cancelled                  ,
14550          amount_delivered,                                    -- <SERVICES FPJ>
14551          amount_billed                            ,
14552          amount_cancelled,                                    -- <SERVICES FPJ>
14553          req_header_reference_num             ,
14554          req_line_reference_num                ,
14555          req_distribution_id                    ,
14556          deliver_to_location_id                  ,
14557          deliver_to_person_id                    ,
14558          rate_date                               ,
14559          rate                                    ,
14560          accrued_flag                             ,
14561          encumbered_flag                          ,
14562          encumbered_amount                        ,
14563          unencumbered_quantity                    ,
14564          unencumbered_amount                      ,
14565          failed_funds_lookup_code                 ,
14566          gl_encumbered_date                       ,
14567          gl_encumbered_period_name                ,
14568          gl_cancelled_date                        ,
14569          destination_type_code                    ,
14570          destination_organization_id              ,
14571          destination_subinventory                ,
14572          attribute_category                       ,
14573          attribute1                               ,
14574          attribute2                               ,
14575          attribute3                               ,
14576          attribute4                               ,
14577          attribute5                               ,
14578          attribute6                               ,
14579          attribute7                               ,
14580          attribute8                               ,
14581          attribute9                               ,
14582          attribute10                              ,
14583          attribute11                              ,
14584          attribute12                              ,
14585          attribute13                              ,
14586          attribute14                              ,
14587          attribute15                              ,
14588          wip_entity_id                            ,
14589          wip_operation_seq_num                    ,
14590          wip_resource_seq_num                     ,
14591          wip_repetitive_schedule_id               ,
14592          wip_line_id                              ,
14593          bom_resource_id                          ,
14594          budget_account_id                        ,
14595          accrual_account_id                       ,
14596          variance_account_id                      ,
14597          prevent_encumbrance_flag                ,
14598          government_context                      ,
14599          destination_context                     ,
14600          distribution_num                  ,
14601          source_distribution_id             ,
14602          request_id                         ,
14603          program_application_id              ,
14604          program_id                           ,
14605          program_update_date                   ,
14606          project_id                             ,
14607          task_id                                 ,
14608          expenditure_type                         ,
14609          project_accounting_context              ,
14610          expenditure_organization_id              ,
14611          gl_closed_date                           ,
14612          accrue_on_receipt_flag                  ,
14613          expenditure_item_date                   ,
14614          org_id                                   ,
14615          kanban_card_id                           ,
14616          award_id                                ,
14617          mrc_rate_date                           ,
14618          mrc_rate                                 ,
14619          mrc_encumbered_amount                   ,
14620          mrc_unencumbered_amount                  ,
14621          end_item_unit_number                     ,
14622          tax_recovery_override_flag               ,
14623          recoverable_tax                          ,
14624          nonrecoverable_tax                       ,
14625          recovery_rate                            ,
14626          oke_contract_line_id                     ,
14627          oke_contract_deliverable_id
14628       ,  distribution_type
14629       ,  amount_to_encumber
14630    FROM PO_DISTRIBUTIONS_ALL POD
14631    WHERE POD.po_distribution_id = l_dist_id_tbl(i)
14632    ;
14633 
14634    l_progress := '060';
14635 
14636 END IF;
14637 
14638 l_progress := '900';
14639 
14640 IF g_debug_stmt THEN
14641    PO_DEBUG.debug_end(l_log_head);
14642 END IF;
14643 
14644 EXCEPTION
14645 WHEN OTHERS THEN
14646    IF g_debug_unexp THEN
14647       PO_DEBUG.debug_exc(l_log_head,l_progress);
14648    END IF;
14649    RAISE;
14650 
14651 END populate_distributions_gt;
14652 
14653 
14654 
14655 
14656 -------------------------------------------------------------------------------
14657 --Start of Comments
14658 --Name: check_gl_date
14659 --Pre-reqs:
14660 --  The global temp tables for the appropriate doc type
14661 --  have been populated with all of the rows required for the check.
14662 --  This should only be called if encumbrance is on.
14663 --Modifies:
14664 --  PO_ONLINE_REPORT_TEXT_GT
14665 --Locks:
14666 --  None.
14667 --Function:
14668 --  Checks that the GL date is usable (valid for GL and PO).
14669 --Parameters:
14670 --IN:
14671 --p_doc_type
14672 --  Document type.  Use the g_doc_type_<> variables, where <> is:
14673 --    REQUISITION
14674 --    PA
14675 --    PO
14676 --    RELEASE
14677 --p_online_report_id
14678 --  ID used to insert into PO_ONLINE_REPORT_TEXT_GT.
14679 --p_user_id
14680 --  User performing the action.
14681 --p_login_id
14682 --  Last update login_id.
14683 --IN OUT:
14684 --p_sequence
14685 --  Contains the running count of error messages inserted.
14686 --Testing:
14687 --
14688 --End of Comments
14689 -------------------------------------------------------------------------------
14690 PROCEDURE check_gl_date(
14691    p_doc_type                       IN             VARCHAR2
14692 ,  p_online_report_id               IN             NUMBER
14693 ,  p_login_id                       IN             NUMBER
14694 ,  p_user_id                        IN             NUMBER
14695 ,  p_sequence                       IN OUT NOCOPY  NUMBER
14696 )
14697 IS
14698 
14699 l_log_head     CONSTANT VARCHAR2(100) := g_log_head||'CHECK_GL_DATE';
14700 l_progress     VARCHAR2(3) := '000';
14701 
14702 l_msg_name     PO_ONLINE_REPORT_TEXT_GT.message_name%TYPE;
14703 l_textline     PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
14704 
14705 l_date_tbl              po_tbl_date;
14706 l_line_num_tbl          po_tbl_number;
14707 l_shipment_num_tbl      po_tbl_number;
14708 l_distribution_num_tbl  po_tbl_number;
14709 
14710 l_period_name_tbl       po_tbl_varchar30;
14711 l_period_year_tbl       po_tbl_number;
14712 l_period_num_tbl        po_tbl_number;
14713 l_quarter_num_tbl       po_tbl_number;
14714 l_invalid_period_flag   VARCHAR2(1);
14715 
14716 l_dates_key    NUMBER;
14717 
14718 BEGIN
14719 
14720 IF g_debug_stmt THEN
14721    PO_DEBUG.debug_begin(l_log_head);
14722    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_type',p_doc_type);
14723    PO_DEBUG.debug_var(l_log_head,l_progress,'p_online_report_id',p_online_report_id);
14724    PO_DEBUG.debug_var(l_log_head,l_progress,'p_user_id',p_user_id);
14725    PO_DEBUG.debug_var(l_log_head,l_progress,'p_login_id',p_login_id);
14726    PO_DEBUG.debug_var(l_log_head,l_progress,'p_sequence',p_sequence);
14727 END IF;
14728 
14729 l_progress := '010';
14730 
14731 -- Get the appropriate message.
14732 
14733 /* Bug 3210344: refactored different messages into one message */
14734 IF ((p_doc_type = g_document_type_REQUISITION) or
14735    (p_doc_type = g_document_type_RELEASE) or
14736    (p_doc_type = g_document_type_PO) or
14737    (p_doc_type = g_document_type_PA))
14738 THEN
14739    l_msg_name := 'PO_PDOI_INVALID_GL_ENC_PER';
14740 ELSE
14741    l_progress := '020';
14742    RAISE PO_CORE_S.g_INVALID_CALL_EXC;
14743 END IF;
14744 
14745 l_progress := '030';
14746 IF g_debug_stmt THEN
14747    PO_DEBUG.debug_var(l_log_head,l_progress,'l_msg_name',l_msg_name);
14748 END IF;
14749 
14750 l_textline := FND_MESSAGE.get_string('PO',l_msg_name);
14751 
14752 l_progress := '040';
14753 IF g_debug_stmt THEN
14754    PO_DEBUG.debug_var(l_log_head,l_progress,'l_textline',l_textline);
14755 END IF;
14756 
14757 -- Get the dates to validate.
14758 
14759 IF (p_doc_type = g_document_type_REQUISITION) THEN
14760 
14761    l_progress := '100';
14762    IF g_debug_stmt THEN
14763       PO_DEBUG.debug_stmt(l_log_head,l_progress,'requisition');
14764    END IF;
14765 
14766    SELECT
14767       PRD.gl_encumbered_date
14768    ,  PRL.line_num
14769    ,  NULL
14770    ,  PRD.distribution_num
14771    BULK COLLECT INTO
14772       l_date_tbl
14773    ,  l_line_num_tbl
14774    ,  l_shipment_num_tbl
14775    ,  l_distribution_num_tbl
14776    FROM
14777       PO_REQ_DISTRIBUTIONS_GT PRD
14778    ,  PO_REQ_LINES_GT PRL
14779    ,  PO_REQ_HEADERS_GT PRH
14780    WHERE PRL.requisition_line_id = PRD.requisition_line_id  --JOIN
14781    AND   PRH.requisition_header_id = PRL.requisition_header_id  --JOIN
14782    AND   PRL.line_location_id IS NULL
14783    AND
14784       (  NVL(PRH.transferred_to_oe_flag,'N') <> 'Y'
14785       OR NVL(PRL.source_type_code,'VENDOR') <> 'INVENTORY'
14786       )
14787    AND   NVL(PRD.encumbered_flag,'N') = 'N'
14788    AND   NVL(PRL.cancel_flag,'N') = 'N'
14789    AND   NVL(PRL.closed_code,'OPEN') <> 'FINALLY CLOSED'
14790    AND    Nvl(prl.modified_by_agent_flag,'N') = 'N' /*Bug 4882209*/
14791    ;
14792 
14793    l_progress := '110';
14794 
14795 ELSE
14796 
14797    l_progress := '150';
14798    IF g_debug_stmt THEN
14799       PO_DEBUG.debug_stmt(l_log_head,l_progress,'not requisition');
14800    END IF;
14801 
14802    SELECT
14803       POD.gl_encumbered_date
14804    ,  POL.line_num
14805    ,  POLL.shipment_num
14806    ,  POD.distribution_num
14807    BULK COLLECT INTO
14808       l_date_tbl
14809    ,  l_line_num_tbl
14810    ,  l_shipment_num_tbl
14811    ,  l_distribution_num_tbl
14812    FROM
14813       PO_DISTRIBUTIONS_GT POD
14814    ,  PO_LINE_LOCATIONS_GT POLL
14815    ,  PO_LINES_ALL POL     -- For Releases, PO_LINES_GT doesn't get populated.
14816    ,  PO_HEADERS_GT POH
14817    WHERE POLL.line_location_id(+) = POD.line_location_id    --JOIN
14818    AND   POL.po_line_id(+) = POD.po_line_id                 --JOIN
14819       -- PA distributions don't have associated lines or shipments
14820    AND   POH.po_header_id = POD.po_header_id                --JOIN
14821    AND   NVL(POD.encumbered_flag,'N') = 'N'
14822    AND   NVL(POLL.cancel_flag,'N') = 'N'
14823    AND   NVL(POLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
14824    AND   NVL(POLL.approved_flag,'N') <> 'Y'
14825    AND ( ( p_doc_type = g_document_type_PA
14826          AND POH.encumbrance_required_flag = 'Y'
14827          )
14828       OR ( p_doc_type <> g_document_type_PA )
14829       )
14830    ;
14831 
14832    l_progress := '160';
14833 
14834 END IF;
14835 
14836 l_progress := '200';
14837 
14838 -- Validate the dates.
14839 
14840 PO_PERIODS_SV.get_period_info(
14841    p_roll_logic => NULL
14842 ,  p_set_of_books_id => NULL
14843 ,  p_date_tbl => l_date_tbl
14844 ,  x_period_name_tbl => l_period_name_tbl
14845 ,  x_period_year_tbl => l_period_year_tbl
14846 ,  x_period_num_tbl => l_period_num_tbl
14847 ,  x_quarter_num_tbl => l_quarter_num_tbl
14848 ,  x_invalid_period_flag => l_invalid_period_flag
14849 );
14850 
14851 l_progress := '210';
14852 
14853 IF (l_invalid_period_flag = FND_API.G_TRUE) THEN
14854 
14855    l_progress := '215';
14856    IF g_debug_stmt THEN
14857       PO_DEBUG.debug_stmt(l_log_head,l_progress,'invalid period');
14858    END IF;
14859 
14860    -- Collect this information into the scratchpad,
14861    -- along with the info we need for error reporting.
14862 
14863    -----------------------------------------
14864    -- PO_SESSION_GT column mapping
14865    --
14866    -- num1     line_num
14867    -- num2     shipment_num
14868    -- num3     distribution_num
14869    -- char1    period_name
14870    -----------------------------------------
14871 
14872    SELECT PO_SESSION_GT_S.nextval
14873    INTO l_dates_key
14874    FROM DUAL
14875    ;
14876 
14877    l_progress := '220';
14878 
14879    FORALL i IN 1 .. l_period_name_tbl.COUNT
14880    INSERT INTO PO_SESSION_GT
14881    (  key
14882    ,  num1     -- line_num
14883    ,  num2     -- shipment_num
14884    ,  num3     -- distribution_num
14885    ,  char1    -- period_name
14886    )
14887    VALUES
14888    (  l_dates_key
14889    ,  l_line_num_tbl(i)
14890    ,  l_shipment_num_tbl(i)
14891    ,  l_distribution_num_tbl(i)
14892    ,  l_period_name_tbl(i)
14893    )
14894    ;
14895 
14896    l_progress := '230';
14897 
14898    -- Report the invalid dates.
14899 
14900    INSERT INTO PO_ONLINE_REPORT_TEXT_GT
14901    (  online_report_id
14902    ,  last_update_login
14903    ,  last_updated_by
14904    ,  last_update_date
14905    ,  created_by
14906    ,  creation_date
14907    ,  line_num
14908    ,  shipment_num
14909    ,  distribution_num
14910    ,  sequence
14911    ,  text_line
14912    ,  message_name
14913    )
14914    SELECT
14915       p_online_report_id
14916    ,  p_login_id
14917    ,  p_user_id
14918    ,  sysdate
14919    ,  p_user_id
14920    ,  sysdate
14921    ,  NVL(DATES.num1,0)    -- line_num
14922    ,  NVL(DATES.num2,0)    -- shipment_num
14923    ,  NVL(DATES.num3,0)    -- distribution_num
14924    ,  p_sequence + rownum
14925    ,  substr(
14926                DECODE(  DATES.num1  -- line_num
14927                      ,  NULL, ''
14928                      ,  g_linemsg||g_delim||TO_CHAR(DATES.num1)||g_delim
14929                      )
14930                ||
14931                DECODE(  DATES.num2  -- shipment_num
14932                      ,  NULL, ''
14933                      ,  g_shipmsg||g_delim||TO_CHAR(DATES.num2)||g_delim
14934                      )
14935                ||
14936                DECODE(  p_doc_type
14937                      ,  g_document_type_PA, ''
14938                      ,  g_distmsg||g_delim||TO_CHAR(DATES.num3)||g_delim
14939                      )
14940 
14941                ||l_textline
14942 
14943             ,  1
14944             ,  240
14945             )
14946    ,  l_msg_name
14947    FROM PO_SESSION_GT DATES
14948    WHERE DATES.key = l_dates_key
14949    AND   DATES.char1 IS NULL  -- period_name not found
14950    ;
14951 
14952    l_progress := '240';
14953 
14954    -- Increment the p_sequence with number of errors reported in last query
14955    p_sequence := p_sequence + SQL%ROWCOUNT;
14956 
14957    l_progress := '245';
14958 
14959    IF g_debug_stmt THEN
14960       PO_DEBUG.debug_var(l_log_head,l_progress,'l_textline',p_sequence);
14961    END IF;
14962 
14963    l_progress := '250';
14964 
14965 ELSE
14966    l_progress := '270';
14967    IF g_debug_stmt THEN
14968       PO_DEBUG.debug_stmt(l_log_head,l_progress,'No invalid periods.');
14969    END IF;
14970 END IF;
14971 
14972 l_progress := '900';
14973 
14974 IF g_debug_stmt THEN
14975    PO_DEBUG.debug_var(l_log_head,l_progress,'l_textline',p_sequence);
14976    PO_DEBUG.debug_end(l_log_head);
14977 END IF;
14978 
14979 EXCEPTION
14980 WHEN OTHERS THEN
14981    IF g_debug_unexp THEN
14982       PO_DEBUG.debug_exc(l_log_head,l_progress);
14983    END IF;
14984    RAISE;
14985 
14986 END check_gl_date;
14987 
14988 
14989 -- bug3592160 START
14990 -------------------------------------------------------------------------------
14991 --Start of Comments
14992 --Name: complete_po_header_id_tbl
14993 --Pre-reqs:
14994 --  All the IN parameters should be initialized and populated with values.
14995 --  All the table object type IN parameters should have the same number of
14996 --  records
14997 --  Org context should also be set before calling.
14998 --Modifies:
14999 --  None
15000 --Locks:
15001 --  None.
15002 --Function:
15003 --  For each record in the table objects pased in, derive po_header_id.
15004 --Parameters:
15005 --IN:
15006 --p_count
15007 --  Number of records in each of the table object
15008 --p_header_id
15009 --  Table Object containing po_header_id
15010 --p_release_id
15011 --  Table Object containing po_release_id
15012 --p_vendor_order_num
15013 --  Table Object containing vendor_order_num
15014 --p_document_num
15015 --  Table object containing document num (segment1)
15016 --p_type_lookup_code
15017 --  Table obejct containing type_lookup_code
15018 --OUT:
15019 --x_header_id
15020 --  Contains the derived po_header_ids
15021 --Testing:
15022 --
15023 --End of Comments
15024 -------------------------------------------------------------------------------
15025 PROCEDURE complete_po_header_id_tbl
15026 ( p_count            IN NUMBER,
15027   p_header_id        IN PO_TBL_NUMBER,
15028   p_release_id       IN PO_TBL_NUMBER,
15029   p_vendor_order_num IN PO_TBL_VARCHAR30,
15030   p_document_num     IN PO_TBL_VARCHAR30,
15031   p_type_lookup_code IN PO_TBL_VARCHAR30,
15032   x_header_id        OUT NOCOPY PO_TBL_NUMBER
15033 ) IS
15034 
15035 l_api_name     CONSTANT VARCHAR2(30) := 'COMPLETE_PO_HEADER_ID_TBL';
15036 l_log_head     CONSTANT VARCHAR2(100) := g_log_head|| l_api_name;
15037 l_progress     VARCHAR2(3) := '000';
15038 
15039 BEGIN
15040 
15041   IF g_debug_stmt THEN
15042      PO_DEBUG.debug_begin(l_log_head);
15043   END IF;
15044 
15045   x_header_id := PO_TBL_NUMBER();
15046   x_header_id.extend(p_count);
15047 
15048   l_progress := '010';
15049 
15050   FOR i IN 1..p_count LOOP
15051     IF (p_release_id(i) IS NOT NULL) THEN
15052 
15053       -- If p_release_id is not null, don't bother deriving po_header_id
15054       l_progress := '020';
15055       x_header_id(i) := NULL;
15056     ELSE
15057       IF p_header_id(i) IS NOT NULL THEN
15058         x_header_id(i) := p_header_id(i);
15059       END IF;
15060 
15061       l_progress := '030';
15062 
15063       IF (p_document_num(i) IS NOT NULL) THEN
15064 
15065         -- SQL What: Derive po_header_id based on document_num and
15066         --           type lookup_code. If x_header_id(i) already has value,
15067         --           validate it as well. If the po_header_id cannot be found,
15068         --           set x_header_id(i) as -999, indicating that po_header_Id
15069         --           cannot be derived by the IN paramters
15070         SELECT NVL(MIN(po_header_id), '-999')
15071         INTO   x_header_id(i)
15072         FROM   po_headers
15073         WHERE  segment1 = p_document_num(i)
15074         AND    type_lookup_code = p_type_lookup_code(i)
15075         AND    po_header_id = NVL(x_header_id(i), po_header_id);
15076 
15077         IF g_debug_stmt THEN
15078            PO_DEBUG.debug_stmt(l_log_head,l_progress,
15079                                'After checking document_num. id = ' || x_header_id(i));
15080         END IF;
15081       END IF;
15082 
15083       l_progress := '040';
15084 
15085       IF (p_vendor_order_num(i) IS NOT NULL) THEN
15086 
15087         -- SQL What: Derive po_header_id based on vendor_order_num and
15088         --           type lookup_code. If x_header_id(i) already has value,
15089         --           validate it as well. If the po_header_id cannot be found,
15090         --           set x_header_id(i) as -999, indicating that po_header_Id
15091         --           cannot be derived by the IN paramters
15092         SELECT NVL(MIN(po_header_id), '-999')
15093         INTO x_header_id(i)
15094         FROM   po_headers_all
15095         WHERE  vendor_order_num = p_vendor_order_num(i)
15096         AND  po_header_id = NVL(x_header_id(i), po_header_id);
15097 
15098         IF g_debug_stmt THEN
15099            PO_DEBUG.debug_stmt(l_log_head,l_progress,
15100                                'After checking vendor_order_num. id = ' || x_header_id(i));
15101         END IF;
15102       END IF;
15103 
15104     END IF;
15105 
15106     l_progress := '050';
15107 
15108     IF (x_header_id(i) = -999) THEN
15109       x_header_id(i) := NULL;
15110     END IF;
15111 
15112     IF g_debug_stmt THEN
15113        PO_DEBUG.debug_stmt(l_log_head,l_progress,
15114                            'Final ID for rec ' ||i|| '= ' || x_header_id(i));
15115     END IF;
15116 
15117   END LOOP;
15118 
15119   IF g_debug_stmt THEN
15120      PO_DEBUG.debug_end(l_log_head);
15121   END IF;
15122 
15123 EXCEPTION
15124 WHEN OTHERS THEN
15125    IF g_debug_unexp THEN
15126       PO_DEBUG.debug_exc(l_log_head,l_progress);
15127    END IF;
15128    RAISE;
15129 
15130 END complete_po_header_id_tbl;
15131 -- bug3592160 END
15132 
15133 --<JFMIP Vendor Registration FPJ Start>
15134 -------------------------------------------------------------------------------
15135 --Start of Comments
15136 --Name: check_vendor_site_ccr_regis
15137 --Pre-reqs:
15138 --  None
15139 --Modifies:
15140 --  po_online_report_text_gt
15141 --Locks:
15142 --  None.
15143 --Function:
15144 -- This procedure is used to perform the submission check related to CCR
15145 -- registration status of a vendor site. The resulting error (if any) is
15146 -- stored in the global temp table
15147 --Parameters:
15148 --IN:
15149 --p_document_id
15150 --  po_header_id of a Purchase Order or Purchase Agreement
15151 --p_online_report_id
15152 --  unique error report id
15153 --p_user_id
15154 --  user id
15155 --p_login_id
15156 --  login id
15157 --IN OUT:
15158 --p_sequence
15159 --  maintains a count of total number of submission check errors/warnings
15160 --OUT:
15161 --x_return_status
15162 --  return status of the procedure. Possible values are
15163 --  FND_API.G_RET_STS_SUCCESS and FND_API.G_RET_STS_UNEXP_ERROR
15164 --Notes:
15165 --  None
15166 --Testing:
15167 --  None
15168 --End of Comments
15169 -------------------------------------------------------------------------------
15170 PROCEDURE check_vendor_site_ccr_regis(
15171   p_document_id        IN NUMBER,
15172   p_online_report_id   IN NUMBER,
15173   p_user_id            IN NUMBER,
15174   p_login_id           IN NUMBER,
15175   p_sequence           IN OUT NOCOPY NUMBER,
15176   x_return_status      OUT NOCOPY VARCHAR2)
15177 IS
15178   l_api_name           CONSTANT VARCHAR2(30) := 'CHECK_VENDOR_SITE_CCR_REGIS';
15179   l_progress           VARCHAR2(3);
15180 
15181   l_vendor_id          PO_HEADERS.vendor_id%TYPE;
15182   l_vendor_site_id     PO_HEADERS.vendor_site_id%TYPE;
15183   l_valid_registration BOOLEAN := FALSE;
15184 
15185   l_text_line           PO_ONLINE_REPORT_TEXT.text_line%TYPE := NULL;
15186 
15187 BEGIN
15188 
15189   l_progress := '000';
15190   x_return_status := FND_API.G_RET_STS_SUCCESS;
15191 
15192   IF g_debug_stmt THEN
15193      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
15194        FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
15195              || l_progress, ' Before retrieving vendor/vendor site info');
15196      END IF;
15197   END IF;
15198 
15199   -- SQL What: retrieves vendor id and vendor site id from the document header
15200   -- SQL Why:  need to check vendor site registration status below
15201   BEGIN
15202     SELECT    vendor_id, vendor_site_id
15203     INTO      l_vendor_id, l_vendor_site_id
15204     FROM      po_headers_all
15205     WHERE     po_header_id = p_document_id;
15206   EXCEPTION
15207     WHEN OTHERS THEN
15208       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
15209   END;
15210 
15211   l_progress := '010';
15212   IF g_debug_stmt THEN
15213      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
15214        FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
15215              || l_progress, ' After retrieving vendor/vendor site info');
15216      END IF;
15217   END IF;
15218 
15219   IF (l_vendor_id IS NOT NULL) AND (l_vendor_site_id IS NOT NULL) THEN
15220 
15221      -- Call PO_FV_INTEGRATION_PVT.val_vendor_site_ccr_regis to check the
15222      -- Central Contractor Registration(CCR) status of the vendor site
15223 
15224      l_progress := '020';
15225      IF g_debug_stmt THEN
15226      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
15227        FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
15228          || l_progress, ' Before validating vendor site registration');
15229      END IF;
15230      END IF;
15231 
15232      l_valid_registration := PO_FV_INTEGRATION_PVT.val_vendor_site_ccr_regis(
15233                           p_vendor_id      => l_vendor_id,
15234                           p_vendor_site_id => l_vendor_site_id);
15235 
15236      IF NOT l_valid_registration THEN
15237 
15238         l_progress := '030';
15239         IF g_debug_stmt THEN
15240             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
15241               FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
15242               l_api_name||'.' || l_progress,
15243               ' Vendor site registration is not valid');
15244             END IF;
15245         END IF;
15246 
15247         l_text_line := FND_MESSAGE.get_string('PO', 'PO_VENDOR_SITE_CCR_INVALID');
15248         INSERT INTO po_online_report_text_gt
15249                 (online_report_id
15250                 ,last_updated_by
15251                 ,last_update_date
15252                 ,created_by
15253                 ,creation_date
15254                 ,line_num
15255                 ,shipment_num
15256                 ,distribution_num
15257                 ,sequence
15258                 ,text_line
15259                 ,message_name)
15260          VALUES
15261                 (p_online_report_id
15262                 ,p_login_id
15263                 ,sysdate
15264                 ,p_user_id
15265                 ,sysdate
15266                 ,0 ,0 ,0
15267                 ,p_sequence+1
15268                 ,substr(l_text_line,1,240)
15269                 ,'PO_VENDOR_SITE_CCR_INVALID');
15270 
15271           -- Increment p_sequence by 1
15272           p_sequence := p_sequence+1;
15273      ELSE -- l_valid_registration is TRUE
15274          l_progress := '040';
15275          IF g_debug_stmt THEN
15276             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
15277               FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
15278                 l_api_name||'.' || l_progress,
15279                 ' Vendor site registration is valid');
15280             END IF;
15281          END IF;
15282      END IF; -- l_valid_registration check
15283   END IF; -- l_vendor_id and l_vendor_site_id check
15284 
15285 EXCEPTION
15286   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
15287      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
15288   WHEN OTHERS THEN
15289      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
15290      IF (g_debug_unexp) THEN
15291         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
15292           FND_LOG.string(FND_LOG.level_unexpected,
15293                        G_PKG_NAME||l_api_name|| '.others_exception',
15294                        'EXCEPTION: Location is '|| l_progress ||' SQL CODE is '||sqlcode);
15295         END IF;
15296      END IF;
15297 
15298 END check_vendor_site_ccr_regis;
15299 --<JFMIP Vendor Registration FPJ End>
15300 
15301 
15302 -- <Doc Manager Rewrite 11.5.11 Start>
15303 PROCEDURE check_final_close(
15304    p_document_type        IN VARCHAR2
15305 ,  p_document_subtype     IN VARCHAR2
15306 ,  p_document_level       IN VARCHAR2
15307 ,  p_document_id          IN NUMBER
15308 ,  p_online_report_id     IN NUMBER
15309 ,  p_user_id              IN NUMBER
15310 ,  p_login_id             IN NUMBER
15311 ,  p_origin_doc_id        IN NUMBER := NULL --Bug#5462677
15312 ,  p_doc_level_id         IN NUMBER
15313 ,  p_sequence             IN OUT NOCOPY NUMBER
15314 ,  x_return_status        OUT NOCOPY VARCHAR2
15315 )
15316 IS
15317 
15318 d_module    VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.check_final_close';
15319 d_progress  NUMBER;
15320 d_msg       VARCHAR2(200);
15321 
15322 l_ret_sts   VARCHAR2(1);
15323 
15324 BEGIN
15325 
15326   IF (PO_LOG.d_proc) THEN
15327     PO_LOG.proc_begin(d_module);
15328     PO_LOG.proc_begin(d_module, 'p_document_type', p_document_type);
15329     PO_LOG.proc_begin(d_module, 'p_document_subtype', p_document_subtype);
15330     PO_LOG.proc_begin(d_module, 'p_document_level', p_document_level);
15331     PO_LOG.proc_begin(d_module, 'p_document_id', p_document_id);
15332     PO_LOG.proc_begin(d_module, 'p_online_report_id', p_online_report_id);
15333     PO_LOG.proc_begin(d_module, 'p_user_id', p_user_id);
15334     PO_LOG.proc_begin(d_module, 'p_login_id', p_login_id);
15335     PO_LOG.proc_begin(d_module, 'p_sequence', p_sequence);
15336   END IF;
15337 
15338   BEGIN
15339 
15340     d_progress := 10;
15341 
15342     IF (((p_document_type = g_document_type_PO) AND (p_document_subtype = 'STANDARD'))
15343          OR (p_document_type = g_document_type_RELEASE))
15344     THEN
15345 
15346       d_progress := 20;
15347 
15348       check_rcv_trans_interface(
15349          p_document_type     => p_document_type
15350       ,  p_document_level    => p_document_level
15351       ,  p_online_report_id  => p_online_report_id
15352       ,  p_user_id           => p_user_id
15353       ,  p_login_id          => p_login_id
15354       ,  p_document_id       => p_document_id
15355       ,  p_sequence          => p_sequence
15356       ,  x_return_status     => l_ret_sts
15357       );
15358 
15359       IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
15360         d_msg := 'check_rcv_trans_interface not successful';
15361         d_progress := 30;
15362         RAISE PO_CORE_S.g_early_return_exc;
15363       END IF;
15364 
15365       d_progress := 40;
15366 
15367       check_asn_not_fully_received(
15368          p_document_type     => p_document_type
15369       ,  p_online_report_id  => p_online_report_id
15370       ,  p_user_id           => p_user_id
15371       ,  p_login_id          => p_login_id
15372       ,  p_sequence          => p_sequence
15373       ,  x_return_status     => l_ret_sts
15374       );
15375 
15376       IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
15377         d_msg := 'check_asn_not_fully_received not successful';
15378         d_progress := 50;
15379         RAISE PO_CORE_S.g_early_return_exc;
15380       END IF;
15381 
15382       d_progress := 60;
15383 
15384       check_qty_rcv_but_not_deliv(
15385          p_document_type     => p_document_type
15386       ,  p_online_report_id  => p_online_report_id
15387       ,  p_user_id           => p_user_id
15388       ,  p_login_id          => p_login_id
15389       ,  p_sequence          => p_sequence
15390       ,  x_return_status     => l_ret_sts
15391       );
15392 
15393       IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
15394         d_msg := 'check_qty_rcv_but_not_deliv not successful';
15395         d_progress := 70;
15396         RAISE PO_CORE_S.g_early_return_exc;
15397       END IF;
15398 
15399       d_progress := 80;
15400 
15401       check_amt_rcv_but_not_deliv(
15402          p_document_type     => p_document_type
15403       ,  p_online_report_id  => p_online_report_id
15404       ,  p_user_id           => p_user_id
15405       ,  p_login_id          => p_login_id
15406       ,  p_sequence          => p_sequence
15407       ,  x_return_status     => l_ret_sts
15408       );
15409 
15410       IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
15411         d_msg := 'check_amt_rcv_but_not_deliv not successful';
15412         d_progress := 90;
15413         RAISE PO_CORE_S.g_early_return_exc;
15414       END IF;
15415 
15416       d_progress := 100;
15417 
15418       check_invalid_acct_flex(
15419          p_document_type     => p_document_type
15420       ,  p_action_requested  => g_action_FINAL_CLOSE_CHECK
15421       ,  p_action_date       => SYSDATE
15422       ,  p_online_report_id  => p_online_report_id
15423       ,  p_user_id           => p_user_id
15424       ,  p_login_id          => p_login_id
15425       ,  p_document_id       => p_document_id
15426       ,  p_sequence          => p_sequence
15427       ,  x_return_status     => l_ret_sts
15428       );
15429 
15430       IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
15431         d_msg := 'check_invalid_acct_flex not successful';
15432         d_progress := 110;
15433         RAISE PO_CORE_S.g_early_return_exc;
15434       END IF;
15435 
15436       -- Bug#5462677
15437       d_progress := 100;
15438 
15439       check_unvalidated_invoices(
15440          p_document_type     => p_document_type
15441       ,  p_document_subtype  => p_document_subtype
15442       ,  p_action_requested  => g_action_FINAL_CLOSE_CHECK
15443       ,  p_action_date       => SYSDATE
15444       ,  p_online_report_id  => p_online_report_id
15445       ,  p_user_id           => p_user_id
15446       ,  p_login_id          => p_login_id
15447       ,  p_document_level    => p_document_level --Bug#5462677
15448       ,  p_origin_doc_id     => p_origin_doc_id  --Bug#5462677
15449       ,  p_doc_level_id      => p_doc_level_id
15450       ,  p_sequence          => p_sequence
15451       ,  x_return_status     => l_ret_sts
15452       );
15453 
15454       IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
15455         d_msg := 'check_unvalidated_invoices not successful';
15456         d_progress := 110;
15457         RAISE PO_CORE_S.g_early_return_exc;
15458       END IF;
15459 
15460     ELSIF ((p_document_type = g_document_type_PO) AND (p_document_subtype = 'PLANNED'))
15461     THEN
15462 
15463       d_progress := 200;
15464 
15465       check_invalid_acct_flex(
15466          p_document_type     => p_document_type
15467       ,  p_action_requested  => g_action_FINAL_CLOSE_CHECK
15468       ,  p_action_date       => SYSDATE
15469       ,  p_online_report_id  => p_online_report_id
15470       ,  p_user_id           => p_user_id
15471       ,  p_login_id          => p_login_id
15472       ,  p_document_id       => p_document_id
15473       ,  p_sequence          => p_sequence
15474       ,  x_return_status     => l_ret_sts
15475       );
15476 
15477       IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
15478         d_msg := 'check_invalid_acct_flex not successful';
15479         d_progress := 210;
15480         RAISE PO_CORE_S.g_early_return_exc;
15481       END IF;
15482 
15483       d_progress := 220;
15484 
15485       check_ppo_has_open_release(
15486          p_online_report_id  => p_online_report_id
15487       ,  p_user_id           => p_user_id
15488       ,  p_login_id          => p_login_id
15489       ,  p_sequence          => p_sequence
15490       ,  x_return_status     => l_ret_sts
15491       );
15492 
15493       IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
15494         d_msg := 'check_ppo_has_open_release not successful';
15495         d_progress := 230;
15496         RAISE PO_CORE_S.g_early_return_exc;
15497       END IF;
15498 
15499     ELSIF ((p_document_type = g_document_type_PA) AND (p_document_subtype = 'BLANKET'))
15500     THEN
15501 
15502       d_progress := 300;
15503 
15504       check_bpa_has_open_release(
15505          p_online_report_id  => p_online_report_id
15506       ,  p_user_id           => p_user_id
15507       ,  p_login_id          => p_login_id
15508       ,  p_sequence          => p_sequence
15509       ,  x_return_status     => l_ret_sts
15510       );
15511 
15512       IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
15513         d_msg := 'check_bpa_has_open_release not successful';
15514         d_progress := 310;
15515         RAISE PO_CORE_S.g_early_return_exc;
15516       END IF;
15517 
15518       d_progress := 320;
15519 
15520       check_bpa_has_open_stdref(
15521          p_online_report_id  => p_online_report_id
15522       ,  p_user_id           => p_user_id
15523       ,  p_login_id          => p_login_id
15524       ,  p_sequence          => p_sequence
15525       ,  x_return_status     => l_ret_sts
15526       );
15527 
15528       IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
15529         d_msg := 'check_bpa_has_open_stdref not successful';
15530         d_progress := 330;
15531         RAISE PO_CORE_S.g_early_return_exc;
15532       END IF;
15533 
15534     ELSIF ((p_document_type = g_document_type_PA) AND (p_document_subtype = 'CONTRACT'))
15535     THEN
15536 
15537       d_progress := 400;
15538 
15539       check_cpa_has_open_stdref(
15540          p_online_report_id  => p_online_report_id
15541       ,  p_user_id           => p_user_id
15542       ,  p_login_id          => p_login_id
15543       ,  p_sequence          => p_sequence
15544       ,  x_return_status     => l_ret_sts
15545       );
15546 
15547       IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
15548         d_msg := 'check_cpa_has_open_stdref not successful';
15549         d_progress := 410;
15550         RAISE PO_CORE_S.g_early_return_exc;
15551       END IF;
15552 
15553     END IF;  -- p_document_type = ...
15554 
15555     l_ret_sts := FND_API.G_RET_STS_SUCCESS;
15556 
15557   EXCEPTION
15558     WHEN PO_CORE_S.g_early_return_exc THEN
15559       IF (PO_LOG.d_exc) THEN
15560         PO_LOG.exc(d_module, d_progress, d_msg);
15561       END IF;
15562       l_ret_sts := FND_API.G_RET_STS_UNEXP_ERROR;
15563   END;
15564 
15565   x_return_status := l_ret_sts;
15566 
15567   IF (PO_LOG.d_proc) THEN
15568     PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
15569     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
15570     PO_LOG.proc_end(d_module);
15571   END IF;
15572 
15573 EXCEPTION
15574 
15575   WHEN others THEN
15576     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
15577 
15578     IF (PO_LOG.d_exc) THEN
15579       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
15580       PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
15581       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
15582       PO_LOG.proc_end(d_module);
15583     END IF;
15584 
15585     RETURN;
15586 
15587 END check_final_close;
15588 
15589 
15590 -- Determine if there are any receiving transactions in the
15591 -- receiving interface that have not yet been processed
15592 PROCEDURE check_rcv_trans_interface(
15593    p_document_type        IN VARCHAR2
15594 ,  p_document_level       IN VARCHAR2  --<Bug 4118145, Issue 7>: Corrected type
15595 ,  p_online_report_id     IN NUMBER
15596 ,  p_user_id              IN NUMBER
15597 ,  p_login_id             IN NUMBER
15598 ,  p_document_id          IN NUMBER
15599 ,  p_sequence             IN OUT NOCOPY NUMBER
15600 ,  x_return_status        OUT NOCOPY VARCHAR2
15601 )
15602 IS
15603 
15604 l_textline  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
15605 l_ret_sts   VARCHAR2(1);
15606 l_is_complex_po     boolean;
15607 l_token_value VARCHAR2(256);
15608 
15609 d_module   VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.check_rcv_trans_interface';
15610 d_progress NUMBER;
15611 d_msg      VARCHAR2(60);
15612 
15613 
15614 BEGIN
15615 
15616   d_progress := 0;
15617   IF (PO_LOG.d_proc) THEN
15618     PO_LOG.proc_begin(d_module);
15619     PO_LOG.proc_begin(d_module, 'p_document_type', p_document_type);
15620     PO_LOG.proc_begin(d_module, 'p_document_level', p_document_level);
15621     PO_LOG.proc_begin(d_module, 'p_online_report_id', p_online_report_id);
15622     PO_LOG.proc_begin(d_module, 'p_user_id', p_user_id);
15623     PO_LOG.proc_begin(d_module, 'p_login_id', p_login_id);
15624     PO_LOG.proc_begin(d_module, 'p_sequence', p_sequence);
15625   END IF;
15626 
15627   BEGIN
15628 
15629     IF (p_document_level = g_document_level_HEADER)
15630     THEN
15631 
15632       IF (p_document_type <> g_document_type_RELEASE)
15633       THEN
15634 
15635         d_progress := 10;
15636 
15637         l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_POH_WITH_RCV_TRX');
15638 
15639         INSERT INTO PO_ONLINE_REPORT_TEXT_GT
15640         (  online_report_id
15641         ,  last_update_login
15642         ,  last_updated_by
15643         ,  last_update_date
15644         ,  created_by
15645         ,  creation_date
15646         ,  line_num
15647         ,  shipment_num
15648         ,  distribution_num
15649         ,  sequence
15650         ,  text_line
15651         ,  message_name
15652         )
15653         SELECT
15654            p_online_report_id
15655         ,  p_login_id
15656         ,  p_user_id
15657         ,  SYSDATE
15658         ,  p_user_id
15659         ,  SYSDATE
15660         ,  0
15661         ,  0
15662         ,  0
15663         ,  p_sequence + ROWNUM
15664         ,  substr(l_textline, 1, 240)
15665         ,  'PO_CAN_POH_WITH_RCV_TRX'
15666         FROM po_headers_gt poh
15667         WHERE EXISTS
15668           (
15669             SELECT 'Eligible shipment'
15670             FROM po_line_locations_gt poll
15671             WHERE poll.po_header_id = poh.po_header_id
15672               AND NVL(poll.cancel_flag, 'N') = 'N'
15673               AND NVL(poll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
15674           )
15675           AND EXISTS
15676           (
15677             SELECT 'Transaction to process'
15678             FROM rcv_transactions_interface rti
15679             WHERE rti.processing_status_code = 'PENDING'
15680               AND rti.po_header_id = poh.po_header_id
15681           );
15682 
15683         d_progress := 15;
15684 
15685       ELSE
15686 
15687         d_progress := 20;
15688 
15689         l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_REL_WITH_RCV_TRX');
15690 
15691         INSERT INTO PO_ONLINE_REPORT_TEXT_GT
15692         (  online_report_id
15693         ,  last_update_login
15694         ,  last_updated_by
15695         ,  last_update_date
15696         ,  created_by
15697         ,  creation_date
15698         ,  line_num
15699         ,  shipment_num
15700         ,  distribution_num
15701         ,  sequence
15702         ,  text_line
15703         ,  message_name
15704         )
15705         SELECT
15706            p_online_report_id
15707         ,  p_login_id
15708         ,  p_user_id
15709         ,  SYSDATE
15710         ,  p_user_id
15711         ,  SYSDATE
15712         ,  0
15713         ,  0
15714         ,  0
15715         ,  p_sequence + ROWNUM
15716         ,  substr(l_textline, 1, 240)
15717         ,  'PO_CAN_REL_WITH_RCV_TRX'
15718         FROM po_releases_gt por
15719         WHERE EXISTS
15720           (
15721             SELECT 'Eligible shipment'
15722             FROM po_line_locations_gt poll
15723             WHERE poll.po_release_id = por.po_release_id
15724               AND NVL(poll.cancel_flag, 'N') = 'N'
15725               AND NVL(poll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
15726           )
15727           AND EXISTS
15728           (
15729             SELECT 'Transaction to process'
15730             FROM rcv_transactions_interface rti
15731             WHERE rti.processing_status_code = 'PENDING'
15732               AND rti.po_release_id = por.po_release_id
15733           );
15734 
15735         d_progress := 25;
15736 
15737       END IF;  -- p_document_type = ...
15738 
15739     ELSIF (p_document_level = g_document_level_LINE)
15740     THEN
15741 
15742       d_progress := 30;
15743 
15744       l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_POL_WITH_RCV_TRX');
15745 
15746       INSERT INTO PO_ONLINE_REPORT_TEXT_GT
15747       (  online_report_id
15748       ,  last_update_login
15749       ,  last_updated_by
15750       ,  last_update_date
15751       ,  created_by
15752       ,  creation_date
15753       ,  line_num
15754       ,  shipment_num
15755       ,  distribution_num
15756       ,  sequence
15757       ,  text_line
15758       ,  message_name
15759       )
15760       SELECT
15761          p_online_report_id
15762       ,  p_login_id
15763       ,  p_user_id
15764       ,  SYSDATE
15765       ,  p_user_id
15766       ,  SYSDATE
15767       ,  pol.line_num
15768       ,  0
15769       ,  0
15770       ,  p_sequence + ROWNUM
15771       ,  substr(g_linemsg || g_delim || pol.line_num || g_delim || l_textline, 1, 240)
15772       ,  'PO_CAN_POL_WITH_RCV_TRX'
15773       FROM po_lines_gt pol
15774       WHERE EXISTS
15775         (
15776           SELECT 'Eligible shipment'
15777           FROM po_line_locations_gt poll
15778           WHERE poll.po_line_id = pol.po_line_id
15779             AND NVL(poll.cancel_flag, 'N') = 'N'
15780             AND NVL(poll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
15781         )
15782         AND EXISTS
15783         (
15784           SELECT 'Transaction to process'
15785           FROM rcv_transactions_interface rti
15786           WHERE rti.processing_status_code = 'PENDING'
15787             AND rti.po_line_id = pol.po_line_id
15788         );
15789 
15790       d_progress := 40;
15791 
15792     ELSIF (p_document_level = g_document_level_SHIPMENT)
15793     THEN
15794 
15795       d_progress := 50;
15796 
15797       IF (p_document_type <> g_document_type_RELEASE)
15798       THEN
15799         l_is_complex_po := PO_COMPLEX_WORK_PVT.is_complex_work_po(p_document_id);
15800 
15801         IF (l_is_complex_po) THEN
15802           l_token_value := FND_MESSAGE.GET_STRING('PO', 'PO_LINE_LOC_TYPE_LOW_P_PAYITEM');
15803         ELSE
15804           l_token_value := FND_MESSAGE.GET_STRING('PO', 'PO_LINE_LOC_TYPE_LOWER_S_SCH');
15805         END IF;
15806 
15807         FND_MESSAGE.SET_NAME('PO','PO_CAN_POLL_WITH_RCV_TRX');
15808         FND_MESSAGE.SET_TOKEN('LINE_LOCATION_TYPE', l_token_value);
15809 
15810         l_textline := FND_MESSAGE.GET;
15811       ELSE
15812         l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_RELS_WITH_RCV_TRX');
15813       END IF;
15814 
15815       INSERT INTO PO_ONLINE_REPORT_TEXT_GT
15816       (  online_report_id
15817       ,  last_update_login
15818       ,  last_updated_by
15819       ,  last_update_date
15820       ,  created_by
15821       ,  creation_date
15822       ,  line_num
15823       ,  shipment_num
15824       ,  distribution_num
15825       ,  sequence
15826       ,  text_line
15827       ,  message_name
15828       )
15829       SELECT
15830          p_online_report_id
15831       ,  p_login_id
15832       ,  p_user_id
15833       ,  SYSDATE
15834       ,  p_user_id
15835       ,  SYSDATE
15836       ,  DECODE(p_document_type, g_document_type_RELEASE, 0, pol.line_num)
15837       ,  poll.shipment_num
15838       ,  0
15839       ,  p_sequence + ROWNUM
15840       ,  DECODE(p_document_type,
15841            g_document_type_RELEASE, substr(g_shipmsg || g_delim || poll.shipment_num
15842                                             || g_delim || l_textline, 1, 240),
15843            substr(g_linemsg || g_delim || pol.line_num || g_delim || g_shipmsg || g_delim
15844                   || poll.shipment_num || g_delim || l_textline, 1, 240)
15845          )
15846       ,  DECODE(p_document_type, g_document_type_RELEASE, 'PO_CAN_RELS_WITH_RCV_TRX',
15847                  'PO_CAN_POLL_WITH_RCV_TRX')
15848       FROM po_lines_gt pol, po_line_locations_gt poll
15849       WHERE pol.po_line_id = poll.po_line_id
15850         AND NVL(poll.cancel_flag, 'N') = 'N'
15851         AND NVL(poll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
15852         AND EXISTS
15853         (
15854           SELECT 'Transaction to process'
15855           FROM rcv_transactions_interface rti
15856           WHERE rti.processing_status_code = 'PENDING'
15857             AND rti.po_line_location_id = poll.line_location_id
15858         );
15859 
15860       d_progress := 60;
15861 
15862     ELSE
15863 
15864       d_progress := 70;
15865       d_msg := 'Bad document level';
15866       l_ret_sts := FND_API.G_RET_STS_UNEXP_ERROR;
15867       RAISE PO_CORE_S.g_early_return_exc;
15868 
15869     END IF;  -- if p_document_level = ...
15870 
15871     p_sequence := p_sequence + SQL%ROWCOUNT;
15872     l_ret_sts := FND_API.G_RET_STS_SUCCESS;
15873 
15874   EXCEPTION
15875     WHEN PO_CORE_S.g_early_return_exc THEN
15876       IF (PO_LOG.d_exc) THEN
15877         PO_LOG.exc(d_module, d_progress, d_msg);
15878       END IF;
15879   END;
15880 
15881   x_return_status := l_ret_sts;
15882 
15883   IF (PO_LOG.d_proc) THEN
15884     PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
15885     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
15886     PO_LOG.proc_end(d_module);
15887   END IF;
15888 
15889   RETURN;
15890 
15891 EXCEPTION
15892   WHEN others THEN
15893     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
15894 
15895     IF (PO_LOG.d_exc) THEN
15896       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
15897       PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
15898       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
15899       PO_LOG.proc_end(d_module);
15900     END IF;
15901 
15902     RETURN;
15903 
15904 END check_rcv_trans_interface;
15905 
15906 -- Deterimine if there are any ASN that have not been fully received
15907 PROCEDURE check_asn_not_fully_received(
15908    p_document_type        IN VARCHAR2
15909 ,  p_online_report_id     IN NUMBER
15910 ,  p_user_id              IN NUMBER
15911 ,  p_login_id             IN NUMBER
15912 ,  p_sequence             IN OUT NOCOPY NUMBER
15913 ,  x_return_status        OUT NOCOPY VARCHAR2
15914 )
15915 IS
15916 
15917 l_text_normal_po  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
15918 l_text_complex_po  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
15919 
15920 d_module   VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.check_asn_not_fully_received';
15921 d_progress NUMBER;
15922 
15923 
15924 BEGIN
15925 
15926   d_progress := 0;
15927   IF (PO_LOG.d_proc) THEN
15928     PO_LOG.proc_begin(d_module);
15929     PO_LOG.proc_begin(d_module, 'p_document_type', p_document_type);
15930     PO_LOG.proc_begin(d_module, 'p_online_report_id', p_online_report_id);
15931     PO_LOG.proc_begin(d_module, 'p_user_id', p_user_id);
15932     PO_LOG.proc_begin(d_module, 'p_login_id', p_login_id);
15933     PO_LOG.proc_begin(d_module, 'p_sequence', p_sequence);
15934   END IF;
15935 
15936   d_progress := 10;
15937 
15938   l_text_normal_po := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_POLL_WITH_ASN');
15939   l_text_complex_po := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_POLL_WITH_PENDING_WCR');
15940 
15941   --<Complex Work R12>: change message to use Work Confirmation instead of ASN for Complex Work
15942   INSERT INTO PO_ONLINE_REPORT_TEXT_GT
15943   (  online_report_id
15944   ,  last_update_login
15945   ,  last_updated_by
15946   ,  last_update_date
15947   ,  created_by
15948   ,  creation_date
15949   ,  line_num
15950   ,  shipment_num
15951   ,  distribution_num
15952   ,  sequence
15953   ,  text_line
15954   ,  message_name
15955   )
15956   SELECT
15957      p_online_report_id
15958   ,  p_login_id
15959   ,  p_user_id
15960   ,  SYSDATE
15961   ,  p_user_id
15962   ,  SYSDATE
15963   ,  DECODE(p_document_type, g_document_type_RELEASE, 0, pol.line_num)
15964   ,  poll.shipment_num
15965   ,  0
15966   ,  p_sequence + ROWNUM
15967   ,  DECODE(p_document_type, g_document_type_RELEASE,
15968               substr(g_shipmsg || g_delim || poll.shipment_num || g_delim || l_text_normal_po, 1, 240),
15969               substr(g_linemsg || g_delim || pol.line_num || g_delim || g_shipmsg || g_delim
15970                      || poll.shipment_num || g_delim ||
15971                      DECODE(poll.payment_type , NULL, l_text_normal_po, l_text_complex_po), 1, 240))
15972   ,  DECODE(poll.payment_type, NULL, 'PO_CAN_POLL_WITH_ASN', 'PO_CAN_POLL_WITH_PENDING_WCR')
15973   FROM po_lines_gt pol, po_line_locations_gt poll
15974   WHERE NVL(poll.cancel_flag, 'N') = 'N'
15975     AND NVL(poll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
15976     AND EXISTS
15977      (
15978        SELECT 'ASN outstanding'
15979        FROM rcv_shipment_lines rsl
15980        WHERE rsl.po_line_location_id = poll.line_location_id
15981          AND NVL(rsl.quantity_shipped, 0) > NVL(rsl.quantity_received, 0)
15982          AND NVL(rsl.asn_line_flag, 'N') = 'Y'
15983          AND NVL(rsl.shipment_line_status_code, 'EXPECTED') <> 'CANCELLED'
15984      );
15985 
15986   d_progress := 20;
15987 
15988   p_sequence := p_sequence + SQL%ROWCOUNT;
15989   x_return_status := FND_API.G_RET_STS_SUCCESS;
15990 
15991   IF (PO_LOG.d_proc) THEN
15992     PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
15993     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
15994     PO_LOG.proc_end(d_module);
15995   END IF;
15996 
15997   RETURN;
15998 
15999 EXCEPTION
16000   WHEN others THEN
16001     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
16002 
16003     IF (PO_LOG.d_exc) THEN
16004       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
16005       PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
16006       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
16007       PO_LOG.proc_end(d_module);
16008     END IF;
16009 
16010     RETURN;
16011 
16012 END check_asn_not_fully_received;
16013 
16014 -- Determine if there is any shipment being finally closed that
16015 -- has received more than has been delivered to its distributions.
16016 -- Does not check ('RATE', 'FIXED PRICE') lines
16017 PROCEDURE check_qty_rcv_but_not_deliv(
16018    p_document_type        IN VARCHAR2
16019 ,  p_online_report_id     IN NUMBER
16020 ,  p_user_id              IN NUMBER
16021 ,  p_login_id             IN NUMBER
16022 ,  p_sequence             IN OUT NOCOPY NUMBER
16023 ,  x_return_status        OUT NOCOPY VARCHAR2
16024 )
16025 IS
16026 
16027 l_textline  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
16028 
16029 d_module   VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.check_qty_rcv_but_not_deliv';
16030 d_progress NUMBER;
16031 
16032 
16033 BEGIN
16034 
16035   d_progress := 0;
16036   IF (PO_LOG.d_proc) THEN
16037     PO_LOG.proc_begin(d_module);
16038     PO_LOG.proc_begin(d_module, 'p_document_type', p_document_type);
16039     PO_LOG.proc_begin(d_module, 'p_online_report_id', p_online_report_id);
16040     PO_LOG.proc_begin(d_module, 'p_user_id', p_user_id);
16041     PO_LOG.proc_begin(d_module, 'p_login_id', p_login_id);
16042     PO_LOG.proc_begin(d_module, 'p_sequence', p_sequence);
16043   END IF;
16044 
16045   d_progress := 10;
16046 
16047   INSERT INTO PO_ONLINE_REPORT_TEXT_GT
16048   (  online_report_id
16049   ,  last_update_login
16050   ,  last_updated_by
16051   ,  last_update_date
16052   ,  created_by
16053   ,  creation_date
16054   ,  line_num
16055   ,  shipment_num
16056   ,  distribution_num
16057   ,  sequence
16058   ,  text_line
16059   ,  message_name
16060   )
16061   SELECT
16062      p_online_report_id
16063   ,  p_login_id
16064   ,  p_user_id
16065   ,  SYSDATE
16066   ,  p_user_id
16067   ,  SYSDATE
16068   ,  DECODE(p_document_type, g_document_type_RELEASE, 0, pol.line_num)
16069   ,  poll.shipment_num
16070   ,  0
16071   ,  p_sequence + ROWNUM
16072   ,  substr(
16073        DECODE(p_document_type, g_document_type_RELEASE,
16074          g_shipmsg || g_delim || poll.shipment_num || g_delim
16075            || PO_CORE_S.get_translated_text('PO_CAN_POLL_REC_NOT_DEL'
16076                                             , 'QTY2', NVL(poll.quantity_received, 0)
16077                                             , 'QTY1', sum(NVL(pod.quantity_delivered, 0))
16078                                             ),
16079          g_linemsg || g_delim || pol.line_num || g_delim || g_shipmsg || g_delim
16080            || poll.shipment_num || g_delim
16081            || PO_CORE_S.get_translated_text('PO_CAN_POLL_REC_NOT_DEL'
16082                                             , 'QTY2', NVL(poll.quantity_received, 0)
16083                                             , 'QTY1', sum(NVL(pod.quantity_delivered, 0))
16084                                             )
16085        ), 1, 240)
16086   ,  'PO_CAN_POLL_REC_NOT_DEL'
16087   FROM po_lines_gt pol, po_line_locations_gt poll, po_distributions_gt pod
16088   WHERE pod.line_location_id = poll.line_location_id
16089     AND pol.po_line_id = poll.po_line_id
16090     AND pol.order_type_lookup_code NOT IN ('RATE', 'FIXED PRICE')
16091     AND NVL(poll.cancel_flag, 'N') = 'N'
16092     AND NVL(poll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
16093     AND NVL(poll.quantity_received, 0) >
16094          (
16095            SELECT sum(NVL(pod2.quantity_delivered, 0))
16096            FROM po_distributions_gt pod2
16097            WHERE pod2.line_location_id = poll.line_location_id
16098          )
16099   GROUP BY pol.line_num
16100          , poll.shipment_num
16101          , NVL(poll.quantity_received, 0)
16102          , ROWNUM  -- <Bug 4118145, Issue 8>
16103   ;
16104 
16105   d_progress := 20;
16106 
16107   p_sequence := p_sequence + SQL%ROWCOUNT;
16108   x_return_status := FND_API.G_RET_STS_SUCCESS;
16109 
16110   IF (PO_LOG.d_proc) THEN
16111     PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
16112     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
16113     PO_LOG.proc_end(d_module);
16114   END IF;
16115 
16116   RETURN;
16117 
16118 EXCEPTION
16119   WHEN others THEN
16120     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
16121 
16122     IF (PO_LOG.d_exc) THEN
16123       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
16124       PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
16125       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
16126       PO_LOG.proc_end(d_module);
16127     END IF;
16128 
16129     RETURN;
16130 
16131 END check_qty_rcv_but_not_deliv;
16132 
16133 -- Determine if there is any shipment being finally closed that
16134 -- has received more than has been delivered to its distributions.
16135 -- For ('RATE', 'FIXED PRICE') lines only
16136 PROCEDURE check_amt_rcv_but_not_deliv(
16137    p_document_type        IN VARCHAR2
16138 ,  p_online_report_id     IN NUMBER
16139 ,  p_user_id              IN NUMBER
16140 ,  p_login_id             IN NUMBER
16141 ,  p_sequence             IN OUT NOCOPY NUMBER
16142 ,  x_return_status        OUT NOCOPY VARCHAR2
16143 )
16144 IS
16145 
16146 l_textline  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
16147 
16148 d_module   VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.check_amt_rcv_but_not_deliv';
16149 d_progress NUMBER;
16150 
16151 
16152 BEGIN
16153 
16154   d_progress := 0;
16155   IF (PO_LOG.d_proc) THEN
16156     PO_LOG.proc_begin(d_module);
16157     PO_LOG.proc_begin(d_module, 'p_document_type', p_document_type);
16158     PO_LOG.proc_begin(d_module, 'p_online_report_id', p_online_report_id);
16159     PO_LOG.proc_begin(d_module, 'p_user_id', p_user_id);
16160     PO_LOG.proc_begin(d_module, 'p_login_id', p_login_id);
16161     PO_LOG.proc_begin(d_module, 'p_sequence', p_sequence);
16162   END IF;
16163 
16164   d_progress := 10;
16165 
16166   -- <Bug 4118145, Issue 8 Start>
16167   -- In query below, changed quantity columns to amount columns and also
16168   -- group by ROWNUM so that it can be used in the select clause
16169 
16170   INSERT INTO PO_ONLINE_REPORT_TEXT_GT
16171   (  online_report_id
16172   ,  last_update_login
16173   ,  last_updated_by
16174   ,  last_update_date
16175   ,  created_by
16176   ,  creation_date
16177   ,  line_num
16178   ,  shipment_num
16179   ,  distribution_num
16180   ,  sequence
16181   ,  text_line
16182   ,  message_name
16183   )
16184   SELECT
16185      p_online_report_id
16186   ,  p_login_id
16187   ,  p_user_id
16188   ,  SYSDATE
16189   ,  p_user_id
16190   ,  SYSDATE
16191   ,  DECODE(p_document_type, g_document_type_RELEASE, 0, pol.line_num)
16192   ,  poll.shipment_num
16193   ,  0
16194   ,  p_sequence + ROWNUM
16195   ,  substr(
16196        DECODE(p_document_type, g_document_type_RELEASE,
16197          g_shipmsg || g_delim || poll.shipment_num || g_delim
16198            || PO_CORE_S.get_translated_text('PO_CAN_POLL_AMT_REC_NOT_DEL'
16199                                             , 'QTY2', NVL(poll.amount_received, 0)
16200                                             , 'QTY1', sum(NVL(pod.amount_delivered, 0))
16201                                             ),
16202          g_linemsg || g_delim || pol.line_num || g_delim || g_shipmsg || g_delim
16203            || poll.shipment_num || g_delim
16204            || PO_CORE_S.get_translated_text('PO_CAN_POLL_AMT_REC_NOT_DEL'
16205                                             , 'QTY2', NVL(poll.amount_received, 0)
16206                                             , 'QTY1', sum(NVL(pod.amount_delivered, 0))
16207                                             )
16208        ), 1, 240)
16209   ,  'PO_CAN_POLL_AMT_REC_NOT_DEL'
16210   FROM po_lines_gt pol, po_line_locations_gt poll, po_distributions_gt pod
16211   WHERE pod.line_location_id = poll.line_location_id
16212     AND pol.po_line_id = poll.po_line_id
16213     AND pol.order_type_lookup_code IN ('RATE', 'FIXED PRICE')
16214     AND NVL(poll.cancel_flag, 'N') = 'N'
16215     AND NVL(poll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
16216     AND NVL(poll.amount_received, 0) >
16217          (
16218            SELECT sum(NVL(pod2.amount_delivered, 0))
16219            FROM po_distributions_gt pod2
16220            WHERE pod2.line_location_id = poll.line_location_id
16221          )
16222   GROUP BY pol.line_num
16223          , poll.shipment_num
16224          , NVL(poll.amount_received, 0)
16225          , ROWNUM   -- <Bug 4118145, Issue 8>
16226   ;
16227 
16228   -- <Bug 4118145, Issue 8 End>
16229 
16230   d_progress := 20;
16231 
16232   p_sequence := p_sequence + SQL%ROWCOUNT;
16233   x_return_status := FND_API.G_RET_STS_SUCCESS;
16234 
16235   IF (PO_LOG.d_proc) THEN
16236     PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
16237     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
16238     PO_LOG.proc_end(d_module);
16239   END IF;
16240 
16241   RETURN;
16242 
16243 EXCEPTION
16244   WHEN others THEN
16245     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
16246 
16247     IF (PO_LOG.d_exc) THEN
16248       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
16249       PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
16250       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
16251       PO_LOG.proc_end(d_module);
16252     END IF;
16253 
16254     RETURN;
16255 
16256 END check_amt_rcv_but_not_deliv;
16257 
16258 -- Determine if there are any invalid accounting flexfields
16259 PROCEDURE check_invalid_acct_flex(
16260    p_document_type        IN VARCHAR2
16261 ,  p_action_requested     IN VARCHAR2
16262 ,  p_action_date          IN DATE
16263 ,  p_online_report_id     IN NUMBER
16264 ,  p_user_id              IN NUMBER
16265 ,  p_login_id             IN NUMBER
16266 ,  p_document_id          IN NUMBER
16267 ,  p_sequence             IN OUT NOCOPY NUMBER
16268 ,  x_return_status        OUT NOCOPY VARCHAR2
16269 )
16270 IS
16271 
16272 l_textline  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
16273 l_is_complex_po     boolean;
16274 l_token_value VARCHAR2(256);
16275 
16276 d_module   VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.check_invalid_acct_flex';
16277 d_progress NUMBER;
16278 
16279 
16280 BEGIN
16281 
16282   d_progress := 0;
16283   IF (PO_LOG.d_proc) THEN
16284     PO_LOG.proc_begin(d_module);
16285     PO_LOG.proc_begin(d_module, 'p_document_type', p_document_type);
16286     PO_LOG.proc_begin(d_module, 'p_action_date', p_action_date);
16287     PO_LOG.proc_begin(d_module, 'p_action_requested', p_action_requested);
16288     PO_LOG.proc_begin(d_module, 'p_online_report_id', p_online_report_id);
16289     PO_LOG.proc_begin(d_module, 'p_user_id', p_user_id);
16290     PO_LOG.proc_begin(d_module, 'p_login_id', p_login_id);
16291     PO_LOG.proc_begin(d_module, 'p_sequence', p_sequence);
16292   END IF;
16293 
16294   d_progress := 10;
16295 
16296   --Bug5072310
16297   --Added Condition to filter releaes before calling complex work API
16298   if p_document_type <> g_document_type_RELEASE  then
16299      l_is_complex_po := PO_COMPLEX_WORK_PVT.is_complex_work_po(p_document_id);
16300   end if;
16301 
16302   IF (l_is_complex_po) THEN
16303     l_token_value := FND_MESSAGE.GET_STRING('PO', 'PO_LINE_LOC_TYPE_LOW_P_PAYITEM');
16304   ELSE
16305     l_token_value := FND_MESSAGE.GET_STRING('PO', 'PO_LINE_LOC_TYPE_LOWER_S_SCH');
16306   END IF;
16307 
16308   FND_MESSAGE.SET_NAME('PO','PO_CAN_POLL_INVALID_ACCT_FLEX');
16309   FND_MESSAGE.SET_TOKEN('LINE_LOCATION_TYPE', l_token_value);
16310 
16311   l_textline := FND_MESSAGE.GET;
16312 
16313   INSERT INTO PO_ONLINE_REPORT_TEXT_GT
16314   (  online_report_id
16315   ,  last_update_login
16316   ,  last_updated_by
16317   ,  last_update_date
16318   ,  created_by
16319   ,  creation_date
16320   ,  line_num
16321   ,  shipment_num
16322   ,  distribution_num
16323   ,  sequence
16324   ,  text_line
16325   ,  message_name
16326   )
16327   SELECT
16328      p_online_report_id
16329   ,  p_login_id
16330   ,  p_user_id
16331   ,  SYSDATE
16332   ,  p_user_id
16333   ,  SYSDATE
16334   ,  DECODE(p_document_type, g_document_type_RELEASE, 0, pol.line_num)
16335   ,  poll.shipment_num
16336   ,  pod.distribution_num
16337   ,  p_sequence + ROWNUM
16338   ,  substr(
16339        DECODE(p_document_type, g_document_type_RELEASE,
16340            g_shipmsg || g_delim || poll.shipment_num || g_delim || g_distmsg || g_delim || l_textline
16341          , g_linemsg || g_delim || pol.line_num || g_delim || g_shipmsg || g_delim
16342              || poll.shipment_num || g_delim || g_distmsg || g_delim || l_textline
16343        ), 1, 240)
16344   ,  'PO_CAN_POLL_INVALID_ACCT_FLEX'
16345   FROM po_lines_gt pol, po_line_locations_gt poll
16346      , po_distributions_gt pod, gl_code_combinations gcc
16347   WHERE pod.line_location_id = poll.line_location_id
16348     AND pol.po_line_id = poll.po_line_id
16349     AND poll.shipment_type IN ('STANDARD', 'PLANNED', 'PREPAYMENT') --<Complex Work R12>
16350     AND NVL(poll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
16351     AND ( NVL(poll.cancel_flag, 'N') = 'N' OR p_action_requested = g_action_FINAL_CLOSE_CHECK)
16352     AND gcc.code_combination_id = pod.code_combination_id
16353     AND NVL(p_action_date, trunc(SYSDATE)) NOT BETWEEN
16354           NVL(gcc.start_date_active, NVL(p_action_date, trunc(SYSDATE) - 1))
16355             AND
16356           NVL(gcc.end_date_active, NVL(p_action_date, trunc(SYSDATE) + 1));
16357 
16358   d_progress := 20;
16359 
16360   p_sequence := p_sequence + SQL%ROWCOUNT;
16361   x_return_status := FND_API.G_RET_STS_SUCCESS;
16362 
16363   IF (PO_LOG.d_proc) THEN
16364     PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
16365     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
16366     PO_LOG.proc_end(d_module);
16367   END IF;
16368 
16369   RETURN;
16370 
16371 EXCEPTION
16372   WHEN others THEN
16373     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
16374 
16375     IF (PO_LOG.d_exc) THEN
16376       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
16377       PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
16378       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
16379       PO_LOG.proc_end(d_module);
16380     END IF;
16381 
16382     RETURN;
16383 
16384 END check_invalid_acct_flex;
16385 
16386 -- Deterimine if a BPA has any open releases against it
16387 PROCEDURE check_bpa_has_open_release(
16388    p_online_report_id     IN NUMBER
16389 ,  p_user_id              IN NUMBER
16390 ,  p_login_id             IN NUMBER
16391 ,  p_sequence             IN OUT NOCOPY NUMBER
16392 ,  x_return_status        OUT NOCOPY VARCHAR2
16393 )
16394 IS
16395 
16396 l_textline  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
16397 
16398 d_module   VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.check_bpa_has_open_release';
16399 d_progress NUMBER;
16400 
16401 
16402 BEGIN
16403 
16404   d_progress := 0;
16405   IF (PO_LOG.d_proc) THEN
16406     PO_LOG.proc_begin(d_module);
16407     PO_LOG.proc_begin(d_module, 'p_online_report_id', p_online_report_id);
16408     PO_LOG.proc_begin(d_module, 'p_user_id', p_user_id);
16409     PO_LOG.proc_begin(d_module, 'p_login_id', p_login_id);
16410     PO_LOG.proc_begin(d_module, 'p_sequence', p_sequence);
16411   END IF;
16412 
16413   d_progress := 10;
16414 
16415   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_POL_PLAN_WITH_OPEN_REL');
16416 
16417   INSERT INTO PO_ONLINE_REPORT_TEXT_GT
16418   (  online_report_id
16419   ,  last_update_login
16420   ,  last_updated_by
16421   ,  last_update_date
16422   ,  created_by
16423   ,  creation_date
16424   ,  line_num
16425   ,  shipment_num
16426   ,  distribution_num
16427   ,  sequence
16428   ,  text_line
16429   ,  message_name
16430   )
16431   SELECT
16432      p_online_report_id
16433   ,  p_login_id
16434   ,  p_user_id
16435   ,  SYSDATE
16436   ,  p_user_id
16437   ,  SYSDATE
16438   ,  pol.line_num
16439   ,  0
16440   ,  0
16441   ,  p_sequence + ROWNUM
16442   ,  substr(g_linemsg || g_delim || pol.line_num || g_delim || l_textline,1,240)  --Bug5096900
16443   ,  'PO_CAN_POL_PLAN_WITH_OPEN_REL'
16444   FROM po_lines_gt pol
16445   WHERE EXISTS
16446      (
16447        SELECT 'Uncancelled Open Releases Exist'
16448        FROM po_line_locations pll
16449        WHERE pll.po_line_id = pol.po_line_id
16450          AND pll.shipment_type = 'BLANKET'
16451          AND NVL(pll.cancel_flag, 'N') = 'N'
16452          AND NVL(pll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
16453      );
16454 
16455   d_progress := 20;
16456 
16457   p_sequence := p_sequence + SQL%ROWCOUNT;
16458   x_return_status := FND_API.G_RET_STS_SUCCESS;
16459 
16460   IF (PO_LOG.d_proc) THEN
16461     PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
16462     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
16463     PO_LOG.proc_end(d_module);
16464   END IF;
16465 
16466   RETURN;
16467 
16468 EXCEPTION
16469   WHEN others THEN
16470     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
16471 
16472     IF (PO_LOG.d_exc) THEN
16473       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
16474       PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
16475       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
16476       PO_LOG.proc_end(d_module);
16477     END IF;
16478 
16479     RETURN;
16480 
16481 END check_bpa_has_open_release;
16482 
16483 -- Deterimine if a GA has any open Std. POs against it
16484 PROCEDURE check_bpa_has_open_stdref(
16485    p_online_report_id     IN NUMBER
16486 ,  p_user_id              IN NUMBER
16487 ,  p_login_id             IN NUMBER
16488 ,  p_sequence             IN OUT NOCOPY NUMBER
16489 ,  x_return_status        OUT NOCOPY VARCHAR2
16490 )
16491 IS
16492 
16493 l_textline  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
16494 
16495 d_module   VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.check_bpa_has_open_stdref';
16496 d_progress NUMBER;
16497 
16498 
16499 BEGIN
16500 
16501   d_progress := 0;
16502   IF (PO_LOG.d_proc) THEN
16503     PO_LOG.proc_begin(d_module);
16504     PO_LOG.proc_begin(d_module, 'p_online_report_id', p_online_report_id);
16505     PO_LOG.proc_begin(d_module, 'p_user_id', p_user_id);
16506     PO_LOG.proc_begin(d_module, 'p_login_id', p_login_id);
16507     PO_LOG.proc_begin(d_module, 'p_sequence', p_sequence);
16508   END IF;
16509 
16510   d_progress := 10;
16511 
16512   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_GAL_WITH_OPEN_STD_REF');
16513 
16514   INSERT INTO PO_ONLINE_REPORT_TEXT_GT
16515   (  online_report_id
16516   ,  last_update_login
16517   ,  last_updated_by
16518   ,  last_update_date
16519   ,  created_by
16520   ,  creation_date
16521   ,  line_num
16522   ,  shipment_num
16523   ,  distribution_num
16524   ,  sequence
16525   ,  text_line
16526   ,  message_name
16527   )
16528   SELECT
16529      p_online_report_id
16530   ,  p_login_id
16531   ,  p_user_id
16532   ,  SYSDATE
16533   ,  p_user_id
16534   ,  SYSDATE
16535   ,  pol.line_num
16536   ,  0
16537   ,  0
16538   ,  p_sequence + ROWNUM
16539   ,  substr(g_linemsg || g_delim || pol.line_num || g_delim || l_textline,1,240)   --Bug5096900
16540   ,  'PO_CAN_GAL_WITH_OPEN_STD_REF'
16541   FROM po_lines_gt pol
16542   WHERE EXISTS
16543      (
16544        SELECT 'Uncancelled Std PO lines referencing this GA line exist'
16545        FROM po_lines_all pol2
16546        WHERE pol2.from_line_id = pol.po_line_id
16547          AND NVL(pol2.cancel_flag, 'N') = 'N'
16548          AND NVL(pol2.closed_code, 'OPEN') <> 'FINALLY CLOSED'
16549      );
16550 
16551   d_progress := 20;
16552 
16553   p_sequence := p_sequence + SQL%ROWCOUNT;
16554   x_return_status := FND_API.G_RET_STS_SUCCESS;
16555 
16556   IF (PO_LOG.d_proc) THEN
16557     PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
16558     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
16559     PO_LOG.proc_end(d_module);
16560   END IF;
16561 
16562   RETURN;
16563 
16564 EXCEPTION
16565   WHEN others THEN
16566     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
16567 
16568     IF (PO_LOG.d_exc) THEN
16569       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
16570       PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
16571       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
16572       PO_LOG.proc_end(d_module);
16573     END IF;
16574 
16575     RETURN;
16576 
16577 END check_bpa_has_open_stdref;
16578 
16579 -- Determine if a GC has any open Std. POs against it
16580 PROCEDURE check_cpa_has_open_stdref(
16581    p_online_report_id     IN NUMBER
16582 ,  p_user_id              IN NUMBER
16583 ,  p_login_id             IN NUMBER
16584 ,  p_sequence             IN OUT NOCOPY NUMBER
16585 ,  x_return_status        OUT NOCOPY VARCHAR2
16586 )
16587 IS
16588 
16589 l_textline  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
16590 
16591 d_module   VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.check_cpa_has_open_stdref';
16592 d_progress NUMBER;
16593 
16594 
16595 BEGIN
16596 
16597   d_progress := 0;
16598   IF (PO_LOG.d_proc) THEN
16599     PO_LOG.proc_begin(d_module);
16600     PO_LOG.proc_begin(d_module, 'p_online_report_id', p_online_report_id);
16601     PO_LOG.proc_begin(d_module, 'p_user_id', p_user_id);
16602     PO_LOG.proc_begin(d_module, 'p_login_id', p_login_id);
16603     PO_LOG.proc_begin(d_module, 'p_sequence', p_sequence);
16604   END IF;
16605 
16606   d_progress := 10;
16607 
16608   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_CGA_WITH_OPEN_STD_REF');
16609 
16610   INSERT INTO PO_ONLINE_REPORT_TEXT_GT
16611   (  online_report_id
16612   ,  last_update_login
16613   ,  last_updated_by
16614   ,  last_update_date
16615   ,  created_by
16616   ,  creation_date
16617   ,  line_num
16618   ,  shipment_num
16619   ,  distribution_num
16620   ,  sequence
16621   ,  text_line
16622   ,  message_name
16623   )
16624   SELECT
16625      p_online_report_id
16626   ,  p_login_id
16627   ,  p_user_id
16628   ,  SYSDATE
16629   ,  p_user_id
16630   ,  SYSDATE
16631   ,  0
16632   ,  0
16633   ,  0
16634   ,  p_sequence + ROWNUM
16635   ,  substr(l_textline,1,240)      --Bug5096900
16636   ,  'PO_CAN_CGA_WITH_OPEN_STD_REF'
16637   FROM po_headers_gt poh
16638   WHERE EXISTS
16639      (
16640        SELECT 'Open Std PO lines referencing this contract exist'
16641        FROM po_lines_all pol
16642        WHERE pol.contract_id = poh.po_header_id
16643          AND NVL(pol.cancel_flag, 'N') = 'N'
16644          AND NVL(pol.closed_code, 'OPEN') <> 'FINALLY CLOSED'
16645      );
16646 
16647   d_progress := 20;
16648 
16649   p_sequence := p_sequence + SQL%ROWCOUNT;
16650   x_return_status := FND_API.G_RET_STS_SUCCESS;
16651 
16652   IF (PO_LOG.d_proc) THEN
16653     PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
16654     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
16655     PO_LOG.proc_end(d_module);
16656   END IF;
16657 
16658   RETURN;
16659 
16660 EXCEPTION
16661   WHEN others THEN
16662     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
16663 
16664     IF (PO_LOG.d_exc) THEN
16665       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
16666       PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
16667       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
16668       PO_LOG.proc_end(d_module);
16669     END IF;
16670 
16671     RETURN;
16672 
16673 END check_cpa_has_open_stdref;
16674 
16675 -- Determine if a PPO has any open releases against it
16676 PROCEDURE check_ppo_has_open_release(
16677    p_online_report_id     IN NUMBER
16678 ,  p_user_id              IN NUMBER
16679 ,  p_login_id             IN NUMBER
16680 ,  p_sequence             IN OUT NOCOPY NUMBER
16681 ,  x_return_status        OUT NOCOPY VARCHAR2
16682 )
16683 IS
16684 
16685 l_textline  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
16686 
16687 d_module   VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.check_ppo_has_open_release';
16688 d_progress NUMBER;
16689 
16690 
16691 BEGIN
16692 
16693   d_progress := 0;
16694   IF (PO_LOG.d_proc) THEN
16695     PO_LOG.proc_begin(d_module);
16696     PO_LOG.proc_begin(d_module, 'p_online_report_id', p_online_report_id);
16697     PO_LOG.proc_begin(d_module, 'p_user_id', p_user_id);
16698     PO_LOG.proc_begin(d_module, 'p_login_id', p_login_id);
16699     PO_LOG.proc_begin(d_module, 'p_sequence', p_sequence);
16700   END IF;
16701 
16702   d_progress := 10;
16703 
16704   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_POLL_PLAN_WITH_OPEN_REL');
16705 
16706   INSERT INTO PO_ONLINE_REPORT_TEXT_GT
16707   (  online_report_id
16708   ,  last_update_login
16709   ,  last_updated_by
16710   ,  last_update_date
16711   ,  created_by
16712   ,  creation_date
16713   ,  line_num
16714   ,  shipment_num
16715   ,  distribution_num
16716   ,  sequence
16717   ,  text_line
16718   ,  message_name
16719   )
16720   SELECT
16721      p_online_report_id
16722   ,  p_login_id
16723   ,  p_user_id
16724   ,  SYSDATE
16725   ,  p_user_id
16726   ,  SYSDATE
16727   ,  pol.line_num
16728   ,  poll.shipment_num
16729   ,  0
16730   ,  p_sequence + ROWNUM
16731   ,  substr(g_linemsg || g_delim || pol.line_num || g_delim || g_shipmsg
16732               || poll.shipment_num || g_delim || l_textline,1,240)   --Bug5096900
16733   ,  'PO_CAN_POLL_PLAN_WITH_OPEN_REL'
16734   FROM po_lines_gt pol, po_line_locations_gt poll
16735   WHERE poll.po_line_id = pol.po_line_id
16736     AND NVL(poll.cancel_flag, 'N') = 'N'
16737     AND NVL(poll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
16738     AND EXISTS
16739      (
16740        SELECT 'Uncancelled Open Releases Exist'
16741        FROM po_line_locations poll2
16742        WHERE poll2.source_shipment_id = poll.line_location_id
16743          AND poll2.shipment_type = 'SCHEDULED'
16744          AND NVL(poll2.cancel_flag, 'N') = 'N'
16745          AND NVL(poll2.closed_code, 'OPEN') <> 'FINALLY CLOSED'
16746      );
16747 
16748   d_progress := 20;
16749 
16750   p_sequence := p_sequence + SQL%ROWCOUNT;
16751   x_return_status := FND_API.G_RET_STS_SUCCESS;
16752 
16753   IF (PO_LOG.d_proc) THEN
16754     PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
16755     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
16756     PO_LOG.proc_end(d_module);
16757   END IF;
16758 
16759   RETURN;
16760 
16761 EXCEPTION
16762   WHEN others THEN
16763     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
16764 
16765     IF (PO_LOG.d_exc) THEN
16766       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
16767       PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
16768       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
16769       PO_LOG.proc_end(d_module);
16770     END IF;
16771 
16772     RETURN;
16773 
16774 END check_ppo_has_open_release;
16775 -- <Doc Manager Rewrite 11.5.11 End>
16776 
16777 
16778 --<Complex Work R12 START>
16779 -- PO Quantity/Amount Rollup Checks
16780 PROCEDURE check_po_qty_amt_rollup(
16781    p_online_report_id     IN NUMBER
16782 ,  p_document_id          IN NUMBER
16783 ,  p_login_id             IN NUMBER
16784 ,  p_user_id              IN NUMBER
16785 ,  x_sequence             IN OUT NOCOPY NUMBER
16786 )
16787 IS
16788   l_api_name CONSTANT VARCHAR2(40) := 'CHECK_PO_QTY_AMT_ROLLUP';
16789   l_progress VARCHAR2(3);
16790   l_is_complex_po     boolean;
16791   l_is_financing_flag VARCHAR2(1);
16792   TYPE NumTab is TABLE of NUMBER INDEX by BINARY_INTEGER;
16793   l_rowcount NumTab;
16794   l_line_num        NumTab;
16795   l_shipment_num    NumTab;
16796   l_dist_num        NumTab;
16797   l_line_qty_tbl    NumTab;     -- <SERVICES FPJ>
16798   l_line_amt_tbl    NumTab;     -- <SERVICES FPJ>
16799   l_lineloc_qty_tbl NumTab;     -- <SERVICES FPJ>
16800   l_lineloc_amt_tbl NumTab;     -- <SERVICES FPJ>
16801   l_dist_qty_tbl    NumTab;     -- <SERVICES FPJ>
16802   l_dist_amt_tbl    NumTab;     -- <SERVICES FPJ>
16803   l_fin_adv_amount  NumTab;
16804   l_currency_code   VARCHAR2(15); -- <Complex Work R12>
16805   l_min_acct_unit   VARCHAR2(15); -- <Complex Work R12>
16806   l_precision       VARCHAR2(15); -- <Complex Work R12>
16807 BEGIN
16808   l_progress := '001';
16809 
16810   IF g_debug_stmt THEN
16811      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
16812        FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
16813                       || l_progress,'Start PO Qty/Amt Rollup Checks');
16814      END IF;
16815   END IF;
16816 
16817   -- Method Logic:
16818   -- If not a Complex Work PO, run the existing shipment level checks.
16819   -- For Complex Work PO, run separate pay item rollup check
16820   -- In both cases, run the existing distribution level checks.
16821 
16822   l_is_complex_po := PO_COMPLEX_WORK_PVT.is_complex_work_po(p_document_id);
16823 
16824   IF (NOT l_is_complex_po) THEN
16825 
16826     l_progress := '010';
16827     -- Check 1a: Quantities/Amounts between Purchase Order Line and Shipments
16828     -- must match (existing logic)
16829 
16830     IF g_debug_stmt THEN
16831        IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
16832          FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
16833                         || l_progress,'PO Shipment Qty/Amt Rollup');
16834        END IF;
16835     END IF;
16836 
16837     SELECT
16838         POL.line_num
16839     ,   POL.quantity
16840     ,   POL.amount                                            -- <SERVICES FPJ>
16841     ,   sum( PLL.quantity - nvl(PLL.quantity_cancelled,0) )   -- <SERVICES FPJ>
16842     ,   sum( PLL.amount - nvl(PLL.amount_cancelled,0) )       -- <SERVICES FPJ>
16843     BULK COLLECT INTO
16844         l_line_num
16845     ,   l_line_qty_tbl                                        -- <SERVICES FPJ>
16846     ,   l_line_amt_tbl                                        -- <SERVICES FPJ>
16847     ,   l_lineloc_qty_tbl                                     -- <SERVICES FPJ>
16848     ,   l_lineloc_amt_tbl                                     -- <SERVICES FPJ>
16849     FROM
16850         PO_LINE_LOCATIONS_GT PLL
16851     ,   PO_LINES_GT POL
16852     WHERE
16853         POL.po_line_id = PLL.po_line_id
16854     AND PLL.shipment_type in ('STANDARD', 'PLANNED')
16855     AND POL.po_header_id = p_document_id
16856     AND nvl(POL.cancel_flag,'N') = 'N'
16857     AND nvl(POL.closed_code,'OPEN') <> 'FINALLY CLOSED'
16858     AND (
16859             (   ( POL.quantity IS NOT NULL )                  -- <SERVICES FPJ>
16860             AND ( round(POL.quantity, 10) <>
16861                 (SELECT round(sum(PLL2.quantity) -
16862                               sum(nvl(PLL2.quantity_cancelled, 0)), 10)
16863                  FROM PO_LINE_LOCATIONS_GT PLL2
16864                  WHERE PLL2.po_line_id = POL.po_line_id AND
16865                        PLL2.shipment_type in ('STANDARD', 'PLANNED') ) )
16866             )
16867         OR                                                    -- <SERVICES FPJ>
16868             (   ( POL.amount IS NOT NULL )
16869             AND ( round(POL.amount, 10) <>
16870                   (   SELECT round ( sum ( PLL3.amount
16871                                          - nvl(PLL3.amount_cancelled, 0) )
16872                                    , 10
16873                                    )
16874                       FROM   po_line_locations_gt PLL3
16875                       WHERE  PLL3.po_line_id = POL.po_line_id
16876                       AND    PLL3.shipment_type IN ('STANDARD','PLANNED')
16877                   )
16878                 )
16879             )
16880         )
16881     GROUP BY
16882         POL.line_num
16883     ,   POL.quantity
16884     ,   POL.amount;                                           -- <SERVICES FPJ>
16885 
16886     l_progress := '015';
16887 
16888     FOR i IN 1..l_line_num.COUNT LOOP
16889         l_rowCount(i) := i;
16890     END LOOP;
16891 
16892     FORALL i IN 1..l_line_num.COUNT
16893         INSERT INTO po_online_report_text_gt(online_report_id,
16894         last_update_login,
16895         last_updated_by,
16896         last_update_date,
16897         created_by,
16898         creation_date,
16899         line_num,
16900         shipment_num,
16901         distribution_num,
16902         sequence,
16903         text_line,
16904                 message_name)
16905         VALUES(p_online_report_id,
16906             p_login_id,
16907             p_user_id,
16908             sysdate,
16909             p_user_id,
16910             sysdate,
16911             l_line_num(i),
16912             NULL,                                             -- <SERVICES FPJ>
16913             NULL,                                             -- <SERVICES FPJ>
16914             x_sequence+ l_rowCount(i),
16915             decode ( l_line_qty_tbl(i)                        -- <SERVICES FPJ>
16916                    , NULL , PO_CORE_S.get_translated_text
16917                             (   'PO_SUB_PO_LINE_NE_SHIP_AMT'
16918                             ,   'LINE_NUM', l_line_num(i)
16919                             ,   'LINE_AMT', l_line_amt_tbl(i)
16920                             ,   'SHIP_AMT', l_lineloc_amt_tbl(i)
16921                             )
16922                    ,        PO_CORE_S.get_translated_text
16923                             (   'PO_SUB_PO_LINE_NE_SHIP_QTY'
16924                             ,   'LINE_NUM', l_line_num(i)
16925                             ,   'LINE_QTY', l_line_qty_tbl(i)
16926                             ,   'SHIP_QTY', l_lineloc_qty_tbl(i)
16927                             )
16928                    ),
16929             decode ( l_line_qty_tbl(i)                        -- <SERVICES FPJ>
16930                    , NULL , 'PO_SUB_PO_LINE_NE_SHIP_AMT'
16931                    ,        'PO_SUB_PO_LINE_NE_SHIP_QTY'
16932                    )
16933           );
16934 
16935     l_progress := '017';
16936 
16937     --Increment the x_sequence with number of errors reported in last query
16938     x_sequence := x_sequence + SQL%ROWCOUNT;
16939 
16940   ELSE
16941     -- The document IS a complex work PO
16942     -- Check 1b: Amounts roll up between Purchase Order Line and Pay Items
16943 
16944     l_progress := '020';
16945 
16946     IF g_debug_stmt THEN
16947        IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
16948          FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
16949                         || l_progress,'PO Pay Item Qty/Amt Rollup');
16950        END IF;
16951     END IF;
16952 
16953     l_is_financing_flag := PO_CORE_S.boolean_to_flag(
16954                            PO_COMPLEX_WORK_PVT.is_financing_po(p_document_id));
16955 
16956     l_progress := '022';
16957 
16958     -- Get the currency code and precision
16959     SELECT poh.currency_code
16960     INTO   l_currency_code
16961     FROM   po_headers_all poh
16962     WHERE  poh.po_header_id = p_document_id;
16963 
16964     PO_CORE_S2.get_currency_info(
16965       x_currency_code => l_currency_code
16966     , x_min_unit      => l_min_acct_unit
16967     , x_precision     => l_precision);
16968 
16969     l_progress := '023';
16970 
16971    --Bug 5440038 , included ADVANCES for roll-up logic but this would apply
16972    --  only to financing case, as the for actual case only the
16973    --  STANDARD line locations are rolled up
16974    -- for a financing case, the line location rollups would include all pay items
16975    -- and the advance amounts which together make up the prepayment amount
16976    -- should be less than the line amount
16977    -- the financing_advance_amount advance amount is also calculated for
16978    -- better reporting purposes
16979 
16980     SELECT
16981         subtotal.line_num
16982     ,   subtotal.line_amount
16983     ,   subtotal.line_loc_amount
16984     ,   subtotal.financing_advance_amount
16985     BULK COLLECT INTO
16986         l_line_num
16987     ,   l_line_amt_tbl
16988     ,   l_lineloc_amt_tbl
16989     ,   l_fin_adv_amount
16990     FROM
16991     ( SELECT
16992         POL.line_num
16993       , CASE
16994           WHEN (    POL.order_type_lookup_code = 'FIXED PRICE'
16995                  OR POL.order_type_lookup_code = 'RATE')
16996 	  THEN
16997             POL.amount
16998           ELSE
16999             CASE
17000               WHEN l_min_acct_unit IS NOT NULL THEN
17001                 -- Round to minimum accountable unit.
17002                 ROUND(
17003                       NVL(POL.quantity * POL.unit_price,0) / l_min_acct_unit
17004                      ) * l_min_acct_unit
17005               ELSE
17006                 -- Round to currency precision.
17007                 ROUND(NVL(POL.quantity * POL.unit_price,0), l_precision)
17008             END
17009         END line_amount
17010       , SUM (CASE
17011           WHEN (    PLL.value_basis = 'FIXED PRICE'
17012                  OR PLL.value_basis = 'RATE')
17013 	  THEN
17014             PLL.amount - NVL(PLL.amount_cancelled, 0)
17015           ELSE
17016             CASE
17017               WHEN l_min_acct_unit IS NOT NULL THEN
17018                 -- Round to minimum accountable unit.
17019                 ROUND(
17020                       ((PLL.quantity-NVL(PLL.quantity_cancelled,0)) * PLL.price_override) / l_min_acct_unit
17021                      ) * l_min_acct_unit
17022               ELSE
17023                 -- Round to currency precision.
17024                 ROUND((PLL.quantity-NVL(PLL.quantity_cancelled,0)) * PLL.price_override, l_precision)
17025             END
17026         END) line_loc_amount
17027      , SUM (CASE
17028           WHEN  PLL.payment_type = 'ADVANCE' THEN
17029              PLL.amount
17030        END) financing_advance_amount
17031       FROM
17032         PO_LINE_LOCATIONS_GT PLL
17033       , PO_LINES_GT POL
17034       WHERE
17035         POL.po_line_id = PLL.po_line_id
17036       AND (   (l_is_financing_flag = 'N' AND PLL.shipment_type = 'STANDARD')
17037            OR (l_is_financing_flag = 'Y' and PLL.shipment_type = 'PREPAYMENT'))
17038       AND POL.po_header_id = p_document_id
17039       AND nvl(POL.cancel_flag,'N') = 'N'
17040       AND nvl(POL.closed_code,'OPEN') <> 'FINALLY CLOSED'
17041    --   AND nvl(payment_type, 'NULL') <> 'ADVANCE' --Bug 5440038
17042       GROUP BY POL.line_num, POL.order_type_lookup_code, POL.amount, POL.quantity, POL.unit_price
17043     ) subtotal
17044     WHERE
17045     ( (l_is_financing_flag = 'Y' AND NOT (subtotal.line_amount >= subtotal.line_loc_amount))
17046     OR
17047       (l_is_financing_flag = 'N' AND NOT (subtotal.line_amount = subtotal.line_loc_amount))
17048     );
17049 
17050     l_progress := '025';
17051 
17052     FOR i IN 1..l_line_num.COUNT LOOP
17053         l_rowCount(i) := i;
17054     END LOOP;
17055 
17056    --Bug 5440038 and 5517131: Cleared the TODOs for complex work submission checks error messages
17057 
17058     FORALL i IN 1..l_line_num.COUNT
17059         INSERT INTO po_online_report_text_gt(online_report_id,
17060         last_update_login,
17061         last_updated_by,
17062         last_update_date,
17063         created_by,
17064         creation_date,
17065         line_num,
17066         shipment_num,
17067         distribution_num,
17068         sequence,
17069         text_line,
17070         message_name)
17071         VALUES(p_online_report_id,
17072             p_login_id,
17073             p_user_id,
17074             sysdate,
17075             p_user_id,
17076             sysdate,
17077             l_line_num(i),
17078             NULL,
17079             NULL,
17080             x_sequence+ l_rowCount(i),
17081             decode ( l_fin_adv_amount(i),
17082                      NULL,
17083                      PO_CORE_S.get_translated_text
17084                      (  'PO_SUB_PAY_ITEM_NE_LINE_AMT'
17085                      ,  'LINE_NUM', l_line_num(i)
17086                      ,  'PAY_ITEM_AMT', l_lineloc_amt_tbl(i)
17087                      ,  'LINE_AMT', l_line_amt_tbl(i)
17088                      ),
17089                       PO_CORE_S.get_translated_text
17090                     (  'PO_SUB_PRE_PAY_GE_LINE_AMT'
17091                     ,  'LINE_NUM', l_line_num(i)
17092                     ,  'PAY_ITEM_AMT', (l_lineloc_amt_tbl(i)-l_fin_adv_amount(i) )
17093                     ,  'ADV_AMT',  l_fin_adv_amount(i)
17094                     ,  'LINE_AMT', l_line_amt_tbl(i)
17095                     )
17096                   ),
17097             decode ( l_fin_adv_amount(i),
17098                      NULL,  'PO_SUB_PAY_ITEM_NE_LINE_AMT'
17099                    ,  'PO_SUB_PRE_PAY_GE_LINE_AMT'
17100                    )
17101           );
17102 
17103     l_progress := '027';
17104     --Increment the x_sequence with number of errors reported in last query
17105     x_sequence := x_sequence + SQL%ROWCOUNT;
17106 
17107   END IF;  -- If Complex Work PO or not
17108 -----------------------------------------------
17109 
17110   l_progress := '030';
17111   IF g_debug_stmt THEN
17112      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
17113        FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
17114                       || l_progress,'PO Dist qty/amt rollup check');
17115      END IF;
17116   END IF;
17117 
17118     -- Check 2: The sum of the distribution quantities/amounts should match the
17119     -- shipment quantity/amount.
17120 
17121     SELECT
17122         POL.line_num
17123     ,   PLL.shipment_num
17124     ,   PLL.quantity
17125     ,   PLL.amount                                            -- <SERVICES FPJ>
17126     ,   sum( nvl(POD.quantity_ordered,0) - nvl(POD.quantity_cancelled,0) )
17127     ,   sum( nvl(POD.amount_ordered,0) - nvl(POD.amount_cancelled,0) )
17128     BULK COLLECT INTO
17129         l_line_num
17130     ,   l_shipment_num
17131     ,   l_lineloc_qty_tbl
17132     ,   l_lineloc_amt_tbl
17133     ,   l_dist_qty_tbl
17134     ,   l_dist_amt_tbl
17135     FROM PO_DISTRIBUTIONS_GT POD,PO_LINE_LOCATIONS_GT PLL, PO_LINES_GT POL
17136     WHERE PLL.po_line_id = POL.po_line_id
17137     AND POD.line_location_id = PLL.line_location_id
17138     AND PLL.po_header_id = p_document_id
17139     AND nvl(PLL.cancel_flag,'N') = 'N'
17140     AND nvl(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
17141     AND PLL.shipment_type in ('STANDARD', 'PLANNED', 'PREPAYMENT') --<Complex Work R12>
17142     GROUP BY
17143         POL.line_num
17144     ,   PLL.shipment_num
17145     ,   PLL.quantity
17146     ,   PLL.amount                                            -- <SERVICES FPJ>
17147     ,   PLL.amount_cancelled
17148     ,   PLL.quantity_cancelled
17149     ,   PLL.shipment_type   --<Complex Work R12>
17150     HAVING
17151         decode ( PLL.quantity                                 -- <SERVICES FPJ>
17152                , NULL , abs (   ( PLL.amount - nvl(PLL.amount_cancelled,0) )
17153                             -   sum( POD.amount_ordered - nvl(POD.amount_cancelled,0) ) )
17154                ,        abs (   ( PLL.quantity - nvl(PLL.quantity_cancelled,0) )
17155                             -   sum( POD.quantity_ordered - nvl(POD.quantity_cancelled,0) ) )
17156                ) > .00001;
17157 
17158     l_progress := '035';
17159 
17160     FOR i IN 1..l_line_num.COUNT LOOP
17161         l_rowCount(i) := i;
17162     END LOOP;
17163 
17164     FORALL i IN 1..l_line_num.COUNT
17165         INSERT INTO po_online_report_text_gt (online_report_id,
17166         last_update_login,
17167         last_updated_by,
17168         last_update_date,
17169         created_by,
17170         creation_date,
17171         line_num,
17172         shipment_num,
17173         distribution_num,
17174         sequence,
17175         text_line,
17176                                 message_name)
17177         VALUES(
17178             p_online_report_id,
17179              p_login_id,
17180              p_user_id,
17181             sysdate,
17182             p_user_id,
17183             sysdate,
17184             l_line_num(i),
17185             l_shipment_num(i),
17186             NULL,                                             -- <SERVICES FPJ>
17187             x_sequence+l_rowCount(i),
17188             decode ( l_lineloc_qty_tbl(i)
17189                    , NULL , PO_CORE_S.get_translated_text
17190                             (   'PO_SUB_PO_SHIP_NE_DIST_AMT'
17191                             ,   'LINE_NUM', l_line_num(i)
17192                             ,   'SHIP_NUM', l_shipment_num(i)
17193                             ,   'SHIP_AMT', l_lineloc_amt_tbl(i)
17194                             ,   'DIST_AMT', l_dist_amt_tbl(i)
17195                             )
17196                           , PO_CORE_S.get_translated_text
17197                             (   'PO_SUB_PO_SHIP_NE_DIST_QTY'
17198                             ,   'LINE_NUM', l_line_num(i)
17199                             ,   'SHIP_NUM', l_shipment_num(i)
17200                             ,   'SHIP_QTY', l_lineloc_qty_tbl(i)
17201                             ,   'DIST_QTY', l_dist_qty_tbl(i)
17202                             )
17203                    ),
17204             decode ( l_lineloc_qty_tbl(i)
17205                    , NULL , 'PO_SUB_PO_SHIP_NE_DIST_AMT'
17206                    ,        'PO_SUB_PO_SHIP_NE_DIST_QTY'
17207                    )
17208             );
17209 
17210     l_progress := '037';
17211     --Increment the x_sequence with number of errors reported in last query
17212     x_sequence := x_sequence + l_line_num.COUNT;
17213 -----------------------------------------------
17214 
17215   IF g_debug_stmt THEN
17216      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
17217        FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
17218                       || l_progress,'End PO Qty/Amt Rollup Checks');
17219      END IF;
17220   END IF;
17221 
17222 END check_po_qty_amt_rollup;
17223 --<Complex Work R12 END>
17224 
17225 
17226 
17227 --<BUG 4624736 START>
17228 -- Checks if the pay item's price can be updated.
17229 -- NOTE: does not verify that line location is in fact
17230 -- a pay item.
17231 FUNCTION is_pay_item_price_updateable (
17232   p_line_location_id          IN NUMBER
17233 , p_add_reasons_to_msg_list   IN VARCHAR2)
17234 RETURN BOOLEAN
17235 IS
17236 
17237   d_module VARCHAR(70) :=
17238                 'po.plsql.PO_DOCUMENT_CHECKS_PVT.is_pay_item_price_updateable';
17239   d_progress NUMBER;
17240   l_is_price_updateable BOOLEAN;
17241   l_quantity_received   NUMBER;
17242   l_quantity_billed     NUMBER;
17243   l_quantity_financed   NUMBER;
17244 
17245 BEGIN
17246   d_progress := 0;
17247 
17248   IF (PO_LOG.d_proc) THEN
17249     PO_LOG.proc_begin(d_module);
17250     PO_LOG.proc_begin(d_module, 'p_line_location_id', p_line_location_id);
17251   END IF;
17252 
17253   d_progress := 10;
17254 
17255   l_is_price_updateable := TRUE;
17256 
17257   -- get the execution quantities for the pay item
17258   SELECT NVL(pll.quantity_received, 0)
17259        , NVL(pll.quantity_billed, 0)
17260        , NVL(pll.quantity_financed, 0)
17261   INTO   l_quantity_received
17262        , l_quantity_billed
17263        , l_quantity_financed
17264   FROM   po_line_locations_all pll
17265   WHERE  line_location_id = p_line_location_id;
17266 
17267   d_progress := 20;
17268 
17269   -- the price is not updateable if the pay item has been executed
17270   -- against
17271   IF (    l_quantity_received <> 0
17272        OR l_quantity_billed <> 0
17273        OR l_quantity_financed <> 0)
17274   THEN
17275     l_is_price_updateable := FALSE;
17276 
17277     d_progress := 30;
17278 
17279     IF (p_add_reasons_to_msg_list = PO_CORE_S.G_PARAMETER_YES) THEN
17280       d_progress := 40;
17281       -- <Complex Work TODO>: FILL IN THE MESSAGES
17282       FND_MESSAGE.set_name('PO','CWPOTODOMESSAGE');
17283       FND_MSG_PUB.add;
17284     END IF;
17285   END IF;
17286 
17287   d_progress := 50;
17288 
17289   IF (PO_LOG.d_proc) THEN
17290     PO_LOG.proc_return(d_module, l_is_price_updateable);
17291   END IF;
17292 
17293   RETURN(l_is_price_updateable);
17294 
17295 EXCEPTION
17296   WHEN OTHERS THEN
17297     IF (PO_LOG.d_exc) THEN
17298       PO_LOG.exc(d_module, d_progress, SQLCODE || SQLERRM);
17299       PO_LOG.proc_end(d_module);
17300     END IF;
17301     RAISE;
17302 END is_pay_item_price_updateable;
17303 --<BUG 4624736 END>
17304 
17305 
17306  --Bug#5462677: copied from 11.5.9 code line of POXPOSCB.pls
17307  FUNCTION chk_unv_invoices(p_invoice_type	IN  VARCHAR2 DEFAULT 'BOTH',
17308 			   p_po_header_id	IN  NUMBER,
17309 			   p_po_release_id	IN  NUMBER DEFAULT NULL,
17310 			   p_po_line_id		IN  NUMBER DEFAULT NULL,
17311 			   p_line_location_id	IN  NUMBER DEFAULT NULL,
17312 			   p_po_distribution_id	IN  NUMBER DEFAULT NULL,
17313 			   p_invoice_id		IN  NUMBER DEFAULT NULL,
17314 			   p_calling_sequence	IN  VARCHAR2) RETURN NUMBER IS
17315 
17316 x_chk_unv_invoices NUMBER := 0;
17317 BEGIN
17318 
17319   If Not AP_MATCH_UTILITIES_PUB.Check_Unvalidated_Invoices(
17320                               p_invoice_type => p_invoice_type,
17321                               p_po_header_id => p_po_header_id,
17322                               p_po_release_id => p_po_release_id,
17323                               p_po_line_id => p_po_line_id,
17324                               p_line_location_id => p_line_location_id,
17325                               p_po_distribution_id => p_po_distribution_id,
17326                               p_invoice_id => p_invoice_id,
17327                               p_calling_sequence => p_calling_sequence) THEN
17328 
17329     -- Unvalidated Credit Memos/Invoices Does Not Exists for this Shipment
17330     x_chk_unv_invoices := 0;
17331   Else
17332     -- Unvalidated Credit Memos/Invoices Do Exists for this Shipment
17333     x_chk_unv_invoices := 1;
17334   End If;
17335 
17336   return(x_chk_unv_invoices);
17337 
17338 EXCEPTION
17339   WHEN OTHERS THEN
17340   PO_MESSAGE_S.APP_ERROR(error_name => 'PO_ALL_TRACE_ERROR',
17341                            token1 => 'FILE',
17342                            value1 => 'PO_CONTROL_CHECKS',
17343                            token2 => 'ERR_NUMBER',
17344                            value2 => '360',
17345                            token3 => 'SUBROUTINE',
17346                            value3 => 'CHK_UNV_INVOICES()');
17347   RAISE;
17348 
17349 END chk_unv_invoices;
17350 -- Determine if there are any unvalidated invoices
17351 --Bug#5462677
17352 PROCEDURE check_unvalidated_invoices(
17353    p_document_type        IN VARCHAR2
17354 ,  p_document_subtype     IN VARCHAR2
17355 ,  p_action_requested     IN VARCHAR2
17356 ,  p_action_date          IN DATE
17357 ,  p_online_report_id     IN NUMBER
17358 ,  p_user_id              IN NUMBER
17359 ,  p_login_id             IN NUMBER
17360 ,  p_document_level       IN VARCHAR2
17361 ,  p_origin_doc_id        IN NUMBER
17362 ,  p_doc_level_id         IN NUMBER
17363 ,  p_sequence             IN OUT NOCOPY NUMBER
17364 ,  x_return_status        OUT NOCOPY VARCHAR2
17365 )
17366 IS
17367 
17368 l_textline  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
17369 l_message_name  FND_NEW_MESSAGES.MESSAGE_NAME%TYPE;
17370 l_calling_sequence VARCHAR2(100);
17371 l_invoice_type     VARCHAR2(100) := 'BOTH';
17372 l_is_complex_po     boolean;
17373 l_token_value VARCHAR2(256);
17374 l_return_status VARCHAR2(1);
17375 d_module   VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.check_unvalidated_invoices';
17376 d_progress NUMBER;
17377 
17378 BEGIN
17379 
17380   d_progress := 0;
17381   IF (PO_LOG.d_proc) THEN
17382     PO_LOG.proc_begin(d_module);
17383     PO_LOG.proc_begin(d_module, 'p_document_type', p_document_type);
17384     PO_LOG.proc_begin(d_module, 'p_action_date', p_action_date);
17385     PO_LOG.proc_begin(d_module, 'p_action_requested', p_action_requested);
17386     PO_LOG.proc_begin(d_module, 'p_online_report_id', p_online_report_id);
17387     PO_LOG.proc_begin(d_module, 'p_user_id', p_user_id);
17388     PO_LOG.proc_begin(d_module, 'p_login_id', p_login_id);
17389     PO_LOG.proc_begin(d_module, 'p_origin_doc_id', p_origin_doc_id);
17390     PO_LOG.proc_begin(d_module, 'p_document_level', p_document_level);
17391     PO_LOG.proc_begin(d_module, 'p_doc_level_id', p_doc_level_id);
17392     PO_LOG.proc_begin(d_module, 'p_sequence', p_sequence);
17393   END IF;
17394 
17395 
17396   d_progress := 10;
17397 
17398   -- Initialize the error mesage before hand, since we need this in
17399   -- the finallyclose/unreserve check sql
17400   -- Error Messages for Finally close hdr/line/shipment and Release
17401   -- Error Messages for Unreserve SPO
17402 
17403   get_message_info(p_document_type    => p_document_type ,
17404                    p_document_subtype => p_document_subtype,
17405                    p_action_requested => p_action_requested ,
17406                    p_document_level   => p_document_level,
17407                    p_doc_level_id     => p_doc_level_id,
17408                    x_text_line        => l_textline,
17409                    x_message_name     => l_message_name,
17410                    x_invoice_type     => l_invoice_type ,
17411                    x_calling_sequence => l_calling_sequence,
17412                    x_return_status    => l_return_status);
17413 
17414   IF p_document_type = 'PO' AND p_document_subtype = 'STANDARD'
17415      AND (p_document_level = g_document_level_HEADER
17416        OR p_document_level = g_document_level_LINE) THEN
17417      --
17418      -- Handle Header final close / Unreserve SPO
17419      --
17420     IF  (p_document_level = g_document_level_HEADER) THEN
17421 
17422         INSERT INTO PO_ONLINE_REPORT_TEXT_GT
17423         (  online_report_id
17424         ,  last_update_login
17425         ,  last_updated_by
17426         ,  last_update_date
17427         ,  created_by
17428         ,  creation_date
17429         ,  line_num
17430         ,  shipment_num
17431         ,  distribution_num
17432         ,  sequence
17433         ,  text_line
17434         ,  message_name
17435         )
17436         SELECT
17437            p_online_report_id
17438         ,  p_login_id
17439         ,  p_user_id
17440         ,  SYSDATE
17441         ,  p_user_id
17442         ,  SYSDATE
17443         ,  null -- lines
17444         ,  null -- shipments
17445         ,  null -- distribution_num
17446         ,  p_sequence + ROWNUM
17447         ,  substr(l_textline, 1, 240)
17448         ,  l_message_name
17449         FROM po_headers_gt poh
17450         WHERE poh.po_header_id = p_doc_level_id
17451           AND  chk_unv_invoices(l_invoice_type, poh.po_header_id, NULL, NULL,NULL, NULL, p_origin_doc_id, l_calling_sequence) = 1;
17452 
17453      --
17454      -- Handle Line final close / Unreserve SPO
17455      --
17456     ELSIF (p_document_level = g_document_level_LINE) THEN
17457         INSERT INTO PO_ONLINE_REPORT_TEXT_GT
17458         (  online_report_id
17459         ,  last_update_login
17460         ,  last_updated_by
17461         ,  last_update_date
17462         ,  created_by
17463         ,  creation_date
17464         ,  line_num
17465         ,  shipment_num
17466         ,  distribution_num
17467         ,  sequence
17468         ,  text_line
17469         ,  message_name
17470         )
17471         SELECT
17472            p_online_report_id
17473         ,  p_login_id
17474         ,  p_user_id
17475         ,  SYSDATE
17476         ,  p_user_id
17477         ,  SYSDATE
17478         ,  line_num -- lines
17479         ,  null -- shipments
17480         ,  null -- distribution_num
17481         ,  p_sequence + ROWNUM
17482         ,  substr(l_textline, 1, 240)
17483         ,  l_message_name
17484         FROM po_lines_gt pol
17485         WHERE pol.po_line_id=p_doc_level_id
17486           AND  chk_unv_invoices(l_invoice_type, pol.po_header_id, NULL, pol.po_line_id,NULL, NULL, p_origin_doc_id, 'CHECK_PO_LINE_FINAL_CLOSE') = 1;
17487     END IF;
17488   --
17489   -- Handle Release Header related checks for unvalidated AP Invoices
17490   --
17491   ELSIF (p_document_type = 'RELEASE' AND p_document_level = g_document_level_HEADER) THEN
17492     INSERT INTO PO_ONLINE_REPORT_TEXT_GT
17493     (  online_report_id
17494         ,  last_update_login
17495         ,  last_updated_by
17496         ,  last_update_date
17497         ,  created_by
17498         ,  creation_date
17499         ,  line_num
17500         ,  shipment_num
17501         ,  distribution_num
17502         ,  sequence
17503         ,  text_line
17504         ,  message_name
17505     )
17506     SELECT
17507            p_online_report_id
17508         ,  p_login_id
17509         ,  p_user_id
17510         ,  SYSDATE
17511         ,  p_user_id
17512         ,  SYSDATE
17513         ,  null -- lines
17514         ,  null -- shipments
17515         ,  null -- distribution_num
17516         ,  p_sequence + ROWNUM
17517         ,  substr(l_textline, 1, 240)
17518         ,  l_message_name
17519     FROM po_releases_gt por
17520     WHERE por.po_release_id=p_doc_level_id
17521      AND  chk_unv_invoices(l_invoice_type, por.po_header_id, por.po_release_id, NULL,NULL, NULL, p_origin_doc_id, l_calling_sequence) = 1;
17522 
17523    --
17524    -- Handle Shipment/Payitem final close and Unreserve(Both SPO and Release)
17525    --
17526   ELSIF (p_document_level = g_document_level_SHIPMENT) THEN
17527       INSERT INTO PO_ONLINE_REPORT_TEXT_GT
17528       (  online_report_id
17529       ,  last_update_login
17530       ,  last_updated_by
17531       ,  last_update_date
17532       ,  created_by
17533       ,  creation_date
17534       ,  line_num
17535       ,  shipment_num
17536       ,  distribution_num
17537       ,  sequence
17538       ,  text_line
17539       ,  message_name
17540       )
17541       SELECT
17542          p_online_report_id
17543       ,  p_login_id
17544       ,  p_user_id
17545       ,  SYSDATE
17546       ,  p_user_id
17547       ,  SYSDATE
17548       ,  null -- lines
17549       ,  shipment_num -- shipments
17550       ,  null -- distribution_num
17551       ,  p_sequence + ROWNUM
17552       ,  substr(l_textline, 1, 240)
17553       ,  l_message_name
17554       FROM po_line_locations_gt poll
17555       WHERE poll.line_location_id=p_doc_level_id
17556         AND  chk_unv_invoices(l_invoice_type, poll.po_header_id, poll.po_release_id, NULL,poll.line_location_id, NULL, p_origin_doc_id, l_calling_sequence) = 1;
17557    --
17558    -- Handle Distribution Unreserve(for both SPO/Release)
17559    --
17560   ELSIF (p_document_level=g_document_level_DISTRIBUTION  AND p_action_requested = g_action_UNRESERVE) THEN
17561       INSERT INTO PO_ONLINE_REPORT_TEXT_GT
17562       (  online_report_id
17563       ,  last_update_login
17564       ,  last_updated_by
17565       ,  last_update_date
17566       ,  created_by
17567       ,  creation_date
17568       ,  line_num
17569       ,  shipment_num
17570       ,  distribution_num
17571       ,  sequence
17572       ,  text_line
17573       ,  message_name
17574       )
17575       SELECT
17576          p_online_report_id
17577       ,  p_login_id
17578       ,  p_user_id
17579       ,  SYSDATE
17580       ,  p_user_id
17581       ,  SYSDATE
17582       ,  null -- lines
17583       ,  null -- shipments
17584       ,  distribution_num
17585       ,  p_sequence + ROWNUM
17586       ,  substr(l_textline, 1, 240)
17587       ,  l_message_name
17588       FROM po_distributions_gt pod
17589       WHERE pod.po_distribution_id=p_doc_level_id
17590         AND  chk_unv_invoices(l_invoice_type, pod.po_header_id, pod.po_release_id, NULL,NULL, pod.po_distribution_id, p_origin_doc_id, l_calling_sequence) = 1;
17591   END IF; --
17592 
17593   d_progress := 20;
17594 
17595   p_sequence := p_sequence + SQL%ROWCOUNT;
17596   x_return_status := FND_API.G_RET_STS_SUCCESS;
17597 
17598   IF (PO_LOG.d_proc) THEN
17599     PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
17600     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
17601     PO_LOG.proc_end(d_module);
17602   END IF;
17603 
17604   RETURN;
17605 
17606 EXCEPTION
17607   WHEN others THEN
17608     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
17609     IF (PO_LOG.d_exc) THEN
17610       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
17611       PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
17612       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
17613       PO_LOG.proc_end(d_module);
17614     END IF;
17615 
17616     RETURN;
17617 END check_unvalidated_invoices;
17618 
17619 PROCEDURE get_message_info(p_document_type    IN VARCHAR2,
17620                            p_document_subtype IN VARCHAR2,
17621                            p_action_requested IN VARCHAR2,
17622                            p_document_level   IN VARCHAR2,
17623                            p_doc_level_id     IN NUMBER,
17624                            x_text_line        OUT  NOCOPY VARCHAR2,
17625                            x_message_name     OUT  NOCOPY VARCHAR2,
17626                            x_invoice_type     OUT  NOCOPY VARCHAR2,
17627                            x_calling_sequence OUT  NOCOPY VARCHAR2,
17628                            x_return_status    OUT  NOCOPY VARCHAR2) IS
17629 
17630   l_textline         PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
17631   l_message_name     FND_NEW_MESSAGES.MESSAGE_NAME%TYPE;
17632   l_calling_sequence VARCHAR2(100);
17633   l_invoice_type     VARCHAR2(100) := 'BOTH';
17634   l_is_complex_po    boolean := false;
17635   l_document_id      NUMBER;
17636   d_module   VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.get_message_text';
17637   d_progress NUMBER;
17638 BEGIN
17639   d_progress := 0;
17640   IF (PO_LOG.d_proc) THEN
17641     PO_LOG.proc_begin(d_module);
17642     PO_LOG.proc_begin(d_module, 'p_document_type', p_document_type);
17643     PO_LOG.proc_begin(d_module, 'p_document_subtype', p_document_subtype);
17644     PO_LOG.proc_begin(d_module, 'p_action_requested', p_action_requested);
17645     PO_LOG.proc_begin(d_module, 'p_document_level', p_document_level);
17646     PO_LOG.proc_begin(d_module, 'p_doc_level_id', p_doc_level_id);
17647   END IF;
17648 
17649   IF p_document_subtype = 'STANDARD' THEN
17650     IF p_document_level = g_document_level_HEADER THEN
17651      l_document_id := p_doc_level_id;
17652     ELSIF p_document_level = g_document_level_LINE THEN
17653       SELECT po_header_id
17654       INTO   l_document_id
17655       FROM   po_lines_gt
17656       WHERE po_line_id=p_doc_level_id;
17657     ELSIF p_document_level = g_document_level_SHIPMENT THEN
17658       SELECT po_header_id
17659       INTO   l_document_id
17660       FROM   po_line_locations_gt
17661       WHERE line_location_id=p_doc_level_id;
17662     ELSIF p_document_level = g_document_level_DISTRIBUTION THEN
17663       SELECT po_header_id
17664       INTO   l_document_id
17665       FROM   po_distributions_gt
17666       WHERE  po_distribution_id=p_doc_level_id;
17667     END IF;
17668     IF p_document_type <> g_document_type_RELEASE THEN
17669       l_is_complex_po := PO_COMPLEX_WORK_PVT.is_complex_work_po(l_document_id);
17670     END IF;
17671   END IF;
17672 
17673   IF (p_action_requested = g_action_UNRESERVE) THEN
17674     l_invoice_type     := 'CREDIT';
17675 
17676     IF (p_document_level = g_document_level_HEADER) THEN
17677        l_message_name     := 'PO_UNRES_AP_DOCS_PENDING';
17678        l_textline         := FND_MESSAGE.GET_STRING('PO',l_message_name);
17679     ELSIF (p_document_level = g_document_level_LINE) THEN
17680        l_message_name     := 'PO_UNRES_POL_AP_DOCS_PENDING';
17681        l_textline         := FND_MESSAGE.GET_STRING('PO',l_message_name);
17682     ELSIF (p_document_level = g_document_level_SHIPMENT) THEN
17683        IF (l_is_complex_po) THEN
17684          l_message_name     := 'PO_UNRES_POPI_AP_DOCS_PENDING';
17685          l_textline         := FND_MESSAGE.GET_STRING('PO',l_message_name);
17686        ELSE
17687          l_message_name     := 'PO_UNRES_POLL_AP_DOCS_PENDING';
17688          l_textline         := FND_MESSAGE.GET_STRING('PO',l_message_name);
17689        END IF;
17690     ELSIF (p_document_level = g_document_level_DISTRIBUTION) THEN
17691        l_message_name     := 'PO_UNRES_POD_AP_DOCS_PENDING';
17692        l_textline         := FND_MESSAGE.GET_STRING('PO',l_message_name);
17693     END IF;
17694   ELSIF (p_action_requested = g_action_FINAL_CLOSE_CHECK) THEN
17695     l_invoice_type     := 'BOTH';
17696 
17697     IF (p_document_level = g_document_level_HEADER) THEN
17698        l_message_name     := 'PO_FC_POH_AP_DOCS_PENDING';
17699        l_textline         := FND_MESSAGE.GET_STRING('PO',l_message_name);
17700     ELSIF (p_document_level = g_document_level_LINE) THEN
17701        l_message_name     := 'PO_FC_POL_AP_DOCS_PENDING';
17702        l_textline         := FND_MESSAGE.GET_STRING('PO',l_message_name);
17703     ELSIF (p_document_level = g_document_level_SHIPMENT) THEN
17704        IF (l_is_complex_po) THEN
17705          l_message_name     := 'PO_FC_POPI_AP_DOCS_PENDING';
17706          l_textline         := FND_MESSAGE.GET_STRING('PO',l_message_name);
17707        ELSE
17708          l_message_name     := 'PO_FC_POLL_AP_DOCS_PENDING';
17709          l_textline         := FND_MESSAGE.GET_STRING('PO',l_message_name);
17710        END IF;
17711     END IF;
17712   END IF;
17713   l_calling_sequence := l_message_name; -- can be any string(tracking in AP)
17714 
17715   x_message_name     := l_message_name;
17716   x_text_line        := l_textline;
17717   x_calling_sequence := l_calling_sequence;
17718   x_invoice_type     := l_invoice_type;
17719   x_return_status    := FND_API.G_RET_STS_SUCCESS;
17720 
17721   IF (PO_LOG.d_proc) THEN
17722     PO_LOG.proc_end(d_module, 'x_text_line', x_text_line);
17723     PO_LOG.proc_end(d_module, 'x_message_name', x_message_name);
17724     PO_LOG.proc_end(d_module, 'x_invoice_type', x_invoice_type);
17725     PO_LOG.proc_end(d_module, 'x_calling_sequence', x_calling_sequence);
17726     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
17727     PO_LOG.proc_end(d_module);
17728   END IF;
17729 
17730 EXCEPTION
17731   WHEN OTHERS THEN
17732     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
17733     IF (PO_LOG.d_exc) THEN
17734       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
17735       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
17736       PO_LOG.proc_end(d_module);
17737     END IF;
17738 END;
17739 
17740 PROCEDURE set_lcm_flag(p_line_location_id  IN NUMBER,
17741 			p_doc_check_status IN VARCHAR2,
17742 			x_return_status    OUT nocopy VARCHAR2)
17743 IS
17744 
17745 l_return_status VARCHAR2(10) := NULL;
17746 l_vendor_id NUMBER;
17747 l_vendor_site_id NUMBER;
17748 l_inventory_item_id NUMBER;
17749 l_ship_to_organization_id NUMBER;
17750 l_consigned_flag VARCHAR2(20);
17751 l_outsourced_assembly VARCHAR2(20);
17752 l_log_head VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.Set_LCM_Flag';
17753 l_progress VARCHAR2(3) := '000';
17754 
17755 
17756 BEGIN
17757   IF g_debug_stmt THEN
17758      PO_DEBUG.debug_begin(l_log_head);
17759      PO_DEBUG.debug_var(l_log_head,l_progress,'p_line_location_id',   p_line_location_id);
17760      PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_check_status',   p_doc_check_status);
17761   END IF;
17762 
17763   l_progress := '001';
17764   --<BUG 7594807 Added Table Alias>
17765   --<BUG 8233135 Added exception block for the below sql to avoid
17766   --errors while supplier user tries to split the shipment lines
17767   --while performing supplier change request>
17768   BEGIN
17769 	SELECT poh.vendor_id,
17770 	       poh.vendor_site_id,
17771   	       pol.item_id,
17772   	       poll.ship_to_organization_id,
17773   	       poll.consigned_flag,
17774   	       poll.outsourced_assembly
17775           INTO l_vendor_id,
17776   	       l_vendor_site_id,
17777   	       l_inventory_item_id,
17778   	       l_ship_to_organization_id,
17779   	       l_consigned_flag,
17780   	       l_outsourced_assembly
17781           FROM po_line_locations_all poll,
17782     	       po_lines_all pol,
17783     	       po_headers_all poh
17784          WHERE poh.po_header_id = pol.po_header_id
17785            AND pol.po_line_id = poll.po_line_id
17786            AND poll.line_location_id = p_line_location_id;
17787   EXCEPTION
17788   WHEN OTHERS THEN
17789 	NULL;
17790   END;
17791 
17792      IF g_debug_stmt THEN
17793             PO_DEBUG.debug_var(l_log_head,l_progress,'l_vendor_id',l_vendor_id);
17794             PO_DEBUG.debug_var(l_log_head,l_progress,'l_vendor_site_id',l_vendor_site_id);
17795             PO_DEBUG.debug_var(l_log_head,l_progress,'l_inventory_item_id',l_inventory_item_id);
17796             PO_DEBUG.debug_var(l_log_head,l_progress,'l_ship_to_organization_id',l_ship_to_organization_id);
17797             PO_DEBUG.debug_var(l_log_head,l_progress,'l_consigned_flag',l_consigned_flag);
17798             PO_DEBUG.debug_var(l_log_head,l_progress,'l_outsourced_assembly',l_outsourced_assembly);
17799 
17800       END IF;
17801 
17802     l_progress:= '002';
17803 
17804     --<BUG 7594807 Call the Inventory API only when the PO line has item so that
17805     -- we can avoid the call for other line types>
17806     IF ( l_inventory_item_id IS NOT NULL ) THEN
17807 
17808 	l_return_status := inv_utilities.inv_check_lcm(l_inventory_item_id,
17809   						       l_ship_to_organization_id,
17810   						       l_consigned_flag,
17811 						       l_outsourced_assembly,
17812 						       l_vendor_id,
17813 						       l_vendor_site_id);
17814 
17815     END IF;
17816 
17817 
17818     PO_DEBUG.debug_var(l_log_head,l_progress,'l_return_status',l_return_status);
17819 
17820       IF l_return_status = 'Y' THEN
17821       	IF p_doc_check_status = 'BEFORE' THEN
17822       		UPDATE po_line_locations_gt
17823 		  SET lcm_flag = 'Y'
17824 		  WHERE line_location_id = p_line_location_id
17825 		  and lcm_flag is null;
17826 
17827 		UPDATE po_distributions_gt
17828 		  SET lcm_flag = 'Y'
17829                   WHERE line_location_id = p_line_location_id
17830                   and lcm_flag is null;
17831 
17832 
17833         ELSIF p_doc_check_status = 'AFTER' THEN
17834 
17835                 UPDATE po_line_locations_all
17836                  SET lcm_flag = 'Y'
17837                  WHERE line_location_id = p_line_location_id;
17838 
17839                 UPDATE po_distributions_all
17840                 SET lcm_flag = 'Y'
17841                 WHERE line_location_id = p_line_location_id;
17842        END IF;
17843 
17844       ELSIF l_return_status = 'N' THEN
17845 
17846       		UPDATE po_line_locations_all
17847 		 SET lcm_flag = null
17848 		 WHERE line_location_id = p_line_location_id
17849 		 AND lcm_flag = 'Y';
17850 
17851 		UPDATE po_distributions_all
17852 		 SET lcm_flag = null
17853                  WHERE line_location_id = p_line_location_id
17854                  AND lcm_flag = 'Y';
17855 
17856       END IF;
17857 
17858   x_return_status := fnd_api.g_ret_sts_success;
17859 
17860   EXCEPTION
17861   WHEN others THEN
17862     x_return_status := fnd_api.g_ret_sts_unexp_error;
17863 
17864     IF (g_debug_unexp) THEN
17865                     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
17866                       FND_LOG.string(FND_LOG.level_unexpected, l_log_head || '.others_exception', 'EXCEPTION: Location is '
17867                            || l_progress || ' SQL CODE is '||sqlcode);
17868                     END IF;
17869     END IF;
17870 
17871   END set_lcm_flag;
17872 
17873 END PO_DOCUMENT_CHECKS_PVT;