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.177.12020000.38 2013/05/10 08:35:16 inagdeo 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 -- Declare private package variables.
44 -----------------------------------------------------------------------------
45 
46 -- Refactored debugging
47 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*/
48 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*/
49 
50 
51 
52 
53 --------------------------------------------------------------------------------
54 -- Forward procedure declarations
55 --------------------------------------------------------------------------------
56 
57 PROCEDURE populate_global_temp_tables(
58    x_return_status                  OUT NOCOPY     VARCHAR2
59 ,  p_doc_type                       IN             VARCHAR2
60 ,  p_doc_level                      IN             VARCHAR2
61 ,  p_doc_level_id                   IN             NUMBER
62 ,  p_draft_id							IN 			NUMBER := -1   -- CLM Aprvl
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 ,  p_draft_id							IN 			NUMBER := -1   -- CLM Aprvl
71 ,  x_return_status                  OUT NOCOPY     VARCHAR2
72 );
73 -- <Doc Manager Rewrite 11.5.11 End>
74 
75 PROCEDURE populate_line_locations_gt(
76    p_doc_type                       IN             VARCHAR2
77 ,  p_doc_level                      IN             VARCHAR2
78 ,  p_doc_level_id                   IN             NUMBER
79 ,  p_draft_id							IN 			NUMBER := -1   -- CLM Aprvl
80 );
81 
82 PROCEDURE populate_distributions_gt(
83    p_doc_type                       IN             VARCHAR2
84 ,  p_doc_level                      IN             VARCHAR2
85 ,  p_doc_level_id                   IN             NUMBER
86 ,  p_draft_id							IN 			NUMBER := -1   -- CLM Aprvl
87 );
88 
89 PROCEDURE check_unreserve(
90    p_online_report_id               IN             NUMBER
91 ,  p_document_type                  IN             VARCHAR2 --Bug#5462677
92 ,  p_document_subtype               IN             VARCHAR2 --Bug#5462677
93 ,  p_document_level                 IN             VARCHAR2 --Bug#5462677
94 ,  p_doc_level_id                   IN             NUMBER   --Bug#5462677
95 ,  p_user_id                        IN             NUMBER
96 ,  p_login_id                       IN             NUMBER
97 ,  p_sequence                       IN OUT NOCOPY  NUMBER
98 );
99 
100 PROCEDURE check_gl_date(
101    p_doc_type                       IN             VARCHAR2
102 ,  p_online_report_id               IN             NUMBER
103 ,  p_login_id                       IN             NUMBER
104 ,  p_user_id                        IN             NUMBER
105 ,  p_sequence                       IN OUT NOCOPY  NUMBER
106 );
107 
108 PROCEDURE check_blanket_agreement(p_document_id IN NUMBER,
109                        p_online_report_id IN NUMBER,
110                        p_user_id IN NUMBER,
111                        p_login_id IN NUMBER,
112                        p_check_asl IN BOOLEAN,                     -- <2757450>
113                        p_sequence IN OUT NOCOPY NUMBER,
114                        x_return_status OUT NOCOPY VARCHAR2);
115 
116 -- bug3592160 START
117 PROCEDURE complete_po_header_id_tbl
118 ( p_count            IN NUMBER,
119   p_header_id        IN PO_TBL_NUMBER,
120   p_release_id       IN PO_TBL_NUMBER,
121   p_vendor_order_num IN PO_TBL_VARCHAR30,
122   p_document_num     IN PO_TBL_VARCHAR30,
123   p_type_lookup_code IN PO_TBL_VARCHAR30,
124   x_header_id        OUT NOCOPY PO_TBL_NUMBER
125 );
126 -- bug3592160 END
127 
128 -- <Doc Manager Rewrite 11.5.11 Start>
129 PROCEDURE check_final_close(
130    p_document_type        IN VARCHAR2
131 ,  p_document_subtype     IN VARCHAR2
132 ,  p_document_level       IN VARCHAR2
133 ,  p_document_id          IN NUMBER
134 ,  p_online_report_id     IN NUMBER
135 ,  p_user_id              IN NUMBER
136 ,  p_login_id             IN NUMBER
137 ,  p_origin_doc_id        IN NUMBER := NULL --Bug#5462677
138 ,  p_doc_level_id         IN NUMBER
139 ,  p_sequence             IN OUT NOCOPY NUMBER
140 ,  x_return_status        OUT NOCOPY VARCHAR2
141 );
142 
143 PROCEDURE check_rcv_trans_interface(
144    p_document_type        IN VARCHAR2
145 ,  p_document_level       IN VARCHAR2  --<Bug 4118145, Issue 7>: Corrected type
146 ,  p_online_report_id     IN NUMBER
147 ,  p_user_id              IN NUMBER
148 ,  p_login_id             IN NUMBER
149 ,  p_document_id          IN NUMBER
150 ,  p_sequence             IN OUT NOCOPY NUMBER
151 ,  x_return_status        OUT NOCOPY VARCHAR2
152 );
153 
154 PROCEDURE check_asn_not_fully_received(
155    p_document_type        IN VARCHAR2
156 ,  p_document_level       IN VARCHAR2    --<Bug 9012072, Added the p_document_level IN parameter
157 ,  p_online_report_id     IN NUMBER
158 ,  p_user_id              IN NUMBER
159 ,  p_login_id             IN NUMBER
160 ,  p_sequence             IN OUT NOCOPY NUMBER
161 ,  x_return_status        OUT NOCOPY VARCHAR2
162 );
163 
164 PROCEDURE check_qty_rcv_but_not_deliv(
165    p_document_type        IN VARCHAR2
166 ,  p_online_report_id     IN NUMBER
167 ,  p_user_id              IN NUMBER
168 ,  p_login_id             IN NUMBER
169 ,  p_sequence             IN OUT NOCOPY NUMBER
170 ,  x_return_status        OUT NOCOPY VARCHAR2
171 );
172 
173 PROCEDURE check_amt_rcv_but_not_deliv(
174    p_document_type        IN VARCHAR2
175 ,  p_online_report_id     IN NUMBER
176 ,  p_user_id              IN NUMBER
177 ,  p_login_id             IN NUMBER
178 ,  p_sequence             IN OUT NOCOPY NUMBER
179 ,  x_return_status        OUT NOCOPY VARCHAR2
180 );
181 
182 PROCEDURE check_invalid_acct_flex(
183    p_document_type        IN VARCHAR2
184 ,  p_action_requested     IN VARCHAR2
185 ,  p_action_date          IN DATE
186 ,  p_online_report_id     IN NUMBER
187 ,  p_user_id              IN NUMBER
188 ,  p_login_id             IN NUMBER
189 ,  p_document_id          IN NUMBER
190 ,  p_sequence             IN OUT NOCOPY NUMBER
191 ,  x_return_status        OUT NOCOPY VARCHAR2
192 );
193 
194 PROCEDURE check_bpa_has_open_release(
195    p_online_report_id     IN NUMBER
196 ,  p_user_id              IN NUMBER
197 ,  p_login_id             IN NUMBER
198 ,  p_sequence             IN OUT NOCOPY NUMBER
199 ,  x_return_status        OUT NOCOPY VARCHAR2
200 );
201 
202 PROCEDURE check_bpa_has_open_stdref(
203    p_online_report_id     IN NUMBER
204 ,  p_user_id              IN NUMBER
205 ,  p_login_id             IN NUMBER
206 ,  p_sequence             IN OUT NOCOPY NUMBER
207 ,  x_return_status        OUT NOCOPY VARCHAR2
208 );
209 
210 PROCEDURE check_cpa_has_open_stdref(
211    p_online_report_id     IN NUMBER
212 ,  p_user_id              IN NUMBER
213 ,  p_login_id             IN NUMBER
214 ,  p_sequence             IN OUT NOCOPY NUMBER
215 ,  x_return_status        OUT NOCOPY VARCHAR2
216 );
217 
218 PROCEDURE check_ppo_has_open_release(
219    p_online_report_id     IN NUMBER
220 ,  p_user_id              IN NUMBER
221 ,  p_login_id             IN NUMBER
222 ,  p_sequence             IN OUT NOCOPY NUMBER
223 ,  x_return_status        OUT NOCOPY VARCHAR2
224 );
225 
226 --<Complex Work R12 START>
227 PROCEDURE check_po_qty_amt_rollup(
228    p_online_report_id     IN NUMBER
229 ,  p_document_id          IN NUMBER
230 ,  p_login_id             IN NUMBER
231 ,  p_user_id              IN NUMBER
232 ,  p_clm_document	  IN VARCHAR := 'N' --CLM Mod
233 ,  x_sequence             IN OUT NOCOPY NUMBER
234 );
235 PROCEDURE check_unvalidated_invoices(p_document_type    IN VARCHAR2,
236                                      p_document_subtype     IN VARCHAR2,
237                                      p_action_requested IN VARCHAR2,
238                                      p_action_date      IN DATE,
239                                      p_online_report_id IN NUMBER,
240                                      p_user_id          IN NUMBER,
241                                      p_login_id         IN NUMBER,
242                                      p_document_level   IN VARCHAR2,
243                                      p_origin_doc_id    IN NUMBER,
244                                      p_doc_level_id     IN NUMBER,
245                                      p_sequence         IN OUT NOCOPY NUMBER,
246                                      x_return_status    OUT NOCOPY VARCHAR2);
247 
248 PROCEDURE get_message_info(p_document_type    IN VARCHAR2,
249                            p_document_subtype IN VARCHAR2,
250                            p_action_requested IN VARCHAR2,
251                            p_document_level   IN VARCHAR2,
252                            p_doc_level_id      IN NUMBER,
253                            x_text_line        OUT  NOCOPY VARCHAR2,
254                            x_message_name     OUT  NOCOPY VARCHAR2,
255                            x_invoice_type     OUT  NOCOPY VARCHAR2,
256                            x_calling_sequence OUT  NOCOPY VARCHAR2,
257                            x_return_status    OUT  NOCOPY VARCHAR2);
258 
259 --<Complex Work R12 END>
260 
261 
262 -- <Doc Manager Rewrite 11.5.11 End>
263 
264 PROCEDURE val_pa_ord_constraints(
265    p_header_id     IN NUMBER
266 ,  p_online_report_id     IN NUMBER
267 ,  p_user_id              IN NUMBER
268 ,  p_login_id             IN NUMBER
269 ,  p_sequence             IN OUT NOCOPY NUMBER
270 ,  x_return_status        OUT NOCOPY VARCHAR2
271 );
272 
273 PROCEDURE PO_ACCOUNTS_CHECK(P_DOCUMENT_ID            IN NUMBER,
274                             P_DOCUMENT_TYPE          IN VARCHAR2,
275                             P_ONLINE_REPORT_ID       IN NUMBER,
276                             P_USER_ID                IN NUMBER,
277                             P_LOGIN_ID               IN NUMBER,
278                             P_SEQUENCE               IN OUT NOCOPY NUMBER,
279                             X_RETURN_STATUS          OUT NOCOPY VARCHAR2,
280                             x_msg_data               OUT NOCOPY VARCHAR2);
281 
282 -- Added for bug 13797764
283 PROCEDURE po_idv_reference_equal_check(p_document_id	IN NUMBER,
284 					p_online_report_id	IN NUMBER,
285 					p_user_id		IN NUMBER,
286 					p_login_id		IN NUMBER,
287 					p_sequence		IN OUT NOCOPY NUMBER,
288 					x_return_status		OUT NOCOPY VARCHAR2);
289 
290 --<Event Based Delivery>
291 PROCEDURE DELIVERY_EVENT_CHECKS(p_document_id IN NUMBER,
292                        p_online_report_id IN NUMBER,
293                        p_user_id IN NUMBER,
294                        p_login_id IN NUMBER,
295                        p_sequence IN OUT NOCOPY NUMBER,
296                        x_return_status OUT NOCOPY VARCHAR2);
297 -- Payment instruction
298 PROCEDURE payment_instruction_checks(p_document_id IN NUMBER,
299                        p_draft_type IN VARCHAR2,
300                        p_online_report_id IN NUMBER,
301                        p_user_id IN NUMBER,
302                        p_login_id IN NUMBER,
303                        p_sequence IN OUT NOCOPY NUMBER,
304                        x_return_status OUT NOCOPY VARCHAR2);
305 
306 --<Sunset Memo - POP Validation for Severable Line Type>
307 PROCEDURE VALIDATE_POP_FOR_SEVERABLE(p_document_id IN NUMBER,
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);
313 --UCA Project - CLMR4 Changes Begin
314 PROCEDURE check_line_ucas(p_document_id IN NUMBER,
315                        p_online_report_id IN NUMBER,
316                        p_user_id IN NUMBER,
317                        p_login_id IN NUMBER,
318                        p_sequence IN OUT NOCOPY NUMBER,
319                        x_return_status OUT NOCOPY VARCHAR2);
320 --UCA Project - CLMR4 Changes End
321 --CLM Phase 4 -Elins project Start
322 PROCEDURE validate_exhibits(p_document_id IN NUMBER,
323                        p_draft_id IN NUMBER,
324                        p_online_report_id IN NUMBER,
325                        p_user_id IN NUMBER,
326                        p_login_id IN NUMBER,
327                        p_sequence IN OUT NOCOPY NUMBER,
328                        x_return_status OUT NOCOPY VARCHAR2);
329 --CLM Phase 4 -Elins project End
330 --CLM Phase 4 - MIPR project Begin
331 PROCEDURE check_po_mipr_reference(p_document_id IN NUMBER,
332                                   p_draft_id IN NUMBER,
333                                   p_online_report_id IN NUMBER,
334                                   p_user_id IN NUMBER,
335                                   p_login_id IN NUMBER,
336                                   p_sequence IN OUT NOCOPY NUMBER,
337                                   x_return_status OUT NOCOPY VARCHAR2);
338 --CLM Phase 4 - MIPR project End
339 
340 /* CONTERMS FPJ START*/
341 -------------------------------------------------------------------------------
342 --Start of Comments
343 --Name: check_terms
344 --Pre-reqs:
345 --  This procedure is dependent on the Contracts group API OKC_TERMS_QA_GRP and
346 --  calls procedure qa_doc. It also subtypes the qa_result_tbl_type to store
347 --  returned error messages.
348 --Modifies:
349 --  po_online_report_text_gt
350 --Locks:
351 --  None.
352 --Function:
353 -- This procedure is used to validate contract articles and deliverables and is
354 -- called during the submission check. The contracts APi qa_doc is called in
355 -- normal or amend mode based on the status of the document. The resulting errors/
356 -- warnings if any are stored in the global temp table.
357 --Parameters:
358 --  Otherwise, include the IN:, IN OUT:, and/or OUT: sections as needed.
359 --IN:
360 --p_document_id
361 --  po_header_id
362 --p_document_type
363 --  po document type PO or PA
364 --p_document_subtype
365 --  po_document subtype 'STANDARD', 'CONTRACT' or 'BLANKET'
366 --p_online_report_id
367 --  unique error report id
368 --p_user_id
369 --  user id
370 --p_login_id
371 --  login id
372 --IN OUT:
373 --p_sequence
374 --  Description of why/how the parameter is used.
375 --OUT:
376 --x_return_status
377 --  return status of the procedure
378 --Notes:
379 --Testing:
380 --  test the qa doc is called in the correct mode and that the returned errors
381 -- are correctly populated in the error table.
382 --End of Comments
383 -------------------------------------------------------------------------------
384 
385 PROCEDURE check_terms(
386    p_document_id          IN NUMBER,
387    p_document_type        IN VARCHAR2,
388    p_document_subtype     IN VARCHAR2,
389    p_online_report_id     IN NUMBER,
390    p_user_id              IN NUMBER,
391    p_login_id             IN NUMBER,
392    p_sequence             IN OUT NOCOPY NUMBER,
393    x_return_status        OUT NOCOPY VARCHAR2) IS
394 
395 -- declare cursor to fetch the revision, authorization status and approved flag
396 CURSOR c_po_status (p_document_id NUMBER) IS
397   SELECT revision_num
398          ,start_date
399    ,end_date
400    , nvl(draft_id, -1)  -- Mod Project
401   FROM   po_headers_gt
402   WHERE  po_header_id = p_document_id;
403 
404 -- contracts dependency
405 SUBTYPE qa_result_tbl_type IS OKC_TERMS_QA_GRP.qa_result_tbl_type;
406 l_qa_result_tbl qa_result_tbl_type;
407 l_qa_mode VARCHAR2(30);
408 l_contracts_document_type VARCHAR2(150);
409 SUBTYPE Event_tbl_type IS OKC_TERMS_QA_GRP.busdocdates_tbl_type;
410 l_event_tbl Event_tbl_type;
411 
412 
413 l_revision_num po_headers.revision_num%TYPE;
414 l_authorization_status po_headers.authorization_status%TYPE;
415 l_approved_flag po_headers.approved_flag%TYPE;
416 l_po_start_date po_headers.start_date%TYPE;
417 l_po_end_date po_headers.end_date%TYPE;
418 
419 l_draft_id NUMBER; -- Mod Project
420 l_document_id NUMBER; --Mod Project
421 l_api_name CONSTANT VARCHAR2(30) := 'Check Terms';
422 l_progress VARCHAR2(3);
423 l_row_index PLS_INTEGER;
424 
425 l_return_status VARCHAR2(1);
426 l_qa_return_status VARCHAR2(1);
427 l_msg_count NUMBER;
428 l_msg_data VARCHAR2(2000);
429 
430 
431 BEGIN
432 
433   x_return_status := FND_API.G_RET_STS_SUCCESS;
434 
435   -- contracts document type
436   l_contracts_document_type := p_document_type||'_'||p_document_subtype;
437   l_document_id := p_document_id; --Mod Project
438 
439   -- fetch the document status indicators
440   OPEN c_po_status(p_document_id);
441   FETCH c_po_status INTO l_revision_num, l_po_start_date, l_po_end_date, l_draft_id;
442   CLOSE c_po_status;
443 
444   if l_draft_id <> -1 then --Mod Project
445   -- For modification the contact document type is different
446     l_contracts_document_type := l_contracts_document_type ||'_MOD';
447     l_document_id := l_draft_id;
448   end if;
449 
450   -- decode status indicators to decide qa mode
451   -- Migrate PO:
452   -- Now that conterms can be added at any revision of the PO
453   -- We need to check if the current rev is the first one with
454   -- contracts and if so do the normal qa
455   IF (l_revision_num > 0) AND
456      (PO_CONTERMS_UTL_GRP.get_archive_conterms_flag (p_po_header_id => p_document_id) = 'Y')
457   THEN
458     l_qa_mode := 'AMEND';
459   ELSE
460     l_qa_mode := 'NORMAL';
461   END IF;
462 
463   l_event_tbl(1).event_code := 'PO_START_DATE';
464   l_event_tbl(1).event_date := l_po_start_date;
465   l_event_tbl(2).event_code := 'PO_END_DATE';
466   l_event_tbl(2).event_date := l_po_end_date;
467 
468   l_progress := '001';
469 
470   -- call the contracts QA
471   OKC_TERMS_QA_GRP.qa_Doc(
472     p_api_version      => 1.0,
473     p_init_msg_list    => FND_API.G_FALSE,
474     x_return_status    => l_return_status,
475     x_msg_data       => l_msg_data,
476     x_msg_count      => l_msg_count,
477     p_qa_mode        => l_qa_mode,
478     p_doc_type       => l_contracts_document_type,
479 --    p_doc_id           => p_document_id, -- Mod Project
480     p_doc_id           => l_document_id,
481     p_bus_doc_date_events_tbl => l_event_tbl,
482     x_qa_result_tbl    => l_qa_result_tbl,
483     x_qa_return_status => l_qa_return_status
484   );
485 
486   IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
487     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
488   END IF;
489 
490   -- if successful update error table with the messages if qa returned errors
491   IF (l_qa_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
492     l_progress := '002';
493     l_row_index := l_qa_result_tbl.FIRST;
494     LOOP
495     EXIT WHEN l_row_index IS NULL;
496       INSERT INTO po_online_report_text_gt
497         (online_report_id
498         ,last_updated_by
499         ,last_update_date
500         ,created_by
501         ,creation_date
502         ,line_num
503         ,shipment_num
504         ,distribution_num
505         ,sequence
506         ,text_line
507         ,message_name
508         ,message_type
509         )
510       VALUES
511         (p_online_report_id
512         ,p_login_id
513         ,sysdate
514         ,p_user_id
515         ,sysdate
516         ,0 ,0 ,0
517         ,p_sequence+1
518         ,l_qa_result_tbl(l_row_index).problem_details
519         ,l_qa_result_tbl(l_row_index).message_name
520         ,l_qa_result_tbl(l_row_index).error_severity
521         );
522       l_row_index := l_qa_result_tbl.NEXT(l_row_index);
523       --increment the sequence by the error count
524       p_sequence := p_sequence + 1;
525     END LOOP;
526   END IF;
527 
528 EXCEPTION
529     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
530         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
531     WHEN OTHERS THEN
532         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
533   IF ( g_debug_unexp ) THEN
534             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
535               FND_LOG.string( FND_LOG.level_unexpected,
536                             G_PKG_NAME||l_api_name|| '.others_exception',
537                             'EXCEPTION: Location is '|| l_progress || ' SQL CODE is '||sqlcode);
538             END IF;
539         END IF;
540 
541 END check_terms;
542 /* CONTERMS FPJ END*/
543 
544 PROCEDURE verify_vendor_check(
545    p_document_id          IN NUMBER,
546    p_document_type        IN VARCHAR2,
547    p_document_subtype     IN VARCHAR2,
548    p_online_report_id     IN NUMBER,
549    p_user_id              IN NUMBER,
550    p_login_id             IN NUMBER,
551    p_org_id               IN NUMBER,
552    p_sequence             IN OUT NOCOPY NUMBER,
553    x_return_status        OUT NOCOPY VARCHAR2) IS
554 
555 l_draft_id NUMBER; -- Mod Project
556 l_document_id NUMBER; --Mod Project
557 l_api_name CONSTANT VARCHAR2(30) := 'Verify Vendor Check';
558 l_progress VARCHAR2(3);
559 l_row_index PLS_INTEGER;
560 
561 l_return_status VARCHAR2(1);
562 l_qa_return_status VARCHAR2(1);
563 l_msg_count NUMBER;
564 l_msg_data VARCHAR2(2000);
565 
566 l_doc_type VARCHAR2(30);
567 l_textline  po_online_report_text.text_line%TYPE := NULL;
568 
569 l_revision_num po_headers.revision_num%TYPE;
570 l_authorization_status po_headers.authorization_status%TYPE;
571 l_approved_flag po_headers.approved_flag%TYPE;
572 l_po_start_date po_headers.start_date%TYPE;
573 l_po_end_date po_headers.end_date%TYPE;
574 
575 
576 CURSOR c_po_status (p_document_id NUMBER) IS
577   SELECT revision_num
578          ,start_date
579    ,end_date
580    , nvl(draft_id, -1)  -- Mod Project
581   FROM   po_headers_gt
582   WHERE  po_header_id = p_document_id;
583 
584 CURSOR c_vendor_check_params IS
585 SELECT ENABLE_EPLS , Trunc(EPLS_TIMEFRAME)
586 FROM po_system_parameters_all
587 WHERE org_id = p_org_id;
588 
589 l_enable_epls VARCHAR2(1):='N';
590 l_epls_timeframe NUMBER;
591 
592 CURSOR c_eligibility_status_lookup(cp_lookup_code VARCHAR2) IS
593 SELECT meaning
594 FROM fnd_lookups
595 WHERE lookup_type = 'PO_VENDOR_CHECK_STATUS'
596 AND   lookup_code = cp_lookup_code
597 AND (SYSDATE BETWEEN start_date_active AND Nvl(end_date_active,SYSDATE+1));
598 
599 l_eligible VARCHAR2(30);
600 l_ineligible VARCHAR2(30);
601 
602 CURSOR c_vendor_eligibility IS
603 SELECT (CASE
604        WHEN eligibility_status = l_eligible AND Trunc((SYSDATE - received_Date)*24) <= l_epls_timeframe   THEN 'ELIGIBLE'
605        WHEN approved_without_check = 'Y' THEN 'APPROVED'
606        WHEN eligibility_status = l_ineligible AND Trunc((SYSDATE - received_Date)*24) <= l_epls_timeframe   THEN 'INELIGIBLE'
607        ELSE  'NOTRUN' END)
608 FROM po_vendor_eligibility
609 WHERE doc_id = l_document_id
610 AND   Nvl(draft_id,-1) = Nvl(l_draft_id,-1)
611 AND   doc_type = l_doc_type;
612 
613 l_eligibility VARCHAR2(30):='NOTRUN';
614 mod_action_valid VARCHAR2(1):='Y';
615 
616 BEGIN
617 
618   x_return_status := FND_API.G_RET_STS_SUCCESS;
619 
620   -- contracts document type
621   l_doc_type := p_document_type||'_'||p_document_subtype;
622   l_document_id := p_document_id; --Mod Project
623 
624   -- fetch the document status indicators
625   OPEN c_po_status(p_document_id);
626   FETCH c_po_status INTO l_revision_num, l_po_start_date, l_po_end_date, l_draft_id;
627   CLOSE c_po_status;
628 
629   if l_draft_id <> -1 then --Mod Project
630     l_doc_type := l_doc_type ||'_MOD';
631   end if;
632 
633   IF l_draft_id = -1 THEN
634     mod_action_valid :='Y';
635   ELSE
636     mod_action_valid := PO_VERIFY_VENDOR_ELIGIBILITY.is_modaction_valid(p_document_id,l_draft_id);
637   END IF;
638 
639 IF mod_action_valid = 'Y' THEN
640 
641   OPEN c_vendor_check_params;
642   FETCH c_vendor_check_params INTO l_enable_epls, l_epls_timeframe;
643   CLOSE c_vendor_check_params;
644 
645   OPEN c_eligibility_status_lookup('ELIGIBLE');
646   FETCH c_eligibility_status_lookup INTO l_eligible;
647   CLOSE c_eligibility_status_lookup;
648 
649   OPEN c_eligibility_status_lookup('INELIGIBLE');
650   FETCH c_eligibility_status_lookup INTO l_ineligible;
651   CLOSE c_eligibility_status_lookup;
652 
653   OPEN c_vendor_eligibility;
654   FETCH c_vendor_eligibility INTO l_eligibility;
655   CLOSE c_vendor_eligibility;
656 
657   IF l_enable_epls = 'Y' AND l_eligibility = 'NOTRUN' THEN
658 
659       l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_VENDOR_ELIGIBILITY_NOTRUN');
660 
661       INSERT INTO po_online_report_text_gt
662         (online_report_id
663         ,last_updated_by
664         ,last_update_date
665         ,created_by
666         ,creation_date
667         ,line_num
668         ,shipment_num
669         ,distribution_num
670         ,sequence
671         ,text_line
672         ,message_name
673         ,message_type
674         )
675       VALUES
676         (p_online_report_id
677         ,p_login_id
678         ,sysdate
679         ,p_user_id
680         ,sysdate
681         ,0 ,0 ,0
682         ,p_sequence+1
683         ,l_textline
684         ,'PO_VENDOR_ELIGIBILITY_NOTRUN'
685         ,'E'
686         );
687       --increment the sequence by the error count
688       p_sequence := p_sequence + 1;
689 
690   END IF;
691 
692     IF l_enable_epls = 'Y' AND l_eligibility = 'INELIGIBILE' THEN
693 
694       l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_VENDOR_ELIGIBILITY_INVALID');
695 
696       INSERT INTO po_online_report_text_gt
697         (online_report_id
698         ,last_updated_by
699         ,last_update_date
700         ,created_by
701         ,creation_date
702         ,line_num
703         ,shipment_num
704         ,distribution_num
705         ,sequence
706         ,text_line
707         ,message_name
708         ,message_type
709         )
710       VALUES
711         (p_online_report_id
712         ,p_login_id
713         ,sysdate
714         ,p_user_id
715         ,sysdate
716         ,0 ,0 ,0
717         ,p_sequence+1
718         ,l_textline
719         ,'PO_VENDOR_ELIGIBILITY_INVALID'
720         ,'W'
721         );
722       --increment the sequence by the error count
723       p_sequence := p_sequence + 1;
724 
725   END IF;
726 
727 END IF;
728 
729 
730 EXCEPTION
731     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
732         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
733     WHEN OTHERS THEN
734         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
735   IF ( g_debug_unexp ) THEN
736             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
737               FND_LOG.string( FND_LOG.level_unexpected,
738                             G_PKG_NAME||l_api_name|| '.others_exception',
739                             'EXCEPTION: Location is '|| l_progress || ' SQL CODE is '||sqlcode);
740             END IF;
741         END IF;
742 
743 END verify_vendor_check;
744 
745 
746 PROCEDURE verify_wage_determination(
747    p_document_id          IN NUMBER,
748    p_document_type        IN VARCHAR2,
749    p_document_subtype     IN VARCHAR2,
750    p_online_report_id     IN NUMBER,
751    p_user_id              IN NUMBER,
752    p_login_id             IN NUMBER,
753    p_org_id               IN NUMBER,
754    p_sequence             IN OUT NOCOPY NUMBER,
755    x_return_status        OUT NOCOPY VARCHAR2) IS
756 
757 l_draft_id NUMBER; -- Mod Project
758 l_document_id NUMBER; --Mod Project
759 l_api_name CONSTANT VARCHAR2(30) := 'Verify Wage Determination';
760 l_progress VARCHAR2(3);
761 l_row_index PLS_INTEGER;
762 
763 l_return_status VARCHAR2(1);
764 l_qa_return_status VARCHAR2(1);
765 l_msg_count NUMBER;
766 l_msg_data VARCHAR2(2000);
767 
768 l_doc_type VARCHAR2(30);
769 l_textline  po_online_report_text.text_line%TYPE := NULL;
770 
771 l_revision_num po_headers.revision_num%TYPE;
772 l_authorization_status po_headers.authorization_status%TYPE;
773 l_approved_flag po_headers.approved_flag%TYPE;
774 l_po_start_date po_headers.start_date%TYPE;
775 l_po_end_date po_headers.end_date%TYPE;
776 
777 
778 CURSOR c_po_status (p_document_id NUMBER) IS
779   SELECT revision_num
780          ,start_date
781    ,end_date
782    , nvl(draft_id, -1)  -- Mod Project
783   FROM   po_headers_gt
784   WHERE  po_header_id = p_document_id;
785 
786 
787 CURSOR c_wdol_params IS
788 SELECT ENABLE_WDOL
789 FROM po_system_parameters_all
790 WHERE org_id = p_org_id;
791 
792 l_enable_wdol VARCHAR2(1):='N';
793 l_attachment_pk1 VARCHAR2(100);
794 
795 CURSOR c_wdol_attachment_exists IS
796 SELECT  'Y'
797 FROM fnd_attached_documents attach, fnd_documents_tl doc
798 WHERE pk1_value = l_attachment_pk1
799 AND   attach.document_id = doc.document_id
800 AND   doc.title =  'Wage Determination Statement'
801 AND   doc.description = 'WD Statement URL'
802 AND   doc.LANGUAGE = 'US'
803 AND   attach.category_id = 33;
804 
805 l_wdol_attachment_exists VARCHAR2(1):='N';
806 
807 CURSOR c_not_par IS
808 SELECT 'Y'
809 FROM po_drafts
810 WHERE draft_id = l_draft_id
811 AND draft_type = 'MOD';
812 
813 l_not_par VARCHAR2(1):='N';
814 
815 
816 BEGIN
817 
818   x_return_status := FND_API.G_RET_STS_SUCCESS;
819 
820   -- contracts document type
821   l_doc_type := p_document_type||'_'||p_document_subtype;
822   l_document_id := p_document_id; --Mod Project
823 
824   -- fetch the document status indicators
825   OPEN c_po_status(p_document_id);
826   FETCH c_po_status INTO l_revision_num, l_po_start_date, l_po_end_date, l_draft_id;
827   CLOSE c_po_status;
828 
829   IF Nvl(l_draft_id,-1) = -1 THEN
830     l_attachment_pk1 := l_document_id;
831   ELSE
832     l_attachment_pk1 := l_document_id||'-'||l_draft_id;
833   END IF;
834 
835 
836   if l_draft_id <> -1 then --Mod Project
837     l_doc_type := l_doc_type ||'_MOD';
838   end if;
839 
840   OPEN c_wdol_params;
841   FETCH c_wdol_params INTO l_enable_wdol;
842   CLOSE c_wdol_params;
843 
844   OPEN c_wdol_attachment_exists;
845   FETCH c_wdol_attachment_exists INTO l_wdol_attachment_exists;
846   CLOSE c_wdol_attachment_exists;
847 
848   OPEN c_not_par;
849   FETCH c_not_par INTO l_not_par;
850   CLOSE c_not_par;
851 
852   IF l_enable_wdol = 'Y' AND l_wdol_attachment_exists = 'N' AND l_not_par = 'Y' THEN
853 
854       l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_WDOL_NOTRUN');
855 
856       INSERT INTO po_online_report_text_gt
857         (online_report_id
858         ,last_updated_by
859         ,last_update_date
860         ,created_by
861         ,creation_date
862         ,line_num
863         ,shipment_num
864         ,distribution_num
865         ,sequence
866         ,text_line
867         ,message_name
868         ,message_type
869         )
870       VALUES
871         (p_online_report_id
872         ,p_login_id
873         ,sysdate
874         ,p_user_id
875         ,sysdate
876         ,0 ,0 ,0
877         ,p_sequence+1
878         ,l_textline
879         ,'PO_WDOL_NOTRUN'
880         ,'W'
881         );
882       --increment the sequence by the error count
883       p_sequence := p_sequence + 1;
884 
885   END IF;
886 
887 EXCEPTION
888     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
889         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
890     WHEN OTHERS THEN
891         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
892   IF ( g_debug_unexp ) THEN
893             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
894               FND_LOG.string( FND_LOG.level_unexpected,
895                             G_PKG_NAME||l_api_name|| '.others_exception',
896                             'EXCEPTION: Location is '|| l_progress || ' SQL CODE is '||sqlcode);
897             END IF;
898         END IF;
899 
900 END verify_wage_determination;
901 
902 PROCEDURE verify_validate_pds(
903    p_document_id          IN NUMBER,
904    p_document_type        IN VARCHAR2,
905    p_document_subtype     IN VARCHAR2,
906    p_online_report_id     IN NUMBER,
907    p_user_id              IN NUMBER,
908    p_login_id             IN NUMBER,
909    p_org_id               IN NUMBER,
910    p_sequence             IN OUT NOCOPY NUMBER,
911    x_return_status        OUT NOCOPY VARCHAR2) IS
912 
913 l_draft_id NUMBER; -- Mod Project
914 l_document_id NUMBER; --Mod Project
915 l_api_name CONSTANT VARCHAR2(30) := 'Verify Validate PDS';
916 l_progress VARCHAR2(3);
917 l_row_index PLS_INTEGER;
918 
919 l_return_status VARCHAR2(1);
920 l_qa_return_status VARCHAR2(1);
921 l_msg_count NUMBER;
922 l_msg_data VARCHAR2(2000);
923 
924 l_doc_type VARCHAR2(30);
925 l_textline  po_online_report_text.text_line%TYPE := NULL;
926 
927 l_revision_num po_headers.revision_num%TYPE;
928 l_authorization_status po_headers.authorization_status%TYPE;
929 l_approved_flag po_headers.approved_flag%TYPE;
930 l_po_start_date po_headers.start_date%TYPE;
931 l_po_end_date po_headers.end_date%TYPE;
932 
933 
934 CURSOR c_po_status (p_document_id NUMBER) IS
935   SELECT revision_num
936          ,start_date
937    ,end_date
938    , nvl(draft_id, -1)  -- Mod Project
939   FROM   po_headers_gt
940   WHERE  po_header_id = p_document_id;
941 
942 CURSOR c_eda_params IS
943 SELECT ENABLE_EDA
944 FROM po_system_parameters_all
945 WHERE org_id = p_org_id;
946 
947 l_complete_lookup VARCHAR2(100);
948 
949 CURSOR c_validate_pds_status IS
950 SELECT 'Y'
951 FROM po_electronic_data_access
952 WHERE doc_id = l_document_id
953 AND   doc_type = l_doc_type
954 AND   Nvl(draft_id,-1) = Nvl(l_draft_id,-1)
955 AND   history_record = 'N'
956 AND   approved_without_check = 'Y'
957 UNION
958 SELECT 'Y'
959 FROM po_electronic_data_access
960 WHERE doc_id = l_document_id
961 AND   doc_type = l_doc_type
962 AND   Nvl(draft_id,-1) = Nvl(l_draft_id,-1)
963 AND   history_record = 'Y'
964 AND   status = l_complete_lookup
965 AND   request_date = (SELECT Max(request_date) FROM po_electronic_data_access WHERE doc_id = l_document_id AND doc_type = l_doc_type AND   Nvl(draft_id,-1) = Nvl(l_draft_id,-1) AND history_record = 'Y');
966 
967 l_enable_eda VARCHAR2(1);
968 
969 CURSOR c_eda_status_lookup(cp_lookup_code VARCHAR2) IS
970 SELECT meaning
971 FROM fnd_lookups
972 WHERE lookup_type = 'PO_EDA_COMPLETION_STATUS'
973 AND   lookup_code = cp_lookup_code
974 AND (SYSDATE BETWEEN start_date_active AND Nvl(end_date_active,SYSDATE+1));
975 
976 l_validate_pds_status VARCHAR2(1):='N';
977 
978 CURSOR c_not_par IS
979 SELECT 'Y'
980 FROM po_drafts
981 WHERE draft_id = l_draft_id
982 AND draft_type = 'MOD';
983 
984 l_not_par VARCHAR2(1):='N';
985 
986 
987 BEGIN
988 
989   x_return_status := FND_API.G_RET_STS_SUCCESS;
990 
991   -- contracts document type
992   l_doc_type := p_document_type||'_'||p_document_subtype;
993   l_document_id := p_document_id; --Mod Project
994 
995   -- fetch the document status indicators
996   OPEN c_po_status(p_document_id);
997   FETCH c_po_status INTO l_revision_num, l_po_start_date, l_po_end_date, l_draft_id;
998   CLOSE c_po_status;
999 
1000   if l_draft_id <> -1 then --Mod Project
1001     l_doc_type := l_doc_type ||'_MOD';
1002   end if;
1003 
1004   OPEN c_eda_params;
1005   FETCH c_eda_params INTO l_enable_eda;
1006   CLOSE c_eda_params;
1007 
1008   OPEN c_eda_status_lookup('COMPLETE');
1009   FETCH c_eda_status_lookup INTO l_complete_lookup;
1010   CLOSE c_eda_status_lookup;
1011 
1012   OPEN c_validate_pds_status;
1013   FETCH c_validate_pds_status INTO l_validate_pds_status;
1014   CLOSE c_validate_pds_status;
1015 
1016   OPEN c_not_par;
1017   FETCH c_not_par INTO l_not_par;
1018   CLOSE c_not_par;
1019 
1020 
1021   IF l_enable_eda = 'Y' AND l_validate_pds_status = 'N'  AND l_not_par = 'Y' THEN
1022 
1023 l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_VALIDATE_PDS_NOTRUN');
1024 
1025       INSERT INTO po_online_report_text_gt
1026         (online_report_id
1027         ,last_updated_by
1028         ,last_update_date
1029         ,created_by
1030         ,creation_date
1031         ,line_num
1032         ,shipment_num
1033         ,distribution_num
1034         ,sequence
1035         ,text_line
1036         ,message_name
1037         ,message_type
1038         )
1039       VALUES
1040         (p_online_report_id
1041         ,p_login_id
1042         ,sysdate
1043         ,p_user_id
1044         ,sysdate
1045         ,0 ,0 ,0
1046         ,p_sequence+1
1047         ,l_textline
1048         ,'PO_VALIDATE_PDS_NOTRUN'
1049         ,'W'
1050         );
1051       --increment the sequence by the error count
1052       p_sequence := p_sequence + 1;
1053 
1054   END IF;
1055 
1056 EXCEPTION
1057     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1058         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1059     WHEN OTHERS THEN
1060         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1061   IF ( g_debug_unexp ) THEN
1062             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
1063               FND_LOG.string( FND_LOG.level_unexpected,
1064                             G_PKG_NAME||l_api_name|| '.others_exception',
1065                             'EXCEPTION: Location is '|| l_progress || ' SQL CODE is '||sqlcode);
1066             END IF;
1067         END IF;
1068 
1069 END verify_validate_pds;
1070 /**
1071 * Public Procedure: PO_SUBMISSION_CHECK
1072 * Requires:
1073 *   IN PARAMETERS:
1074 *     p_api_version:       Version number of API that caller expects.
1075 *     p_action_requested:  The action to perform
1076 *     p_document_type:     The type of the document to perform
1077 *                          the submission check on.
1078 *     p_document_subtype:  The subtype of the document.
1079 *                          Valid Document types and Document subtypes are
1080 *                          Document Type      Document Subtype
1081 *                          REQUISITION  --->
1082 *                          RELEASE      --->  SCHEDULED/BLANKET
1083 *                          PO           --->  PLANNED/STANDARD
1084 *                          PA           --->  CONTRACT/BLANKET
1085 --
1086 --  <FPJ ENCUMBRANCE>
1087 --    The following 2 parameters replace the p_document_id parameter.
1088 --p_document_level
1089 --  The type of id that is being passed.  Use g_doc_level_<>
1090 --  The following is supported for all actions:
1091 --    HEADER
1092 --  The following are also supported for UNRESERVE checks (PO/RELEASE):
1093 --    LINE
1094 --    SHIPMENT
1095 --    DISTRIBUTION
1096 --  The following are also supported for FINAL_CLOSE checks (PO/RELEASE):
1097 --    LINE
1098 --    SHIPMENT
1099 --p_document_level_id
1100 --  Id of the doc level type on which to perform the check.
1101 --
1102 *     p_requested_changes: This object contains all the requested changes to
1103 *                          the document. It contains 5 objects. These objects
1104 *                          are: 1.Header_Changes 2.Release_Changes 3.Line_
1105 *                          Changes 4.Shipment_Changes 5.Distribution_Changes.
1106 *                          In FPI, following change requests are allowed:
1107 *                          1. HEADER_CHANGES: None
1108 *                          2. RELEASE_CHANGES: None
1109 *                          3. LINE_CHANGES: unit_price, vendor_product_num
1110 *                          4. SHIPMENT_CHANGES: quantity, promised_date,
1111 *                             price_override
1112 *                          5. DISTRIBUTION_CHANGES: quantity_ordered
1113 *     p_check_asl:         Determines whether or not to perform the checks:
1114 *                          PO_SUB_ITEM_NOT_APPROVED / PO_SUB_ITEM_ASL_DEBARRED
1115 *                          (a) TRUE  : Perform check
1116 *                          (b) FALSE : Do not perform check
1117 *     p_req_chg_initiator: Caller of the change request if its a change request
1118 *
1119 * Modifies: Inserts error msgs in online_report_text table, uses global_temp
1120 *           tables for processing
1121 * Effects:  This procedure runs the document submission checks on passed in
1122 *           document.
1123 * Returns:
1124 *  x_return_status:    FND_API.G_RET_STS_SUCCESS if API succeeds
1125 *                      FND_API.G_RET_STS_ERROR if API fails
1126 *                      FND_API.G_RET_STS_UNEXP_ERROR if unexpected error occurs
1127 *  x_sub_check_status: FND_API.G_RET_STS_SUCCESS if document passes all
1128 *                      submission checks, even if warnings are found
1129 *                      FND_API.G_RET_STS_ERROR if document fails atleast one
1130 *                      submission check
1131 *  x_has_warnings:     FND_API.G_TRUE if submission check returns warnings
1132 *                      FND_API.G_FALSE if no warnings are found
1133 *  x_msg_data:         Contains error msg in case x_return_status returned
1134 *                      FND_API.G_RET_STS_ERROR or FND_API.G_RET_STS_UNEXP_ERROR
1135 *  x_online_report_id: This id can be used to get all submission check errors
1136 *                      for given document from online_report_text table
1137 *  x_doc_check_error_record: If x_sub_check_status returned G_RET_STS_ERROR
1138 *                      then this object of tables will contain information about
1139 *                      all submission check errors for given document including
1140 *                      message_name and text_line.
1141 *  NOTE: This package does no validation of INPUT parameters. That is taken
1142 *        care of in Group package PO_DOCUMENT_CHECKS_GRP
1143 */
1144 PROCEDURE po_submission_check(
1145    p_api_version                    IN             NUMBER
1146 ,  p_action_requested               IN             VARCHAR2
1147 ,  p_document_type                  IN             VARCHAR2
1148 ,  p_document_subtype               IN             VARCHAR2
1149 -- <ENCUMBRANCE FPJ> Replaced p_document_id with doc_level, doc_level_id
1150 ,  p_document_level                 IN             VARCHAR2
1151 ,  p_document_level_id              IN             NUMBER
1152 -- <PO_CHANGE_API FPJ> Renamed the type to PO_CHANGES_REC_TYPE:
1153 ,  p_requested_changes              IN             PO_CHANGES_REC_TYPE
1154 ,  p_check_asl                      IN             BOOLEAN  -- bug 2757450
1155 ,  p_draft_id                         IN             NUMBER := -1
1156 ,  p_req_chg_initiator              IN             VARCHAR2 -- bug 4957243
1157 ,  p_origin_doc_id                  IN             NUMBER := NULL --Bug#5462677
1158 ,  x_return_status                  OUT NOCOPY     VARCHAR2
1159 ,  x_sub_check_status               OUT NOCOPY     VARCHAR2
1160 ,  x_has_warnings                   OUT NOCOPY     VARCHAR2  -- bug3574165
1161 ,  x_msg_data                       OUT NOCOPY     VARCHAR2
1162 ,  x_online_report_id               OUT NOCOPY     NUMBER
1163 ,  x_doc_check_error_record         OUT NOCOPY     doc_check_Return_Type
1164 )
1165 IS
1166 
1167 l_api_name              CONSTANT varchar2(30) := 'PO_SUBMISSION_CHECK';
1168 l_api_version           CONSTANT NUMBER       := 2.0;
1169 l_log_head     CONSTANT VARCHAR2(100) := g_log_head||l_api_name;
1170 l_progress              VARCHAR2(8); --changed from 3 to 8 for bug 13481176
1171 
1172 l_document_id  NUMBER;
1173 l_id_tbl    po_tbl_number;
1174 
1175 l_num_messages NUMBER := 0;  -- bug3574165: Changed l_num_errors to l_num_messages
1176 l_num_warnings NUMBER := 0;  -- bug3574165
1177 
1178 l_online_report_id  NUMBER;
1179 l_user_id    po_lines.last_updated_by%TYPE := -1;
1180 l_login_id   po_lines.last_update_login%TYPE := -1;
1181 p_sequence   po_online_report_text.sequence%TYPE :=0;
1182 
1183 l_return_status varchar2(1);
1184 
1185 l_conterms_exist_flag VARCHAR2(1); -- <CONTERMS FPJ>
1186 l_po_header_id PO_HEADERS_ALL.po_header_id%TYPE; --<JFMIP Vendor Registration FPJ>
1187 
1188  -- BELOW CODE FOR CLM PROJECT
1189 l_functional_area_code      VARCHAR2(500);
1190 l_document_type_code        VARCHAR2(500);
1191 l_document_style_id         NUMBER;
1192 l_enabled_flag              VARCHAR2(5);
1193 l_errorcode                 VARCHAR2(50);
1194 l_msg_count                 NUMBER;
1195 l_msg_data                  VARCHAR2(4000);
1196 l_external_attr_value_pairs EGO_COL_NAME_VALUE_PAIR_TABLE;
1197 l_failure                   BOOLEAN;
1198 ext_attr_value_pairs_ind    NUMBER;
1199 ext_name_val_obj            EGO_COL_NAME_VALUE_PAIR_OBJ;
1200 l_return_clm_err_message    VARCHAR2(200);
1201 l_print_doc_type            PO_PRINT_FORM_FORMATS.document_type%TYPE;
1202 msg_counter                 NUMBER;
1203 l_ret_clm_err_msg_txt       VARCHAR2(2000);
1204 l_is_ord_off_idv            varchar2(1) := 'N';
1205 l_is_clm_doc                varchar2(1) := 'N';
1206 l_contracting_officer       NUMBER;
1207 
1208 -- BELOW CODE FOR FPDS-NG PROJECT
1209 l_reporting_method VARCHAR2(60);
1210 l_car_status VARCHAR2(60);
1211 l_approved_without_reporting VARCHAR2(1);
1212 
1213 --FedBizOpps Project
1214 l_no_comp_reason VARCHAR2(100);
1215 is_ja_notice_posted VARCHAR2(1) := 'N';
1216 l_draft_sub_type PO_DRAFTS.draft_sub_type%TYPE;
1217 l_org_id NUMBER; -- CLM Controls Project changes
1218 -- Payment Instruction
1219 l_enable_payment_inst VARCHAR2(1);
1220 -- UCA Project CLM R4 changes Begin
1221 l_isucaenabled VARCHAR2(1);
1222 -- UCA Project CLM R4 Changes End
1223 -- PAR Project
1224 l_draft_type VARCHAR2(30);
1225 -- Elins project
1226 l_isexhibitenabled VARCHAR2(1);
1227 --SBCR Integration Changes Begin.
1228 l_isSbcrEnabled VARCHAR2(1);
1229 --SBCR Integration Changes End.
1230 BEGIN
1231 
1232 IF g_debug_stmt THEN
1233    PO_DEBUG.debug_begin(l_log_head);
1234    PO_DEBUG.debug_var(l_log_head,l_progress,'p_api_version',p_api_version);
1235    PO_DEBUG.debug_var(l_log_head,l_progress,'p_action_requested',p_action_requested);
1236    PO_DEBUG.debug_var(l_log_head,l_progress,'p_document_type',p_document_type);
1237    PO_DEBUG.debug_var(l_log_head,l_progress,'p_document_subtype',p_document_subtype);
1238    PO_DEBUG.debug_var(l_log_head,l_progress,'p_document_level',p_document_level);
1239    PO_DEBUG.debug_var(l_log_head,l_progress,'p_document_level_id',p_document_level_id);
1240    PO_DEBUG.debug_var(l_log_head,l_progress,'p_check_asl',p_check_asl);
1241 END IF;
1242 
1243 --Standard Start og API savepoint
1244 SAVEPOINT PO_SUBMISSION_CHECK_SP;
1245 
1246 l_progress := '000';
1247     -- Standard call to check for call compatibility
1248     IF NOT FND_API.Compatible_API_Call(l_api_version, p_api_version, l_api_name, G_PKG_NAME)
1249     THEN
1250           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1251     END IF;
1252 
1253 l_progress := '001';
1254 
1255 IF g_debug_stmt THEN
1256    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1257      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
1258           || l_progress,'Populating global temp tables');
1259    END IF;
1260 END IF;
1261 
1262 
1263 PO_CORE_S.get_document_ids(
1264    p_doc_type => p_document_type
1265 ,  p_doc_level => p_document_level
1266 ,  p_doc_level_id_tbl => po_tbl_number( p_document_level_id )
1267 ,  x_doc_id_tbl => l_id_tbl
1268 );
1269 
1270 l_document_id := l_id_tbl(1);
1271 
1272 IF g_debug_stmt THEN
1273    PO_DEBUG.debug_var(l_log_head,l_progress,'l_document_id',l_document_id);
1274 END IF;
1275 
1276 
1277 -- bug3413891
1278 -- GT table cleanup has moved to populate_global_temp_tables procedure
1279 
1280 --Populate global temp tables with data from main PO tables for
1281 --given PO_HEADER_ID
1282 
1283 populate_global_temp_tables(
1284    x_return_status => l_return_status
1285 ,  p_doc_type => p_document_type
1286 ,  p_doc_level => p_document_level
1287 ,  p_doc_level_id => p_document_level_id
1288 ,  p_draft_id => p_draft_id    -- CLM Aprvl
1289 );
1290 
1291     IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1292         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1293     END IF;
1294 
1295 l_progress := '002';
1296     --if the p_requested_changes is NOT NULL then update the global temp
1297     --tables with these changes
1298     IF p_requested_changes IS NOT NULL THEN
1299 
1300 IF g_debug_stmt THEN
1301    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1302      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
1303           || l_progress,'update requested, calling updating global tables');
1304    END IF;
1305 END IF;
1306 
1307         update_global_temp_tables(p_document_type,
1308                                 p_document_subtype,
1309                                 l_document_id,
1310                                 p_requested_changes,
1311                                 l_return_status);
1312 
1313         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1314                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1315         END IF;
1316 
1317     END IF;
1318 
1319 IF g_debug_stmt THEN
1320    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1321      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
1322           || l_progress,'Getting online_report_id');
1323    END IF;
1324 END IF;
1325 
1326 l_progress := '003';
1327     --Get the unique id to be used for this document
1328     SELECT PO_ONLINE_REPORT_TEXT_S.nextval
1329     INTO   l_online_report_id
1330     FROM   sys.dual;
1331 
1332 l_progress := '004';
1333     --Get User ID and Login ID
1334     l_user_id := FND_GLOBAL.USER_ID;
1335     IF (FND_GLOBAL.CONC_LOGIN_ID >= 0) THEN
1336         l_login_id := FND_GLOBAL.CONC_LOGIN_ID;
1337     ELSE
1338         l_login_id := FND_GLOBAL.LOGIN_ID;
1339     END IF;
1340 
1341 IF g_debug_stmt THEN
1342      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1343        FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name||'.'
1344           || l_progress,'user_id '|| to_char(l_user_id) || 'Login_id ' || to_char(l_login_id));
1345      END IF;
1346 END IF;
1347 
1348     -- <Bug 7655436 Start>
1349     -- Call the custom code hook
1350     PO_CUSTOM_SUBMISSION_CHECK_PVT.do_pre_submission_check(
1351       p_api_version         => 1.0,
1352       P_document_id         => l_document_id,
1353       p_action_requested    => p_action_requested,
1354       p_document_type       => p_document_type,
1355       p_document_subtype    => p_document_subtype,
1356       p_document_level      => p_document_level,
1357       p_document_level_id   => p_document_level_id,
1358       p_requested_changes   => p_requested_changes,
1359       p_check_asl           => p_check_asl,
1360       p_req_chg_initiator   => p_req_chg_initiator,
1361       p_origin_doc_id       => p_origin_doc_id,
1362       p_online_report_id    => l_online_report_id,
1363       p_user_id             => l_user_id,
1364       p_login_id            => l_login_id,
1365       p_sequence            => p_sequence,
1366       x_return_status       => l_return_status
1367     );
1368 
1369     IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR
1370     THEN
1371       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1372     END IF;
1373     -- <Bug 7655436 End>
1374 
1375     -- SGD PROJECT VALIDATIONS AT THE TIME OF SUBMISSION START
1376     PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'SGD validation starts');
1377     l_draft_type := 'AWARD';
1378     IF p_draft_id IS NOT NULL THEN
1379     BEGIN
1380 	  SELECT draft_type, draft_sub_type
1381 	  INTO l_draft_type, l_draft_sub_type
1382           FROM po_drafts
1383           WHERE draft_id = p_draft_id;
1384 	EXCEPTION
1385     	WHEN OTHERS THEN
1386 	  l_draft_type := 'AWARD';
1387           l_draft_sub_type := null;
1388     END;
1389   END IF;
1390 
1391     --Mod Commercialization : Added check to make this validation only for CLM Mods
1392     -- Sunset Memo Project : Skip SGD generation, if Mod is of Special Draft type
1393     IF ( (PO_CLM_INTG_GRP.is_clm_document(p_doc_type => p_document_type,
1394                                           p_document_id => l_document_id)='Y' )
1395     AND (l_draft_type = 'MOD') -- Change Desc only for Mod.
1396     AND ( l_draft_sub_type IS NULL OR PO_CORE_S3.get_bypass_sgd_generation_flag(l_draft_sub_type) <> 'Y') ) THEN
1397 
1398     PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'SGD validation starts');
1399 
1400     SELECT  Nvl(Max(SEQUENCE), 0) + 1
1401     INTO    p_sequence
1402     FROM    po_online_report_text_gt
1403     WHERE   online_report_id = x_online_report_id;
1404     PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'p_sequence' ||p_sequence);
1405 
1406     validate_genchangedesc(p_draft_id              => p_draft_id
1407                         ,p_header_id             => l_document_id
1408                         ,p_online_report_id      => l_online_report_id
1409                         ,p_user_id               => l_user_id
1410                         ,p_login_id              => l_login_id
1411                         ,p_sequence              => p_sequence
1412                         ,x_return_status         => l_return_status);
1413     PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'l_return_status' ||l_return_status);
1414             IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1415                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1416             ELSIF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1417                 NULL;
1418              END IF;
1419     END IF;
1420     -- SGD PROJECT VALIDATIONS AT THE TIME OF SUBMISSION END
1421 
1422 IF  (p_document_type IN ('PO', 'PA'))
1423     AND (p_document_subtype IN ('STANDARD', 'BLANKET', 'CONTRACT'))
1424     THEN
1425         -- CONTRACTING OFFICER VALIDATION
1426         --IF l_is_clm_doc= 'Y' THEN
1427         IF (PO_CLM_INTG_GRP.is_clm_document(p_doc_type => p_document_type,
1428                                               p_document_id => l_document_id)='Y'
1429         AND PO_DRAFT_MERGE_PKG.G_RESERVE_FROM_MOD_MERGE ='N' )-- Bug 14046925
1430         THEN
1431             IF l_draft_type in ('PAR', 'MOD') THEN
1432                 SELECT      clm_contract_officer
1433                 INTO        l_contracting_officer
1434                 FROM        po_drafts
1435                 WHERE       draft_id = p_draft_id;
1436             ELSE
1437                 SELECT      clm_contract_officer
1438                 INTO        l_contracting_officer
1439                 FROM        po_headers_all
1440                 WHERE       po_header_id = l_document_id;
1441             END IF; -- end if l_draft_type
1442 
1443 	    -- No need to call validate_co_warrant in PAR.
1444 	    IF l_draft_type <> 'PAR' THEN
1445             PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'Calling validate_co_warrant');
1446 
1447             validate_co_warrant(p_draft_id              => p_draft_id
1448                                 ,p_header_id             => l_document_id
1449                                 ,p_contracting_officer   => l_contracting_officer
1450                                 ,p_user_id               => l_user_id
1451                                 ,p_login_id              => l_login_id
1452                                 ,p_online_report_id      => l_online_report_id
1453                                 ,p_sequence              => p_sequence
1454                                 ,x_return_status         => l_return_status);
1455             PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'l_return_status' ||l_return_status);
1456             IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1457                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1458             END IF;
1459 
1460 	    END IF;
1461 
1462          -- Supplier Site Debarment Validation
1463          IF (p_action_requested = g_action_DOC_SUBMISSION_CHECK) THEN
1464            PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, ' Calling validate_supplier_debarred');
1465               validate_supplier_debarred(  p_draft_id              => p_draft_id
1466                                           ,p_header_id             => l_document_id
1467                                           ,p_online_report_id      => l_online_report_id
1468                                           ,p_user_id               => l_user_id
1469                                           ,p_login_id              => l_login_id
1470                                           ,p_sequence              => p_sequence
1471                                           ,x_return_status         => l_return_status);
1472                   PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'l_return_status' ||l_return_status);
1473                   IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1474                       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1475                   END IF;
1476 
1477               END IF;
1478 
1479         END IF; -- l_clm_document = 'Y' THEN
1480     END IF; --IF  (p_document_type IN ('PO', 'PA'))
1481     -- BELOW CODE FOR CLM PROJECT
1482     IF  (p_document_type IN ('PO', 'PA'))
1483     AND (p_document_subtype IN ('STANDARD', 'BLANKET', 'CONTRACT'))
1484     AND (p_action_requested = g_action_DOC_SUBMISSION_CHECK)
1485     THEN
1486 
1487         l_progress := '680';
1488 
1489         -- CLM Controls Project changes
1490 	-- Added Org_id in the query
1491         SELECT 'PURCHASING', PH.TYPE_LOOKUP_CODE, PH.STYLE_ID, nvl(PDSH.CLM_FLAG, 'N'), PH.ORG_ID
1492         INTO    l_functional_area_code, l_document_type_code, l_document_style_id, l_is_clm_doc, l_org_id
1493         FROM    po_headers_all PH, po_doc_style_headers PDSH
1494         WHERE   PH.PO_HEADER_ID = l_document_id
1495         AND     PH.style_id = PDSH.style_id;
1496 
1497         PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'Fetched template details');
1498         PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'l_functional_area_code :' || l_functional_area_code);
1499         PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'l_document_type_code : ' || l_document_type_code);
1500         PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'l_document_style_id : ' || l_document_style_id);
1501         PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'l_is_clm_doc : ' || l_is_clm_doc);
1502 
1503         l_progress := '690';
1504         PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'Starting effective check for print form formats');
1505 
1506         -- Standard form and document format should not be null and
1507         -- not expired on the current date.
1508         IF p_draft_id <> -1 THEN
1509             l_print_doc_type := 'PO_MOD_STD_FORM';
1510 	else
1511 	-- bug 10389450 start
1512 	begin
1513 		select 'Y'
1514 		into l_is_ord_off_idv
1515 		from po_headers_all
1516 		where po_header_id = l_document_id and clm_award_type in ('BPA_CALL', 'DELIVERY_ORD');
1517 	exception
1518 	when no_data_found then
1519 	null;
1520 	end;
1521        -- bug 10389450 end
1522 
1523 		if l_is_ord_off_idv = 'Y' then
1524 		  l_print_doc_type := 'PO_DEL_ORD_STD_FORM';
1525 		else
1526 		  IF p_document_subtype = 'STANDARD' THEN
1527 		    l_print_doc_type := 'PO_AWARD_STD_FORM';
1528 		  ELSIF p_document_subtype = 'BLANKET' THEN
1529 		    l_print_doc_type := 'PO_IDV_STD_FORM';
1530 		  ELSIF p_document_subtype = 'CONTRACT' THEN
1531 		    l_print_doc_type := 'PO_IDV_STD_FORM';
1532 		  end if;
1533 		end if;
1534         END IF;
1535 
1536         PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'l_print_doc_type :' || l_print_doc_type);
1537 
1538         IF ( (p_draft_id <> -1) AND (l_is_clm_doc = 'Y') ) THEN --Mod Commercialization
1539             INSERT INTO po_online_report_text_gt
1540             (
1541                online_report_id,
1542                last_update_login,
1543                last_updated_by,
1544                last_update_date,
1545                created_by,
1546                creation_date,
1547                line_num,
1548                shipment_num,
1549                distribution_num,
1550                sequence,
1551                text_line,
1552                message_name,
1553                message_type
1554             )
1555             SELECT
1556                l_online_report_id,
1557                l_login_id,
1558                l_user_id,
1559                SYSDATE,
1560                l_user_id,
1561                SYSDATE,
1562                0,
1563                0,
1564                0,
1565                p_sequence +1,
1566                FND_MESSAGE.GET_STRING('PO', 'PO_PRINT_FORM_FORMAT_INVALID'),
1567                'PO_PRINT_FORM_FORMAT_INVALID',
1568                'E'
1569             FROM    po_drafts phmv
1570             WHERE   phmv.draft_id = p_draft_id
1571             AND     NOT EXISTS (
1572                     SELECT 1
1573                     FROM    po_print_form_formats ppff
1574                     WHERE   ppff.document_type = l_print_doc_type
1575                     AND     ppff.standard_form = phmv.clm_standard_form
1576                     AND     ppff.document_format = phmv.clm_document_format
1577                     AND     trunc(sysdate) <= trunc(nvl(ppff.inactive_date, sysdate +1))
1578                     )
1579             AND     Nvl(phmv.draft_type, '*') <> 'PAR'; --<PAR Project>
1580 
1581         ELSIF (l_is_clm_doc = 'Y') THEN
1582             INSERT INTO po_online_report_text_gt
1583             (
1584                online_report_id,
1585                last_update_login,
1586                last_updated_by,
1587                last_update_date,
1588                created_by,
1589                creation_date,
1590                line_num,
1591                shipment_num,
1592                distribution_num,
1593                sequence,
1594                text_line,
1595                message_name,
1596                message_type
1597             )
1598             SELECT
1599                l_online_report_id,
1600                l_login_id,
1601                l_user_id,
1602                SYSDATE,
1603                l_user_id,
1604                SYSDATE,
1605                0,
1606                0,
1607                0,
1608                p_sequence +1,
1609                FND_MESSAGE.GET_STRING('PO', 'PO_PRINT_FORM_FORMAT_INVALID'),
1610                'PO_PRINT_FORM_FORMAT_INVALID',
1611                'E'
1612             FROM    po_headers_all phmv
1613             WHERE   phmv.po_header_id = l_document_id
1614             AND     NOT EXISTS (
1615                     SELECT 1
1616                     FROM    po_print_form_formats ppff
1617                     WHERE   ppff.document_type = l_print_doc_type
1618                     AND     ppff.standard_form = phmv.clm_standard_form
1619                     AND     ppff.document_format = phmv.clm_document_format
1620                     AND     trunc(sysdate) <= trunc(nvl(ppff.inactive_date, sysdate +1))
1621                     );
1622         END IF;
1623 
1624         PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress,
1625                 'No of errors logged into the error report table'||sql%rowcount);
1626 
1627         l_progress := '700';
1628         PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'Starting code for UDA Submission check');
1629 
1630 
1631         l_progress := '701';
1632 
1633         IF PO_CLM_CLO_UTIL.isCLMInstalled THEN
1634             PO_CLM_COLS_SUB_CHECK.CLM_NAMED_COLS_SUB_CHK
1635             (
1636                 p_document_id           => l_document_id
1637                ,p_draft_id              => p_draft_id
1638                ,p_online_report_id      => l_online_report_id
1639                ,p_login_id              => l_login_id
1640                ,p_user_id               => l_user_id
1641                ,p_sequence              => p_sequence
1642                ,x_return_message        => l_return_clm_err_message
1643                ,x_return_status         => l_return_status
1644                ,x_errorcode             => l_errorcode
1645                ,x_msg_count             => l_msg_count
1646                ,x_msg_data              => l_msg_data
1647             );
1648 
1649             PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'After CLM_NAMED_COLS_SUB_CHK with x_return_status : ' || l_return_status);
1650             IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1651                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1652             ELSIF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1653                 -- ALREADY LOGED INTO REPORT TABLE in PO_CLM_COLS_SUB_CHECK.LOG_ERR_CLM_NMD_COLS_SUB_CHK
1654                 NULL;
1655             END IF;
1656 
1657             --Submission check to validate total amount in UDA = total amount on the line
1658            /*CLM Phase-2 Changes*/
1659            INSERT INTO po_online_report_text_gt
1660                (  online_report_id,
1661                   last_update_login,
1662                   last_updated_by,
1663                   last_update_date,
1664                   created_by,
1665                   creation_date,
1666                   shipment_num,
1667                   distribution_num,
1668                   sequence,
1669                   text_line,
1670                   message_name,
1671                   message_type,
1672                   line_num
1673                )
1674                SELECT
1675                   l_online_report_id,
1676                   l_login_id,
1677                   l_user_id,
1678                   SYSDATE,
1679                   l_user_id,
1680                   SYSDATE,
1681                   0,
1682                   0,
1683                   p_sequence+1,
1684                   FND_MESSAGE.GET_STRING('PO', 'PO_PRICING_MISMATCH'),
1685                   'PO_PRICING_MISMATCH',
1686                   'E',
1687                   pol.line_num
1688                   FROM po_lines_gt pol,
1689                        po_lines_all_ext_b ple,
1690                        po_uda_ag_template_usages ptu
1691                   WHERE ple.po_line_id = pol.po_line_id
1692                   AND   ple.draft_id = p_draft_id
1693                   AND   ple.attr_group_id = ptu.attribute_group_id
1694                   AND   ptu.attribute_category = 'PRICING'
1695                   AND   ptu.attribute2 = pol.contract_type
1696                   AND   ptu.attribute1 = pol.clm_idc_type
1697                   AND   ptu.template_id = ple.uda_template_id
1698                   AND   Nvl(ple.n_ext_attr20, 0) <>
1699                         Decode(pol.matching_basis, 'QUANTITY',
1700                               (Nvl(pol.quantity, 0) * Nvl(pol.unit_price, 0)),
1701                                'AMOUNT', Nvl(pol.amount, 0))
1702                   AND   pol.order_type_lookup_code <> 'QUANTITY'
1703                   AND   pol.clm_undef_flag <> 'Y' -- UCA Project
1704                   --Bug 13543817
1705                   AND   ple.pk1_value IS NULL;
1706 
1707            PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress,
1708                   'No of errors logged into the error report table for PO_PRICING_MISMATCH '||sql%rowcount);
1709 
1710            INSERT INTO po_online_report_text_gt
1711                (  online_report_id,
1712                   last_update_login,
1713                   last_updated_by,
1714                   last_update_date,
1715                   created_by,
1716                   creation_date,
1717                   shipment_num,
1718                   distribution_num,
1719                   sequence,
1720                   text_line,
1721                   message_name,
1722                   message_type,
1723                   line_num
1724                )
1725                SELECT
1726                   l_online_report_id,
1727                   l_login_id,
1728                   l_user_id,
1729                   SYSDATE,
1730                   l_user_id,
1731                   SYSDATE,
1732                   0,
1733                   0,
1734                   p_sequence+1,
1735                   FND_MESSAGE.GET_STRING('PO', 'PO_PRICING_MISMATCH'),
1736                   'PO_PRICING_MISMATCH',
1737                   'E',
1738                   pol.line_num
1739                   FROM po_lines_gt pol
1740                   WHERE pol.order_type_lookup_code <> 'QUANTITY'
1741                   AND NOT EXISTS ( Select 1
1742                                    FROM  po_uda_ag_template_usages ptu,
1743                                          po_lines_all_ext_b ple
1744                                    WHERE ptu.attribute_category = 'PRICING'
1745                                    AND   ptu.attribute2 = pol.contract_type
1746                                    AND   ptu.attribute1 = pol.clm_idc_type
1747                                    AND   ptu.template_id = ple.uda_template_id
1748                                    AND   ple.po_line_id = pol.po_line_id
1749                                    AND   ple.draft_id = p_draft_id
1750                                    AND   ple.attr_group_id = ptu.attribute_group_id
1751                                  )
1752                   --Bug 13543817
1753                   AND ( EXISTS ( SELECT 1 FROM po_lines_draft_all
1754                                  WHERE draft_id = p_draft_id
1755                                  AND po_line_id = pol.po_line_id
1756                                 )
1757                         OR nvl(p_draft_id,-1) = -1
1758                       );
1759            PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress,
1760                   'No of errors logged into the error report table when no attribute group id was found '||sql%rowcount);
1761            /*CLM-PHASE2 changes END*/
1762         END IF;
1763 
1764         l_progress := '702';
1765         PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'Before Check_Uda_Enabled');
1766 
1767         PO_CLM_CLO_UTIL.Check_Uda_Enabled
1768         (
1769             p_functional_area_code  => l_functional_area_code
1770            ,p_document_type_code    => l_document_type_code
1771            ,p_document_style_id     => l_document_style_id
1772            ,x_enabled_flag          => l_enabled_flag
1773            ,x_return_status         => l_return_status
1774            ,x_errorcode             => l_errorcode
1775            ,x_msg_count             => l_msg_count
1776            ,x_msg_data              => l_msg_data
1777         );
1778 
1779         l_progress := '703';
1780         PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'After Check_Uda_Enabled with x_return_status : ' || l_return_status);
1781         PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'Check_Uda_Enabled with x_enabled_flag : ' || l_enabled_flag);
1782 
1783 
1784         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR
1785         THEN
1786            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1787         END IF;
1788 
1789         IF l_enabled_flag = 'Y' THEN
1790 
1791             l_progress := '704';
1792             PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'Before calling PO_UDA_SUBMISSION_CHECK');
1793             FND_MSG_PUB.Initialize;
1794 
1795             PO_UDA_DEFAULTING_PKG.PO_UDA_SUBMISSION_CHECK
1796             (
1797                 p_po_header_id              => l_document_id
1798                ,p_draft_id                  => p_draft_id
1799                ,x_external_attr_value_pairs => l_external_attr_value_pairs
1800                ,x_return_status             => l_return_status
1801                ,x_msg_count                 => l_msg_count
1802             );
1803 
1804             l_progress := '705';
1805             PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'After calling PO_UDA_SUBMISSION_CHECK x_return_status :' || l_return_status);
1806 
1807             IF l_return_status <> FND_API.G_RET_STS_SUCCESS
1808             THEN
1809               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1810             END IF;
1811 
1812 	    --FedBizOpps Project
1813             IF l_draft_type = 'AWARD' THEN -- <Bug 16512779> FedBizOpps only for Award.
1814 
1815               SELECT PO_UDA_PUB.GET_SINGLE_ATTR_VALUE(p_template_id => pha.uda_template_id, pk1_value => pha.po_header_id,
1816                                                       pk2_value => -1, p_attr_grp_int_name => 'AWD_CTGR', p_attr_int_name => 'NO_COMP_RSN')
1817               INTO l_no_comp_reason
1818               FROM po_headers_all pha
1819               WHERE pha.po_header_id =  l_document_id;
1820 
1821               IF (l_no_comp_reason IS NOT NULL
1822                    AND l_no_comp_reason <> 'NONE') THEN
1823 
1824                   BEGIN
1825 
1826                   SELECT 'Y' INTO is_ja_notice_posted
1827                   FROM pon_fbo_posts
1828                   WHERE document_id =  l_document_id
1829                   AND FORM_CODE  =  'FED_JA'
1830                   AND fbo_post_status = 'SUCCESS'
1831                   AND ROWNUM =1;
1832 
1833                   EXCEPTION
1834 
1835                   WHEN No_Data_Found THEN
1836                     INSERT INTO po_online_report_text_gt
1837                     (
1838                        online_report_id,
1839                        last_update_login,
1840                        last_updated_by,
1841                        last_update_date,
1842                        created_by,
1843                        creation_date,
1844                        line_num,
1845                        shipment_num,
1846                        distribution_num,
1847                        sequence,
1848                        text_line,
1849                        message_name,
1850                        message_type
1851                     )
1852                     VALUES
1853                     (
1854                        l_online_report_id,
1855                        l_login_id,
1856                        l_user_id,
1857                        SYSDATE,
1858                        l_user_id,
1859                        SYSDATE,
1860                        0,
1861                        0,
1862                        0,
1863                        p_sequence+1,
1864                        FND_MESSAGE.GET_STRING('PO', 'PO_FBO_JA_NOT_POSTED'),
1865                        'PO_FBO_JA_NOT_POSTED',
1866                        'W'
1867                     );
1868                   END;
1869 
1870               END IF;
1871 
1872             END IF;
1873 
1874             l_progress := '706';
1875             PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'Before looping external parameters for errors to log on to the report');
1876 
1877             l_failure := FALSE;
1878 
1879             PO_UDA_DEFAULTING_PKG.GET_EXT_ATTR_NAME_VALUE_PAIR('x_return_status', l_external_attr_value_pairs, ext_name_val_obj);
1880             x_return_status :=   ext_name_val_obj.VALUE ;
1881 
1882             PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'Submission check has status ' || x_return_status);
1883 
1884             IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1885                 l_progress := '708';
1886                 PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'Submission check has failed should log onto the report');
1887                 l_failure := TRUE;
1888             END IF;
1889 
1890             l_progress := '709';
1891             PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'Before logging the online failure report');
1892             l_msg_count := FND_MSG_PUB.Count_Msg;
1893 
1894             IF l_failure THEN
1895                FOR msg_counter IN 1 ..l_msg_count
1896                LOOP
1897                     INSERT INTO po_online_report_text_gt
1898                     (
1899                        online_report_id,
1900                        last_update_login,
1901                        last_updated_by,
1902                        last_update_date,
1903                        created_by,
1904                        creation_date,
1905                        line_num,
1906                        shipment_num,
1907                        distribution_num,
1908                        sequence,
1909                        text_line,
1910                        message_name,
1911                        message_type
1912                     )
1913                     VALUES
1914                     (
1915                        l_online_report_id,
1916                        l_login_id,
1917                        l_user_id,
1918                        SYSDATE,
1919                        l_user_id,
1920                        SYSDATE,
1921                        0,
1922                        0,
1923                        0,
1924                        p_sequence,
1925                        FND_MSG_PUB.get (msg_counter, FND_API.g_false),
1926                        'UDA Attr Sub Check Failure',
1927                        'E'
1928                     );
1929 
1930                     PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'Submission check has status ' || FND_MSG_PUB.get (msg_counter, FND_API.g_false));
1931 
1932                END LOOP;
1933             END IF;
1934         END IF;
1935 
1936         l_progress := '713';
1937         PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'After code for UDA Submission check');
1938 
1939         IF (l_is_clm_doc = 'Y')
1940         AND  (p_document_type IN ('PA'))
1941         AND (p_document_subtype IN ('BLANKET', 'CONTRACT')) THEN
1942             l_progress := '713';
1943             PO_DEBUG.debug_stmt(l_log_head || l_api_name, l_progress, 'CLM IDV order constraints validation');
1944 
1945             val_pa_ord_constraints(
1946                    l_document_id
1947                 ,  l_online_report_id
1948                 ,  l_user_id
1949                 ,  l_login_id
1950                 ,  p_sequence
1951                 ,  l_return_status
1952                 );
1953         END IF;
1954 
1955   -- For PAR, FPDS-NG (CAR) is not applicable. Therefore, the related validations needs to be skipped.
1956 	-- BELOW CODE FOR FPDS-NG PROJECT
1957         IF (l_is_clm_doc = 'Y' AND l_draft_type <> 'PAR' ) AND
1958 	  -- CLM Controls Project changes
1959 	  --(NVL(FND_PROFILE.VALUE('PO_CLM_ENABLE_FPDS_REPORTING'),'N') = 'Y') THEN
1960           (NVL(PO_CORE_S.retrieveOptionValue(p_org_id        => l_org_id,
1961 	                                     p_option_column => PO_CORE_S.g_FPDS_REPORTING_COL)
1962 	       ,'N') = 'Y') THEN
1963             BEGIN
1964                 SELECT REPORTING_METHOD, CAR_STATUS, APPROVED_WITHOUT_REPORTING
1965                 INTO l_reporting_method, l_car_status, l_approved_without_reporting
1966                 FROM PO_CLM_CARS
1967                 WHERE PO_HEADER_ID = l_document_id
1968                 AND PO_DRAFT_ID = p_draft_id;
1969             Exception
1970                 When NO_DATA_FOUND Then
1971                     l_reporting_method := 'NONE';
1972 		    l_approved_without_reporting := '2';
1973             END;
1974 
1975             IF (l_reporting_method <> 'EXEMPT' AND l_approved_without_reporting <> '1') THEN
1976                 IF (l_reporting_method = 'NONE') OR
1977                    (l_reporting_method = 'SINGLE_CAR' AND
1978                     l_car_status = 'NOT_REPORTED') THEN
1979                     INSERT INTO po_online_report_text_gt
1980                     (
1981                        online_report_id,
1982                        last_update_login,
1983                        last_updated_by,
1984                        last_update_date,
1985                        created_by,
1986                        creation_date,
1987                        line_num,
1988                        shipment_num,
1989                        distribution_num,
1990                        sequence,
1991                        text_line,
1992                        message_name,
1993                        message_type
1994                     )
1995                     VALUES
1996                     (
1997                        l_online_report_id,
1998                        l_login_id,
1999                        l_user_id,
2000                        SYSDATE,
2001                        l_user_id,
2002                        SYSDATE,
2003                        0,
2004                        0,
2005                        0,
2006                        p_sequence,
2007                        FND_MESSAGE.GET_STRING('PO', 'PO_CLM_CAR_NOT_REPORTED'),
2008                        'PO_CLM_CAR_NOT_REPORTED',
2009                        'W'
2010                     );
2011 		ELSIF (l_reporting_method = 'SINGLE_CAR' AND
2012                     l_car_status = 'DRAFT') THEN
2013                     INSERT INTO po_online_report_text_gt
2014                     (
2015                        online_report_id,
2016                        last_update_login,
2017                        last_updated_by,
2018                        last_update_date,
2019                        created_by,
2020                        creation_date,
2021                        line_num,
2022                        shipment_num,
2023                        distribution_num,
2024                        sequence,
2025                        text_line,
2026                        message_name,
2027                        message_type
2028                     )
2029                     VALUES
2030                     (
2031                        l_online_report_id,
2032                        l_login_id,
2033                        l_user_id,
2034                        SYSDATE,
2035                        l_user_id,
2036                        SYSDATE,
2037                        0,
2038                        0,
2039                        0,
2040                        p_sequence,
2041                        FND_MESSAGE.GET_STRING('PO', 'PO_CLM_CAR_NOT_AUTHENTICATED'),
2042                        'PO_CLM_CAR_NOT_AUTHENTICATED',
2043                        'W'
2044                     );
2045                 END IF;
2046             END IF;
2047         END IF;
2048         -- ABOVE CODE FOR FPDS-NG PROJECT
2049     END IF;
2050     -- ABOVE CODE FOR CLM PROJECT
2051 
2052 -- Added for  Bug 10214801
2053 IF(p_action_requested = g_action_DOC_SUBMISSION_CHECK) THEN
2054 
2055   -- Added for Bug 10218898
2056                PO_UOM_CHECK(
2057  	                  P_document_id         => l_document_id,
2058  		          p_document_type       => p_document_type,
2059  			  p_online_report_id    => l_online_report_id,
2060  			  p_user_id             => l_user_id,
2061  			  p_login_id            => l_login_id,
2062  			  p_sequence            => p_sequence,
2063  			  x_return_status       => l_return_status,
2064  			  x_msg_data          => x_msg_data);
2065      IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR
2066      THEN
2067        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2068      END IF;
2069 
2070 
2071 			l_progress := '00420';
2072 	     PO_VALIDATE_ACCOUNTS(P_document_id => l_document_id,
2073  			 p_document_type       => p_document_type,
2074  			 p_online_report_id    => l_online_report_id,
2075  			  p_user_id             => l_user_id,
2076  			  p_login_id            => l_login_id,
2077  			  p_sequence            => p_sequence,
2078  			  x_return_status       => l_return_status,
2079  			  x_msg_data          => x_msg_data);
2080 
2081 	    IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR
2082      THEN
2083        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2084      END IF;
2085 
2086              -- Bug 15843328
2087         --Added this submission check for accrue on receipt flag.
2088       	l_progress := '00430';
2089 
2090       check_accrue_on_receipt(
2091 		          P_document_id         => l_document_id,
2092  			  p_document_type       => p_document_type,
2093  			  p_online_report_id    => l_online_report_id,
2094  			  p_user_id             => l_user_id,
2095  			  p_login_id            => l_login_id,
2096  			  p_sequence            => p_sequence,
2097  			  x_return_status       => l_return_status,
2098  			  x_msg_data            => x_msg_data);
2099 
2100     IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR
2101      THEN
2102        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2103     END IF;
2104 
2105 
2106   END IF;
2107 
2108 
2109 
2110 
2111 l_progress := '005';
2112     IF p_document_type = 'REQUISITION' THEN
2113 
2114 l_progress := '006';
2115 IF g_debug_stmt THEN
2116    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2117      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2118           || l_progress,'REQUISITION checks');
2119    END IF;
2120 END IF;
2121 
2122         --check REQUISITIONS
2123         check_requisitions(l_document_id,
2124                            l_online_report_id,
2125                            l_user_id,
2126                            l_login_id,
2127                            p_sequence,
2128                            l_return_status);
2129 
2130         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2131                  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2132         END IF;
2133 
2134 l_progress := '007';
2135         --CBC header check for REQ
2136         do_cbc_related_validations(p_document_type ,
2137                        p_document_subtype,
2138                        l_document_id ,
2139                        l_online_report_id ,
2140                        l_user_id ,
2141                        l_login_id ,
2142                        p_sequence,
2143                        l_return_status);
2144 
2145         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2146              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2147         END IF;
2148 
2149 
2150 	-- Added for bug 12951645
2151 
2152         IF g_debug_stmt THEN
2153            IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2154                FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2155                || l_progress,'Close Wip JOb checks');
2156            END IF;
2157         END IF;
2158            --Submission check to check whther document
2159 	   --associated WIP Job is closed or not?
2160            check_close_wip_job(l_document_id,
2161                                p_document_type,
2162 			       l_online_report_id,
2163                                l_user_id,
2164                                l_login_id,
2165 		               p_sequence,
2166                                l_return_status);
2167 
2168            IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2169                  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2170            END IF;
2171 --Added for bug 12951645
2172 
2173     ELSIF p_document_type = 'RELEASE' THEN
2174 
2175        IF p_action_requested = g_action_UNRESERVE THEN
2176 
2177 l_progress := '008';
2178 IF g_debug_stmt THEN
2179    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2180      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2181           || l_progress,'RELEASE: Calling UNRESERVE checks');
2182    END IF;
2183 END IF;
2184 
2185       check_unreserve(
2186          p_online_report_id => l_online_report_id
2187       ,  p_document_type    => p_document_type       -- Bug#5462677
2188       ,  p_document_subtype => p_document_subtype -- Bug#5462677
2189       ,  p_document_level   => p_document_level     -- Bug#5462677
2190       ,  p_doc_level_id     => p_document_level_id         --Bug#5462677
2191       ,  p_user_id          => l_user_id
2192       ,  p_login_id         => l_login_id
2193       ,  p_sequence         => p_sequence);
2194 
2195        -- <Doc Manager Rewrite 11.5.11 Start>
2196        ELSIF (p_action_requested = g_action_FINAL_CLOSE_CHECK) THEN
2197 
2198          l_progress := '008';
2199          IF g_debug_stmt THEN
2200             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2201               FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2202             || l_progress,'RELEASE: Calling FINAL CLOSE checks');
2203             END IF;
2204          END IF;
2205 
2206             check_final_close(
2207                p_document_type     =>p_document_type
2208             ,  p_document_subtype  =>p_document_subtype
2209             ,  p_document_level    =>p_document_level
2210             ,  p_document_id       =>l_document_id
2211             ,  p_online_report_id  =>l_online_report_id
2212             ,  p_user_id           =>l_user_id
2213             ,  p_login_id          =>l_login_id
2214             ,  p_origin_doc_id     =>p_origin_doc_id --Bug#5462677
2215             ,  p_doc_level_id       =>p_document_level_id
2216             ,  p_sequence          =>p_sequence
2217             ,  x_return_status     =>l_return_status);
2218 
2219          IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2220            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2221          END IF;
2222 
2223        -- <Doc Manager Rewrite 11.5.11 End>
2224 
2225        ELSE --its 'DOC_SUBMISSION_CHECK'
2226 
2227 l_progress := '009';
2228 IF g_debug_stmt THEN
2229    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2230      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2231           || l_progress,'RELEASE checks');
2232    END IF;
2233 END IF;
2234            --check RELEASES
2235            check_releases(l_document_id,
2236                        l_online_report_id,
2237                        l_user_id,
2238                        l_login_id, p_sequence,
2239                        l_return_status);
2240 
2241            IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2242                  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2243            END IF;
2244 
2245 --Added for bug 12951645
2246 
2247  IF g_debug_stmt THEN
2248     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2249         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2250         || l_progress,'Close Wip JOb checks');
2251     END IF;
2252  END IF;
2253            --Submission check to check whther document
2254 	   --associated WIP Job is closed or not?
2255            check_close_wip_job(l_document_id,
2256                                p_document_type,
2257 			       l_online_report_id,
2258                                l_user_id,
2259                                l_login_id,
2260 		               p_sequence,
2261                                l_return_status);
2262 
2263            IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2264                  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2265            END IF;
2266 --Added for bug 12951645
2267 
2268 l_progress := '010';
2269 
2270            -- bug4957243
2271            --Do requisition price within tolerance check for PO, RELEASES
2272            -- This is not done for requester change orders
2273             IF p_req_chg_initiator is null or
2274                p_req_chg_initiator <> 'REQUESTER'
2275             THEN
2276 
2277               check_po_rel_reqprice(p_document_type,
2278                        l_document_id,
2279                        l_online_report_id,
2280                        l_user_id,
2281                        l_login_id, p_sequence,
2282                        l_return_status);
2283             END IF;
2284 
2285             IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2286                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2287             END IF;
2288 
2289 l_progress := '011';
2290             --CBC header check for Releases
2291             do_cbc_related_validations(p_document_type ,
2292                        p_document_subtype,
2293                        l_document_id ,
2294                        l_online_report_id ,
2295                        l_user_id ,
2296                        l_login_id ,
2297                        p_sequence,
2298                        l_return_status);
2299 
2300             IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2301                  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2302             END IF;
2303 
2304        END IF; --check action requested
2305 
2306     ELSIF p_document_type = 'PO' THEN
2307        IF p_action_requested = g_action_UNRESERVE THEN
2308 
2309 l_progress := '012';
2310 IF g_debug_stmt THEN
2311    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2312      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2313           || l_progress,'PO: Calling UNRESERVE checks ');
2314    END IF;
2315 END IF;
2316 
2317          check_unreserve(
2318             p_online_report_id  => l_online_report_id
2319          ,  p_document_type     => p_document_type      -- Bug#5462677
2320          ,  p_document_subtype  => p_document_subtype   -- Bug#5462677
2321          ,  p_document_level    => p_document_level     -- Bug#5462677
2322          ,  p_doc_level_id      => p_document_level_id  -- Bug#5462677
2323          ,  p_user_id           => l_user_id
2324          ,  p_login_id          => l_login_id
2325          ,  p_sequence          => p_sequence );
2326 
2327        -- <Doc Manager Rewrite 11.5.11 Start>
2328        ELSIF (p_action_requested = g_action_FINAL_CLOSE_CHECK) THEN
2329 
2330          l_progress := '012';
2331          IF g_debug_stmt THEN
2332             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2333               FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2334             || l_progress,'PO: Calling FINAL CLOSE checks');
2335             END IF;
2336          END IF;
2337 
2338             check_final_close(
2339                p_document_type     =>p_document_type
2340             ,  p_document_subtype  =>p_document_subtype
2341             ,  p_document_level    =>p_document_level
2342             ,  p_document_id       =>l_document_id
2343             ,  p_online_report_id  =>l_online_report_id
2344             ,  p_user_id           =>l_user_id
2345             ,  p_login_id          =>l_login_id
2346             ,  p_origin_doc_id     =>p_origin_doc_id --Bug#5462677
2347             ,  p_doc_level_id      =>p_document_level_id
2348             ,  p_sequence          =>p_sequence
2349             ,  x_return_status     =>l_return_status);
2350 
2351          IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2352            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2353          END IF;
2354 
2355        -- <Doc Manager Rewrite 11.5.11 End>
2356 
2357        ELSE --its 'DOC_SUBMISSION_CHECK'
2358 
2359 l_progress := '013';
2360 IF g_debug_stmt THEN
2361    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2362      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2363           || l_progress,'PO checks');
2364    END IF;
2365 END IF;
2366             --First do all checks at header level
2367             check_po_pa_header(l_document_id,
2368                        l_online_report_id,
2369                        l_user_id,
2370                        l_login_id, p_sequence,
2371                        l_return_status);
2372 
2373             IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2374                  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2375             END IF;
2376 
2377 --Added for bug 12951645
2378 IF g_debug_stmt THEN
2379    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2380        FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2381        || l_progress,'Close Wip JOb check');
2382    END IF;
2383 END IF;
2384             --Submission check to check whther document
2385 	    --associated WIP Job is closed or not?
2386            check_close_wip_job(l_document_id,
2387                                p_document_type,
2388 			       l_online_report_id,
2389                                l_user_id,
2390                                l_login_id,
2391 		               p_sequence,
2392                                l_return_status);
2393 
2394            IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2395                  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2396            END IF;
2397 --Added for bug 12951645
2398 
2399 l_progress := '014';
2400             --Do all checks common to Standard and PLanned POs
2401             check_po(l_document_id,
2402                        l_online_report_id,
2403                        l_user_id,
2404                        l_login_id,
2405 --                       p_check_asl,                                -- <2757450>
2406                        p_sequence,
2407                        l_return_status);
2408 
2409             IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2410                   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2411             END IF;
2412 
2413 	    IF(l_is_clm_doc = 'Y' AND p_action_requested = g_action_DOC_SUBMISSION_CHECK) THEN
2414 
2415                 PO_ACCOUNTS_CHECK(
2416                           P_document_id         => l_document_id,
2417                           p_document_type       => p_document_type,
2418                           p_online_report_id    => l_online_report_id,
2419                           p_user_id             => l_user_id,
2420                           p_login_id            => l_login_id,
2421                           p_sequence            => p_sequence,
2422                           x_return_status       => l_return_status,
2423                           x_msg_data            => x_msg_data);
2424 
2425 		IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2426                   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2427                 END IF;
2428             --Adding DraftID Parameter for new ACRN CHECK <bug 13541656>
2429             PO_ACRN_CHECK(P_document_id         => l_document_id,
2430                           P_draft_id            => p_draft_id,
2431                           p_document_type       => p_document_type,
2432                           p_online_report_id    => l_online_report_id,
2433                           p_user_id             => l_user_id,
2434                           p_login_id            => l_login_id,
2435                           p_sequence            => p_sequence,
2436                           x_return_status       => l_return_status,
2437                           x_msg_data            => x_msg_data);
2438 
2439             IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2440                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2441             END IF;
2442 
2443 
2444             END IF;
2445 
2446 l_progress := '015';
2447             --CBC header check for POs
2448             do_cbc_related_validations(p_document_type ,
2449                        p_document_subtype,
2450                        l_document_id ,
2451                        l_online_report_id ,
2452                        l_user_id ,
2453                        l_login_id ,
2454                        p_sequence,
2455                        l_return_status);
2456 
2457             IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2458                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2459             END IF;
2460 
2461 l_progress := '016';
2462 
2463             -- bug4957243
2464             --Do requisition price within tolerance check for PO, RELEASES
2465             -- This is not done for requester change orders
2466             IF p_req_chg_initiator is null or
2467                p_req_chg_initiator <> 'REQUESTER'
2468             THEN
2469 
2470               check_po_rel_reqprice(p_document_type,
2471                        l_document_id,
2472                        l_online_report_id,
2473                        l_user_id,
2474                        l_login_id, p_sequence,
2475                        l_return_status);
2476             END IF;
2477 
2478             IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2479                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2480             END IF;
2481 
2482             --Do some additional checks if its PLANNED PO
2483             IF p_document_subtype = 'PLANNED' THEN
2484                 --check planned PO
2485 l_progress := '017';
2486 IF g_debug_stmt THEN
2487    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2488      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2489           || l_progress,'PO: PLANNED checks');
2490    END IF;
2491 END IF;
2492                 --Call checks common to Planned PO and Blanket PA
2493                 check_planned_po_blanket_pa(l_document_id,
2494                              l_online_report_id,
2495                              l_user_id,
2496                              l_login_id, p_sequence,
2497                              l_return_status);
2498 
2499                  IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2500                       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2501                  END IF;
2502 
2503             ELSIF p_document_subtype = 'STANDARD' THEN
2504 
2505 l_progress := '018';
2506 IF g_debug_stmt THEN
2507    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2508      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2509           || l_progress,'PO: STANDARD checks');
2510    END IF;
2511 END IF;
2512                 --Call additional checks for Standard PO
2513                 check_standard_po(l_document_id,
2514                              l_online_report_id,
2515                              l_user_id,
2516                              l_login_id,
2517                              p_sequence,
2518                              l_return_status);
2519 
2520                 IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2521                      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2522                 END IF;
2523 
2524 	l_progress := '019';
2525 	IF g_debug_stmt THEN
2526 	   IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2527 		FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2528 		|| l_progress,'PO: STANDARD checks');
2529 	   END IF;
2530 	END IF;
2531 
2532       IF  l_is_clm_doc = 'Y' THEN
2533         -- <Event Based Delivery Project>
2534         -- Check if Delivery Event is defined and
2535         -- Delivery Period is not defined.
2536         delivery_event_checks(l_document_id,
2537                               l_online_report_id,
2538                               l_user_id,
2539                               l_login_id,
2540                               p_sequence,
2541                               l_return_status);
2542 
2543         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2544           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2545         END IF;
2546 
2547         l_progress := '020';
2548         IF g_debug_stmt THEN
2549           IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2550             FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2551                            || l_progress,'Delivery Event Checks');
2552           END IF;
2553         END IF;
2554 
2555         -- Payment Instruction Checks.
2556         -- Payment instruction checks must be performed only if it is enabled in clm options page
2557         l_enable_payment_inst := PO_CORE_S.retrieveOptionValue(p_org_id => l_org_id,
2558                                                                p_option_column => PO_CORE_S.g_PAYMENT_INSTR_COL);
2559         IF l_enable_payment_inst = 'Y' THEN
2560           payment_instruction_checks(l_document_id,
2561                                      l_draft_type,
2562                                      l_online_report_id,
2563                                      l_user_id,
2564                                      l_login_id,
2565                                      p_sequence,
2566                                      l_return_status);
2567 
2568           IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2569             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2570           END IF;
2571 
2572           l_progress := '021';
2573           IF g_debug_stmt THEN
2574             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2575               FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2576                              || l_progress,'Payment Isntruction Checks');
2577             END IF;
2578           END IF;
2579         END IF; --IF  l_enable_payment_inst = 'Y'
2580 
2581         --Sunset Memo - POP Validation for Severable Line Type.
2582         VALIDATE_POP_FOR_SEVERABLE(l_document_id,
2583                                    l_online_report_id,
2584                                    l_user_id,
2585                                    l_login_id,
2586                                    p_sequence,
2587                                    l_return_status);
2588 
2589         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2590           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2591         END IF;
2592 
2593         l_progress := '022';
2594         IF g_debug_stmt THEN
2595           IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2596             FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2597                            || l_progress,'POP Validations for Severable Line Types');
2598           END IF;
2599         END IF;
2600 
2601         --UCA Project CLMR4 Changes - Begin
2602         -- Call to Check_line_ucas
2603         l_isucaenabled := Nvl(po_core_s.Retrieveoptionvalue(l_org_id,
2604                                                             po_core_s.g_undef_cont_act_col), 'N');
2605         IF l_isucaenabled = 'Y' THEN
2606           check_line_ucas(l_document_id,
2607                           l_online_report_id,
2608                           l_user_id,
2609                           l_login_id,
2610                           p_sequence,
2611                           l_return_status);
2612 
2613           IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2614             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2615           END IF;
2616 
2617           l_progress := '023';
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,'UCA Line Checks');
2622             END IF;
2623           END IF;
2624         END IF; --IF l_isucaenabled = 'Y'
2625         --UCA Project CLMR4 Changes - End
2626 
2627         --CLM Phase 4 -MIPR project Begin
2628         check_po_mipr_reference(p_document_id => l_document_id,
2629                                 p_draft_id => p_draft_id,
2630                                 p_online_report_id => l_online_report_id,
2631                                 p_user_id => l_user_id,
2632                                 p_login_id => l_login_id,
2633                                 p_sequence => p_sequence,
2634                                 x_return_status => l_return_status);
2635 
2636         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2637           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2638         END IF;
2639 
2640         l_progress := '025';
2641         IF g_debug_stmt THEN
2642           IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2643             FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2644                            || l_progress,'MIPR Checks');
2645           END IF;
2646         END IF;
2647         --CLM Phase 4 -MIPR project End
2648 
2649         /*Bug 13797764:
2650           Including new validation check to verify that IDV reference at header
2651           and line level is same.*/
2652         po_idv_reference_equal_check(l_document_id,
2653                                      l_online_report_id,
2654                                      l_user_id,
2655                                      l_login_id,
2656                                      p_sequence,
2657                                      l_return_status);
2658 
2659         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2660           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2661         END IF;
2662 
2663         l_progress := '026';
2664         IF g_debug_stmt THEN
2665           IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2666             FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2667                            || l_progress,'IDV Reference Checks');
2668           END IF;
2669         END IF;
2670       END IF; --End if clm doc
2671 
2672     END IF; --check doc_subtype
2673 
2674 
2675        END IF; --check action requested
2676     ELSIF  p_document_type = 'PA' THEN
2677 
2678       -- <Doc Manager Rewrite 11.5.11 Start>
2679       IF (p_action_requested = g_action_FINAL_CLOSE_CHECK) THEN
2680 
2681         l_progress := '019';
2682         IF g_debug_stmt THEN
2683           IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2684             FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2685           || l_progress,'PA: Calling FINAL CLOSE checks');
2686           END IF;
2687         END IF;
2688 
2689         check_final_close(
2690            p_document_type     =>p_document_type
2691         ,  p_document_subtype  =>p_document_subtype
2692         ,  p_document_level    =>p_document_level
2693         ,  p_document_id       =>l_document_id
2694         ,  p_online_report_id  =>l_online_report_id
2695         ,  p_user_id           =>l_user_id
2696         ,  p_login_id          =>l_login_id
2697         ,  p_origin_doc_id     =>p_origin_doc_id --Bug#5462677
2698         ,  p_doc_level_id      =>p_document_level_id
2699         ,  p_sequence          =>p_sequence
2700         ,  x_return_status     =>l_return_status);
2701 
2702         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2703           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2704         END IF;
2705 
2706       ELSE
2707       -- <Doc Manager Rewrite 11.5.11 End>
2708 
2709         -- It's  'DOC_SUBMISSION_CHECK'
2710         --check PAs
2711         l_progress := '019';
2712         IF g_debug_stmt THEN
2713           IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2714             FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2715             || l_progress,'PA checks');
2716           END IF;
2717         END IF;
2718 
2719         --First do all checks at header level
2720         check_po_pa_header(l_document_id,
2721                        l_online_report_id,
2722                        l_user_id,
2723                        l_login_id,
2724                        p_sequence,
2725                        l_return_status);
2726 
2727         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2728               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2729         END IF;
2730 
2731 	--Added for bug 12951645
2732 
2733 IF g_debug_stmt THEN
2734    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2735       FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2736       || l_progress,'Close Wip JOb checks');
2737    END IF;
2738 END IF;
2739             --Submission check to check whther document
2740 	    --associated WIP Job is closed or not?
2741            check_close_wip_job(l_document_id,
2742                                p_document_type,
2743 			       l_online_report_id,
2744                                l_user_id,
2745                                l_login_id,
2746 		               p_sequence,
2747                                l_return_status);
2748 
2749            IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2750                  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2751            END IF;
2752 --Added for bug 12951645
2753 
2754         IF p_document_subtype = 'BLANKET' THEN
2755 
2756           l_progress := '020';
2757           IF g_debug_stmt THEN
2758             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2759               FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2760               || l_progress,'PA: BLANKET checks');
2761             END IF;
2762           END IF;
2763 
2764           --check blanket agreement
2765           check_blanket_agreement(l_document_id,
2766                                   l_online_report_id,
2767                                   l_user_id,
2768                                   l_login_id,
2769                                   p_check_asl,                   -- <2757450>
2770                                   p_sequence,
2771                                   l_return_status);
2772 
2773           IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2774             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2775           END IF;
2776 
2777           l_progress := '021';
2778           --Call checks common to Planned PO and Blanket PA
2779           check_planned_po_blanket_pa(l_document_id,
2780                            l_online_report_id,
2781                            l_user_id,
2782                            l_login_id,
2783                            p_sequence,
2784                            l_return_status);
2785 
2786           IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2787               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2788           END IF;
2789 
2790         ELSIF p_document_subtype = 'CONTRACT' THEN
2791 
2792           l_progress := '022';
2793           IF g_debug_stmt THEN
2794             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2795               FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2796               || l_progress,'PA: CONTRACT checks');
2797             END IF;
2798           END IF;
2799 
2800           --check contract agreement
2801           check_contract_agreement(l_document_id,
2802                                    l_online_report_id,
2803                                    l_user_id,
2804                                    l_login_id,
2805                                    p_sequence,
2806                                    l_return_status);
2807 
2808           IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2809             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2810           END IF;
2811         END IF;  --PA p_document_subtype
2812 
2813       END IF; -- PA p_action_requested
2814 
2815     END IF; --document_type lookup
2816 
2817     l_progress := '023';
2818     IF g_debug_stmt THEN
2819        IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2820          FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2821               || l_progress,'Done with all checks');
2822        END IF;
2823     END IF;
2824 
2825     --bug 16713804
2826     IF (p_document_type IN ('PO', 'PA')) THEN
2827       --SBCR Coding Begin
2828       l_isSbcrEnabled := Nvl(po_core_s.Retrieveoptionvalue(l_org_id,
2829                                                               po_core_s.g_BUSS_COORDINATION_COL), 'N');
2830       IF l_isSbcrEnabled = 'Y' AND l_is_clm_doc = 'Y' THEN
2831            check_po_sbcr_reference(p_document_id => l_document_id,
2832                                    p_draft_id => p_draft_id,
2833                                    p_online_report_id => l_online_report_id,
2834                                    p_user_id => l_user_id,
2835                                    p_login_id => l_login_id,
2836                                    p_org_id   =>  l_org_id,
2837                                    p_sequence => p_sequence,
2838                                    x_return_status => l_return_status);
2839       END IF;
2840       --SBCR Coding End
2841     END IF;
2842     --bug 16713804
2843 
2844     /* CLM Phase 4 - Elins project Start */
2845     IF (p_document_type IN ('PO', 'PA')) AND
2846        (p_document_subtype IN ('STANDARD', 'BLANKET'))
2847        AND (p_action_requested = g_action_DOC_SUBMISSION_CHECK) THEN
2848 
2849         -- Profile Option IsExhibitEnabled
2850         l_isexhibitenabled := Nvl(po_core_s.Retrieveoptionvalue(l_org_id,
2851                                                                 po_core_s.g_exhibits_lines_col), 'N');
2852         l_progress := '044';
2853         IF g_debug_stmt THEN
2854             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2855               FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2856                              || l_progress,'Exhibit Checks. l_isexhibitenabled'||l_isexhibitenabled);
2857             END IF;
2858         END IF;
2859 
2860         IF l_isexhibitenabled = 'Y' THEN
2861           validate_exhibits(l_document_id,
2862                             p_draft_id,
2863                             l_online_report_id,
2864                             l_user_id,
2865                             l_login_id,
2866                             p_sequence,
2867                             l_return_status);
2868 
2869           IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2870             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2871           END IF;
2872 
2873           l_progress := '045';
2874           IF g_debug_stmt THEN
2875             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2876               FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
2877                              || l_progress,'Exhibit Checks');
2878             END IF;
2879           END IF;
2880         END IF; --IF l_isexhibitenabled = 'Y'
2881     END IF; -- document type is PO or PA
2882     --CLM Phase 4 -Elins project End
2883 
2884 
2885 /* CONTERMS FPJ START*/
2886 
2887   -- <Doc Manager Rewrite 11.5.11> : Only check okc terms for 'DOC_SUBMISSION_CHECK'
2888 
2889 IF (p_document_type IN ('PO', 'PA')) AND
2890    (p_document_subtype IN ('STANDARD', 'BLANKET', 'CONTRACT'))
2891    AND (p_action_requested = g_action_DOC_SUBMISSION_CHECK) THEN
2892 
2893     -- SQL What: conterms exist flag
2894     -- SQL why : need to check before calling contracts qa
2895     -- SQL join: po_header_id
2896     SELECT conterms_exist_flag
2897     INTO   l_conterms_exist_flag
2898     FROM   po_headers_gt
2899     WHERE  po_header_id = l_document_id;
2900 
2901     IF (NVL(l_conterms_exist_flag, 'N')='Y') THEN
2902       check_terms(
2903         p_document_id          => l_document_id,
2904         p_document_type        => p_document_type,
2905         p_document_subtype     => p_document_subtype,
2906         p_online_report_id     => l_online_report_id,
2907         p_user_id              => l_user_id,
2908         p_login_id             => l_login_id,
2909         p_sequence             => p_sequence,
2910         x_return_status        => l_return_status);
2911 
2912       IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
2913         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2914       END IF;
2915     END IF; --conterms exist
2916 END IF; -- document type is PO or PA
2917 
2918     l_progress := '024';
2919     IF g_debug_stmt THEN
2920       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2921         FND_LOG.string(FND_LOG.LEVEL_STATEMENT
2922                     ,g_log_head || '.'||l_api_name||'.'|| l_progress
2923                     ,'Done with contracts qa');
2924       END IF;
2925     END IF;
2926     /* CONTERMS FPJ END */
2927 
2928     -- IAE Projects
2929 
2930 IF l_is_clm_doc = 'Y' THEN
2931 
2932       verify_vendor_check(
2933         p_document_id          => l_document_id,
2934         p_document_type        => p_document_type,
2935         p_document_subtype     => p_document_subtype,
2936         p_online_report_id     => l_online_report_id,
2937         p_user_id              => l_user_id,
2938         p_login_id             => l_login_id,
2939         p_sequence             => p_sequence,
2940         p_org_id               => l_org_id,
2941         x_return_status        => l_return_status);
2942 
2943       IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
2944         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2945       END IF;
2946 
2947       /*This code is commented for the bug 16534713
2948 	verify_wage_determination(
2949         p_document_id          => l_document_id,
2950         p_document_type        => p_document_type,
2951         p_document_subtype     => p_document_subtype,
2952         p_online_report_id     => l_online_report_id,
2953         p_user_id              => l_user_id,
2954         p_login_id             => l_login_id,
2955         p_sequence             => p_sequence,
2956         p_org_id               => l_org_id,
2957         x_return_status        => l_return_status);
2958 
2959       IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
2960         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2961       END IF;*/
2962 
2963       verify_validate_pds(
2964         p_document_id          => l_document_id,
2965         p_document_type        => p_document_type,
2966         p_document_subtype     => p_document_subtype,
2967         p_online_report_id     => l_online_report_id,
2968         p_user_id              => l_user_id,
2969         p_login_id             => l_login_id,
2970         p_sequence             => p_sequence,
2971         p_org_id               => l_org_id,
2972         x_return_status        => l_return_status);
2973 
2974       IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
2975         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2976       END IF;
2977 
2978 END IF;
2979 -- IAE Projects
2980 
2981 
2982   -- <Doc Manager Rewrite 11.5.11> : Do vendor registration check only
2983   -- for DOC_SUBMISSION_CHECK and UNRESERVE
2984 
2985   IF (p_action_requested IN (g_action_DOC_SUBMISSION_CHECK, g_action_UNRESERVE))
2986   THEN
2987 
2988     --<JFMIP Vendor Registration FPJ Start>
2989     -- This part of the code is called when the action requested is
2990     -- DOC_SUBMISSION_CHECK, as well as UNRESERVE. This is because if
2991     -- vendor site does not have a valid registration, reserve/unreserve
2992     -- actions should be prevented.
2993     IF (p_document_type = 'RELEASE') THEN
2994         -- SQL What: Retrieve the blanket header id based on the release id
2995         -- SQL Why:  This header id is used in check_vendor_site_ccr_regis to
2996         --           retrieve the vendor and vendor site id
2997        BEGIN
2998          SELECT  po_header_id
2999          INTO    l_po_header_id
3000          FROM    PO_RELEASES_ALL
3001          WHERE   po_release_id = l_document_id;
3002       EXCEPTION
3003          WHEN OTHERS THEN
3004            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3005       END;
3006     ELSE
3007        l_po_header_id := l_document_id;
3008     END IF;
3009 
3010     IF p_document_type IN ('RELEASE', 'PO', 'PA') THEN
3011      check_vendor_site_ccr_regis(
3012                                   p_document_id      => l_po_header_id,
3013           p_online_report_id => l_online_report_id,
3014           p_user_id          => l_user_id,
3015           p_login_id         => l_login_id,
3016           p_sequence         => p_sequence,
3017           x_return_status    => l_return_status);
3018 
3019         IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
3020      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3021         END IF;
3022     END IF;
3023 
3024     l_progress := '025';
3025     IF g_debug_stmt THEN
3026        IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3027          FND_LOG.string(FND_LOG.LEVEL_STATEMENT
3028                     ,g_log_head || '.'||l_api_name||'.'|| l_progress,
3029                     'Done with vendor site registration check');
3030        END IF;
3031     END IF;
3032     --<JFMIP Vendor Registration FPJ End>
3033 
3034   END IF; -- p_action_requested NOT IN ('DOC_SUBMISSION_CHECK', 'UNRESREVE')
3035 
3036     -- <Bug 7655436 Start>
3037     -- Call the custom code hook
3038     PO_CUSTOM_SUBMISSION_CHECK_PVT.do_post_submission_check(
3039       p_api_version         => 1.0,
3040       P_document_id         => l_document_id,
3041       p_action_requested    => p_action_requested,
3042       p_document_type       => p_document_type,
3043       p_document_subtype    => p_document_subtype,
3044       p_document_level      => p_document_level,
3045       p_document_level_id   => p_document_level_id,
3046       p_requested_changes   => p_requested_changes,
3047       p_check_asl           => p_check_asl,
3048       p_req_chg_initiator   => p_req_chg_initiator,
3049       p_origin_doc_id       => p_origin_doc_id,
3050       p_online_report_id    => l_online_report_id,
3051       p_user_id             => l_user_id,
3052       p_login_id            => l_login_id,
3053       p_sequence            => p_sequence,
3054       x_return_status       => l_return_status
3055     );
3056 
3057     IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR
3058     THEN
3059       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3060     END IF;
3061     -- <Bug 7655436 End>
3062 
3063 
3064     --Done with CHECKS, now prepare OUT parameters to pass back
3065 
3066     -- bug3574165
3067     -- Count number of warning messages as well.
3068 
3069     SELECT count(text_line),
3070            COUNT(DECODE(message_type, 'W', 'W', NULL))
3071     INTO   l_num_messages,
3072            l_num_warnings
3073     FROM   po_online_report_text_gt
3074     WHERE  online_report_id = l_online_report_id;
3075 
3076 
3077     l_progress := '026';
3078     IF g_debug_stmt THEN
3079       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3080         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
3081           || l_progress,'Number of messagess reported ' || l_num_messages);
3082       END IF;
3083     END IF;
3084 
3085     x_online_report_id := l_online_report_id;
3086 
3087     IF l_num_messages > 0 THEN
3088 
3089         -- bug3574165
3090         -- 1) Return Error only if there are messages that are not warnings
3091         -- 2) Set x_has_warnings flag to TRUE if there are warnings reported
3092 
3093         IF (l_num_messages > l_num_warnings) THEN
3094           x_sub_check_status := FND_API.G_RET_STS_ERROR;
3095         ELSE
3096           x_sub_check_status := FND_API.G_RET_STS_SUCCESS;
3097         END IF;
3098 
3099         IF ( l_num_warnings > 0 ) THEN
3100           x_has_warnings := FND_API.G_TRUE;
3101         END IF;
3102 
3103         --Bulk update online_report_text table with errors
3104         INSERT INTO po_online_report_text(online_report_id,
3105             last_update_login,
3106             last_updated_by,
3107             last_update_date,
3108             created_by,
3109             creation_date,
3110             line_num,
3111             shipment_num,
3112             distribution_num,
3113             sequence,
3114             text_line,
3115                         message_type) --<CONTERMS FPJ>
3116             SELECT online_report_id,
3117             last_update_login,
3118             last_updated_by,
3119             last_update_date,
3120             created_by,
3121             creation_date,
3122             line_num,
3123             shipment_num,
3124             distribution_num,
3125             sequence,
3126             text_line,
3127                         message_type --<CONTERMS FPJ>
3128             FROM po_online_report_text_gt
3129             WHERE online_report_id = x_online_report_id;
3130 
3131 select count(*) into l_num_messages from po_online_report_text_gt where online_report_id = x_online_report_id;
3132 
3133 l_progress := '027';
3134 
3135         -- SQL What: Gets the relevent messages from global temp table
3136         -- SQL Why: Need to collect all errors in x_doc_check_error_record
3137         SELECT  online_report_id, sequence, text_line,
3138             line_num, shipment_num, distribution_num, message_name, message_type
3139         BULK COLLECT INTO x_doc_check_error_record.online_report_id,
3140                           x_doc_check_error_record.sequence_num,
3141                           x_doc_check_error_record.text_line,
3142                           x_doc_check_error_record.line_num,
3143                           x_doc_check_error_record.shipment_num,
3144                           x_doc_check_error_record.distribution_num,
3145                           x_doc_check_error_record.message_name,
3146                           x_doc_check_error_record.message_type --<CONTERMS FPJ>
3147         FROM  po_online_report_text_gt
3148         WHERE online_report_id = x_online_report_id;
3149 
3150     ELSE
3151         x_sub_check_status := FND_API.G_RET_STS_SUCCESS;
3152     END IF;
3153 
3154 l_progress := '027';
3155     x_return_status := FND_API.G_RET_STS_SUCCESS;
3156 
3157     -- 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.
3158         IF p_document_type in ('PO','RELEASE') THEN
3159 
3160         	FOR ship_rec in (select line_location_id from po_line_locations_gt)
3161 
3162         	LOOP
3163         		set_lcm_flag(ship_rec.line_location_id,'AFTER',l_return_status);
3164         	END LOOP;
3165 
3166         	IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
3167     	     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3168             END IF;
3169         END IF;
3170     --LCM ER end
3171 
3172     -- bug3413891
3173     -- We are now deleting data from GT tables at the beginning of submission
3174     -- check. Deletion at the end is no longer needed
3175 
3176 
3177 l_progress := '029';
3178 IF g_debug_stmt THEN
3179    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3180      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
3181           || l_progress,'Returning from PVT package');
3182    END IF;
3183 END IF;
3184 
3185 EXCEPTION
3186     WHEN FND_API.G_EXC_ERROR THEN
3187         ROLLBACK TO PO_SUBMISSION_CHECK_SP;
3188         x_return_status := FND_API.G_RET_STS_ERROR;
3189         x_sub_check_status := FND_API.G_RET_STS_ERROR;
3190         x_msg_data := FND_MSG_PUB.GET(p_msg_index => FND_MSG_PUB.G_LAST,
3191                                         p_encoded => 'F');
3192 
3193     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3194         ROLLBACK TO PO_SUBMISSION_CHECK_SP;
3195         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3196         x_sub_check_status := FND_API.G_RET_STS_ERROR;
3197         x_msg_data := FND_MSG_PUB.GET(p_msg_index => FND_MSG_PUB.G_LAST,
3198                                     p_encoded => 'F');
3199 
3200     WHEN OTHERS THEN
3201         ROLLBACK TO PO_SUBMISSION_CHECK_SP;
3202         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3203         x_sub_check_status := FND_API.G_RET_STS_ERROR;
3204         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
3205             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
3206         END IF;
3207 
3208         IF (g_debug_unexp) THEN
3209                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
3210                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
3211                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
3212                        || l_progress || ' SQL CODE is '||sqlcode);
3213                 END IF;
3214         END IF;
3215 
3216 END PO_SUBMISSION_CHECK;
3217 
3218 
3219 PROCEDURE post_submission_check                                   -- <2757450>
3220 (
3221     p_api_version             IN            NUMBER
3222 ,   p_document_type           IN            VARCHAR2
3223 ,   p_document_subtype        IN            VARCHAR2
3224 ,   p_document_id             IN            NUMBER
3225 ,   x_return_status              OUT NOCOPY VARCHAR2
3226 ,   x_sub_check_status           OUT NOCOPY VARCHAR2
3227 ,   x_online_report_id           OUT NOCOPY NUMBER
3228 )
3229 IS
3230     l_api_name                CONSTANT varchar2(30) := 'POST_SUBMISSION_CHECK';
3231     l_api_version             CONSTANT NUMBER       := 1.0;
3232     l_progress                VARCHAR2(3);
3233 
3234     l_num_errors              NUMBER := 0;
3235     l_online_report_id        NUMBER;
3236     l_user_id                 PO_LINES.last_updated_by%TYPE := -1;
3237     l_login_id                PO_LINES.last_update_login%TYPE := -1;
3238     p_sequence                PO_ONLINE_REPORT_TEXT.sequence%TYPE :=0;
3239 
3240     l_return_status           VARCHAR2(1);
3241 
3242 BEGIN
3243 
3244 SAVEPOINT POST_SUBMISSION_CHECK_SP;
3245 
3246 l_progress := '000';
3247 
3248     -- Standard call to check for call compatibility
3249     IF NOT FND_API.Compatible_API_Call(l_api_version, p_api_version, l_api_name, G_PKG_NAME)
3250     THEN
3251         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3252     END IF;
3253 
3254 l_progress := '001';
3255 
3256 IF g_debug_stmt THEN
3257     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3258       FND_LOG.string( FND_LOG.LEVEL_STATEMENT,
3259                     g_log_head || '.'||l_api_name||'.' || l_progress,
3260                     'Populating global temp tables');
3261     END IF;
3262 END IF;
3263 
3264    --Populate global temp tables with data from main PO tables for
3265    --given PO_HEADER_ID
3266    populate_global_temp_tables(
3267       x_return_status => l_return_status
3268    ,  p_doc_type => p_document_type
3269    ,  p_doc_level => g_document_level_HEADER
3270    ,  p_doc_level_id => p_document_id
3271    );
3272 
3273     IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3274         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3275     END IF;
3276 
3277 IF g_debug_stmt THEN
3278    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3279      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
3280           || l_progress,'Getting online_report_id');
3281    END IF;
3282 END IF;
3283 
3284 l_progress := '003';
3285 
3286     --Get the unique id to be used for this document
3287     SELECT PO_ONLINE_REPORT_TEXT_S.nextval
3288     INTO   l_online_report_id
3289     FROM   sys.dual;
3290 
3291 l_progress := '004';
3292 
3293     --Get User ID and Login ID
3294     l_user_id := FND_GLOBAL.USER_ID;
3295 
3296     IF ( FND_GLOBAL.CONC_LOGIN_ID >= 0 )
3297     THEN
3298         l_login_id := FND_GLOBAL.CONC_LOGIN_ID;
3299     ELSE
3300         l_login_id := FND_GLOBAL.LOGIN_ID;
3301     END IF;
3302 
3303 IF g_debug_stmt THEN
3304      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3305        FND_LOG.string( FND_LOG.LEVEL_STATEMENT,
3306                      g_log_head || l_api_name||'.'|| l_progress,
3307                      'user_id '|| to_char(l_user_id) ||
3308                      'Login_id ' || to_char(l_login_id));
3309      END IF;
3310 END IF;
3311 
3312     --============================ CHECKS =====================================
3313     --Bug 4943365 We should not do any ASL checking for Blanket agreements
3314     --Removed the call to check_asl
3315 
3316 l_progress := '007';
3317 
3318 IF g_debug_stmt THEN
3319    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3320      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
3321           || l_progress,'Done with all checks');
3322    END IF;
3323 END IF;
3324 
3325     --Done with CHECKS, now prepare OUT parameters to pass back
3326     SELECT count(text_line)
3327     INTO   l_num_errors
3328     FROM   po_online_report_text_gt
3329     WHERE  online_report_id = l_online_report_id;
3330 
3331 l_progress := '008';
3332 
3333 IF g_debug_stmt THEN
3334    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3335      FND_LOG.string( FND_LOG.LEVEL_STATEMENT,
3336                    g_log_head || '.'||l_api_name||'.'|| l_progress,
3337                    'Number of errors reported ' || l_num_errors);
3338    END IF;
3339 END IF;
3340 
3341     x_online_report_id := l_online_report_id;
3342 
3343     IF l_num_errors > 0 THEN
3344 
3345         x_sub_check_status := FND_API.G_RET_STS_ERROR;
3346 
3347         --Bulk update online_report_text table with errors
3348         INSERT INTO po_online_report_text(
3349                     online_report_id,
3350             last_update_login,
3351             last_updated_by,
3352             last_update_date,
3353             created_by,
3354               creation_date,
3355             line_num,
3356             shipment_num,
3357             distribution_num,
3358             sequence,
3359             text_line)
3360              SELECT online_report_id,
3361             last_update_login,
3362             last_updated_by,
3363             last_update_date,
3364             created_by,
3365               creation_date,
3366             line_num,
3367             shipment_num,
3368             distribution_num,
3369             sequence,
3370             text_line
3371                FROM po_online_report_text_gt
3372               WHERE online_report_id = x_online_report_id;
3373 
3374 l_progress := '009';
3375 
3376     ELSE
3377         x_sub_check_status := FND_API.G_RET_STS_SUCCESS;
3378     END IF;
3379 
3380 l_progress := '010';
3381 
3382     x_return_status := FND_API.G_RET_STS_SUCCESS;
3383 
3384 -- bug3413891
3385 -- Deletion of GT tables has been moved to populate_global_temp_tables procedure
3386 
3387 l_progress := '011';
3388 
3389 IF g_debug_stmt THEN
3390     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3391       FND_LOG.string( FND_LOG.LEVEL_STATEMENT,
3392                     g_log_head || '.'||l_api_name||'.'|| l_progress,
3393                     'Returning from PVT package');
3394     END IF;
3395 END IF;
3396 
3397 EXCEPTION
3398 
3399     WHEN FND_API.G_EXC_ERROR THEN
3400         ROLLBACK TO POST_SUBMISSION_CHECK_SP;
3401         x_return_status := FND_API.G_RET_STS_ERROR;
3402         x_sub_check_status := FND_API.G_RET_STS_ERROR;
3403 
3404     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3405         ROLLBACK TO POST_SUBMISSION_CHECK_SP;
3406         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3407         x_sub_check_status := FND_API.G_RET_STS_ERROR;
3408 
3409     WHEN OTHERS THEN
3410         ROLLBACK TO POST_SUBMISSION_CHECK_SP;
3411         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3412         x_sub_check_status := FND_API.G_RET_STS_ERROR;
3413         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
3414             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
3415         END IF;
3416 
3417         IF ( g_debug_unexp ) THEN
3418             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
3419               FND_LOG.string( FND_LOG.level_unexpected,
3420                             g_log_head || l_api_name || '.others_exception',
3421                             'EXCEPTION: Location is ' || l_progress || ' SQL CODE is '||sqlcode);
3422             END IF;
3423         END IF;
3424 
3425 END post_submission_check;
3426 
3427  --Bug 4943365 Removed the check_asl procedure because blankets
3428  --should not do the asl checks.
3429  --PROCEDURE check_asl
3430 
3431 /**
3432 * Private Procedure: CHECK_REQUISITIONS
3433 * Requires:
3434 *   IN PARAMETERS:
3435 *       p_document_id:      The requisition_header_id of submitted document
3436 *       p_online_report_id: Id used to INSERT INTO online_report_text table
3437 *       p_user_id:          User performing the action
3438 *       p_login_id:         Last update login_id
3439 *   IN OUT PARAMETERS
3440 *       p_sequence:          Sequence number of last reported error
3441 * Modifies: Updates the PO_REQUISITION_LINES table with RATE information.
3442 *           Inserts error msgs in online_report_text_gt table, uses global_temp
3443 *           tables for processing
3444 * Effects:  This procedure runs the document submission checks for
3445 *           REQUISITIONS
3446 * Returns:
3447 *  p_sequence: This parameter contains the current count of number of error
3448 *              messages inserted
3449 */
3450 PROCEDURE check_requisitions(p_document_id IN NUMBER,
3451                        p_online_report_id IN NUMBER,
3452                        p_user_id IN NUMBER,
3453                        p_login_id IN NUMBER,
3454                        p_sequence IN OUT NOCOPY NUMBER,
3455                        x_return_status OUT NOCOPY VARCHAR2) IS
3456 
3457 l_textline  po_online_report_text.text_line%TYPE := NULL;
3458 l_api_name  CONSTANT varchar2(40) := 'CHECK_REQUISITIONS';
3459 l_progress VARCHAR2(3);
3460 
3461 TYPE NumTab is TABLE of NUMBER INDEX by BINARY_INTEGER;
3462 l_quantity1 NumTab;
3463 l_quantity2 NumTab;
3464 l_line_qty_tbl   NumTab;                                      -- <SERVICES FPJ>
3465 l_line_amt_tbl   NumTab;                                      -- <SERVICES FPJ>
3466 l_dist_qty_tbl   NumTab;                                      -- <SERVICES FPJ>
3467 l_dist_amt_tbl   NumTab;                                      -- <SERVICES FPJ>
3468 l_line_num   NumTab;
3469 l_shipment_num NumTab;
3470 l_dist_num NumTab;
3471 l_rowcount NumTab;
3472 
3473 TYPE value_basis_tbl_type IS
3474     TABLE OF PO_LINE_TYPES_B.order_type_lookup_code%TYPE;     -- <SERVICES FPJ>
3475 l_value_basis_tbl         value_basis_tbl_type;               -- <SERVICES FPJ>
3476 
3477 --<R12 eTax Integration Start>
3478 l_return_status    VARCHAR2(1);
3479 l_msg_count        NUMBER;
3480 l_msg_data         VARCHAR2(2000);
3481 l_tax_status       VARCHAR2(1);
3482 l_tax_message      fnd_new_messages.message_text%TYPE;
3483 --<R12 eTax Integration End>
3484 
3485 l_clm_req          VARCHAR2(1);   -- Bug 9667820
3486 
3487 BEGIN
3488 
3489 l_progress := '000';
3490 -- BUG 2687600 mbhargav
3491 --Removed Update statement to update rate in po_requistion_lines
3492 
3493 l_progress := '001';
3494 IF g_debug_stmt THEN
3495    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3496      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head||l_api_name||'.'
3497           || l_progress,'REQ 1: No lines ');
3498    END IF;
3499 END IF;
3500 
3501     -- Check 1: Each Requisition Header must have atleast one line
3502     -- PO_SUB_REQ_HEADER_NO_LINES
3503   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REQ_HEADER_NO_LINES');
3504   INSERT INTO po_online_report_text_gt(online_report_id,
3505         last_update_login,
3506         last_updated_by,
3507         last_update_date,
3508         created_by,
3509         creation_date,
3510         line_num,
3511         shipment_num,
3512         distribution_num,
3513         sequence,
3514         text_line,
3515                 message_name)
3516   SELECT  p_online_report_id,
3517         p_login_id,
3518         p_user_id,
3519           sysdate,
3520         p_user_id,
3521         sysdate,
3522         0,
3523         0,
3524         0,
3525         p_sequence + ROWNUM,
3526         substr(l_textline,1,240),
3527             'PO_SUB_REQ_HEADER_NO_LINES'
3528      FROM   PO_REQ_HEADERS_GT PRH
3529      WHERE  PRH.requisition_header_id = p_document_id
3530      AND    NOT EXISTS (SELECT 'Lines Exist'
3531                         FROM   PO_REQ_LINES_GT PRL
3532                         WHERE  PRL.requisition_header_id = PRH.requisition_header_id
3533                         AND    nvl(PRL.cancel_flag,'N') = 'N');
3534 
3535     --Increment the p_sequence with number of errors reported in last query
3536     p_sequence := p_sequence + SQL%ROWCOUNT;
3537 ------------------------------------------------
3538 
3539 l_progress := '002';
3540 IF g_debug_stmt THEN
3541    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3542      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head||l_api_name||'.'
3543           || l_progress,'REQ 2: No distributions');
3544    END IF;
3545 END IF;
3546 
3547     -- Check 2: Each Requisition line must have atleast one distribution
3548     -- PO_SUB_REQ_LINE_NO_DIST
3549 
3550   -- Bug 9667820
3551   -- CLM Req may have line without the distribution. So, relax this check for CLM Requisitions.
3552 
3553   l_clm_req := PO_CLM_INTG_GRP.is_clm_document(    p_doc_type => 'REQUISITION',
3554                                                 p_document_id => p_document_id );
3555 
3556   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REQ_LINE_NO_DIST');
3557   INSERT INTO po_online_report_text_gt(online_report_id,
3558         last_update_login,
3559         last_updated_by,
3560         last_update_date,
3561         created_by,
3562         creation_date,
3563         line_num,
3564         shipment_num,
3565         distribution_num,
3566         sequence,
3567         text_line,
3568                 message_name)
3569   SELECT  p_online_report_id,
3570         p_login_id,
3571         p_user_id,
3572           sysdate,
3573         p_user_id,
3574         sysdate,
3575         PRL.line_num,
3576         0,
3577         0,
3578         p_sequence + ROWNUM,
3579         substr(g_linemsg||g_delim||PRL.line_num||g_delim||l_textline,1,240),
3580             'PO_SUB_REQ_LINE_NO_DIST'
3581      FROM  PO_REQ_LINES_GT PRL
3582      WHERE PRL.requisition_header_id = p_document_id AND
3583            nvl(PRL.cancel_flag,'N') = 'N' AND
3584            nvl(PRL.closed_code,'OPEN') <> 'FINALLY CLOSED' AND
3585            nvl(PRL.modified_by_agent_flag,'N') = 'N' AND
3586            NOT EXISTS (SELECT 'Dist Exist'
3587                        FROM PO_REQ_DISTRIBUTIONS_GT PRD
3588                        WHERE PRD.requisition_line_id = PRL.requisition_line_id)
3589 		   AND Nvl(l_clm_req,'N') = 'N';
3590 
3591     --Increment the p_sequence with number of errors reported in last query
3592     p_sequence := p_sequence + SQL%ROWCOUNT;
3593 -------------------------------------------------
3594 
3595 l_progress := '003';
3596 IF g_debug_stmt THEN
3597    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3598      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head||l_api_name||'.'
3599           || l_progress,'REQ 3: Line qty does not match dist qty');
3600    END IF;
3601 END IF;
3602 
3603     -- Check 3: The sum of all distribution quantities/amounts must equal their
3604     -- corresponding line quantity/amount.
3605 
3606     SELECT
3607         PRL.line_num
3608     ,   PLT.order_type_lookup_code                            -- <SERVICES FPJ>
3609     ,   PRL.quantity
3610     ,   PRL.amount                                            -- <SERVICES FPJ>
3611     --Start Bug 13065293
3612     ,   round(sum(nvl(PRD.req_line_quantity, 0)),15)                   -- <SERVICES FPJ>
3613     --End Bug 13065293
3614     ,   sum(nvl(PRD.req_line_amount, 0))                      -- <SERVICES FPJ>
3615     BULK COLLECT INTO
3616         l_line_num
3617     ,   l_value_basis_tbl                                     -- <SERVICES FPJ>
3618     ,   l_line_qty_tbl                                        -- <SERVICES FPJ>
3619     ,   l_line_amt_tbl                                        -- <SERVICES FPJ>
3620     ,   l_dist_qty_tbl                                        -- <SERVICES FPJ>
3621     ,   l_dist_amt_tbl                                        -- <SERVICES FPJ>
3622     FROM
3623         PO_REQ_DISTRIBUTIONS_GT PRD
3624     ,   PO_REQ_LINES_GT         PRL
3625     ,   PO_LINE_TYPES_B         PLT                           -- <SERVICES FPJ>
3626     WHERE
3627         PRL.requisition_line_id = PRD.requisition_line_id
3628     AND PRL.requisition_header_id = p_document_id
3629     AND nvl(PRL.cancel_flag,'N') = 'N'
3630     AND nvl(PRL.closed_code,'OPEN') <> 'FINALLY CLOSED'
3631     AND nvl(PRL.modified_by_agent_flag,'N') = 'N'
3632     AND PRL.line_type_id = PLT.line_type_id                   -- <SERVICES FPJ>
3633     AND                                                       -- <SERVICES FPJ>
3634         (   (   ( PLT.order_type_lookup_code IN ('QUANTITY','AMOUNT')
3635                --Start Bug 13065293
3636                AND ( round(PRL.quantity,15) <> ( SELECT round(nvl(sum(PRD2.req_line_quantity),0),15)
3637                --End Bug 13065293
3638                                     FROM   PO_REQ_DISTRIBUTIONS_GT PRD2
3639                                     WHERE  PRD2.requisition_line_id = PRL.requisition_line_id ) ) )
3640         OR  (   ( PLT.order_type_lookup_code IN ('RATE','FIXED PRICE') )
3641             AND ( PRL.amount <>   ( SELECT nvl(sum(PRD2.req_line_amount),0)
3642                                     FROM   PO_REQ_DISTRIBUTIONS_GT PRD2
3643                                     WHERE  PRD2.requisition_line_id = PRL.requisition_line_id ) ) ) )
3644         )
3645     GROUP BY
3646         PRL.line_num
3647     ,   PLT.order_type_lookup_code                            -- <SERVICES FPJ>
3648     ,   PRL.quantity
3649     ,   PRL.amount;                                           -- <SERVICES FPJ>
3650 
3651     FOR i IN 1..l_line_num.COUNT LOOP
3652         l_rowCount(i) := i;
3653     END LOOP;
3654 
3655     FORALL i IN 1..l_line_num.COUNT
3656         INSERT INTO po_online_report_text_gt (online_report_id,
3657         last_update_login,
3658         last_updated_by,
3659         last_update_date,
3660         created_by,
3661         creation_date,
3662         line_num,
3663         shipment_num,
3664         distribution_num,
3665         sequence,
3666         text_line,
3667                 message_name)
3668         VALUES(
3669             p_online_report_id,
3670             p_login_id,
3671             p_user_id,
3672             sysdate,
3673             p_user_id,
3674             sysdate,
3675             l_line_num(i),
3676             NULL,                                             -- <SERVICES FPJ>
3677             NULL,                                             -- <SERVICES FPJ>
3678             p_sequence+l_rowCount(i),
3679             decode ( l_value_basis_tbl(i)                     -- <SERVICES FPJ>
3680                    , 'RATE'        , PO_CORE_S.get_translated_text
3681                                      (   'PO_SUB_REQ_LINE_NE_DIST_AMT'
3682                                      ,   'LINE_NUM', l_line_num(i)
3683                                      ,   'LINE_AMT', l_line_amt_tbl(i)
3684                                      ,   'DIST_AMT', l_dist_amt_tbl(i)
3685                                      )
3686                    , 'FIXED PRICE' , PO_CORE_S.get_translated_text
3687                                      (   'PO_SUB_REQ_LINE_NE_DIST_AMT'
3688                                      ,   'LINE_NUM', l_line_num(i)
3689                                      ,   'LINE_AMT', l_line_amt_tbl(i)
3690                                      ,   'DIST_AMT', l_dist_amt_tbl(i)
3691                                      )
3692                                    , PO_CORE_S.get_translated_text
3693                                      (   'PO_SUB_REQ_LINE_NE_DIST_QTY'
3694                                      ,   'LINE_NUM', l_line_num(i)
3695                                      ,   'LINE_QTY', l_line_qty_tbl(i)
3696                                      ,   'DIST_QTY', l_dist_qty_tbl(i)
3697                                      )
3698                    ),
3699             decode ( l_value_basis_tbl(i)                     -- <SERVICES FPJ>
3700                    , 'RATE'        , 'PO_SUB_REQ_LINE_NE_DIST_AMT'
3701                    , 'FIXED PRICE' , 'PO_SUB_REQ_LINE_NE_DIST_AMT'
3702                                    , 'PO_SUB_REQ_LINE_NE_DIST_QTY'
3703                    )
3704           );
3705 
3706     --Increment the p_sequence with number of errors reported in last query
3707     p_sequence := p_sequence + l_line_num.COUNT;
3708 ------------------------------------------------
3709 
3710 l_progress := '004';
3711 IF g_debug_stmt THEN
3712    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3713      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head||l_api_name||'.'
3714           || l_progress,'REQ 4: More than one dist for INVENTORY src type');
3715    END IF;
3716 END IF;
3717 
3718   -- Check 4:Lines with SOURCE type as INVENTORY can have one only one dist
3719     -- PO_SUB_REQ_SOURCE_ONE_DIST
3720 
3721   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REQ_SOURCE_ONE_DIST');
3722   INSERT INTO po_online_report_text_gt(online_report_id,
3723         last_update_login,
3724         last_updated_by,
3725         last_update_date,
3726         created_by,
3727         creation_date,
3728         line_num,
3729         shipment_num,
3730         distribution_num,
3731         sequence,
3732         text_line,
3733                 message_name)
3734   SELECT  p_online_report_id,
3735         p_login_id,
3736         p_user_id,
3737           sysdate,
3738         p_user_id,
3739         sysdate,
3740         PRL.line_num,
3741         0,
3742         0,
3743         p_sequence + ROWNUM,
3744         substr(g_linemsg||g_delim||PRL.line_num||g_delim||l_textline,1,240),
3745             'PO_SUB_REQ_SOURCE_ONE_DIST'
3746      FROM  PO_REQ_LINES_GT PRL
3747      WHERE PRL.requisition_header_id = p_document_id AND
3748            PRL.source_type_code = 'INVENTORY' AND
3749            nvl(PRL.cancel_flag,'N') = 'N' AND
3750            nvl(PRL.closed_code, 'OPEN') <> 'FINALLY CLOSED' AND
3751            1 < (SELECT count(PRD.requisition_line_id)
3752                 FROM  PO_REQ_DISTRIBUTIONS_GT PRD
3753                 WHERE PRD.requisition_line_id = PRL.requisition_line_id);
3754 
3755     --Increment the p_sequence with number of errors reported in last query
3756     p_sequence := p_sequence + SQL%ROWCOUNT;
3757 -----------------------------------------------------
3758 
3759 l_progress := '005';
3760 IF g_debug_stmt THEN
3761    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3762      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head||l_api_name||'.'
3763           || l_progress,'REQ 5: Rate cannot be nULL for foreign currency vendor');
3764    END IF;
3765 END IF;
3766 
3767   -- Check 5: Requistion Rate cannot be NULL if using a foreign currency vendor
3768     -- PO_SUB_REQ_RATE_NULL
3769   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REQ_RATE_NULL');
3770   INSERT INTO po_online_report_text_gt(online_report_id,
3771         last_update_login,
3772         last_updated_by,
3773         last_update_date,
3774         created_by,
3775         creation_date,
3776         line_num,
3777         shipment_num,
3778         distribution_num,
3779         sequence,
3780         text_line,
3781                 message_name)
3782   SELECT  p_online_report_id,
3783         p_login_id,
3784         p_user_id,
3785           sysdate,
3786         p_user_id,
3787         sysdate,
3788         PRL.line_num,
3789         0,
3790         0,
3791         p_sequence + ROWNUM,
3792         substr(g_linemsg||g_delim||PRL.line_num||g_delim||l_textline,1,240),
3793             'PO_SUB_REQ_RATE_NULL'
3794      FROM PO_REQ_LINES_GT PRL, FINANCIALS_SYSTEM_PARAMETERS FSP,
3795           GL_SETS_OF_BOOKS SOB
3796      WHERE PRL.requisition_header_id = p_document_id AND
3797            nvl(PRL.cancel_flag, 'N') = 'N' AND
3798            nvl(PRL.closed_code, 'OPEN') <> 'FINALLY CLOSED' AND
3799            SOB.set_of_books_id = FSP.set_of_books_id AND
3800            SOB.currency_code <> PRL.currency_code AND
3801            (PRL.rate is NULL OR
3802             PRL.rate_type is NULL OR
3803            (PRL.rate_type <> 'User' AND PRL.rate_date is NULL));
3804 
3805     --Increment the p_sequence with number of errors reported in last query
3806     p_sequence := p_sequence + SQL%ROWCOUNT;
3807 ----------------------------------------------
3808 
3809 l_progress := '006';
3810 
3811 -- Check 6: The Req GL date should be within an open purchasing period
3812 -- PO_SUB_REQ_INVALID_GL_DATE
3813 
3814 --<FPJ ENCUMBRANCE>
3815 
3816 IF (  PO_CORE_S.is_encumbrance_on(
3817          p_doc_type => g_document_type_REQUISITION
3818       ,  p_org_id => NULL
3819       )
3820    )
3821 THEN
3822 
3823    l_progress := '061';
3824    IF g_debug_stmt THEN
3825       PO_DEBUG.debug_stmt(g_log_head||l_api_name,
3826               l_progress,'REQ 6: GL date within open purchasing period check');
3827    END IF;
3828 
3829    check_gl_date(
3830       p_doc_type => g_document_type_REQUISITION
3831    ,  p_online_report_id => p_online_report_id
3832    ,  p_login_id => p_login_id
3833    ,  p_user_id => p_user_id
3834    ,  p_sequence => p_sequence
3835    );
3836 
3837    l_progress := '062';
3838 
3839 ELSE
3840    l_progress := '063';
3841    IF g_debug_stmt THEN
3842       PO_DEBUG.debug_stmt(g_log_head||l_api_name,
3843               l_progress,'REQ 6: Req encumbrance not on');
3844    END IF;
3845 END IF;
3846 
3847 l_progress := '007';
3848 IF g_debug_stmt THEN
3849    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3850      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head||l_api_name||'.'
3851           || l_progress,'REQ 7: Requisitions cannot have ATO/CTO model items');
3852    END IF;
3853 END IF;
3854 
3855   -- Check 7: Requisitions cannot have ATO/CTO model items (Bug 3362369)
3856         -- PO_ATO_ITEM_NA
3857   l_textline := FND_MESSAGE.GET_STRING('PO','PO_ATO_ITEM_NA');
3858   INSERT INTO po_online_report_text_gt(online_report_id,
3859         last_update_login,
3860         last_updated_by,
3861         last_update_date,
3862         created_by,
3863         creation_date,
3864         line_num,
3865         shipment_num,
3866         distribution_num,
3867         sequence,
3868         text_line,
3869                                 message_name)
3870   SELECT  p_online_report_id,
3871         p_login_id,
3872         p_user_id,
3873                   sysdate,
3874         p_user_id,
3875         sysdate,
3876         PRL.line_num,
3877         0,
3878         0,
3879         p_sequence + ROWNUM,
3880         substr(g_linemsg||g_delim||PRL.line_num||g_delim||l_textline,1,240),
3881                    'PO_ATO_ITEM_NA'
3882      FROM PO_REQ_LINES_GT PRL, FINANCIALS_SYSTEM_PARAMETERS FSP,
3883           MTL_SYSTEM_ITEMS MSI
3884      WHERE PRL.requisition_header_id = p_document_id AND
3885            nvl(PRL.cancel_flag, 'N') = 'N' AND
3886            nvl(PRL.closed_code, 'OPEN') <> 'FINALLY CLOSED' AND
3887            PRL.item_id is not null AND
3888            PRL.item_id = MSI.inventory_item_id AND
3889            FSP.inventory_organization_id = MSI.organization_id AND
3890            MSI.bom_item_type in (1,2);
3891 
3892     --Increment the p_sequence with number of errors reported in last query
3893     p_sequence := p_sequence + SQL%ROWCOUNT;
3894 -------------------------------------------------------------------------
3895 
3896     l_progress := '008';
3897 -- Check 8:
3898 --<R12 eTax Integration Start>
3899 -----------------------------------------------------------------------------
3900 
3901     l_tax_status := po_tax_interface_pvt.calculate_tax_yes_no(p_po_header_id   => NULL,
3902                                                               p_po_release_id  => NULL,
3903                                                               p_req_header_id  => p_document_id);
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,'REQ 8: Recalculate tax before approval = ' || l_tax_status);
3908         END IF;
3909     END IF;
3910     l_progress := '009';
3911     IF  l_tax_status = 'Y' THEN
3912       IF g_debug_stmt THEN
3913           IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3914             FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
3915                          l_api_name||'.' || l_progress,
3916                          'REQ 9: Calculate tax as the current one is not correct');
3917           END IF;
3918       END IF;
3919       l_progress := '010';
3920       po_tax_interface_pvt.calculate_tax_requisition( x_return_status         => l_return_status,
3921                                                       p_requisition_header_id => p_document_id,
3922                                                       p_calling_program       => g_action_DOC_SUBMISSION_CHECK);
3923 
3924       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3925         l_progress := '011';
3926         IF g_debug_stmt THEN
3927              IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3928                FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
3929                             l_api_name||'.' || l_progress,
3930                             'REQ 10: Calculate tax has errored out');
3931              END IF;
3932         END IF;
3933 
3934         l_tax_message := fnd_message.get_string('PO','PO_TAX_CALCULATION')||' : ' ;
3935 
3936         FOR i IN 1..po_tax_interface_pvt.G_TAX_ERRORS_TBL.MESSAGE_TEXT.COUNT
3937 	LOOP
3938            INSERT INTO po_online_report_text_gt
3939            (
3940             online_report_id,
3941             last_update_login,
3942             last_updated_by,
3943             last_update_date,
3944             created_by,
3945             creation_date,
3946             line_num,
3947             shipment_num,
3948             distribution_num,
3949             sequence,
3950             text_line,
3951             message_name,
3952             message_type
3953            )
3954            VALUES
3955            (
3956              p_online_report_id,
3957              p_login_id,
3958              p_user_id,
3959              sysdate,
3960              p_user_id,
3961              sysdate,
3962              po_tax_interface_pvt.G_TAX_ERRORS_TBL.line_num(i),
3963              po_tax_interface_pvt.G_TAX_ERRORS_TBL.shipment_num(i),
3964              po_tax_interface_pvt.G_TAX_ERRORS_TBL.distribution_num(i),
3965              p_sequence + i, /* 11851142 replaced with rownum */
3966              l_tax_message || po_tax_interface_pvt.G_TAX_ERRORS_TBL.message_text(i),
3967              'PO_TAX_CALCULATION_FAILED',
3968              'E'
3969            );
3970 	END LOOP;
3971         l_progress := '012';
3972       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3973         l_progress := '013';
3974         IF g_debug_stmt THEN
3975            IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
3976                FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
3977                             l_api_name||'.' || l_progress,
3978                             'REQ 11: Calculate tax raised unexpected error');
3979            END IF;
3980         END IF;
3981         l_textline := l_progress ||' - ';
3982         IF po_tax_interface_pvt.G_TAX_ERRORS_TBL.MESSAGE_TEXT.COUNT > 0 THEN
3983           l_textline := l_textline || po_tax_interface_pvt.G_TAX_ERRORS_TBL.MESSAGE_TEXT(1);
3984         ELSE
3985           l_textline := l_textline || SQLERRM();
3986         END IF;
3987         fnd_message.set_name('PO','PO_TAX_CALC_UNEXPECTED_ERROR');
3988         fnd_message.set_token('ERROR',l_textline);
3989         FND_MSG_PUB.Add;
3990         l_progress := '014';
3991         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3992       END IF;
3993     END IF;
3994 
3995     p_sequence := p_sequence + SQL%ROWCOUNT;
3996 
3997 -----------------------------------------------------------------------------
3998 
3999     l_progress := '015';
4000 --<R12 eTax Integration End>
4001     x_return_status := FND_API.G_RET_STS_SUCCESS;
4002 EXCEPTION
4003     WHEN FND_API.G_EXC_ERROR THEN
4004         x_return_status := FND_API.G_RET_STS_ERROR;
4005 
4006     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4007         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4008 
4009     WHEN OTHERS THEN
4010         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4011         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
4012             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
4013         END IF;
4014 
4015         IF (g_debug_unexp) THEN
4016                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
4017                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
4018                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
4019                        || l_progress || ' SQL CODE is '||sqlcode);
4020                 END IF;
4021         END IF;
4022 
4023 END CHECK_REQUISITIONS;
4024 --------------------------------------------------
4025 
4026 --For RELEASES
4027 /**
4028 * Private Procedure: CHECK_RELEASES
4029 * Requires:
4030 *   IN PARAMETERS:
4031 *       p_document_id:      The requisition_header_id of submitted document
4032 *       p_online_report_id: Id used to INSERT INTO online_report_text table
4033 *       p_user_id:          User performing the action
4034 *       p_login_id:         Last update login_id
4035 *   IN OUT PARAMETERS
4036 *       p_sequence:          Sequence number of last reported error
4037 * Modifies: Inserts error msgs in online_report_text_gt table, uses
4038 *           global_temp tables for processing
4039 * Effects:  This procedure runs the document submission checks for RELEASES
4040 * Returns:
4041 *  p_sequence: This parameter contains the current count of number of error
4042 *              messages inserted
4043 */
4044 PROCEDURE check_releases(p_document_id IN NUMBER,
4045                        p_online_report_id IN NUMBER,
4046                        p_user_id IN NUMBER,
4047                        p_login_id IN NUMBER,
4048                        p_sequence IN OUT NOCOPY NUMBER,
4049                        x_return_status OUT NOCOPY VARCHAR2) IS
4050 
4051 l_textline  po_online_report_text.text_line%TYPE := NULL;
4052 l_api_name  CONSTANT varchar2(40) := 'CHECK_RELEASES';
4053 l_progress VARCHAR2(3);
4054 
4055 --<Bug 2800804, 2792477 mbhargav START>
4056 l_total_rel_amount NUMBER :=0;
4057 l_this_rel_amount NUMBER :=0;
4058 l_previous_rel_amount NUMBER :=0;
4059 l_previous_rel_archive_amount NUMBER :=0; -- Bug13587303
4060 --<Bug 2800804, 2792477 mbhargav END>
4061 
4062 TYPE NumTab is TABLE of NUMBER INDEX by BINARY_INTEGER;
4063 l_quantity1 NumTab;
4064 l_quantity2 NumTab;
4065 l_line_num   NumTab;
4066 l_shipment_num NumTab;
4067 l_dist_num NumTab;
4068 l_rowcount NumTab;
4069 l_ship_qty_tbl    NumTab;                                     -- <SERVICES FPJ>
4070 l_ship_amt_tbl    NumTab;                                     -- <SERVICES FPJ>
4071 l_dist_qty_tbl    NumTab;                                     -- <SERVICES FPJ>
4072 l_dist_amt_tbl    NumTab;                                     -- <SERVICES FPJ>
4073 
4074 -- bug 6530879 Releases < Var addition START>
4075 l_agreement_id    PO_HEADERS_ALL.po_header_id%TYPE;
4076 l_vendor_id       PO_HEADERS_ALL.vendor_id%TYPE;
4077 l_vendor_site_id  PO_HEADERS_ALL.vendor_site_id%TYPE;
4078 l_vendor_contact_id PO_HEADERS_ALL.vendor_contact_id%TYPE;
4079 -- bug 6530879 Releases < Var addition END>
4080 
4081 --<R12 eTax Integration Start>
4082 l_return_status    VARCHAR2(1);
4083 l_tax_status       VARCHAR2(1);
4084 l_msg_count        NUMBER;
4085 l_msg_data         VARCHAR2(2000);
4086 l_tax_message      fnd_new_messages.message_text%TYPE;
4087 --<R12 eTax Integration End>
4088 
4089 --bug#3987438
4090 --A new table to hold the invalid ship to location codes
4091 TYPE CharTab is TABLE of HR_LOCATIONS_ALL_TL.location_code%type INDEX by BINARY_INTEGER;
4092 l_ship_to_location_tbl CharTab;
4093 --bug#3987438
4094 
4095 BEGIN
4096 
4097 l_progress := '000';
4098 -- BUG 2687600 mbhargav
4099 --Removed update statement to update rate in po_distributions
4100 
4101 l_progress := '001';
4102 IF g_debug_stmt THEN
4103    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4104      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
4105           || l_progress,'REL 1: PA should be approved');
4106    END IF;
4107 END IF;
4108 
4109     -- Check 1: The Purchase Agreement associated with the Release
4110     -- must be Approved.
4111     -- PO_SUB_REL_PA_APPROVED
4112     --< Bug 3422733 > Only do this check if the BPA is not ON HOLD. The ON HOLD
4113     -- check is done later. Avoids showing 2 msgs for BPA that is ON HOLD.
4114 
4115   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REL_PA_APPROVED');
4116   INSERT INTO po_online_report_text_gt(online_report_id,
4117         last_update_login,
4118         last_updated_by,
4119         last_update_date,
4120         created_by,
4121         creation_date,
4122         line_num,
4123         shipment_num,
4124         distribution_num,
4125         sequence,
4126         text_line,
4127                 message_name)
4128   SELECT  p_online_report_id,
4129         p_login_id,
4130         p_user_id,
4131           sysdate,
4132         p_user_id,
4133         sysdate,
4134         0,0,0,
4135         p_sequence + ROWNUM,
4136         substr(l_textline,1,240),
4137             'PO_SUB_REL_PA_APPROVED'
4138     FROM   PO_RELEASES_GT POR,PO_HEADERS_GT POH
4139     WHERE  POR.po_header_id = POH.po_header_id
4140     AND    POR.po_release_id = p_document_id
4141     AND    NVL(POH.approved_flag, 'N') <> 'Y'
4142     AND    NVL(POH.user_hold_flag, 'N') <> 'Y';     --< Bug 3422733 >
4143 
4144     --Increment the p_sequence with number of errors reported in last query
4145     p_sequence := p_sequence + SQL%ROWCOUNT;
4146 ---------------------------------------------------------
4147 
4148 l_progress := '002';
4149 IF g_debug_stmt THEN
4150    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4151      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
4152           || l_progress,'REL 2: Within PA effective dates ');
4153    END IF;
4154 END IF;
4155 
4156   -- Check 2: (Bug3199869)
4157         -- If release has not been approved before, the blanket
4158         -- cannot be expired already
4159         -- PO_SUB_REL_PA_EXPIRED
4160 
4161        -- bug3199869
4162        -- Modified Check 2 so that an error will be thrown only when
4163        -- 1) Release has not been approved once yet, AND
4164        -- 2) SYSDATE is after expiration date on the blanket header
4165 
4166        --Bug 8302986
4167        --TO CREATE RELEASES AGAINST BLANKETS OUT SIDE THE EFFECTIVE DATA RANGES OF THE BLANKET
4168        --WITH IN  THE TOLERANCE SPECIFIED BY THE PROFILE "PO: Release Creation Tolerance For Expired Blankets; in Days",
4169        --CHANGED THE WHERE CLAUSE TO EXCEEDE THE END DATE BY THE TOLERANCE VALUE
4170 
4171 
4172   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REL_PA_EXPIRED');
4173   INSERT INTO po_online_report_text_gt(online_report_id,
4174         last_update_login,
4175         last_updated_by,
4176         last_update_date,
4177         created_by,
4178         creation_date,
4179         line_num,
4180         shipment_num,
4181         distribution_num,
4182         sequence,
4183         text_line,
4184                 message_name)
4185   SELECT  p_online_report_id,
4186         p_login_id,
4187         p_user_id,
4188           sysdate,
4189         p_user_id,
4190         sysdate,
4191         0,
4192         0,
4193         0,
4194         p_sequence + ROWNUM,
4195         substr(l_textline,1,240),
4196             'PO_SUB_REL_PA_EXPIRED'                    -- bug3199869
4197     FROM   PO_RELEASES_GT POR,PO_HEADERS_GT POH
4198     WHERE  POR.po_header_id = POH.po_header_id
4199     AND    POR.po_release_id = p_document_id
4200     AND    POR.approved_date IS NULL                   -- bug3199869
4201     AND    TRUNC(SYSDATE) >                            -- bug3199869
4202            TRUNC(NVL(POH.end_date + nvl(FND_PROFILE.VALUE('PO_REL_CREATE_TOLERANCE'),0), SYSDATE + 1));      -- bug3199869
4203 
4204      --Increment the p_sequence with number of errors reported in last query
4205     p_sequence := p_sequence + SQL%ROWCOUNT;
4206 ----------------------------------------
4207 
4208 l_progress := '003';
4209 IF g_debug_stmt THEN
4210    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4211      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
4212           || l_progress,'REL 3: PA should not be ON HOLD');
4213    END IF;
4214 END IF;
4215 
4216   -- Check 3: Purchase Agreement assocaited with this release must not be
4217     -- on hold
4218     -- PO_SUB_REL_PA_ON_HOLD
4219 
4220   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REL_PA_ON_HOLD');
4221   INSERT INTO po_online_report_text_gt(online_report_id,
4222         last_update_login,
4223         last_updated_by,
4224         last_update_date,
4225         created_by,
4226         creation_date,
4227         line_num,
4228         shipment_num,
4229         distribution_num,
4230         sequence,
4231         text_line,
4232                 message_name)
4233   SELECT  p_online_report_id,
4234         p_login_id,
4235         p_user_id,
4236           sysdate,
4237         p_user_id,
4238         sysdate,
4239         0,
4240         0,
4241         0,
4242         p_sequence + ROWNUM,
4243         substr(l_textline,1,240),
4244             'PO_SUB_REL_PA_ON_HOLD'
4245     FROM  PO_RELEASES_GT POR,PO_HEADERS_GT POH
4246     WHERE  POR.po_header_id = POH.po_header_id
4247     AND    POR.po_release_id = p_document_id
4248     AND    nvl(POH.user_hold_flag, 'N') = 'Y';
4249 
4250 
4251      --Increment the p_sequence with number of errors reported in last query
4252     p_sequence := p_sequence + SQL%ROWCOUNT;
4253 -------------------------------------------------
4254 
4255 l_progress := '004';
4256 IF g_debug_stmt THEN
4257    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4258      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
4259           || l_progress,'REL 4: PAs Supplier should not be ON HOLD');
4260    END IF;
4261 END IF;
4262 
4263   -- Check 4: Associated Purchase Agreement's supplier should not be on hold
4264     -- PO_SUB_REL_VENDOR_ON_HOLD
4265 
4266   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REL_VENDOR_ON_HOLD');
4267   INSERT INTO po_online_report_text_gt(online_report_id,
4268         last_update_login,
4269         last_updated_by,
4270         last_update_date,
4271         created_by,
4272         creation_date,
4273         line_num,
4274         shipment_num,
4275         distribution_num,
4276         sequence,
4277         text_line,
4278                 message_name)
4279     SELECT  p_online_report_id,
4280         p_login_id,
4281         p_user_id,
4282           sysdate,
4283         p_user_id,
4284         sysdate,
4285         0,
4286         0,
4287         0,
4288         p_sequence + ROWNUM,
4289         substr(l_textline,1,240),
4290             'PO_SUB_REL_VENDOR_ON_HOLD'
4291     FROM  PO_RELEASES_GT POR,PO_HEADERS_GT POH,PO_VENDORS POV,
4292           PO_SYSTEM_PARAMETERS PSP
4293     WHERE  POR.po_header_id  = POH.po_header_id
4294     AND    POV.vendor_id     = POH.vendor_id
4295     AND    POR.po_release_id = p_document_id
4296     AND    nvl(PSP.ENFORCE_VENDOR_HOLD_FLAG,'N') = 'Y'
4297     AND    nvl(POV.hold_flag,'N') = 'Y';
4298 
4299 
4300      --Increment the p_sequence with number of errors reported in last query
4301     p_sequence := p_sequence + SQL%ROWCOUNT;
4302 ---------------------------------------------------
4303 
4304 l_progress := '005';
4305 IF g_debug_stmt THEN
4306    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4307      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
4308           || l_progress,'REL 5: Release must have atleast one shipment');
4309    END IF;
4310 END IF;
4311 
4312   -- Check 5: Every Release must have atleast one shipment
4313     -- PO_SUB_REL_NO_SHIP
4314 
4315   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REL_NO_SHIP');
4316   INSERT INTO po_online_report_text_gt(online_report_id,
4317         last_update_login,
4318         last_updated_by,
4319         last_update_date,
4320         created_by,
4321         creation_date,
4322         line_num,
4323         shipment_num,
4324         distribution_num,
4325         sequence,
4326         text_line,
4327                 message_name)
4328   SELECT  p_online_report_id,
4329         p_login_id,
4330         p_user_id,
4331           sysdate,
4332         p_user_id,
4333         sysdate,
4334         0,
4335         0,
4336         0,
4337         p_sequence + ROWNUM,
4338         substr(l_textline,1,240),
4339             'PO_SUB_REL_NO_SHIP'
4340     FROM  PO_RELEASES_GT POR
4341     WHERE  POR.po_release_id = p_document_id
4342     AND    NOT EXISTS
4343        (SELECT 'Shipment Exist'
4344         FROM   PO_LINE_LOCATIONS_GT PLL
4345         WHERE  PLL.po_release_id = POR.po_release_id);
4346         -- AND    nvl(PLL.cancel_flag,'N') = 'N');
4347         -- bug 3305488
4348 
4349      --Increment the p_sequence with number of errors reported in last query
4350     p_sequence := p_sequence + SQL%ROWCOUNT;
4351 
4352 -------------------------------------------
4353 --Bug5075191
4354 --Following submission check should exclude cancelled/finally closed lines.
4355 --Adding those conditions.
4356 --bug#3987438
4357 --Added a new submission check to validate the
4358 --ship to location at the shipment level.
4359 
4360 IF g_debug_stmt THEN
4361    FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
4362           || l_progress,'REL : Invalid Ship To Locations');
4363 END IF;
4364 
4365     SELECT POLG.shipment_num,
4366            HLT.location_code
4367     BULK COLLECT INTO
4368            l_line_num,
4369            l_ship_to_location_tbl
4370     FROM  PO_LINE_LOCATIONS_GT POLG,
4371           HR_LOCATIONS_ALL HLA,
4372           HR_LOCATIONS_ALL_TL HLT
4373     WHERE POLG.po_release_id = p_document_id
4374     AND   POLG.ship_to_location_id=HLA.location_id
4375     AND   nvl(POLG.cancel_flag,'N') = 'N'
4376     AND   nvl(POLG.closed_code,'OPEN') <> 'FINALLY CLOSED'
4377     AND   NVL (TRUNC (HLA.INACTIVE_DATE), TRUNC (SYSDATE)+1 )<= TRUNC (SYSDATE)
4378     AND   HLA.location_id=HLT.location_id
4379     AND   HLT.language=USERENV('LANG');
4380 
4381     FOR i IN 1..l_line_num.COUNT LOOP
4382         l_rowCount(i) := i;
4383     END LOOP;
4384 
4385 FORALL i IN 1..l_line_num.COUNT
4386     INSERT INTO po_online_report_text_gt (
4387             online_report_id,
4388 			last_update_login,
4389 			last_updated_by,
4390 			last_update_date,
4391 			created_by,
4392 			creation_date,
4393 			line_num,
4394 			shipment_num,
4395 			distribution_num,
4396 			sequence,
4397 			text_line,
4398             message_name)
4399     VALUES(
4400             p_online_report_id,
4401             p_login_id,
4402             p_user_id,
4403             sysdate,
4404             p_user_id,
4405             sysdate,
4406             0,
4407             0,
4408             0,                                             -- <SERVICES FPJ>
4409             p_sequence+l_rowCount(i),
4410             substr(PO_CORE_S.get_translated_text
4411                         ( 'PO_SUB_REL_INVALID_SHIP_TO',
4412                           'LINE_NUM',
4413                           l_line_num(i),
4414                           'SHIP_TO_LOC',
4415                           l_ship_to_location_tbl(i)
4416                           ),1,240),
4417             'PO_SUB_REL_INVALID_SHIP_TO'
4418         );
4419 
4420     p_sequence := p_sequence + l_line_num.COUNT;
4421 --bug#3987438
4422 
4423 -------------------------------------------
4424 
4425 l_progress := '006';
4426 IF g_debug_stmt THEN
4427    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4428      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
4429           || l_progress,'REL 6: check Price tolerance exceed on line');
4430    END IF;
4431 END IF;
4432 
4433   -- Check 6: Release Shipment Price/Amount should follow the pricing rules
4434     -- designated on the Purchase Agreement Line.
4435     -- Details: If price override is equal to 'Y', the Release Shipment
4436     -- Price/Amount must be less than the Price Override.
4437     --
4438     -- Bug 3177525: This check should not be done if allow_price_override_flag
4439     -- is 'N' because the user can never modify the release price/amount; it is
4440     -- automatically defaulted.
4441 
4442   INSERT INTO po_online_report_text_gt(online_report_id,
4443         last_update_login,
4444         last_updated_by,
4445         last_update_date,
4446         created_by,
4447         creation_date,
4448         line_num,
4449         shipment_num,
4450         distribution_num,
4451         sequence,
4452         text_line,
4453                 message_name)
4454   SELECT  p_online_report_id,
4455         p_login_id,
4456         p_user_id,
4457           sysdate,
4458         p_user_id,
4459         sysdate,
4460         0,
4461         PLL.shipment_num,
4462         0,
4463         p_sequence + ROWNUM,
4464             decode ( POL.order_type_lookup_code                -- <BUG 3262859>
4465                    , 'FIXED PRICE' , PO_CORE_S.get_translated_text
4466                                    (   'PO_SUB_REL_PRICE_GT_LIMIT'
4467                                    ,   'SHIP_NUM'   , PLL.shipment_num
4468                                    ,   'SHIP_PRICE' , nvl( PLL.price_override,PLL.amount)
4469                                    ,   'LINE_PRICE' , nvl ( POL.not_to_exceed_price
4470                                                           , PLL.price_override )
4471                                    )
4472                    ,               PO_CORE_S.get_translated_text
4473                                    (   'PO_SUB_REL_AMT_GT_LIMIT'
4474                                    ,   'SHIP_NUM'   , PLL.shipment_num
4475                                    ,   'SHIP_AMT'   , PLL.amount
4476                                    ,   'LINE_AMT'   , nvl ( POL.not_to_exceed_price
4477                                                           , PLL.amount )
4478                                    )
4479                    ),
4480             decode ( POL.order_type_lookup_code                -- <BUG 3262859>
4481                    , 'FIXED PRICE' , 'PO_SUB_REL_SHIP_PRICE_GT_LIMIT'
4482                    ,                 'PO_SUB_REL_SHIP_AMT_GT_LIMIT'
4483                    )
4484     FROM PO_LINE_LOCATIONS_GT PLL,PO_LINES POL
4485     WHERE PLL.po_line_id = POL.po_line_id
4486     AND   PLL.po_release_id = p_document_id
4487     AND   nvl(PLL.cancel_flag,'N')= 'N'
4488     AND   nvl(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
4489     AND   POL.allow_price_override_flag = 'Y'                 -- Bug 3177525
4490     AND   (                                                   -- <SERVICES FPJ>
4491               (   ( POL.order_type_lookup_code IN ('QUANTITY','AMOUNT','RATE'))-- <BUG 3262859>
4492               AND ( PLL.price_override > nvl ( POL.not_to_exceed_price
4493                                              , PLL.price_override ) )
4494               )
4495           OR
4496               (   ( POL.order_type_lookup_code IN ('FIXED PRICE') )               -- <BUG 3262859>
4497               AND ( PLL.amount > nvl ( POL.not_to_exceed_price, PLL.amount ) ) )
4498           )
4499     ;
4500 
4501      --Increment the p_sequence with number of errors reported in last query
4502     p_sequence := p_sequence + SQL%ROWCOUNT;
4503 ----------------------------------------
4504 
4505 l_progress := '007';
4506 IF g_debug_stmt THEN
4507    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4508      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
4509           || l_progress,'REL 7: Rel shipment qty should match dist qty');
4510    END IF;
4511 END IF;
4512 
4513   -- Check 7: Quantities/Amounts must match between Release Shipments and
4514     -- Distributions
4515 
4516     SELECT
4517         0
4518     ,   PLL.shipment_num
4519     ,   0
4520     ,   PLL.quantity
4521     ,   PLL.amount                                            -- <SERVICES FPJ>
4522     ,   nvl(sum(POD.quantity_ordered),0)                      -- <SERVICES FPJ>
4523     ,   nvl(sum(POD.amount_ordered),0)                        -- <SERVICES FPJ>
4524     BULK COLLECT INTO
4525         l_line_num
4526     ,   l_shipment_num
4527     ,   l_dist_num
4528     ,   l_ship_qty_tbl                                        -- <SERVICES FPJ>
4529     ,   l_ship_amt_tbl                                        -- <SERVICES FPJ>
4530     ,   l_dist_qty_tbl                                        -- <SERVICES FPJ>
4531     ,   l_dist_amt_tbl                                        -- <SERVICES FPJ>
4532     FROM
4533         PO_DISTRIBUTIONS_GT POD                       -- <PO_CHANGE_API FPJ>
4534     ,   PO_LINE_LOCATIONS_GT PLL
4535     WHERE
4536         PLL.line_location_id = POD.line_location_id
4537     AND PLL.po_release_id = p_document_id
4538     AND nvl(PLL.cancel_flag,'N') = 'N'
4539     AND nvl(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
4540     AND (                                                     -- <SERVICES FPJ>
4541             (   ( PLL.quantity IS NOT NULL )
4542             AND ( PLL.quantity <> ( SELECT sum(POD2.quantity_ordered)
4543                                     FROM   PO_DISTRIBUTIONS_GT POD2
4544                                     WHERE  POD2.line_location_id = PLL.line_location_id ) ) )
4545         OR  (   ( PLL.amount IS NOT NULL )
4546             AND ( PLL.amount <> ( SELECT sum(POD2.amount_ordered)
4547                                   FROM   PO_DISTRIBUTIONS_GT POD2
4548                                   WHERE  POD2.line_location_id = PLL.line_location_id ) ) )
4549         )
4550     GROUP BY
4551         PLL.shipment_num
4552     ,   PLL.quantity
4553     ,   PLL.amount;                                           -- <SERVICES FPJ>
4554 
4555     FOR i IN 1..l_line_num.COUNT LOOP
4556         l_rowCount(i) := i;
4557     END LOOP;
4558 
4559     FORALL i IN 1..l_line_num.COUNT
4560         INSERT INTO po_online_report_text_gt(online_report_id,
4561         last_update_login,
4562         last_updated_by,
4563         last_update_date,
4564         created_by,
4565         creation_date,
4566         line_num,
4567         shipment_num,
4568         distribution_num,
4569         sequence,
4570         text_line,
4571                 message_name)
4572         VALUES(
4573             p_online_report_id,
4574             p_login_id,
4575             p_user_id,
4576             sysdate,
4577             p_user_id,
4578             sysdate,
4579             NULL,                                             -- <SERVICES FPJ>
4580             l_shipment_num(i),
4581             NULL,                                             -- <SERVICES FPJ>
4582             p_sequence+l_rowCount(i),
4583             decode ( l_ship_qty_tbl(i)                        -- <SERVICES FPJ>
4584                    , NULL , PO_CORE_S.get_translated_text
4585                             (   'PO_SUB_REL_SHIP_NE_DIST_AMT'
4586                             ,   'SHIP_NUM', l_shipment_num(i)
4587                             ,   'SHIP_AMT', l_ship_amt_tbl(i)
4588                             ,   'DIST_AMT', l_dist_amt_tbl(i)
4589                             )
4590                    ,        PO_CORE_S.get_translated_text
4591                             (   'PO_SUB_REL_SHIP_NE_DIST_QTY'
4592                             ,   'SHIP_NUM', l_shipment_num(i)
4593                             ,   'SHIP_QTY', l_ship_qty_tbl(i)
4594                             ,   'DIST_QTY', l_dist_qty_tbl(i)
4595                             )
4596                    ),
4597             decode ( l_ship_qty_tbl(i)                        -- <SERVICES FPJ>
4598                    , NULL , 'PO_SUB_REL_SHIP_NE_DIST_AMT'
4599                    ,        'PO_SUB_REL_SHIP_NE_DIST_QTY'
4600                    )
4601         );
4602 
4603     --Increment the p_sequence with number of errors reported in last query
4604     p_sequence := p_sequence + l_line_num.COUNT;
4605 ---------------------------------------------------------
4606 
4607 l_progress := '008';
4608 IF g_debug_stmt THEN
4609    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4610      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
4611           || l_progress,'REL 8: Rel shipment should have atleast one dist ');
4612    END IF;
4613 END IF;
4614 
4615   -- Check 8: All Release shipments should have atleast one distribution
4616     -- PO_SUB_REL_SHIP_NO_DIST
4617 
4618   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REL_SHIP_NO_DIST');
4619   INSERT INTO po_online_report_text_gt(online_report_id,
4620         last_update_login,
4621         last_updated_by,
4622         last_update_date,
4623         created_by,
4624         creation_date,
4625         line_num,
4626         shipment_num,
4627         distribution_num,
4628         sequence,
4629         text_line,
4630                 message_name)
4631   SELECT  p_online_report_id,
4632         p_login_id,
4633         p_user_id,
4634           sysdate,
4635         p_user_id,
4636         sysdate,
4637         0,
4638         PLL.shipment_num,
4639         0,
4640         p_sequence + ROWNUM,
4641         substr(g_shipmsg||g_delim||PLL.shipment_num||g_delim
4642                    ||l_textline,1,240),
4643             'PO_SUB_REL_SHIP_NO_DIST'
4644     FROM PO_LINE_LOCATIONS_GT PLL
4645     WHERE PLL.po_release_id = p_document_id
4646     AND nvl(PLL.cancel_flag,'N') = 'N'
4647     AND nvl(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
4648     AND NOT EXISTS
4649        (SELECT 'Distribution Exists'
4650         FROM PO_DISTRIBUTIONS_GT POD                  -- <PO_CHANGE_API FPJ>
4651         WHERE POD.line_location_id = PLL.line_location_id);
4652 
4653 
4654      --Increment the p_sequence with number of errors reported in last query
4655     p_sequence := p_sequence + SQL%ROWCOUNT;
4656 -----------------------------------------------------
4657 
4658 l_progress := '009';
4659 IF g_debug_stmt THEN
4660    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4661      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
4662           || l_progress,'REL 9: Dist rate cannot be NULL if using foreign currency vendor');
4663    END IF;
4664 END IF;
4665 
4666   -- Check 9: Rate on the Release shipment must not be NULL if using a foreign
4667     -- currency vendor. We are using foreign currency if the currency on PO
4668     -- Header does not match Set of Books currency
4669     -- PO_SUB_REL_RATE_NULL
4670   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REL_RATE_NULL');
4671     INSERT INTO po_online_report_text_gt(online_report_id,
4672         last_update_login,
4673         last_updated_by,
4674         last_update_date,
4675         created_by,
4676         creation_date,
4677         line_num,
4678         shipment_num,
4679         distribution_num,
4680         sequence,
4681         text_line,
4682                 message_name)
4683     SELECT  p_online_report_id,
4684         p_login_id,
4685         p_user_id,
4686           sysdate,
4687         p_user_id,
4688         sysdate,
4689         0,
4690         POLL.shipment_num,
4691             POD.distribution_num,
4692         p_sequence + ROWNUM,
4693         substr(g_shipmsg||g_delim||POLL.shipment_num||g_delim
4694                    ||g_distmsg||g_delim||POD.distribution_num||g_delim
4695                    ||l_textline,1,240),
4696             'PO_SUB_REL_RATE_NULL'
4697     FROM PO_DISTRIBUTIONS_GT POD,                     -- <PO_CHANGE_API FPJ>
4698          PO_LINE_LOCATIONS_GT POLL,PO_LINES POL,
4699          PO_RELEASES_GT POR,PO_HEADERS_GT POH,GL_SETS_OF_BOOKS SOB,
4700          FINANCIALS_SYSTEM_PARAMETERS FSP
4701     WHERE POLL.po_release_id = POR.po_release_id
4702     AND POD.line_location_id = POLL.line_location_id
4703     AND POLL.po_line_id = POL.po_line_id
4704     AND POH.po_header_id = POR.po_header_id
4705     AND POR.po_release_id = p_document_id
4706     AND SOB.set_of_books_id = FSP.set_of_books_id
4707     AND nvl(POLL.cancel_flag, 'N') = 'N'
4708     AND nvl(POLL.closed_code, 'OPEN') <> 'FINALLY CLOSED'
4709     AND SOB.currency_code <> POH.currency_code
4710     AND POD.rate is null;
4711 
4712 
4713      --Increment the p_sequence with number of errors reported in last query
4714     p_sequence := p_sequence + SQL%ROWCOUNT;
4715 -----------------------------------------------
4716 
4717 l_progress := '010';
4718 IF g_debug_stmt THEN
4719    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4720      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
4721           || l_progress,'REL 10: Amount should be greater than min release amount ');
4722    END IF;
4723 END IF;
4724 
4725   -- Check 10:Release total amount must be greater than Purchase Agreement
4726     -- minimum release amount
4727     -- PO_SUB_REL_AMT_LESS_MINREL_AMT
4728 
4729   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REL_AMT_LESS_MINREL_AMT');
4730   INSERT INTO po_online_report_text_gt(online_report_id,
4731         last_update_login,
4732         last_updated_by,
4733         last_update_date,
4734         created_by,
4735         creation_date,
4736         line_num,
4737         shipment_num,
4738         distribution_num,
4739         sequence,
4740         text_line,
4741                 message_name)
4742   SELECT  p_online_report_id,
4743         p_login_id,
4744         p_user_id,
4745           sysdate,
4746         p_user_id,
4747         sysdate,
4748         0,
4749         0,
4750         0,
4751         p_sequence + ROWNUM,
4752         substr(l_textline,1,240),
4753             'PO_SUB_REL_AMT_LESS_MINREL_AMT'
4754     FROM  PO_HEADERS_GT POH,PO_RELEASES_GT POR
4755     WHERE POR.po_release_id = p_document_id
4756     AND   POH.po_header_id  = POR.po_header_id
4757     AND   POH.min_release_amount IS NOT NULL
4758     AND   POH.min_release_amount >                            -- <SERVICES FPJ>
4759               ( SELECT decode ( sum( decode ( PLL2.quantity
4760                                             , NULL , ( PLL2.amount - nvl(PLL2.amount_cancelled,0) )
4761                                             ,        ( PLL2.quantity - nvl(PLL2.quantity_cancelled,0) )
4762                                             )
4763                                    )
4764                               , 0 , POH.min_release_amount
4765                               ,     sum ( decode ( PLL2.quantity
4766                                                  , NULL , ( PLL2.amount - nvl(PLL2.amount_cancelled,0) )
4767                                                  ,        (   ( PLL2.quantity - nvl(PLL2.quantity_cancelled,0) )
4768                                                           *   PLL2.price_override )
4769                                                  )
4770                                         )
4771                               )
4772                 FROM   PO_LINE_LOCATIONS_GT PLL2
4773                 WHERE  PLL2.po_release_id = POR.po_release_id
4774                 AND    PLL2.shipment_type IN ('BLANKET', 'SCHEDULED')
4775               );
4776 
4777      --Increment the p_sequence with number of errors reported in last query
4778     p_sequence := p_sequence + SQL%ROWCOUNT;
4779 -------------------------------------------------
4780 -- Bug 7188760
4781 -- Added the POH.Amount_Limit is Not Null condition in the following sqls
4782 l_progress := '011';
4783 IF g_debug_stmt THEN
4784    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4785      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
4786           || l_progress,'REL 11: Not to exceed Amount Limit');
4787    END IF;
4788 END IF;
4789 
4790   -- Check 11: The amount being released plus the amount released to-date
4791     -- against the PA must be less than the amount limit for the agreement
4792     -- PO_SUB_REL_AMT_GRT_LIMIT_AMT
4793 
4794 
4795        --<Bug 2800804, 2792477 mbhargav START>
4796        --Split the amount calculation to two select statements,
4797        --this was required because iSP is sending some chnage_requests
4798        --which are only in GT tables so need to get the amount for
4799        --current document from GT table and not the base tables
4800 
4801        --This select statement gets the amount on current document
4802 
4803        SELECT                                                 -- <SERVICES FPJ>
4804               sum ( decode ( PLL1.quantity
4805                            , NULL , ( PLL1.amount
4806                                     - nvl(PLL1.amount_cancelled,0) )
4807                            ,        (   ( PLL1.quantity
4808                                         - nvl(PLL1.quantity_cancelled,0) )
4809                                     * PLL1.price_override ) ) )
4810        INTO  l_this_rel_amount
4811        FROM PO_LINE_LOCATIONS_GT PLL1, PO_RELEASES_GT POR1, PO_HEADERS_GT POH
4812        WHERE  POR1.po_release_id = p_document_id
4813        AND    POR1.po_header_id  = POH.po_header_id	-- Bug 7188760
4814        AND    POH.amount_limit is Not Null		-- Bug 7188760
4815        AND    PLL1.po_release_id = POR1.po_release_id;
4816 
4817        --This select statement adds amount from all OTHER approved releases
4818        --for this Blanket
4819 
4820        	--Bug 13587303
4821 	/*
4822 	1.	We will consider the Sum of Quantity for Approved Releases + Sum of Quantity for all other releases that were approved and now in Requires Reapproval or Rejected status.
4823 	2.	For sum of quantity for other releases that were approved and now in Requires Reapproval or Rejected Status, we will be getting the quantity  details from Archive Tables for the latest row.
4824 	*/
4825 
4826        SELECT                                                 -- <SERVICES FPJ>
4827               /* FULL(POH)*/
4828               nvl ( sum ( decode ( PLL2.quantity
4829                                  , NULL , ( PLL2.amount
4830                                           - nvl(PLL2.amount_cancelled,0) )
4831                                  ,        (   ( PLL2.quantity
4832                                           - nvl(PLL2.quantity_cancelled,0) )
4833                                           * PLL2.price_override ) ) )
4834                   , 0 )
4835        INTO l_previous_rel_amount
4836        FROM PO_LINE_LOCATIONS PLL2, PO_RELEASES POR2, PO_HEADERS_GT POH
4837        WHERE PLL2.po_release_id = POR2.po_release_id
4838        AND POR2.po_header_id = POH.po_header_id
4839        AND nvl(POR2.approved_flag, 'N') = 'Y'
4840        AND POH.amount_limit is Not Null		-- Bug 7188760
4841        AND POR2.po_release_id <> p_document_id;
4842 
4843 -- For sum of quantity for other releases that were approved and now in Requires Reapproval or Rejected Status,
4844 -- we will be getting the quantity  details from Archive Tables for the latest row.
4845 --Bug 13587303
4846 
4847  			SELECT           -- <SERVICES FPJ>
4848               /* FULL(POH)*/
4849               nvl ( sum ( decode ( PLL2.quantity
4850                                  , NULL , ( PLL2.amount
4851                                           - nvl(PLL2.amount_cancelled,0) )
4852                                  ,        (   ( PLL2.quantity
4853                                           - nvl(PLL2.quantity_cancelled,0) )
4854                                           * PLL2.price_override ) ) )
4855                   , 0 )
4856        INTO l_previous_rel_archive_amount
4857        FROM po_line_locations_archive_all PLL2, po_releases_all POR2, PO_HEADERS_GT POH
4858        WHERE PLL2.po_release_id = POR2.po_release_id
4859        AND POR2.po_header_id = POH.po_header_id
4860        AND nvl(POR2.approved_flag, 'N') IN ('R','F')
4861        and NVL(PLL2.LATEST_EXTERNAL_FLAG,'N') = 'Y'
4862        AND POH.amount_limit is Not Null
4863        AND POR2.po_release_id <> p_document_id;
4864 
4865 
4866        --Get the total amount released/to be released for this shipment
4867        l_total_rel_amount := l_this_rel_amount + l_previous_rel_amount + l_previous_rel_archive_amount; --Bug 13587303
4868 
4869        --<Bug 2800804, 2792477 mbhargav END>
4870 
4871 
4872   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REL_AMT_GRT_LIMIT_AMT');
4873   INSERT INTO po_online_report_text_gt (online_report_id,
4874         last_update_login,
4875         last_updated_by,
4876         last_update_date,
4877         created_by,
4878         creation_date,
4879         line_num,
4880         shipment_num,
4881         distribution_num,
4882         sequence,
4883         text_line,
4884                 message_name)
4885   SELECT  p_online_report_id,
4886         p_login_id,
4887         p_user_id,
4888           sysdate,
4889         p_user_id,
4890         sysdate,
4891         0,
4892         0,
4893         0,
4894         p_sequence + ROWNUM,
4895         substr(l_textline,1,240),
4896             'PO_SUB_REL_AMT_GRT_LIMIT_AMT'
4897     FROM   PO_HEADERS_GT POH,PO_RELEASES_GT POR
4898     WHERE  POR.po_release_id = p_document_id
4899     AND    POH.po_header_id  = POR.po_header_id
4900 
4901     AND    POH.amount_limit is not null
4902     --<Bug 2800804, 2792477 mbhargav START>
4903     --Compare it to total of current release amount and
4904     --already released amount calculated above the INSERT statement
4905     AND    POH.amount_limit < l_total_rel_amount;
4906 
4907      --Increment the p_sequence with number of errors reported in last query
4908     p_sequence := p_sequence + SQL%ROWCOUNT;
4909 ------------------------------------------------
4910 
4911 l_progress := '012';
4912 IF g_debug_stmt THEN
4913    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4914      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
4915           || l_progress,
4916           'REL 12: Amount released check for min release amt');
4917    END IF;
4918 END IF;
4919 
4920   -- Check 12: The Amount being released for all shipments for a particular line
4921     -- must be greater than the min release amount specified in agreement line
4922     -- PO_SUB_REL_SHIPAMT_LESS_MINREL
4923 
4924     l_textline := FND_MESSAGE.GET_STRING('PO','PO_SUB_REL_SHIPAMT_LESS_MINREL');
4925     SELECT
4926         POL.line_num,
4927         0,
4928         0,
4929         POL.min_release_amount,
4930         0
4931     BULK COLLECT INTO
4932         l_line_num,
4933         l_shipment_num,
4934         l_dist_num,
4935         l_quantity1,
4936         l_quantity2
4937     FROM   PO_LINES_ALL POL,PO_RELEASES_GT POR,PO_LINE_LOCATIONS_GT PLL
4938     WHERE  PLL.po_release_id = POR.po_release_id
4939     AND    PLL.po_release_id = p_document_id
4940     AND    POL.po_line_id  = PLL.po_line_id
4941     AND    POL.min_release_amount is not null
4942     AND    POL.min_release_amount >
4943            (   SELECT
4944                   decode ( sum ( decode ( PLL2.quantity                   /*Bug 5028960 pol.quantity */
4945                                          , NULL , PLL2.amount - nvl(PLL2.amount_cancelled,0)
4946                                          ,        PLL2.quantity - nvl(PLL2.quantity_cancelled,0)
4947                                          )
4948                                 )
4949                           , 0 , POL.min_release_amount
4950                           ,     sum ( decode ( PLL2.quantity     /*Bug 5028960  pol.quantity */
4951                                              , NULL , PLL2.amount - nvl(PLL2.amount_cancelled,0)
4952                                              ,        (  ( PLL2.quantity - nvl(PLL2.quantity_cancelled,0) )
4953                                                       *  PLL2.price_override )
4954                                              )
4955                                     )
4956                           )
4957                --<Bug 2792477 mbhargav>
4958                --Change the table in from clause from PO_LINE_LOCATIONS to PO_LINE_LOCATIONS_GT
4959                FROM PO_LINE_LOCATIONS_GT PLL2
4960                WHERE PLL2.po_line_id = POL.po_line_id
4961                AND PLL2.po_release_id = POR.po_release_id
4962                AND PLL2.shipment_type in ('BLANKET', 'SCHEDULED')
4963           )
4964     GROUP BY POL.line_num,POL.min_release_amount;
4965 
4966     FOR i IN 1..l_line_num.COUNT LOOP
4967         l_rowCount(i) := i;
4968     END LOOP;
4969 
4970     FORALL i IN 1..l_line_num.COUNT
4971         INSERT INTO po_online_report_text_gt(online_report_id,
4972         last_update_login,
4973         last_updated_by,
4974         last_update_date,
4975         created_by,
4976         creation_date,
4977         line_num,
4978         shipment_num,
4979         distribution_num,
4980         sequence,
4981         text_line,
4982                 message_name)
4983         VALUES(
4984             p_online_report_id,
4985             p_login_id,
4986             p_user_id,
4987             sysdate,
4988             p_user_id,
4989             sysdate,
4990             0,0,0,
4991             p_sequence+l_rowCount(i),
4992             substr(l_textline||g_delim||l_quantity1(i),1,240),
4993             'PO_SUB_REL_SHIPAMT_LESS_MINREL');
4994 
4995     --Increment the p_sequence with number of errors reported in last query
4996     p_sequence := p_sequence + l_line_num.COUNT;
4997 ----------------------------------------------
4998 
4999 l_progress := '600';
5000 
5001 -- Check 13: The Release GL date should be within an open purchasing period
5002 -- PO_SUB_REL_INVALID_GL_DATE
5003 
5004 --<FPJ ENCUMBRANCE>
5005 
5006 IF (  PO_CORE_S.is_encumbrance_on(
5007          p_doc_type => g_document_type_RELEASE
5008       ,  p_org_id => NULL
5009       )
5010    )
5011 THEN
5012 
5013    l_progress := '610';
5014    IF g_debug_stmt THEN
5015       PO_DEBUG.debug_stmt(g_log_head || '.'||l_api_name||'.',
5016                           l_progress,'REL 13: GL date check ');
5017    END IF;
5018 
5019    check_gl_date(
5020       p_doc_type => g_document_type_RELEASE
5021    ,  p_online_report_id => p_online_report_id
5022    ,  p_login_id => p_login_id
5023    ,  p_user_id => p_user_id
5024    ,  p_sequence => p_sequence
5025    );
5026 
5027    l_progress := '620';
5028 
5029 ELSE
5030    l_progress := '630';
5031    IF g_debug_stmt THEN
5032       PO_DEBUG.debug_stmt(g_log_head || '.'||l_api_name||'.',
5033                           l_progress,'REL 13: release encumbrance not on');
5034    END IF;
5035 END IF;
5036 
5037 ----------------------------------------------
5038 
5039 l_progress := '014';
5040 IF g_debug_stmt THEN
5041    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5042      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
5043           || l_progress,'Rel 14: UOM Interclass conversions check');
5044    END IF;
5045 END IF;
5046 
5047   -- Check 14: Invalid Interclass conversions between UOMs should not be allowed
5048     -- PO_SUB_UOM_CLASS_CONVERSION, PO_SUB_REL_INVALID_CLASS_CONV
5049     -- Message inserted is:
5050     --'Shipment# <ShipNum> Following Interclass UOM conversion is not defined or
5051     -- is disabled <UOM1> <UOM2>'
5052     --   Bug #1630662
5053   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_UOM_CLASS_CONVERSION');
5054     INSERT INTO po_online_report_text_gt(online_report_id,
5055         last_update_login,
5056         last_updated_by,
5057         last_update_date,
5058         created_by,
5059         creation_date,
5060         line_num,
5061         shipment_num,
5062         distribution_num,
5063         sequence,
5064         text_line,
5065                                 message_name)
5066   SELECT  p_online_report_id,
5067         p_login_id,
5068         p_user_id,
5069           sysdate,
5070         p_user_id,
5071         sysdate,
5072             0,
5073             POLL.shipment_num,
5074             0,
5075             p_sequence + ROWNUM,
5076             substr(g_shipmsg||g_delim||POLL.shipment_num||g_delim||l_textline||
5077                    MTL1.uom_class||' , '||MTL2.uom_class,1,240),
5078             'PO_SUB_UOM_CLASS_CONVERSION'
5079     FROM MTL_UOM_CLASS_CONVERSIONS MOU, PO_LINE_LOCATIONS_GT POLL,
5080          PO_LINES POL, MTL_UOM_CLASSES_TL MTL1,
5081          MTL_UOM_CLASSES_TL MTL2
5082     WHERE MOU.inventory_item_id = POL.item_id
5083     AND   (NVL(MOU.disable_date, TRUNC(SYSDATE)) + 1) < TRUNC(SYSDATE)
5084     AND   POL.po_line_id = POLL.po_line_id
5085     AND   POLL.po_release_id = p_document_id
5086     AND   MOU.from_uom_class = MTL1.uom_class
5087     AND   MOU.to_uom_class = MTL2.uom_class
5088     AND EXISTS
5089        (SELECT 'uom conversion exists'
5090         FROM MTL_UNITS_OF_MEASURE MUM
5091         WHERE POL.unit_meas_lookup_code = MUM.unit_of_measure
5092         AND   MOU.to_uom_class = MUM.uom_class);
5093 
5094     --Increment the p_sequence with number of errors reported in last query
5095     p_sequence := p_sequence + SQL%ROWCOUNT;
5096 
5097 --------------------------------------------------
5098 
5099 l_progress := '015';
5100 IF g_debug_stmt THEN
5101    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5102      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
5103           || l_progress,'REL 15: Item restricted check ');
5104    END IF;
5105 END IF;
5106 
5107   -- Check 15:  If an item is restricted then the Purchase Order Vendor
5108     -- must be listed in the Approved Suppliers List table and must be approved.
5109     -- PO_SUB_ITEM_NOT_APPROVED_REL
5110     -- Bug# 2461828
5111     /*Bug5597639 Modifying the below sql to ensure that whenever the item
5112      is restricted by checking 'Use approved supplier list' there should be
5113      atlease one approved ASL either at item level or at category level
5114      if there is no item ASL*/
5115 
5116   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_ITEM_NOT_APPROVED_REL');
5117   INSERT INTO po_online_report_text_gt(online_report_id,
5118         last_update_login,
5119         last_updated_by,
5120         last_update_date,
5121         created_by,
5122         creation_date,
5123         line_num,
5124         shipment_num,
5125         distribution_num,
5126         sequence,
5127         text_line,
5128                 message_name)
5129   SELECT  p_online_report_id,
5130         p_login_id,
5131         p_user_id,
5132           sysdate,
5133         p_user_id,
5134         sysdate,
5135         POL.line_num, --<Bug 3123365>
5136         PLL.shipment_num,  --<Bug 3123365>
5137         0,
5138         p_sequence + ROWNUM,
5139                     --<Bug 3123365 mbhargav START>
5140                     substr(g_linemsg||g_delim||POL.line_num||g_delim
5141                    ||g_shipmsg||g_delim||PLL.shipment_num||g_delim
5142                    ||l_textline,1,240),
5143                    --<Bug 3123365 mbhargav END>
5144             'PO_SUB_ITEM_NOT_APPROVED_REL'
5145     FROM MTL_SYSTEM_ITEMS MSI, PO_LINE_LOCATIONS_GT PLL,
5146          PO_RELEASES_GT POR,PO_LINES POL, PO_HEADERS_GT POH,
5147          FINANCIALS_SYSTEM_PARAMETERS FSP
5148     WHERE POR.po_release_id = p_document_id
5149     AND POR.po_header_id = POH.po_header_id
5150     AND POR.po_header_id = POL.po_header_id
5151     AND POL.po_line_id = PLL.po_line_id
5152     AND POR.po_release_id = PLL.po_release_id
5153     AND MSI.organization_id = PLL.SHIP_TO_ORGANIZATION_id
5154     AND MSI.inventory_item_id = POL.item_id
5155     AND POL.item_id is not null
5156     AND nvl(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
5157     AND nvl(POL.cancel_flag,'N') = 'N'
5158     AND nvl(PLL.cancel_flag,'N') = 'N'
5159     AND nvl(MSI.must_use_approved_vendor_flag,'N') = 'Y'
5160     AND not exists
5161        (SELECT 1
5162         FROM PO_APPROVED_SUPPLIER_LIS_VAL_V ASL, PO_ASL_STATUS_RULES ASR
5163         WHERE  ASL.using_organization_id in (PLL.ship_to_organization_id, -1)
5164         AND    ASL.vendor_id = POH.vendor_id
5165         AND    nvl(ASL.vendor_site_id, POH.vendor_site_id) = POH.vendor_site_id
5166         AND   ASL.item_id = POL.item_id
5167         AND    ASL.asl_status_id = ASR.status_id
5168         AND    ASR.business_rule = '1_PO_APPROVAL'
5169 	AND    ASR.allow_action_flag = 'Y'        --Bug5597639
5170         UNION ALL
5171         SELECT 1
5172         FROM PO_APPROVED_SUPPLIER_LIS_VAL_V ASL, PO_ASL_STATUS_RULES ASR
5173         WHERE  ASL.using_organization_id in (PLL.ship_to_organization_id , -1)
5174         AND    ASL.vendor_id = POH.vendor_id
5175         AND    nvl(ASL.vendor_site_id, POH.vendor_site_id) = POH.vendor_site_id
5176         AND    ASL.item_id is NULL
5177         AND    not exists
5178            (SELECT ASL1.ASL_ID
5179             FROM PO_APPROVED_SUPPLIER_LIS_VAL_V ASL1
5180             WHERE ASL1.ITEM_ID = POL.item_id
5181             AND ASL1.using_organization_id in (PLL.ship_to_organization_id, -1))
5182         AND    ASL.category_id in
5183            (SELECT MIC.category_id
5184             FROM   MTL_ITEM_CATEGORIES MIC
5185             WHERE MIC.inventory_item_id = POL.item_id
5186             AND MIC.organization_id = PLL.ship_to_organization_id)
5187         AND    ASL.asl_status_id = ASR.status_id
5188         AND    ASR.business_rule = '1_PO_APPROVAL'
5189         AND    ASR.allow_action_flag = 'Y') ;  --Bug5597639
5190 
5191      --Increment the p_sequence with number of errors reported in last query
5192     p_sequence := p_sequence + SQL%ROWCOUNT;
5193 ---------------------------------------------
5194 
5195 l_progress := '016';
5196 IF g_debug_stmt THEN
5197    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5198      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
5199           || l_progress,'REL 16: ASL Debarred check ');
5200    END IF;
5201 END IF;
5202 
5203   -- Check 16: Determine if an item is restricted.  If it is restricted the
5204     -- Purchase Order Vendor must be listed in the Approved Suppliers
5205     -- List table and must be approved for release to get approved.
5206     -- Bug 839743
5207     -- PO_SUB_ITEM_ASL_DEBARRED_REL
5208 
5209       /*Bug5597639 This check would throw an error message if atleast one ASL
5210        entry is debarred either for item /Category irrespective of 'Use approved
5211        supplier flag'. This check would apply even for one time items.
5212        If supplier is debarred in any of the ASL item/category (Global/Local)
5213        (Suplier/Supplier+site) then the approval of the PO will not be allowed
5214        Need to remove the join with mtl_item_categories for one
5215        time items as there will not be any record
5216  */
5217 
5218   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_ITEM_ASL_DEBARRED_REL');
5219   INSERT INTO po_online_report_text_gt(online_report_id,
5220         last_update_login,
5221         last_updated_by,
5222         last_update_date,
5223         created_by,
5224         creation_date,
5225         line_num,
5226         shipment_num,
5227         distribution_num,
5228         sequence,
5229         text_line,
5230                 message_name)
5231   SELECT  p_online_report_id,
5232         p_login_id,
5233         p_user_id,
5234           sysdate,
5235         p_user_id,
5236         sysdate,
5237                     POL.line_num, --<Bug 3123365>
5238                     PLL.shipment_num,  --<Bug 3123365>
5239                     0,
5240                     p_sequence + ROWNUM,
5241                     --<Bug 3123365 mbhargav START>
5242                     substr(g_linemsg||g_delim||POL.line_num||g_delim
5243                    ||g_shipmsg||g_delim||PLL.shipment_num||g_delim
5244                    ||l_textline,1,240),
5245                    --<Bug 3123365 mbhargav END>
5246             'PO_SUB_ITEM_ASL_DEBARRED_REL'
5247     FROM PO_LINE_LOCATIONS_GT PLL,
5248          PO_RELEASES_GT POR,PO_LINES POL, PO_HEADERS_GT POH,
5249          FINANCIALS_SYSTEM_PARAMETERS FSP
5250     WHERE POR.po_release_id = p_document_id
5251     AND POR.po_header_id = POH.po_header_id
5252     AND POR.po_header_id = POL.po_header_id
5253     AND POL.po_line_id = PLL.po_line_id
5254     AND POR.po_release_id = PLL.po_release_id
5255     AND nvl(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
5256     AND nvl(POL.cancel_flag,'N') = 'N'
5257     AND nvl(PLL.cancel_flag,'N') = 'N'
5258     AND exists
5259       (SELECT 1
5260         FROM PO_APPROVED_SUPPLIER_LIS_VAL_V ASL, PO_ASL_STATUS_RULES ASR,
5261 	 MTL_SYSTEM_ITEMS MSI  --Bug5597639
5262          WHERE  ASL.using_organization_id in (PLL.ship_to_organization_id, -1)
5263         /*Bug5553138 Adding the below three conditions */
5264 	AND MSI.organization_id = FSP.inventory_organization_id
5265 	AND MSI.inventory_item_id = POL.item_id
5266 	AND  POL.item_id is not null
5267         AND    ASL.vendor_id = POH.vendor_id
5268         AND    nvl(ASL.vendor_site_id, POH.vendor_site_id) = POH.vendor_site_id
5269         AND   ASL.item_id = POL.item_id
5270         AND    ASL.asl_status_id = ASR.status_id
5271         AND    ASR.business_rule = '1_PO_APPROVAL'
5272  	AND   ASR.allow_action_flag <> 'Y'   --Bug5597639
5273         UNION ALL
5274          SELECT 1
5275         FROM PO_APPROVED_SUPPLIER_LIS_VAL_V ASL, PO_ASL_STATUS_RULES ASR
5276         WHERE  ASL.using_organization_id in (PLL.ship_to_organization_id , -1)
5277         AND    ASL.vendor_id = POH.vendor_id
5278         AND    nvl(ASL.vendor_site_id, POH.vendor_site_id) = POH.vendor_site_id
5279         AND    ASL.item_id is NULL
5280 	AND ASL.category_id = POL.category_id --Bug5597639
5281         AND    ASL.asl_status_id = ASR.status_id
5282         AND    ASR.business_rule = '1_PO_APPROVAL'
5283         AND    ASR.allow_action_flag <> 'Y' );  --Bug5597639
5284      --Increment the p_sequence with number of errors reported in last query
5285     p_sequence := p_sequence + SQL%ROWCOUNT;
5286 -------------------------------------------------------------------------
5287 
5288  l_progress := '017';
5289 
5290     IF g_debug_stmt THEN
5291         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5292           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
5293                        l_api_name||'.' || l_progress,
5294                        'REL 17: ATO/CTO Model items not allowed on Releases');
5295         END IF;
5296     END IF;
5297 
5298     -- Check 17: ATO/CTO Model items not allowed on Releases (Bug 3362369)
5299 
5300     l_textline := FND_MESSAGE.get_string('PO', 'PO_ATO_ITEM_NA');
5301 
5302     INSERT INTO po_online_report_text_gt(
5303        online_report_id,
5304        last_update_login,
5305        last_updated_by,
5306        last_update_date,
5307        created_by,
5308        creation_date,
5309        line_num,
5310        shipment_num,
5311        distribution_num,
5312        sequence,
5313        text_line,
5314        message_name
5315     )
5316    SELECT   p_online_report_id,
5317         p_login_id,
5318         p_user_id,
5319           sysdate,
5320         p_user_id,
5321         sysdate,
5322                     POL.line_num,
5323                     PLL.shipment_num,
5324                     0,
5325                     p_sequence + ROWNUM,
5326                     substr(g_linemsg||g_delim||POL.line_num||g_delim
5327                    ||g_shipmsg||g_delim||PLL.shipment_num||g_delim
5328                    ||l_textline,1,240),
5329            'PO_ATO_ITEM_NA'
5330     FROM   po_lines POL,
5331            po_line_locations_gt PLL,
5332            financials_system_parameters FSP,
5333            mtl_system_items MSI
5334     WHERE  PLL.po_release_id = p_document_id
5335     AND    PLL.po_line_id = POL.po_line_id
5336     AND    POL.item_id is not null
5337     AND    nvl(POL.cancel_flag, 'N') = 'N'                     --Bug5353423
5338     AND    nvl(POL.closed_code, 'OPEN') <> 'FINALLY CLOSED'    --Bug5353423
5339     AND    POL.item_id = MSI.inventory_item_id
5340     AND    MSI.organization_id = FSP.inventory_organization_id
5341     AND    MSI.bom_item_type in (1,2);
5342 
5343     --Increment the p_sequence with number of errors reported in last query
5344     p_sequence := p_sequence + SQL%ROWCOUNT;
5345 --------------------------------------------------------------------
5346 
5347 
5348  /* Start Bug #3512688
5349     To check the validity of the item added at the line level*/
5350       l_progress := '018';
5351       IF g_debug_stmt THEN
5352       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5353         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
5354        || l_progress,'REL 018: Item has to purchasable');
5355       END IF;
5356        END IF;
5357 
5358        -- Check 018: Item has to purchasable
5359 
5360        l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_ALL_NO_ITEM');
5361        INSERT INTO po_online_report_text_gt(online_report_id,
5362        last_update_login,
5363        last_updated_by,
5364        last_update_date,
5365        created_by,
5366        creation_date,
5367        line_num,
5368        shipment_num,
5369        distribution_num,
5370        sequence,
5371        text_line,
5372        message_name)
5373        SELECT  p_online_report_id,
5374        p_login_id,
5375        p_user_id,
5376        sysdate,
5377        p_user_id,
5378        sysdate,
5379        0,
5380        pll.shipment_num,
5381        0,
5382        p_sequence + ROWNUM,
5383         substr(g_shipmsg||g_delim||pll.shipment_num||g_delim||l_textline,1,240),
5384        'PO_ALL_NO_ITEM'
5385        from po_releases_gt por,po_lines pl,po_line_locations_gt pll,mtl_system_items  itm,po_line_types_b plt
5386        where itm.inventory_item_id  = pl.item_id
5387        and   pl.item_id is not null
5388 
5389       and   pl.po_line_id   = pll.po_line_id
5390        and   itm.organization_id    = pll.ship_to_organization_id
5391        and   itm.purchasing_enabled_flag = 'N'
5392        and   pll.po_release_id   = por.po_release_id
5393        and   por.po_release_id = p_document_id
5394        and   pll.po_release_id is not null
5395        and   pl.line_type_id = plt.line_type_id
5396        and   nvl(plt.outside_operation_flag,'N')=nvl(itm.outside_operation_flag,'N')
5397        and   (pll.creation_date >= nvl(por.approved_date,pll.creation_date));
5398 
5399        --Increment the p_sequence with number of errors reported in last query
5400        p_sequence := p_sequence + SQL%ROWCOUNT;
5401      --End Bug #3512688
5402 
5403 l_progress := '019';
5404 IF g_debug_stmt THEN
5405    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5406      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
5407           || l_progress,'Rel 19: Cannot approve documents on hold');
5408    END IF;
5409 END IF;
5410     -- Check 19: Release should not be on hold (Bug 3678912)
5411         -- PO_ON_HOLD_CANNOT_APPROVE
5412 
5413   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_ON_HOLD_CANNOT_APPROVE');
5414   INSERT INTO po_online_report_text_gt(online_report_id,
5415         last_update_login,
5416         last_updated_by,
5417         last_update_date,
5418         created_by,
5419         creation_date,
5420         line_num,
5421         shipment_num,
5422         distribution_num,
5423         sequence,
5424         text_line,
5425                                 message_name)
5426   SELECT  p_online_report_id,
5427         p_login_id,
5428         p_user_id,
5429                   sysdate,
5430         p_user_id,
5431         sysdate,
5432         0,
5433         0,
5434         0,
5435         p_sequence + ROWNUM,
5436         substr(l_textline,1,240),
5437                    'PO_ON_HOLD_CANNOT_APPROVE'
5438     FROM  PO_RELEASES_GT POR
5439     WHERE  POR.po_release_id = p_document_id
5440     AND    nvl(POR.HOLD_FLAG,'N') = 'Y';
5441 
5442     --Increment the p_sequence with number of errors reported in last query
5443     p_sequence := p_sequence + SQL%ROWCOUNT;
5444 
5445     l_progress := '020';
5446 
5447 ---------------------------------------------------------------------------------
5448 -- bug 6530879 Releases <Start>
5449 select poh.po_header_id,poh.vendor_id, poh.vendor_site_id, poh.vendor_contact_id
5450   into l_agreement_id, l_vendor_id,l_vendor_site_id,l_vendor_contact_id
5451   from po_headers_gt poh, po_releases_gt por
5452   where poh.po_header_id = por.po_header_id
5453     and por.po_release_id = p_document_id;
5454 -- bug 6530879
5455 -- Check 20: Vendor should be valid when approving the document.
5456 -- Important for reapproval, to avoid the case when the vendor has
5457 -- been invalidated by first successful approval.
5458 
5459 l_textline :=  FND_MESSAGE.GET_STRING('PO', 'PO_PDOI_INVALID_VENDOR');
5460 ---------------------------------------------------------------------------
5461   if (l_vendor_id is NOT NULL) then
5462 	fnd_message.set_name('PO', 'PO_PDOI_INVALID_VENDOR');
5463         fnd_message.set_token('VALUE', to_char(l_vendor_id), FALSE);
5464 	l_textline := substr(fnd_message.get, 1, 240);
5465 
5466 
5467  	INSERT INTO po_online_report_text_gt(online_report_id,
5468  				last_update_login,
5469  				last_updated_by,
5470  				last_update_date,
5471  				created_by,
5472  				creation_date,
5473  				line_num,
5474  				shipment_num,
5475  				distribution_num,
5476  				sequence,
5477  				text_line,
5478                                 message_name)
5479  	SELECT 	p_online_report_id,
5480  		    p_login_id,
5481  		    p_user_id,
5482      	            sysdate,
5483  		    p_user_id,
5484  		    sysdate,
5485  		    0,
5486  		    0,
5487  		    0,
5488  		    p_sequence + ROWNUM,
5489  		    substr(l_textline,1,240),
5490                    'PO_PDOI_INVALID_VENDOR'
5491 	FROM  dual
5492     where not exists (select 'Y'
5493 			from PO_HEADERS_GT POH, po_vendors pov
5494 		       WHERE  POH.po_header_id = l_agreement_id
5495 		         AND  pov.vendor_id = poh.vendor_id
5496 			 AND  pov.enabled_flag = 'Y'
5497 			 AND  SYSDATE BETWEEN nvl(pov.start_date_active, SYSDATE-1)
5498                                   AND nvl(pov.end_date_active, SYSDATE+1));
5499  --Increment the p_sequence with number of errors reported in last query
5500         p_sequence := p_sequence + SQL%ROWCOUNT;
5501 
5502  end if;
5503 
5504 -------------------------------------------------------------------------------------
5505 -- Check 21: check the validity of the vendor site.
5506  if (l_vendor_site_id is not null) then
5507 
5508   	fnd_message.set_name('PO', 'PO_PDOI_INVALID_VENDOR_SITE');
5509  	fnd_message.set_token('VALUE', to_char(l_vendor_site_id), FALSE);
5510 	l_textline := substr(fnd_message.get, 1, 240);
5511 
5512  	INSERT INTO po_online_report_text_gt(online_report_id,
5513  				last_update_login,
5514  				last_updated_by,
5515  				last_update_date,
5516  				created_by,
5517  				creation_date,
5518  				line_num,
5519  				shipment_num,
5520  				distribution_num,
5521  				sequence,
5522  				text_line,
5523                                 message_name)
5524  	SELECT 	p_online_report_id,
5525  		    p_login_id,
5526  		    p_user_id,
5527      	            sysdate,
5528  		    p_user_id,
5529  		    sysdate,
5530  		    0,
5531  		    0,
5532  		    0,
5533  		    p_sequence + ROWNUM,
5534  		    substr(l_textline,1,240),
5535                    'PO_PDOI_INVALID_VENDOR_SITE'
5536     FROM  dual
5537     where not exists (select 'Y'
5538 			from PO_HEADERS_GT POH, po_vendor_sites povs
5539 		        WHERE  POH.po_header_id = l_agreement_id
5540 		        AND  povs.vendor_site_id = poh.vendor_site_id
5541 			AND    nvl(povs.rfq_only_site_flag,'N') <> 'Y'
5542 			AND    povs.purchasing_site_flag = 'Y'
5543 		        AND    SYSDATE < nvl(povs.inactive_date, SYSDATE + 1));
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 -------------------------------------------------------------------------------------
5549 -- check 22: validate vendor contact
5550 if (l_vendor_contact_id is not null) then
5551 
5552 	fnd_message.set_name('PO', 'PO_PDOI_INVALID_VDR_CNTCT');
5553  	fnd_message.set_token('VALUE', to_char(l_vendor_contact_id), FALSE);
5554 	l_textline := substr(fnd_message.get, 1, 240);
5555 
5556 	INSERT INTO po_online_report_text_gt(online_report_id,
5557  				last_update_login,
5558  				last_updated_by,
5559  				last_update_date,
5560  				created_by,
5561  				creation_date,
5562  				line_num,
5563  				shipment_num,
5564  				distribution_num,
5565  				sequence,
5566  				text_line,
5567                                 message_name)
5568  	SELECT 	p_online_report_id,
5569  		    p_login_id,
5570  		    p_user_id,
5571      	            sysdate,
5572  		    p_user_id,
5573  		    sysdate,
5574  		    0,
5575  		    0,
5576  		    0,
5577  		    p_sequence + ROWNUM,
5578  		    substr(l_textline,1,240),
5579                    'PO_PDOI_INVALID_VDR_CNTCT'
5580 	FROM  dual
5581 	--Start of code changes for the bug 16244229
5582 	WHERE NOT EXISTS ( SELECT  'Y'
5583 			FROM
5584 			AP_SUPPLIER_CONTACTS PVC ,
5585 			AP_SUPPLIER_SITES_ALL PVS ,
5586 			HZ_PARTIES HP ,
5587 			HZ_RELATIONSHIPS HPR ,
5588 			HZ_PARTY_SITES HPS ,
5589 			HZ_ORG_CONTACTS HOC ,
5590 			HZ_PARTIES HP2 ,
5591 			AP_SUPPLIERS APS,
5592 			PO_HEADERS_ALL POH
5593 			WHERE PVC.PER_PARTY_ID = HP.PARTY_ID
5594 			AND PVC.REL_PARTY_ID   = HP2.PARTY_ID
5595 			AND PVC.ORG_CONTACT_ID                           = HOC.ORG_CONTACT_ID(+)
5596 			AND PVC.RELATIONSHIP_ID                          = HPR.RELATIONSHIP_ID
5597 			AND HPR.DIRECTIONAL_FLAG                         ='F'
5598 			AND NVL( APS.VENDOR_TYPE_LOOKUP_CODE, 'DUMMY' ) <> 'EMPLOYEE'
5599 			AND ( (Pvc.Party_Site_Id  = Hps.Party_Site_Id
5600 				AND PVS.PARTY_SITE_ID                     = PVC.ORG_PARTY_SITE_ID
5601 				AND PVS.VENDOR_ID                                = APS.VENDOR_ID
5602 				AND SYSDATE < nvl( LEAST(NVL(HPR.END_DATE, TO_DATE('12/31/4712','MM/DD/RRRR')), NVL(PVC.INACTIVE_DATE, TO_DATE('12/31/4712','MM/DD/RRRR'))), SYSDATE+1))
5603 			OR (PVC.ORG_PARTY_SITE_ID                       IS NULL
5604 				AND PVC.VENDOR_SITE_ID                          IS NULL
5605 				AND HPR.OBJECT_ID                                = APS.PARTY_ID
5606 				AND HPR.RELATIONSHIP_CODE                        = 'CONTACT_OF'
5607 				And Hpr.Object_Type                              = 'ORGANIZATION'
5608 				AND SYSDATE < NVL(HPR.END_DATE, SYSDATE+1) )
5609 			)
5610 			AND POH.VENDOR_CONTACT_ID = PVC.VENDOR_CONTACT_ID
5611 			AND POH.PO_HEADER_ID=l_agreement_id);
5612 	--End of code changes for the bug 16244229
5613 
5614    --Increment the p_sequence with number of errors reported in last query
5615         p_sequence := p_sequence + SQL%ROWCOUNT;
5616 end if;
5617 
5618 -- bug 6530879 Releases <END>
5619 
5620 
5621 --<R12 eTax Integration Start>
5622 -----------------------------------------------------------------------------
5623 
5624     l_tax_status := po_tax_interface_pvt.calculate_tax_yes_no(p_po_header_id  => NULL,
5625                                                               p_po_release_id => p_document_id,
5626                                                               p_req_header_id => NULL);
5627     l_progress := '021';
5628     IF g_debug_stmt THEN
5629         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5630           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head ||l_api_name||'.'
5631                         || l_progress, 'Rel 21: Recalculate tax before approval = ' || l_tax_status);
5632         END IF;
5633     END IF;
5634 
5635     IF l_tax_status = 'Y' THEN
5636       IF g_debug_stmt THEN
5637           IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5638             FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
5639                          l_api_name||'.' || l_progress,
5640                          'Rel 22: Calculate tax as the current one is not correct');
5641           END IF;
5642       END IF;
5643       l_progress := '021';
5644       po_tax_interface_pvt.calculate_tax( x_return_status    => l_return_status,
5645                                           p_po_header_id     => NULL,
5646                                           p_po_release_id    => p_document_id,
5647                                           p_calling_program  => g_action_DOC_SUBMISSION_CHECK);
5648       l_progress := '022';
5649       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
5650          IF g_debug_stmt THEN
5651              IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5652                FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
5653                             l_api_name||'.' || l_progress,
5654                             'Rel 23: Calculate tax has errored out');
5655              END IF;
5656          END IF;
5657          l_progress := '023';
5658 
5659          l_tax_message := fnd_message.get_string('PO','PO_TAX_CALCULATION')||' : ' ;
5660 
5661          FOR i IN 1..po_tax_interface_pvt.G_TAX_ERRORS_TBL.MESSAGE_TEXT.COUNT
5662 	LOOP
5663            INSERT INTO po_online_report_text_gt
5664            (
5665             online_report_id,
5666             last_update_login,
5667             last_updated_by,
5668             last_update_date,
5669             created_by,
5670             creation_date,
5671             line_num,
5672             shipment_num,
5673             distribution_num,
5674             sequence,
5675             text_line,
5676             message_name,
5677             message_type
5678            )
5679            VALUES
5680            (
5681              p_online_report_id,
5682              p_login_id,
5683              p_user_id,
5684              sysdate,
5685              p_user_id,
5686              sysdate,
5687              po_tax_interface_pvt.G_TAX_ERRORS_TBL.line_num(i),
5688              po_tax_interface_pvt.G_TAX_ERRORS_TBL.shipment_num(i),
5689              po_tax_interface_pvt.G_TAX_ERRORS_TBL.distribution_num(i),
5690              p_sequence + i, /* 11851142 Replaced with i from ROWNUM */
5691              l_tax_message || po_tax_interface_pvt.G_TAX_ERRORS_TBL.message_text(i),
5692              'PO_TAX_CALCULATION_FAILED',
5693              'E'
5694            );
5695 	END LOOP;
5696          l_progress := '024';
5697       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5698         IF g_debug_stmt THEN
5699            IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5700                FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
5701                             l_api_name||'.' || l_progress,
5702                             'Rel 24: Calculate tax raised unexpected error');
5703            END IF;
5704         END IF;
5705         l_textline := l_progress ||' - ';
5706         IF po_tax_interface_pvt.G_TAX_ERRORS_TBL.MESSAGE_TEXT.COUNT > 0 THEN
5707           l_textline := l_textline || po_tax_interface_pvt.G_TAX_ERRORS_TBL.MESSAGE_TEXT(1);
5708         ELSE
5709           l_textline := l_textline || SQLERRM();
5710         END IF;
5711         fnd_message.set_name('PO','PO_TAX_CALC_UNEXPECTED_ERROR');
5712         fnd_message.set_token('ERROR',l_textline);
5713         FND_MSG_PUB.Add;
5714         l_progress := '025';
5715         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5716       END IF;
5717     END IF;
5718 
5719     p_sequence := p_sequence + SQL%ROWCOUNT;
5720 
5721 -----------------------------------------------------------------------------
5722 
5723     l_progress := '026';
5724 --<R12 eTax Integration End>
5725 
5726 IF g_debug_stmt THEN
5727     	IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5728         	FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
5729          		       || l_progress,'Rel 25: LCM enabled release shipment should have invoice match option as receipt');
5730    	END IF;
5731     END IF;
5732     -- Check 25: Validation for LCM enabled release to check whether its shipment has invoice match option as 'Receipt'
5733     l_textline := FND_MESSAGE.GET_STRING('PO','PO_SUB_REL_SHIP_INV_MATCH_NE_R');
5734     INSERT INTO po_online_report_text_gt (online_report_id,
5735 	 				  last_update_login,
5736 	 				  last_updated_by,
5737 	 				  last_update_date,
5738 	 				  created_by,
5739 	 				  creation_date,
5740 	 				  line_num,
5741 	 				  shipment_num,
5742 	 				  distribution_num,
5743 	 				  sequence,
5744 	 				  text_line,
5745 	                                  message_name)
5746     SELECT p_online_report_id,
5747 	   p_login_id,
5748 	   p_user_id,
5749 	   sysdate,
5750 	   p_user_id,
5751 	   sysdate,
5752 	   0,
5753 	   PLL.shipment_num,
5754            0,
5755 	   p_sequence + ROWNUM,
5756 	   substr (g_shipmsg||g_delim||PLL.shipment_num||g_delim||l_textline,1,240),
5757 	   'PO_SUB_REL_SHIP_INV_MATCH_NE_R'
5758       FROM PO_RELEASES_GT POR,
5759            PO_LINE_LOCATIONS_GT PLL
5760      WHERE POR.po_release_id = PLL.po_release_id
5761        AND POR.po_release_id = p_document_id
5762        AND Nvl(PLL.LCM_FLAG,'N') = 'Y'
5763        AND Nvl(PLL.match_option,'P') <> 'R'
5764        AND Nvl(PLL.cancel_flag,'N') = 'N'; -- Bug 13809830: Added a condition to skip the submission check for cancelled shipments;
5765 
5766     --Increment the p_sequence with number of errors reported in last query
5767     p_sequence := p_sequence + SQL%ROWCOUNT;
5768 
5769     -------------------------------------------------------------------------------------
5770     l_progress := '027';
5771     IF g_debug_stmt THEN
5772     	IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5773         	FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
5774          		       || l_progress,'Rel 26: LCM enabled release distribution should have destination type as Inventory');
5775    	END IF;
5776     END IF;
5777     -- Check 26: Validation for LCM enabled release to check whether its distribution has destination type as 'Inventory'
5778     l_textline := FND_MESSAGE.GET_STRING('PO','PO_SUB_REL_DIST_DEST_TYPE_NE_I');
5779     INSERT INTO po_online_report_text_gt (online_report_id,
5780 	 				  last_update_login,
5781 	 				  last_updated_by,
5782 	 				  last_update_date,
5783 	 				  created_by,
5784 	 				  creation_date,
5785 	 				  line_num,
5786 	 				  shipment_num,
5787 	 				  distribution_num,
5788 	 				  sequence,
5789 	 				  text_line,
5790 	                                  message_name)
5791     SELECT p_online_report_id,
5792 	   p_login_id,
5793 	   p_user_id,
5794 	   sysdate,
5795 	   p_user_id,
5796 	   sysdate,
5797 	   0,
5798 	   PLL.shipment_num,
5799 	   POD.distribution_num,
5800 	   p_sequence + ROWNUM,
5801 	   substr (g_shipmsg||g_delim||PLL.shipment_num||g_delim||g_distmsg||g_delim||
5802                    POD.distribution_num||g_delim||l_textline, 1,240),
5803 	   'PO_SUB_REL_DIST_DEST_TYPE_NE_I'
5804       FROM PO_RELEASES_GT POR,
5805            PO_LINE_LOCATIONS_GT PLL,
5806            PO_DISTRIBUTIONS_GT POD
5807      WHERE POR.po_release_id = POD.po_release_id
5808        AND POD.line_location_id = PLL.line_location_id
5809        AND POR.po_release_id = p_document_id
5810        AND Nvl(POD.LCM_FLAG,'N') = 'Y'
5811        AND POD.DESTINATION_TYPE_CODE <> 'INVENTORY';
5812 
5813     --Increment the p_sequence with number of errors reported in last query
5814     p_sequence := p_sequence + SQL%ROWCOUNT;
5815     ----------------------------------------------------------------------------------------
5816 --<Bug 9040655 START Buyer should not be inactive on the document>
5817 IF g_debug_stmt THEN
5818    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5819      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
5820           || l_progress,'Rel 27: Cannot approve documents with invalid buyer');
5821    END IF;
5822 END IF;
5823     -- Check 27: Buyer on the release should not be inactive
5824         -- PO_BUYER_INACTIVE
5825 
5826   l_textline := substr(FND_MESSAGE.GET_STRING('PO', 'PO_BUYER_INACTIVE'),1,240);
5827   INSERT INTO po_online_report_text_gt(online_report_id,
5828         last_update_login,
5829         last_updated_by,
5830         last_update_date,
5831         created_by,
5832         creation_date,
5833         line_num,
5834         shipment_num,
5835         distribution_num,
5836         sequence,
5837         text_line,
5838                                 message_name)
5839   SELECT  p_online_report_id,
5840         p_login_id,
5841         p_user_id,
5842                   sysdate,
5843         p_user_id,
5844         sysdate,
5845         0,
5846         0,
5847         0,
5848         p_sequence + ROWNUM,
5849         l_textline,
5850                    'PO_BUYER_INACTIVE'
5851     FROM  dual
5852     where not exists (select 'inactive buyer'
5853                       from PO_RELEASES_GT POR,
5854                            PO_BUYERS_V POB -- <Bug 11686275> Replace PO_BUYERS_VAL_V with PO_BUYERS_V
5855 		      where por.agent_id = pob.employee_id);
5856 
5857 
5858 
5859     --Increment the p_sequence with number of errors reported in last query
5860     p_sequence := p_sequence + SQL%ROWCOUNT;
5861  --<Bug 9040655 END>
5862 
5863     x_return_status := FND_API.G_RET_STS_SUCCESS;
5864 EXCEPTION
5865     WHEN FND_API.G_EXC_ERROR THEN
5866         x_return_status := FND_API.G_RET_STS_ERROR;
5867 
5868     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5869         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5870 
5871     WHEN OTHERS THEN
5872 
5873         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5874         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
5875             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
5876         END IF;
5877 
5878         IF (g_debug_unexp) THEN
5879                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
5880                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
5881                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
5882                        || l_progress || ' SQL CODE is '||sqlcode);
5883                 END IF;
5884         END IF;
5885 END CHECK_RELEASES;
5886 
5887 --For RELEASES, PO
5888 /**
5889 * Private Procedure: CHECK_PO_REL_REQPRICE
5890 * Requires:
5891 *   IN PARAMETERS:
5892 *       p_document_id:      The requisition_header_id of submitted document
5893 *       p_online_report_id: Id used to INSERT INTO online_report_text table
5894 *       p_user_id:          User performing the action
5895 *       p_login_id:         Last update login_id
5896 *   IN OUT PARAMETERS
5897 *       p_sequence:          Sequence number of last reported error
5898 * Modifies: Inserts error msgs in online_report_text_gt table, uses
5899 *           global_temp tables for processing
5900 * Effects:  This procedure runs the document submission checks for PO and
5901 *           RELEASES. This procedure compares the price of the PO or Release
5902 *           Shipment to the price of the Requisition Line. The Shipment
5903 *           Price should be within the tolerance of the Requisition Line
5904 * Returns:
5905 *  p_sequence: This parameter contains the current count of number of error
5906 *              messages inserted
5907 */
5908 PROCEDURE check_po_rel_reqprice(p_document_type IN VARCHAR2,
5909                        p_document_id IN NUMBER,
5910                        p_online_report_id IN NUMBER,
5911                        p_user_id IN NUMBER,
5912                        p_login_id IN NUMBER,
5913                        p_sequence IN OUT NOCOPY NUMBER,
5914                        x_return_status OUT NOCOPY VARCHAR2) IS
5915 
5916 l_textline  po_online_report_text.text_line%TYPE := NULL;
5917 l_api_name  CONSTANT varchar2(40) := 'CHECK_PO_REL_REQPRICE';
5918 l_progress VARCHAR2(3);
5919 
5920 l_enforce_price_tolerance po_system_parameters.enforce_price_change_allowance%TYPE;
5921 l_enforce_price_amount  po_system_parameters.enforce_price_change_amount%TYPE;
5922 l_amount_tolerance po_system_parameters.price_change_amount%TYPE;
5923 
5924 TYPE unit_of_measure IS TABLE of PO_LINES.unit_meas_lookup_code%TYPE;
5925 TYPE NumTab IS TABLE of NUMBER;
5926 l_ship_price_in_base_curr NumTab;
5927 l_ship_unit_of_measure unit_of_measure;
5928 l_ship_num NumTab;
5929 l_line_num NumTab;
5930 l_quantity NumTab;
5931 l_item_id NumTab;
5932 l_line_location_id NumTab;
5933 
5934 --For Req Cursor
5935 l_req_unit_of_measure unit_of_measure;
5936 l_req_line_unit_price NumTab;
5937 l_po_req_line_num NumTab;
5938 l_po_req_ship_num NumTab;
5939 l_po_req_quantity NumTab;
5940 
5941 l_ship_price_ext_precn NUMBER;
5942 l_shipment_to_req_rate NUMBER := 0;
5943 l_price_tolerance_allowed NUMBER := 0;
5944 
5945 --<Bug 3266272 mbhargav START>
5946 l_pou_func_curr                FND_CURRENCIES.currency_code%TYPE;
5947 l_pou_func_curr_ext_precn      FND_CURRENCIES.extended_precision%TYPE;
5948 l_po_curr                      FND_CURRENCIES.currency_code%TYPE;
5949 l_req_ou_func_curr             FND_CURRENCIES.currency_code%TYPE;
5950 l_rate_date                    DATE;
5951 l_rate                         NUMBER;
5952 l_rate_type                    PO_SYSTEM_PARAMETERS_ALL.default_rate_type%TYPE;
5953 l_requesting_org_id            NumTab;
5954 l_display_rate                 NUMBER;
5955 l_return_status                VARCHAR2(1);
5956 l_error_message_name           FND_NEW_MESSAGES.message_name%TYPE;
5957 l_req_line_price_pou_base_curr PO_REQUISITION_LINES_ALL.unit_price%TYPE;
5958 l_req_line_price_ext_precn     PO_REQUISITION_LINES_ALL.unit_price%TYPE;
5959 --<Bug 3266272 mbhargav END>
5960 
5961 l_is_complex_po BOOLEAN := FALSE; --<Complex Work R12>
5962 -- Start of Bug# 13857241
5963 TYPE CurrencyTab IS TABLE of   FND_CURRENCIES.CURRENCY_CODE%TYPE;
5964 l_is_same_foreign_currency     VARCHAR2(1);
5965 l_req_line_curr_unit_price     NumTab;
5966 l_req_line_curr_code           CurrencyTab;
5967 l_po_line_type		       PO_LINES_ALL.ORDER_TYPE_LOOKUP_CODE%TYPE;
5968 -- End of Bug# 13857241
5969 /*
5970 ** Setup the PO select cursor
5971 ** Select shipment price and convert it to base currency.
5972 ** this is done by taking the distribution rate and applying
5973 ** it evenly over all distributions.  Additionally get the
5974 ** shipment unit of measure, quantity, and item_id to be
5975 ** passed to the UomC function.
5976 */
5977 
5978 /*Bug4302950 :The shipments were updated with wrong price when supplier submits
5979 change request from ISP to split shipment quantity between two individual shipments.
5980 As the ISP doesnot handle change in distributions,replacing the shipments
5981 quantity with the sum of the distributions quantity in the calculation
5982 of price override so that the shipment price will reflect the correct value.*/
5983 
5984 CURSOR po_shipment_cursor (p_document_id NUMBER) IS
5985     SELECT nvl(max(POLL.price_override ) *
5986         sum(decode(plt.order_type_lookup_code,'AMOUNT',1,nvl(POD.rate,1))*
5987                   (POD.quantity_ordered -
5988                    nvl(POD.quantity_cancelled, 0))) /
5989             /*	 (max(POLL.quantity) -
5990                  nvl(max(POLL.quantity_cancelled),0)), -1) Price, */ --Bug4302950
5991                   --Bug16222308 Handling the quantity zero on distribution
5992                 decode(
5993                                   (sum(POD.quantity_ordered -
5994                   nvl(POD.quantity_cancelled,0))),0,1,(sum(POD.quantity_ordered -
5995                   nvl(POD.quantity_cancelled,0))) )
5996                                   , -1) Price,
5997         POL.unit_meas_lookup_code uom,
5998         nvl(POLL.shipment_num,0) ship_num,
5999         nvl(POL.line_num,0) line_num,
6000         nvl(POLL.quantity,0) quantity,
6001         nvl(POL.item_id,-1) item_id,
6002         nvl( POLL.line_location_id,0) line_loc_id
6003     FROM   PO_LINE_LOCATIONS_GT POLL,
6004         PO_LINE_TYPES_B PLT,            -- bug3413891
6005         PO_LINES_GT POL,
6006         PO_DISTRIBUTIONS_GT POD
6007      WHERE  POLL.po_line_id    = POL.po_line_id
6008      AND    POLL.line_location_id = POD.line_location_id
6009      AND    POLL.po_header_id = p_document_id
6010      AND    POL.line_type_id = PLT.line_type_id
6011      AND    nvl(POLL.cancel_flag,'N') <> 'Y'
6012      AND    nvl(POLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
6013      AND    POLL.shipment_type in ('PLANNED', 'STANDARD')
6014      GROUP BY POL.unit_meas_lookup_code, nvl(POLL.shipment_num,0),
6015               nvl(POL.line_num,0), nvl(POLL.quantity,0),
6016               nvl(POL.item_id,-1), POLL.price_override,
6017               nvl(POLL.line_location_id,0);
6018 
6019 
6020 -- <Complex Work R12 START>
6021 /* Setup the Complex Work PO select cursor
6022 ** Select Complex Work PO Line information and the
6023 ** line loc ID for the first STANDARD pay item for
6024 ** that line
6025 */
6026 CURSOR po_pay_item_cursor (p_document_id NUMBER) IS
6027     SELECT (POL.unit_price
6028               *  (sum(POD.rate
6029                    * (POD.quantity_ordered - nvl(POD.quantity_cancelled,0)) ))
6030 			--<Bug 15871378> : Avoid divide by zero error
6031               / Decode(POLL.quantity, 0, 1) - nvl(POLL.quantity_cancelled, 0)
6032             ) price,
6033         POL.unit_meas_lookup_code uom,
6034         POLL.shipment_num ship_num,
6035         POL.line_num line_num,
6036         POL.quantity quantity,
6037         nvl(POL.item_id,-1) item_id,
6038         POLL.line_location_id line_loc_id
6039     FROM   PO_LINE_LOCATIONS_GT POLL,
6040         PO_LINES_GT POL,
6041         PO_DISTRIBUTIONS_GT POD
6042     WHERE POL.po_header_id = p_document_id
6043     AND   POD.line_location_id = POLL.line_location_id
6044     AND   POLL.line_location_id =
6045            (SELECT min(POLL2.line_location_id)
6046             FROM PO_LINE_LOCATIONS_GT POLL2
6047             WHERE POLL2.po_line_id = POL.po_line_id
6048             AND POLL2.shipment_type = 'STANDARD'
6049            )
6050     GROUP BY POL.unit_price, POLL.quantity, nvl(POLL.quantity_cancelled, 0),
6051              POL.unit_meas_lookup_code,
6052              POLL.shipment_num,
6053              POL.line_num,
6054              POL.quantity,
6055              nvl(POL.item_id,-1),
6056              POLL.line_location_id;
6057 -- <Complex Work R12 END>
6058 
6059 
6060 /*
6061 ** Setup the Release select cursor
6062 ** Select shipment price and convert it to base currency.
6063 ** this is done by taking the distribution rate and applying
6064 ** it evenly over all distributions.  Additionally get the
6065 ** shipment unit of measure, quantity, and item_id to be
6066 ** passed to the UomC function.  Get the shipment_num and
6067 ** line_num to be passed to the pooinsingle function.
6068 */
6069 CURSOR rel_shipment_cursor (p_document_id NUMBER) IS
6070     SELECT /*+ FULL(POLL) */                          -- bug3413891
6071         nvl(max(POLL.price_override) *
6072         sum(decode(plt.order_type_lookup_code,'AMOUNT',1,nvl(POD.rate,1))*
6073                   (POD.quantity_ordered -
6074                    nvl(POD.quantity_cancelled, 0))) /
6075             /*	 (max(POLL.quantity) -
6076 		 nvl(max(POLL.quantity_cancelled),0)), -1) Price, */ --Bug4302950
6077                  --Bug16222308 Handling the quantity zero on distribution
6078                 decode( (sum(POD.quantity_ordered -
6079                  nvl(POD.quantity_cancelled,0))),0,1,(sum(POD.quantity_ordered -
6080                  nvl(POD.quantity_cancelled,0))) )
6081                                  , -1) Price,
6082         POL.unit_meas_lookup_code uom,
6083         nvl(POLL.shipment_num,0) ship_num,
6084         nvl(POL.line_num,0) line_num,
6085         nvl(POLL.quantity,0) quantity,
6086         nvl(POL.item_id,0) item_id,
6087         nvl( POLL.line_location_id,0) line_loc_id
6088     FROM   PO_LINE_LOCATIONS_GT POLL,
6089         PO_LINE_TYPES_B PLT,                          -- bug3413891
6090         PO_LINES POL,
6091         PO_DISTRIBUTIONS_GT POD                       -- <PO_CHANGE_API FPJ>
6092     WHERE  POLL.po_line_id    = POL.po_line_id
6093      AND    POLL.line_location_id = POD.line_location_id
6094      AND    POLL.po_release_id = p_document_id
6095      AND    POL.line_type_id = PLT.line_type_id
6096      AND    nvl(POLL.cancel_flag,'N') <> 'Y'
6097      AND    nvl(POLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
6098     GROUP BY POL.unit_meas_lookup_code, nvl(POLL.shipment_num,0),
6099               nvl(POL.line_num,0), nvl(POLL.quantity,0),
6100               nvl(POL.item_id,0), POLL.price_override,
6101               nvl(POLL.line_location_id,0);
6102 
6103  CURSOR req_price_tol_cursor(p_line_location_id  NUMBER) IS
6104          SELECT min(PRL.unit_price),
6105                 PRL.unit_meas_lookup_code,
6106                 min(POL.line_num),
6107                 min(POLL.shipment_num),
6108                 min(PRL.org_id)               --<Bug 3266272>
6109          FROM   PO_REQUISITION_LINES_ALL PRL, --<Bug 3266272>
6110                 PO_LINE_LOCATIONS_GT POLL,
6111                 PO_LINES          POL
6112          WHERE  PRL.line_location_id  = POLL.line_location_id
6113          AND    POLL.line_location_id = p_line_location_id
6114          AND    PRL.unit_price        >= 0
6115          AND    POLL.po_line_id       = POL.po_line_id
6116          GROUP BY PRL.unit_meas_LOOKUP_code;
6117 
6118 -- Start of Bug# 1385724
6119 CURSOR req_price_tol_cursor_req_cur(p_line_location_id NUMBER)
6120   IS
6121           SELECT   MIN(PRL.currency_unit_price),
6122                    MIN(PRL.currency_code),
6123                    PRL.unit_meas_lookup_code,
6124                    MIN(POL.line_num)        ,
6125                    MIN(POLL.shipment_num)   ,
6126                    MIN(PRL.org_id)               --<Bug 3266272>
6127           FROM     PO_REQUISITION_LINES_ALL PRL, --<Bug 3266272>
6128                    PO_LINE_LOCATIONS_GT POLL   ,
6129                    PO_LINES POL
6130           WHERE    PRL.line_location_id  = POLL.line_location_id
6131                AND POLL.line_location_id = p_line_location_id
6132                AND PRL.unit_price       >= 0
6133                AND POLL.po_line_id       = POL.po_line_id
6134           GROUP BY PRL.unit_meas_LOOKUP_code;
6135 -- End of Bug# 13857241
6136 
6137 CURSOR req_price_amt_cursor(p_line_location_id  NUMBER) IS
6138          SELECT min(PRL.unit_price),
6139                  PRL.unit_meas_lookup_code,
6140                  sum(PD.quantity_ordered),
6141                  min(POL.line_num),
6142                  min(POLL.shipment_num),
6143                  min(PRL.org_id)              --<Bug 3266272>
6144          FROM   PO_REQUISITION_LINES_ALL PRL, --<Bug 3266272>
6145                  PO_LINE_LOCATIONS_GT POLL,
6146                  PO_LINES          POL,
6147                  PO_DISTRIBUTIONS  PD,
6148                  PO_REQ_DISTRIBUTIONS_ALL PRD --<Bug 3266272>
6149          WHERE  POLL.line_location_id = p_line_location_id
6150           AND    POLL.po_line_id = POL.po_line_id
6151           AND    PRL.unit_price >= 0
6152           AND    POLL.line_location_id = PD.line_location_id
6153           AND    PD.req_distribution_id = PRD.distribution_id
6154           AND    PRD.requisition_line_id = PRL.requisition_line_id
6155          GROUP BY PRL.requisition_line_id, PRL.unit_meas_lookup_code;
6156 
6157 -- Start of Bug# 1385724
6158 CURSOR req_price_amt_cursor_req_curr(p_line_location_id  NUMBER) IS
6159          SELECT  min(PRL.currency_unit_price),
6160                  min(PRL.currency_code),
6161                  PRL.unit_meas_lookup_code,
6162                  sum(PD.quantity_ordered),
6163                  min(POL.line_num),
6164                  min(POLL.shipment_num),
6165                  min(PRL.org_id)              --<Bug 3266272>
6166          FROM   PO_REQUISITION_LINES_ALL PRL, --<Bug 3266272>
6167                  PO_LINE_LOCATIONS_GT POLL,
6168                  PO_LINES          POL,
6169                  PO_DISTRIBUTIONS  PD,
6170                  PO_REQ_DISTRIBUTIONS_ALL PRD --<Bug 3266272>
6171          WHERE  POLL.line_location_id = p_line_location_id
6172           AND    POLL.po_line_id = POL.po_line_id
6173           AND    PRL.unit_price >= 0
6174           AND    POLL.line_location_id = PD.line_location_id
6175           AND    PD.req_distribution_id = PRD.distribution_id
6176           AND    PRD.requisition_line_id = PRL.requisition_line_id
6177          GROUP BY PRL.requisition_line_id, PRL.unit_meas_lookup_code;
6178 -- End of Bug# 13857241
6179 
6180 BEGIN
6181 l_progress := '000';
6182 IF g_debug_stmt THEN
6183    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6184      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name||'.'
6185           || l_progress,'PO REQ: Price, Amount Toleance check');
6186    END IF;
6187 END IF;
6188 
6189     --check if this check is enforced
6190     SELECT nvl(enforce_price_change_allowance, 'N'),
6191                     nvl(enforce_price_change_amount, 'N'),
6192                     nvl(price_change_amount, -1)
6193     INTO   l_enforce_price_tolerance,
6194            l_enforce_price_amount,
6195            l_amount_tolerance
6196     FROM   po_system_parameters;
6197 
6198 l_progress := '001';
6199 IF g_debug_stmt THEN
6200    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6201      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name||'.'
6202        || l_progress,'Is price tol check enforced '||l_enforce_price_tolerance
6203        || ' Is price amount check enforced ' || l_enforce_price_amount);
6204    END IF;
6205 END IF;
6206 
6207     --if we are not enforcing the price tolerance checks then return success
6208     IF  l_enforce_price_tolerance = 'N' AND l_enforce_price_amount = 'N' THEN
6209         RETURN;
6210     END IF;
6211 
6212      /*Depending on the document type execute a different sql
6213           statement to fetch all of the line_location_ids, prices, and
6214           uom_code for a particular document_id and document type.
6215         Use above information to determine if the po shipment
6216           price is within the requisition price + tolerance.
6217     */
6218     IF p_document_type = 'PO' THEN
6219    l_progress := '002';
6220    IF g_debug_stmt THEN
6221      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6222        FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name||'.'
6223        || l_progress,'p_document_type '||p_document_type);
6224      END IF;
6225    END IF;
6226   --<Complex Work R12>: use a different cursor for Complex Work
6227         l_is_complex_po := PO_COMPLEX_WORK_PVT.is_complex_work_po(p_document_id);
6228 
6229         IF (l_is_complex_po) THEN
6230           --The PO is a Complex Work PO
6231           --Use the Pay Item cursor
6232 
6233           OPEN po_pay_item_cursor(p_document_id);
6234 
6235           FETCH po_pay_item_cursor BULK COLLECT INTO
6236                 l_ship_price_in_base_curr,
6237                 l_ship_unit_of_measure,
6238                 l_ship_num,
6239                 l_line_num,
6240                 l_quantity,
6241                 l_item_id,
6242                 l_line_location_id;
6243 
6244           CLOSE po_pay_item_cursor;
6245 
6246         ELSE
6247           --The PO is not Complex Work.
6248           --Use the Shipment cursor
6249 
6250           OPEN po_shipment_cursor(p_document_id);
6251 
6252           FETCH po_shipment_cursor BULK COLLECT INTO
6253                 l_ship_price_in_base_curr,
6254                 l_ship_unit_of_measure,
6255                 l_ship_num,
6256                 l_line_num,
6257                 l_quantity,
6258                 l_item_id,
6259                 l_line_location_id;
6260 
6261           CLOSE po_shipment_cursor;
6262 
6263         END IF; -- l_is_complex_po check
6264 
6265     ELSIF p_document_type = 'RELEASE' THEN
6266     l_progress := '003';
6267       IF g_debug_stmt THEN
6268         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6269           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name||'.'
6270           || l_progress,'p_document_type '||p_document_type);
6271         END IF;
6272       END IF;
6273         OPEN rel_shipment_cursor(p_document_id);
6274 
6275         FETCH rel_shipment_cursor BULK COLLECT INTO
6276                 l_ship_price_in_base_curr,
6277                 l_ship_unit_of_measure,
6278                 l_ship_num,
6279                 l_line_num,
6280                 l_quantity,
6281                 l_item_id,
6282                 l_line_location_id;
6283 
6284         CLOSE rel_shipment_cursor;
6285     END IF;
6286 
6287     --<Bug 3266272 mbhargav START>
6288     --Bug 1991546
6289     --Obtain extended precision of PO/Release functional currency which is
6290     --used for rounding while checking for tolerance
6291       BEGIN
6292         SELECT  FND.currency_code, nvl(FND.extended_precision,5)
6293         INTO  l_pou_func_curr, l_pou_func_curr_ext_precn
6294         FROM  fnd_currencies FND, financials_system_parameters FSP,
6295               gl_sets_of_books SOB
6296         WHERE  FSP.set_of_books_id = SOB.set_of_books_id
6297          AND  SOB.currency_code = FND.currency_code;
6298       EXCEPTION
6299         WHEN OTHERS THEN
6300             RAISE;
6301       END;
6302       --<Bug 3266272 mbhargav END>
6303 
6304 
6305 l_progress := '004';
6306     FOR shipment_line IN 1..l_line_location_id.COUNT LOOP
6307 
6308        --<Bug 3266272 mbhargav START>
6309        --Round off the shipment price (in functional currency of Purchasing
6310        --Operating Unit) to the extended precision of the functional currency
6311        l_ship_price_ext_precn :=
6312           round(l_ship_price_in_base_curr(shipment_line),l_pou_func_curr_ext_precn);
6313            l_progress := '005';
6314            IF g_debug_stmt THEN
6315              FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'||
6316                             l_progress,'l_ship_price_in_base_curr ::' || l_ship_price_in_base_curr(shipment_line));
6317              FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'||
6318                             l_progress,'l_ship_price_ext_precn ::' || l_ship_price_ext_precn);
6319            END IF;
6320 
6321       --Obtain the currency and rate_date from PO. If a rate_date exists on PO
6322       --Header then it means PO Currency is different from POU functional
6323       --currency. In this case take the rate_date on PO Header as rate date.
6324       --If rate_date on PO Header is NULL then it means PO Currency is same as
6325       --POU functional currency. Use the Shipment creation date as rate_date for
6326       --such cases. This l_rate_date will be used to get rate between POU
6327       --functional currency and ROU functional currency for currency conversions
6328       BEGIN
6329         l_progress := '006';
6330         SELECT POH.currency_code, nvl(trunc(POH.rate_date), trunc(POLL.creation_date))
6331         INTO l_po_curr, l_rate_date
6332         FROM PO_HEADERS POH, PO_LINE_LOCATIONS_GT POLL
6333         WHERE POLL.line_location_id = l_line_location_id(shipment_line)
6334          AND POLL.po_header_id = POH.po_header_id;
6335       EXCEPTION
6336         WHEN OTHERS THEN
6337              RAISE;
6338       END;
6339      -- Start of Bug# 13857241
6340 	 --Bug 16024095 , using the GT tables instead of the base tables as in case of mods the base tables will nto have the data
6341         l_progress := '007';
6342         BEGIN
6343 			SELECT POL.ORDER_TYPE_LOOKUP_CODE
6344 			  INTO l_po_line_type
6345 			  FROM PO_LINE_LOCATIONS_GT POLL,
6346 				   PO_LINES_GT POL
6347 			 WHERE POLL.po_line_id = POL.po_line_id
6348 			   AND POLL.line_location_id = l_line_location_id(shipment_line);
6349 		EXCEPTION
6350 		WHEN no_data_found THEN
6351 			l_po_line_type := null;
6352 		END;
6353 
6354       --<Bug 3266272 mbhargav END>
6355         BEGIN
6356            l_progress := '008';
6357            IF g_debug_stmt THEN
6358              FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'||
6359                             l_progress,'LINE_LOCATION_ID ::' || l_line_location_id(shipment_line));
6360              FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'||
6361                             l_progress,'l_po_curr ::' || l_po_curr);
6362              FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'||
6363                             l_progress,'l_pou_func_curr ::' || l_pou_func_curr);
6364              FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'||
6365                             l_progress,'l_po_line_type ::' || l_po_line_type);
6366            END IF;
6367 
6368            /* Bug7461672
6369               Initializing l_is_same_foreign_currency */
6370            l_is_same_foreign_currency := 'N';
6371 
6372            /* Check if there exist any Req Line which have different currency from PO.
6373               If NOT found then setting l_is_same_foreign_currency to Y */
6374            SELECT 'Y'
6375              INTO l_is_same_foreign_currency
6376              FROM DUAL
6377             WHERE NOT EXISTS
6378                     (SELECT DISTINCT 1
6379                        FROM PO_REQUISITION_LINES_ALL PRL,
6380                             FINANCIALS_SYSTEM_PARAMS_ALL FSP,
6381                             GL_SETS_OF_BOOKS SOB
6382                       WHERE PRL.LINE_LOCATION_ID   = l_line_location_id(shipment_line)
6383                         AND FSP.SET_OF_BOOKS_ID  = SOB.SET_OF_BOOKS_ID
6384                         AND FSP.ORG_ID = PRL.ORG_ID
6385                         AND NVL(PRL.CURRENCY_CODE,SOB.CURRENCY_CODE) <> l_po_curr)
6386               AND l_po_curr <> l_pou_func_curr
6387 	      	  AND l_po_line_type = 'QUANTITY';
6388 
6389         EXCEPTION
6390         WHEN NO_DATA_FOUND THEN
6391                 l_is_same_foreign_currency := 'N';
6392         END;
6393 
6394         l_progress := '009';
6395         IF g_debug_stmt THEN
6396 	      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6397             FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'||
6398                            l_progress,'l_is_same_foreign_currency ::' || l_is_same_foreign_currency);
6399           END IF;
6400 	    END IF;
6401 
6402         l_progress := '010';
6403         --Do price tolerance check
6404         --Price Tolerance Check Starts Here
6405         IF l_enforce_price_tolerance = 'Y' THEN
6406 
6407           IF g_debug_stmt THEN
6408             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6409               FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name||'.'
6410                             || l_progress,'Doing Price Tolerance check');
6411             END IF;
6412           END IF;
6413         l_progress := '011';
6414 
6415         --Get the tolerance allowed.  This is the tolerance allowed between
6416         --the requisition price and shipment price.
6417         SELECT NVL(MSI.price_tolerance_percent/100,
6418                    NVL(POSP.price_change_allowance/100,-1))
6419         INTO   l_price_tolerance_allowed
6420         FROM   MTL_SYSTEM_ITEMS MSI,
6421                PO_SYSTEM_PARAMETERS POSP,
6422                FINANCIALS_SYSTEM_PARAMETERS FSP
6423         WHERE  msi.inventory_item_id(+) = l_item_id(shipment_line)
6424         AND  MSI.organization_id(+) = FSP.inventory_organization_id;
6425 
6426         l_progress := '012';
6427         IF g_debug_stmt THEN
6428 	      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6429             FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'||
6430                            l_progress,'l_price_tolerance_allowed ::' || l_price_tolerance_allowed);
6431           END IF;
6432         END IF;
6433         /* This logic will be used for Price Tolerance Check when both
6434            PO and REQ are created in same foreign currency.
6435            Old/existing logic is used in ELSE part of this condition */
6436         IF (l_is_same_foreign_currency = 'Y' )
6437         THEN
6438             OPEN req_price_tol_cursor_req_cur(l_line_location_id(shipment_line));
6439 
6440             FETCH req_price_tol_cursor_req_cur BULK COLLECT INTO
6441                     l_req_line_curr_unit_price,
6442                     l_req_line_curr_code,
6443                     l_req_unit_of_measure,
6444                     l_po_req_line_num,
6445                     l_po_req_ship_num,
6446                     l_requesting_org_id;
6447 
6448             CLOSE req_price_tol_cursor_req_cur;
6449 
6450             l_progress := '013';
6451             FOR req_line IN 1..l_req_line_curr_unit_price.COUNT LOOP
6452 
6453                 l_progress := '014';
6454                 IF g_debug_stmt THEN
6455                     FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'||
6456                                    l_progress,'Processing Req Line ::' || req_line);
6457                 END IF;
6458 
6459                 l_req_line_price_ext_precn := round(l_req_line_curr_unit_price(req_line),l_pou_func_curr_ext_precn);
6460 
6461                 l_progress := '015';
6462                 po_uom_s.po_uom_conversion(
6463                     l_ship_unit_of_measure(shipment_line),
6464                     l_req_unit_of_measure(req_line),
6465                     l_item_id(shipment_line),
6466                     l_shipment_to_req_rate);
6467 
6468                 IF g_debug_stmt THEN
6469                     FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'||
6470                                    l_progress,'l_shipment_to_req_rate ::' || l_shipment_to_req_rate);
6471                 END IF;
6472 
6473 
6474                 IF l_shipment_to_req_rate = 0.0 THEN
6475                     l_progress := '016';
6476                     l_shipment_to_req_rate :=1.0;
6477                 END IF;
6478 
6479 
6480                 IF g_debug_stmt THEN
6481                     FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'||
6482                                    l_progress,'l_req_line_curr_unit_price ::' || l_req_line_curr_unit_price(req_line));
6483                 END IF;
6484 
6485                 l_progress := '017';
6486                 IF l_price_tolerance_allowed <> -1 AND l_req_line_curr_unit_price(req_line) <> 0 THEN
6487 
6488                     l_progress := '018';
6489                     --Get Shipment Price without applying Rate and Qty
6490                     SELECT   NVL(ROUND(MAX(POLL.price_override) ,l_pou_func_curr_ext_precn) ,-1) Price
6491                     INTO     l_ship_price_ext_precn
6492                     FROM     PO_LINE_LOCATIONS_ALL POLL
6493                     WHERE    POLL.line_location_id         = l_line_location_id(shipment_line)
6494                          AND NVL(POLL.cancel_flag,'N')    <> 'Y'
6495                          AND NVL(POLL.closed_code,'OPEN') <> 'FINALLY CLOSED';
6496 
6497                     IF g_debug_stmt THEN
6498                         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'|| l_progress,'l_ship_price_ext_precn ::' || l_ship_price_ext_precn);
6499                         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'|| l_progress,'l_req_line_price_ext_precn ::' || l_req_line_price_ext_precn);
6500                         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'|| l_progress,'l_shipment_to_req_rate ::' || l_shipment_to_req_rate);
6501                         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'|| l_progress,'l_price_tolerance_allowed ::' || l_price_tolerance_allowed);
6502                         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'|| l_progress,'l_pou_func_curr_ext_precn ::' || l_pou_func_curr_ext_precn);
6503                     END IF;
6504 
6505                 IF (l_ship_price_ext_precn > ROUND((l_req_line_price_ext_precn * l_shipment_to_req_rate*(1+l_price_tolerance_allowed)),l_pou_func_curr_ext_precn))
6506 				THEN
6507 
6508                             l_progress := '019';
6509                             l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REQ_PRICE_TOL_EXCEED');
6510                             INSERT
6511                             INTO   po_online_report_text_gt
6512                                    (
6513                                           online_report_id ,
6514                                           last_update_login,
6515                                           last_updated_by  ,
6516                                           last_update_date ,
6517                                           created_by       ,
6518                                           creation_date    ,
6519                                           line_num         ,
6520                                           shipment_num     ,
6521                                           distribution_num ,
6522                                           sequence         ,
6523                                           text_line        ,
6524                                           message_name
6525                                    )
6526                                    VALUES
6527                                    (
6528                                           p_online_report_id         ,
6529                                           p_login_id                 ,
6530                                           p_user_id                  ,
6531                                           sysdate                    ,
6532                                           p_user_id                  ,
6533                                           sysdate                    ,
6534                                           l_po_req_line_num(req_line),
6535                                           l_po_req_ship_num(req_line),
6536                                           0                          ,
6537                                           p_sequence +1              ,
6538                                           SUBSTR(g_linemsg
6539                                                  ||g_delim
6540                                                  || l_po_req_line_num(req_line)
6541                                                  ||g_delim
6542                                                  || g_shipmsg
6543                                                  ||g_delim
6544                                                  ||l_po_req_ship_num(req_line)
6545                                                  ||g_delim
6546                                                  ||l_textline,1,240),
6547                                           'PO_SUB_REQ_PRICE_TOL_EXCEED'
6548                                    );
6549 
6550                             p_sequence := p_sequence +1;
6551 
6552                 END IF; --IF (l_ship_price_ext_precn > ROUND
6553 
6554             END IF; --IF l_price_tolerance_allowed <> -1
6555 
6556            END LOOP; --req_price_tol_cursor
6557 
6558         ELSE
6559        -- End of Bug# 13857241
6560             OPEN req_price_tol_cursor(l_line_location_id(shipment_line));
6561 
6562             FETCH req_price_tol_cursor BULK COLLECT INTO
6563                     l_req_line_unit_price,
6564                     l_req_unit_of_measure,
6565                     l_po_req_line_num,
6566                     l_po_req_ship_num,
6567                     l_requesting_org_id; --Bug 3266272
6568 
6569             CLOSE req_price_tol_cursor;
6570 
6571        l_progress := '019';
6572             FOR req_line IN 1..l_req_line_unit_price.COUNT LOOP
6573 
6574           /*
6575            ** If a row was returned then the PO or Release is associated
6576            ** with a requisition and you should continue with the logic.
6577            ** If a row was not returned.  It does not mean that an error
6578            ** occurred, it meas that the submission check does not apply
6579            ** to this document.
6580            */
6581                 --<Bug 3266272 mbhargav START>
6582                 /* Bug 4537974: while comparing org_id we need an NVL condition
6583 		   around both the operands of = since for single org installations
6584 		   the org_id can be null */
6585 
6586                 IF l_req_ou_func_curr IS NULL THEN
6587                   BEGIN
6588                     SELECT  SOB.currency_code
6589                     INTO  l_req_ou_func_curr
6590                     FROM  financials_system_params_all FSP, gl_sets_of_books SOB
6591                     WHERE  FSP.set_of_books_id = SOB.set_of_books_id
6592                       AND  NVL(FSP.org_id, -99) = NVL(l_requesting_org_id(req_line),-99);
6593                   EXCEPTION
6594                     WHEN OTHERS THEN
6595                        RAISE;
6596                   END;
6597                 END IF;
6598 
6599                 IF l_req_ou_func_curr <> l_pou_func_curr THEN
6600 
6601                    --Obtain the conversion rate between two functional currencies
6602                    --using the rate type from POU setup.
6603                    IF l_rate IS NULL THEN
6604                       BEGIN
6605                         SELECT default_rate_type
6606                         INTO   l_rate_type
6607                         FROM   po_system_parameters;
6608                       EXCEPTION
6609                         WHEN OTHERS THEN
6610                           RAISE;
6611                       END;
6612 
6613                       --Get the conversion rate between Purchasing Operating Unit func
6614                       --currency and Req Operating Unit functional currency
6615                       po_currency_sv.get_rate(
6616                               p_from_currency => l_req_ou_func_curr,
6617                               p_to_currency   => l_pou_func_curr,
6618                               p_rate_type     => l_rate_type,
6619                               p_rate_date     => l_rate_date,
6620                               p_inverse_rate_display_flag => 'N',
6621                               x_rate          => l_rate,
6622                               x_display_rate  => l_display_rate,
6623                               x_return_status => l_return_status,
6624                               x_error_message_name => l_error_message_name);
6625                    END IF; --rate check
6626 
6627                    --Convert the Req line price (which is in Req OU func currency)
6628                    --to Purchasing OU functional currency for comparison
6629                    l_req_line_price_pou_base_curr :=
6630                            l_req_line_unit_price(req_line) * nvl(l_rate,1);
6631 
6632 IF g_debug_stmt THEN
6633    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6634      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name||'.'
6635        || l_progress,'POU Func Currency and ROU Func Curr are different'
6636        || ' l_pou_func_curr= '||l_pou_func_curr
6637        || ' l_req_ou_func_curr= ' || l_req_ou_func_curr
6638        || ' l_req_line_unit_price= ' || l_req_line_unit_price(req_line)
6639        || ' l_rate_type= ' ||  l_rate_type
6640        || ' l_rate_date= ' || l_rate_date
6641        || ' l_rate= ' || l_rate
6642        || ' l_req_line_price_pou_base_curr= ' ||l_req_line_price_pou_base_curr);
6643    END IF;
6644 END IF;
6645                    --Round off the Req line price (in functional currency of
6646                    --Purchasing Operating Unit) to the extended precision of
6647                    --the functional currency
6648                    l_req_line_price_ext_precn :=
6649                        round(l_req_line_price_pou_base_curr,l_pou_func_curr_ext_precn);
6650                 ELSE  --POU func curr <> ROU func curr
6651                    l_req_line_price_ext_precn :=
6652                       round(l_req_line_unit_price(req_line),l_pou_func_curr_ext_precn);
6653                 END IF; --func curr check
6654                 --<Bug 3266272 mbhargav END>
6655 
6656                 --Call function that returns the shipment price
6657                 --converted to the correct UOM.
6658                 po_uom_s.po_uom_conversion(
6659                     l_ship_unit_of_measure(shipment_line),
6660                     l_req_unit_of_measure(req_line),
6661                     l_item_id(shipment_line),
6662                     l_shipment_to_req_rate);
6663 
6664                 IF l_shipment_to_req_rate = 0.0 THEN
6665                     l_shipment_to_req_rate :=1.0;
6666                 END IF;
6667 
6668 l_progress := '020';
6669                 IF l_price_tolerance_allowed <> -1 AND
6670                     l_req_line_unit_price(req_line) <> 0 THEN
6671 
6672 IF g_debug_stmt THEN
6673    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6674      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name||'.'
6675        || l_progress,'l_ship_price_ext_precn= '||l_ship_price_ext_precn
6676        || ' l_req_line_price_ext_precn= ' || l_req_line_price_ext_precn
6677        || ' l_shipment_to_req_rate= ' || l_shipment_to_req_rate
6678        || ' l_price_tolerance_allowed= ' ||  l_price_tolerance_allowed);
6679    END IF;
6680 END IF;
6681 
6682                    /*
6683                    **  Check to see if the rate returned from the function
6684                    **  multiplied by the shipment price in base currency and
6685                    **  then divided by the requisition price is less then
6686                    **  the tolerance.  If not, call the function to
6687                    **  insert into the Online Report Text Table.
6688                    **
6689                    ** The following formula will cost precision erro when the
6690                    ** increase equals to the tolerance.
6691                    ** Patched as part of bug 432746.
6692                    **
6693                    **if ((((ship_price_in_base_curr * rate) /
6694                    **   req_line_unit_price[i]) -1) <= tolerance)
6695                    */
6696 
6697                    /* Bug 638073
6698                       the formula for tolerance check should be
6699                       ship_price_in_base_curr/ req_line_unit_pric e[i] *rate
6700                       since rate is the conversion from shipment uom to req uom
6701                     */
6702 
6703                    /*    svaidyan 09/10/98   726568  Modified the price tolerance
6704                       to check against tolerance + 1.000001. This is because,
6705                       the reqs sourced to a blanket store the unit price rounded
6706                       to 5 decimal places and hence we compare only upto the 5th
6707                       decimal place.
6708                     */
6709 
6710                     /* Bug 3262304, 3266272 mbhargav Using the req price to
6711                        the ext_precn of the currency. Also replacing the division
6712                         by multiplication on the other side
6713                        IF (((l_ship_price_ext_precn) /
6714                         (l_req_line_unit_price(req_line) *
6715                             l_shipment_to_req_rate ))
6716                                   > (l_price_tolerance_allowed + 1.000001))
6717                        THEN
6718                    */
6719                    --<Bug 3266272 mbhargav START>
6720                    IF (l_ship_price_ext_precn >
6721                           ( (l_req_line_price_ext_precn * l_shipment_to_req_rate )
6722                               * (l_price_tolerance_allowed + 1.000001)
6723                           )
6724                    ) THEN
6725                    --<Bug 3266272 mbhargav END>
6726 l_progress := '030';
6727                       --Report the price tolerance error
6728                       l_textline := FND_MESSAGE.GET_STRING('PO',
6729                                           'PO_SUB_REQ_PRICE_TOL_EXCEED');
6730                       INSERT into po_online_report_text_gt(
6731                             online_report_id,
6732                         last_update_login,
6733                         last_updated_by,
6734                         last_update_date,
6735                         created_by,
6736                         creation_date,
6737                         line_num,
6738                         shipment_num,
6739                         distribution_num,
6740                         sequence,
6741                         text_line,
6742                                 message_name)
6743                       VALUES ( p_online_report_id,
6744                             p_login_id,
6745                             p_user_id,
6746                             sysdate,
6747                             p_user_id,
6748                             sysdate,
6749                             l_po_req_line_num(req_line),
6750                             l_po_req_ship_num(req_line),
6751                             0,
6752                             p_sequence +1,
6753                             substr(g_linemsg||g_delim||
6754                                 l_po_req_line_num(req_line)||g_delim||
6755                                 g_shipmsg||g_delim||l_po_req_ship_num(req_line)
6756                                 ||g_delim||l_textline,1,240),
6757                             'PO_SUB_REQ_PRICE_TOL_EXCEED');
6758 
6759                       p_sequence := p_sequence +1;
6760 
6761                      END IF; --check for tolerance
6762 
6763                  END IF; --check l_price_tolerance_allowed
6764 
6765              END LOOP; --req line
6766 
6767            END IF;
6768         END IF; --price tolerance check
6769 
6770 l_progress := '040';
6771         --Do price 'not to exceed' amount check
6772         --Amount Tolerance Check Starts Here
6773         IF l_enforce_price_amount = 'Y' THEN
6774 IF g_debug_stmt THEN
6775    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
6776      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name||'.'
6777                       || l_progress,'Amount Tolerance Check');
6778                FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name||'.'
6779           || l_progress,'Doing Not to exceed amt check');
6780    END IF;
6781 END IF;
6782 
6783  -- Start of Bug# 13857241
6784         /* This logic will be used for Amount Tolerance Check when both
6785            PO and REQ are created in same foreign currency.
6786            Old/existing logic is used in ELSE part of this condition */
6787         IF (l_is_same_foreign_currency = 'Y' )
6788         THEN
6789 
6790             /*Introduced new cursor to get Req price in base currency */
6791             OPEN req_price_amt_cursor_req_curr(l_line_location_id(shipment_line));
6792 
6793             FETCH req_price_amt_cursor_req_curr BULK COLLECT INTO
6794                     l_req_line_curr_unit_price,
6795                     l_req_line_curr_code,
6796                     l_req_unit_of_measure,
6797                     l_po_req_quantity,
6798                     l_po_req_line_num,
6799                     l_po_req_ship_num,
6800                     l_requesting_org_id; --Bug 3266272
6801 
6802             CLOSE req_price_amt_cursor_req_curr;
6803 
6804             l_progress := '050';
6805             FOR req_line IN 1..l_req_line_curr_unit_price.COUNT LOOP
6806 
6807                 l_progress := '051';
6808                 IF g_debug_stmt THEN
6809                     FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'||
6810                                    l_progress,'Processing Req Line ::' || req_line);
6811                 END IF;
6812 
6813                 l_req_line_price_ext_precn := round(l_req_line_curr_unit_price(req_line),l_pou_func_curr_ext_precn);
6814 
6815                 --Call function that returns the shipment price
6816                 --converted to the correct UOM.
6817                 l_progress := '053';
6818                 po_uom_s.po_uom_conversion(
6819                     l_ship_unit_of_measure(shipment_line),
6820                     l_req_unit_of_measure(req_line),
6821                     l_item_id(shipment_line),
6822                     l_shipment_to_req_rate);
6823 
6824                 IF g_debug_stmt THEN
6825                     FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'||
6826                                    l_progress,'l_shipment_to_req_rate ::' || l_shipment_to_req_rate);
6827                 END IF;
6828 
6829 
6830                 IF l_shipment_to_req_rate = 0.0 THEN
6831                     l_shipment_to_req_rate :=1.0;
6832                 END IF;
6833 
6834                 l_progress := '054';
6835                 IF g_debug_stmt THEN
6836                     FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'||
6837                                    l_progress,'l_amount_tolerance ::' || l_amount_tolerance);
6838                     FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'||
6839                                    l_progress,'l_req_line_unit_price ::' || l_req_line_curr_unit_price(req_line));
6840                 END IF;
6841 
6842                 l_progress := '055';
6843                 IF l_amount_tolerance >= 0 AND l_req_line_curr_unit_price(req_line) <> 0 THEN
6844 
6845                     l_progress := '207';
6846                     --Get Shipment Price without applying Rate and Qty
6847                     SELECT   NVL(ROUND(MAX(POLL.price_override) ,l_pou_func_curr_ext_precn) ,-1) Price
6848                     INTO     l_ship_price_ext_precn
6849                     FROM     PO_LINE_LOCATIONS_ALL POLL
6850                     WHERE    POLL.line_location_id         = l_line_location_id(shipment_line)
6851                          AND NVL(POLL.cancel_flag,'N')    <> 'Y'
6852                          AND NVL(POLL.closed_code,'OPEN') <> 'FINALLY CLOSED';
6853 
6854                     IF g_debug_stmt THEN
6855                         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'|| l_progress,'l_ship_price_ext_precn ::' || l_ship_price_ext_precn);
6856                         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'|| l_progress,'l_req_line_price_ext_precn ::' || l_req_line_price_ext_precn);
6857                         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'|| l_progress,'l_shipment_to_req_rate ::' || l_shipment_to_req_rate);
6858                         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'|| l_progress,'l_price_tolerance_allowed ::' || l_price_tolerance_allowed);
6859                         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head|| l_api_name||'.'|| l_progress,'l_pou_func_curr_ext_precn ::' || l_pou_func_curr_ext_precn);
6860                     END IF;
6861 
6862                    IF ((l_ship_price_ext_precn -
6863                          (l_req_line_price_ext_precn * l_shipment_to_req_rate)
6864                         ) * l_po_req_quantity(req_line)
6865                                            > l_amount_tolerance
6866                        )
6867                    THEN
6868 
6869                             l_progress := '060';
6870                             l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REQ_AMT_TOL_EXCEED');
6871                             INSERT
6872                             INTO   po_online_report_text_gt
6873                                    (
6874                                           online_report_id ,
6875                                           last_update_login,
6876                                           last_updated_by  ,
6877                                           last_update_date ,
6878                                           created_by       ,
6879                                           creation_date    ,
6880                                           line_num         ,
6881                                           shipment_num     ,
6882                                           distribution_num ,
6883                                           sequence         ,
6884                                           text_line        ,
6885                                           message_name
6886                                    )
6887                                    VALUES
6888                                    (
6889                                           p_online_report_id         ,
6890                                           p_login_id                 ,
6891                                           p_user_id                  ,
6892                                           sysdate                    ,
6893                                           p_user_id                  ,
6894                                           sysdate                    ,
6895                                           l_po_req_line_num(req_line),
6896                                           l_po_req_ship_num(req_line),
6897                                           0                          ,
6898                                           p_sequence +1              ,
6899                                           SUBSTR(g_linemsg
6900                                                  ||g_delim
6901                                                  || l_po_req_line_num(req_line)
6902                                                  ||g_delim
6903                                                  || g_shipmsg
6904                                                  ||g_delim
6905                                                  ||l_po_req_ship_num(req_line)
6906                                                  ||g_delim
6907                                                  ||l_textline,1,240),
6908                                           'PO_SUB_REQ_PRICE_TOL_EXCEED'
6909                                    );
6910 
6911                             p_sequence := p_sequence +1;
6912 
6913                 END IF; --Tolerance Fail Logic
6914 
6915             END IF; --l_amount_tolerance >= 0
6916 
6917            END LOOP; --req_price_amt_cursor_req_curr cursor
6918 
6919         ELSE --l_is_same_foreign_cuurecy logic
6920 -- End of Bug# 13857241
6921             OPEN req_price_amt_cursor(l_line_location_id(shipment_line));
6922 
6923             FETCH req_price_amt_cursor BULK COLLECT INTO
6924                     l_req_line_unit_price,
6925                     l_req_unit_of_measure,
6926                     l_po_req_quantity,
6927                     l_po_req_line_num,
6928                     l_po_req_ship_num,
6929                     l_requesting_org_id; --Bug 3266272
6930 
6931             CLOSE req_price_amt_cursor;
6932 
6933 l_progress := '070';
6934             FOR req_line IN 1..l_req_line_unit_price.COUNT LOOP
6935 
6936           /*
6937            ** If a row was returned then the PO or Release is associated
6938            ** with a requisition and you should continue with the logic.
6939            ** If a row was not returned.  It does not mean that an error
6940            ** occurred, it meas that the submission check does not apply
6941            ** to this document.
6942            */
6943 
6944                 --<Bug 3266272 mbhargav START>
6945                 /* Bug 4537974: while comparing org_id we need an NVL condition
6946 		   around both the operands of = since for single org installations
6947 		   the org_id can be null */
6948                 IF l_req_ou_func_curr IS NULL THEN
6949                   BEGIN
6950                     SELECT  SOB.currency_code
6951                     INTO  l_req_ou_func_curr
6952                     FROM  financials_system_params_all FSP, gl_sets_of_books SOB
6953                     WHERE  FSP.set_of_books_id = SOB.set_of_books_id
6954                       AND  NVL(FSP.org_id, -99) = NVL(l_requesting_org_id(req_line),-99);
6955                   EXCEPTION
6956                     WHEN OTHERS THEN
6957                        RAISE;
6958                   END;
6959                 END IF;
6960 
6961                 IF l_req_ou_func_curr <> l_pou_func_curr THEN
6962 
6963                    --Obtain the conversion rate between two functional currencies
6964                    --using the rate type from POU setup.
6965                    IF l_rate IS NULL THEN
6966                       BEGIN
6967                         SELECT default_rate_type
6968                         INTO   l_rate_type
6969                         FROM   po_system_parameters;
6970                       EXCEPTION
6971                         WHEN OTHERS THEN
6972                           RAISE;
6973                       END;
6974 
6975                       --Get the conversion rate between Purchasing Operating Unit func
6976                       --currency and Req Operating Unit functional currency
6977                       po_currency_sv.get_rate(
6978                               p_from_currency => l_req_ou_func_curr,
6979                               p_to_currency   => l_pou_func_curr,
6980                               p_rate_type     => l_rate_type,
6981                               p_rate_date     => l_rate_date,
6982                               p_inverse_rate_display_flag => 'N',
6983                               x_rate          => l_rate,
6984                               x_display_rate  => l_display_rate,
6985                               x_return_status => l_return_status,
6986                               x_error_message_name => l_error_message_name);
6987                    END IF; --rate check
6988 
6989                    --Convert the Req line price (which is in Req OU func currency)
6990                    --to Purchasing OU functional currency for comparison
6991                    l_req_line_price_pou_base_curr :=
6992                            l_req_line_unit_price(req_line) * nvl(l_rate,1);
6993 
6994                    --Round off the Req line price (in functional currency of
6995                    --Purchasing Operating Unit) to the extended precision of
6996                    --the functional currency
6997                    l_req_line_price_ext_precn :=
6998                        round(l_req_line_price_pou_base_curr,l_pou_func_curr_ext_precn);
6999                 ELSE  --POU func curr <> ROU func curr
7000                    l_req_line_price_ext_precn :=
7001                       round(l_req_line_unit_price(req_line),l_pou_func_curr_ext_precn);
7002                 END IF; --func curr check
7003                 --<Bug 3266272 mbhargav END>
7004 
7005                 --Call function that returns the shipment price
7006                 --converted to the correct UOM.
7007                 po_uom_s.po_uom_conversion(
7008                     l_ship_unit_of_measure(shipment_line),
7009                     l_req_unit_of_measure(req_line),
7010                     l_item_id(shipment_line),
7011                     l_shipment_to_req_rate);
7012 
7013                 IF l_shipment_to_req_rate = 0.0 THEN
7014                     l_shipment_to_req_rate :=1.0;
7015                 END IF;
7016 
7017 
7018                 IF l_amount_tolerance >= 0 AND
7019                     l_req_line_unit_price(req_line) <> 0 THEN
7020 
7021 IF g_debug_stmt THEN
7022    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
7023      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name||'.'
7024        || l_progress,'l_ship_price_ext_precn= '||l_ship_price_ext_precn
7025        || ' l_req_line_price_ext_precn= ' || l_req_line_price_ext_precn
7026        || ' l_shipment_to_req_rate= ' || l_shipment_to_req_rate
7027        || ' l_po_req_quantity= ' ||  l_po_req_quantity(req_line)
7028        || ' l_amount_tolerance= ' || l_amount_tolerance);
7029    END IF;
7030 END IF;
7031                    --do the amount check
7032                    --makes sure the requisition amount and
7033                    --PO amount for each shipment line is within the value
7034                    --defined in the column PRICE_CHANGE_AMOUNT of table
7035                    --PO_SYSTEM_PARAMETERS.
7036                     --Bug 3262304, 3266272 mbhargav Using the Req price which is
7037                     --rounded to the ext_precn of the its currency.
7038                    IF ((l_ship_price_ext_precn -
7039                          (l_req_line_price_ext_precn * l_shipment_to_req_rate)
7040                         ) * l_po_req_quantity(req_line)
7041                                            > l_amount_tolerance
7042                        )
7043                    THEN
7044 l_progress := '080';
7045                       --Report the price amount exceeded error
7046                       l_textline :=
7047                    FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REQ_AMT_TOL_EXCEED');
7048                       INSERT into po_online_report_text_gt(
7049                             online_report_id,
7050                         last_update_login,
7051                         last_updated_by,
7052                         last_update_date,
7053                         created_by,
7054                         creation_date,
7055                         line_num,
7056                         shipment_num,
7057                         distribution_num,
7058                         sequence,
7059                         text_line,
7060                                 message_name)
7061                       VALUES ( p_online_report_id,
7062                             p_login_id,
7063                             p_user_id,
7064                             sysdate,
7065                             p_user_id,
7066                             sysdate,
7067                             l_po_req_line_num(req_line),
7068                             l_po_req_ship_num(req_line),
7069                             0,
7070                             p_sequence +1,
7071                             substr(g_linemsg||g_delim||
7072                                 l_po_req_line_num(req_line)||g_delim||
7073                                 g_shipmsg||g_delim||l_po_req_ship_num(req_line)
7074                                 ||g_delim||l_textline,1,240),
7075                             'PO_SUB_REQ_AMT_TOL_EXCEED');
7076 
7077                       p_sequence := p_sequence +1;
7078 
7079                      END IF; --amount check
7080 
7081                  END IF; --check l_amount_tolerance_allowed
7082 
7083              END LOOP; --req line
7084 
7085           END IF;
7086 
7087         END IF; --not to exceed amount check
7088 
7089    END LOOP;  --for shipment_line
7090 
7091 l_progress := '090';
7092     x_return_status := FND_API.G_RET_STS_SUCCESS;
7093 EXCEPTION
7094     WHEN FND_API.G_EXC_ERROR THEN
7095         x_return_status := FND_API.G_RET_STS_ERROR;
7096 
7097     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7098         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7099 
7100     WHEN OTHERS THEN
7101         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7102         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
7103             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
7104         END IF;
7105 
7106         IF (g_debug_unexp) THEN
7107                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
7108                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
7109                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
7110                        || l_progress || ' SQL CODE is '||sqlcode);
7111                 END IF;
7112         END IF;
7113 END CHECK_PO_REL_REQPRICE;
7114 
7115 
7116 --For PO,PA: Header Checks
7117 /**
7118 * Private Procedure: CHECK_PO_PA_HEADER
7119 * Requires:
7120 *   IN PARAMETERS:
7121 *       p_document_id:      The requisition_header_id of submitted document
7122 *       p_online_report_id: Id used to INSERT INTO online_report_text table
7123 *       p_user_id:          User performing the action
7124 *       p_login_id:         Last update login_id
7125 *   IN OUT PARAMETERS
7126 *       p_sequence:          Sequence number of last reported error
7127 * Modifies: Inserts error msgs in online_report_text_gt table, uses
7128 *           global_temp tables for processing
7129 * Effects:  This procedure runs the document submission checks for HEADERS
7130 *           of POs and PAs
7131 * Returns:
7132 *  p_sequence: This parameter contains the current count of number of error
7133 *              messages inserted
7134 */
7135 PROCEDURE check_po_pa_header(p_document_id IN NUMBER,
7136                        p_online_report_id IN NUMBER,
7137                        p_user_id IN NUMBER,
7138                        p_login_id IN NUMBER,
7139                        p_sequence IN OUT NOCOPY NUMBER,
7140                        x_return_status OUT NOCOPY VARCHAR2) IS
7141 
7142 l_textline            po_online_report_text.text_line%TYPE := NULL;
7143 l_api_name  CONSTANT varchar2(40) := 'CHECK_PO_PA_HEADER';
7144 l_progress VARCHAR2(3);
7145 
7146 l_vendor_id           po_headers.vendor_id%TYPE;
7147 l_vendor_site_id      po_headers.vendor_site_id%TYPE;
7148 l_vendor_contact_id   po_headers.vendor_contact_id%TYPE; /*bug 6530879*/
7149 l_ship_to_location_id po_headers.ship_to_location_id%TYPE;
7150 l_bill_to_location_id po_headers.bill_to_location_id%TYPE;
7151 l_currency_code       po_headers.currency_code%TYPE;
7152 l_sob_currency_code   po_headers.currency_code%TYPE;
7153 l_rate_type           po_headers.rate_type%TYPE;
7154 l_rate                po_headers.rate%TYPE;
7155 l_rate_date           po_headers.rate_date%TYPE;
7156 l_acceptance_required po_headers.acceptance_required_flag%TYPE;
7157 
7158 --bug#3987438
7159 --Adding a new variable that would hold the name of the invalid
7160 --ship-to or bill-to location
7161 l_invalid_location   HR_LOCATIONS_ALL_TL.location_code%type;
7162 --bug#3987438
7163 
7164 -- CLM Award/ Idv Type
7165 l_clm_po          VARCHAR2(1);
7166 
7167 BEGIN
7168 
7169 l_progress := '000';
7170 
7171 l_clm_po := PO_CLM_INTG_GRP.is_clm_document(    p_doc_type => 'PO',
7172                                                 p_document_id => p_document_id );
7173 
7174 l_progress := '001';
7175 IF g_debug_stmt THEN
7176    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
7177      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
7178           || l_progress,'PO/PA Header 1: Vendor On Hold ');
7179    END IF;
7180 END IF;
7181 
7182   -- Check 1: Purchase Order vendor should not be on hold
7183     -- PO_SUB_VENDOR_ON_HOLD
7184 
7185   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_VENDOR_ON_HOLD');
7186   INSERT INTO po_online_report_text_gt(online_report_id,
7187         last_update_login,
7188         last_updated_by,
7189         last_update_date,
7190         created_by,
7191         creation_date,
7192         line_num,
7193         shipment_num,
7194         distribution_num,
7195         sequence,
7196         text_line,
7197                 message_name)
7198   SELECT  p_online_report_id,
7199         p_login_id,
7200         p_user_id,
7201           sysdate,
7202         p_user_id,
7203         sysdate,
7204         0,
7205         0,
7206         0,
7207         p_sequence + ROWNUM,
7208         substr(l_textline,1,240),
7209             'PO_SUB_VENDOR_ON_HOLD'
7210     FROM  PO_HEADERS_GT POH, PO_VENDORS POV, PO_SYSTEM_PARAMETERS PSP
7211     WHERE  POV.vendor_id     = POH.vendor_id
7212     AND    POH.po_header_id = p_document_id
7213     AND    nvl(PSP.ENFORCE_VENDOR_HOLD_FLAG,'N') = 'Y'
7214     AND    nvl(POV.hold_flag,'N') = 'Y';
7215 
7216 
7217      --Increment the p_sequence with number of errors reported in last query
7218     p_sequence := p_sequence + SQL%ROWCOUNT;
7219 ---------------------------------------------
7220 l_progress := '002';
7221     BEGIN
7222         SELECT POH.vendor_id,
7223                     POH.vendor_site_id,
7224                     POH.vendor_contact_id,
7225 		    POH.ship_to_location_id,
7226                     POH.bill_to_location_id,
7227                     POH.currency_code,
7228                     SOB.currency_code,
7229                     POH.rate_type,
7230                     POH.rate,
7231                     POH.rate_date,
7232 				    POH.acceptance_required_flag
7233         INTO        l_vendor_id,
7234                     l_vendor_site_id,
7235 					l_vendor_contact_id,
7236                     l_ship_to_location_id,
7237                     l_bill_to_location_id,
7238                     l_currency_code,
7239                     l_sob_currency_code,
7240                     l_rate_type,
7241                     l_rate,
7242                     l_rate_date,
7243 					l_acceptance_required
7244         FROM   PO_HEADERS_GT POH,
7245                GL_SETS_OF_BOOKS SOB,
7246                FINANCIALS_SYSTEM_PARAMETERS FSP
7247         WHERE  POH.po_header_id    = p_document_id
7248         AND    SOB.set_of_books_id = FSP.set_of_books_id;
7249     EXCEPTION
7250         WHEN NO_DATA_FOUND THEN
7251 l_progress := '003';
7252 IF g_debug_stmt THEN
7253    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
7254      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
7255           || l_progress,'PO/PA Header 2:System setup check');
7256    END IF;
7257 END IF;
7258             -- Check 2: When no rows are returned, its likely that there
7259             --are problems with system setup
7260             --<NOTE> See if we need to stop further processing
7261             l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_SYSTEM_SETUP');
7262             INSERT INTO po_online_report_text_gt(online_report_id,
7263         last_update_login,
7264         last_updated_by,
7265         last_update_date,
7266         created_by,
7267         creation_date,
7268         line_num,
7269         shipment_num,
7270         distribution_num,
7271         sequence,
7272         text_line,
7273                 message_name)
7274           VALUES (p_online_report_id,
7275         p_login_id,
7276         p_user_id,
7277           sysdate,
7278         p_user_id,
7279         sysdate,
7280         0,
7281         0,
7282         0,
7283         p_sequence + 1,
7284         substr(l_textline,1,240),
7285             'PO_SUB_SYSTEM_SETUP');
7286 
7287         --Increment the p_sequence with number of errors reported in last query
7288         p_sequence := p_sequence + 1;
7289 ---------------------------------------------
7290     END;
7291 
7292     --Check to see if the fields are null.  If the are null copy then
7293     --call the online report function.  Only print the message for the
7294     -- rate type if the rate type is null and they are using a foreign
7295     -- currency.  You know if a foreign currency is used if the currency
7296     -- code and sob_currency_code do not match.
7297 
7298 l_progress := '004';
7299 IF g_debug_stmt THEN
7300    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
7301      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
7302           || l_progress,'PO/PA Header 3: No Vendor');
7303    END IF;
7304 END IF;
7305 
7306     --Check 3: vendor_id is NULL
7307     IF l_vendor_id IS NULL THEN
7308          l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_ENTER_VENDOR');
7309          INSERT INTO po_online_report_text_gt(online_report_id,
7310         last_update_login,
7311         last_updated_by,
7312         last_update_date,
7313         created_by,
7314         creation_date,
7315         line_num,
7316         shipment_num,
7317         distribution_num,
7318         sequence,
7319         text_line,
7320                 message_name)
7321         VALUES (p_online_report_id,
7322         p_login_id,
7323         p_user_id,
7324           sysdate,
7325         p_user_id,
7326         sysdate,
7327         0,
7328         0,
7329         0,
7330         p_sequence + 1,
7331         substr(l_textline,1,240),
7332             'PO_SUB_ENTER_VENDOR');
7333 
7334          --Increment the p_sequence with number of errors reported in last query
7335          p_sequence := p_sequence + 1;
7336     END IF; --vendor_id
7337 
7338 l_progress := '005';
7339 IF g_debug_stmt THEN
7340    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
7341      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
7342           || l_progress,'PO/PA Header 4: No Vendor Site');
7343    END IF;
7344 END IF;
7345 
7346     --Check 4: vendor_site_id is NULL
7347     IF l_vendor_site_id IS NULL THEN
7348          l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_ENTER_VENDOR_SITE');
7349          INSERT INTO po_online_report_text_gt(online_report_id,
7350         last_update_login,
7351         last_updated_by,
7352         last_update_date,
7353         created_by,
7354         creation_date,
7355         line_num,
7356         shipment_num,
7357         distribution_num,
7358         sequence,
7359         text_line,
7360                 message_name)
7361         VALUES (p_online_report_id,
7362         p_login_id,
7363         p_user_id,
7364           sysdate,
7365         p_user_id,
7366         sysdate,
7367         0,
7368         0,
7369         0,
7370         p_sequence + 1,
7371         substr(l_textline,1,240),
7372             'PO_SUB_ENTER_VENDOR_SITE');
7373 
7374          --Increment the p_sequence with number of errors reported in last query
7375          p_sequence := p_sequence + 1;
7376     END IF; --vendor_site_id
7377 
7378 l_progress := '006';
7379 IF g_debug_stmt THEN
7380    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
7381      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
7382           || l_progress,'PO/PA Header 5: Ship_to_loc_id check ');
7383    END IF;
7384 END IF;
7385 
7386     --Check 5: ship_to_location_id is NULL
7387     IF l_ship_to_location_id IS NULL THEN
7388          l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_NO_SHIP_TO_LOC_ID');
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         VALUES (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 + 1,
7411         substr(l_textline,1,240),
7412             'PO_SUB_NO_SHIP_TO_LOC_ID');
7413 
7414          --Increment the p_sequence with number of errors reported in last query
7415          p_sequence := p_sequence + 1;
7416 
7417 --bug#3987438 Added a check to verify that the ship to location is active
7418     ELSE
7419         BEGIN
7420             SELECT hlt.location_code
7421             INTO l_invalid_location
7422             FROM hr_locations_all hla,
7423                  hr_locations_all_tl hlt
7424             WHERE hla.location_id = l_ship_to_location_id
7425             AND NVL(TRUNC(hla.inactive_date), TRUNC(SYSDATE)+1 ) <= TRUNC(SYSDATE)
7426             AND hlt.location_id=hla.location_id
7427             AND hlt.language=USERENV('LANG');
7428 
7429             INSERT INTO po_online_report_text_gt(online_report_id,
7430                 last_update_login,
7431                 last_updated_by,
7432                 last_update_date,
7433                 created_by,
7434                 creation_date,
7435                 line_num,
7436                 shipment_num,
7437                 distribution_num,
7438                 sequence,
7439                 text_line,
7440                 message_name)
7441             VALUES
7442                (
7443                 p_online_report_id,
7444                 p_login_id,
7445                 p_user_id,
7446                 SYSDATE,
7447                 p_user_id,
7448                 SYSDATE,
7449                 0,
7450                 0,
7451                 0,
7452                 p_sequence + 1,
7453                 substr(PO_CORE_S.get_translated_text
7454                 ( 'PO_SUB_INVALID_SHIP_TO_LOC',
7455                   'SHIP_TO_LOC',
7456                   l_invalid_location),1,240),
7457                 'PO_SUB_INVALID_SHIP_TO_LOC'
7458                 );
7459                  p_sequence := p_sequence + 1;
7460         EXCEPTION
7461             WHEN NO_DATA_FOUND THEN
7462                 null;
7463         END;
7464 --bug#3987438
7465 
7466     END IF; --ship_to_loc_id
7467 
7468 l_progress := '007';
7469 IF g_debug_stmt THEN
7470    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
7471      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
7472           || l_progress,'PO/PA Header 6: Bill_to_loc check');
7473    END IF;
7474 END IF;
7475 
7476     --Check 6: bill_to_location_id is NULL
7477     IF l_bill_to_location_id IS NULL THEN
7478 
7479          l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_NO_BILL_TO_LOC_ID');
7480          INSERT INTO po_online_report_text_gt(online_report_id,
7481         last_update_login,
7482         last_updated_by,
7483         last_update_date,
7484         created_by,
7485         creation_date,
7486         line_num,
7487         shipment_num,
7488         distribution_num,
7489         sequence,
7490         text_line,
7491                 message_name)
7492         VALUES (p_online_report_id,
7493         p_login_id,
7494         p_user_id,
7495           sysdate,
7496         p_user_id,
7497         sysdate,
7498         0,
7499         0,
7500         0,
7501         p_sequence + 1,
7502         substr(l_textline,1,240),
7503             'PO_SUB_NO_BILL_TO_LOC_ID');
7504 
7505          --Increment the p_sequence with number of errors reported in last query
7506          p_sequence := p_sequence + 1;
7507 
7508 --bug#3987438 Added a check to verify that the bill to location is active
7509     ELSE
7510         BEGIN
7511             SELECT hlt.location_code
7512             INTO l_invalid_location
7513             FROM hr_locations_all hla,
7514                  hr_locations_all_tl hlt
7515             WHERE hla.location_id = l_bill_to_location_id
7516             AND NVL(TRUNC(hla.inactive_date), TRUNC(SYSDATE)+1 ) <= TRUNC(SYSDATE)
7517             AND hlt.location_id=hla.location_id
7518             AND hlt.language=USERENV('LANG');
7519 
7520             INSERT INTO po_online_report_text_gt(online_report_id,
7521                 last_update_login,
7522                 last_updated_by,
7523                 last_update_date,
7524                 created_by,
7525                 creation_date,
7526                 line_num,
7527                 shipment_num,
7528                 distribution_num,
7529                 sequence,
7530                 text_line,
7531                 message_name)
7532             VALUES
7533                (
7534                 p_online_report_id,
7535                 p_login_id,
7536                 p_user_id,
7537                 SYSDATE,
7538                 p_user_id,
7539                 SYSDATE,
7540                 0,
7541                 0,
7542                 0,
7543                 p_sequence + 1,
7544                 substr(PO_CORE_S.get_translated_text('PO_SUB_INVALID_BILL_TO_LOC','BILL_TO_LOC',l_invalid_location),1,240),
7545                 'PO_SUB_INVALID_BILL_TO_LOC'
7546                 );
7547                  p_sequence := p_sequence + 1;
7548         EXCEPTION
7549             WHEN NO_DATA_FOUND THEN
7550                     null;
7551         END;
7552 --bug#3987438
7553 
7554     END IF; --bill_to_loc_id
7555 
7556 l_progress := '008';
7557 IF g_debug_stmt THEN
7558    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
7559      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
7560           || l_progress,'PO/PA Header 7: No currency code');
7561    END IF;
7562 END IF;
7563 
7564     --Check 7: currency_code is NULL
7565     IF l_currency_code IS NULL THEN
7566          l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_NO_CURRENCY_CODE');
7567          INSERT INTO po_online_report_text_gt(online_report_id,
7568         last_update_login,
7569         last_updated_by,
7570         last_update_date,
7571         created_by,
7572         creation_date,
7573         line_num,
7574         shipment_num,
7575         distribution_num,
7576         sequence,
7577         text_line,
7578                 message_name)
7579         VALUES (p_online_report_id,
7580         p_login_id,
7581         p_user_id,
7582           sysdate,
7583         p_user_id,
7584         sysdate,
7585         0,
7586         0,
7587         0,
7588         p_sequence + 1,
7589         substr(l_textline,1,240),
7590             'PO_SUB_NO_CURRENCY_CODE');
7591 
7592          --Increment the p_sequence with number of errors reported in last query
7593          p_sequence := p_sequence + 1;
7594     END IF; --currency_code
7595 
7596 l_progress := '009';
7597 IF g_debug_stmt THEN
7598    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
7599      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
7600           || l_progress,'PO/PA Header 8: Rate related checks');
7601    END IF;
7602 END IF;
7603 
7604     --Check 8: rate
7605     IF l_currency_code <> l_sob_currency_code AND
7606         (l_rate_type IS NULL OR l_rate IS NULL OR
7607             (l_rate_type <> 'User' AND l_rate_date IS NULL)) -- Bug 3759198
7608     THEN
7609          l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_NO_RATE_TYPE');
7610          INSERT INTO po_online_report_text_gt(online_report_id,
7611         last_update_login,
7612         last_updated_by,
7613         last_update_date,
7614         created_by,
7615         creation_date,
7616         line_num,
7617         shipment_num,
7618         distribution_num,
7619         sequence,
7620         text_line,
7621                 message_name)
7622         VALUES (p_online_report_id,
7623         p_login_id,
7624         p_user_id,
7625           sysdate,
7626         p_user_id,
7627         sysdate,
7628         0,
7629         0,
7630         0,
7631         p_sequence + 1,
7632         substr(l_textline,1,240),
7633             'PO_SUB_NO_RATE_TYPE');
7634 
7635          --Increment the p_sequence with number of errors reported in last query
7636          p_sequence := p_sequence + 1;
7637 
7638     END IF; --rate related
7639 
7640 -- <SERVICES FPJ START>
7641 
7642 l_progress := '015';
7643 IF g_debug_stmt THEN
7644    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
7645      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
7646           || l_progress,'PO/PA Header 9: Currency Rate Type');
7647    END IF;
7648 END IF;
7649 
7650     -- CHECK 9: The Currency Rate Type cannot be "User" if the document
7651     --          contains any Lines with Value Basis of "Rate".
7652 
7653     IF ( l_rate_type = 'User' ) THEN
7654 
7655         l_textline := FND_MESSAGE.get_string('PO','PO_SUB_USER_RATE_TYPE');
7656 
7657         INSERT INTO po_online_report_text_gt
7658         (   online_report_id
7659         ,   last_update_login
7660         ,   last_updated_by
7661         ,   last_update_date
7662         ,   created_by
7663         ,   creation_date
7664         ,   line_num
7665         ,   shipment_num
7666         ,   distribution_num
7667         ,   sequence
7668         ,   text_line
7669         ,   message_name
7670         )
7671         SELECT p_online_report_id
7672         ,      p_login_id
7673         ,      p_user_id
7674         ,      sysdate
7675         ,      p_user_id
7676         ,      sysdate
7677         ,      NULL
7678         ,      NULL
7679         ,      NULL
7680         ,      p_sequence + ROWNUM
7681         ,      l_textline
7682         ,      'PO_SUB_USER_RATE_TYPE'
7683         FROM   dual
7684         WHERE  exists ( SELECT 'Rate-based lines exist'
7685                         FROM   po_lines_gt      POL
7686                         ,      po_line_types_b  PLT
7687                         WHERE  p_document_id = POL.po_header_id
7688                         AND    POL.line_type_id = PLT.line_type_id
7689                         AND    PLT.order_type_lookup_code = 'RATE'
7690                       );
7691 
7692         --Increment the p_sequence with number of errors reported in last query
7693         p_sequence := p_sequence + SQL%ROWCOUNT;
7694 
7695     END IF; -- ( l_rate_type = 'User' )
7696 
7697 -- <SERVICES FPJ END>
7698 
7699 l_progress := '020';
7700 IF g_debug_stmt THEN
7701    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
7702      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
7703           || l_progress,'PO/PA Header 10: Cannot approve documents on hold');
7704    END IF;
7705 END IF;
7706     -- Check 10: Purchase Order should not be on hold (Bug 3678912)
7707         -- PO_ON_HOLD_CANNOT_APPROVE
7708 
7709   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_ON_HOLD_CANNOT_APPROVE');
7710   INSERT INTO po_online_report_text_gt(online_report_id,
7711         last_update_login,
7712         last_updated_by,
7713         last_update_date,
7714         created_by,
7715         creation_date,
7716         line_num,
7717         shipment_num,
7718         distribution_num,
7719         sequence,
7720         text_line,
7721                                 message_name)
7722   SELECT  p_online_report_id,
7723         p_login_id,
7724         p_user_id,
7725                   sysdate,
7726         p_user_id,
7727         sysdate,
7728         0,
7729         0,
7730         0,
7731         p_sequence + ROWNUM,
7732         substr(l_textline,1,240),
7733                    'PO_ON_HOLD_CANNOT_APPROVE'
7734     FROM  PO_HEADERS_GT POH
7735     WHERE  POH.po_header_id = p_document_id
7736     AND    nvl(POH.USER_HOLD_FLAG,'N') = 'Y';
7737 
7738     --Increment the p_sequence with number of errors reported in last query
7739         p_sequence := p_sequence + SQL%ROWCOUNT;
7740 
7741 -- bug 6530879
7742 -- Check 11: Vendor should be valid when approving the document.
7743 -- Important for reapproval, to avoid the case when the vendor has
7744 -- been invalidated by first successful approval.
7745 
7746 /* Need to find out.
7747 
7748  fnd_message.set_name('PO', 'PO_PDOI_INVALID_VENDOR');
7749  fnd_message.set_token('VALUE', to_char(x_vendor_id), FALSE);
7750 l_textline := fnd_message.get;
7751 
7752 */
7753 
7754 l_textline :=  FND_MESSAGE.GET_STRING('PO', 'PO_PDOI_INVALID_VENDOR');
7755 
7756   if (l_vendor_id is NOT NULL) then
7757 	fnd_message.set_name('PO', 'PO_PDOI_INVALID_VENDOR');
7758         fnd_message.set_token('VALUE', to_char(l_vendor_id), FALSE);
7759 	l_textline := substr(fnd_message.get, 1, 240);
7760 
7761 
7762  	INSERT INTO po_online_report_text_gt(online_report_id,
7763  				last_update_login,
7764  				last_updated_by,
7765  				last_update_date,
7766  				created_by,
7767  				creation_date,
7768  				line_num,
7769  				shipment_num,
7770  				distribution_num,
7771  				sequence,
7772  				text_line,
7773                                 message_name)
7774  	SELECT 	p_online_report_id,
7775  		    p_login_id,
7776  		    p_user_id,
7777      	            sysdate,
7778  		    p_user_id,
7779  		    sysdate,
7780  		    0,
7781  		    0,
7782  		    0,
7783  		    p_sequence + ROWNUM,
7784  		    substr(l_textline,1,240),
7785                    'PO_PDOI_INVALID_VENDOR'
7786 	FROM  dual
7787     where not exists (select 'Y'
7788 			from PO_HEADERS_GT POH, po_vendors pov
7789 		       WHERE  POH.po_header_id = p_document_id
7790 		         AND  pov.vendor_id = poh.vendor_id
7791 			 AND  pov.enabled_flag = 'Y'
7792 			 AND  SYSDATE BETWEEN nvl(pov.start_date_active, SYSDATE-1)
7793                                   AND nvl(pov.end_date_active, SYSDATE+1));
7794  --Increment the p_sequence with number of errors reported in last query
7795         p_sequence := p_sequence + SQL%ROWCOUNT;
7796 
7797  end if;
7798 
7799 -- Check 12: check the validity of the vendor site.
7800  if (l_vendor_site_id is not null) then
7801 
7802   	fnd_message.set_name('PO', 'PO_PDOI_INVALID_VENDOR_SITE');
7803  	fnd_message.set_token('VALUE', to_char(l_vendor_site_id), FALSE);
7804 	l_textline := substr(fnd_message.get, 1, 240);
7805 
7806  	INSERT INTO po_online_report_text_gt(online_report_id,
7807  				last_update_login,
7808  				last_updated_by,
7809  				last_update_date,
7810  				created_by,
7811  				creation_date,
7812  				line_num,
7813  				shipment_num,
7814  				distribution_num,
7815  				sequence,
7816  				text_line,
7817                                 message_name)
7818  	SELECT 	p_online_report_id,
7819  		    p_login_id,
7820  		    p_user_id,
7821      	            sysdate,
7822  		    p_user_id,
7823  		    sysdate,
7824  		    0,
7825  		    0,
7826  		    0,
7827  		    p_sequence + ROWNUM,
7828  		    substr(l_textline,1,240),
7829                    'PO_PDOI_INVALID_VENDOR_SITE'
7830     FROM  dual
7831     where not exists (select 'Y'
7832 			from PO_HEADERS_GT POH, po_vendor_sites povs
7833 		        WHERE  POH.po_header_id = p_document_id
7834 		        AND  povs.vendor_site_id = poh.vendor_site_id
7835 			AND    nvl(povs.rfq_only_site_flag,'N') <> 'Y'
7836 			AND    povs.purchasing_site_flag = 'Y'
7837 		        AND    SYSDATE < nvl(povs.inactive_date, SYSDATE + 1));
7838  --Increment the p_sequence with number of errors reported in last query
7839         p_sequence := p_sequence + SQL%ROWCOUNT;
7840 end if;
7841 
7842 -- check 13: validate vendor contact
7843 if (l_vendor_contact_id is not null) then
7844 
7845 	fnd_message.set_name('PO', 'PO_PDOI_INVALID_VDR_CNTCT');
7846  	fnd_message.set_token('VALUE', to_char(l_vendor_contact_id), FALSE);
7847 	l_textline := substr(fnd_message.get, 1, 240);
7848 
7849 	INSERT INTO po_online_report_text_gt(online_report_id,
7850  				last_update_login,
7851  				last_updated_by,
7852  				last_update_date,
7853  				created_by,
7854  				creation_date,
7855  				line_num,
7856  				shipment_num,
7857  				distribution_num,
7858  				sequence,
7859  				text_line,
7860                                 message_name)
7861  	SELECT 	p_online_report_id,
7862  		    p_login_id,
7863  		    p_user_id,
7864      	            sysdate,
7865  		    p_user_id,
7866  		    sysdate,
7867  		    0,
7868  		    0,
7869  		    0,
7870  		    p_sequence + ROWNUM,
7871  		    substr(l_textline,1,240),
7872                    'PO_PDOI_INVALID_VDR_CNTCT'
7873 	FROM  dual
7874 	--Start of code changes for the bug 16244229
7875 	WHERE NOT EXISTS ( SELECT  'Y'
7876 			FROM
7877 			AP_SUPPLIER_CONTACTS PVC ,
7878 			AP_SUPPLIER_SITES_ALL PVS ,
7879 			HZ_PARTIES HP ,
7880 			HZ_RELATIONSHIPS HPR ,
7881 			HZ_PARTY_SITES HPS ,
7882 			HZ_ORG_CONTACTS HOC ,
7883 			HZ_PARTIES HP2 ,
7884 			AP_SUPPLIERS APS,
7885 			po_headers_gt POH
7886 			WHERE PVC.PER_PARTY_ID = HP.PARTY_ID
7887 			AND PVC.REL_PARTY_ID   = HP2.PARTY_ID
7888 			AND PVC.ORG_CONTACT_ID                           = HOC.ORG_CONTACT_ID(+)
7889 			AND PVC.RELATIONSHIP_ID                          = HPR.RELATIONSHIP_ID
7890 			AND HPR.DIRECTIONAL_FLAG                         ='F'
7891 			AND NVL( APS.VENDOR_TYPE_LOOKUP_CODE, 'DUMMY' ) <> 'EMPLOYEE'
7892 			AND ( (Pvc.Party_Site_Id  = Hps.Party_Site_Id
7893 				AND PVS.PARTY_SITE_ID                     = PVC.ORG_PARTY_SITE_ID
7894 				AND PVS.VENDOR_ID                                = APS.VENDOR_ID
7895 				AND SYSDATE < nvl( LEAST(NVL(HPR.END_DATE, TO_DATE('12/31/4712','MM/DD/RRRR')), NVL(PVC.INACTIVE_DATE, TO_DATE('12/31/4712','MM/DD/RRRR'))), SYSDATE+1))
7896 			OR (PVC.ORG_PARTY_SITE_ID                       IS NULL
7897 				AND PVC.VENDOR_SITE_ID                          IS NULL
7898 				AND HPR.OBJECT_ID                                = APS.PARTY_ID
7899 				AND HPR.RELATIONSHIP_CODE                        = 'CONTACT_OF'
7900 				And Hpr.Object_Type                              = 'ORGANIZATION'
7901 				AND SYSDATE < NVL(HPR.END_DATE, SYSDATE+1) )
7902 			)
7903 			AND POH.VENDOR_CONTACT_ID = PVC.VENDOR_CONTACT_ID
7904 			AND POH.PO_HEADER_ID=p_document_id);
7905 	--End of code changes for the bug 16244229
7906 
7907    --Increment the p_sequence with number of errors reported in last query
7908         p_sequence := p_sequence + SQL%ROWCOUNT;
7909 end if;
7910 
7911 -- CLM apprvl checks. If acceptance is required for a document then there must exist a valid vendor contact with valid user name associated with it
7912 if l_acceptance_required in ('S') then
7913 	-- check 14: Check if  vendor contact is available.
7914 	if l_vendor_contact_id is null then
7915 		l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_ENTER_VENDOR_CONTACT');
7916          INSERT INTO po_online_report_text_gt(online_report_id,
7917         last_update_login,
7918         last_updated_by,
7919         last_update_date,
7920         created_by,
7921         creation_date,
7922         line_num,
7923         shipment_num,
7924         distribution_num,
7925         sequence,
7926         text_line,
7927                 message_name)
7928         VALUES (p_online_report_id,
7929         p_login_id,
7930         p_user_id,
7931           sysdate,
7932         p_user_id,
7933         sysdate,
7934         0,
7935         0,
7936         0,
7937         p_sequence + 1,
7938         substr(l_textline,1,240),
7939             'PO_SUB_ENTER_VENDOR_CONTACT');
7940 
7941          --Increment the p_sequence with number of errors reported in last query
7942          p_sequence := p_sequence + 1;
7943 	else
7944 		-- check 15: Check if vendor contact has a valid fnd user.
7945 		fnd_message.set_name('PO', 'PO_CONTACT_WITHOUT_VALID_USER');
7946 		fnd_message.set_token('VALUE', to_char(l_vendor_contact_id), FALSE);
7947 		l_textline := substr(fnd_message.get, 1, 240);
7948 
7949 		INSERT INTO po_online_report_text_gt(online_report_id,
7950 					last_update_login,
7951 					last_updated_by,
7952 					last_update_date,
7953 					created_by,
7954 					creation_date,
7955 					line_num,
7956 					shipment_num,
7957 					distribution_num,
7958 					sequence,
7959 					text_line,
7960 									message_name)
7961 		SELECT 	p_online_report_id,
7962 				p_login_id,
7963 				p_user_id,
7964 						sysdate,
7965 				p_user_id,
7966 				sysdate,
7967 				0,
7968 				0,
7969 				0,
7970 				p_sequence + ROWNUM,
7971 				substr(l_textline,1,240),
7972 					   'PO_CONTACT_WITHOUT_VALID_USER'
7973 		FROM  dual
7974 		WHERE NOT EXISTS (SELECT 'Y'
7975 							FROM po_supplier_contacts_val_v pscv,
7976 							po_headers_gt poh,
7977 							fnd_user fu
7978 							WHERE pscv.vendor_contact_id = poh.vendor_contact_id
7979 							AND pscv.vendor_site_id      = poh.vendor_site_id -- bug 9865162, included the check for vendor_site_id
7980 							AND poh.po_header_id         = p_document_id
7981 							AND fu.user_name = pscv.user_name
7982 							AND trunc(sysdate) BETWEEN trunc(fu.start_date)
7983                                                         and trunc(nvl(fu.end_date,SYSDATE+1)));
7984 
7985    --Increment the p_sequence with number of errors reported in last query
7986         p_sequence := p_sequence + SQL%ROWCOUNT;
7987 	end if;
7988 end if; --l_acceptance_required
7989 --End CLM Apprvl
7990 ------------------------------------------------------------------------
7991 
7992 -- CLM Award Type
7993   -- Check 16: Award/IDV Type should not be NULL
7994     -- PO_SUB_AWARD_TYPE_NULL
7995 
7996   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_AWARD_TYPE_NULL');
7997   INSERT INTO po_online_report_text_gt(online_report_id,
7998         last_update_login,
7999         last_updated_by,
8000         last_update_date,
8001         created_by,
8002         creation_date,
8003         line_num,
8004         shipment_num,
8005         distribution_num,
8006         sequence,
8007         text_line,
8008                 message_name)
8009   SELECT  p_online_report_id,
8010         p_login_id,
8011         p_user_id,
8012           sysdate,
8013         p_user_id,
8014         sysdate,
8015         0,
8016         0,
8017         0,
8018         p_sequence + ROWNUM,
8019         substr(l_textline,1,240),
8020             'PO_SUB_AWARD_TYPE_NULL'
8021     FROM  PO_HEADERS_GT POH
8022     WHERE  POH.po_header_id = p_document_id
8023     AND    POH.type_lookup_code = 'STANDARD'
8024     AND    POH.clm_award_type IS NULL
8025     AND    nvl(l_clm_po,'N') = 'Y';
8026 
8027 --Increment the p_sequence with number of errors reported in last query
8028     p_sequence := p_sequence + SQL%ROWCOUNT;
8029 -----------------------------------------------------------------------
8030 
8031 -- Check 17: Order of IDV Type without source IDV or External IDV
8032     -- PO_SUB_IDV_WITHOUT_SOURCE
8033 
8034   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_IDV_WITHOUT_SOURCE');
8035   INSERT INTO po_online_report_text_gt(online_report_id,
8036         last_update_login,
8037         last_updated_by,
8038         last_update_date,
8039         created_by,
8040         creation_date,
8041         line_num,
8042         shipment_num,
8043         distribution_num,
8044         sequence,
8045         text_line,
8046                 message_name)
8047   SELECT  p_online_report_id,
8048         p_login_id,
8049         p_user_id,
8050           sysdate,
8051         p_user_id,
8052         sysdate,
8053         0,
8054         0,
8055         0,
8056         p_sequence + ROWNUM,
8057         substr(l_textline,1,240),
8058             'PO_SUB_IDV_WITHOUT_SOURCE'
8059     FROM  PO_HEADERS_GT POH
8060     WHERE  POH.po_header_id = p_document_id
8061       AND  POH.type_lookup_code = 'STANDARD'
8062       AND  Nvl(POH.clm_award_type,'N') IN (SELECT lookup_code
8063                                              FROM PO_LOOKUP_CODES
8064                                             WHERE lookup_type = 'PO_CLM_AWD_TYPE_ORD_OFF_IDV')
8065       AND  POH.clm_source_document_id IS NULL
8066       AND  POH.clm_external_idv IS NULL
8067       AND  nvl(l_clm_po,'N') = 'Y';
8068 
8069 --Increment the p_sequence with number of errors reported in last query
8070     p_sequence := p_sequence + SQL%ROWCOUNT;
8071 
8072 -- Check 18: Award Type with source IDV or External IDV
8073     -- PO_SUB_IDV_WITHOUT_SOURCE
8074 
8075   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_AWARD_WITH_SOURCE');
8076   INSERT INTO po_online_report_text_gt(online_report_id,
8077         last_update_login,
8078         last_updated_by,
8079         last_update_date,
8080         created_by,
8081         creation_date,
8082         line_num,
8083         shipment_num,
8084         distribution_num,
8085         sequence,
8086         text_line,
8087                 message_name)
8088   SELECT  p_online_report_id,
8089         p_login_id,
8090         p_user_id,
8091           sysdate,
8092         p_user_id,
8093         sysdate,
8094         0,
8095         0,
8096         0,
8097         p_sequence + ROWNUM,
8098         substr(l_textline,1,240),
8099             'PO_SUB_AWARD_WITH_SOURCE'
8100     FROM  PO_HEADERS_GT POH
8101     WHERE  POH.po_header_id = p_document_id
8102       AND  POH.type_lookup_code = 'STANDARD'
8103       AND  Nvl(POH.clm_award_type,'N') IN (SELECT lookup_code
8104                                            FROM PO_LOOKUP_CODES
8105                                           WHERE lookup_type = 'PO_CLM_AWD_TYPE_SPO')
8106       AND  (POH.clm_source_document_id IS NOT NULL
8107             OR
8108             POH.clm_external_idv IS NOT NULL )
8109       AND  nvl(l_clm_po,'N') = 'Y';
8110 
8111 --Increment the p_sequence with number of errors reported in last query
8112     p_sequence := p_sequence + SQL%ROWCOUNT;
8113 
8114 --<Bug 9040655 START Buyer should not be inactive on the document>
8115     -- Check 19: Buyer on the po/pa should not be inactive
8116         -- PO_BUYER_INACTIVE
8117 
8118   l_textline := substr(FND_MESSAGE.GET_STRING('PO', 'PO_BUYER_INACTIVE'),1,240);
8119   INSERT INTO po_online_report_text_gt(online_report_id,
8120         last_update_login,
8121         last_updated_by,
8122         last_update_date,
8123         created_by,
8124         creation_date,
8125         line_num,
8126         shipment_num,
8127         distribution_num,
8128         sequence,
8129         text_line,
8130                                 message_name)
8131   SELECT  p_online_report_id,
8132         p_login_id,
8133         p_user_id,
8134                   sysdate,
8135         p_user_id,
8136         sysdate,
8137         0,
8138         0,
8139         0,
8140         p_sequence + ROWNUM,
8141         l_textline,
8142                    'PO_BUYER_INACTIVE'
8143         FROM  dual
8144     where not exists (select 'inactive buyer'
8145                       from PO_HEADERS_GT POH,
8146                            PO_BUYERS_V POB -- <Bug 11686275> Replace PO_BUYERS_VAL_V with PO_BUYERS_V
8147 		      where poh.agent_id = pob.employee_id);
8148 
8149     --Increment the p_sequence with number of errors reported in last query
8150     p_sequence := p_sequence + SQL%ROWCOUNT;
8151 
8152 --<Bug 9040655 END>
8153 
8154 
8155     l_progress := '050';
8156     x_return_status := FND_API.G_RET_STS_SUCCESS;
8157 EXCEPTION
8158     WHEN FND_API.G_EXC_ERROR THEN
8159         x_return_status := FND_API.G_RET_STS_ERROR;
8160 
8161     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8162         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8163 
8164     WHEN OTHERS THEN
8165         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8166         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
8167             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
8168         END IF;
8169 
8170         IF (g_debug_unexp) THEN
8171                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
8172                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
8173                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
8174                        || l_progress || ' SQL CODE is '||sqlcode);
8175                 END IF;
8176         END IF;
8177 
8178 END CHECK_PO_PA_HEADER;
8179 
8180 --For PO
8181 /**
8182 * Private Procedure: CHECK_PO
8183 * Requires:
8184 *   IN PARAMETERS:
8185 *       p_document_id:      The requisition_header_id of submitted document
8186 *       p_online_report_id: Id used to INSERT INTO online_report_text table
8187 *       p_user_id:          User performing the action
8188 *       p_login_id:         Last update login_id
8189 *       p_check_asl:        Determines whether to perform ASL checks...
8190 *                           PO_SUB_ITEM_NOT_APPROVED, PO_SUB_ITEM_ASL_DEBARRED
8191 *   IN OUT PARAMETERS
8192 *       p_sequence:          Sequence number of last reported error
8193 * Modifies: Updates PO_DISTRIBUTIONS table with RATE information.
8194 *           Inserts error msgs in online_report_text_gt table, uses
8195 *           global_temp tables for processing
8196 * Effects:  This procedure runs the document submission checks for POs
8197 * Returns:
8198 *  p_sequence: This parameter contains the current count of number of error
8199 *              messages inserted
8200 */
8201 PROCEDURE check_po(p_document_id IN NUMBER,
8202                        p_online_report_id IN NUMBER,
8203                        p_user_id IN NUMBER,
8204                        p_login_id IN NUMBER,
8205                        p_sequence IN OUT NOCOPY NUMBER,
8206                        x_return_status OUT NOCOPY VARCHAR2) IS
8207 
8208 l_textline  po_online_report_text.text_line%TYPE := NULL;
8209 l_api_name  CONSTANT varchar2(40) := 'CHECK_PO';
8210 l_progress VARCHAR2(3);
8211 l_is_complex_po     boolean;
8212 l_line_loc_token_value fnd_new_messages.message_text%TYPE;
8213 TYPE NumTab is TABLE of NUMBER INDEX by BINARY_INTEGER;
8214 l_quantity1 NumTab;
8215 l_quantity2 NumTab;
8216 l_line_num   NumTab;
8217 l_shipment_num NumTab;
8218 l_dist_num NumTab;
8219 l_line_qty_tbl    NumTab;                                     -- <SERVICES FPJ>
8220 l_line_amt_tbl    NumTab;                                     -- <SERVICES FPJ>
8221 l_ship_qty_tbl    NumTab;                                     -- <SERVICES FPJ>
8222 l_ship_amt_tbl    NumTab;                                     -- <SERVICES FPJ>
8223 l_dist_qty_tbl    NumTab;                                     -- <SERVICES FPJ>
8224 l_dist_amt_tbl    NumTab;                                     -- <SERVICES FPJ>
8225 l_rowcount NumTab;
8226 
8227 l_val_contract_limit NUMBER;                                  -- bug3673292
8228 
8229 --bug#3987438
8230 --A new table to hold the invalid ship to location codes
8231 TYPE CharTab is TABLE of HR_LOCATIONS_ALL_TL.location_code%type INDEX by BINARY_INTEGER;
8232 l_ship_to_location_tbl CharTab;
8233 --bug#3987438
8234 
8235 --<R12 eTax Integration Start>
8236 l_return_status    VARCHAR2(1);
8237 l_tax_status       VARCHAR2(1);
8238 l_msg_count        NUMBER;
8239 l_msg_data         VARCHAR2(2000);
8240 l_tax_message      fnd_new_messages.message_text%TYPE;
8241 --<R12 eTax Integration End>
8242 
8243 l_po_vmi_display_warning VARCHAR2(1);
8244 -- CLM Aprvl
8245 l_clm_document VARCHAR2(1) := 'N';  -- Whether its a CLM document or Not
8246 -- CLM Aprvl
8247 -- CLM-LnSc-st
8248 l_enf_funds_change_allowance po_system_parameters.enforce_funds_change_allowance%TYPE;
8249 l_funds_change_allowance     po_system_parameters.funds_change_allowance%TYPE;
8250 l_funds_change_amount        po_system_parameters.funds_change_amount%TYPE;
8251 -- CLM-LnSc-end
8252 
8253 BEGIN
8254 
8255 l_progress := '000';
8256 -- BUG 2687600 mbhargav
8257 --Removed Update statement to update rate in po_distributions
8258 
8259 l_progress := '001';
8260 IF g_debug_stmt THEN
8261    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
8262      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
8263           || l_progress,'PO 1: No lines');
8264    END IF;
8265 END IF;
8266 
8267 -- CLM Aprvl
8268 Begin
8269     SELECT 'Y' into l_clm_document
8270     FROM   po_headers_all h,
8271            po_doc_style_headers s
8272     WHERE  h.po_header_id = p_document_id
8273            AND h.style_id = s.style_id
8274            AND Nvl(s.clm_flag,'N') = 'Y';
8275 Exception
8276 When No_Data_Found Then
8277     l_clm_document := 'N';
8278 End;
8279   -- CLM Aprvl
8280 
8281     -- Check 1: Header must have at least one line
8282     -- PO_SUB_HEADER_NO_LINES
8283     -- Message inserted is 'Purchase Document has no lines'
8284     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_HEADER_NO_LINES');
8285     INSERT INTO po_online_report_text_gt(online_report_id,
8286         last_update_login,
8287         last_updated_by,
8288         last_update_date,
8289         created_by,
8290           creation_date,
8291         line_num,
8292         shipment_num,
8293         distribution_num,
8294         sequence,
8295         text_line,
8296                 message_name)
8297     SELECT  p_online_report_id,
8298             p_login_id,
8299             p_user_id,
8300             sysdate,
8301             p_user_id,
8302             sysdate,
8303             0, 0, 0,
8304             p_sequence + ROWNUM,
8305             substr(l_textline,1,240),
8306             'PO_SUB_HEADER_NO_LINES'
8307     FROM   PO_HEADERS_GT POH
8308     WHERE  POH.po_header_id = p_document_id AND
8309            NOT EXISTS (SELECT 'Lines Exist'
8310                        FROM   PO_LINES_GT POL
8311                        WHERE  POL.po_header_id = POH.po_header_id
8312                        --Bug 3289638 Check for any line to exist irrespective of cancel_flag
8313                        --AND    nvl(POL.cancel_flag,'N') = 'N'
8314                        )
8315            AND Nvl(poh.draft_id,-1) = -1 ;--<PAR Project> added this conditions so that this Sql is executed only for awards and is skipped for mods and par.
8316 
8317     --Increment the p_sequence with number of errors reported in last query
8318     p_sequence := p_sequence + SQL%ROWCOUNT;
8319 --------------------------------------------------
8320 
8321 l_progress := '002';
8322 IF g_debug_stmt THEN
8323    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
8324      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
8325           || l_progress,'PO 2: No shipment ');
8326    END IF;
8327 END IF;
8328 
8329   -- Check 2: Each Purchase Order Line must have at least one shipment
8330     -- PO_SUB_LINE_NO_SHIP
8331 
8332   l_is_complex_po := PO_COMPLEX_WORK_PVT.is_complex_work_po(p_document_id);
8333 
8334   IF (l_is_complex_po) THEN
8335     l_line_loc_token_value := FND_MESSAGE.GET_STRING('PO', 'PO_LINE_LOC_TYPE_LOW_PAYITEMS');
8336   ELSE
8337     l_line_loc_token_value := FND_MESSAGE.GET_STRING('PO', 'PO_LINE_LOC_TYPE_LOW_SCHEDULES');
8338   END IF;
8339 
8340   FND_MESSAGE.SET_NAME('PO','PO_SUB_LINE_NO_SHIP');
8341   FND_MESSAGE.SET_TOKEN('LINE_LOCATION_TYPE', l_line_loc_token_value);
8342 
8343   l_textline := FND_MESSAGE.GET;
8344   INSERT INTO po_online_report_text_gt(online_report_id,
8345         last_update_login,
8346         last_updated_by,
8347         last_update_date,
8348         created_by,
8349         creation_date,
8350         line_num,
8351         shipment_num,
8352         distribution_num,
8353         sequence,
8354         text_line,
8355                 message_name)
8356   SELECT  p_online_report_id,
8357         p_login_id,
8358         p_user_id,
8359           sysdate,
8360         p_user_id,
8361         sysdate,
8362         POL.line_num,
8363         0,
8364         0,
8365         p_sequence + ROWNUM,
8366         substr(g_linemsg||g_delim||POL.line_num||g_delim||l_textline,1,240),
8367             'PO_SUB_LINE_NO_SHIP'
8368   FROM    PO_LINES_GT POL
8369     WHERE   POL.po_header_id = p_document_id AND
8370         nvl(POL.cancel_flag,'N') = 'N' AND
8371 		  -- CLM Aprvl  Info Lines will not be having Shipments
8372 		  Nvl(POL.CLM_INFO_FLAG,'N')= 'N' AND
8373           -- CLM Aprvl
8374         nvl(POL.closed_code,'OPEN') <> 'FINALLY CLOSED' AND
8375         NOT EXISTS (SELECT 'Shipments Exist'
8376                   FROM   PO_LINE_LOCATIONS_GT PLL
8377                   WHERE  PLL.po_line_id = POL.po_line_id AND
8378                        PLL.shipment_type in ('STANDARD','PLANNED'));
8379 
8380     --Increment the p_sequence with number of errors reported in last query
8381     p_sequence := p_sequence + SQL%ROWCOUNT;
8382 -------------------------------------------------
8383 
8384 l_progress := '003';
8385 IF g_debug_stmt THEN
8386    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
8387      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
8388           || l_progress,'PO 3: Atleast One dist ');
8389    END IF;
8390 END IF;
8391 
8392      --Check 3: Each PO shipment must have atleast one distribution
8393      --PO_SUB_SHIP_NO_DIST
8394 
8395      -- <CLM Sunset Memo Project: Allow Shipments without Distributions with a Warning>
8396 
8397      IF (l_is_complex_po) THEN
8398        l_line_loc_token_value := FND_MESSAGE.GET_STRING('PO', 'PO_LINE_LOC_TYPE_LOW_P_PAYITEM');
8399      ELSE
8400        l_line_loc_token_value := FND_MESSAGE.GET_STRING('PO', 'PO_LINE_LOC_TYPE_LOWER_S_SCH');
8401      END IF;
8402 
8403      IF (l_clm_document <> 'Y') THEN
8404        FND_MESSAGE.SET_NAME('PO', 'PO_SUB_SHIP_NO_DIST');
8405        FND_MESSAGE.SET_TOKEN('LINE_LOCATION_TYPE', l_line_loc_token_value);
8406        l_textline := FND_MESSAGE.GET;
8407      ELSE
8408        l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_CLM_SUB_SHIP_NO_DIST_WARN');
8409      END IF;
8410 
8411      INSERT INTO po_online_report_text_gt(online_report_id,
8412         last_update_login,
8413         last_updated_by,
8414         last_update_date,
8415         created_by,
8416         creation_date,
8417         line_num,
8418         shipment_num,
8419         distribution_num,
8420         sequence,
8421         text_line,
8422         message_name,
8423         message_type)
8424      SELECT p_online_report_id,
8425             p_login_id,
8426             p_user_id,
8427             sysdate,
8428             p_user_id,
8429             sysdate,
8430             POL.line_num,
8431             PLL.shipment_num,
8432             0,
8433             p_sequence + ROWNUM,
8434             substr(g_linemsg||g_delim||Nvl(POL.line_num_display,POL.line_num)||g_delim||
8435                    g_shipmsg||g_delim||PLL.shipment_num||g_delim||l_textline,1,240),
8436             Decode(l_clm_document, 'Y', 'PO_CLM_SUB_SHIP_NO_DIST_WARN', 'PO_SUB_SHIP_NO_DIST'),
8437             Decode(l_clm_document, 'Y', 'W', NULL)
8438      FROM   PO_LINE_LOCATIONS_GT PLL,PO_LINES_GT POL
8439      WHERE  PLL.po_line_id   = POL.po_line_id AND
8440             PLL.po_header_id = p_document_id AND
8441             nvl(PLL.cancel_flag, 'N')  = 'N' AND
8442             nvl(POL.cancel_flag, 'N')  = 'N' AND
8443             nvl(PLL.closed_code, 'OPEN') <> 'FINALLY CLOSED' AND
8444             PLL.shipment_type in ('STANDARD', 'PLANNED', 'PREPAYMENT') --<Complex Work R12>
8445 	    AND Nvl(POL.CLM_OPTION_INDICATOR, 'B') <> 'O'
8446 	    AND Nvl(POL.CLM_INFO_FLAG,'N')= 'N'
8447             AND NOT EXISTS (SELECT 'Distribution Exists'
8448                             FROM   PO_DISTRIBUTIONS_GT POD
8449                             WHERE  POD.line_location_id = PLL.line_location_id);
8450 
8451     --Increment the p_sequence with number of errors reported in last query
8452     p_sequence := p_sequence + SQL%ROWCOUNT;
8453 -----------------------------------------------
8454 
8455 l_progress := '004';
8456 IF g_debug_stmt THEN
8457    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
8458      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
8459           || l_progress,'PO 4/5: Qty/Amt rollup checks');
8460    END IF;
8461 END IF;
8462 
8463     -- Check 4: Quantities/Amounts between Purchase Order Line and Shipments
8464     -- must match.
8465     -- Check 5: The sum of the distribution quantities/amounts should match the
8466     -- shipment quantity/amount.
8467 
8468     --<Complex Work R12>: moved the rollup checks into a separate
8469     -- subprocedure call
8470 	check_po_qty_amt_rollup(
8471 	   p_online_report_id => p_online_report_id
8472 	,  p_document_id => p_document_id
8473 	,  p_login_id => p_login_id
8474 	,  p_user_id => p_user_id
8475 	,  p_clm_document => l_clm_document -- CLM Mod
8476 	,  x_sequence => p_sequence --in out param
8477 	);
8478 
8479 -----------------------------------------------
8480 l_progress := '006';
8481 IF g_debug_stmt THEN
8482    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
8483      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
8484           || l_progress,'PO 6: Dist rate NULL' );
8485    END IF;
8486 END IF;
8487 
8488   -- Check 6:  The rate cannot be NULL for the distribution if we are
8489     -- using a foreign currency.  We are using a foreign currency
8490     -- if the po header currency code is not the same as the sets of
8491     -- books currency code.
8492     -- PO_SUB_DIST_RATE_NULL
8493 
8494   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_DIST_RATE_NULL');
8495   INSERT INTO po_online_report_text_gt(online_report_id,
8496         last_update_login,
8497         last_updated_by,
8498         last_update_date,
8499         created_by,
8500         creation_date,
8501         line_num,
8502         shipment_num,
8503         distribution_num,
8504         sequence,
8505         text_line,
8506                 message_name)
8507   SELECT  p_online_report_id,
8508         p_login_id,
8509         p_user_id,
8510           sysdate,
8511         p_user_id,
8512         sysdate,
8513             POL.line_num,
8514             PLL.shipment_num,
8515             POD.distribution_num,
8516         p_sequence + ROWNUM,
8517           substr(g_linemsg||g_delim||POL.line_num||g_delim
8518                    ||g_shipmsg||g_delim||PLL.shipment_num||g_delim
8519                    ||g_distmsg||g_delim||POD.distribution_num||g_delim
8520                    ||l_textline,1,240),
8521             'PO_SUB_DIST_RATE_NULL'
8522     FROM PO_DISTRIBUTIONS_GT POD,PO_LINE_LOCATIONS_GT PLL,PO_LINES_GT POL,
8523          PO_HEADERS_GT POH,GL_SETS_OF_BOOKS SOB,FINANCIALS_SYSTEM_PARAMETERS FSP
8524     WHERE POD.po_header_id = POH.po_header_id
8525     AND POD.line_location_id = PLL.line_location_id
8526     AND PLL.po_line_id = POL.po_line_id
8527     AND POH.po_header_id = p_document_id
8528     AND nvl(PLL.cancel_flag,'N') = 'N'
8529     AND nvl(POL.cancel_flag,'N') = 'N'
8530     AND nvl(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
8531     AND PLL.shipment_type in ('STANDARD', 'PLANNED')
8532     AND SOB.set_of_books_id = FSP.set_of_books_id
8533     AND SOB.currency_code <> POH.currency_code
8534     AND (POD.rate is null
8535         OR (POH.rate_type <> 'User'
8536             AND POD.rate_date is null));
8537 
8538 
8539      --Increment the p_sequence with number of errors reported in last query
8540     p_sequence := p_sequence + SQL%ROWCOUNT;
8541 --------------------------------------------
8542 
8543 l_progress := '007';
8544 IF g_debug_stmt THEN
8545    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
8546      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
8547           || l_progress,'PO 7: Dist rate NOT NULL');
8548    END IF;
8549 END IF;
8550 
8551   -- Check 7: If using functional currency then rate has to be null.
8552     -- PO_SUB_DIST_RATE_NOT_NULL
8553   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_DIST_RATE_NOT_NULL');
8554   INSERT INTO po_online_report_text_gt(online_report_id,
8555         last_update_login,
8556         last_updated_by,
8557         last_update_date,
8558         created_by,
8559         creation_date,
8560         line_num,
8561         shipment_num,
8562         distribution_num,
8563         sequence,
8564         text_line,
8565                 message_name)
8566   SELECT  p_online_report_id,
8567         p_login_id,
8568         p_user_id,
8569           sysdate,
8570         p_user_id,
8571         sysdate,
8572             POL.line_num,
8573             PLL.shipment_num,
8574             POD.distribution_num,
8575         p_sequence + ROWNUM,
8576           substr(g_linemsg||g_delim||POL.line_num||g_delim
8577                    ||g_shipmsg||g_delim||PLL.shipment_num||g_delim
8578                    ||g_distmsg||g_delim||POD.distribution_num||g_delim
8579                    ||l_textline,1,240),
8580             'PO_SUB_DIST_RATE_NOT_NULL'
8581     FROM PO_DISTRIBUTIONS_GT POD,PO_LINE_LOCATIONS_GT PLL,PO_LINES_GT POL,
8582          PO_HEADERS_GT POH,GL_SETS_OF_BOOKS SOB,FINANCIALS_SYSTEM_PARAMETERS FSP
8583     WHERE POD.po_header_id = POH.po_header_id
8584     AND POD.line_location_id = PLL.line_location_id
8585     AND PLL.po_line_id = POL.po_line_id
8586     AND POH.po_header_id = p_document_id
8587     AND nvl(PLL.cancel_flag,'N') = 'N'
8588     AND nvl(POL.cancel_flag,'N') = 'N'
8589     AND nvl(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
8590     AND PLL.shipment_type in ('STANDARD', 'PLANNED')
8591     AND SOB.set_of_books_id = FSP.set_of_books_id
8592     AND SOB.currency_code = POH.currency_code
8593     AND POD.rate is not null;
8594 
8595      --Increment the p_sequence with number of errors reported in last query
8596     p_sequence := p_sequence + SQL%ROWCOUNT;
8597 ----------------------------------------------------
8598 
8599 l_progress := '008';
8600 IF g_debug_stmt THEN
8601    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
8602      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
8603           || l_progress,'PO 8: Contract Amount exceed check');
8604    END IF;
8605 END IF;
8606 
8607   -- Check 8:  The amount of all standard purchase orders
8608     -- for a contract should not exceed the amount limit of the contract.
8609     -- PO_SUB_STD_CONTRACT_AMT_LIMIT
8610 
8611     -- bug3673292
8612     -- Take out val_contract_amount procedure call from the query.
8613     -- By doing that we can be sure that we call insert statement only when
8614     -- contract limit is exceeded.
8615 
8616     l_val_contract_limit := PO_CONTRACTS_S.val_contract_amount
8617                             ( x_po_header_id => p_document_id
8618                             );
8619 
8620     IF (l_val_contract_limit = 0) THEN
8621 
8622   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_STD_CONTRACT_AMT_LIMIT');
8623         INSERT INTO po_online_report_text_gt(online_report_id,
8624         last_update_login,
8625         last_updated_by,
8626         last_update_date,
8627         created_by,
8628         creation_date,
8629         line_num,
8630         shipment_num,
8631         distribution_num,
8632         sequence,
8633         text_line,
8634                 message_name)
8635   SELECT  p_online_report_id,
8636         p_login_id,
8637         p_user_id,
8638           sysdate,
8639         p_user_id,
8640         sysdate,
8641         0,
8642         0,
8643         0,
8644         p_sequence + ROWNUM,
8645         substr(l_textline,1,240),
8646             'PO_SUB_STD_CONTRACT_AMT_LIMIT'
8647         FROM DUAL;
8648 
8649         --Increment the p_sequence with number of errors reported in last query
8650         p_sequence := p_sequence + SQL%ROWCOUNT;
8651 
8652     END IF;
8653 
8654 
8655 
8656 -------------------------------------------------------------------------------
8657 
8658 l_progress := '009';
8659 IF g_debug_stmt THEN
8660    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
8661      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
8662           || l_progress,'PO 9: Ref to unapproved contract check ');
8663    END IF;
8664 END IF;
8665 
8666   -- Check 9: Any of the standard PO's lines should not references an
8667     -- unapproved contract.
8668     -- PO_SUB_REF_UNAPPROVED_CONTRACT
8669     --< Bug 3422733 > Only do this check if Contract is not ON HOLD. The ON HOLD
8670     -- check is done later. Avoids showing 2 msgs for Contract that is ON HOLD.
8671 
8672   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_REF_UNAPPROVED_CONTRACT');
8673   INSERT INTO po_online_report_text_gt(online_report_id,
8674         last_update_login,
8675         last_updated_by,
8676         last_update_date,
8677         created_by,
8678         creation_date,
8679         line_num,
8680         shipment_num,
8681         distribution_num,
8682         sequence,
8683         text_line,
8684                 message_name)
8685   SELECT  p_online_report_id,
8686         p_login_id,
8687         p_user_id,
8688           sysdate,
8689         p_user_id,
8690         sysdate,
8691         POL.line_num,
8692         0,
8693         0,
8694         p_sequence + ROWNUM,
8695         substr(g_linemsg||g_delim||POL.line_num||g_delim||l_textline,1,240),
8696             'PO_SUB_REF_UNAPPROVED_CONTRACT'
8697     FROM PO_LINES_GT POL,
8698          PO_HEADERS_ALL POC   -- <GC FPJ> : use all table
8699     WHERE POL.po_header_id = p_document_id
8700     AND   POL.contract_id = POC.po_header_id    -- <GC FPJ>
8701     AND   POC.type_lookup_code = 'CONTRACT'
8702   /* R12 GCPA
8703   + If Profile ALLOW_REFERENCING_CPA_UNDER_AMENDMENT is Y, then we can refer any Contract Which is approved Once
8704   + Else Contract should be in APPROVED state  */
8705   AND    ( (NVL(FND_PROFILE.VALUE('ALLOW_REFERENCING_CPA_UNDER_AMENDMENT'),'N') = 'Y'
8706            AND POC.Approved_Date Is Null
8707 	    )
8708 	 or (  NVL(FND_PROFILE.VALUE('ALLOW_REFERENCING_CPA_UNDER_AMENDMENT'),'N') = 'N'
8709           and nvl(POC.APPROVED_FLAG,'N') <> 'Y')
8710 	 )
8711     AND   NVL(POC.user_hold_flag, 'N') <> 'Y';      --< Bug 3422733 >
8712 
8713 
8714     --Increment the p_sequence with number of errors reported in last query
8715     p_sequence := p_sequence + SQL%ROWCOUNT;
8716 -------------------------------------------------------------------------------
8717 
8718 l_progress := '010';
8719 IF g_debug_stmt THEN
8720    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
8721      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
8722           || l_progress,'PO 10: Ref contract diff vendor');
8723    END IF;
8724 END IF;
8725 
8726   -- Check 10: Any of the standard PO's lines should not reference a
8727     -- contract whose vendor is different than the one on PO header.
8728     -- PO_SUB_LINE_CONTRACT_MISMATCH
8729     --< CLM Novation > bug13059027 - This check is a Warning for OrderOfIDV Modifications
8730     -- or Re-approval Base docs whose Supplier is different from that of referenced IDV
8731   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_LINE_CONTRACT_MISMATCH');
8732   INSERT INTO po_online_report_text_gt(online_report_id,
8733         last_update_login,
8734         last_updated_by,
8735         last_update_date,
8736         created_by,
8737         creation_date,
8738         line_num,
8739         shipment_num,
8740         distribution_num,
8741         sequence,
8742         text_line,
8743         message_name,
8744         message_type)
8745   SELECT  p_online_report_id,
8746         p_login_id,
8747         p_user_id,
8748           sysdate,
8749         p_user_id,
8750         sysdate,
8751         POL.line_num,
8752         0,
8753         0,
8754         p_sequence + ROWNUM,
8755         substr(g_linemsg||g_delim||POL.line_num||g_delim||l_textline,1,240),
8756         'PO_SUB_LINE_CONTRACT_MISMATCH',
8757         Decode(is_approved_clm_document(p_document_id), 'Y', 'W', NULL)
8758     FROM PO_LINES_GT POL,
8759          PO_HEADERS_ALL POC,  -- <GC FPJ>: Use _ALL table
8760          PO_HEADERS_GT POH
8761     WHERE POH.po_header_id = p_document_id
8762     AND   POL.po_header_id = POH.po_header_id
8763     AND   POL.contract_id = POC.po_header_id  -- <GC FPJ>
8764     AND   POC.type_lookup_code = 'CONTRACT'
8765     AND   nvl(POC.cancel_flag , 'N') = 'N'
8766     AND   POC.vendor_id <> POH.vendor_id;
8767 
8768      --Increment the p_sequence with number of errors reported in last query
8769     p_sequence := p_sequence + SQL%ROWCOUNT;
8770 -----------------------------------
8771 
8772 l_progress := '600';
8773 
8774 -- Check 11: The PO GL date should be within an open purchasing period
8775 -- PO_SUB_PO_INVALID_GL_DATE
8776 
8777 --<FPJ ENCUMBRANCE>
8778 
8779 IF (  PO_CORE_S.is_encumbrance_on(
8780          p_doc_type => g_document_type_PO
8781       ,  p_org_id => NULL
8782       )
8783    )
8784 THEN
8785 
8786    l_progress := '610';
8787    IF g_debug_stmt THEN
8788       PO_DEBUG.debug_stmt(g_log_head || '.'||l_api_name||'.',
8789                           l_progress,'PO 11: GL date');
8790    END IF;
8791 
8792    check_gl_date(
8793       p_doc_type => g_document_type_PO
8794    ,  p_online_report_id => p_online_report_id
8795    ,  p_login_id => p_login_id
8796    ,  p_user_id => p_user_id
8797    ,  p_sequence => p_sequence
8798    );
8799 
8800    l_progress := '620';
8801 
8802 ELSE
8803    l_progress := '630';
8804    IF g_debug_stmt THEN
8805       PO_DEBUG.debug_stmt(g_log_head || '.'||l_api_name||'.',
8806                           l_progress,'PO 11: PO encumbrance not on');
8807    END IF;
8808 END IF;
8809 
8810 ---------------------------------------
8811 
8812 l_progress := '012';
8813 IF g_debug_stmt THEN
8814    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
8815      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
8816           || l_progress,'PO 12: UOM Interclass coversions');
8817    END IF;
8818 END IF;
8819 
8820     -- Check 12: Invalid Interclass conversions between UOMs should not be allowed
8821     -- PO_SUB_UOM_CLASS_CONVERSION, PO_SUB_PO_INVALID_CLASS_CONV
8822     -- Message inserted is:
8823     --'Line# <LineNum> Following Interclass UOM conversion is not defined or
8824     -- is disabled <UOM1> <UOM2>'
8825     -- Bug #1630662
8826   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_UOM_CLASS_CONVERSION');
8827     INSERT INTO po_online_report_text_gt(online_report_id,
8828         last_update_login,
8829         last_updated_by,
8830         last_update_date,
8831         created_by,
8832         creation_date,
8833         line_num,
8834         shipment_num,
8835         distribution_num,
8836         sequence,
8837         text_line,
8838                 message_name)
8839   SELECT  /*+ FULL(POL) */  -- bug3413891
8840                  p_online_report_id,
8841         p_login_id,
8842         p_user_id,
8843           sysdate,
8844         p_user_id,
8845         sysdate,
8846             POL.line_num,
8847             0,
8848             0,
8849             p_sequence + ROWNUM,
8850             substr(g_linemsg||g_delim||POL.line_num||g_delim||l_textline||
8851                    MTL1.uom_class||' , '||MTL2.uom_class,1,240),
8852             'PO_SUB_UOM_CLASS_CONVERSION'
8853     FROM MTL_UOM_CLASS_CONVERSIONS MOU, PO_LINES_GT POL,
8854          MTL_UOM_CLASSES_TL MTL1, MTL_UOM_CLASSES_TL MTL2
8855     WHERE POL.item_id = MOU.inventory_item_id
8856     AND   (nvl(MOU.disable_date, TRUNC(SYSDATE))+1) < TRUNC(SYSDATE)
8857     AND   POL.po_header_id = p_document_id
8858     AND   MOU.from_uom_class = MTL1.uom_class
8859     AND   MOU.to_uom_class = MTL2.uom_class
8860     AND EXISTS
8861        (SELECT 'uom conversion exists'
8862         FROM MTL_UNITS_OF_MEASURE MUM
8863         WHERE POL.unit_meas_lookup_code = MUM.unit_of_measure
8864         AND   MOU.to_uom_class = MUM.uom_class);
8865 
8866     --Increment the p_sequence with number of errors reported in last query
8867     p_sequence := p_sequence + SQL%ROWCOUNT;
8868 --------------------------------------------------
8869 
8870 l_progress := '013';
8871 IF g_debug_stmt THEN
8872    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
8873      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
8874           || l_progress,'PO 13: Item restricyed');
8875    END IF;
8876 END IF;
8877 
8878   -- Check 13: If an item is restricted then the Purchase Order Vendor
8879     -- must be listed in the Approved Suppliers List table and must be approved.
8880     -- PO_SUB_ITEM_NOT_APPROVED
8881     -- History:
8882     /*
8883     ** Determine if an item is restricted.  If it is restricted the
8884     ** Purchase Order Vendor must be listed in the Approved Suppliers
8885     ** List table and must be approved.
8886     ** Bug 918932 sugupta
8887     ** Submission check for Blanket PO will follow the following ASL rules when
8888     **Use approved supplier flag is checked ON:
8889     ** 1) Global ASL's  with non-debarred supplier will allow approval all times.
8890     ** 2) Global ASL's with debarred supplier will disallow approval at all times.
8891     ** 3) If Global ASL is absent, any Local ASL with approved supplier will allow
8892     ** approval
8893     ** 4) If Global ASL is absent and all local ASL's have debarred supplier, approval
8894     **will fail.
8895     */
8896     /* Bug# 1109001: kagarwal
8897     ** Split the ASL check to ASL check for Blanket and PO.
8898     ** Also modified the query for performance.
8899     ** UNION is replaced with UNION ALL in all ASL checks
8900     ** In PO_SUB_ITEM_NOT_APPROVED the Sum clause has also been modified
8901     ** PO_SUB_ITEM_NOT_APPROVED for Standard PO
8902     ** PA_SUB_ITEM_NOT_APPROVED for Blanket/Contract PO
8903     */
8904     /* Bug# 1570115:          While checking for Restricted items the query was
8905     ** checking with FSP.inventory_organization_id to instead of checking with the
8906     ** PLL.SHIP_TO_ORGANIZATION_ID and not allowing the user to approve the
8907     ** document though it is not restricted in the ship to org which user has
8908     ** entered.  Changed the FSP.inventory_organization_id to
8909     ** PLL.SHIP_TO_ORGANIZATION_ID the query which check for PO.
8910     */
8911     /* Bug# 1761513: kagarwal
8912     ** Desc: Changed the ASL Rules:
8913     ** 1. If the ASL is defined at the commodity level and also at the item level,
8914     ** the item level ASL will be considered. But if the ASL is not defined at the
8915     ** item level then the ASL at the commodity level will be considered for that
8916     ** item.
8917     **
8918     ** Eg i): If A, B and C are approved suppliers at the commodity level and A and D are
8919     ** the approved suppliera at the item level, then A and D will be considered as
8920     ** approved suppliers for this item. Hence a PO for this item will get approved
8921     ** for Suppliers A and D but not for suppliers B and C.
8922     **
8923     ** Eg ii): If A, B and C are approved suppliers at the commodity level and there is
8924     ** no ASL defined at the item level, then A, B and C will be considered as approved
8925     ** supplier for this item. Hence a PO for this item will get approved for
8926     ** Suppliers A, B and C.
8927     **
8928     ** 2. If a Supplier is debarred at the commodity level even if it is approved
8929     ** supplier at the item level then this supplier will be considered debarred for
8930     ** this item.
8931     */
8932      /*Bug5597639 Modifying the below sql to ensure that whenever the item
8933      is restricted by checking 'Use approved supplier list' there should be
8934      atlease one approved ASL either at item level or at category level
8935      if there is no item ASL*/
8936 
8937   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_ITEM_NOT_APPROVED');
8938   INSERT INTO po_online_report_text_gt(online_report_id,
8939         last_update_login,
8940         last_updated_by,
8941         last_update_date,
8942         created_by,
8943         creation_date,
8944         line_num,
8945         shipment_num,
8946         distribution_num,
8947         sequence,
8948         text_line,
8949                 message_name)
8950   SELECT  p_online_report_id,
8951         p_login_id,
8952         p_user_id,
8953           sysdate,
8954         p_user_id,
8955         sysdate,
8956         POL.line_num,
8957         PLL.shipment_num,
8958         0,
8959         p_sequence + ROWNUM,
8960         substr(g_linemsg||g_delim||POL.line_num||g_delim||
8961                    g_shipmsg||g_delim||PLL.shipment_num||g_delim
8962                    ||l_textline,1,240),
8963             'PO_SUB_ITEM_NOT_APPROVED'
8964     FROM MTL_SYSTEM_ITEMS MSI, PO_LINE_LOCATIONS_GT PLL,
8965          PO_LINES_GT POL, PO_HEADERS_GT POH,
8966          FINANCIALS_SYSTEM_PARAMETERS FSP
8967     WHERE POH.po_header_id = p_document_id
8968     AND POH.po_header_id = POL.po_header_id
8969     AND PLL.po_line_id(+) = POL.po_line_id
8970     AND PLL.po_release_id IS NULL
8971     AND MSI.organization_id = PLL.ship_to_organization_id
8972     AND MSI.inventory_item_id = POL.item_id
8973     AND POL.item_id is not null
8974     AND nvl(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
8975     AND nvl(POL.cancel_flag,'N') = 'N'
8976     AND nvl(PLL.cancel_flag,'N') = 'N'
8977     AND nvl(MSI.must_use_approved_vendor_flag,'N') = 'Y'
8978     AND NOT exists
8979       (SELECT 1
8980         FROM PO_APPROVED_SUPPLIER_LIS_VAL_V ASL, PO_ASL_STATUS_RULES ASR
8981         WHERE  ASL.using_organization_id in (PLL.ship_to_organization_id, -1)
8982         AND    ASL.vendor_id = POH.vendor_id
8983         --Bug 16573818: Added the 'OR' clause below to match the vendor site code
8984 	--for cases in which the vendor site IDs are different, but code is same.
8985         AND    (nvl(ASL.vendor_site_id, POH.vendor_site_id) = POH.vendor_site_id
8986 		OR EXISTS (  SELECT  'vendor site code matches ASL'
8987                        FROM  po_vendor_sites_all pvs1, po_vendor_sites_all pvs2
8988                        WHERE  pvs1. vendor_site_id = ASL.vendor_site_id
8989                        and pvs2.vendor_site_id = POH.vendor_site_id
8990                        and pvs1.vendor_site_code = pvs2.vendor_site_code) )
8991         AND  ASL.item_id = POL.item_id
8992         AND    ASL.asl_status_id = ASR.status_id
8993         AND    ASR.business_rule = '1_PO_APPROVAL'
8994 	AND   ASR.allow_action_flag = 'Y'    --Bug5597639
8995         UNION ALL
8996        SELECT  1
8997         FROM PO_APPROVED_SUPPLIER_LIS_VAL_V ASL, PO_ASL_STATUS_RULES ASR
8998         WHERE  ASL.using_organization_id in (PLL.ship_to_organization_id, -1)
8999         AND    ASL.vendor_id = POH.vendor_id
9000         AND    nvl(ASL.vendor_site_id, POH.vendor_site_id) = POH.vendor_site_id
9001         AND    ASL.item_id is NULL
9002         AND    not exists
9003            (SELECT ASL1.ASL_ID
9004             FROM PO_APPROVED_SUPPLIER_LIS_VAL_V ASL1
9005             WHERE ASL1.ITEM_ID = POL.item_id
9006             AND ASL1.using_organization_id in
9007                 (PLL.ship_to_organization_id, -1))
9008         AND    ASL.category_id in
9009            (SELECT MIC.category_id
9010             FROM   MTL_ITEM_CATEGORIES MIC
9011             WHERE MIC.inventory_item_id = POL.item_id
9012             AND MIC.organization_id = PLL.ship_to_organization_id)
9013         AND    ASL.asl_status_id = ASR.status_id
9014         AND    ASR.business_rule = '1_PO_APPROVAL'
9015         AND   ASR.allow_action_flag = 'Y');    --Bug5597639
9016 
9017      --Increment the p_sequence with number of errors reported in last query
9018     p_sequence := p_sequence + SQL%ROWCOUNT;
9019 
9020 -------------------------------------------------------------------------------
9021 
9022 l_progress := '014';
9023 IF g_debug_stmt THEN
9024    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9025      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
9026           || l_progress,'PO 14: ASL Debarred');
9027    END IF;
9028 END IF;
9029 
9030     -- Check 14: If an item is restricted then the Purchase Order Vendor
9031     -- must be listed in the Approved Suppliers List table and must not be
9032     -- DEBARRED.
9033     -- PO_SUB_ITEM_ASL_DEBARRED
9034 
9035    /*Bug5597639 This check would throw an error message if atleast one ASL
9036     entry is debarred either for item /Category irrespective of 'Use approved
9037     supplier flag'. This check would apply even for one time items.
9038     If supplier is debarred in any of the ASL item/category (Global/Local)
9039     (Suplier/Supplier+site) then the approval of the PO will not be allowed */
9040 
9041 l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_ITEM_ASL_DEBARRED');
9042   INSERT INTO po_online_report_text_gt(online_report_id,
9043         last_update_login,
9044         last_updated_by,
9045         last_update_date,
9046         created_by,
9047         creation_date,
9048         line_num,
9049         shipment_num,
9050         distribution_num,
9051         sequence,
9052         text_line,
9053                 message_name)
9054   SELECT  p_online_report_id,
9055         p_login_id,
9056         p_user_id,
9057           sysdate,
9058         p_user_id,
9059         sysdate,
9060         POL.line_num,
9061         PLL.shipment_num,
9062         0,
9063         p_sequence + ROWNUM,
9064         substr(g_linemsg||g_delim||POL.line_num||g_delim
9065                    ||g_shipmsg||g_delim||PLL.shipment_num||g_delim
9066                    ||l_textline,1,240),
9067             'PO_SUB_ITEM_ASL_DEBARRED'
9068      FROM PO_LINE_LOCATIONS_GT PLL,
9069          PO_LINES_GT POL, PO_HEADERS_GT POH,
9070          FINANCIALS_SYSTEM_PARAMETERS FSP
9071     WHERE POH.po_header_id = p_document_id
9072     AND POH.po_header_id = POL.po_header_id
9073     AND PLL.po_line_id(+) = POL.po_line_id
9074     AND PLL.po_release_id IS NULL
9075     AND nvl(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
9076     AND nvl(POL.cancel_flag,'N') = 'N'
9077     AND nvl(PLL.cancel_flag,'N') = 'N'
9078     AND exists
9079        (SELECT 1
9080         FROM PO_APPROVED_SUPPLIER_LIS_VAL_V ASL, PO_ASL_STATUS_RULES ASR,
9081 	MTL_SYSTEM_ITEMS MSI  --Bug5597639
9082         WHERE  ASL.using_organization_id in (PLL.ship_to_organization_id, -1)
9083 	/*Bug5597639 Adding the below three conditions */
9084 	AND MSI.organization_id = FSP.inventory_organization_id
9085 	AND MSI.inventory_item_id = POL.item_id
9086 	AND POL.item_id is not null
9087         AND    ASL.vendor_id = POH.vendor_id
9088         AND    nvl(ASL.vendor_site_id, POH.vendor_site_id) = POH.vendor_site_id
9089         AND  ASL.item_id = POL.item_id
9090         AND    ASL.asl_status_id = ASR.status_id
9091         AND    ASR.business_rule = '1_PO_APPROVAL'
9092         AND    ASR.allow_action_flag <> 'Y' -- Bug 5724696
9093         UNION ALL
9094         SELECT 1
9095         FROM PO_APPROVED_SUPPLIER_LIS_VAL_V ASL, PO_ASL_STATUS_RULES ASR
9096         WHERE  ASL.using_organization_id in (PLL.ship_to_organization_id, -1)
9097         AND    ASL.vendor_id = POH.vendor_id
9098         AND    nvl(ASL.vendor_site_id, POH.vendor_site_id) = POH.vendor_site_id
9099         AND    ASL.item_id is NULL
9100 	AND  POL.category_id = ASL.category_id  --Bug5597639
9101         AND    ASL.asl_status_id = ASR.status_id
9102         AND    ASR.business_rule = '1_PO_APPROVAL'
9103         AND    ASR.allow_action_flag <> 'Y' );  --Bug5597639
9104 
9105      --Increment the p_sequence with number of errors reported in last query
9106     p_sequence := p_sequence + SQL%ROWCOUNT;
9107 
9108 
9109 ----------------------------------------------------------------------------
9110 
9111 -- <GC FPJ START>
9112 
9113     l_progress := '015';
9114 
9115     IF g_debug_stmt THEN
9116         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9117           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
9118                        l_api_name||'.' || l_progress,
9119                        'PO 15: Contract ref on hold');
9120         END IF;
9121     END IF;
9122 
9123     -- Check 15: Contract referenced on a PO line should not be on hold
9124     --           (If contract is in incomplete status)
9125 
9126     l_textline := FND_MESSAGE.get_string('PO', 'PO_SUB_LINE_CONTRACT_HOLD');
9127 
9128     INSERT INTO po_online_report_text_gt(
9129        online_report_id,
9130        last_update_login,
9131        last_updated_by,
9132        last_update_date,
9133        created_by,
9134        creation_date,
9135        line_num,
9136        shipment_num,
9137        distribution_num,
9138        sequence,
9139        text_line,
9140        message_name
9141     )
9142     SELECT p_online_report_id,
9143            p_login_id,
9144            p_user_id,
9145            sysdate,
9146            p_user_id,
9147            sysdate,
9148            POL.line_num,
9149            0,
9150            0,
9151            p_sequence + ROWNUM,
9152            SUBSTR (g_linemsg || g_delim || POL.line_num ||g_delim ||
9153                    l_textline,1,240),
9154            'PO_SUB_LINE_CONTRACT_HOLD'
9155     FROM   po_lines_gt POL,
9156            po_headers_gt POH,
9157            po_headers_all POC
9158     WHERE  POH.po_header_id = p_document_id
9159     AND    NVL(POH.authorization_status, 'INCOMPLETE') = 'INCOMPLETE'
9160     AND    POL.po_header_id = POH.po_header_id
9161     AND    POC.po_header_id = POL.contract_id
9162     AND    POC.user_hold_flag = 'Y';
9163 
9164     p_sequence := p_sequence + SQL%ROWCOUNT;
9165 
9166 -----------------------------------------------------------------------------
9167 --Bug 5525381
9168 /* Commented the validation of checking for the contract start date.
9169     l_progress := '016';
9170 
9171     IF g_debug_stmt THEN
9172         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9173           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
9174                        l_api_name||'.' || l_progress,
9175                        'PO 16: Contract ref effective dates');
9176         END IF;
9177     END IF;
9178 
9179     -- Check 16: Creation date of the contract should be within the effective
9180     --           dates of the contracts referenced
9181 
9182     l_textline := FND_MESSAGE.get_string('PO', 'PO_SUB_LINE_CONTRACT_EXP');
9183 
9184     INSERT INTO po_online_report_text_gt(
9185        online_report_id,
9186        last_update_login,
9187        last_updated_by,
9188        last_update_date,
9189        created_by,
9190        creation_date,
9191        line_num,
9192        shipment_num,
9193        distribution_num,
9194        sequence,
9195        text_line,
9196        message_name
9197     )
9198     SELECT p_online_report_id,
9199            p_login_id,
9200            p_user_id,
9201            sysdate,
9202            p_user_id,
9203            sysdate,
9204            POL.line_num,
9205            0,
9206            0,
9207            p_sequence + ROWNUM,
9208            SUBSTR (g_linemsg || g_delim || POL.line_num ||g_delim ||
9209                    l_textline,1,240),
9210            'PO_SUB_LINE_CONTRACT_EXP'
9211     FROM   po_lines_gt POL,
9212            po_headers_gt POH,
9213            po_headers_all POC
9214     WHERE  POH.po_header_id = p_document_id
9215     AND    NVL(POH.authorization_status, 'INCOMPLETE') = 'INCOMPLETE'
9216     AND    POL.po_header_id = POH.po_header_id
9217     AND    POC.po_header_id = POL.contract_id
9218     AND    TRUNC(POL.creation_date) NOT BETWEEN NVL(TRUNC(POC.start_date),
9219                                                     POL.creation_date-1)
9220                                         AND     NVL(TRUNC(POC.end_date),
9221                                                     POL.creation_date+1);
9222 
9223     p_sequence := p_sequence + SQL%ROWCOUNT;*/
9224 
9225 -----------------------------------------------------------------------------
9226  l_progress := '017';
9227 
9228     IF g_debug_stmt THEN
9229         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9230           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
9231                        l_api_name||'.' || l_progress,
9232                        'PO 17: ATO/CTO Model items not allowed on POs');
9233         END IF;
9234     END IF;
9235 
9236     -- Check 17: ATO/CTO Model items not allowed on PO's (Bug 3362369)
9237 
9238     l_textline := FND_MESSAGE.get_string('PO', 'PO_ATO_ITEM_NA');
9239     --Bug10064616<BEGIN>
9240     INSERT INTO po_online_report_text_gt(
9241        online_report_id,
9242        last_update_login,
9243        last_updated_by,
9244        last_update_date,
9245        created_by,
9246        creation_date,
9247        line_num,
9248        shipment_num,
9249        distribution_num,
9250        sequence,
9251        text_line,
9252        message_name
9253     )
9254     SELECT p_online_report_id,
9255            p_login_id,
9256            p_user_id,
9257            sysdate,
9258            p_user_id,
9259            sysdate,
9260            POL.line_num,
9261            0,
9262            0,
9263            p_sequence + ROWNUM,
9264            SUBSTR (g_linemsg || g_delim || POL.line_num ||g_delim ||
9265                    l_textline,1,240),
9266            'PO_ATO_ITEM_NA'
9267     FROM   po_lines_gt POL,
9268            po_headers_gt POH,
9269            financials_system_params_all FSP,
9270            mtl_system_items MSI
9271     WHERE  fsp.org_id = poh.org_id
9272     AND    POH.po_header_id = p_document_id
9273     AND    POL.po_header_id = POH.po_header_id
9274     AND    POL.item_id is not null
9275     AND    nvl(POL.cancel_flag, 'N') = 'N'                   --5353423
9276     AND    nvl(POL.closed_code, 'OPEN') <> 'FINALLY CLOSED'  --5353423
9277     AND    POL.item_id = MSI.inventory_item_id
9278     AND    MSI.organization_id = FSP.inventory_organization_id
9279     AND    MSI.bom_item_type in (1,2);
9280     --Bug10064616<END>
9281     p_sequence := p_sequence + SQL%ROWCOUNT;
9282 
9283 -----------------------------------------------------------------------------
9284 
9285 
9286 /*Start Bug #3512688 */
9287       /* Check 18 : To check the validity of the item at line level for newly added  line */
9288       l_progress := '018';
9289       IF g_debug_stmt  THEN
9290       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9291         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
9292       l_api_name||'.' || l_progress,
9293       'PO 18: Non Purchasable Item is not allowed');
9294       END IF;
9295       END IF;
9296       l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_ALL_NO_ITEM');
9297         --Bug10064616<BEGIN>
9298       INSERT INTO po_online_report_text_gt(online_report_id,
9299       last_update_login,
9300       last_updated_by,
9301       last_update_date,
9302       created_by,
9303       creation_date,
9304       line_num,
9305       shipment_num,
9306       distribution_num,
9307       sequence,
9308       text_line,
9309       message_name)
9310       SELECT  p_online_report_id,
9311       p_login_id,
9312       p_user_id,
9313 
9314      sysdate,
9315       p_user_id,
9316       sysdate,
9317       pl.line_num,
9318       0,
9319       0,
9320       p_sequence + ROWNUM,
9321        substr(g_linemsg||g_delim||pl.line_num||g_delim||l_textline,1,240),
9322       'PO_ALL_NO_ITEM'
9323       from po_headers_gt ph, po_lines_gt pl,mtl_system_items  itm,financials_system_params_all fsp,po_line_types_b plt
9324       where   fsp.org_id = ph.org_id
9325       and itm.inventory_item_id  = pl.item_id
9326       and   pl.item_id is not null
9327       and   itm.organization_id    = fsp.inventory_organization_id
9328       and   itm.purchasing_enabled_flag = 'N'
9329       and   ph.po_header_id = p_document_id
9330       and   pl.po_header_id = ph.po_header_id
9331       and   pl.line_type_id = plt.line_type_id
9332       and   nvl(plt.outside_operation_flag,'N') =  nvl(itm.outside_operation_flag,'N')
9333       and   (pl.creation_date >= nvl(ph.approved_date ,pl.creation_date));
9334         --Bug10064616<END>
9335       --Increment the p_sequence with number of errors reported in last query
9336       p_sequence := p_sequence + SQL%ROWCOUNT;
9337 
9338       -----------------------5601    /* Check 19 : To check the validity of the item at shipment level for newly added  line */
9339       l_progress := '019';
9340       IF g_debug_stmt THEN
9341       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9342         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
9343      l_api_name||'.' || l_progress,
9344       'PO 19: Non Purchasable Item is not allowed');     END IF;
9345       END IF;
9346       l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_ALL_NO_ITEM');
9347       INSERT INTO po_online_report_text_gt(online_report_id,
9348       last_update_login,
9349       last_updated_by,
9350       last_update_date,
9351       created_by,
9352       creation_date,
9353       line_num,
9354       shipment_num,
9355       distribution_num,
9356       sequence,
9357       text_line,
9358       message_name)
9359       SELECT  p_online_report_id,
9360       p_login_id,
9361       p_user_id,
9362       sysdate,
9363       p_user_id,
9364       sysdate,
9365       pl.line_num,
9366       pll.shipment_num,
9367       0,
9368       p_sequence + ROWNUM,
9369       substr(g_linemsg||g_delim||pl.line_num||g_delim||
9370        g_shipmsg||g_delim||pll.shipment_num||g_delim||l_textline,1,240),
9371       'PO_ALL_NO_ITEM'
9372       from po_headers_gt ph,po_lines_gt pl,po_line_locations_gt  pll,mtl_system_items itm,po_line_types_b plt
9373     where itm.inventory_item_id  = pl.item_id
9374     and   pl.item_id is not null
9375       and   itm.organization_id    = pll.ship_to_organization_id
9376       and   itm.purchasing_enabled_flag = 'N'
9377       and   pl.po_line_id   = pll.po_line_id
9378       and   ph.po_header_id = pll.po_header_id
9379       and   ph.po_header_id = p_document_id
9380       and   pl.po_header_id = ph.po_header_id
9381       and   pl.line_type_id = plt.line_type_id
9382       and   nvl(plt.outside_operation_flag,'N') =  nvl(itm.outside_operation_flag,'N')
9383       and   (pl.creation_date >= nvl(ph.approved_date,pl.creation_date))
9384       and   pll.shipment_type <> 'PREPAYMENT' --<Complex Work R12>
9385       and   pll.po_release_id is null;
9386 
9387 
9388       --Increment the p_sequence with number of errors reported in last query
9389       p_sequence := p_sequence + SQL%ROWCOUNT;
9390 
9391 --End Bug #3512688
9392 ------------------------------------------------------------------------------------------
9393 
9394  l_progress := '020';
9395 
9396     IF g_debug_stmt THEN
9397         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9398           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
9399                        l_api_name||'.' || l_progress,
9400                        'PO 20: Either Promised/Need by date required for planned items');
9401         END IF;
9402     END IF;
9403 
9404     -- Check 20: Either Promised Date or Need by date is required for planned
9405     -- items (ECO 4503425 for HTML Orders R12)
9406 
9407     l_textline := FND_MESSAGE.get_string('PO', 'PO_PO_PLANNED_ITEM_DATE_REQ');
9408 
9409     INSERT INTO po_online_report_text_gt(
9410        online_report_id,
9411        last_update_login,
9412        last_updated_by,
9413        last_update_date,
9414        created_by,
9415        creation_date,
9416        line_num,
9417        shipment_num,
9418        distribution_num,
9419        sequence,
9420        text_line,
9421        message_name
9422     )
9423     SELECT p_online_report_id,
9424            p_login_id,
9425            p_user_id,
9426            sysdate,
9427            p_user_id,
9428            sysdate,
9429            POL.line_num,
9430            PLL.shipment_num,
9431            0,
9432            p_sequence + ROWNUM,
9433            SUBSTR (g_linemsg || g_delim || POL.line_num ||g_delim ||
9434              g_shipmsg || g_delim || PLL.shipment_num ||g_delim || l_textline,1,240),
9435            'PO_PO_PLANNED_ITEM_DATE_REQ'
9436     FROM   po_lines_gt POL,
9437            po_headers_gt POH,
9438            po_line_locations_gt PLL,
9439            financials_system_parameters FSP,
9440            mtl_system_items MSI
9441     WHERE  POH.po_header_id = p_document_id
9442     AND    POL.po_header_id = POH.po_header_id
9443     AND    POL.po_line_id = PLL.po_line_id
9444     AND    PLL.po_header_id = POH.po_header_id
9445     AND    POL.item_id is not null
9446     AND    PLL.need_by_date is null
9447     AND    PLL.promised_date is null
9448     AND    PLL.shipment_type <> 'PREPAYMENT' --bug 4997671 <Complex Work R12>
9449     AND    POL.item_id = MSI.inventory_item_id
9450     AND    nvl(POL.cancel_flag, 'N') = 'N'                   --8518511 including the cancel condition for the planned item data check
9451     AND    nvl(POL.closed_code, 'OPEN') <> 'FINALLY CLOSED'  --8518511
9452     AND    MSI.organization_id = FSP.inventory_organization_id
9453     AND    (MSI.mrp_planning_code IN (3,4,7,8,9) OR
9454             MSI.inventory_planning_code IN (1,2) );
9455 
9456     p_sequence := p_sequence + SQL%ROWCOUNT;
9457 
9458 
9459 -----------------------------------------------
9460 --Bug5075191
9461 --Following submission check should exclude cancelled/finally closed lines.
9462 --Adding those conditions.
9463 --bug#3987438
9464 --Added a new submission check to validate the
9465 --ship to location at the shipment level.
9466 --Bug#12396691 (12597960)
9467 --Modified the closed_code condition. The shipments which are in OPEN or
9468 -- Requires ReApproval State should be considered to validate the ship to
9469 --location
9470 
9471 IF g_debug_stmt THEN
9472    FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
9473           || l_progress,'PO : Invalid Ship To Locations');
9474 END IF;
9475 
9476     SELECT  pol.line_num,
9477             poll.shipment_num,
9478             hlat.location_code
9479     BULK COLLECT INTO
9480             l_line_num,
9481             l_shipment_num,
9482             l_ship_to_location_tbl
9483     FROM
9484             po_lines_gt pol,
9485             po_line_locations_gt poll,
9486             hr_locations_all hla,
9487             hr_locations_all_tl hlat
9488     WHERE poll.po_header_id=p_document_id
9489     and pol.po_Header_id=p_document_id
9490     and pol.po_line_id = poll.po_line_id
9491     and poll.ship_to_location_id = hla.location_id
9492     and nvl(poll.cancel_flag,'N') = 'N'
9493        --and nvl(poll.closed_code,'OPEN') <> 'FINALLY CLOSED'--Bug#12396691
9494     and (nvl(poll.closed_code,'OPEN') = 'OPEN' OR NVL(poll.APPROVED_FLAG,'N') = 'R')--Bug#12396691
9495     and hla.location_id = hlat.location_id
9496     and nvl (trunc (hla.inactive_date), trunc (sysdate)+1 )<= trunc (sysdate)
9497     and hlat.language=userenv('lang');
9498 
9499     FOR i IN 1..l_line_num.COUNT LOOP
9500         l_rowCount(i) := i;
9501     END LOOP;
9502 
9503     FORALL i IN 1..l_line_num.COUNT
9504     INSERT INTO po_online_report_text_gt (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     VALUES(
9517             p_online_report_id,
9518             p_login_id,
9519             p_user_id,
9520             sysdate,
9521             p_user_id,
9522             sysdate,
9523             l_line_num(i),
9524             l_shipment_num(i),
9525             NULL,                                             -- <SERVICES FPJ>
9526             p_sequence+l_rowCount(i),
9527             PO_CORE_S.get_translated_text
9528                 (   'PO_SUB_SHIPTO_LOC_INVALID'
9529                 ,   'LINE_NUM', l_line_num(i)
9530                 ,   'SHIPMENT_NUM', l_shipment_num(i)
9531                 ,   'SHIP_TO_LOC', l_ship_to_location_tbl(i)
9532                 ),
9533             'PO_SUB_PO_SHIPTO_LOC_INVALID'
9534         );
9535 
9536     p_sequence := p_sequence + l_line_num.COUNT;
9537 
9538 --bug#3987438 END
9539 
9540 -----------------------------------------------------------------------------
9541 
9542     l_progress := '021';
9543 --<R12 eTax Integration Start>
9544 -----------------------------------------------------------------------------
9545 
9546     l_tax_status := po_tax_interface_pvt.calculate_tax_yes_no(p_po_header_id    => p_document_id,
9547                                                               p_po_release_id   => NULL,
9548                                                               p_req_header_id   => NULL);
9549     l_progress := '022';
9550     IF g_debug_stmt THEN
9551         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9552           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head ||l_api_name||'.' ||
9553                         l_progress, 'PO 21: Recalculate tax before approval = ' || l_tax_status);
9554         END IF;
9555     END IF;
9556 
9557     IF l_tax_status = 'Y' THEN
9558       IF g_debug_stmt THEN
9559           IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9560             FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head ||
9561                          l_api_name||'.' || l_progress,
9562                          'PO 22: Calculate tax as the current one is not correct');
9563           END IF;
9564       END IF;
9565       l_progress := '023';
9566       po_tax_interface_pvt.calculate_tax( x_return_status    => l_return_status,
9567                                           p_po_header_id     => p_document_id,
9568                                           p_po_release_id    => NULL,
9569                                           p_calling_program  => g_action_DOC_SUBMISSION_CHECK);
9570 
9571       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
9572         IF g_debug_stmt THEN
9573              IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9574                FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head ||
9575                             l_api_name||'.' || l_progress,
9576                             'PO 23: Calculate tax has errored out');
9577              END IF;
9578          END IF;
9579          l_tax_message := fnd_message.get_string('PO','PO_TAX_CALCULATION')||' : ' ;
9580          FOR i IN 1..po_tax_interface_pvt.G_TAX_ERRORS_TBL.MESSAGE_TEXT.COUNT
9581 	LOOP
9582             INSERT INTO po_online_report_text_gt
9583             (
9584              online_report_id,
9585              last_update_login,
9586              last_updated_by,
9587              last_update_date,
9588              created_by,
9589              creation_date,
9590              line_num,
9591              shipment_num,
9592              distribution_num,
9593              sequence,
9594              text_line,
9595              message_name,
9596              message_type
9597             )
9598             VALUES
9599             (
9600               p_online_report_id,
9601               p_login_id,
9602               p_user_id,
9603               sysdate,
9604               p_user_id,
9605               sysdate,
9606               po_tax_interface_pvt.G_TAX_ERRORS_TBL.line_num(i),
9607               po_tax_interface_pvt.G_TAX_ERRORS_TBL.shipment_num(i),
9608               po_tax_interface_pvt.G_TAX_ERRORS_TBL.distribution_num(i),
9609               p_sequence + i, /* 11851142 replaced from ROWNUM */
9610               l_tax_message || po_tax_interface_pvt.G_TAX_ERRORS_TBL.message_text(i),
9611               'PO_TAX_CALCULATION_FAILED',
9612               'E'
9613             );
9614 	END LOOP;
9615         l_progress := '024';
9616       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
9617         IF g_debug_stmt THEN
9618            IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9619                FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head ||
9620                             l_api_name||'.' || l_progress,
9621                             'PO 24: Calculate tax raised unexpected error');
9622            END IF;
9623         END IF;
9624         l_textline := l_progress ||' - ';
9625         IF po_tax_interface_pvt.G_TAX_ERRORS_TBL.MESSAGE_TEXT.COUNT > 0 THEN
9626           l_textline := l_textline || po_tax_interface_pvt.G_TAX_ERRORS_TBL.MESSAGE_TEXT(1);
9627         ELSE
9628           l_textline := l_textline || SQLERRM();
9629         END IF;
9630         fnd_message.set_name('PO','PO_TAX_CALC_UNEXPECTED_ERROR');
9631         fnd_message.set_token('ERROR',l_textline);
9632         FND_MSG_PUB.Add;
9633         l_progress := '025';
9634         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9635       END IF;
9636     END IF;
9637 
9638     p_sequence := p_sequence + SQL%ROWCOUNT;
9639 
9640 --<R12 eTax Integration End>
9641 
9642 -----------------------------------------------------------------------------
9643 
9644  l_progress := '026';
9645 
9646     IF g_debug_stmt THEN
9647         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9648           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
9649                        l_api_name||'.' || l_progress,
9650                        'PO 25: A VMI relationship exists for this item');
9651         END IF;
9652     END IF;
9653 
9654     -- Check 22: If the profile option PO_VMI_DISPLAY_WARNING is on,
9655     -- display a warning if the item has a VMI relationship.
9656 
9657     -- Flag indicating if profile PO_VMI_DISPLAY_WARNING is on
9658     fnd_profile.get(PO_PROFILES.PO_VMI_DISPLAY_WARNING,l_po_vmi_display_warning);
9659 
9660     IF (l_po_vmi_display_warning = 'Y') THEN
9661 
9662       l_textline := FND_MESSAGE.get_string('PO', 'PO_SUB_VMI_ASL_EXISTS');
9663 
9664       INSERT INTO po_online_report_text_gt(
9665          online_report_id,
9666          last_update_login,
9667          last_updated_by,
9668          last_update_date,
9669          created_by,
9670          creation_date,
9671          line_num,
9672          shipment_num,
9673          distribution_num,
9674          sequence,
9675          text_line,
9676          message_name,
9677          message_type
9678       )
9679       SELECT p_online_report_id,
9680              p_login_id,
9681              p_user_id,
9682              sysdate,
9683              p_user_id,
9684              sysdate,
9685              POL.line_num,
9686              PLL.shipment_num,
9687              0,
9688              p_sequence + ROWNUM,
9689              SUBSTR (g_linemsg || POL.line_num || g_delim ||
9690                      g_shipmsg || PLL.shipment_num || g_delim ||
9691                      l_textline,
9692                      1,240),
9693              'PO_SUB_VMI_ASL_EXISTS',
9694              'W'
9695       FROM   PO_LINES_GT POL,
9696              PO_HEADERS_GT POH,
9697              PO_LINE_LOCATIONS_GT PLL,
9698              PO_APPROVED_SUPPLIER_LIS_VAL_V PASL,
9699              PO_ASL_ATTRIBUTES PAA,
9700              PO_ASL_STATUS_RULES_V PASR
9701 
9702       WHERE  POH.po_header_id = p_document_id
9703       AND    POL.po_header_id = POH.po_header_id
9704       AND    PLL.po_header_id = POH.po_header_id
9705       AND    PLL.po_line_id   = POL.po_line_id
9706 
9707       -- item is not null
9708       AND    POL.item_id IS NOT NULL
9709 
9710       -- Document is standard PO
9711       AND    POH.type_lookup_code = 'STANDARD'
9712 
9713       --VMI is enabled
9714       AND    PASL.item_id = POL.item_id
9715       AND    PASL.vendor_id = POH.vendor_id
9716       AND    nvl(PASL.vendor_site_id,-1) = nvl(POH.vendor_site_id,-1)
9717       AND    PASL.using_organization_id IN (PLL.ship_to_organization_id, -1)
9718       AND    PASR.status_id = PASL.asl_status_id
9719       AND    PASR.business_rule = '2_SOURCING'
9720       AND    PASR.allow_action_flag = 'Y'
9721       AND    PASL.asl_id = PAA.asl_id
9722       AND    PAA.enable_vmi_flag = 'Y'
9723       AND    PAA.using_organization_id =
9724                (SELECT max(paa2.using_organization_id)
9725                 FROM   po_asl_attributes paa2
9726                 WHERE  paa2.asl_id = pasl.asl_id
9727                 AND    paa2.using_organization_id IN (-1, PLL.ship_to_organization_id));
9728 
9729     END IF;
9730 
9731     p_sequence := p_sequence + SQL%ROWCOUNT;
9732 
9733 -------------------------------------------------------------------------
9734 
9735     l_progress := '027';
9736     IF g_debug_stmt THEN
9737         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9738           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
9739                        l_api_name||'.' || l_progress,
9740                        'PO 23: Validate OKE Contract Details');
9741         END IF;
9742     END IF;
9743 
9744     -- Bug 7001748: Added new validation check
9745     -- Check 23: Validate OKE Contract Version, Line Id and Deliverable Id.
9746 
9747     -- Check 23a: Validate Contract Version
9748     INSERT INTO po_online_report_text_gt
9749                (online_report_id,
9750                 last_update_login,
9751                 last_updated_by,
9752                 last_update_date,
9753                 created_by,
9754                 creation_date,
9755                 line_num,
9756                 shipment_num,
9757                 distribution_num,
9758                 SEQUENCE,
9759                 text_line,
9760                 message_name)
9761     SELECT p_online_report_id,
9762            p_login_id,
9763            p_user_id,
9764            SYSDATE,
9765            p_user_id,
9766            SYSDATE,
9767            pol.line_num,
9768            0,
9769            0,
9770            p_sequence + ROWNUM,
9771            po_core_s.GET_TRANSLATED_TEXT('PO_INVALID_OKE_CONTRACT_VER_ID',
9772                                          'LINE_NUM',pol.line_num),
9773            'PO_INVALID_OKE_CONTRACT_VER_ID'
9774     FROM   po_lines_gt pol
9775     WHERE  pol.oke_contract_version_id IS NOT NULL
9776            AND NVL(pol.cancel_flag,'N') = 'N' --<Bug 11784215>--
9777            AND pol.oke_contract_version_id NOT IN (SELECT major_version
9778                                                    FROM   oke_k_vers_numbers_v
9779                                                    WHERE  chr_id = pol.oke_contract_header_id
9780                                                    UNION
9781                                                    SELECT major_version
9782                                                    FROM   okc_k_vers_numbers_h
9783                                                    WHERE  chr_id = pol.oke_contract_header_id);
9784 
9785     --Increment the p_sequence with number of errors reported in last query
9786     p_sequence := p_sequence + SQL%ROWCOUNT;
9787 
9788     -- Check 23b: Validate Contract Line Id
9789     INSERT INTO po_online_report_text_gt
9790                (online_report_id,
9791                 last_update_login,
9792                 last_updated_by,
9793                 last_update_date,
9794                 created_by,
9795                 creation_date,
9796                 line_num,
9797                 shipment_num,
9798                 distribution_num,
9799                 SEQUENCE,
9800                 text_line,
9801                 message_name)
9802     SELECT p_online_report_id,
9803            p_login_id,
9804            p_user_id,
9805            SYSDATE,
9806            p_user_id,
9807            SYSDATE,
9808            pol.line_num,
9809            pos.shipment_num,
9810            pod.distribution_num,
9811            p_sequence + ROWNUM,
9812            po_core_s.GET_TRANSLATED_TEXT('PO_INVALID_OKE_CONTRACT_LNE_ID',
9813                                          'LINE_NUM',pol.line_num,
9814                                          'SHIP_NUM',pos.shipment_num,
9815                                          'DIST_NUM',pod.distribution_num),
9816            'PO_INVALID_OKE_CONTRACT_LNE_ID'
9817     FROM   po_distributions_gt pod,
9818            po_line_locations_gt pos,
9819            po_lines_gt pol
9820     WHERE  pod.po_line_id = pol.po_line_id
9821            AND pod.line_location_id = pos.line_location_id
9822            AND pod.oke_contract_line_id IS NOT NULL
9823 	    AND NVL(pol.cancel_flag,'N') = 'N' --<Bug 11784215>--
9824            AND pod.oke_contract_line_id NOT IN (SELECT id
9825                                                 FROM   okc_k_lines_b
9826                                                 WHERE  dnz_chr_id = pol.oke_contract_header_id
9827                                                 -- <Bug 7695529>
9828                                                 -- Look for contract lines in oke_deliverables_b also (DTS flow)
9829                                                 UNION
9830                                                 SELECT deliverable_id
9831                                                 FROM   oke_deliverables_b
9832                                                 WHERE  source_header_id = pol.oke_contract_header_id);
9833 
9834     --Increment the p_sequence with number of errors reported in last query
9835     p_sequence := p_sequence + SQL%ROWCOUNT;
9836 
9837     -- Check 23c: Validate Contract Deliverable Id
9838     INSERT INTO po_online_report_text_gt
9839                (online_report_id,
9840                 last_update_login,
9841                 last_updated_by,
9842                 last_update_date,
9843                 created_by,
9844                 creation_date,
9845                 line_num,
9846                 shipment_num,
9847                 distribution_num,
9848                 SEQUENCE,
9849                 text_line,
9850                 message_name)
9851     SELECT p_online_report_id,
9852            p_login_id,
9853            p_user_id,
9854            SYSDATE,
9855            p_user_id,
9856            SYSDATE,
9857            pol.line_num,
9858            pos.shipment_num,
9859            pod.distribution_num,
9860            p_sequence + ROWNUM,
9861            po_core_s.GET_TRANSLATED_TEXT('PO_INVALID_OKE_CONTRACT_DLV_ID',
9862                                          'LINE_NUM',pol.line_num,
9863                                          'SHIP_NUM',pos.shipment_num,
9864                                          'DIST_NUM',pod.distribution_num),
9865            'PO_INVALID_OKE_CONTRACT_DLV_ID'
9866     FROM   po_distributions_gt pod,
9867            po_line_locations_gt pos,
9868            po_lines_gt pol
9869     WHERE  pod.po_line_id = pol.po_line_id
9870            AND pod.line_location_id = pos.line_location_id
9871            AND pod.oke_contract_deliverable_id IS NOT NULL
9872 	    AND NVL(pol.cancel_flag,'N') = 'N' --<Bug 11784215>--
9873            AND pod.oke_contract_deliverable_id NOT IN (SELECT deliverable_id
9874                                                        FROM   oke_k_deliverables_b
9875                                                        WHERE  k_line_id = pod.oke_contract_line_id
9876                                                        -- <Bug 7695529>
9877                                                        -- Look for contract deliverables in
9878                                                        -- oke_deliverable_actions also (DTS flow)
9879                                                        UNION
9880                                                        SELECT action_id
9881                                                        FROM   oke_deliverable_actions
9882                                                        WHERE  deliverable_id = pod.oke_contract_line_id);
9883 
9884     --Increment the p_sequence with number of errors reported in last query
9885     p_sequence := p_sequence + SQL%ROWCOUNT;
9886 
9887      --BUG 10161562 : For Complex PO, the check is not required as the individula pay item price may vary from the Line unit price.
9888     --               the Sum of pay items price will be equal to the line price.
9889     IF NOT l_is_complex_po THEN
9890     --Bug 9430831 start.Submission Check for unit_price and price_override mismatch
9891             FOR price_rec in (select pol.line_num,poll.shipment_num,pol.unit_price, poll.price_override,rownum
9892                               from po_line_locations_gt poll, po_lines_gt pol
9893                               where pol.po_header_id = p_document_id AND
9894                   		  pol.po_line_id = poll.po_line_id AND
9895                               pol.unit_price <> poll.price_override AND
9896                               nvl(pol.cancel_flag,'N') = 'N' AND
9897                               nvl(pol.closed_code,'OPEN') <> 'FINALLY CLOSED' AND
9898                               poll.shipment_type in ('STANDARD','PLANNED')
9899                               )
9900             LOOP
9901             	FND_MESSAGE.SET_NAME('PO','PO_PDOI_SHIP_PRICE_NE_LINE');
9902             	FND_MESSAGE.SET_TOKEN('SHIP_PRICE', price_rec.price_override);
9903             	FND_MESSAGE.SET_TOKEN('LINE_PRICE',price_rec.unit_price);
9904             	l_textline := FND_MESSAGE.GET;
9905 
9906             INSERT INTO po_online_report_text_gt
9907                             (online_report_id,
9908                             last_update_login,
9909                             last_updated_by,
9910                             last_update_date,
9911                             created_by,
9912                             creation_date,
9913                             line_num,
9914                             shipment_num,
9915                             SEQUENCE,
9916                             text_line,
9917                             message_name)
9918             VALUES (p_online_report_id,
9919                        p_login_id,
9920                        p_user_id,
9921                        SYSDATE,
9922                        p_user_id,
9923                        SYSDATE,
9924                        price_rec.line_num,
9925                        price_rec.shipment_num,
9926                        p_sequence + price_rec.rownum,
9927                        l_textline,
9928                        'PO_PDOI_SHIP_PRICE_NE_LINE');
9929 
9930              --Increment the p_sequence with number of errors reported in last query
9931             p_sequence := p_sequence + SQL%ROWCOUNT;
9932             END LOOP;
9933     --End Bug 9430831
9934 
9935      END IF;
9936      -- Bug 10161562 ends
9937     ----------------------------------------------------------------------------
9938     l_progress := '028';
9939     IF g_debug_stmt THEN
9940             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9941               FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
9942                            l_api_name||'.' || l_progress,'PO 24: Validate the LCM enabled PO to check whether its invoice match option is set as Receipt');
9943             END IF;
9944         END IF;
9945         -- Check 24: Validate the LCM enabled PO to check whether its invoice match option is set as 'Receipt'.
9946         l_textline := FND_MESSAGE.GET_STRING('PO','PO_SUB_PO_SHIP_INV_MATCH_NE_R');
9947         INSERT INTO po_online_report_text_gt (online_report_id,
9948       		                          last_update_login,
9949     	 				  last_updated_by,
9950     	 				  last_update_date,
9951     	 				  created_by,
9952     	 				  creation_date,
9953     	 				  line_num,
9954     	 				  shipment_num,
9955     	 				  distribution_num,
9956     	 				  sequence,
9957     	 				  text_line,
9958     	          			  message_name)
9959         SELECT p_online_report_id,
9960     	   p_login_id,
9961     	   p_user_id,
9962     	   sysdate,
9963     	   p_user_id,
9964     	   sysdate,
9965     	   pol.line_num,
9966     	   pll.shipment_num, 0,
9967     	   p_sequence + ROWNUM,
9968     	   substr(g_linemsg||g_delim||POL.line_num||g_delim||g_shipmsg||g_delim||
9969     		  PLL.shipment_num||g_delim||l_textline,1,240),
9970     	   'PO_SUB_PO_SHIP_INV_MATCH_NE_R'
9971           FROM PO_HEADERS_GT POH,
9972                PO_LINES_GT POL,
9973     	   PO_LINE_LOCATIONS_GT PLL
9974          WHERE POH.po_header_id = POL.po_header_id
9975            AND POL.po_line_id = PLL.po_line_id
9976            AND POH.po_header_id  = p_document_id
9977            AND Nvl(PLL.LCM_FLAG,'N') = 'Y'
9978            AND Nvl(PLL.match_option,'P') <> 'R'
9979 	   AND Nvl(PLL.cancel_flag,'N') = 'N'; -- Bug 13809830: Added a condition to skip the submission check for cancelled shipments;
9980 
9981         --Increment the p_sequence with number of errors reported in last query
9982         p_sequence := p_sequence + SQL%ROWCOUNT;
9983 
9984         -----------------------------------------------------------------------------------
9985 
9986         l_progress := '029';
9987         IF g_debug_stmt THEN
9988             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
9989               FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
9990                            l_api_name||'.' || l_progress,'PO 25: Validate the LCM enabled PO to check whether its destination type is set as Inventory');
9991 
9992             END IF;
9993 
9994 
9995         END IF;
9996         -- Check 25: Validate the LCM enabled PO to check whether its destination type is set as 'Inventory'
9997         l_textline := FND_MESSAGE.GET_STRING('PO','PO_SUB_PO_DIST_DEST_TYPE_NE_IN');
9998         INSERT INTO po_online_report_text_gt (online_report_id,
9999     	 				  last_update_login,
10000     	 				  last_updated_by,
10001     	 				  last_update_date,
10002     	 				  created_by,
10003     	 				  creation_date,
10004     	 				  line_num,
10005     	 				  shipment_num,
10006     	 				  distribution_num,
10007     	 				  sequence,
10008     	 				  text_line,
10009     	                                  message_name)
10010         SELECT p_online_report_id,
10011     	   p_login_id,
10012     	   p_user_id,
10013     	   sysdate,
10014     	   p_user_id,
10015     	   sysdate,
10016     	   pol.line_num,
10017     	   pll.shipment_num,
10018     	   pod.distribution_num,
10019     	   p_sequence + ROWNUM,
10020     	   substr(g_linemsg||g_delim||POL.line_num||g_delim||g_shipmsg||g_delim||PLL.shipment_num||g_delim
10021     	          ||g_distmsg||g_delim||POD.distribution_num||g_delim||l_textline,1,240),
10022     	   'PO_SUB_PO_DIST_DEST_TYPE_NE_IN'
10023           FROM PO_HEADERS_GT POH,
10024                PO_LINES_GT POL,
10025     	   PO_LINE_LOCATIONS_GT PLL,
10026     	   PO_DISTRIBUTIONS_GT POD
10027          WHERE POH.po_header_id = POD.po_header_id
10028            AND POD.line_location_id = PLL.line_location_id
10029            AND PLL.po_line_id = POL.po_line_id
10030            AND POH.po_header_id = p_document_id
10031            AND Nvl(POD.LCM_FLAG,'N') = 'Y'
10032            AND POD.DESTINATION_TYPE_CODE <> 'INVENTORY';
10033 
10034 
10035         --Increment the p_sequence with number of errors reported in last query
10036         p_sequence := p_sequence + SQL%ROWCOUNT;
10037 
10038     -------------------------------------------------------------------------------------------------------
10039     -- CLM Partial Funding Changes
10040     l_progress := '030';
10041 
10042     -- Check 26: The Funded Value of the PO distribution should not be greater
10043     --           than its corresponding Total Order Value.
10044     -- PO_PDOI_INVALID_FUND_VAL_ENC
10045 
10046     --<FPJ ENCUMBRANCE>
10047 
10048     IF (  PO_CORE_S.is_encumbrance_on(
10049              p_doc_type => g_document_type_PO
10050           ,  p_org_id => NULL
10051           ) AND (l_clm_document = 'Y')
10052        )
10053     THEN
10054 
10055        IF g_debug_stmt THEN
10056           PO_DEBUG.debug_stmt(g_log_head || '.'||l_api_name||'.',
10057                               l_progress,'PO 26: Funded Value Should be less than or equal to Total Order Value');
10058        END IF;
10059 
10060        INSERT INTO po_online_report_text_gt
10061                   (online_report_id,
10062                    last_update_login,
10063                    last_updated_by,
10064                    last_update_date,
10065                    created_by,
10066                    creation_date,
10067                    line_num,
10068                    shipment_num,
10069                    distribution_num,
10070                    SEQUENCE,
10071                    text_line,
10072                    message_name)
10073        SELECT p_online_report_id,
10074               p_login_id,
10075               p_user_id,
10076               SYSDATE,
10077               p_user_id,
10078               SYSDATE,
10079               POL.line_num,
10080               POLL.shipment_num,
10081               POD.distribution_num,
10082               p_sequence + ROWNUM,
10083               substr(l_textline,1,240),
10084               'PO_PDOI_INVALID_FUND_VAL_ENC'
10085        FROM
10086           PO_DISTRIBUTIONS_GT POD
10087        ,  PO_LINE_LOCATIONS_GT POLL
10088        ,  PO_LINES_GT POL
10089        WHERE POLL.line_location_id(+) = POD.line_location_id    --JOIN
10090        AND   POL.po_line_id(+) = POD.po_line_id                 --JOIN
10091           -- PA distributions don't have associated lines or shipments
10092        AND   NVL(POD.encumbered_flag,'N') = 'N'
10093        AND   NVL(POLL.cancel_flag,'N') = 'N'
10094        AND   NVL(POLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
10095        AND   NVL(POLL.approved_flag,'N') <> 'Y'
10096        AND   NVL(POD.destination_type_code,'EXPENSE') <> 'SHOP FLOOR'
10097        AND   NVL(POD.funded_value,0) > DECODE(POL.matching_basis,'QUANTITY',
10098                                               ((POD.quantity_ordered * NVL(POLL.price_override,0)) +
10099                                                  NVL(POD.nonrecoverable_tax,0)) * NVL(POD.rate,1),
10100                                               (POD.amount_ordered + NVL(POD.nonrecoverable_tax,0)) * NVL(POD.rate,1)
10101                                               )
10102        AND   NVL(POD.DISTRIBUTION_TYPE, 'STANDARD') = 'STANDARD'
10103        ;
10104 
10105     ELSE
10106        l_progress := '031';
10107        IF g_debug_stmt THEN
10108           PO_DEBUG.debug_stmt(g_log_head || '.'||l_api_name||'.',
10109                               l_progress,'PO 26: PO encumbrance not on or not a CLM document');
10110        END IF;
10111     END IF;
10112 
10113     p_sequence := p_sequence + SQL%ROWCOUNT;
10114 
10115     -------------------------------------------------------------------------------------------------------
10116     -- <CLM-LnSc-START>
10117     l_progress := '032';
10118 
10119     -- Check 27: For CLM Documents, Check if Award Line has more funds than the Tolerance specified in Purchasing Options
10120     -- PO_SUB_PO_CLM_FUNDS_VAL_GT_TOL
10121 
10122     IF (  PO_CORE_S.is_encumbrance_on(
10123              p_doc_type => g_document_type_PO
10124           ,  p_org_id => NULL
10125           ) AND (l_clm_document = 'Y')
10126        )
10127     THEN
10128 
10129       IF g_debug_stmt THEN
10130          PO_DEBUG.debug_stmt(g_log_head || '.'||l_api_name||'.',
10131                              l_progress,'PO 27: Total Funded Value for a Line should be less than that on the backing PR, based on tolerance specified');
10132       END IF;
10133 
10134       SELECT nvl(enforce_funds_change_allowance, 'NONE'),
10135              nvl(funds_change_allowance/100, -1),
10136              nvl(funds_change_amount, -1)
10137       INTO   l_enf_funds_change_allowance,
10138              l_funds_change_allowance,
10139              l_funds_change_amount
10140       FROM   po_system_parameters;
10141 
10142       IF g_debug_stmt THEN
10143         PO_DEBUG.debug_stmt(g_log_head || '.'||l_api_name||'.', l_progress,
10144                             'Is Funds Change Allowance enforced ' || l_enf_funds_change_allowance
10145                             || ' Funds Change Allowance ' || l_funds_change_allowance
10146                             || ' Funds Change Amount ' || l_funds_change_amount);
10147       END IF;
10148 
10149       -- Validate only if Funds Tolerance is enforced
10150       IF l_enf_funds_change_allowance <> 'NONE' AND
10151          (l_funds_change_allowance <> -1 OR l_funds_change_amount <> -1) THEN
10152 
10153         SELECT POL.line_num
10154         BULK COLLECT INTO
10155                l_line_num
10156         FROM   PO_DISTRIBUTIONS_GT POD1,
10157                PO_DISTRIBUTIONS_GT POD2,
10158                PO_LINES_GT POL
10159         WHERE  POD1.po_line_id = POL.po_line_id
10160         AND    POD2.po_line_id = POL.po_line_id
10161         AND    POL.po_header_id = p_document_id
10162         AND    NVL(POD1.distribution_type, 'STANDARD') = 'STANDARD'
10163         AND    NVL(POD2.distribution_type, 'STANDARD') = 'STANDARD'
10164         AND    POD1.req_distribution_id IS NOT NULL -- Linked to PR Dists
10165         AND    POD2.req_distribution_id IS NULL     -- Not Linked to PR Dists
10166         GROUP BY POL.line_num
10167         HAVING ((l_funds_change_amount BETWEEN 0 AND Sum(Nvl(POD2.funded_value,0)))
10168                 OR
10169                 (Sum(Nvl(POD1.funded_value,0)) <> 0 AND
10170                  l_funds_change_allowance BETWEEN 0 AND
10171                  (Sum(Nvl(POD2.funded_value,0))/Sum(Nvl(POD1.funded_value,0)))));
10172 
10173         FOR i IN 1..l_line_num.COUNT LOOP
10174           l_rowCount(i) := i;
10175         END LOOP;
10176 
10177         FORALL i IN 1..l_line_num.COUNT
10178           INSERT INTO po_online_report_text_gt(
10179             online_report_id,
10180             last_update_login,
10181             last_updated_by,
10182             last_update_date,
10183             created_by,
10184             creation_date,
10185             line_num,
10186             shipment_num,
10187             distribution_num,
10188             sequence,
10189             text_line,
10190             message_name,
10191             message_type)
10192           VALUES(
10193             p_online_report_id,
10194             p_login_id,
10195             p_user_id,
10196             sysdate,
10197             p_user_id,
10198             sysdate,
10199             l_line_num(i),
10200             NULL,
10201             NULL,
10202             p_sequence+l_rowCount(i),
10203             PO_CORE_S.get_translated_text(
10204               'PO_SUB_PO_CLM_FUNDS_VAL_GT_TOL',
10205               'LINE_NUM', l_line_num(i)),
10206             'PO_SUB_PO_CLM_FUNDS_VAL_GT_TOL',
10207             Decode(l_enf_funds_change_allowance, 'ADVISORY', 'W', 'E'));
10208 
10209       END IF;
10210 
10211     ELSE
10212        l_progress := '033';
10213        IF g_debug_stmt THEN
10214           PO_DEBUG.debug_stmt(g_log_head || '.'||l_api_name||'.',
10215                               l_progress,'PO 27: PO encumbrance not on or not a CLM document');
10216        END IF;
10217     END IF;
10218 
10219     p_sequence := p_sequence + SQL%ROWCOUNT;
10220 
10221     -------------------------------------------------------------------------------------------------------
10222 
10223     l_progress := '033';
10224 
10225     -- Check 28: For CLM Documents, Check if Total Funded Value of Distributions is greater than Funds remaining on the backing Requisition
10226     -- PO_SUB_FUNDED_VAL_GT_REQ_FUNDS
10227 
10228     IF (  PO_CORE_S.is_encumbrance_on(
10229              p_doc_type => g_document_type_PO
10230           ,  p_org_id => NULL
10231           ) AND (l_clm_document = 'Y')
10232        )
10233     THEN
10234 
10235       IF g_debug_stmt THEN
10236          PO_DEBUG.debug_stmt(g_log_head || '.'||l_api_name||'.',
10237                              l_progress,'PO 28: Total Funded Value of Distributions should be less than Funds remaining on the backing Requisition');
10238       END IF;
10239 
10240       l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_FUNDED_VAL_GT_REQ_FUNDS');
10241       INSERT INTO po_online_report_text_gt(
10242         online_report_id,
10243         last_update_login,
10244         last_updated_by,
10245         last_update_date,
10246         created_by,
10247         creation_date,
10248         line_num,
10249         shipment_num,
10250         distribution_num,
10251         sequence,
10252         text_line,
10253         message_name)
10254       SELECT
10255         p_online_report_id,
10256         p_login_id,
10257         p_user_id,
10258         sysdate,
10259         p_user_id,
10260         sysdate,
10261         NULL,
10262         NULL,
10263         NULL,
10264         p_sequence + ROWNUM,
10265         substr(l_textline||g_delim||PRD.requisition_number||g_delim||
10266               g_linemsg||g_delim||PRD.line_number||g_delim||
10267               g_distmsg||g_delim||PRD.distribution_num,1,240),
10268         'PO_SUB_FUNDED_VAL_GT_REQ_FUNDS'
10269         FROM   PO_CLMREQ_DIST_DETAILS_V PRD
10270         WHERE  PRD.funds_remaining < 0
10271         AND    exists(
10272           SELECT NULL
10273           FROM   PO_DISTRIBUTIONS_GT POD
10274           WHERE  POD.po_header_id = p_document_id
10275           AND    NVL(POD.DISTRIBUTION_TYPE, 'STANDARD') = 'STANDARD'
10276           AND    POD.req_distribution_id = PRD.distribution_id);
10277 
10278     ELSE
10279        l_progress := '034';
10280        IF g_debug_stmt THEN
10281           PO_DEBUG.debug_stmt(g_log_head || '.'||l_api_name||'.',
10282                               l_progress,'PO 28: PO encumbrance not on or not a CLM document');
10283        END IF;
10284     END IF;
10285 
10286     --Increment the p_sequence with number of errors reported in last query
10287     p_sequence := p_sequence + SQL%ROWCOUNT;
10288 
10289     -- <CLM-LnSc-END>
10290     x_return_status := FND_API.G_RET_STS_SUCCESS;
10291 
10292 
10293 EXCEPTION
10294     WHEN FND_API.G_EXC_ERROR THEN
10295         x_return_status := FND_API.G_RET_STS_ERROR;
10296 
10297     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
10298         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10299 
10300     WHEN OTHERS THEN
10301         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10302         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
10303             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
10304         END IF;
10305 
10306         IF (g_debug_unexp) THEN
10307                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
10308                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
10309                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
10310                        || l_progress || ' SQL CODE is '|| sqlcode);
10311                 END IF;
10312         END IF;
10313 END CHECK_PO;
10314 
10315 
10316 --Added for bug 13797764  (For clm document)
10317 /**
10318 * Procedure: PO_IDV_REFERENCE_EQUAL_CHECK
10319 * Requires:
10320 *   IN PARAMETERS:
10321 *       p_document_id:      The po_header_id of submitted document
10322 *       p_online_report_id: Id used to INSERT INTO online_report_text table
10323 *       p_user_id:          User performing the action
10324 *       p_login_id:         Last update login_id
10325 *   IN OUT PARAMETERS
10326 *       p_sequence:          Sequence number of last reported error
10327 *       x_result_status      Result of validation
10328 * Modifies: Inserts error msgs in online_report_text_gt table, uses
10329 *           global_temp tables for processing
10330 * Effects:  This procedure runs the document submission checks for clm
10331 *           documents
10332 * Returns:
10333 *  p_sequence: This parameter contains the current count of number of error
10334 *              messages inserted
10335 */
10336 PROCEDURE po_idv_reference_equal_check(p_document_id IN NUMBER,
10337 					p_online_report_id IN NUMBER,
10338 					p_user_id IN NUMBER,
10339 					p_login_id IN NUMBER,
10340 					p_sequence IN OUT NOCOPY NUMBER,
10341 					x_return_status OUT NOCOPY VARCHAR2) IS
10342 
10343 l_textline  po_online_report_text.text_line%TYPE := NULL;
10344 l_clm_po VARCHAR2(1);
10345 l_api_name  CONSTANT varchar2(40) := 'PO_IDV_REFERENCE_EQUAL_CHECK';
10346 
10347 BEGIN
10348   l_clm_po := PO_CLM_INTG_GRP.is_clm_document(  p_doc_type	=> 'PO',
10349                                                 p_document_id	=> p_document_id );
10350 
10351   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_IDV_REF_NOT_EQUAL');
10352 
10353   INSERT INTO po_online_report_text_gt(online_report_id,
10354         last_update_login,
10355         last_updated_by,
10356         last_update_date,
10357         created_by,
10358         creation_date,
10359         line_num,
10360         shipment_num,
10361         distribution_num,
10362         sequence,
10363         text_line,
10364         message_name)
10365   SELECT  p_online_report_id,
10366         p_login_id,
10367         p_user_id,
10368         sysdate,
10369         p_user_id,
10370         sysdate,
10371         POL.line_num,
10372         0,
10373         0,
10374         p_sequence + ROWNUM,
10375         substr(g_linemsg || g_delim || POL.line_num || g_delim
10376                    || l_textline,1,240),
10377         'PO_IDV_REF_NOT_EQUAL'
10378   FROM   PO_HEADERS_GT POH,
10379          PO_LINES_GT POL
10380   WHERE    POH.po_header_id = p_document_id
10381       AND  POH.po_header_id = POL.po_header_id
10382       AND  POH.type_lookup_code = 'STANDARD'
10383       AND  Nvl(POH.clm_source_document_id, -1) <> Nvl(Nvl(POL.from_header_id,POL.contract_id), -1)
10384       AND  nvl(l_clm_po,'N') = 'Y';
10385 
10386 
10387   --Increment the p_sequence with number of errors reported in last query
10388   p_sequence := p_sequence + SQL%ROWCOUNT;
10389 
10390   x_return_status := FND_API.G_RET_STS_SUCCESS;
10391 
10392 EXCEPTION
10393     WHEN FND_API.G_EXC_ERROR THEN
10394         x_return_status := FND_API.G_RET_STS_ERROR;
10395 
10396     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
10397         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10398 
10399     WHEN OTHERS THEN
10400         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10401         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
10402             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
10403         END IF;
10404 
10405         IF (g_debug_unexp) THEN
10406                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
10407                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
10408                        l_api_name || '.others_exception', ' SQL CODE is '|| sqlcode);
10409                 END IF;
10410         END IF;
10411 END po_idv_reference_equal_check;
10412 
10413 
10414 --For Planned POs and Blanket PAs
10415 /**
10416 * Private Procedure: CHECK_PLANNED_PO_BLANKET_PA
10417 * Requires:
10418 *   IN PARAMETERS:
10419 *       p_document_id:      The requisition_header_id of submitted document
10420 *       p_online_report_id: Id used to INSERT INTO online_report_text table
10421 *       p_user_id:          User performing the action
10422 *       p_login_id:         Last update login_id
10423 *   IN OUT PARAMETERS
10424 *       p_sequence:          Sequence number of last reported error
10425 * Modifies: Inserts error msgs in online_report_text_gt table, uses
10426 *           global_temp tables for processing
10427 * Effects:  This procedure runs the document submission checks for PLANNED POs
10428 *           and BLANKET PAs
10429 * Returns:
10430 *  p_sequence: This parameter contains the current count of number of error
10431 *              messages inserted
10432 */
10433 PROCEDURE check_planned_po_blanket_pa(p_document_id IN NUMBER,
10434                        p_online_report_id IN NUMBER,
10435                        p_user_id IN NUMBER,
10436                        p_login_id IN NUMBER,
10437                        p_sequence IN OUT NOCOPY NUMBER,
10438                        x_return_status OUT NOCOPY VARCHAR2) IS
10439 
10440 l_textline  po_online_report_text.text_line%TYPE := NULL;
10441 l_api_name  CONSTANT varchar2(40) := 'CHECK_PLANNED_PO_BLANKET_PA';
10442 l_progress VARCHAR2(3);
10443 /* bug 14196636 */
10444 l_currency_code    PO_HEADERS_ALL.CURRENCY_CODE%TYPE;
10445 l_precision        NUMBER  := null;
10446 l_ext_precision    NUMBER  := null;
10447 l_min_acct_unit    NUMBER  := null;
10448 /*end bug 14196636 */
10449 
10450 BEGIN
10451 l_progress := '000';
10452 
10453 l_progress := '001';
10454 IF g_debug_stmt THEN
10455    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
10456      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
10457           || l_progress,
10458           'PA/PO BLANKET/PLANNED 1: Amount agreed greater than Amount limit');
10459    END IF;
10460 END IF;
10461 
10462   -- Check 1: The amount agreed specified on the planned po and blanket
10463     -- pa should be less than the amount limit.
10464     -- PO_SUB_AGREED_GRT_LIMIT
10465 
10466   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_AGREED_GRT_LIMIT');
10467   INSERT INTO po_online_report_text_gt(online_report_id,
10468         last_update_login,
10469         last_updated_by,
10470         last_update_date,
10471         created_by,
10472         creation_date,
10473         line_num,
10474         shipment_num,
10475         distribution_num,
10476         sequence,
10477         text_line,
10478                 message_name)
10479   SELECT  p_online_report_id,
10480         p_login_id,
10481         p_user_id,
10482           sysdate,
10483         p_user_id,
10484         sysdate,
10485         0,
10486         0,
10487         0,
10488         p_sequence + ROWNUM,
10489         substr(l_textline,1,240),
10490             'PO_SUB_AGREED_GRT_LIMIT'
10491     FROM  PO_HEADERS_GT POH
10492     WHERE  POH.po_header_id = p_document_id
10493     AND    POH.blanket_total_amount is not null
10494     AND    POH.amount_limit is not null
10495     AND    POH.blanket_total_amount > POH.amount_limit;
10496 
10497 
10498     --Increment the p_sequence with number of errors reported in last query
10499     p_sequence := p_sequence + SQL%ROWCOUNT;
10500 ------------------------------------------
10501 
10502 l_progress := '002';
10503 IF g_debug_stmt THEN
10504    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
10505      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
10506           || l_progress,
10507           'PA/PO BLANKET/PLANNED 2: Min Rel Amount greater than Amount limit');
10508    END IF;
10509 END IF;
10510 
10511   -- Check 2: The min release amount specified on the planned po and
10512     -- blanket pa should be less than the amount limit.
10513     -- PO_SUB_MINREL_GRT_LIMIT
10514 
10515   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_MINREL_GRT_LIMIT');
10516   INSERT INTO po_online_report_text_gt(online_report_id,
10517         last_update_login,
10518         last_updated_by,
10519         last_update_date,
10520         created_by,
10521         creation_date,
10522         line_num,
10523         shipment_num,
10524         distribution_num,
10525         sequence,
10526         text_line,
10527                 message_name)
10528   SELECT  p_online_report_id,
10529         p_login_id,
10530         p_user_id,
10531           sysdate,
10532         p_user_id,
10533         sysdate,
10534         0,
10535         0,
10536         0,
10537         p_sequence + ROWNUM,
10538         substr(l_textline,1,240),
10539             'PO_SUB_MINREL_GRT_LIMIT'
10540     FROM  PO_HEADERS_GT POH
10541     WHERE  POH.po_header_id = p_document_id
10542     AND    POH.min_release_amount is not null
10543     AND    POH.amount_limit is not null
10544     AND    POH.min_release_amount > POH.amount_limit;
10545 
10546 
10547      --Increment the p_sequence with number of errors reported in last query
10548     p_sequence := p_sequence + SQL%ROWCOUNT;
10549 ------------------------------------------
10550 
10551 l_progress := '003';
10552 IF g_debug_stmt THEN
10553    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
10554      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
10555           || l_progress,
10556           'PA/PO BLANKET/PLANNED 3: Amount released greater than Amount limit');
10557    END IF;
10558 END IF;
10559 
10560 /* bug 14196636 - Query to fetch precision for currency */
10561   select poh.currency_code
10562     into l_currency_code
10563     FROM   PO_HEADERS_GT POH
10564     WHERE  POH.po_header_id = p_document_id;
10565 
10566   fnd_currency.get_info (l_currency_code,
10567                          l_precision,
10568                          l_ext_precision,
10569                          l_min_acct_unit);
10570 /* End bug 14196636 - Query to fetch precision for currency */
10571 
10572   -- Check 3: The Amount Limit should be greater than the total of all
10573     -- Releases.
10574     -- PO_SUB_LIMIT_GRT_REL_AMT
10575 
10576   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_LIMIT_GRT_REL_AMT');
10577   INSERT INTO po_online_report_text_gt(online_report_id,
10578         last_update_login,
10579         last_updated_by,
10580         last_update_date,
10581         created_by,
10582         creation_date,
10583         line_num,
10584         shipment_num,
10585         distribution_num,
10586         sequence,
10587         text_line,
10588                 message_name)
10589   SELECT  p_online_report_id,
10590         p_login_id,
10591         p_user_id,
10592           sysdate,
10593         p_user_id,
10594         sysdate,
10595         0,
10596         0,
10597         0,
10598         p_sequence + ROWNUM,
10599         substr(l_textline,1,240),
10600             'PO_SUB_LIMIT_GRT_REL_AMT'
10601      FROM   PO_HEADERS_GT POH
10602     WHERE  POH.po_header_id = p_document_id
10603     AND    POH.amount_limit is not null
10604     AND ( (NVL(POH.global_agreement_flag, 'N') = 'N'
10605            AND
10606            round(POH.amount_limit, l_precision) <
10607            (   SELECT                                         -- <SERVICES FPJ>
10608                    sum ( decode ( PLL2.quantity
10609                                 , NULL , round(( PLL2.amount
10610                                          - nvl(PLL2.amount_cancelled,0)
10611                                          ),l_precision)
10612                                 ,        round(   ( PLL2.quantity
10613                                              - nvl(PLL2.quantity_cancelled,0) )
10614                                          * PLL2.price_override,l_precision
10615                                          )
10616                                 )
10617                        )
10618                FROM PO_LINE_LOCATIONS PLL2
10619                WHERE PLL2.po_header_id = POH.po_header_id
10620                AND PLL2.shipment_type in ('BLANKET', 'SCHEDULED')
10621            )
10622           )
10623          OR
10624          --bug2969379
10625          --GA should have amount limit checked in a different way
10626           ( POH.global_agreement_flag = 'Y'
10627             AND
10628             round(POH.amount_limit, l_precision) <
10629             (   SELECT
10630                    sum ( decode ( PLL3.quantity
10631                                 , NULL , round(( PLL3.amount
10632                                          - nvl(PLL3.amount_cancelled,0)
10633                                          ),l_precision)
10634                                 ,        round(   ( PLL3.quantity
10635                                              - nvl(PLL3.quantity_cancelled,0) )
10636                                          * PLL3.price_override,l_precision
10637                                          )
10638                                 )
10639                        )
10640                FROM PO_LINE_LOCATIONS_ALL PLL3
10641                WHERE PLL3.from_header_id = POH.po_header_id
10642             )
10643           )
10644         );
10645 
10646 --<FPI commented>
10647 --    GROUP BY POH.amount_limit);
10648 
10649      --Increment the p_sequence with number of errors reported in last query
10650     p_sequence := p_sequence + SQL%ROWCOUNT;
10651 
10652 ------------------------------------------
10653 
10654 /* Start Bug 3286940 */
10655 
10656 
10657 l_progress := '004';
10658 IF g_debug_stmt THEN
10659    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
10660      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
10661           || l_progress,
10662           'PA/PO BLANKET/PLANNED 4: Price Limit less than Line Price');
10663    END IF;
10664 END IF;
10665 
10666 
10667   -- Check 4: The price limit when price_override_flag = 'Y' should be
10668    --          at least as large as the unit price for that line.
10669     -- PO_SVC_PRICE_LIMIT_LT_PRICE
10670 
10671     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SVC_PRICE_LIMIT_LT_PRICE');
10672 
10673 
10674       INSERT into po_online_report_text_gt(online_report_id,
10675                                            last_update_login,
10676                                            last_updated_by,
10677                                            last_update_date,
10678                                            created_by,
10679                                            creation_date,
10680                                            line_num,
10681                                            shipment_num,
10682                                            distribution_num,
10683                                            sequence,
10684                                            text_line,
10685                                            message_name)
10686       SELECT  p_online_report_id,
10687               p_login_id,
10688               p_user_id,
10689               sysdate,
10690               p_user_id,
10691               sysdate,
10692               POL.line_num,
10693               0,
10694               0,
10695               p_sequence + ROWNUM,
10696               substr(g_linemsg||g_delim||POL.line_num
10697                          ||g_delim||l_textline,1,240),
10698               'PO_SVC_PRICE_LIMIT_LT_PRICE'
10699       FROM PO_LINES_GT POL
10700       WHERE POL.po_header_id = p_document_id
10701       AND trunc(sysdate) <= trunc(nvl(POL.expiration_date, sysdate + 1)) -- bug 3449694
10702       AND nvl(POL.cancel_flag,'N')= 'N'
10703       AND nvl(POL.closed_code,'OPEN') <> 'FINALLY CLOSED'
10704       AND nvl(POL.allow_price_override_flag, 'N') = 'Y'
10705       AND POL.not_to_exceed_price IS NOT NULL
10706       AND ((POL.unit_price IS NOT NULL and POL.not_to_exceed_price < POL.unit_price)
10707            or
10708           (POL.amount IS NOT NULL and POL.not_to_exceed_price < POL.amount));
10709 
10710 
10711 
10712      --Increment the p_sequence with number of errors reported in last query
10713     p_sequence := p_sequence + SQL%ROWCOUNT;
10714 
10715 
10716 /* End Bug 3286940 */
10717 
10718 
10719     x_return_status := FND_API.G_RET_STS_SUCCESS;
10720 EXCEPTION
10721     WHEN FND_API.G_EXC_ERROR THEN
10722         x_return_status := FND_API.G_RET_STS_ERROR;
10723 
10724     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
10725         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10726 
10727     WHEN OTHERS THEN
10728         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10729         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
10730             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
10731         END IF;
10732 
10733         IF (g_debug_unexp) THEN
10734                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
10735                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
10736                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
10737                        || l_progress || ' SQL CODE is '||sqlcode);
10738                 END IF;
10739         END IF;
10740 
10741 END CHECK_PLANNED_PO_BLANKET_PA;
10742 
10743 --For Blanket PA
10744 /**
10745 * Private Procedure: CHECK_BLANKET_AGREEMENT
10746 * Requires:
10747 *   IN PARAMETERS:
10748 *       p_document_id:      The requisition_header_id of submitted document
10749 *       p_online_report_id: Id used to INSERT INTO online_report_text table
10750 *       p_user_id:          User performing the action
10751 *       p_login_id:         Last update login_id
10752 *   IN OUT PARAMETERS
10753 *       p_sequence:          Sequence number of last reported error
10754 * Modifies: Inserts error msgs in online_report_text_gt table, uses
10755 *           global_temp tables for processing
10756 * Effects:  This procedure runs the document submission checks for BLANKETS
10757 * Returns:
10758 *  p_sequence: This parameter contains the current count of number of error
10759 *              messages inserted
10760 */
10761 PROCEDURE check_blanket_agreement(p_document_id IN NUMBER,
10762                        p_online_report_id IN NUMBER,
10763                        p_user_id IN NUMBER,
10764                        p_login_id IN NUMBER,
10765                        p_check_asl IN BOOLEAN,                     -- <2757450>
10766                        p_sequence IN OUT NOCOPY NUMBER,
10767                        x_return_status OUT NOCOPY VARCHAR2) IS
10768 
10769 l_textline  po_online_report_text.text_line%TYPE := NULL;
10770 l_api_name  CONSTANT varchar2(40) := 'CHECK_BLANKET_AGREEMENT';
10771 l_progress VARCHAR2(3);
10772 
10773 BEGIN
10774 l_progress := '000';
10775 
10776 l_progress := '001';
10777 IF g_debug_stmt THEN
10778    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
10779      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
10780           || l_progress,'PA BLANKET 1: No lines');
10781    END IF;
10782 END IF;
10783 
10784     -- Check 1: Blanket Header must have at least one line
10785     -- PO_SUB_HEADER_NO_LINES
10786     -- Message inserted is 'Purchase Document has no lines'
10787     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_HEADER_NO_LINES');
10788     INSERT INTO po_online_report_text_gt(online_report_id,
10789         last_update_login,
10790         last_updated_by,
10791         last_update_date,
10792         created_by,
10793           creation_date,
10794         line_num,
10795         shipment_num,
10796         distribution_num,
10797         sequence,
10798         text_line,
10799                 message_name)
10800     SELECT  p_online_report_id,
10801             p_login_id,
10802             p_user_id,
10803             sysdate,
10804             p_user_id,
10805             sysdate,
10806             0, 0, 0,
10807             p_sequence + ROWNUM,
10808             substr(l_textline,1,240),
10809             'PO_SUB_HEADER_NO_LINES'
10810     FROM   PO_HEADERS_GT POH
10811     WHERE  POH.po_header_id = p_document_id AND
10812            NOT EXISTS (SELECT 'Lines Exist'
10813                        FROM   PO_LINES_GT POL
10814                        WHERE  POL.po_header_id = POH.po_header_id);
10815                        --AND    nvl(POL.cancel_flag,'N') = 'N');
10816                        -- bug 3300632
10817 
10818     --Increment the p_sequence with number of errors reported in last query
10819     p_sequence := p_sequence + SQL%ROWCOUNT;
10820 
10821 --Bug 16360871 Check the end_date of BPA
10822 l_progress := '100' ;
10823 IF g_debug_stmt THEN
10824    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
10825      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
10826           || l_progress,'PA BLANKET 2: BPA header end date expired.');
10827    END IF;
10828 END IF;
10829     l_textline := FND_MESSAGE.GET_STRING('PO','PO_SUB_HEADER_END_DATED');
10830 
10831     INSERT INTO po_online_report_text_gt(
10832                 online_report_id,
10833                 last_update_login,
10834                 last_updated_by,
10835                 last_update_date,
10836                 created_by,
10837                 creation_date,
10838                 line_num,
10839                 shipment_num,
10840                 distribution_num,
10841                 sequence,
10842                 text_line,
10843                 message_name)
10844     SELECT  p_online_report_id,
10845             p_login_id,
10846             p_user_id,
10847             sysdate,
10848             p_user_id,
10849             sysdate,
10850             0, 0, 0,
10851             p_sequence + ROWNUM,
10852             substr(l_textline,1,240),
10853             'PO_SUB_HEADER_END_DATED'
10854     FROM   PO_HEADERS_GT POH
10855     WHERE  POH.po_header_id = p_document_id
10856       AND  TRUNC(sysdate) > TRUNC(POH.end_date);
10857   -- Increment the p_sequence with number of errors reported in last query
10858    p_sequence := p_sequence + SQL%ROWCOUNT;
10859   --Bug 16360871 end
10860 ----------------------------------------------------
10861 
10862 --Bug 4943365 We should not make any ASL checks for Blanket agreements.
10863 --Removed the call to check_asl
10864 
10865 ---------------------------------------------
10866 
10867 --<FPJ ENCUMBRANCE>
10868 
10869 l_progress := '400';
10870 
10871 -- Check 4: The PA GL date should be within an open purchasing period
10872 -- PO_SUB_PA_INVALID_GL_DATE ?
10873 
10874 IF (  PO_CORE_S.is_encumbrance_on(
10875          p_doc_type => g_document_type_PA
10876       ,  p_org_id => NULL
10877       )
10878    )
10879 THEN
10880 
10881    l_progress := '410';
10882    IF g_debug_stmt THEN
10883       PO_DEBUG.debug_stmt(g_log_head||'.'||l_api_name||'.',
10884                            l_progress,'PA BLANKET 4: GL date');
10885    END IF;
10886 
10887    check_gl_date(
10888       p_doc_type => g_document_type_PA
10889    ,  p_online_report_id => p_online_report_id
10890    ,  p_login_id => p_login_id
10891    ,  p_user_id => p_user_id
10892    ,  p_sequence => p_sequence
10893    );
10894 
10895    l_progress := '420';
10896 
10897 ELSE
10898    l_progress := '430';
10899    IF g_debug_stmt THEN
10900       PO_DEBUG.debug_stmt(g_log_head||'.'||l_api_name||'.',
10901                            l_progress,'PA BLANKET 4: PA encumbrance not on');
10902    END IF;
10903 END IF;
10904 
10905 
10906   /* Start bug #3512688*/
10907       /* Check 05 : To check the validity of the item at line level for newly added  line */
10908       l_progress := '04';
10909       IF g_debug_stmt  THEN
10910       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
10911         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
10912       l_api_name||'.' || l_progress,
10913       'PO 04: Non Purchasable Item is not allowed');
10914       END IF;
10915       END IF;
10916       l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_ALL_NO_ITEM');
10917       INSERT INTO po_online_report_text_gt(online_report_id,
10918       last_update_login,
10919       last_updated_by,
10920       last_update_date,
10921       created_by,
10922       creation_date,
10923       line_num,
10924       shipment_num,
10925       distribution_num,
10926       sequence,
10927       text_line,
10928       message_name)
10929       SELECT  p_online_report_id,
10930       p_login_id,
10931       p_user_id,
10932       sysdate,
10933       p_user_id,
10934       sysdate,
10935       pl.line_num,
10936       0,
10937       0,
10938       p_sequence + ROWNUM,
10939        substr(g_linemsg||g_delim||pl.line_num||g_delim||l_textline,1,240),
10940       'PO_ALL_NO_ITEM'
10941       from po_headers_gt ph,po_lines_gt pl,mtl_system_items  itm,financials_system_parameters fsp,po_line_types_b plt
10942       where itm.inventory_item_id  = pl.item_id
10943       and   pl.item_id is not null
10944       and   itm.organization_id    = fsp.inventory_organization_id
10945       and   itm.purchasing_enabled_flag = 'N'
10946       and   ph.po_header_id = p_document_id
10947       and   pl.po_header_id = ph.po_header_id
10948       and   pl.line_type_id = plt.line_type_id
10949 
10950      and   nvl(plt.outside_operation_flag,'N') =  nvl(itm.outside_operation_flag,'N')
10951       and   (pl.creation_date >= nvl(ph.approved_date ,pl.creation_date)) ;
10952 
10953       --Increment the p_sequence with number of errors reported in last query
10954       p_sequence := p_sequence + SQL%ROWCOUNT;
10955       --End Bug #3512688
10956 
10957 
10958 
10959 --<Begin Bug#: 5132541> EFFECTIVE DATE ON THE HEADER IS NOT VALIDATED AGAINST THAT OF PRICE BREAK
10960     /*Check 06: Check if the effective range on the price break is within the effective range
10961                 of the header and before the expiration date of the line.*/
10962 
10963     l_progress := '06';
10964     IF g_debug_stmt  THEN
10965       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
10966         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
10967                         l_api_name||'.' || l_progress,
10968                         'BPA 06: Checking price breaks effective date range');
10969       END IF;
10970     END IF;
10971 
10972     INSERT INTO po_online_report_text_gt(online_report_id,
10973     last_update_login,
10974     last_updated_by,
10975     last_update_date,
10976     created_by,
10977     creation_date,
10978     line_num,
10979     shipment_num,
10980     distribution_num,
10981     sequence,
10982     text_line,
10983     message_name)
10984     SELECT  p_online_report_id,
10985     p_login_id,
10986     p_user_id,
10987     sysdate,
10988     p_user_id,
10989     sysdate,
10990     pl.line_num,
10991     pll.shipment_num,
10992     0,
10993     p_sequence + ROWNUM,
10994     CASE
10995         WHEN pll.start_date < ph.start_date
10996         THEN substr(g_linemsg||g_delim||pl.line_num||g_delim
10997                    ||g_price_breakmsg||g_delim||pll.shipment_num||g_delim
10998                    ||FND_MESSAGE.GET_STRING('PO', PO_MESSAGE_S.POX_EFFECTIVE_DATES1),1,240)
10999         WHEN pll.end_date > ph.end_date
11000         THEN substr(g_linemsg||g_delim||pl.line_num||g_delim
11001                    ||g_price_breakmsg||g_delim||pll.shipment_num||g_delim
11002                    ||FND_MESSAGE.GET_STRING('PO', PO_MESSAGE_S.POX_EFFECTIVE_DATES),1,240)
11003         WHEN pll.start_date > ph.end_date
11004         THEN substr(g_linemsg||g_delim||pl.line_num||g_delim
11005                    ||g_price_breakmsg||g_delim||pll.shipment_num||g_delim
11006                    ||FND_MESSAGE.GET_STRING('PO', PO_MESSAGE_S.POX_EFFECTIVE_DATES4),1,240)
11007         WHEN pll.end_date < ph.start_date
11008         THEN substr(g_linemsg||g_delim||pl.line_num||g_delim
11009                    ||g_price_breakmsg||g_delim||pll.shipment_num||g_delim
11010                    ||FND_MESSAGE.GET_STRING('PO', PO_MESSAGE_S.POX_EFFECTIVE_DATES5),1,240)
11011         WHEN pll.start_date > pl.expiration_date
11012         THEN substr(g_linemsg||g_delim||pl.line_num||g_delim
11013                    ||g_price_breakmsg||g_delim||pll.shipment_num||g_delim
11014                    ||FND_MESSAGE.GET_STRING('PO', PO_MESSAGE_S.POX_EFFECTIVE_DATES6),1,240)
11015         WHEN pll.end_date > pl.expiration_date
11016         THEN substr(g_linemsg||g_delim||pl.line_num||g_delim
11017                    ||g_price_breakmsg||g_delim||pll.shipment_num||g_delim
11018                    ||FND_MESSAGE.GET_STRING('PO', PO_MESSAGE_S.POX_EFFECTIVE_DATES2),1,240)
11019      END
11020     ,
11021     CASE
11022         WHEN pll.start_date < ph.start_date
11023         THEN PO_MESSAGE_S.POX_EFFECTIVE_DATES1
11024         WHEN pll.end_date > ph.end_date
11025         THEN PO_MESSAGE_S.POX_EFFECTIVE_DATES
11026         WHEN pll.start_date > ph.end_date
11027         THEN PO_MESSAGE_S.POX_EFFECTIVE_DATES4
11028         WHEN pll.end_date < ph.start_date
11029         THEN PO_MESSAGE_S.POX_EFFECTIVE_DATES5
11030         WHEN pll.start_date > pl.expiration_date
11031         THEN PO_MESSAGE_S.POX_EFFECTIVE_DATES6
11032         WHEN pll.end_date > pl.expiration_date
11033         THEN PO_MESSAGE_S.POX_EFFECTIVE_DATES2
11034     END
11035     FROM po_headers_gt ph, po_lines_gt pl, po_line_locations_gt pll
11036     WHERE ph.po_header_id = p_document_id
11037     AND pl.po_header_id = ph.po_header_id
11038     AND pll.po_line_id = pl.po_line_id
11039     AND pll.shipment_type = 'PRICE BREAK'
11040     AND (pll.start_date < ph.start_date
11041       or pll.end_date > ph.end_date
11042       or pll.start_date > ph.end_date
11043       or pll.end_date < ph.start_date
11044       or pll.start_date > pl.expiration_date
11045       or pll.end_date > pl.expiration_date);
11046 
11047     p_sequence := p_sequence + SQL%ROWCOUNT;
11048 
11049     /*Check 07: Check if the expiration date on the line is within the effective range
11050                 of the header.*/
11051 
11052     l_progress := '07';
11053     IF g_debug_stmt  THEN
11054       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
11055         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
11056                         l_api_name||'.' || l_progress,
11057                         'BPA 07: Checking the lines expiration date');
11058       END IF;
11059     END IF;
11060 
11061     -- Bug 9816849 : Added condition to avoid checking expiration date for lines
11062     -- having canceled_flag=Y
11063     l_textline := FND_MESSAGE.GET_STRING('PO', PO_MESSAGE_S.POX_EXPIRATION_DATES);
11064     INSERT INTO po_online_report_text_gt(online_report_id,
11065     last_update_login,
11066     last_updated_by,
11067     last_update_date,
11068     created_by,
11069     creation_date,
11070     line_num,
11071     shipment_num,
11072     distribution_num,
11073     sequence,
11074     text_line,
11075     message_name)
11076     SELECT  p_online_report_id,
11077     p_login_id,
11078     p_user_id,
11079     sysdate,
11080     p_user_id,
11081     sysdate,
11082     pl.line_num,
11083     0,
11084     0,
11085     p_sequence + ROWNUM,
11086     substr(g_linemsg||g_delim||pl.line_num||g_delim||l_textline,1,240),
11087     PO_MESSAGE_S.POX_EXPIRATION_DATES
11088     FROM po_headers_gt ph, po_lines_gt pl
11089     WHERE ph.po_header_id = p_document_id
11090     AND pl.po_header_id = ph.po_header_id
11091     AND (pl.expiration_date < ph.start_date
11092       or pl.expiration_date > ph.end_date)
11093     AND nvl(pl.cancel_flag,'N') = 'N' ; -- bug 9816849
11094 
11095 
11096     p_sequence := p_sequence + SQL%ROWCOUNT;
11097     --<End Bug#: 5132541>
11098 
11099 
11100 l_progress := '008';
11101 
11102 --Bug 14761965 start. Added submission check for Debarred.
11103 	IF g_debug_stmt THEN
11104      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
11105        FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
11106              || l_progress,'BPA 08: ASL Debarred');
11107       END IF;
11108     END IF;
11109 
11110        -- Check 8: If an item is restricted then the Purchase Order Vendor
11111        -- must be listed in the Approved Suppliers List table and must not be
11112        -- DEBARRED.
11113        -- PO_SUB_ITEM_ASL_DEBARRED
11114 
11115     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_ITEM_ASL_DEBARRED');
11116      INSERT INTO po_online_report_text_gt(online_report_id,
11117            last_update_login,
11118            last_updated_by,
11119            last_update_date,
11120            created_by,
11121            creation_date,
11122            line_num,
11123            shipment_num,
11124            distribution_num,
11125            sequence,
11126            text_line,
11127                    message_name)
11128      SELECT  p_online_report_id,
11129            p_login_id,
11130            p_user_id,
11131              sysdate,
11132            p_user_id,
11133            sysdate,
11134            POL.line_num,
11135            PLL.shipment_num,
11136            0,
11137            p_sequence + ROWNUM,
11138            substr(g_linemsg||g_delim||POL.line_num||g_delim
11139                       ||g_shipmsg||g_delim||PLL.shipment_num||g_delim
11140                       ||l_textline,1,240),
11141                'PO_SUB_ITEM_ASL_DEBARRED'
11142         FROM PO_LINE_LOCATIONS_GT PLL,
11143             PO_LINES_GT POL, PO_HEADERS_GT POH,
11144             FINANCIALS_SYSTEM_PARAMETERS FSP
11145        WHERE POH.po_header_id = p_document_id
11146        AND POH.po_header_id = POL.po_header_id
11147        AND PLL.po_line_id(+) = POL.po_line_id
11148        AND PLL.po_release_id IS NULL
11149        AND nvl(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
11150        AND nvl(POL.cancel_flag,'N') = 'N'
11151        AND nvl(PLL.cancel_flag,'N') = 'N'
11152        AND exists
11153           (SELECT 1
11154            FROM PO_APPROVED_SUPPLIER_LIS_VAL_V ASL, PO_ASL_STATUS_RULES ASR,
11155    	MTL_SYSTEM_ITEMS MSI  --Bug5597639
11156            WHERE  ASL.using_organization_id in (PLL.ship_to_organization_id, -1)
11157    	/*Bug5597639 Adding the below three conditions */
11158    	AND MSI.organization_id = FSP.inventory_organization_id
11159    	AND MSI.inventory_item_id = POL.item_id
11160    	AND POL.item_id is not null
11161            AND    ASL.vendor_id = POH.vendor_id
11162            AND    nvl(ASL.vendor_site_id, POH.vendor_site_id) = POH.vendor_site_id
11163            AND  ASL.item_id = POL.item_id
11164            AND    ASL.asl_status_id = ASR.status_id
11165            AND    ASR.business_rule = '1_PO_APPROVAL'
11166            AND    ASR.allow_action_flag <> 'Y' -- Bug 5724696
11167            UNION ALL
11168            SELECT 1
11169            FROM PO_APPROVED_SUPPLIER_LIS_VAL_V ASL, PO_ASL_STATUS_RULES ASR
11170            WHERE  ASL.using_organization_id in (PLL.ship_to_organization_id, -1)
11171            AND    ASL.vendor_id = POH.vendor_id
11172            AND    nvl(ASL.vendor_site_id, POH.vendor_site_id) = POH.vendor_site_id
11173            AND    ASL.item_id is NULL
11174    	       AND  POL.category_id = ASL.category_id  --Bug5597639
11175            AND    ASL.asl_status_id = ASR.status_id
11176            AND    ASR.business_rule = '1_PO_APPROVAL'
11177            AND    ASR.allow_action_flag <> 'Y' );  --Bug5597639
11178 
11179         --Increment the p_sequence with number of errors reported in last query
11180        p_sequence := p_sequence + SQL%ROWCOUNT;
11181 	--Bug 14761965 end
11182 
11183     x_return_status := FND_API.G_RET_STS_SUCCESS;
11184 EXCEPTION
11185     WHEN FND_API.G_EXC_ERROR THEN
11186         x_return_status := FND_API.G_RET_STS_ERROR;
11187 
11188     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
11189         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11190 
11191     WHEN OTHERS THEN
11192         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11193         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
11194             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
11195         END IF;
11196 
11197         IF (g_debug_unexp) THEN
11198                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
11199                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
11200                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
11201                        || l_progress || ' SQL CODE is '||sqlcode);
11202                 END IF;
11203         END IF;
11204 
11205 END CHECK_BLANKET_AGREEMENT;
11206 
11207 --For Standard POs
11208 /**
11209 * Private Procedure: CHECK_STANDARD_PO
11210 * Requires:
11211 *   IN PARAMETERS:
11212 *       p_document_id:      The requisition_header_id of submitted document
11213 *       p_online_report_id: Id used to INSERT INTO online_report_text table
11214 *       p_user_id:          User performing the action
11215 *       p_login_id:         Last update login_id
11216 *   IN OUT PARAMETERS
11217 *       p_sequence:          Sequence number of last reported error
11218 * Modifies: Inserts error msgs in online_report_text_gt table, uses
11219 *           global_temp tables for processing
11220 * Effects:  This procedure runs the document submission checks for Standard
11221 *           POs including GLOBAL AGREEMENTS reference checks and Consigned
11222 *           Inventory checks
11223 * Returns:
11224 *  p_sequence: This parameter contains the current count of number of error
11225 *              messages inserted
11226 */
11227 PROCEDURE check_standard_po(p_document_id IN NUMBER,
11228                        p_online_report_id IN NUMBER,
11229                        p_user_id IN NUMBER,
11230                        p_login_id IN NUMBER,
11231                        p_sequence IN OUT NOCOPY NUMBER,
11232                        x_return_status OUT NOCOPY VARCHAR2) IS
11233 
11234 l_is_ga_referenced VARCHAR2(1) := NULL;
11235 l_api_name  CONSTANT varchar2(40) := 'CHECK_STANDARD_PO';
11236 l_progress VARCHAR2(3);
11237 
11238 l_is_gc_referenced VARCHAR2(1) := NULL;         -- <GC FPJ>
11239 l_textline PO_ONLINE_REPORT_TEXT.text_line%TYPE := NULL;  --< Shared Proc FPJ >
11240 l_return_status VARCHAR2(1);
11241 
11242 -- Bug 2818810. Added extra join to alias POHA to return 'Y' only if at least
11243 -- one line references a GA.
11244 CURSOR std_ga_ref_cursor(p_document_id NUMBER) IS
11245     SELECT 'Y'
11246     FROM PO_HEADERS_GT POH, PO_LINES_GT POL, PO_HEADERS_ALL POHA
11247     WHERE POH.po_header_id = p_document_id
11248      AND  POH.po_header_id = POL.po_header_id
11249      AND  POL.from_header_id = POHA.po_header_id
11250      AND  POHA.type_lookup_code = 'BLANKET'
11251      AND  POHA.global_agreement_flag = 'Y';
11252 
11253 -- <GC FPJ START>
11254 CURSOR std_gc_ref_cursor (p_doc_id NUMBER) IS
11255   SELECT 'Y'
11256   FROM   po_lines_gt POL,
11257          po_headers_all POHA
11258   WHERE  POL.po_header_id = p_doc_id
11259   AND    POL.contract_id = POHA.po_header_id
11260   AND    POHA.global_agreement_flag = 'Y';
11261 -- <GC FPJ END>
11262 
11263 BEGIN
11264 
11265 l_progress := '000';
11266 
11267     --check if atleast one line of Standard PO has Global Agreement reference
11268     --if so then call check_std_global_ref
11269     OPEN std_ga_ref_cursor(p_document_id);
11270 
11271     FETCH std_ga_ref_cursor INTO l_is_ga_referenced;
11272 
11273     CLOSE std_ga_ref_cursor;
11274 
11275     IF l_is_ga_referenced = 'Y' THEN
11276 l_progress := '001';
11277 IF g_debug_stmt THEN
11278    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
11279      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
11280           || l_progress,'PO STD: GA is referenced. Call GA checks');
11281    END IF;
11282 END IF;
11283 
11284        check_std_global_ref(p_document_id ,
11285                        p_online_report_id ,
11286                        p_user_id ,
11287                        p_login_id ,
11288                        p_sequence,
11289                        l_return_status);
11290 
11291         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
11292              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
11293         END IF;
11294 
11295     END IF;
11296 
11297     -- <GC FPJ START>
11298     -- If there exists a line referencing global contract, make sure that
11299     -- the reference is valid by calling check_std_gc_ref
11300 
11301     OPEN std_gc_ref_cursor (p_document_id);
11302     FETCH std_gc_ref_cursor INTO l_is_gc_referenced;
11303     CLOSE std_gc_ref_cursor;
11304 
11305     IF (l_is_gc_referenced = 'Y') THEN
11306 
11307         check_std_gc_ref
11308         (  p_document_id      => p_document_id,
11309            p_online_report_id => p_online_report_id,
11310            p_user_id          => p_user_id,
11311            p_login_id         => p_login_id,
11312            x_sequence         => p_sequence,
11313            x_return_status    => l_return_status
11314         );
11315 
11316         IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
11317             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
11318         END IF;
11319     END IF;
11320 
11321     -- <GC FPJ END>
11322 
11323 l_progress := '002';
11324 
11325     --Call Consinged checks
11326     check_std_consigned_ref(p_document_id ,
11327                        p_online_report_id ,
11328                        p_user_id ,
11329                        p_login_id ,
11330                        p_sequence,
11331                        l_return_status);
11332 
11333     IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
11334         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
11335     END IF;
11336 
11337 l_progress := '003';
11338 
11339     -- Bug 3379488: Removed the Dest OU check for shipments with expense
11340     -- destinations with project-specified, because this scenario is prevented
11341     -- at an early stage and would not happen here
11342 
11343     x_return_status := FND_API.G_RET_STS_SUCCESS;
11344 EXCEPTION
11345     WHEN FND_API.G_EXC_ERROR THEN
11346         x_return_status := FND_API.G_RET_STS_ERROR;
11347 
11348     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
11349         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11350 
11351     WHEN OTHERS THEN
11352         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11353         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
11354             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
11355         END IF;
11356 
11357         IF (g_debug_unexp) THEN
11358                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
11359                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
11360                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
11361                        || l_progress || ' SQL CODE is '||sqlcode);
11362                 END IF;
11363         END IF;
11364 END CHECK_STANDARD_PO;
11365 
11366 
11367 /**
11368 * Private Procedure: CHECK_STD_CONSIGNED_REF
11369 * Requires:
11370 *   IN PARAMETERS:
11371 *       p_document_id:      The requisition_header_id of submitted document
11372 *       p_online_report_id: Id used to INSERT INTO online_report_text table
11373 *       p_user_id:          User performing the action
11374 *       p_login_id:         Last update login_id
11375 *   IN OUT PARAMETERS
11376 *       p_sequence:          Sequence number of last reported error
11377 * Modifies: Inserts error msgs in online_report_text_gt table, uses
11378 *           global_temp tables for processing
11379 * Effects:  This procedure performs checks for the Consigned from Supplier
11380 *           project in order to enforce the following:
11381 *           1) Consigned status on any new or modified shipment that is not
11382 *              partially received or partially invoiced should match the
11383 *              consigned setting on the corresponding ASL entry.
11384 *           2) Document references cannot exist for PO lines with one or
11385 *              more consigned shipments
11386 * Returns:
11387 *  p_sequence: This parameter contains the current count of number of error
11388 *              messages inserted
11389 *  x_return_status: This parameter signifies whether the checks contained in
11390 *                   this procedure completed successfully.
11391 */
11392 PROCEDURE check_std_consigned_ref(p_document_id IN NUMBER,
11393                        p_online_report_id IN NUMBER,
11394                        p_user_id IN NUMBER,
11395                        p_login_id IN NUMBER,
11396                        p_sequence IN OUT NOCOPY NUMBER,
11397                        x_return_status OUT NOCOPY VARCHAR2) IS
11398 
11399 l_api_name  CONSTANT varchar2(40) := 'CHECK_STD_CONSIGNED_REF';
11400 l_progress VARCHAR2(3);
11401 
11402 l_textline  po_online_report_text.text_line%TYPE    := NULL;
11403 l_consigned_from_supplier_flag
11404 po_asl_attributes.consigned_from_supplier_flag%TYPE := NULL;
11405 l_enable_vmi_flag
11406 po_asl_attributes.enable_vmi_flag%TYPE              := NULL;
11407 l_last_billing_date            DATE                 := NULL;
11408 l_consigned_billing_cycle      NUMBER               := NULL;
11409 l_consigned_mismatch_found     BOOLEAN              := FALSE;
11410 l_count_expense_dist           NUMBER               := NULL;
11411 
11412 TYPE NumTab is TABLE of NUMBER INDEX by BINARY_INTEGER;
11413 TYPE AslConsignedFlagTab is TABLE of
11414 po_asl_attributes.consigned_from_supplier_flag%TYPE
11415 INDEX by BINARY_INTEGER;
11416 TYPE ConsignedFlagTab is TABLE of
11417 po_line_locations.consigned_flag%TYPE
11418 INDEX by BINARY_INTEGER;
11419 
11420 l_vendor_id          NumTab;
11421 l_vendor_site_id     NumTab;
11422 l_item_id            NumTab;
11423 l_ship_to_org_id     NumTab;
11424 l_line_num           NumTab;
11425 l_shipment_num       NumTab;
11426 l_line_location_id   NumTab;
11427 l_consigned_flag     ConsignedFlagTab;
11428 l_asl_consigned_flag AslConsignedFlagTab;
11429 l_return_status      varchar2(1)            := NULL;
11430 l_msg_count          number                 := NULL;
11431 l_msg_data           varchar2(2000)         := NULL;
11432 
11433 BEGIN
11434 
11435 l_progress := '000';
11436 IF g_debug_stmt THEN
11437    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
11438      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
11439           || l_progress,
11440           'PO STD CONSIGNED 1: Doc Ref and Consigned ship cannot coexist');
11441    END IF;
11442 END IF;
11443 
11444   -- Check 1: PO_SUP_CONS_DOC_REF_COEXIST
11445   -- Organization cannot be used to create a consigned shipment because
11446   -- order line contains a document reference. Enter a new order line
11447   -- for this shipment.
11448 
11449 --  l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUP_CONS_DOC_REF_COEXIST');
11450 --
11451 --  INSERT INTO
11452 --    po_online_report_text_gt(online_report_id,
11453 --                       last_update_login,
11454 --           last_updated_by,
11455 --                 last_update_date,
11456 --           created_by,
11457 --           creation_date,
11458 --           line_num,
11459 --           shipment_num,
11460 --           distribution_num,
11461 --           sequence,
11462 --           text_line,
11463 --                             message_name)
11464 --     -- SQL What: Selects any PO Line with at least one consigned shipment
11465 --     --           and a document reference or contract
11466 --     -- SQL Why: Document references and consigned shipments cannot exist
11467 --     --          for the same PO Line
11468 --     -- SQL Join: po_line_id
11469 --    SELECT p_online_report_id,
11470 --     p_login_id,
11471 --     p_user_id,
11472 --         sysdate,
11473 --     p_user_id,
11474 --     sysdate,
11475 --           pol.line_num,
11476 --     pll.shipment_num,
11477 --     0,
11478 --           p_sequence + ROWNUM,
11479 --     substr(g_linemsg||g_delim||POL.line_num||g_delim||g_shipmsg
11480 --     ||g_delim||PLL.shipment_num||':'||g_delim||l_textline,1,240),
11481 --     'PO_DOC_REF_SUP_CONS_COEXIST'
11482 --    FROM   po_lines_gt pol,
11483 --     po_line_locations_gt pll
11484 --    WHERE  pol.po_header_id = p_document_id
11485 --    AND    pol.po_line_id = pll.po_line_id
11486 --    AND    pll.shipment_type = 'STANDARD'
11487 --    AND    pll.consigned_flag = 'Y'
11488 --    AND    nvl(pol.cancel_flag,'N') = 'N'
11489 --    AND    nvl(pol.closed_code,'OPEN') <> 'FINALLY CLOSED'
11490 --    AND   (pol.oke_contract_header_id is not null or
11491 --           pol.oke_contract_version_id is not null or
11492 --     pol.from_header_id is not null or
11493 --     pol.from_line_id is not null or
11494 --     pol.contract_num is not null);
11495 --
11496 --  --Increment the p_sequence with number of errors reported in last query
11497 --  p_sequence := p_sequence + SQL%ROWCOUNT;
11498 --------------------------------------
11499 
11500 l_progress := '002';
11501 IF g_debug_stmt THEN
11502    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
11503      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
11504           || l_progress,
11505           'PO STD CONSIGNED 2: Consigned status on shipments and ASL must match');
11506    END IF;
11507 END IF;
11508 
11509   -- Check 2: PO_SUP_CONS_STATUS_MISMATCH
11510   -- Consigned attribute and the ASL consigned attribute do not match.
11511   -- Shipment line must be cancelled and re-entered.'
11512 
11513   -- SQL What: Collect the vendor_id, vendor_site_id, item_id, ship_to_organization_id and
11514   --           consigned_flag into pl/sql tables, get the consigned_from_supplier_flag on
11515   --           the corresponding ASL entry for each shipment and match it to the consigned
11516   --           flag of the shipment.  The consigned_flag of the shipments are stored in a
11517   --           PL/SQL table.  If the item/supplier/supplier site/organization combination
11518   --           of the current shipment matches with that of the previous shipment, get the
11519   --           consigned_flag from the previous shipment instead of calling
11520   --           get_asl_attributes.  This can be done since the info of each shipment of
11521   --           the current PO to be approved is ordered by the vendor_id, vendor_site_id,
11522   --           item_id and ship_to_organization_id, in the bulk collct select statement.
11523   -- SQL Why: Consigned status on shipments and that on ASL must match
11524   -- SQL Join: po_header_id, po_line_id
11525 
11526   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUP_CONS_STATUS_MISMATCH');
11527 
11528   SELECT  POH.vendor_id,
11529           POH.vendor_site_id,
11530           POL.item_id,
11531     PLL.ship_to_organization_id,
11532     PLL.consigned_flag,
11533     POL.line_num,
11534     PLL.shipment_num,
11535           PLL.line_location_id
11536   BULK COLLECT INTO
11537           l_vendor_id,
11538           l_vendor_site_id,
11539           l_item_id,
11540           l_ship_to_org_id,
11541           l_consigned_flag,
11542     l_line_num,
11543     l_shipment_num,
11544           l_line_location_id
11545   FROM    PO_HEADERS_GT POH,
11546           PO_LINE_LOCATIONS_GT PLL,
11547           PO_LINES_GT POL
11548   WHERE   POH.po_header_id = p_document_id AND
11549           POH.po_header_id = POL.po_header_id AND
11550 --          POH.po_header_id = PLL.po_header_id AND
11551           POL.po_line_id = PLL.po_line_id AND
11552           PLL.shipment_type = 'STANDARD' AND
11553           nvl(PLL.cancel_flag,'N') = 'N' AND
11554           nvl(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED' AND
11555           -- Bug fix for #2733398
11556           -- nvl(PLL.approved_flag, 'N') IN ('N','R') AND
11557           nvl(PLL.approved_flag, 'N') = 'N' AND
11558           PLL.quantity_received <= 0 AND
11559           PLL.quantity_billed <= 0
11560   ORDER BY
11561           POH.vendor_id,
11562           POH.vendor_site_id,
11563           POL.item_id,
11564     PLL.ship_to_organization_id;
11565 
11566 l_progress := '003';
11567   FOR i IN 1..l_line_num.COUNT
11568   LOOP
11569 
11570     IF(i > 1 AND
11571        l_vendor_id(i) = l_vendor_id(i - 1) AND
11572        l_vendor_site_id(i) = l_vendor_site_id(i - 1) AND
11573        l_item_id(i) = l_item_id(i - 1) AND
11574        l_ship_to_org_id(i) = l_ship_to_org_id(i - 1))
11575     THEN
11576       l_asl_consigned_flag(i) := l_asl_consigned_flag(i - 1);
11577     ELSE
11578       -- getting the consigned setting of the ASL entry for the
11579       -- item/supplier/supplier site/organization combination,
11580       -- if there exists one
11581       po_third_party_stock_grp.get_asl_attributes
11582       (p_api_version                  => 1.0                           ,
11583        p_init_msg_list                => NULL                          ,
11584        x_return_status                => l_return_status               ,
11585        x_msg_count                    => l_msg_count                   ,
11586        x_msg_data                     => l_msg_data                    ,
11587        p_inventory_item_id            => l_item_id(i)                  ,
11588        p_vendor_id                    => l_vendor_id(i)                ,
11589        p_vendor_site_id               => l_vendor_site_id(i)           ,
11590        p_using_organization_id        => l_ship_to_org_id(i)           ,
11591        x_consigned_from_supplier_flag => l_consigned_from_supplier_flag,
11592        x_enable_vmi_flag              => l_enable_vmi_flag             ,
11593        x_last_billing_date            => l_last_billing_date           ,
11594        x_consigned_billing_cycle      => l_consigned_billing_cycle     );
11595 
11596       l_asl_consigned_flag(i) := l_consigned_from_supplier_flag;
11597     END IF;
11598 
11599 l_progress := '004';
11600     IF(NVL(l_consigned_flag(i), 'N') <> NVL(l_asl_consigned_flag(i), 'N'))
11601     THEN
11602       -- Bug fix for #2701648
11603       -- Do not report consigned status mismatch for the case when the ASL
11604       -- is consigned while the shipment is not consigned, if there exists
11605       -- distributions with destination type being EXPENSE
11606       IF(NVL(l_consigned_flag(i), 'N') = 'N' AND
11607          NVL(l_asl_consigned_flag(i), 'N') = 'Y')
11608       THEN
11609         SELECT count('Y')
11610         INTO   l_count_expense_dist
11611         FROM   DUAL
11612         WHERE  EXISTS(SELECT 'Y'
11613                       FROM   PO_DISTRIBUTIONS_GT
11614                       WHERE  LINE_LOCATION_ID = l_line_location_id(i)
11615                       AND    DESTINATION_TYPE_CODE = 'EXPENSE');
11616 
11617         IF(l_count_expense_dist <= 0)
11618         THEN
11619           l_consigned_mismatch_found := TRUE;
11620         ELSE
11621           l_consigned_mismatch_found := FALSE;
11622         END IF;
11623       ELSE
11624         l_consigned_mismatch_found := TRUE;
11625       END IF;
11626     -- if the consigned status matches
11627     ELSE
11628       l_consigned_mismatch_found := FALSE;
11629     END IF;
11630 
11631     -- insert an error into the report table if the consigned status on the
11632     -- ASL and the shipment does not match
11633     IF(l_consigned_mismatch_found)
11634     THEN
11635       INSERT INTO
11636         po_online_report_text_gt
11637         (online_report_id,
11638          last_update_login,
11639    last_updated_by,
11640    last_update_date,
11641    created_by,
11642    creation_date,
11643    line_num,
11644    shipment_num,
11645    distribution_num,
11646    sequence,
11647    text_line,
11648          message_name)
11649       VALUES
11650   (p_online_report_id,
11651    p_login_id,
11652    p_user_id,
11653    sysdate,
11654    p_user_id,
11655    sysdate,
11656          l_line_num(i),
11657    l_shipment_num(i),
11658    0,
11659    p_sequence + i,
11660          substr(g_linemsg||g_delim||l_line_num(i)||g_delim||g_shipmsg
11661                 ||g_delim||l_shipment_num(i)||':'||g_delim||l_textline,1,240),
11662    'PO_SUP_CONS_STATUS_MISMATCH');
11663 
11664     END IF;
11665 
11666   END LOOP;
11667 
11668   --Increment the p_sequence with number of errors reported in last query
11669   p_sequence := p_sequence + l_line_num.COUNT;
11670 
11671 l_progress := '005';
11672     x_return_status := FND_API.G_RET_STS_SUCCESS;
11673 EXCEPTION
11674     WHEN FND_API.G_EXC_ERROR THEN
11675         x_return_status := FND_API.G_RET_STS_ERROR;
11676 
11677     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
11678         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11679 
11680     WHEN OTHERS THEN
11681         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11682         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
11683             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
11684         END IF;
11685 
11686         IF (g_debug_unexp) THEN
11687                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
11688                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
11689                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
11690                        || l_progress || ' SQL CODE is '||sqlcode);
11691                 END IF;
11692         END IF;
11693 
11694 END CHECK_STD_CONSIGNED_REF;
11695 
11696 
11697 --For Standard POs
11698 /**
11699 * Private Procedure: CHECK_STD_GLOBAL_REF
11700 * Requires:
11701 *   IN PARAMETERS:
11702 *       p_document_id:      The requisition_header_id of submitted document
11703 *       p_online_report_id: Id used to INSERT INTO online_report_text table
11704 *       p_user_id:          User performing the action
11705 *       p_login_id:         Last update login_id
11706 *   IN OUT PARAMETERS
11707 *       p_sequence:          Sequence number of last reported error
11708 * Modifies: Inserts error msgs in online_report_text_gt table, uses
11709 *           global_temp tables for processing
11710 * Effects:  This procedure runs the document submission checks for Standard
11711 *           POs which have GLOBAL AGREEMENTS reference
11712 * Returns:
11713 *  p_sequence: This parameter contains the current count of number of error
11714 *              messages inserted
11715 */
11716 PROCEDURE check_std_global_ref(p_document_id IN NUMBER,
11717                        p_online_report_id IN NUMBER,
11718                        p_user_id IN NUMBER,
11719                        p_login_id IN NUMBER,
11720                        p_sequence IN OUT NOCOPY NUMBER,
11721                        x_return_status OUT NOCOPY VARCHAR2) IS
11722 
11723 l_textline  po_online_report_text.text_line%TYPE := NULL;
11724 l_api_name  CONSTANT varchar2(40) := 'CHECK_STD_GLOBAL_REF';
11725 l_progress VARCHAR2(3);
11726 l_currency_mismatch BOOLEAN := FALSE; -- Bug 2716769
11727 
11728 --<Bug 2800804, 2792477 mbhargav START>
11729 TYPE NumTab is TABLE of NUMBER INDEX by BINARY_INTEGER;
11730 
11731   l_curr_doc_line_num NumTab;
11732   l_prev_doc_line_num NumTab;
11733   l_rowcount Number :=0;
11734 
11735   l_po_amount NumTab;
11736   l_prev_rel_amount NumTab;
11737   l_amount_limit NumTab;
11738 --<Bug 2800804, 2792477 mbhargav END>
11739 
11740 BEGIN
11741 l_progress := '000';
11742 
11743 l_progress := '001';
11744 IF g_debug_stmt THEN
11745    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
11746      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
11747           || l_progress,'PO STD GA 1: GA enabled in current OU');
11748    END IF;
11749 END IF;
11750 
11751     --<Shared Proc FPJ>
11752     --Check 1: The GA should be enabled for purchasing in the current OU.
11753     --< Shared Proc FPJ > Bug 3301427: Only do this check for new SPO lines
11754     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_STD_GA_DISABLED');
11755     INSERT into po_online_report_text_gt(online_report_id,
11756                                          last_update_login,
11757                                          last_updated_by,
11758                                          last_update_date,
11759                                          created_by,
11760                                          creation_date,
11761                                          line_num,
11762                                          shipment_num,
11763                                          distribution_num,
11764                                          sequence,
11765                                          text_line,
11766                                          message_name)
11767     --<Shared Proc FPJ>
11768     --SQL Querying for PO lines that reference GAs that are not enabled
11769     --SQL for purchasing in this org, to report an error message.
11770     SELECT  p_online_report_id,
11771             p_login_id,
11772             p_user_id,
11773             sysdate,
11774             p_user_id,
11775             sysdate,
11776             POL.line_num,
11777             0,
11778             0,
11779             p_sequence + ROWNUM,
11780             substr(g_linemsg||g_delim||POL.line_num||g_delim
11781                    ||l_textline,1,240),
11782             'PO_SUB_STD_GA_DISABLED'
11783     FROM PO_HEADERS_GT POH1, PO_LINES_GT POL, PO_HEADERS_ALL POH
11784     WHERE POH1.po_header_id = p_document_id
11785     AND POL.po_header_id = POH1.po_header_id
11786     AND POL.from_header_id = POH.po_header_id  --JOIN
11787     AND POH.type_lookup_code = 'BLANKET'
11788     AND POH.global_agreement_flag = 'Y'
11789     --<Shared Proc FPJ START>
11790     AND NOT EXISTS                                  --< Bug 3301427 Start >
11791         (SELECT 'previously approved shipment'
11792            FROM po_line_locations_gt pllg
11793           WHERE pllg.po_line_id = pol.po_line_id
11794             AND pllg.approved_date IS NOT NULL)     --< Bug 3301427 End >
11795     AND NOT EXISTS
11796         --SQL Query enabled org assignments of this current purchasing org
11797        (SELECT 'Enabled purchasing org'
11798           FROM PO_GA_ORG_ASSIGNMENTS PGOA
11799          WHERE PGOA.po_header_id = POH.po_header_id
11800            AND PGOA.purchasing_org_id = POH1.org_id
11801            AND PGOA.enabled_flag = 'Y');
11802 
11803     --<Shared Proc FPJ END>
11804 
11805      --Increment the p_sequence with number of errors reported in last query
11806     p_sequence := p_sequence + SQL%ROWCOUNT;
11807 
11808 -----------------------------
11809 l_progress := '002';
11810 IF g_debug_stmt THEN
11811    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
11812      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
11813           || l_progress,'PO STD GA 2: Ref GA approved');
11814    END IF;
11815 END IF;
11816 
11817     --Check 2: If the standard PO line is referencing a GA then that GA
11818     --should be in an approved status.
11819     --< Shared Proc FPJ > Bug 3301427: Only do this check for new SPO lines
11820     --< Bug 3422733 > Only do this check if GA is not ON HOLD. The ON HOLD check
11821     -- is done later. Avoids showing 2 msgs for Contract that is ON HOLD.
11822 
11823     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_STD_GA_APPROVED');
11824     INSERT into po_online_report_text_gt(online_report_id,
11825                                          last_update_login,
11826                                          last_updated_by,
11827                                          last_update_date,
11828                                          created_by,
11829                                          creation_date,
11830                                          line_num,
11831                                          shipment_num,
11832                                          distribution_num,
11833                                          sequence,
11834                                          text_line,
11835                                          message_name)
11836     --SQL What: Querying for PO lines that reference GAs that are not approved.
11837     --SQL Why: Add appropriate error message to po_online_report_text_gt
11838     SELECT  p_online_report_id,
11839             p_login_id,
11840             p_user_id,
11841             sysdate,
11842             p_user_id,
11843             sysdate,
11844             POL.line_num,
11845             0,
11846             0,
11847             p_sequence + ROWNUM,
11848             substr(g_linemsg||g_delim||POL.line_num||g_delim
11849                    ||l_textline,1,240),
11850             'PO_SUB_STD_GA_APPROVED'
11851     FROM PO_HEADERS_GT POH1, PO_LINES_GT POL, PO_HEADERS_ALL POH2
11852     WHERE POH1.po_header_id = p_document_id
11853     AND POL.po_header_id = POH1.po_header_id   --JOIN
11854     AND POH2.po_header_id = POL.from_header_id --JOIN
11855     AND POH2.type_lookup_code = 'BLANKET'
11856     AND POH2.global_agreement_flag = 'Y'
11857     AND NVL(POH2.approved_flag, 'N') <> 'Y'
11858     AND NVL(POH2.user_hold_flag, 'N') <> 'Y'        --< Bug 3422733 >
11859     AND NOT EXISTS                                  --< Bug 3301427 Start >
11860         (SELECT 'previously approved shipment'
11861            FROM po_line_locations_gt pllg
11862           WHERE pllg.po_line_id = pol.po_line_id
11863             AND pllg.approved_date IS NOT NULL);    --< Bug 3301427 End >
11864 
11865     --Increment the p_sequence with number of errors reported in last query
11866     p_sequence := p_sequence + SQL%ROWCOUNT;
11867 
11868 --------------------------------------
11869 
11870 l_progress := '003';
11871 IF g_debug_stmt THEN
11872    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
11873      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
11874           || l_progress,'PO STD GA 3: Ref GA ON HOLD');
11875    END IF;
11876 END IF;
11877 
11878     --Check 3: The GA should not be on hold.
11879     --< Shared Proc FPJ > Bug 3301427: Only do this check for new SPO lines
11880     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_STD_GA_ON_HOLD');
11881     INSERT into po_online_report_text_gt(online_report_id,
11882                                          last_update_login,
11883                                          last_updated_by,
11884                                          last_update_date,
11885                                          created_by,
11886                                          creation_date,
11887                                          line_num,
11888                                          shipment_num,
11889                                          distribution_num,
11890                                          sequence,
11891                                          text_line,
11892                                          message_name)
11893     --SQL What: Querying for PO lines that reference GAs that are on hold
11894     --SQL Why: Add appropriate error message to po_online_report_text_gt
11895     SELECT  p_online_report_id,
11896             p_login_id,
11897             p_user_id,
11898             sysdate,
11899             p_user_id,
11900             sysdate,
11901             POL.line_num,
11902             0,
11903             0,
11904             p_sequence + ROWNUM,
11905             substr(g_linemsg||g_delim||POL.line_num||g_delim
11906                    ||l_textline,1,240),
11907             'PO_SUB_STD_GA_ON_HOLD'
11908     FROM PO_HEADERS_GT POH1, PO_LINES_GT POL, PO_HEADERS_ALL POH2
11909     WHERE POH1.po_header_id = p_document_id
11910     AND POL.po_header_id = POH1.po_header_id   --JOIN
11911     AND POH2.po_header_id = POL.from_header_id --JOIN
11912     AND POH2.type_lookup_code = 'BLANKET'
11913     AND POH2.global_agreement_flag = 'Y'
11914     AND POH2.user_hold_flag = 'Y'
11915     AND NOT EXISTS                                  --< Bug 3301427 Start >
11916         (SELECT 'previously approved shipment'
11917            FROM po_line_locations_gt pllg
11918           WHERE pllg.po_line_id = pol.po_line_id
11919             AND pllg.approved_date IS NOT NULL);    --< Bug 3301427 End >
11920 
11921      --Increment the p_sequence with number of errors reported in last query
11922     p_sequence := p_sequence + SQL%ROWCOUNT;
11923 
11924 ------------------------------------------
11925 
11926    -- Bug 2716769 tpoon 12/26/2002
11927    -- In version 115.8, moved Checks 4 and 5 to after the currency check.
11928    -- Re-numbered checks 4-10 accordingly.
11929 
11930 l_progress := '004';
11931 IF g_debug_stmt THEN
11932    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
11933      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
11934           || l_progress,'PO STD GA 4: Vendor match check');
11935    END IF;
11936 END IF;
11937 
11938     --Check 4: The vendor on the PO should match the GA.
11939     --< CLM Novation > bug13059027 - This check is a Warning for OrderOfIDV Modifications
11940     -- or Re-approval Base docs whose Supplier is different from that of referenced IDV
11941     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_STD_GA_VENDOR_MISMATCH');
11942     INSERT into po_online_report_text_gt(online_report_id,
11943                                          last_update_login,
11944                                          last_updated_by,
11945                                          last_update_date,
11946                                          created_by,
11947                                          creation_date,
11948                                          line_num,
11949                                          shipment_num,
11950                                          distribution_num,
11951                                          sequence,
11952                                          text_line,
11953                                          message_name,
11954                                          message_type)
11955     --SQL What: Querying for PO lines whose vendor does not
11956     --SQL       match the vendor of the referenced GA
11957     --SQL Why: Add appropriate error message to po_online_report_text_gt
11958     SELECT  p_online_report_id,
11959             p_login_id,
11960             p_user_id,
11961             sysdate,
11962             p_user_id,
11963             sysdate,
11964             POL.line_num,
11965             0,
11966             0,
11967             p_sequence + ROWNUM,
11968             substr(g_linemsg||g_delim||POL.line_num||g_delim
11969                    ||l_textline,1,240),
11970             'PO_SUB_STD_GA_VENDOR_MISMATCH',
11971             Decode(is_approved_clm_document(p_document_id), 'Y', 'W', NULL)
11972     FROM PO_LINES_GT POL, PO_HEADERS_GT POH1, PO_HEADERS_ALL POH2
11973     WHERE POL.po_header_id = p_document_id
11974     AND POL.po_header_id = POH1.po_header_id      --JOIN
11975     AND POL.from_header_id = POH2.po_header_id    --JOIN
11976     AND POH2.type_lookup_code = 'BLANKET'
11977     AND POH2.global_agreement_flag = 'Y'
11978     AND POH1.vendor_id <> POH2.vendor_id;
11979 
11980      --Increment the p_sequence with number of errors reported in last query
11981     p_sequence := p_sequence + SQL%ROWCOUNT;
11982 
11983 -------------------------------------
11984 
11985 l_progress := '005';
11986 IF g_debug_stmt THEN
11987    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
11988      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
11989           || l_progress,'PO STD GA 5: Vendor Site match check');
11990    END IF;
11991 END IF;
11992 
11993     --Check 5: The vendor site on the PO should match a vendor site on one of
11994     -- the GA's enabled org assignments.
11995     --< Shared Proc FPJ > Bug 3301427: Only do this check for new SPO lines
11996     --< CLM Novation > bug13059027 - This check is a Warning for OrderOfIDV Modifications
11997     -- or Re-approval Base docs whose Supplier is different from that of referenced IDV
11998     l_textline :=
11999         FND_MESSAGE.GET_STRING('PO', 'PO_SUB_STD_GA_VDR_SITE_MISMT');
12000     INSERT into po_online_report_text_gt(online_report_id,
12001                                          last_update_login,
12002                                          last_updated_by,
12003                                          last_update_date,
12004                                          created_by,
12005                                          creation_date,
12006                                          line_num,
12007                                          shipment_num,
12008                                          distribution_num,
12009                                          sequence,
12010                                          text_line,
12011                                          message_name,
12012                                          message_type)
12013     --SQL What: Querying for PO lines whose vendor_site_id does not
12014     --SQL       match a valid vendor_site_id in the GA's org assignments
12015     --SQL Why: Add appropriate error message to po_online_report_text_gt
12016     SELECT  p_online_report_id,
12017             p_login_id,
12018             p_user_id,
12019             sysdate,
12020             p_user_id,
12021             sysdate,
12022             POL.line_num,
12023             0,
12024             0,
12025             p_sequence + ROWNUM,
12026             substr(g_linemsg||g_delim||POL.line_num||g_delim
12027                    ||l_textline,1,240),
12028             'PO_SUB_STD_GA_VDR_SITE_MISMT',
12029             Decode(is_approved_clm_document(p_document_id), 'Y', Decode(POH1.vendor_id, POH2.vendor_id, NULL, 'W'), NULL)
12030     FROM PO_LINES_GT POL, PO_HEADERS_GT POH1, PO_HEADERS_ALL POH2
12031     WHERE POL.po_header_id = p_document_id
12032     AND POL.po_header_id = POH1.po_header_id            --JOIN
12033     AND POL.from_header_id = POH2.po_header_id          --JOIN
12034     AND POH2.type_lookup_code = 'BLANKET'
12035     AND POH2.global_agreement_flag = 'Y'
12036     --<Shared Proc FPJ START>
12037     AND NOT EXISTS                                  --< Bug 3301427 Start >
12038         (SELECT 'previously approved shipment'
12039            FROM po_line_locations_gt pllg
12040           WHERE pllg.po_line_id = pol.po_line_id
12041             AND pllg.approved_date IS NOT NULL)     --< Bug 3301427 End >
12042     AND NOT EXISTS
12043         (SELECT 'Enabled vendor site'
12044            FROM PO_GA_ORG_ASSIGNMENTS pgoa
12045           WHERE PGOA.po_header_id = POH2.po_header_id
12046             AND PGOA.vendor_site_id = POH1.vendor_site_id
12047             AND PGOA.enabled_flag = 'Y');
12048 
12049     --<Shared Proc FPJ END>
12050 
12051      --Increment the p_sequence with number of errors reported in last query
12052     p_sequence := p_sequence + SQL%ROWCOUNT;
12053 
12054 ---------------------------------------
12055 
12056 l_progress := '006';
12057 IF g_debug_stmt THEN
12058    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
12059      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
12060           || l_progress,'PO STD GA 6: Creation date check on header');
12061    END IF;
12062 END IF;
12063 
12064     --Check 6: The PO creation date should be before Blanket(GA) or
12065     -- corresponding Line expiration date
12066     --Bug8847964 the need by date should be just greater than the start date of the blanket
12067     -- and can be later to the end date.
12068     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_STD_AFTER_GA_DATE');
12069     INSERT into po_online_report_text_gt(online_report_id,
12070                                          last_update_login,
12071                                          last_updated_by,
12072                                          last_update_date,
12073                                          created_by,
12074                                          creation_date,
12075                                          line_num,
12076                                          shipment_num,
12077                                          distribution_num,
12078                                          sequence,
12079                                          text_line,
12080                                          message_name)
12081     --SQL What: Querying for PO lines that were not created within
12082     --SQL       the effective dates of the referenced GA
12083     --SQL Why: Add appropriate error message to po_online_report_text_gt
12084     SELECT  p_online_report_id,
12085             p_login_id,
12086             p_user_id,
12087             sysdate,
12088             p_user_id,
12089             sysdate,
12090             POL.line_num,
12091             PLL.shipment_num, -- Bug #5415428 As Need by date also forms part of the checks
12092             0,
12093             p_sequence + ROWNUM,
12094             substr(g_linemsg||g_delim||POL.line_num||g_delim
12095                    ||l_textline,1,240),
12096             'PO_SUB_STD_AFTER_GA_DATE'
12097     FROM PO_LINES_GT POL, PO_HEADERS_GT POH, PO_HEADERS_ALL POH2, PO_LINES_ALL POL2, PO_LINE_LOCATIONS_GT PLL
12098     WHERE POL.po_header_id = p_document_id
12099     AND POL.po_header_id = POH.po_header_id -- JOIN
12100     AND PLL.po_line_id = POL.po_line_id  --JOIN, Bug #5415428 - Get the Need by date
12101     AND POL.from_header_id = POH2.po_header_id --JOIN
12102     AND POL.from_line_id = POL2.po_line_id     --JOIN
12103     AND POH2.type_lookup_code = 'BLANKET'
12104     AND POH2.global_agreement_flag = 'Y'
12105     AND Nvl(pol.cancel_flag,'N') = 'N'  --Bug8847964
12106     AND Nvl(pol.closed_code,'OPEN') <> 'FINALLY CLOSED' --Bug8847964
12107 --Bug #2699630: Adding trunc on both sides of the check
12108 --Bug #5415428: Start date and need by date also needs to be considered
12109   -- Bug #13550798: Submit Date cannot be later then effective to date of Source Document
12110     /* AND (NVL(TRUNC(POL.start_date), TRUNC(POH.creation_date))
12111          > NVL ( TRUNC(POL2.expiration_date) , TRUNC(POH2.end_date))); */
12112      AND TRUNC(sysdate)
12113          > NVL ( TRUNC(POL2.expiration_date) , TRUNC(POH2.end_date+ nvl(FND_PROFILE.VALUE('PO_REL_CREATE_TOLERANCE'),0)))
12114     AND Nvl(PLL.approved_flag,'N') <> 'Y'; --Bug 16493253
12115 
12116      --Increment the p_sequence with number of errors reported in last query
12117     p_sequence := p_sequence + SQL%ROWCOUNT;
12118 
12119 /*Bug # 13550798 - Commenting to remove check on 'effective from date' of the source document*/
12120 ------------------------------------
12121 /*
12122 l_progress := '007';
12123 IF g_debug_stmt THEN
12124    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
12125      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
12126           || l_progress,'PO STD GA 7: Need-by-date and Creation date check');
12127    END IF;
12128 END IF;
12129 
12130     --Check 7: The Need-by-date (or if NULL the PO creation date) should be
12131     -- after the start dates of the GA
12132     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_STD_BEFORE_GA_DATE');
12133     INSERT into po_online_report_text_gt(online_report_id,
12134                                          last_update_login,
12135                                          last_updated_by,
12136                                          last_update_date,
12137                                          created_by,
12138                                          creation_date,
12139                                          line_num,
12140                                          shipment_num,
12141                                          distribution_num,
12142                                          sequence,
12143                                          text_line,
12144                                          message_name)
12145     --SQL What: Querying for PO lines that were not created within
12146     --SQL       the effective dates of the referenced GA
12147     --SQL Why: Add appropriate error message to po_online_report_text_gt
12148     SELECT  p_online_report_id,
12149             p_login_id,
12150             p_user_id,
12151             sysdate,
12152             p_user_id,
12153             sysdate,
12154             POL.line_num,
12155             PLL.shipment_num,
12156             0,
12157             p_sequence + ROWNUM,
12158             substr(g_linemsg||g_delim||POL.line_num||g_delim||
12159                    g_shipmsg||g_delim||PLL.shipment_num||g_delim||l_textline,1,240),
12160             'PO_SUB_STD_BEFORE_GA_DATE'
12161     FROM PO_LINES_GT POL, PO_HEADERS_GT POH, PO_HEADERS_ALL POH2, PO_LINE_LOCATIONS_GT PLL
12162     WHERE POL.po_header_id = p_document_id
12163     AND POL.po_header_id = POH.po_header_id
12164     AND   PLL.po_line_id = POL.po_line_id  --JOIN
12165     AND POL.from_header_id = POH2.po_header_id --JOIN
12166     AND POH2.type_lookup_code = 'BLANKET'
12167     AND POH2.global_agreement_flag = 'Y'
12168 --Bug #2699630: Adding trunc on both sides of the check
12169 --Bug #5415428: Start date also needs to be considered
12170     AND NVL(TRUNC(PLL.need_by_date), NVL(TRUNC(POL.start_date),TRUNC(POH.creation_date))) < TRUNC(POH2.start_date);
12171 
12172      --Increment the p_sequence with number of errors reported in last query
12173     p_sequence := p_sequence + SQL%ROWCOUNT;
12174 */
12175 
12176 ---------------------------------
12177 l_progress := '008';
12178 IF g_debug_stmt THEN
12179    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
12180      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
12181           || l_progress,'PO STD GA 8: Currency code mismatch');
12182    END IF;
12183 END IF;
12184 
12185     --Check 8: The currency_code on the PO should match the GA.
12186     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_STD_GA_CURR_MISMATCH');
12187     INSERT into po_online_report_text_gt(online_report_id,
12188                                          last_update_login,
12189                                          last_updated_by,
12190                                          last_update_date,
12191                                          created_by,
12192                                          creation_date,
12193                                          line_num,
12194                                          shipment_num,
12195                                          distribution_num,
12196                                          sequence,
12197                                          text_line,
12198                                          message_name)
12199     --SQL What: Querying for PO lines whose currency code does not
12200     --SQL       match the currency code of the referenced GA
12201     --SQL Why: Add appropriate error message to po_online_report_text_gt
12202     SELECT  p_online_report_id,
12203             p_login_id,
12204             p_user_id,
12205             sysdate,
12206             p_user_id,
12207             sysdate,
12208             POL.line_num,
12209             0,
12210             0,
12211             p_sequence + ROWNUM,
12212             substr(g_linemsg||g_delim||POL.line_num||g_delim
12213                    ||l_textline,1,240),
12214             'PO_SUB_STD_GA_CURR_MISMATCH'
12215     FROM PO_LINES_GT POL, PO_HEADERS_GT POH1, PO_HEADERS_ALL POH2
12216     WHERE POL.po_header_id = p_document_id
12217     AND POL.po_header_id = POH1.po_header_id      --JOIN
12218     AND POL.from_header_id = POH2.po_header_id    --JOIN
12219     AND POH2.type_lookup_code = 'BLANKET'
12220     AND POH2.global_agreement_flag = 'Y'
12221     AND POH1.currency_code <> POH2.currency_code;
12222 
12223      --Increment the p_sequence with number of errors reported in last query
12224     p_sequence := p_sequence + SQL%ROWCOUNT;
12225 
12226     -- Bug 2716769 tpoon 12/26/2002
12227     -- If there is a currency mismatch, do not perform the currency-dependent
12228     -- checks below. These checks assume that the PO and the GA use the same
12229     -- currency.
12230     IF (SQL%ROWCOUNT > 0) THEN
12231       l_currency_mismatch := TRUE;
12232     END IF;
12233 
12234     -- Bug 2716769 tpoon 12/26/2002
12235     -- In version 115.8, the following checks were moved here. They will
12236     -- only be performed if the currency check is successful.
12237     --   Check 4: Total amount on PO shipments should be less than the GA
12238     --     amount limit.
12239     --   Check 5: If price override is yes, then the line unit price cannot
12240     --     be more than the price tolerance on the GA line.
12241     -- Re-numbered checks 4-10 accordingly.
12242 
12243 -----------------------------------------
12244 
12245     -- Bug 2716769 Only perform this check if PO and GA use the same currency
12246     IF (NOT l_currency_mismatch) THEN
12247 
12248       l_progress := '009';
12249 
12250       IF g_debug_stmt THEN
12251         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
12252           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
12253           || l_progress,'PO STD GA 9: Amt limit check for GA header');
12254         END IF;
12255       END IF;
12256 
12257       --bug11802312 Amount limit calculation should not include consigned shipments
12258       --Added this check as we are retaining the document reference on consigned shipments
12259       --as well.
12260       --Check 9: The total amount on all the standard PO SHIPMENT lines in the
12261       --current setup referencing the same GA should be less than the
12262       --amount limit on the GA header
12263 
12264      --<Bug 2800804, 2792477 mbhargav END>
12265       --Split the amount calculation to two select statements,
12266       --this was required because iSP is sending some chnage_requests
12267       --which are only in GT tables so need to get the amount for
12268       --current document from GT table and not the base tables
12269 
12270       --Get the sum of all shipments of this PO which refer to same GA
12271       --as is referenced on given line
12272       -- Bug 2818810. Also get the amount limits for those GA's here. Removed
12273       -- the extra select statement that was retrieving the amount limit.
12274       SELECT /*+ FULL(POL) ORDERED */                         -- bug3413891
12275              POL.line_num
12276       ,      nvl ( decode ( POL.quantity                      -- <SERVICES FPJ>
12277                           , NULL , sum ( PLL1.amount
12278                                        - nvl(PLL1.amount_cancelled,0) )
12279                           ,        sum (   ( PLL1.quantity
12280                                            - nvl(PLL1.quantity_cancelled,0) )
12281                                        * PLL1.price_override )
12282                           )
12283                  , 0 )
12284       ,      POH1.amount_limit
12285       BULK COLLECT INTO l_curr_doc_line_num,l_po_amount,l_amount_limit
12286       FROM   PO_LINES_GT POL, PO_HEADERS_ALL POH1, PO_LINE_LOCATIONS_GT PLL1
12287       WHERE  PLL1.from_header_id = POL.from_header_id
12288       AND    POL.po_header_id = p_document_id
12289       AND    POL.from_header_id = POH1.po_header_id
12290       AND    POH1.type_lookup_code = 'BLANKET'
12291       AND    POH1.global_agreement_flag = 'Y'
12292       AND    POH1.amount_limit IS NOT NULL
12293       AND    Nvl(PLL1.consigned_flag,'N') <> 'Y' --bug11802312
12294       GROUP BY
12295              POL.line_num
12296       ,      POL.quantity                                     -- <SERVICES FPJ>
12297       ,      POH1.amount_limit;
12298 
12299       --Get the sum of all shipments of approved POs in current setup which refer to same
12300       --GA as is referenced on given line
12301       -- Bug 2818810. Added extra join to aliases POH1 and PLL to return rows of
12302       -- given lines with shipments that reference GA's with amount limits only.
12303 
12304       -- bug3413891
12305       -- 1) Added optimizer hint FULL(POL)
12306       -- 2) Removed the join to PO_LINE_LOCATIONS_GT as it will bring in wrong
12307       --    result if the standard PO line has multiple shipments
12308 
12309       SELECT /*+ FULL(POL) */
12310              POL.line_num
12311       ,      nvl ( decode ( POL.quantity                      -- <SERVICES FPJ>
12312                           , NULL , sum ( PLL2.amount
12313                                        - nvl(PLL2.amount_cancelled,0) )
12314                           ,        sum (   ( PLL2.quantity
12315                                            - nvl(PLL2.quantity_cancelled,0) )
12316                                        * PLL2.price_override )
12317                           )
12318                  , 0 )
12319       BULK COLLECT INTO l_prev_doc_line_num, l_prev_rel_amount
12320       FROM PO_LINE_LOCATIONS_ALL PLL2, PO_HEADERS_ALL POH1,
12321            PO_HEADERS_ALL POH2, PO_LINES_GT POL
12322       WHERE POL.po_header_id = p_document_id
12323       AND   POL.from_header_id = POH1.po_header_id    --JOIN
12324       AND   POH1.type_lookup_code = 'BLANKET'
12325       AND   POH1.global_agreement_flag = 'Y'
12326       AND   POH1.amount_limit IS NOT NULL
12327       AND   PLL2.from_header_id = POL.from_header_id  --JOIN
12328       AND   POH2.po_header_id = PLL2.po_header_id     --JOIN
12329       AND   nvl(POH2.approved_flag, 'N') = 'Y'
12330       AND   PLL2.po_header_id <> p_document_id
12331       AND   Nvl(PLL2.consigned_flag,'N') <> 'Y'  --bug11802312
12332       GROUP BY
12333             POL.line_num
12334       ,     POL.quantity;                                     -- <SERVICES FPJ>
12335 
12336       --Addup the released amount as obtained from above sqls. This loop goes by prev_doc_line_num.COUNT
12337       --as its possible we may not have any previous POs cut against those GAs.
12338       FOR l_prev_index IN 1..l_prev_doc_line_num.COUNT LOOP
12339 
12340           -- Bug 2818810. Fixed looping and the indexing used to ensure that
12341           -- correct PO amounts are matched with existing PO amounts by adding
12342           -- an inner loop and correcting the IF condition.
12343           FOR l_index IN 1..l_curr_doc_line_num.COUNT LOOP
12344               --first check for line number matching before adding up
12345               IF l_curr_doc_line_num(l_index) = l_prev_doc_line_num(l_prev_index) THEN
12346                   -- Found match, so add prev amount and exit inner loop
12347                   l_po_amount(l_index) := l_po_amount(l_index)+l_prev_rel_amount(l_prev_index);
12348                   EXIT;
12349               END IF;
12350           END LOOP;
12351 
12352       END LOOP;
12353 
12354       l_textline :=
12355           FND_MESSAGE.GET_STRING('PO', 'PO_SUB_STD_AMT_GRT_GA_AMT_LMT');
12356 
12357       --Go through all lines in current PO (which refer to a GA)
12358       FOR l_curr_doc_line_index IN 1..l_curr_doc_line_num.COUNT LOOP
12359 
12360          --If the amount released and current amount is greater than amount limit then
12361          --raise an error for that line
12362          IF (l_amount_limit(l_curr_doc_line_index) < l_po_amount(l_curr_doc_line_index)) THEN
12363              l_rowcount := l_rowcount +1;
12364              -- Bug 2818810. Use l_curr_doc_line_num to retrieve the line number
12365              INSERT INTO po_online_report_text_gt(online_report_id,
12366                                 last_update_login,
12367                                 last_updated_by,
12368                                 last_update_date,
12369                                 created_by,
12370                                 creation_date,
12371                                 line_num,
12372                                 shipment_num,
12373                                 distribution_num,
12374                                 sequence,
12375                                 text_line,
12376                 message_name)
12377              VALUES(p_online_report_id,
12378                      p_login_id,
12379                      p_user_id,
12380                      sysdate,
12381                      p_user_id,
12382                      sysdate,
12383                      l_curr_doc_line_num(l_curr_doc_line_index),
12384                      0,
12385                      0,
12386                      p_sequence+ l_rowcount,
12387                      substr(g_linemsg||g_delim||l_curr_doc_line_num(l_curr_doc_line_index)||g_delim
12388                             ||l_textline,1,240),
12389                      'PO_SUB_STD_AMT_GRT_GA_AMT_LMT');
12390 
12391           END IF; --amount limit check
12392 
12393        END LOOP;  --end of going through each line on current PO
12394 
12395        --Increment the p_sequence with number of errors reported in last query
12396        p_sequence := p_sequence + l_rowcount;
12397        --<Bug 2800804, 2792477 mbhargav END>
12398 
12399    END IF; -- NOT l_currency_mismatch
12400 
12401 ------------------------------------------
12402 
12403     -- Bug 2716769 Only perform this check if PO and GA use the same currency
12404     IF (NOT l_currency_mismatch) THEN
12405 
12406       l_progress := '010';
12407       IF g_debug_stmt THEN
12408         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
12409           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
12410           || l_progress,'PO STD GA 10: Price Tolerance check');
12411         END IF;
12412       END IF;
12413 
12414       --Check 10: If price override is yes then the line unit price cannot be more
12415       --than the price tolerance on the GA line.
12416       --
12417       -- Bug 3177525: This check should not be done if allow_price_override_flag
12418       -- is 'N' because the user can never modify the standard PO price/amount;
12419       -- it is automatically defaulted.
12420 
12421       INSERT into po_online_report_text_gt(online_report_id,
12422                                            last_update_login,
12423                                            last_updated_by,
12424                                            last_update_date,
12425                                            created_by,
12426                                            creation_date,
12427                                            line_num,
12428                                            shipment_num,
12429                                            distribution_num,
12430                                            sequence,
12431                                            text_line,
12432                                            message_name)
12433       --SQL What: Querying for PO line unit prices that exceed the
12434       --SQL       price tolerance on the GA line
12435       --SQL Why: Add appropriate error message to po_online_report_text_gt
12436       SELECT  p_online_report_id,
12437               p_login_id,
12438               p_user_id,
12439               sysdate,
12440               p_user_id,
12441               sysdate,
12442               POL1.line_num,
12443               0,
12444               0,
12445               p_sequence + ROWNUM,
12446               decode ( POL1.order_type_lookup_code             -- <BUG 3262859>
12447                      , 'FIXED PRICE' , PO_CORE_S.get_translated_text
12448                                        (   'PO_SUB_PO_LINE_GT_GA_AMT_TOL'
12449                                        ,   'LINE_NUM', POL1.line_num
12450                                        ,   'LINE_AMT', POL1.amount
12451                                        ,   'AMT_TOL' , nvl ( POL2.not_to_exceed_price
12452                                                      , POL1.amount )
12453                                        )
12454                      ,                 PO_CORE_S.get_translated_text
12455                                        (   'PO_SUB_PO_LINE_GT_GA_PRICE_TOL'
12456                                        ,   'LINE_NUM'  , POL1.line_num
12457                                        ,   'LINE_PRICE', POL1.unit_price
12458                                        ,   'PRICE_TOL' , nvl ( POL2.not_to_exceed_price
12459                                        , POL1.unit_price )
12460                                        )
12461                      ),
12462               decode ( POL1.order_type_lookup_code             -- <BUG 3262859>
12463                      , 'FIXED PRICE' , 'PO_SUB_PO_LINE_GT_GA_AMT_TOL'
12464                      ,                 'PO_SUB_PO_LINE_GT_GA_PRICE_TOL'
12465                      )
12466       FROM PO_LINES_GT POL1, PO_LINES_ALL POL2, PO_HEADERS_ALL POH
12467       WHERE POL1.po_header_id = p_document_id
12468       AND POH.po_header_id = POL1.from_header_id --JOIN
12469       AND POH.type_lookup_code = 'BLANKET'
12470       AND POH.global_agreement_flag = 'Y'
12471       AND POL1.from_line_id = POL2.po_line_id     --JOIN
12472       AND nvl(POL1.cancel_flag,'N')= 'N'
12473       AND nvl(POL1.closed_code,'OPEN') <> 'FINALLY CLOSED'
12474       AND POL2.allow_price_override_flag = 'Y'                -- Bug 3177525
12475       AND (                                                   -- <SERVICES FPJ>
12476               (   ( POL1.order_type_lookup_code IN ('QUANTITY','AMOUNT','RATE') ) -- <BUG 3262859>
12477               AND ( POL1.unit_price > nvl ( POL2.not_to_exceed_price
12478                                           , POL1.unit_price )
12479                   )
12480               )
12481           OR
12482               (   ( POL1.order_type_lookup_code IN ('FIXED PRICE') )           -- <BUG 3262859>
12483               AND ( POL1.amount > nvl( POL2.not_to_exceed_price, POL1.amount ) )
12484               )
12485           );
12486 
12487        --Increment the p_sequence with number of errors reported in last query
12488       p_sequence := p_sequence + SQL%ROWCOUNT;
12489 
12490     END IF; -- NOT l_currency_mismatch
12491 
12492 -------------------------------------------
12493 
12494     -- Bug 2716769 Only perform this check if PO and GA use the same currency
12495     IF (NOT l_currency_mismatch) THEN
12496 
12497       l_progress := '011';
12498       IF g_debug_stmt THEN
12499         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
12500           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
12501           || l_progress,'PO STD GA 11: Min Release Amount at line level check');
12502         END IF;
12503       END IF;
12504 
12505       --Check 11: The total amount on all the lines on the standard PO
12506       --referencing same GA line should be greater than the minimum release amount
12507       --on the GA line.
12508       l_textline :=                                        -- <2710030>
12509           FND_MESSAGE.GET_STRING('PO','PO_SUB_STD_GA_LINE_LESS_MINREL');
12510       INSERT into po_online_report_text_gt(online_report_id,
12511                                            last_update_login,
12512                                            last_updated_by,
12513                                            last_update_date,
12514                                            created_by,
12515                                            creation_date,
12516                                            line_num,
12517                                            shipment_num,
12518                                            distribution_num,
12519                                            sequence,
12520                                            text_line,
12521                                            message_name)
12522       --SQL What: Querying for PO shipment totals that fail to meet the
12523       --SQL       minimum release amount of the GA line
12524       --SQL Why: Add appropriate error message to po_online_report_text_gt
12525       SELECT  p_online_report_id,
12526               p_login_id,
12527               p_user_id,
12528               sysdate,
12529               p_user_id,
12530               sysdate,
12531               POL1.line_num,
12532               0,
12533               0,
12534               p_sequence + ROWNUM,
12535               substr(g_linemsg||g_delim||POL1.line_num||g_delim
12536                      ||l_textline,1,240),
12537               'PO_SUB_STD_GA_LINE_LESS_MINREL'                      -- <2710030>
12538       FROM PO_LINES_GT POL1, PO_LINES_ALL POL2, PO_HEADERS_ALL POH
12539       WHERE POL1.po_header_id = p_document_id
12540       AND POL1.from_header_id = POL2.po_header_id  --JOIN
12541       AND POL1.from_line_id = POL2.po_line_id      --JOIN
12542       AND POL1.from_header_id = POH.po_header_id   --JOIN
12543       AND POH.type_lookup_code = 'BLANKET'
12544       AND POH.global_agreement_flag = 'Y'
12545       AND POL2.min_release_amount IS NOT NULL
12546       AND POL2.min_release_amount >
12547           --SQL What: Querying PO_LINE_LOCATIONS for the total amount of the
12548           --SQL       shipments in this PO that reference the current GA line
12549           --SQL Why: This sum determines whether the minimum release amount
12550           --SQL      for the GA line has been met
12551           (   SELECT                                          -- <SERVICES FPJ>
12552                   decode ( POL1.quantity
12553                          , NULL , decode ( sum ( PLL.amount
12554                                                - nvl(PLL.amount_cancelled,0) )
12555                                          , 0 , POL2.min_release_amount
12556                                          ,     sum ( PLL.amount
12557                                                    - nvl(PLL.amount_cancelled,0) )
12558                                          )
12559                          ,        decode ( sum ( PLL.quantity
12560                                                - nvl(PLL.quantity_cancelled,0) )
12561                                          , 0 , POL2.min_release_amount
12562                                          ,     sum ( ( PLL.quantity
12563                                                      - nvl(PLL.quantity_cancelled,0) )
12564                                                    * PLL.price_override )
12565                                          )
12566                          )
12567               --<Bug 2792477 mbhargav>
12568               --Changing the query to go to PO_LINE_LOCATIONS_GT instead of PO_LINE_LOCATIONS
12569               FROM PO_LINE_LOCATIONS_GT PLL
12570               WHERE PLL.po_header_id = p_document_id
12571               AND PLL.from_line_id = POL2.po_line_id);
12572 
12573        --Increment the p_sequence with number of errors reported in last query
12574       p_sequence := p_sequence + SQL%ROWCOUNT;
12575 
12576     END IF; -- NOT l_currency_mismatch
12577 
12578 ------------------------------------
12579 
12580 l_progress := '012';
12581     x_return_status := FND_API.G_RET_STS_SUCCESS;
12582 EXCEPTION
12583     WHEN FND_API.G_EXC_ERROR THEN
12584         x_return_status := FND_API.G_RET_STS_ERROR;
12585 
12586     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
12587         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12588 
12589     WHEN OTHERS THEN
12590         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12591         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
12592             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
12593         END IF;
12594 
12595         IF (g_debug_unexp) THEN
12596                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
12597                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
12598                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
12599                        || l_progress || ' SQL CODE is '||sqlcode);
12600                 END IF;
12601         END IF;
12602 
12603 END CHECK_STD_GLOBAL_REF;
12604 
12605 -- <GC FPJ START>
12606 /**
12607 * Private Procedure: CHECK_STD_GC_REF
12608 * Requires:
12609 *   IN PARAMETERS:
12610 *       p_document_id:      The requisition_header_id of submitted document
12611 *       p_online_report_id: Id used to INSERT INTO online_report_text table
12612 *       p_user_id:          User performing the action
12613 *       p_login_id:         Last update login_id
12614 *   IN OUT PARAMETERS
12615 *       x_sequence:          Sequence number of last reported error
12616 * Modifies: Inserts error msgs in online_report_text_gt table, uses
12617 *           global_temp tables for processing
12618 * Effects:  This procedure performs checks for the lines referencing global
12619 *           contracts to enforce the following:
12620 *           1) Contract is still enabled for purchasing in current OU
12621 *           2) Supplier Site is still enabled on the referenced GC
12622 *           3) Amount released should be less than amount limit on GC
12623 * Returns:
12624 *  x_sequence: This parameter contains the current count of number of error
12625 *              messages inserted
12626 *  x_return_status: This parameter signifies whether the checks contained in
12627 *                   this procedure completed successfully.
12628 */
12629 
12630 PROCEDURE check_std_gc_ref
12631 ( p_document_id IN NUMBER,
12632    p_online_report_id IN NUMBER,
12633    p_user_id IN NUMBER,
12634    p_login_id IN NUMBER,
12635    x_sequence IN OUT NOCOPY NUMBER,
12636    x_return_status OUT NOCOPY VARCHAR2
12637 ) IS
12638 
12639 l_textline          PO_ONLINE_REPORT_TEXT.text_line%TYPE := NULL;
12640 l_api_name          CONSTANT VARCHAR2(40) := 'CHECK_STD_GC_REF';
12641 l_progress          VARCHAR2(3);
12642 l_currency_mismatch VARCHAR2(1) := FND_API.G_FALSE;
12643 
12644 TYPE NumTab is TABLE of NUMBER INDEX BY BINARY_INTEGER;
12645 l_curr_doc_line_num NumTab;
12646 l_prev_doc_line_num NumTab;
12647 l_rowcount          NUMBER := 0;
12648 l_po_amount         NumTab;
12649 l_prev_rel_amount   NumTab;
12650 l_amount_limit      NumTab;
12651 l_module            FND_LOG_MESSAGES.module%TYPE;
12652 --<<Bug8422577>>
12653 l_current_different NumTab;
12654 l_prev_different NumTab;
12655 l_current_rate NumTab;
12656 l_prev_rate NumTab ;
12657 l_contract_rate NumTab ;
12658 --<<Bug8422577>>
12659 BEGIN
12660 
12661     l_progress := '010';
12662     l_module := g_log_head || '.' || l_api_name || '.' || l_progress;
12663 
12664     IF g_debug_stmt THEN
12665        IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
12666          FND_LOG.string(FND_LOG.LEVEL_STATEMENT, l_module,
12667                       'PO STD GC 1: GC enabled for purchasing in current OU');
12668        END IF;
12669     END IF;
12670 
12671     -- Check 1: Current OU should still be enabled for purchasing on the GC
12672     --          being referenced
12673     --< Shared Proc FPJ > Bug 3301427: Only do this check for new SPO lines
12674 
12675     l_textline := FND_MESSAGE.get_string ('PO', 'PO_SUB_STD_GC_NOT_EN_PUR');
12676 
12677     INSERT INTO po_online_report_text_gt (
12678         online_report_id,
12679         last_update_login,
12680         last_updated_by,
12681         last_update_date,
12682         created_by,
12683         creation_date,
12684         line_num,
12685         shipment_num,
12686         distribution_num,
12687         sequence,
12688         text_line,
12689         message_name
12690     )
12691     SELECT
12692         p_online_report_id,
12693         p_login_id,
12694         p_user_id,
12695         SYSDATE,
12696         p_user_id,
12697         SYSDATE,
12698         POL.line_num,
12699         0,
12700         0,
12701         x_sequence + ROWNUM,
12702         SUBSTR (g_linemsg || g_delim || POL.line_num || g_delim || l_textline,
12703                  1,
12704                  240),
12705         'PA_SUB_STD_GC_NOT_EN_PUR'
12706     FROM
12707         po_headers_gt  POH,
12708         po_lines_gt    POL,
12709         po_headers_all POHA
12710     WHERE
12711         POH.po_header_id = p_document_id
12712     AND POL.po_header_id = POH.po_header_id
12713     AND POL.contract_id = POHA.po_header_id
12714     AND POHA.global_agreement_flag = 'Y'
12715     AND NOT EXISTS                                  --< Bug 3301427 Start >
12716         (SELECT 'previously approved shipment'
12717            FROM po_line_locations_gt pllg
12718           WHERE pllg.po_line_id = pol.po_line_id
12719             AND pllg.approved_date IS NOT NULL)     --< Bug 3301427 End >
12720     AND NOT EXISTS (SELECT 1
12721                     FROM   po_ga_org_assignments PGOA,
12722                            po_system_parameters  PSP
12723                     WHERE  PGOA.po_header_id = POHA.po_header_id
12724                     AND    PGOA.purchasing_org_id = PSP.org_id
12725                     AND    PGOA.enabled_flag = 'Y');
12726 
12727     x_sequence := x_sequence + SQL%ROWCOUNT;
12728 
12729     ----------------------------------
12730 
12731     l_progress := '020';
12732     l_module := g_log_head || '.' || l_api_name || '.' || l_progress;
12733 
12734     IF g_debug_stmt THEN
12735        IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
12736          FND_LOG.string(FND_LOG.LEVEL_STATEMENT, l_module,
12737                       'PO STD GC 2: supplier site en for pur in current OU');
12738        END IF;
12739     END IF;
12740 
12741     -- Check 2: Supplier Site should be a purchasing site defined in GC
12742     --          Org Assignments
12743     --< Shared Proc FPJ > Bug 3301427: Only do this check for new SPO lines
12744     /* R12 GCPA
12745     Skip Vendor Site validation for Contracts having "Enable All Sites" is set to Y
12746     */
12747     --< CLM Novation > bug13059027 - This check is a Warning for OrderOfIDV Modifications
12748     -- or Re-approval Base docs whose Supplier is different from that of referenced IDV
12749     l_textline := FND_MESSAGE.get_string ('PO', 'PO_SUB_STD_GC_INVALID_SITE');
12750 
12751     INSERT INTO po_online_report_text_gt (
12752         online_report_id,
12753         last_update_login,
12754         last_updated_by,
12755         last_update_date,
12756         created_by,
12757         creation_date,
12758         line_num,
12759         shipment_num,
12760         distribution_num,
12761         sequence,
12762         text_line,
12763         message_name,
12764         message_type
12765     )
12766     SELECT
12767         p_online_report_id,
12768         p_login_id,
12769         p_user_id,
12770         SYSDATE,
12771         p_user_id,
12772         SYSDATE,
12773         POL.line_num,
12774         0,
12775         0,
12776         x_sequence + ROWNUM,
12777         SUBSTR  (g_linemsg || g_delim || POL.line_num || g_delim || l_textline,
12778                  1,
12779                  240),
12780         'PA_SUB_STD_GC_INVALID_SITE',
12781         Decode(is_approved_clm_document(p_document_id), 'Y', Decode(POH.vendor_id, POHA.vendor_id, NULL, 'W'), NULL)
12782     FROM
12783         po_headers_gt  POH,
12784         po_lines_gt    POL,
12785         po_headers_all POHA
12786     WHERE
12787         POH.po_header_id = p_document_id
12788     AND POL.po_header_id = POH.po_header_id
12789     AND POL.contract_id = POHA.po_header_id
12790     AND POHA.global_agreement_flag = 'Y'
12791     AND NOT EXISTS                                  --< Bug 3301427 Start >
12792         (SELECT 'previously approved shipment'
12793            FROM po_line_locations_gt pllg
12794           WHERE pllg.po_line_id = pol.po_line_id
12795             AND pllg.approved_date IS NOT NULL)     --< Bug 3301427 End >
12796     AND NOT EXISTS (SELECT 1
12797                     FROM   po_ga_org_assignments PGOA
12798                     WHERE  PGOA.po_header_id = POHA.po_header_id
12799                     AND    PGOA.vendor_site_id = Decode( Nvl ( poha.Enable_All_Sites,'N'),'N',POH.vendor_site_id,pgoa.Vendor_Site_Id)
12800                     AND    PGOA.enabled_flag = 'Y');
12801 
12802     x_sequence := x_sequence + SQL%ROWCOUNT;
12803 
12804     ----------------------------------
12805 
12806     l_progress := '030';
12807     l_module := g_log_head || '.' || l_api_name || '.' || l_progress;
12808 
12809     IF g_debug_stmt THEN
12810        IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
12811          FND_LOG.string(FND_LOG.LEVEL_STATEMENT, l_module,
12812                       'PO STD GC 3: currency check against referenced GC');
12813        END IF;
12814     END IF;
12815 
12816     -- bug5153099
12817     -- Removed Check 3 as different currencies are being allowed if OU is same.
12818     -- Renumbered Check 4 as Check 3.
12819     -- In the new Check 3, Removed the IF condition as currency mismatch is allowed.
12820     -- Also multiplied amount values by rate for handling currency mismatch.
12821     -- Added a join with po_headers_all POHA1 for getting rate.
12822 
12823     -- Check 3: Amount Released should not exceed the amount limit on the GC
12824 
12825     -- bug3251561
12826     -- Need to check amount based lines as well.
12827 
12828     -- SQL What: For each line that has a contract reference, get the
12829     --           total amount released for that contract on this PO
12830     -- SQL Why:  Need to calculate the total amount released for the
12831     --           contract and we are calculating current PO and other
12832     --           POs separately because current PO lines (stored in
12833     --           po_lines_gt) may not go to po_lines_all eventually
12834     --<Complex Work R12>: changed query to use Line qty/amt/price
12835     --instead of summing up the Line Loc values.  The results should be
12836     --equivalent for SPOs.
12837 
12838 --    Bug8422577 When any one of the lines against the contract is in a different
12839 --    currency, then we must convert all the lines to base currency.
12840 --    l_current_different indicates if the current line is in a different currency
12841 --    from the contract currency
12842 --    l_current_rate is the rate on the PO containing the current line
12843 --    l_contract_rate is the rate of the contract corresponding to the current line
12844 
12845     SELECT /*+ FULL(POL) ORDERED */   -- bug3413891
12846            POL.line_num,
12847             sum(DECODE (POL1.order_type_lookup_code--Bug 12828203
12848                    , 'RATE', POL1.amount
12849                    , 'FIXED PRICE', POL1.amount
12850                    , POL1.quantity * POL1.unit_price)) ,
12851            POHA.amount_limit,
12852            Decode(poha.currency_code,poha1.currency_code,0,1),
12853            Nvl(poha1.rate,1),
12854            Nvl(poha.rate,1)
12855     BULK COLLECT INTO l_curr_doc_line_num,
12856                       l_po_amount,
12857                       l_amount_limit,
12858                       l_current_different,
12859                       l_current_rate,
12860                       l_contract_rate
12861     FROM po_lines_gt          POL,     -- target_line
12862          po_headers_all       POHA,    -- global contract
12863          po_lines_gt          POL1,     -- all lines in current doc
12864                                        -- having the same contract ref
12865          po_headers_all       POHA1    -- document
12866     WHERE
12867          POL.po_header_id = p_document_id
12868     AND  POL.po_header_id = POHA1.po_header_id
12869     AND  POL.contract_id  = POHA.po_header_id
12870     AND  POHA.global_agreement_flag = 'Y'
12871     AND  POHA.amount_limit IS NOT NULL
12872     AND  POL.contract_id = POL1.contract_id
12873 	AND  POL.po_header_id=POL1.po_header_id --Bug: 12828203
12874        GROUP BY POL.line_num,POHA.amount_limit,Decode(poha.currency_code,poha1.currency_code,0,1),
12875            Nvl(poha1.rate,1),
12876            Nvl(poha.rate,1);
12877 
12878     -- Get the sum of all other shipments of approved POs referring
12879     -- to the same GC
12880 
12881     -- bug5153099
12882     -- Multiplied amount values by rate for handling currency mismatch.
12883 
12884     -- bug3251561
12885     -- Need to check amount based lines as well.
12886 
12887     -- SQL What: For each line referencing a global contract, get total
12888     --           amount released for the contract, without including
12889     --           the current PO
12890     -- SQL Why:  Need to calculate the total amount released of the GC
12891     --           to determine whether amount limit is exceeded
12892     --<Complex Work R12>: changed query to use Line qty/amt/price
12893     --instead of summing up the Line Loc values.  The results should be
12894     --equivalent for SPOs.
12895 
12896 --    Bug8422577 l_prev_different indicates if any one of the previously released
12897 --    lines against the same contract, as the current line, is of different currency
12898 --    l_prev_rate indicates the rate on the PO of each line corresponding to the contract
12899 --    on the current line
12900 --    l_prev_rel_amount now contains the previously released amount on each line
12901 --    corresponding to the contract on currenct line
12902 
12903     SELECT /*+ FULL(POL) ORDERED*/     -- bug3413891, 9242146
12904            POL.line_num,
12905            sum(DECODE (POL1.order_type_lookup_code
12906                    , 'RATE', POL1.amount
12907                    , 'FIXED PRICE', POL1.amount
12908                    , POL1.quantity * POL1.unit_price)),
12909                    Decode(POHA.currency_code,POH1.currency_code,0,1),
12910                    Nvl(POH1.rate,1)
12911     BULK COLLECT INTO l_prev_doc_line_num,
12912                       l_prev_rel_amount,
12913                       l_prev_different,
12914                       l_prev_rate
12915     FROM po_lines_gt           POL,     -- target line
12916                po_headers_all        POHA,    -- global contract
12917    		           --bug 9242146, force tables
12918   		           --to be joined in the order specified.
12919          po_lines_all          POL1,    -- all lines from other doc with
12920                                         -- the same GC ref
12921          po_headers_all        POH1    -- headers of lines in POL1
12922 
12923     WHERE
12924          POL.po_header_id = p_document_id
12925     AND  POL.contract_id = POHA.po_header_id
12926     AND  POHA.global_agreement_flag = 'Y'
12927     AND  POHA.amount_limit IS NOT NULL
12928     AND  POL1.contract_id = POL.contract_id
12929     AND  POL1.po_header_id <> POL.po_header_id
12930     AND  POH1.po_header_id = POL1.po_header_id
12931     AND  POH1.approved_flag = 'Y'
12932     GROUP BY POL.line_num, Decode(POHA.currency_code,POH1.currency_code,0,1),
12933                    Nvl(POH1.rate,1);
12934 
12935 	FOR l_index IN 1..l_curr_doc_line_num.COUNT LOOP--Bug: 12828203 Start
12936 		IF(l_current_different(l_index) =1) THEN
12937 		l_po_amount(l_index) := (l_po_amount(l_index) * l_current_rate(l_index));
12938 		l_amount_limit(l_index) :=  l_amount_limit(l_index) * l_contract_rate(l_index);
12939 		END IF;
12940 	END LOOP;       								--Bug: 12828203 End
12941 
12942     -- For each line having GC reference, the line number will be stored
12943     -- in l_curr_doc_line_num and the amount released of the GC from the
12944     -- curent document will be stored in l_po_amount. Also, for the same
12945     -- line there will be a corresponding entry in p_prev_doc_line_num,
12946     -- which stores the line number, and l_prev_rel_amount, which stores
12947     -- the amount released from all other lines referencing the same
12948     -- GC. The loop below is to add up l_po_amount and l_prev_rel_amount
12949     -- to get the total amount released for the GC
12950 
12951 --    Bug8422577 When ever the current line or the any of the previous lines
12952 --    are of different currency we convert the amounts to base currency
12953 --    else we do not.
12954 
12955     FOR l_prev_index IN 1..l_prev_doc_line_num.COUNT LOOP
12956         FOR l_index IN 1..l_curr_doc_line_num.COUNT LOOP
12957             -- first check for line number matching before adding up
12958             IF l_curr_doc_line_num(l_index) =
12959                l_prev_doc_line_num(l_prev_index) THEN
12960                IF(l_current_different(l_index) =1 OR l_prev_different(l_prev_index) =1) THEN--Bug 12828203--start
12961                 l_po_amount(l_index) := l_po_amount(l_index) + (l_prev_rel_amount(l_prev_index)* l_prev_rate(l_prev_index));--Bug: 12828203
12962                 ELSE
12963                   l_po_amount(l_index) := l_po_amount(l_index) + l_prev_rel_amount(l_prev_index);
12964                END IF;
12965                 EXIT;
12966             END IF;
12967         END LOOP;
12968     END LOOP;
12969 
12970     IF g_debug_stmt THEN
12971         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
12972           FND_LOG.string(FND_LOG.LEVEL_STATEMENT, l_module,
12973                       'PO STD GC 3: Amount Released Check');
12974         END IF;
12975     END IF;
12976 
12977     l_textline := FND_MESSAGE.get_string('PO',
12978                                           'PO_SUB_STD_CONTRACT_AMT_LIMIT');
12979 
12980     -- Go through all lines in current PO (which refer to GCs)
12981     FOR l_curr_doc_line_index IN 1..l_curr_doc_line_num.COUNT LOOP
12982 
12983         IF (l_amount_limit(l_curr_doc_line_index) <
12984             l_po_amount(l_curr_doc_line_index))
12985         THEN
12986             l_rowcount := l_rowcount + 1;
12987 
12988             INSERT INTO po_online_report_text_gt (
12989                 online_report_id,
12990                 last_update_login,
12991                 last_updated_by,
12992                 last_update_date,
12993                 created_by,
12994                 creation_date,
12995                 line_num,
12996                 shipment_num,
12997                 distribution_num,
12998                 sequence,
12999                 text_line,
13000                 message_name
13001             ) VALUES (
13002                 p_online_report_id,
13003                 p_login_id,
13004                 p_user_id,
13005                 SYSDATE,
13006                 p_user_id,
13007                 SYSDATE,
13008                 l_curr_doc_line_num(l_curr_doc_line_index),
13009                 0,
13010                 0,
13011                 x_sequence + l_rowcount,
13012                 SUBSTR (g_linemsg || g_delim ||
13013                         l_curr_doc_line_num(l_curr_doc_line_index) ||
13014                         g_delim || l_textline, 1, 240),
13015                 'PO_SUB_STD_CONTRACT_AMT_LIMIT'
13016             );
13017 
13018         END IF; -- if amount limit < amount released
13019     END LOOP;
13020 
13021     x_sequence := x_sequence + l_rowcount;
13022    /* Bug  13550798 - Added new validation to avoid PO refering Contract which had expired. */
13023    l_progress := '040';
13024 
13025    IF g_debug_stmt THEN
13026 	   IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
13027 		 FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
13028 			  || l_progress,'PO STD GC 4: Exp Date Check on Source Document');
13029 	   END IF;
13030 	END IF;
13031     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_STD_AFTER_GA_DATE');
13032     INSERT INTO po_online_report_text_gt (
13033               online_report_id,
13034               last_update_login,
13035               last_updated_by,
13036               last_update_date,
13037               created_by,
13038               creation_date,
13039               line_num,
13040               shipment_num,
13041               distribution_num,
13042               sequence,
13043               text_line,
13044               message_name
13045           )
13046     SELECT  p_online_report_id,
13047           p_login_id,
13048           p_user_id,
13049           sysdate,
13050           p_user_id,
13051           sysdate,
13052           POL.line_num,
13053           PLL.shipment_num,
13054           0,
13055           x_sequence + ROWNUM,
13056           substr(g_linemsg||g_delim||POL.line_num||g_delim
13057                  ||l_textline,1,240),
13058           'PO_SUB_STD_AFTER_GA_DATE'
13059     FROM PO_LINES_GT POL, PO_HEADERS_GT POH, PO_HEADERS_ALL POH2, PO_LINE_LOCATIONS_GT PLL
13060     WHERE POL.po_header_id = p_document_id
13061       AND POL.po_header_id = POH.po_header_id
13062       AND PLL.po_line_id = POL.po_line_id
13063       AND POL.contract_id = POH2.po_header_id
13064       AND POH2.type_lookup_code = 'CONTRACT'
13065       AND POH2.global_agreement_flag = 'Y'
13066       AND Nvl(pol.cancel_flag,'N') = 'N'
13067       AND Nvl(pol.closed_code,'OPEN') <> 'FINALLY CLOSED'
13068       AND TRUNC(sysdate)
13069          > TRUNC(POH2.end_date+ nvl(FND_PROFILE.VALUE('PO_REL_CREATE_TOLERANCE'),0))
13070       AND Nvl(PLL.approved_flag, 'N') <> 'Y';
13071 
13072 	 x_sequence := x_sequence + SQL%ROWCOUNT;
13073     /* Bug  13550798 - End */
13074 
13075     l_progress := '999';
13076     x_return_status := FND_API.G_RET_STS_SUCCESS;
13077 
13078 EXCEPTION
13079     WHEN FND_API.G_EXC_ERROR THEN
13080         x_return_status := FND_API.G_RET_STS_ERROR;
13081 
13082     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
13083         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13084 
13085     WHEN OTHERS THEN
13086         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13087         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
13088             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
13089         END IF;
13090 
13091         IF (g_debug_unexp) THEN
13092                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
13093                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
13094                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
13095                        || l_progress || ' SQL CODE is '||sqlcode);
13096                 END IF;
13097         END IF;
13098 
13099 END CHECK_STD_GC_REF;
13100 
13101 -- <GC FPJ END>
13102 
13103 --For Contract PA
13104 /**
13105 * Private Procedure: CHECK_CONTRACT_AGREEMENT
13106 * Requires:
13107 *   IN PARAMETERS:
13108 *       p_document_id:      The requisition_header_id of submitted document
13109 *       p_online_report_id: Id used to INSERT INTO online_report_text table
13110 *       p_user_id:          User performing the action
13111 *       p_login_id:         Last update login_id
13112 *   IN OUT PARAMETERS
13113 *       p_sequence:          Sequence number of last reported error
13114 * Modifies: Inserts error msgs in online_report_text_gt table, uses
13115 *           global_temp tables for processing
13116 * Effects:  This procedure runs the document submission checks for CONTRACT
13117 *           PAs
13118 * Returns:
13119 *  p_sequence: This parameter contains the current count of number of error
13120 *              messages inserted
13121 */
13122 PROCEDURE check_contract_agreement(p_document_id IN NUMBER,
13123                        p_online_report_id IN NUMBER,
13124                        p_user_id IN NUMBER,
13125                        p_login_id IN NUMBER,
13126                        p_sequence IN OUT NOCOPY NUMBER,
13127                        x_return_status OUT NOCOPY VARCHAR2) IS
13128 
13129 l_textline  po_online_report_text.text_line%TYPE := NULL;
13130 l_api_name  CONSTANT varchar2(40) := 'CHECK_CONTRACT_AGREEMENT';
13131 l_progress VARCHAR2(3);
13132 
13133 BEGIN
13134 
13135 l_progress := '000';
13136 
13137 l_progress := '001';
13138 IF g_debug_stmt THEN
13139    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
13140      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
13141           || l_progress,'PA CONTRACT 1: Released Amount exceeds Amount Limit');
13142    END IF;
13143 END IF;
13144 
13145     -- Check 1: The amount of all standard purchase orders
13146     -- for a contract should not exceed the amount limit of the contract.
13147     -- PO_SUB_CONTRACT_AMT_LIMIT
13148     --<Complex Work R12>: changed query to use Line qty/amt/price
13149     --instead of summing up the Line Loc values.  The results should be
13150     --equivalent for SPOs.
13151     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_SUB_CONTRACT_AMT_LIMIT');
13152     INSERT INTO po_online_report_text_gt(online_report_id,
13153                                          last_update_login,
13154                                          last_updated_by,
13155                                          last_update_date,
13156                                          created_by,
13157                                          creation_date,
13158                                          line_num,
13159                                          shipment_num,
13160                                          distribution_num,
13161                                          sequence,
13162                                          text_line,
13163                                          message_name)
13164   SELECT  p_online_report_id,
13165                 p_login_id,
13166                 p_user_id,
13167                 sysdate,
13168                 p_user_id,
13169                 sysdate,
13170                 0,
13171                 0,
13172                 0,
13173                 p_sequence + ROWNUM,
13174                 substr(l_textline,1,240),
13175                 'PO_SUB_CONTRACT_AMT_LIMIT'
13176        -- <GC FPJ START>
13177        -- For local contract, need to take care of currency conversion
13178        -- since the std PO referencing a contract may be in a different
13179        -- currency
13180        --<Bug#4619187>
13181        --Added a sum function to the sql's select clause
13182 
13183        -- bug5138959
13184        -- Added a sum function to the second select clause
13185        -- bug5153099
13186        -- Removed group by clause in subquery.Also removed the checking for
13187        -- global_agreement_flag and the corresponding OR case logic.
13188 
13189        FROM PO_HEADERS_GT POH
13190        WHERE POH.po_header_id = p_document_id
13191        AND   POH.type_lookup_code = 'CONTRACT'
13192        AND   POH.amount_limit IS NOT NULL
13193        AND   ((POH.amount_limit * NVL(POH.rate, 1))  -- amt limit in fn currency --<SERVICES FPJ>
13194                        <
13195                        (SELECT SUM(                           --Bug#4619187
13196                                     DECODE (POL1.order_type_lookup_code
13197                                            , 'RATE', POL1.amount
13198                                            , 'FIXED PRICE', POL1.amount
13199                                            , POL1.quantity * POL1.unit_price)
13200                                    * NVL(POH1.rate,1)
13201                                   )
13202                         FROM   po_headers POH1,
13203                                po_lines   POL1
13204                         WHERE  POL1.contract_id = POH.po_header_id
13205                         AND    POL1.po_header_id = POH1.po_header_id
13206                         AND    NVL(POL1.cancel_flag, 'N') = 'N'
13207                         )  -- amt released in fn currency
13208              );
13209 
13210        -- <GC FPJ END>
13211 
13212      --Increment the p_sequence with number of errors reported in last query
13213     p_sequence := p_sequence + SQL%ROWCOUNT;
13214 
13215 l_progress := '002';
13216     x_return_status := FND_API.G_RET_STS_SUCCESS;
13217 EXCEPTION
13218     WHEN FND_API.G_EXC_ERROR THEN
13219         x_return_status := FND_API.G_RET_STS_ERROR;
13220 
13221     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
13222         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13223 
13224     WHEN OTHERS THEN
13225         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13226         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
13227             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
13228         END IF;
13229 
13230         IF (g_debug_unexp) THEN
13231                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
13232                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
13233                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
13234                        || l_progress || ' SQL CODE is '||sqlcode);
13235                 END IF;
13236         END IF;
13237 
13238 END CHECK_CONTRACT_AGREEMENT;
13239 
13240 --For PO, REQ, REL
13241 /**
13242 * Private Procedure: DO_CBC_RELATED_VALIDATIONS
13243 * Requires:
13244 *   IN PARAMETERS:
13245 *       p_document_id:      The requisition_header_id of submitted document
13246 *       p_online_report_id: Id used to INSERT INTO online_report_text table
13247 *       p_user_id:          User performing the action
13248 *       p_login_id:         Last update login_id
13249 *   IN OUT PARAMETERS
13250 *       p_sequence:          Sequence number of last reported error
13251 * Modifies: Inserts error msgs in online_report_text_gt table, uses
13252 *           global_temp tables for processing
13253 * Effects:  This procedure runs the document submission checks for HEADER
13254 *           level CBC (FPI Project) validations for PO, REQ, REL
13255 * Returns:
13256 *  p_sequence: This parameter contains the current count of number of error
13257 *              messages inserted
13258 */
13259 PROCEDURE do_cbc_related_validations(p_document_type IN VARCHAR2,
13260                        p_document_subtype IN VARCHAR2,
13261                        p_document_id IN NUMBER,
13262                        p_online_report_id IN NUMBER,
13263                        p_user_id IN NUMBER,
13264                        p_login_id IN NUMBER,
13265                        p_sequence IN OUT NOCOPY NUMBER,
13266                        x_return_status OUT NOCOPY VARCHAR2) IS
13267 
13268 l_msg_count NUMBER;
13269 l_msg_data  VARCHAR2(2400);
13270 l_return_status VARCHAR2(1);
13271 l_cbc_enabled VARCHAR2(1);
13272 l_result NUMBER :=0;
13273 
13274 l_textline po_online_report_text.text_line%TYPE := NULL;
13275 l_api_name  CONSTANT varchar2(40) := 'DO_CBC_RELATED_VALIDATIONS';
13276 l_progress VARCHAR2(3);
13277 
13278 BEGIN
13279 
13280 l_progress := '000';
13281 IF g_debug_stmt THEN
13282    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
13283      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
13284           || l_progress,'CBC 1: Check CBC');
13285    END IF;
13286 END IF;
13287 
13288      IGC_CBC_PO_GRP.IS_CBC_ENABLED ( p_api_version       => 1.0,
13289                                  p_init_msg_list     => FND_API.G_FALSE,
13290                                  p_commit            => FND_API.G_FALSE,
13291                                  p_validation_level  => 100,
13292                                  x_return_status    => l_return_status,
13293                                  x_msg_count         => l_msg_count,
13294                                  x_msg_data          => l_msg_data,
13295                                  x_cbc_enabled       => l_cbc_enabled);
13296 
13297      IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13298         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13299      END IF;
13300 
13301 l_progress := '001';
13302 IF g_debug_stmt THEN
13303    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
13304      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
13305           || l_progress,'Is CBC enabled '|| l_cbc_enabled);
13306    END IF;
13307 END IF;
13308 
13309      IF  l_cbc_enabled = 'Y' THEN
13310 
13311           IGC_CBC_PO_GRP.CBC_HEADER_VALIDATIONS(
13312             p_api_version   => 1.0,
13313             p_init_msg_list => FND_API.G_FALSE,
13314             p_commit        => FND_API.G_FALSE,
13315             p_validation_level => FND_API.G_VALID_LEVEL_FULL,
13316             x_return_status => l_return_status,
13317             x_msg_count => l_msg_count,
13318             x_msg_data =>l_msg_data,
13319             p_document_id           => p_document_id,
13320             p_document_type         => p_document_type,
13321             p_document_sub_type     => p_document_subtype);
13322 
13323 l_progress := '002';
13324 IF g_debug_stmt THEN
13325    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
13326      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
13327           || l_progress,'CBC call returned '|| l_return_status);
13328    END IF;
13329 END IF;
13330 
13331           IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13332                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13333           END IF;
13334 
13335           IF l_return_status = FND_API.G_RET_STS_ERROR
13336           THEN
13337                 l_textline := FND_MSG_PUB.GET(p_msg_index => FND_MSG_PUB.G_LAST,
13338                                         p_encoded => 'F');
13339 
13340                 IF l_textline = NULL THEN
13341                     l_textline :=  FND_MESSAGE.GET_STRING('IGC',
13342                                           'IGC_MULT_FISCAL_YEARS');
13343                 END IF;
13344                 --Deleting msg so that we can always use G_Last to get the
13345                 --message we want
13346                 FND_MSG_PUB.Delete_Msg(p_msg_index => FND_MSG_PUB.G_LAST);
13347 l_progress := '003';
13348                 INSERT INTO PO_ONLINE_REPORT_TEXT_GT (online_report_id,
13349                         last_update_login,
13350                 last_updated_by,
13351                 last_update_date,
13352                 created_by,
13353                 creation_date,
13354                 line_num,
13355                 shipment_num,
13356                 distribution_num,
13357                 sequence,
13358                 text_line,
13359                         message_name)
13360                 VALUES (p_online_report_id,
13361                     p_login_id,
13362                     p_user_id,
13363                       sysdate,
13364                     p_user_id,
13365                     sysdate,
13366                     0,
13367                     0,
13368                     0,
13369                     p_sequence + 1,
13370                     substr(l_textline,1,240),
13371                         'IGC_MULT_FISCAL_YEARS');
13372 
13373                 p_sequence := p_sequence + 1;
13374           END IF; --expected error
13375 
13376     END IF; --cbc is enabled
13377 
13378 l_progress := '004';
13379     x_return_status := FND_API.G_RET_STS_SUCCESS;
13380 EXCEPTION
13381     WHEN FND_API.G_EXC_ERROR THEN
13382         x_return_status := FND_API.G_RET_STS_ERROR;
13383 
13384     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
13385         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13386 
13387     WHEN OTHERS THEN
13388         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13389         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
13390             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
13391         END IF;
13392 
13393         IF (g_debug_unexp) THEN
13394                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
13395                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
13396                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
13397                        || l_progress || ' SQL CODE is '||sqlcode);
13398                 END IF;
13399         END IF;
13400 
13401 END DO_CBC_RELATED_VALIDATIONS;
13402 
13403 
13404 
13405 
13406 -------------------------------------------------------------------------------
13407 --Start of Comments
13408 --Name: populate_global_temp_tables
13409 --Pre-reqs:
13410 --  None.
13411 --Modifies:
13412 --  See the called procedures.
13413 --Locks:
13414 --  None.
13415 --Function:
13416 --  Populates the global temp tables for submission checks.
13417 --Parameters:
13418 --IN:
13419 --p_doc_type
13420 --  Document type.  Use the g_doc_type_<> variables, where <> is:
13421 --    REQUISITION
13422 --    PA
13423 --    PO
13424 --    RELEASE
13425 --p_doc_level
13426 --  The type of id that is being passed.  Use g_doc_level_<>
13427 --  The following is supported for all actions:
13428 --    HEADER
13429 --  The following are also supported for UNRESERVE checks (PO/RELEASE):
13430 --    LINE
13431 --    SHIPMENT
13432 --    DISTRIBUTION
13433 --  The following are also supported for FINAL_CLOSE checks (PO/RELEASE):
13434 --    LINE
13435 --    SHIPMENT
13436 --p_doc_level_id
13437 --  Id of the doc level type of which to populate the tables.
13438 --p_draft_id
13439 -- Draft Id of the Modification Document
13440 -- Added this parameter for CLM Aprvl
13441 --OUT:
13442 --x_return_status
13443 --  APPS standard parameter.
13444 --Testing:
13445 --
13446 --End of Comments
13447 -------------------------------------------------------------------------------
13448 PROCEDURE populate_global_temp_tables(
13449    x_return_status                  OUT NOCOPY     VARCHAR2
13450 ,  p_doc_type                       IN             VARCHAR2
13451 ,  p_doc_level                      IN             VARCHAR2
13452 ,  p_doc_level_id                   IN             NUMBER
13453 ,  p_draft_id                          IN NUMBER := -1 -- CLM Aprvl
13454 )
13455 IS
13456 
13457 l_api_name  CONSTANT varchar2(40) := 'POPULATE_GLOBAL_TEMP_TABLES';
13458 l_log_head CONSTANT VARCHAR2(100) := g_log_head||l_api_name;
13459 l_progress VARCHAR2(3);
13460 
13461 l_blanket_header_id po_headers.po_header_id%TYPE;
13462 l_return_status VARCHAR2(1);
13463 
13464 l_doc_id    NUMBER;
13465 
13466 l_id_tbl    po_tbl_number;
13467 
13468 BEGIN
13469 l_progress := '000';
13470 
13471 IF g_debug_stmt THEN
13472    PO_DEBUG.debug_begin(l_log_head);
13473    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_type', p_doc_type);
13474    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level', p_doc_level);
13475    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level_id',p_doc_level_id);
13476 END IF;
13477 
13478 l_progress := '500';
13479 
13480 -- bug3413891
13481 -- Clean up gt tables to make sure that all the records in GT tables are from
13482 -- the same document
13483 
13484 DELETE FROM po_headers_gt;
13485 DELETE FROM po_lines_gt;
13486 DELETE FROM po_line_locations_gt;
13487 DELETE FROM po_distributions_gt;
13488 DELETE FROM po_releases_gt;
13489 DELETE FROM po_req_headers_gt;
13490 DELETE FROM po_req_lines_gt;
13491 DELETE FROM po_req_distributions_gt;
13492 DELETE FROM po_online_report_text_gt;
13493 
13494 
13495 PO_CORE_S.get_document_ids(
13496    p_doc_type => p_doc_type
13497 ,  p_doc_level => p_doc_level
13498 ,  p_doc_level_id_tbl => po_tbl_number( p_doc_level_id )
13499 ,  x_doc_id_tbl => l_id_tbl
13500 );
13501 
13502 l_progress := '510';
13503 
13504 l_doc_id := l_id_tbl(1);
13505 
13506 
13507     IF p_doc_type = 'REQUISITION' THEN
13508 
13509 l_progress := '001';
13510 IF g_debug_stmt THEN
13511    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
13512      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
13513           || l_progress,'Populating from REQS');
13514    END IF;
13515 END IF;
13516 
13517         --populate the global REQ headers table
13518         populate_req_headers_gt(l_doc_id, l_return_status);
13519 
13520         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13521                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13522         END IF;
13523 
13524 l_progress := '002';
13525         --populate the global REQ line table
13526         populate_req_lines_gt(l_doc_id, l_return_status);
13527 
13528         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13529                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13530         END IF;
13531 
13532     ELSIF p_doc_type in ('PO', 'PA') THEN
13533 l_progress := '004';
13534 IF g_debug_stmt THEN
13535    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
13536      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
13537           || l_progress,'Populating from PO tables for PO/PA');
13538    END IF;
13539 END IF;
13540 
13541         --populate the global headers table
13542         populate_po_headers_gt(
13543 		p_document_id => l_doc_id,
13544         p_draft_id => p_draft_id,   -- CLM Aprvl
13545 		x_return_status => l_return_status);
13546 
13547         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13548                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13549         END IF;
13550 
13551 l_progress := '005';
13552 
13553         --populate the global line table
13554         -- <Doc Manager Rewrite 11.5.11>
13555         -- Change populate logic for po_lines_gt to handle document levels
13556 
13557         populate_po_lines_gt(
13558            p_doc_type => p_doc_type
13559         ,  p_doc_level => p_doc_level
13560         ,  p_doc_level_id => p_doc_level_id
13561 		,  p_draft_id => p_draft_id   -- CLM Aprvl
13562         ,  x_return_status => l_return_status
13563         );
13564 
13565         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13566                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13567         END IF;
13568 
13569     ELSIF p_doc_type = 'RELEASE' THEN
13570 l_progress := '008';
13571 IF g_debug_stmt THEN
13572    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
13573      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
13574           || l_progress,'Populating RELEASES');
13575    END IF;
13576 END IF;
13577 
13578         --populate the global release table
13579         populate_releases_gt(l_doc_id, l_return_status);
13580 
13581         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13582                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13583         END IF;
13584 
13585 l_progress := '009';
13586         -- Get the header_id of the relevent Blanket
13587         SELECT po_header_id
13588         INTO l_blanket_header_id
13589         FROM po_releases_gt
13590         WHERE po_release_id = l_doc_id;
13591 
13592 l_progress := '010';
13593         --populate the global headers table with header of blanket/planned PO
13594         --for which this is a Release
13595         populate_po_headers_gt(
13596 		p_document_id => l_blanket_header_id,
13597         p_draft_id => p_draft_id,   -- CLM Aprvl
13598 		x_return_status => l_return_status);
13599 
13600         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13601                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13602         END IF;
13603 
13604     END IF;
13605 
13606 --<FPJ ENCUMBRANCE>
13607 
13608 l_progress := '100';
13609 
13610 IF (p_doc_type <> g_document_type_REQUISITION) THEN
13611 
13612    populate_line_locations_gt(
13613       p_doc_type => p_doc_type
13614    ,  p_doc_level => p_doc_level
13615    ,  p_doc_level_id => p_doc_level_id
13616    ,  p_draft_id => p_draft_id   -- CLM Aprvl
13617    );
13618 
13619 END IF;
13620 
13621 l_progress := '200';
13622 
13623 populate_distributions_gt(
13624    p_doc_type => p_doc_type
13625 ,  p_doc_level => p_doc_level
13626 ,  p_doc_level_id => p_doc_level_id
13627 ,  p_draft_id => p_draft_id   -- CLM Aprvl
13628 );
13629 
13630 l_progress := 300;
13631 
13632 --LCM ER start. Populate the lcm flag in the GT tables first
13633 IF p_doc_type in ('PO','RELEASE') THEN
13634 
13635 	FOR ship_rec in (select line_location_id from po_line_locations_gt)
13636 	LOOP
13637 		set_lcm_flag(ship_rec.line_location_id,'BEFORE',l_return_status);
13638 	END LOOP;
13639 END IF;
13640 
13641 --LCM ER end.
13642 l_progress := '900';
13643 
13644     x_return_status := FND_API.G_RET_STS_SUCCESS;
13645 EXCEPTION
13646     WHEN FND_API.G_EXC_ERROR THEN
13647         x_return_status := FND_API.G_RET_STS_ERROR;
13648 
13649     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
13650         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13651 
13652     WHEN OTHERS THEN
13653         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13654         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
13655             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
13656         END IF;
13657 
13658         IF (g_debug_unexp) THEN
13659                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
13660                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
13661                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
13662                        || l_progress || ' SQL CODE is '||sqlcode);
13663                 END IF;
13664         END IF;
13665 
13666 END populate_global_temp_tables;
13667 
13668 /**
13669 * Private Procedure: UPDATE_GLOBAL_TEMP_TABLES
13670 * Requires:
13671 *   IN PARAMETERS:
13672 *     p_document_type:     Type of submitted document
13673 *     p_document_subtype:  Subtype of submitted document
13674 *     p_document_id:       Id of submitted document
13675 *     p_requested_changes: This object contains all the requested changes to
13676 *                          the document. It contains 5 objects. These objects
13677 *                          are: 1.Header_Changes 2.Release_Changes 3.Line_
13678 *                          Changes 4.Shipment_Changes 5.Distribution_Changes.
13679 *                          In FPI, following change requests are allowed:
13680 *                          1. HEADER_CHANGES: None
13681 *                          2. RELEASE_CHANGES: None
13682 *                          3. LINE_CHANGES: unit_price, vendor_product_num
13683 *                          4. SHIPMENT_CHANGES: quantity, promised_date,
13684 *                             price_override
13685 *                          5. DISTRIBUTION_CHANGES: quantity_ordered
13686 * Modifies:
13687 * Effects:  Updates the global temp tables with the changes in object
13688 *           p_requested_changes
13689 * Returns:
13690 */
13691 PROCEDURE update_global_temp_tables(p_document_type IN VARCHAR2,
13692                      p_document_subtype IN VARCHAR2,
13693                      p_document_id IN NUMBER,
13694                 -- <PO_CHANGE_API FPJ> Renamed the type to PO_CHANGES_REC_TYPE:
13695                      p_requested_changes  IN PO_CHANGES_REC_TYPE,
13696                      x_return_status OUT NOCOPY VARCHAR2) IS
13697 
13698 l_api_name  CONSTANT varchar2(40) := 'UPDATE_GLOBAL_TEMP_TABLES';
13699 l_progress VARCHAR2(3);
13700 
13701 BEGIN
13702 
13703 l_progress := '000';
13704     IF p_document_type = 'REQUISITION' THEN
13705         --right now no updates to requisitions are allowed
13706         return;
13707     END IF;
13708 
13709     IF p_requested_changes.line_changes IS NOT NULL THEN
13710 l_progress := '001';
13711 IF g_debug_stmt THEN
13712    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
13713      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
13714           || l_progress,'Making Line Chnages');
13715    END IF;
13716 END IF;
13717         -- SQL What: Updating the changeable quantities with either passed
13718         --           in values or if NULL then with existing values in the table
13719         -- SQL Why: Need to apply requested line level changes to global temp table
13720         -- SQL Join: po_line_id
13721         FORALL i IN 1..p_requested_changes.line_changes.po_line_id.COUNT
13722            UPDATE po_lines_gt
13723             SET unit_price = nvl(p_requested_changes.line_changes.unit_price(i),unit_price),
13724                 vendor_product_num = nvl(p_requested_changes.line_changes.vendor_product_num(i),
13725                                             vendor_product_num),
13726                -- <PO_CHANGE_API FPJ START>
13727                -- Added several changeable fields:
13728                quantity = nvl(p_requested_changes.line_changes.quantity(i),
13729                               quantity),
13730                start_date = nvl(p_requested_changes.line_changes.start_date(i),
13731                                 start_date),
13732                expiration_date =
13733                  nvl(p_requested_changes.line_changes.expiration_date(i),
13734                      expiration_date),
13735                amount = nvl(p_requested_changes.line_changes.amount(i),
13736                             amount)
13737                -- <PO_CHANGE_API FPJ END>
13738              WHERE po_line_id = p_requested_changes.line_changes.po_line_id(i);
13739 
13740         --To propogate line price change to shipment level for Standard PO
13741         IF (p_document_type = 'PO' AND p_document_subtype = 'STANDARD') THEN
13742 
13743 l_progress := '002';
13744             -- SQL What: Setting the priceoverride at Shipment level
13745             -- SQL Why: Need to propogate line price change to shipment level
13746             --          for Standard PO
13747             -- SQL Join: po_line_id
13748             FORALL i IN 1..p_requested_changes.line_changes.po_line_id.COUNT
13749                 UPDATE po_line_locations_gt
13750                  SET price_override = nvl(p_requested_changes.line_changes.unit_price(i),price_override)
13751                  WHERE po_line_id = p_requested_changes.line_changes.po_line_id(i)
13752                  AND nvl(payment_type, 'NULL') NOT IN ('MILESTONE', 'ADVANCE')
13753                  -- <Complex Work R12>: do not carry line price down in Qty Milestone case
13754                  ;
13755         END IF;
13756     END IF;
13757 
13758     IF p_requested_changes.shipment_changes IS NOT NULL THEN
13759 l_progress := '003';
13760 IF g_debug_stmt THEN
13761    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
13762      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
13763           || l_progress,'Making Shipment chanbges');
13764    END IF;
13765 END IF;
13766         -- SQL What: Updating the changeable quantities with either passed
13767         --           in values or if NULL then with existing values in the table
13768         -- SQL Why: Need to apply requested line level changes to global temp table
13769         -- SQL Join: po_line_location_id
13770         FORALL i IN 1..p_requested_changes.shipment_changes.po_line_location_id.COUNT
13771            UPDATE po_line_locations_gt
13772             SET quantity = nvl(p_requested_changes.shipment_changes.quantity(i),quantity),
13773                 promised_date = nvl(p_requested_changes.shipment_changes.promised_date(i),
13774                                             promised_date),
13775                 price_override = nvl(p_requested_changes.shipment_changes.price_override(i),
13776                                             price_override),
13777                 -- <PO_CHANGE_API FPJ START>
13778                 -- Added several changeable fields:
13779                 need_by_date =
13780                   nvl(p_requested_changes.shipment_changes.need_by_date(i),
13781                       need_by_date),
13782                 ship_to_location_id =
13783                   nvl(p_requested_changes.shipment_changes.ship_to_location_id(i),
13784                       ship_to_location_id),
13785                 amount = nvl(p_requested_changes.shipment_changes.amount(i),
13786                              amount),
13787                 -- <PO_CHANGE_API FPJ END>
13788                 -- <Complex Work R12 START>
13789                 payment_type = nvl(p_requested_changes.shipment_changes.payment_type(i),
13790                                    payment_type),
13791                 description = nvl(p_requested_changes.shipment_changes.description(i),
13792                                   description),
13793                 value_basis = DECODE(p_requested_changes.shipment_changes.payment_type(i)
13794                                      , NULL, value_basis
13795                                      , 'RATE', 'QUANTITY'
13796                                      , 'LUMPSUM', 'FIXED PRICE'
13797                                      , 'MILESTONE', 'FIXED PRICE'
13798                               )
13799                 -- Note: the value basis decode assumes Milestone Pay Items are Amount
13800                 -- Milestones, since payment type is not changeable on Qty-based lines
13801                 -- <Complex Work R12 END>
13802             WHERE line_location_id = p_requested_changes.shipment_changes.po_line_location_id(i)
13803             AND   p_requested_changes.shipment_changes.parent_line_location_id(i) IS NULL;
13804 
13805 l_progress := '004';
13806 
13807         --<case of split shipment>
13808         FORALL i IN 1..p_requested_changes.shipment_changes.po_line_location_id.COUNT
13809             INSERT INTO po_line_locations_gt(
13810                  LINE_LOCATION_ID     ,
13811                  LAST_UPDATE_DATE      ,
13812                  LAST_UPDATED_BY        ,
13813                  PO_HEADER_ID            ,
13814                  PO_LINE_ID               ,
13815                  LAST_UPDATE_LOGIN         ,
13816                  CREATION_DATE              ,
13817                  CREATED_BY                  ,
13818                  QUANTITY                     ,
13819                  QUANTITY_RECEIVED             ,
13820                  QUANTITY_ACCEPTED              ,
13821                  QUANTITY_REJECTED               ,
13822                  QUANTITY_BILLED                  ,
13823                  QUANTITY_CANCELLED                ,
13824                  UNIT_MEAS_LOOKUP_CODE              ,
13825                  PO_RELEASE_ID                       ,
13826                  SHIP_TO_LOCATION_ID                  ,
13827                  SHIP_VIA_LOOKUP_CODE                  ,
13828                  NEED_BY_DATE                           ,
13829                  PROMISED_DATE                           ,
13830                  LAST_ACCEPT_DATE                         ,
13831                  PRICE_OVERRIDE                          ,
13832                  ENCUMBERED_FLAG                        ,
13833                  ENCUMBERED_DATE                         ,
13834                  UNENCUMBERED_QUANTITY                    ,
13835                  FOB_LOOKUP_CODE                         ,
13836                  FREIGHT_TERMS_LOOKUP_CODE                ,
13837                  TAXABLE_FLAG                            ,
13838                  ESTIMATED_TAX_AMOUNT                    ,
13839                  FROM_HEADER_ID                          ,
13840                  FROM_LINE_ID                             ,
13841                  FROM_LINE_LOCATION_ID                   ,
13842                  START_DATE                               ,
13843                  END_DATE                                ,
13844                  LEAD_TIME                              ,
13845                  LEAD_TIME_UNIT                          ,
13846                  PRICE_DISCOUNT                           ,
13847                  TERMS_ID                                 ,
13848                  APPROVED_FLAG                            ,
13849                  APPROVED_DATE                            ,
13850                  CLOSED_FLAG                              ,
13851                  CANCEL_FLAG                              ,
13852                  CANCELLED_BY                             ,
13853                  CANCEL_DATE                              ,
13854                  CANCEL_REASON                            ,
13855                  FIRM_STATUS_LOOKUP_CODE                  ,
13856                  FIRM_DATE                                ,
13857                  ATTRIBUTE_CATEGORY                       ,
13858                  ATTRIBUTE1                               ,
13859                  ATTRIBUTE2                               ,
13860                  ATTRIBUTE3                               ,
13861                  ATTRIBUTE4                               ,
13862                  ATTRIBUTE5                               ,
13863                  ATTRIBUTE6                               ,
13864                  ATTRIBUTE7                               ,
13865                  ATTRIBUTE8                               ,
13866                  ATTRIBUTE9                               ,
13867                  ATTRIBUTE10                              ,
13868                  UNIT_OF_MEASURE_CLASS                    ,
13869                  ENCUMBER_NOW                             ,
13870                  ATTRIBUTE11                              ,
13871                  ATTRIBUTE12                              ,
13872                  ATTRIBUTE13                              ,
13873                  ATTRIBUTE14                              ,
13874                  ATTRIBUTE15                              ,
13875                  INSPECTION_REQUIRED_FLAG                 ,
13876                  RECEIPT_REQUIRED_FLAG                    ,
13877                  QTY_RCV_TOLERANCE                       ,
13878                  QTY_RCV_EXCEPTION_CODE                   ,
13879                  ENFORCE_SHIP_TO_LOCATION_CODE            ,
13880                  ALLOW_SUBSTITUTE_RECEIPTS_FLAG           ,
13881                  DAYS_EARLY_RECEIPT_ALLOWED               ,
13882                  DAYS_LATE_RECEIPT_ALLOWED                ,
13883                  RECEIPT_DAYS_EXCEPTION_CODE             ,
13884                  INVOICE_CLOSE_TOLERANCE                  ,
13885                  RECEIVE_CLOSE_TOLERANCE                  ,
13886                  SHIP_TO_ORGANIZATION_ID                 ,
13887                  SHIPMENT_NUM                            ,
13888                  SOURCE_SHIPMENT_ID                      ,
13889                  SHIPMENT_TYPE                     ,
13890                  CLOSED_CODE                        ,
13891                  REQUEST_ID                          ,
13892                  PROGRAM_APPLICATION_ID               ,
13893                  PROGRAM_ID                            ,
13894                  PROGRAM_UPDATE_DATE                    ,
13895                  GOVERNMENT_CONTEXT                      ,
13896                  RECEIVING_ROUTING_ID                     ,
13897                  ACCRUE_ON_RECEIPT_FLAG                  ,
13898                  CLOSED_REASON                           ,
13899                  CLOSED_DATE                              ,
13900                  CLOSED_BY                               ,
13901                  ORG_ID                                  ,
13902                  GLOBAL_ATTRIBUTE1                        ,
13903                  GLOBAL_ATTRIBUTE2                        ,
13904                  GLOBAL_ATTRIBUTE3                        ,
13905                  GLOBAL_ATTRIBUTE4                        ,
13906                  GLOBAL_ATTRIBUTE5                        ,
13907                  GLOBAL_ATTRIBUTE6                        ,
13908                  GLOBAL_ATTRIBUTE7                        ,
13909                  GLOBAL_ATTRIBUTE8                        ,
13910                  GLOBAL_ATTRIBUTE9                        ,
13911                  GLOBAL_ATTRIBUTE10                       ,
13912                  GLOBAL_ATTRIBUTE11                       ,
13913                  GLOBAL_ATTRIBUTE12                       ,
13914                  GLOBAL_ATTRIBUTE13                       ,
13915                  GLOBAL_ATTRIBUTE14                       ,
13916                  GLOBAL_ATTRIBUTE15                       ,
13917                  GLOBAL_ATTRIBUTE16                       ,
13918                  GLOBAL_ATTRIBUTE17                       ,
13919                  GLOBAL_ATTRIBUTE18                       ,
13920                  GLOBAL_ATTRIBUTE19                       ,
13921                  GLOBAL_ATTRIBUTE20                       ,
13922                  GLOBAL_ATTRIBUTE_CATEGORY                ,
13923                  QUANTITY_SHIPPED                        ,
13924                  COUNTRY_OF_ORIGIN_CODE                   ,
13925                  TAX_USER_OVERRIDE_FLAG                  ,
13926                  MATCH_OPTION                            ,
13927                  TAX_CODE_ID                              ,
13928                  CALCULATE_TAX_FLAG                      ,
13929                  CHANGE_PROMISED_DATE_REASON            ,
13930                  NOTE_TO_RECEIVER                        ,
13931                  SECONDARY_QUANTITY                      ,
13932                  SECONDARY_UNIT_OF_MEASURE               ,
13933                  PREFERRED_GRADE                         ,
13934                  SECONDARY_QUANTITY_RECEIVED             ,
13935                  SECONDARY_QUANTITY_ACCEPTED              ,
13936                  SECONDARY_QUANTITY_REJECTED             ,
13937                  SECONDARY_QUANTITY_CANCELLED             ,
13938                  VMI_FLAG                                 ,
13939                  CONSIGNED_FLAG                           ,
13940                  RETROACTIVE_DATE                         ,
13941                  AMOUNT                                   , -- <PO_CHANGE_API FPJ>
13942                  DESCRIPTION                              , --<Complex Work R12>
13943                  PAYMENT_TYPE                             , --<Complex Work R12>
13944                  VALUE_BASIS                                --<Complex Work R12>
13945              )
13946              SELECT
13947                  -- bug3611217
13948                  -- Use a sequence number rather than FND_API.G_MISS_NUM
13949                  -- since we have added unique constraint on LINE_LOCATION_ID
13950                  PO_LINE_LOCATIONS_S.nextval,
13951                  poll.LAST_UPDATE_DATE      ,
13952                  poll.LAST_UPDATED_BY        ,
13953                  poll.PO_HEADER_ID            ,
13954                  poll.PO_LINE_ID               ,
13955                  poll.LAST_UPDATE_LOGIN         ,
13956                  poll.CREATION_DATE              ,
13957                  poll.CREATED_BY                  ,
13958                  nvl(p_requested_changes.shipment_changes.quantity(i),poll.quantity),
13959                  -- Bug 3322019 START
13960                  -- Quantity received, accepted, cancelled, etc. should be
13961                  -- NULL or 0 on the split shipment.
13962                  decode(poll.quantity_received,NULL,NULL,0),
13963                  decode(poll.quantity_accepted,NULL,NULL,0),
13964                  decode(poll.quantity_rejected,NULL,NULL,0),
13965                  decode(poll.quantity_billed,NULL,NULL,0),
13966                  decode(poll.quantity_cancelled,NULL,NULL,0),
13967                  -- Bug 3322019 END
13968                  poll.UNIT_MEAS_LOOKUP_CODE              ,
13969                  poll.PO_RELEASE_ID                       ,
13970                  -- <PO_CHANGE_API FPJ> Added as a changeable field:
13971                  nvl(p_requested_changes.shipment_changes.ship_to_location_id(i),
13972                      poll.ship_to_location_id),
13973                  poll.SHIP_VIA_LOOKUP_CODE                  ,
13974                  -- <PO_CHANGE_API FPJ> Added as a changeable field:
13975                  nvl(p_requested_changes.shipment_changes.need_by_date(i),
13976                      poll.need_by_date),
13977                  nvl(p_requested_changes.shipment_changes.promised_date(i),
13978                                             poll.promised_date),
13979                  poll.LAST_ACCEPT_DATE                         ,
13980                  nvl(p_requested_changes.shipment_changes.price_override(i),
13981                                             poll.price_override),
13982                  -- Bug 3322019 START
13983                  NULL, -- ENCUMBERED_FLAG
13984                  NULL, -- ENCUMBERED_DATE
13985                  NULL, -- UNENCUMBERED_QUANTITY
13986                  -- Bug 3322019 END
13987                  poll.FOB_LOOKUP_CODE                         ,
13988                  poll.FREIGHT_TERMS_LOOKUP_CODE                ,
13989                  poll.TAXABLE_FLAG                            ,
13990                  0, -- ESTIMATED_TAX_AMOUNT (Bug 3322019)
13991                  poll.FROM_HEADER_ID                          ,
13992                  poll.FROM_LINE_ID                             ,
13993                  poll.FROM_LINE_LOCATION_ID                   ,
13994                  poll.START_DATE                               ,
13995                  poll.END_DATE                                ,
13996                  poll.LEAD_TIME                              ,
13997                  poll.LEAD_TIME_UNIT                          ,
13998                  poll.PRICE_DISCOUNT                           ,
13999                  poll.TERMS_ID                                 ,
14000                  -- Bug 3322019 START
14001                  'N', -- APPROVED_FLAG
14002                  NULL, -- APPROVED_DATE
14003                  NULL, -- CLOSED_FLAG
14004                  'N', -- CANCEL_FLAG
14005                  NULL, -- CANCELLED_BY
14006                  NULL, -- CANCEL_DATE
14007                  NULL, -- CANCEL_REASON
14008                  -- Bug 3322019 END
14009                  poll.FIRM_STATUS_LOOKUP_CODE                  ,
14010                  poll.FIRM_DATE                                ,
14011                  poll.ATTRIBUTE_CATEGORY                       ,
14012                  poll.ATTRIBUTE1                               ,
14013                  poll.ATTRIBUTE2                               ,
14014                  poll.ATTRIBUTE3                               ,
14015                  poll.ATTRIBUTE4                               ,
14016                  poll.ATTRIBUTE5                               ,
14017                  poll.ATTRIBUTE6                               ,
14018                  poll.ATTRIBUTE7                               ,
14019                  poll.ATTRIBUTE8                               ,
14020                  poll.ATTRIBUTE9                               ,
14021                  poll.ATTRIBUTE10                              ,
14022                  poll.UNIT_OF_MEASURE_CLASS                    ,
14023                  poll.ENCUMBER_NOW                             ,
14024                  poll.ATTRIBUTE11                              ,
14025                  poll.ATTRIBUTE12                              ,
14026                  poll.ATTRIBUTE13                              ,
14027                  poll.ATTRIBUTE14                              ,
14028                  poll.ATTRIBUTE15                              ,
14029                  poll.INSPECTION_REQUIRED_FLAG                 ,
14030                  poll.RECEIPT_REQUIRED_FLAG                    ,
14031                  poll.QTY_RCV_TOLERANCE                       ,
14032                  poll.QTY_RCV_EXCEPTION_CODE                   ,
14033                  poll.ENFORCE_SHIP_TO_LOCATION_CODE            ,
14034                  poll.ALLOW_SUBSTITUTE_RECEIPTS_FLAG           ,
14035                  poll.DAYS_EARLY_RECEIPT_ALLOWED               ,
14036                  poll.DAYS_LATE_RECEIPT_ALLOWED                ,
14037                  poll.RECEIPT_DAYS_EXCEPTION_CODE             ,
14038                  poll.INVOICE_CLOSE_TOLERANCE                  ,
14039                  poll.RECEIVE_CLOSE_TOLERANCE                  ,
14040                  poll.SHIP_TO_ORGANIZATION_ID                 ,
14041 
14042                  --<Bug 2798040 mbhargav START>
14043                  --iSP is passing shipment_num now
14044                  nvl(p_requested_changes.shipment_changes.split_shipment_num(i),
14045                                             FND_API.G_MISS_NUM),
14046                  --SHIPMENT_NUM                            ,
14047                  --<Bug 2798040 mbhargav START>
14048 
14049                  poll.SOURCE_SHIPMENT_ID                      ,
14050                  poll.SHIPMENT_TYPE                     ,
14051                  -- Bug 3322019 START
14052                  'OPEN', -- CLOSED_CODE
14053                  NULL, -- REQUEST_ID
14054                  NULL, -- PROGRAM_APPLICATION_ID
14055                  NULL, -- PROGRAM_ID
14056                  NULL, -- PROGRAM_UPDATE_DATE
14057                  -- Bug 3322019 START
14058                  poll.GOVERNMENT_CONTEXT                      ,
14059                  poll.RECEIVING_ROUTING_ID                     ,
14060                  poll.ACCRUE_ON_RECEIPT_FLAG                  ,
14061                  -- Bug 3322019 START
14062                  NULL, -- CLOSED_REASON
14063                  NULL, -- CLOSED_DATE
14064                  NULL, -- CLOSED_BY
14065                  -- Bug 3322019 END
14066                  poll.ORG_ID                                  ,
14067                  poll.GLOBAL_ATTRIBUTE1                        ,
14068                  poll.GLOBAL_ATTRIBUTE2                        ,
14069                  poll.GLOBAL_ATTRIBUTE3                        ,
14070                  poll.GLOBAL_ATTRIBUTE4                        ,
14071                  poll.GLOBAL_ATTRIBUTE5                        ,
14072                  poll.GLOBAL_ATTRIBUTE6                        ,
14073                  poll.GLOBAL_ATTRIBUTE7                        ,
14074                  poll.GLOBAL_ATTRIBUTE8                        ,
14075                  poll.GLOBAL_ATTRIBUTE9                        ,
14076                  poll.GLOBAL_ATTRIBUTE10                       ,
14077                  poll.GLOBAL_ATTRIBUTE11                       ,
14078                  poll.GLOBAL_ATTRIBUTE12                       ,
14079                  poll.GLOBAL_ATTRIBUTE13                       ,
14080                  poll.GLOBAL_ATTRIBUTE14                       ,
14081                  poll.GLOBAL_ATTRIBUTE15                       ,
14082                  poll.GLOBAL_ATTRIBUTE16                       ,
14083                  poll.GLOBAL_ATTRIBUTE17                       ,
14084                  poll.GLOBAL_ATTRIBUTE18                       ,
14085                  poll.GLOBAL_ATTRIBUTE19                       ,
14086                  poll.GLOBAL_ATTRIBUTE20                       ,
14087                  poll.GLOBAL_ATTRIBUTE_CATEGORY                ,
14088                  decode(poll.quantity_shipped,NULL,NULL,0), -- Bug 3322019
14089                  poll.COUNTRY_OF_ORIGIN_CODE                   ,
14090                  poll.TAX_USER_OVERRIDE_FLAG                  ,
14091                  poll.MATCH_OPTION                            ,
14092                  poll.TAX_CODE_ID                              ,
14093                  poll.CALCULATE_TAX_FLAG                      ,
14094                  poll.CHANGE_PROMISED_DATE_REASON            ,
14095                  poll.NOTE_TO_RECEIVER                        ,
14096                  decode(poll.secondary_quantity,NULL,NULL,0), -- Bug 3322019
14097                  poll.SECONDARY_UNIT_OF_MEASURE               ,
14098                  poll.PREFERRED_GRADE                         ,
14099                  -- Bug 3322019 START
14100                  decode(poll.secondary_quantity_received,NULL,NULL,0),
14101                  decode(poll.secondary_quantity_accepted,NULL,NULL,0),
14102                  decode(poll.secondary_quantity_rejected,NULL,NULL,0),
14103                  decode(poll.secondary_quantity_cancelled,NULL,NULL,0),
14104                  -- Bug 3322019 END
14105                  poll.VMI_FLAG                                 ,
14106                  poll.CONSIGNED_FLAG                           ,
14107                  poll.RETROACTIVE_DATE                         ,
14108                  -- <PO_CHANGE_API FPJ START> Added a changeable field:
14109                  NVL(p_requested_changes.shipment_changes.amount(i), poll.amount),
14110                  -- <PO_CHANGE_API FPJ END>
14111                  --<Complex Work R12 START>
14112                  poll.DESCRIPTION,
14113                  poll.PAYMENT_TYPE,
14114                  DECODE(p_requested_changes.shipment_changes.payment_type(i)
14115                        , 'RATE', 'QUANTITY'
14116                        , 'LUMPSUM', 'FIXED PRICE'
14117                        , POL.order_type_lookup_code
14118                  )
14119                 -- Note: the value basis decode assumes Milestone Pay Items are Amount
14120                 -- Milestones, since payment type is not changeable on Qty-based lines
14121                  --<Complex Work R12 END>
14122               FROM po_line_locations poll
14123                  , po_lines_all pol --<Complex Work R12>
14124               WHERE poll.line_location_id =
14125                     p_requested_changes.shipment_changes.parent_line_location_id(i)
14126               AND   p_requested_changes.shipment_changes.po_line_location_id(i) IS NULL
14127               AND   poll.po_line_id = pol.po_line_id;  --<Complex Work R12>
14128 
14129     END IF;
14130 
14131     IF p_requested_changes.distribution_changes IS NOT NULL THEN
14132 l_progress := '005';
14133 IF g_debug_stmt THEN
14134    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
14135      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
14136           || l_progress,'Making Distribution changes');
14137    END IF;
14138 END IF;
14139         -- SQL What: Updating the changeable quantities with either passed
14140         --           in values or if NULL then with existing values in the table
14141         -- SQL Why: Need to apply requested line level changes to global temp table
14142         -- SQL Join: po_line_id
14143         FORALL i IN 1..p_requested_changes.distribution_changes.po_distribution_id.COUNT
14144            UPDATE po_distributions_gt
14145             SET quantity_ordered = nvl(p_requested_changes.distribution_changes.quantity_ordered(i),
14146                                         quantity_ordered),
14147                 -- <PO_CHANGE_API FPJ START>
14148                 -- Added amount_ordered as a changeable field:
14149                 amount_ordered =
14150                   nvl(p_requested_changes.distribution_changes.amount_ordered(i),
14151                       amount_ordered)
14152                 -- <PO_CHANGE_API FPJ END>
14153             WHERE po_distribution_id = p_requested_changes.distribution_changes.po_distribution_id(i);
14154     END IF;
14155 
14156 l_progress := '006';
14157     x_return_status := FND_API.G_RET_STS_SUCCESS;
14158 EXCEPTION
14159     WHEN FND_API.G_EXC_ERROR THEN
14160         x_return_status := FND_API.G_RET_STS_ERROR;
14161 
14162     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
14163         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
14164 
14165     WHEN OTHERS THEN
14166         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
14167         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
14168             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
14169         END IF;
14170 
14171         IF (g_debug_unexp) THEN
14172                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
14173                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
14174                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
14175                        || l_progress || ' SQL CODE is '||sqlcode);
14176                 END IF;
14177         END IF;
14178 
14179 END UPDATE_GLOBAL_TEMP_TABLES;
14180 
14181 /**
14182 * Private Procedure: POPULATE_PO_HEADERS_GT
14183 * Requires:
14184 *   IN PARAMETERS:
14185 *       p_document_id: Id of submitted document
14186 *       p_draft_id: Draft Id of Mod Document  -- CLM Aprvl
14187 * Modifies:
14188 * Effects:  Populates the global temp tables po_headers_gt
14189 * Returns:
14190 */
14191 PROCEDURE populate_po_headers_gt(p_document_id IN number,
14192 									p_draft_id IN NUMBER := -1,
14193                                     x_return_status OUT NOCOPY VARCHAR2)IS
14194 
14195 l_api_name  CONSTANT varchar2(40) := 'POPULATE_PO_HEADERS_GT';
14196 l_progress VARCHAR2(3);
14197 
14198 t_po_header_id NUMBER;
14199 t_segment1 po_headers.segment1%TYPE;
14200 
14201 BEGIN
14202 
14203 l_progress := '000';
14204 IF g_debug_stmt THEN
14205    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
14206      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
14207           || l_progress,'Populate HEADERS');
14208    END IF;
14209 END IF;
14210 
14211     INSERT INTO po_headers_gt(
14212             PO_HEADER_ID,
14213           AGENT_ID,
14214           TYPE_LOOKUP_CODE,
14215           LAST_UPDATE_DATE,
14216           LAST_UPDATED_BY,
14217           SEGMENT1,
14218           SUMMARY_FLAG,
14219           ENABLED_FLAG,
14220           SEGMENT2,
14221           SEGMENT3,
14222           SEGMENT4,
14223           SEGMENT5,
14224           START_DATE_ACTIVE,
14225           END_DATE_ACTIVE,
14226           LAST_UPDATE_LOGIN,
14227           CREATION_DATE ,
14228           CREATED_BY,
14229           VENDOR_ID,
14230           VENDOR_SITE_ID,
14231           VENDOR_CONTACT_ID,
14232           SHIP_TO_LOCATION_ID,
14233           BILL_TO_LOCATION_ID ,
14234           TERMS_ID             ,
14235           SHIP_VIA_LOOKUP_CODE  ,
14236           FOB_LOOKUP_CODE        ,
14237           FREIGHT_TERMS_LOOKUP_CODE,
14238           STATUS_LOOKUP_CODE,
14239           CURRENCY_CODE,
14240           RATE_TYPE,
14241           RATE_DATE,
14242           RATE,
14243           FROM_HEADER_ID,
14244           FROM_TYPE_LOOKUP_CODE,
14245           START_DATE,
14246           END_DATE,
14247           BLANKET_TOTAL_AMOUNT,
14248           AUTHORIZATION_STATUS,
14249           REVISION_NUM,
14250           REVISED_DATE,
14251           APPROVED_FLAG,
14252           APPROVED_DATE,
14253           AMOUNT_LIMIT,
14254           MIN_RELEASE_AMOUNT,
14255           NOTE_TO_AUTHORIZER,
14256           NOTE_TO_VENDOR,
14257           NOTE_TO_RECEIVER,
14258           PRINT_COUNT,
14259           PRINTED_DATE,
14260           VENDOR_ORDER_NUM,
14261           CONFIRMING_ORDER_FLAG,
14262           COMMENTS,
14263           REPLY_DATE,
14264           REPLY_METHOD_LOOKUP_CODE,
14265           RFQ_CLOSE_DATE,
14266           QUOTE_TYPE_LOOKUP_CODE,
14267           QUOTATION_CLASS_CODE,
14268           QUOTE_WARNING_DELAY_UNIT,
14269           QUOTE_WARNING_DELAY,
14270           QUOTE_VENDOR_QUOTE_NUMBER,
14271           ACCEPTANCE_REQUIRED_FLAG,
14272           ACCEPTANCE_DUE_DATE,
14273           CLOSED_DATE,
14274           USER_HOLD_FLAG,
14275           APPROVAL_REQUIRED_FLAG,
14276           CANCEL_FLAG,
14277           FIRM_STATUS_LOOKUP_CODE,
14278           FIRM_DATE,
14279           FROZEN_FLAG,
14280           ATTRIBUTE_CATEGORY,
14281           ATTRIBUTE1,
14282           ATTRIBUTE2,
14283           ATTRIBUTE3,
14284           ATTRIBUTE4,
14285           ATTRIBUTE5,
14286           ATTRIBUTE6,
14287           ATTRIBUTE7,
14288           ATTRIBUTE8,
14289           ATTRIBUTE9,
14290           ATTRIBUTE10,
14291           ATTRIBUTE11,
14292           ATTRIBUTE12,
14293             ATTRIBUTE13,
14294           ATTRIBUTE14,
14295           ATTRIBUTE15,
14296           CLOSED_CODE,
14297           GOVERNMENT_CONTEXT,
14298           REQUEST_ID,
14299           PROGRAM_APPLICATION_ID,
14300           PROGRAM_ID,
14301           PROGRAM_UPDATE_DATE,
14302           ORG_ID,
14303           SUPPLY_AGREEMENT_FLAG,
14304           EDI_PROCESSED_FLAG,
14305           EDI_PROCESSED_STATUS,
14306           GLOBAL_ATTRIBUTE_CATEGORY,
14307           GLOBAL_ATTRIBUTE1,
14308           GLOBAL_ATTRIBUTE2,
14309           GLOBAL_ATTRIBUTE3,
14310           GLOBAL_ATTRIBUTE4,
14311           GLOBAL_ATTRIBUTE5,
14312           GLOBAL_ATTRIBUTE6,
14313           GLOBAL_ATTRIBUTE7,
14314           GLOBAL_ATTRIBUTE8,
14315           GLOBAL_ATTRIBUTE9,
14316           GLOBAL_ATTRIBUTE10,
14317           GLOBAL_ATTRIBUTE11,
14318           GLOBAL_ATTRIBUTE12,
14319           GLOBAL_ATTRIBUTE13,
14320           GLOBAL_ATTRIBUTE14,
14321           GLOBAL_ATTRIBUTE15,
14322           GLOBAL_ATTRIBUTE16,
14323           GLOBAL_ATTRIBUTE17,
14324           GLOBAL_ATTRIBUTE18,
14325           GLOBAL_ATTRIBUTE19,
14326           GLOBAL_ATTRIBUTE20,
14327           INTERFACE_SOURCE_CODE,
14328           REFERENCE_NUM,
14329           WF_ITEM_TYPE,
14330           WF_ITEM_KEY,
14331           MRC_RATE_TYPE,
14332           MRC_RATE_DATE,
14333           MRC_RATE,
14334           PCARD_ID,
14335           PRICE_UPDATE_TOLERANCE,
14336           PAY_ON_CODE,
14337           XML_FLAG,
14338           XML_SEND_DATE,
14339           XML_CHANGE_SEND_DATE,
14340           GLOBAL_AGREEMENT_FLAG,
14341           CONSIGNED_CONSUMPTION_FLAG,
14342           CBC_ACCOUNTING_DATE,
14343           CONTERMS_EXIST_FLAG , --<CONTERMS FPJ>
14344           encumbrance_required_flag,  --<ENCUMBRANCE FPJ>
14345           enable_all_sites,          --<R12GCPA>
14346           DRAFT_ID ,    --Mod Project
14347           CLM_EFFECTIVE_DATE                  ,
14348           CLM_VENDOR_OFFER_NUMBER                 ,
14349           CLM_AWARD_ADMINISTRATOR                ,
14350           CLM_NO_SIGNED_COPIES_TO_RETURN         ,
14351           CLM_MIN_GUARANTEE_AWARD_AMT            ,
14352           CLM_MIN_GUAR_AWARD_AMT_PERCENT         ,
14353           CLM_MIN_ORDER_AMOUNT                   ,
14354           CLM_MAX_ORDER_AMOUNT                   ,
14355           CLM_AMT_SYNCED_TO_AGREEMENT             ,
14356           CLM_AMOUNT_RELEASED                     ,
14357           CLM_EXTERNAL_IDV                        ,
14358           CLM_SUPPLIER_NAME                        ,
14359           CLM_SUPPLIER_SITE_NAME                  ,
14360           CLM_DOCUMENT_NUMBER                     ,
14361 
14362           CLM_ISSUING_OFFICE                      ,
14363           CLM_COTR_OFFICE                         ,
14364           CLM_COTR_CONTACT                        ,
14365           CLM_PRIORITY_CODE                       ,
14366           CLM_MOD_ISSUING_OFFICE                  ,
14367           CLM_STANDARD_FORM                       ,
14368           CLM_DOCUMENT_FORMAT                     ,
14369           AME_TRANSACTION_TYPE                    ,
14370           CLM_AWARD_TYPE                          ,
14371           CLM_SOURCE_DOCUMENT_ID                  ,
14372           CLM_PAYMENT_INSTR_CODE, -- Payment Instruction
14373           SUPPLIER_NOTIF_METHOD -- MIPR
14374             )
14375           SELECT
14376             PO_HEADER_ID,
14377           AGENT_ID,
14378           TYPE_LOOKUP_CODE,
14379           LAST_UPDATE_DATE,
14380           LAST_UPDATED_BY,
14381           SEGMENT1,
14382           SUMMARY_FLAG,
14383           ENABLED_FLAG,
14384           SEGMENT2,
14385           SEGMENT3,
14386           SEGMENT4,
14387           SEGMENT5,
14388           START_DATE_ACTIVE,
14389           END_DATE_ACTIVE,
14390           LAST_UPDATE_LOGIN,
14391           CREATION_DATE ,
14392           CREATED_BY,
14393           VENDOR_ID,
14394           VENDOR_SITE_ID,
14395           VENDOR_CONTACT_ID,
14396           SHIP_TO_LOCATION_ID,
14397           BILL_TO_LOCATION_ID ,
14398           TERMS_ID             ,
14399           SHIP_VIA_LOOKUP_CODE  ,
14400           FOB_LOOKUP_CODE        ,
14401           FREIGHT_TERMS_LOOKUP_CODE,
14402           STATUS_LOOKUP_CODE,
14403           CURRENCY_CODE,
14404           RATE_TYPE,
14405           RATE_DATE,
14406           RATE,
14407           FROM_HEADER_ID,
14408           FROM_TYPE_LOOKUP_CODE,
14409           START_DATE,
14410           END_DATE,
14411           BLANKET_TOTAL_AMOUNT,
14412           AUTHORIZATION_STATUS,
14413           REVISION_NUM,
14414           REVISED_DATE,
14415           APPROVED_FLAG,
14416           APPROVED_DATE,
14417           AMOUNT_LIMIT,
14418           MIN_RELEASE_AMOUNT,
14419           NOTE_TO_AUTHORIZER,
14420           NOTE_TO_VENDOR,
14421           NOTE_TO_RECEIVER,
14422           PRINT_COUNT,
14423           PRINTED_DATE,
14424           VENDOR_ORDER_NUM,
14425           CONFIRMING_ORDER_FLAG,
14426           COMMENTS,
14427           REPLY_DATE,
14428           REPLY_METHOD_LOOKUP_CODE,
14429           RFQ_CLOSE_DATE,
14430           QUOTE_TYPE_LOOKUP_CODE,
14431           QUOTATION_CLASS_CODE,
14432           QUOTE_WARNING_DELAY_UNIT,
14433           QUOTE_WARNING_DELAY,
14434           QUOTE_VENDOR_QUOTE_NUMBER,
14435           ACCEPTANCE_REQUIRED_FLAG,
14436           ACCEPTANCE_DUE_DATE,
14437           CLOSED_DATE,
14438           USER_HOLD_FLAG,
14439           APPROVAL_REQUIRED_FLAG,
14440           CANCEL_FLAG,
14441           FIRM_STATUS_LOOKUP_CODE,
14442           FIRM_DATE,
14443           FROZEN_FLAG,
14444           ATTRIBUTE_CATEGORY,
14445           ATTRIBUTE1,
14446           ATTRIBUTE2,
14447           ATTRIBUTE3,
14448           ATTRIBUTE4,
14449           ATTRIBUTE5,
14450           ATTRIBUTE6,
14451           ATTRIBUTE7,
14452           ATTRIBUTE8,
14453           ATTRIBUTE9,
14454           ATTRIBUTE10,
14455           ATTRIBUTE11,
14456           ATTRIBUTE12,
14457             ATTRIBUTE13,
14458           ATTRIBUTE14,
14459           ATTRIBUTE15,
14460           CLOSED_CODE,
14461           GOVERNMENT_CONTEXT,
14462           REQUEST_ID,
14463           PROGRAM_APPLICATION_ID,
14464           PROGRAM_ID,
14465           PROGRAM_UPDATE_DATE,
14466           ORG_ID,
14467           SUPPLY_AGREEMENT_FLAG,
14468           EDI_PROCESSED_FLAG,
14469           EDI_PROCESSED_STATUS,
14470           GLOBAL_ATTRIBUTE_CATEGORY,
14471           GLOBAL_ATTRIBUTE1,
14472           GLOBAL_ATTRIBUTE2,
14473           GLOBAL_ATTRIBUTE3,
14474           GLOBAL_ATTRIBUTE4,
14475           GLOBAL_ATTRIBUTE5,
14476           GLOBAL_ATTRIBUTE6,
14477           GLOBAL_ATTRIBUTE7,
14478           GLOBAL_ATTRIBUTE8,
14479           GLOBAL_ATTRIBUTE9,
14480           GLOBAL_ATTRIBUTE10,
14481           GLOBAL_ATTRIBUTE11,
14482           GLOBAL_ATTRIBUTE12,
14483           GLOBAL_ATTRIBUTE13,
14484           GLOBAL_ATTRIBUTE14,
14485           GLOBAL_ATTRIBUTE15,
14486           GLOBAL_ATTRIBUTE16,
14487           GLOBAL_ATTRIBUTE17,
14488           GLOBAL_ATTRIBUTE18,
14489           GLOBAL_ATTRIBUTE19,
14490           GLOBAL_ATTRIBUTE20,
14491           INTERFACE_SOURCE_CODE,
14492           REFERENCE_NUM,
14493           WF_ITEM_TYPE,
14494           WF_ITEM_KEY,
14495           MRC_RATE_TYPE,
14496           MRC_RATE_DATE,
14497           MRC_RATE,
14498           PCARD_ID,
14499           PRICE_UPDATE_TOLERANCE,
14500           PAY_ON_CODE,
14501           XML_FLAG,
14502           XML_SEND_DATE,
14503           XML_CHANGE_SEND_DATE,
14504           GLOBAL_AGREEMENT_FLAG,
14505           CONSIGNED_CONSUMPTION_FLAG,
14506           CBC_ACCOUNTING_DATE,
14507             CONTERMS_EXIST_FLAG  --<CONTERMS FPJ>
14508          ,  encumbrance_required_flag  --<ENCUMBRANCE FPJ>
14509          ,NULL --Temp, enable_all_sites  --<R12GCPA>
14510           , DRAFT_ID,
14511           CLM_EFFECTIVE_DATE                  ,
14512           CLM_VENDOR_OFFER_NUMBER                 ,
14513           CLM_AWARD_ADMINISTRATOR                ,
14514           CLM_NO_SIGNED_COPIES_TO_RETURN         ,
14515           CLM_MIN_GUARANTEE_AWARD_AMT            ,
14516           CLM_MIN_GUAR_AWARD_AMT_PERCENT         ,
14517           CLM_MIN_ORDER_AMOUNT                   ,
14518           CLM_MAX_ORDER_AMOUNT                   ,
14519           CLM_AMT_SYNCED_TO_AGREEMENT             ,
14520           CLM_AMOUNT_RELEASED                     ,
14521           CLM_EXTERNAL_IDV                        ,
14522           CLM_SUPPLIER_NAME                        ,
14523           CLM_SUPPLIER_SITE_NAME                  ,
14524           CLM_DOCUMENT_NUMBER                     ,
14525 
14526           CLM_ISSUING_OFFICE                      ,
14527           CLM_COTR_OFFICE                         ,
14528           CLM_COTR_CONTACT                        ,
14529           CLM_PRIORITY_CODE                       ,
14530           CLM_MOD_ISSUING_OFFICE                  ,
14531           CLM_STANDARD_FORM                       ,
14532           CLM_DOCUMENT_FORMAT                     ,
14533           AME_TRANSACTION_TYPE                    ,
14534           CLM_AWARD_TYPE                          ,
14535           CLM_SOURCE_DOCUMENT_ID                  ,
14536           CLM_PAYMENT_INSTR_CODE, -- Payment Instruction
14537           SUPPLIER_NOTIF_METHOD -- MIPR
14538            --Mod Project
14539 		 /*-- CLM Aprvl
14540 		 FROM po_headers
14541 		 WHERE po_header_id = p_document_id
14542 		 */-- CLM Aprvl
14543           FROM po_headers_merge_v
14544           WHERE po_header_id = p_document_id
14545 		  and draft_id = p_draft_id;
14546 
14547 l_progress := '001';
14548 --SANITY check
14549 SELECT po_header_id, segment1 into t_po_header_id, t_segment1
14550 from po_headers_gt where po_header_id = p_document_id;
14551 
14552 IF g_debug_stmt THEN
14553    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
14554      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
14555           || l_progress,'HEADER ' || to_char(t_po_header_id) ||
14556           'PO NUM ' || t_segment1);
14557    END IF;
14558 END IF;
14559 
14560 l_progress := '002';
14561  x_return_status := FND_API.G_RET_STS_SUCCESS;
14562 EXCEPTION
14563     WHEN FND_API.G_EXC_ERROR THEN
14564         x_return_status := FND_API.G_RET_STS_ERROR;
14565 
14566     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
14567         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
14568 
14569     WHEN OTHERS THEN
14570 
14571         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
14572         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
14573             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
14574         END IF;
14575 
14576         IF (g_debug_unexp) THEN
14577                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
14578                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
14579                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
14580                        || l_progress || ' SQL CODE is '||sqlcode);
14581                 END IF;
14582         END IF;
14583 
14584 END POPULATE_PO_HEADERS_GT;
14585 
14586 
14587 -- <Doc Manager Rewrite 11.5.11 Start>
14588 -------------------------------------------------------------------------------
14589 --Start of Comments
14590 --Name: populate_po_lines_gt
14591 --Pre-reqs:
14592 --  None.
14593 --Modifies:
14594 --  PO_LINES_GT
14595 --Locks:
14596 --  None.
14597 --Function:
14598 --  Populates the lines GTT for submission checks.
14599 --Parameters:
14600 --IN:
14601 --p_doc_type
14602 --  Document type.  Use the g_doc_type_<> variables, where <> is:
14603 --    PA
14604 --    PO
14605 --p_doc_level
14606 --  The type of id that is being passed.  Use g_doc_level_<>
14607 --    HEADER
14608 --    LINE
14609 --    SHIPMENT
14610 --    DISTRIBUTION
14611 --p_doc_level_id
14612 --  Id of the doc level type of which to populate the table.
14613 --p_draft_id
14614 -- Added for CLM Aprvl
14615 -- Draft Id of the Mod
14616 --
14617 --Testing:
14618 --
14619 --End of Comments
14620 -------------------------------------------------------------------------------
14621 PROCEDURE populate_po_lines_gt(
14622    p_doc_type                       IN             VARCHAR2
14623 ,  p_doc_level                      IN             VARCHAR2
14624 ,  p_doc_level_id                   IN             NUMBER
14625 ,  p_draft_id                       IN             NUMBER -- CLM Aprvl
14626 ,  x_return_status                  OUT NOCOPY     VARCHAR2
14627 )
14628 IS
14629 
14630 l_api_name  CONSTANT varchar2(40) := 'POPULATE_PO_LINES_GT';
14631 l_progress VARCHAR2(3);
14632 
14633 l_line_id_tbl    po_tbl_number;
14634 -- PAR Project
14635 l_draft_type VARCHAR2(30);
14636 
14637 BEGIN
14638 
14639   l_progress := '000';
14640 
14641   IF g_debug_stmt THEN
14642      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
14643        FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
14644             || l_progress,'Popluate LINES');
14645      END IF;
14646   END IF;
14647 
14648   l_progress := '010';
14649 
14650   PO_CORE_S.get_line_ids(
14651      p_doc_type => p_doc_type
14652   ,  p_doc_level => p_doc_level
14653   ,  p_doc_level_id_tbl => po_tbl_number( p_doc_level_id )
14654   ,  p_draft_id => p_draft_id
14655   ,  x_line_id_tbl => l_line_id_tbl
14656   );
14657 
14658   l_progress := '020';
14659 
14660    --<PAR Project>
14661    l_draft_type := 'AWARD';
14662    IF p_draft_id <> -1 THEN
14663    BEGIN
14664    SELECT draft_type
14665    INTO l_draft_type
14666    FROM po_drafts
14667    WHERE draft_id = p_draft_id;
14668    Exception
14669    When No_Data_Found Then
14670     l_draft_type := 'AWARD';
14671    End;
14672    END IF;
14673 
14674 
14675   FORALL i IN 1 .. l_line_id_tbl.COUNT
14676     INSERT INTO po_lines_gt(
14677          PO_LINE_ID       ,
14678          LAST_UPDATE_DATE  ,
14679          LAST_UPDATED_BY    ,
14680          PO_HEADER_ID        ,
14681          LINE_TYPE_ID         ,
14682          LINE_NUM              ,
14683          LAST_UPDATE_LOGIN      ,
14684          CREATION_DATE           ,
14685          CREATED_BY               ,
14686          ITEM_ID                   ,
14687          ITEM_REVISION              ,
14688          CATEGORY_ID                 ,
14689          ITEM_DESCRIPTION             ,
14690          UNIT_MEAS_LOOKUP_CODE         ,
14691          QUANTITY_COMMITTED             ,
14692          COMMITTED_AMOUNT                ,
14693          ALLOW_PRICE_OVERRIDE_FLAG        ,
14694          NOT_TO_EXCEED_PRICE               ,
14695          LIST_PRICE_PER_UNIT                ,
14696          UNIT_PRICE                          ,
14697          QUANTITY                             ,
14698          UN_NUMBER_ID                          ,
14699          HAZARD_CLASS_ID                        ,
14700          NOTE_TO_VENDOR                          ,
14701          FROM_HEADER_ID                          ,
14702          FROM_LINE_ID                            ,
14703          MIN_ORDER_QUANTITY                      ,
14704          MAX_ORDER_QUANTITY                      ,
14705          QTY_RCV_TOLERANCE                       ,
14706          OVER_TOLERANCE_ERROR_FLAG               ,
14707          MARKET_PRICE                            ,
14708          UNORDERED_FLAG                          ,
14709          CLOSED_FLAG                              ,
14710          USER_HOLD_FLAG                          ,
14711          CANCEL_FLAG                              ,
14712          CANCELLED_BY                             ,
14713          CANCEL_DATE                             ,
14714          CANCEL_REASON                            ,
14715          FIRM_STATUS_LOOKUP_CODE                  ,
14716          FIRM_DATE                                ,
14717          VENDOR_PRODUCT_NUM                       ,
14718          CONTRACT_NUM                             ,
14719          TAXABLE_FLAG                             ,
14720          TAX_NAME                                 ,
14721          TYPE_1099                                ,
14722          CAPITAL_EXPENSE_FLAG                     ,
14723          NEGOTIATED_BY_PREPARER_FLAG              ,
14724          ATTRIBUTE_CATEGORY                       ,
14725          ATTRIBUTE1                               ,
14726          ATTRIBUTE2                               ,
14727          ATTRIBUTE3                               ,
14728          ATTRIBUTE4                               ,
14729          ATTRIBUTE5                               ,
14730          ATTRIBUTE6                               ,
14731          ATTRIBUTE7                               ,
14732          ATTRIBUTE8                               ,
14733          ATTRIBUTE9                              ,
14734          ATTRIBUTE10                              ,
14735          REFERENCE_NUM                            ,
14736          ATTRIBUTE11                              ,
14737          ATTRIBUTE12                              ,
14738          ATTRIBUTE13                              ,
14739          ATTRIBUTE14                              ,
14740          ATTRIBUTE15                              ,
14741          MIN_RELEASE_AMOUNT                       ,
14742          PRICE_TYPE_LOOKUP_CODE                   ,
14743          CLOSED_CODE                              ,
14744          PRICE_BREAK_LOOKUP_CODE                  ,
14745          GOVERNMENT_CONTEXT                       ,
14746          REQUEST_ID                               ,
14747          PROGRAM_APPLICATION_ID                   ,
14748          PROGRAM_ID                               ,
14749          PROGRAM_UPDATE_DATE                      ,
14750          CLOSED_DATE                              ,
14751          CLOSED_REASON                            ,
14752          CLOSED_BY                                ,
14753          TRANSACTION_REASON_CODE                 ,
14754          ORG_ID                                   ,
14755          QC_GRADE                                 ,
14756          BASE_UOM                                 ,
14757          BASE_QTY                                 ,
14758          SECONDARY_UOM                            ,
14759          SECONDARY_QTY                            ,
14760          GLOBAL_ATTRIBUTE_CATEGORY                ,
14761          GLOBAL_ATTRIBUTE1                        ,
14762          GLOBAL_ATTRIBUTE2                        ,
14763          GLOBAL_ATTRIBUTE3                        ,
14764          GLOBAL_ATTRIBUTE4                        ,
14765          GLOBAL_ATTRIBUTE5                        ,
14766          GLOBAL_ATTRIBUTE6                        ,
14767          GLOBAL_ATTRIBUTE7                        ,
14768          GLOBAL_ATTRIBUTE8                        ,
14769          GLOBAL_ATTRIBUTE9                        ,
14770          GLOBAL_ATTRIBUTE10                       ,
14771          GLOBAL_ATTRIBUTE11                       ,
14772          GLOBAL_ATTRIBUTE12                       ,
14773          GLOBAL_ATTRIBUTE13                       ,
14774          GLOBAL_ATTRIBUTE14                       ,
14775          GLOBAL_ATTRIBUTE15                       ,
14776          GLOBAL_ATTRIBUTE16                       ,
14777          GLOBAL_ATTRIBUTE17                      ,
14778          GLOBAL_ATTRIBUTE18                      ,
14779          GLOBAL_ATTRIBUTE19                      ,
14780          GLOBAL_ATTRIBUTE20                      ,
14781          LINE_REFERENCE_NUM                       ,
14782          PROJECT_ID                               ,
14783          TASK_ID                                  ,
14784          EXPIRATION_DATE                          ,
14785          TAX_CODE_ID                              ,
14786          OKE_CONTRACT_HEADER_ID                   ,
14787          OKE_CONTRACT_VERSION_ID                  ,
14788          SECONDARY_QUANTITY                       ,
14789          SECONDARY_UNIT_OF_MEASURE               ,
14790          PREFERRED_GRADE                          ,
14791          AUCTION_HEADER_ID                       ,
14792          AUCTION_DISPLAY_NUMBER                  ,
14793          AUCTION_LINE_NUMBER                     ,
14794          BID_NUMBER                              ,
14795          BID_LINE_NUMBER                          ,
14796          RETROACTIVE_DATE                         ,
14797          CONTRACT_ID                              ,   -- <GC FPJ>
14798          START_DATE                               , -- <PO_CHANGE_API FPJ>
14799          AMOUNT                                   , -- <PO_CHANGE_API FPJ>
14800          ORDER_TYPE_LOOKUP_CODE                   ,            -- <BUG 3262859>
14801          PURCHASE_BASIS                           ,            -- <BUG 3262859>
14802          MATCHING_BASIS                           ,             -- <BUG 3262859>
14803          CLM_INFO_FLAG                            ,              -- <BUG 9165625>
14804 	       CLM_OPTION_INDICATOR			               ,
14805          CLM_MIN_TOTAL_AMOUNT                    ,
14806          CLM_MAX_TOTAL_AMOUNT                    ,
14807          CLM_MIN_TOTAL_QUANTITY                  ,
14808          CLM_MAX_TOTAL_QUANTITY                  ,
14809          CLM_MIN_ORDER_AMOUNT                    ,
14810          CLM_MAX_ORDER_AMOUNT                     ,
14811          CLM_MIN_ORDER_QUANTITY                  ,
14812          CLM_MAX_ORDER_QUANTITY                  ,
14813          CLM_TOTAL_AMOUNT_ORDERED                ,
14814          CLM_TOTAL_QUANTITY_ORDERED              ,
14815          CLM_FSC_PSC                             ,
14816          CLM_MDAPS_MAIS                          ,
14817          CLM_NAICS                               ,
14818          CLM_ORDER_START_DATE                    ,
14819          CLM_ORDER_END_DATE                      ,
14820          CLM_EXERCISED_FLAG                      ,
14821          CLM_EXERCISED_DATE                      ,
14822 	 LINE_NUM_DISPLAY                        ,
14823          CONTRACT_TYPE                           , -- CLM-PHASE-2 Submission Check
14824          CLM_IDC_TYPE                            ,  -- CLM-PHASE-2 Submission Check
14825          CLM_DELIVERY_EVENT_CODE                 ,   --<Event Based Delivery>
14826          CLM_PAYMENT_INSTR_CODE                  ,
14827          CLM_POP_EXCEPTION_REASON                ,   --<Sunset Memo POP validation>
14828          OLD_AMOUNT                              ,--UCA Project - CLMR4 Changes Begin
14829          OLD_QUANTITY                            ,
14830          OLD_UNIT_PRICE                          ,
14831          CLM_UNDEF_FLAG                          ,
14832          CLM_UNDEF_ACTION_CODE                   ,--UCA Project - CLMR4 Changes End
14833          CLM_EXHIBIT_NAME                        , -- CLM Phase 4 - Elins project
14834          COST_CONSTRAINT                           -- CLM Phase 4 - Elins project
14835      )
14836      SELECT
14837               pol.PO_LINE_ID                              ,
14838          pol.LAST_UPDATE_DATE                        ,
14839          pol.LAST_UPDATED_BY                 ,
14840          pol.PO_HEADER_ID                    ,
14841          pol.LINE_TYPE_ID                   ,
14842          pol.LINE_NUM                        ,
14843          pol.LAST_UPDATE_LOGIN                ,
14844          pol.CREATION_DATE                     ,
14845          pol.CREATED_BY                         ,
14846          pol.ITEM_ID                             ,
14847          pol.ITEM_REVISION                        ,
14848          pol.CATEGORY_ID                           ,
14849          pol.ITEM_DESCRIPTION                       ,
14850          pol.UNIT_MEAS_LOOKUP_CODE                   ,
14851          pol.QUANTITY_COMMITTED                      ,
14852          pol.COMMITTED_AMOUNT                         ,
14853          pol.ALLOW_PRICE_OVERRIDE_FLAG               ,
14854          pol.NOT_TO_EXCEED_PRICE                      ,
14855          pol.LIST_PRICE_PER_UNIT                      ,
14856          pol.UNIT_PRICE                               ,
14857          pol.QUANTITY                                 ,
14858          pol.UN_NUMBER_ID                             ,
14859          pol.HAZARD_CLASS_ID                          ,
14860          pol.NOTE_TO_VENDOR                           ,
14861          pol.FROM_HEADER_ID                          ,
14862          pol.FROM_LINE_ID                            ,
14863          pol.MIN_ORDER_QUANTITY                      ,
14864          pol.MAX_ORDER_QUANTITY                      ,
14865          pol.QTY_RCV_TOLERANCE                       ,
14866          pol.OVER_TOLERANCE_ERROR_FLAG               ,
14867          pol.MARKET_PRICE                            ,
14868          pol.UNORDERED_FLAG                          ,
14869          pol.CLOSED_FLAG                              ,
14870          pol.USER_HOLD_FLAG                          ,
14871          pol.CANCEL_FLAG                              ,
14872          pol.CANCELLED_BY                             ,
14873          pol.CANCEL_DATE                             ,
14874          pol.CANCEL_REASON                            ,
14875          pol.FIRM_STATUS_LOOKUP_CODE                  ,
14876          pol.FIRM_DATE                                ,
14877          pol.VENDOR_PRODUCT_NUM                       ,
14878          pol.CONTRACT_NUM                             ,
14879          pol.TAXABLE_FLAG                             ,
14880          pol.TAX_NAME                                 ,
14881          pol.TYPE_1099                                ,
14882          pol.CAPITAL_EXPENSE_FLAG                     ,
14883          pol.NEGOTIATED_BY_PREPARER_FLAG              ,
14884          pol.ATTRIBUTE_CATEGORY                       ,
14885          pol.ATTRIBUTE1                               ,
14886          pol.ATTRIBUTE2                               ,
14887          pol.ATTRIBUTE3                               ,
14888          pol.ATTRIBUTE4                               ,
14889          pol.ATTRIBUTE5                               ,
14890          pol.ATTRIBUTE6                               ,
14891          pol.ATTRIBUTE7                               ,
14892          pol.ATTRIBUTE8                               ,
14893          pol.ATTRIBUTE9                              ,
14894          pol.ATTRIBUTE10                              ,
14895          pol.REFERENCE_NUM                            ,
14896          pol.ATTRIBUTE11                              ,
14897          pol.ATTRIBUTE12                              ,
14898          pol.ATTRIBUTE13                              ,
14899          pol.ATTRIBUTE14                              ,
14900          pol.ATTRIBUTE15                              ,
14901          pol.MIN_RELEASE_AMOUNT                       ,
14902          pol.PRICE_TYPE_LOOKUP_CODE                   ,
14903          pol.CLOSED_CODE                              ,
14904          pol.PRICE_BREAK_LOOKUP_CODE                  ,
14905          pol.GOVERNMENT_CONTEXT                       ,
14906          pol.REQUEST_ID                               ,
14907          pol.PROGRAM_APPLICATION_ID                   ,
14908          pol.PROGRAM_ID                               ,
14909          pol.PROGRAM_UPDATE_DATE                      ,
14910          pol.CLOSED_DATE                              ,
14911          pol.CLOSED_REASON                            ,
14912          pol.CLOSED_BY                                ,
14913          pol.TRANSACTION_REASON_CODE                 ,
14914          pol.ORG_ID                                   ,
14915          pol.QC_GRADE                                 ,
14916          pol.BASE_UOM                                 ,
14917          pol.BASE_QTY                                 ,
14918          pol.SECONDARY_UOM                            ,
14919         pol.SECONDARY_QTY                            ,
14920          pol.GLOBAL_ATTRIBUTE_CATEGORY                ,
14921          pol.GLOBAL_ATTRIBUTE1                        ,
14922          pol.GLOBAL_ATTRIBUTE2                        ,
14923          pol.GLOBAL_ATTRIBUTE3                        ,
14924          pol.GLOBAL_ATTRIBUTE4                        ,
14925          pol.GLOBAL_ATTRIBUTE5                        ,
14926          pol.GLOBAL_ATTRIBUTE6                        ,
14927          pol.GLOBAL_ATTRIBUTE7                        ,
14928          pol.GLOBAL_ATTRIBUTE8                        ,
14929          pol.GLOBAL_ATTRIBUTE9                        ,
14930          pol.GLOBAL_ATTRIBUTE10                       ,
14931          pol.GLOBAL_ATTRIBUTE11                       ,
14932          pol.GLOBAL_ATTRIBUTE12                       ,
14933          pol.GLOBAL_ATTRIBUTE13                       ,
14934          pol.GLOBAL_ATTRIBUTE14                       ,
14935          pol.GLOBAL_ATTRIBUTE15                       ,
14936          pol.GLOBAL_ATTRIBUTE16                       ,
14937          pol.GLOBAL_ATTRIBUTE17                      ,
14938          pol.GLOBAL_ATTRIBUTE18                      ,
14939          pol.GLOBAL_ATTRIBUTE19                      ,
14940          pol.GLOBAL_ATTRIBUTE20                      ,
14941          pol.LINE_REFERENCE_NUM                       ,
14942          pol.PROJECT_ID                               ,
14943          pol.TASK_ID                                  ,
14944          pol.EXPIRATION_DATE                          ,
14945          pol.TAX_CODE_ID                              ,
14946          pol.OKE_CONTRACT_HEADER_ID                   ,
14947          pol.OKE_CONTRACT_VERSION_ID                  ,
14948          pol.SECONDARY_QUANTITY                       ,
14949          pol.SECONDARY_UNIT_OF_MEASURE               ,
14950          pol.PREFERRED_GRADE                          ,
14951          pol.AUCTION_HEADER_ID                       ,
14952          pol.AUCTION_DISPLAY_NUMBER                  ,
14953          pol.AUCTION_LINE_NUMBER                     ,
14954          pol.BID_NUMBER                              ,
14955         pol.BID_LINE_NUMBER                          ,
14956          pol.RETROACTIVE_DATE                        ,
14957          pol.CONTRACT_ID                              , -- <GC FPJ>
14958          pol.START_DATE                               , -- <PO_CHANGE_API FPJ>
14959          pol.AMOUNT                                   , -- <PO_CHANGE_API FPJ>
14960          pol.ORDER_TYPE_LOOKUP_CODE                   ,            -- <BUG 3262859>
14961          pol.PURCHASE_BASIS                           ,            -- <BUG 3262859>
14962          pol.MATCHING_BASIS                           ,             -- <BUG 3262859>
14963          pol.CLM_INFO_FLAG                           ,               -- <BUG 9165625>
14964 	 pol.CLM_OPTION_INDICATOR			 ,
14965          pol.CLM_MIN_TOTAL_AMOUNT                    ,
14966          pol.CLM_MAX_TOTAL_AMOUNT                    ,
14967          pol.CLM_MIN_TOTAL_QUANTITY                  ,
14968          pol.CLM_MAX_TOTAL_QUANTITY                  ,
14969          pol.CLM_MIN_ORDER_AMOUNT                    ,
14970          pol.CLM_MAX_ORDER_AMOUNT                     ,
14971          pol.CLM_MIN_ORDER_QUANTITY                  ,
14972          pol.CLM_MAX_ORDER_QUANTITY                  ,
14973          pol.CLM_TOTAL_AMOUNT_ORDERED                ,
14974          pol.CLM_TOTAL_QUANTITY_ORDERED              ,
14975          pol.CLM_FSC_PSC                             ,
14976          pol.CLM_MDAPS_MAIS                          ,
14977          pol.CLM_NAICS                               ,
14978          pol.CLM_ORDER_START_DATE                    ,
14979          pol.CLM_ORDER_END_DATE                      ,
14980          pol.CLM_EXERCISED_FLAG                      ,
14981          pol.CLM_EXERCISED_DATE                      ,
14982 	       pol.LINE_NUM_DISPLAY                        ,
14983          pol.CONTRACT_TYPE                           , -- CLM-PHASE-2 Submission Check
14984          pol.CLM_IDC_TYPE                            , -- CLM-PHASE-2 Submission Check
14985          pol.CLM_DELIVERY_EVENT_CODE                 ,        --<Event Based Delivery>
14986          pol.CLM_PAYMENT_INSTR_CODE                  ,
14987          pol.CLM_POP_EXCEPTION_REASON                ,   --<Sunset Memo POP validation>
14988          pol.OLD_AMOUNT                              ,--UCA Project - CLMR4 Changes Begin
14989          pol.OLD_QUANTITY                            ,
14990          pol.OLD_UNIT_PRICE                          ,
14991          pol.CLM_UNDEF_FLAG                          ,
14992          pol.CLM_UNDEF_ACTION_CODE                   , --UCA Project - CLMR4 Changes End
14993          pol.CLM_EXHIBIT_NAME                        , -- CLM Phase 4 - Elins project
14994          pol.COST_CONSTRAINT                           -- CLM Phase 4 - Elins project
14995 	   FROM po_lines_merge_v pol
14996       Where pol.po_line_id = l_line_id_tbl(i)
14997       and pol.draft_id = p_draft_id
14998             /*-- CLM Aprvl
14999 	        FROM po_lines_all pol
15000             WHERE pol.po_line_id = l_line_id_tbl(i)
15001 	        -- CLM Aprvl*/
15002       --<PAR Project>  need to change the entities which should be validated. Only records in the draft tables should be validated.
15003       AND (l_draft_type <> 'PAR'
15004           OR (l_draft_type = 'PAR' AND pol.change_status IS NOT NULL ));
15005 
15006 
15007 
15008   l_progress := '030';
15009   x_return_status := FND_API.G_RET_STS_SUCCESS;
15010 
15011 EXCEPTION
15012     WHEN FND_API.G_EXC_ERROR THEN
15013         x_return_status := FND_API.G_RET_STS_ERROR;
15014 
15015     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
15016         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
15017 
15018     WHEN OTHERS THEN
15019         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
15020         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
15021             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
15022         END IF;
15023 
15024         IF (g_debug_unexp) THEN
15025                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
15026                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
15027                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
15028                        || l_progress || ' SQL CODE is '||sqlcode);
15029                 END IF;
15030         END IF;
15031 
15032 END POPULATE_PO_LINES_GT;
15033 -- <End Doc Manager Rewrite 11.5.11>
15034 
15035 
15036 /**
15037 * Private Procedure: POPULATE_RELEASES_GT
15038 * Requires:
15039 *   IN PARAMETERS:
15040 *       p_document_id: Id of submitted document
15041 * Modifies:
15042 * Effects:  Populates the global temp tables po_headers_gt
15043 * Returns:
15044 */
15045 PROCEDURE populate_releases_gt(p_document_id IN NUMBER,
15046                  x_return_status OUT NOCOPY VARCHAR2) IS
15047 
15048 l_api_name  CONSTANT varchar2(40) := 'POPULATE_RELEASES_GT';
15049 l_progress VARCHAR2(3);
15050 
15051 BEGIN
15052 l_progress := '000';
15053 IF g_debug_stmt THEN
15054    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
15055      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
15056           || l_progress,'Populate PO Releases');
15057    END IF;
15058 END IF;
15059 
15060     INSERT INTO po_releases_gt(
15061          PO_RELEASE_ID                   ,
15062          LAST_UPDATE_DATE                ,
15063          LAST_UPDATED_BY                ,
15064          PO_HEADER_ID                  ,
15065          RELEASE_NUM                    ,
15066          AGENT_ID                       ,
15067          RELEASE_DATE                  ,
15068          LAST_UPDATE_LOGIN              ,
15069          CREATION_DATE                  ,
15070          CREATED_BY                      ,
15071          REVISION_NUM                     ,
15072          REVISED_DATE                      ,
15073          APPROVED_FLAG                      ,
15074          APPROVED_DATE                       ,
15075          PRINT_COUNT                          ,
15076          PRINTED_DATE                          ,
15077          ACCEPTANCE_REQUIRED_FLAG               ,
15078          ACCEPTANCE_DUE_DATE                     ,
15079          HOLD_BY                                  ,
15080          HOLD_DATE                                ,
15081          HOLD_REASON                              ,
15082          HOLD_FLAG                                ,
15083          CANCEL_FLAG                              ,
15084          CANCELLED_BY                             ,
15085          CANCEL_DATE                              ,
15086          CANCEL_REASON                            ,
15087          FIRM_STATUS_LOOKUP_CODE                  ,
15088          FIRM_DATE                                ,
15089          ATTRIBUTE_CATEGORY                       ,
15090          ATTRIBUTE1                               ,
15091          ATTRIBUTE2                               ,
15092          ATTRIBUTE3                               ,
15093          ATTRIBUTE4                               ,
15094          ATTRIBUTE5                               ,
15095          ATTRIBUTE6                               ,
15096          ATTRIBUTE7                               ,
15097          ATTRIBUTE8                               ,
15098          ATTRIBUTE9                               ,
15099          ATTRIBUTE10                              ,
15100          ATTRIBUTE11                              ,
15101          ATTRIBUTE12                              ,
15102          ATTRIBUTE13                              ,
15103          ATTRIBUTE14                              ,
15104          ATTRIBUTE15                              ,
15105          AUTHORIZATION_STATUS                     ,
15106          GOVERNMENT_CONTEXT                       ,
15107          REQUEST_ID                               ,
15108          PROGRAM_APPLICATION_ID                   ,
15109          PROGRAM_ID                               ,
15110          PROGRAM_UPDATE_DATE                      ,
15111          CLOSED_CODE                              ,
15112          FROZEN_FLAG                              ,
15113          RELEASE_TYPE                             ,
15114          NOTE_TO_VENDOR                           ,
15115          ORG_ID                                   ,
15116          EDI_PROCESSED_FLAG                       ,
15117          GLOBAL_ATTRIBUTE_CATEGORY                ,
15118          GLOBAL_ATTRIBUTE1                        ,
15119          GLOBAL_ATTRIBUTE2                        ,
15120          GLOBAL_ATTRIBUTE3                        ,
15121          GLOBAL_ATTRIBUTE4                        ,
15122          GLOBAL_ATTRIBUTE5                        ,
15123          GLOBAL_ATTRIBUTE6                        ,
15124          GLOBAL_ATTRIBUTE7                        ,
15125          GLOBAL_ATTRIBUTE8                        ,
15126          GLOBAL_ATTRIBUTE9                        ,
15127          GLOBAL_ATTRIBUTE10                       ,
15128          GLOBAL_ATTRIBUTE11                       ,
15129          GLOBAL_ATTRIBUTE12                       ,
15130          GLOBAL_ATTRIBUTE13                       ,
15131          GLOBAL_ATTRIBUTE14                       ,
15132          GLOBAL_ATTRIBUTE15                       ,
15133          GLOBAL_ATTRIBUTE16                       ,
15134          GLOBAL_ATTRIBUTE17                       ,
15135          GLOBAL_ATTRIBUTE18                       ,
15136          GLOBAL_ATTRIBUTE19                       ,
15137          GLOBAL_ATTRIBUTE20                       ,
15138          WF_ITEM_TYPE                             ,
15139          WF_ITEM_KEY                              ,
15140          PCARD_ID                                ,
15141          PAY_ON_CODE                             ,
15142          XML_FLAG                                 ,
15143          XML_SEND_DATE                            ,
15144          XML_CHANGE_SEND_DATE                     ,
15145          CONSIGNED_CONSUMPTION_FLAG               ,
15146          CBC_ACCOUNTING_DATE)
15147       SELECT
15148                   PO_RELEASE_ID                   ,
15149          LAST_UPDATE_DATE                ,
15150          LAST_UPDATED_BY                ,
15151          PO_HEADER_ID                  ,
15152          RELEASE_NUM                    ,
15153          AGENT_ID                       ,
15154          RELEASE_DATE                  ,
15155          LAST_UPDATE_LOGIN              ,
15156          CREATION_DATE                  ,
15157          CREATED_BY                      ,
15158          REVISION_NUM                     ,
15159          REVISED_DATE                      ,
15160          APPROVED_FLAG                      ,
15161          APPROVED_DATE                       ,
15162          PRINT_COUNT                          ,
15163          PRINTED_DATE                          ,
15164          ACCEPTANCE_REQUIRED_FLAG               ,
15165          ACCEPTANCE_DUE_DATE                     ,
15166          HOLD_BY                                  ,
15167          HOLD_DATE                                ,
15168          HOLD_REASON                              ,
15169          HOLD_FLAG                                ,
15170          CANCEL_FLAG                              ,
15171          CANCELLED_BY                             ,
15172          CANCEL_DATE                              ,
15173          CANCEL_REASON                            ,
15174          FIRM_STATUS_LOOKUP_CODE                  ,
15175          FIRM_DATE                                ,
15176          ATTRIBUTE_CATEGORY                       ,
15177          ATTRIBUTE1                               ,
15178          ATTRIBUTE2                               ,
15179          ATTRIBUTE3                               ,
15180          ATTRIBUTE4                               ,
15181          ATTRIBUTE5                               ,
15182          ATTRIBUTE6                               ,
15183          ATTRIBUTE7                               ,
15184          ATTRIBUTE8                               ,
15185          ATTRIBUTE9                               ,
15186          ATTRIBUTE10                              ,
15187          ATTRIBUTE11                              ,
15188          ATTRIBUTE12                              ,
15189          ATTRIBUTE13                              ,
15190          ATTRIBUTE14                              ,
15191          ATTRIBUTE15                              ,
15192          AUTHORIZATION_STATUS                     ,
15193          GOVERNMENT_CONTEXT                       ,
15194          REQUEST_ID                               ,
15195          PROGRAM_APPLICATION_ID                   ,
15196          PROGRAM_ID                               ,
15197          PROGRAM_UPDATE_DATE                      ,
15198          CLOSED_CODE                              ,
15199          FROZEN_FLAG                              ,
15200          RELEASE_TYPE                             ,
15201          NOTE_TO_VENDOR                           ,
15202          ORG_ID                                   ,
15203          EDI_PROCESSED_FLAG                       ,
15204          GLOBAL_ATTRIBUTE_CATEGORY                ,
15205          GLOBAL_ATTRIBUTE1                        ,
15206          GLOBAL_ATTRIBUTE2                        ,
15207          GLOBAL_ATTRIBUTE3                        ,
15208          GLOBAL_ATTRIBUTE4                        ,
15209          GLOBAL_ATTRIBUTE5                        ,
15210          GLOBAL_ATTRIBUTE6                        ,
15211          GLOBAL_ATTRIBUTE7                        ,
15212          GLOBAL_ATTRIBUTE8                        ,
15213          GLOBAL_ATTRIBUTE9                        ,
15214          GLOBAL_ATTRIBUTE10                       ,
15215          GLOBAL_ATTRIBUTE11                       ,
15216          GLOBAL_ATTRIBUTE12                       ,
15217          GLOBAL_ATTRIBUTE13                       ,
15218          GLOBAL_ATTRIBUTE14                       ,
15219          GLOBAL_ATTRIBUTE15                       ,
15220          GLOBAL_ATTRIBUTE16                       ,
15221          GLOBAL_ATTRIBUTE17                       ,
15222          GLOBAL_ATTRIBUTE18                       ,
15223          GLOBAL_ATTRIBUTE19                       ,
15224          GLOBAL_ATTRIBUTE20                       ,
15225          WF_ITEM_TYPE                             ,
15226          WF_ITEM_KEY                              ,
15227          PCARD_ID                                ,
15228          PAY_ON_CODE                             ,
15229          XML_FLAG                                 ,
15230          XML_SEND_DATE                            ,
15231          XML_CHANGE_SEND_DATE                     ,
15232          CONSIGNED_CONSUMPTION_FLAG               ,
15233          CBC_ACCOUNTING_DATE
15234      FROM po_releases
15235      WHERE po_release_id = p_document_id;
15236 
15237 l_progress := '001';
15238     x_return_status := FND_API.G_RET_STS_SUCCESS;
15239 EXCEPTION
15240     WHEN FND_API.G_EXC_ERROR THEN
15241         x_return_status := FND_API.G_RET_STS_ERROR;
15242 
15243     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
15244         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
15245 
15246     WHEN OTHERS THEN
15247         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
15248         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
15249             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
15250         END IF;
15251 
15252         IF (g_debug_unexp) THEN
15253                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
15254                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
15255                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
15256                        || l_progress || ' SQL CODE is '||sqlcode);
15257                 END IF;
15258         END IF;
15259 
15260 END POPULATE_RELEASES_GT;
15261 
15262 
15263 /**
15264 * Private Procedure: POPULATE_REQ_HEADERS_GT
15265 * Requires:
15266 *   IN PARAMETERS:
15267 *       p_document_id: Id of submitted document
15268 * Modifies:
15269 * Effects:  Populates the global temp tables po_headers_gt
15270 * Returns:
15271 */
15272 PROCEDURE populate_req_headers_gt(p_document_id IN NUMBER,
15273                         x_return_status OUT NOCOPY VARCHAR2) IS
15274 
15275 l_api_name  CONSTANT varchar2(40) := 'POPULATE_REQ_HEADERS_GT';
15276 l_progress VARCHAR2(3);
15277 
15278 BEGIN
15279 
15280 l_progress := '000';
15281 IF g_debug_stmt THEN
15282    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
15283      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
15284           || l_progress,'Populate Req Headers');
15285    END IF;
15286 END IF;
15287 
15288     INSERT INTO po_req_headers_gt(
15289          PROGRAM_UPDATE_DATE                      ,
15290          INTERFACE_SOURCE_CODE                    ,
15291          INTERFACE_SOURCE_LINE_ID                 ,
15292          CLOSED_CODE                              ,
15293          ORG_ID                                   ,
15294          DESCRIPTION                              ,
15295          AUTHORIZATION_STATUS                     ,
15296          NOTE_TO_AUTHORIZER                       ,
15297          TYPE_LOOKUP_CODE                         ,
15298          TRANSFERRED_TO_OE_FLAG                   ,
15299          ATTRIBUTE_CATEGORY                       ,
15300          ATTRIBUTE1                               ,
15301          ATTRIBUTE2                               ,
15302          ATTRIBUTE3                               ,
15303          ATTRIBUTE4                               ,
15304          ATTRIBUTE5                               ,
15305          ON_LINE_FLAG                             ,
15306          PRELIMINARY_RESEARCH_FLAG                ,
15307          RESEARCH_COMPLETE_FLAG                   ,
15308          PREPARER_FINISHED_FLAG                   ,
15309          PREPARER_FINISHED_DATE                   ,
15310          AGENT_RETURN_FLAG                        ,
15311          AGENT_RETURN_NOTE                        ,
15312          CANCEL_FLAG                              ,
15313          ATTRIBUTE6                               ,
15314          ATTRIBUTE7                               ,
15315          ATTRIBUTE8                               ,
15316          ATTRIBUTE9                               ,
15317          ATTRIBUTE10                              ,
15318          ATTRIBUTE11                              ,
15319          ATTRIBUTE12                              ,
15320          ATTRIBUTE13                              ,
15321          ATTRIBUTE14                              ,
15322          ATTRIBUTE15                              ,
15323          GOVERNMENT_CONTEXT                       ,
15324          REQUEST_ID                               ,
15325          PROGRAM_APPLICATION_ID                   ,
15326          PROGRAM_ID                               ,
15327          REQUISITION_HEADER_ID             ,
15328          PREPARER_ID                       ,
15329          LAST_UPDATE_DATE                   ,
15330          LAST_UPDATED_BY                   ,
15331          SEGMENT1                          ,
15332          SUMMARY_FLAG                       ,
15333          ENABLED_FLAG                       ,
15334          SEGMENT2                                 ,
15335          SEGMENT3                                 ,
15336          SEGMENT4                                 ,
15337          SEGMENT5                                 ,
15338          START_DATE_ACTIVE                        ,
15339          END_DATE_ACTIVE                          ,
15340          LAST_UPDATE_LOGIN                        ,
15341          CREATION_DATE                            ,
15342          CREATED_BY                               ,
15343          WF_ITEM_TYPE                             ,
15344          WF_ITEM_KEY                              ,
15345          EMERGENCY_PO_NUM                          ,
15346          PCARD_ID                                    ,
15347          APPS_SOURCE_CODE                          ,
15348          CBC_ACCOUNTING_DATE)
15349      SELECT
15350          PROGRAM_UPDATE_DATE                      ,
15351          INTERFACE_SOURCE_CODE                    ,
15352          INTERFACE_SOURCE_LINE_ID                 ,
15353          CLOSED_CODE                              ,
15354          ORG_ID                                   ,
15355          DESCRIPTION                              ,
15356          AUTHORIZATION_STATUS                     ,
15357          substrb(NOTE_TO_AUTHORIZER,1,480)         , -- Bug4443295(added substr)
15358          TYPE_LOOKUP_CODE                         ,
15359          TRANSFERRED_TO_OE_FLAG                   ,
15360          ATTRIBUTE_CATEGORY                       ,
15361          ATTRIBUTE1                               ,
15362          ATTRIBUTE2                               ,
15363          ATTRIBUTE3                               ,
15364          ATTRIBUTE4                               ,
15365          ATTRIBUTE5                               ,
15366          ON_LINE_FLAG                             ,
15367          PRELIMINARY_RESEARCH_FLAG                ,
15368          RESEARCH_COMPLETE_FLAG                   ,
15369          PREPARER_FINISHED_FLAG                   ,
15370          PREPARER_FINISHED_DATE                   ,
15371          AGENT_RETURN_FLAG                        ,
15372          AGENT_RETURN_NOTE                        ,
15373          CANCEL_FLAG                              ,
15374          ATTRIBUTE6                               ,
15375          ATTRIBUTE7                               ,
15376          ATTRIBUTE8                               ,
15377          ATTRIBUTE9                               ,
15378          ATTRIBUTE10                              ,
15379          ATTRIBUTE11                              ,
15380          ATTRIBUTE12                              ,
15381          ATTRIBUTE13                              ,
15382          ATTRIBUTE14                              ,
15383          ATTRIBUTE15                              ,
15384          GOVERNMENT_CONTEXT                       ,
15385          REQUEST_ID                               ,
15386          PROGRAM_APPLICATION_ID                   ,
15387          PROGRAM_ID                               ,
15388          REQUISITION_HEADER_ID             ,
15389          PREPARER_ID                       ,
15390          LAST_UPDATE_DATE                   ,
15391          LAST_UPDATED_BY                   ,
15392          SEGMENT1                          ,
15393          SUMMARY_FLAG                       ,
15394          ENABLED_FLAG                       ,
15395          SEGMENT2                                 ,
15396          SEGMENT3                                 ,
15397          SEGMENT4                                 ,
15398          SEGMENT5                                 ,
15399          START_DATE_ACTIVE                        ,
15400          END_DATE_ACTIVE                          ,
15401          LAST_UPDATE_LOGIN                        ,
15402          CREATION_DATE                            ,
15403          CREATED_BY                               ,
15404          WF_ITEM_TYPE                             ,
15405          WF_ITEM_KEY                              ,
15406          EMERGENCY_PO_NUM                          ,
15407          PCARD_ID                                    ,
15408          APPS_SOURCE_CODE                          ,
15409          CBC_ACCOUNTING_DATE
15410     FROM po_requisition_headers
15411     WHERE requisition_header_id = p_document_id;
15412 
15413 l_progress := '001';
15414     x_return_status := FND_API.G_RET_STS_SUCCESS;
15415 EXCEPTION
15416     WHEN FND_API.G_EXC_ERROR THEN
15417         x_return_status := FND_API.G_RET_STS_ERROR;
15418 
15419     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
15420         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
15421 
15422     WHEN OTHERS THEN
15423         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
15424         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
15425             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
15426         END IF;
15427 
15428         IF (g_debug_unexp) THEN
15429                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
15430                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
15431                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
15432                        || l_progress || ' SQL CODE is '||sqlcode);
15433                 END IF;
15434         END IF;
15435 
15436 END POPULATE_REQ_HEADERS_GT;
15437 
15438 /**
15439 * Private Procedure: POPULATE_REQ_LINES_GT
15440 * Requires:
15441 *   IN PARAMETERS:
15442 *       p_document_id: Id of submitted document
15443 * Modifies:
15444 * Effects:  Populates the global temp tables po_headers_gt
15445 * Returns:
15446 */
15447 PROCEDURE populate_req_lines_gt(p_document_id IN NUMBER,
15448                        x_return_status OUT NOCOPY VARCHAR2) IS
15449 
15450 l_api_name  CONSTANT varchar2(40) := 'POPULATE_REQ_LINES_GT';
15451 l_progress VARCHAR2(3);
15452 
15453 BEGIN
15454 
15455 l_progress := '000';
15456 IF g_debug_stmt THEN
15457    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
15458      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
15459           || l_progress,'Populate Req Lines');
15460    END IF;
15461 END IF;
15462 
15463     INSERT INTO po_req_lines_gt(
15464          REQUEST_ID                               ,
15465          PROGRAM_APPLICATION_ID                   ,
15466          PROGRAM_ID                               ,
15467          PROGRAM_UPDATE_DATE                      ,
15468          GOVERNMENT_CONTEXT                       ,
15469          CLOSED_REASON                            ,
15470          CLOSED_DATE                              ,
15471          TRANSACTION_REASON_CODE                  ,
15472          QUANTITY_RECEIVED                        ,
15473          SOURCE_REQ_LINE_ID                       ,
15474          ORG_ID                                   ,
15475          CANCEL_REASON                            ,
15476          CLOSED_CODE                              ,
15477          AGENT_RETURN_NOTE                        ,
15478          CHANGED_AFTER_RESEARCH_FLAG              ,
15479          VENDOR_ID                                ,
15480          VENDOR_SITE_ID                           ,
15481          VENDOR_CONTACT_ID                        ,
15482          RESEARCH_AGENT_ID                        ,
15483          ON_LINE_FLAG                             ,
15484          WIP_ENTITY_ID                            ,
15485          WIP_LINE_ID                              ,
15486          WIP_REPETITIVE_SCHEDULE_ID               ,
15487          WIP_OPERATION_SEQ_NUM                    ,
15488          WIP_RESOURCE_SEQ_NUM                     ,
15489          ATTRIBUTE_CATEGORY                       ,
15490          DESTINATION_CONTEXT                      ,
15491          INVENTORY_SOURCE_CONTEXT                 ,
15492          VENDOR_SOURCE_CONTEXT                    ,
15493          ATTRIBUTE1                               ,
15494          ATTRIBUTE2                               ,
15495          ATTRIBUTE3                               ,
15496          ATTRIBUTE4                               ,
15497          ATTRIBUTE5                               ,
15498          ATTRIBUTE6                               ,
15499          ATTRIBUTE7                               ,
15500          ATTRIBUTE8                               ,
15501          ATTRIBUTE9                               ,
15502          ATTRIBUTE10                              ,
15503          ATTRIBUTE11                              ,
15504          ATTRIBUTE12                              ,
15505          ATTRIBUTE13                              ,
15506          ATTRIBUTE14                              ,
15507          ATTRIBUTE15                              ,
15508          BOM_RESOURCE_ID                          ,
15509          PARENT_REQ_LINE_ID                       ,
15510          JUSTIFICATION                            ,
15511          NOTE_TO_AGENT                            ,
15512          NOTE_TO_RECEIVER                         ,
15513          PURCHASING_AGENT_ID                      ,
15514          DOCUMENT_TYPE_CODE                       ,
15515          BLANKET_PO_HEADER_ID                     ,
15516          BLANKET_PO_LINE_NUM                      ,
15517          CURRENCY_CODE                            ,
15518          RATE_TYPE                                ,
15519          RATE_DATE                                ,
15520          RATE                                     ,
15521          CURRENCY_UNIT_PRICE                      ,
15522          SUGGESTED_VENDOR_NAME                    ,
15523          SUGGESTED_VENDOR_LOCATION                ,
15524          SUGGESTED_VENDOR_CONTACT                 ,
15525          SUGGESTED_VENDOR_PHONE                   ,
15526          SUGGESTED_VENDOR_PRODUCT_CODE            ,
15527          UN_NUMBER_ID                             ,
15528          HAZARD_CLASS_ID                          ,
15529          MUST_USE_SUGG_VENDOR_FLAG                ,
15530          REFERENCE_NUM                            ,
15531          ON_RFQ_FLAG                              ,
15532          URGENT_FLAG                              ,
15533          CANCEL_FLAG                              ,
15534          SOURCE_ORGANIZATION_ID                   ,
15535          SOURCE_SUBINVENTORY                      ,
15536          DESTINATION_TYPE_CODE                    ,
15537          DESTINATION_ORGANIZATION_ID              ,
15538          DESTINATION_SUBINVENTORY                 ,
15539          QUANTITY_CANCELLED                       ,
15540          CANCEL_DATE                              ,
15541          REQUISITION_LINE_ID               ,
15542          REQUISITION_HEADER_ID             ,
15543          LINE_NUM                          ,
15544          LINE_TYPE_ID                      ,
15545          CATEGORY_ID                       ,
15546          ITEM_DESCRIPTION                  ,
15547          UNIT_MEAS_LOOKUP_CODE             ,
15548          UNIT_PRICE                        ,
15549          QUANTITY                          ,
15550          AMOUNT                            ,                  -- <SERVICES FPJ>
15551          DELIVER_TO_LOCATION_ID           ,
15552          TO_PERSON_ID                     ,
15553          LAST_UPDATE_DATE                 ,
15554          LAST_UPDATED_BY                   ,
15555          SOURCE_TYPE_CODE                  ,
15556          LAST_UPDATE_LOGIN                        ,
15557          CREATION_DATE                            ,
15558          CREATED_BY                               ,
15559          ITEM_ID                                  ,
15560          ITEM_REVISION                            ,
15561          QUANTITY_DELIVERED                       ,
15562          SUGGESTED_BUYER_ID                       ,
15563          ENCUMBERED_FLAG                          ,
15564          RFQ_REQUIRED_FLAG                        ,
15565          NEED_BY_DATE                             ,
15566          LINE_LOCATION_ID                         ,
15567          MODIFIED_BY_AGENT_FLAG                   ,
15568          KANBAN_CARD_ID                           ,
15569          CATALOG_TYPE                             ,
15570          CATALOG_SOURCE                           ,
15571          MANUFACTURER_ID                          ,
15572          MANUFACTURER_NAME                        ,
15573          MANUFACTURER_PART_NUMBER                 ,
15574          REQUESTER_EMAIL                          ,
15575          REQUESTER_FAX                            ,
15576          REQUESTER_PHONE                          ,
15577          UNSPSC_CODE                              ,
15578          OTHER_CATEGORY_CODE                      ,
15579          SUPPLIER_DUNS                            ,
15580          TAX_STATUS_INDICATOR                     ,
15581          PCARD_FLAG                               ,
15582          NEW_SUPPLIER_FLAG                        ,
15583          AUTO_RECEIVE_FLAG                        ,
15584          TAX_USER_OVERRIDE_FLAG                   ,
15585          TAX_CODE_ID                              ,
15586          NOTE_TO_VENDOR                           ,
15587          OKE_CONTRACT_HEADER_ID                   ,
15588          OKE_CONTRACT_VERSION_ID                  ,
15589          ITEM_SOURCE_ID                           ,
15590          SUPPLIER_REF_NUMBER                      ,
15591          SECONDARY_UNIT_OF_MEASURE                ,
15592          SECONDARY_QUANTITY                       ,
15593          PREFERRED_GRADE                          ,
15594          SECONDARY_QUANTITY_RECEIVED              ,
15595          SECONDARY_QUANTITY_CANCELLED             ,
15596          AUCTION_HEADER_ID                        ,
15597          AUCTION_DISPLAY_NUMBER                   ,
15598          AUCTION_LINE_NUMBER                      ,
15599          REQS_IN_POOL_FLAG                        ,
15600          VMI_FLAG                                 ,
15601          BID_NUMBER                               ,
15602          BID_LINE_NUMBER)
15603     SELECT
15604          REQUEST_ID                               ,
15605          PROGRAM_APPLICATION_ID                   ,
15606          PROGRAM_ID                               ,
15607          PROGRAM_UPDATE_DATE                      ,
15608          GOVERNMENT_CONTEXT                       ,
15609          CLOSED_REASON                            ,
15610          CLOSED_DATE                              ,
15611          TRANSACTION_REASON_CODE                  ,
15612          QUANTITY_RECEIVED                        ,
15613          SOURCE_REQ_LINE_ID                       ,
15614          ORG_ID                                   ,
15615          CANCEL_REASON                            ,
15616          CLOSED_CODE                              ,
15617          AGENT_RETURN_NOTE                        ,
15618          CHANGED_AFTER_RESEARCH_FLAG              ,
15619          VENDOR_ID                                ,
15620          VENDOR_SITE_ID                           ,
15621          VENDOR_CONTACT_ID                        ,
15622          RESEARCH_AGENT_ID                        ,
15623          ON_LINE_FLAG                             ,
15624          WIP_ENTITY_ID                            ,
15625          WIP_LINE_ID                              ,
15626          WIP_REPETITIVE_SCHEDULE_ID               ,
15627          WIP_OPERATION_SEQ_NUM                    ,
15628          WIP_RESOURCE_SEQ_NUM                     ,
15629          ATTRIBUTE_CATEGORY                       ,
15630          DESTINATION_CONTEXT                      ,
15631          INVENTORY_SOURCE_CONTEXT                 ,
15632          VENDOR_SOURCE_CONTEXT                    ,
15633          ATTRIBUTE1                               ,
15634          ATTRIBUTE2                               ,
15635          ATTRIBUTE3                               ,
15636          ATTRIBUTE4                               ,
15637          ATTRIBUTE5                               ,
15638          ATTRIBUTE6                               ,
15639          ATTRIBUTE7                               ,
15640          ATTRIBUTE8                               ,
15641          ATTRIBUTE9                               ,
15642          ATTRIBUTE10                              ,
15643          ATTRIBUTE11                              ,
15644          ATTRIBUTE12                              ,
15645          ATTRIBUTE13                              ,
15646          ATTRIBUTE14                              ,
15647          ATTRIBUTE15                              ,
15648          BOM_RESOURCE_ID                          ,
15649          PARENT_REQ_LINE_ID                       ,
15650          JUSTIFICATION                            ,
15651          NOTE_TO_AGENT                            ,
15652          NOTE_TO_RECEIVER                         ,
15653          PURCHASING_AGENT_ID                      ,
15654          DOCUMENT_TYPE_CODE                       ,
15655          BLANKET_PO_HEADER_ID                     ,
15656          BLANKET_PO_LINE_NUM                      ,
15657          CURRENCY_CODE                            ,
15658          RATE_TYPE                                ,
15659          RATE_DATE                                ,
15660          RATE                                     ,
15661          CURRENCY_UNIT_PRICE                      ,
15662          SUGGESTED_VENDOR_NAME                    ,
15663          SUGGESTED_VENDOR_LOCATION                ,
15664          SUGGESTED_VENDOR_CONTACT                 ,
15665          SUGGESTED_VENDOR_PHONE                   ,
15666          SUGGESTED_VENDOR_PRODUCT_CODE            ,
15667          UN_NUMBER_ID                             ,
15668          HAZARD_CLASS_ID                          ,
15669          MUST_USE_SUGG_VENDOR_FLAG                ,
15670          REFERENCE_NUM                            ,
15671          ON_RFQ_FLAG                              ,
15672          URGENT_FLAG                              ,
15673          CANCEL_FLAG                              ,
15674          SOURCE_ORGANIZATION_ID                   ,
15675          SOURCE_SUBINVENTORY                      ,
15676          DESTINATION_TYPE_CODE                    ,
15677          DESTINATION_ORGANIZATION_ID              ,
15678          DESTINATION_SUBINVENTORY                 ,
15679          QUANTITY_CANCELLED                       ,
15680          CANCEL_DATE                              ,
15681          REQUISITION_LINE_ID               ,
15682          REQUISITION_HEADER_ID             ,
15683          LINE_NUM                          ,
15684          LINE_TYPE_ID                      ,
15685          CATEGORY_ID                       ,
15686          ITEM_DESCRIPTION                  ,
15687          UNIT_MEAS_LOOKUP_CODE             ,
15688          UNIT_PRICE                        ,
15689          QUANTITY                          ,
15690          AMOUNT                            ,                  -- <SERVICES FPJ>
15691          DELIVER_TO_LOCATION_ID           ,
15692          TO_PERSON_ID                     ,
15693          LAST_UPDATE_DATE                 ,
15694          LAST_UPDATED_BY                   ,
15695          SOURCE_TYPE_CODE                  ,
15696          LAST_UPDATE_LOGIN                        ,
15697          CREATION_DATE                            ,
15698          CREATED_BY                               ,
15699          ITEM_ID                                  ,
15700          ITEM_REVISION                            ,
15701          QUANTITY_DELIVERED                       ,
15702          SUGGESTED_BUYER_ID                       ,
15703          ENCUMBERED_FLAG                          ,
15704          RFQ_REQUIRED_FLAG                        ,
15705          NEED_BY_DATE                             ,
15706          LINE_LOCATION_ID                         ,
15707          MODIFIED_BY_AGENT_FLAG                   ,
15708          KANBAN_CARD_ID                           ,
15709          CATALOG_TYPE                             ,
15710          CATALOG_SOURCE                           ,
15711          MANUFACTURER_ID                          ,
15712          MANUFACTURER_NAME                        ,
15713          MANUFACTURER_PART_NUMBER                 ,
15714          REQUESTER_EMAIL                          ,
15715          REQUESTER_FAX                            ,
15716          REQUESTER_PHONE                          ,
15717          UNSPSC_CODE                              ,
15718          OTHER_CATEGORY_CODE                      ,
15719          SUPPLIER_DUNS                            ,
15720          TAX_STATUS_INDICATOR                     ,
15721          PCARD_FLAG                               ,
15722          NEW_SUPPLIER_FLAG                        ,
15723          AUTO_RECEIVE_FLAG                        ,
15724          TAX_USER_OVERRIDE_FLAG                   ,
15725          TAX_CODE_ID                              ,
15726          NOTE_TO_VENDOR                           ,
15727          OKE_CONTRACT_HEADER_ID                   ,
15728          OKE_CONTRACT_VERSION_ID                  ,
15729          ITEM_SOURCE_ID                           ,
15730          SUPPLIER_REF_NUMBER                      ,
15731          SECONDARY_UNIT_OF_MEASURE                ,
15732          SECONDARY_QUANTITY                       ,
15733          PREFERRED_GRADE                          ,
15734          SECONDARY_QUANTITY_RECEIVED              ,
15735          SECONDARY_QUANTITY_CANCELLED             ,
15736          AUCTION_HEADER_ID                        ,
15737          AUCTION_DISPLAY_NUMBER                   ,
15738          AUCTION_LINE_NUMBER                      ,
15739          REQS_IN_POOL_FLAG                        ,
15740          VMI_FLAG                                 ,
15741          BID_NUMBER                               ,
15742          BID_LINE_NUMBER
15743     FROM po_requisition_lines
15744     WHERE requisition_header_id = p_document_id;
15745 
15746 l_progress := '001';
15747     x_return_status := FND_API.G_RET_STS_SUCCESS;
15748 EXCEPTION
15749     WHEN FND_API.G_EXC_ERROR THEN
15750         x_return_status := FND_API.G_RET_STS_ERROR;
15751 
15752     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
15753         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
15754 
15755     WHEN OTHERS THEN
15756         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
15757         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
15758             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
15759         END IF;
15760 
15761         IF (g_debug_unexp) THEN
15762                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
15763                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
15764                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
15765                        || l_progress || ' SQL CODE is '||sqlcode);
15766                 END IF;
15767         END IF;
15768 
15769 END POPULATE_REQ_LINES_GT;
15770 
15771 /**
15772 * Private Procedure: POPULATE_REQ_DISTRIBUTIONS_GT
15773 * Requires:
15774 *   IN PARAMETERS:
15775 *       p_document_id: Id of submitted document
15776 * Modifies:
15777 * Effects:  Populates the global temp tables po_headers_gt
15778 * Returns:
15779 */
15780 PROCEDURE populate_req_distributions_gt(
15781    p_document_id                    IN             NUMBER
15782 )
15783 IS
15784 
15785 l_api_name  CONSTANT varchar2(40) := 'POPULATE_REQ_DISTRIBUTIONS_GT';
15786 l_progress VARCHAR2(3);
15787 
15788 BEGIN
15789 
15790 l_progress := '000';
15791 IF g_debug_stmt THEN
15792    IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
15793      FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
15794           || l_progress,'Populate Req Distributions');
15795    END IF;
15796 END IF;
15797 
15798     INSERT INTO po_req_distributions_gt(
15799          DISTRIBUTION_ID                   ,
15800          LAST_UPDATE_DATE                  ,
15801          LAST_UPDATED_BY                   ,
15802          REQUISITION_LINE_ID               ,
15803          SET_OF_BOOKS_ID                   ,
15804          CODE_COMBINATION_ID               ,
15805          REQ_LINE_QUANTITY                 ,
15806          REQ_LINE_AMOUNT                   ,                  -- <SERVICES FPJ>
15807          LAST_UPDATE_LOGIN                        ,
15808          CREATION_DATE                            ,
15809          CREATED_BY                               ,
15810          ENCUMBERED_FLAG                          ,
15811          GL_ENCUMBERED_DATE                       ,
15812          GL_ENCUMBERED_PERIOD_NAME                ,
15813          GL_CANCELLED_DATE                        ,
15814          FAILED_FUNDS_LOOKUP_CODE                 ,
15815          ENCUMBERED_AMOUNT                        ,
15816          BUDGET_ACCOUNT_ID                        ,
15817          ACCRUAL_ACCOUNT_ID                       ,
15818          ORG_ID                                   ,
15819          VARIANCE_ACCOUNT_ID                      ,
15820          PREVENT_ENCUMBRANCE_FLAG                 ,
15821          ATTRIBUTE_CATEGORY                       ,
15822          ATTRIBUTE1                               ,
15823          ATTRIBUTE2                               ,
15824          ATTRIBUTE3                               ,
15825          ATTRIBUTE4                               ,
15826          ATTRIBUTE5                               ,
15827          ATTRIBUTE6                               ,
15828          ATTRIBUTE7                               ,
15829          ATTRIBUTE8                               ,
15830          ATTRIBUTE9                               ,
15831          ATTRIBUTE10                              ,
15832          ATTRIBUTE11                              ,
15833          ATTRIBUTE12                              ,
15834          ATTRIBUTE13                              ,
15835          ATTRIBUTE14                              ,
15836          ATTRIBUTE15                              ,
15837          GOVERNMENT_CONTEXT                       ,
15838          REQUEST_ID                               ,
15839          PROGRAM_APPLICATION_ID                   ,
15840          PROGRAM_ID                               ,
15841          PROGRAM_UPDATE_DATE                      ,
15842          PROJECT_ID                               ,
15843          TASK_ID                                  ,
15844          EXPENDITURE_TYPE                         ,
15845          PROJECT_ACCOUNTING_CONTEXT               ,
15846          EXPENDITURE_ORGANIZATION_ID              ,
15847          GL_CLOSED_DATE                           ,
15848          SOURCE_REQ_DISTRIBUTION_ID               ,
15849          DISTRIBUTION_NUM                         ,
15850          PROJECT_RELATED_FLAG                     ,
15851          EXPENDITURE_ITEM_DATE                    ,
15852          ALLOCATION_TYPE                          ,
15853          ALLOCATION_VALUE                         ,
15854          AWARD_ID                                 ,
15855          END_ITEM_UNIT_NUMBER                     ,
15856          RECOVERABLE_TAX                          ,
15857          NONRECOVERABLE_TAX                       ,
15858          RECOVERY_RATE                            ,
15859          TAX_RECOVERY_OVERRIDE_FLAG               ,
15860          OKE_CONTRACT_LINE_ID                     ,
15861          OKE_CONTRACT_DELIVERABLE_ID)
15862     SELECT
15863          PRD.DISTRIBUTION_ID                   ,
15864          PRD.LAST_UPDATE_DATE                  ,
15865          PRD.LAST_UPDATED_BY                   ,
15866          PRD.REQUISITION_LINE_ID               ,
15867          PRD.SET_OF_BOOKS_ID                   ,
15868          PRD.CODE_COMBINATION_ID               ,
15869          PRD.REQ_LINE_QUANTITY                 ,
15870          PRD.REQ_LINE_AMOUNT                   ,              -- <SERVICES FPJ>
15871          PRD.LAST_UPDATE_LOGIN                        ,
15872          PRD.CREATION_DATE                            ,
15873          PRD.CREATED_BY                               ,
15874          PRD.ENCUMBERED_FLAG                          ,
15875          PRD.GL_ENCUMBERED_DATE                       ,
15876          PRD.GL_ENCUMBERED_PERIOD_NAME                ,
15877          PRD.GL_CANCELLED_DATE                        ,
15878          PRD.FAILED_FUNDS_LOOKUP_CODE                 ,
15879          PRD.ENCUMBERED_AMOUNT                        ,
15880          PRD.BUDGET_ACCOUNT_ID                        ,
15881          PRD.ACCRUAL_ACCOUNT_ID                       ,
15882          PRD.ORG_ID                                   ,
15883          PRD.VARIANCE_ACCOUNT_ID                      ,
15884          PRD.PREVENT_ENCUMBRANCE_FLAG                 ,
15885          PRD.ATTRIBUTE_CATEGORY                       ,
15886          PRD.ATTRIBUTE1                               ,
15887          PRD.ATTRIBUTE2                               ,
15888          PRD.ATTRIBUTE3                               ,
15889          PRD.ATTRIBUTE4                               ,
15890          PRD.ATTRIBUTE5                               ,
15891          PRD.ATTRIBUTE6                               ,
15892          PRD.ATTRIBUTE7                               ,
15893          PRD.ATTRIBUTE8                               ,
15894          PRD.ATTRIBUTE9                               ,
15895          PRD.ATTRIBUTE10                              ,
15896          PRD.ATTRIBUTE11                              ,
15897          PRD.ATTRIBUTE12                              ,
15898          PRD.ATTRIBUTE13                              ,
15899          PRD.ATTRIBUTE14                              ,
15900          PRD.ATTRIBUTE15                              ,
15901          PRD.GOVERNMENT_CONTEXT                       ,
15902          PRD.REQUEST_ID                               ,
15903          PRD.PROGRAM_APPLICATION_ID                   ,
15904          PRD.PROGRAM_ID                               ,
15905          PRD.PROGRAM_UPDATE_DATE                      ,
15906          PRD.PROJECT_ID                               ,
15907          PRD.TASK_ID                                  ,
15908          PRD.EXPENDITURE_TYPE                         ,
15909          PRD.PROJECT_ACCOUNTING_CONTEXT               ,
15910          PRD.EXPENDITURE_ORGANIZATION_ID              ,
15911          PRD.GL_CLOSED_DATE                           ,
15912          PRD.SOURCE_REQ_DISTRIBUTION_ID               ,
15913          PRD.DISTRIBUTION_NUM                         ,
15914          PRD.PROJECT_RELATED_FLAG                     ,
15915          PRD.EXPENDITURE_ITEM_DATE                    ,
15916          PRD.ALLOCATION_TYPE                          ,
15917          PRD.ALLOCATION_VALUE                         ,
15918          PRD.AWARD_ID                                 ,
15919          PRD.END_ITEM_UNIT_NUMBER                     ,
15920          PRD.RECOVERABLE_TAX                          ,
15921          PRD.NONRECOVERABLE_TAX                       ,
15922          PRD.RECOVERY_RATE                            ,
15923          PRD.TAX_RECOVERY_OVERRIDE_FLAG               ,
15924          PRD.OKE_CONTRACT_LINE_ID                     ,
15925          PRD.OKE_CONTRACT_DELIVERABLE_ID
15926     FROM po_req_distributions PRD, po_requisition_lines PRL
15927     WHERE PRD.requisition_line_id = PRL.requisition_line_id AND
15928           PRL.requisition_header_id = p_document_id;
15929 
15930 l_progress := '001';
15931 
15932 END POPULATE_REQ_DISTRIBUTIONS_GT;
15933 
15934 -- <FPJ, Refactor Security API START>
15935 /**
15936 * Public Procedure: PO_Security_Check
15937 * Requires:
15938 *   IN PARAMETERS:
15939 *     p_api_version:          Version number of API that caller expects. It
15940 *                             should match the l_api_version defined in the
15941 *                             procedure
15942 *     p_query_table:          Table you want to check
15943 *     p_owner_id_column:      Owner id column of the table
15944 *     p_employee_id:          User id to access the document
15945 *     p_minimum_access_level: Minimum access level to the document
15946 *     p_document_type:        The type of the document to perform
15947 *                             the security check on
15948 *     p_document_subtype:     The subtype of the document.
15949 *     p_type_clause:          The document type clause to be used in
15950 *                             constructing where clause
15951 *
15952 * Modifies: None
15953 * Effects:  This procedure builds dynamic WHERE clause fragments based on
15954 *           document security parameters.
15955 * Returns:
15956 *   x_return_status: FND_API.G_RET_STS_SUCCESS if API succeeds
15957 *                    FND_API.G_RET_STS_ERROR if API fails
15958 *                    FND_API.G_RET_STS_UNEXP_ERROR if unexpected error
15959 *   x_msg_data:      Contains error msg in case x_return_status returned
15960 *                    FND_API.G_RET_STS_ERROR or
15961 *                    FND_API.G_RET_STS_UNEXP_ERROR
15962 *   x_where_clause:  The constructed where clause
15963 */
15964 
15965 PROCEDURE PO_SECURITY_CHECK (p_api_version          IN NUMBER,
15966                              p_query_table          IN VARCHAR2,
15967                              p_owner_id_column      IN VARCHAR2,
15968                              p_employee_id          IN VARCHAR2,
15969                              p_minimum_access_level IN VARCHAR2,
15970                              p_document_type        IN VARCHAR2,
15971                              p_document_subtype     IN VARCHAR2,
15972                              p_type_clause          IN VARCHAR2,
15973                              x_return_status        OUT NOCOPY VARCHAR2,
15974                              x_msg_data             OUT NOCOPY VARCHAR2,
15975                              x_where_clause         OUT NOCOPY VARCHAR2)
15976 IS
15977 
15978   l_api_name    CONSTANT varchar2(30) := 'PO_SECURITY_CHECK';
15979   l_api_version   CONSTANT NUMBER       := 1.0;
15980   l_progress    VARCHAR2(3);
15981   l_access_level  PO_DOCUMENT_TYPES.access_level_code%TYPE;
15982   l_security_level  PO_DOCUMENT_TYPES.security_level_code%TYPE;
15983   l_security_hierarchy  NUMBER;
15984   l_id_column           varchar2(30);
15985   l_doctype_column      varchar2(30); /*Bug 7229262/7239696*/
15986 
15987 BEGIN
15988 
15989   l_progress := '000';
15990   -- Standard call to check for call compatibility
15991   IF NOT FND_API.Compatible_API_Call(l_api_version, p_api_version, l_api_name, G_PKG_NAME)
15992   THEN
15993     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
15994   END IF;
15995 
15996   l_progress := '010';
15997   IF g_debug_stmt THEN
15998      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
15999        FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
16000             || l_progress, 'deciding security level');
16001      END IF;
16002   END IF;
16003 
16004    l_progress := '020';
16005 
16006   -- Bug 3663057
16007   -- Get the id column based on the document type being queried
16008   -- Bug 5054685: Performance issue: Added 'PO_PA' as another parameter to collapse
16009   -- multiple security related where-clauses into one.
16010   -- Bug 4082310: Handling the missed case of QUOTATION and RFQ.
16011     -- l_id_column is used to construct the where clause of the pre-query
16012     -- in join condition against po_action_history table. This fix
16013     -- is just to make the where clause valid even though QUOTATION
16014     -- and RFQ are not logged in po_action_history table. Avoiding
16015     -- adding logic to omit the condition in these cases as it will
16016     -- unnecessarily complicate the code.
16017 
16018    /* Bug 7229262/7239696 */
16019    l_doctype_column := 'TYPE_LOOKUP_CODE';
16020    /* End Bug 7229262/7239696 */
16021   IF p_document_type in ('PO','PA', 'PO_PA','QUOTATION','RFQ') THEN
16022      l_id_column := 'PO_HEADER_ID';
16023   ELSIF p_document_type = 'RELEASE' THEN
16024      l_id_column := 'PO_RELEASE_ID';
16025 	 l_doctype_column := 'DOCUMENT_TYPE';   -- Bug 9311634
16026   ELSIF p_document_type = 'REQUISITION' THEN
16027      l_id_column := 'REQUISITION_HEADER_ID';
16028   ELSE
16029     l_id_column := 'PO_HEADER_ID';
16030   END IF;
16031   -- <R12 MOAC start>
16032   IF (p_query_table = 'PO_WF_NOTIFICATIONS_V' ) THEN
16033     l_id_column := 'OBJECT_ID' ;
16034 	l_doctype_column := 'DOC_TYPE'; /*Bug 7229262/7239696*/
16035   END IF;
16036   -- <R12 MOAC end>
16037 
16038 
16039   l_progress := '060';
16040   IF g_debug_stmt THEN
16041      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
16042        FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
16043             || l_progress, 'deciding WHERE clause');
16044      END IF;
16045   END IF;
16046 
16047    -- Construct WHERE clause with type_clause
16048   x_where_clause := '(NOT(' || p_type_clause || ') OR (';
16049 
16050   /*Bug6640107 Removed the check for the access level here. First we check for all the security levels and
16051     then finally AND with the access level check
16052     For security level of private only the owner should be able to view the document on the enter PO form
16053     irrespective of the access level*/
16054 
16055   x_where_clause := x_where_clause ||
16056                    '(('||p_query_table||'.security_level_code = ''PUBLIC'' )';
16057 
16058   x_where_clause := x_where_clause ||
16059                     ' OR '||'('||p_query_table || '.security_level_code = ''PRIVATE'' AND '||
16060          p_employee_id || '=' || p_owner_id_column ||')';
16061 	/* OR (' ||
16062         'EXISTS (SELECT ''Y'' FROM PO_ACTION_HISTORY POAH2 ' ||
16063         'WHERE POAH2.employee_id = ' || p_employee_id ||
16064         ' AND POAH2.object_type_code = (DECODE(' || p_query_table || '.type_lookup_code, ''BLANKET'', ''PA'', ''STANDARD'', ''PO'' , ''PLANNED'' , ''PO'' , ''CONTRACT'' , ''PA'', ''RELEASE'' , ''RELEASE'' ) )' ||
16065         ' AND POAH2.object_id = ' || p_query_table || '.' || l_id_column || '))))';*/
16066 
16067   x_where_clause := x_where_clause ||
16068                    ' OR '||'('||p_query_table || '.security_level_code = ''HIERARCHY'' AND '||
16069        '((' || p_employee_id || '=' || p_owner_id_column ||') OR (' ||
16070        'EXISTS (SELECT ''Y'' FROM PO_ACTION_HISTORY POAH2 ' ||
16071        'WHERE POAH2.employee_id =' || p_employee_id ||
16072 	   /* Bug 7229262/7239696
16073 	   ' AND POAH2.object_type_code = (DECODE(' || p_query_table || '.type_lookup_code, ''BLANKET'', ''PA'', ''STANDARD'', ''PO'' , ''PLANNED'' , ''PO'' , ''CONTRACT'' , ''PA'', ''RELEASE'' , ''RELEASE'' ) ) ' ||
16074 	   */
16075 	   ' AND POAH2.object_type_code = (DECODE(' || p_query_table || '.'||l_doctype_column||', ''BLANKET'', ''PA'', ''STANDARD'', ''PO'' , ''PLANNED'' , ''PO'' , ''CONTRACT'' , ''PA'', ''RELEASE'' , ''RELEASE'' ) )' ||
16076 		/* End Bug 7229262/7239696 */
16077 
16078        ' AND POAH2.object_id = ' || p_query_table || '.' || l_id_column || ')) OR (' ||
16079        p_employee_id || ' IN (SELECT H.superior_id ' ||
16080        ' FROM PO_EMPLOYEE_HIERARCHIES H, PO_SYSTEM_PARAMETERS PSP WHERE H.employee_id = ' ||
16081        p_query_table || '.' || p_owner_id_column ||
16082        ' AND H.position_structure_id = NVL(PSP.SECURITY_POSITION_STRUCTURE_ID,-1) '
16083        ||'AND PSP.ORG_ID = '||p_query_table || '.ORG_ID '
16084        ||'))))';
16085 
16086   x_where_clause := x_where_clause ||
16087                    ' OR '||'('||p_query_table || '.security_level_code = ''PURCHASING''  AND '||
16088        '((' || p_employee_id || '=' || p_owner_id_column ||') OR (' ||
16089        'EXISTS (SELECT ''Y'' FROM PO_ACTION_HISTORY POAH2 ' ||
16090        'WHERE POAH2.employee_id =' || p_employee_id ||
16091 	   /* Bug 7229262/7239696
16092 	   ' AND POAH2.object_type_code = (DECODE(' || p_query_table || '.type_lookup_code, ''BLANKET'', ''PA'', ''STANDARD'', ''PO'' , ''PLANNED'' , ''PO'' , ''CONTRACT'' , ''PA'', ''RELEASE'' , ''RELEASE'' ) ) ' ||
16093 	   */
16094        ' AND POAH2.object_type_code = (DECODE(' || p_query_table || '.'||l_doctype_column||', ''BLANKET'', ''PA'', ''STANDARD'', ''PO'' , ''PLANNED'' , ''PO'' , ''CONTRACT'' , ''PA'', ''RELEASE'' , ''RELEASE'' ) ) ' ||
16095 		/* End Bug 7229262/7239696 */
16096        ' AND POAH2.object_id = ' || p_query_table || '.' || l_id_column || ')) OR (' ||
16097        'EXISTS(SELECT NULL FROM PO_AGENTS WHERE agent_id= ' ||
16098        p_employee_id || ' AND SYSDATE BETWEEN NVL(start_date_active, ' ||
16099        'SYSDATE) AND NVL(end_date_active, SYSDATE+1))))' ||')'
16100        ||')';
16101 /*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.
16102 The following allows us to either view the document or to open and mofify it depending on the document access level*/
16103 
16104   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 ( '''
16105   || p_minimum_access_level||''' = ''FULL'' AND '|| p_query_table||'.access_level_code = ''FULL'' ) OR ( '||p_employee_id ||' = '|| p_owner_id_column ||'))))';
16106 
16107 
16108 
16109   l_progress := '070';
16110   x_return_status := FND_API.G_RET_STS_SUCCESS;
16111   x_msg_data := NULL;
16112 
16113   l_progress := '100';
16114   IF g_debug_stmt THEN
16115     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
16116       FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
16117                    || l_progress,'Returning from PVT package: ' ||
16118                    x_where_clause);
16119     END IF;
16120   END IF;
16121 
16122 EXCEPTION
16123   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
16124     x_msg_data := FND_MSG_PUB.GET(p_msg_index => FND_MSG_PUB.G_LAST,
16125                                   p_encoded => 'F');
16126     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
16127     x_where_clause := NULL;
16128   WHEN FND_API.G_EXC_ERROR THEN
16129     x_msg_data := FND_MSG_PUB.GET(p_msg_index => FND_MSG_PUB.G_LAST,
16130                                   p_encoded => 'F');
16131     x_return_status := FND_API.G_RET_STS_ERROR;
16132     x_where_clause := NULL;
16133   WHEN OTHERS THEN
16134     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
16135       FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
16136     END IF;
16137 
16138     IF (g_debug_unexp) THEN
16139       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
16140         FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
16141                      l_api_name || '.others_exception', 'EXCEPTION: Location is '
16142                      || l_progress || ' SQL CODE is '||sqlcode);
16143       END IF;
16144     END IF;
16145 
16146     x_msg_data := FND_MSG_PUB.GET(p_msg_index => FND_MSG_PUB.G_LAST,
16147                                   p_encoded => 'F');
16148     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
16149     x_where_clause := NULL;
16150 
16151 END PO_SECURITY_CHECK;
16152 
16153 -- <FPJ Refactor Security API END>
16154 
16155 -- The following new procedures for status check added in DropShip FPJ project
16156 
16157 -------------------------------------------------------------------------------
16158 --Start of Comments
16159 --Name: check_updatable
16160 --Pre-reqs:
16161 --  None.
16162 --Modifies:
16163 --  None.
16164 --Locks:
16165 --  None.
16166 --Function:
16167 --  Finds if a PurchaseOrder/Release, or Line/Shipment is updatable based on status.
16168 --  A Header or Release has to be specified. Line/Shipment are optional.
16169 --  A Purchase Order/Release is updatable if
16170 --    it is not Pre Approved, not In Process, not canceled, not finally closed, not frozen.
16171 --  A Line or Shipment is updatable if it is not canceled, not finally closed.
16172 --Parameters:
16173 --IN:
16174 --p_count
16175 --  Specifies the number of entities in table IN parameters like p_header_id, p_release_id
16176 --  Other IN parameters are detailed in main procedure po_status_check
16177 --OUT:
16178 --x_return_status
16179 --  Indicates API return status as 'S', 'E' or 'U'.
16180 --x_po_status_rec
16181 --  Table x_po_status_rec.updateable_flag will be 'Y' or 'N' for each input entity
16182 --Notes:
16183 --  The implementation of updatable_flag involves a fake "update dual" statement to
16184 --    optimize performance.
16185 --End of Comments
16186 -------------------------------------------------------------------------------
16187 
16188 PROCEDURE check_updatable (
16189     p_count               IN NUMBER,
16190     p_header_id           IN PO_TBL_NUMBER,
16191     p_release_id          IN PO_TBL_NUMBER,
16192     p_document_type       IN PO_TBL_VARCHAR30,
16193     p_document_subtype    IN PO_TBL_VARCHAR30,
16194     p_document_num        IN PO_TBL_VARCHAR30,
16195     p_vendor_order_num    IN PO_TBL_VARCHAR30,
16196     p_line_id             IN PO_TBL_NUMBER,
16197     p_line_location_id    IN PO_TBL_NUMBER,
16198     p_distribution_id     IN PO_TBL_NUMBER,
16199     p_lock_flag           IN VARCHAR2 := 'N',
16200     p_calling_module      IN VARCHAR2 := NULL,  -- PDOI Rewrite R12
16201     p_role                IN VARCHAR2 := NULL,  -- PDOI Rewrite R12
16202     p_skip_cat_upload_chk IN VARCHAR2 := NULL,  -- PDOI Rewrite R12
16203     x_po_status_rec       IN OUT NOCOPY PO_STATUS_REC_TYPE,
16204     x_return_status       IN OUT NOCOPY VARCHAR2
16205 ) IS
16206 
16207 l_api_name       CONSTANT VARCHAR(30) := 'CHECK_UPDATABLE';
16208 l_progress       VARCHAR2(3) := '000';
16209 l_document_id    PO_HEADERS.po_header_id%TYPE;
16210 l_document_type  PO_DOCUMENT_TYPES.DOCUMENT_TYPE_CODE%TYPE;
16211 
16212 -- bug3592160 START
16213 l_header_id   PO_TBL_NUMBER;
16214 l_procedure_id PO_SESSION_GT.key%TYPE;
16215 -- bug3592160 END
16216 
16217 -- <PDOI Rewrite R12 START>
16218 l_role PO_DRAFTS.owner_role%TYPE := NVL(p_role, PO_GLOBAL.g_ROLE_BUYER);
16219 l_skip_cat_upload_chk VARCHAR2(1) := NVL(p_skip_cat_upload_chk, FND_API.G_FALSE);
16220 
16221 l_update_allowed VARCHAR2(1);
16222 l_locking_applicable VARCHAR2(1);
16223 l_unlock_required VARCHAR2(1);
16224 l_message VARCHAR2(30);
16225 -- <PDOI Rewrite R12 END>
16226 BEGIN
16227 
16228 IF g_debug_stmt THEN
16229     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
16230       FND_LOG.string(FND_LOG.LEVEL_STATEMENT, g_log_head || '.'||l_api_name||'.'
16231           || l_progress, 'Entering Procedure '||l_api_name);
16232     END IF;
16233 END IF;
16234 
16235 --To obtimize performance, Execute a fake "update dual" in BULK. The WHERE clause
16236 -- of the fake update statement checks if the current entity is updatable or not.
16237 -- One dual row updated <==> where clause is true <==> current entity is updatable.
16238 -- Later, Examine BULK_ROWCOUNT in a loop to determine updatable_flag
16239 l_progress := '010';
16240 
16241 -- bug3592160 START
16242 
16243 -- For some records, po_header_id needs to be derived (e.g. document_num is
16244 -- passed in instead of po_header_id). The following the procedure is to
16245 -- fill out po_header_ids, if missing
16246 complete_po_header_id_tbl
16247 ( p_count            => p_count,
16248   p_header_id        => p_header_id,
16249   p_release_id       => p_release_id,
16250   p_vendor_order_num => p_vendor_order_num,
16251   p_document_num     => p_document_num,
16252   p_type_lookup_code => p_document_subtype,
16253   x_header_id        => l_header_id
16254 );
16255 
16256 l_progress := '015';
16257 
16258 -- The original approach was to do a fake UPDATE on DUAL table. However, this
16259 -- is causing locking issue. Therefore, BULK INSERT is used instead of
16260 -- BULK UPDATE
16261 l_procedure_id := PO_CORE_S.get_session_gt_nextval;
16262 
16263 FORALL i IN 1..p_count
16264   INSERT INTO PO_SESSION_GT
16265   ( key,
16266     num1
16267   )
16268   SELECT l_procedure_id,
16269          1                 -- Dummy Value
16270   FROM DUAL
16271   WHERE  (p_release_id(i) IS NOT NULL
16272       --Case 1: No Release is specified, PO Header has to be specified
16273       --   Through any of HeaderId, DocNum-and-SubType, or VendorOrderNum
16274       OR EXISTS (select null from po_headers_all h
16275         WHERE h.po_header_id = l_header_id(i)
16276         AND (h.authorization_status is NULL
16277              OR h.authorization_status NOT IN ('PRE-APPROVED', 'IN PROCESS'))
16278         AND (h.cancel_flag is null or h.cancel_flag <> 'Y')
16279         AND (h.closed_code is NULL or h.closed_code NOT IN ('FINALLY CLOSED'))
16280         AND (h.frozen_flag is NULL or h.frozen_flag <> 'Y')))
16281     AND (p_release_id(i) IS NULL
16282       --Case 2: PO Release is specified
16283       OR EXISTS (select null from po_releases_all h
16284         WHERE h.po_release_id = p_release_id(i)
16285         AND (h.authorization_status is NULL
16286              OR h.authorization_status NOT IN ('PRE-APPROVED', 'IN PROCESS'))
16287         AND (h.cancel_flag is null or h.cancel_flag <> 'Y')
16288         AND (h.closed_code is NULL or h.closed_code NOT IN ('FINALLY CLOSED'))
16289         AND (h.frozen_flag is NULL or h.frozen_flag <> 'Y')))
16290     AND (p_line_id(i) IS NULL
16291       --Case 3: Optionally, Line is specified
16292       OR EXISTS (SELECT null from po_lines_all l
16293         WHERE l.po_line_id = p_line_id(i)
16294         AND (l.cancel_flag is null or l.cancel_flag <> 'Y')
16295         AND (l.closed_code is NULL or l.closed_code NOT IN ('FINALLY CLOSED'))))
16296     AND (p_line_location_id(i) IS NULL
16297       --Case 4: Optionally, Line Location is specified
16298       OR EXISTS (SELECT null from po_line_locations_all l
16299         WHERE l.line_location_id = p_line_location_id(i)
16300         AND (l.cancel_flag is null or l.cancel_flag <> 'Y')
16301         AND (l.closed_code is NULL or l.closed_code NOT IN ('FINALLY CLOSED'))))    ;
16302 -- bug3592160 END
16303 
16304 -- Allocate memory for updatable_flag Table to p_count size
16305 l_progress := '020';
16306 x_po_status_rec.updatable_flag := po_tbl_varchar1();
16307 x_po_status_rec.updatable_flag.extend(p_count);
16308 
16309 -- Set Updatable_flag for each Entity using BULK_ROWCOUNT
16310 l_progress := '030';
16311 FOR i IN 1..p_count LOOP
16312 
16313     IF SQL%BULK_ROWCOUNT(i) > 0 THEN
16314         -- Updateable Header/Line/Shipment found in the fake "update dual" stmt
16315         x_po_status_rec.updatable_flag(i) := 'Y';
16316 
16317         -- This document is updatable, lock the document if p_lock_flag=Y
16318         l_progress := '040';
16319         IF p_lock_flag = 'Y' THEN
16320             IF p_release_id(i) is not null THEN
16321                 l_document_id := p_release_id(i);
16322                 l_document_type := 'RELEASE';
16323             ELSE
16324                 l_document_id := p_header_id(i);
16325                 IF p_document_type(i) is null THEN
16326                     l_document_type := 'PO';
16327                 ELSE
16328                     l_document_type := p_document_type(i);
16329                 END IF;
16330             END IF;
16331             PO_DOCUMENT_LOCK_GRP.LOCK_DOCUMENT (
16332                 p_api_version => 1.0,
16333                 P_INIT_MSG_LIST => FND_API.G_FALSE,
16334                 P_DOCUMENT_TYPE => l_document_type,
16335                 P_DOCUMENT_ID => l_document_id,
16336                 x_return_status  => x_return_status);
16337 
16338             IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
16339                 return;
16340             END IF;
16341         END IF;
16342     ELSE
16343         x_po_status_rec.updatable_flag(i) := 'N';
16344     END IF; --END of IF SQL%BULK_ROWCOUNT(i) > 0
16345 
16346 END LOOP;
16347 
16348 -- bug3592160 START
16349 -- Remove everything that has been inserted into PO_SESSION_GT
16350 DELETE FROM po_session_gt
16351 WHERE key = l_procedure_id;
16352 -- bug3592160 END
16353 
16354 -- <PDOI Rewrite R12 START>
16355 -- For all the documents being checked, we also need to make sure that
16356 -- current draft status allows the document to be updated
16357 
16358 FOR i IN 1..p_count LOOP
16359   l_progress := 40;
16360   IF (x_po_status_rec.updatable_flag(i) = 'Y' AND
16361       p_release_id(i) IS NULL) THEN
16362 
16363     PO_DRAFTS_PVT.update_permission_check
16364     ( p_calling_module => p_calling_module,
16365       p_po_header_id => p_header_id(i),
16366       p_role => l_role,
16367       p_skip_cat_upload_chk => l_skip_cat_upload_chk,
16368       x_update_allowed => l_update_allowed,
16369       x_locking_applicable => l_locking_applicable,
16370       x_unlock_required => l_unlock_required,
16371       x_message => l_message
16372     );
16373 
16374     IF (l_update_allowed = FND_API.G_FALSE) THEN
16375       x_po_status_rec.updatable_flag(i) := 'N';
16376     END IF;
16377   END IF;
16378 END LOOP;
16379 
16380 -- <PDOI Rewrite R12 END>
16381 
16382 
16383 x_return_status := FND_API.G_RET_STS_SUCCESS;
16384 
16385 EXCEPTION
16386     WHEN FND_API.G_EXC_ERROR THEN
16387         x_return_status := FND_API.G_RET_STS_ERROR;
16388     WHEN OTHERS THEN
16389         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
16390         FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name || '.' || l_progress);
16391 
16392 END check_updatable;
16393 
16394 -------------------------------------------------------------------------------
16395 --Start of Comments
16396 --Name: check_reservable
16397 --Pre-reqs:
16398 --  None.
16399 --Modifies:
16400 --  None.
16401 --Locks:
16402 --  None.
16403 --Function:
16404 --  Finds if a PurchaseOrder/Release, or Line/Shipment is reservable based on status.
16405 --  A Header or Release has to be specified. Line/Shipment are optional.
16406 --  A Purchase Order/Release Header/Line/Shipment is reservable if
16407 --    Authorization Status not APPROVED, AND Closed Code is CLOSED or OPEN,
16408 --    AND Frozen Flag is N, AND User Hold Flag is N
16409 --Parameters:
16410 --IN:
16411 --p_count
16412 --  Specifies the number of entities in table IN parameters like p_header_id, p_release_id
16413 --  Other IN parameters are detailed in main procedure po_status_check
16414 --OUT:
16415 --x_return_status
16416 --  Indicates API return status as 'S', 'E' or 'U'.
16417 --x_po_status_rec
16418 --  Table x_po_status_rec.updateable_flag will be 'Y' or 'N' for each input entity
16419 --Notes:
16420 --  The implementation of reservable_flag involves a fake "update dual" statement to
16421 --    optimize performance.
16422 --End of Comments
16423 -------------------------------------------------------------------------------
16424 
16425 PROCEDURE check_reservable (
16426     p_count               IN NUMBER,
16427     p_header_id           IN PO_TBL_NUMBER,
16428     p_release_id          IN PO_TBL_NUMBER,
16429     p_document_type       IN PO_TBL_VARCHAR30,
16430     p_document_subtype    IN PO_TBL_VARCHAR30,
16431     p_document_num        IN PO_TBL_VARCHAR30,
16432     p_vendor_order_num    IN PO_TBL_VARCHAR30,
16433     p_line_id             IN PO_TBL_NUMBER,
16434     p_line_location_id    IN PO_TBL_NUMBER,
16435     p_distribution_id     IN PO_TBL_NUMBER,
16436     p_lock_flag           IN VARCHAR2 := 'N',
16437     x_po_status_rec       IN OUT NOCOPY PO_STATUS_REC_TYPE,
16438     x_return_status       IN OUT NOCOPY VARCHAR2
16439 ) IS
16440 
16441 l_api_name       CONSTANT VARCHAR(30) := 'check_reservable';
16442 l_progress       VARCHAR2(3) := '000';
16443 l_document_id    PO_HEADERS.po_header_id%TYPE;
16444 l_document_type  PO_DOCUMENT_TYPES.DOCUMENT_TYPE_CODE%TYPE;
16445 
16446 -- bug3592160 START
16447 l_header_id   PO_TBL_NUMBER;
16448 l_procedure_id PO_SESSION_GT.key%TYPE;
16449 -- bug3592160 END
16450 
16451 BEGIN
16452 
16453 IF g_debug_stmt THEN
16454     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
16455       FND_LOG.string(FND_LOG.LEVEL_STATEMENT, g_log_head || '.'||l_api_name||'.'
16456           || l_progress, 'Entering Procedure '||l_api_name);
16457     END IF;
16458 END IF;
16459 
16460 --To obtimize performance, Execute a fake "update dual" in BULK. The WHERE clause
16461 -- of the fake update statement checks if the current entity is reservable or not.
16462 -- One dual row updated <==> where clause is true <==> current entity is reservable.
16463 -- Later, Examine BULK_ROWCOUNT in a loop to determine reservable_flag
16464 l_progress := '010';
16465 
16466 -- bug3592160 START
16467 
16468 -- For some records, po_header_id needs to be derived (e.g. document_num is
16469 -- passed in instead of po_header_id). The following the procedure is to
16470 -- fill out po_header_ids, if missing
16471 complete_po_header_id_tbl
16472 ( p_count            => p_count,
16473   p_header_id        => p_header_id,
16474   p_release_id       => p_release_id,
16475   p_vendor_order_num => p_vendor_order_num,
16476   p_document_num     => p_document_num,
16477   p_type_lookup_code => p_document_subtype,
16478   x_header_id        => l_header_id
16479 );
16480 
16481 l_progress := '015';
16482 
16483 
16484 -- The original approach was to do a fake UPDATE on DUAL table. However, this
16485 -- is causing locking issue. Therefore, BULK INSERT is used instead of
16486 -- BULK UPDATE
16487 l_procedure_id := PO_CORE_S.get_session_gt_nextval;
16488 
16489 FORALL i IN 1..p_count
16490   INSERT INTO PO_SESSION_GT
16491   ( key,
16492     num1
16493   )
16494   SELECT l_procedure_id,
16495          1                 -- Dummy Value
16496   FROM DUAL
16497   WHERE (p_release_id(i) IS NOT NULL
16498       --Case 1: No Release is specified, PO Header has to be specified
16499       --   Through any of HeaderId, DocNum-and-SubType, or VendorOrderNum
16500       -- Bug 3407980: Modified closed_code condition so that it only discards 'FINALLY CLOSED'
16501       OR EXISTS (select null from po_headers_all h
16502         WHERE h.po_header_id = l_header_id(i)
16503         AND (h.authorization_status is NULL
16504              OR h.authorization_status NOT IN ('APPROVED')
16505              /* <<CLM Partial Funding Code Changes>> */
16506              -- for Mod Reserve Action
16507              OR (po_partial_funding_pkg.is_clm_document('PO',h.po_header_id) = 'Y' )
16508              /* <<CLM Partial Funding Code Changes>> */
16509              )
16510         AND (h.closed_code is NULL or h.closed_code <> 'FINALLY CLOSED')
16511         AND (h.frozen_flag is NULL or h.frozen_flag <> 'Y')
16512         AND (h.user_hold_flag is NULL or h.user_hold_flag <> 'Y')))
16513     AND (p_release_id(i) IS NULL
16514       --Case 2: PO Release is specified
16515       -- Bug 3407980: Modified closed_code condition so that it only discards 'FINALLY CLOSED'
16516       OR EXISTS (select null from po_releases_all h
16517         WHERE h.po_release_id = p_release_id(i)
16518         AND (h.authorization_status is NULL
16519              OR h.authorization_status NOT IN ('APPROVED'))
16520         AND (h.closed_code is NULL or h.closed_code <>  'FINALLY CLOSED')
16521         AND (h.frozen_flag is NULL or h.frozen_flag <> 'Y')
16522         AND (h.hold_flag is NULL or h.hold_flag <> 'Y')))
16523     AND (p_line_id(i) IS NULL
16524       --Case 3: Optionally, Line is specified
16525       -- Bug 3407980: Modified closed_code condition so that it only discards 'FINALLY CLOSED'
16526       OR EXISTS (SELECT null from po_lines_all l
16527         WHERE l.po_line_id = p_line_id(i)
16528         AND (l.closed_code is NULL or l.closed_code <> 'FINALLY CLOSED')))
16529     AND (p_line_location_id(i) IS NULL
16530       --Case 4: Optionally, Line Location is specified
16531       -- Bug 3407980: Modified closed_code condition so that it only discards 'FINALLY CLOSED'
16532       OR EXISTS (SELECT null from po_line_locations_all l
16533         WHERE l.line_location_id = p_line_location_id(i)
16534         AND (l.closed_code is NULL or l.closed_code <> 'FINALLY CLOSED')))
16535     ;
16536 
16537 -- Allocate memory for reservable_flag Table to p_count size
16538 l_progress := '020';
16539 x_po_status_rec.reservable_flag := po_tbl_varchar1();
16540 x_po_status_rec.reservable_flag.extend(p_count);
16541 
16542 -- Set reservable_flag for each Entity using BULK_ROWCOUNT
16543 l_progress := '030';
16544 FOR i IN 1..p_count LOOP
16545 
16546     IF SQL%BULK_ROWCOUNT(i) > 0 THEN
16547         -- Reservable Header/Line/Shipment found in the fake "update dual" stmt
16548         x_po_status_rec.reservable_flag(i) := 'Y';
16549     ELSE
16550         x_po_status_rec.reservable_flag(i) := 'N';
16551     END IF; --END of IF SQL%BULK_ROWCOUNT(i) > 0
16552 
16553 END LOOP;
16554 
16555 -- bug3592160 START
16556 -- Remove everything that has been inserted into PO_SESSION_GT
16557 DELETE FROM po_session_gt
16558 WHERE key = l_procedure_id;
16559 -- bug3592160 END
16560 
16561 x_return_status := FND_API.G_RET_STS_SUCCESS;
16562 
16563 EXCEPTION
16564     WHEN FND_API.G_EXC_ERROR THEN
16565         x_return_status := FND_API.G_RET_STS_ERROR;
16566     WHEN OTHERS THEN
16567         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
16568         FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name || '.' || l_progress);
16569 
16570 END check_reservable;
16571 
16572 -------------------------------------------------------------------------------
16573 --Start of Comments
16574 --Name: check_unreservable
16575 --Pre-reqs:
16576 --  None.
16577 --Modifies:
16578 --  None.
16579 --Locks:
16580 --  None.
16581 --Function:
16582 --  Finds if a PurchaseOrder/Release, or Line/Shipment is unreservable based on status.
16583 --  A Header or Release has to be specified. Line/Shipment are optional.
16584 --  A Purchase Order/Release Header/Line/Shipment is unreservable if
16585 --    Any Authorization Status, Closed Code is CLOSED or OPEN,
16586 --    AND Frozen Flag is N, AND User Hold Flag is N
16587 --Parameters:
16588 --IN:
16589 --p_count
16590 --  Specifies the number of entities in table IN parameters like p_header_id, p_release_id
16591 --  Other IN parameters are detailed in main procedure po_status_check
16592 --OUT:
16593 --x_return_status
16594 --  Indicates API return status as 'S', 'E' or 'U'.
16595 --x_po_status_rec
16596 --  Table x_po_status_rec.updateable_flag will be 'Y' or 'N' for each input entity
16597 --Notes:
16598 --  The implementation of unreservable_flag involves a fake "update dual" statement to
16599 --    optimize performance.
16600 --End of Comments
16601 -------------------------------------------------------------------------------
16602 
16603 PROCEDURE check_unreservable (
16604     p_count               IN NUMBER,
16605     p_header_id           IN PO_TBL_NUMBER,
16606     p_release_id          IN PO_TBL_NUMBER,
16607     p_document_type       IN PO_TBL_VARCHAR30,
16608     p_document_subtype    IN PO_TBL_VARCHAR30,
16609     p_document_num        IN PO_TBL_VARCHAR30,
16610     p_vendor_order_num    IN PO_TBL_VARCHAR30,
16611     p_line_id             IN PO_TBL_NUMBER,
16612     p_line_location_id    IN PO_TBL_NUMBER,
16613     p_distribution_id     IN PO_TBL_NUMBER,
16614     p_lock_flag           IN VARCHAR2 := 'N',
16615     x_po_status_rec       IN OUT NOCOPY PO_STATUS_REC_TYPE,
16616     x_return_status       IN OUT NOCOPY VARCHAR2
16617 ) IS
16618 
16619 l_api_name       CONSTANT VARCHAR(30) := 'check_unreservable';
16620 l_progress       VARCHAR2(3) := '000';
16621 l_document_id    PO_HEADERS.po_header_id%TYPE;
16622 l_document_type  PO_DOCUMENT_TYPES.DOCUMENT_TYPE_CODE%TYPE;
16623 
16624 -- bug3592160 START
16625 l_header_id   PO_TBL_NUMBER;
16626 l_procedure_id PO_SESSION_GT.key%TYPE;
16627 -- bug3592160 END
16628 
16629 BEGIN
16630 
16631 IF g_debug_stmt THEN
16632     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
16633       FND_LOG.string(FND_LOG.LEVEL_STATEMENT, g_log_head || '.'||l_api_name||'.'
16634           || l_progress, 'Entering Procedure '||l_api_name);
16635     END IF;
16636 END IF;
16637 
16638 --To obtimize performance, Execute a fake "update dual" in BULK. The WHERE clause
16639 -- of the fake update statement checks if the current entity is unreservable or not.
16640 -- One dual row updated <==> where clause is true <==> current entity is unreservable.
16641 -- Later, Examine BULK_ROWCOUNT in a loop to determine unreservable_flag
16642 l_progress := '010';
16643 
16644 -- bug3592160 START
16645 
16646 -- For some records, po_header_id needs to be derived (e.g. document_num is
16647 -- passed in instead of po_header_id). The following the procedure is to
16648 -- fill out po_header_ids, if missing
16649 complete_po_header_id_tbl
16650 ( p_count            => p_count,
16651   p_header_id        => p_header_id,
16652   p_release_id       => p_release_id,
16653   p_vendor_order_num => p_vendor_order_num,
16654   p_document_num     => p_document_num,
16655   p_type_lookup_code => p_document_subtype,
16656   x_header_id        => l_header_id
16657 );
16658 
16659 l_progress := '015';
16660 
16661 -- SQL What: Checks if current PO Header/Line/Shipment is in unreservable status
16662 -- The original approach was to do a fake UPDATE on DUAL table. However, this
16663 -- is causing locking issue. Therefore, BULK INSERT is used instead of
16664 -- BULK UPDATE
16665 l_procedure_id := PO_CORE_S.get_session_gt_nextval;
16666 
16667 FORALL i IN 1..p_count
16668   INSERT INTO PO_SESSION_GT
16669   ( key,
16670     num1
16671   )
16672   SELECT l_procedure_id,
16673          1                 -- Dummy Value
16674   FROM DUAL
16675   WHERE (p_release_id(i) IS NOT NULL
16676       --Case 1: No Release is specified, PO Header has to be specified
16677       --   Through any of HeaderId, DocNum-and-SubType, or VendorOrderNum
16678       -- Bug 3407980: Modified closed_code condition so that it only discards 'FINALLY CLOSED'
16679       OR EXISTS (select null from po_headers_all h
16680         WHERE h.po_header_id = l_header_id(i)
16681         AND (h.closed_code is NULL or h.closed_code <> 'FINALLY CLOSED')
16682         AND (h.frozen_flag is NULL or h.frozen_flag <> 'Y')
16683         AND (h.user_hold_flag is NULL or h.user_hold_flag <> 'Y')))
16684     AND (p_release_id(i) IS NULL
16685       --Case 2: PO Release is specified
16686       -- Bug 3407980: Modified closed_code condition so that it only discards 'FINALLY CLOSED'
16687       OR EXISTS (select null from po_releases_all h
16688         WHERE h.po_release_id = p_release_id(i)
16689         AND (h.closed_code is NULL or h.closed_code <> 'FINALLY CLOSED')
16690         AND (h.frozen_flag is NULL or h.frozen_flag <> 'Y')
16691         AND (h.hold_flag is NULL or h.hold_flag <> 'Y')))
16692     AND (p_line_id(i) IS NULL
16693       --Case 3: Optionally, Line is specified
16694       -- Bug 3407980: Modified closed_code condition so that it only discards 'FINALLY CLOSED'
16695       OR EXISTS (SELECT null from po_lines_all l
16696         WHERE l.po_line_id = p_line_id(i)
16697         AND (l.closed_code is NULL or l.closed_code <> 'FINALLY CLOSED')))
16698     AND (p_line_location_id(i) IS NULL
16699       --Case 4: Optionally, Line Location is specified
16700       -- Bug 3407980: Modified closed_code condition so that it only discards 'FINALLY CLOSED'
16701       OR EXISTS (SELECT null from po_line_locations_all l
16702         WHERE l.line_location_id = p_line_location_id(i)
16703         AND (l.closed_code is NULL or l.closed_code <> 'FINALLY CLOSED')))
16704     ;
16705 
16706 -- Allocate memory for unreservable_flag Table to p_count size
16707 l_progress := '020';
16708 x_po_status_rec.unreservable_flag := po_tbl_varchar1();
16709 x_po_status_rec.unreservable_flag.extend(p_count);
16710 
16711 -- Set unreservable_flag for each Entity using BULK_ROWCOUNT
16712 l_progress := '030';
16713 FOR i IN 1..p_count LOOP
16714 
16715     IF SQL%BULK_ROWCOUNT(i) > 0 THEN
16716         -- Unreservable Header/Line/Shipment found in the fake "update dual" stmt
16717         x_po_status_rec.unreservable_flag(i) := 'Y';
16718     ELSE
16719         x_po_status_rec.unreservable_flag(i) := 'N';
16720     END IF; --END of IF SQL%BULK_ROWCOUNT(i) > 0
16721 
16722 END LOOP;
16723 
16724 -- bug3592160 START
16725 -- Remove everything that has been inserted into PO_SESSION_GT
16726 DELETE FROM po_session_gt
16727 WHERE key = l_procedure_id;
16728 -- bug3592160 END
16729 
16730 x_return_status := FND_API.G_RET_STS_SUCCESS;
16731 
16732 EXCEPTION
16733     WHEN FND_API.G_EXC_ERROR THEN
16734         x_return_status := FND_API.G_RET_STS_ERROR;
16735     WHEN OTHERS THEN
16736         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
16737         FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name || '.' || l_progress);
16738 
16739 END check_unreservable;
16740 
16741 -------------------------------------------------------------------------------
16742 --Start of Comments
16743 --Name: get_status
16744 --Pre-reqs:
16745 --  None.
16746 --Modifies:
16747 --  None.
16748 --Locks:
16749 --  None.
16750 --Function:
16751 --  Helper to po_status_check to find status of a Purchase Order/Release/Line/Shipment
16752 --  The following status fields of PO Header or Release are put into
16753 --     the OUT parameter x_po_status_rec
16754 --   AUTHORIZATION_STATUS, APPROVED_FLAG, CLOSED_CODE, CANCEL_FLAG, FROZEN_FLAG, HOLD_FLAG
16755 --  When an optional Line specified, following Line level values are overwritten
16756 --   CLOSED_CODE, CANCEL_FLAG, HOLD_FLAG
16757 --  When an optional Shipment specified, following Shipment level values are overwritten
16758 --   APPROVED_FLAG, CLOSED_CODE, CANCEL_FLAG
16759 --Parameters:
16760 --IN:
16761 --p_count
16762 --  Specifies the number of entities in table IN parameters like p_header_id, p_release_id
16763 --    All the table IN parameters are assumed to be of the same size
16764 --  Other IN parameters are detailed in main procedure po_status_check
16765 --OUT:
16766 --x_return_status
16767 --  Indicates API return status as 'S', 'E' or 'U'.
16768 --x_po_status_rec
16769 --  The various status fields would have the PO/Rel Line/Shipment status values
16770 --End of Comments
16771 -------------------------------------------------------------------------------
16772 
16773 PROCEDURE get_status (
16774     p_count               IN NUMBER,
16775     p_header_id           IN PO_TBL_NUMBER,
16776     p_release_id          IN PO_TBL_NUMBER,
16777     p_document_type       IN PO_TBL_VARCHAR30,
16778     p_document_subtype    IN PO_TBL_VARCHAR30,
16779     p_document_num        IN PO_TBL_VARCHAR30,
16780     p_vendor_order_num    IN PO_TBL_VARCHAR30,
16781     p_line_id             IN PO_TBL_NUMBER,
16782     p_line_location_id    IN PO_TBL_NUMBER,
16783     p_distribution_id     IN PO_TBL_NUMBER,
16784     x_po_status_rec       IN OUT NOCOPY PO_STATUS_REC_TYPE,
16785     x_return_status       IN OUT NOCOPY VARCHAR2
16786 ) IS
16787 
16788 l_api_name    CONSTANT VARCHAR(30) := 'GET_STATUS';
16789 l_progress    VARCHAR2(3) := '000';
16790 l_sequence    PO_TBL_NUMBER := PO_TBL_NUMBER();
16791 
16792 BEGIN
16793 
16794 IF g_debug_stmt THEN
16795     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
16796       FND_LOG.string(FND_LOG.LEVEL_STATEMENT, g_log_head || '.'||l_api_name||'.'
16797           || l_progress, 'Entering Procedure '||l_api_name);
16798     END IF;
16799 END IF;
16800 
16801 --Use sequence(i) to simulate i inside FORALL as direct reference to i is not allowed
16802 --Initialize sequence array to contain 1,2,3, ..., p_count
16803 l_progress := '010';
16804 l_sequence.extend(p_count);
16805 FOR i IN 1..p_count LOOP
16806   l_sequence(i) := i;
16807 END LOOP;
16808 
16809 l_progress := '020';
16810 
16811 delete from po_headers_gt;
16812 
16813 -- For all the entities with null p_release_id(i), get Header status fields into
16814 -- global temprary table while storing sequence into po_headers_gt.PO_HEADER_ID column
16815 l_progress := '030';
16816 -- bug 4931241 modified the sql query to avoid FTS on PO_HEADERS_ALL
16817 -- Removed the where clause for document num and vendor_order_num
16818 -- They do not uniquely identify the document. Added validation in group package
16819 FORALL i IN 1..p_count
16820     INSERT
16821       INTO po_headers_gt
16822       ( AGENT_ID, TYPE_LOOKUP_CODE, LAST_UPDATE_DATE, LAST_UPDATED_BY,
16823         SEGMENT1, SUMMARY_FLAG, ENABLED_FLAG,
16824         authorization_status, approved_flag,
16825         closed_code, cancel_flag,
16826         frozen_flag, user_hold_flag, PO_HEADER_ID)
16827     SELECT
16828       AGENT_ID, TYPE_LOOKUP_CODE, LAST_UPDATE_DATE, LAST_UPDATED_BY,
16829       SEGMENT1, SUMMARY_FLAG, ENABLED_FLAG,
16830       NVL(authorization_status, 'INCOMPLETE'), nvl(approved_flag, 'N'),
16831       nvl(closed_code, 'OPEN'), NVL(cancel_flag, 'N'),
16832       NVL(frozen_flag, 'N'), NVL(user_hold_flag, 'N'), l_sequence(i)
16833       FROM po_headers_all h
16834       WHERE p_release_id(i) is null
16835         AND h.po_header_id = p_header_id(i)
16836     ;
16837 
16838 -- For all the entities with non-null p_release_id(i), get Relase status fields into
16839 -- global temprary table while storing sequence into po_headers_gt.PO_HEADER_ID column
16840 l_progress := '035';
16841 FORALL i IN 1..p_count
16842     INSERT
16843       INTO po_headers_gt
16844       ( AGENT_ID, TYPE_LOOKUP_CODE, LAST_UPDATE_DATE, LAST_UPDATED_BY,
16845         SEGMENT1, SUMMARY_FLAG, ENABLED_FLAG,
16846         authorization_status, approved_flag,
16847         closed_code, cancel_flag,
16848         frozen_flag, user_hold_flag, PO_HEADER_ID)
16849     SELECT
16850       0 dummy, ' ' dummy, LAST_UPDATE_DATE, LAST_UPDATED_BY,
16851       ' ' dummy, ' ' dummy, ' ' dummy,
16852       NVL(authorization_status, 'INCOMPLETE'), nvl(approved_flag, 'N'),
16853       nvl(closed_code, 'OPEN'), NVL(cancel_flag, 'N'),
16854       NVL(frozen_flag, 'N'), NVL(hold_flag, 'N'), l_sequence(i)
16855       FROM po_releases_all h
16856       WHERE h.po_release_id = p_release_id(i)
16857     ;
16858 
16859 --IF line ID present at an index, overwrite the status fields with Line Level status
16860 l_progress := '040';
16861 FORALL i IN 1..p_count
16862     UPDATE po_headers_gt gt
16863       SET (closed_code, cancel_flag, user_hold_flag)
16864       =
16865       (SELECT nvl(closed_code, 'OPEN'), NVL(cancel_flag, 'N'), NVL(user_hold_flag, 'N')
16866       FROM po_lines_all s
16867       WHERE s.po_line_id = p_line_id(i))
16868     WHERE p_line_id(i) is not null and gt.po_header_id = l_sequence(i)
16869     ;
16870 
16871 --IF line location present at an index, overwrite status fields with Shipment Level status
16872 l_progress := '050';
16873 FORALL i IN 1..p_count
16874     UPDATE po_headers_gt gt
16875       SET (approved_flag, closed_code, cancel_flag)
16876       =
16877       (SELECT nvl(approved_flag, 'N'), nvl(closed_code, 'OPEN'), NVL(cancel_flag, 'N')
16878       FROM po_line_locations_all s
16879       WHERE s.line_location_id = p_line_location_id(i))
16880     WHERE p_line_location_id(i) is not null and gt.po_header_id = l_sequence(i)
16881     ;
16882 
16883 -- Fetch status fields from global temporary table into pl/sql table.
16884 -- Order by sequence (stored in PO_HEADER_ID column) ensures
16885 --   that input tables like p_header_id are in sync with
16886 --   output status field tables like x_po_status_rec.authorization_status
16887 l_progress := '060';
16888 SELECT
16889   authorization_status, approved_flag, closed_code, cancel_flag, frozen_flag, user_hold_flag
16890 BULK COLLECT INTO
16891   x_po_status_rec.authorization_status, x_po_status_rec.approval_flag, x_po_status_rec.closed_code,
16892   x_po_status_rec.cancel_flag, x_po_status_rec.frozen_flag, x_po_status_rec.hold_flag
16893 FROM po_headers_gt
16894 ORDER BY PO_HEADER_ID;
16895 
16896 x_return_status := FND_API.G_RET_STS_SUCCESS;
16897 
16898 EXCEPTION
16899     WHEN FND_API.G_EXC_ERROR THEN
16900         x_return_status := FND_API.G_RET_STS_ERROR;
16901     WHEN OTHERS THEN
16902         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
16903         FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name || '.' || l_progress);
16904 
16905 END get_status;
16906 
16907 -------------------------------------------------------------------------------
16908 --Start of Comments
16909 --Name: po_status_check
16910 --Pre-reqs:
16911 --  None.
16912 --Modifies:
16913 --  None.
16914 --Locks:
16915 --  None.
16916 --Function:
16917 --  Finds the status of a Purchase Order or a Release. Refer to p_mode parameter
16918 --  and PO_STATUS_REC_TYPE for various status information this procedure can find out.
16919 --  A unique header OR Release has to be specified through various input combinations.
16920 --  A line or shipment can optionally be specified to check status at that level also.
16921 --Parameters:
16922 --IN:
16923 --p_api_version
16924 --  Specifies API version.
16925 --p_header_id
16926 --  Specifies Purchase Order Header ID. This is ignored if p_release_id is not NULL
16927 --p_release_id
16928 --  Specifies Purchase Order Release ID.
16929 --p_document_type
16930 --  Specifies the document type: PA, PO, or RELEASE
16931 --p_document_subtype
16932 --  Specifies the document subtype.
16933 --p_document_num
16934 --  Document Number and Document Type together identify a unique document
16935 --p_vendor_order_num
16936 --  Document Vendor Order Number uniquely identifies a document
16937 --p_line_id
16938 --  Optionally Specifies Purchase Order Line ID to check status at line level
16939 --p_line_location_id := NULL
16940 --  Optionally Specifies Purchase Order Shipment ID to check status at shipment level
16941 --p_distribution_id := NULL
16942 --  Specifies Purchase Order Distribution ID, currently not used. May be used in future.
16943 --p_mode
16944 --  Indicates what status to check.
16945 --    Can contain one or more of the following requests to check status
16946 --      CHECK_UPDATEABLE to check if the current PO Header/Line/Shipment is updatable
16947 --      GET_STATUS to return various statuses of the current PO Header/Release
16948 --p_calling_module
16949 --  To be used by updatalbe chk only.
16950 --  String specifying where this API is calling from
16951 --p_role
16952 --  To be used by updatalbe chk only.
16953 --  Role of the suer calling this API. (BUYER, SUPPLIER, CAT ADMIN, etc.)
16954 --p_skip_cat_upload_chk
16955 --  To be used by updatalbe chk only.
16956 --  FND_API.G_TRUE if catalog upload status check should not be performed
16957 --  FND_API.G_FALSE otherwise
16958 --OUT:
16959 --x_return_status
16960 --  Indicates API return status as 'S', 'E' or 'U'.
16961 --x_po_status_rec
16962 --  Contains the returned status elements
16963 --  If p_mode contains CHECK_UPDATEABLE,
16964 --    the updateable_flag would have 'Y' or 'N' for each entity in the Table
16965 --  If p_mode contains GET_APPROVAL_STATUS,
16966 --    the various status fields Header/Release/Line/Shipment status values
16967 --Testing:
16968 --  All the input table parameters should have the exact same length.
16969 --    They may have null values at some indexes, but need to identify an entity uniquely
16970 --  Call the API when only Requisition Exist, PO/Release Exist
16971 --    and for all the combinations of attributes.
16972 --End of Comments
16973 -------------------------------------------------------------------------------
16974 
16975 PROCEDURE po_status_check (
16976     p_api_version         IN NUMBER,
16977     p_header_id           IN PO_TBL_NUMBER,
16978     p_release_id          IN PO_TBL_NUMBER,
16979     p_document_type       IN PO_TBL_VARCHAR30,
16980     p_document_subtype    IN PO_TBL_VARCHAR30,
16981     p_document_num        IN PO_TBL_VARCHAR30,
16982     p_vendor_order_num    IN PO_TBL_VARCHAR30,
16983     p_line_id             IN PO_TBL_NUMBER,
16984     p_line_location_id    IN PO_TBL_NUMBER,
16985     p_distribution_id     IN PO_TBL_NUMBER,
16986     p_mode                IN VARCHAR2,
16987     p_lock_flag           IN VARCHAR2 := 'N',
16988     p_calling_module      IN VARCHAR2 := NULL,  -- PDOI Rewrite R12
16989     p_role                IN VARCHAR2 := NULL,  -- PDOI Rewrite R12
16990     p_skip_cat_upload_chk IN VARCHAR2 := NULL,  -- PDOI Rewrite R12
16991     x_po_status_rec       OUT NOCOPY PO_STATUS_REC_TYPE,
16992     x_return_status       OUT NOCOPY VARCHAR2
16993 ) IS
16994 
16995 l_api_name    CONSTANT VARCHAR(30) := 'PO_STATUS_CHECK';
16996 l_api_version CONSTANT NUMBER := 1.0;
16997 l_progress    VARCHAR2(3) := '000';
16998 l_count       NUMBER;
16999 
17000 BEGIN
17001 
17002 IF g_debug_stmt THEN
17003     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
17004       FND_LOG.string(FND_LOG.LEVEL_STATEMENT, g_log_head || '.'||l_api_name||'.'
17005           || l_progress, 'Entering Procedure '||l_api_name);
17006     END IF;
17007 END IF;
17008 
17009 -- Standard call to check for call compatibility
17010 l_progress := '010';
17011 IF NOT FND_API.Compatible_API_Call(l_api_version, p_api_version, l_api_name, G_PKG_NAME) THEN
17012     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
17013 END IF;
17014 
17015 l_progress := '020'; -- Initialize Local/Output Variables
17016 l_count := p_header_id.count;
17017 x_po_status_rec := PO_STATUS_REC_TYPE(null, null, null, null, null, null, null, null, null);
17018 
17019 l_progress := '030';
17020 
17021 IF INSTR(p_mode, G_CHECK_UPDATEABLE) > 0 THEN --{
17022 
17023     check_updatable (
17024         p_count => l_count,
17025         p_header_id => p_header_id,
17026         p_release_id => p_release_id,
17027         p_document_type => p_document_type,
17028         p_document_subtype => p_document_subtype,
17029         p_document_num => p_document_num,
17030         p_vendor_order_num => p_vendor_order_num,
17031         p_line_id => p_line_id,
17032         p_line_location_id => p_line_location_id,
17033         p_distribution_id => p_distribution_id,
17034         p_lock_flag => p_lock_flag,
17035         p_calling_module   => p_calling_module,          -- PDOI Rewrite R12
17036         p_role             => p_role,                    -- PDOI Rewrite R12
17037         p_skip_cat_upload_chk => p_skip_cat_upload_chk,  -- PDOI Rewrite R12
17038         x_po_status_rec => x_po_status_rec,
17039         x_return_status  => x_return_status);
17040 
17041     IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
17042         return;
17043     END IF;
17044 
17045 END IF; --}END of IF INSTR(p_mode, G_CHECK_UPDATEABLE) > 0
17046 
17047 l_progress := '033';
17048 
17049 IF INSTR(p_mode, G_CHECK_RESERVABLE) > 0 THEN --{
17050 
17051     check_reservable (
17052         p_count => l_count,
17053         p_header_id => p_header_id,
17054         p_release_id => p_release_id,
17055         p_document_type => p_document_type,
17056         p_document_subtype => p_document_subtype,
17057         p_document_num => p_document_num,
17058         p_vendor_order_num => p_vendor_order_num,
17059         p_line_id => p_line_id,
17060         p_line_location_id => p_line_location_id,
17061         p_distribution_id => p_distribution_id,
17062         p_lock_flag => p_lock_flag,
17063         x_po_status_rec => x_po_status_rec,
17064         x_return_status  => x_return_status);
17065 
17066     IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
17067         return;
17068     END IF;
17069 
17070 END IF; --}END of IF INSTR(p_mode, G_CHECK_RESERVABLE) > 0
17071 
17072 l_progress := '036';
17073 
17074 IF INSTR(p_mode, G_CHECK_UNRESERVABLE) > 0 THEN --{
17075 
17076     check_unreservable (
17077         p_count => l_count,
17078         p_header_id => p_header_id,
17079         p_release_id => p_release_id,
17080         p_document_type => p_document_type,
17081         p_document_subtype => p_document_subtype,
17082         p_document_num => p_document_num,
17083         p_vendor_order_num => p_vendor_order_num,
17084         p_line_id => p_line_id,
17085         p_line_location_id => p_line_location_id,
17086         p_distribution_id => p_distribution_id,
17087         p_lock_flag => p_lock_flag,
17088         x_po_status_rec => x_po_status_rec,
17089         x_return_status  => x_return_status);
17090 
17091     IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
17092         return;
17093     END IF;
17094 
17095 END IF; --}END of IF INSTR(p_mode, G_CHECK_UNRESERVABLE) > 0
17096 
17097 l_progress := '040';
17098 
17099 IF INSTR(p_mode, G_GET_STATUS) > 0 THEN --{ Get Header/Release status fields
17100 
17101     get_status (
17102         p_count => l_count,
17103         p_header_id => p_header_id,
17104         p_release_id => p_release_id,
17105         p_document_type => p_document_type,
17106         p_document_subtype => p_document_subtype,
17107         p_document_num => p_document_num,
17108         p_vendor_order_num => p_vendor_order_num,
17109         p_line_id => p_line_id,
17110         p_line_location_id => p_line_location_id,
17111         p_distribution_id => p_distribution_id,
17112         x_po_status_rec => x_po_status_rec,
17113         x_return_status  => x_return_status);
17114 
17115     IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
17116         return;
17117     END IF;
17118 
17119 END IF; --}END of IF INSTR(p_mode, G_GET_STATUS) > 0
17120 
17121 l_progress := '050';
17122 
17123 IF x_return_status is null THEN -- no valid check status request specified
17124     FND_MESSAGE.set_name('PO', 'PO_STATCHK_GENERAL_ERROR');
17125     FND_MESSAGE.set_token('ERROR_TEXT', 'No Valid p_mode specified !');
17126     FND_MSG_PUB.Add;
17127     RAISE FND_API.G_EXC_ERROR;
17128 END IF;
17129 
17130 EXCEPTION
17131     WHEN FND_API.G_EXC_ERROR THEN
17132         x_return_status := FND_API.G_RET_STS_ERROR;
17133     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
17134         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
17135     WHEN OTHERS THEN
17136         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
17137         FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name || '.' || l_progress);
17138 
17139 END po_status_check;
17140 
17141 
17142 -------------------------------------------------------------------------------
17143 --Start of Comments
17144 --Name: check_unreserve
17145 --Pre-reqs:
17146 --  The global temp tables are populated with exactly those rows
17147 --  that should undergo the checks.
17148 --Modifies:
17149 --  PO_ONLINE_REPORT_TEXT_GT
17150 --Locks:
17151 --  None.
17152 --Function:
17153 --  This procedure performs the document checks for an UNRESERVE action.
17154 --Parameters:
17155 --IN:
17156 --p_online_report_id
17157 --  ID used to insert into PO_ONLINE_REPORT_TEXT_GT.
17158 --p_user_id
17159 --  User performing the action.
17160 --p_login_id
17161 --  Last update login_id.
17162 --IN OUT:
17163 --p_sequence
17164 --  Contains the running count of error messages inserted.
17165 --Testing:
17166 --
17167 --End of Comments
17168 -------------------------------------------------------------------------------
17169 PROCEDURE check_unreserve(
17170    p_online_report_id               IN             NUMBER
17171 ,  p_document_type                  IN             VARCHAR2 --Bug#5462677
17172 ,  p_document_subtype               IN             VARCHAR2 --Bug#5462677
17173 ,  p_document_level                 IN             VARCHAR2 --Bug#5462677
17174 ,  p_doc_level_id                   IN             NUMBER   --Bug#5462677
17175 ,  p_user_id                        IN             NUMBER
17176 ,  p_login_id                       IN             NUMBER
17177 ,  p_sequence                       IN OUT NOCOPY  NUMBER
17178 )
17179 IS
17180 
17181 l_log_head     CONSTANT VARCHAR2(100) := g_log_head||'CHECK_UNRESERVE';
17182 l_progress     VARCHAR2(3) := '000';
17183 
17184 l_textline  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE := NULL;
17185 l_ret_sts VARCHAR2(1);
17186 BEGIN
17187 
17188 IF g_debug_stmt THEN
17189    PO_DEBUG.debug_begin(l_log_head);
17190    PO_DEBUG.debug_var(l_log_head,l_progress,'p_online_report_id',p_online_report_id);
17191    PO_DEBUG.debug_var(l_log_head,l_progress,'p_user_id',p_user_id);
17192    PO_DEBUG.debug_var(l_log_head,l_progress,'p_login_id',p_login_id);
17193    PO_DEBUG.debug_var(l_log_head,l_progress,'p_sequence',p_sequence);
17194    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level_id',p_doc_level_id);
17195    PO_DEBUG.debug_var(l_log_head,l_progress,'p_document_type',p_document_type);
17196 END IF;
17197 
17198 l_progress := '010';
17199 
17200 IF g_debug_stmt THEN
17201    PO_DEBUG.debug_stmt(l_log_head,l_progress,
17202       'UNRESERVE 1: Ship Qty billed greater than qty ordered');
17203 END IF;
17204 
17205 -- Check 1: Unreserve action not allowed if shipment quantity/amount billed is
17206 -- greater than quantity/amount ordered
17207 -- Bug# 1286701
17208 
17209 l_progress := '040';
17210 
17211 -- Perform the check.
17212 
17213 INSERT INTO PO_ONLINE_REPORT_TEXT_GT
17214 (  online_report_id
17215 ,  last_update_login
17216 ,  last_updated_by
17217 ,  last_update_date
17218 ,  created_by
17219 ,  creation_date
17220 ,  line_num
17221 ,  shipment_num
17222 ,  distribution_num
17223 ,  sequence
17224 ,  text_line
17225 ,  message_name
17226 )
17227 SELECT
17228    p_online_report_id
17229 ,  p_login_id
17230 ,  p_user_id
17231 ,  sysdate
17232 ,  p_user_id
17233 ,  sysdate
17234 ,  POL.line_num
17235 ,  POLL.shipment_num
17236 ,  0
17237 ,  p_sequence + ROWNUM
17238 ,  decode ( POL.quantity                                      -- <SERVICES FPJ>
17239           , NULL , PO_CORE_S.get_translated_text
17240                    (   'PO_SUB_SHIP_BILL_GT_ORD_AMT'
17241                    ,   'LINE_NUM' , POL.line_num
17242                    ,   'SHIP_NUM' , POLL.shipment_num
17243                    ,   'AMT_BILLED' , nvl(POLL.amount_billed, 0)
17244                    ,   'AMT_ORDERED' , nvl(POLL.amount, 0)
17245                    )
17246                  , PO_CORE_S.get_translated_text
17247                    (   'PO_SUB_SHIP_BILL_GT_ORD_QTY'
17248                    ,   'LINE_NUM' , POL.line_num
17249                    ,   'SHIP_NUM' , POLL.shipment_num
17250                    ,   'QTY_BILLED' , nvl(POLL.quantity_billed, 0)
17251                    ,   'QTY_ORDERED' , nvl(POLL.quantity, 0)
17252                    )
17253           )
17254 ,  decode ( POL.quantity                                      -- <SERVICES FPJ>
17255           , NULL , 'PO_SUB_SHIP_BILL_GT_ORD_AMT'
17256                  , 'PO_SUB_SHIP_BILL_GT_ORD_QTY'
17257           )
17258 FROM
17259    PO_LINE_LOCATIONS_GT POLL
17260 ,  PO_LINES_ALL POL  -- For releases, PO_LINES_GT table isn't populated.
17261 WHERE POLL.po_line_id = POL.po_line_id
17262 AND   nvl(POLL.cancel_flag, 'N') = 'N'
17263 AND   nvl(POLL.closed_code, 'OPEN') <> 'FINALLY CLOSED'
17264 AND   (                                                       -- <SERVICES FPJ>
17265           --<Complex Work R12 START>: Use POLL value basis
17266           (   ( POLL.value_basis IN ('QUANTITY', 'AMOUNT') )
17267           AND (nvl(POLL.quantity_billed,0) > nvl(POLL.quantity,0) ) )
17268       OR  (   ( POLL.value_basis IN ('FIXED PRICE', 'RATE') )
17269           AND (nvl(POLL.amount_billed,0) > nvl(POLL.amount,0) ) )
17270           --<Complex Work R12 END>
17271       );
17272 
17273 l_progress := '050';
17274 
17275 --Increment the p_sequence with number of errors reported in last query
17276 p_sequence := p_sequence + SQL%ROWCOUNT;
17277 
17278 IF g_debug_stmt THEN
17279    PO_DEBUG.debug_var(l_log_head,l_progress,'p_sequence',p_sequence);
17280 END IF;
17281 
17282 --------------------------------------------------------
17283 
17284 l_progress := '100';
17285 
17286 IF g_debug_stmt THEN
17287    PO_DEBUG.debug_stmt(l_log_head,l_progress,
17288       'UNRESERVE 2: Dist qty billed greater than qty ordered');
17289 END IF;
17290 
17291 -- Check 2: Unreserve action not allowed if distribution quantity/amount billed
17292 -- is greater than quantity/amount ordered
17293 
17294 l_progress := '110';
17295 
17296 -- Perform the check.
17297 
17298 INSERT INTO PO_ONLINE_REPORT_TEXT_GT
17299 (  online_report_id
17300 ,  last_update_login
17301 ,  last_updated_by
17302 ,  last_update_date
17303 ,  created_by
17304 ,  creation_date
17305 ,  line_num
17306 ,  shipment_num
17307 ,  distribution_num
17308 ,  sequence
17309 ,  text_line
17310 ,  message_name
17311 )
17312 SELECT
17313    p_online_report_id
17314 ,  p_login_id
17315 ,  p_user_id
17316 ,  sysdate
17317 ,  p_user_id
17318 ,  sysdate
17319 ,  POL.line_num
17320 ,  POLL.shipment_num
17321 ,  POD.distribution_num
17322 ,  p_sequence + ROWNUM
17323 ,  decode ( POL.quantity                                      -- <SERVICES FPJ>
17324           , NULL , PO_CORE_S.get_translated_text
17325                    (   'PO_SUB_DIST_BILL_GT_ORD_AMT'
17326                    ,   'LINE_NUM'    , POL.line_num
17327                    ,   'SHIP_NUM'    , POLL.shipment_num
17328                    ,   'DIST_NUM'    , POD.distribution_num
17329                    ,   'AMT_BILLED'  , nvl(POD.amount_billed, 0)
17330                    ,   'AMT_ORDERED' , nvl(POD.amount_ordered, 0)
17331                    )
17332                  , PO_CORE_S.get_translated_text
17333                    (   'PO_SUB_DIST_BILL_GT_ORD_QTY'
17334                    ,   'LINE_NUM'    , POL.line_num
17335                    ,   'SHIP_NUM'    , POLL.shipment_num
17336                    ,   'DIST_NUM'    , POD.distribution_num
17337                    ,   'QTY_BILLED'  , nvl(POD.quantity_billed, 0)
17338                    ,   'QTY_ORDERED' , nvl(POD.quantity_ordered, 0)
17339                    )
17340           )
17341 ,  decode ( POL.quantity                                      -- <SERVICES FPJ>
17342           , NULL , 'PO_SUB_DIST_BILL_GT_ORD_AMT'
17343                  , 'PO_SUB_DIST_BILL_GT_ORD_QTY'
17344           )
17345 FROM
17346    PO_DISTRIBUTIONS_GT POD
17347 ,  PO_LINE_LOCATIONS_GT POLL
17348 ,  PO_LINES_ALL POL  -- For releases, PO_LINES_GT table isn't populated.
17349 WHERE POD.line_location_id = POLL.line_location_id
17350 AND   POL.po_line_id = POLL.po_line_id
17351 AND   nvl(POLL.cancel_flag, 'N') = 'N'
17352 AND   nvl(POLL.closed_code, 'OPEN') <> 'FINALLY CLOSED'
17353 AND   (                                                       -- <SERVICES FPJ>
17354           (   ( POL.quantity IS NOT NULL )
17355           AND ( nvl(POD.quantity_billed,0) > nvl(POD.quantity_ordered,0) ) )
17356       OR  (   ( POL.amount IS NOT NULL )
17357           AND ( nvl(POD.amount_billed,0) > nvl(POD.amount_ordered,0) ) )
17358       );
17359 
17360 l_progress := '150';
17361 
17362 --Increment the p_sequence with number of errors reported in last query
17363 p_sequence := p_sequence + SQL%ROWCOUNT;
17364 
17365 IF g_debug_stmt THEN
17366    PO_DEBUG.debug_var(l_log_head,l_progress,'p_sequence',p_sequence);
17367 END IF;
17368 
17369 ----------------------------------------------------------
17370 
17371 l_progress := '200';
17372 
17373 IF g_debug_stmt THEN
17374    PO_DEBUG.debug_stmt(l_log_head,l_progress,
17375       'UNRESERVE 3: Dist qty delivered greater than qty ordered');
17376 END IF;
17377 
17378 -- Check 3: Unreserve action not allowed if distribution quantity/amount
17379 -- delivered is greater than the quantity/amount ordered
17380 
17381 l_progress := '210';
17382 
17383 -- Perform the check.
17384 
17385 INSERT INTO PO_ONLINE_REPORT_TEXT_GT
17386 (  online_report_id
17387 ,  last_update_login
17388 ,  last_updated_by
17389 ,  last_update_date
17390 ,  created_by
17391 ,  creation_date
17392 ,  line_num
17393 ,  shipment_num
17394 ,  distribution_num
17395 ,  sequence
17396 ,  text_line
17397 ,  message_name
17398 )
17399 SELECT
17400    p_online_report_id
17401 ,  p_login_id
17402 ,  p_user_id
17403 ,  sysdate
17404 ,  p_user_id
17405 ,  sysdate
17406 ,  POL.line_num
17407 ,  POLL.shipment_num
17408 ,  POD.distribution_num
17409 ,  p_sequence + ROWNUM
17410 ,  decode ( POL.quantity                                      -- <SERVICES FPJ>
17411           , NULL , PO_CORE_S.get_translated_text
17412                    (   'PO_SUB_DIST_DLVR_GT_ORD_AMT'
17413                    ,   'LINE_NUM'      , POL.line_num
17414                    ,   'SHIP_NUM'      , POLL.shipment_num
17415                    ,   'DIST_NUM'      , POD.distribution_num
17416                    ,   'AMT_DELIVERED' , nvl(POD.amount_delivered, 0)
17417                    ,   'AMT_ORDERED'   , nvl(POD.amount_ordered, 0)
17418                    )
17419                  , PO_CORE_S.get_translated_text
17420                    (   'PO_SUB_DIST_DLVR_GT_ORD_QTY'
17421                    ,   'LINE_NUM'      , POL.line_num
17422                    ,   'SHIP_NUM'      , POLL.shipment_num
17423                    ,   'DIST_NUM'      , POD.distribution_num
17424                    ,   'QTY_DELIVERED' , nvl(POD.quantity_delivered, 0)
17425                    ,   'QTY_ORDERED'   , nvl(POD.quantity_ordered, 0)
17426                    )
17427           )
17428 ,  decode ( POL.quantity                                      -- <SERVICES FPJ>
17429           , NULL , 'PO_SUB_DIST_DLVR_GT_ORD_AMT'
17430                  , 'PO_SUB_DIST_DLVR_GT_ORD_QTY'
17431           )
17432 FROM
17433    PO_DISTRIBUTIONS_GT POD
17434 ,  PO_LINE_LOCATIONS_GT POLL
17435 ,  PO_LINES_ALL POL  -- For releases, PO_LINES_GT table isn't populated.
17436 WHERE POD.line_location_id = POLL.line_location_id
17437 AND   POL.po_line_id = POLL.po_line_id
17438 AND   nvl(POLL.cancel_flag,'N') = 'N'
17439 AND   nvl(POLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
17440 AND   (                                                       -- <SERVICES FPJ>
17441           (   ( POL.quantity IS NOT NULL )
17442           AND ( nvl(POD.quantity_delivered,0) > nvl(POD.quantity_ordered,0) ) )
17443       OR  (   ( POL.amount IS NOT NULL )
17444           AND ( nvl(POD.amount_delivered,0) > nvl(POD.amount_ordered,0) ) )
17445       )
17446 ;
17447 
17448 l_progress := '250';
17449 -- Check 4: Check if there are unvalidated invoices/credit memo
17450 -- BuG#5462677
17451 -- Per Bug#4155351 p_origin_doc_id need to be passed only for finally close
17452 -- "..PO will pass invoice_id during final close due to a final match. "
17453       check_unvalidated_invoices(
17454          p_document_type     => p_document_type
17455       ,  p_document_subtype  => p_document_subtype
17456       ,  p_action_requested  => g_action_UNRESERVE
17457       ,  p_action_date       => SYSDATE
17458       ,  p_online_report_id  => p_online_report_id
17459       ,  p_user_id           => p_user_id
17460       ,  p_login_id          => p_login_id
17461       ,  p_document_level    => p_document_level
17462       ,  p_origin_doc_id     => NULL
17463       ,  p_doc_level_id      => p_doc_level_id
17464       ,  p_sequence          => p_sequence
17465       ,  x_return_status     => l_ret_sts
17466       );
17467 
17468       IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
17469         --d_msg := 'check_unvalidated_invoices not successful';
17470         l_progress := 110;
17471         RAISE PO_CORE_S.g_early_return_exc;
17472       END IF;
17473 
17474 
17475 l_progress := '260';
17476 
17477 --Increment the p_sequence with number of errors reported in last query
17478 p_sequence := p_sequence + SQL%ROWCOUNT;
17479 
17480 IF g_debug_stmt THEN
17481    PO_DEBUG.debug_var(l_log_head,l_progress,'p_sequence',p_sequence);
17482 END IF;
17483 
17484 l_progress := '300';
17485 
17486 IF g_debug_stmt THEN
17487    PO_DEBUG.debug_end(l_log_head);
17488 END IF;
17489 
17490 EXCEPTION
17491 WHEN OTHERS THEN
17492    IF g_debug_unexp THEN
17493       PO_DEBUG.debug_exc(l_log_head,l_progress);
17494    END IF;
17495    RAISE;
17496 
17497 END check_unreserve;
17498 
17499 
17500 
17501 
17502 -------------------------------------------------------------------------------
17503 --Start of Comments
17504 --Name: populate_line_locations_gt
17505 --Pre-reqs:
17506 --  None.
17507 --Modifies:
17508 --  PO_LINE_LOCATIONS_GT
17509 --Locks:
17510 --  None.
17511 --Function:
17512 --  Populates the line locations GTT for submission checks.
17513 --Parameters:
17514 --IN:
17515 --p_doc_type
17516 --  Document type.  Use the g_doc_type_<> variables, where <> is:
17517 --    PA
17518 --    PO
17519 --    RELEASE
17520 --p_doc_level
17521 --  The type of id that is being passed.  Use g_doc_level_<>
17522 --    HEADER
17523 --    LINE
17524 --    SHIPMENT
17525 --    DISTRIBUTION
17526 --p_doc_level_id
17527 --  Id of the doc level type of which to populate the table.
17528 --Testing:
17529 --
17530 --End of Comments
17531 -------------------------------------------------------------------------------
17532 PROCEDURE populate_line_locations_gt(
17533    p_doc_type                       IN             VARCHAR2
17534 ,  p_doc_level                      IN             VARCHAR2
17535 ,  p_doc_level_id                   IN             NUMBER
17536 ,  p_draft_id                       IN             NUMBER  -- CLM Aprvl
17537 )
17538 IS
17539 
17540 l_log_head CONSTANT VARCHAR2(100) := g_log_head||'POPULATE_LINE_LOCATIONS_GT';
17541 l_progress VARCHAR2(3) := '000';
17542 
17543 l_line_location_id_tbl  po_tbl_number;
17544 -- PAR Project
17545 l_draft_type VARCHAR2(30);
17546 
17547 BEGIN
17548 
17549 IF g_debug_stmt THEN
17550    PO_DEBUG.debug_begin(l_log_head);
17551    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_type', p_doc_type);
17552    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level', p_doc_level);
17553    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level_id',p_doc_level_id);
17554    PO_DEBUG.debug_var(l_log_head,l_progress,'p_draft_id',p_draft_id); -- CLM Aprvl
17555 END IF;
17556 
17557 l_progress := '010';
17558 
17559 PO_CORE_S.get_line_location_ids(
17560    p_doc_type => p_doc_type
17561 ,  p_doc_level => p_doc_level
17562 ,  p_doc_level_id_tbl => po_tbl_number( p_doc_level_id )
17563 ,  p_draft_id => p_draft_id
17564 ,  x_line_location_id_tbl => l_line_location_id_tbl
17565 );
17566 
17567 l_progress := '020';
17568 
17569 --<PAR Project>
17570 l_draft_type := 'AWARD';
17571 IF p_draft_id <> -1 THEN
17572 BEGIN
17573 SELECT draft_type
17574 INTO l_draft_type
17575 FROM po_drafts
17576 WHERE draft_id = p_draft_id;
17577 Exception
17578 When No_Data_Found Then
17579 l_draft_type := 'AWARD';
17580 End;
17581 END IF;
17582 
17583 FORALL i IN 1 .. l_line_location_id_tbl.COUNT
17584       INSERT INTO PO_LINE_LOCATIONS_GT (
17585          line_location_id     ,
17586          last_update_date      ,
17587          last_updated_by        ,
17588          po_header_id            ,
17589          po_line_id               ,
17590          last_update_login         ,
17591          creation_date              ,
17592          created_by                  ,
17593          quantity                     ,
17594          quantity_received             ,
17595          quantity_accepted              ,
17596          quantity_rejected               ,
17597          quantity_billed                  ,
17598          quantity_cancelled                ,
17599          amount,                                         -- <PO_CHANGE_API FPJ>
17600          amount_received,                                     -- <SERVICES FPJ>
17601          amount_accepted,                                     -- <SERVICES FPJ>
17602          amount_rejected,                                     -- <SERVICES FPJ>
17603          amount_billed,                                       -- <SERVICES FPJ>
17604          amount_cancelled,                                    -- <SERVICES FPJ>
17605          unit_meas_lookup_code              ,
17606          po_release_id                       ,
17607          ship_to_location_id                  ,
17608          ship_via_lookup_code                  ,
17609          need_by_date                           ,
17610          promised_date                           ,
17611          last_accept_date                         ,
17612          price_override                          ,
17613          encumbered_flag                        ,
17614          encumbered_date                         ,
17615          unencumbered_quantity                    ,
17616          fob_lookup_code                         ,
17617          freight_terms_lookup_code                ,
17618          taxable_flag                            ,
17619          estimated_tax_amount                    ,
17620          from_header_id                          ,
17621          from_line_id                             ,
17622          from_line_location_id                   ,
17623          start_date                               ,
17624          end_date                                ,
17625          lead_time                              ,
17626          lead_time_unit                          ,
17627          price_discount                           ,
17628          terms_id                                 ,
17629          approved_flag                            ,
17630          approved_date                            ,
17631          closed_flag                              ,
17632          cancel_flag                              ,
17633          cancelled_by                             ,
17634          cancel_date                              ,
17635          cancel_reason                            ,
17636          firm_status_lookup_code                  ,
17637          firm_date                                ,
17638          attribute_category                       ,
17639          attribute1                               ,
17640          attribute2                               ,
17641          attribute3                               ,
17642          attribute4                               ,
17643          attribute5                               ,
17644          attribute6                               ,
17645          attribute7                               ,
17646          attribute8                               ,
17647          attribute9                               ,
17648          attribute10                              ,
17649          unit_of_measure_class                    ,
17650          encumber_now                             ,
17651          attribute11                              ,
17652          attribute12                              ,
17653          attribute13                              ,
17654          attribute14                              ,
17655          attribute15                              ,
17656          inspection_required_flag                 ,
17657          receipt_required_flag                    ,
17658          qty_rcv_tolerance                       ,
17659          qty_rcv_exception_code                   ,
17660          enforce_ship_to_location_code            ,
17661          allow_substitute_receipts_flag           ,
17662          days_early_receipt_allowed               ,
17663          days_late_receipt_allowed                ,
17664          receipt_days_exception_code             ,
17665          invoice_close_tolerance                  ,
17666          receive_close_tolerance                  ,
17667          ship_to_organization_id                 ,
17668          shipment_num                            ,
17669          source_shipment_id                      ,
17670          shipment_type                     ,
17671          closed_code                        ,
17672          request_id                          ,
17673          program_application_id               ,
17674          program_id                            ,
17675          program_update_date                    ,
17676          government_context                      ,
17677          receiving_routing_id                     ,
17678          accrue_on_receipt_flag                  ,
17679          closed_reason                           ,
17680          closed_date                              ,
17681          closed_by                               ,
17682          org_id                                  ,
17683          global_attribute1                        ,
17684          global_attribute2                        ,
17685          global_attribute3                        ,
17686          global_attribute4                        ,
17687          global_attribute5                        ,
17688          global_attribute6                        ,
17689          global_attribute7                        ,
17690          global_attribute8                        ,
17691          global_attribute9                        ,
17692          global_attribute10                       ,
17693          global_attribute11                       ,
17694          global_attribute12                       ,
17695          global_attribute13                       ,
17696          global_attribute14                       ,
17697          global_attribute15                       ,
17698          global_attribute16                       ,
17699          global_attribute17                       ,
17700          global_attribute18                       ,
17701          global_attribute19                       ,
17702          global_attribute20                       ,
17703          global_attribute_category                ,
17704          quantity_shipped                        ,
17705          country_of_origin_code                   ,
17706          tax_user_override_flag                  ,
17707          match_option                            ,
17708          tax_code_id                              ,
17709          calculate_tax_flag                      ,
17710          change_promised_date_reason            ,
17711          note_to_receiver                        ,
17712          secondary_quantity                      ,
17713          secondary_unit_of_measure               ,
17714          preferred_grade                         ,
17715          secondary_quantity_received             ,
17716          secondary_quantity_accepted              ,
17717          secondary_quantity_rejected             ,
17718          secondary_quantity_cancelled             ,
17719          vmi_flag                                 ,
17720          consigned_flag                           ,
17721          retroactive_date                         ,
17722          payment_type                             , --<Complex Work R12>
17723          description                              , --<Complex Work R12>
17724          value_basis                              ,  --<Complex Work R12>
17725          UDA_TEMPLATE_ID                         ,
17726          DRAFT_ID                                ,
17727          CLM_PERIOD_PERF_END_DATE                ,
17728          CLM_PERIOD_PERF_START_DATE              ,
17729          CLM_DELIVERY_PERIOD                     , --<Event Based Delivery>
17730          CLM_DELIVERY_PERIOD_UOM                 ,
17731          CLM_PROMISE_PERIOD                      ,
17732          CLM_PROMISE_PERIOD_UOM                  ,
17733          CLM_POP_DURATION                        ,
17734          CLM_POP_DURATION_UOM
17735      )
17736      SELECT
17737                   poll.line_location_id     ,
17738          poll.last_update_date      ,
17739          poll.last_updated_by        ,
17740          poll.po_header_id            ,
17741          poll.po_line_id               ,
17742          poll.last_update_login         ,
17743          poll.creation_date              ,
17744          poll.created_by                  ,
17745          poll.quantity                     ,
17746          poll.quantity_received             ,
17747          poll.quantity_accepted              ,
17748          poll.quantity_rejected               ,
17749          poll.quantity_billed                  ,
17750          poll.quantity_cancelled                ,
17751          poll.amount,                                         -- <PO_CHANGE_API FPJ>
17752          poll.amount_received,                                     -- <SERVICES FPJ>
17753          poll.amount_accepted,                                     -- <SERVICES FPJ>
17754          poll.amount_rejected,                                     -- <SERVICES FPJ>
17755          poll.amount_billed,                                       -- <SERVICES FPJ>
17756          poll.amount_cancelled,                                    -- <SERVICES FPJ>
17757          poll.unit_meas_lookup_code              ,
17758          poll.po_release_id                       ,
17759          poll.ship_to_location_id                  ,
17760          poll.ship_via_lookup_code                  ,
17761          poll.need_by_date                           ,
17762          poll.promised_date                           ,
17763          poll.last_accept_date                         ,
17764          poll.price_override                          ,
17765          poll.encumbered_flag                        ,
17766          poll.encumbered_date                         ,
17767          poll.unencumbered_quantity                    ,
17768          poll.fob_lookup_code                         ,
17769          poll.freight_terms_lookup_code                ,
17770          poll.taxable_flag                            ,
17771          poll.estimated_tax_amount                    ,
17772          poll.from_header_id                          ,
17773          poll.from_line_id                             ,
17774          poll.from_line_location_id                   ,
17775          poll.start_date                               ,
17776          poll.end_date                                ,
17777          poll.lead_time                              ,
17778          poll.lead_time_unit                          ,
17779          poll.price_discount                           ,
17780          poll.terms_id                                 ,
17781          poll.approved_flag                            ,
17782          poll.approved_date                            ,
17783          poll.closed_flag                              ,
17784          poll.cancel_flag                              ,
17785          poll.cancelled_by                             ,
17786          poll.cancel_date                              ,
17787          poll.cancel_reason                            ,
17788          poll.firm_status_lookup_code                  ,
17789          poll.firm_date                                ,
17790          poll.attribute_category                       ,
17791          poll.attribute1                               ,
17792          poll.attribute2                               ,
17793          poll.attribute3                               ,
17794          poll.attribute4                               ,
17795          poll.attribute5                               ,
17796          poll.attribute6                               ,
17797          poll.attribute7                               ,
17798          poll.attribute8                               ,
17799          poll.attribute9                               ,
17800          poll.attribute10                              ,
17801          poll.unit_of_measure_class                    ,
17802          poll.encumber_now                             ,
17803          poll.attribute11                              ,
17804          poll.attribute12                              ,
17805          poll.attribute13                              ,
17806          poll.attribute14                              ,
17807          poll.attribute15                              ,
17808          poll.inspection_required_flag                 ,
17809          poll.receipt_required_flag                    ,
17810          poll.qty_rcv_tolerance                       ,
17811          poll.qty_rcv_exception_code                   ,
17812          poll.enforce_ship_to_location_code            ,
17813          poll.allow_substitute_receipts_flag           ,
17814          poll.days_early_receipt_allowed               ,
17815          poll.days_late_receipt_allowed                ,
17816          poll.receipt_days_exception_code             ,
17817          poll.invoice_close_tolerance                  ,
17818          poll.receive_close_tolerance                  ,
17819          poll.ship_to_organization_id                 ,
17820          poll.shipment_num                            ,
17821          poll.source_shipment_id                      ,
17822          poll.shipment_type                     ,
17823          poll.closed_code                        ,
17824          poll.request_id                          ,
17825          poll.program_application_id               ,
17826          poll.program_id                            ,
17827         poll.program_update_date                    ,
17828          poll.government_context                      ,
17829          poll.receiving_routing_id                     ,
17830          poll.accrue_on_receipt_flag                  ,
17831          poll.closed_reason                           ,
17832          poll.closed_date                              ,
17833          poll.closed_by                               ,
17834          poll.org_id                                  ,
17835          poll.global_attribute1                        ,
17836          poll.global_attribute2                        ,
17837          poll.global_attribute3                        ,
17838          poll.global_attribute4                        ,
17839          poll.global_attribute5                        ,
17840          poll.global_attribute6                        ,
17841          poll.global_attribute7                        ,
17842          poll.global_attribute8                        ,
17843          poll.global_attribute9                        ,
17844          poll.global_attribute10                       ,
17845          poll.global_attribute11                       ,
17846          poll.global_attribute12                       ,
17847          poll.global_attribute13                       ,
17848          poll.global_attribute14                       ,
17849          poll.global_attribute15                       ,
17850          poll.global_attribute16                       ,
17851          poll.global_attribute17                       ,
17852          poll.global_attribute18                       ,
17853          poll.global_attribute19                       ,
17854          poll.global_attribute20                       ,
17855          poll.global_attribute_category                ,
17856          poll.quantity_shipped                        ,
17857          poll.country_of_origin_code                   ,
17858          poll.tax_user_override_flag                  ,
17859          poll.match_option                            ,
17860          poll.tax_code_id                              ,
17861          poll.calculate_tax_flag                      ,
17862          poll.change_promised_date_reason            ,
17863          poll.note_to_receiver                        ,
17864          poll.secondary_quantity                      ,
17865          poll.secondary_unit_of_measure               ,
17866          poll.preferred_grade                         ,
17867          poll.secondary_quantity_received             ,
17868          poll.secondary_quantity_accepted              ,
17869         poll.secondary_quantity_rejected             ,
17870          poll.secondary_quantity_cancelled             ,
17871          poll.vmi_flag                                 ,
17872          poll.consigned_flag                           ,
17873          poll.retroactive_date                         ,
17874          poll.payment_type                             , --<Complex Work R12>
17875          poll.description                              , --<Complex Work R12>
17876          poll.value_basis                             ,   --<Complex Work R12>
17877          poll.UDA_TEMPLATE_ID                         ,
17878          poll.DRAFT_ID                                ,
17879          poll.CLM_PERIOD_PERF_END_DATE                ,
17880          poll.CLM_PERIOD_PERF_START_DATE              ,
17881          poll.CLM_DELIVERY_PERIOD                     , --<Event Based Delivery>
17882          poll.CLM_DELIVERY_PERIOD_UOM                 ,
17883          poll.CLM_PROMISE_PERIOD                      ,
17884          poll.CLM_PROMISE_PERIOD_UOM                  ,
17885          poll.CLM_POP_DURATION                        ,
17886          poll.CLM_POP_DURATION_UOM
17887 
17888 		 FROM PO_LINE_LOCATIONS_MERGE_V POLL
17889 		 WHERE POLL.LINE_LOCATION_ID =  l_line_location_id_tbl(i)
17890 		 AND POLL.DRAFT_ID = p_draft_id
17891 		 /*-- CLM Aprvl
17892       FROM PO_LINE_LOCATIONS_ALL POLL
17893       WHERE POLL.line_location_id = l_line_location_id_tbl(i)
17894 	  -- CLM Aprvl*/
17895     --<PAR Project>  need to change the entities which should be validated. Only records in the draft tables should be validated.
17896      AND (l_draft_type <> 'PAR'
17897           OR (l_draft_type = 'PAR' AND poll.change_status IS NOT NULL ));
17898 
17899 
17900 l_progress := '900';
17901 
17902 IF g_debug_stmt THEN
17903    PO_DEBUG.debug_end(l_log_head);
17904 END IF;
17905 
17906 EXCEPTION
17907 WHEN OTHERS THEN
17908    IF g_debug_unexp THEN
17909       PO_DEBUG.debug_exc(l_log_head,l_progress);
17910    END IF;
17911    RAISE;
17912 
17913 END populate_line_locations_gt;
17914 
17915 
17916 
17917 
17918 -------------------------------------------------------------------------------
17919 --Start of Comments
17920 --Name: populate_distributions_gt
17921 --Pre-reqs:
17922 --  None.
17923 --Modifies:
17924 --  PO_DISTRIBTIONS_GT
17925 --Locks:
17926 --  None.
17927 --Function:
17928 --  Populates the distributions GTT for submission checks.
17929 --Parameters:
17930 --IN:
17931 --p_doc_type
17932 --  Document type.  Use the g_doc_type_<> variables, where <> is:
17933 --    REQUISITION
17934 --    PA
17935 --    PO
17936 --    RELEASE
17937 --p_doc_level
17938 --  The type of id that is being passed.  Use g_doc_level_<>
17939 --    HEADER
17940 --    LINE
17941 --    SHIPMENT
17942 --    DISTRIBUTION
17943 --p_doc_level_id
17944 --  Id of the doc level type of which to populate the distributions table.
17945 --Testing:
17946 --
17947 --End of Comments
17948 -------------------------------------------------------------------------------
17949 PROCEDURE populate_distributions_gt(
17950    p_doc_type                       IN             VARCHAR2
17951 ,  p_doc_level                      IN             VARCHAR2
17952 ,  p_doc_level_id                   IN             NUMBER
17953 ,  p_draft_id                       IN             NUMBER -- CLM Aprvl
17954 )
17955 IS
17956 
17957 l_log_head CONSTANT VARCHAR2(100) := g_log_head||'POPULATE_DISTRIBUTIONS_GT';
17958 l_progress VARCHAR2(3) := '000';
17959 
17960 l_dist_id_tbl  po_tbl_number;
17961 -- PAR Project
17962 l_draft_type VARCHAR2(30);
17963 
17964 BEGIN
17965 
17966 IF g_debug_stmt THEN
17967    PO_DEBUG.debug_begin(l_log_head);
17968    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_type', p_doc_type);
17969    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level', p_doc_level);
17970    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_level_id',p_doc_level_id);
17971    PO_DEBUG.debug_var(l_log_head,l_progress,'p_draft_id',p_draft_id);   -- CLM Aprvl
17972 END IF;
17973 
17974 l_progress := '010';
17975 
17976 IF (p_doc_type = g_document_type_REQUISITION) THEN
17977 
17978    l_progress := '020';
17979    IF g_debug_stmt THEN
17980       PO_DEBUG.debug_stmt(l_log_head,l_progress,'requisition');
17981    END IF;
17982 
17983    populate_req_distributions_gt(
17984       p_document_id => p_doc_level_id
17985    );
17986 
17987    l_progress := '030';
17988 
17989 ELSE
17990 
17991    l_progress := '040';
17992    IF g_debug_stmt THEN
17993       PO_DEBUG.debug_stmt(l_log_head,l_progress,'not requisition');
17994    END IF;
17995 
17996    PO_CORE_S.get_distribution_ids(
17997       p_doc_type => p_doc_type
17998    ,  p_doc_level => p_doc_level
17999    ,  p_doc_level_id_tbl => po_tbl_number( p_doc_level_id )
18000    ,  p_draft_id => p_draft_id
18001    ,  x_distribution_id_tbl => l_dist_id_tbl
18002    );
18003 
18004    l_progress := '050';
18005 
18006    --<PAR Project>
18007    l_draft_type := 'AWARD';
18008    IF p_draft_id <> -1 THEN
18009    BEGIN
18010    SELECT draft_type
18011    INTO l_draft_type
18012    FROM po_drafts
18013    WHERE draft_id = p_draft_id;
18014    Exception
18015    When No_Data_Found Then
18016     l_draft_type := 'AWARD';
18017    End;
18018    END IF;
18019 
18020    FORALL i IN 1 .. l_dist_id_tbl.COUNT
18021    INSERT INTO PO_DISTRIBUTIONS_GT
18022    (
18023          po_distribution_id    ,
18024          last_update_date       ,
18025          last_updated_by         ,
18026          po_header_id             ,
18027          po_line_id                ,
18028          line_location_id           ,
18029          set_of_books_id            ,
18030          code_combination_id         ,
18031          quantity_ordered             ,
18032          amount_ordered,                                 -- <PO_CHANGE_API FPJ>
18033          last_update_login             ,
18034          creation_date                  ,
18035          created_by                      ,
18036          po_release_id                    ,
18037          quantity_delivered                ,
18038          quantity_billed                    ,
18039          quantity_cancelled                  ,
18040          amount_delivered,                                    -- <SERVICES FPJ>
18041          amount_billed                            ,
18042          amount_cancelled,                                    -- <SERVICES FPJ>
18043          req_header_reference_num             ,
18044          req_line_reference_num                ,
18045          req_distribution_id                    ,
18046          deliver_to_location_id                  ,
18047          deliver_to_person_id                    ,
18048          rate_date                               ,
18049          rate                                    ,
18050          accrued_flag                             ,
18051          encumbered_flag                          ,
18052          encumbered_amount                        ,
18053          unencumbered_quantity                    ,
18054          unencumbered_amount                      ,
18055          failed_funds_lookup_code                 ,
18056          gl_encumbered_date                       ,
18057          gl_encumbered_period_name                ,
18058          gl_cancelled_date                        ,
18059          destination_type_code                    ,
18060          destination_organization_id              ,
18061          destination_subinventory                ,
18062          attribute_category                       ,
18063          attribute1                               ,
18064          attribute2                               ,
18065          attribute3                               ,
18066          attribute4                               ,
18067          attribute5                               ,
18068          attribute6                               ,
18069          attribute7                               ,
18070          attribute8                               ,
18071          attribute9                               ,
18072          attribute10                              ,
18073          attribute11                              ,
18074          attribute12                              ,
18075          attribute13                              ,
18076          attribute14                              ,
18077          attribute15                              ,
18078          wip_entity_id                            ,
18079          wip_operation_seq_num                    ,
18080          wip_resource_seq_num                     ,
18081          wip_repetitive_schedule_id               ,
18082          wip_line_id                              ,
18083          bom_resource_id                          ,
18084          budget_account_id                        ,
18085          accrual_account_id                       ,
18086          variance_account_id                      ,
18087          prevent_encumbrance_flag                ,
18088          government_context                      ,
18089          destination_context                     ,
18090          distribution_num                  ,
18091          source_distribution_id             ,
18092          request_id                         ,
18093          program_application_id              ,
18094          program_id                           ,
18095          program_update_date                   ,
18096          project_id                             ,
18097          task_id                                 ,
18098          expenditure_type                         ,
18099          project_accounting_context              ,
18100          expenditure_organization_id              ,
18101          gl_closed_date                           ,
18102          accrue_on_receipt_flag                  ,
18103          expenditure_item_date                   ,
18104          org_id                                   ,
18105          kanban_card_id                           ,
18106          award_id                                ,
18107          mrc_rate_date                           ,
18108          mrc_rate                                 ,
18109          mrc_encumbered_amount                   ,
18110          mrc_unencumbered_amount                  ,
18111          end_item_unit_number                     ,
18112          tax_recovery_override_flag               ,
18113          recoverable_tax                          ,
18114          nonrecoverable_tax                       ,
18115          recovery_rate                            ,
18116          oke_contract_line_id                     ,
18117          oke_contract_deliverable_id
18118       ,  distribution_type
18119       ,  amount_to_encumber
18120       ,  funded_value,
18121          acrn,
18122          clm_payment_sequence_num
18123    )
18124    SELECT
18125                   pod.po_distribution_id    ,
18126          pod.last_update_date       ,
18127          pod.last_updated_by         ,
18128          pod.po_header_id             ,
18129          pod.po_line_id                ,
18130          pod.line_location_id           ,
18131          pod.set_of_books_id            ,
18132          pod.code_combination_id         ,
18133          pod.quantity_ordered             ,
18134          pod.amount_ordered,                                 -- <PO_CHANGE_API FPJ>
18135          pod.last_update_login             ,
18136          pod.creation_date                  ,
18137          pod.created_by                      ,
18138          pod.po_release_id                    ,
18139          pod.quantity_delivered                ,
18140          pod.quantity_billed                    ,
18141          pod.quantity_cancelled                  ,
18142          pod.amount_delivered,                                    -- <SERVICES FPJ>
18143          pod.amount_billed                            ,
18144          pod.amount_cancelled,                                    -- <SERVICES FPJ>
18145          pod.req_header_reference_num             ,
18146          pod.req_line_reference_num                ,
18147          pod.req_distribution_id                    ,
18148          pod.deliver_to_location_id                  ,
18149          pod.deliver_to_person_id                    ,
18150          pod.rate_date                               ,
18151          pod.rate                                    ,
18152          pod.accrued_flag                             ,
18153          pod.encumbered_flag                          ,
18154          pod.encumbered_amount                        ,
18155          pod.unencumbered_quantity                    ,
18156          pod.unencumbered_amount                      ,
18157          pod.failed_funds_lookup_code                 ,
18158          pod.gl_encumbered_date                       ,
18159          pod.gl_encumbered_period_name                ,
18160          pod.gl_cancelled_date                        ,
18161          pod.destination_type_code                    ,
18162          pod.destination_organization_id              ,
18163          pod.destination_subinventory                ,
18164          pod.attribute_category                       ,
18165          pod.attribute1                               ,
18166          pod.attribute2                               ,
18167          pod.attribute3                               ,
18168          pod.attribute4                               ,
18169          pod.attribute5                               ,
18170          pod.attribute6                               ,
18171          pod.attribute7                               ,
18172          pod.attribute8                               ,
18173          pod.attribute9                               ,
18174          pod.attribute10                              ,
18175          pod.attribute11                              ,
18176          pod.attribute12                              ,
18177          pod.attribute13                              ,
18178          pod.attribute14                              ,
18179          pod.attribute15                              ,
18180          pod.wip_entity_id                            ,
18181          pod.wip_operation_seq_num                    ,
18182          pod.wip_resource_seq_num                     ,
18183          pod.wip_repetitive_schedule_id               ,
18184          pod.wip_line_id                              ,
18185          pod.bom_resource_id                          ,
18186          pod.budget_account_id                        ,
18187          pod.accrual_account_id                       ,
18188          pod.variance_account_id                      ,
18189          pod.prevent_encumbrance_flag                ,
18190          pod.government_context                      ,
18191          pod.destination_context                     ,
18192          pod.distribution_num                  ,
18193          pod.source_distribution_id             ,
18194          pod.request_id                         ,
18195          pod.program_application_id              ,
18196          pod.program_id                           ,
18197          pod.program_update_date                   ,
18198          pod.project_id                             ,
18199          pod.task_id                                 ,
18200          pod.expenditure_type                         ,
18201          pod.project_accounting_context              ,
18202          pod.expenditure_organization_id              ,
18203          pod.gl_closed_date                           ,
18204          pod.accrue_on_receipt_flag                  ,
18205          pod.expenditure_item_date                   ,
18206          pod.org_id                                   ,
18207          pod.kanban_card_id                           ,
18208          pod.award_id                                ,
18209          pod.mrc_rate_date                           ,
18210          pod.mrc_rate                                 ,
18211          pod.mrc_encumbered_amount                   ,
18212          pod.mrc_unencumbered_amount                  ,
18213          pod.end_item_unit_number                     ,
18214          pod.tax_recovery_override_flag               ,
18215          pod.recoverable_tax                          ,
18216          pod.nonrecoverable_tax                       ,
18217          pod.recovery_rate                            ,
18218          pod.oke_contract_line_id                     ,
18219          pod.oke_contract_deliverable_id
18220       ,  pod.distribution_type
18221       ,  pod.amount_to_encumber
18222       ,  pod.funded_value ,
18223          pod.acrn,
18224          pod.clm_payment_sequence_num
18225 
18226 	  FROM PO_DISTRIBUTIONS_MERGE_V POD
18227 	  WHERE POD.po_distribution_id = l_dist_id_tbl(i)
18228 	  and pod.draft_id = p_draft_id
18229 	  /*   -- CLM Aprvl
18230    FROM PO_DISTRIBUTIONS_ALL POD
18231    WHERE POD.po_distribution_id = l_dist_id_tbl(i)
18232       */-- CLM Aprvl
18233    --<PAR Project>  need to change the entities which should be validated. Only records in the draft tables should be validated.
18234    AND (l_draft_type <> 'PAR'
18235           OR (l_draft_type = 'PAR' AND pod.change_status IS NOT NULL ));
18236 
18237    l_progress := '060';
18238 
18239 END IF;
18240 
18241 l_progress := '900';
18242 
18243 IF g_debug_stmt THEN
18244    PO_DEBUG.debug_end(l_log_head);
18245 END IF;
18246 
18247 EXCEPTION
18248 WHEN OTHERS THEN
18249    IF g_debug_unexp THEN
18250       PO_DEBUG.debug_exc(l_log_head,l_progress);
18251    END IF;
18252    RAISE;
18253 
18254 END populate_distributions_gt;
18255 
18256 
18257 
18258 
18259 -------------------------------------------------------------------------------
18260 --Start of Comments
18261 --Name: check_gl_date
18262 --Pre-reqs:
18263 --  The global temp tables for the appropriate doc type
18264 --  have been populated with all of the rows required for the check.
18265 --  This should only be called if encumbrance is on.
18266 --Modifies:
18267 --  PO_ONLINE_REPORT_TEXT_GT
18268 --Locks:
18269 --  None.
18270 --Function:
18271 --  Checks that the GL date is usable (valid for GL and PO).
18272 --Parameters:
18273 --IN:
18274 --p_doc_type
18275 --  Document type.  Use the g_doc_type_<> variables, where <> is:
18276 --    REQUISITION
18277 --    PA
18278 --    PO
18279 --    RELEASE
18280 --p_online_report_id
18281 --  ID used to insert into PO_ONLINE_REPORT_TEXT_GT.
18282 --p_user_id
18283 --  User performing the action.
18284 --p_login_id
18285 --  Last update login_id.
18286 --IN OUT:
18287 --p_sequence
18288 --  Contains the running count of error messages inserted.
18289 --Testing:
18290 --
18291 --End of Comments
18292 -------------------------------------------------------------------------------
18293 PROCEDURE check_gl_date(
18294    p_doc_type                       IN             VARCHAR2
18295 ,  p_online_report_id               IN             NUMBER
18296 ,  p_login_id                       IN             NUMBER
18297 ,  p_user_id                        IN             NUMBER
18298 ,  p_sequence                       IN OUT NOCOPY  NUMBER
18299 )
18300 IS
18301 
18302 l_log_head     CONSTANT VARCHAR2(100) := g_log_head||'CHECK_GL_DATE';
18303 l_progress     VARCHAR2(3) := '000';
18304 
18305 l_msg_name     PO_ONLINE_REPORT_TEXT_GT.message_name%TYPE;
18306 l_textline     PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
18307 
18308 l_date_tbl              po_tbl_date;
18309 l_line_num_tbl          po_tbl_number;
18310 l_shipment_num_tbl      po_tbl_number;
18311 l_distribution_num_tbl  po_tbl_number;
18312 
18313 l_period_name_tbl       po_tbl_varchar30;
18314 l_period_year_tbl       po_tbl_number;
18315 l_period_num_tbl        po_tbl_number;
18316 l_quarter_num_tbl       po_tbl_number;
18317 l_invalid_period_flag   VARCHAR2(1);
18318 
18319 l_dates_key    NUMBER;
18320 
18321 BEGIN
18322 
18323 IF g_debug_stmt THEN
18324    PO_DEBUG.debug_begin(l_log_head);
18325    PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_type',p_doc_type);
18326    PO_DEBUG.debug_var(l_log_head,l_progress,'p_online_report_id',p_online_report_id);
18327    PO_DEBUG.debug_var(l_log_head,l_progress,'p_user_id',p_user_id);
18328    PO_DEBUG.debug_var(l_log_head,l_progress,'p_login_id',p_login_id);
18329    PO_DEBUG.debug_var(l_log_head,l_progress,'p_sequence',p_sequence);
18330 END IF;
18331 
18332 l_progress := '010';
18333 
18334 -- Get the appropriate message.
18335 
18336 /* Bug 3210344: refactored different messages into one message */
18337 IF ((p_doc_type = g_document_type_REQUISITION) or
18338    (p_doc_type = g_document_type_RELEASE) or
18339    (p_doc_type = g_document_type_PO) or
18340    (p_doc_type = g_document_type_PA))
18341 THEN
18342    l_msg_name := 'PO_PDOI_INVALID_GL_ENC_PER';
18343 ELSE
18344    l_progress := '020';
18345    RAISE PO_CORE_S.g_INVALID_CALL_EXC;
18346 END IF;
18347 
18348 l_progress := '030';
18349 IF g_debug_stmt THEN
18350    PO_DEBUG.debug_var(l_log_head,l_progress,'l_msg_name',l_msg_name);
18351 END IF;
18352 
18353 l_textline := FND_MESSAGE.get_string('PO',l_msg_name);
18354 
18355 l_progress := '040';
18356 IF g_debug_stmt THEN
18357    PO_DEBUG.debug_var(l_log_head,l_progress,'l_textline',l_textline);
18358 END IF;
18359 
18360 -- Get the dates to validate.
18361 
18362 IF (p_doc_type = g_document_type_REQUISITION) THEN
18363 
18364    l_progress := '100';
18365    IF g_debug_stmt THEN
18366       PO_DEBUG.debug_stmt(l_log_head,l_progress,'requisition');
18367    END IF;
18368 
18369    SELECT
18370       PRD.gl_encumbered_date
18371    ,  PRL.line_num
18372    ,  NULL
18373    ,  PRD.distribution_num
18374    BULK COLLECT INTO
18375       l_date_tbl
18376    ,  l_line_num_tbl
18377    ,  l_shipment_num_tbl
18378    ,  l_distribution_num_tbl
18379    FROM
18380       PO_REQ_DISTRIBUTIONS_GT PRD
18381    ,  PO_REQ_LINES_GT PRL
18382    ,  PO_REQ_HEADERS_GT PRH
18383    WHERE PRL.requisition_line_id = PRD.requisition_line_id  --JOIN
18384    AND   PRH.requisition_header_id = PRL.requisition_header_id  --JOIN
18385    AND   PRL.line_location_id IS NULL
18386    AND
18387       (  NVL(PRH.transferred_to_oe_flag,'N') <> 'Y'
18388       OR NVL(PRL.source_type_code,'VENDOR') <> 'INVENTORY'
18389       )
18390    AND   NVL(PRD.encumbered_flag,'N') = 'N'
18391    AND   NVL(PRD.prevent_encumbrance_flag,'N') <> 'Y' -- Bug 10428042
18392    AND   NVL(PRL.cancel_flag,'N') = 'N'
18393    AND   NVL(PRL.closed_code,'OPEN') <> 'FINALLY CLOSED'
18394    AND    Nvl(prl.modified_by_agent_flag,'N') = 'N' /*Bug 4882209*/
18395    ;
18396 
18397    l_progress := '110';
18398 
18399 ELSE
18400 
18401    l_progress := '150';
18402    IF g_debug_stmt THEN
18403       PO_DEBUG.debug_stmt(l_log_head,l_progress,'not requisition');
18404    END IF;
18405 
18406    SELECT
18407       POD.gl_encumbered_date
18408    ,  POL.line_num
18409    ,  POLL.shipment_num
18410    ,  POD.distribution_num
18411    BULK COLLECT INTO
18412       l_date_tbl
18413    ,  l_line_num_tbl
18414    ,  l_shipment_num_tbl
18415    ,  l_distribution_num_tbl
18416    FROM
18417       PO_DISTRIBUTIONS_GT POD
18418    ,  PO_LINE_LOCATIONS_GT POLL
18419    ,  PO_LINES_ALL POL     -- For Releases, PO_LINES_GT doesn't get populated.
18420    ,  PO_HEADERS_GT POH
18421    WHERE POLL.line_location_id(+) = POD.line_location_id    --JOIN
18422    AND   POL.po_line_id(+) = POD.po_line_id                 --JOIN
18423       -- PA distributions don't have associated lines or shipments
18424    AND   POH.po_header_id = POD.po_header_id                --JOIN
18425    AND   NVL(POD.encumbered_flag,'N') = 'N'
18426    AND   NVL(POLL.cancel_flag,'N') = 'N'
18427    AND   NVL(POLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
18428    AND   NVL(POLL.approved_flag,'N') <> 'Y'
18429    AND   NVL(POD.prevent_encumbrance_flag,'N') <> 'Y' -- Bug 10428042
18430       -- Bug 8326256, Bug 8468327, Bug 8468328: For SHOP FLOOR destination type
18431       -- distributions the GL date validation should be skipped.
18432    AND   (NVL(POD.destination_type_code,'EXPENSE') <> 'SHOP FLOOR'  /* 8326256 */
18433           OR (NVL(POD.destination_type_code,'EXPENSE') = 'SHOP FLOOR'
18434                    AND (SELECT entity_type from wip_entities where wip_entity_id = POD.wip_entity_id)= 6))   /* Encumbrance project  */
18435    AND ( ( p_doc_type = g_document_type_PA
18436          AND POH.encumbrance_required_flag = 'Y'
18437          )
18438       OR ( p_doc_type <> g_document_type_PA )
18439       )
18440    ;
18441 
18442    l_progress := '160';
18443 
18444 END IF;
18445 
18446 l_progress := '200';
18447 
18448 -- Validate the dates.
18449 
18450 PO_PERIODS_SV.get_period_info(
18451    p_roll_logic => NULL
18452 ,  p_set_of_books_id => NULL
18453 ,  p_date_tbl => l_date_tbl
18454 ,  x_period_name_tbl => l_period_name_tbl
18455 ,  x_period_year_tbl => l_period_year_tbl
18456 ,  x_period_num_tbl => l_period_num_tbl
18457 ,  x_quarter_num_tbl => l_quarter_num_tbl
18458 ,  x_invalid_period_flag => l_invalid_period_flag
18459 );
18460 
18461 l_progress := '210';
18462 
18463 IF (l_invalid_period_flag = FND_API.G_TRUE) THEN
18464 
18465    l_progress := '215';
18466    IF g_debug_stmt THEN
18467       PO_DEBUG.debug_stmt(l_log_head,l_progress,'invalid period');
18468    END IF;
18469 
18470    -- Collect this information into the scratchpad,
18471    -- along with the info we need for error reporting.
18472 
18473    -----------------------------------------
18474    -- PO_SESSION_GT column mapping
18475    --
18476    -- num1     line_num
18477    -- num2     shipment_num
18478    -- num3     distribution_num
18479    -- char1    period_name
18480    -----------------------------------------
18481 
18482    SELECT PO_SESSION_GT_S.nextval
18483    INTO l_dates_key
18484    FROM DUAL
18485    ;
18486 
18487    l_progress := '220';
18488 
18489    FORALL i IN 1 .. l_period_name_tbl.COUNT
18490    INSERT INTO PO_SESSION_GT
18491    (  key
18492    ,  num1     -- line_num
18493    ,  num2     -- shipment_num
18494    ,  num3     -- distribution_num
18495    ,  char1    -- period_name
18496    )
18497    VALUES
18498    (  l_dates_key
18499    ,  l_line_num_tbl(i)
18500    ,  l_shipment_num_tbl(i)
18501    ,  l_distribution_num_tbl(i)
18502    ,  l_period_name_tbl(i)
18503    )
18504    ;
18505 
18506    l_progress := '230';
18507 
18508    -- Report the invalid dates.
18509 
18510    INSERT INTO PO_ONLINE_REPORT_TEXT_GT
18511    (  online_report_id
18512    ,  last_update_login
18513    ,  last_updated_by
18514    ,  last_update_date
18515    ,  created_by
18516    ,  creation_date
18517    ,  line_num
18518    ,  shipment_num
18519    ,  distribution_num
18520    ,  sequence
18521    ,  text_line
18522    ,  message_name
18523    )
18524    SELECT
18525       p_online_report_id
18526    ,  p_login_id
18527    ,  p_user_id
18528    ,  sysdate
18529    ,  p_user_id
18530    ,  sysdate
18531    ,  NVL(DATES.num1,0)    -- line_num
18532    ,  NVL(DATES.num2,0)    -- shipment_num
18533    ,  NVL(DATES.num3,0)    -- distribution_num
18534    ,  p_sequence + rownum
18535    ,  substr(
18536                DECODE(  DATES.num1  -- line_num
18537                      ,  NULL, ''
18538                      ,  g_linemsg||g_delim||TO_CHAR(DATES.num1)||g_delim
18539                      )
18540                ||
18541                DECODE(  DATES.num2  -- shipment_num
18542                      ,  NULL, ''
18543                      ,  g_shipmsg||g_delim||TO_CHAR(DATES.num2)||g_delim
18544                      )
18545                ||
18546                DECODE(  p_doc_type
18547                      ,  g_document_type_PA, ''
18548                      ,  g_distmsg||g_delim||TO_CHAR(DATES.num3)||g_delim
18549                      )
18550 
18551                ||l_textline
18552 
18553             ,  1
18554             ,  240
18555             )
18556    ,  l_msg_name
18557    FROM PO_SESSION_GT DATES
18558    WHERE DATES.key = l_dates_key
18559    AND   DATES.char1 IS NULL  -- period_name not found
18560    ;
18561 
18562    l_progress := '240';
18563 
18564    -- Increment the p_sequence with number of errors reported in last query
18565    p_sequence := p_sequence + SQL%ROWCOUNT;
18566 
18567    l_progress := '245';
18568 
18569    IF g_debug_stmt THEN
18570       PO_DEBUG.debug_var(l_log_head,l_progress,'l_textline',p_sequence);
18571    END IF;
18572 
18573    l_progress := '250';
18574 
18575 ELSE
18576    l_progress := '270';
18577    IF g_debug_stmt THEN
18578       PO_DEBUG.debug_stmt(l_log_head,l_progress,'No invalid periods.');
18579    END IF;
18580 END IF;
18581 
18582 l_progress := '900';
18583 
18584 IF g_debug_stmt THEN
18585    PO_DEBUG.debug_var(l_log_head,l_progress,'l_textline',p_sequence);
18586    PO_DEBUG.debug_end(l_log_head);
18587 END IF;
18588 
18589 EXCEPTION
18590 WHEN OTHERS THEN
18591    IF g_debug_unexp THEN
18592       PO_DEBUG.debug_exc(l_log_head,l_progress);
18593    END IF;
18594    RAISE;
18595 
18596 END check_gl_date;
18597 
18598 
18599 -- bug3592160 START
18600 -------------------------------------------------------------------------------
18601 --Start of Comments
18602 --Name: complete_po_header_id_tbl
18603 --Pre-reqs:
18604 --  All the IN parameters should be initialized and populated with values.
18605 --  All the table object type IN parameters should have the same number of
18606 --  records
18607 --  Org context should also be set before calling.
18608 --Modifies:
18609 --  None
18610 --Locks:
18611 --  None.
18612 --Function:
18613 --  For each record in the table objects pased in, derive po_header_id.
18614 --Parameters:
18615 --IN:
18616 --p_count
18617 --  Number of records in each of the table object
18618 --p_header_id
18619 --  Table Object containing po_header_id
18620 --p_release_id
18621 --  Table Object containing po_release_id
18622 --p_vendor_order_num
18623 --  Table Object containing vendor_order_num
18624 --p_document_num
18625 --  Table object containing document num (segment1)
18626 --p_type_lookup_code
18627 --  Table obejct containing type_lookup_code
18628 --OUT:
18629 --x_header_id
18630 --  Contains the derived po_header_ids
18631 --Testing:
18632 --
18633 --End of Comments
18634 -------------------------------------------------------------------------------
18635 PROCEDURE complete_po_header_id_tbl
18636 ( p_count            IN NUMBER,
18637   p_header_id        IN PO_TBL_NUMBER,
18638   p_release_id       IN PO_TBL_NUMBER,
18639   p_vendor_order_num IN PO_TBL_VARCHAR30,
18640   p_document_num     IN PO_TBL_VARCHAR30,
18641   p_type_lookup_code IN PO_TBL_VARCHAR30,
18642   x_header_id        OUT NOCOPY PO_TBL_NUMBER
18643 ) IS
18644 
18645 l_api_name     CONSTANT VARCHAR2(30) := 'COMPLETE_PO_HEADER_ID_TBL';
18646 l_log_head     CONSTANT VARCHAR2(100) := g_log_head|| l_api_name;
18647 l_progress     VARCHAR2(3) := '000';
18648 
18649 BEGIN
18650 
18651   IF g_debug_stmt THEN
18652      PO_DEBUG.debug_begin(l_log_head);
18653   END IF;
18654 
18655   x_header_id := PO_TBL_NUMBER();
18656   x_header_id.extend(p_count);
18657 
18658   l_progress := '010';
18659 
18660   FOR i IN 1..p_count LOOP
18661     IF (p_release_id(i) IS NOT NULL) THEN
18662 
18663       -- If p_release_id is not null, don't bother deriving po_header_id
18664       l_progress := '020';
18665       x_header_id(i) := NULL;
18666     ELSE
18667       IF p_header_id(i) IS NOT NULL THEN
18668         x_header_id(i) := p_header_id(i);
18669       END IF;
18670 
18671       l_progress := '030';
18672 
18673       IF (p_document_num(i) IS NOT NULL) THEN
18674 
18675         -- SQL What: Derive po_header_id based on document_num and
18676         --           type lookup_code. If x_header_id(i) already has value,
18677         --           validate it as well. If the po_header_id cannot be found,
18678         --           set x_header_id(i) as -999, indicating that po_header_Id
18679         --           cannot be derived by the IN paramters
18680         SELECT NVL(MIN(po_header_id), '-999')
18681         INTO   x_header_id(i)
18682         FROM   po_headers
18683         WHERE  segment1 = p_document_num(i)
18684         AND    type_lookup_code = p_type_lookup_code(i)
18685         AND    po_header_id = NVL(x_header_id(i), po_header_id);
18686 
18687         IF g_debug_stmt THEN
18688            PO_DEBUG.debug_stmt(l_log_head,l_progress,
18689                                'After checking document_num. id = ' || x_header_id(i));
18690         END IF;
18691       END IF;
18692 
18693       l_progress := '040';
18694 
18695       IF (p_vendor_order_num(i) IS NOT NULL) THEN
18696 
18697         -- SQL What: Derive po_header_id based on vendor_order_num and
18698         --           type lookup_code. If x_header_id(i) already has value,
18699         --           validate it as well. If the po_header_id cannot be found,
18700         --           set x_header_id(i) as -999, indicating that po_header_Id
18701         --           cannot be derived by the IN paramters
18702         SELECT NVL(MIN(po_header_id), '-999')
18703         INTO x_header_id(i)
18704         FROM   po_headers_all
18705         WHERE  vendor_order_num = p_vendor_order_num(i)
18706         AND  po_header_id = NVL(x_header_id(i), po_header_id);
18707 
18708         IF g_debug_stmt THEN
18709            PO_DEBUG.debug_stmt(l_log_head,l_progress,
18710                                'After checking vendor_order_num. id = ' || x_header_id(i));
18711         END IF;
18712       END IF;
18713 
18714     END IF;
18715 
18716     l_progress := '050';
18717 
18718     IF (x_header_id(i) = -999) THEN
18719       x_header_id(i) := NULL;
18720     END IF;
18721 
18722     IF g_debug_stmt THEN
18723        PO_DEBUG.debug_stmt(l_log_head,l_progress,
18724                            'Final ID for rec ' ||i|| '= ' || x_header_id(i));
18725     END IF;
18726 
18727   END LOOP;
18728 
18729   IF g_debug_stmt THEN
18730      PO_DEBUG.debug_end(l_log_head);
18731   END IF;
18732 
18733 EXCEPTION
18734 WHEN OTHERS THEN
18735    IF g_debug_unexp THEN
18736       PO_DEBUG.debug_exc(l_log_head,l_progress);
18737    END IF;
18738    RAISE;
18739 
18740 END complete_po_header_id_tbl;
18741 -- bug3592160 END
18742 
18743 --<JFMIP Vendor Registration FPJ Start>
18744 -------------------------------------------------------------------------------
18745 --Start of Comments
18746 --Name: check_vendor_site_ccr_regis
18747 --Pre-reqs:
18748 --  None
18749 --Modifies:
18750 --  po_online_report_text_gt
18751 --Locks:
18752 --  None.
18753 --Function:
18754 -- This procedure is used to perform the submission check related to CCR
18755 -- registration status of a vendor site. The resulting error (if any) is
18756 -- stored in the global temp table
18757 --Parameters:
18758 --IN:
18759 --p_document_id
18760 --  po_header_id of a Purchase Order or Purchase Agreement
18761 --p_online_report_id
18762 --  unique error report id
18763 --p_user_id
18764 --  user id
18765 --p_login_id
18766 --  login id
18767 --IN OUT:
18768 --p_sequence
18769 --  maintains a count of total number of submission check errors/warnings
18770 --OUT:
18771 --x_return_status
18772 --  return status of the procedure. Possible values are
18773 --  FND_API.G_RET_STS_SUCCESS and FND_API.G_RET_STS_UNEXP_ERROR
18774 --Notes:
18775 --  None
18776 --Testing:
18777 --  None
18778 --End of Comments
18779 -------------------------------------------------------------------------------
18780 PROCEDURE check_vendor_site_ccr_regis(
18781   p_document_id        IN NUMBER,
18782   p_online_report_id   IN NUMBER,
18783   p_user_id            IN NUMBER,
18784   p_login_id           IN NUMBER,
18785   p_sequence           IN OUT NOCOPY NUMBER,
18786   x_return_status      OUT NOCOPY VARCHAR2)
18787 IS
18788   l_api_name           CONSTANT VARCHAR2(30) := 'CHECK_VENDOR_SITE_CCR_REGIS';
18789   l_progress           VARCHAR2(3);
18790 
18791   l_vendor_id          PO_HEADERS.vendor_id%TYPE;
18792   l_vendor_site_id     PO_HEADERS.vendor_site_id%TYPE;
18793   l_valid_registration BOOLEAN := FALSE;
18794 
18795   l_text_line           PO_ONLINE_REPORT_TEXT.text_line%TYPE := NULL;
18796 
18797 BEGIN
18798 
18799   l_progress := '000';
18800   x_return_status := FND_API.G_RET_STS_SUCCESS;
18801 
18802   IF g_debug_stmt THEN
18803      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
18804        FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
18805              || l_progress, ' Before retrieving vendor/vendor site info');
18806      END IF;
18807   END IF;
18808 
18809   -- SQL What: retrieves vendor id and vendor site id from the document header
18810   -- SQL Why:  need to check vendor site registration status below
18811   BEGIN
18812     SELECT    vendor_id, vendor_site_id
18813     INTO      l_vendor_id, l_vendor_site_id
18814     FROM      po_headers_all
18815     WHERE     po_header_id = p_document_id;
18816   EXCEPTION
18817     WHEN OTHERS THEN
18818       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
18819   END;
18820 
18821   l_progress := '010';
18822   IF g_debug_stmt THEN
18823      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
18824        FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
18825              || l_progress, ' After retrieving vendor/vendor site info');
18826      END IF;
18827   END IF;
18828 
18829   IF (l_vendor_id IS NOT NULL) AND (l_vendor_site_id IS NOT NULL) THEN
18830 
18831      -- Call PO_FV_INTEGRATION_PVT.val_vendor_site_ccr_regis to check the
18832      -- Central Contractor Registration(CCR) status of the vendor site
18833 
18834      l_progress := '020';
18835      IF g_debug_stmt THEN
18836      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
18837        FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
18838          || l_progress, ' Before validating vendor site registration');
18839      END IF;
18840      END IF;
18841 
18842      l_valid_registration := PO_FV_INTEGRATION_PVT.val_vendor_site_ccr_regis(
18843                           p_vendor_id      => l_vendor_id,
18844                           p_vendor_site_id => l_vendor_site_id);
18845 
18846      IF NOT l_valid_registration THEN
18847 
18848         l_progress := '030';
18849         IF g_debug_stmt THEN
18850             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
18851               FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
18852               l_api_name||'.' || l_progress,
18853               ' Vendor site registration is not valid');
18854             END IF;
18855         END IF;
18856 
18857         l_text_line := FND_MESSAGE.get_string('PO', 'PO_VENDOR_SITE_CCR_INVALID');
18858         INSERT INTO po_online_report_text_gt
18859                 (online_report_id
18860                 ,last_updated_by
18861                 ,last_update_date
18862                 ,created_by
18863                 ,creation_date
18864                 ,line_num
18865                 ,shipment_num
18866                 ,distribution_num
18867                 ,sequence
18868                 ,text_line
18869                 ,message_name)
18870          VALUES
18871                 (p_online_report_id
18872                 ,p_login_id
18873                 ,sysdate
18874                 ,p_user_id
18875                 ,sysdate
18876                 ,0 ,0 ,0
18877                 ,p_sequence+1
18878                 ,substr(l_text_line,1,240)
18879                 ,'PO_VENDOR_SITE_CCR_INVALID');
18880 
18881           -- Increment p_sequence by 1
18882           p_sequence := p_sequence+1;
18883      ELSE -- l_valid_registration is TRUE
18884          l_progress := '040';
18885          IF g_debug_stmt THEN
18886             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
18887               FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
18888                 l_api_name||'.' || l_progress,
18889                 ' Vendor site registration is valid');
18890             END IF;
18891          END IF;
18892      END IF; -- l_valid_registration check
18893   END IF; -- l_vendor_id and l_vendor_site_id check
18894 
18895 EXCEPTION
18896   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
18897      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
18898   WHEN OTHERS THEN
18899      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
18900      IF (g_debug_unexp) THEN
18901         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
18902           FND_LOG.string(FND_LOG.level_unexpected,
18903                        G_PKG_NAME||l_api_name|| '.others_exception',
18904                        'EXCEPTION: Location is '|| l_progress ||' SQL CODE is '||sqlcode);
18905         END IF;
18906      END IF;
18907 
18908 END check_vendor_site_ccr_regis;
18909 --<JFMIP Vendor Registration FPJ End>
18910 
18911 
18912 -- <Doc Manager Rewrite 11.5.11 Start>
18913 PROCEDURE check_final_close(
18914    p_document_type        IN VARCHAR2
18915 ,  p_document_subtype     IN VARCHAR2
18916 ,  p_document_level       IN VARCHAR2
18917 ,  p_document_id          IN NUMBER
18918 ,  p_online_report_id     IN NUMBER
18919 ,  p_user_id              IN NUMBER
18920 ,  p_login_id             IN NUMBER
18921 ,  p_origin_doc_id        IN NUMBER := NULL --Bug#5462677
18922 ,  p_doc_level_id         IN NUMBER
18923 ,  p_sequence             IN OUT NOCOPY NUMBER
18924 ,  x_return_status        OUT NOCOPY VARCHAR2
18925 )
18926 IS
18927 
18928 d_module    VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.check_final_close';
18929 d_progress  NUMBER;
18930 d_msg       VARCHAR2(200);
18931 
18932 l_ret_sts   VARCHAR2(1);
18933 
18934 BEGIN
18935 
18936   IF (PO_LOG.d_proc) THEN
18937     PO_LOG.proc_begin(d_module);
18938     PO_LOG.proc_begin(d_module, 'p_document_type', p_document_type);
18939     PO_LOG.proc_begin(d_module, 'p_document_subtype', p_document_subtype);
18940     PO_LOG.proc_begin(d_module, 'p_document_level', p_document_level);
18941     PO_LOG.proc_begin(d_module, 'p_document_id', p_document_id);
18942     PO_LOG.proc_begin(d_module, 'p_online_report_id', p_online_report_id);
18943     PO_LOG.proc_begin(d_module, 'p_user_id', p_user_id);
18944     PO_LOG.proc_begin(d_module, 'p_login_id', p_login_id);
18945     PO_LOG.proc_begin(d_module, 'p_sequence', p_sequence);
18946   END IF;
18947 
18948   BEGIN
18949 
18950     d_progress := 10;
18951 
18952     IF (((p_document_type = g_document_type_PO) AND (p_document_subtype = 'STANDARD'))
18953          OR (p_document_type = g_document_type_RELEASE))
18954     THEN
18955 
18956       d_progress := 20;
18957 
18958       check_rcv_trans_interface(
18959          p_document_type     => p_document_type
18960       ,  p_document_level    => p_document_level
18961       ,  p_online_report_id  => p_online_report_id
18962       ,  p_user_id           => p_user_id
18963       ,  p_login_id          => p_login_id
18964       ,  p_document_id       => p_document_id
18965       ,  p_sequence          => p_sequence
18966       ,  x_return_status     => l_ret_sts
18967       );
18968 
18969       IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
18970         d_msg := 'check_rcv_trans_interface not successful';
18971         d_progress := 30;
18972         RAISE PO_CORE_S.g_early_return_exc;
18973       END IF;
18974 
18975       d_progress := 40;
18976 
18977       check_asn_not_fully_received(
18978          p_document_type     => p_document_type
18979 	  ,  p_document_level    => p_document_level    --<Bug 9012072, Added the p_document_level IN parameter
18980       ,  p_online_report_id  => p_online_report_id
18981       ,  p_user_id           => p_user_id
18982       ,  p_login_id          => p_login_id
18983       ,  p_sequence          => p_sequence
18984       ,  x_return_status     => l_ret_sts
18985       );
18986 
18987       IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
18988         d_msg := 'check_asn_not_fully_received not successful';
18989         d_progress := 50;
18990         RAISE PO_CORE_S.g_early_return_exc;
18991       END IF;
18992 
18993       d_progress := 60;
18994 
18995       check_qty_rcv_but_not_deliv(
18996          p_document_type     => p_document_type
18997       ,  p_online_report_id  => p_online_report_id
18998       ,  p_user_id           => p_user_id
18999       ,  p_login_id          => p_login_id
19000       ,  p_sequence          => p_sequence
19001       ,  x_return_status     => l_ret_sts
19002       );
19003 
19004       IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
19005         d_msg := 'check_qty_rcv_but_not_deliv not successful';
19006         d_progress := 70;
19007         RAISE PO_CORE_S.g_early_return_exc;
19008       END IF;
19009 
19010       d_progress := 80;
19011 
19012       check_amt_rcv_but_not_deliv(
19013          p_document_type     => p_document_type
19014       ,  p_online_report_id  => p_online_report_id
19015       ,  p_user_id           => p_user_id
19016       ,  p_login_id          => p_login_id
19017       ,  p_sequence          => p_sequence
19018       ,  x_return_status     => l_ret_sts
19019       );
19020 
19021       IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
19022         d_msg := 'check_amt_rcv_but_not_deliv not successful';
19023         d_progress := 90;
19024         RAISE PO_CORE_S.g_early_return_exc;
19025       END IF;
19026 
19027       d_progress := 100;
19028 
19029       check_invalid_acct_flex(
19030          p_document_type     => p_document_type
19031       ,  p_action_requested  => g_action_FINAL_CLOSE_CHECK
19032       ,  p_action_date       => SYSDATE
19033       ,  p_online_report_id  => p_online_report_id
19034       ,  p_user_id           => p_user_id
19035       ,  p_login_id          => p_login_id
19036       ,  p_document_id       => p_document_id
19037       ,  p_sequence          => p_sequence
19038       ,  x_return_status     => l_ret_sts
19039       );
19040 
19041       IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
19042         d_msg := 'check_invalid_acct_flex not successful';
19043         d_progress := 110;
19044         RAISE PO_CORE_S.g_early_return_exc;
19045       END IF;
19046 
19047       -- Bug#5462677
19048       d_progress := 100;
19049 
19050       check_unvalidated_invoices(
19051          p_document_type     => p_document_type
19052       ,  p_document_subtype  => p_document_subtype
19053       ,  p_action_requested  => g_action_FINAL_CLOSE_CHECK
19054       ,  p_action_date       => SYSDATE
19055       ,  p_online_report_id  => p_online_report_id
19056       ,  p_user_id           => p_user_id
19057       ,  p_login_id          => p_login_id
19058       ,  p_document_level    => p_document_level --Bug#5462677
19059       ,  p_origin_doc_id     => p_origin_doc_id  --Bug#5462677
19060       ,  p_doc_level_id      => p_doc_level_id
19061       ,  p_sequence          => p_sequence
19062       ,  x_return_status     => l_ret_sts
19063       );
19064 
19065       IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
19066         d_msg := 'check_unvalidated_invoices not successful';
19067         d_progress := 110;
19068         RAISE PO_CORE_S.g_early_return_exc;
19069       END IF;
19070 
19071     ELSIF ((p_document_type = g_document_type_PO) AND (p_document_subtype = 'PLANNED'))
19072     THEN
19073 
19074       d_progress := 200;
19075 
19076       check_invalid_acct_flex(
19077          p_document_type     => p_document_type
19078       ,  p_action_requested  => g_action_FINAL_CLOSE_CHECK
19079       ,  p_action_date       => SYSDATE
19080       ,  p_online_report_id  => p_online_report_id
19081       ,  p_user_id           => p_user_id
19082       ,  p_login_id          => p_login_id
19083       ,  p_document_id       => p_document_id
19084       ,  p_sequence          => p_sequence
19085       ,  x_return_status     => l_ret_sts
19086       );
19087 
19088       IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
19089         d_msg := 'check_invalid_acct_flex not successful';
19090         d_progress := 210;
19091         RAISE PO_CORE_S.g_early_return_exc;
19092       END IF;
19093 
19094       d_progress := 220;
19095 
19096       check_ppo_has_open_release(
19097          p_online_report_id  => p_online_report_id
19098       ,  p_user_id           => p_user_id
19099       ,  p_login_id          => p_login_id
19100       ,  p_sequence          => p_sequence
19101       ,  x_return_status     => l_ret_sts
19102       );
19103 
19104       IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
19105         d_msg := 'check_ppo_has_open_release not successful';
19106         d_progress := 230;
19107         RAISE PO_CORE_S.g_early_return_exc;
19108       END IF;
19109 
19110     ELSIF ((p_document_type = g_document_type_PA) AND (p_document_subtype = 'BLANKET'))
19111     THEN
19112 
19113       d_progress := 300;
19114 
19115       check_bpa_has_open_release(
19116          p_online_report_id  => p_online_report_id
19117       ,  p_user_id           => p_user_id
19118       ,  p_login_id          => p_login_id
19119       ,  p_sequence          => p_sequence
19120       ,  x_return_status     => l_ret_sts
19121       );
19122 
19123       IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
19124         d_msg := 'check_bpa_has_open_release not successful';
19125         d_progress := 310;
19126         RAISE PO_CORE_S.g_early_return_exc;
19127       END IF;
19128 
19129       d_progress := 320;
19130 
19131       check_bpa_has_open_stdref(
19132          p_online_report_id  => p_online_report_id
19133       ,  p_user_id           => p_user_id
19134       ,  p_login_id          => p_login_id
19135       ,  p_sequence          => p_sequence
19136       ,  x_return_status     => l_ret_sts
19137       );
19138 
19139       IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
19140         d_msg := 'check_bpa_has_open_stdref not successful';
19141         d_progress := 330;
19142         RAISE PO_CORE_S.g_early_return_exc;
19143       END IF;
19144 
19145     ELSIF ((p_document_type = g_document_type_PA) AND (p_document_subtype = 'CONTRACT'))
19146     THEN
19147 
19148       d_progress := 400;
19149 
19150       check_cpa_has_open_stdref(
19151          p_online_report_id  => p_online_report_id
19152       ,  p_user_id           => p_user_id
19153       ,  p_login_id          => p_login_id
19154       ,  p_sequence          => p_sequence
19155       ,  x_return_status     => l_ret_sts
19156       );
19157 
19158       IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
19159         d_msg := 'check_cpa_has_open_stdref not successful';
19160         d_progress := 410;
19161         RAISE PO_CORE_S.g_early_return_exc;
19162       END IF;
19163 
19164     END IF;  -- p_document_type = ...
19165 
19166     l_ret_sts := FND_API.G_RET_STS_SUCCESS;
19167 
19168   EXCEPTION
19169     WHEN PO_CORE_S.g_early_return_exc THEN
19170       IF (PO_LOG.d_exc) THEN
19171         PO_LOG.exc(d_module, d_progress, d_msg);
19172       END IF;
19173       l_ret_sts := FND_API.G_RET_STS_UNEXP_ERROR;
19174   END;
19175 
19176   x_return_status := l_ret_sts;
19177 
19178   IF (PO_LOG.d_proc) THEN
19179     PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
19180     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
19181     PO_LOG.proc_end(d_module);
19182   END IF;
19183 
19184 EXCEPTION
19185 
19186   WHEN others THEN
19187     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
19188 
19189     IF (PO_LOG.d_exc) THEN
19190       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
19191       PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
19192       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
19193       PO_LOG.proc_end(d_module);
19194     END IF;
19195 
19196     RETURN;
19197 
19198 END check_final_close;
19199 
19200 
19201 -- Determine if there are any receiving transactions in the
19202 -- receiving interface that have not yet been processed
19203 PROCEDURE check_rcv_trans_interface(
19204    p_document_type        IN VARCHAR2
19205 ,  p_document_level       IN VARCHAR2  --<Bug 4118145, Issue 7>: Corrected type
19206 ,  p_online_report_id     IN NUMBER
19207 ,  p_user_id              IN NUMBER
19208 ,  p_login_id             IN NUMBER
19209 ,  p_document_id          IN NUMBER
19210 ,  p_sequence             IN OUT NOCOPY NUMBER
19211 ,  x_return_status        OUT NOCOPY VARCHAR2
19212 )
19213 IS
19214 
19215 l_textline  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
19216 l_ret_sts   VARCHAR2(1);
19217 l_is_complex_po     boolean;
19218 l_token_value VARCHAR2(256);
19219 
19220 d_module   VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.check_rcv_trans_interface';
19221 d_progress NUMBER;
19222 d_msg      VARCHAR2(60);
19223 
19224 
19225 BEGIN
19226 
19227   d_progress := 0;
19228   IF (PO_LOG.d_proc) THEN
19229     PO_LOG.proc_begin(d_module);
19230     PO_LOG.proc_begin(d_module, 'p_document_type', p_document_type);
19231     PO_LOG.proc_begin(d_module, 'p_document_level', p_document_level);
19232     PO_LOG.proc_begin(d_module, 'p_online_report_id', p_online_report_id);
19233     PO_LOG.proc_begin(d_module, 'p_user_id', p_user_id);
19234     PO_LOG.proc_begin(d_module, 'p_login_id', p_login_id);
19235     PO_LOG.proc_begin(d_module, 'p_sequence', p_sequence);
19236   END IF;
19237 
19238   BEGIN
19239 
19240     IF (p_document_level = g_document_level_HEADER)
19241     THEN
19242 
19243       IF (p_document_type <> g_document_type_RELEASE)
19244       THEN
19245 
19246         d_progress := 10;
19247 
19248         l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_POH_WITH_RCV_TRX');
19249 
19250         INSERT INTO PO_ONLINE_REPORT_TEXT_GT
19251         (  online_report_id
19252         ,  last_update_login
19253         ,  last_updated_by
19254         ,  last_update_date
19255         ,  created_by
19256         ,  creation_date
19257         ,  line_num
19258         ,  shipment_num
19259         ,  distribution_num
19260         ,  sequence
19261         ,  text_line
19262         ,  message_name
19263         )
19264         SELECT
19265            p_online_report_id
19266         ,  p_login_id
19267         ,  p_user_id
19268         ,  SYSDATE
19269         ,  p_user_id
19270         ,  SYSDATE
19271         ,  0
19272         ,  0
19273         ,  0
19274         ,  p_sequence + ROWNUM
19275         ,  substr(l_textline, 1, 240)
19276         ,  'PO_CAN_POH_WITH_RCV_TRX'
19277         FROM po_headers_gt poh
19278         WHERE EXISTS
19279           (
19280             SELECT 'Eligible shipment'
19281             FROM po_line_locations_gt poll
19282             WHERE poll.po_header_id = poh.po_header_id
19283               AND NVL(poll.cancel_flag, 'N') = 'N'
19284               AND NVL(poll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
19285           )
19286           AND EXISTS
19287           (
19288             SELECT 'Transaction to process'
19289             FROM rcv_transactions_interface rti
19290             WHERE rti.processing_status_code = 'PENDING'
19291               AND rti.po_header_id = poh.po_header_id
19292           );
19293 
19294         d_progress := 15;
19295 
19296       ELSE
19297 
19298         d_progress := 20;
19299 
19300         l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_REL_WITH_RCV_TRX');
19301 
19302         INSERT INTO PO_ONLINE_REPORT_TEXT_GT
19303         (  online_report_id
19304         ,  last_update_login
19305         ,  last_updated_by
19306         ,  last_update_date
19307         ,  created_by
19308         ,  creation_date
19309         ,  line_num
19310         ,  shipment_num
19311         ,  distribution_num
19312         ,  sequence
19313         ,  text_line
19314         ,  message_name
19315         )
19316         SELECT
19317            p_online_report_id
19318         ,  p_login_id
19319         ,  p_user_id
19320         ,  SYSDATE
19321         ,  p_user_id
19322         ,  SYSDATE
19323         ,  0
19324         ,  0
19325         ,  0
19326         ,  p_sequence + ROWNUM
19327         ,  substr(l_textline, 1, 240)
19328         ,  'PO_CAN_REL_WITH_RCV_TRX'
19329         FROM po_releases_gt por
19330         WHERE EXISTS
19331           (
19332             SELECT 'Eligible shipment'
19333             FROM po_line_locations_gt poll
19334             WHERE poll.po_release_id = por.po_release_id
19335               AND NVL(poll.cancel_flag, 'N') = 'N'
19336               AND NVL(poll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
19337           )
19338           AND EXISTS
19339           (
19340             SELECT 'Transaction to process'
19341             FROM rcv_transactions_interface rti
19342             WHERE rti.processing_status_code = 'PENDING'
19343               AND rti.po_release_id = por.po_release_id
19344           );
19345 
19346         d_progress := 25;
19347 
19348       END IF;  -- p_document_type = ...
19349 
19350     ELSIF (p_document_level = g_document_level_LINE)
19351     THEN
19352 
19353       d_progress := 30;
19354 
19355       l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_POL_WITH_RCV_TRX');
19356 
19357       INSERT INTO PO_ONLINE_REPORT_TEXT_GT
19358       (  online_report_id
19359       ,  last_update_login
19360       ,  last_updated_by
19361       ,  last_update_date
19362       ,  created_by
19363       ,  creation_date
19364       ,  line_num
19365       ,  shipment_num
19366       ,  distribution_num
19367       ,  sequence
19368       ,  text_line
19369       ,  message_name
19370       )
19371       SELECT
19372          p_online_report_id
19373       ,  p_login_id
19374       ,  p_user_id
19375       ,  SYSDATE
19376       ,  p_user_id
19377       ,  SYSDATE
19378       ,  pol.line_num
19379       ,  0
19380       ,  0
19381       ,  p_sequence + ROWNUM
19382       ,  substr(g_linemsg || g_delim || pol.line_num || g_delim || l_textline, 1, 240)
19383       ,  'PO_CAN_POL_WITH_RCV_TRX'
19384       FROM po_lines_gt pol
19385       WHERE EXISTS
19386         (
19387           SELECT 'Eligible shipment'
19388           FROM po_line_locations_gt poll
19389           WHERE poll.po_line_id = pol.po_line_id
19390             AND NVL(poll.cancel_flag, 'N') = 'N'
19391             AND NVL(poll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
19392         )
19393         AND EXISTS
19394         (
19395           SELECT 'Transaction to process'
19396           FROM rcv_transactions_interface rti
19397           WHERE rti.processing_status_code = 'PENDING'
19398             AND rti.po_line_id = pol.po_line_id
19399         );
19400 
19401       d_progress := 40;
19402 
19403     ELSIF (p_document_level = g_document_level_SHIPMENT)
19404     THEN
19405 
19406       d_progress := 50;
19407 
19408       IF (p_document_type <> g_document_type_RELEASE)
19409       THEN
19410         l_is_complex_po := PO_COMPLEX_WORK_PVT.is_complex_work_po(p_document_id);
19411 
19412         IF (l_is_complex_po) THEN
19413           l_token_value := FND_MESSAGE.GET_STRING('PO', 'PO_LINE_LOC_TYPE_LOW_P_PAYITEM');
19414         ELSE
19415           l_token_value := FND_MESSAGE.GET_STRING('PO', 'PO_LINE_LOC_TYPE_LOWER_S_SCH');
19416         END IF;
19417 
19418         FND_MESSAGE.SET_NAME('PO','PO_CAN_POLL_WITH_RCV_TRX');
19419         FND_MESSAGE.SET_TOKEN('LINE_LOCATION_TYPE', l_token_value);
19420 
19421         l_textline := FND_MESSAGE.GET;
19422       ELSE
19423         l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_RELS_WITH_RCV_TRX');
19424       END IF;
19425 /*Bug 9012072 START--->Determine if there are any receiving transactions that have not been fully received for the shipments
19426        of the PO or Release Shipments being finally closed*/
19427 
19428 	  IF (p_document_type <> g_document_type_RELEASE)
19429       THEN
19430 		  INSERT INTO PO_ONLINE_REPORT_TEXT_GT
19431 		  (  online_report_id
19432 		  ,  last_update_login
19433 		  ,  last_updated_by
19434 		  ,  last_update_date
19435 		  ,  created_by
19436 		  ,  creation_date
19437 		  ,  line_num
19438 		  ,  shipment_num
19439 		  ,  distribution_num
19440 		  ,  sequence
19441 		  ,  text_line
19442 		  ,  message_name
19443 		  )
19444 		  SELECT
19445 			 p_online_report_id
19446 		  ,  p_login_id
19447 		  ,  p_user_id
19448 		  ,  SYSDATE
19449 		  ,  p_user_id
19450 		  ,  SYSDATE
19451 		  ,  DECODE(p_document_type, g_document_type_RELEASE, 0, pol.line_num)
19452 		  ,  poll.shipment_num
19453 		  ,  0
19454 		  ,  p_sequence + ROWNUM
19455 		  ,  DECODE(p_document_type,
19456 			   g_document_type_RELEASE, substr(g_shipmsg || g_delim || poll.shipment_num
19457 												|| g_delim || l_textline, 1, 240),
19458 			   substr(g_linemsg || g_delim || pol.line_num || g_delim || g_shipmsg || g_delim
19459 					  || poll.shipment_num || g_delim || l_textline, 1, 240)
19460 			 )
19461 		  ,  DECODE(p_document_type, g_document_type_RELEASE, 'PO_CAN_RELS_WITH_RCV_TRX',
19462 					 'PO_CAN_POLL_WITH_RCV_TRX')
19463 		  FROM po_lines_gt pol, po_line_locations_gt poll
19464 		  WHERE pol.po_line_id = poll.po_line_id
19465 			AND NVL(poll.cancel_flag, 'N') = 'N'
19466 			AND NVL(poll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
19467 			AND EXISTS
19468 			(
19469 			  SELECT 'Transaction to process'
19470 			  FROM rcv_transactions_interface rti
19471 			  WHERE rti.processing_status_code = 'PENDING'
19472 				AND rti.po_line_location_id = poll.line_location_id
19473 			);
19474 
19475 	  ELSE
19476 		INSERT INTO PO_ONLINE_REPORT_TEXT_GT
19477         (  online_report_id
19478         ,  last_update_login
19479         ,  last_updated_by
19480         ,  last_update_date
19481         ,  created_by
19482         ,  creation_date
19483         ,  line_num
19484         ,  shipment_num
19485         ,  distribution_num
19486         ,  sequence
19487         ,  text_line
19488         ,  message_name
19489         )
19490         SELECT
19491            p_online_report_id
19492         ,  p_login_id
19493         ,  p_user_id
19494         ,  SYSDATE
19495         ,  p_user_id
19496         ,  SYSDATE
19497         ,  0
19498         ,  poll.shipment_num
19499         ,  0
19500         ,  p_sequence + ROWNUM
19501         ,  substr(g_shipmsg || g_delim || poll.shipment_num || g_delim || l_textline, 1, 240)
19502         ,  'PO_CAN_RELS_WITH_RCV_TRX'
19503         FROM    po_line_locations_gt poll
19504         WHERE   EXISTS
19505                (
19506                   SELECT 'Eligible shipment'
19507                   FROM    po_releases_gt por
19508                   WHERE   por.po_release_id = poll.po_release_id
19509                           AND NVL(por.cancel_flag, 'N') = 'N'
19510                           AND NVL(por.closed_code, 'OPEN') <> 'FINALLY CLOSED'
19511                )
19512                AND EXISTS
19513                (
19514                  SELECT 'Transaction to process'
19515                  FROM   rcv_transactions_interface rti
19516                  WHERE  rti.processing_status_code = 'PENDING'
19517                         AND rti.po_line_location_id = poll.line_location_id
19518                );
19519 
19520       END IF;
19521 
19522       /*Bug 9012072 END--->Determine if there are any receiving transactions that have not been fully received for the shipments
19523        of the PO or Release Shipments being finally closed*/
19524 
19525 
19526       d_progress := 60;
19527 
19528     ELSE
19529 
19530       d_progress := 70;
19531       d_msg := 'Bad document level';
19532       l_ret_sts := FND_API.G_RET_STS_UNEXP_ERROR;
19533       RAISE PO_CORE_S.g_early_return_exc;
19534 
19535     END IF;  -- if p_document_level = ...
19536 
19537     p_sequence := p_sequence + SQL%ROWCOUNT;
19538     l_ret_sts := FND_API.G_RET_STS_SUCCESS;
19539 
19540   EXCEPTION
19541     WHEN PO_CORE_S.g_early_return_exc THEN
19542       IF (PO_LOG.d_exc) THEN
19543         PO_LOG.exc(d_module, d_progress, d_msg);
19544       END IF;
19545   END;
19546 
19547   x_return_status := l_ret_sts;
19548 
19549   IF (PO_LOG.d_proc) THEN
19550     PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
19551     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
19552     PO_LOG.proc_end(d_module);
19553   END IF;
19554 
19555   RETURN;
19556 
19557 EXCEPTION
19558   WHEN others THEN
19559     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
19560 
19561     IF (PO_LOG.d_exc) THEN
19562       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
19563       PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
19564       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
19565       PO_LOG.proc_end(d_module);
19566     END IF;
19567 
19568     RETURN;
19569 
19570 END check_rcv_trans_interface;
19571 
19572 -- Deterimine if there are any ASN that have not been fully received
19573 /*Bug 9012072 START--->Modified the check_asn_not_fully_received procedure*/
19574 PROCEDURE check_asn_not_fully_received(
19575    p_document_type        IN VARCHAR2
19576 ,  p_document_level       IN VARCHAR2
19577 ,  p_online_report_id     IN NUMBER
19578 ,  p_user_id              IN NUMBER
19579 ,  p_login_id             IN NUMBER
19580 ,  p_sequence             IN OUT NOCOPY NUMBER
19581 ,  x_return_status        OUT NOCOPY VARCHAR2
19582 )
19583 IS
19584 l_textline  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;  --Bug9012072
19585 l_ret_sts   VARCHAR2(1);	--Bug9012072
19586 d_msg      VARCHAR2(60);	--Bug9012072
19587 
19588 
19589 l_text_normal_po  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
19590 l_text_complex_po  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
19591 
19592 d_module   VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.check_asn_not_fully_received';
19593 d_progress NUMBER;
19594 
19595 
19596 BEGIN
19597 
19598   d_progress := 0;
19599   IF (PO_LOG.d_proc) THEN
19600     PO_LOG.proc_begin(d_module);
19601     PO_LOG.proc_begin(d_module, 'p_document_type', p_document_type);
19602     PO_LOG.proc_begin(d_module, 'p_online_report_id', p_online_report_id);
19603     PO_LOG.proc_begin(d_module, 'p_user_id', p_user_id);
19604     PO_LOG.proc_begin(d_module, 'p_login_id', p_login_id);
19605     PO_LOG.proc_begin(d_module, 'p_sequence', p_sequence);
19606   END IF;
19607  d_progress := 5;
19608 
19609  /*Bug 9012072 START Determine if there are any ASNs that have not been fully received for the shipments of the
19610   PO or Release being finally closed.*/
19611    IF (p_document_level = g_document_level_HEADER)
19612     THEN
19613 
19614       IF (p_document_type <> g_document_type_RELEASE)
19615       THEN
19616 		  d_progress := 10;
19617 
19618 		  l_text_normal_po := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_POLL_WITH_ASN');
19619 		  l_text_complex_po := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_POLL_WITH_PENDING_WCR');
19620 
19621 		  --<Complex Work R12>: change message to use Work Confirmation instead of ASN for Complex Work
19622 		  INSERT INTO PO_ONLINE_REPORT_TEXT_GT
19623 		  (  online_report_id
19624 		  ,  last_update_login
19625 		  ,  last_updated_by
19626 		  ,  last_update_date
19627 		  ,  created_by
19628 		  ,  creation_date
19629 		  ,  line_num
19630 		  ,  shipment_num
19631 		  ,  distribution_num
19632 		  ,  sequence
19633 		  ,  text_line
19634 		  ,  message_name
19635 		  )
19636 		  SELECT
19637 			 p_online_report_id
19638 		  ,  p_login_id
19639 		  ,  p_user_id
19640 		  ,  SYSDATE
19641 		  ,  p_user_id
19642 		  ,  SYSDATE
19643 		  ,  DECODE(p_document_type, g_document_type_RELEASE, 0, pol.line_num)
19644 		  ,  poll.shipment_num
19645 		  ,  0
19646 		  ,  p_sequence + ROWNUM
19647 		  ,  DECODE(p_document_type, g_document_type_RELEASE,
19648 					  substr(g_shipmsg || g_delim || poll.shipment_num || g_delim || l_text_normal_po, 1, 240),
19649 					  substr(g_linemsg || g_delim || pol.line_num || g_delim || g_shipmsg || g_delim
19650 							 || poll.shipment_num || g_delim ||
19651 							 DECODE(poll.payment_type , NULL, l_text_normal_po, l_text_complex_po), 1, 240))
19652 		  ,  DECODE(poll.payment_type, NULL, 'PO_CAN_POLL_WITH_ASN', 'PO_CAN_POLL_WITH_PENDING_WCR')
19653 		  FROM po_lines_gt pol, po_line_locations_gt poll
19654 		  WHERE NVL(poll.cancel_flag, 'N') = 'N'
19655 			AND NVL(poll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
19656 			AND EXISTS
19657 			 (
19658 			   SELECT 'ASN outstanding'
19659 			   FROM rcv_shipment_lines rsl
19660 			   WHERE rsl.po_line_location_id = poll.line_location_id
19661 				 AND NVL(rsl.quantity_shipped, 0) > NVL(rsl.quantity_received, 0)
19662 				 AND NVL(rsl.asn_line_flag, 'N') = 'Y'
19663 				 AND NVL(rsl.shipment_line_status_code, 'EXPECTED') <> 'CANCELLED'
19664 			 );
19665 
19666 			 d_progress := 15;
19667 
19668       ELSE
19669 			d_progress := 20;
19670 
19671 			l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_POLL_WITH_ASN');
19672 
19673 			INSERT INTO PO_ONLINE_REPORT_TEXT_GT
19674 			(  online_report_id
19675 			,  last_update_login
19676 			,  last_updated_by
19677 			,  last_update_date
19678 			,  created_by
19679 			,  creation_date
19680 			,  line_num
19681 			,  shipment_num
19682 			,  distribution_num
19683 			,  sequence
19684 			,  text_line
19685 			,  message_name
19686 			)
19687 			SELECT
19688 			   p_online_report_id
19689 			,  p_login_id
19690 			,  p_user_id
19691 			,  SYSDATE
19692 			,  p_user_id
19693 			,  SYSDATE
19694 			,  0
19695 			,  0
19696 			,  0
19697 			,  p_sequence + ROWNUM
19698 			,  substr(g_shipmsg || g_delim || poll.shipment_num || g_delim || l_textline, 1, 240)
19699 			,  'PO_CAN_POLL_WITH_ASN'
19700 		FROM po_releases_gt por, po_line_locations_gt poll
19701 			WHERE poll.po_release_id = por.po_release_id
19702 		AND NVL(poll.cancel_flag, 'N') = 'N'
19703 			AND NVL(poll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
19704 			AND EXISTS
19705 			  (
19706 				SELECT 'ASN outstanding'
19707 				FROM rcv_shipment_lines rsl
19708 				WHERE rsl.po_release_id = por.po_release_id
19709 					  AND NVL(rsl.quantity_shipped, 0) > NVL(rsl.quantity_received, 0)
19710 					  AND NVL(rsl.asn_line_flag, 'N') = 'Y'
19711 					  AND NVL(rsl.shipment_line_status_code, 'EXPECTED') <> 'CANCELLED'
19712 			  );
19713 
19714 			d_progress := 25;
19715 
19716       END IF;  -- p_document_type = ...
19717 
19718 
19719   /*Bug 9012072 END Determine if there are any ASNs that have not been fully received for the shipments of the
19720          PO or Release being finally closed.*/
19721 
19722       /*Bug 9012072 START Determine if there are any ASNs that have not been fully received for the shipments of the
19723         PO Lines being finally closed.*/
19724 
19725 	ELSIF (p_document_level = g_document_level_LINE)
19726     THEN
19727 
19728       d_progress := 30;
19729 
19730          l_text_normal_po := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_POLL_WITH_ASN');
19731          l_text_complex_po := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_POLL_WITH_PENDING_WCR');
19732 
19733         INSERT INTO PO_ONLINE_REPORT_TEXT_GT
19734         (  online_report_id
19735         ,  last_update_login
19736         ,  last_updated_by
19737         ,  last_update_date
19738         ,  created_by
19739         ,  creation_date
19740         ,  line_num
19741         ,  shipment_num
19742         ,  distribution_num
19743         ,  sequence
19744         ,  text_line
19745         ,  message_name
19746         )
19747         SELECT
19748            p_online_report_id
19749         ,  p_login_id
19750         ,  p_user_id
19751         ,  SYSDATE
19752         ,  p_user_id
19753         ,  SYSDATE
19754         ,  pol.line_num
19755         ,  0
19756         ,  0
19757         ,  p_sequence + ROWNUM
19758         ,  substr(g_linemsg || g_delim || pol.line_num || g_delim || g_shipmsg || g_delim
19759                        || poll.shipment_num || g_delim ||
19760                        DECODE(poll.payment_type , NULL, l_text_normal_po, l_text_complex_po), 1, 240)
19761         ,  DECODE(poll.payment_type, NULL, 'PO_CAN_POLL_WITH_ASN', 'PO_CAN_POLL_WITH_PENDING_WCR')
19762         FROM po_lines_gt pol, po_line_locations_gt poll
19763         WHERE poll.po_line_id = pol.po_line_id
19764 	AND NVL(poll.cancel_flag, 'N') = 'N'
19765         AND NVL(poll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
19766         AND EXISTS
19767           (
19768             SELECT 'ASN outstanding'
19769             FROM rcv_shipment_lines rsl
19770             WHERE rsl.po_line_id = pol.po_line_id
19771                   AND NVL(rsl.quantity_shipped, 0) > NVL(rsl.quantity_received, 0)
19772                   AND NVL(rsl.asn_line_flag, 'N') = 'Y'
19773                   AND NVL(rsl.shipment_line_status_code, 'EXPECTED') <> 'CANCELLED'
19774                    );
19775 
19776     /*Bug 9012072 END Determine if there are any ASNs that have not been fully received for the shipments of the
19777       PO Lines being finally closed.*/
19778 
19779       d_progress := 40;
19780 
19781     ELSIF (p_document_level = g_document_level_SHIPMENT)
19782     THEN
19783 
19784       d_progress := 50;
19785 
19786       /*Bug 9012072 START Determine if there are any ASNs that have not been fully received for the shipments of the
19787         PO or Release Shipments being finally closed.*/
19788 
19789       IF (p_document_type <> g_document_type_RELEASE)
19790       THEN
19791 
19792          l_text_normal_po := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_POLL_WITH_ASN');
19793          l_text_complex_po := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_POLL_WITH_PENDING_WCR');
19794 
19795         INSERT INTO PO_ONLINE_REPORT_TEXT_GT
19796         (  online_report_id
19797         ,  last_update_login
19798         ,  last_updated_by
19799         ,  last_update_date
19800         ,  created_by
19801         ,  creation_date
19802         ,  line_num
19803         ,  shipment_num
19804         ,  distribution_num
19805         ,  sequence
19806         ,  text_line
19807         ,  message_name
19808         )
19809         SELECT
19810            p_online_report_id
19811         ,  p_login_id
19812         ,  p_user_id
19813         ,  SYSDATE
19814         ,  p_user_id
19815         ,  SYSDATE
19816         ,  pol.line_num
19817         ,  poll.shipment_num
19818         ,  0
19819         ,  p_sequence + ROWNUM
19820         ,  substr(g_linemsg || g_delim || pol.line_num || g_delim || g_shipmsg || g_delim
19821                        || poll.shipment_num || g_delim ||
19822                        DECODE(poll.payment_type , NULL, l_text_normal_po, l_text_complex_po), 1, 240)
19823         ,  DECODE(poll.payment_type, NULL, 'PO_CAN_POLL_WITH_ASN', 'PO_CAN_POLL_WITH_PENDING_WCR')
19824         FROM po_lines_gt pol, po_line_locations_gt poll
19825         WHERE poll.po_line_id = pol.po_line_id
19826 	AND NVL(poll.cancel_flag, 'N') = 'N'
19827         AND NVL(poll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
19828         AND EXISTS
19829           (
19830             SELECT 'ASN outstanding'
19831             FROM rcv_shipment_lines rsl
19832             WHERE rsl.po_line_location_id = poll.line_location_id
19833                   AND NVL(rsl.quantity_shipped, 0) > NVL(rsl.quantity_received, 0)
19834                   AND NVL(rsl.asn_line_flag, 'N') = 'Y'
19835                   AND NVL(rsl.shipment_line_status_code, 'EXPECTED') <> 'CANCELLED'
19836                    );
19837 
19838 
19839 
19840       ELSE
19841 
19842       l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_POLL_WITH_ASN');
19843 
19844         INSERT INTO PO_ONLINE_REPORT_TEXT_GT
19845         (  online_report_id
19846         ,  last_update_login
19847         ,  last_updated_by
19848         ,  last_update_date
19849         ,  created_by
19850         ,  creation_date
19851         ,  line_num
19852         ,  shipment_num
19853         ,  distribution_num
19854         ,  sequence
19855         ,  text_line
19856         ,  message_name
19857         )
19858         SELECT
19859            p_online_report_id
19860         ,  p_login_id
19861         ,  p_user_id
19862         ,  SYSDATE
19863         ,  p_user_id
19864         ,  SYSDATE
19865         ,  0
19866         ,  poll.shipment_num
19867         ,  0
19868         ,  p_sequence + ROWNUM
19869         ,  substr(g_shipmsg || g_delim || poll.shipment_num || g_delim || l_textline, 1, 240)
19870         ,  'PO_CAN_POLL_WITH_ASN'
19871         FROM    po_line_locations_gt poll
19872         WHERE   EXISTS
19873                (
19874                   SELECT 'Eligible shipment'
19875                   FROM    po_releases_gt por
19876                   WHERE   por.po_release_id = poll.po_release_id
19877                           AND NVL(por.cancel_flag, 'N') = 'N'
19878                           AND NVL(por.closed_code, 'OPEN') <> 'FINALLY CLOSED'
19879                )
19880                AND EXISTS
19881                (
19882                  SELECT 'ASN outstanding'
19883                  FROM rcv_shipment_lines rsl
19884                  WHERE rsl.po_line_location_id = poll.line_location_id
19885                       AND NVL(rsl.quantity_shipped, 0) > NVL(rsl.quantity_received, 0)
19886                       AND NVL(rsl.asn_line_flag, 'N') = 'Y'
19887                       AND NVL(rsl.shipment_line_status_code, 'EXPECTED') <> 'CANCELLED'
19888                );
19889 
19890       END IF;
19891 
19892  /*Bug 9012072 END Determine if there are any ASNs that have not been fully received for the shipments of the
19893    PO or Release Shipments being finally closed.*/
19894 
19895     d_progress := 60;
19896 
19897     ELSE
19898 
19899       d_progress := 70;
19900       d_msg := 'Bad document level';
19901       l_ret_sts := FND_API.G_RET_STS_UNEXP_ERROR;
19902       RAISE PO_CORE_S.g_early_return_exc;
19903 
19904     END IF;  -- if p_document_level = ...
19905 
19906   d_progress := 80;
19907 
19908   p_sequence := p_sequence + SQL%ROWCOUNT;
19909   x_return_status := FND_API.G_RET_STS_SUCCESS;
19910 
19911   IF (PO_LOG.d_proc) THEN
19912     PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
19913     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
19914     PO_LOG.proc_end(d_module);
19915   END IF;
19916 
19917   RETURN;
19918 
19919 EXCEPTION
19920   WHEN others THEN
19921     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
19922 
19923     IF (PO_LOG.d_exc) THEN
19924       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
19925       PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
19926       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
19927       PO_LOG.proc_end(d_module);
19928     END IF;
19929 
19930     RETURN;
19931 
19932 END check_asn_not_fully_received;
19933 /*Bug 9012072 START--->Modified the check_asn_not_fully_received procedure*/
19934 
19935 -- Determine if there is any shipment being finally closed that
19936 -- has received more than has been delivered to its distributions.
19937 -- Does not check ('RATE', 'FIXED PRICE') lines
19938 PROCEDURE check_qty_rcv_but_not_deliv(
19939    p_document_type        IN VARCHAR2
19940 ,  p_online_report_id     IN NUMBER
19941 ,  p_user_id              IN NUMBER
19942 ,  p_login_id             IN NUMBER
19943 ,  p_sequence             IN OUT NOCOPY NUMBER
19944 ,  x_return_status        OUT NOCOPY VARCHAR2
19945 )
19946 IS
19947 
19948 l_textline  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
19949 
19950 d_module   VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.check_qty_rcv_but_not_deliv';
19951 d_progress NUMBER;
19952 
19953 
19954 BEGIN
19955 
19956   d_progress := 0;
19957   IF (PO_LOG.d_proc) THEN
19958     PO_LOG.proc_begin(d_module);
19959     PO_LOG.proc_begin(d_module, 'p_document_type', p_document_type);
19960     PO_LOG.proc_begin(d_module, 'p_online_report_id', p_online_report_id);
19961     PO_LOG.proc_begin(d_module, 'p_user_id', p_user_id);
19962     PO_LOG.proc_begin(d_module, 'p_login_id', p_login_id);
19963     PO_LOG.proc_begin(d_module, 'p_sequence', p_sequence);
19964   END IF;
19965 
19966   d_progress := 10;
19967 
19968   INSERT INTO PO_ONLINE_REPORT_TEXT_GT
19969   (  online_report_id
19970   ,  last_update_login
19971   ,  last_updated_by
19972   ,  last_update_date
19973   ,  created_by
19974   ,  creation_date
19975   ,  line_num
19976   ,  shipment_num
19977   ,  distribution_num
19978   ,  sequence
19979   ,  text_line
19980   ,  message_name
19981   )
19982   SELECT
19983      p_online_report_id
19984   ,  p_login_id
19985   ,  p_user_id
19986   ,  SYSDATE
19987   ,  p_user_id
19988   ,  SYSDATE
19989   ,  DECODE(p_document_type, g_document_type_RELEASE, 0, pol.line_num)
19990   ,  poll.shipment_num
19991   ,  0
19992   ,  p_sequence + ROWNUM
19993   ,  substr(
19994        DECODE(p_document_type, g_document_type_RELEASE,
19995          g_shipmsg || g_delim || poll.shipment_num || g_delim
19996            || PO_CORE_S.get_translated_text('PO_CAN_POLL_REC_NOT_DEL'
19997                                              , 'QTY2', round(NVL(poll.quantity_received, 0),5) /* Bug:13427569 rounded to 5 digits */
19998                                             , 'QTY1', round(sum(NVL(pod.quantity_delivered, 0)),5) /* Bug:13427569 rounded to 5 digits */
19999                                             ),
20000          g_linemsg || g_delim || pol.line_num || g_delim || g_shipmsg || g_delim
20001            || poll.shipment_num || g_delim
20002            || PO_CORE_S.get_translated_text('PO_CAN_POLL_REC_NOT_DEL'
20003                                              , 'QTY2', round(NVL(poll.quantity_received, 0),5) /* Bug:13427569 rounded to 5 digits */
20004                                             , 'QTY1', round(sum(NVL(pod.quantity_delivered, 0)),5) /* Bug:13427569 rounded to 5 digits */
20005                                             )
20006        ), 1, 240)
20007   ,  'PO_CAN_POLL_REC_NOT_DEL'
20008   FROM po_lines_gt pol, po_line_locations_gt poll, po_distributions_gt pod
20009   WHERE pod.line_location_id = poll.line_location_id
20010     AND pol.po_line_id = poll.po_line_id
20011     AND pol.order_type_lookup_code NOT IN ('RATE', 'FIXED PRICE')
20012     AND NVL(poll.cancel_flag, 'N') = 'N'
20013     AND NVL(poll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
20014     AND round(NVL(poll.quantity_received, 0),5) > /* Bug:13427569 rounded to 5 digits */
20015          (
20016            SELECT round(sum(NVL(pod2.quantity_delivered, 0)),5) /* Bug:13427569 rounded to 5 digits */
20017            FROM po_distributions_gt pod2
20018            WHERE pod2.line_location_id = poll.line_location_id
20019          )
20020   GROUP BY pol.line_num
20021          , poll.shipment_num
20022          , NVL(poll.quantity_received, 0)
20023          , ROWNUM  -- <Bug 4118145, Issue 8>
20024   ;
20025 
20026   d_progress := 20;
20027 
20028   p_sequence := p_sequence + SQL%ROWCOUNT;
20029   x_return_status := FND_API.G_RET_STS_SUCCESS;
20030 
20031   IF (PO_LOG.d_proc) THEN
20032     PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
20033     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
20034     PO_LOG.proc_end(d_module);
20035   END IF;
20036 
20037   RETURN;
20038 
20039 EXCEPTION
20040   WHEN others THEN
20041     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
20042 
20043     IF (PO_LOG.d_exc) THEN
20044       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
20045       PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
20046       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
20047       PO_LOG.proc_end(d_module);
20048     END IF;
20049 
20050     RETURN;
20051 
20052 END check_qty_rcv_but_not_deliv;
20053 
20054 -- Determine if there is any shipment being finally closed that
20055 -- has received more than has been delivered to its distributions.
20056 -- For ('RATE', 'FIXED PRICE') lines only
20057 PROCEDURE check_amt_rcv_but_not_deliv(
20058    p_document_type        IN VARCHAR2
20059 ,  p_online_report_id     IN NUMBER
20060 ,  p_user_id              IN NUMBER
20061 ,  p_login_id             IN NUMBER
20062 ,  p_sequence             IN OUT NOCOPY NUMBER
20063 ,  x_return_status        OUT NOCOPY VARCHAR2
20064 )
20065 IS
20066 
20067 l_textline  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
20068 
20069 d_module   VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.check_amt_rcv_but_not_deliv';
20070 d_progress NUMBER;
20071 
20072 
20073 BEGIN
20074 
20075   d_progress := 0;
20076   IF (PO_LOG.d_proc) THEN
20077     PO_LOG.proc_begin(d_module);
20078     PO_LOG.proc_begin(d_module, 'p_document_type', p_document_type);
20079     PO_LOG.proc_begin(d_module, 'p_online_report_id', p_online_report_id);
20080     PO_LOG.proc_begin(d_module, 'p_user_id', p_user_id);
20081     PO_LOG.proc_begin(d_module, 'p_login_id', p_login_id);
20082     PO_LOG.proc_begin(d_module, 'p_sequence', p_sequence);
20083   END IF;
20084 
20085   d_progress := 10;
20086 
20087   -- <Bug 4118145, Issue 8 Start>
20088   -- In query below, changed quantity columns to amount columns and also
20089   -- group by ROWNUM so that it can be used in the select clause
20090 
20091   INSERT INTO PO_ONLINE_REPORT_TEXT_GT
20092   (  online_report_id
20093   ,  last_update_login
20094   ,  last_updated_by
20095   ,  last_update_date
20096   ,  created_by
20097   ,  creation_date
20098   ,  line_num
20099   ,  shipment_num
20100   ,  distribution_num
20101   ,  sequence
20102   ,  text_line
20103   ,  message_name
20104   )
20105   SELECT
20106      p_online_report_id
20107   ,  p_login_id
20108   ,  p_user_id
20109   ,  SYSDATE
20110   ,  p_user_id
20111   ,  SYSDATE
20112   ,  DECODE(p_document_type, g_document_type_RELEASE, 0, pol.line_num)
20113   ,  poll.shipment_num
20114   ,  0
20115   ,  p_sequence + ROWNUM
20116   ,  substr(
20117        DECODE(p_document_type, g_document_type_RELEASE,
20118          g_shipmsg || g_delim || poll.shipment_num || g_delim
20119            || PO_CORE_S.get_translated_text('PO_CAN_POLL_AMT_REC_NOT_DEL'
20120                                             , 'QTY2', NVL(poll.amount_received, 0)
20121                                             , 'QTY1', sum(NVL(pod.amount_delivered, 0))
20122                                             ),
20123          g_linemsg || g_delim || pol.line_num || g_delim || g_shipmsg || g_delim
20124            || poll.shipment_num || g_delim
20125            || PO_CORE_S.get_translated_text('PO_CAN_POLL_AMT_REC_NOT_DEL'
20126                                             , 'QTY2', NVL(poll.amount_received, 0)
20127                                             , 'QTY1', sum(NVL(pod.amount_delivered, 0))
20128                                             )
20129        ), 1, 240)
20130   ,  'PO_CAN_POLL_AMT_REC_NOT_DEL'
20131   FROM po_lines_gt pol, po_line_locations_gt poll, po_distributions_gt pod
20132   WHERE pod.line_location_id = poll.line_location_id
20133     AND pol.po_line_id = poll.po_line_id
20134     AND pol.order_type_lookup_code IN ('RATE', 'FIXED PRICE')
20135     AND NVL(poll.cancel_flag, 'N') = 'N'
20136     AND NVL(poll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
20137     AND NVL(poll.amount_received, 0) >
20138          (
20139            SELECT sum(NVL(pod2.amount_delivered, 0))
20140            FROM po_distributions_gt pod2
20141            WHERE pod2.line_location_id = poll.line_location_id
20142          )
20143   GROUP BY pol.line_num
20144          , poll.shipment_num
20145          , NVL(poll.amount_received, 0)
20146          , ROWNUM   -- <Bug 4118145, Issue 8>
20147   ;
20148 
20149   -- <Bug 4118145, Issue 8 End>
20150 
20151   d_progress := 20;
20152 
20153   p_sequence := p_sequence + SQL%ROWCOUNT;
20154   x_return_status := FND_API.G_RET_STS_SUCCESS;
20155 
20156   IF (PO_LOG.d_proc) THEN
20157     PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
20158     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
20159     PO_LOG.proc_end(d_module);
20160   END IF;
20161 
20162   RETURN;
20163 
20164 EXCEPTION
20165   WHEN others THEN
20166     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
20167 
20168     IF (PO_LOG.d_exc) THEN
20169       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
20170       PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
20171       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
20172       PO_LOG.proc_end(d_module);
20173     END IF;
20174 
20175     RETURN;
20176 
20177 END check_amt_rcv_but_not_deliv;
20178 
20179 -- Determine if there are any invalid accounting flexfields
20180 PROCEDURE check_invalid_acct_flex(
20181    p_document_type        IN VARCHAR2
20182 ,  p_action_requested     IN VARCHAR2
20183 ,  p_action_date          IN DATE
20184 ,  p_online_report_id     IN NUMBER
20185 ,  p_user_id              IN NUMBER
20186 ,  p_login_id             IN NUMBER
20187 ,  p_document_id          IN NUMBER
20188 ,  p_sequence             IN OUT NOCOPY NUMBER
20189 ,  x_return_status        OUT NOCOPY VARCHAR2
20190 )
20191 IS
20192 
20193 l_textline  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
20194 l_is_complex_po     boolean;
20195 l_token_value VARCHAR2(256);
20196 
20197 d_module   VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.check_invalid_acct_flex';
20198 d_progress NUMBER;
20199 
20200 
20201 BEGIN
20202 
20203   d_progress := 0;
20204   IF (PO_LOG.d_proc) THEN
20205     PO_LOG.proc_begin(d_module);
20206     PO_LOG.proc_begin(d_module, 'p_document_type', p_document_type);
20207     PO_LOG.proc_begin(d_module, 'p_action_date', p_action_date);
20208     PO_LOG.proc_begin(d_module, 'p_action_requested', p_action_requested);
20209     PO_LOG.proc_begin(d_module, 'p_online_report_id', p_online_report_id);
20210     PO_LOG.proc_begin(d_module, 'p_user_id', p_user_id);
20211     PO_LOG.proc_begin(d_module, 'p_login_id', p_login_id);
20212     PO_LOG.proc_begin(d_module, 'p_sequence', p_sequence);
20213   END IF;
20214 
20215   d_progress := 10;
20216 
20217   --Bug5072310
20218   --Added Condition to filter releaes before calling complex work API
20219   if p_document_type <> g_document_type_RELEASE  then
20220      l_is_complex_po := PO_COMPLEX_WORK_PVT.is_complex_work_po(p_document_id);
20221   end if;
20222 
20223   IF (l_is_complex_po) THEN
20224     l_token_value := FND_MESSAGE.GET_STRING('PO', 'PO_LINE_LOC_TYPE_LOW_P_PAYITEM');
20225   ELSE
20226     l_token_value := FND_MESSAGE.GET_STRING('PO', 'PO_LINE_LOC_TYPE_LOWER_S_SCH');
20227   END IF;
20228 
20229   FND_MESSAGE.SET_NAME('PO','PO_CAN_POLL_INVALID_ACCT_FLEX');
20230   FND_MESSAGE.SET_TOKEN('LINE_LOCATION_TYPE', l_token_value);
20231 
20232   l_textline := FND_MESSAGE.GET;
20233 
20234   INSERT INTO PO_ONLINE_REPORT_TEXT_GT
20235   (  online_report_id
20236   ,  last_update_login
20237   ,  last_updated_by
20238   ,  last_update_date
20239   ,  created_by
20240   ,  creation_date
20241   ,  line_num
20242   ,  shipment_num
20243   ,  distribution_num
20244   ,  sequence
20245   ,  text_line
20246   ,  message_name
20247   )
20248   SELECT
20249      p_online_report_id
20250   ,  p_login_id
20251   ,  p_user_id
20252   ,  SYSDATE
20253   ,  p_user_id
20254   ,  SYSDATE
20255   ,  DECODE(p_document_type, g_document_type_RELEASE, 0, pol.line_num)
20256   ,  poll.shipment_num
20257   ,  pod.distribution_num
20258   ,  p_sequence + ROWNUM
20259   ,  substr(
20260        DECODE(p_document_type, g_document_type_RELEASE,
20261            g_shipmsg || g_delim || poll.shipment_num || g_delim || g_distmsg || g_delim || l_textline
20262          , g_linemsg || g_delim || pol.line_num || g_delim || g_shipmsg || g_delim
20263              || poll.shipment_num || g_delim || g_distmsg || g_delim || l_textline
20264        ), 1, 240)
20265   ,  'PO_CAN_POLL_INVALID_ACCT_FLEX'
20266   FROM po_lines_gt pol, po_line_locations_gt poll
20267      , po_distributions_gt pod, gl_code_combinations gcc
20268   WHERE pod.line_location_id = poll.line_location_id
20269     AND pol.po_line_id = poll.po_line_id
20270     AND poll.shipment_type IN ('STANDARD', 'PLANNED', 'PREPAYMENT') --<Complex Work R12>
20271     AND NVL(poll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
20272     AND ( NVL(poll.cancel_flag, 'N') = 'N' OR p_action_requested = g_action_FINAL_CLOSE_CHECK)
20273     AND gcc.code_combination_id = pod.code_combination_id
20274     AND NVL(p_action_date, trunc(SYSDATE)) NOT BETWEEN
20275           NVL(gcc.start_date_active, NVL(p_action_date, trunc(SYSDATE) - 1))
20276             AND
20277           NVL(gcc.end_date_active, NVL(p_action_date, trunc(SYSDATE) + 1));
20278 
20279   d_progress := 20;
20280 
20281   p_sequence := p_sequence + SQL%ROWCOUNT;
20282   x_return_status := FND_API.G_RET_STS_SUCCESS;
20283 
20284   IF (PO_LOG.d_proc) THEN
20285     PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
20286     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
20287     PO_LOG.proc_end(d_module);
20288   END IF;
20289 
20290   RETURN;
20291 
20292 EXCEPTION
20293   WHEN others THEN
20294     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
20295 
20296     IF (PO_LOG.d_exc) THEN
20297       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
20298       PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
20299       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
20300       PO_LOG.proc_end(d_module);
20301     END IF;
20302 
20303     RETURN;
20304 
20305 END check_invalid_acct_flex;
20306 
20307 -- Deterimine if a BPA has any open releases against it
20308 PROCEDURE check_bpa_has_open_release(
20309    p_online_report_id     IN NUMBER
20310 ,  p_user_id              IN NUMBER
20311 ,  p_login_id             IN NUMBER
20312 ,  p_sequence             IN OUT NOCOPY NUMBER
20313 ,  x_return_status        OUT NOCOPY VARCHAR2
20314 )
20315 IS
20316 
20317 l_textline  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
20318 
20319 d_module   VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.check_bpa_has_open_release';
20320 d_progress NUMBER;
20321 
20322 
20323 BEGIN
20324 
20325   d_progress := 0;
20326   IF (PO_LOG.d_proc) THEN
20327     PO_LOG.proc_begin(d_module);
20328     PO_LOG.proc_begin(d_module, 'p_online_report_id', p_online_report_id);
20329     PO_LOG.proc_begin(d_module, 'p_user_id', p_user_id);
20330     PO_LOG.proc_begin(d_module, 'p_login_id', p_login_id);
20331     PO_LOG.proc_begin(d_module, 'p_sequence', p_sequence);
20332   END IF;
20333 
20334   d_progress := 10;
20335 
20336   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_POL_PLAN_WITH_OPEN_REL');
20337 
20338   INSERT INTO PO_ONLINE_REPORT_TEXT_GT
20339   (  online_report_id
20340   ,  last_update_login
20341   ,  last_updated_by
20342   ,  last_update_date
20343   ,  created_by
20344   ,  creation_date
20345   ,  line_num
20346   ,  shipment_num
20347   ,  distribution_num
20348   ,  sequence
20349   ,  text_line
20350   ,  message_name
20351   )
20352   SELECT
20353      p_online_report_id
20354   ,  p_login_id
20355   ,  p_user_id
20356   ,  SYSDATE
20357   ,  p_user_id
20358   ,  SYSDATE
20359   ,  pol.line_num
20360   ,  0
20361   ,  0
20362   ,  p_sequence + ROWNUM
20363   ,  substr(g_linemsg || g_delim || pol.line_num || g_delim || l_textline,1,240)  --Bug5096900
20364   ,  'PO_CAN_POL_PLAN_WITH_OPEN_REL'
20365   FROM po_lines_gt pol
20366   WHERE EXISTS
20367      (
20368        SELECT 'Uncancelled Open Releases Exist'
20369        FROM po_line_locations pll
20370        WHERE pll.po_line_id = pol.po_line_id
20371          AND pll.shipment_type = 'BLANKET'
20372          AND NVL(pll.cancel_flag, 'N') = 'N'
20373          AND NVL(pll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
20374      );
20375 
20376   d_progress := 20;
20377 
20378   p_sequence := p_sequence + SQL%ROWCOUNT;
20379   x_return_status := FND_API.G_RET_STS_SUCCESS;
20380 
20381   IF (PO_LOG.d_proc) THEN
20382     PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
20383     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
20384     PO_LOG.proc_end(d_module);
20385   END IF;
20386 
20387   RETURN;
20388 
20389 EXCEPTION
20390   WHEN others THEN
20391     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
20392 
20393     IF (PO_LOG.d_exc) THEN
20394       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
20395       PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
20396       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
20397       PO_LOG.proc_end(d_module);
20398     END IF;
20399 
20400     RETURN;
20401 
20402 END check_bpa_has_open_release;
20403 
20404 -- Deterimine if a GA has any open Std. POs against it
20405 PROCEDURE check_bpa_has_open_stdref(
20406    p_online_report_id     IN NUMBER
20407 ,  p_user_id              IN NUMBER
20408 ,  p_login_id             IN NUMBER
20409 ,  p_sequence             IN OUT NOCOPY NUMBER
20410 ,  x_return_status        OUT NOCOPY VARCHAR2
20411 )
20412 IS
20413 
20414 l_textline  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
20415 
20416 d_module   VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.check_bpa_has_open_stdref';
20417 d_progress NUMBER;
20418 
20419 
20420 BEGIN
20421 
20422   d_progress := 0;
20423   IF (PO_LOG.d_proc) THEN
20424     PO_LOG.proc_begin(d_module);
20425     PO_LOG.proc_begin(d_module, 'p_online_report_id', p_online_report_id);
20426     PO_LOG.proc_begin(d_module, 'p_user_id', p_user_id);
20427     PO_LOG.proc_begin(d_module, 'p_login_id', p_login_id);
20428     PO_LOG.proc_begin(d_module, 'p_sequence', p_sequence);
20429   END IF;
20430 
20431   d_progress := 10;
20432 
20433   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_GAL_WITH_OPEN_STD_REF');
20434 
20435   INSERT INTO PO_ONLINE_REPORT_TEXT_GT
20436   (  online_report_id
20437   ,  last_update_login
20438   ,  last_updated_by
20439   ,  last_update_date
20440   ,  created_by
20441   ,  creation_date
20442   ,  line_num
20443   ,  shipment_num
20444   ,  distribution_num
20445   ,  sequence
20446   ,  text_line
20447   ,  message_name
20448   )
20449   SELECT
20450      p_online_report_id
20451   ,  p_login_id
20452   ,  p_user_id
20453   ,  SYSDATE
20454   ,  p_user_id
20455   ,  SYSDATE
20456   ,  pol.line_num
20457   ,  0
20458   ,  0
20459   ,  p_sequence + ROWNUM
20460   ,  substr(g_linemsg || g_delim || pol.line_num || g_delim || l_textline,1,240)   --Bug5096900
20461   ,  'PO_CAN_GAL_WITH_OPEN_STD_REF'
20462   FROM po_lines_gt pol
20463   WHERE EXISTS
20464      (
20465        SELECT 'Uncancelled Std PO lines referencing this GA line exist'
20466        FROM po_lines_all pol2
20467        WHERE pol2.from_line_id = pol.po_line_id
20468          AND NVL(pol2.cancel_flag, 'N') = 'N'
20469          AND NVL(pol2.closed_code, 'OPEN') <> 'FINALLY CLOSED'
20470      );
20471 
20472   d_progress := 20;
20473 
20474   p_sequence := p_sequence + SQL%ROWCOUNT;
20475   x_return_status := FND_API.G_RET_STS_SUCCESS;
20476 
20477   IF (PO_LOG.d_proc) THEN
20478     PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
20479     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
20480     PO_LOG.proc_end(d_module);
20481   END IF;
20482 
20483   RETURN;
20484 
20485 EXCEPTION
20486   WHEN others THEN
20487     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
20488 
20489     IF (PO_LOG.d_exc) THEN
20490       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
20491       PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
20492       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
20493       PO_LOG.proc_end(d_module);
20494     END IF;
20495 
20496     RETURN;
20497 
20498 END check_bpa_has_open_stdref;
20499 
20500 -- Determine if a GC has any open Std. POs against it
20501 PROCEDURE check_cpa_has_open_stdref(
20502    p_online_report_id     IN NUMBER
20503 ,  p_user_id              IN NUMBER
20504 ,  p_login_id             IN NUMBER
20505 ,  p_sequence             IN OUT NOCOPY NUMBER
20506 ,  x_return_status        OUT NOCOPY VARCHAR2
20507 )
20508 IS
20509 
20510 l_textline  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
20511 
20512 d_module   VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.check_cpa_has_open_stdref';
20513 d_progress NUMBER;
20514 
20515 
20516 BEGIN
20517 
20518   d_progress := 0;
20519   IF (PO_LOG.d_proc) THEN
20520     PO_LOG.proc_begin(d_module);
20521     PO_LOG.proc_begin(d_module, 'p_online_report_id', p_online_report_id);
20522     PO_LOG.proc_begin(d_module, 'p_user_id', p_user_id);
20523     PO_LOG.proc_begin(d_module, 'p_login_id', p_login_id);
20524     PO_LOG.proc_begin(d_module, 'p_sequence', p_sequence);
20525   END IF;
20526 
20527   d_progress := 10;
20528 
20529   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_CGA_WITH_OPEN_STD_REF');
20530 
20531   INSERT INTO PO_ONLINE_REPORT_TEXT_GT
20532   (  online_report_id
20533   ,  last_update_login
20534   ,  last_updated_by
20535   ,  last_update_date
20536   ,  created_by
20537   ,  creation_date
20538   ,  line_num
20539   ,  shipment_num
20540   ,  distribution_num
20541   ,  sequence
20542   ,  text_line
20543   ,  message_name
20544   )
20545   SELECT
20546      p_online_report_id
20547   ,  p_login_id
20548   ,  p_user_id
20549   ,  SYSDATE
20550   ,  p_user_id
20551   ,  SYSDATE
20552   ,  0
20553   ,  0
20554   ,  0
20555   ,  p_sequence + ROWNUM
20556   ,  substr(l_textline,1,240)      --Bug5096900
20557   ,  'PO_CAN_CGA_WITH_OPEN_STD_REF'
20558   FROM po_headers_gt poh
20559   WHERE EXISTS
20560      (
20561        SELECT 'Open Std PO lines referencing this contract exist'
20562        FROM po_lines_all pol
20563        WHERE pol.contract_id = poh.po_header_id
20564          AND NVL(pol.cancel_flag, 'N') = 'N'
20565          AND NVL(pol.closed_code, 'OPEN') <> 'FINALLY CLOSED'
20566      );
20567 
20568   d_progress := 20;
20569 
20570   p_sequence := p_sequence + SQL%ROWCOUNT;
20571   x_return_status := FND_API.G_RET_STS_SUCCESS;
20572 
20573   IF (PO_LOG.d_proc) THEN
20574     PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
20575     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
20576     PO_LOG.proc_end(d_module);
20577   END IF;
20578 
20579   RETURN;
20580 
20581 EXCEPTION
20582   WHEN others THEN
20583     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
20584 
20585     IF (PO_LOG.d_exc) THEN
20586       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
20587       PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
20588       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
20589       PO_LOG.proc_end(d_module);
20590     END IF;
20591 
20592     RETURN;
20593 
20594 END check_cpa_has_open_stdref;
20595 
20596 -- Determine if a PPO has any open releases against it
20597 PROCEDURE check_ppo_has_open_release(
20598    p_online_report_id     IN NUMBER
20599 ,  p_user_id              IN NUMBER
20600 ,  p_login_id             IN NUMBER
20601 ,  p_sequence             IN OUT NOCOPY NUMBER
20602 ,  x_return_status        OUT NOCOPY VARCHAR2
20603 )
20604 IS
20605 
20606 l_textline  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
20607 
20608 d_module   VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.check_ppo_has_open_release';
20609 d_progress NUMBER;
20610 
20611 
20612 BEGIN
20613 
20614   d_progress := 0;
20615   IF (PO_LOG.d_proc) THEN
20616     PO_LOG.proc_begin(d_module);
20617     PO_LOG.proc_begin(d_module, 'p_online_report_id', p_online_report_id);
20618     PO_LOG.proc_begin(d_module, 'p_user_id', p_user_id);
20619     PO_LOG.proc_begin(d_module, 'p_login_id', p_login_id);
20620     PO_LOG.proc_begin(d_module, 'p_sequence', p_sequence);
20621   END IF;
20622 
20623   d_progress := 10;
20624 
20625   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_CAN_POLL_PLAN_WITH_OPEN_REL');
20626 
20627   INSERT INTO PO_ONLINE_REPORT_TEXT_GT
20628   (  online_report_id
20629   ,  last_update_login
20630   ,  last_updated_by
20631   ,  last_update_date
20632   ,  created_by
20633   ,  creation_date
20634   ,  line_num
20635   ,  shipment_num
20636   ,  distribution_num
20637   ,  sequence
20638   ,  text_line
20639   ,  message_name
20640   )
20641   SELECT
20642      p_online_report_id
20643   ,  p_login_id
20644   ,  p_user_id
20645   ,  SYSDATE
20646   ,  p_user_id
20647   ,  SYSDATE
20648   ,  pol.line_num
20649   ,  poll.shipment_num
20650   ,  0
20651   ,  p_sequence + ROWNUM
20652   ,  substr(g_linemsg || g_delim || pol.line_num || g_delim || g_shipmsg
20653               || poll.shipment_num || g_delim || l_textline,1,240)   --Bug5096900
20654   ,  'PO_CAN_POLL_PLAN_WITH_OPEN_REL'
20655   FROM po_lines_gt pol, po_line_locations_gt poll
20656   WHERE poll.po_line_id = pol.po_line_id
20657     AND NVL(poll.cancel_flag, 'N') = 'N'
20658     AND NVL(poll.closed_code, 'OPEN') <> 'FINALLY CLOSED'
20659     AND EXISTS
20660      (
20661        SELECT 'Uncancelled Open Releases Exist'
20662        FROM po_line_locations poll2
20663        WHERE poll2.source_shipment_id = poll.line_location_id
20664          AND poll2.shipment_type = 'SCHEDULED'
20665          AND NVL(poll2.cancel_flag, 'N') = 'N'
20666          AND NVL(poll2.closed_code, 'OPEN') <> 'FINALLY CLOSED'
20667      );
20668 
20669   d_progress := 20;
20670 
20671   p_sequence := p_sequence + SQL%ROWCOUNT;
20672   x_return_status := FND_API.G_RET_STS_SUCCESS;
20673 
20674   IF (PO_LOG.d_proc) THEN
20675     PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
20676     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
20677     PO_LOG.proc_end(d_module);
20678   END IF;
20679 
20680   RETURN;
20681 
20682 EXCEPTION
20683   WHEN others THEN
20684     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
20685 
20686     IF (PO_LOG.d_exc) THEN
20687       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
20688       PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
20689       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
20690       PO_LOG.proc_end(d_module);
20691     END IF;
20692 
20693     RETURN;
20694 
20695 END check_ppo_has_open_release;
20696 -- <Doc Manager Rewrite 11.5.11 End>
20697 
20698 
20699 --<Complex Work R12 START>
20700 -- PO Quantity/Amount Rollup Checks
20701 PROCEDURE check_po_qty_amt_rollup(
20702    p_online_report_id     IN NUMBER
20703 ,  p_document_id          IN NUMBER
20704 ,  p_login_id             IN NUMBER
20705 ,  p_user_id              IN NUMBER
20706 ,  p_clm_document	  IN VARCHAR := 'N' --CLM Mod
20707 ,  x_sequence             IN OUT NOCOPY NUMBER
20708 )
20709 IS
20710   l_api_name CONSTANT VARCHAR2(40) := 'CHECK_PO_QTY_AMT_ROLLUP';
20711   l_progress VARCHAR2(3);
20712   l_is_complex_po     boolean;
20713   l_is_financing_flag VARCHAR2(1);
20714   TYPE NumTab is TABLE of NUMBER INDEX by BINARY_INTEGER;
20715   l_rowcount NumTab;
20716   l_line_num        NumTab;
20717   l_shipment_num    NumTab;
20718   l_dist_num        NumTab;
20719   l_line_qty_tbl    NumTab;     -- <SERVICES FPJ>
20720   l_line_amt_tbl    NumTab;     -- <SERVICES FPJ>
20721   l_lineloc_qty_tbl NumTab;     -- <SERVICES FPJ>
20722   l_lineloc_amt_tbl NumTab;     -- <SERVICES FPJ>
20723   l_dist_qty_tbl    NumTab;     -- <SERVICES FPJ>
20724   l_dist_amt_tbl    NumTab;     -- <SERVICES FPJ>
20725   l_fin_adv_amount  NumTab;
20726   l_currency_code   VARCHAR2(15); -- <Complex Work R12>
20727   l_min_acct_unit   VARCHAR2(15); -- <Complex Work R12>
20728   l_precision       VARCHAR2(15); -- <Complex Work R12>
20729   l_draft_id        NUMBER;       -- PAR Project
20730   l_draft_type      VARCHAR2(10); -- PAR Project
20731   l_line_id_tbl     NumTab; --   bug 16474947
20732 BEGIN
20733   l_progress := '001';
20734 
20735   IF g_debug_stmt THEN
20736      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
20737        FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
20738                       || l_progress,'Start PO Qty/Amt Rollup Checks');
20739      END IF;
20740   END IF;
20741 
20742   -- Method Logic:
20743   -- If not a Complex Work PO, run the existing shipment level checks.
20744   -- For Complex Work PO, run separate pay item rollup check
20745   -- In both cases, run the existing distribution level checks.
20746 
20747   l_is_complex_po := PO_COMPLEX_WORK_PVT.is_complex_work_po(p_document_id);
20748 
20749   IF (NOT l_is_complex_po) THEN
20750 
20751     l_progress := '010';
20752     -- Check 1a: Quantities/Amounts between Purchase Order Line and Shipments
20753     -- must match (existing logic)
20754 
20755     IF g_debug_stmt THEN
20756        IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
20757          FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
20758                         || l_progress,'PO Shipment Qty/Amt Rollup');
20759        END IF;
20760     END IF;
20761 
20762     SELECT
20763         POL.line_num
20764     ,   POL.quantity
20765     ,   POL.amount                                            -- <SERVICES FPJ>
20766     ,   sum( PLL.quantity - nvl(PLL.quantity_cancelled,0) )   -- <SERVICES FPJ>
20767     ,   sum( PLL.amount - nvl(PLL.amount_cancelled,0) )       -- <SERVICES FPJ>
20768     BULK COLLECT INTO
20769         l_line_num
20770     ,   l_line_qty_tbl                                        -- <SERVICES FPJ>
20771     ,   l_line_amt_tbl                                        -- <SERVICES FPJ>
20772     ,   l_lineloc_qty_tbl                                     -- <SERVICES FPJ>
20773     ,   l_lineloc_amt_tbl                                     -- <SERVICES FPJ>
20774     FROM
20775         PO_LINE_LOCATIONS_GT PLL
20776     ,   PO_LINES_GT POL
20777     WHERE
20778         POL.po_line_id = PLL.po_line_id
20779     AND PLL.shipment_type in ('STANDARD', 'PLANNED')
20780     AND POL.po_header_id = p_document_id
20781     AND nvl(POL.cancel_flag,'N') = 'N'
20782     AND nvl(POL.closed_code,'OPEN') <> 'FINALLY CLOSED'
20783     AND (
20784             (   ( POL.quantity IS NOT NULL )                  -- <SERVICES FPJ>
20785             AND ( round(POL.quantity, 10) <>
20786                 (SELECT round(sum(PLL2.quantity) -
20787                               sum(nvl(PLL2.quantity_cancelled, 0)), 10)
20788                  FROM PO_LINE_LOCATIONS_GT PLL2
20789                  WHERE PLL2.po_line_id = POL.po_line_id AND
20790                        PLL2.shipment_type in ('STANDARD', 'PLANNED') ) )
20791             )
20792         OR                                                    -- <SERVICES FPJ>
20793             (   ( POL.amount IS NOT NULL )
20794             AND ( round(POL.amount, 10) <>
20795                   (   SELECT round ( sum ( PLL3.amount
20796                                          - nvl(PLL3.amount_cancelled, 0) )
20797                                    , 10
20798                                    )
20799                       FROM   po_line_locations_gt PLL3
20800                       WHERE  PLL3.po_line_id = POL.po_line_id
20801                       AND    PLL3.shipment_type IN ('STANDARD','PLANNED')
20802                   )
20803                 )
20804             )
20805         )
20806     GROUP BY
20807         POL.line_num
20808     ,   POL.quantity
20809     ,   POL.amount;                                           -- <SERVICES FPJ>
20810 
20811     l_progress := '015';
20812 
20813     FOR i IN 1..l_line_num.COUNT LOOP
20814         l_rowCount(i) := i;
20815     END LOOP;
20816 
20817     FORALL i IN 1..l_line_num.COUNT
20818         INSERT INTO po_online_report_text_gt(online_report_id,
20819         last_update_login,
20820         last_updated_by,
20821         last_update_date,
20822         created_by,
20823         creation_date,
20824         line_num,
20825         shipment_num,
20826         distribution_num,
20827         sequence,
20828         text_line,
20829                 message_name)
20830         VALUES(p_online_report_id,
20831             p_login_id,
20832             p_user_id,
20833             sysdate,
20834             p_user_id,
20835             sysdate,
20836             l_line_num(i),
20837             NULL,                                             -- <SERVICES FPJ>
20838             NULL,                                             -- <SERVICES FPJ>
20839             x_sequence+ l_rowCount(i),
20840             decode ( l_line_qty_tbl(i)                        -- <SERVICES FPJ>
20841                    , NULL , PO_CORE_S.get_translated_text
20842                             (   'PO_SUB_PO_LINE_NE_SHIP_AMT'
20843                             ,   'LINE_NUM', l_line_num(i)
20844                             ,   'LINE_AMT', l_line_amt_tbl(i)
20845                             ,   'SHIP_AMT', l_lineloc_amt_tbl(i)
20846                             )
20847                    ,        PO_CORE_S.get_translated_text
20848                             (   'PO_SUB_PO_LINE_NE_SHIP_QTY'
20849                             ,   'LINE_NUM', l_line_num(i)
20850                             ,   'LINE_QTY', l_line_qty_tbl(i)
20851                             ,   'SHIP_QTY', l_lineloc_qty_tbl(i)
20852                             )
20853                    ),
20854             decode ( l_line_qty_tbl(i)                        -- <SERVICES FPJ>
20855                    , NULL , 'PO_SUB_PO_LINE_NE_SHIP_AMT'
20856                    ,        'PO_SUB_PO_LINE_NE_SHIP_QTY'
20857                    )
20858           );
20859 
20860     l_progress := '017';
20861 
20862     --Increment the x_sequence with number of errors reported in last query
20863     x_sequence := x_sequence + SQL%ROWCOUNT;
20864 
20865   ELSE
20866     -- The document IS a complex work PO
20867     -- Check 1b: Amounts roll up between Purchase Order Line and Pay Items
20868 
20869     l_progress := '020';
20870 
20871     IF g_debug_stmt THEN
20872        IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
20873          FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
20874                         || l_progress,'PO Pay Item Qty/Amt Rollup');
20875        END IF;
20876     END IF;
20877 
20878     l_is_financing_flag := PO_CORE_S.boolean_to_flag(
20879                            PO_COMPLEX_WORK_PVT.is_financing_po(p_document_id));
20880 
20881     l_progress := '022';
20882 
20883     -- Get the currency code and precision
20884     SELECT poh.currency_code
20885     INTO   l_currency_code
20886     FROM   po_headers_all poh
20887     WHERE  poh.po_header_id = p_document_id;
20888 
20889     PO_CORE_S2.get_currency_info(
20890       x_currency_code => l_currency_code
20891     , x_min_unit      => l_min_acct_unit
20892     , x_precision     => l_precision);
20893 
20894     l_progress := '023';
20895 
20896    --Bug 5440038 , included ADVANCES for roll-up logic but this would apply
20897    --  only to financing case, as the for actual case only the
20898    --  STANDARD line locations are rolled up
20899    -- for a financing case, the line location rollups would include all pay items
20900    -- and the advance amounts which together make up the prepayment amount
20901    -- should be less than the line amount
20902    -- the financing_advance_amount advance amount is also calculated for
20903    -- better reporting purposes
20904 
20905     SELECT
20906         subtotal.line_num
20907     ,   subtotal.line_amount
20908     ,   subtotal.line_loc_amount
20909     ,   subtotal.financing_advance_amount
20910     BULK COLLECT INTO
20911         l_line_num
20912     ,   l_line_amt_tbl
20913     ,   l_lineloc_amt_tbl
20914     ,   l_fin_adv_amount
20915     FROM
20916     ( SELECT
20917         POL.line_num
20918       , CASE
20919           WHEN (    POL.order_type_lookup_code = 'FIXED PRICE'
20920                  OR POL.order_type_lookup_code = 'RATE')
20921 	  THEN
20922      --Revert Bug # 13840480 (FP of 13833174): Rounding amount at line level to make in synch with
20923 	    --to make in synch with amount at distribution level.
20924 	    POL.amount
20925           ELSE
20926             CASE
20927               WHEN l_min_acct_unit IS NOT NULL THEN
20928                 -- Round to minimum accountable unit.
20929                 ROUND(
20930                       NVL(POL.quantity * POL.unit_price,0) / l_min_acct_unit
20931                      ) * l_min_acct_unit
20932               ELSE
20933                 -- Round to currency precision.
20934                 ROUND(NVL(POL.quantity * POL.unit_price,0), l_precision)
20935             END
20936         END line_amount
20937 	--Revert Block Modified for bug 10041694 starts
20938       , SUM (CASE
20939           WHEN (    PLL.value_basis = 'FIXED PRICE'
20940                  OR PLL.value_basis = 'RATE')
20941 	  THEN
20942 		   PLL.amount - NVL(PLL.amount_cancelled, 0)
20943           ELSE
20944             CASE
20945               WHEN l_min_acct_unit IS NOT NULL THEN
20946                 -- Round to minimum accountable unit.
20947                 ROUND(
20948                       ((PLL.quantity-NVL(PLL.quantity_cancelled,0)) * PLL.price_override) / l_min_acct_unit
20949                      ) * l_min_acct_unit
20950               ELSE
20951                 -- Round to currency precision.
20952                 ROUND((PLL.quantity-NVL(PLL.quantity_cancelled,0)) * PLL.price_override, l_precision)
20953             END
20954             END ) line_loc_amount
20955     --Revert Block Modified for bug 10041694 ends
20956      , SUM (CASE
20957           WHEN  PLL.payment_type = 'ADVANCE' THEN
20958              PLL.amount
20959        END) financing_advance_amount
20960       FROM
20961         PO_LINE_LOCATIONS_GT PLL
20962       , PO_LINES_GT POL
20963       WHERE
20964         POL.po_line_id = PLL.po_line_id
20965       AND (   (l_is_financing_flag = 'N' AND PLL.shipment_type = 'STANDARD')
20966            OR (l_is_financing_flag = 'Y' and PLL.shipment_type = 'PREPAYMENT'))
20967       AND POL.po_header_id = p_document_id
20968       AND nvl(POL.cancel_flag,'N') = 'N'
20969       AND nvl(POL.closed_code,'OPEN') <> 'FINALLY CLOSED'
20970    --   AND nvl(payment_type, 'NULL') <> 'ADVANCE' --Bug 5440038
20971       GROUP BY POL.line_num, POL.order_type_lookup_code, POL.amount, POL.quantity, POL.unit_price
20972     ) subtotal
20973     WHERE
20974     ( (l_is_financing_flag = 'Y' AND NOT (subtotal.line_amount >= subtotal.line_loc_amount))
20975     OR
20976       (l_is_financing_flag = 'N' AND NOT (subtotal.line_amount = subtotal.line_loc_amount))
20977     );
20978 
20979     l_progress := '025';
20980 
20981     FOR i IN 1..l_line_num.COUNT LOOP
20982         l_rowCount(i) := i;
20983     END LOOP;
20984 
20985    --Bug 5440038 and 5517131: Cleared the TODOs for complex work submission checks error messages
20986 
20987     FORALL i IN 1..l_line_num.COUNT
20988         INSERT INTO po_online_report_text_gt(online_report_id,
20989         last_update_login,
20990         last_updated_by,
20991         last_update_date,
20992         created_by,
20993         creation_date,
20994         line_num,
20995         shipment_num,
20996         distribution_num,
20997         sequence,
20998         text_line,
20999         message_name)
21000         VALUES(p_online_report_id,
21001             p_login_id,
21002             p_user_id,
21003             sysdate,
21004             p_user_id,
21005             sysdate,
21006             l_line_num(i),
21007             NULL,
21008             NULL,
21009             x_sequence+ l_rowCount(i),
21010             decode ( l_fin_adv_amount(i),
21011                      NULL,
21012                      PO_CORE_S.get_translated_text
21013                      (  'PO_SUB_PAY_ITEM_NE_LINE_AMT'
21014                      ,  'LINE_NUM', l_line_num(i)
21015                      ,  'PAY_ITEM_AMT', l_lineloc_amt_tbl(i)
21016                      ,  'LINE_AMT', l_line_amt_tbl(i)
21017                      ),
21018                       PO_CORE_S.get_translated_text
21019                     (  'PO_SUB_PRE_PAY_GE_LINE_AMT'
21020                     ,  'LINE_NUM', l_line_num(i)
21021                     ,  'PAY_ITEM_AMT', (l_lineloc_amt_tbl(i)-l_fin_adv_amount(i) )
21022                     ,  'ADV_AMT',  l_fin_adv_amount(i)
21023                     ,  'LINE_AMT', l_line_amt_tbl(i)
21024                     )
21025                   ),
21026             decode ( l_fin_adv_amount(i),
21027                      NULL,  'PO_SUB_PAY_ITEM_NE_LINE_AMT'
21028                    ,  'PO_SUB_PRE_PAY_GE_LINE_AMT'
21029                    )
21030           );
21031 
21032     l_progress := '027';
21033     --Increment the x_sequence with number of errors reported in last query
21034     x_sequence := x_sequence + SQL%ROWCOUNT;
21035 
21036   END IF;  -- If Complex Work PO or not
21037 -----------------------------------------------
21038 
21039   l_progress := '030';
21040   IF g_debug_stmt THEN
21041      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
21042        FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
21043                       || l_progress,'PO Dist qty/amt rollup check');
21044      END IF;
21045   END IF;
21046 
21047     -- Check 2: The sum of the distribution quantities/amounts should match the
21048     -- shipment quantity/amount.
21049     --CLM Phase 4: PAR Changes Begin.
21050 
21051     --Getting the draft id to get the draft type from po drafts
21052     SELECT draft_id
21053     INTO   l_draft_id
21054     FROM   po_headers_gt
21055     WHERE  po_header_id = p_document_id;
21056 
21057     --Getting the draft type
21058   --SQL to get l_draft_type is executed only when l_draft_id is not -1
21059     l_draft_type := 'AWARD';
21060 
21061     IF l_draft_id <> -1 THEN
21062       BEGIN
21063           SELECT draft_type
21064           INTO   l_draft_type
21065           FROM   po_drafts
21066           WHERE  draft_id = l_draft_id;
21067       EXCEPTION
21068           WHEN no_data_found THEN
21069             l_draft_type := 'AWARD';
21070       END;
21071     END IF;
21072 
21073 
21074 
21075     SELECT
21076         POL.line_num
21077     ,   PLL.shipment_num
21078     ,   PLL.quantity
21079     ,   PLL.amount                                            -- <SERVICES FPJ>
21080     ,   sum( nvl(POD.quantity_ordered,0) - nvl(POD.quantity_cancelled,0) )
21081     ,   sum( nvl(POD.amount_ordered,0) - nvl(POD.amount_cancelled,0) )
21082     ,   pol.po_line_id --bug 16474947
21083     BULK COLLECT INTO
21084         l_line_num
21085     ,   l_shipment_num
21086     ,   l_lineloc_qty_tbl
21087     ,   l_lineloc_amt_tbl
21088     ,   l_dist_qty_tbl
21089     ,   l_dist_amt_tbl
21090     ,   l_line_id_tbl -- bug 16474947
21091     FROM PO_DISTRIBUTIONS_GT POD,PO_LINE_LOCATIONS_GT PLL, PO_LINES_GT POL
21092     WHERE PLL.po_line_id = POL.po_line_id
21093     AND POD.line_location_id = PLL.line_location_id
21094     AND PLL.po_header_id = p_document_id
21095     AND nvl(PLL.cancel_flag,'N') = 'N'
21096     AND nvl(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
21097     AND PLL.shipment_type in ('STANDARD', 'PLANNED', 'PREPAYMENT') --<Complex Work R12>
21098     GROUP BY
21099         POL.line_num
21100     ,   PLL.shipment_num
21101     ,   PLL.quantity
21102     ,   PLL.amount                                            -- <SERVICES FPJ>
21103     ,   PLL.amount_cancelled
21104     ,   PLL.quantity_cancelled
21105     ,   PLL.shipment_type   --<Complex Work R12>
21106     ,   pol.po_line_id --bug 16474947
21107     HAVING
21108         ((p_clm_document <> 'Y' AND
21109           decode ( PLL.quantity                                 -- <SERVICES FPJ>
21110                , NULL , abs (   ( PLL.amount - nvl(PLL.amount_cancelled,0) )
21111                             -   sum( POD.amount_ordered - nvl(POD.amount_cancelled,0) ) )
21112                ,        abs (   ( PLL.quantity - nvl(PLL.quantity_cancelled,0) )
21113                             -   sum( POD.quantity_ordered - nvl(POD.quantity_cancelled,0) ) )
21114                ) > .00001)
21115          OR
21116          ( p_clm_document = 'Y'
21117          AND l_draft_type = 'PAR'
21118          AND Decode (pll.quantity --
21119              , NULL, Abs (( pll.amount - Nvl(pll.amount_cancelled, 0) ) - SUM(
21120                                   pod.amount_ordered -
21121                                   Nvl(pod.amount_cancelled, 0))),
21122                Abs (( pll.quantity - Nvl(pll.quantity_cancelled, 0) ) - SUM(
21123                    pod.quantity_ordered - Nvl(pod.quantity_cancelled, 0)))) >
21124              .00001 )-- PAR Project
21125          OR
21126          (p_clm_document = 'Y' AND
21127           decode ( PLL.quantity                                 -- <SERVICES FPJ>
21128                , NULL ,         ( PLL.amount - nvl(PLL.amount_cancelled,0) )
21129                             -   sum( POD.amount_ordered - nvl(POD.amount_cancelled,0) )
21130                ,                ( PLL.quantity - nvl(PLL.quantity_cancelled,0) )
21131                             -   sum( POD.quantity_ordered - nvl(POD.quantity_cancelled,0) )
21132                ) < 0)
21133         );
21134   -- CLM-LnSc
21135   -- For CLM documents, the sum of Dist qty or amt cannot be more than that of the Shipment
21136   -- Non CLM case the sum of Dist qty or amt cannot be different from that of the Shipment
21137 
21138     l_progress := '035';
21139 
21140     FOR i IN 1..l_line_num.COUNT LOOP
21141         l_rowCount(i) := i;
21142     END LOOP;
21143 
21144     FORALL i IN 1..l_line_num.COUNT
21145         INSERT INTO po_online_report_text_gt (online_report_id,
21146         last_update_login,
21147         last_updated_by,
21148         last_update_date,
21149         created_by,
21150         creation_date,
21151         line_num,
21152         shipment_num,
21153         distribution_num,
21154         sequence,
21155         text_line,
21156         message_name,
21157         message_type)
21158         SELECT
21159             p_online_report_id,
21160              p_login_id,
21161              p_user_id,
21162             sysdate,
21163             p_user_id,
21164             sysdate,
21165             l_line_num(i),
21166             l_shipment_num(i),
21167             NULL,                                             -- <SERVICES FPJ>
21168             x_sequence+l_rowCount(i),
21169             decode ( l_lineloc_qty_tbl(i)
21170                    , NULL , PO_CORE_S.get_translated_text
21171                             (   'PO_SUB_PO_SHIP_NE_DIST_AMT'
21172                             ,   'LINE_NUM', l_line_num(i)
21173                             ,   'SHIP_NUM', l_shipment_num(i)
21174                             ,   'SHIP_AMT', l_lineloc_amt_tbl(i)
21175                             ,   'DIST_AMT', l_dist_amt_tbl(i)
21176                             )
21177                           , PO_CORE_S.get_translated_text
21178                             (   'PO_SUB_PO_SHIP_NE_DIST_QTY'
21179                             ,   'LINE_NUM', l_line_num(i)
21180                             ,   'SHIP_NUM', l_shipment_num(i)
21181                             ,   'SHIP_QTY', l_lineloc_qty_tbl(i)
21182                             ,   'DIST_QTY', l_dist_qty_tbl(i)
21183                             )
21184                    ),
21185             decode ( l_lineloc_qty_tbl(i)
21186                    , NULL , 'PO_SUB_PO_SHIP_NE_DIST_AMT'
21187                    ,        'PO_SUB_PO_SHIP_NE_DIST_QTY'
21188                    ),
21189             Decode(l_draft_type,'PAR','W',NULL) --WARNING FOR PAR and ERROR FOR OTHERS
21190           FROM DUAL --bug 16474947
21191           WHERE
21192               ( (l_draft_type <> 'PAR') OR
21193                 (l_draft_type = 'PAR' AND NOT EXISTS (SELECT 1 FROM po_lines_draft_all WHERE po_line_id = l_line_id_tbl(i)
21194                   AND draft_id = l_draft_id AND change_status = 'NEW'))
21195               );
21196        --CLM Phase 4: PAR Changes End.
21197     l_progress := '037';
21198     --Increment the x_sequence with number of errors reported in last query
21199     x_sequence := x_sequence + l_line_num.COUNT;
21200 -----------------------------------------------
21201 
21202   IF g_debug_stmt THEN
21203      IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
21204        FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||l_api_name||'.'
21205                       || l_progress,'End PO Qty/Amt Rollup Checks');
21206      END IF;
21207   END IF;
21208 
21209 END check_po_qty_amt_rollup;
21210 --<Complex Work R12 END>
21211 
21212 
21213 
21214 --<BUG 4624736 START>
21215 -- Checks if the pay item's price can be updated.
21216 -- NOTE: does not verify that line location is in fact
21217 -- a pay item.
21218 FUNCTION is_pay_item_price_updateable (
21219   p_line_location_id          IN NUMBER
21220 , p_add_reasons_to_msg_list   IN VARCHAR2)
21221 RETURN BOOLEAN
21222 IS
21223 
21224   d_module VARCHAR(70) :=
21225                 'po.plsql.PO_DOCUMENT_CHECKS_PVT.is_pay_item_price_updateable';
21226   d_progress NUMBER;
21227   l_is_price_updateable BOOLEAN;
21228   l_quantity_received   NUMBER;
21229   l_quantity_billed     NUMBER;
21230   l_quantity_financed   NUMBER;
21231 
21232 BEGIN
21233   d_progress := 0;
21234 
21235   IF (PO_LOG.d_proc) THEN
21236     PO_LOG.proc_begin(d_module);
21237     PO_LOG.proc_begin(d_module, 'p_line_location_id', p_line_location_id);
21238   END IF;
21239 
21240   d_progress := 10;
21241 
21242   l_is_price_updateable := TRUE;
21243 
21244   -- get the execution quantities for the pay item
21245   SELECT NVL(pll.quantity_received, 0)
21246        , NVL(pll.quantity_billed, 0)
21247        , NVL(pll.quantity_financed, 0)
21248   INTO   l_quantity_received
21249        , l_quantity_billed
21250        , l_quantity_financed
21251   FROM   po_line_locations_all pll
21252   WHERE  line_location_id = p_line_location_id;
21253 
21254   d_progress := 20;
21255 
21256   -- the price is not updateable if the pay item has been executed
21257   -- against
21258   IF (    l_quantity_received <> 0
21259        OR l_quantity_billed <> 0
21260        OR l_quantity_financed <> 0)
21261   THEN
21262     l_is_price_updateable := FALSE;
21263 
21264     d_progress := 30;
21265 
21266     IF (p_add_reasons_to_msg_list = PO_CORE_S.G_PARAMETER_YES) THEN
21267       d_progress := 40;
21268       -- <Complex Work TODO>: FILL IN THE MESSAGES
21269       FND_MESSAGE.set_name('PO','CWPOTODOMESSAGE');
21270       FND_MSG_PUB.add;
21271     END IF;
21272   END IF;
21273 
21274   d_progress := 50;
21275 
21276   IF (PO_LOG.d_proc) THEN
21277     PO_LOG.proc_return(d_module, l_is_price_updateable);
21278   END IF;
21279 
21280   RETURN(l_is_price_updateable);
21281 
21282 EXCEPTION
21283   WHEN OTHERS THEN
21284     IF (PO_LOG.d_exc) THEN
21285       PO_LOG.exc(d_module, d_progress, SQLCODE || SQLERRM);
21286       PO_LOG.proc_end(d_module);
21287     END IF;
21288     RAISE;
21289 END is_pay_item_price_updateable;
21290 --<BUG 4624736 END>
21291 
21292 
21293  --Bug#5462677: copied from 11.5.9 code line of POXPOSCB.pls
21294  FUNCTION chk_unv_invoices(p_invoice_type	IN  VARCHAR2 DEFAULT 'BOTH',
21295 			   p_po_header_id	IN  NUMBER,
21296 			   p_po_release_id	IN  NUMBER DEFAULT NULL,
21297 			   p_po_line_id		IN  NUMBER DEFAULT NULL,
21298 			   p_line_location_id	IN  NUMBER DEFAULT NULL,
21299 			   p_po_distribution_id	IN  NUMBER DEFAULT NULL,
21300 			   p_invoice_id		IN  NUMBER DEFAULT NULL,
21301 			   p_calling_sequence	IN  VARCHAR2) RETURN NUMBER IS
21302 
21303 x_chk_unv_invoices NUMBER := 0;
21304 BEGIN
21305 
21306   If Not AP_MATCH_UTILITIES_PUB.Check_Unvalidated_Invoices(
21307                               p_invoice_type => p_invoice_type,
21308                               p_po_header_id => p_po_header_id,
21309                               p_po_release_id => p_po_release_id,
21310                               p_po_line_id => p_po_line_id,
21311                               p_line_location_id => p_line_location_id,
21312                               p_po_distribution_id => p_po_distribution_id,
21313                               p_invoice_id => p_invoice_id,
21314                               p_calling_sequence => p_calling_sequence) THEN
21315 
21316     -- Unvalidated Credit Memos/Invoices Does Not Exists for this Shipment
21317     x_chk_unv_invoices := 0;
21318   Else
21319     -- Unvalidated Credit Memos/Invoices Do Exists for this Shipment
21320     x_chk_unv_invoices := 1;
21321   End If;
21322 
21323   return(x_chk_unv_invoices);
21324 
21325 EXCEPTION
21326   WHEN OTHERS THEN
21327   PO_MESSAGE_S.APP_ERROR(error_name => 'PO_ALL_TRACE_ERROR',
21328                            token1 => 'FILE',
21329                            value1 => 'PO_CONTROL_CHECKS',
21330                            token2 => 'ERR_NUMBER',
21331                            value2 => '360',
21332                            token3 => 'SUBROUTINE',
21333                            value3 => 'CHK_UNV_INVOICES()');
21334   RAISE;
21335 
21336 END chk_unv_invoices;
21337 -- Determine if there are any unvalidated invoices
21338 --Bug#5462677
21339 PROCEDURE check_unvalidated_invoices(
21340    p_document_type        IN VARCHAR2
21341 ,  p_document_subtype     IN VARCHAR2
21342 ,  p_action_requested     IN VARCHAR2
21343 ,  p_action_date          IN DATE
21344 ,  p_online_report_id     IN NUMBER
21345 ,  p_user_id              IN NUMBER
21346 ,  p_login_id             IN NUMBER
21347 ,  p_document_level       IN VARCHAR2
21348 ,  p_origin_doc_id        IN NUMBER
21349 ,  p_doc_level_id         IN NUMBER
21350 ,  p_sequence             IN OUT NOCOPY NUMBER
21351 ,  x_return_status        OUT NOCOPY VARCHAR2
21352 )
21353 IS
21354 
21355 l_textline  PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
21356 l_message_name  FND_NEW_MESSAGES.MESSAGE_NAME%TYPE;
21357 l_calling_sequence VARCHAR2(100);
21358 l_invoice_type     VARCHAR2(100) := 'BOTH';
21359 l_is_complex_po     boolean;
21360 l_token_value VARCHAR2(256);
21361 l_return_status VARCHAR2(1);
21362 d_module   VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.check_unvalidated_invoices';
21363 d_progress NUMBER;
21364 
21365 BEGIN
21366 
21367   d_progress := 0;
21368   IF (PO_LOG.d_proc) THEN
21369     PO_LOG.proc_begin(d_module);
21370     PO_LOG.proc_begin(d_module, 'p_document_type', p_document_type);
21371     PO_LOG.proc_begin(d_module, 'p_action_date', p_action_date);
21372     PO_LOG.proc_begin(d_module, 'p_action_requested', p_action_requested);
21373     PO_LOG.proc_begin(d_module, 'p_online_report_id', p_online_report_id);
21374     PO_LOG.proc_begin(d_module, 'p_user_id', p_user_id);
21375     PO_LOG.proc_begin(d_module, 'p_login_id', p_login_id);
21376     PO_LOG.proc_begin(d_module, 'p_origin_doc_id', p_origin_doc_id);
21377     PO_LOG.proc_begin(d_module, 'p_document_level', p_document_level);
21378     PO_LOG.proc_begin(d_module, 'p_doc_level_id', p_doc_level_id);
21379     PO_LOG.proc_begin(d_module, 'p_sequence', p_sequence);
21380   END IF;
21381 
21382 
21383   d_progress := 10;
21384 
21385   -- Initialize the error mesage before hand, since we need this in
21386   -- the finallyclose/unreserve check sql
21387   -- Error Messages for Finally close hdr/line/shipment and Release
21388   -- Error Messages for Unreserve SPO
21389 
21390   get_message_info(p_document_type    => p_document_type ,
21391                    p_document_subtype => p_document_subtype,
21392                    p_action_requested => p_action_requested ,
21393                    p_document_level   => p_document_level,
21394                    p_doc_level_id     => p_doc_level_id,
21395                    x_text_line        => l_textline,
21396                    x_message_name     => l_message_name,
21397                    x_invoice_type     => l_invoice_type ,
21398                    x_calling_sequence => l_calling_sequence,
21399                    x_return_status    => l_return_status);
21400 
21401   IF p_document_type = 'PO' AND p_document_subtype = 'STANDARD'
21402      AND (p_document_level = g_document_level_HEADER
21403        OR p_document_level = g_document_level_LINE) THEN
21404      --
21405      -- Handle Header final close / Unreserve SPO
21406      --
21407     IF  (p_document_level = g_document_level_HEADER) THEN
21408 
21409         INSERT INTO PO_ONLINE_REPORT_TEXT_GT
21410         (  online_report_id
21411         ,  last_update_login
21412         ,  last_updated_by
21413         ,  last_update_date
21414         ,  created_by
21415         ,  creation_date
21416         ,  line_num
21417         ,  shipment_num
21418         ,  distribution_num
21419         ,  sequence
21420         ,  text_line
21421         ,  message_name
21422         )
21423         SELECT
21424            p_online_report_id
21425         ,  p_login_id
21426         ,  p_user_id
21427         ,  SYSDATE
21428         ,  p_user_id
21429         ,  SYSDATE
21430         ,  null -- lines
21431         ,  null -- shipments
21432         ,  null -- distribution_num
21433         ,  p_sequence + ROWNUM
21434         ,  substr(l_textline, 1, 240)
21435         ,  l_message_name
21436         FROM po_headers_gt poh
21437         WHERE poh.po_header_id = p_doc_level_id
21438           AND  chk_unv_invoices(l_invoice_type, poh.po_header_id, NULL, NULL,NULL, NULL, p_origin_doc_id, l_calling_sequence) = 1;
21439 
21440      --
21441      -- Handle Line final close / Unreserve SPO
21442      --
21443     ELSIF (p_document_level = g_document_level_LINE) THEN
21444         INSERT INTO PO_ONLINE_REPORT_TEXT_GT
21445         (  online_report_id
21446         ,  last_update_login
21447         ,  last_updated_by
21448         ,  last_update_date
21449         ,  created_by
21450         ,  creation_date
21451         ,  line_num
21452         ,  shipment_num
21453         ,  distribution_num
21454         ,  sequence
21455         ,  text_line
21456         ,  message_name
21457         )
21458         SELECT
21459            p_online_report_id
21460         ,  p_login_id
21461         ,  p_user_id
21462         ,  SYSDATE
21463         ,  p_user_id
21464         ,  SYSDATE
21465         ,  line_num -- lines
21466         ,  null -- shipments
21467         ,  null -- distribution_num
21468         ,  p_sequence + ROWNUM
21469         ,  substr(l_textline, 1, 240)
21470         ,  l_message_name
21471         FROM po_lines_gt pol
21472         WHERE pol.po_line_id=p_doc_level_id
21473           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;
21474     END IF;
21475   --
21476   -- Handle Release Header related checks for unvalidated AP Invoices
21477   --
21478   ELSIF (p_document_type = 'RELEASE' AND p_document_level = g_document_level_HEADER) THEN
21479     INSERT INTO PO_ONLINE_REPORT_TEXT_GT
21480     (  online_report_id
21481         ,  last_update_login
21482         ,  last_updated_by
21483         ,  last_update_date
21484         ,  created_by
21485         ,  creation_date
21486         ,  line_num
21487         ,  shipment_num
21488         ,  distribution_num
21489         ,  sequence
21490         ,  text_line
21491         ,  message_name
21492     )
21493     SELECT
21494            p_online_report_id
21495         ,  p_login_id
21496         ,  p_user_id
21497         ,  SYSDATE
21498         ,  p_user_id
21499         ,  SYSDATE
21500         ,  null -- lines
21501         ,  null -- shipments
21502         ,  null -- distribution_num
21503         ,  p_sequence + ROWNUM
21504         ,  substr(l_textline, 1, 240)
21505         ,  l_message_name
21506     FROM po_releases_gt por
21507     WHERE por.po_release_id=p_doc_level_id
21508      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;
21509 
21510    --
21511    -- Handle Shipment/Payitem final close and Unreserve(Both SPO and Release)
21512    --
21513   ELSIF (p_document_level = g_document_level_SHIPMENT) THEN
21514       INSERT INTO PO_ONLINE_REPORT_TEXT_GT
21515       (  online_report_id
21516       ,  last_update_login
21517       ,  last_updated_by
21518       ,  last_update_date
21519       ,  created_by
21520       ,  creation_date
21521       ,  line_num
21522       ,  shipment_num
21523       ,  distribution_num
21524       ,  sequence
21525       ,  text_line
21526       ,  message_name
21527       )
21528       SELECT
21529          p_online_report_id
21530       ,  p_login_id
21531       ,  p_user_id
21532       ,  SYSDATE
21533       ,  p_user_id
21534       ,  SYSDATE
21535       ,  null -- lines
21536       ,  shipment_num -- shipments
21537       ,  null -- distribution_num
21538       ,  p_sequence + ROWNUM
21539       ,  substr(l_textline, 1, 240)
21540       ,  l_message_name
21541       FROM po_line_locations_gt poll
21542       WHERE poll.line_location_id=p_doc_level_id
21543         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;
21544    --
21545    -- Handle Distribution Unreserve(for both SPO/Release)
21546    --
21547   ELSIF (p_document_level=g_document_level_DISTRIBUTION  AND p_action_requested = g_action_UNRESERVE) THEN
21548       INSERT INTO PO_ONLINE_REPORT_TEXT_GT
21549       (  online_report_id
21550       ,  last_update_login
21551       ,  last_updated_by
21552       ,  last_update_date
21553       ,  created_by
21554       ,  creation_date
21555       ,  line_num
21556       ,  shipment_num
21557       ,  distribution_num
21558       ,  sequence
21559       ,  text_line
21560       ,  message_name
21561       )
21562       SELECT
21563          p_online_report_id
21564       ,  p_login_id
21565       ,  p_user_id
21566       ,  SYSDATE
21567       ,  p_user_id
21568       ,  SYSDATE
21569       ,  null -- lines
21570       ,  null -- shipments
21571       ,  distribution_num
21572       ,  p_sequence + ROWNUM
21573       ,  substr(l_textline, 1, 240)
21574       ,  l_message_name
21575       FROM po_distributions_gt pod
21576       WHERE pod.po_distribution_id=p_doc_level_id
21577         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;
21578   END IF; --
21579 
21580   d_progress := 20;
21581 
21582   p_sequence := p_sequence + SQL%ROWCOUNT;
21583   x_return_status := FND_API.G_RET_STS_SUCCESS;
21584 
21585   IF (PO_LOG.d_proc) THEN
21586     PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
21587     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
21588     PO_LOG.proc_end(d_module);
21589   END IF;
21590 
21591   RETURN;
21592 
21593 EXCEPTION
21594   WHEN others THEN
21595     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
21596     IF (PO_LOG.d_exc) THEN
21597       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
21598       PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
21599       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
21600       PO_LOG.proc_end(d_module);
21601     END IF;
21602 
21603     RETURN;
21604 END check_unvalidated_invoices;
21605 
21606 PROCEDURE get_message_info(p_document_type    IN VARCHAR2,
21607                            p_document_subtype IN VARCHAR2,
21608                            p_action_requested IN VARCHAR2,
21609                            p_document_level   IN VARCHAR2,
21610                            p_doc_level_id     IN NUMBER,
21611                            x_text_line        OUT  NOCOPY VARCHAR2,
21612                            x_message_name     OUT  NOCOPY VARCHAR2,
21613                            x_invoice_type     OUT  NOCOPY VARCHAR2,
21614                            x_calling_sequence OUT  NOCOPY VARCHAR2,
21615                            x_return_status    OUT  NOCOPY VARCHAR2) IS
21616 
21617   l_textline         PO_ONLINE_REPORT_TEXT_GT.text_line%TYPE;
21618   l_message_name     FND_NEW_MESSAGES.MESSAGE_NAME%TYPE;
21619   l_calling_sequence VARCHAR2(100);
21620   l_invoice_type     VARCHAR2(100) := 'BOTH';
21621   l_is_complex_po    boolean := false;
21622   l_document_id      NUMBER;
21623   d_module   VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.get_message_text';
21624   d_progress NUMBER;
21625 BEGIN
21626   d_progress := 0;
21627   IF (PO_LOG.d_proc) THEN
21628     PO_LOG.proc_begin(d_module);
21629     PO_LOG.proc_begin(d_module, 'p_document_type', p_document_type);
21630     PO_LOG.proc_begin(d_module, 'p_document_subtype', p_document_subtype);
21631     PO_LOG.proc_begin(d_module, 'p_action_requested', p_action_requested);
21632     PO_LOG.proc_begin(d_module, 'p_document_level', p_document_level);
21633     PO_LOG.proc_begin(d_module, 'p_doc_level_id', p_doc_level_id);
21634   END IF;
21635 
21636   IF p_document_subtype = 'STANDARD' THEN
21637     IF p_document_level = g_document_level_HEADER THEN
21638      l_document_id := p_doc_level_id;
21639     ELSIF p_document_level = g_document_level_LINE THEN
21640       SELECT po_header_id
21641       INTO   l_document_id
21642       FROM   po_lines_gt
21643       WHERE po_line_id=p_doc_level_id;
21644     ELSIF p_document_level = g_document_level_SHIPMENT THEN
21645       SELECT po_header_id
21646       INTO   l_document_id
21647       FROM   po_line_locations_gt
21648       WHERE line_location_id=p_doc_level_id;
21649     ELSIF p_document_level = g_document_level_DISTRIBUTION THEN
21650       SELECT po_header_id
21651       INTO   l_document_id
21652       FROM   po_distributions_gt
21653       WHERE  po_distribution_id=p_doc_level_id;
21654     END IF;
21655     IF p_document_type <> g_document_type_RELEASE THEN
21656       l_is_complex_po := PO_COMPLEX_WORK_PVT.is_complex_work_po(l_document_id);
21657     END IF;
21658   END IF;
21659 
21660   IF (p_action_requested = g_action_UNRESERVE) THEN
21661 -- bug#16471988 : not allow to unreseved when there are unvalidated
21662 -- invoices too, not just credit memos
21663 --
21664 --    l_invoice_type     := 'CREDIT';
21665    l_invoice_type     := 'BOTH';    -- bug#16471988
21666 
21667     IF (p_document_level = g_document_level_HEADER) THEN
21668        l_message_name     := 'PO_UNRES_AP_DOCS_PENDING';
21669        l_textline         := FND_MESSAGE.GET_STRING('PO',l_message_name);
21670     ELSIF (p_document_level = g_document_level_LINE) THEN
21671        l_message_name     := 'PO_UNRES_POL_AP_DOCS_PENDING';
21672        l_textline         := FND_MESSAGE.GET_STRING('PO',l_message_name);
21673     ELSIF (p_document_level = g_document_level_SHIPMENT) THEN
21674        IF (l_is_complex_po) THEN
21675          l_message_name     := 'PO_UNRES_POPI_AP_DOCS_PENDING';
21676          l_textline         := FND_MESSAGE.GET_STRING('PO',l_message_name);
21677        ELSE
21678          l_message_name     := 'PO_UNRES_POLL_AP_DOCS_PENDING';
21679          l_textline         := FND_MESSAGE.GET_STRING('PO',l_message_name);
21680        END IF;
21681     ELSIF (p_document_level = g_document_level_DISTRIBUTION) THEN
21682        l_message_name     := 'PO_UNRES_POD_AP_DOCS_PENDING';
21683        l_textline         := FND_MESSAGE.GET_STRING('PO',l_message_name);
21684     END IF;
21685   ELSIF (p_action_requested = g_action_FINAL_CLOSE_CHECK) THEN
21686     l_invoice_type     := 'BOTH';
21687 
21688     IF (p_document_level = g_document_level_HEADER) THEN
21689        l_message_name     := 'PO_FC_POH_AP_DOCS_PENDING';
21690        l_textline         := FND_MESSAGE.GET_STRING('PO',l_message_name);
21691     ELSIF (p_document_level = g_document_level_LINE) THEN
21692        l_message_name     := 'PO_FC_POL_AP_DOCS_PENDING';
21693        l_textline         := FND_MESSAGE.GET_STRING('PO',l_message_name);
21694     ELSIF (p_document_level = g_document_level_SHIPMENT) THEN
21695        IF (l_is_complex_po) THEN
21696          l_message_name     := 'PO_FC_POPI_AP_DOCS_PENDING';
21697          l_textline         := FND_MESSAGE.GET_STRING('PO',l_message_name);
21698        ELSE
21699          l_message_name     := 'PO_FC_POLL_AP_DOCS_PENDING';
21700          l_textline         := FND_MESSAGE.GET_STRING('PO',l_message_name);
21701        END IF;
21702     END IF;
21703   END IF;
21704   l_calling_sequence := l_message_name; -- can be any string(tracking in AP)
21705 
21706   x_message_name     := l_message_name;
21707   x_text_line        := l_textline;
21708   x_calling_sequence := l_calling_sequence;
21709   x_invoice_type     := l_invoice_type;
21710   x_return_status    := FND_API.G_RET_STS_SUCCESS;
21711 
21712   IF (PO_LOG.d_proc) THEN
21713     PO_LOG.proc_end(d_module, 'x_text_line', x_text_line);
21714     PO_LOG.proc_end(d_module, 'x_message_name', x_message_name);
21715     PO_LOG.proc_end(d_module, 'x_invoice_type', x_invoice_type);
21716     PO_LOG.proc_end(d_module, 'x_calling_sequence', x_calling_sequence);
21717     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
21718     PO_LOG.proc_end(d_module);
21719   END IF;
21720 
21721 EXCEPTION
21722   WHEN OTHERS THEN
21723     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
21724     IF (PO_LOG.d_exc) THEN
21725       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
21726       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
21727       PO_LOG.proc_end(d_module);
21728     END IF;
21729 END;
21730 
21731 PROCEDURE set_lcm_flag(p_line_location_id  IN NUMBER,
21732 			p_doc_check_status IN VARCHAR2,
21733 			x_return_status    OUT nocopy VARCHAR2)
21734 IS
21735 
21736 l_return_status VARCHAR2(10) := NULL;
21737 l_vendor_id NUMBER;
21738 l_vendor_site_id NUMBER;
21739 l_inventory_item_id NUMBER;
21740 l_ship_to_organization_id NUMBER;
21741 l_consigned_flag VARCHAR2(20);
21742 l_outsourced_assembly VARCHAR2(20);
21743 l_log_head VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.Set_LCM_Flag';
21744 l_progress VARCHAR2(3) := '000';
21745 l_line_location_id po_line_locations_all.line_location_id%TYPE; --<BUG 10377000>
21746 
21747 BEGIN
21748   IF g_debug_stmt THEN
21749      PO_DEBUG.debug_begin(l_log_head);
21750      PO_DEBUG.debug_var(l_log_head,l_progress,'p_line_location_id',   p_line_location_id);
21751      PO_DEBUG.debug_var(l_log_head,l_progress,'p_doc_check_status',   p_doc_check_status);
21752   END IF;
21753 
21754   l_progress := '001';
21755   --<BUG 7594807 Added Table Alias>
21756   --<BUG 8233135 Added exception block for the below sql to avoid
21757   --errors while supplier user tries to split the shipment lines
21758   --while performing supplier change request>
21759   --<BUG 10377000 fetching line location id to pass to inv_check_lcm>
21760   BEGIN
21761 	SELECT poh.vendor_id,
21762 	       poh.vendor_site_id,
21763   	       pol.item_id,
21764   	       poll.ship_to_organization_id,
21765   	       poll.consigned_flag,
21766   	       poll.outsourced_assembly,
21767   	       poll.line_location_id
21768           INTO l_vendor_id,
21769   	       l_vendor_site_id,
21770   	       l_inventory_item_id,
21771   	       l_ship_to_organization_id,
21772   	       l_consigned_flag,
21773   	       l_outsourced_assembly,
21774   	       l_line_location_id
21775           FROM po_line_locations_all poll,
21776     	       po_lines_all pol,
21777     	       po_headers_all poh
21778          WHERE poh.po_header_id = pol.po_header_id
21779            AND pol.po_line_id = poll.po_line_id
21780            AND poll.line_location_id = p_line_location_id;
21781   EXCEPTION
21782   WHEN OTHERS THEN
21783 	NULL;
21784   END;
21785 
21786      IF g_debug_stmt THEN
21787             PO_DEBUG.debug_var(l_log_head,l_progress,'l_vendor_id',l_vendor_id);
21788             PO_DEBUG.debug_var(l_log_head,l_progress,'l_vendor_site_id',l_vendor_site_id);
21789             PO_DEBUG.debug_var(l_log_head,l_progress,'l_inventory_item_id',l_inventory_item_id);
21790             PO_DEBUG.debug_var(l_log_head,l_progress,'l_ship_to_organization_id',l_ship_to_organization_id);
21791             PO_DEBUG.debug_var(l_log_head,l_progress,'l_consigned_flag',l_consigned_flag);
21792             PO_DEBUG.debug_var(l_log_head,l_progress,'l_outsourced_assembly',l_outsourced_assembly);
21793             PO_DEBUG.debug_var(l_log_head,l_progress,'l_line_location_id',l_line_location_id);
21794 
21795       END IF;
21796 
21797     l_progress:= '002';
21798 
21799     --<BUG 7594807 Call the Inventory API only when the PO line has item so that
21800     -- we can avoid the call for other line types>
21801     IF ( l_inventory_item_id IS NOT NULL ) THEN
21802 
21803 	l_return_status := inv_utilities.inv_check_lcm(l_inventory_item_id,
21804   						       l_ship_to_organization_id,
21805   						       l_consigned_flag,
21806 						       l_outsourced_assembly,
21807 						       l_vendor_id,
21808 						       l_vendor_site_id,
21809 						       l_line_location_id); --<BUG 10377000>
21810 
21811     END IF;
21812 
21813 
21814     PO_DEBUG.debug_var(l_log_head,l_progress,'l_return_status',l_return_status);
21815 
21816       IF l_return_status = 'Y' THEN
21817       	IF p_doc_check_status = 'BEFORE' THEN
21818       		UPDATE po_line_locations_gt
21819 		  SET lcm_flag = 'Y'
21820 		  WHERE line_location_id = p_line_location_id
21821 		  and lcm_flag is null;
21822 
21823 		UPDATE po_distributions_gt
21824 		  SET lcm_flag = 'Y'
21825                   WHERE line_location_id = p_line_location_id
21826                   and lcm_flag is null;
21827 
21828 
21829         ELSIF p_doc_check_status = 'AFTER' THEN
21830 
21831                 UPDATE po_line_locations_all
21832                  SET lcm_flag = 'Y'
21833                  WHERE line_location_id = p_line_location_id;
21834 
21835                 UPDATE po_distributions_all
21836                 SET lcm_flag = 'Y'
21837                 WHERE line_location_id = p_line_location_id;
21838        END IF;
21839 
21840       ELSIF l_return_status = 'N' THEN
21841 
21842       		UPDATE po_line_locations_all
21843 		 SET lcm_flag = null
21844 		 WHERE line_location_id = p_line_location_id
21845 		 AND lcm_flag = 'Y';
21846 
21847 		UPDATE po_distributions_all
21848 		 SET lcm_flag = null
21849                  WHERE line_location_id = p_line_location_id
21850                  AND lcm_flag = 'Y';
21851 
21852       END IF;
21853 
21854   x_return_status := fnd_api.g_ret_sts_success;
21855 
21856   EXCEPTION
21857   WHEN others THEN
21858     x_return_status := fnd_api.g_ret_sts_unexp_error;
21859 
21860     IF (g_debug_unexp) THEN
21861                     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
21862                       FND_LOG.string(FND_LOG.level_unexpected, l_log_head || '.others_exception', 'EXCEPTION: Location is '
21863                            || l_progress || ' SQL CODE is '||sqlcode);
21864                     END IF;
21865     END IF;
21866 
21867   END set_lcm_flag;
21868 
21869 -------------------------------------------------------------------------------
21870 --Start of Comments
21871 --Name: validate_supplier_debarred
21872 --Pre-reqs:
21873 --  This procedure validates if the supplier site is debarred or not
21874 --  at the time of submitting the document and
21875 --  throws warning message based on the conditions
21876 --End of Comments
21877 -------------------------------------------------------------------------------
21878 
21879 PROCEDURE validate_supplier_debarred(  p_draft_id        IN NUMBER
21880 ,  p_header_id     IN NUMBER
21881 ,  p_online_report_id     IN NUMBER
21882 ,  p_user_id              IN NUMBER
21883 ,  p_login_id             IN NUMBER
21884 ,  p_sequence             IN OUT NOCOPY NUMBER
21885 ,  x_return_status        OUT NOCOPY VARCHAR2
21886 )
21887 IS
21888 
21889 l_log_head VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.validate_supplier_debarred';
21890 l_progress VARCHAR2(3) := '000';
21891 l_vendor_site_id PO_VENDOR_SITES_ALL.VENDOR_SITE_ID%type;
21892 l_debar_start_date PO_VENDOR_SITES_ALL.DEBARMENT_START_DATE%type;
21893 l_debar_end_date PO_VENDOR_SITES_ALL.DEBARMENT_END_DATE%type;
21894 debar_status VARCHAR2(50) := NULL;
21895 
21896 BEGIN
21897   l_progress := '010';
21898   IF g_debug_stmt THEN
21899      PO_DEBUG.debug_begin(l_log_head);
21900      PO_DEBUG.debug_var(l_log_head,l_progress,'p_draft_id',   p_draft_id);
21901      PO_DEBUG.debug_var(l_log_head,l_progress,'p_header_id',   p_header_id);
21902      PO_DEBUG.debug_var(l_log_head,l_progress,'p_online_report_id',   p_online_report_id);
21903      PO_DEBUG.debug_var(l_log_head,l_progress,'p_sequence',   p_sequence);
21904   END IF;
21905 
21906   -- Check whether Supplier is debarred or not.
21907 
21908   IF (p_draft_id IS NULL or p_draft_id = -1) THEN
21909 
21910 	SELECT PHA.VENDOR_SITE_ID
21911 	INTO l_vendor_site_id
21912 	FROM PO_HEADERS_ALL PHA
21913 	WHERE PHA.PO_HEADER_ID = p_header_id;
21914 
21915 	ELSE
21916 		SELECT PHA.VENDOR_SITE_ID
21917 		INTO l_vendor_site_id
21918 		FROM PO_HEADERS_DRAFT_ALL PHA
21919 		WHERE PHA.PO_HEADER_ID = p_header_id AND PHA.draft_id = p_draft_id;
21920 
21921 	END IF;
21922 
21923   l_progress := '020';
21924 	PO_DEBUG.debug_var(l_log_head,l_progress,'l_vendor_site_id',   l_vendor_site_id);
21925 
21926 	IF l_vendor_site_id IS NOT NULL THEN
21927 
21928 	SELECT PVSA.DEBARMENT_START_DATE, PVSA.DEBARMENT_END_DATE
21929 	INTO l_debar_start_date, l_debar_end_date
21930 	FROM PO_VENDOR_SITES_ALL PVSA
21931 	WHERE PVSA.VENDOR_SITE_ID =l_vendor_site_id;
21932 
21933   l_progress := '030';
21934 	PO_DEBUG.debug_var(l_log_head,l_progress,'l_debar_start_date',   l_debar_start_date);
21935 	PO_DEBUG.debug_var(l_log_head,l_progress,'l_debar_end_date',   l_debar_end_date);
21936 
21937 		IF (l_debar_start_date IS NULL) THEN
21938 			debar_status := 'N';
21939 				ELSIF (l_debar_start_date > SYSDATE) THEN
21940 				debar_status := 'N';
21941 					ELSIF(l_debar_end_date IS NULL) THEN
21942 					debar_status := 'Y';
21943 						ELSIF (l_debar_end_date > SYSDATE) THEN
21944 						debar_status := 'Y';
21945 						ELSE debar_status := 'N';
21946 		END IF;
21947 	ELSE debar_status := 'N';
21948 	END IF;
21949 
21950 
21951    IF( debar_status = 'Y') THEN
21952    l_progress := '040';
21953 
21954         INSERT INTO PO_ONLINE_REPORT_TEXT_GT
21955         (  online_report_id
21956         ,  last_update_login
21957         ,  last_updated_by
21958         ,  last_update_date
21959         ,  created_by
21960         ,  creation_date
21961         ,  sequence
21962         ,  text_line
21963         ,  message_name
21964         ,  message_type
21965         )
21966         VALUES(
21967            p_online_report_id
21968         ,  p_login_id
21969         ,  p_user_id
21970         ,  SYSDATE
21971         ,  p_user_id
21972         ,  SYSDATE
21973         ,  p_sequence
21974         ,  FND_MESSAGE.GET_STRING('PO', 'PO_SUPPLIER_DEBAR_WARN')
21975         ,  'PO_SUPPLIER_DEBAR_WARN'
21976         ,  'W');
21977 
21978     PO_DEBUG.debug_var(l_log_head,l_progress,'p_sequence',   'inserted');
21979    END IF;
21980 
21981   l_progress := '050';
21982   P_SEQUENCE := P_SEQUENCE + SQL%ROWCOUNT;
21983   X_RETURN_STATUS := FND_API.G_RET_STS_SUCCESS;
21984   PO_DEBUG.debug_var(l_log_head,l_progress,'p_sequence',   p_sequence);
21985   PO_DEBUG.debug_var(l_log_head,l_progress,'x_return_status',   x_return_status);
21986 EXCEPTION
21987   WHEN others THEN
21988     PO_DEBUG.debug_var(l_log_head,l_progress,'p_sequence', 'Supplier Debarred unexpected Error'||sqlcode);
21989     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
21990     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
21991   RETURN;
21992 
21993 END validate_supplier_debarred;
21994 
21995 -------------------------------------------------------------------------------
21996 --Start of Comments
21997 --Name: validate_co_warrant
21998 --Pre-reqs:
21999 --  This procedure validates the contracting officer and warrant
22000 --  at the time of submitting the modification document
22001 --  throw the warning/error message based on the conditions
22002 --End of Comments
22003 -------------------------------------------------------------------------------
22004 PROCEDURE validate_co_warrant(  p_draft_id        IN NUMBER
22005 ,  p_header_id            IN NUMBER
22006 ,  p_contracting_officer  IN NUMBER
22007 ,  p_user_id              IN NUMBER
22008 ,  p_login_id             IN NUMBER
22009 ,  itemtype               IN VARCHAR2 default null
22010 ,  itemkey                IN VARCHAR2 default null
22011 ,  p_online_report_id     IN OUT NOCOPY NUMBER
22012 ,  p_sequence             IN OUT NOCOPY NUMBER
22013 ,  x_return_status        OUT NOCOPY VARCHAR2
22014 )
22015 IS
22016 l_log_head VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.validate_co_warrant';
22017 l_progress VARCHAR2(3) := '000';
22018 l_err_msg_txt   VARCHAR2(3000);
22019 msg_name   VARCHAR2(50) := NULL;
22020 msg_type        VARCHAR2(50) := NULL;
22021 l_isCompletedCount      NUMBER := 0;
22022 l_warrant_enabled_flag  VARCHAR2(1) := 'N';
22023 l_valid_co              VARCHAR2(1) := 'N';
22024 l_type_lookup_code      PO_HEADERS_ALL.type_lookup_code%TYPE;
22025 l_warrant_found         boolean := true;
22026 l_warrant_amount        NUMBER;
22027 l_doc_amount            NUMBER;
22028 l_warrant_currency      VARCHAR2(15);
22029 l_doc_currency          VARCHAR2(15);
22030 l_ko_sign_required      varchar2(1);
22031 l_currency_code         VARCHAR2(15);
22032 l_rate_type             VARCHAR2(25);
22033 l_rate                  PO_HEADERS_ALL.rate%TYPE;
22034 l_rate_date             DATE;
22035 l_mau                   NUMBER;
22036 l_curr_status           VARCHAR2(1);
22037 l_org_id                NUMBER; -- CLM Controls Project changes
22038 BEGIN
22039 
22040     IF itemtype is not NULL and itemkey is not NULL THEN
22041          PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey, l_log_head);
22042          PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey, l_log_head||'p_draft_id'||  p_draft_id);
22043          PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey, l_log_head||'p_header_id'||  p_header_id);
22044          PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey, l_log_head||'p_contracting_officer'|| p_contracting_officer);
22045          PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey, l_log_head||'p_online_report_id'||  p_online_report_id);
22046          PO_WF_DEBUG_PKG.insert_debug( itemtype, itemkey,l_log_head||'p_sequence'||  p_sequence);
22047 
22048     ELSE
22049         IF g_debug_stmt THEN
22050          PO_DEBUG.debug_begin(l_log_head);
22051          PO_DEBUG.debug_var(l_log_head,l_progress,'p_draft_id',   p_draft_id);
22052          PO_DEBUG.debug_var(l_log_head,l_progress,'p_header_id',   p_header_id);
22053          PO_DEBUG.debug_var(l_log_head,l_progress,'p_contracting_officer',  p_contracting_officer);
22054          PO_DEBUG.debug_var(l_log_head,l_progress,'p_online_report_id',   p_online_report_id);
22055          PO_DEBUG.debug_var(l_log_head,l_progress,'p_sequence',   p_sequence);
22056         END IF;
22057     END IF;
22058     X_RETURN_STATUS := FND_API.G_RET_STS_SUCCESS;
22059 
22060     l_progress := '010';
22061     IF p_online_report_id is NULL or p_online_report_id = 0 THEN
22062         SELECT PO_ONLINE_REPORT_TEXT_S.nextval
22063         INTO   p_online_report_id
22064         FROM   sys.dual;
22065         p_sequence := 0;
22066     ELSE
22067         BEGIN
22068         SELECT  Nvl(Max(SEQUENCE), 0)
22069         INTO    p_sequence
22070         FROM    po_online_report_text_gt
22071         WHERE   online_report_id = p_online_report_id;
22072         EXCEPTION
22073             WHEN OTHERS THEN
22074                 p_sequence := 0;
22075         END;
22076     END IF;
22077 
22078     IF p_contracting_officer IS NOT NULL THEN
22079         BEGIN
22080             select 'Y'
22081             into l_valid_co
22082             from PO_BUYERS_VAL_V POB
22083             where pob.employee_id = p_contracting_officer;
22084         EXCEPTION
22085             WHEN OTHERS THEN
22086                 l_valid_co := 'N';
22087         END;
22088 
22089         IF l_valid_co = 'N' THEN
22090             p_sequence := p_sequence + 1;
22091             INSERT INTO po_online_report_text_gt(online_report_id,
22092                 last_update_login,
22093                 last_updated_by,
22094                 last_update_date,
22095                 created_by,
22096                 creation_date,
22097                 sequence,
22098                 text_line,
22099                 message_name
22100                 ,  message_type
22101                 )
22102             SELECT  p_online_report_id,
22103                 p_login_id,
22104                 p_user_id,
22105                 sysdate,
22106                 p_user_id,
22107                 sysdate,
22108                 p_sequence,
22109                 substr(FND_MESSAGE.GET_STRING('PO', 'PO_CO_INACTIVE'),1,240),
22110                 'PO_BUYER_INACTIVE',
22111                 'E'
22112             FROM  dual;
22113 
22114             po_debug.debug_stmt(l_log_head , l_progress, 'Contracting Officer is Invalid');
22115             IF itemtype is not NULL and itemkey is not NULL THEN
22116                 PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey,l_log_head ||  'Contracting Officer is not valid');
22117             END IF;
22118             x_return_status := FND_API.G_RET_STS_ERROR;
22119         END IF; -- l_valid_co = 'N'
22120     END IF; --p_contracting_officer IS NOT NULL
22121 
22122     -- CLM Controls Project changes
22123     BEGIN
22124        SELECT ORG_ID
22125          INTO l_org_id
22126 	 FROM PO_HEADERS_MERGE_V
22127 	WHERE PO_HEADER_ID = p_header_id
22128 	  AND DRAFT_ID = p_draft_id;
22129     EXCEPTION
22130     WHEN NO_DATA_FOUND THEN
22131          l_org_id := NULL;
22132     END;
22133 
22134     l_ko_sign_required := PO_CORE_S.retrieveOptionValue(p_org_id => l_org_id,
22135                                                         p_option_column => PO_CORE_S.g_KO_SIGNATURE_REQD_COL);
22136     --NVL(FND_PROFILE.VALUE('PO_CLM_KO_SIGNATURE_REQD'),'N');
22137     --We need to check the profile determine if the ko signature is required.
22138     IF l_ko_sign_required <> 'Y' THEN
22139     -- if Contract officer signature is not required, then there is no warrant check.
22140         PO_DEBUG.debug_stmt(l_log_head , l_progress, 'Contracting Officer signature is not required');
22141         IF itemtype is not NULL and itemkey is not NULL THEN
22142             PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey,l_log_head ||  'Contracting Officer signature is not required');
22143         END IF;
22144         return;
22145     END IF;
22146 
22147     l_progress := '020';
22148     -- Warrant Validations
22149     -- Contracting Officer validations
22150     IF p_contracting_officer IS NULL THEN
22151         p_sequence := p_sequence + 1;
22152         INSERT INTO PO_ONLINE_REPORT_TEXT_GT
22153         (  online_report_id
22154         ,  last_update_login
22155         ,  last_updated_by
22156         ,  last_update_date
22157         ,  created_by
22158         ,  creation_date
22159         ,  sequence
22160         ,  text_line
22161         ,  message_name
22162         ,  message_type
22163         )
22164         VALUES(
22165            p_online_report_id
22166         ,  p_login_id
22167         ,  p_user_id
22168         ,  SYSDATE
22169         ,  p_user_id
22170         ,  SYSDATE
22171         ,  p_sequence
22172         ,  FND_MESSAGE.GET_STRING('PO', 'PO_CO_IS_NULL')
22173         ,  'PO_CO_IS_NULL'
22174         ,  'E');
22175         PO_DEBUG.debug_stmt(l_log_head , l_progress, 'Contracting Officer is NULL');
22176         IF itemtype is not NULL and itemkey is not NULL THEN
22177             PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey,l_log_head ||  'Contracting officer is NULL');
22178         END IF;
22179         x_return_status := FND_API.G_RET_STS_ERROR;
22180         RETURN;
22181 
22182     END IF; -- p_contracting_officer IS NULL THEN
22183 
22184 
22185 
22186     l_progress := '030';
22187 
22188     BEGIN
22189     l_warrant_enabled_flag := 'N';
22190 
22191     SELECT      'Y'
22192     INTO        l_warrant_enabled_flag
22193     FROM        po_doc_style_headers pdsh,
22194                 po_headers_all pha
22195     WHERE       pha.po_header_id = p_header_id
22196     AND         pha.style_id = pdsh.style_id
22197     AND         pdsh.warrant_type is not NULL;
22198     EXCEPTION
22199         WHEN OTHERS THEN
22200                 null;
22201     END;
22202 
22203     PO_DEBUG.debug_stmt(l_log_head , l_progress, 'Warrant enabled for the style');
22204     IF itemtype is not NULL and itemkey is not NULL THEN
22205         PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey,l_log_head ||  'Warrant enabled for the style');
22206     END IF;
22207     IF l_warrant_enabled_flag = 'Y' THEN
22208         l_progress := '040';
22209 
22210         BEGIN
22211             SELECT      nvl(pwl.warrant_amount, 0), puw.warrant_currency, phm.currency_code,
22212                         phm.type_lookup_code, phm.rate
22213             INTO        l_warrant_amount, l_warrant_currency, l_doc_currency,
22214                         l_type_lookup_code, l_rate
22215             FROM        po_warrant_lines pwl,
22216                         po_user_warrants puw,
22217                         po_doc_style_headers pdsh,
22218                         po_agents pa,
22219                         po_headers_all phm
22220             WHERE       phm.po_header_id       = p_header_id
22221             AND         phm.style_id             = pdsh.style_id
22222             AND         p_contracting_officer    = pa.agent_id
22223             AND         pa.warrant_id            = puw.warrant_id
22224             AND         puw.warrant_status       = 'A'
22225             AND         pwl.warrant_id           = pa.warrant_id
22226             AND         pwl.type_lookup_code     = phm.type_lookup_code
22227             AND         pwl.clm_award_type       = phm.clm_award_type
22228             AND         pwl.warrant_type_code    = pdsh.warrant_type;
22229 
22230         EXCEPTION
22231             WHEN OTHERS THEN
22232                 l_warrant_found := false;
22233             NULL;
22234         END;
22235         IF not l_warrant_found THEN
22236         -- If warrant not found, search for warrants with AWARD TYPE as ALL
22237             l_warrant_found := true;
22238             BEGIN
22239                 SELECT      nvl(pwl.warrant_amount, 0), puw.warrant_currency, phm.currency_code,
22240                             phm.type_lookup_code, phm.rate
22241                 INTO        l_warrant_amount, l_warrant_currency, l_doc_currency,
22242                             l_type_lookup_code, l_rate
22243                 FROM        po_warrant_lines pwl,
22244                             po_user_warrants puw,
22245                             po_doc_style_headers pdsh,
22246                             po_agents pa,
22247                             po_headers_all phm
22248                 WHERE       phm.po_header_id       = p_header_id
22249                 AND         phm.style_id             = pdsh.style_id
22250                 AND         p_contracting_officer    = pa.agent_id
22251                 AND         pa.warrant_id            = puw.warrant_id
22252                 AND         puw.warrant_status       = 'A'
22253                 AND         pwl.warrant_id           = pa.warrant_id
22254                 AND         pwl.type_lookup_code     = phm.type_lookup_code
22255                 AND         pwl.clm_award_type       = 'ALL'
22256                 AND         pwl.warrant_type_code    = pdsh.warrant_type;
22257 
22258             EXCEPTION
22259                 WHEN OTHERS THEN
22260                     l_warrant_found := false;
22261                 NULL;
22262             END;
22263         END IF;
22264 
22265         IF l_warrant_found THEN
22266             PO_DEBUG.debug_stmt(l_log_head , l_progress, 'Warrant found');
22267             PO_DEBUG.debug_stmt(l_log_head , l_progress, 'l_doc_currency '||l_doc_currency);
22268             PO_DEBUG.debug_stmt(l_log_head , l_progress, 'l_type_lookup_code '||l_type_lookup_code);
22269             PO_DEBUG.debug_stmt(l_log_head , l_progress, 'l_rate '||l_rate);
22270             PO_DEBUG.debug_stmt(l_log_head , l_progress, 'l_warrant_currency '||l_warrant_currency);
22271             PO_DEBUG.debug_stmt(l_log_head , l_progress, 'l_warrant_amount '||l_warrant_amount);
22272             IF itemtype is not NULL and itemkey is not NULL THEN
22273                 PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey,l_log_head ||  'Warrant found');
22274                 PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey,l_log_head ||  'l_doc_currency '||l_doc_currency);
22275                 PO_DEBUG.debug_stmt(l_log_head , l_progress, 'l_type_lookup_code '||l_type_lookup_code);
22276                 PO_DEBUG.debug_stmt(l_log_head , l_progress, 'l_rate '||l_rate);
22277                 PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey,l_log_head ||  'l_warrant_currency '||l_warrant_currency);
22278                 PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey,l_log_head ||  'l_doc_amount '||l_doc_amount);
22279             END IF;
22280 
22281             -- Getting the ledger currency
22282             po_core_s2.get_po_currency(p_header_id, l_currency_code, l_doc_currency);
22283             PO_DEBUG.debug_stmt(l_log_head , l_progress, 'ledger currency '||l_currency_code);
22284             IF itemtype is not NULL and itemkey is not NULL THEN
22285                 PO_DEBUG.debug_stmt(l_log_head , l_progress, 'ledger currency '||l_currency_code);
22286             END IF;
22287 
22288             IF l_type_lookup_code = 'STANDARD' THEN
22289                 IF p_draft_id is NULL or p_draft_id = -1 THEN
22290                     -- Finding the document amount
22291                     -- When warrant currency is same as doc currency get the amount in doc currency
22292                     l_doc_amount := po_document_totals_pvt.getAmountOrdered(
22293                                         p_doc_level => po_document_totals_pvt.g_doc_level_HEADER,
22294                                         p_doc_level_id => p_header_id,
22295                                         p_data_source => po_document_totals_pvt.g_data_source_TRANSACTION,
22296                                         p_draft_id => -1);
22297                 ELSE
22298                     -- Finding the mod amount
22299                     l_doc_amount := PO_AME_SETUP_PVT.get_mod_amount_for_warrant(
22300                                                 p_header_id,
22301                                                 p_draft_id);
22302                 END IF;
22303             ELSIF (l_type_lookup_code = 'CONTRACT' or l_type_lookup_code = 'BLANKET') THEN
22304                 IF p_draft_id is NULL or p_draft_id = -1 THEN
22305                     -- Finding the document amount, agreed amount in the case of PA
22306                     SELECT  nvl(BLANKET_TOTAL_AMOUNT, 0)
22307                     INTO    l_doc_amount
22308                     FROM    po_headers_all phm
22309                     WHERE   phm.po_header_id      = p_header_id;
22310                 ELSE
22311                     -- Finding the mod amount, agreed amount in the case of PA
22312                     SELECT  nvl(BLANKET_TOTAL_AMOUNT, 0)
22313                     INTO    l_doc_amount
22314                     FROM    po_headers_draft_all phm
22315                     WHERE   phm.po_header_id      = p_header_id
22316                     AND     phm.draft_id = p_draft_id;
22317                 END IF;
22318             END IF; -- l_type_lookup_code = 'STANDARD' THEN
22319             IF l_doc_currency <> l_warrant_currency AND
22320                l_warrant_currency = l_currency_code THEN
22321             -- When warrant currency is same as ledger currency convert the amount
22322                 l_doc_amount := l_doc_amount * l_rate;
22323             END IF;
22324 
22325             PO_DEBUG.debug_stmt(l_log_head , l_progress, 'l_doc_currency '||l_doc_currency);
22326             PO_DEBUG.debug_stmt(l_log_head , l_progress, 'l_doc_amount '||l_doc_amount);
22327             PO_DEBUG.debug_stmt(l_log_head , l_progress, 'l_warrant_currency '||l_warrant_currency);
22328             PO_DEBUG.debug_stmt(l_log_head , l_progress, 'l_warrant_amount '||l_warrant_amount);
22329             IF itemtype is not NULL and itemkey is not NULL THEN
22330                 PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey,l_log_head ||  'l_doc_currency '||l_doc_currency);
22331                 PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey,l_log_head ||  'l_doc_amount '||l_doc_amount);
22332                 PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey,l_log_head ||  'l_warrant_currency '||l_warrant_currency);
22333                 PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey,l_log_head ||  'l_doc_amount '||l_doc_amount);
22334             END IF;
22335 
22336             IF l_doc_currency <> l_warrant_currency AND l_currency_code <> l_warrant_currency THEN
22337                     PO_DEBUG.debug_stmt(l_log_head , l_progress, 'Warrant not found for the doc currency');
22338                     IF itemtype is not NULL and itemkey is not NULL THEN
22339                         PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey,l_log_head ||  'Warrant not found for the doc currency');
22340                     END IF;
22341                     p_sequence := p_sequence + 1;
22342                     INSERT INTO PO_ONLINE_REPORT_TEXT_GT
22343                     (  online_report_id
22344                     ,  last_update_login
22345                     ,  last_updated_by
22346                     ,  last_update_date
22347                     ,  created_by
22348                     ,  creation_date
22349                     ,  sequence
22350                     ,  text_line
22351                     ,  message_name
22352                     ,  message_type
22353                     )
22354                     VALUES(
22355                        p_online_report_id
22356                     ,  p_login_id
22357                     ,  p_user_id
22358                     ,  SYSDATE
22359                     ,  p_user_id
22360                     ,  SYSDATE
22361                     ,  p_sequence
22362                     ,  FND_MESSAGE.GET_STRING('PO', 'PO_WARRANT_NOT_FOUND_CURR')
22363                     ,  'PO_WARRANT_NOT_FOUND_CURR'
22364                     ,  'E');
22365                     x_return_status := FND_API.G_RET_STS_ERROR;
22366             ELSIF l_warrant_amount < l_doc_amount THEN
22367                 p_sequence := p_sequence + 1;
22368                 INSERT INTO PO_ONLINE_REPORT_TEXT_GT
22369                 (  online_report_id
22370                 ,  last_update_login
22371                 ,  last_updated_by
22372                 ,  last_update_date
22373                 ,  created_by
22374                 ,  creation_date
22375                 ,  sequence
22376                 ,  text_line
22377                 ,  message_name
22378                 ,  message_type
22379                 )
22380                 VALUES(
22381                    p_online_report_id
22382                 ,  p_login_id
22383                 ,  p_user_id
22384                 ,  SYSDATE
22385                 ,  p_user_id
22386                 ,  SYSDATE
22387                 ,  p_sequence
22388                 ,  FND_MESSAGE.GET_STRING('PO', 'PO_WARRANT_NOT_ENOUGH')
22389                 ,  'PO_WARRANT_NOT_ENOUGH'
22390                 ,  'E');
22391 
22392                 IF itemtype is not NULL and itemkey is not NULL THEN
22393                     PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey,l_log_head ||  'Warrant amount less then document amount');
22394                 END IF;
22395                 x_return_status := FND_API.G_RET_STS_ERROR;
22396             END IF;
22397         ELSE
22398             PO_DEBUG.debug_stmt(l_log_head , l_progress, 'Warrant not found');
22399             IF itemtype is not NULL and itemkey is not NULL THEN
22400                 PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey,l_log_head ||  'Warrant not found');
22401             END IF;
22402             p_sequence := p_sequence + 1;
22403             INSERT INTO PO_ONLINE_REPORT_TEXT_GT
22404             (  online_report_id
22405             ,  last_update_login
22406             ,  last_updated_by
22407             ,  last_update_date
22408             ,  created_by
22409             ,  creation_date
22410             ,  sequence
22411             ,  text_line
22412             ,  message_name
22413             ,  message_type
22414             )
22415             VALUES(
22416                p_online_report_id
22417             ,  p_login_id
22418             ,  p_user_id
22419             ,  SYSDATE
22420             ,  p_user_id
22421             ,  SYSDATE
22422             ,  p_sequence
22423             ,  FND_MESSAGE.GET_STRING('PO', 'PO_WARRANT_NOT_FOUND')
22424             ,  'PO_WARRANT_NOT_FOUND'
22425             ,  'E');
22426             x_return_status := FND_API.G_RET_STS_ERROR;
22427 
22428         END IF; -- l_warrant_found THEN
22429 
22430     END IF; -- l_warrant_enabled_flag = 'Y' THEN
22431 
22432 EXCEPTION
22433     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
22434         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
22435     WHEN OTHERS THEN
22436         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
22437     IF ( g_debug_unexp ) THEN
22438        IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
22439               FND_LOG.string( FND_LOG.level_unexpected,
22440                             G_PKG_NAME||l_log_head|| '.others_exception',
22441                             'EXCEPTION: Location is '|| l_progress || ' SQL CODE is '||sqlcode);
22442         END IF;
22443     END IF;
22444 END validate_co_warrant;
22445 
22446 -------------------------------------------------------------------------------
22447 --Start of Comments
22448 --Name: validate_genchangedesc
22449 --Pre-reqs:
22450 --  This procedure validates whether change description is generated or not
22451 --  at the time of submitting the modification document
22452 --  throw the warning/error message based on the conditions
22453 --End of Comments
22454 -------------------------------------------------------------------------------
22455 PROCEDURE validate_genchangedesc(  p_draft_id        IN NUMBER
22456 ,  p_header_id     IN NUMBER
22457 ,  p_online_report_id     IN NUMBER
22458 ,  p_user_id              IN NUMBER
22459 ,  p_login_id             IN NUMBER
22460 ,  p_sequence             IN OUT NOCOPY NUMBER
22461 ,  x_return_status        OUT NOCOPY VARCHAR2
22462 )
22463 IS
22464 
22465 l_return_status VARCHAR2(10) := NULL;
22466 l_log_head VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.validate_genchangedesc';
22467 l_progress VARCHAR2(3) := '000';
22468 mod_last_update_date PO_DRAFTS.last_update_date%type;
22469 changedesc_gen_date PO_DRAFTS.cd_generated_date%type;
22470 changedesc_gen_flag PO_DRAFTS.cd_generated_flag%type;
22471 changedesc_edit_flag PO_DRAFTS.cd_editbyuser_flag%type;
22472 concurrent_request_id PO_DRAFTS.concurrent_reqid%type;
22473 l_err_msg_txt   VARCHAR2(3000);
22474 msg_name   VARCHAR2(50) := NULL;
22475 msg_type   VARCHAR2(50) := NULL;
22476 l_isCompletedCount NUMBER := 0;
22477 
22478 
22479 
22480 BEGIN
22481   IF g_debug_stmt THEN
22482      PO_DEBUG.debug_begin(l_log_head);
22483      PO_DEBUG.debug_var(l_log_head,l_progress,'p_draft_id',   p_draft_id);
22484      PO_DEBUG.debug_var(l_log_head,l_progress,'p_header_id',   p_header_id);
22485      PO_DEBUG.debug_var(l_log_head,l_progress,'p_online_report_id',   p_online_report_id);
22486      PO_DEBUG.debug_var(l_log_head,l_progress,'p_sequence',   p_sequence);
22487   END IF;
22488 
22489   -- Check whether the change description is generated or not
22490   SELECT  cd_generated_flag, concurrent_reqid
22491   INTO    changedesc_gen_flag, concurrent_request_id
22492   FROM    PO_DRAFTS
22493   WHERE   DRAFT_ID = p_draft_id;
22494 
22495   PO_DEBUG.debug_var(l_log_head,l_progress,'changedesc_gen_flag',   changedesc_gen_flag);
22496   PO_DEBUG.debug_var(l_log_head,l_progress,'concurrent_request_id',   concurrent_request_id);
22497 
22498   IF  changedesc_gen_flag IS NULL  THEN
22499   changedesc_gen_flag := 'N';
22500   END IF;
22501 
22502   PO_DEBUG.debug_var(l_log_head,l_progress,'changedesc_gen_flag',   changedesc_gen_flag);
22503   -- If change description is not generated, throw the error message to generate it
22504   IF  changedesc_gen_flag = 'N' THEN
22505     msg_name :='PO_CHG_DESC_GENERATE_ERROR';
22506     msg_type :='E';
22507   ELSE
22508     --fetch the change description generation date and edit by user flag value
22509     SELECT cd_generated_date, cd_editbyuser_flag
22510     INTO   changedesc_gen_date, changedesc_edit_flag
22511     FROM   po_drafts
22512     WHERE  draft_id = p_draft_id;
22513 
22514     PO_DEBUG.debug_var(l_log_head,l_progress,'changedesc_gen_date', changedesc_gen_date);
22515     PO_DEBUG.debug_var(l_log_head,l_progress,'changedesc_edit_flag', changedesc_edit_flag);
22516 
22517     --find the latest date of the modification document
22518     mod_last_update_date := PO_CORE_S.get_last_update_date_for_mod(p_draft_id);
22519 
22520     PO_DEBUG.debug_var(l_log_head,l_progress,'mod_last_update_date', mod_last_update_date);
22521     --IF edited_by_user flag is Y, throw the warning message saying that
22522     -- change description is edited and out of sync with mod document
22523     --ELSE throw warning message just saying that mod document
22524     -- has been updated after change description generated
22525 
22526     IF( mod_last_update_date  > changedesc_gen_date) THEN
22527         msg_type :='W';
22528         IF( changedesc_edit_flag = 'Y') THEN
22529             msg_name :='PO_CHG_DESC_REGENEDITED_WARN';
22530         ELSE
22531             msg_name :='PO_CHG_DESC_REGENERATE_WARN';
22532         END IF;
22533     END IF;
22534 
22535     -- Bug 9950717 - Fix starts
22536     -- If the concurrent request submitted to generate change description is not
22537     -- completed or completed with error, should not allow the user to submit
22538     -- the document
22539     IF concurrent_request_id IS NOT NULL THEN
22540 
22541       PO_DEBUG.debug_var(l_log_head,l_progress,'concurrent_request_id', concurrent_request_id);
22542 
22543       SELECT Count(*)
22544       INTO   l_isCompletedCount
22545       FROM   fnd_concurrent_requests
22546       WHERE  request_id = To_Number(concurrent_request_id)
22547       AND    phase_code = 'C'
22548       AND    status_code = 'C';
22549 
22550       PO_DEBUG.debug_var(l_log_head,l_progress,'l_isCompletedCount', l_isCompletedCount);
22551       IF l_isCompletedCount <= 0 THEN
22552          msg_type :='E';
22553          msg_name :='PO_CHG_DESC_GEN_NOTCOMPLETED';
22554       END IF;
22555 
22556     END IF;
22557     -- Bug 9950717 - Fix ends
22558 
22559   PO_DEBUG.debug_var(l_log_head,l_progress,'changedesc_gen_date', changedesc_gen_date);
22560   PO_DEBUG.debug_var(l_log_head,l_progress,'changedesc_edit_flag', changedesc_edit_flag);
22561 
22562   END IF;
22563 
22564    IF msg_name IS NOT NULL THEN
22565    PO_DEBUG.debug_var(l_log_head,l_progress,'msg_name',   msg_name);
22566    FND_MESSAGE.set_name('PO', msg_name);
22567    l_err_msg_txt := FND_MESSAGE.GET;
22568 
22569 
22570         INSERT INTO PO_ONLINE_REPORT_TEXT_GT
22571         (  online_report_id
22572         ,  last_update_login
22573         ,  last_updated_by
22574         ,  last_update_date
22575         ,  created_by
22576         ,  creation_date
22577         ,  sequence
22578         ,  text_line
22579         ,  message_name
22580         ,  message_type
22581         )
22582         VALUES(
22583            p_online_report_id
22584         ,  p_login_id
22585         ,  p_user_id
22586         ,  SYSDATE
22587         ,  p_user_id
22588         ,  SYSDATE
22589         ,  p_sequence
22590         ,  l_err_msg_txt
22591         ,  msg_name
22592         ,  msg_type);
22593 
22594     PO_DEBUG.debug_var(l_log_head,l_progress,'p_sequence',   'inserted');
22595    END IF;
22596 
22597 
22598   p_sequence := p_sequence + SQL%ROWCOUNT;
22599   x_return_status := FND_API.G_RET_STS_ERROR;
22600   PO_DEBUG.debug_var(l_log_head,l_progress,'p_sequence',   p_sequence);
22601   PO_DEBUG.debug_var(l_log_head,l_progress,'x_return_status',   x_return_status);
22602 EXCEPTION
22603   WHEN others THEN
22604     PO_DEBUG.debug_var(l_log_head,l_progress,'p_sequence', 'SGD Unexpected Error'||sqlcode);
22605     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
22606     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
22607   RETURN;
22608 
22609 END validate_genchangedesc;
22610 
22611 -------------------------------------------------------------------------------
22612 --Start of Comments
22613 --Name: validate_pa_header_ord_constraints
22614 --Pre-reqs:
22615 --  This procedure validates agreements ordering constraints
22616 --End of Comments
22617 -------------------------------------------------------------------------------
22618 PROCEDURE val_pa_ord_constraints(
22619    p_header_id     IN NUMBER
22620 ,  p_online_report_id     IN NUMBER
22621 ,  p_user_id              IN NUMBER
22622 ,  p_login_id             IN NUMBER
22623 ,  p_sequence             IN OUT NOCOPY NUMBER
22624 ,  x_return_status        OUT NOCOPY VARCHAR2
22625 )
22626 IS
22627 
22628 l_return_status VARCHAR2(10) := NULL;
22629 l_log_head VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.validate_pa_header_ord_constraints';
22630 l_progress VARCHAR2(3) := '000';
22631 l_textline  po_online_report_text.text_line%TYPE := NULL;
22632 
22633 BEGIN
22634   l_textline := 'Minimum Guarantee Award Amount Percentage: '|| FND_MESSAGE.GET_STRING('PO', PO_MESSAGE_S.PO_ALL_ENTER_PERCENT);
22635   --FND_MESSAGE.SET_NAME('PO',PO_MESSAGE_S.PO_ALL_ENTER_PERCENT);
22636   --FND_MESSAGE.SET_TOKEN('LINE_LOCATION_TYPE', l_line_loc_token_value);
22637 
22638   --l_textline := FND_MESSAGE.GET;
22639 -- Percentage should be between 0 and 100
22640   INSERT INTO PO_ONLINE_REPORT_TEXT_GT
22641   (  online_report_id
22642   ,  last_update_login
22643   ,  last_updated_by
22644   ,  last_update_date
22645   ,  created_by
22646   ,  creation_date
22647   ,  sequence
22648   ,  text_line
22649   ,  message_name
22650   ,  message_type
22651   )
22652   SELECT
22653      p_online_report_id
22654   ,  p_login_id
22655   ,  p_user_id
22656   ,  SYSDATE
22657   ,  p_user_id
22658   ,  SYSDATE
22659   ,  p_sequence + ROWNUM
22660   ,  substr(l_textline,1,240)
22661   ,  PO_MESSAGE_S.PO_ALL_ENTER_PERCENT
22662   ,  'E'
22663   FROM   PO_HEADERS_GT POH
22664   WHERE  POH.po_header_id = p_header_id
22665   AND   (POH.clm_min_guar_award_amt_percent > 100
22666     OR  POH.clm_min_guar_award_amt_percent < 0);
22667 
22668 -- Maximum Ceiling Award Amount >= Maximum Per Order Amount
22669   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_IDV_MX_CL_AMT_GT_MXPM_CLM');
22670   INSERT INTO PO_ONLINE_REPORT_TEXT_GT
22671   (  online_report_id
22672   ,  last_update_login
22673   ,  last_updated_by
22674   ,  last_update_date
22675   ,  created_by
22676   ,  creation_date
22677   ,  sequence
22678   ,  text_line
22679   ,  message_name
22680   ,  message_type
22681   )
22682   SELECT
22683      p_online_report_id
22684   ,  p_login_id
22685   ,  p_user_id
22686   ,  SYSDATE
22687   ,  p_user_id
22688   ,  SYSDATE
22689   ,  p_sequence + ROWNUM
22690   ,  substr(l_textline,1,240)
22691   ,  'PO_IDV_MX_CL_AMT_GT_MXPM_CLM'
22692   ,  'E'
22693   FROM   PO_HEADERS_GT POH
22694   WHERE  POH.po_header_id = p_header_id
22695   AND    POH.amount_limit < POH.clm_max_order_amount;
22696 
22697 -- Maximum Per Order Amount(Line) <= Maximum Per Order Amount(Header)
22698   l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_IDV_MX_AMT_H_GT_MX_AMT_L');
22699   INSERT INTO PO_ONLINE_REPORT_TEXT_GT
22700   (  online_report_id
22701   ,  last_update_login
22702   ,  last_updated_by
22703   ,  last_update_date
22704   ,  created_by
22705   ,  creation_date
22706   ,  sequence
22707   ,  text_line
22708   ,  message_name
22709   ,  message_type
22710   )
22711   SELECT
22712      p_online_report_id
22713   ,  p_login_id
22714   ,  p_user_id
22715   ,  SYSDATE
22716   ,  p_user_id
22717   ,  SYSDATE
22718   ,  p_sequence + ROWNUM
22719   ,
22720    CASE
22721         WHEN (POH.clm_max_order_amount is not null and POL.clm_max_order_amount is not NULL)
22722         THEN substr(FND_MESSAGE.GET_STRING('PO', 'PO_IDV_MX_AMT_H_GT_MX_AMT_L'), 1, 240)
22723         WHEN (POH.clm_max_order_amount is not null and POL.clm_max_order_amount is NULL)
22724         THEN substr(FND_MESSAGE.GET_STRING('PO', 'PO_IDV_MX_AMT_H_GT_MN_AMT_L'), 1, 240)
22725         WHEN (POH.clm_max_order_amount is null and POL.clm_max_order_amount is not NULL)
22726         THEN substr(FND_MESSAGE.GET_STRING('PO', 'PO_IDV_MXPM_H_GT_MX_AMT_L'), 1, 240)
22727         WHEN (POH.clm_max_order_amount is null and POL.clm_max_order_amount is NULL)
22728         THEN substr(FND_MESSAGE.GET_STRING('PO', 'PO_IDV_MXPM_H_GT_MN_AMT_L'), 1, 240)
22729    END,
22730    CASE
22731         WHEN (POH.clm_max_order_amount is not null and POL.clm_max_order_amount is not NULL)
22732         THEN 'PO_IDV_MX_AMT_H_GT_MX_AMT_L'
22733         WHEN (POH.clm_max_order_amount is not null and POL.clm_max_order_amount is NULL)
22734         THEN 'PO_IDV_MX_AMT_H_GT_MN_AMT_L'
22735         WHEN (POH.clm_max_order_amount is null and POL.clm_max_order_amount is not NULL)
22736         THEN 'PO_IDV_MXPM_H_GT_MX_AMT_L'
22737         WHEN (POH.clm_max_order_amount is null and POL.clm_max_order_amount is NULL)
22738         THEN 'PO_IDV_MXPM_H_GT_MN_AMT_L'
22739    END
22740   ,  'E'
22741   FROM   PO_HEADERS_GT POH, PO_LINES_GT POL
22742   WHERE  POH.po_header_id = p_header_id
22743   AND    POH.po_header_id = POL.po_header_id
22744   AND    nvl(POH.clm_max_order_amount, POH.amount_limit) < nvl(POL.clm_max_order_amount, POL.clm_min_order_amount);
22745 
22746 END val_pa_ord_constraints;
22747 
22748 PROCEDURE PO_ACCOUNTS_CHECK(P_DOCUMENT_ID           IN NUMBER,
22749 			    P_DOCUMENT_TYPE          IN VARCHAR2,
22750                             P_ONLINE_REPORT_ID       IN NUMBER,
22751                             P_USER_ID                IN NUMBER,
22752                             P_LOGIN_ID               IN NUMBER,
22753                             P_SEQUENCE               IN OUT NOCOPY NUMBER,
22754                             X_RETURN_STATUS          OUT NOCOPY VARCHAR2,
22755                             x_msg_data             OUT NOCOPY VARCHAR2) IS
22756 
22757 
22758 L_TEXTLINE  PO_ONLINE_REPORT_TEXT.TEXT_LINE%TYPE := NULL;
22759 L_API_NAME  CONSTANT VARCHAR2(40) := 'PO_ACCOUNTS_CHECK';
22760 L_PROGRESS VARCHAR2(3);
22761 
22762 BEGIN
22763   L_PROGRESS := '000';
22764 
22765   L_PROGRESS := '001';
22766   IF G_DEBUG_STMT THEN
22767      PO_DEBUG.DEBUG_STMT(G_LOG_HEAD || '.'||L_API_NAME||'.',
22768                               L_PROGRESS,'PO_CHARGE_NOT_NULL');
22769   END IF;
22770 
22771   IF  p_document_type IN ('PO') THEN
22772      l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_CHARGE_NOT_NULL');
22773 
22774      INSERT INTO PO_ONLINE_REPORT_TEXT_GT
22775                   (ONLINE_REPORT_ID,
22776                    LAST_UPDATE_LOGIN,
22777                    LAST_UPDATED_BY,
22778                    LAST_UPDATE_DATE,
22779                    CREATED_BY,
22780                    CREATION_DATE,
22781                    LINE_NUM,
22782                    SHIPMENT_NUM,
22783                    DISTRIBUTION_NUM,
22784                    SEQUENCE,
22785                    TEXT_LINE,
22786                    MESSAGE_NAME)
22787        SELECT P_ONLINE_REPORT_ID,
22788               P_LOGIN_ID,
22789               P_USER_ID,
22790               SYSDATE,
22791               P_USER_ID,
22792               SYSDATE,
22793               POL.LINE_NUM,
22794               PLL.SHIPMENT_NUM,
22795               POD.DISTRIBUTION_NUM,
22796               P_SEQUENCE + ROWNUM,
22797               SUBSTR(g_linemsg||g_delim||POL.line_num_display||g_delim
22798                    ||g_shipmsg||g_delim||PLL.shipment_num||g_delim||g_distmsg||g_delim||POD.DISTRIBUTION_NUM||g_delim||L_TEXTLINE,1,240),
22799               'PO_CHARGE_NOT_NULL'
22800        FROM
22801           PO_DISTRIBUTIONS_GT POD,
22802           PO_LINE_LOCATIONS_GT PLL,
22803           PO_LINES_GT POL
22804        WHERE  POD.CODE_COMBINATION_ID IS NULL
22805         AND   POD.PO_HEADER_ID = P_DOCUMENT_ID
22806         AND   NVL(POD.DISTRIBUTION_TYPE, 'STANDARD') <> 'AGREEMENT'
22807         AND   POD.LINE_LOCATION_ID = PLL.LINE_LOCATION_ID
22808         AND   PLL.PO_LINE_ID = POL.PO_LINE_ID;
22809 
22810      P_SEQUENCE := P_SEQUENCE + SQL%ROWCOUNT;
22811 
22812      L_PROGRESS := '002';
22813      IF G_DEBUG_STMT THEN
22814         PO_DEBUG.DEBUG_STMT(G_LOG_HEAD || '.'||L_API_NAME||'.',
22815                               L_PROGRESS,'PO_ALL_NO_BUDGET_FLEX');
22816      END IF;
22817 
22818      IF  (PO_CORE_S.is_encumbrance_on(
22819               p_doc_type => g_document_type_PO
22820            ,  p_org_id => NULL)) THEN
22821 
22822         l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_ALL_NO_BUDGET_FLEX');
22823         INSERT INTO PO_ONLINE_REPORT_TEXT_GT
22824                   (ONLINE_REPORT_ID,
22825                    LAST_UPDATE_LOGIN,
22826                    LAST_UPDATED_BY,
22827                    LAST_UPDATE_DATE,
22828                    CREATED_BY,
22829                    CREATION_DATE,
22830                    LINE_NUM,
22831                    SHIPMENT_NUM,
22832                    DISTRIBUTION_NUM,
22833                    SEQUENCE,
22834                    TEXT_LINE,
22835                    MESSAGE_NAME)
22836         SELECT P_ONLINE_REPORT_ID,
22837               P_LOGIN_ID,
22838               P_USER_ID,
22839               SYSDATE,
22840               P_USER_ID,
22841               SYSDATE,
22842               POL.LINE_NUM,
22843               PLL.SHIPMENT_NUM,
22844               POD.DISTRIBUTION_NUM,
22845               P_SEQUENCE + ROWNUM,
22846               SUBSTR(g_linemsg||g_delim||POL.line_num_display||g_delim
22847                    ||g_shipmsg||g_delim||PLL.shipment_num||g_delim||g_distmsg||g_delim||POD.DISTRIBUTION_NUM||g_delim||L_TEXTLINE,1,240),
22848               'PO_ALL_NO_BUDGET_FLEX'
22849         FROM
22850           PO_DISTRIBUTIONS_GT POD,
22851           PO_LINE_LOCATIONS_GT PLL,
22852           PO_LINES_GT POL
22853         WHERE  POD.BUDGET_ACCOUNT_ID IS NULL
22854          AND   POD.PO_HEADER_ID = P_DOCUMENT_ID
22855          AND   NVL(POD.DISTRIBUTION_TYPE, 'STANDARD') <> 'AGREEMENT'
22856          AND   Nvl(PREVENT_ENCUMBRANCE_FLAG,'N') = 'N'
22857          AND   POD.LINE_LOCATION_ID = PLL.LINE_LOCATION_ID
22858          AND   PLL.PO_LINE_ID = POL.PO_LINE_ID;
22859 
22860         P_SEQUENCE := P_SEQUENCE + SQL%ROWCOUNT;
22861 
22862      END IF;
22863   END IF;
22864 
22865   X_RETURN_STATUS := FND_API.G_RET_STS_SUCCESS;
22866 
22867 EXCEPTION
22868   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
22869     X_MSG_DATA := FND_MSG_PUB.GET(P_MSG_INDEX => FND_MSG_PUB.G_LAST,
22870                                   P_ENCODED => 'F');
22871     X_RETURN_STATUS := FND_API.G_RET_STS_UNEXP_ERROR;
22872 
22873   WHEN FND_API.G_EXC_ERROR THEN
22874     X_MSG_DATA := FND_MSG_PUB.GET(P_MSG_INDEX => FND_MSG_PUB.G_LAST,
22875                                   P_ENCODED => 'F');
22876     X_RETURN_STATUS := FND_API.G_RET_STS_ERROR;
22877 
22878   WHEN OTHERS THEN
22879     IF FND_MSG_PUB.CHECK_MSG_LEVEL(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
22880       FND_MSG_PUB.ADD_EXC_MSG(G_PKG_NAME, L_API_NAME);
22881     END IF;
22882 
22883     IF (G_DEBUG_UNEXP) THEN
22884       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
22885         FND_LOG.STRING(FND_LOG.LEVEL_UNEXPECTED, G_LOG_HEAD ||
22886                      L_API_NAME || '.OTHERS_EXCEPTION', 'EXCEPTION: LOCATION IS '
22887                      || L_PROGRESS || ' SQL CODE IS '||SQLCODE);
22888       END IF;
22889     END IF;
22890 
22891     X_MSG_DATA := FND_MSG_PUB.GET(P_MSG_INDEX => FND_MSG_PUB.G_LAST,
22892                                   P_ENCODED => 'F');
22893     X_RETURN_STATUS := FND_API.G_RET_STS_UNEXP_ERROR;
22894 
22895 END PO_ACCOUNTS_CHECK;
22896 
22897 
22898  -- Bug 10218898
22899  PROCEDURE PO_UOM_CHECK(P_DOCUMENT_ID		IN NUMBER,
22900  		       P_DOCUMENT_TYPE          IN VARCHAR2,
22901                         P_ONLINE_REPORT_ID	IN NUMBER,
22902                         P_USER_ID		IN NUMBER,
22903                         P_LOGIN_ID		IN NUMBER,
22904                         P_SEQUENCE		IN OUT NOCOPY NUMBER,
22905                         X_RETURN_STATUS		OUT NOCOPY VARCHAR2
22906                          ,x_msg_data             OUT NOCOPY VARCHAR2) IS
22907 
22908 
22909   L_TEXTLINE  PO_ONLINE_REPORT_TEXT.TEXT_LINE%TYPE := NULL;
22910  L_API_NAME  CONSTANT VARCHAR2(40) := 'PO_UOM_CHECK';
22911  L_PROGRESS VARCHAR2(3);
22912  TYPE CCID_TYPE IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
22913  L_CCID CCID_TYPE;
22914 
22915  BEGIN
22916  L_PROGRESS := '000';
22917 
22918  L_PROGRESS := '001';
22919    IF G_DEBUG_STMT THEN
22920            PO_DEBUG.DEBUG_STMT(G_LOG_HEAD || '.'||L_API_NAME||'.',
22921                                L_PROGRESS,'PO_LINE_SHIP_UOM_MISMATCH');
22922         END IF;
22923 
22924    l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_LINE_SHIP_UOM_MISMATCH');
22925   IF P_DOCUMENT_TYPE ='RELEASE'   THEN
22926 
22927    INSERT INTO PO_ONLINE_REPORT_TEXT_GT
22928                    (ONLINE_REPORT_ID,
22929                     LAST_UPDATE_LOGIN,
22930                     LAST_UPDATED_BY,
22931                     LAST_UPDATE_DATE,
22932                     CREATED_BY,
22933                     CREATION_DATE,
22934                        LINE_NUM,
22935                     SHIPMENT_NUM,
22936                     DISTRIBUTION_NUM,
22937                     SEQUENCE,
22938                     TEXT_LINE,
22939                     MESSAGE_NAME)
22940        SELECT P_ONLINE_REPORT_ID,
22941                P_LOGIN_ID,
22942                P_USER_ID,
22943                SYSDATE,
22944                P_USER_ID,
22945                SYSDATE,
22946                POL.LINE_NUM,
22947                PLL.SHIPMENT_NUM,
22948                0,
22949                P_SEQUENCE + ROWNUM,
22950                PO_CORE_S.get_translated_text
22951                              ('PO_LINE_SHIP_UOM_MISMATCH'
22952                              ,   'LINE_NUM',  POL.LINE_NUM
22953                              ,   'SHIP_NUM',  PLL.SHIPMENT_NUM
22954                               ),
22955  	      'PO_LINE_SHIP_UOM_MISMATCH'
22956 
22957         FROM
22958             PO_LINE_LOCATIONS_ALL PLL,
22959  	    	  PO_LINES_ALL POL
22960         WHERE  POL.PO_LINE_ID=PLL.PO_LINE_ID
22961  	     AND NVL(PLL.unit_meas_lookup_code,-1) <> NVL(POL.unit_meas_lookup_code,-1)
22962           AND   PLL.PO_RELEASE_ID = P_DOCUMENT_ID;
22963 
22964  ELSIF P_DOCUMENT_TYPE ='PO'   THEN  -- Bug 12760632 [Restricting UOM mismatch check for Agreements]
22965 
22966   INSERT INTO PO_ONLINE_REPORT_TEXT_GT
22967                    (ONLINE_REPORT_ID,
22968                     LAST_UPDATE_LOGIN,
22969                     LAST_UPDATED_BY,
22970                     LAST_UPDATE_DATE,
22971                     CREATED_BY,
22972                     CREATION_DATE,
22973                     LINE_NUM,
22974                     SHIPMENT_NUM,
22975                     DISTRIBUTION_NUM,
22976                     SEQUENCE,
22977                     TEXT_LINE,
22978                     MESSAGE_NAME)
22979        SELECT P_ONLINE_REPORT_ID,
22980                P_LOGIN_ID,
22981                P_USER_ID,
22982                SYSDATE,
22983                P_USER_ID,
22984                SYSDATE,
22985                POL.LINE_NUM,
22986                PLL.SHIPMENT_NUM,
22987                0,
22988                P_SEQUENCE + ROWNUM,
22989                PO_CORE_S.get_translated_text
22990                              ('PO_LINE_SHIP_UOM_MISMATCH'
22991                              ,   'LINE_NUM',  POL.LINE_NUM
22992                              ,   'SHIP_NUM',  PLL.SHIPMENT_NUM
22993                               ),
22994  	      'PO_LINE_SHIP_UOM_MISMATCH'
22995         FROM
22996            PO_LINE_LOCATIONS_ALL PLL,
22997                   PO_LINES_ALL POL
22998         WHERE  POL.PO_LINE_ID=PLL.PO_LINE_ID
22999         AND NVL(PLL.unit_meas_lookup_code,-1)<>NVL(POL.unit_meas_lookup_code,-1)
23000         -- bug 12597958 : bypassing check for amount based lines.
23001         AND (PLL.VALUE_BASIS not in ('FIXED PRICE','AMOUNT')  -- Bug 12332819 # Do not consider Advance and Fixed Priced Shipments
23002               AND (PLL.PAYMENT_TYPE IS  NULL   -- Consider Non Complex PO shipments with value basis <> fixed price
23003                     OR PLL.PAYMENT_TYPE <>'RATE')) --Bug 12332819 #  Do not consider the Rate type Pay items
23004         AND POL.PO_HEADER_ID = P_DOCUMENT_ID  ;
23005 
23006    END IF;
23007 
23008      P_SEQUENCE := P_SEQUENCE + SQL%ROWCOUNT;
23009      X_RETURN_STATUS := FND_API.G_RET_STS_SUCCESS;
23010 
23011  EXCEPTION
23012    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
23013      X_MSG_DATA := FND_MSG_PUB.GET(P_MSG_INDEX => FND_MSG_PUB.G_LAST,
23014                                    P_ENCODED => 'F');
23015      X_RETURN_STATUS := FND_API.G_RET_STS_UNEXP_ERROR;
23016 
23017    WHEN FND_API.G_EXC_ERROR THEN
23018      X_MSG_DATA := FND_MSG_PUB.GET(P_MSG_INDEX => FND_MSG_PUB.G_LAST,
23019                                    P_ENCODED => 'F');
23020      X_RETURN_STATUS := FND_API.G_RET_STS_ERROR;
23021 
23022    WHEN OTHERS THEN
23023      IF FND_MSG_PUB.CHECK_MSG_LEVEL(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
23024        FND_MSG_PUB.ADD_EXC_MSG(G_PKG_NAME, L_API_NAME);
23025      END IF;
23026 
23027      IF (G_DEBUG_UNEXP) THEN
23028        IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
23029          FND_LOG.STRING(FND_LOG.LEVEL_UNEXPECTED, G_LOG_HEAD ||
23030                       L_API_NAME || '.OTHERS_EXCEPTION', 'EXCEPTION: LOCATION IS '
23031                       || L_PROGRESS || ' SQL CODE IS '||SQLCODE);
23032        END IF;
23033      END IF;
23034 
23035      X_MSG_DATA := FND_MSG_PUB.GET(P_MSG_INDEX => FND_MSG_PUB.G_LAST,
23036                                    P_ENCODED => 'F');
23037      X_RETURN_STATUS := FND_API.G_RET_STS_UNEXP_ERROR;
23038 
23039  END;
23040 
23041 
23042 
23043  --<bug13059027 START>
23044 -- Checks if the PO is:
23045 -- CLM Document and
23046 -- has been approved at least once (Modification or Re-approval)
23047 FUNCTION is_approved_clm_document (p_document_id IN NUMBER)
23048 RETURN VARCHAR2
23049 IS
23050   l_is_approved_clm_document VARCHAR2(1) := 'N';
23051 BEGIN
23052 
23053   -- check if doc is CLM Modification or CLM Reapproval Base document
23054   SELECT 'Y'
23055   INTO   l_is_approved_clm_document
23056   FROM   po_headers_gt POH1,
23057          po_headers_all POH,
23058          po_doc_style_headers PDSH
23059   WHERE  POH1.po_header_id = p_document_id
23060   AND    POH1.po_header_id = POH.po_header_id
23061   AND    POH.style_id = PDSH.style_id
23062   AND    Nvl(PDSH.clm_flag,'N') = 'Y'
23063   AND    (Nvl(POH1.draft_id, -1) <> -1 OR -- Modification
23064           POH1.approved_date IS NOT NULL); -- ReApproval
23065 
23066   RETURN(l_is_approved_clm_document);
23067 
23068 EXCEPTION
23069   WHEN No_Data_Found THEN
23070     RETURN 'N';
23071   WHEN OTHERS THEN
23072     RETURN 'N';
23073 END is_approved_clm_document;
23074 --<bug13059027 END>
23075 
23076 PROCEDURE PO_ACRN_CHECK(P_DOCUMENT_ID      IN NUMBER,
23077                         P_DRAFT_ID         IN NUMBER,
23078                         P_DOCUMENT_TYPE    IN VARCHAR2,
23079                         P_ONLINE_REPORT_ID IN NUMBER,
23080                         P_USER_ID          IN NUMBER,
23081                         P_LOGIN_ID         IN NUMBER,
23082                         P_SEQUENCE         IN OUT NOCOPY NUMBER,
23083                         X_RETURN_STATUS    OUT NOCOPY VARCHAR2,
23084                         x_msg_data         OUT NOCOPY VARCHAR2) IS
23085 
23086   L_API_NAME CONSTANT VARCHAR2(40) := 'PO_ACRN_CHECK';
23087   L_PROGRESS VARCHAR2(3);
23088   l_key number;
23089   l_loas varchar2(100);
23090   l_charge_acc varchar2(100);
23091 Begin
23092 
23093 l_key      := PO_CORE_S.get_session_gt_nextval;
23094 
23095 
23096 /*Collecting the Charge Accounts, LOAs and the ACRN values populated
23097   into session gt for validation. */
23098 INSERT INTO po_session_gt
23099   (KEY,num1,char1,char2,char3,char5
23100   )
23101 SELECT l_key,
23102   po_distribution_id,
23103   charge_acc,
23104   clm_misc_loa,
23105   NVL(clm_misc_loa,charge_acc) gen_val,
23106   acrn
23107 FROM
23108   (SELECT pda.po_distribution_id,
23109     glcc.concatenated_segments charge_acc,
23110     pda.clm_misc_loa ,
23111     pda.acrn
23112   FROM po_distributions_merge_v pda,
23113   gl_code_combinations_kfv glcc
23114   WHERE pda.po_header_id = P_DOCUMENT_ID
23115   and pda.draft_id = P_DRAFT_ID
23116   AND pda.code_combination_id = glcc.code_combination_id
23117   );
23118 
23119   L_PROGRESS := '001';
23120   IF G_DEBUG_STMT THEN
23121     PO_DEBUG.DEBUG_STMT(G_LOG_HEAD || '.' || L_API_NAME || '.',
23122                         L_PROGRESS,
23123                         'PO_CHARGE_NOT_NULL');
23124   END IF;
23125 
23126 /*
23127 
23128 This query will find out the distinct Charge accounts and LOAs which are using same ACRN
23129 and will insert them into po_online_report_text_gt to display error message.
23130 Logic of the query is as shown below
23131 Data:
23132 
23133  CHARGE_ACC |  LOA   |  ACRN
23134     A            B       AA
23135 	A                    AA
23136 	A            D       AK
23137 	B                    AB
23138 	C            D       AB
23139 	E                    AA
23140 	F            A       AA
23141 
23142 Stage 1 of the query gets data in this format
23143 
23144 ACRN  |   CHARGE/LOA |  VALUES
23145 AA          CHARGE_ACC   A,E
23146 AA          LOA          B,A
23147 
23148 Stage 2 of the query gets the data in this format
23149 
23150 ACRN | ERROR_MESSAGE
23151 AA      CHARGE_ACC A,E and LOA B,A
23152 
23153 These two values will be pased to form the message PO_DUPLICATE_ACRN which
23154 will be inserted into the po_online_report_text_gt
23155 */
23156 --Extracting the names from the
23157 l_loas := PO_CORE_S.get_translated_text('PO_LOA_DISPLAY');
23158 l_charge_acc := PO_CORE_S.get_translated_text('PO_CHARGE_ACC_DISP');
23159 
23160 INSERT INTO PO_ONLINE_REPORT_TEXT_GT
23161           (ONLINE_REPORT_ID,
23162            LAST_UPDATE_LOGIN,
23163            LAST_UPDATED_BY,
23164            LAST_UPDATE_DATE,
23165            CREATED_BY,
23166            CREATION_DATE,
23167            SEQUENCE,
23168            TEXT_LINE,
23169            MESSAGE_NAME)
23170           SELECT P_ONLINE_REPORT_ID,
23171                  P_LOGIN_ID,
23172                  P_USER_ID,
23173                  SYSDATE,
23174                  P_USER_ID,
23175                  SYSDATE,
23176                  P_SEQUENCE + ROWNUM,
23177                  PO_CORE_S.get_translated_text
23178                                      (   'PO_DUPLICATE_ACRN'
23179                                      ,   'ACRN', acrn
23180                                      ,   'CHARGE_LOA',ERROR_MESSAGE
23181                                      ),
23182                  'PO_DUPLICATE_ACRN'
23183           from
23184           (SELECT Acrn,
23185   error_message
23186 FROM
23187   (SELECT acrn,
23188     MAX(ltrim(sys_connect_by_path(gen_vals,' and '),' and ')) error_message
23189   FROM
23190     (SELECT acrn acrn ,
23191       c_l
23192       || MAX(ltrim(sys_connect_by_path(gen_value,', '),', ')) gen_vals
23193     FROM
23194       (SELECT char5 acrn,
23195         (
23196         CASE
23197           WHEN char2 IS NOT NULL
23198           THEN l_loas
23199           ELSE l_charge_acc
23200         END ) c_l,
23201         CHAR3 Gen_value
23202       FROM po_session_gt
23203 	  where KEY = l_key
23204       )
23205       CONNECT BY NOCYCLE prior acrn=acrn
23206     AND prior Gen_value           <> Gen_value
23207     AND prior c_l                  = c_l
23208     GROUP BY acrn,
23209       c_l
23210     HAVING acrn IS NOT NULL
23211     )
23212     CONNECT BY NOCYCLE prior acrn=acrn
23213   AND prior gen_vals            <> gen_vals
23214   GROUP BY acrn
23215   )
23216 WHERE
23217 (
23218 instr(error_message,',') <> 0
23219 OR
23220 ( instr(error_message,',') = 0
23221   and instr(error_message,'and') <> 0
23222 ))
23223 );
23224 
23225 P_SEQUENCE := p_sequence +  SQL%ROWCOUNT;
23226 
23227 L_PROGRESS := '002';
23228   IF G_DEBUG_STMT THEN
23229     PO_DEBUG.DEBUG_STMT(G_LOG_HEAD || '.' || L_API_NAME || '.',
23230                         L_PROGRESS,
23231                         'PO_CHARGE_NOT_NULL');
23232   END IF;
23233 
23234 /*
23235 
23236 This query will find out if a Charge account or LOA is being assigned
23237 multiple ACRNS
23238 Logic of the query is as shown below
23239 Data:
23240 
23241  CHARGE_ACC |  LOA   |  ACRN
23242     A            B       AA
23243 	A                    AA
23244 	A                    AK
23245 	B            D       AB
23246 	C            D       AE
23247 	E                    AA
23248 	F            A       AA
23249 
23250 Stage 1 of the query gets data in this format
23251 
23252 CHARGE/LOA   | VALUE   |  ACRNS
23253 CHARGE           A        AA,AK
23254 LOA              D        AB,AE
23255 Stage 2 of the query gets the data in this format
23256 
23257 CHARGE_LOA			|	ACRNS
23258 Charge Account A        AA,AK
23259 LOA D                   AB,AE
23260 These two values will be pased to form the message PO_DUPLICATE_ACRN which
23261 will be inserted into the po_online_report_text_gt
23262 */
23263 
23264 INSERT
23265 INTO PO_ONLINE_REPORT_TEXT_GT
23266   (
23267     ONLINE_REPORT_ID,
23268     LAST_UPDATE_LOGIN,
23269     LAST_UPDATED_BY,
23270     LAST_UPDATE_DATE,
23271     CREATED_BY,
23272     CREATION_DATE,
23273     SEQUENCE,
23274     TEXT_LINE,
23275     MESSAGE_NAME
23276   )
23277 SELECT P_ONLINE_REPORT_ID,
23278   P_LOGIN_ID,
23279   P_USER_ID,
23280   SYSDATE,
23281   P_USER_ID,
23282   SYSDATE,
23283   P_SEQUENCE + ROWNUM,
23284   PO_CORE_S.get_translated_text ( 'PO_ACRN_CHARGE_ACCOUNT' , 'CHARGE_LOA', c_l , 'ACRNS',ERROR_MESSAGE ),
23285   'PO_ACRN_CHARGE_ACCOUNT'
23286 FROM -- This part of the query takes the o/p from below query, inserts into gt
23287   (SELECT c_l
23288     || ' '
23289     || gen_val c_l,
23290     acrns error_message
23291   FROM
23292     (SELECT gen_val,
23293       c_l,
23294       MAX(ltrim(sys_connect_by_path(acrn,', '),', ')) acrns
23295     FROM
23296       (SELECT char3 gen_val,
23297         (
23298         CASE -- case statement to identify whether or not it is a loa
23299           WHEN char2 IS NOT NULL
23300           THEN l_loas
23301           ELSE l_charge_acc
23302         END) c_l,
23303         char5 acrn
23304       FROM po_session_gt
23305       WHERE char5 IS NOT NULL
23306 	  and  KEY = l_key
23307       )-- This part of the sql selects the gen value and identifies it as loa/ca
23308       CONNECT BY NOCYCLE prior gen_val=gen_val
23309     AND prior acrn                   <> acrn
23310     AND prior c_l                     = c_l
23311     GROUP BY gen_val,
23312       c_l
23313     )-- This part of the sql joins the result with its previous rows and forms
23314 	-- comma seperated values.
23315   WHERE instr(acrns,',') <> 0
23316   );
23317 
23318 p_sequence := p_sequence + SQL%ROWCOUNT;
23319 
23320 L_PROGRESS := '003';
23321   IF G_DEBUG_STMT THEN
23322     PO_DEBUG.DEBUG_STMT(G_LOG_HEAD || '.' || L_API_NAME || '.',
23323                         L_PROGRESS,
23324                         'PO_CHARGE_NOT_NULL');
23325   END IF;
23326 
23327   X_RETURN_STATUS := FND_API.G_RET_STS_SUCCESS;
23328 
23329 EXCEPTION
23330   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
23331     X_MSG_DATA      := FND_MSG_PUB.GET(P_MSG_INDEX => FND_MSG_PUB.G_LAST,
23332                                        P_ENCODED   => 'F');
23333     X_RETURN_STATUS := FND_API.G_RET_STS_UNEXP_ERROR;
23334 
23335   WHEN FND_API.G_EXC_ERROR THEN
23336     X_MSG_DATA      := FND_MSG_PUB.GET(P_MSG_INDEX => FND_MSG_PUB.G_LAST,
23337                                        P_ENCODED   => 'F');
23338     X_RETURN_STATUS := FND_API.G_RET_STS_ERROR;
23339 
23340   WHEN OTHERS THEN
23341     IF FND_MSG_PUB.CHECK_MSG_LEVEL(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
23342       FND_MSG_PUB.ADD_EXC_MSG(G_PKG_NAME, L_API_NAME);
23343     END IF;
23344 
23345     IF (G_DEBUG_UNEXP) THEN
23346       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
23347         FND_LOG.STRING(FND_LOG.LEVEL_UNEXPECTED,
23348                        G_LOG_HEAD || L_API_NAME || '.OTHERS_EXCEPTION',
23349                        'EXCEPTION: LOCATION IS ' || L_PROGRESS ||
23350                        ' SQL CODE IS ' || SQLCODE);
23351       END IF;
23352     END IF;
23353 
23354     X_MSG_DATA      := FND_MSG_PUB.GET(P_MSG_INDEX => FND_MSG_PUB.G_LAST,
23355                                        P_ENCODED   => 'F');
23356     X_RETURN_STATUS := FND_API.G_RET_STS_UNEXP_ERROR;
23357 
23358 END PO_ACRN_CHECK;
23359 
23360 
23361 ---Added for bug12951645
23362 /**
23363 * Procedure: CHECK_CLOSE_WIP_JOB
23364 * Requires:
23365 *   IN PARAMETERS:
23366 *       p_document_id:      The requisition_header_id of submitted document
23367 *       p_document_type     Document type.  Use the g_doc_type_<> variables, where <> is:
23368 *				--    REQUISITION
23369 *				--    PA
23370 *				--    PO
23371 *				--    RELEASE
23372 *       p_online_report_id: Id used to INSERT INTO online_report_text table
23373 *       p_user_id:          User performing the action
23374 *       p_login_id:         Last update login_id
23375 *   IN OUT PARAMETERS
23376 *       p_sequence:          Sequence number of last reported error
23377 * Modifies: Inserts error msgs in online_report_text_gt table, uses
23378 *           global_temp tables for processing
23379 * Effects:  This procedure runs the document submission checks for all documents
23380 * Returns:
23381 *  p_sequence: This parameter contains the current count of number of error
23382 *              messages inserted
23383 -- Updated for bug#15939036
23384 -- Details:
23385 --   Instead of checking the WO invalid state from view WIP_OSP_JOBSVAL_V
23386 --   checking the status from WIP_DISCRETE_JOBS table.Whichever WO is not in
23387 --   status 3,4,6 are treated as invalid WO's.As the validation from view is failing for
23388 --   some flows when WO is created from EAM.Please refer bug for further details.
23389 */
23390   PROCEDURE CHECK_CLOSE_WIP_JOB(p_document_id                   IN NUMBER,
23391                                 p_document_type                 IN  VARCHAR2,
23392                                 p_online_report_id              IN NUMBER,
23393                                 p_user_id                        IN NUMBER,
23394                                 p_login_id                        IN NUMBER,
23395                                 p_sequence                        IN OUT NOCOPY NUMBER,
23396                                 x_return_status                        OUT NOCOPY VARCHAR2
23397                                 )  IS
23398    l_progress NUMBER := 0;
23399    l_document_id NUMBER := 0;
23400    l_api_name CONSTANT VARCHAR2(30) := 'CHECK_CLOSE_WIP_JOB';
23401    l_text_line PO_ONLINE_REPORT_TEXT.text_line%TYPE := NULL;
23402    l_log_head VARCHAR2(70) := 'po.plsql.PO_DOCUMENT_CHECKS_PVT.CHECK_CLOSE_WIP_JOB';
23403 BEGIN
23404 
23405    l_text_line := FND_MESSAGE.GET_STRING('PO', 'PO_CLOSE_WIP_JOB');
23406   --Start of Process
23407   --When document type is REQUISITION
23408   IF (p_document_type =  g_document_type_REQUISITION) THEN
23409   l_progress := 10;
23410      --Why:To check whether passed document has its associated WIP JOB closed or not?
23411 
23412         INSERT INTO PO_ONLINE_REPORT_TEXT_GT
23413            (  online_report_id
23414            ,  last_update_login
23415            ,  last_updated_by
23416            ,  last_update_date
23417            ,  created_by
23418            ,  creation_date
23419            ,  line_num
23420            ,  shipment_num
23421            ,  distribution_num
23422            ,  sequence
23423            ,  text_line
23424            ,  message_name
23425            )
23426            SELECT p_online_report_id,
23427            p_login_id,
23428            p_user_id,
23429            SYSDATE,
23430            p_user_id,
23431            SYSDATE,
23432            prl.line_num,
23433            0,
23434            0,
23435            p_sequence+ ROWNUM,
23436            l_text_line||
23437            ' on line '             ||
23438            prl.line_num,
23439            'PO_CLOSE_WIP_JOB'
23440      FROM   po_req_headers_gt prh,
23441             po_req_lines_gt prl
23442      WHERE prh.requisition_header_id = prl.requisition_header_id
23443      AND   nvl(prh.authorization_status,'INCOMPLETE') <> 'APPROVED'
23444      AND   nvl(prl.cancel_flag,'N') = 'N'
23445      AND   nvl(prl.closed_code,'OPEN') NOT IN ('FINALLY CLOSED' , 'CLOSED')
23446      AND   prl.requisition_header_id = p_document_id
23447      AND   prl.wip_entity_id IS NOT NULL --Bug 14383315
23448      AND   NOT EXISTS (SELECT 'JOB IS VALID'
23449 	                   FROM   wip_discrete_jobs wdj
23450                        WHERE  prl.wip_entity_id = wdj.wip_entity_id
23451 					          AND wdj.status_type IN (3,4,6)
23452                        UNION ALL    --Bug 16767247
23453                        SELECT 'JOB IS VALID'
23454                         FROM   wip_repetitive_schedules wdj
23455                         WHERE  prl.wip_entity_id = wdj.wip_entity_id
23456                                AND wdj.status_type IN (3,4,6));
23457 
23458  ELSIF  p_document_type =  g_document_type_RELEASE THEN
23459      --- Document type RELEASE
23460 l_progress := 20;
23461 
23462 INSERT INTO PO_ONLINE_REPORT_TEXT_GT
23463    (  online_report_id
23464    ,  last_update_login
23465    ,  last_updated_by
23466    ,  last_update_date
23467    ,  created_by
23468    ,  creation_date
23469    ,  line_num
23470    ,  shipment_num
23471    ,  distribution_num
23472    ,  sequence
23473    ,  text_line
23474    ,  message_name
23475    )
23476 
23477    SELECT p_online_report_id,
23478            p_login_id,
23479            p_user_id,
23480            SYSDATE,
23481            p_user_id,
23482            SYSDATE,
23483            0,
23484            pll.shipment_num,
23485            pd.distribution_num,
23486            p_sequence+ ROWNUM,
23487            l_text_line||
23488            ' on distribution '     ||
23489            pd.distribution_num     ||
23490            ' of shipment '         ||
23491            pll.shipment_num,
23492            'PO_CLOSE_WIP_JOB'
23493         FROM po_headers_gt ph,
23494                  po_line_locations_gt pll,
23495                  po_distributions_gt pd,
23496                  po_releases_gt pr
23497         WHERE   1=1
23498         AND ph.po_header_id = pll.po_header_id
23499         AND pll.line_location_id = pd.line_location_id
23500         AND pr.po_release_id (+) = pd.po_release_id
23501         AND nvl(pll.approved_flag,'N') <> 'Y'
23502         AND nvl(pll.cancel_flag,'N') <> 'Y'
23503         AND nvl(pll.closed_code,'OPEN') NOT IN ('FINALLY CLOSED' , 'CLOSED')
23504         AND  pr.po_release_id = p_document_id
23505         AND  pd.wip_entity_id IS NOT NULL --Bug 14383315
23506         AND   NOT EXISTS (SELECT 'JOB IS VALID'
23507 	                      FROM   wip_discrete_jobs wdj
23508 				          WHERE  pd.wip_entity_id = wdj.wip_entity_id
23509 					             AND wdj.status_type IN (3,4,6)
23510                           UNION ALL  --Bug 16767247
23511                           SELECT 'JOB IS VALID'
23512                             FROM   wip_repetitive_schedules wdj
23513                            WHERE  pd.wip_entity_id = wdj.wip_entity_id
23514                                   AND wdj.status_type IN (3,4,6));
23515 
23516 ELSE--Other than document type RELEASE,REQUISITION
23517 
23518    l_progress := 30;
23519    INSERT INTO PO_ONLINE_REPORT_TEXT_GT
23520    (  online_report_id
23521    ,  last_update_login
23522    ,  last_updated_by
23523    ,  last_update_date
23524    ,  created_by
23525    ,  creation_date
23526    ,  line_num
23527    ,  shipment_num
23528    ,  distribution_num
23529    ,  sequence
23530    ,  text_line
23531    ,  message_name
23532    )
23533 
23534    SELECT p_online_report_id,
23535            p_login_id,
23536            p_user_id,
23537            SYSDATE,
23538            p_user_id,
23539            SYSDATE,
23540            pl.line_num,
23541            pll.shipment_num,
23542            pd.distribution_num,
23543            p_sequence+ ROWNUM,
23544            l_text_line||
23545            ' on distribution '     ||
23546            pd.distribution_num     ||
23547            ' of shipment '         ||
23548            pll.shipment_num        ||
23549            ' of line '             ||
23550            pl.line_num,
23551            'PO_CLOSE_WIP_JOB'
23552         FROM po_headers_gt ph,
23553                  po_lines_gt pl,
23554                  po_line_locations_gt pll,
23555                  po_distributions_gt pd
23556         WHERE   1=1
23557         AND ph.po_header_id = pl.po_header_id
23558         AND pl.po_line_id=pll.po_line_id
23559         AND ph.type_lookup_code = 'STANDARD'
23560         AND pll.line_location_id = pd.line_location_id
23561         AND nvl(pll.approved_flag,'N') <> 'Y'
23562         AND nvl(pll.cancel_flag,'N') <> 'Y'
23563         AND nvl(pll.closed_code,'OPEN') NOT IN ('FINALLY CLOSED' , 'CLOSED')
23564         AND ph.po_header_id = p_document_id
23565         AND pd.wip_entity_id IS NOT NULL --Bug 14383315
23566         AND NOT EXISTS (SELECT 'JOB IS VALID'
23567 	                    FROM   wip_discrete_jobs wdj
23568                         WHERE  pd.wip_entity_id = wdj.wip_entity_id
23569 					           AND wdj.status_type IN (3,4,6)
23570                        UNION ALL  --Bug 16767247
23571                        SELECT 'JOB IS VALID'
23572                         FROM   wip_repetitive_schedules wdj
23573                         WHERE  pd.wip_entity_id = wdj.wip_entity_id
23574                                AND wdj.status_type IN (3,4,6));
23575 
23576  END IF;
23577 EXCEPTION
23578 WHEN NO_DATA_FOUND THEN
23579  NULL;
23580 
23581 WHEN OTHERS THEN
23582     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
23583 
23584     IF (PO_LOG.d_exc) THEN
23585       PO_LOG.exc(g_log_head||l_api_name, l_progress, SQLCODE || ': ' || SQLERRM);
23586       PO_LOG.proc_end(g_log_head||l_api_name, 'p_sequence', p_sequence);
23587       PO_LOG.proc_end(g_log_head||l_api_name, 'x_return_status', x_return_status);
23588       PO_LOG.proc_end(g_log_head||l_api_name);
23589     END IF;
23590 END CHECK_CLOSE_WIP_JOB;
23591 
23592 --End of Code added for bug 12951645
23593 
23594 --<Bug 13019003>
23595 --Added this procedure
23596 PROCEDURE PO_VALIDATE_ACCOUNTS(
23597 	P_DOCUMENT_ID      IN NUMBER,
23598 	P_DOCUMENT_TYPE    IN VARCHAR2,
23599 	P_ONLINE_REPORT_ID IN NUMBER,
23600 	P_USER_ID          IN NUMBER,
23601 	P_LOGIN_ID         IN NUMBER,
23602 	P_SEQUENCE         IN OUT NOCOPY NUMBER,
23603 	X_RETURN_STATUS OUT NOCOPY       VARCHAR2,
23604 	x_msg_data OUT NOCOPY            VARCHAR2 )
23605 IS
23606 
23607   L_TEXTLINE PO_ONLINE_REPORT_TEXT.TEXT_LINE%TYPE := NULL;
23608   L_TEXTLINE2 PO_ONLINE_REPORT_TEXT.TEXT_LINE%TYPE := NULL;
23609   L_API_NAME              CONSTANT VARCHAR2( 40 ) := 'PO_VALIDATE_ACCOUNTS';
23610   L_PROGRESS              VARCHAR2( 3 );
23611   l_seed_message_invalid  VARCHAR2( 30 );
23612   l_seed_message_not_null VARCHAR2( 30 );
23613 BEGIN
23614 
23615    L_PROGRESS := '001';
23616    IF G_DEBUG_STMT THEN
23617    PO_DEBUG.DEBUG_STMT( G_LOG_HEAD ||'.'||L_API_NAME||'.', L_PROGRESS, 'PO_VALIDATE_ACCOUNTS' );
23618    END IF;
23619 
23620 
23621    IF( P_DOCUMENT_TYPE = 'REQUISITION' ) THEN
23622 
23623    L_PROGRESS := '002';
23624    IF G_DEBUG_STMT THEN
23625    PO_DEBUG.DEBUG_STMT( G_LOG_HEAD ||'.' || L_API_NAME ||'.', L_PROGRESS, 'REQUISITION' );
23626    END IF;
23627 
23628    -- Validate Req Charge Account
23629    INSERT INTO PO_ONLINE_REPORT_TEXT_GT(
23630 	   ONLINE_REPORT_ID,
23631 	   LAST_UPDATE_LOGIN,
23632 	   LAST_UPDATED_BY,
23633 	   LAST_UPDATE_DATE,
23634 	   CREATED_BY,
23635 	   CREATION_DATE,
23636 	   LINE_NUM,
23637 	   SHIPMENT_NUM,
23638 	   DISTRIBUTION_NUM,
23639 	   SEQUENCE,
23640 	   TEXT_LINE,
23641 	   MESSAGE_NAME
23642    	)
23643    SELECT
23644 	   p_online_report_id,
23645 	   p_login_id,
23646 	   p_user_id,
23647 	   SYSDATE,
23648 	   p_user_id,
23649 	   SYSDATE,
23650 	   PRL.line_num,
23651 	   0,
23652 	   PRD.distribution_num,
23653 	   p_sequence + ROWNUM,
23654 	   SUBSTR( Nvl2( PRD.code_combination_id,
23655    	      fnd_message.Get_string( 'PO','PO_RI_INVALID_CHARGE_ACC_ID' ),
23656 	      fnd_message.Get_string( 'PO','PO_CHARGE_NOT_NULL' )
23657 	   )               ||
23658 	   ' on distribution '    ||
23659 	   PRD.distribution_num   ||
23660 	   ' of line '            ||
23661 	   PRL.line_num, 1, 240
23662 	   ),
23663 	   Nvl2( PRD.code_combination_id, 'PO_RI_INVALID_CHARGE_ACC_ID','PO_CHARGE_NOT_NULL' )
23664    FROM
23665 	   po_req_distributions_gt PRD,
23666 	   po_req_lines_gt PRL,
23667 	   gl_sets_of_books sob
23668    WHERE
23669 	   PRD.requisition_line_id = PRL.requisition_line_id
23670 	   AND PRL.requisition_header_id = p_document_id
23671 	   AND sob.set_of_books_id = prd.set_of_books_id
23672 	   AND nvl(PRL.cancel_flag, 'N') <> 'Y'
23673 	   AND( PRD.code_combination_id IS NULL
23674 	        OR Validate_account_wrapper(
23675 	            sob.chart_of_accounts_id,
23676 	            PRD.code_combination_id,
23677 	            SYSDATE
23678 	           ) <> 'Y'
23679 	   );
23680 
23681    p_sequence := p_sequence + SQL%ROWCOUNT;
23682 
23683 
23684    -- Validate Req Budget Account
23685    IF PO_CORE_S.is_encumbrance_on( p_doc_type => p_document_type, p_org_id => NULL ) THEN
23686    INSERT INTO PO_ONLINE_REPORT_TEXT_GT(
23687 	   ONLINE_REPORT_ID,
23688 	   LAST_UPDATE_LOGIN,
23689 	   LAST_UPDATED_BY,
23690 	   LAST_UPDATE_DATE,
23691 	   CREATED_BY,
23692 	   CREATION_DATE,
23693 	   LINE_NUM,
23694 	   SHIPMENT_NUM,
23695 	   DISTRIBUTION_NUM,
23696 	   SEQUENCE,
23697 	   TEXT_LINE,
23698 	   MESSAGE_NAME
23699 	   )
23700    SELECT
23701 	   p_online_report_id,
23702 	   p_login_id,
23703 	   p_user_id,
23704 	   SYSDATE,
23705 	   p_user_id,
23706 	   SYSDATE,
23707 	   PRL.line_num,
23708 	   0,
23709 	   PRD.distribution_num,
23710 	   p_sequence + ROWNUM,
23711 	   SUBSTR( Nvl2( prd.budget_account_id,
23712 	   fnd_message.Get_string( 'PO','PO_RI_INVALID_BUDGET_ACC_ID' ),
23713 	   fnd_message.Get_string( 'PO','PO_BUDGET_NOT_NULL' )
23714 	   ) ||
23715 	   ' on distribution '      ||
23716 	   PRD.distribution_num     ||
23717 	   ' of line '              ||
23718 	   PRL.line_num, 1, 240
23719 	   ),
23720 	   Nvl2( prd.budget_account_id, 'PO_RI_INVALID_BUDGET_ACC_ID', 'PO_BUDGET_NOT_NULL' )
23721    FROM
23722 	   po_req_distributions_gt PRD,
23723 	   po_req_lines_gt PRL,
23724 	   gl_sets_of_books sob
23725    WHERE
23726 	   PRD.requisition_line_id = PRL.requisition_line_id
23727 	   AND PRL.requisition_header_id = p_document_id
23728 	   AND sob.set_of_books_id = prd.set_of_books_id
23729 	   AND nvl(PRL.cancel_flag, 'N') <> 'Y'
23730 	   AND( prd.budget_account_id IS NULL
23731 	        OR Validate_account_wrapper(
23732 	            sob.chart_of_accounts_id,
23733 	            prd.budget_account_id,
23734 				prd.gl_encumbered_date
23735 	           ) <> 'Y'
23736 	   );
23737    p_sequence := p_sequence + SQL%ROWCOUNT;
23738 
23739    END IF; ---End iSEncumbranceOn Check
23740 
23741 
23742    -- Validate Req Variance Account
23743    INSERT INTO PO_ONLINE_REPORT_TEXT_GT(
23744 	   ONLINE_REPORT_ID,
23745 	   LAST_UPDATE_LOGIN,
23746 	   LAST_UPDATED_BY,
23747 	   LAST_UPDATE_DATE,
23748 	   CREATED_BY,
23749 	   CREATION_DATE,
23750 	   LINE_NUM,
23751 	   SHIPMENT_NUM,
23752 	   DISTRIBUTION_NUM,
23753 	   SEQUENCE,
23754 	   TEXT_LINE,
23755 	   MESSAGE_NAME
23756 	   )
23757    SELECT
23758 	   p_online_report_id,
23759 	   p_login_id,
23760 	   p_user_id,
23761 	   SYSDATE,
23762 	   p_user_id,
23763 	   SYSDATE,
23764 	   PRL.line_num,
23765 	   0,
23766 	   PRD.distribution_num,
23767 	   p_sequence + ROWNUM,
23768 	   SUBSTR( Nvl2( prd.variance_account_id,
23769 	   fnd_message.Get_string( 'PO','PO_RI_INVALID_VARIANCE_ACC_ID' ),
23770 	   fnd_message.Get_string( 'PO', 'PO_VARIANCE_NOT_NULL' )
23771 	   ) ||
23772 	   ' on distribution '      ||
23773 	   PRD.distribution_num     ||
23774 	   ' of line '              ||
23775 	   PRL.line_num, 1, 240
23776 	   ),
23777 	   Nvl2( prd.variance_account_id, 'PO_RI_INVALID_VARIANCE_ACC_ID', 'PO_VARIANCE_NOT_NULL' )
23778    FROM
23779 	   po_req_distributions_gt PRD,
23780 	   po_req_lines_gt PRL,
23781 	   gl_sets_of_books sob
23782    WHERE
23783 	   PRD.requisition_line_id = PRL.requisition_line_id
23784 	   AND PRL.requisition_header_id = p_document_id
23785 	   AND sob.set_of_books_id = prd.set_of_books_id
23786 	   AND nvl(PRL.cancel_flag, 'N') <> 'Y'
23787 	   AND( prd.variance_account_id IS NULL
23788 	        OR Validate_account_wrapper(
23789 	             sob.chart_of_accounts_id,
23790 	             prd.variance_account_id,
23791 	             SYSDATE
23792 	           ) <> 'Y'
23793 	   );
23794 
23795    p_sequence := p_sequence + SQL%ROWCOUNT;
23796 
23797 
23798    -- Validate Req Accrual Account
23799    INSERT INTO PO_ONLINE_REPORT_TEXT_GT(
23800 	   ONLINE_REPORT_ID,
23801 	   LAST_UPDATE_LOGIN,
23802 	   LAST_UPDATED_BY,
23803 	   LAST_UPDATE_DATE,
23804 	   CREATED_BY,
23805 	   CREATION_DATE,
23806 	   LINE_NUM,
23807 	   SHIPMENT_NUM,
23808 	   DISTRIBUTION_NUM,
23809 	   SEQUENCE,
23810 	   TEXT_LINE,
23811 	   MESSAGE_NAME
23812 	   )
23813    SELECT
23814 	   p_online_report_id,
23815 	   p_login_id,
23816 	   p_user_id,
23817 	   SYSDATE,
23818 	   p_user_id,
23819 	   SYSDATE,
23820 	   PRL.line_num,
23821 	   0,
23822 	   PRD.distribution_num,
23823 	   p_sequence + ROWNUM,
23824 	   SUBSTR( Nvl2( prd.accrual_account_id,
23825 	   fnd_message.Get_string( 'PO', 'PO_RI_INVALID_ACCRUAL_ACC_ID' ),
23826 	   fnd_message.Get_string( 'PO', 'PO_ACCRUAL_NOT_NULL' )
23827 	   ) ||
23828 	   ' on distribution '      ||
23829 	   PRD.distribution_num     ||
23830 	   ' of line '              ||
23831 	   PRL.line_num, 1, 240
23832 	   ),
23833 	   Nvl2( prd.accrual_account_id, 'PO_RI_INVALID_ACCRUAL_ACC_ID', 'PO_ACCRUAL_NOT_NULL' )
23834    FROM
23835 	   po_req_distributions_gt PRD,
23836 	   po_req_lines_gt PRL,
23837 	   gl_sets_of_books sob
23838    WHERE
23839 	   PRD.requisition_line_id = PRL.requisition_line_id
23840 	   AND PRL.requisition_header_id = p_document_id
23841 	   AND sob.set_of_books_id = prd.set_of_books_id
23842 	   AND nvl(PRL.cancel_flag, 'N') <> 'Y'
23843 	   AND( prd.accrual_account_id IS NULL
23844 	        OR Validate_account_wrapper(
23845 	            sob.chart_of_accounts_id,
23846 	            prd.accrual_account_id,
23847 	            SYSDATE
23848 	           ) <> 'Y'
23849 	   );
23850 
23851 
23852    p_sequence := p_sequence + SQL%ROWCOUNT;
23853 
23854 
23855    --  Account Validations of releases
23856 
23857    ELSIF( P_DOCUMENT_TYPE = 'RELEASE' ) THEN
23858 
23859    L_PROGRESS := '003';
23860    IF G_DEBUG_STMT THEN
23861    PO_DEBUG.DEBUG_STMT( G_LOG_HEAD ||'.'|| L_API_NAME ||'.', L_PROGRESS, 'RELEASE' );
23862    END IF;
23863 
23864 
23865    -- Validate Release Charge Account for non cancelled, non finally closed, not approved shipments.
23866    INSERT INTO PO_ONLINE_REPORT_TEXT_GT(
23867 	   ONLINE_REPORT_ID,
23868 	   LAST_UPDATE_LOGIN,
23869 	   LAST_UPDATED_BY,
23870 	   LAST_UPDATE_DATE,
23871 	   CREATED_BY,
23872 	   CREATION_DATE,
23873 	   LINE_NUM,
23874 	   SHIPMENT_NUM,
23875 	   DISTRIBUTION_NUM,
23876 	   SEQUENCE,
23877 	   TEXT_LINE,
23878 	   MESSAGE_NAME
23879 	   )
23880    SELECT
23881 	   P_ONLINE_REPORT_ID,
23882 	   P_LOGIN_ID,
23883 	   P_USER_ID,
23884 	   SYSDATE,
23885 	   P_USER_ID,
23886 	   SYSDATE,
23887 	   POD.PO_LINE_ID,
23888 	   POLL.shipment_num,
23889 	   POD.DISTRIBUTION_NUM,
23890 	   P_SEQUENCE + ROWNUM,
23891 	   SUBSTR( Nvl2( PoD.code_combination_id,
23892 	   fnd_message.Get_string( 'PO', 'PO_RI_INVALID_CHARGE_ACC_ID' ),
23893 	   fnd_message.Get_string( 'PO', 'PO_CHARGE_NOT_NULL' )
23894 	   )||
23895 	   ' on distribution '     ||
23896 	   POD.DISTRIBUTION_NUM    ||
23897 	   ' of shipment '         ||
23898 	   POLL.shipment_num, 1, 240
23899 	   ),
23900 	   Nvl2( PoD.code_combination_id, 'PO_RI_INVALID_CHARGE_ACC_ID', 'PO_CHARGE_NOT_NULL' )
23901    FROM
23902 	   PO_DISTRIBUTIONS_GT POD,
23903 	   gl_sets_of_books sob,
23904 	   PO_LINE_LOCATIONS_GT POLL
23905    WHERE
23906 	   POD.PO_RELEASE_ID = P_DOCUMENT_ID
23907 	   AND POD.line_location_id = POLL.line_location_id
23908 	   AND POD.line_location_id IS NOT NULL
23909 	   AND sob.set_of_books_id = pod.set_of_books_id
23910 	   AND NVL( POLL.APPROVED_FLAG, 'N' ) <> 'Y'
23911 	   AND NVL( POLL.CANCEL_FLAG, 'N' ) <> 'Y'
23912 	   AND NVL( POLL.CLOSED_CODE, 'OPEN' ) <> 'FINALLY CLOSED'
23913 	   AND( POD.CODE_COMBINATION_ID IS NULL
23914 	        OR Validate_account_wrapper(
23915 	             sob.chart_of_accounts_id,
23916 	             POD.code_combination_id,
23917 	             SYSDATE
23918 	           ) <> 'Y'
23919 	   );
23920    p_sequence := p_sequence + SQL%ROWCOUNT;
23921 
23922 
23923    -- Validate Release Budget Account for non cancelled, non finally closed, not approved shipments.
23924    IF PO_CORE_S.is_encumbrance_on( p_doc_type => 'PO', p_org_id => NULL ) THEN
23925 
23926    INSERT INTO PO_ONLINE_REPORT_TEXT_GT(
23927 	   ONLINE_REPORT_ID,
23928 	   LAST_UPDATE_LOGIN,
23929 	   LAST_UPDATED_BY,
23930 	   LAST_UPDATE_DATE,
23931 	   CREATED_BY,
23932 	   CREATION_DATE,
23933 	   LINE_NUM,
23934 	   SHIPMENT_NUM,
23935 	   DISTRIBUTION_NUM,
23936 	   SEQUENCE,
23937 	   TEXT_LINE,
23938 	   MESSAGE_NAME
23939 	   )
23940    SELECT
23941 	   P_ONLINE_REPORT_ID,
23942 	   P_LOGIN_ID,
23943 	   P_USER_ID,
23944 	   SYSDATE,
23945 	   P_USER_ID,
23946 	   SYSDATE,
23947 	   POD.PO_LINE_ID,
23948 	   POLL.shipment_num,
23949 	   POD.DISTRIBUTION_NUM,
23950 	   P_SEQUENCE + ROWNUM,
23951 	   SUBSTR( Nvl2( PoD.budget_account_id,
23952 	   fnd_message.Get_string( 'PO', 'PO_RI_INVALID_BUDGET_ACC_ID' ),
23953 	   fnd_message.Get_string( 'PO', 'PO_BUDGET_NOT_NULL' )
23954 	   )||
23955 	   ' on distribution '     ||
23956 	   POD.DISTRIBUTION_NUM    ||
23957 	   ' of shipment '         ||
23958 	   POLL.shipment_num, 1, 240
23959 	   ),
23960 	   Nvl2( PoD.budget_account_id, 'PO_RI_INVALID_BUDGET_ACC_ID', 'PO_BUDGET_NOT_NULL' )
23961    FROM
23962 	   PO_DISTRIBUTIONS_GT POD,
23963 	   gl_sets_of_books sob,
23964 	   PO_LINE_LOCATIONS_GT POLL
23965    WHERE
23966 	   POD.PO_RELEASE_ID = P_DOCUMENT_ID
23967 	   AND POD.line_location_id = POLL.line_location_id
23968 	   AND POD.line_location_id IS NOT NULL
23969 	   AND sob.set_of_books_id = pod.set_of_books_id
23970 	   AND NVL( POLL.APPROVED_FLAG, 'N' ) <> 'Y'
23971 	   AND NVL( POLL.CANCEL_FLAG, 'N' ) <> 'Y'
23972 	   AND NVL( POLL.CLOSED_CODE, 'OPEN' ) <> 'FINALLY CLOSED'
23973 	   AND( POD.budget_account_id IS NULL
23974 	        OR Validate_account_wrapper(
23975 	             sob.chart_of_accounts_id,
23976 	             POD.budget_account_id,
23977 	             pod.gl_encumbered_date
23978 	           ) <> 'Y'
23979 	   );
23980    p_sequence := p_sequence + SQL%ROWCOUNT;
23981 
23982    END IF; --- Enc on Check
23983 
23984 
23985    -- Validate Release Accrual Account for shipments which are not approved atleast once.
23986    INSERT INTO PO_ONLINE_REPORT_TEXT_GT(
23987 	   ONLINE_REPORT_ID,
23988 	   LAST_UPDATE_LOGIN,
23989 	   LAST_UPDATED_BY,
23990 	   LAST_UPDATE_DATE,
23991 	   CREATED_BY,
23992 	   CREATION_DATE,
23993 	   LINE_NUM,
23994 	   SHIPMENT_NUM,
23995 	   DISTRIBUTION_NUM,
23996 	   SEQUENCE,
23997 	   TEXT_LINE,
23998 	   MESSAGE_NAME
23999 	   )
24000    SELECT
24001 	   P_ONLINE_REPORT_ID,
24002 	   P_LOGIN_ID,
24003 	   P_USER_ID,
24004 	   SYSDATE,
24005 	   P_USER_ID,
24006 	   SYSDATE,
24007 	   POD.PO_LINE_ID,
24008 	   POLL.shipment_num,
24009 	   POD.DISTRIBUTION_NUM,
24010 	   P_SEQUENCE + ROWNUM,
24011 	   SUBSTR( Nvl2( PoD.accrual_account_id,
24012 	   fnd_message.Get_string( 'PO', 'PO_RI_INVALID_ACCRUAL_ACC_ID' ),
24013 	   fnd_message.Get_string( 'PO', 'PO_ACCRUAL_NOT_NULL' )
24014 	   )||
24015 	   ' on distribution '      ||
24016 	   POD.DISTRIBUTION_NUM     ||
24017 	   ' of shipment '          ||
24018 	   POLL.shipment_num, 1, 240
24019 	   ),
24020 	   Nvl2( PoD.accrual_account_id, 'PO_RI_INVALID_ACCRUAL_ACC_ID', 'PO_ACCRUAL_NOT_NULL' )
24021    FROM
24022 	   PO_DISTRIBUTIONS_GT POD,
24023 	   gl_sets_of_books sob,
24024 	   PO_LINE_LOCATIONS_GT POLL
24025    WHERE
24026 	   POD.PO_RELEASE_ID = P_DOCUMENT_ID
24027 	   AND POD.line_location_id = POLL.line_location_id
24028 	   AND POD.line_location_id IS NOT NULL
24029 	   AND sob.set_of_books_id = pod.set_of_books_id
24030 	   AND POLL.APPROVED_DATE IS NULL
24031 	   AND NVL( POLL.CANCEL_FLAG, 'N' ) <> 'Y'
24032 	   AND NVL( POLL.CLOSED_CODE, 'OPEN' ) <> 'FINALLY CLOSED'
24033 	   AND( POD.accrual_account_id IS NULL
24034 	        OR Validate_account_wrapper(
24035 	             sob.chart_of_accounts_id,
24036 	             POD.accrual_account_id,
24037 	             SYSDATE
24038 	           ) <> 'Y'
24039 	   );
24040    p_sequence := p_sequence + SQL%ROWCOUNT;
24041 
24042 
24043    -- Validate Release Variance Account for shipments which are not approved atleast once.
24044    INSERT 	INTO PO_ONLINE_REPORT_TEXT_GT(
24045 	   ONLINE_REPORT_ID,
24046 	   LAST_UPDATE_LOGIN,
24047 	   LAST_UPDATED_BY,
24048 	   LAST_UPDATE_DATE,
24049 	   CREATED_BY,
24050 	   CREATION_DATE,
24051 	   LINE_NUM,
24052 	   SHIPMENT_NUM,
24053 	   DISTRIBUTION_NUM,
24054 	   SEQUENCE,
24055 	   TEXT_LINE,
24056 	   MESSAGE_NAME
24057 	   )
24058    SELECT
24059 	   P_ONLINE_REPORT_ID,
24060 	   P_LOGIN_ID,
24061 	   P_USER_ID,
24062 	   SYSDATE,
24063 	   P_USER_ID,
24064 	   SYSDATE,
24065 	   POD.PO_LINE_ID,
24066 	   POLL.shipment_num,
24067 	   POD.DISTRIBUTION_NUM,
24068 	   P_SEQUENCE + ROWNUM,
24069 	   SUBSTR( Nvl2( PoD.variance_account_id,
24070 	   fnd_message.Get_string( 'PO', 'PO_RI_INVALID_VARIANCE_ACC_ID' ),
24071 	   fnd_message.Get_string( 'PO', 'PO_VARIANCE_NOT_NULL' )
24072 	   )||
24073 	   ' on distribution '       ||
24074 	   POD.DISTRIBUTION_NUM      ||
24075 	   ' of shipment '           ||
24076 	   POLL.shipment_num, 1, 240
24077 	   ),
24078 	   Nvl2( PoD.variance_account_id, 'PO_RI_INVALID_VARIANCE_ACC_ID', 'PO_VARIANCE_NOT_NULL' )
24079    FROM
24080 	   PO_DISTRIBUTIONS_GT POD,
24081 	   gl_sets_of_books sob,
24082 	   PO_LINE_LOCATIONS_GT POLL
24083    WHERE
24084 	   POD.PO_RELEASE_ID = P_DOCUMENT_ID
24085 	   AND POD.line_location_id = POLL.line_location_id
24086 	   AND POD.line_location_id IS NOT NULL
24087 	   AND sob.set_of_books_id = pod.set_of_books_id
24088 	   AND POLL.APPROVED_DATE IS NULL
24089 	   AND NVL( POLL.CANCEL_FLAG, 'N' ) <> 'Y'
24090 	   AND NVL( POLL.CLOSED_CODE, 'OPEN' ) <> 'FINALLY CLOSED'
24091 	   AND( POD.variance_account_id IS NULL
24092 	        OR Validate_account_wrapper(
24093 	             sob.chart_of_accounts_id,
24094 	             POD.variance_account_id,
24095 	             SYSDATE
24096 	           ) <> 'Y'
24097 	   );
24098    p_sequence := p_sequence + SQL%ROWCOUNT;
24099 
24100 
24101    ELSE
24102    --validating the PO Accounts
24103 
24104 
24105    L_PROGRESS := '004';
24106    IF G_DEBUG_STMT THEN
24107    PO_DEBUG.DEBUG_STMT( G_LOG_HEAD ||'.'|| L_API_NAME ||'.', L_PROGRESS, 'PO or BPA' );
24108    END IF;
24109 
24110    -- Validate PO Charge Account
24111    INSERT 	INTO PO_ONLINE_REPORT_TEXT_GT(
24112 	   ONLINE_REPORT_ID,
24113 	   LAST_UPDATE_LOGIN,
24114 	   LAST_UPDATED_BY,
24115 	   LAST_UPDATE_DATE,
24116 	   CREATED_BY,
24117 	   CREATION_DATE,
24118 	   LINE_NUM,
24119 	   SHIPMENT_NUM,
24120 	   DISTRIBUTION_NUM,
24121 	   SEQUENCE,
24122 	   TEXT_LINE,
24123 	   MESSAGE_NAME
24124 	   )
24125    SELECT
24126 	   P_ONLINE_REPORT_ID,
24127 	   P_LOGIN_ID,
24128 	   P_USER_ID,
24129 	   SYSDATE,
24130 	   P_USER_ID,
24131 	   SYSDATE,
24132 	   POL.line_num,
24133 	   POLL.shipment_num,
24134 	   POD.DISTRIBUTION_NUM,
24135 	   P_SEQUENCE + ROWNUM,
24136 	   SUBSTR( Nvl2( PoD.code_combination_id,
24137 	   fnd_message.Get_string( 'PO', 'PO_RI_INVALID_CHARGE_ACC_ID' ),
24138 	   fnd_message.Get_string( 'PO', 'PO_CHARGE_NOT_NULL' )
24139 	   )||
24140 	   ' on distribution '     ||
24141 	   POD.DISTRIBUTION_NUM    ||
24142 	   ' of shipment '         ||
24143 	   POLL.shipment_num       ||
24144 	   ' of line '             ||
24145 	   POL.line_num, 1, 240
24146 	   ),
24147 	   Nvl2( PoD.code_combination_id, 'PO_RI_INVALID_CHARGE_ACC_ID', 'PO_CHARGE_NOT_NULL' )
24148    FROM
24149 	   PO_DISTRIBUTIONS_GT POD,
24150 	   gl_sets_of_books sob,
24151 	   PO_LINE_LOCATIONS_GT POLL,
24152 	   PO_LINES_GT POL
24153    WHERE
24154 	   POD.PO_HEADER_ID = P_DOCUMENT_ID
24155 	   AND POD.line_location_id = POLL.line_location_id
24156 	   AND POLL.po_line_id = POL.po_line_id
24157 	   AND sob.set_of_books_id = pod.set_of_books_id
24158 	   AND NVL( POLL.APPROVED_FLAG, 'N' ) <> 'Y'
24159 	   AND NVL( POLL.CANCEL_FLAG, 'N' ) <> 'Y'
24160 	   AND NVL( POLL.CLOSED_CODE, 'OPEN' ) <> 'FINALLY CLOSED'
24161 	   AND( POD.CODE_COMBINATION_ID IS NULL
24162 	        OR Validate_account_wrapper(
24163 	            sob.chart_of_accounts_id,
24164 	            POD.code_combination_id,
24165 	            SYSDATE
24166 	           ) <> 'Y'
24167 	   );
24168    p_sequence := p_sequence + SQL%ROWCOUNT;
24169 
24170 
24171    -- Validate PO/BPA Budget Account
24172    IF PO_CORE_S.is_encumbrance_on( p_doc_type => 'PO', p_org_id => NULL ) THEN
24173    -- Validate PO/BPA Budget Account
24174    INSERT	INTO PO_ONLINE_REPORT_TEXT_GT(
24175 	   ONLINE_REPORT_ID,
24176 	   LAST_UPDATE_LOGIN,
24177 	   LAST_UPDATED_BY,
24178 	   LAST_UPDATE_DATE,
24179 	   CREATED_BY,
24180 	   CREATION_DATE,
24181 	   LINE_NUM,
24182 	   SHIPMENT_NUM,
24183 	   DISTRIBUTION_NUM,
24184 	   SEQUENCE,
24185 	   TEXT_LINE,
24186 	   MESSAGE_NAME
24187 	   )
24188    SELECT
24189 	   P_ONLINE_REPORT_ID,
24190 	   P_LOGIN_ID,
24191 	   P_USER_ID,
24192 	   SYSDATE,
24193 	   P_USER_ID,
24194 	   SYSDATE,
24195 	   POL.line_num,
24196 	   POLL.shipment_num,
24197 	   POD.DISTRIBUTION_NUM,
24198 	   P_SEQUENCE + ROWNUM,
24199 	   SUBSTR( Nvl2( PoD.budget_account_id,
24200 	   fnd_message.Get_string( 'PO','PO_RI_INVALID_BUDGET_ACC_ID' ),
24201 	   fnd_message.Get_string( 'PO', 'PO_BUDGET_NOT_NULL' )
24202 	   )||
24203 	   ' on distribution '     ||
24204 	   POD.DISTRIBUTION_NUM    ||
24205 	   ' of shipment '         ||
24206 	   POLL.shipment_num       ||
24207 	   ' of line '             ||
24208 	   POL.line_num, 1, 240
24209 	   ),
24210 	   Nvl2( PoD.budget_account_id, 'PO_RI_INVALID_BUDGET_ACC_ID', 'PO_BUDGET_NOT_NULL' )
24211    FROM
24212 	   PO_DISTRIBUTIONS_GT POD,
24213 	   gl_sets_of_books sob,
24214 	   PO_LINE_LOCATIONS_GT POLL,
24215 	   PO_LINES_GT POL
24216    WHERE
24217 	   POD.PO_HEADER_ID = P_DOCUMENT_ID
24218 	   AND POD.po_release_id IS NULL
24219 	   AND POD.line_location_id = POLL.line_location_id(+)
24220 	   AND POLL.po_line_id = POL.po_line_id(+)
24221 	   AND sob.set_of_books_id = pod.set_of_books_id
24222 	   AND NVL( POLL.APPROVED_FLAG, 'N' ) <> 'Y'
24223 	   AND NVL( POLL.CANCEL_FLAG, 'N' ) <> 'Y'
24224 	   AND NVL( POLL.CLOSED_CODE, 'OPEN' ) <> 'FINALLY CLOSED'
24225             ---<Bug 14664343: Begin>
24226 	   AND POD.distribution_type = 'STANDARD'
24227 	   ---<Bug 14664343: End>
24228 
24229 	   AND( POD.budget_account_id IS NULL
24230 	        OR Validate_account_wrapper(
24231 	            sob.chart_of_accounts_id,
24232 	            POD.budget_account_id,
24233 	            pod.gl_encumbered_date
24234 	           ) <> 'Y'
24235 	   );
24236    p_sequence := p_sequence + SQL%ROWCOUNT;
24237    END IF; --- Enc on Check
24238 
24239 
24240    -- Validate PO Accrual Account on shipments which are not approved atleast once.
24241    INSERT	INTO PO_ONLINE_REPORT_TEXT_GT(
24242 	   ONLINE_REPORT_ID,
24243 	   LAST_UPDATE_LOGIN,
24244 	   LAST_UPDATED_BY,
24245 	   LAST_UPDATE_DATE,
24246 	   CREATED_BY,
24247 	   CREATION_DATE,
24248 	   LINE_NUM,
24249 	   SHIPMENT_NUM,
24250 	   DISTRIBUTION_NUM,
24251 	   SEQUENCE,
24252 	   TEXT_LINE,
24253 	   MESSAGE_NAME
24254 	   )
24255    SELECT
24256 	   P_ONLINE_REPORT_ID,
24257 	   P_LOGIN_ID,
24258 	   P_USER_ID,
24259 	   SYSDATE,
24260 	   P_USER_ID,
24261 	   SYSDATE,
24262 	   POL.line_num,
24263 	   POLL.shipment_num,
24264 	   POD.DISTRIBUTION_NUM,
24265 	   P_SEQUENCE + ROWNUM,
24266 	   SUBSTR( Nvl2( PoD.accrual_account_id,
24267 	   fnd_message.Get_string( 'PO','PO_RI_INVALID_ACCRUAL_ACC_ID' ),
24268 	   fnd_message.Get_string( 'PO','PO_ACCRUAL_NOT_NULL' )
24269 	   )||
24270 	   ' on distribution '      ||
24271 	   POD.DISTRIBUTION_NUM     ||
24272 	   ' of shipment '          ||
24273 	   POLL.shipment_num        ||
24274 	   ' of line '              ||
24275 	   POL.line_num, 1, 240
24276 	   ),
24277 	   Nvl2( PoD.accrual_account_id, 'PO_RI_INVALID_ACCRUAL_ACC_ID', 'PO_ACCRUAL_NOT_NULL' )
24278    FROM
24279 	   PO_DISTRIBUTIONS_GT POD,
24280 	   gl_sets_of_books sob,
24281 	   PO_LINE_LOCATIONS_GT POLL,
24282 	   PO_LINES_GT POL
24283    WHERE
24284 	   POD.PO_HEADER_ID = P_DOCUMENT_ID
24285 	   AND POD.line_location_id = POLL.line_location_id
24286 	   AND POLL.po_line_id = POL.po_line_id
24287 	   AND sob.set_of_books_id = pod.set_of_books_id
24288 	   AND POLL.APPROVED_DATE IS NULL
24289 	   AND NVL( POLL.CANCEL_FLAG, 'N' ) <> 'Y'
24290 	   AND NVL( POLL.CLOSED_CODE, 'OPEN' ) <> 'FINALLY CLOSED'
24291 	   AND( POD.accrual_account_id IS NULL
24292 	        OR Validate_account_wrapper(
24293 	             sob.chart_of_accounts_id,
24294 	             POD.accrual_account_id,
24295 	             SYSDATE
24296 	           ) <> 'Y'
24297 	   );
24298 
24299    p_sequence := p_sequence + SQL%ROWCOUNT;
24300 
24301 
24302    -- Validate PO Variance Account on shipments which are not approved atleast once.
24303    INSERT	INTO PO_ONLINE_REPORT_TEXT_GT(
24304 	   ONLINE_REPORT_ID,
24305 	   LAST_UPDATE_LOGIN,
24306 	   LAST_UPDATED_BY,
24307 	   LAST_UPDATE_DATE,
24308 	   CREATED_BY,
24309 	   CREATION_DATE,
24310 	   LINE_NUM,
24311 	   SHIPMENT_NUM,
24312 	   DISTRIBUTION_NUM,
24313 	   SEQUENCE,
24314 	   TEXT_LINE,
24315 	   MESSAGE_NAME
24316 	   )
24317    SELECT
24318 	   P_ONLINE_REPORT_ID,
24319 	   P_LOGIN_ID,
24320 	   P_USER_ID,
24321 	   SYSDATE,
24322 	   P_USER_ID,
24323 	   SYSDATE,
24324 	   POL.line_num,
24325 	   POLL.shipment_num,
24326 	   POD.DISTRIBUTION_NUM,
24327 	   P_SEQUENCE + ROWNUM,
24328 	   SUBSTR( Nvl2( PoD.variance_account_id,
24329 	   fnd_message.Get_string( 'PO', 'PO_RI_INVALID_VARIANCE_ACC_ID' ),
24330 	   fnd_message.Get_string( 'PO', 'PO_VARIANCE_NOT_NULL' )
24331 	   )||
24332 	   ' on distribution '       ||
24333 	   POD.DISTRIBUTION_NUM      ||
24334 	   ' of shipment '           ||
24335 	   POLL.shipment_num         ||
24336 	   ' of line '               ||
24337 	   POL.line_num, 1, 240
24338 	   ),
24339 	   Nvl2( PoD.variance_account_id, 'PO_RI_INVALID_VARIANCE_ACC_ID', 'PO_VARIANCE_NOT_NULL' )
24340    FROM
24341 	   PO_DISTRIBUTIONS_GT POD,
24342 	   gl_sets_of_books sob,
24343 	   PO_LINE_LOCATIONS_GT POLL,
24344 	   PO_LINES_GT POL
24345    WHERE
24346 	   POD.PO_HEADER_ID = P_DOCUMENT_ID
24347 	   AND POD.line_location_id = POLL.line_location_id
24348 	   AND POLL.po_line_id = POL.po_line_id
24349 	   AND sob.set_of_books_id = pod.set_of_books_id
24350 	   AND POLL.APPROVED_DATE IS NULL
24351 	   AND NVL( POLL.CANCEL_FLAG, 'N' ) <> 'Y'
24352 	   AND NVL( POLL.CLOSED_CODE, 'OPEN' ) <> 'FINALLY CLOSED'
24353 	   AND( POD.variance_account_id IS NULL
24354 	        OR Validate_account_wrapper(
24355 	             sob.chart_of_accounts_id,
24356 	             POD.variance_account_id,
24357 	             SYSDATE
24358 	           ) <> 'Y'
24359 	   );
24360    p_sequence := p_sequence + SQL%ROWCOUNT;
24361 
24362    END IF;
24363 
24364    X_RETURN_STATUS := FND_API.G_RET_STS_SUCCESS;
24365 
24366 EXCEPTION
24367    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
24368      X_MSG_DATA := FND_MSG_PUB.GET( P_MSG_INDEX => FND_MSG_PUB.G_LAST, P_ENCODED => 'F' );
24369      X_RETURN_STATUS := FND_API.G_RET_STS_UNEXP_ERROR;
24370 
24371    WHEN FND_API.G_EXC_ERROR THEN
24372      X_MSG_DATA := FND_MSG_PUB.GET( P_MSG_INDEX => FND_MSG_PUB.G_LAST, P_ENCODED => 'F' );
24373      X_RETURN_STATUS := FND_API.G_RET_STS_ERROR;
24374 
24375    WHEN OTHERS THEN
24376 
24377      -- SQLStmtGetText(mycontext, l_dyn_string, size1, size2 );
24378      IF FND_MSG_PUB.CHECK_MSG_LEVEL( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR ) THEN
24379      FND_MSG_PUB.ADD_EXC_MSG( G_PKG_NAME, L_API_NAME );
24380      END IF;
24381 
24382      IF( G_DEBUG_UNEXP ) THEN
24383       IF( FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED ) THEN
24384        FND_LOG.STRING( FND_LOG.LEVEL_UNEXPECTED, G_LOG_HEAD ||
24385        L_API_NAME                                           ||
24386        '.OTHERS_EXCEPTION', 'EXCEPTION: LOCATION IS '       ||
24387        L_PROGRESS                                           ||
24388        ' SQL CODE IS '                                      ||
24389        SQLCODE );
24390       END IF;
24391      END IF;
24392 
24393      X_MSG_DATA := FND_MSG_PUB.GET( P_MSG_INDEX => FND_MSG_PUB.G_LAST,
24394      P_ENCODED => 'F' );
24395      X_RETURN_STATUS := FND_API.G_RET_STS_UNEXP_ERROR;
24396 
24397 END PO_VALIDATE_ACCOUNTS;
24398 --<Bug 13019003> End------------------------------------------
24399 
24400    --<Bug 13019003>
24401    --Added this function.
24402 FUNCTION validate_account_wrapper(
24403 	p_structure_number IN NUMBER,
24404 	p_combination_id  IN NUMBER,
24405 	p_val_date IN DATE)
24406 RETURN VARCHAR2  IS
24407 	l_concat_segs VARCHAR2(2000);
24408 	l_result1 BOOLEAN;
24409 
24410 	--Added as part of bug 16021525 fix
24411 	L_API_NAME CONSTANT VARCHAR2( 40 ) := 'validate_account_wrapper';
24412 	L_PROGRESS  VARCHAR2( 3 );
24413 	l_application_id number;
24414 	l_responsibility_id number;
24415 	l_user_id number;
24416 BEGIN
24417 
24418 	L_PROGRESS := '001';
24419 	IF G_DEBUG_STMT THEN
24420 		PO_DEBUG.DEBUG_STMT( G_LOG_HEAD ||'.'|| L_API_NAME ||'.', L_PROGRESS, 'p_structure_number :'||p_structure_number );
24421 		PO_DEBUG.DEBUG_STMT( G_LOG_HEAD ||'.'|| L_API_NAME ||'.', L_PROGRESS, 'p_combination_id :'||p_combination_id );
24422 		PO_DEBUG.DEBUG_STMT( G_LOG_HEAD ||'.'|| L_API_NAME ||'.', L_PROGRESS, 'p_val_date :'||p_val_date );
24423 	END IF;
24424 
24425 	l_concat_segs := fnd_flex_ext.get_segs(
24426 			application_short_name => 'SQLGL',
24427 			key_flex_code => 'GL#',
24428 			structure_number => p_structure_number,
24429 			combination_id => p_combination_id
24430 		) ;
24431 
24432 	L_PROGRESS := '002';
24433 	IF G_DEBUG_STMT THEN
24434 		PO_DEBUG.DEBUG_STMT( G_LOG_HEAD ||'.'|| L_API_NAME ||'.', L_PROGRESS, 'l_concat_segs :'||l_concat_segs );
24435 	END IF;
24436 
24437 IF( l_concat_segs IS NULL)
24438 then
24439 	RETURN 'N';
24440 else
24441 	L_PROGRESS := '003';
24442 
24443 	IF G_DEBUG_STMT THEN
24444 		PO_DEBUG.DEBUG_STMT( G_LOG_HEAD ||'.'|| L_API_NAME ||'.', L_PROGRESS, 'calling fnd_flex_keyval.validate_segs');
24445 	END IF;
24446 
24447 	--Start of code changes added as part of bug 16021525 fix
24448 	l_application_id := PO_REQAPPROVAL_ACTION.application_id;
24449 	l_responsibility_id := PO_REQAPPROVAL_ACTION.responsibility_id;
24450 	l_user_id := PO_REQAPPROVAL_ACTION.user_id;
24451 
24452 	IF G_DEBUG_STMT THEN
24453 		PO_DEBUG.DEBUG_STMT( G_LOG_HEAD ||'.'|| L_API_NAME ||'.', L_PROGRESS, 'l_application_id : '||l_application_id);
24454 		PO_DEBUG.DEBUG_STMT( G_LOG_HEAD ||'.'|| L_API_NAME ||'.', L_PROGRESS, 'l_responsibility_id : '||l_responsibility_id);
24455 		PO_DEBUG.DEBUG_STMT( G_LOG_HEAD ||'.'|| L_API_NAME ||'.', L_PROGRESS, 'l_user_id : '||l_user_id);
24456 	END IF;
24457 
24458 	gl_global.set_aff_validation('XX',null);
24459 
24460 	IF (l_application_id is NULL OR l_responsibility_id is NULL OR l_user_id is NULL) THEN
24461 
24462       l_result1 := fnd_flex_keyval.validate_segs(
24463               operation => 'CHECK_COMBINATION',
24464               appl_short_name => 'SQLGL',
24465               key_flex_code => 'GL#',
24466               structure_number => p_structure_number,
24467               concat_segments => l_concat_segs,
24468               validation_date => P_val_date,
24469               vrule => '\nSUMMARY_FLAG\nI' ||
24470               '\nAPPL=SQLGL;NAME=GL_NO_PARENT_SEGMENT_ALLOWED\nN\0' ||
24471               'GL_GLOBAL\nDETAIL_POSTING_ALLOWED\nI\nNAME=PO_ALL_POSTING_NA\nY'
24472               );
24473 
24474 	ELSE
24475 
24476 		l_result1 := fnd_flex_keyval.validate_segs(
24477 			operation => 'CHECK_COMBINATION',
24478 			appl_short_name => 'SQLGL',
24479 			key_flex_code => 'GL#',
24480 			structure_number => p_structure_number,
24481 			concat_segments => l_concat_segs,
24482 			validation_date => P_val_date,
24483 			vrule => '\nSUMMARY_FLAG\nI' ||
24484 			'\nAPPL=SQLGL;NAME=GL_NO_PARENT_SEGMENT_ALLOWED\nN\0' ||
24485 			'GL_GLOBAL\nDETAIL_POSTING_ALLOWED\nI\nNAME=PO_ALL_POSTING_NA\nY',
24486 			resp_appl_id => l_application_id,
24487 			resp_id => l_responsibility_id,
24488 			user_id => l_user_id);
24489 	END IF;
24490 	--END of code changes added as part of bug 16021525 fix
24491 
24492 END IF;
24493 
24494 IF(l_result1) THEN RETURN 'Y'; ELSE RETURN 'N'; END IF;
24495 
24496 END  validate_account_wrapper;
24497 
24498 --<Event Based Delivery Project>
24499 /**
24500 * Private Procedure: DELIVERY_EVENT_CHECKS
24501 * Requires:
24502 *   IN PARAMETERS:
24503 *       p_document_id:      The po_header_id of submitted document
24504 *       p_online_report_id: Id used to INSERT INTO online_report_text table
24505 *       p_user_id:          User performing the action
24506 *       p_login_id:         Last update login_id
24507 *   IN OUT PARAMETERS
24508 *       p_sequence:          Sequence number of last reported error
24509 * Modifies: Inserts error msgs in online_report_text_gt table, uses
24510 *           global_temp tables for processing
24511 * Effects: This procedure runs the document submission checks for CLM Awards.
24512 *           Check if delivery event is defined and delivery period is not defined.
24513 *
24514 * Returns:
24515 *  p_sequence: This parameter contains the current count of number of error
24516 *              messages inserted
24517 */
24518 PROCEDURE DELIVERY_EVENT_CHECKS(p_document_id IN NUMBER,
24519                        p_online_report_id IN NUMBER,
24520                        p_user_id IN NUMBER,
24521                        p_login_id IN NUMBER,
24522                        p_sequence IN OUT NOCOPY NUMBER,
24523                        x_return_status OUT NOCOPY VARCHAR2) IS
24524 
24525 l_api_name  CONSTANT varchar2(40) := 'DELIVERY_EVENT_CHECKS';
24526 l_progress VARCHAR2(3);
24527 l_textline  po_online_report_text.text_line%TYPE := NULL;
24528 
24529 BEGIN
24530 
24531  l_progress := '000';
24532 
24533     IF g_debug_stmt THEN
24534         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
24535           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
24536                        l_api_name||'.' || l_progress,
24537                        '');
24538         END IF;
24539     END IF;
24540 
24541     l_textline := FND_MESSAGE.get_string('PO', 'CLM_NO_DELIVERY_PERIOD');
24542 
24543     INSERT INTO po_online_report_text_gt
24544                 (online_report_id,
24545                  last_update_login,
24546                  last_updated_by,
24547                  last_update_date,
24548                  created_by,
24549                  creation_date,
24550                  line_num,
24551                  shipment_num,
24552                  distribution_num,
24553                  sequence,
24554                  text_line,
24555                  message_name,
24556                  message_type)
24557     SELECT p_online_report_id,
24558            p_login_id,
24559            p_user_id,
24560            SYSDATE,
24561            p_user_id,
24562            SYSDATE,
24563            POL.line_num,
24564            PLL.shipment_num,
24565            0,
24566            p_sequence + ROWNUM,
24567            Substr (g_linemsg
24568                    || g_delim
24569                    || Nvl(POL.line_num_display, POL.line_num)
24570                    || g_delim
24571                    || g_shipmsg
24572                    || g_delim
24573                    || PLL.shipment_num
24574                    || g_delim
24575                    || l_textline, 1, 240),
24576            'CLM_NO_DELIVERY_PERIOD',
24577            'E'
24578     FROM   po_lines_gt POL,
24579            po_headers_gt POH,
24580            po_line_locations_gt PLL
24581     WHERE  POH.po_header_id = p_document_id
24582            AND POL.po_header_id = POH.po_header_id
24583            AND POL.po_line_id = PLL.po_line_id
24584            AND PLL.po_header_id = POH.po_header_id
24585            AND POL.clm_delivery_event_code IS NOT NULL
24586            AND ( PLL.clm_delivery_period IS NULL
24587                   OR PLL.clm_delivery_period_uom IS NULL )
24588            AND NVL(PLL.payment_type,'NULL') NOT IN ('DELIVERY','ADVANCE');
24589 
24590    l_progress := '001';
24591 
24592     p_sequence := p_sequence + SQL%ROWCOUNT;
24593 
24594 
24595  x_return_status := FND_API.G_RET_STS_SUCCESS;
24596 
24597 EXCEPTION
24598     WHEN FND_API.G_EXC_ERROR THEN
24599         x_return_status := FND_API.G_RET_STS_ERROR;
24600 
24601     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
24602         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
24603 
24604     WHEN OTHERS THEN
24605         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
24606         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
24607             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
24608         END IF;
24609 
24610         IF (g_debug_unexp) THEN
24611                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
24612                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
24613                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
24614                        || l_progress || ' SQL CODE is '||sqlcode);
24615                 END IF;
24616         END IF;
24617 
24618 END DELIVERY_EVENT_CHECKS;
24619 
24620 /**
24621 * Private Procedure: payment_instruction_checks
24622 * Requires:
24623 *   IN PARAMETERS:
24624 *       p_document_id:      The po_header_id of submitted document
24625 *       p_online_report_id: Id used to INSERT INTO online_report_text table
24626 *       p_user_id:          User performing the action
24627 *       p_login_id:         Last update login_id
24628 *   IN OUT PARAMETERS
24629 *       p_sequence:          Sequence number of last reported error
24630 * Modifies: Inserts error msgs in online_report_text_gt table, uses
24631 *           global_temp tables for processing
24632 * Effects: This procedure runs the document submission checks for CLM Awards.
24633 *   Check 1 : Payment instruction Sequence is not specfied in case payment isntruction code is
24634 *   KO Specified and FV is not enabled in case of BY Fiscal year and By cancellation date
24635 *   Check 2 : Payment instruction is specified as "SINGLE_FUNDING", and there exists multiple
24636 *   ACRN'son distributions against same line.
24637 *   Check 3 : Warning if Payment isntruction sequence is blank
24638 *   Check 4 : For mod doc , check whether at any level instruction was changed to manula type (CO Specified,
24639     or By Fiscal year and By Cancellation Date where FV is not enabled)
24640 * Returns:
24641 *  p_sequence: This parameter contains the current count of number of error
24642 *              messages inserted
24643 */
24644 PROCEDURE payment_instruction_checks(p_document_id IN NUMBER,
24645                        p_draft_type IN VARCHAR2,
24646                        p_online_report_id IN NUMBER,
24647                        p_user_id IN NUMBER,
24648                        p_login_id IN NUMBER,
24649                        p_sequence IN OUT NOCOPY NUMBER,
24650                        x_return_status OUT NOCOPY VARCHAR2) IS
24651 
24652 l_api_name  CONSTANT varchar2(40) := '';
24653 l_progress VARCHAR2(3);
24654 l_textline  po_online_report_text.text_line%TYPE := NULL;
24655 fv_enabled_flag VARCHAR2(1);
24656 l_org_id NUMBEr;
24657 BEGIN
24658 l_progress := '000';
24659 
24660     IF g_debug_stmt THEN
24661         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
24662           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
24663                        l_api_name||'.' || l_progress,
24664                        '');
24665         END IF;
24666     END IF;
24667 
24668     -- Check 1 : Payment instruction Sequence is not specfied in case payment isntruction code is
24669     -- KO Specified and FV is not enabled in case of BY Fiscal year and By cancellation date
24670     SELECT org_id
24671     INTO   l_org_id
24672     FROM po_headers_gt
24673     WHERE po_header_id = p_document_id;
24674 
24675     IF fv_install.enabled(l_org_id) THEN
24676        fv_enabled_flag :='Y';
24677     ELSE
24678        fv_enabled_flag :='N';
24679     END IF;
24680 
24681     l_textline := FND_MESSAGE.get_string('PO', 'PO_PAY_INSTR_SEQ_NOT_SPECIFIED');
24682 
24683     INSERT INTO po_online_report_text_gt(
24684        online_report_id,
24685        last_update_login,
24686        last_updated_by,
24687        last_update_date,
24688        created_by,
24689        creation_date,
24690        line_num,
24691        shipment_num,
24692        distribution_num,
24693        sequence,
24694        text_line,
24695        message_name,
24696        message_type
24697     )
24698     SELECT p_online_report_id,
24699            p_login_id,
24700            p_user_id,
24701            sysdate,
24702            p_user_id,
24703            sysdate,
24704            POL.line_num,
24705            POLL.shipment_num,
24706            POD.distribution_num,
24707            p_sequence + ROWNUM,
24708            SUBSTR (g_linemsg || g_delim ||  Nvl(POL.line_num_display, POL.line_num) ||g_delim ||
24709                    g_shipmsg || g_delim || POLL.shipment_num ||g_delim ||
24710                    g_distmsg || g_delim || POD.distribution_num ||g_delim || l_textline,1,240),
24711            'PO_PAY_INSTR_SEQ_NOT_SPECIFIED',
24712            'E'
24713     FROM   po_headers_gt POH,
24714            po_lines_gt POL,
24715            po_line_locations_gt POLL,
24716            po_distributions_gt POD
24717     WHERE  POH.po_header_id = p_document_id
24718     AND    POL.po_header_id = POH.po_header_id
24719     AND    POLL.po_header_id = POH.po_header_id
24720     AND    POLL.po_line_id  = POL.po_line_id
24721     AND    POD.po_header_id = POH.po_header_id
24722     AND    POD.po_line_id = POL.po_line_id
24723     AND    POD.line_location_id = POLL.line_location_id
24724     AND    POD.clm_payment_sequence_num is NULL
24725     AND    ( POH.clm_payment_instr_code = 'KO_SPECIFIED'
24726              OR (POL.clm_payment_instr_code = 'KO_SPECIFIED'
24727                  AND  POH.clm_payment_instr_code IS NULL)
24728              OR ( (POH.clm_payment_instr_code = 'BY_CANCELLATION_DATE'
24729                    OR ( POL.clm_payment_instr_code = 'BY_CANCELLATION_DATE'
24730                        AND POH.clm_payment_instr_code IS NULL))
24731                    AND fv_enabled_flag ='N')
24732              OR ( (POH.clm_payment_instr_code = 'BY_FISCAL_YR'
24733                    OR ( POL.clm_payment_instr_code = 'BY_FISCAL_YR'
24734                         AND POH.clm_payment_instr_code IS NULL))
24735                    AND fv_enabled_flag ='N') );
24736 
24737     l_progress := '001';
24738 
24739     p_sequence := p_sequence + SQL%ROWCOUNT;
24740 
24741     -- Check 2 : Payment instruction is specified as "SINGLE_FUNDING", and there exists multiple
24742     --  ACRN'son distributions against same line.
24743     l_textline := FND_MESSAGE.get_string('PO', 'PO_PAYMENT_INSTR_SINGLE_FUND');
24744 
24745     INSERT INTO po_online_report_text_gt(
24746        online_report_id,
24747        last_update_login,
24748        last_updated_by,
24749        last_update_date,
24750        created_by,
24751        creation_date,
24752        line_num,
24753        shipment_num,
24754        distribution_num,
24755        sequence,
24756        text_line,
24757        message_name,
24758        message_type
24759     )
24760     SELECT p_online_report_id,
24761            p_login_id,
24762            p_user_id,
24763            sysdate,
24764            p_user_id,
24765            sysdate,
24766            POL.line_num,
24767            0,
24768            0,
24769            p_sequence + ROWNUM,
24770            SUBSTR (g_linemsg || g_delim ||  Nvl(POL.line_num_display, POL.line_num) ||g_delim || l_textline,1,240),
24771            'PO_PAYMENT_INSTR_SINGLE_FUND',
24772            'E'
24773     FROM   po_lines_gt POL
24774     WHERE  POL.po_header_id = p_document_id
24775     AND    POL.clm_payment_instr_code = 'SINGLE_FUNDING'
24776     AND    EXISTS ( SELECT 1
24777                          FROM po_distributions_gt POD, po_distributions_gt POD2
24778                          WHERE POD2.po_header_id = POL.po_header_id
24779                                AND POD2.po_line_id = POL.po_line_id
24780                                AND POD2.po_header_id = POD.po_header_id
24781                                AND POD2.po_line_id = POD.po_line_id
24782                                AND POD2.po_distribution_id <> POD.po_distribution_id
24783                                AND POD2.acrn <> POD.acrn);
24784 
24785     p_sequence := p_sequence + SQL%ROWCOUNT;
24786 
24787   -- Check 3 : Warning if Payment isntruction sequence is blank
24788     l_textline := FND_MESSAGE.get_string('PO', 'PO_PAYMENT_INSTR_BLANK');
24789 
24790     INSERT INTO po_online_report_text_gt(
24791        online_report_id,
24792        last_update_login,
24793        last_updated_by,
24794        last_update_date,
24795        created_by,
24796        creation_date,
24797        line_num,
24798        shipment_num,
24799        distribution_num,
24800        sequence,
24801        text_line,
24802        message_name,
24803        message_type
24804     )
24805     SELECT p_online_report_id,
24806            p_login_id,
24807            p_user_id,
24808            sysdate,
24809            p_user_id,
24810            sysdate,
24811            POL.line_num,
24812            0,
24813            0,
24814            p_sequence + ROWNUM,
24815            SUBSTR (g_linemsg || g_delim ||  Nvl(POL.line_num_display, POL.line_num) || g_delim || l_textline,1,240),
24816            'PO_PAYMENT_INSTR_BLANK',
24817            'W'
24818     FROM   po_lines_gt POL,
24819            po_headers_gt POH
24820     WHERE  POH.po_header_id = p_document_id
24821     AND    POL.po_header_id = POH.po_header_id
24822     AND    Nvl(POL.clm_info_flag,'N')= 'N'
24823     AND    NVl(POL.cost_constraint,'Priced') NOT IN ('NSP', 'NC')
24824     AND    (POL.clm_payment_instr_code is NULL
24825             AND POH.clm_payment_instr_code is NULL )
24826     AND EXISTS ( SELECT 'At least one distribution exists'
24827                  FROM po_distributions_gt pd
24828                  WHERE pd.po_line_id = POL.po_line_id
24829                        AND pd.po_header_id = POL.po_header_id);
24830 
24831   p_sequence := p_sequence + SQL%ROWCOUNT;
24832   --Check 4 : For mod doc , check whether at any level instruction was changed to manula type (CO Specified,
24833   -- or By Fiscal year and By Cancellation Date where FV is not enabled)
24834   IF p_draft_type IN ('MOD', 'PAR') THEN
24835 
24836     -- If payment isntrc was changed to Manual in Mod at header level, warning to reverify sequence
24837     -- at distribution level must be thrown
24838     l_textline := FND_MESSAGE.get_string('PO', 'PO_PAY_INSTR_CHANGED_TO_MANUAL');
24839     INSERT INTO po_online_report_text_gt(
24840        online_report_id,
24841        last_update_login,
24842        last_updated_by,
24843        last_update_date,
24844        created_by,
24845        creation_date,
24846        line_num,
24847        shipment_num,
24848        distribution_num,
24849        sequence,
24850        text_line,
24851        message_name,
24852        message_type
24853     )
24854     SELECT p_online_report_id,
24855            p_login_id,
24856            p_user_id,
24857            sysdate,
24858            p_user_id,
24859            sysdate,
24860            0,
24861            0,
24862            0,
24863            p_sequence + ROWNUM,
24864            SUBSTR ( l_textline ||g_delim||plc.displayed_field||'.',1,240),
24865            'PO_PAY_INSTR_CHANGED_TO_MANUAL',
24866            'W'
24867      FROM  po_headers_gt POH,
24868            po_headers_all POH1,
24869            po_lookup_codes plc
24870     WHERE  POH.po_header_id = p_document_id
24871            AND POH1.po_header_id = POH.po_header_id
24872            AND NVL(POH.clm_payment_instr_code, 'null') <> NVL(POH1.clm_payment_instr_code, 'null')
24873            AND ( NVL(POH.clm_payment_instr_code, 'null') = 'KO_SPECIFIED'
24874                  OR ( NVL(POH.clm_payment_instr_code, 'null') IN ('BY_CANCELLATION_DATE', 'BY_FISCAL_YR')
24875                       AND fv_enabled_flag ='N') )
24876           AND plc.lookup_type ='CLM_PAYMENT_INST'
24877           AND plc.lookup_code = POH.clm_payment_instr_code;
24878 
24879     p_sequence := p_sequence + SQL%ROWCOUNT;
24880     l_textline := FND_MESSAGE.get_string('PO', 'PO_PAY_INSTR_CHANGED_TO_MANUAL');
24881     -- If payment instruction was changed to Manual in Mod at line level, warning to reverify sequence
24882     -- at distribution level must be thrown
24883     INSERT INTO po_online_report_text_gt(
24884        online_report_id,
24885        last_update_login,
24886        last_updated_by,
24887        last_update_date,
24888        created_by,
24889        creation_date,
24890        line_num,
24891        shipment_num,
24892        distribution_num,
24893        sequence,
24894        text_line,
24895        message_name,
24896        message_type
24897     )
24898     SELECT p_online_report_id,
24899            p_login_id,
24900            p_user_id,
24901            sysdate,
24902            p_user_id,
24903            sysdate,
24904            POL.line_num,
24905            0,
24906            0,
24907            p_sequence + ROWNUM,
24908            SUBSTR ( g_linemsg || g_delim ||  Nvl(POL.line_num_display, POL.line_num) || g_delim ||
24909                     l_textline ||g_delim||plc.displayed_field||'.',1,240),
24910            'PO_PAY_INSTR_CHANGED_TO_MANUAL',
24911            'W'
24912     FROM   po_lines_gt POL,
24913            po_lines_all POL1,
24914            po_lookup_codes plc
24915     WHERE  POL.po_header_id = p_document_id
24916            AND POL1.po_header_id = POL.po_header_id
24917            AND POL1.po_line_id = POL.po_line_id
24918            AND NVL(POL.clm_payment_instr_code, 'null') <> NVL(POL1.clm_payment_instr_code, 'null')
24919            AND ( NVL(POL.clm_payment_instr_code, 'null') = 'KO_SPECIFIED'
24920                  OR ( NVL(POL.clm_payment_instr_code, 'null') IN ('BY_CANCELLATION_DATE', 'BY_FISCAL_YR')
24921                       AND fv_enabled_flag ='N') )
24922           AND plc.lookup_type ='CLM_PAYMENT_INST'
24923           AND plc.lookup_code = POL.clm_payment_instr_code;
24924 
24925     p_sequence := p_sequence + SQL%ROWCOUNT;
24926 
24927   END IF;
24928 
24929   x_return_status := FND_API.G_RET_STS_SUCCESS;
24930 
24931   EXCEPTION
24932     WHEN FND_API.G_EXC_ERROR THEN
24933         x_return_status := FND_API.G_RET_STS_ERROR;
24934 
24935     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
24936         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
24937 
24938     WHEN OTHERS THEN
24939         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
24940         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
24941             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
24942         END IF;
24943 
24944         IF (g_debug_unexp) THEN
24945                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
24946                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
24947                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
24948                        || l_progress || ' SQL CODE is '||sqlcode);
24949                 END IF;
24950         END IF;
24951 END payment_instruction_checks;
24952 
24953 --<Sunset Memo POP Validation for Severable Line type>
24954 /**
24955 * Private Procedure: POPVALIDATION_FOR_SEVERABLELINE
24956 * Requires:
24957 *   IN PARAMETERS:
24958 *       p_document_id:      The po_header_id of submitted document
24959 *       p_online_report_id: Id used to INSERT INTO online_report_text table
24960 *       p_user_id:          User performing the action
24961 *       p_login_id:         Last update login_id
24962 *   IN OUT PARAMETERS
24963 *       p_sequence:          Sequence number of last reported error
24964 * Modifies: Inserts error msgs in online_report_text_gt table, uses
24965 *           global_temp tables for processing
24966 * Effects: This procedure runs the document submission checks for CLM Awards.
24967 *          Validate POP for Severable Line Type.
24968 *
24969 * Returns:
24970 *  p_sequence: This parameter contains the current count of number of error
24971 *              messages inserted
24972 */
24973 
24974 PROCEDURE VALIDATE_POP_FOR_SEVERABLE(p_document_id IN NUMBER,
24975                        p_online_report_id IN NUMBER,
24976                        p_user_id IN NUMBER,
24977                        p_login_id IN NUMBER,
24978                        p_sequence IN OUT NOCOPY NUMBER,
24979                        x_return_status OUT NOCOPY VARCHAR2)
24980 IS
24981 
24982 l_api_name  CONSTANT varchar2(40) := 'VALIDATE_POP_FOR_SEVERABLE';
24983 l_progress VARCHAR2(3);
24984 l_textline  po_online_report_text.text_line%TYPE := NULL;
24985 
24986 BEGIN
24987 
24988  l_progress := '000';
24989 
24990     IF g_debug_stmt THEN
24991         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
24992           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || '.'||
24993                        l_api_name||'.' || l_progress,
24994                        '');
24995         END IF;
24996     END IF;
24997 
24998 
24999     -- Check 1 If Line type is Severable and delivery event is not used
25000     -- then difference between earliset POP start date and latest POP end date
25001     -- cannot be beyond 12 months
25002     l_textline := FND_MESSAGE.get_string('PO', 'CLM_POP_BEYOND_12');
25003 
25004     INSERT INTO po_online_report_text_gt
25005                 (online_report_id,
25006                  last_update_login,
25007                  last_updated_by,
25008                  last_update_date,
25009                  created_by,
25010                  creation_date,
25011                  line_num,
25012                  shipment_num,
25013                  distribution_num,
25014                  sequence,
25015                  text_line,
25016                  message_name,
25017                  message_type)
25018     SELECT p_online_report_id,
25019            p_login_id,
25020            p_user_id,
25021            SYSDATE,
25022            p_user_id,
25023            SYSDATE,
25024            POL.line_num,
25025            0,
25026            0,
25027            p_sequence + ROWNUM,
25028            Substr (g_linemsg
25029                    || g_delim
25030                    || Nvl(POL.line_num_display,POL.line_num)
25031                    ||g_delim
25032                    || l_textline, 1, 240),
25033            'CLM_POP_BEYOND_12',
25034            'E'
25035     FROM   po_lines_gt pol,
25036            po_line_types plt,
25037            po_headers_gt pha
25038     WHERE  pha.po_header_id = p_document_id
25039            AND pol.po_header_id = pha.po_header_id
25040            AND pol.line_type_id = plt. line_type_id
25041            AND Nvl(plt.clm_severable_flag, 'N') = 'Y'
25042            AND pol.clm_pop_exception_reason IS NULL
25043            AND pol.clm_delivery_event_code IS NULL
25044            AND 12 < (SELECT Months_between (Max(clm_period_perf_end_date),
25045                                             Min(clm_period_perf_start_date))
25046                      FROM   po_line_locations_gt poll
25047                      WHERE  poll.po_line_id = pol.po_line_id
25048                             AND poll.po_header_id = pol.po_header_id);
25049 
25050     p_sequence := p_sequence + SQL%ROWCOUNT;
25051 
25052     l_progress := '001';
25053 
25054     -- Check 2 - If line type is severable and the difference between
25055     -- earliset start date and latest end date is less than equal to 12 months
25056     -- And Non annual fund is used then
25057     -- POP End date cannot be beyond Fiscal Year end date.
25058     -- To get fiscal year end date :
25059     -- 1) get the earliset start date.
25060     -- 2) Pass this to FV to get the corresponding fiscal year
25061     --    and fiscal year end date.
25062 
25063     l_textline := FND_MESSAGE.get_string('PO', 'CLM_POP_BEYOND_FISCAL_END');
25064 
25065     INSERT INTO po_online_report_text_gt
25066                 (online_report_id,
25067                  last_update_login,
25068                  last_updated_by,
25069                  last_update_date,
25070                  created_by,
25071                  creation_date,
25072                  line_num,
25073                  shipment_num,
25074                  distribution_num,
25075                  sequence,
25076                  text_line,
25077                  message_name,
25078                  message_type)
25079     SELECT p_online_report_id,
25080            p_login_id,
25081            p_user_id,
25082            SYSDATE,
25083            p_user_id,
25084            SYSDATE,
25085            line_num,
25086            shipment_num,
25087            distribution_num,
25088            p_sequence + ROWNUM,
25089            Substr(g_linemsg
25090                   ||g_delim
25091                   ||line_num
25092                   ||g_delim
25093                   ||g_shipmsg
25094                   ||g_delim
25095                   ||shipment_num
25096                   ||g_delim
25097                   ||g_distmsg
25098                   ||g_delim
25099                   ||distribution_num
25100                   ||g_delim
25101                   ||l_textline, 1, 240),
25102            'CLM_POP_BEYOND_FISCAL_END',
25103            'E'
25104     FROM   (SELECT lines.line_num,
25105                    pll.shipment_num,
25106                    pda.distribution_num,
25107                    po_core_s3.Get_fiscal_year(
25108                     (SELECT Min(pll.clm_period_perf_start_date)
25109                        FROM   po_line_locations_gt poll
25110                       WHERE  poll.po_line_id = pll.po_line_id
25111                         AND poll.po_header_id = pll.po_header_id),
25112                      pda.set_of_books_id) Min_start_date_fiscal_year,
25113                    po_core_s3.Get_fiscal_year(pll.clm_period_perf_end_date,
25114                                               pda.set_of_books_id) end_date_fiscal_year
25115             FROM   po_distributions_gt pda,
25116                    po_line_locations_gt pll,
25117                    (SELECT pol.po_line_id,
25118                            Nvl(POL.line_num_display,POL.line_num) line_num
25119                     FROM   po_headers_gt pha,
25120                            po_lines_gt pol,
25121                            po_line_types plt
25122                     WHERE  pha.po_header_id = p_document_id
25123                            AND pol.po_header_id = pha.po_header_id
25124                            AND pol.line_type_id = plt. line_type_id
25125                            AND Nvl(plt.clm_severable_flag, 'N') = 'Y'
25126                            AND pol.clm_pop_exception_reason IS NULL
25127                            AND pol.clm_delivery_event_code IS NULL
25128                            AND 12 >= (SELECT Months_between (
25129                                               Max(clm_period_perf_end_date),
25130                                               Min(clm_period_perf_start_date))
25131                                       FROM   po_line_locations_gt poll
25132                                       WHERE  poll.po_line_id = pol.po_line_id
25133                                              AND poll.po_header_id = pol.po_header_id))
25134                    lines
25135             WHERE  pda.po_line_id = lines.po_line_id
25136                    AND pll.po_line_id = lines.po_line_id
25137                    AND pda.line_location_id = pll.line_location_id
25138                    AND pda.po_line_id = pll.po_line_id
25139                    AND pda.po_header_id = pll.po_header_id
25140                    AND po_core_s3.Is_non_annual_fund(pda.code_combination_id, pda.set_of_books_id) = 'Y'
25141                    AND pda.code_combination_id IS NOT NULL)
25142     WHERE  min_start_date_fiscal_year <> end_date_fiscal_year ;
25143 
25144     p_sequence := p_sequence + SQL%ROWCOUNT;
25145 
25146     l_progress := '002';
25147 
25148     -- Check 3 If Line type is Severable and delivery event is used
25149     -- then POP Duration cannot be beyond 12 months
25150     l_textline := FND_MESSAGE.get_string('PO', 'CLM_POP_DURATION_BEYOND_12');
25151 
25152     INSERT INTO po_online_report_text_gt
25153                 (online_report_id,
25154                  last_update_login,
25155                  last_updated_by,
25156                  last_update_date,
25157                  created_by,
25158                  creation_date,
25159                  line_num,
25160                  shipment_num,
25161                  distribution_num,
25162                  sequence,
25163                  text_line,
25164                  message_name,
25165                  message_type)
25166     SELECT p_online_report_id,
25167            p_login_id,
25168            p_user_id,
25169            SYSDATE,
25170            p_user_id,
25171            SYSDATE,
25172            POL.line_num,
25173            poll.shipment_num,
25174            0,
25175            p_sequence + ROWNUM,
25176            Substr (g_linemsg
25177                    || g_delim
25178                    || Nvl(POL.line_num_display,POL.line_num)
25179                    || g_delim
25180                    || g_shipmsg
25181                    || g_delim
25182                    || poll.shipment_num
25183                    || g_delim
25184                    || l_textline, 1, 240),
25185            'CLM_POP_DURATION_BEYOND_12',
25186            'E'
25187     FROM   po_lines_gt pol,
25188            po_line_types plt,
25189            po_headers_gt pha,
25190            po_line_locations_gt poll
25191     WHERE  pha.po_header_id = p_document_id
25192            AND pol.po_header_id = pha.po_header_id
25193            AND poll.po_line_id = pol.po_line_id
25194            AND poll.po_header_id = pol.po_header_id
25195            AND pol.line_type_id = plt. line_type_id
25196            AND Nvl(plt.clm_severable_flag, 'N') = 'Y'
25197            AND pol.clm_pop_exception_reason IS NULL
25198            AND pol.clm_delivery_event_code IS NOT NULL
25199            AND poll.clm_pop_duration IS NOT NULL
25200            AND poll.clm_pop_duration_uom IS NOT NULL
25201            AND Decode(poll.clm_pop_duration_uom, 'MONTHS', poll.clm_pop_duration,
25202                                                  'DAYS',Months_between
25203                                                         (SYSDATE + poll.clm_pop_duration,
25204                                                          SYSDATE),
25205                                                  'WEEKS',Months_between(
25206                                                           SYSDATE + ( poll.clm_pop_duration * 7 ),
25207                                                           SYSDATE)) > 12;
25208 
25209     p_sequence := p_sequence + SQL%ROWCOUNT;
25210 
25211     l_progress := '003';
25212 
25213 
25214  x_return_status := FND_API.G_RET_STS_SUCCESS;
25215 
25216 EXCEPTION
25217     WHEN FND_API.G_EXC_ERROR THEN
25218         x_return_status := FND_API.G_RET_STS_ERROR;
25219 
25220     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
25221         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
25222 
25223     WHEN OTHERS THEN
25224         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
25225         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
25226             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
25227         END IF;
25228 
25229         IF (g_debug_unexp) THEN
25230                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
25231                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
25232                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
25233                        || l_progress || ' SQL CODE is '||sqlcode);
25234                 END IF;
25235         END IF;
25236 
25237 
25238 END VALIDATE_POP_FOR_SEVERABLE;
25239 
25240 --UCA Project - CLMR4 Changes Begin
25241 /**
25242 * Private Procedure: check_line_ucas
25243 * Requires:
25244 *   IN PARAMETERS:
25245 *       p_document_id:      The po_header_id of submitted document
25246 *       p_online_report_id: Id used to INSERT INTO online_report_text table
25247 *       p_user_id:          User performing the action
25248 *       p_login_id:         Last update login_id
25249 *   IN OUT PARAMETERS
25250 *       p_sequence:          Sequence number of last reported error
25251 * Modifies: Inserts error msgs in online_report_text_gt table, uses
25252 *           global_temp tables for processing
25253 * Effects: This procedure runs the document submission checks for CLM Awards.
25254 *          for Uca Lines.
25255 *
25256 * Returns:
25257 *  p_sequence: This parameter contains the current count of number of error
25258 *              messages inserted
25259 */
25260 PROCEDURE check_line_ucas(p_document_id IN NUMBER,
25261                        p_online_report_id IN NUMBER,
25262                        p_user_id IN NUMBER,
25263                        p_login_id IN NUMBER,
25264                        p_sequence IN OUT NOCOPY NUMBER,
25265                        x_return_status OUT NOCOPY VARCHAR2)
25266 IS
25267 l_api_name  CONSTANT varchar2(40) := 'check_line_ucas';
25268 l_progress VARCHAR2(3);
25269 
25270 
25271 BEGIN
25272 
25273   ---UCA Check 1 for Undef Lines PDD should be greater than Sysdate
25274   l_progress := '000';
25275 
25276   INSERT INTO po_online_report_text_gt
25277               (online_report_id,
25278                last_update_login,
25279                last_updated_by,
25280                last_update_date,
25281                created_by,
25282                creation_date,
25283                line_num,
25284                shipment_num,
25285                distribution_num,
25286                SEQUENCE,
25287                text_line,
25288                message_name,
25289                message_type)
25290   SELECT p_online_report_id,
25291          p_login_id,
25292          p_user_id,
25293          SYSDATE,
25294          p_user_id,
25295          SYSDATE,
25296          POL.line_num,
25297          0,
25298          0,
25299          p_sequence + ROWNUM,
25300          po_core_s.Get_translated_text('PO_UCA_PDD_PAST_DATE',
25301          'LINE_NUM', pol.line_num_display),
25302          'PO_UCA_PDD_PAST_DATE',
25303          'W'
25304   FROM   po_line_ucas uc,
25305          po_lines_gt pol,
25306          po_headers_gt poh
25307   WHERE  Nvl(pol.clm_undef_flag, 'N') = 'Y'
25308          AND uc.planned_def_date IS NOT NULL
25309          AND uc.planned_def_date < SYSDATE
25310          AND poh.po_header_id = pol.po_header_id
25311          AND poh.draft_id = uc.undef_draft_id
25312          AND pol.po_line_id = uc.po_line_id
25313          AND poh.po_header_id = p_document_id;
25314 
25315   p_sequence := p_sequence + SQL%ROWCOUNT;
25316 
25317   -------------------------------------------------------------------------------
25318   ---UCA Check 2 for Undef Lines
25319   --If the user has chosen the Action as Undefinitize and hasn?t entered a value
25320   --for either or all of the fields following fields ? Undefinitized Amount, Not
25321   --To Exceed Amount, Limitation Of Government Liability, Planned Definitization Date
25322   --and Undefinitization Description -   and clicks on Submit
25323   l_progress := '001';
25324 
25325 
25326   INSERT INTO po_online_report_text_gt
25327               (online_report_id,
25328                last_update_login,
25329                last_updated_by,
25330                last_update_date,
25331                created_by,
25332                creation_date,
25333                line_num,
25334                shipment_num,
25335                distribution_num,
25336                SEQUENCE,
25337                text_line,
25338                message_name,
25339                message_type)
25340   SELECT p_online_report_id,
25341          p_login_id,
25342          p_user_id,
25343          SYSDATE,
25344          p_user_id,
25345          SYSDATE,
25346          POL.line_num,
25347          0,
25348          0,
25349          p_sequence + ROWNUM,
25350          po_core_s.Get_translated_text('PO_UCA_UNDEF_INCOMPLETE', 'LINE_NUM',
25351          pol.line_num_display),
25352          'PO_UCA_UNDEF_INCOMPLETE',
25353          'E'
25354   FROM   po_line_ucas uc,
25355          po_lines_gt pol,
25356          po_headers_gt poh
25357   WHERE  Nvl(pol.clm_undef_flag, 'N') = 'Y'
25358        AND ( NOT ( ( uc.planned_def_date IS NOT NULL
25359                      AND uc.undef_amount IS NOT NULL
25360                      AND uc.limit_gov_liability_amt IS NOT NULL
25361                      AND uc.not_to_exceed_amount IS NOT NULL )
25362                     OR ( uc.planned_def_date IS NOT NULL
25363                          AND uc.undef_description IS NOT NULL
25364                          AND uc.undef_amount IS NULL
25365                          AND uc.limit_gov_liability_amt IS NULL
25366                          AND uc.not_to_exceed_amount IS NULL ) ) )
25367          AND poh.po_header_id = pol.po_header_id
25368          AND poh.draft_id = uc.undef_draft_id
25369          AND pol.po_line_id = uc.po_line_id
25370          AND poh.po_header_id = p_document_id;
25371 
25372     p_sequence := p_sequence + SQL%ROWCOUNT;
25373 
25374   -------------------------------------------------------------------------------
25375   ---UCA Check 3 for Def Lines
25376   --- For Def Lines, If the user doesnt enter a value for Definitization Description
25377   ---for a description based UCA and clicks on Submit
25378     l_progress := '002';
25379 
25380 
25381   INSERT INTO po_online_report_text_gt
25382               (online_report_id,
25383                last_update_login,
25384                last_updated_by,
25385                last_update_date,
25386                created_by,
25387                creation_date,
25388                line_num,
25389                shipment_num,
25390                distribution_num,
25391                SEQUENCE,
25392                text_line,
25393                message_name,
25394                message_type)
25395   SELECT p_online_report_id,
25396          p_login_id,
25397          p_user_id,
25398          SYSDATE,
25399          p_user_id,
25400          SYSDATE,
25401          POL.line_num,
25402          0,
25403          0,
25404          p_sequence + ROWNUM,
25405          po_core_s.Get_translated_text('PO_UCA_DEF_DESC_ERR', 'DOC_NUM',
25406          uc.undef_clm_document_number, 'LINE_NUM', pol.line_num_display),
25407          'PO_UCA_DEF_DESC_ERR',
25408          'E'
25409   FROM   po_line_ucas uc,
25410          po_lines_gt pol,
25411          po_headers_gt poh
25412   WHERE  Nvl(pol.clm_undef_action_code, 'X') = 'DEF'
25413          AND uc.undef_amount IS NULL
25414          AND uc.not_to_exceed_amount IS NULL
25415          AND uc.limit_gov_liability_amt IS NULL
25416          AND uc.def_description IS NULL
25417          AND poh.po_header_id = pol.po_header_id
25418          AND (poh.draft_id = uc.def_draft_id
25419               OR poh.draft_id = uc.def_par_draft_id)
25420          AND pol.po_line_id = uc.po_line_id
25421          AND poh.po_header_id = p_document_id;
25422 
25423   p_sequence := p_sequence + SQL%ROWCOUNT;
25424 
25425   -------------------------------------------------------------------------------
25426   --UCA Check 4,5 for Definetization Lines.
25427   -- For Check 4
25428   --For an Amount based line, the ?Change in <complex pricing attribute label>?
25429   --is more than the ?Maximum Allowable Change in <complex pricing attribute label>?
25430   --and the user clicks on Submit
25431   -- For Check 5
25432   --For a Quantity based line, the ?Change in Extended Price?
25433   --is more than the ?Maximum Allowable Change in Extended Price?
25434   --and the user clicks on Submit
25435 
25436   -------------------------------------------------------------------------------
25437   l_progress := '003';
25438 
25439 
25440   INSERT INTO po_online_report_text_gt
25441               (online_report_id,
25442                last_update_login,
25443                last_updated_by,
25444                last_update_date,
25445                created_by,
25446                creation_date,
25447                line_num,
25448                shipment_num,
25449                distribution_num,
25450                SEQUENCE,
25451                text_line,
25452                message_name,
25453                message_type)
25454   SELECT p_online_report_id,
25455        p_login_id,
25456        p_user_id,
25457        SYSDATE,
25458        p_user_id,
25459        SYSDATE,
25460        line_num,
25461        0,
25462        0,
25463        p_sequence + ROWNUM,
25464        po_core_s.Get_translated_text('PO_UCA_MAX_CHANGE_ERR', 'CP_LABEL',
25465        cp_label,
25466        'CP_LABEL2', cp_label, 'LINE_NUM', line_num_display),
25467        'PO_UCA_MAX_CHANGE_ERR',
25468        'W'
25469   FROM   (SELECT Decode(matching_basis, 'QUANTITY',
25470                ( Nvl(unit_price*quantity, 0) -
25471                   Nvl(old_unit_price* old_quantity, 0) ),
25472                                       ( Nvl(amount, 0) - Nvl(old_amount, 0) ))
25473                change_in_ext_price,
25474                Nvl((SELECT SUM(Nvl(not_to_exceed_amount, 0)) -
25475                            SUM(Nvl(undef_amount, 0))
25476                                max_all_change
25477                     FROM   po_line_ucas uc
25478                     WHERE  pol.po_line_id = uc.po_line_id
25479                            AND ph.po_header_id = uc.po_header_id
25480                            AND (uc.def_draft_id = ph.draft_id
25481                                 OR uc.def_par_draft_id = ph.draft_id)), 0) max_change,
25482                po_clm_cp_label_ext.Getamountlabel(
25483                  pol.order_type_lookup_code,
25484                  pol.contract_type,
25485                  pol.clm_idc_type) cp_label,
25486                line_num_display,
25487                line_num
25488         FROM   po_lines_gt pol,
25489                po_headers_gt ph
25490         WHERE  pol.po_header_id = ph.po_header_id
25491                AND ph.po_header_id = p_document_id
25492                AND Nvl(pol.clm_undef_action_code, 'X') = 'DEF')
25493   WHERE  change_in_ext_price > max_change;
25494 
25495   p_sequence := p_sequence + SQL%ROWCOUNT;
25496   l_progress := '004';
25497   --UCA Line level validation on PO Lines Find out
25498   --whether def is actually done or not.
25499   l_progress := '004';
25500 
25501   INSERT INTO po_online_report_text_gt
25502               (online_report_id,
25503                last_update_login,
25504                last_updated_by,
25505                last_update_date,
25506                created_by,
25507                creation_date,
25508                line_num,
25509                shipment_num,
25510                distribution_num,
25511                SEQUENCE,
25512                text_line,
25513                message_name,
25514                message_type)
25515   SELECT p_online_report_id,
25516          p_login_id,
25517          p_user_id,
25518          SYSDATE,
25519          p_user_id,
25520          SYSDATE,
25521          line_num,
25522          0,
25523          0,
25524          p_sequence + ROWNUM,
25525          po_core_s.Get_translated_text('PO_UCA_LINE_DEF_NO_UCAS', 'LINE_NUM',
25526          line_num_display),
25527          'PO_UCA_LINE_DEF_NO_UCAS',
25528          'E'
25529   FROM   po_lines_gt pol,
25530          po_headers_gt ph
25531   WHERE  ph.po_header_id = p_document_id
25532          AND ph.po_header_id = pol.po_header_id
25533          AND Nvl(pol.clm_undef_action_code,'X') = 'DEF'
25534          AND NOT EXISTS (SELECT 1
25535                          FROM   po_line_ucas ucas
25536                          WHERE  ucas.po_line_id = pol.po_line_id
25537                                 AND ucas.po_header_id = p_document_id
25538                                 AND ( ucas.def_draft_id = Ph.draft_id
25539                                        OR ucas.def_par_draft_id = ph.draft_id ));
25540   p_sequence := p_sequence + SQL%ROWCOUNT;
25541 
25542   l_progress := '005';
25543 
25544 
25545   x_return_status := FND_API.G_RET_STS_SUCCESS;
25546 EXCEPTION
25547     WHEN FND_API.G_EXC_ERROR THEN
25548         x_return_status := FND_API.G_RET_STS_ERROR;
25549 
25550     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
25551         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
25552 
25553     WHEN OTHERS THEN
25554         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
25555         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
25556             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
25557         END IF;
25558 
25559         IF (g_debug_unexp) THEN
25560                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
25561                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
25562                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
25563                        || l_progress || ' SQL CODE is '||sqlcode);
25564                 END IF;
25565         END IF;
25566 END check_line_ucas;
25567 
25568 --UCA Project - CLMR4 Changes End
25569 
25570 
25571 --CLM Phase 4 - Elins project Start
25572 /**
25573 * Private Procedure: check_line_ucas
25574 * Requires:
25575 *   IN PARAMETERS:
25576 *       p_document_id:      The po_header_id of submitted document
25577 *       p_online_report_id: Id used to INSERT INTO online_report_text table
25578 *       p_user_id:          User performing the action
25579 *       p_login_id:         Last update login_id
25580 *   IN OUT PARAMETERS
25581 *       p_sequence:          Sequence number of last reported error
25582 * Modifies: Inserts error msgs in online_report_text_gt table, uses
25583 *           global_temp tables for processing
25584 * Effects: This procedure runs the document submission checks for CLM Awards.
25585 *          for Uca Lines.
25586 *
25587 * Returns:
25588 *  p_sequence: This parameter contains the current count of number of error
25589 *              messages inserted
25590 */
25591 PROCEDURE validate_exhibits(p_document_id IN NUMBER,
25592                        p_draft_id         IN NUMBER,
25593                        p_online_report_id IN NUMBER,
25594                        p_user_id IN NUMBER,
25595                        p_login_id IN NUMBER,
25596                        p_sequence IN OUT NOCOPY NUMBER,
25597                        x_return_status OUT NOCOPY VARCHAR2)
25598 IS
25599 l_api_name CONSTANT varchar2(40) := 'validate_exhibits';
25600 d_module    VARCHAR2(70) := g_log_head||l_api_name;
25601 d_progress NUMBER;
25602 
25603 
25604 BEGIN
25605 
25606   ---Check 1 for Orphan exhibits
25607   d_progress := 0;
25608   IF (PO_LOG.d_proc) THEN
25609     PO_LOG.proc_begin(d_module);
25610     PO_LOG.proc_begin(d_module, 'p_document_id', p_document_id);
25611     PO_LOG.proc_begin(d_module, 'p_draft_id', p_draft_id);
25612   END IF;
25613 
25614 
25615   INSERT INTO po_online_report_text_gt
25616               (online_report_id,
25617                last_update_login,
25618                last_updated_by,
25619                last_update_date,
25620                created_by,
25621                creation_date,
25622                line_num,
25623                shipment_num,
25624                distribution_num,
25625                SEQUENCE,
25626                text_line,
25627                message_name,
25628                message_type)
25629   SELECT p_online_report_id,
25630          p_login_id,
25631          p_user_id,
25632          SYSDATE,
25633          p_user_id,
25634          SYSDATE,
25635          0,
25636          0,
25637          0,
25638          p_sequence + ROWNUM,
25639          po_core_s.Get_translated_text('PO_CLM_EXHIBIT_ORPHAN', 'EXHIBIT_NAME', poe.exhibit_name),
25640          'PO_CLM_EXHIBIT_ORPHAN',
25641          'E'
25642   FROM
25643     po_exhibit_details_merge_v poe,
25644     po_headers_gt poh
25645   WHERE poh.po_header_id = p_document_id
25646         AND poh.po_header_id = poe.po_header_id
25647         AND poh.draft_id = poe.draft_id
25648         AND poe.reference_line_id IS NULL;
25649 
25650   p_sequence := p_sequence + SQL%ROWCOUNT;
25651 
25652   d_progress := 20;
25653   IF (PO_LOG.d_stmt) THEN
25654     PO_LOG.stmt(d_module, d_progress, 'p_sequence', p_sequence);
25655   END IF;
25656 
25657   -- Check for Invalid line-elin reference. Priced - Nsp and vice versa valid. No P-P or NSP-NSP
25658  INSERT INTO po_online_report_text_gt
25659               (online_report_id,
25660                last_update_login,
25661                last_updated_by,
25662                last_update_date,
25663                created_by,
25664                creation_date,
25665                line_num,
25666                shipment_num,
25667                distribution_num,
25668                SEQUENCE,
25669                text_line,
25670                message_name,
25671                message_type)
25672   SELECT p_online_report_id,
25673          p_login_id,
25674          p_user_id,
25675          SYSDATE,
25676          p_user_id,
25677          SYSDATE,
25678          0,
25679          0,
25680          0,
25681          p_sequence + ROWNUM,
25682          po_core_s.Get_translated_text('PO_CLM_EXHIBIT_INVALID_REF', 'EXHIBIT_NAME', poed.exhibit_name),
25683          'PO_CLM_EXHIBIT_INVALID_REF',
25684          'E'
25685   FROM  po_lines_gt pol,
25686         po_exhibit_details_merge_v poed,
25687         (SELECT exhibit_name,
25688                 (SELECT Decode( Count(1), 0, fnd_message.get_string('PO','PO_EXHIBIT_NSP_LABEL'), fnd_message.get_string('PO','PO_EXHIBIT_PRICED_LABEL'))
25689                   FROM po_lines_gt pl
25690                   WHERE
25691                   pl.po_header_id = ped.PO_HEADER_ID AND
25692                   pl.clm_exhibit_name = ped.EXHIBIT_NAME AND
25693                   Nvl(pl.cost_constraint,'Priced') NOT IN ('NSP','NC')
25694                   )  pricing
25695           FROM po_exhibit_details_merge_v ped WHERE ped.po_header_id = p_document_id AND ped.draft_id = p_draft_id) exhibit_pricing_cur
25696   WHERE  pol.po_header_id = p_document_id
25697   AND pol.po_header_id = poed.po_header_id
25698   AND poed.draft_id = p_draft_id
25699   AND exhibit_pricing_cur.exhibit_name = poed.exhibit_name
25700   AND poed.is_cdrl = 'N' -- Data deliverable (CDRL) exhibit can be linked to any line.
25701   AND pol.po_line_id = poed.reference_line_id
25702   AND (
25703         (exhibit_pricing_cur.pricing = fnd_message.get_string('PO','PO_EXHIBIT_NSP_LABEL')
25704           AND  (Nvl(pol.cost_constraint,'Priced') = 'NSP' OR
25705                 Nvl(pol.cost_constraint,'Priced') = 'NC'
25706                 )
25707         )
25708         OR
25709         (exhibit_pricing_cur.pricing = fnd_message.get_string('PO','PO_EXHIBIT_PRICED_LABEL')
25710           AND  Nvl(pol.cost_constraint,'Priced') NOT IN ('NSP','NC')
25711         )
25712        )
25713   ORDER BY poed.exhibit_name;
25714 
25715   p_sequence := p_sequence + SQL%ROWCOUNT;
25716 
25717   d_progress := 30;
25718   IF (PO_LOG.d_stmt) THEN
25719     PO_LOG.stmt(d_module, d_progress, 'p_sequence', p_sequence);
25720   END IF;
25721 
25722   x_return_status := FND_API.G_RET_STS_SUCCESS;
25723 EXCEPTION
25724     WHEN FND_API.G_EXC_ERROR THEN
25725         x_return_status := FND_API.G_RET_STS_ERROR;
25726 
25727     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
25728         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
25729 
25730     WHEN OTHERS THEN
25731         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
25732         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
25733             FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_api_name);
25734         END IF;
25735 
25736         IF (g_debug_unexp) THEN
25737                 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED) THEN
25738                   FND_LOG.string(FND_LOG.level_unexpected, g_log_head ||
25739                        l_api_name || '.others_exception', 'EXCEPTION: Location is '
25740                        || d_progress || ' SQL CODE is '||sqlcode);
25741                 END IF;
25742         END IF;
25743 END validate_exhibits;
25744 --CLM Phase 4 - Elins project End
25745 
25746 --CLM Phase 4 - MIPR Project Begin
25747 /**
25748 * Private Procedure: check_po_mipr_reference
25749 * Requires:
25750 *   IN PARAMETERS:
25751 *       p_document_id:      The po_header_id of submitted document
25752 *       p_draft_id:         The draft_id of submitted document
25753 *       p_online_report_id: Id used to INSERT INTO online_report_text table
25754 *       p_user_id:          User performing the action
25755 *       p_login_id:         Last update login_id
25756 *   IN OUT PARAMETERS
25757 *       p_sequence:         Sequence number of last reported error
25758 * Modifies: Inserts error msgs in online_report_text_gt table, uses
25759 *           global_temp tables for processing
25760 * Effects: This procedure runs the document submission checks for CLM Awards
25761 *          referencing MIPR.
25762 *
25763 * Returns:
25764 *  p_sequence: This parameter contains the current count of number of error
25765 *              messages inserted
25766 */
25767 PROCEDURE check_po_mipr_reference(p_document_id IN NUMBER,
25768                                   p_draft_id IN NUMBER,
25769                                   p_online_report_id IN NUMBER,
25770                                   p_user_id IN NUMBER,
25771                                   p_login_id IN NUMBER,
25772                                   p_sequence IN OUT NOCOPY NUMBER,
25773                                   x_return_status OUT NOCOPY VARCHAR2)
25774 IS
25775 l_api_name CONSTANT varchar2(40) := 'check_po_mipr_reference';
25776 d_module    VARCHAR2(70) := g_log_head||l_api_name;
25777 d_progress NUMBER;
25778 
25779 l_procedure_id PO_SESSION_GT.key%TYPE;
25780 l_textline  po_online_report_text.text_line%TYPE := NULL;
25781 l_mipr_own_ref_exists VARCHAR2(1);
25782 l_mipr_others_ref_exists VARCHAR2(1);
25783 l_non_mipr_ref_exists VARCHAR2(1);
25784 BEGIN
25785 
25786   d_progress := 0;
25787 
25788   l_procedure_id := PO_CORE_S.get_session_gt_nextval;
25789 
25790   IF (PO_LOG.d_proc) THEN
25791     PO_LOG.proc_begin(d_module);
25792     PO_LOG.proc_begin(d_module, 'p_document_id', p_document_id);
25793     PO_LOG.proc_begin(d_module, 'p_draft_id', p_draft_id);
25794     PO_LOG.proc_begin(d_module, 'l_procedure_id', l_procedure_id);
25795   END IF;
25796 
25797   ---Collect the MIPR references on the Award
25798   INSERT INTO po_session_gt(
25799          KEY,  -- Unique Key for procedure
25800          num1, -- PO po_distribution_id
25801          num2, -- MIPR distribution_id
25802          num3, -- MIPR requisition_header_id
25803          num4, -- MIPR clm_assist_office
25804          num5, -- MIPR clm_req_office
25805          char1,-- MIPR clm_mipr_type
25806          char2,-- MIPR clm_mipr_obligation_type
25807          char3,-- PO line_num
25808          char4,-- PO line_num_display
25809          char5,-- PO shipment_num
25810          char6)-- PO distribution_num
25811   SELECT l_procedure_id,
25812          POD.po_distribution_id,
25813          PRD.distribution_id,
25814          PRH.requisition_header_id,
25815          PRH.clm_assist_office,
25816          PRH.clm_req_office,
25817          PRH.clm_mipr_type,
25818          PRL.clm_mipr_obligation_type,
25819          POL.line_num,
25820          POL.line_num_display,
25821          POLL.shipment_num,
25822          POD.distribution_num
25823   FROM   po_distributions_gt POD,
25824          po_req_distributions_all PRD,
25825          po_requisition_lines_all PRL,
25826          po_requisition_headers_all PRH,
25827          po_line_locations_gt POLL,
25828          po_lines_gt POL
25829   WHERE  POD.po_header_id = p_document_id
25830   AND    POD.req_distribution_id = PRD.distribution_id
25831   AND    PRD.requisition_line_id = PRL.requisition_line_id
25832   AND    PRL.requisition_header_id = PRH.requisition_header_id
25833   AND    PRH.clm_mipr_type IN ('MIPR_OWN', 'MIPR_OTHERS')
25834   AND    POD.line_location_id = POLL.line_location_id
25835   AND    POLL.po_line_id = POL.po_line_id
25836   AND    nvl(POLL.cancel_flag, 'N') = 'N'
25837   AND    nvl(POLL.closed_code, 'OPEN') <> 'FINALLY CLOSED';
25838 
25839   IF SQL%ROWCOUNT = 0 THEN -- No MIPR Reference exists on Award, Return
25840     x_return_status := FND_API.G_RET_STS_SUCCESS;
25841     RETURN;
25842   END IF;
25843 
25844   d_progress := 10;
25845   -- Checks if MIPR Own reference exists on Award
25846   BEGIN
25847     SELECT 'Y'
25848     INTO   l_mipr_own_ref_exists
25849     FROM   po_session_gt
25850     WHERE  KEY = l_procedure_id
25851     AND    char1 = 'MIPR_OWN'
25852     AND    ROWNUM = 1;
25853 
25854   EXCEPTION
25855     WHEN NO_DATA_FOUND THEN
25856       l_mipr_own_ref_exists := 'N';
25857   END;
25858 
25859   d_progress := 20;
25860   -- Checks if MIPR Others reference exists on Award
25861   BEGIN
25862     SELECT 'Y'
25863     INTO   l_mipr_others_ref_exists
25864     FROM   po_session_gt
25865     WHERE  KEY = l_procedure_id
25866     AND    char1 = 'MIPR_OTHERS'
25867     AND    ROWNUM = 1;
25868   EXCEPTION
25869     WHEN NO_DATA_FOUND THEN
25870       l_mipr_others_ref_exists := 'N';
25871   END;
25872 
25873   d_progress := 30;
25874   -- Check if non-MIPR references(PR or PAR) exists on Award
25875   BEGIN
25876     SELECT 'Y'
25877     INTO   l_non_mipr_ref_exists
25878     FROM   dual
25879     WHERE EXISTS(
25880       SELECT 1
25881       FROM   po_distributions_gt POD
25882       WHERE  POD.po_header_id = p_document_id
25883       AND    req_distribution_id IS NOT NULL --PO Dist has a req reference
25884       AND NOT EXISTS(
25885         SELECT 1
25886         FROM   po_session_gt GT
25887         WHERE  GT.key = l_procedure_id
25888         AND    GT.num1 = POD.po_distribution_id)); --PO Dist does not have MIPR Ref
25889   EXCEPTION
25890     WHEN NO_DATA_FOUND THEN
25891       l_non_mipr_ref_exists := 'N';
25892   END;
25893 
25894   IF (PO_LOG.d_stmt) THEN
25895     PO_LOG.stmt(d_module, d_progress, 'l_mipr_own_ref_exists', l_mipr_own_ref_exists);
25896     PO_LOG.stmt(d_module, d_progress, 'l_mipr_others_ref_exists', l_mipr_others_ref_exists);
25897     PO_LOG.stmt(d_module, d_progress, 'l_non_mipr_ref_exists', l_non_mipr_ref_exists);
25898   END IF;
25899 
25900   -- Start: MIPR Own reference checks
25901   IF l_mipr_own_ref_exists = 'Y' THEN
25902 
25903     d_progress := 40;
25904     -- Check 1: If the award has MIPR own reference, FPDS reporting method should be 'Exempt'
25905     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_MIPR_CAR_EXEMPT');
25906 
25907     INSERT INTO po_online_report_text_gt(
25908                 online_report_id,
25909                 last_update_login,
25910                 last_updated_by,
25911                 last_update_date,
25912                 created_by,
25913                 creation_date,
25914                 SEQUENCE,
25915                 text_line,
25916                 message_name)
25917     SELECT p_online_report_id,
25918            p_login_id,
25919            p_user_id,
25920            SYSDATE,
25921            p_user_id,
25922            SYSDATE,
25923            p_sequence +	ROWNUM,
25924            l_textline,
25925            'PO_MIPR_CAR_EXEMPT'
25926     FROM   dual
25927     WHERE EXISTS(
25928       SELECT 1
25929       FROM   po_clm_cars
25930       WHERE  po_header_id = p_document_id
25931       AND    po_draft_id = p_draft_id
25932       AND    reporting_method <> 'EXEMPT');
25933 
25934     p_sequence := p_sequence + SQL%ROWCOUNT;
25935 
25936     d_progress := 50;
25937     -- Check 2: If award has MIPR own reference, Supplier communication method must be set to none.
25938     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_MIPR_COMM_METHOD_NONE');
25939 
25940     INSERT INTO	po_online_report_text_gt(
25941                 online_report_id,
25942                 last_update_login,
25943                 last_updated_by,
25944                 last_update_date,
25945                 created_by,
25946                 creation_date,
25947                 SEQUENCE,
25948                 text_line,
25949                 message_name)
25950     SELECT p_online_report_id,
25951            p_login_id,
25952            p_user_id,
25953            SYSDATE,
25954            p_user_id,
25955            SYSDATE,
25956            p_sequence +	ROWNUM,
25957            l_textline,
25958            'PO_MIPR_COMM_METHOD_NONE'
25959     FROM   po_headers_gt
25960     WHERE  po_header_id = p_document_id
25961     AND    Nvl(supplier_notif_method, 'NONE') <> 'NONE';
25962 
25963     p_sequence := p_sequence + SQL%ROWCOUNT;
25964 
25965     d_progress := 60;
25966     -- Check 3: If award has MIPR own reference, Bilateral Indicator must be set to None
25967     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_MIPR_BILATERAL_IND_NONE');
25968 
25969     INSERT INTO po_online_report_text_gt(
25970                 online_report_id,
25971                 last_update_login,
25972                 last_updated_by,
25973                 last_update_date,
25974                 created_by,
25975                 creation_date,
25976                 SEQUENCE,
25977                 text_line,
25978                 message_name)
25979     SELECT p_online_report_id,
25980            p_login_id,
25981            p_user_id,
25982            SYSDATE,
25983            p_user_id,
25984            SYSDATE,
25985            p_sequence +	ROWNUM,
25986            l_textline,
25987            'PO_MIPR_BILATERAL_IND_NONE'
25988     FROM   po_headers_gt
25989     WHERE  po_header_id = p_document_id
25990     AND    Nvl(Acceptance_Required_Flag, 'N') <> 'N';
25991 
25992     p_sequence := p_sequence + SQL%ROWCOUNT;
25993 
25994     d_progress := 70;
25995     -- Check 4: Multiple MIPR-Own are being referenced in the same award (Warning)
25996     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_MIPR_MULTI_OWN_REF');
25997 
25998     INSERT INTO po_online_report_text_gt(
25999                 online_report_id,
26000                 last_update_login,
26001                 last_updated_by,
26002                 last_update_date,
26003                 created_by,
26004                 creation_date,
26005                 SEQUENCE,
26006                 text_line,
26007                 message_name,
26008                 message_type)
26009     SELECT p_online_report_id,
26010            p_login_id,
26011            p_user_id,
26012            SYSDATE,
26013            p_user_id,
26014            SYSDATE,
26015            p_sequence +	ROWNUM,
26016            l_textline,
26017            'PO_MIPR_MULTI_OWN_REF',
26018            'W'
26019     FROM   dual
26020     WHERE EXISTS(
26021       SELECT 1
26022       FROM   po_session_gt
26023       WHERE  key = l_procedure_id
26024       AND    char1 = 'MIPR_OWN'
26025       HAVING Count(DISTINCT num3) > 1);--requisition_header_id of referenced MIPR
26026 
26027     p_sequence := p_sequence + SQL%ROWCOUNT;
26028 
26029     d_progress := 80;
26030     -- Check 5: Dist is referencing MIPR-Own, and the award also references MIPR-Own of other Assisting agency
26031     -- l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_MIPR_MULTI_ASST_OFF_REF');
26032 
26033     INSERT INTO po_online_report_text_gt(
26034                 online_report_id,
26035                 last_update_login,
26036                 last_updated_by,
26037                 last_update_date,
26038                 created_by,
26039                 creation_date,
26040                 line_num,
26041                 shipment_num,
26042                 distribution_num,
26043                 SEQUENCE,
26044                 text_line,
26045                 message_name)
26046     SELECT p_online_report_id,
26047            p_login_id,
26048            p_user_id,
26049            SYSDATE,
26050            p_user_id,
26051            SYSDATE,
26052            GT.char3,
26053            GT.char5,
26054            GT.char6,
26055            p_sequence +	ROWNUM,
26056            PO_CORE_S.get_translated_text(
26057            'PO_MIPR_MULTI_ASST_OFF_REF',
26058            'LINE_NUM', GT.char4,
26059            'SHIP_NUM', GT.char5,
26060            'DIST_NUM', GT.char6),
26061            'PO_MIPR_MULTI_ASST_OFF_REF'
26062     FROM   po_session_gt GT
26063     WHERE  key = l_procedure_id
26064     AND    char1 = 'MIPR_OWN'
26065     AND EXISTS(
26066       SELECT 1
26067       FROM   po_session_gt
26068       WHERE  key = l_procedure_id
26069       AND    char1 = 'MIPR_OWN'
26070       HAVING Count(DISTINCT num4) > 1);--clm_assist_office of referenced MIPR
26071 
26072     p_sequence := p_sequence + SQL%ROWCOUNT;
26073 
26074     d_progress := 90;
26075     -- Check 6: Dist is referencing MIPR-Own, and the award also references PR/PAR/MIPR-Others
26076     -- l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_MIPR_OWN_AND_PR_REF');
26077 
26078     INSERT INTO po_online_report_text_gt(
26079                 online_report_id,
26080                 last_update_login,
26081                 last_updated_by,
26082                 last_update_date,
26083                 created_by,
26084                 creation_date,
26085                 line_num,
26086                 shipment_num,
26087                 distribution_num,
26088                 SEQUENCE,
26089                 text_line,
26090                 message_name)
26091     SELECT p_online_report_id,
26092            p_login_id,
26093            p_user_id,
26094            SYSDATE,
26095            p_user_id,
26096            SYSDATE,
26097            GT.char3,
26098            GT.char5,
26099            GT.char6,
26100            p_sequence +	ROWNUM,
26101            PO_CORE_S.get_translated_text(
26102            'PO_MIPR_OWN_AND_PR_REF',
26103            'LINE_NUM', GT.char4,
26104            'SHIP_NUM', GT.char5,
26105            'DIST_NUM', GT.char6),
26106            'PO_MIPR_OWN_AND_PR_REF'
26107     FROM   po_session_gt GT
26108     WHERE  key = l_procedure_id
26109     AND    char1 = 'MIPR_OWN'
26110     AND    (l_mipr_others_ref_exists = 'Y' OR l_non_mipr_ref_exists = 'Y');
26111 
26112     p_sequence := p_sequence + SQL%ROWCOUNT;
26113 
26114     d_progress := 100;
26115     -- Check 7: Dist is referencing MIPR-Own Direct Cite, and the award also references MIPR-Own Reimbursement
26116     -- l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_MIPR_MULTI_OBLIG_REF');
26117 
26118 	INSERT INTO po_online_report_text_gt(
26119                 online_report_id,
26120                 last_update_login,
26121                 last_updated_by,
26122                 last_update_date,
26123                 created_by,
26124                 creation_date,
26125                 line_num,
26126                 shipment_num,
26127                 distribution_num,
26128                 SEQUENCE,
26129                 text_line,
26130                 message_name)
26131     SELECT p_online_report_id,
26132            p_login_id,
26133            p_user_id,
26134            SYSDATE,
26135            p_user_id,
26136            SYSDATE,
26137            GT.char3,
26138            GT.char5,
26139            GT.char6,
26140            p_sequence +	ROWNUM,
26141            PO_CORE_S.get_translated_text(
26142            'PO_MIPR_MULTI_OBLIG_REF',
26143            'LINE_NUM', GT.char4,
26144            'SHIP_NUM', GT.char5,
26145            'DIST_NUM', GT.char6),
26146            'PO_MIPR_MULTI_OBLIG_REF'
26147     FROM   po_session_gt GT
26148     WHERE  key = l_procedure_id
26149     AND    char1 = 'MIPR_OWN'
26150     AND    char2 = 'DIRECT_CITE' -- Direct Cite clm_mipr_obligation_type of referenced MIPR
26151     AND EXISTS(
26152       SELECT 1
26153       FROM   po_session_gt
26154       WHERE  key = l_procedure_id
26155       AND    char1 = 'MIPR_OWN'
26156       AND    char2 = 'REIMBURSEMENT'); -- Reimbursement clm_mipr_obligation_type
26157 
26158     p_sequence := p_sequence + SQL%ROWCOUNT;
26159 
26160   END IF; -- IF l_mipr_own_ref_exists = 'Y'
26161   -- End: MIPR Own reference checks
26162 
26163   -- Start: MIPR Other reference checks
26164   IF l_mipr_others_ref_exists = 'Y' THEN
26165 
26166     d_progress := 110;
26167     -- Check 8: PR/PAR and MIPR-Others are being referenced in the same award (Warning)
26168     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_MIPR_OTHERS_AND_PR_REF');
26169 
26170     INSERT INTO po_online_report_text_gt(
26171                 online_report_id,
26172                 last_update_login,
26173                 last_updated_by,
26174                 last_update_date,
26175                 created_by,
26176                 creation_date,
26177                 SEQUENCE,
26178                 text_line,
26179                 message_name,
26180                 message_type)
26181     SELECT p_online_report_id,
26182            p_login_id,
26183            p_user_id,
26184            SYSDATE,
26185            p_user_id,
26186            SYSDATE,
26187            p_sequence +	ROWNUM,
26188            l_textline,
26189            'PO_MIPR_OTHERS_AND_PR_REF',
26190            'W'
26191     FROM   dual
26192     WHERE l_non_mipr_ref_exists = 'Y'; -- Non-Mipr reference exists
26193 
26194     p_sequence := p_sequence + SQL%ROWCOUNT;
26195 
26196     d_progress := 120;
26197     -- Check 9: MIPR-Others of different requesting agency are being referenced in the same award (Warning)
26198     l_textline := FND_MESSAGE.GET_STRING('PO', 'PO_MIPR_MULTI_REQ_OFF_REF');
26199 
26200     INSERT INTO po_online_report_text_gt(
26201                 online_report_id,
26202                 last_update_login,
26203                 last_updated_by,
26204                 last_update_date,
26205                 created_by,
26206                 creation_date,
26207                 SEQUENCE,
26208                 text_line,
26209                 message_name,
26210                 message_type)
26211     SELECT p_online_report_id,
26212            p_login_id,
26213            p_user_id,
26214            SYSDATE,
26215            p_user_id,
26216            SYSDATE,
26217            p_sequence +	ROWNUM,
26218            l_textline,
26219            'PO_MIPR_MULTI_REQ_OFF_REF',
26220            'W'
26221     FROM   dual
26222     WHERE  EXISTS(
26223       SELECT 1
26224       FROM   po_session_gt
26225       WHERE  key = l_procedure_id
26226       AND    char1 = 'MIPR_OTHERS'
26227       HAVING Count(DISTINCT num5) > 1);--clm_req_office of the referenced MIPR
26228 
26229     p_sequence := p_sequence + SQL%ROWCOUNT;
26230 
26231   END IF; -- IF l_mipr_others_ref_exists = 'Y'
26232   -- End: MIPR Other reference checks
26233 
26234   d_progress := 130;
26235   -- Delete the records from session table
26236   DELETE FROM po_session_gt
26237   WHERE key = l_procedure_id;
26238 
26239   x_return_status := FND_API.G_RET_STS_SUCCESS;
26240 
26241   IF (PO_LOG.d_proc) THEN
26242     PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
26243     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
26244     PO_LOG.proc_end(d_module);
26245   END IF;
26246 
26247 EXCEPTION
26248   WHEN FND_API.G_EXC_ERROR THEN
26249     x_return_status := FND_API.G_RET_STS_ERROR;
26250 
26251   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
26252     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
26253 
26254   WHEN OTHERS THEN
26255     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
26256 
26257     IF (PO_LOG.d_exc) THEN
26258       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
26259       PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
26260       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
26261       PO_LOG.proc_end(d_module);
26262     END IF;
26263 END check_po_mipr_reference;
26264 --CLM Phase 4 - MIPR Project End
26265 
26266 ---Bug 15843328
26267 PROCEDURE check_accrue_on_receipt(
26268                        P_DOCUMENT_ID		IN NUMBER,
26269 		       P_DOCUMENT_TYPE		IN VARCHAR2,
26270                        P_ONLINE_REPORT_ID	IN NUMBER,
26271                        P_USER_ID		IN NUMBER,
26272                        P_LOGIN_ID		IN NUMBER,
26273                        P_SEQUENCE		IN OUT NOCOPY NUMBER,
26274                        X_RETURN_STATUS		OUT NOCOPY VARCHAR2,
26275                        x_msg_data               OUT NOCOPY VARCHAR2
26276                        )IS
26277 
26278   L_TEXTLINE PO_ONLINE_REPORT_TEXT.TEXT_LINE%TYPE := fnd_message.Get_string( 'PO','PO_RI_INVALID_ACCRUE_ON_RCT' );
26279   L_API_NAME              CONSTANT VARCHAR2( 40 ) := 'check_accrue_on_receipt';
26280   L_PROGRESS              VARCHAR2( 3 );
26281 
26282 BEGIN
26283 
26284    L_PROGRESS := '001';
26285    IF G_DEBUG_STMT THEN
26286    PO_DEBUG.DEBUG_STMT( G_LOG_HEAD ||'.'||L_API_NAME||'.', L_PROGRESS, 'check_accrue_on_receipt' );
26287    END IF;
26288 
26289 IF ( P_DOCUMENT_TYPE = 'PO') THEN
26290 
26291  INSERT	INTO PO_ONLINE_REPORT_TEXT_GT(
26292 	   ONLINE_REPORT_ID,
26293 	   LAST_UPDATE_LOGIN,
26294 	   LAST_UPDATED_BY,
26295 	   LAST_UPDATE_DATE,
26296 	   CREATED_BY,
26297 	   CREATION_DATE,
26298 	   LINE_NUM,
26299 	   SHIPMENT_NUM,
26300 	   DISTRIBUTION_NUM,
26301 	   SEQUENCE,
26302 	   TEXT_LINE,
26303 	   MESSAGE_NAME
26304 	   )
26305    SELECT
26306 	   P_ONLINE_REPORT_ID,
26307 	   P_LOGIN_ID,
26308 	   P_USER_ID,
26309 	   SYSDATE,
26310 	   P_USER_ID,
26311 	   SYSDATE,
26312 	   POL.line_num,
26313 	   POLL.shipment_num,
26314 	   POD.DISTRIBUTION_NUM,
26315 	   P_SEQUENCE + ROWNUM,
26316 	   'Between Shipment '||POLL.shipment_num||
26317            ' and Distribution '||POD.DISTRIBUTION_NUM ||
26318            ' of Line '||POL.line_num||' , '|| L_TEXTLINE,
26319            'PO_RI_INVALID_ACCRUE_ON_RCT'
26320    FROM
26321 	   PO_DISTRIBUTIONS_GT POD,
26322            PO_LINE_LOCATIONS_GT POLL,
26323 	   PO_LINES_GT POL,
26324 	   PO_HEADERS_GT POH
26325 
26326    WHERE
26327 	   POH.po_header_id = P_DOCUMENT_ID
26328 	   AND POL.po_header_id = POH.po_header_id
26329 	   AND POD.line_location_id = POLL.line_location_id
26330 	   AND POLL.po_line_id = POL.po_line_id
26331 	   AND NVL( POLL.CANCEL_FLAG, 'N' ) <> 'Y'
26332 	   AND NVL( POLL.CLOSED_CODE, 'OPEN' ) <> 'FINALLY CLOSED'
26333            AND POD.destination_type_code in ('INVENTORY', 'SHOP FLOOR')
26334            AND Nvl(POD.accrue_on_receipt_flag, 'N') = 'N'
26335            AND Nvl(POLL.CONSIGNED_FLAG, 'N') = 'N'
26336            AND POLL.shipment_type  <> 'PREPAYMENT'
26337            AND POD.award_id IS NULL
26338 	   AND POH.PCARD_ID IS NULL;
26339 
26340 ELSIF ( P_DOCUMENT_TYPE = 'RELEASE') THEN
26341 
26342  INSERT	INTO PO_ONLINE_REPORT_TEXT_GT(
26343 	   ONLINE_REPORT_ID,
26344 	   LAST_UPDATE_LOGIN,
26345 	   LAST_UPDATED_BY,
26346 	   LAST_UPDATE_DATE,
26347 	   CREATED_BY,
26348 	   CREATION_DATE,
26349 	   LINE_NUM,
26350 	   SHIPMENT_NUM,
26351 	   DISTRIBUTION_NUM,
26352 	   SEQUENCE,
26353 	   TEXT_LINE,
26354 	   MESSAGE_NAME
26355 	   )
26356    SELECT
26357 	   P_ONLINE_REPORT_ID,
26358 	   P_LOGIN_ID,
26359 	   P_USER_ID,
26360 	   SYSDATE,
26361 	   P_USER_ID,
26362 	   SYSDATE,
26363 	   POL.line_num,
26364 	   POLL.shipment_num,
26365 	   POD.DISTRIBUTION_NUM,
26366 	   P_SEQUENCE + ROWNUM,
26367 	   'Between Shipment '||POLL.shipment_num||
26368            ' and Distribution '||POD.DISTRIBUTION_NUM ||
26369            ' of Line '||POL.line_num||' , '|| L_TEXTLINE,
26370            'PO_RI_INVALID_ACCRUE_ON_RCT'
26371    FROM
26372 	   PO_DISTRIBUTIONS_GT POD,
26373            PO_LINE_LOCATIONS_GT POLL,
26374 	   PO_LINES POL,
26375 	   PO_RELEASES_GT POR
26376 
26377    WHERE
26378            POR.po_release_id = P_DOCUMENT_ID
26379 	   AND POR.po_header_id = POL.po_header_id
26380 	   AND POD.line_location_id = POLL.line_location_id
26381 	   AND POLL.po_line_id = POL.po_line_id
26382 	   AND NVL( POLL.CANCEL_FLAG, 'N' ) <> 'Y'
26383 	   AND NVL( POLL.CLOSED_CODE, 'OPEN' ) <> 'FINALLY CLOSED'
26384            AND POD.destination_type_code in ('INVENTORY', 'SHOP FLOOR')
26385            AND Nvl(POD.accrue_on_receipt_flag, 'N') = 'N'
26386            AND Nvl(POLL.CONSIGNED_FLAG, 'N') = 'N'
26387            AND POLL.shipment_type  <> 'PREPAYMENT'
26388            AND POD.award_id IS NULL
26389 	   AND POR.PCARD_ID IS NULL;
26390 
26391  END IF;
26392 
26393    p_sequence := p_sequence + SQL%ROWCOUNT;
26394 
26395     X_RETURN_STATUS := FND_API.G_RET_STS_SUCCESS;
26396 
26397  EXCEPTION
26398 
26399  WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
26400      X_MSG_DATA := FND_MSG_PUB.GET( P_MSG_INDEX => FND_MSG_PUB.G_LAST, P_ENCODED => 'F' );
26401      X_RETURN_STATUS := FND_API.G_RET_STS_UNEXP_ERROR;
26402 
26403    WHEN FND_API.G_EXC_ERROR THEN
26404      X_MSG_DATA := FND_MSG_PUB.GET( P_MSG_INDEX => FND_MSG_PUB.G_LAST, P_ENCODED => 'F' );
26405      X_RETURN_STATUS := FND_API.G_RET_STS_ERROR;
26406 
26407    WHEN OTHERS THEN
26408 
26409      -- SQLStmtGetText(mycontext, l_dyn_string, size1, size2 );
26410      IF FND_MSG_PUB.CHECK_MSG_LEVEL( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR ) THEN
26411      FND_MSG_PUB.ADD_EXC_MSG( G_PKG_NAME, L_API_NAME );
26412      END IF;
26413 
26414      IF( G_DEBUG_UNEXP ) THEN
26415       IF( FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_UNEXPECTED ) THEN
26416        FND_LOG.STRING( FND_LOG.LEVEL_UNEXPECTED, G_LOG_HEAD ||
26417        L_API_NAME                                           ||
26418        '.OTHERS_EXCEPTION', 'EXCEPTION: LOCATION IS '       ||
26419        L_PROGRESS                                           ||
26420        ' SQL CODE IS '                                      ||
26421        SQLCODE );
26422       END IF;
26423      END IF;
26424 
26425      X_MSG_DATA := FND_MSG_PUB.GET( P_MSG_INDEX => FND_MSG_PUB.G_LAST, P_ENCODED => 'F' );
26426      X_RETURN_STATUS := FND_API.G_RET_STS_UNEXP_ERROR;
26427 
26428 END  check_accrue_on_receipt;
26429 
26430 -- SBCR Integration Changes Begin
26431 
26432 /**
26433 * Procedure: Check_sbcr_exists
26434 * Requires:
26435 *   IN PARAMETERS:
26436 *       document_id:      The po_header_id of submitted document
26437 *       document_type:    The type of the document
26438 *       document_version: Document Version
26439 * Returns:
26440 *  x_contract_exists: If SBCR Exists for this or not
26441 *  x_contract_status: Status of the SBCR document if exists
26442 *  x_is_draft_status: Y if the status is draft.
26443 */
26444 
26445 PROCEDURE Check_sbcr_exists(document_id       IN NUMBER,
26446                             document_type     IN VARCHAR2,
26447                             document_version  IN NUMBER,
26448                             document_number   IN VARCHAR2,
26449                             x_msg_data        OUT nocopy VARCHAR2,
26450                             x_msg_count       OUT nocopy NUMBER,
26451                             x_contract_exists OUT nocopy VARCHAR2,
26452                             x_contract_status OUT nocopy VARCHAR2,
26453                             x_is_draft_status OUT nocopy VARCHAR2)
26454 IS
26455   p_document_rec  okc_imp_record_types.document_rec_type;
26456   x_contract_rec  okc_imp_record_types.contract_rec_type;
26457   l_msg_data      VARCHAR2(100);
26458   l_msg_count     NUMBER;
26459   l_return_status VARCHAR2(1000);
26460 BEGIN
26461     x_contract_exists := 'N';
26462 
26463     x_contract_status := '';
26464 
26465     p_document_rec.business_document_type := document_type;
26466 
26467     p_document_rec.business_document_id := document_id;
26468 
26469     p_document_rec.business_document_version := document_version;
26470 
26471     p_document_rec.document_number := document_number;
26472 
26473     x_contract_rec.contract_type_txt := 'REP_SBCR';
26474 
26475     x_contract_rec.contract_id := NULL;
26476 
26477     x_contract_rec.contract_status_txt := NULL;
26478 
26479     okc_rep_contract_imp_pub.Check_contract_exists(p_api_version => '1.0',
26480     p_document_rec => p_document_rec, x_contract_rec => x_contract_rec,
26481     x_msg_data => l_msg_data, x_msg_count => l_msg_count,
26482     x_return_status => l_return_status);
26483 
26484     IF ( x_contract_rec.contract_id IS NULL )THEN
26485       x_contract_exists := 'N';
26486     ELSE
26487       x_contract_exists := 'Y';
26488       --x_contract_status := x_contract_rec.contract_status_code;
26489       SELECT meaning
26490       INTO
26491        x_contract_status
26492       FROM fnd_lookups
26493       WHERE lookup_type = 'OKC_REP_CONTRACT_STATUSES'
26494       AND LOOKUP_CODE = x_contract_rec.contract_status_code; -- bug 16713804
26495       IF x_contract_rec.contract_status_code = 'DRAFT' THEN
26496        x_is_draft_status :='Y';
26497       ELSE
26498        x_is_draft_status :='N';
26499       END IF;
26500     END IF;
26501 END check_sbcr_exists;
26502 
26503 /**
26504 * Private Procedure: check_po_sbcr_reference
26505 * Requires:
26506 *   IN PARAMETERS:
26507 *       p_document_id:      The po_header_id of submitted document
26508 *       p_draft_id:         The draft_id of submitted document
26509 *       p_online_report_id: Id used to INSERT INTO online_report_text table
26510 *       p_user_id:          User performing the action
26511 *       p_login_id:         Last update login_id
26512 *       p_org_id  :         current org id
26513 *   IN OUT PARAMETERS
26514 *       p_sequence:         Sequence number of last reported error
26515 * Modifies: Inserts error msgs in online_report_text_gt table, uses
26516 *           global_temp tables for processing
26517 * Effects: This procedure runs the document submission checks for CLM Awards
26518 *          referencing SBCR.
26519 *
26520 * Returns:
26521 *  p_sequence: This parameter contains the current count of number of error
26522 *              messages inserted
26523 */
26524 
26525 PROCEDURE check_po_sbcr_reference(p_document_id IN NUMBER,
26526                                   p_draft_id IN NUMBER,
26527                                   p_online_report_id IN NUMBER,
26528                                   p_user_id IN NUMBER,
26529                                   p_login_id IN NUMBER,
26530                                   p_org_id   IN NUMBER,
26531                                   p_sequence IN OUT NOCOPY NUMBER,
26532                                   x_return_status OUT NOCOPY VARCHAR2)
26533 IS
26534 l_api_name CONSTANT varchar2(40) := 'check_po_mipr_reference';
26535 d_module    VARCHAR2(70) := g_log_head||l_api_name;
26536 d_progress NUMBER;
26537 p_document_rec  okc_imp_record_types.document_rec_type;
26538 p_contract_rec  okc_imp_record_types.contract_rec_type;
26539 l_okc_doc_type VARCHAR2(25);
26540 l_changed_amt  NUMBER;
26541 l_doc_threshold_amount NUMBER;
26542 l_msg_data VARCHAR2(100);
26543 l_msg_count NUMBER;
26544 l_return_status VARCHAR2(1000);
26545 l_limit_amount VARCHAR2(100);
26546 l_user_currency VARCHAR2(10);
26547 
26548 
26549 
26550 BEGIN
26551 
26552   d_progress := 0;
26553 
26554 
26555   IF (PO_LOG.d_proc) THEN
26556     PO_LOG.proc_begin(d_module);
26557     PO_LOG.proc_begin(d_module, 'p_document_id', p_document_id);
26558     PO_LOG.proc_begin(d_module, 'p_draft_id', p_draft_id);
26559 
26560   END IF;
26561  IF Nvl(p_draft_id,-1) <> -1 THEN
26562     l_doc_threshold_amount := Nvl(po_core_s.Retrieveoptionvalue(p_org_id,
26563                                                                 po_core_s.g_DOC_AMT_THRESHOLD_COL), 0);
26564 
26565     SELECT ( CASE
26566            WHEN phda.type_lookup_code = 'BLANKET' THEN 'PA_BLANKET_MOD'
26567            WHEN phda.type_lookup_code = 'CONTRACT' THEN 'PA_CONTRACT_MOD'
26568            ELSE 'PO_STANDARD_MOD'
26569          END ) OKC_DOC_TYPE,
26570          (CASE WHEN phda.type_lookup_code = 'BLANKET' OR phda.type_lookup_code = 'CONTRACT' THEN
26571            Nvl(phda.amount_limit,0) - Nvl(phd.amount_limit,0)
26572          ELSE
26573           Nvl(pod.changed_amt,0)
26574          END) changed_amt -- Bug 16713804
26575     INTO  l_okc_doc_type,l_changed_amt
26576     FROM   po_headers_draft_all phda, po_headers_all phd,
26577        po_drafts pod
26578     WHERE  phda.po_header_id = p_document_id
26579        AND phd.po_header_id = phda.po_header_id
26580        AND phda.draft_id = p_draft_id
26581        AND pod.draft_id = phda.draft_id;
26582 
26583 
26584     p_document_rec.business_document_type := l_okc_doc_type;
26585     p_document_rec.business_document_id := p_draft_id;
26586     p_contract_rec.contract_type_txt := 'REP_SBCR';
26587     p_contract_rec.contract_id := null;
26588     p_contract_rec.contract_status_code := null;
26589     OKC_REP_CONTRACT_IMP_PUB.check_contract_exists(p_api_version => '1.0',
26590                                                    p_document_rec => p_document_rec,
26591                                                    x_contract_rec => p_contract_rec,
26592                                                    x_msg_data => l_msg_data,
26593                                                    x_msg_count => l_msg_count,
26594                                                    x_return_status => l_return_status);
26595 
26596 
26597    IF ( (l_changed_amt > l_doc_threshold_amount AND l_doc_threshold_amount > 0 )AND
26598      (p_contract_rec.contract_id is NULL)
26599      )THEN
26600       l_user_currency := FND_PROFILE.VALUE_SPECIFIC('ICX_PREFERRED_CURRENCY', p_user_id);
26601       l_limit_amount :=  TO_CHAR(l_doc_threshold_amount, FND_CURRENCY.GET_FORMAT_MASK(l_user_currency ,60)) || ' ' || l_user_currency;
26602       INSERT INTO po_online_report_text_gt
26603         (online_report_id
26604         ,last_updated_by
26605         ,last_update_date
26606         ,created_by
26607         ,creation_date
26608         ,line_num
26609         ,shipment_num
26610         ,distribution_num
26611         ,sequence
26612         ,text_line
26613         ,message_name
26614         ,message_type
26615         )
26616       VALUES
26617         (p_online_report_id
26618         ,p_login_id
26619         ,sysdate
26620         ,p_user_id
26621         ,sysdate
26622         ,0 ,0 ,0
26623         ,p_sequence+1
26624         ,po_core_s.Get_translated_text('PO_OKC_NO_SBCR', 'AMOUNT',l_limit_amount)
26625         ,'PO_OKC_NO_SBCR'
26626         ,'W'
26627         );
26628 
26629 
26630    ELSIF( p_contract_rec.contract_id IS NOT NULL
26631       AND Nvl(p_contract_rec.contract_status_code,'X') <> 'SIGNED')THEN
26632 
26633       INSERT INTO po_online_report_text_gt
26634         (online_report_id
26635         ,last_updated_by
26636         ,last_update_date
26637         ,created_by
26638         ,creation_date
26639         ,line_num
26640         ,shipment_num
26641         ,distribution_num
26642         ,sequence
26643         ,text_line
26644         ,message_name
26645         ,message_type
26646         )
26647       VALUES
26648         (p_online_report_id
26649         ,p_login_id
26650         ,sysdate
26651         ,p_user_id
26652         ,sysdate
26653         ,0 ,0 ,0
26654         ,p_sequence+1
26655         ,po_core_s.Get_translated_text('PO_OKC_NO_SIGNED_SBCR')
26656         ,'PO_OKC_NO_SIGNED_SBCR'
26657         ,'W'
26658         );
26659 
26660 
26661      END IF;
26662 
26663   END IF;
26664 
26665   x_return_status := FND_API.G_RET_STS_SUCCESS;
26666 
26667   IF (PO_LOG.d_proc) THEN
26668     PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
26669     PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
26670     PO_LOG.proc_end(d_module);
26671   END IF;
26672 
26673 EXCEPTION
26674   WHEN FND_API.G_EXC_ERROR THEN
26675     x_return_status := FND_API.G_RET_STS_ERROR;
26676 
26677   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
26678     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
26679 
26680   WHEN OTHERS THEN
26681     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
26682 
26683     IF (PO_LOG.d_exc) THEN
26684       PO_LOG.exc(d_module, d_progress, SQLCODE || ': ' || SQLERRM);
26685       PO_LOG.proc_end(d_module, 'p_sequence', p_sequence);
26686       PO_LOG.proc_end(d_module, 'x_return_status', x_return_status);
26687       PO_LOG.proc_end(d_module);
26688     END IF;
26689 END check_po_sbcr_reference;
26690 
26691 -- SBCR Integration Changes End.
26692 
26693 
26694 END PO_DOCUMENT_CHECKS_PVT;
26695